forked from featherbb/featherbb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.php
More file actions
387 lines (307 loc) · 14.2 KB
/
misc.php
File metadata and controls
387 lines (307 loc) · 14.2 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
/**
* Copyright (C) 2015 FeatherBB
* based on code by (C) 2008-2012 FluxBB
* and Rickard Andersson (C) 2002-2008 PunBB
* License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
*/
namespace model;
use DB;
class misc
{
public function __construct()
{
$this->feather = \Slim\Slim::getInstance();
$this->start = $this->feather->start;
$this->config = $this->feather->config;
$this->user = $this->feather->user;
$this->request = $this->feather->request;
}
public function update_last_visit()
{
DB::for_table('users')->where('id', $this->user->id)
->find_one()
->set('last_visit', $this->user->logged)
->save();
}
public function get_info_mail($recipient_id)
{
global $lang_common;
$select_get_info_mail = array('username', 'email', 'email_setting');
$mail = DB::for_table('users')
->select_many($select_get_info_mail)
->where('id', $recipient_id)
->find_one();
if (!$mail) {
message($lang_common['Bad request'], '404');
}
$mail['recipient'] = $mail['username'];
$mail['recipient_email'] = $mail['email'];
return $mail;
}
public function send_email($mail, $id)
{
global $lang_misc;
// Clean up message and subject from POST
$subject = feather_trim($this->request->post('req_subject'));
$message = feather_trim($this->request->post('req_message'));
if ($subject == '') {
message($lang_misc['No email subject']);
} elseif ($message == '') {
message($lang_misc['No email message']);
}
// Here we use strlen() not feather_strlen() as we want to limit the post to FEATHER_MAX_POSTSIZE bytes, not characters
elseif (strlen($message) > FEATHER_MAX_POSTSIZE) {
message($lang_misc['Too long email message']);
}
if ($this->user->last_email_sent != '' && (time() - $this->user->last_email_sent) < $this->user->g_email_flood && (time() - $this->user->last_email_sent) >= 0) {
message(sprintf($lang_misc['Email flood'], $this->user->g_email_flood, $this->user->g_email_flood - (time() - $this->user->last_email_sent)));
}
// Load the "form email" template
$mail_tpl = trim(file_get_contents(FEATHER_ROOT.'lang/'.$this->user->language.'/mail_templates/form_email.tpl'));
// The first row contains the subject
$first_crlf = strpos($mail_tpl, "\n");
$mail_subject = feather_trim(substr($mail_tpl, 8, $first_crlf-8));
$mail_message = feather_trim(substr($mail_tpl, $first_crlf));
$mail_subject = str_replace('<mail_subject>', $subject, $mail_subject);
$mail_message = str_replace('<sender>', $this->user->username, $mail_message);
$mail_message = str_replace('<board_title>', $this->config['o_board_title'], $mail_message);
$mail_message = str_replace('<mail_message>', $message, $mail_message);
$mail_message = str_replace('<board_mailer>', $this->config['o_board_title'], $mail_message);
require_once FEATHER_ROOT.'include/email.php';
pun_mail($mail['recipient_email'], $mail_subject, $mail_message, $this->user->email, $this->user->username);
DB::for_table('users')->where('id', $this->user->id)
->find_one()
->set('last_email_sent', time())
->save();
// Try to determine if the data in redirect_url is valid (if not, we redirect to index.php after the email is sent)
//$redirect_url = validate_redirect($this->request->post('redirect_url'), 'index.php');
redirect(get_base_url(), $lang_misc['Email sent redirect']);
}
public function get_redirect_url($recipient_id)
{
// Try to determine if the data in HTTP_REFERER is valid (if not, we redirect to the user's profile after the email is sent)
// TODO
if ($this->request->getReferrer()) {
$redirect_url = validate_redirect($this->request->getReferrer(), null);
}
if (!isset($redirect_url)) {
$redirect_url = get_link('user/'.$recipient_id.'/');
} elseif (preg_match('%viewtopic\.php\?pid=(\d+)$%', $redirect_url, $matches)) {
$redirect_url .= '#p'.$matches[1];
}
return $redirect_url;
}
public function insert_report($post_id)
{
global $lang_misc, $lang_common;
// Clean up reason from POST
$reason = feather_linebreaks(feather_trim($this->request->post('req_reason')));
if ($reason == '') {
message($lang_misc['No reason']);
} elseif (strlen($reason) > 65535) { // TEXT field can only hold 65535 bytes
message($lang_misc['Reason too long']);
}
if ($this->user->last_report_sent != '' && (time() - $this->user->last_report_sent) < $this->user->g_report_flood && (time() - $this->user->last_report_sent) >= 0) {
message(sprintf($lang_misc['Report flood'], $this->user->g_report_flood, $this->user->g_report_flood - (time() - $this->user->last_report_sent)));
}
// Get the topic ID
$topic = DB::for_table('posts')->select('topic_id')
->where('id', $post_id)
->find_one();
if (!$topic) {
message($lang_common['Bad request'], '404');
}
$select_report = array('subject', 'forum_id');
// Get the subject and forum ID
$report = DB::for_table('topics')->select_many($select_report)
->where('id', $topic['topic_id'])
->find_one();
if (!$report) {
message($lang_common['Bad request'], '404');
}
// Should we use the internal report handling?
if ($this->config['o_report_method'] == '0' || $this->config['o_report_method'] == '2') {
$insert_report = array(
'post_id' => $post_id,
'topic_id' => $topic['topic_id'],
'forum_id' => $report['forum_id'],
'reported_by' => $this->user->id,
'created' => time(),
'message' => $reason,
);
// Insert the report
DB::for_table('reports')
->create()
->set($insert_report)
->save();
}
// Should we email the report?
if ($this->config['o_report_method'] == '1' || $this->config['o_report_method'] == '2') {
// We send it to the complete mailing-list in one swoop
if ($this->config['o_mailing_list'] != '') {
// Load the "new report" template
$mail_tpl = trim(file_get_contents(FEATHER_ROOT.'lang/'.$this->user->language.'/mail_templates/new_report.tpl'));
// The first row contains the subject
$first_crlf = strpos($mail_tpl, "\n");
$mail_subject = trim(substr($mail_tpl, 8, $first_crlf-8));
$mail_message = trim(substr($mail_tpl, $first_crlf));
$mail_subject = str_replace('<forum_id>', $report['forum_id'], $mail_subject);
$mail_subject = str_replace('<topic_subject>', $report['subject'], $mail_subject);
$mail_message = str_replace('<username>', $this->user->username, $mail_message);
$mail_message = str_replace('<post_url>', get_link('post/'.$post_id.'/#p'.$post_id), $mail_message);
$mail_message = str_replace('<reason>', $reason, $mail_message);
$mail_message = str_replace('<board_mailer>', $this->config['o_board_title'], $mail_message);
require FEATHER_ROOT.'include/email.php';
pun_mail($this->config['o_mailing_list'], $mail_subject, $mail_message);
}
}
DB::for_table('users')->where('id', $this->user->id)
->find_one()
->set('last_report_sent', time())
->save();
redirect(get_link('forum/'.$report['forum_id'].'/'.url_friendly($report['subject']).'/'), $lang_misc['Report redirect']);
}
public function get_info_report($post_id)
{
global $lang_common;
$select_get_info_report = array('fid' => 'f.id', 'f.forum_name', 'tid' => 't.id', 't.subject');
$where_get_info_report = array(
array('fp.read_forum' => 'IS NULL'),
array('fp.read_forum' => '1')
);
$cur_post = DB::for_table('posts')
->table_alias('p')
->select_many($select_get_info_report)
->inner_join('topics', array('t.id', '=', 'p.topic_id'), 't')
->inner_join('forums', array('f.id', '=', 't.forum_id'), 'f')
->left_outer_join('forum_perms', array('fp.forum_id', '=', 'f.id'), 'fp')
->left_outer_join('forum_perms', array('fp.group_id', '=', $this->user->g_id), null, true)
->where_any_is($where_get_info_report)
->where('p.id', $post_id)
->find_one();
if (!$cur_post) {
message($lang_common['Bad request'], '404');
}
return $cur_post;
}
public function subscribe_topic($topic_id)
{
global $lang_common, $lang_misc;
if ($this->config['o_topic_subscriptions'] != '1') {
message($lang_common['No permission'], '403');
}
// Make sure the user can view the topic
$where_subscribe_topic = array(
array('fp.read_forum' => 'IS NULL'),
array('fp.read_forum' => '1')
);
$authorized = DB::for_table('topics')
->table_alias('t')
->left_outer_join('forum_perms', array('fp.forum_id', '=', 't.forum_id'), 'fp')
->left_outer_join('forum_perms', array('fp.group_id', '=', $this->user->g_id), null, true)
->where_any_is($where_subscribe_topic)
->where('t.id', $topic_id)
->where_null('t.moved_to')
->find_one();
if (!$authorized) {
message($lang_common['Bad request'], '404');
}
$is_subscribed = DB::for_table('topic_subscriptions')
->where('user_id', $this->user->id)
->where('topic_id', $topic_id)
->find_one();
if ($is_subscribed) {
message($lang_misc['Already subscribed topic']);
}
$insert_subscribe_topic = array(
'user_id' => $this->user->id,
'topic_id' => $topic_id
);
// Insert the subscription
DB::for_table('topic_subscriptions')
->create()
->set($insert_subscribe_topic)
->save();
redirect(get_link('topic/'.$topic_id.'/'), $lang_misc['Subscribe redirect']);
}
public function unsubscribe_topic($topic_id)
{
global $lang_common, $lang_misc;
if ($this->config['o_topic_subscriptions'] != '1') {
message($lang_common['No permission'], '403');
}
$is_subscribed = DB::for_table('topic_subscriptions')
->where('user_id', $this->user->id)
->where('topic_id', $topic_id)
->find_one();
if (!$is_subscribed) {
message($lang_misc['Not subscribed topic']);
}
// Delete the subscription
DB::for_table('topic_subscriptions')
->where('user_id', $this->user->id)
->where('topic_id', $topic_id)
->delete_many();
redirect(get_link('topic/'.$topic_id.'/'), $lang_misc['Unsubscribe redirect']);
}
public function unsubscribe_forum($forum_id)
{
global $lang_common, $lang_misc;
if ($this->config['o_forum_subscriptions'] != '1') {
message($lang_common['No permission'], '403');
}
$is_subscribed = DB::for_table('forum_subscriptions')
->where('user_id', $this->user->id)
->where('forum_id', $forum_id)
->find_one();
if (!$is_subscribed) {
message($lang_misc['Not subscribed forum']);
}
// Delete the subscription
DB::for_table('forum_subscriptions')
->where('user_id', $this->user->id)
->where('forum_id', $forum_id)
->delete_many();
redirect(get_link('forum/'.$forum_id.'/'), $lang_misc['Unsubscribe redirect']);
}
public function subscribe_forum($forum_id)
{
global $lang_common, $lang_misc;
if ($this->config['o_forum_subscriptions'] != '1') {
message($lang_common['No permission'], '403');
}
// Make sure the user can view the forum
$where_subscribe_forum = array(
array('fp.read_forum' => 'IS NULL'),
array('fp.read_forum' => '1')
);
$authorized = DB::for_table('forums')
->table_alias('f')
->left_outer_join('forum_perms', array('fp.forum_id', '=', 'f.id'), 'fp')
->left_outer_join('forum_perms', array('fp.group_id', '=', $this->user->g_id), null, true)
->where_any_is($where_subscribe_forum)
->where('f.id', $forum_id)
->find_one();
if (!$authorized) {
message($lang_common['Bad request'], '404');
}
$is_subscribed = DB::for_table('forum_subscriptions')
->where('user_id', $this->user->id)
->where('forum_id', $forum_id)
->find_one();
if ($is_subscribed) {
message($lang_misc['Already subscribed forum']);
}
$insert_subscribe_forum = array(
'user_id' => $this->user->id,
'forum_id' => $forum_id
);
// Insert the subscription
DB::for_table('forum_subscriptions')
->create()
->set($insert_subscribe_forum)
->save();
redirect(get_link('forum/'.$forum_id.'/'), $lang_misc['Subscribe redirect']);
}
}