1
|
|
|
<?php namespace Taskforcedev\LaravelForum\Http\Controllers; |
2
|
|
|
|
3
|
|
|
use \Auth; |
4
|
|
|
use \Event; |
5
|
|
|
use \Redirect; |
6
|
|
|
use \Schema; |
7
|
|
|
|
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
|
10
|
|
|
use Taskforcedev\LaravelForum\Events\PostCreated; |
11
|
|
|
use Taskforcedev\LaravelForum\Events\PostReply; |
12
|
|
|
use Taskforcedev\LaravelForum\Models\Forum; |
13
|
|
|
use Taskforcedev\LaravelForum\Models\ForumCategory; |
14
|
|
|
use Taskforcedev\LaravelForum\Models\ForumPost; |
15
|
|
|
use Taskforcedev\LaravelForum\Models\ForumReply; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class ApiController |
19
|
|
|
* @package Taskforcedev\LaravelForum\Http\Controllers |
20
|
|
|
*/ |
21
|
|
|
class ApiController extends BaseController |
22
|
|
|
{ |
23
|
|
|
public function forumCategoryStore(Request $request) |
24
|
|
|
{ |
25
|
|
|
$data = [ |
26
|
|
|
"name" => $request->input('name'), |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
$response = $this->adminCheck(); |
30
|
|
|
if (isset($response)) { |
31
|
|
|
return $response; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/* If data invalid return bad request */ |
35
|
|
|
if (!ForumCategory::valid($data)) { |
36
|
|
|
return response('Bad Request', 400); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
ForumCategory::create($data); |
40
|
|
|
|
41
|
|
|
/* After creating a category lets redirect back to the category list. */ |
42
|
|
|
return redirect()->route('laravel-forum.admin.categories'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function forumStore(Request $request) |
46
|
|
|
{ |
47
|
|
|
$data = [ |
48
|
|
|
"name" => $request->input('name'), |
49
|
|
|
"description" => $request->input('description'), |
50
|
|
|
"category_id" => $request->input('category'), |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
$response = $this->adminCheck(); |
54
|
|
|
if (isset($response)) { |
55
|
|
|
return $response; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (!Forum::valid($data)) { |
59
|
|
|
return response('Bad Request', 400); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
Forum::create($data); |
63
|
|
|
|
64
|
|
|
/* After creating a forum lets redirect back to the forum list. */ |
65
|
|
|
return redirect()->route('laravel-forum.admin.forums'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function forumPostStore(Request $request) |
69
|
|
|
{ |
70
|
|
|
if (!Auth::check()) { |
71
|
|
|
return response('Unauthorized', 401); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$user = Auth::user(); |
75
|
|
|
|
76
|
|
|
$forum_id = $request->input('forum_id'); |
77
|
|
|
|
78
|
|
|
$data = [ |
79
|
|
|
"author_id" => $user->id, |
80
|
|
|
"title" => $request->input('title'), |
81
|
|
|
"body" => $this->sanitizeData($request->input('body')), |
82
|
|
|
"forum_id" => $forum_id |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
if (!ForumPost::valid($data)) { |
86
|
|
|
return response('Bad Request', 400); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$post = ForumPost::create($data); |
90
|
|
|
|
91
|
|
|
event(new PostCreated($post, $user)); |
92
|
|
|
return redirect()->route('laravel-forum.view.post', [$forum_id , $post->id]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function forumReplyStore(Request $request) |
96
|
|
|
{ |
97
|
|
|
if (!Auth::check()) { |
98
|
|
|
return response('Unauthorized', 401); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$user = Auth::user(); |
102
|
|
|
|
103
|
|
|
$forum_id = $request->input('forum_id'); |
104
|
|
|
$post_id = $request->input('post_id'); |
105
|
|
|
|
106
|
|
|
$data = [ |
107
|
|
|
'author_id' => $user->id, |
108
|
|
|
'body' => $this->sanitizeData($request->input('body')), |
109
|
|
|
'post_id' => $post_id, |
110
|
|
|
]; |
111
|
|
|
|
112
|
|
|
if (!ForumReply::valid($data)) { |
113
|
|
|
return redirect()->route('laravel-forum.view.post', [$forum_id, $post_id]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$reply = ForumReply::create($data); |
117
|
|
|
|
118
|
|
|
event(new PostReply($reply, $user)); |
119
|
|
|
return redirect()->route('laravel-forum.view.post', [$forum_id, $post_id]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
private function adminCheck() |
123
|
|
|
{ |
124
|
|
|
if (!$this->canCreateForums()) { |
125
|
|
|
return response('Unauthorised', 401); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
private function sanitizeData($data) |
130
|
|
|
{ |
131
|
|
|
/* Sanitize post input */ |
132
|
|
|
$removals = [ |
133
|
|
|
'/<script\b[^>]*>/', |
134
|
|
|
'/<\/script\b[^>]*>/' |
135
|
|
|
]; |
136
|
|
|
foreach ($removals as $r) { |
137
|
|
|
$data = preg_replace($r, '', $data); |
138
|
|
|
} |
139
|
|
|
return $data; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
View Code Duplication |
public function lockPost(Request $request, $id) |
143
|
|
|
{ |
144
|
|
|
if (!$this->canAdministrate() && !$this->canModerate()) { |
145
|
|
|
return response('Unauthorised', 401); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$post = $this->postExists($id); |
149
|
|
|
if (!$post) { |
150
|
|
|
return response('Post not found', 404); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$post->locked = 1; |
154
|
|
|
$post->save(); |
155
|
|
|
return response('Post Locked', 200); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
View Code Duplication |
public function unlockPost(Request $request, $id) |
159
|
|
|
{ |
160
|
|
|
if (!$this->canAdministrate() && !$this->canModerate()) { |
161
|
|
|
return response('Unauthorised', 401); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$post = $this->postExists($id); |
165
|
|
|
if (!$post) { |
166
|
|
|
return response('Post not found', 404); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$post->locked = 0; |
170
|
|
|
$post->save(); |
171
|
|
|
return response('Post Unlocked', 200); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
View Code Duplication |
public function stickyPost(Request $request, $id) |
175
|
|
|
{ |
176
|
|
|
if (!$this->canAdministrate() && !$this->canModerate()) { |
177
|
|
|
return response('Unauthorised', 401); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$post = $this->postExists($id); |
181
|
|
|
if (!$post) { |
182
|
|
|
return response('Post not found', 404); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$post->sticky = 1; |
186
|
|
|
$post->save(); |
187
|
|
|
return response('Post Unlocked', 200); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
View Code Duplication |
public function unstickyPost(Request $request, $id) |
191
|
|
|
{ |
192
|
|
|
if (!$this->canAdministrate() && !$this->canModerate()) { |
193
|
|
|
return response('Unauthorised', 401); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$post = $this->postExists($id); |
197
|
|
|
if (!$post) { |
198
|
|
|
return response('Post not found', 404); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$post->sticky = 0; |
202
|
|
|
$post->save(); |
203
|
|
|
return response('Post Unlocked', 200); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
private function postExists($post_id) |
207
|
|
|
{ |
208
|
|
|
try { |
209
|
|
|
$post = ForumPost::where('id', $post_id)->firstOrFail(); |
210
|
|
|
return $post; |
211
|
|
|
} catch (\Exception $e) { |
212
|
|
|
return false; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
View Code Duplication |
public function postDelete($forum_id, $post_id) |
217
|
|
|
{ |
218
|
|
|
if (!$this->canAdministrate() && !$this->canModerate()) { |
219
|
|
|
return response('Unauthorised', 401); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
$post = $this->postExists($post_id); |
223
|
|
|
if (!$post) { |
224
|
|
|
return response('Post not found', 404); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$post->delete(); |
228
|
|
|
return response('Post Deleted', 200); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
View Code Duplication |
public function forumDelete(Request $request) |
232
|
|
|
{ |
233
|
|
|
if (!$this->canAdministrate() && !$this->canModerate()) { |
234
|
|
|
return response('Unauthorised', 401); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
$forum_id = $request->input('forum_id'); |
238
|
|
|
|
239
|
|
|
$forum = $this->forumExists($forum_id); |
240
|
|
|
if (!$forum) { |
241
|
|
|
return response('Forum not found', 404); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$forum->delete(); |
245
|
|
|
return response('Forum Deleted', 200); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
private function forumExists($id) |
249
|
|
|
{ |
250
|
|
|
try { |
251
|
|
|
$forum = Forum::where('id', $id)->firstOrFail(); |
252
|
|
|
return $forum; |
253
|
|
|
} catch (\Exception $e) { |
254
|
|
|
return false; |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
258
|
|
View Code Duplication |
public function forumCategoryDelete(Request $request) |
259
|
|
|
{ |
260
|
|
|
if (!$this->canAdministrate() && !$this->canModerate()) { |
261
|
|
|
return response('Unauthorised', 401); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
$cat_id = $request->input('category_id'); |
265
|
|
|
|
266
|
|
|
$cat = $this->forumCategoryExists($cat_id); |
|
|
|
|
267
|
|
|
if (!$cat) { |
268
|
|
|
return response('Forum Category not found', 404); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
$cat->delete(); |
272
|
|
|
return response('Forum Category Deleted', 200); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
private function forumCategoryExists(Request $request, $id) |
276
|
|
|
{ |
277
|
|
|
try { |
278
|
|
|
$cat = ForumCategory::where('id', $id)->firstOrFail(); |
279
|
|
|
return $cat; |
280
|
|
|
} catch (\Exception $e) { |
281
|
|
|
return false; |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
This check looks for function calls that miss required arguments.