-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathForum.php
More file actions
289 lines (236 loc) · 12.3 KB
/
Forum.php
File metadata and controls
289 lines (236 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
/**
* Copyright (C) 2015-2019 FeatherBB
* based on code by (C) 2008-2015 FluxBB
* and Rickard Andersson (C) 2002-2008 PunBB
* License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
*/
namespace FeatherBB\Controller;
use FeatherBB\Core\Error;
use FeatherBB\Core\Interfaces\ForumSettings;
use FeatherBB\Core\Interfaces\Hooks;
use FeatherBB\Core\Interfaces\Input;
use FeatherBB\Core\Interfaces\Lang;
use FeatherBB\Core\Interfaces\Router;
use FeatherBB\Core\Interfaces\User;
use FeatherBB\Core\Interfaces\View;
use FeatherBB\Core\Track;
use FeatherBB\Core\Url;
use FeatherBB\Core\Utils;
class Forum
{
public function __construct()
{
$this->model = new \FeatherBB\Model\Forum();
Lang::load('forum');
Lang::load('misc');
}
public function display($req, $res, $args)
{
Hooks::fire('controller.forum.display');
// Fetch some information about the forum
$curForum = $this->model->getForumInfo($args['id']);
// Is this a redirect forum? In that case, redirect!
if ($curForum['redirect_url'] != '') {
return Router::redirect(Router::pathFor('Forum', ['id' => $curForum['redirect_url'], 'name' => $curForum['forum_url']]));
}
// Sort out who the moderators are and if we are currently a moderator (or an admin)
$modsArray = ($curForum['moderators'] != '') ? unserialize($curForum['moderators']) : [];
$isAdmmod = (User::isAdmin() || (User::isAdminMod() && array_key_exists(User::get()->username, $modsArray))) ? true : false;
$sortBy = $this->model->sortForumBy($curForum['sort_by']);
// Can we or can we not post new topics?
if (($curForum['post_topics'] == '' && User::can('topic.post')) || $curForum['post_topics'] == '1' || $isAdmmod) {
$postLink = "\t\t\t".'<p class="postlink conr"><a href="'.Router::pathFor('newTopic', ['fid' => $args['id']]).'">'.__('Post topic').'</a></p>'."\n";
} else {
$postLink = '';
}
// Determine the topic offset (based on $args['page'])
$numPages = ceil($curForum['num_topics'] / User::getPref('disp.topics'));
$p = (!isset($args['page']) || $args['page'] <= 1 || $args['page'] > $numPages) ? 1 : intval($args['page']);
$startFrom = User::getPref('disp.topics') * ($p - 1);
$urlForum = Url::slug($curForum['forum_name']);
// Generate paging links
$pagingLinks = '<span class="pages-label">'.__('Pages').' </span>'.Url::paginate($numPages, $p, 'forum/'.$args['id'].'/'.$urlForum.'/#');
$forumActions = $this->model->getForumActions($args['id'], $urlForum, ($curForum['is_subscribed'] == User::get()->id));
View::addAsset('canonical', Router::pathFor('Forum', ['id' => $args['id'], 'name' => $args['name']]));
if ($numPages > 1) {
if ($p > 1) {
View::addAsset('prev', Router::pathFor('Forum', ['id' => $args['id'], 'name' => $args['name'], 'page' => intval($p-1)]));
}
if ($p < $numPages) {
View::addAsset('next', Router::pathFor('Forum', ['id' => $args['id'], 'name' => $args['name'], 'page' => intval($p+1)]));
}
}
View::setPageInfo([
'title' => [Utils::escape(ForumSettings::get('o_board_title')), Utils::escape($curForum['forum_name'])],
'active_page' => 'Forum',
'page_number' => $p,
'paging_links' => $pagingLinks,
'is_indexed' => true,
'id' => $args['id'],
'fid' => $args['id'],
'forum_data' => $this->model->printTopics($args['id'], $sortBy, $startFrom),
'cur_forum' => $curForum,
'post_link' => $postLink,
'start_from' => $startFrom,
'url_forum' => $urlForum,
'forum_actions' => $forumActions,
'is_admmod' => $isAdmmod,
])->addTemplate('@forum/forum')->display();
}
public function moderate($req, $res, $args)
{
Hooks::fire('controller.forum.moderate');
// Make sure that only admmods allowed access this page
$moderators = $this->model->getModerators($args['id']);
$modsArray = ($moderators != '') ? unserialize($moderators) : [];
if (!User::isAdmin() && (!User::isAdminMod() || !array_key_exists(User::get()->username, $modsArray))) {
throw new Error(__('No permission'), 403);
}
// Fetch some info about the forum
$curForum = $this->model->getForumInfo($args['id']);
// Is this a redirect forum? In that case, abort!
if ($curForum['redirect_url'] != '') {
throw new Error(__('Bad request'), '404');
}
$sortBy = $this->model->sortForumBy($curForum['sort_by']);
// Determine the topic offset (based on $_gET['p'])
$numPages = ceil($curForum['num_topics'] / User::getPref('disp.topics'));
$p = (!isset($args['page']) || $args['page'] <= 1 || $args['page'] > $numPages) ? 1 : intval($args['page']);
$startFrom = User::getPref('disp.topics') * ($p - 1);
$urlForum = Url::slug($curForum['forum_name']);
View::setPageInfo([
'title' => [Utils::escape(ForumSettings::get('o_board_title')), Utils::escape($curForum['forum_name'])],
'active_page' => 'moderate',
'page' => $p,
'id' => $args['id'],
'p' => $p,
'url_forum' => $urlForum,
'cur_forum' => $curForum,
'paging_links' => '<span class="pages-label">'.__('Pages').' </span>'.Url::paginate($numPages, $p, 'forum/'.$args['id'].'/'.$urlForum.'/moderate/#'),
'topic_data' => $this->model->displayTopicsModerate($args['id'], $sortBy, $startFrom),
'start_from' => $startFrom,
]
)->addTemplate('@forum/moderate/moderator_forum')->display();
}
public function markread($req, $res, $args)
{
Hooks::fire('controller.forum.markread');
$trackedTopics = Track::getTrackedTopics();
$trackedTopics['forums'][$args['id']] = time();
Track::setTrackedTopics($trackedTopics);
return Router::redirect(Router::pathFor('Forum', ['id' => $args['id'], 'name' => $args['name']]), __('Mark forum read redirect'));
}
public function subscribe($req, $res, $args)
{
Hooks::fire('controller.forum.subscribe');
$this->model->subscribe($args['id']);
return Router::redirect(Router::pathFor('Forum', ['id' => $args['id'], 'name' => $args['name']]), __('Subscribe redirect'));
}
public function unsubscribe($req, $res, $args)
{
Hooks::fire('controller.forum.unsubscribe');
$this->model->unsubscribe($args['id']);
return Router::redirect(Router::pathFor('Forum', ['id' => $args['id'], 'name' => $args['name']]), __('Unsubscribe redirect'));
}
public function dealposts($req, $res, $args)
{
Hooks::fire('controller.forum.dealposts');
// Make sure that only admmods allowed access this page
$moderators = $this->model->getModerators($args['id']);
$modsArray = ($moderators != '') ? unserialize($moderators) : [];
if (!User::isAdmin() && (!User::isAdminMod() || !array_key_exists(User::get()->username, $modsArray))) {
throw new Error(__('No permission'), 403);
}
$topicModel = new \FeatherBB\Model\Topic();
// Move one or more topics
if (Input::post('move_topics') || Input::post('move_topics_to')) {
$topics = Input::post('topics') ? Input::post('topics') : [];
if (empty($topics)) {
throw new Error(__('No topics selected'), 400);
}
if ($newFid = Input::post('move_to_forum')) {
$topics = explode(',', $topics);
$topicModel->moveTo($args['id'], $newFid, $topics);
return Router::redirect(Router::pathFor('Forum', ['id' => $newFid, 'name' => $args['name']]), __('Move topics redirect'));
}
// Check if there are enough forums to move the topic
if (!$topicModel->checkMove()) {
throw new Error(__('Nowhere to move'), 403);
}
return View::setPageInfo([
'action' => 'multi',
'title' => [Utils::escape(ForumSettings::get('o_board_title')), __('Moderate')],
'active_page' => 'moderate',
'id' => $args['id'],
'topics' => implode(',', array_map('intval', array_keys($topics))),
'list_forums' => $topicModel->getForumListMove($args['id']),
]
)->addTemplate('@forum/moderate/move_topics')->display();
}
// Merge two or more topics
elseif (Input::post('merge_topics') || Input::post('merge_topics_comply')) {
if (Input::post('merge_topics_comply')) {
$this->model->merge($args['id']);
return Router::redirect(Router::pathFor('Forum', ['id' => $args['id'], 'name' => $args['name']]), __('Merge topics redirect'));
}
$topics = Input::post('topics') ? Input::post('topics') : [];
if (count($topics) < 2) {
throw new Error(__('Not enough topics selected'), 400);
}
return View::setPageInfo([
'title' => [Utils::escape(ForumSettings::get('o_board_title')), __('Moderate')],
'active_page' => 'moderate',
'id' => $args['id'],
'topics' => $topics,
]
)->addTemplate('@forum/moderate/merge_topics')->display();
}
// Delete one or more topics
elseif (Input::post('delete_topics') || Input::post('delete_topics_comply')) {
$topics = Input::post('topics') ? Input::post('topics') : [];
if (empty($topics)) {
throw new Error(__('No topics selected'), 400);
}
if (Input::post('delete_topics_comply')) {
$this->model->delete($topics, $args['id']);
return Router::redirect(Router::pathFor('Forum', ['id' => $args['id'], 'name' => $args['name']]), __('Delete topics redirect'));
}
return View::setPageInfo([
'title' => [Utils::escape(ForumSettings::get('o_board_title')), __('Moderate')],
'active_page' => 'moderate',
'id' => $args['id'],
'topics' => $topics,
]
)->addTemplate('@forum/moderate/delete_topics')->display();
}
// Open or close one or more topics
elseif (Input::post('open') || Input::post('close')) {
$action = (Input::post('open')) ? 0 : 1;
// There could be an array of topic IDs in $_pOST
if (Input::post('open') || Input::post('close')) {
$topics = Input::post('topics') ? @array_map('intval', @array_keys(Input::post('topics'))) : [];
if (empty($topics)) {
throw new Error(__('No topics selected'), 400);
}
$this->model->closeMultiple($action, $topics);
$redirectMsg = ($action) ? __('Close topics redirect') : __('Open topics redirect');
return Router::redirect(Router::pathFor('moderateForum', ['id' => $args['id'], 'name' => $args['name'], 'page' => $args['page']]), $redirectMsg);
}
}
// Stick or unstick one or more topics
elseif (Input::post('stick') || Input::post('unstick')) {
$action = (Input::post('stick')) ? 1 : 0;
// There could be an array of topic IDs in $_pOST
if (Input::post('stick') || Input::post('unstick')) {
$topics = Input::post('topics') ? @array_map('intval', @array_keys(Input::post('topics'))) : [];
if (empty($topics)) {
throw new Error(__('No topics selected'), 400);
}
$this->model->stickMultiple($action, $topics);
$redirectMsg = ($action) ? __('Stick topics redirect') : __('Unstick topics redirect');
return Router::redirect(Router::pathFor('moderateForum', ['id' => $args['id'], 'name' => $args['name'], 'page' => $args['page']]), $redirectMsg);
}
}
}
}