1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Posts controller |
5
|
|
|
* |
6
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
7
|
|
|
* @author Omar El Gabry <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
class PostsController extends Controller{ |
|
|
|
|
11
|
|
|
|
12
|
|
|
|
13
|
|
|
public function beforeAction(){ |
14
|
|
|
|
15
|
|
|
parent::beforeAction(); |
16
|
|
|
|
17
|
|
|
Config::addJsConfig('curPage', "posts"); |
18
|
|
|
|
19
|
|
|
$action = $this->request->param('action'); |
20
|
|
|
$actions = ['create', 'update']; |
21
|
|
|
$this->Security->requirePost($actions); |
|
|
|
|
22
|
|
|
|
23
|
|
|
switch($action){ |
24
|
|
|
case "create": |
25
|
|
|
$this->Security->config("form", [ 'fields' => ['title', 'content']]); |
|
|
|
|
26
|
|
|
break; |
27
|
|
|
case "update": |
28
|
|
|
$this->Security->config("form", [ 'fields' => ['post_id', 'title', 'content']]); |
|
|
|
|
29
|
|
|
break; |
30
|
|
|
case "delete": |
31
|
|
|
$this->Security->config("validateCsrfToken", true); |
|
|
|
|
32
|
|
|
$this->Security->config("form", [ 'fields' => ['post_id']]); |
|
|
|
|
33
|
|
|
break; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* show posts page |
39
|
|
|
* |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function index(){ |
|
|
|
|
42
|
|
|
|
43
|
|
|
// clear all notifications |
44
|
|
|
$this->user->clearNotifications(Session::getUserId(), $this->post->table); |
|
|
|
|
45
|
|
|
|
46
|
|
|
$pageNum = $this->request->query("page"); |
47
|
|
|
|
48
|
|
|
echo $this->view->renderWithLayouts(Config::get('VIEWS_PATH') . "layout/default/", Config::get('VIEWS_PATH') . 'posts/index.php', ['pageNum' => $pageNum]); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* view a post |
53
|
|
|
* |
54
|
|
|
* @param integer|string $postId |
55
|
|
|
*/ |
56
|
|
|
public function view($postId = 0){ |
57
|
|
|
|
58
|
|
|
$postId = Encryption::decryptId($postId); |
59
|
|
|
|
60
|
|
|
if(!$this->post->exists($postId)){ |
|
|
|
|
61
|
|
|
$this->error(404); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
Config::addJsConfig('curPage', ["posts", "comments"]); |
65
|
|
|
Config::addJsConfig('postId', Encryption::encryptId($postId)); |
66
|
|
|
|
67
|
|
|
$action = $this->request->query('action'); |
68
|
|
|
echo $this->view->renderWithLayouts(Config::get('VIEWS_PATH') . "layout/default/", Config::get('VIEWS_PATH') . 'posts/viewPost.php', ["action"=> $action, "postId" => $postId]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* show new post form |
73
|
|
|
*/ |
74
|
|
|
public function newPost(){ |
75
|
|
|
echo $this->view->renderWithLayouts(Config::get('VIEWS_PATH') . "layout/default/", Config::get('VIEWS_PATH') . 'posts/newPost.php'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* creates a new post |
80
|
|
|
* |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
public function create(){ |
|
|
|
|
83
|
|
|
|
84
|
|
|
$title = $this->request->data("title"); |
85
|
|
|
$content = $this->request->data("content"); |
86
|
|
|
|
87
|
|
|
$result = $this->post->create(Session::getUserId(), $title, $content); |
|
|
|
|
88
|
|
|
|
89
|
|
|
if(!$result){ |
90
|
|
|
Session::set('posts-errors', $this->post->errors()); |
|
|
|
|
91
|
|
|
}else{ |
92
|
|
|
Session::set('posts-success', "Post has been created"); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
Redirector::root("Posts/newPost"); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* update a post |
100
|
|
|
* |
101
|
|
|
*/ |
102
|
|
|
public function update(){ |
103
|
|
|
|
104
|
|
|
$postId = $this->request->data("post_id"); |
105
|
|
|
$title = $this->request->data("title"); |
106
|
|
|
$content = $this->request->data("content"); |
107
|
|
|
|
108
|
|
|
$postId = Encryption::decryptId($postId); |
109
|
|
|
|
110
|
|
|
if(!$this->post->exists($postId)){ |
|
|
|
|
111
|
|
|
$this->error(404); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$post = $this->post->update($postId, $title, $content); |
|
|
|
|
115
|
|
|
|
116
|
|
|
if(!$post){ |
117
|
|
|
|
118
|
|
|
Session::set('posts-errors', $this->post->errors()); |
|
|
|
|
119
|
|
|
Redirector::root("Posts/View/" . urlencode(Encryption::encryptId($postId)) . "?action=update"); |
120
|
|
|
|
121
|
|
|
}else{ |
122
|
|
|
Redirector::root("Posts/View/" . urlencode(Encryption::encryptId($postId))); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function delete($postId = 0){ |
127
|
|
|
|
128
|
|
|
$postId = Encryption::decryptId($postId); |
129
|
|
|
|
130
|
|
|
if(!$this->post->exists($postId)){ |
|
|
|
|
131
|
|
|
$this->error(404); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$this->post->deleteById($postId); |
|
|
|
|
135
|
|
|
|
136
|
|
|
Redirector::root("Posts"); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function isAuthorized(){ |
140
|
|
|
|
141
|
|
|
$action = $this->request->param('action'); |
142
|
|
|
$role = Session::getUserRole(); |
143
|
|
|
$resource = "posts"; |
144
|
|
|
|
145
|
|
|
// only for admins |
146
|
|
|
Permission::allow('admin', $resource, ['*']); |
147
|
|
|
|
148
|
|
|
// only for normal users |
149
|
|
|
Permission::allow('user', $resource, ['index', 'view', 'newPost', 'create']); |
150
|
|
|
Permission::allow('user', $resource, ['update', 'delete'], 'owner'); |
151
|
|
|
|
152
|
|
|
$postId = ($action === "delete")? $this->request->param("args")[0]: $this->request->data("post_id"); |
153
|
|
|
if(!empty($postId)){ |
154
|
|
|
$postId = Encryption::decryptId($postId); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$config = [ |
158
|
|
|
"user_id" => Session::getUserId(), |
159
|
|
|
"table" => "posts", |
160
|
|
|
"id" => $postId |
161
|
|
|
]; |
162
|
|
|
|
163
|
|
|
return Permission::check($role, $resource, $action, $config); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
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.