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