-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathGoogleClient.php
206 lines (178 loc) · 6.7 KB
/
GoogleClient.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
require 'vendor/autoload.php';
class GoogleClient
{
protected $client_id = '<CLIENT_ID>';
protected $client_secret = '<CLIENT_SECRET>';
protected $redirect_uri = '<REDIRECTED_URI>';
protected $scopes = array('https://www.googleapis.com/auth/drive',);
protected $client;
protected $service;
/**
* Construct an easy to use Google API client.
*/
public function __construct()
{
$this->client = new \Google_Client();
$this->client->setClientId($this->client_id);
$this->client->setClientSecret($this->client_secret);
$this->client->setRedirectUri($this->redirect_uri);
$this->client->setAccessType('offline');
$this->client->setScopes($this->scopes);
if (isset($_SESSION['GOOGLE_ACCESS_TOKEN'])) {
$this->client->setAccessToken($_SESSION['GOOGLE_ACCESS_TOKEN']);
// Checking current access token is expired
if($this->client->isAccessTokenExpired()){
// Refresh access token and add it into session
$client->refreshToken($_SESSION['GOOGLE_REFRESH_TOKEN']);
$access_token = $this->client->getAccessToken();
$_SESSION['GOOGLE_ACCESS_TOKEN'] = $access_token;
}
}
}
/**
* Check if the user is logged in or not
*/
public function isLoggedIn()
{
if (isset($_SESSION['GOOGLE_ACCESS_TOKEN'])) {
return true;
} else {
return false;
}
}
/**
* Authenticate the client and set access token and refresh after login
* @param string $code redirected code
*/
public function authenticate($code)
{
$this->client->authenticate($code);
$_SESSION['GOOGLE_ACCESS_TOKEN'] = $this->client->getAccessToken();
$_SESSION['GOOGLE_REFRESH_TOKEN'] = $this->client->getRefreshToken();
}
/**
* To set access token explicitely
* @param string $accessToken access token
*/
public function setAccessToken($accessToken)
{
$this->client->setAccessToken($accessToken);
}
/**
* To get authentication URL if not in session
* @return string
*/
public function getAuthUrl()
{
return $this->client->createAuthUrl();
}
/**
* Returns the google client object
* @return Google_Client
*/
public function getClient()
{
return $this->client;
}
/**
* Initilize drive services object
*/
public function initDriveService()
{
$this->service = new \Google_Service_Drive($this->client);
}
/**
* Create folder at google drive
* @param string $parentId parent folder id or root where folder will be created
* @param string $folderName folder name to create
* @return string id of created folder
*/
public function createFolder($parentId, $folderName)
{
// Setting File Matadata
$fileMetadata = new \Google_Service_Drive_DriveFile(array(
'name' => $folderName,
'parents' => array($parentId),
'mimeType' => 'application/vnd.google-apps.folder'));
// Creating Folder with given Matadata and asking for ID field as result
$file = $this->service->files->create($fileMetadata, array('fields' => 'id'));
return $file->id;
}
/**
* Get the list of files or folders or both from given folder or root
* @param string $search complete or partial name of file or folder to search
* @param string $parentId parent folder id or root from which the list of
files or folders or both will be generated
* @param string $type='all' file or folder
* @return array list of files or folders or both from given parent directory
*/
public function listFilesFolders($search, $parentId, $type = 'all')
{
$query = '';
// Checking if search is empty the use 'contains' condition if search is empty (to get all files or folders).
// Otherwise use '=' condition
$condition = $search!=''?'=':'contains';
// Search all files and folders otherwise search in root or any folder
$query .= $parentId!='all'?"'".$parentId."' in parents":"";
// Check if want to search files or folders or both
switch ($type) {
case "files":
$query .= $query!=''?' and ':'';
$query .= "mimeType != 'application/vnd.google-apps.folder'
and name ".$condition." '".$search."'";
break;
case "folders":
$query .= $query!=''?' and ':'';
$query .= "mimeType = 'application/vnd.google-apps.folder' and name contains '".$search."'";
break;
default:
$query .= "";
break;
}
// Make sure that not list trashed files
$query .= $query!=''?' and trashed = false':'trashed = false';
$optParams = array('q' => $query,'pageSize' => 1000);
// Returns the list of files and folders as object
$results = $this->service->files->listFiles($optParams);
// Return false if nothing is found
if (count($results->getFiles()) == 0) {
return array();
}
// Converting array to object
$result = array();
foreach ($results->getFiles() as $file) {
$result[$file->getId()] = $file->getName();
}
return $result;
}
/**
* Upload file to given folder
* @param string $parentId parent folder id or root where folder will be upload
* @param string $filePath file local path of file which will be upload
* @param string $fileName file name of the uploaded copy at google drive
* @return string id of uploaded file
*/
public function uploadFile($parentId, $filePath, $fileName = "none")
{
// If file name is 'none' then give the orignal file name
if ($fileName=="none") {
$fileName = end(explode('/', $filePath));
}
// Creating file matadata
$fileMetadata = new \Google_Service_Drive_DriveFile(array(
'name' => $fileName,
'parents' => array($parentId)
));
// Getting file into variable
$content = file_get_contents($filePath);
// Uploading file and getting uploaded file ID as result
$file = $this->service->files->create($fileMetadata, array(
'data' => $content,
'mimeType' => 'image/jpeg',
'uploadType' => 'multipart',
'fields' => 'id'));
// Returning file id of newly uploaded file
return $file->id;
}
}