1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Files controller |
5
|
|
|
* |
6
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
7
|
|
|
* @author Omar El Gabry <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
class FilesController extends Controller { |
|
|
|
|
11
|
|
|
|
12
|
|
|
public function beforeAction(){ |
13
|
|
|
|
14
|
|
|
parent::beforeAction(); |
15
|
|
|
|
16
|
|
|
Config::setJsConfig('curPage', "files"); |
17
|
|
|
|
18
|
|
|
$action = $this->request->param('action'); |
19
|
|
|
$actions = ['create', 'delete']; |
20
|
|
|
$this->Security->requireAjax($actions); |
|
|
|
|
21
|
|
|
$this->Security->requirePost($actions); |
|
|
|
|
22
|
|
|
|
23
|
|
View Code Duplication |
switch($action){ |
|
|
|
|
24
|
|
|
case "create": |
25
|
|
|
$this->Security->config("form", [ 'fields' => ['file']]); |
|
|
|
|
26
|
|
|
break; |
27
|
|
|
case "delete": |
28
|
|
|
$this->Security->config("form", [ 'fields' => ['file_id']]); |
|
|
|
|
29
|
|
|
break; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
View Code Duplication |
public function index(){ |
|
|
|
|
34
|
|
|
|
35
|
|
|
// clear all notifications whenever you hit 'files' in the navigation bar |
36
|
|
|
$this->user->clearNotifications(Session::getUserId(), $this->file->table); |
|
|
|
|
37
|
|
|
|
38
|
|
|
$pageNum = $this->request->query("page"); |
39
|
|
|
|
40
|
|
|
$this->view->renderWithLayouts(Config::get('VIEWS_PATH') . "layout/default/", Config::get('VIEWS_PATH') . 'files/index.php', ['pageNum' => $pageNum]); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
View Code Duplication |
public function create(){ |
|
|
|
|
44
|
|
|
|
45
|
|
|
$fileData = $this->request->data("file"); |
46
|
|
|
|
47
|
|
|
$file = $this->file->create(Session::getUserId(), $fileData); |
|
|
|
|
48
|
|
|
|
49
|
|
|
if(!$file){ |
50
|
|
|
$this->view->renderErrors($this->file->errors()); |
|
|
|
|
51
|
|
|
}else{ |
52
|
|
|
|
53
|
|
|
$fileHTML = $this->view->render(Config::get('VIEWS_PATH') . 'files/files.php', array("files" => $file)); |
54
|
|
|
$this->view->renderJson(array("data" => $fileHTML)); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
View Code Duplication |
public function delete(){ |
|
|
|
|
59
|
|
|
|
60
|
|
|
$fileId = Encryption::decryptIdWithDash($this->request->data("file_id")); |
61
|
|
|
|
62
|
|
|
if(!$this->file->exists($fileId)){ |
|
|
|
|
63
|
|
|
return $this->error(404); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->file->deleteById($fileId); |
|
|
|
|
67
|
|
|
|
68
|
|
|
$this->view->renderJson(array("success" => true)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function isAuthorized(){ |
72
|
|
|
|
73
|
|
|
$action = $this->request->param('action'); |
74
|
|
|
$role = Session::getUserRole(); |
75
|
|
|
$resource = "files"; |
76
|
|
|
|
77
|
|
|
// only for admins |
78
|
|
|
Permission::allow('admin', $resource, ['*']); |
79
|
|
|
|
80
|
|
|
// only for normal users |
81
|
|
|
Permission::allow('user', $resource, ['index', 'create']); |
82
|
|
|
Permission::allow('user', $resource, ['delete'], 'owner'); |
83
|
|
|
|
84
|
|
|
$fileId = $this->request->data("file_id"); |
85
|
|
|
if(!empty($fileId)){ |
86
|
|
|
$fileId = Encryption::decryptIdWithDash($fileId); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$config = [ |
90
|
|
|
"user_id" => Session::getUserId(), |
91
|
|
|
"table" => "files", |
92
|
|
|
"id" => $fileId |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
return Permission::check($role, $resource, $action, $config); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
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.