1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Downloads controller |
5
|
|
|
* |
6
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
7
|
|
|
* @author Omar El Gabry <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
class DownloadsController extends Controller { |
|
|
|
|
10
|
|
|
|
11
|
|
|
|
12
|
|
|
public function beforeAction(){ |
13
|
|
|
|
14
|
|
|
parent::beforeAction(); |
15
|
|
|
|
16
|
|
|
$actions = ['download', 'users']; |
17
|
|
|
$this->Security->requireGet($actions); |
|
|
|
|
18
|
|
|
|
19
|
|
|
// if you want to add csrf_token in the URL of file download |
20
|
|
|
// So, it will be something like this: http://localhost/miniPHP/downloads/download/f850749b62bf3badfb6c0?csrf_token=21eb0f2c6b4fddce8a7f3 |
21
|
|
|
// $this->Security->config("validateCsrfToken", true); |
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* download a file provided by it's hashed name |
26
|
|
|
* the url should be something like: http://localhost/miniPHP/downloads/download/f850749b62bf3ba57b6380b67c6f3096bcdfb6c0 |
27
|
|
|
* |
28
|
|
|
* @param string $hashedFileName |
29
|
|
|
*/ |
30
|
|
|
public function download($hashedFileName = ''){ |
31
|
|
|
|
32
|
|
|
$fullPath = APP . "uploads/" ; |
33
|
|
|
$file = $this->file->getByHashedName($hashedFileName); |
|
|
|
|
34
|
|
|
|
35
|
|
|
if(empty($file)){ |
36
|
|
|
return $this->error(404); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$fullPath .= $hashedFileName . "." . $file["extension"]; |
40
|
|
|
$file["basename"] = $file["filename"] . "." . $file["extension"]; |
41
|
|
|
|
42
|
|
|
if(!Uploader::isFileExists($fullPath)){ |
43
|
|
|
return $this->error(404); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->response->download($fullPath, ["basename" => $file["basename"], "extension" => $file["extension"]]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* download users data as csv file |
51
|
|
|
* |
52
|
|
|
*/ |
53
|
|
|
public function users(){ |
54
|
|
|
|
55
|
|
|
$data = $this->admin->getUsersData(); |
|
|
|
|
56
|
|
|
$this->response->csv(["cols" => $data["cols"], "rows" => $data["rows"]], ["filename" => $data["filename"]]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function isAuthorized(){ |
60
|
|
|
|
61
|
|
|
$action = $this->request->param('action'); |
62
|
|
|
$role = Session::getUserRole(); |
63
|
|
|
$resource = "downloads"; |
64
|
|
|
|
65
|
|
|
//only for admin |
66
|
|
|
Permission::allow('admin', $resource, "*"); |
67
|
|
|
|
68
|
|
|
//only for normal users |
69
|
|
|
Permission::allow('user', $resource, "download"); |
70
|
|
|
|
71
|
|
|
return Permission::check($role, $resource, $action); |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.