-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrive_client_classdef.php
More file actions
executable file
·385 lines (355 loc) · 11.6 KB
/
drive_client_classdef.php
File metadata and controls
executable file
·385 lines (355 loc) · 11.6 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
namespace Drive;
require_once "db.php";
require_once "dbTransactions.php";
require_once "Log.php";
abstract class Client
{
private $file_name;
private $bookid;
private $vendor_name;
private $action;
protected $token;
protected function __construct()
{
$this->vendor_name = $this->getDriveVendorName();
if($this->vendor_name==null || strlen($this->vendor_name)==0) {
\Logs\logWarning('Invalid Vendor code');
}
if(!isset($_SESSION[$this->vendor_name]) || !is_array($_SESSION[$this->vendor_name])) {
$_SESSION[$this->vendor_name]=array();
}
if(isset($_GET['action'])) {
$_SESSION[$this->vendor_name]['action']=$_GET['action'];
}
if(isset($_GET['bookid'])) {
$_SESSION[$this->vendor_name]['current_bookid']=$_GET['bookid'];
}
if(isset($_GET['file_name'])) {
$_SESSION[$this->vendor_name]['current_filename'] = $_GET['file_name'];
}
if(isset($_SESSION[$this->vendor_name]['current_filename'])) {
$this->setFileName($_SESSION[$this->vendor_name]['current_filename']);
}
if(isset($_SESSION[$this->vendor_name]['current_bookid'])) {
$this->bookid=$_SESSION[$this->vendor_name]['current_bookid'];
}
if(isset($_SESSION[$this->vendor_name]['action'])) {
$this->action=$_SESSION[$this->vendor_name]['action'];
}
$this->initFromDb();
}
public function setFileName($file_name)
{
$this->file_name = $file_name;
}
public function getFileName()
{
return $this->file_name;
}
public function getBookId()
{
return $this->bookid;
}
public function setBookId($bid)
{
$this->bookid=$bid;
}
public function isLogged()
{
return ($this->getAccessToken() && !$this->isExpired());
}
public function getAccessToken()
{
//\Logs\logDebug(var_export($this,true));
if($this->token && $this->token->access_token)
{
return $this->token->access_token;
}
return null;
}
public function useDownloadProxy()
{
return false;
}
protected function reparent()
{
throw new \Exception('Reparent not implemented for store ' . $this->getDriveVendorName());
}
protected abstract function uploadFile();
protected abstract function downloadFile();
protected abstract function deleteFile();
protected abstract function getDriveVendorName();
protected abstract function getRedirectUrl();
protected abstract function isExpired();
protected abstract function onCode($code);
protected abstract function refreshToken();
protected abstract function store_info();
protected abstract function downloadLink($fileid);
public function execute()
{
try
{
if($this->login()) {
$this->doAction();
}
}
catch(\Exception $e)
{
$ret=\Logs\logException($e);
if($this->action && $this->action==='login') {
$html_response=sprintf(
"<html><body> <script type='text/javascript'> window.opener.postMessage('{\"error\" : \"' + %s + '\" }',window.location.origin);
window.close(); </script> </body></html>", $e->getMessage()
);
echo $html_response;
return;
}
if($ret && $ret>0) {
header('Location: home.php?errid=' . $ret);
}
}
}
public function login()
{
if($this->getAccessToken()) {
if($this->isExpired()) {
$this->refreshToken();
}
return true;
}
if(isset($_GET['code'])) {
$code = $_GET['code'];
$this->onCode($code);
return ($this->getAccessToken()!=null);
}
$url=$this->getRedirectUrl();
header('Location: '. $url);
}
public function doAction()
{
switch($this->action)
{
case 'upload':
{
$this->uploadFile();
$dbt=\Database\getTransaction();
if(is_a($dbt, '\Database\Transactions\CreateBook')) {
$dbt->commit();
$this->setBookId($dbt->bookid);
}
else
{
throw new \Exception('Transaction not \'\Database\Transactions\CreateBook\' type.');
}
\Database\removeTransaction();
unlink('temp/'.$dbt->filename);
header('Location: home.php?bookid=' . $this->getBookId());
}
break;
case 'download':
{
$this->downloadFile();
header('Location: home.php?bookid=' . $this->getBookId());
}
break;
case 'delete':
{
$this->deleteFile();
$dbt=\Database\getTransaction();
if(is_a($dbt, '\Database\Transactions\DeleteBook')) {
$dbt->commit();
}
else
{
throw new \Exception('Transaction not \'\Database\Transactions\DeleteBook\' type.');
}
\Database\removeTransaction();
header('Location: home.php');
}
break;
case 'login':
header('Location: store.php?action=login&store_code='. $this->getDriveVendorName().'&html=true');
break;
case 'downloadLink':
header('Location: store.php?action=downloadLink&bookid='. $this->getBookId().'&html=true');
break;
case 'reparent':
$this->reparent();
header('Location: logview.php');
break;
default:
throw new \Exception('Unknown action: ' . $this->action);
}
}
protected function getSessionVar($var_name)
{
if(isset($_SESSION[$this->vendor_name][$var_name])) {
return $_SESSION[$this->vendor_name][$var_name];
}
return null;
}
protected function setSessionVar($var_name,$value)
{
$_SESSION[$this->vendor_name][$var_name]=$value;
}
protected function unsetSessionVar($var_name)
{
unset($_SESSION[$this->vendor_name][$var_name]);
}
protected function curl_request($url, $options = array())
{
\Logs\logInfo('curl http request url=' . '"' . $url . '"\n' . var_export($options,true));
$curl = curl_init();
$whole_options=array(
// General options.
CURLOPT_RETURNTRANSFER => true,
CURLOPT_AUTOREFERER => true,
// SSL options.
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_URL => $url
);
if($options) {
$whole_options=$whole_options + $options;
}
curl_setopt_array($curl, $whole_options);
$result = curl_exec($curl);
if ($result===false) {
curl_close($curl);
throw new \Exception('curl_exec() failed: ' . curl_error($curl));
}
\Logs\logInfo('curl http response code=' . curl_getinfo($curl, CURLINFO_HTTP_CODE));
\Logs\logInfo('curl http response: ' . $result);
curl_close($curl);
return $result;
}
protected function curl_post($url,$body,$options=array())
{
$post_options=array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $body
);
if($options) {
$post_options=$options+$post_options;
}
return $this->curl_request($url, $post_options);
}
private function initFromDb()
{
$data=$this->loadLoginFromDb();
if($data) {
$this->token=json_decode($data);
}
}
protected function set_token($obj)
{
if(is_string($obj)) {
$obj=json_decode($obj);
}
$this->token=$obj;
if($this->token && !property_exists($this->token, 'created')) {
$this->token->{'created'}=time();
}
if($this->token && property_exists($this->token, 'access_token')) {
$this->saveLoginToDb(json_encode($this->token));
}
else
{
$this->saveLoginToDb(null);
}
}
protected function register_link($file_id,$size)
{
$dbt = \Database\getTransaction();
if(is_a($dbt, '\Database\Transactions\CreateBook')) {
$dbt->file_id=$file_id;
$dbt->file_size=$size;
}
else
{
throw new \Exception('Cannot register file id: bad type');
}
}
protected function getStoreFileId()
{
$store_fileid=null;
if($this->bookid) {
$dbase = \Database\odbc()->connect();
if($dbase) {
$sql=sprintf('SELECT FILE_ID FROM BOOKS_LINKS WHERE BOOK_ID=%s AND STORE_ID IN (SELECT ID FROM FILE_STORE WHERE VENDOR_CODE=\'%s\')', $this->getBookId(), $this->getDriveVendorName());
$rec=$dbase->query($sql);
if($rec->next()) {
$store_fileid=$rec->field_value('FILE_ID');
}
$dbase->close();
}
}
return $store_fileid;
}
protected function loadLoginFromDb()
{
$log_info=null;
$dbase=\Database\odbc()->connect();
if($dbase) {
$sql=sprintf('SELECT LOGIN_INFO FROM FILE_STORE WHERE VENDOR_CODE=\'%s\'', $this->getDriveVendorName());
$rec=$dbase->query($sql);
if($rec->next()) {
$log_info=$rec->field_value('LOGIN_INFO');
}
$dbase->close();
}
return $log_info;
}
protected function saveLoginToDb($log_info=null)
{
$dbase=\Database\odbc()->connect();
if($dbase) {
$sql='UPDATE FILE_STORE SET LOGIN_INFO=';
if($log_info) {
$sql .= sprintf('\'%s\'', $log_info);
}
else
{
$sql .= 'NULL';
}
$sql .= sprintf(' WHERE VENDOR_CODE=\'%s\'', $this->getDriveVendorName());
$dbase->query($sql);
$dbase->close();
}
else{
\Logs\logWarning('cannot connect to database: token not saved');
}
}
protected function downloadToBrowser($tempfile,$content)
{
file_put_contents($tempfile, $content);
$dbase=\Database\odbc()->connect();
if($dbase) {
$sql='SELECT FILE_SIZE,FILE_NAME FROM BOOKS_LINKS WHERE BOOK_ID=' . $this->getBookId();
$rec=$dbase->query($sql);
if($rec && $rec->next()) {
$filename = $rec->field_value('FILE_NAME');
$filesize = filesize($tempfile);
$dbase->close();
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $filesize);
header('Content-Type: application/octet-stream');
readfile($tempfile);
unlink($tempfile);
}
else
{
$dbase->close();
throw new \Exception('cannot get file name and size from database');
}
}
else
{
throw new \Exception('cannot connect to database');
}
}
}
?>