1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System PHP 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 Resources |
24
|
|
|
* |
25
|
|
|
* @package O2System\Framework\Http\Controllers |
26
|
|
|
*/ |
27
|
|
|
class Resources extends Controller |
28
|
|
|
{ |
29
|
|
|
public $resourcesPath; |
30
|
|
|
public $speedLimit = 1024; |
31
|
|
|
public $resumeable = true; |
32
|
|
|
|
33
|
|
|
public function __construct() |
34
|
|
|
{ |
35
|
|
|
$this->resourcesPath = PATH_RESOURCES; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function route() |
39
|
|
|
{ |
40
|
|
|
$download = false; |
41
|
|
|
if (func_get_arg(0) === 'index') { |
42
|
|
|
$segments = func_get_arg(1); |
43
|
|
|
} else { |
44
|
|
|
$segments = array_merge([func_get_arg(0)], func_get_arg(1)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (false !== ($key = array_search('download', $segments))) { |
48
|
|
|
$download = true; |
49
|
|
|
unset($segments[ $key ]); |
50
|
|
|
$segments = array_values($segments); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (count($segments)) { |
54
|
|
|
$filePath = $this->resourcesPath . implode(DIRECTORY_SEPARATOR, $segments); |
55
|
|
|
if (is_file($filePath)) { |
56
|
|
|
if ($download) { |
57
|
|
|
$downloader = new Downloader($filePath); |
58
|
|
|
$downloader |
59
|
|
|
->speedLimit($this->speedLimit) |
60
|
|
|
->resumeable($this->resumeable) |
61
|
|
|
->download(); |
62
|
|
|
} else { |
63
|
|
|
$fileInfo = new SplFileInfo($filePath); |
64
|
|
|
header('Content-Disposition: filename=' . $fileInfo->getFilename()); |
65
|
|
|
header('Content-Transfer-Encoding: binary'); |
66
|
|
|
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT'); |
67
|
|
|
header('Content-Type: ' . $fileInfo->getMime()); |
68
|
|
|
echo readfile($filePath); |
69
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
} else { |
72
|
|
|
redirect_url('error/404'); |
73
|
|
|
} |
74
|
|
|
} else { |
75
|
|
|
redirect_url('error/403'); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.