aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/report.php
blob: 850c6158e193e4cfa6d1666ba24331529c6ada4b (plain)
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<?php
/** 
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2005 phpBB Group 
* @license http://opensource.org/licenses/gpl-license.php GNU Public License 
*
*/

/**
*/
define('IN_PHPBB', true);
$phpbb_root_path = './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.'.$phpEx);
include($phpbb_root_path . 'includes/functions_display.'.$phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('mcp');

// Report PM or Post?
$id = request_var('p', request_var('pm', 0));
$report_post = (request_var('p', 0)) ? true : false;
$reason_id = request_var('reason_id', 0);
$user_notify = (!empty($_REQUEST['notify']) && $user->data['is_registered']) ? true : false;
$report_text = request_var('report_text', '');

if (!$id)
{
	trigger_error('INVALID_MODE');
}

$redirect_url = ($report_post) ? "{$phpbb_root_path}viewtopic.$phpEx$SID&p=$id#p$id" : "{$phpbb_root_path}ucp.$phpEx$SID&i=pm&p=$id";

// Has the report been cancelled?
if (isset($_POST['cancel']))
{
	redirect($redirect_url);
}

// Grab all relevant data
if ($report_post)
{
	$sql = 'SELECT f.*, t.*, p.*
		FROM ' . POSTS_TABLE . ' p, ' . TOPICS_TABLE . ' t, ' . FORUMS_TABLE . " f
		WHERE p.post_id = $id
			AND p.topic_id = t.topic_id
			AND p.forum_id = f.forum_id";
}
else
{
	// Only the user itself is able to report his Private Messages
	$sql = 'SELECT p.*, t.*
		FROM ' . PRIVMSGS_TABLE . ' p, ' . PRIVMSGS_TO_TABLE . " t
		WHERE t.msg_id = $id
			AND t.user_id = " . $user->data['user_id'] . '
			AND t.msg_id = p.msg_id';
}
$result = $db->sql_query($sql);

if (!($report_data = $db->sql_fetchrow($result)))
{
	$message = ($report_post) ? $user->lang['POST_NOT_EXIST'] : $user->lang['PM_NOT_EXIST'];
	trigger_error($message);
}

if ($report_post)
{
	$forum_id = $report_data['forum_id'];
	$topic_id = $report_data['topic_id'];

	// Check required permissions
	$acl_check_ary = array('f_list' => 'POST_NOT_EXIST', 'f_read' => 'USER_CANNOT_READ', 'f_report' => 'USER_CANNOT_REPORT');
	
	foreach ($acl_check_ary as $acl => $error)
	{
		if (!$auth->acl_get($acl, $forum_id))
		{
			trigger_error($error);
		}
	}
	unset($acl_check_ary);
}
else
{
	if (!$config['auth_report_pm'] || !$auth->acl_get('u_pm_report'))
	{
		trigger_error('USER_CANNOT_REPORT');
	}
}

// Check if the post has already been reported by this user
$sql = 'SELECT *
	FROM ' . REPORTS_TABLE . '
	WHERE ' . (($report_post) ? "post_id = $id" : "msg_id = $id") . '
		AND user_id = ' . $user->data['user_id'];
$result = $db->sql_query($sql);

if ($row = $db->sql_fetchrow($result))
{
	if ($user->data['is_registered'])
	{
		// A report exists, extract $row if we're going to display the form
		if ($reason_id)
		{
			$report_id = (int) $row['report_id'];
		}
		else
		{
			// Overwrite set variables
			extract($row);
		}
	}
	else
	{
		trigger_error($user->lang['ALREADY_REPORTED'] . '<br /><br />' . sprintf($user->lang[(($report_post) ? 'RETURN_TOPIC' : 'RETURN_MESSAGE')], '<a href="' . $redirect_url . '">', '</a>'));
	}
}
else
{
	$report_id = 0;
}

// Has the report been confirmed?
if (isset($_POST['submit']) && $reason_id)
{
	$sql = 'SELECT reason_name
		FROM ' . REASONS_TABLE . " 
		WHERE reason_id = $reason_id";
	$result = $db->sql_query($sql);

	if (!($row = $db->sql_fetchrow($result)) || (!$report_text && $row['reason_name'] == 'other'))
	{
		trigger_error('EMPTY_REPORT');
	}
	$db->sql_freeresult($result);

	$reason_desc = (!empty($user->lang['report_reasons']['DESCRIPTION'][$row['reason_name']])) ? $user->lang['report_reasons']['DESCRIPTION'][$row['reason_name']] : $row['reason_name'];

	$sql_ary = array(
		'reason_id'		=> (int) $reason_id,
		'post_id'		=> ($report_post) ? $id : 0,
		'msg_id'		=> ($report_post) ? 0 : $id,
		'user_id'		=> (int) $user->data['user_id'],
		'user_notify'	=> (int) $user_notify,
		'report_time'	=> (int) time(),
		'report_text'	=> (string) $report_text
	);

	if ($report_id)
	{
		$sql = 'UPDATE ' . REPORTS_TABLE . '
			SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
			WHERE report_id = ' . $report_id;
		$db->sql_query($sql);
	}
	else
	{
		$sql = 'INSERT INTO ' . REPORTS_TABLE . ' ' . 
			$db->sql_build_array('INSERT', $sql_ary);
		$db->sql_query($sql);
		$report_id = $db->sql_nextid();
	}

	if ($report_post)
	{
		if (!$report_data['post_reported'])
		{
			$sql = 'UPDATE ' . POSTS_TABLE . ' 
				SET post_reported = 1 
				WHERE post_id = ' . $id;
			$db->sql_query($sql);
		}

		if (!$report_data['topic_reported'])
		{
			$sql = 'UPDATE ' . TOPICS_TABLE . ' 
				SET topic_reported = 1 
				WHERE topic_id = ' . $report_data['topic_id'];
			$db->sql_query($sql);
		}
	}
	else
	{
		if (!$report_data['message_reported'])
		{
			$sql = 'UPDATE ' . PRIVMSGS_TABLE . " 
				SET message_reported = 1 
				WHERE msg_id = $id";
			$db->sql_query($sql);
		}
	}

	// Send Notifications
	// PM: Reported Post is put into all admin's boxes (not notifying about 'this' PM)
	// All persons get notified about a new report, if notified by PM, send out email notifications too
	
	// Send notifications to moderators
	$acl_list = ($report_post) ? $auth->acl_get_list(false, array('m_', 'a_'), array(0, $report_data['forum_id'])) : $auth->acl_get_list(false, 'a_', 0);
	$notify_user = ($report_post) ? $acl_list[$report_data['forum_id']]['m_'] : array();
	$notify_user = array_unique(array_merge($notify_user, $acl_list[0]['a_']));
	unset($acl_list);

	// Send reported PM to responsible persons (admins)
	if (!$report_post)
	{
		foreach ($notify_user as $user_id)
		{
			$db->sql_query('INSERT INTO ' . PRIVMSGS_TO_TABLE . ' ' . $db->sql_build_array('INSERT', array(
				'msg_id'	=> (int) $id,
				'user_id'	=> (int) $user_id,
				'author_id'	=> (int) $report_data['author_id'],
				'folder_id'	=> PRIVMSGS_NO_BOX,
				'new'		=> 1,
				'unread'	=> 1,
				'forwarded'	=> 0))
			);
		}

		// Update Status
		$sql = 'UPDATE ' . USERS_TABLE . ' 
			SET user_new_privmsg = user_new_privmsg + 1, user_unread_privmsg = user_unread_privmsg + 1
			WHERE user_id IN (' . implode(', ', $notify_user) . ')';
		$db->sql_query($sql);
	}

	// How to notify them?
	$sql = 'SELECT user_id, username, user_options, user_lang, user_email, user_notify_type, user_jabber 
		FROM ' . USERS_TABLE . '
		WHERE user_id IN (' . implode(', ', $notify_user) . ')';
	$result = $db->sql_query($sql);

	$notify_user = array();
	while ($row = $db->sql_fetchrow($result))
	{
		$notify_user[$row['user_id']] = array(
			'name'	=> $row['username'],
			'email' => $row['user_email'],
			'jabber'=> $row['user_jabber'],
			'lang'	=> $row['user_lang'],
			'notify_type'	=> $row['user_notify_type'],
			
			'pm'	=> $user->optionget('report_pm_notify', $row['user_options'])
		);
	}
	$db->sql_freeresult($result);

	$report_data = array(
		'id'		=> $id,
		'report_id'	=> $report_id,
		'reporter'	=> $user->data['username'],
		'reason'	=> $reason_desc,
		'text'		=> $report_text,
		'subject'	=> ($report_post) ? $report_data['post_subject'] : $report_data['message_subject'],
		'view_post'	=> ($report_post) ? "viewtopic.$phpEx?f={$report_data['forum_id']}&t={$report_data['topic_id']}&p=$id&e=$id" : ''
	);

	report_notification($notify_user, $report_post, $report_data);

	meta_refresh(3, $redirect_url);

	$message = $user->lang[(($report_post) ? 'POST' : 'MESSAGE') . '_REPORTED_SUCCESS'] . '<br /><br />' . sprintf($user->lang[(($report_post) ? 'RETURN_TOPIC' : 'RETURN_MESSAGE')], '<a href="' . $redirect_url . '">', '</a>');
	trigger_error($message);
}


// Generate the form
$sql = 'SELECT * 
	FROM ' . REASONS_TABLE . ' 
	ORDER BY reason_priority ASC';
$result = $db->sql_query($sql);

while ($row = $db->sql_fetchrow($result))
{
	$row['reason_name'] = strtoupper($row['reason_name']);

	$reason_title = (!empty($user->lang['report_reasons']['TITLE'][$row['reason_name']])) ? $user->lang['report_reasons']['TITLE'][$row['reason_name']] : ucwords(str_replace('_', ' ', $row['reason_name']));

	$reason_desc = (!empty($user->lang['report_reasons']['DESCRIPTION'][$row['reason_name']])) ? $user->lang['report_reasons']['DESCRIPTION'][$row['reason_name']] : $row['reason_description'];

	$template->assign_block_vars('reason', array(
		'ID'			=>	$row['reason_id'],
		'NAME'			=>	htmlspecialchars($reason_title),
		'DESCRIPTION'	=>	htmlspecialchars($reason_desc),
		'S_SELECTED'	=>	($row['reason_id'] == $reason_id) ? true : false)
	);
}

$u_report = ($report_post) ? "p=$id" : "pm=$id";

$template->assign_vars(array(
	'REPORT_TEXT'		=> $report_text,
	'S_REPORT_ACTION'	=> "{$phpbb_root_path}report.$phpEx$SID&amp;$u_report" . (($report_id) ? "&amp;report_id=$report_id" : ''),

	'S_NOTIFY'			=> (!empty($user_notify)) ? true : false,
	'S_CAN_NOTIFY'		=> ($user->data['is_registered']) ? true : false,
	'S_REPORT_POST'		=> $report_post)
);

if ($report_post)
{
	generate_forum_nav($report_data);
}

// Start output of page
$page_title = ($report_post) ? $user->lang['REPORT_POST'] : $user->lang['REPORT_MESSAGE'];
page_header($page_title);

$template->set_filenames(array(
	'body' => 'report_body.html')
);

page_footer();

function report_notification($notify_user, $report_post, $report_data)
{
	global $config, $phpbb_root_path, $phpEx;

	include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
	include_once($phpbb_root_path . 'includes/functions_privmsgs.' . $phpEx);
	$messenger = new messenger();

	$email_sig = str_replace('<br />', "\n", "-- \n" . $config['board_email_sig']);
	$email_template = ($report_post) ? 'new_report_post' : 'new_report_pm';
	$view_report_url = ($report_post) ? "mcp.$phpEx?i=queue&r=" . $report_data['report_id'] : "ucp.$phpEx?i=pm&p=" . $report_data['id'] . "&r=" . $report_data['report_id'];

	foreach ($notify_user as $user_id => $notify_row)
	{
		// Send notification by email
		if (!$notify_row['pm'])
		{
			$messenger->to($notify_row['email'], $notify_row['name']);
			$messenger->im($notify_row['jabber'], $notify_row['name']);
			$messenger->replyto($config['board_email']);

			$messenger->template($email_template, $notify_row['lang']);

			$messenger->assign_vars(array(
				'EMAIL_SIG'		=> $email_sig,
				'SITENAME'		=> $config['sitename'],
				'USERNAME'		=> $notify_row['name'],
				'SUBJECT'		=> $report_data['subject'],
				'REPORTER'		=> $report_data['reporter'],

				'REPORT_REASON'	=> $report_data['reason'],
				'REPORT_TEXT'	=> $report_data['text'],

				'U_VIEW_REPORT'	=> generate_board_url() . '/' . $view_report_url,
				'U_VIEW_POST'	=> generate_board_url() . '/' . $report_data['view_post'])
			);

			$messenger->send($notify_row['notify_type']);
			$messenger->reset();

			$messenger->save_queue();
		}
		else
		{
			// Use messenger for getting the correct message, we use the email template
			$messenger->template($email_template, $notify_row['lang']);
			
			$messenger->assign_vars(array(
				'EMAIL_SIG'		=> $email_sig,
				'SITENAME'		=> $config['sitename'],
				'USERNAME'		=> $notify_row['name'],
				'SUBJECT'		=> $report_data['subject'],
				'REPORTER'		=> $report_data['reporter'],

				'REPORT_REASON'	=> $report_data['reason'],
				'REPORT_TEXT'	=> $report_data['text'],

				'U_VIEW_REPORT'	=> generate_board_url() . '/' . $view_report_url)
			);

			// break the sending process...
			$messenger->send(false, true);
			$messenger->reset();
			
			// do not put in reporters outbox
			submit_pm('post', $report_data['subject'], '', array(), array(), array(
				'address_list'		=> array('u' => array($user_id => 'to')),
				'from_user_id'		=> $user->data['user_id'],
				'from_user_ip'		=> $user->ip,
				'from_username'		=> $user->data['username'],
				'icon_id'			=> 0,
				'enable_bbcode' 	=> 0,
				'enable_smilies' 	=> 0,
				'enable_magic_url' 	=> 1,
				'enable_sig' 		=> 0,
				'message_md5'		=> md5($messenger->msg),
				'bbcode_bitfield'	=> 0,
				'bbcode_uid'		=> 0,
				'attachment_data'	=> array(),
				'filename_data'		=> array(),
				'message'			=> $messenger->msg
				), true, false);
		}
	}
	unset($messenger);
}

?>