1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Framework\Http\Controllers; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Filesystem\Handlers\Downloader; |
19
|
|
|
use O2System\Framework\Http\Controller; |
20
|
|
|
use O2System\Spl\Info\SplFileInfo; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Storage |
24
|
|
|
* |
25
|
|
|
* @package O2System\Framework\Http\Controllers |
26
|
|
|
*/ |
27
|
|
|
class Storage extends Controller |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Storage::$directoryPath |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
public $directoryPath; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Storage::$speedLimit |
38
|
|
|
* |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
public $speedLimit = 1024; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Storage::$resumeable |
45
|
|
|
* |
46
|
|
|
* @var bool |
47
|
|
|
*/ |
48
|
|
|
public $resumeable = true; |
49
|
|
|
|
50
|
|
|
// ------------------------------------------------------------------------ |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Storage::__construct |
54
|
|
|
*/ |
55
|
|
|
public function __construct() |
56
|
|
|
{ |
57
|
|
|
$this->directoryPath = PATH_STORAGE; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// ------------------------------------------------------------------------ |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Storage::route |
64
|
|
|
*/ |
65
|
|
|
public function route() |
66
|
|
|
{ |
67
|
|
|
$segments = server_request()->getUri()->getSegments()->getParts(); |
68
|
|
|
array_shift($segments); |
69
|
|
|
|
70
|
|
|
$download = false; |
71
|
|
|
|
72
|
|
|
if (false !== ($key = array_search('download', $segments))) { |
73
|
|
|
$download = true; |
74
|
|
|
unset($segments[ $key ]); |
75
|
|
|
$segments = array_values($segments); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (count($segments)) { |
79
|
|
|
$filePath = $this->directoryPath . implode(DIRECTORY_SEPARATOR, $segments); |
80
|
|
|
if (is_file($filePath)) { |
81
|
|
|
if ($download) { |
82
|
|
|
$downloader = new Downloader($filePath); |
83
|
|
|
$downloader |
84
|
|
|
->speedLimit($this->speedLimit) |
85
|
|
|
->resumeable($this->resumeable) |
86
|
|
|
->download(); |
87
|
|
|
} else { |
88
|
|
|
$fileInfo = new SplFileInfo($filePath); |
89
|
|
|
header('Content-Disposition: filename=' . $fileInfo->getFilename()); |
90
|
|
|
header('Content-Transfer-Encoding: binary'); |
91
|
|
|
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT'); |
92
|
|
|
header('Content-Type: ' . $fileInfo->getMime()); |
93
|
|
|
echo readfile($filePath); |
94
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
} else { |
97
|
|
|
redirect_url('error/404'); |
98
|
|
|
} |
99
|
|
|
} else { |
100
|
|
|
redirect_url('error/403'); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.