1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package Blogs |
4
|
|
|
* @category modules |
5
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi |
7
|
|
|
* @license MIT License, see license.txt |
8
|
|
|
*/ |
9
|
|
|
namespace cs\modules\Blogs\api; |
10
|
|
|
use |
11
|
|
|
cs\Config, |
12
|
|
|
cs\ExitException, |
13
|
|
|
cs\Language\Prefix, |
14
|
|
|
cs\User, |
15
|
|
|
cs\modules\Blogs\Posts, |
16
|
|
|
cs\modules\Blogs\Sections, |
17
|
|
|
cs\modules\Blogs\api\Controller\admin; |
18
|
|
|
|
19
|
|
|
class Controller { |
20
|
|
|
use |
21
|
|
|
admin; |
22
|
|
|
|
23
|
|
|
static function __get_settings () { |
24
|
|
|
$User = User::instance(); |
25
|
|
|
$module_data = Config::instance()->module('Blogs'); |
26
|
|
|
$admin = $User->admin() && $User->get_permission('admin/Blogs', 'index'); |
27
|
|
|
return [ |
28
|
|
|
'inline_editor' => functionality('inline_editor'), |
29
|
|
|
'max_sections' => $module_data->max_sections, |
30
|
|
|
'new_posts_only_from_admins' => (bool)$module_data->new_posts_only_from_admins, |
31
|
|
|
'comments_enabled' => $module_data->enable_comments && functionality('comments'), |
32
|
|
|
'admin' => $admin, |
33
|
|
|
'admin_edit' => $admin && $User->get_permission('admin/Blogs', 'edit_post') |
34
|
|
|
]; |
35
|
|
|
} |
36
|
|
|
/** |
37
|
|
|
* @param \cs\Request $Request |
38
|
|
|
* |
39
|
|
|
* @return array |
40
|
|
|
* |
41
|
|
|
* @throws ExitException |
42
|
|
|
*/ |
43
|
|
|
static function posts_get ($Request) { |
44
|
|
|
$id = $Request->route_ids(0); |
45
|
|
|
if ($id) { |
46
|
|
|
$post = Posts::instance()->get($id); |
47
|
|
|
if (!$post) { |
48
|
|
|
throw new ExitException(404); |
49
|
|
|
} |
50
|
|
|
return $post; |
51
|
|
|
} else { |
|
|
|
|
52
|
|
|
// TODO: implement latest posts |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
/** |
56
|
|
|
* @param \cs\Request $Request |
57
|
|
|
* @param \cs\Response $Response |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
* |
61
|
|
|
* @throws ExitException |
62
|
|
|
*/ |
63
|
|
|
static function posts_post ($Request, $Response) { |
64
|
|
|
$Config = Config::instance(); |
65
|
|
|
$module_data = $Config->module('Blogs'); |
66
|
|
|
$L = new Prefix('blogs_'); |
67
|
|
|
$User = User::instance(); |
68
|
|
|
if (!$User->admin() && $module_data->new_posts_only_from_admins) { |
69
|
|
|
throw new ExitException(403); |
70
|
|
|
} |
71
|
|
|
if (!$User->user()) { |
72
|
|
|
throw new ExitException($L->for_registered_users_only, 403); |
73
|
|
|
} |
74
|
|
|
$data = static::check_request_data($Request, $L); |
75
|
|
|
if (!$data) { |
76
|
|
|
throw new ExitException(400); |
77
|
|
|
} |
78
|
|
|
$Posts = Posts::instance(); |
79
|
|
|
$id = $Posts->add($data['title'], $data['path'], $data['content'], $data['sections'], $data['tags'], $data['mode'] == 'draft'); |
80
|
|
|
if (!$id) { |
81
|
|
|
throw new ExitException($L->post_adding_error, 500); |
82
|
|
|
} |
83
|
|
|
$Response->code = 201; |
84
|
|
|
return [ |
85
|
|
|
'id' => $id, |
86
|
|
|
'url' => $Config->base_url().'/'.path($L->Blogs).'/'.$Posts->get($id)['path'].":$id" |
87
|
|
|
]; |
88
|
|
|
} |
89
|
|
|
/** |
90
|
|
|
* @param \cs\Request $Request |
91
|
|
|
* |
92
|
|
|
* @return array |
93
|
|
|
* |
94
|
|
|
* @throws ExitException |
95
|
|
|
*/ |
96
|
|
|
static function posts_put ($Request) { |
97
|
|
|
$Config = Config::instance(); |
98
|
|
|
$L = new Prefix('blogs_'); |
99
|
|
|
$User = User::instance(); |
100
|
|
|
$id = $Request->route(1); |
101
|
|
|
$data = static::check_request_data($Request, $L); |
102
|
|
|
if (!$id || !$data) { |
103
|
|
|
throw new ExitException(400); |
104
|
|
|
} |
105
|
|
|
$Posts = Posts::instance(); |
106
|
|
|
$post = $Posts->get($id); |
107
|
|
|
if (!$post) { |
108
|
|
|
throw new ExitException(404); |
109
|
|
|
} |
110
|
|
|
if ( |
111
|
|
|
!$User->admin() || |
112
|
|
|
!$User->get_permission('admin/Blogs', 'index') || |
113
|
|
|
!$User->get_permission('admin/Blogs', 'edit_post') |
114
|
|
|
) { |
115
|
|
|
throw new ExitException(403); |
116
|
|
|
} |
117
|
|
|
if (!$Posts->set($id, $data['title'], $data['path'], $data['content'], $data['sections'], $data['tags'], $data['mode'] == 'draft')) { |
118
|
|
|
throw new ExitException($L->post_saving_error, 500); |
119
|
|
|
} |
120
|
|
|
return [ |
121
|
|
|
'id' => $id, |
122
|
|
|
'url' => $Config->base_url().'/'.path($L->Blogs).'/'.$Posts->get($id)['path'].":$id" |
123
|
|
|
]; |
124
|
|
|
} |
125
|
|
|
/** |
126
|
|
|
* @param \cs\Request $Request |
127
|
|
|
* |
128
|
|
|
* @throws ExitException |
129
|
|
|
*/ |
130
|
|
|
static function posts_delete ($Request) { |
131
|
|
|
$L = new Prefix('blogs_'); |
132
|
|
|
$User = User::instance(); |
133
|
|
|
$id = $Request->route(1); |
134
|
|
|
if (!$id) { |
135
|
|
|
throw new ExitException(400); |
136
|
|
|
} |
137
|
|
|
$Posts = Posts::instance(); |
138
|
|
|
$post = $Posts->get($id); |
139
|
|
|
if (!$post) { |
140
|
|
|
throw new ExitException(404); |
141
|
|
|
} |
142
|
|
|
if ( |
143
|
|
|
$post['user'] != $User->id && |
144
|
|
|
!( |
145
|
|
|
$User->admin() && |
146
|
|
|
$User->get_permission('admin/Blogs', 'index') && |
147
|
|
|
$User->get_permission('admin/Blogs', 'edit_post') |
148
|
|
|
) |
149
|
|
|
) { |
150
|
|
|
throw new ExitException(403); |
151
|
|
|
} |
152
|
|
|
if (!$Posts->del($id)) { |
153
|
|
|
throw new ExitException($L->post_deleting_error, 500); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
/** |
157
|
|
|
* @param \cs\Request $Request |
158
|
|
|
* @param Prefix $L |
159
|
|
|
* |
160
|
|
|
* @return array |
161
|
|
|
* |
162
|
|
|
* @throws ExitException |
163
|
|
|
*/ |
164
|
|
|
protected static function check_request_data ($Request, $L) { |
165
|
|
|
$data = $Request->data('title', 'sections', 'content', 'tags', 'mode'); |
166
|
|
|
if (!$data) { |
167
|
|
|
throw new ExitException(400); |
168
|
|
|
} |
169
|
|
|
$data['path'] = $Request->data('path'); |
170
|
|
|
if (empty($data['title'])) { |
171
|
|
|
throw new ExitException($L->post_title_empty, 400); |
172
|
|
|
} |
173
|
|
|
if (empty($data['sections']) || !is_array($data['sections'])) { |
174
|
|
|
throw new ExitException($L->no_post_sections_specified, 400); |
175
|
|
|
} |
176
|
|
|
if (empty($data['content'])) { |
177
|
|
|
throw new ExitException($L->post_content_empty, 400); |
178
|
|
|
} |
179
|
|
|
if (empty($data['tags']) || !is_array($data['tags'])) { |
180
|
|
|
throw new ExitException($L->no_post_tags_specified, 400); |
181
|
|
|
} |
182
|
|
|
return $data; |
183
|
|
|
} |
184
|
|
|
/** |
185
|
|
|
* @param \cs\Request $Request |
186
|
|
|
* |
187
|
|
|
* @return array |
188
|
|
|
* |
189
|
|
|
* @throws ExitException |
190
|
|
|
*/ |
191
|
|
|
static function posts_preview ($Request) { |
192
|
|
|
$User = User::instance(); |
193
|
|
|
if (!$User->user()) { |
194
|
|
|
throw new ExitException(403); |
195
|
|
|
} |
196
|
|
|
$data = $Request->data('title', 'sections', 'content', 'tags'); |
197
|
|
|
$data += [ |
198
|
|
|
'id' => 0, |
199
|
|
|
'path' => path($Request->data('path') ?: $data['title']), |
200
|
|
|
'user' => $User->id, |
201
|
|
|
'date' => 0 |
202
|
|
|
]; |
203
|
|
|
$Posts = Posts::instance(); |
204
|
|
|
return $Posts->post_to_jsonld($data); |
205
|
|
|
} |
206
|
|
|
/** |
207
|
|
|
* @param \cs\Request $Request |
208
|
|
|
* |
209
|
|
|
* @return array |
210
|
|
|
* |
211
|
|
|
* @throws ExitException |
212
|
|
|
*/ |
213
|
|
|
static function sections_get ($Request) { |
214
|
|
|
$id = $Request->route_ids(0); |
215
|
|
|
$Sections = Sections::instance(); |
216
|
|
|
if ($id) { |
217
|
|
|
$data = $Sections->get($id); |
218
|
|
|
if (!$data) { |
219
|
|
|
throw new ExitException(404); |
220
|
|
|
} |
221
|
|
|
return $data; |
222
|
|
|
} |
223
|
|
|
return $Sections->get_all(); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
This check looks for the
else
branches ofif
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
else
branches can be removed.could be turned into
This is much more concise to read.