1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System Reactor 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\Reactor\Http\Controllers; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Filesystem\Handlers\Downloader; |
19
|
|
|
use O2System\Reactor\Http\Controller; |
20
|
|
|
use O2System\Spl\Info\SplFileInfo; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Resources |
24
|
|
|
* |
25
|
|
|
* @package O2System\Reactor\Http\Controllers |
26
|
|
|
*/ |
27
|
|
|
class Resources extends Controller |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Resources::$inherited |
31
|
|
|
* |
32
|
|
|
* Controller inherited flag. |
33
|
|
|
* |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
static public $inherited = true; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Resources::$directoryPath |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
public $directoryPath; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Resources::$speedLimit |
47
|
|
|
* |
48
|
|
|
* @var int |
49
|
|
|
*/ |
50
|
|
|
public $speedLimit = 1024; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Resources::$resumeable |
54
|
|
|
* |
55
|
|
|
* @var bool |
56
|
|
|
*/ |
57
|
|
|
public $resumeable = true; |
58
|
|
|
|
59
|
|
|
// ------------------------------------------------------------------------ |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Resources::__construct |
63
|
|
|
*/ |
64
|
|
|
public function __construct() |
65
|
|
|
{ |
66
|
|
|
$this->directoryPath = PATH_RESOURCES; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// ------------------------------------------------------------------------ |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Resources::route |
73
|
|
|
*/ |
74
|
|
|
public function route() |
75
|
|
|
{ |
76
|
|
|
$segments = server_request()->getUri()->segments->getArrayCopy(); |
77
|
|
|
array_shift($segments); |
78
|
|
|
|
79
|
|
|
$download = false; |
80
|
|
|
if (false !== ($key = array_search('download', $segments))) { |
81
|
|
|
$download = true; |
82
|
|
|
unset($segments[ $key ]); |
83
|
|
|
$segments = array_values($segments); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (count($segments)) { |
87
|
|
|
$filePath = $this->directoryPath . implode(DIRECTORY_SEPARATOR, $segments); |
88
|
|
|
if (is_file($filePath)) { |
89
|
|
|
if ($download) { |
90
|
|
|
$downloader = new Downloader($filePath); |
91
|
|
|
$downloader |
92
|
|
|
->speedLimit($this->speedLimit) |
93
|
|
|
->resumeable($this->resumeable) |
94
|
|
|
->download(); |
95
|
|
|
} else { |
96
|
|
|
$fileInfo = new SplFileInfo($filePath); |
97
|
|
|
header('Content-Disposition: filename=' . $fileInfo->getFilename()); |
98
|
|
|
header('Content-Transfer-Encoding: binary'); |
99
|
|
|
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT'); |
100
|
|
|
header('Content-Type: ' . $fileInfo->getMime()); |
101
|
|
|
echo @readfile($filePath); |
102
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
} else { |
105
|
|
|
redirect_url('error/404'); |
106
|
|
|
} |
107
|
|
|
} else { |
108
|
|
|
redirect_url('error/403'); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|