-
Notifications
You must be signed in to change notification settings - Fork 1
/
drive.php
137 lines (121 loc) · 4.16 KB
/
drive.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__.'/chatfuel.php';
require_once __DIR__ . '/config.php';
$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "http" : "https") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$temp = array();
$temp = explode("?",$url);
$url = $temp[0];
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
define('SCOPES', implode(' ', array(
Google_Service_Drive::DRIVE_METADATA_READONLY)
));
function getClient() {
$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);
$client->setAuthConfig(CLIENT_SECRET_PATH);
$client->setAccessType('offline');
// Load previously authorized credentials from a file.
$credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
if (file_exists($credentialsPath)) {
$accessToken = json_decode(file_get_contents($credentialsPath), true);
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser: <a href='%s' target='_blank'>Click</a></br>", $authUrl);
print "Enter verification code: <form> <input type='text' name='authcode' /> <input type='submit' value='Send'/></form>";
if (isset($_GET['authcode']))
{
$authCode = $_GET['authcode'];
echo "<script type='text/javascript'>
// Load one times
(function()
{
if( window.localStorage )
{
if( !localStorage.getItem('firstLoad') )
{
localStorage['firstLoad'] = true;
window.location.reload();
}
else
localStorage.removeItem('firstLoad');
}
})();
</script>";
}
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
// Store the credentials to disk.
if(!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, json_encode($accessToken));
printf("Credentials saved to %s\n", $credentialsPath);
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}
return $client;
}
/**
* Expands the home directory alias '~' to the full path.
* @param string $path the path to expand.
* @return string the expanded path.
*/
function expandHomeDirectory($path) {
$homeDirectory = getenv('HOME');
if (empty($homeDirectory)) {
$homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH');
}
return str_replace('~', realpath($homeDirectory), $path);
}
// Get the API client and construct the service object.
// Main code ^^
$client = getClient();
$service = new Google_Service_Drive($client);
$query = $q." name contains "."'". $_GET['q'] ."'";
// echo $query;
// Print the names and IDs for up to 40 files.
$optParams = array(
'pageSize' => 40,
'fields' => 'nextPageToken, files(id, name)',
'orderBy' => 'folder',
'q' => $query
);
$results = $service->files->listFiles($optParams);
$data = array();
$i = 0;
if (count($results->getFiles()) == 0) {
sendText("NO FILE FOUND!");
} else {
foreach ($results->getFiles() as $file) {
$check = array();
$data[$i]['name'] = $file->getName();
$check = explode(".",$file->getId());
if (count($check) == 1) {
$data[$i]['link'] = "https://drive.google.com/open?id=".$file->getId();
$i++;
}
if (count($check) == 2){
$data[$i]['link'] = "https://drive.google.com/drive/folders/".$file->getId();
$i++;
}
}
};
if (isset($_GET['id'])){
$id = (int)$_GET['id'];
}else $id = 0;
if ($id >= count($data)){
if(isset($_GET['id']))
sendText("Hết rùi. Đừng bấm nữa @@");
}else {
sendButton("Tên: ".$data[$id]['name'], $data[$id]['link'], $url."?q=".$_GET['q']. "&id=".++$id);
}