-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathclass.file.php
629 lines (550 loc) · 17.6 KB
/
class.file.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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
<?php
/**
* main file class
*/
class file
{
public $errorMsg = null;
function __construct()
{
$this->errorMsg = null;
}
public function download()
{
// remove session
if (isset($_SESSION['showDownload']))
{
// reset session variable for next time
$_SESSION['showDownload'] = null;
unset($_SESSION['showDownload']);
session_write_close();
}
// php script timeout for long downloads (2 days!)
set_time_limit(60 * 60 * 24 * 2);
// load the server the file is on
$storageType = 'local';
$storageLocation = _CONFIG_FILE_STORAGE_PATH;
$uploadServerDetails = $this->loadServer();
if ($uploadServerDetails != false)
{
$storageLocation = $uploadServerDetails['storagePath'];
$storageType = $uploadServerDetails['serverType'];
// if no storage path set & local, use system default
if ((strlen($storageLocation) == 0) && ($storageType == 'local'))
{
$storageLocation = _CONFIG_FILE_STORAGE_PATH;
}
}
// get file path
$fullPath = $this->getFullFilePath($storageLocation);
// open file - via ftp
if ($storageType == 'remote')
{
// connect via ftp
$conn_id = ftp_connect($uploadServerDetails['ipAddress'], $uploadServerDetails['ftpPort'], 30);
if ($conn_id === false)
{
$this->errorMsg = 'Could not connect to ' . $uploadServerDetails['ipAddress'] . ' to upload file.';
return false;
}
// authenticate
$login_result = ftp_login($conn_id, $uploadServerDetails['ftpUsername'], $uploadServerDetails['ftpPassword']);
if ($login_result === false)
{
$this->errorMsg = 'Could not login to ' . $uploadServerDetails['ipAddress'] . ' with supplied credentials.';
return false;
}
// prepare the stream of data
$pipes = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
if ($pipes === false)
{
$this->errorMsg = 'Could not create stream to download file on ' . $uploadServerDetails['ipAddress'];
return false;
}
stream_set_write_buffer($pipes[0], 10000);
stream_set_timeout($pipes[1], 10);
stream_set_blocking($pipes[1], 0);
$fail = false;
$ret = ftp_nb_fget($conn_id, $pipes[0], $fullPath, FTP_BINARY, FTP_AUTORESUME);
}
// open file - locally
else
{
$handle = @fopen($fullPath, "r");
if (!$handle)
{
$this->errorMsg = 'Could not open file for reading.';
return false;
}
}
// download speed
$speed = 0;
// if free/non user
$Auth = Auth::getAuth();
if (($Auth->loggedIn == false) || ($Auth->level == 'free user'))
{
$speed = (int) SITE_CONFIG_FREE_USER_MAX_DOWNLOAD_SPEED;
}
else
{
$speed = (int) SITE_CONFIG_PREMIUM_USER_MAX_DOWNLOAD_SPEED;
}
// do we need to throttle the speed?
if ($speed > 0)
{
// create new throttle config
$config = new ThrottleConfig();
// set standard transfer rate (in bytes/second)
$config->burstLimit = $speed;
$config->rateLimit = $speed;
// enable module (this is a default value)
$config->enabled = true;
// start throttling
$x = new Throttle($config);
}
// output some headers
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-type: " . $this->fileType);
header("Pragma: public");
header("Content-Disposition: attachment; filename=\"" . str_replace("\"", "", $this->originalFilename) . "\"");
header("Content-Description: File Transfer");
header("Content-Length: " . $this->fileSize);
// output file - via ftp
if ($storageType == 'remote')
{
while ($ret == FTP_MOREDATA)
{
$contents = stream_get_contents($pipes[1]);
if ($contents !== false)
{
echo $contents;
flush();
}
$ret = ftp_nb_continue($conn_id);
}
/*
$contents = stream_get_contents($pipes[1]);
if($contents !== false)
{
echo $contents;
flush();
}
*/
fclose($pipes[0]);
fclose($pipes[1]);
}
// output file - local
else
{
while (($buffer = fgets($handle, 4096)) !== false)
{
echo $buffer;
}
fclose($handle);
}
exit();
}
public function loadServer()
{
// load the server the file is on
if ((int) $this->serverId)
{
// load from the db
$db = Database::getDatabase(true);
$uploadServerDetails = $db->getRow('SELECT * FROM file_server WHERE id = ' . $db->quote((int) $this->serverId));
$db->close();
if (!$uploadServerDetails)
{
return false;
}
return $uploadServerDetails;
}
return false;
}
public function getFullFilePath($prePath = '')
{
if (substr($prePath, strlen($prePath) - 1, 1) == '/')
{
$prePath = substr($prePath, 0, strlen($prePath) - 1);
}
return $prePath . '/' . $this->localFilePath;
}
/**
* Get full short url path
*
* @return string
*/
public function getFullShortUrl()
{
if (SITE_CONFIG_FILE_URL_SHOW_FILENAME == 'yes')
{
return $this->getFullLongUrl();
}
return _CONFIG_SITE_PROTOCOL . '://' . _CONFIG_SITE_FILE_DOMAIN . '/' . $this->shortUrl;
}
public function getStatisticsUrl()
{
return _CONFIG_SITE_PROTOCOL . '://' . _CONFIG_SITE_FILE_DOMAIN . '/' . $this->shortUrl . '~s';
}
public function getDeleteUrl()
{
return _CONFIG_SITE_PROTOCOL . '://' . _CONFIG_SITE_FILE_DOMAIN . '/' . $this->shortUrl . '~d?' . $this->deleteHash;
}
public function getInfoUrl()
{
return _CONFIG_SITE_PROTOCOL . '://' . _CONFIG_SITE_FILE_DOMAIN . '/' . $this->shortUrl . '~i?' . $this->deleteHash;
}
public function getShortInfoUrl()
{
return _CONFIG_SITE_PROTOCOL . '://' . _CONFIG_SITE_FILE_DOMAIN . '/' . $this->shortUrl . '~i';
}
/**
* Get full long url including the original filename
*
* @return string
*/
public function getFullLongUrl()
{
return _CONFIG_SITE_PROTOCOL . '://' . _CONFIG_SITE_FILE_DOMAIN . '/' . $this->shortUrl . '/' . slugify($this->originalFilename);
}
/**
* Method to increment visitors
*/
public function updateVisitors()
{
$db = Database::getDatabase(true);
$this->visits++;
$db->query('UPDATE file SET visits = :visits WHERE id = :id', array('visits' => $this->visits, 'id' => $this->id));
}
/**
* Method to update last accessed
*/
public function updateLastAccessed()
{
$db = Database::getDatabase(true);
$db->query('UPDATE file SET lastAccessed = NOW() WHERE id = :id', array('id' => $this->id));
}
/**
* Method to set folder
*/
public function updateFolder($folderId = '')
{
$db = Database::getDatabase(true);
$folderId = (int)$folderId;
if($folderId == 0)
{
$folderId = '';
}
$db->query('UPDATE file SET folderId = :folderId WHERE id = :id', array('folderId' => $folderId, 'id' => $this->id));
}
/**
* Remove by user
*/
public function removeByUser()
{
// load the server the file is on
$storageType = 'local';
$storageLocation = _CONFIG_FILE_STORAGE_PATH;
$uploadServerDetails = $this->loadServer();
if ($uploadServerDetails != false)
{
$storageLocation = $uploadServerDetails['storagePath'];
$storageType = $uploadServerDetails['serverType'];
if ((strlen($uploadServerDetails['storagePath']) == 0) && ($storageType == 'local'))
{
$storageLocation = _CONFIG_FILE_STORAGE_PATH;
}
}
// file path
$filePath = $this->getFullFilePath($storageLocation);
// remote - ftp
if ($storageType == 'remote')
{
// connect via ftp
$conn_id = ftp_connect($uploadServerDetails['ipAddress'], $uploadServerDetails['ftpPort'], 30);
if ($conn_id === false)
{
$this->errorMsg = 'Could not connect to ' . $uploadServerDetails['ipAddress'] . ' to upload file.';
return false;
}
// authenticate
$login_result = ftp_login($conn_id, $uploadServerDetails['ftpUsername'], $uploadServerDetails['ftpPassword']);
if ($login_result === false)
{
$this->errorMsg = 'Could not login to ' . $uploadServerDetails['ipAddress'] . ' with supplied credentials.';
return false;
}
// remove file
if (!ftp_delete($conn_id, $filePath))
{
$this->errorMsg = 'Could not remove file on ' . $uploadServerDetails['ipAddress'];
return false;
}
}
// local
else
{
// delete file from server
unlink($filePath);
}
// update db
$db = Database::getDatabase(true);
$db->query('UPDATE file SET statusId = 2 WHERE id = :id', array('id' => $this->id));
}
/**
* Remove by system
*/
public function removeBySystem()
{
// load the server the file is on
$storageType = 'local';
$storageLocation = _CONFIG_FILE_STORAGE_PATH;
$uploadServerDetails = $this->loadServer();
if ($uploadServerDetails != false)
{
$storageLocation = $uploadServerDetails['storagePath'];
$storageType = $uploadServerDetails['serverType'];
if ((strlen($uploadServerDetails['storagePath']) == 0) && ($storageType == 'local'))
{
$storageLocation = _CONFIG_FILE_STORAGE_PATH;
}
}
// file path
$filePath = $this->getFullFilePath($storageLocation);
// remote - ftp
if ($storageType == 'remote')
{
// connect via ftp
$conn_id = ftp_connect($uploadServerDetails['ipAddress'], $uploadServerDetails['ftpPort'], 30);
if ($conn_id === false)
{
$this->errorMsg = 'Could not connect to ' . $uploadServerDetails['ipAddress'] . ' to upload file.';
return false;
}
// authenticate
$login_result = ftp_login($conn_id, $uploadServerDetails['ftpUsername'], $uploadServerDetails['ftpPassword']);
if ($login_result === false)
{
$this->errorMsg = 'Could not login to ' . $uploadServerDetails['ipAddress'] . ' with supplied credentials.';
return false;
}
// remove file
if (!ftp_delete($conn_id, $filePath))
{
$this->errorMsg = 'Could not remove file on ' . $uploadServerDetails['ipAddress'];
return false;
}
}
// local
else
{
// delete file from server
unlink($filePath);
}
// update db
$db = Database::getDatabase(true);
$db->query('UPDATE file SET statusId = 5 WHERE id = :id', array('id' => $this->id));
}
public function getLargeIconPath()
{
$fileTypePath = DOC_ROOT . '/themes/' . SITE_CONFIG_SITE_THEME . '/images/file_icons/512px/' . $this->extension . '.png';
if (!file_exists($fileTypePath))
{
return false;
}
return SITE_IMAGE_PATH . '/file_icons/512px/' . $this->extension . '.png';
}
public function getFilenameExcExtension()
{
$filename = $this->originalFilename;
return basename($filename, '.' . $this->extension);
}
/**
* Load by short url
*
* @param string $shortUrl
* @return file
*/
static function loadByShortUrl($shortUrl)
{
$db = Database::getDatabase(true);
$row = $db->getRow('SELECT * FROM file WHERE shortUrl = ' . $db->quote($shortUrl));
if (!is_array($row))
{
return false;
}
$fileObj = new file();
foreach ($row AS $k => $v)
{
$fileObj->$k = $v;
}
return $fileObj;
}
/**
* Load by delete hash
*
* @param string $deleteHash
* @return file
*/
static function loadByDeleteHash($deleteHash)
{
$db = Database::getDatabase(true);
$row = $db->getRow('SELECT * FROM file WHERE deleteHash = ' . $db->quote($deleteHash));
if (!is_array($row))
{
return false;
}
$fileObj = new file();
foreach ($row AS $k => $v)
{
$fileObj->$k = $v;
}
return $fileObj;
}
/**
* Load by id
*
* @param integer $shortUrl
* @return file
*/
static function loadById($id)
{
$db = Database::getDatabase(true);
$row = $db->getRow('SELECT * FROM file WHERE id = ' . (int) $id);
if (!is_array($row))
{
return false;
}
$fileObj = new file();
foreach ($row AS $k => $v)
{
$fileObj->$k = $v;
}
return $fileObj;
}
/**
* Create short url
*
* @param integer $in
* @return string
*/
static function createShortUrlPart($in)
{
$codeset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$cbLength = strlen($codeset);
while ((int) $in > $cbLength - 1)
{
$out = $codeset[fmod($in, $cbLength)] . $out;
$in = floor($in / $cbLength);
}
return $codeset[$in] . $out;
}
/**
* Update short url for file
*
* @param integer $id
* @param string $shortUrl
*/
static function updateShortUrl($id, $shortUrl = '')
{
$db = Database::getDatabase(true);
$db->query('UPDATE file SET shortUrl = :shorturl WHERE id = :id', array('shorturl' => $shortUrl, 'id' => $id));
}
/**
* Load all by account id
*
* @param integer $accountId
* @return array
*/
static function loadAllByAccount($accountId)
{
$db = Database::getDatabase(true);
$rs = $db->getRows('SELECT * FROM file WHERE userId = ' . $db->quote($accountId) . ' ORDER BY originalFilename');
if (!is_array($rs))
{
return false;
}
return $rs;
}
/**
* Load all active by folder id
*
* @param integer $folderId
* @return array
*/
static function loadAllActiveByFolderId($folderId)
{
$db = Database::getDatabase(true);
$rs = $db->getRows('SELECT * FROM file WHERE folderId = ' . $db->quote($folderId) . ' AND statusId = 1 ORDER BY originalFilename');
if (!is_array($rs))
{
return false;
}
return $rs;
}
/**
* Load all active by account id
*
* @param integer $accountId
* @return array
*/
static function loadAllActiveByAccount($accountId)
{
$db = Database::getDatabase(true);
$rs = $db->getRows('SELECT * FROM file WHERE userId = ' . $db->quote($accountId) . ' AND statusId = 1 ORDER BY originalFilename');
if (!is_array($rs))
{
return false;
}
return $rs;
}
/**
* Load recent files based on account id
*
* @param integer $accountId
* @return array
*/
static function loadAllRecentByAccount($accountId, $activeOnly = false)
{
$db = Database::getDatabase(true);
$rs = $db->getRows('SELECT * FROM file WHERE userId = ' . $db->quote($accountId) . ($activeOnly === true ? ' AND statusId=1' : '') . ' ORDER BY uploadedDate DESC LIMIT 10');
if (!is_array($rs))
{
return false;
}
return $rs;
}
/**
* Load recent files based on IP address
*
* @param string $ip
* @return array
*/
static function loadAllRecentByIp($ip, $activeOnly = false)
{
$db = Database::getDatabase(true);
$rs = $db->getRows('SELECT * FROM file WHERE uploadedIP = ' . $db->quote($ip) . ($activeOnly === true ? ' AND statusId=1' : '') . ' ORDER BY uploadedDate DESC LIMIT 10');
if (!is_array($rs))
{
return false;
}
return $rs;
}
/**
* Get status label
*
* @param integer $statusId
* @return string
*/
static function getStatusLabel($statusId)
{
$db = Database::getDatabase(true);
$row = $db->getRow('SELECT label FROM file_status WHERE id = ' . (int) $statusId);
if (!is_array($row))
{
return 'unknown';
}
return $row['label'];
}
}