Register
phpBB.com Wiki · Home Projects Help

root / trunk / phpBB / posting.php

<
1
<?php
2
/**
3
*
4
* @package phpBB3
5
* @version $Id: posting.php 9064 2008-11-13 13:04:54Z toonarmy $
6
* @copyright (c) 2005 phpBB Group
7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
8
*
9
*/
10
11
/**
12
* @ignore
13
*/
14
define('IN_PHPBB', true);
15
if (!defined('PHPBB_ROOT_PATH')) define('PHPBB_ROOT_PATH', './');
16
if (!defined('PHP_EXT')) define('PHP_EXT', substr(strrchr(__FILE__, '.'), 1));
17
include(PHPBB_ROOT_PATH . 'common.' . PHP_EXT);
18
include(PHPBB_ROOT_PATH . 'includes/functions_posting.' . PHP_EXT);
19
include(PHPBB_ROOT_PATH . 'includes/functions_display.' . PHP_EXT);
20
include(PHPBB_ROOT_PATH . 'includes/message_parser.' . PHP_EXT);
21
22
23
// Start session management
24
$user->session_begin();
25
$auth->acl($user->data);
26
27
28
// Grab only parameters needed here
29
$post_id	= request_var('p', 0);
30
$topic_id	= request_var('t', 0);
31
$forum_id	= request_var('f', 0);
32
$draft_id	= request_var('d', 0);
33
$lastclick	= request_var('lastclick', 0);
34
35
$submit		= (isset($_POST['post'])) ? true : false;
36
$preview	= (isset($_POST['preview'])) ? true : false;
37
$save		= (isset($_POST['save'])) ? true : false;
38
$load		= (isset($_POST['load'])) ? true : false;
39
$delete		= (isset($_POST['delete'])) ? true : false;
40
$cancel		= (isset($_POST['cancel']) && !isset($_POST['save'])) ? true : false;
41
42
$refresh	= (isset($_POST['add_file']) || isset($_POST['delete_file']) || isset($_POST['cancel_unglobalise']) || $save || $load) ? true : false;
43
$mode		= ($delete && !$preview && !$refresh && $submit) ? 'delete' : request_var('mode', '');
44
45
$error = $post_data = array();
46
$current_time = time();
47
48
if ($config['enable_post_confirm'] && !$user->data['is_registered'])
49
{
50
	include(PHPBB_ROOT_PATH . 'includes/captcha/captcha_factory.' . PHP_EXT);
51
	$captcha = phpbb_captcha_factory::get_instance($config['captcha_plugin']);
52
	$captcha->init(CONFIRM_POST);
53
}
54
	
55
// Was cancel pressed? If so then redirect to the appropriate page
56
if ($cancel || ($current_time - $lastclick < 2 && $submit))
57
{
58
	$redirect = ($post_id) ? append_sid('viewtopic', 'p=' . $post_id) . '#p' . $post_id : (($topic_id) ? append_sid('viewtopic', 't=' . $topic_id) : (($forum_id) ? append_sid('viewforum', 'f=' . $forum_id) : append_sid('index')));
59
	redirect($redirect);
60
}
61
62
if (in_array($mode, array('post', 'reply', 'quote', 'edit', 'delete')) && !$forum_id)
63
{
64
	trigger_error('NO_FORUM');
65
}
66
67
// We need to know some basic information in all cases before we do anything.
68
switch ($mode)
69
{
70
	case 'post':
71
		$sql = 'SELECT *
72
			FROM ' . FORUMS_TABLE . "
73
			WHERE forum_id = $forum_id";
74
	break;
75
76
	case 'bump':
77
	case 'reply':
78
		if (!$topic_id)
79
		{
80
			trigger_error('NO_TOPIC');
81
		}
82
83
		$sql = 'SELECT f.*, t.*
84
			FROM ' . TOPICS_TABLE . ' t, ' . FORUMS_TABLE . " f
85
			WHERE t.topic_id = $topic_id
86
				AND (f.forum_id = t.forum_id
87
					OR f.forum_id = $forum_id)";
88
	break;
89
90
	case 'quote':
91
	case 'edit':
92
	case 'delete':
93
		if (!$post_id)
94
		{
95
			$user->setup('posting');
96
			trigger_error('NO_POST');
97
		}
98
99
		$sql = 'SELECT f.*, t.*, p.*, u.username, u.username_clean, u.user_sig, u.user_sig_bbcode_uid, u.user_sig_bbcode_bitfield
100
			FROM ' . POSTS_TABLE . ' p, ' . TOPICS_TABLE . ' t, ' . FORUMS_TABLE . ' f, ' . USERS_TABLE . " u
101
			WHERE p.post_id = $post_id
102
				AND t.topic_id = p.topic_id
103
				AND u.user_id = p.poster_id
104
				AND (f.forum_id = t.forum_id
105
					OR f.forum_id = $forum_id)";
106
	break;
107
108
	case 'smilies':
109
		$sql = '';
110
		generate_smilies('window', $forum_id);
111
	break;
112
113
	case 'popup':
114
		if ($forum_id)
115
		{
116
			$sql = 'SELECT forum_style
117
				FROM ' . FORUMS_TABLE . '
118
				WHERE forum_id = ' . $forum_id;
119
		}
120
		else
121
		{
122
			upload_popup();
123
			return;
124
		}
125
	break;
126
127
	default:
128
		$sql = '';
129
	break;
130
}
131
132
if (!$sql)
133
{
134
	$user->setup('posting');
135
	trigger_error('NO_POST_MODE');
136
}
137
138
$result = $db->sql_query($sql);
139
$post_data = $db->sql_fetchrow($result);
140
$db->sql_freeresult($result);
141
142
if (!$post_data)
143
{
144
	if (!($mode == 'post' || $mode == 'bump' || $mode == 'reply'))
145
	{
146
		$user->setup('posting');
147
	}
148
	trigger_error(($mode == 'post' || $mode == 'bump' || $mode == 'reply') ? 'NO_TOPIC' : 'NO_POST');
149
}
150
151
if ($mode == 'popup')
152
{
153
	upload_popup($post_data['forum_style']);
154
	return;
155
}
156
157
$user->setup(array('posting', 'mcp', 'viewtopic'), $post_data['forum_style']);
158
159
// Use post_row values in favor of submitted ones...
160
$forum_id	= (!empty($post_data['forum_id'])) ? (int) $post_data['forum_id'] : (int) $forum_id;
161
$topic_id	= (!empty($post_data['topic_id'])) ? (int) $post_data['topic_id'] : (int) $topic_id;
162
$post_id	= (!empty($post_data['post_id'])) ? (int) $post_data['post_id'] : (int) $post_id;
163
164
// Need to login to passworded forum first?
165
if ($post_data['forum_password'])
166
{
167
	login_forum_box(array(
168
		'forum_id'			=> $forum_id,
169
		'forum_password'	=> $post_data['forum_password'])
170
	);
171
}
172
173
// Check permissions
174
if ($user->data['is_bot'])
175
{
176
	redirect(append_sid('index'));
177
}
178
179
// Is the user able to read within this forum?
180
if (!$auth->acl_get('f_read', $forum_id))
181
{
182
	if ($user->data['user_id'] != ANONYMOUS)
183
	{
184
		trigger_error('USER_CANNOT_READ');
185
	}
186
187
	login_box('', $user->lang['LOGIN_EXPLAIN_POST']);
188
}
189
190
// Permission to do the action asked?
191
$is_authed = false;
192
193
switch ($mode)
194
{
195
	case 'post':
196
		if ($auth->acl_get('f_post', $forum_id))
197
		{
198
			$is_authed = true;
199
		}
200
	break;
201
202
	case 'bump':
203
		if ($auth->acl_get('f_bump', $forum_id))
204
		{
205
			$is_authed = true;
206
		}
207
	break;
208
209
	case 'quote':
210
211
		$post_data['post_edit_locked'] = 0;
212
213
	// no break;
214
215
	case 'reply':
216
		if ($auth->acl_get('f_reply', $forum_id))
217
		{
218
			$is_authed = true;
219
		}
220
	break;
221
222
	case 'edit':
223
		if ($user->data['is_registered'] && $auth->acl_gets('f_edit', 'm_edit', $forum_id))
224
		{
225
			$is_authed = true;
226
		}
227
	break;
228
229
	case 'delete':
230
		if ($user->data['is_registered'] && $auth->acl_gets('f_delete', 'm_delete', $forum_id))
231
		{
232
			$is_authed = true;
233
		}
234
	break;
235
}
236
237
if (!$is_authed)
238
{
239
	$check_auth = ($mode == 'quote') ? 'reply' : $mode;
240
241
	if ($user->data['is_registered'])
242
	{
243
		trigger_error('USER_CANNOT_' . strtoupper($check_auth));
244
	}
245
246
	login_box('', $user->lang['LOGIN_EXPLAIN_' . strtoupper($mode)]);
247
}
248
249
// Is the user able to post within this forum?
250
if ($post_data['forum_type'] != FORUM_POST && in_array($mode, array('post', 'bump', 'quote', 'reply')))
251
{
252
	trigger_error('USER_CANNOT_FORUM_POST');
253
}
254
255
// Forum/Topic locked?
256
if (($post_data['forum_status'] == ITEM_LOCKED || (isset($post_data['topic_status']) && $post_data['topic_status'] == ITEM_LOCKED)) && !$auth->acl_get('m_edit', $forum_id))
257
{
258
	trigger_error(($post_data['forum_status'] == ITEM_LOCKED) ? 'FORUM_LOCKED' : 'TOPIC_LOCKED');
259
}
260
261
// Can we edit this post ... if we're a moderator with rights then always yes
262
// else it depends on editing times, lock status and if we're the correct user
263
if ($mode == 'edit' && !$auth->acl_get('m_edit', $forum_id))
264
{
265
	if ($user->data['user_id'] != $post_data['poster_id'])
266
	{
267
		trigger_error('USER_CANNOT_EDIT');
268
	}
269
270
	if (!($post_data['post_time'] > time() - ($config['edit_time'] * 60) || !$config['edit_time']))
271
	{
272
		trigger_error('CANNOT_EDIT_TIME');
273
	}
274
275
	if ($post_data['post_edit_locked'])
276
	{
277
		trigger_error('CANNOT_EDIT_POST_LOCKED');
278
	}
279
}
280
281
// Handle delete mode...
282
if ($mode == 'delete')
283
{
284
	handle_post_delete($forum_id, $topic_id, $post_id, $post_data);
285
	return;
286
}
287
288
// Handle bump mode...
289
if ($mode == 'bump')
290
{
291
	if ($bump_time = bump_topic_allowed($forum_id, $post_data['topic_bumped'], $post_data['topic_last_post_time'], $post_data['topic_poster'], $post_data['topic_last_poster_id'])
292
	   && check_link_hash(request_var('hash', ''),"topic_{$post_data['topic_id']}"))
293
	{
294
		$db->sql_transaction('begin');
295
296
		$sql = 'UPDATE ' . POSTS_TABLE . "
297
			SET post_time = $current_time
298
			WHERE post_id = {$post_data['topic_last_post_id']}
299
				AND topic_id = $topic_id";
300
		$db->sql_query($sql);
301
302
		$sql = 'UPDATE ' . TOPICS_TABLE . "
303
			SET topic_last_post_time = $current_time,
304
				topic_bumped = 1,
305
				topic_bumper = " . $user->data['user_id'] . "
306
			WHERE topic_id = $topic_id";
307
		$db->sql_query($sql);
308
309
		update_post_information('forum', $forum_id);
310
311
		$sql = 'UPDATE ' . USERS_TABLE . "
312
			SET user_lastpost_time = $current_time
313
			WHERE user_id = " . $user->data['user_id'];
314
		$db->sql_query($sql);
315
316
		$db->sql_transaction('commit');
317
318
		markread('post', $forum_id, $topic_id, $current_time);
319
320
		add_log('mod', $forum_id, $topic_id, 'LOG_BUMP_TOPIC', $post_data['topic_title']);
321
322
		$meta_url = append_sid('viewtopic', "f=$forum_id&amp;t=$topic_id&amp;p={$post_data['topic_last_post_id']}") . "#p{$post_data['topic_last_post_id']}";
323
		meta_refresh(3, $meta_url);
324
325
		$message = $user->lang['TOPIC_BUMPED'] . '<br /><br />' . sprintf($user->lang['VIEW_MESSAGE'], '<a href="' . $meta_url . '">', '</a>');
326
		$message .= '<br /><br />' . sprintf($user->lang['RETURN_FORUM'], '<a href="' . append_sid('viewforum', 'f=' . $forum_id) . '">', '</a>');
327
328
		trigger_error($message);
329
	}
330
331
	trigger_error('BUMP_ERROR');
332
}
333
334
// Subject length limiting to 60 characters if first post...
335
if ($mode == 'post' || ($mode == 'edit' && $post_data['topic_first_post_id'] == $post_data['post_id']))
336
{
337
	$template->assign_var('S_NEW_MESSAGE', true);
338
}
339
340
// Determine some vars
341
if (isset($post_data['poster_id']) && $post_data['poster_id'] == ANONYMOUS)
342
{
343
	$post_data['quote_username'] = (!empty($post_data['post_username'])) ? $post_data['post_username'] : $user->lang['GUEST'];
344
}
345
else
346
{
347
	$post_data['quote_username'] = isset($post_data['username']) ? $post_data['username'] : '';
348
}
349
350
$post_data['post_edit_locked']	= (isset($post_data['post_edit_locked'])) ? (int) $post_data['post_edit_locked'] : 0;
351
$post_data['post_subject']		= (in_array($mode, array('quote', 'edit'))) ? $post_data['post_subject'] : ((isset($post_data['topic_title'])) ? $post_data['topic_title'] : '');
352
$post_data['topic_time_limit']	= (isset($post_data['topic_time_limit'])) ? (($post_data['topic_time_limit']) ? (int) $post_data['topic_time_limit'] / 86400 : (int) $post_data['topic_time_limit']) : 0;
353
$post_data['poll_length']		= (!empty($post_data['poll_length'])) ? (int) $post_data['poll_length'] / 86400 : 0;
354
$post_data['poll_start']		= (!empty($post_data['poll_start'])) ? (int) $post_data['poll_start'] : 0;
355
$post_data['icon_id']			= (!isset($post_data['icon_id']) || in_array($mode, array('quote', 'reply'))) ? 0 : (int) $post_data['icon_id'];
356
$post_data['poll_options']		= array();
357
358
// Get Poll Data
359
if ($post_data['poll_start'])
360
{
361
	$sql = 'SELECT poll_option_text
362
		FROM ' . POLL_OPTIONS_TABLE . "
363
		WHERE topic_id = $topic_id
364
		ORDER BY poll_option_id";
365
	$result = $db->sql_query($sql);
366
367
	while ($row = $db->sql_fetchrow($result))
368
	{
369
		$post_data['poll_options'][] = trim($row['poll_option_text']);
370
	}
371
	$db->sql_freeresult($result);
372
}
373
374
$orig_poll_options_size = sizeof($post_data['poll_options']);
375
376
$message_parser = new parse_message();
377
378
if (isset($post_data['post_text']))
379
{
380
	$message_parser->message = &$post_data['post_text'];
381
	unset($post_data['post_text']);
382
}
383
384
// Set some default variables
385
$uninit = array('post_attachment' => 0, 'poster_id' => $user->data['user_id'], 'enable_magic_url' => 0, 'topic_status' => 0, 'topic_type' => POST_NORMAL, 'post_subject' => '', 'topic_title' => '', 'post_time' => 0, 'post_edit_reason' => '', 'notify_set' => 0);
386
387
foreach ($uninit as $var_name => $default_value)
388
{
389
	if (!isset($post_data[$var_name]))
390
	{
391
		$post_data[$var_name] = $default_value;
392
	}
393
}
394
unset($uninit);
395
396
// Always check if the submitted attachment data is valid and belongs to the user.
397
// Further down (especially in submit_post()) we do not check this again.
398
$message_parser->get_submitted_attachment_data($post_data['poster_id']);
399
400
if ($post_data['post_attachment'] && !$submit && !$refresh && !$preview && $mode == 'edit')
401
{
402
	// Do not change to SELECT *
403
	$sql = 'SELECT attach_id, is_orphan, attach_comment, real_filename
404
		FROM ' . ATTACHMENTS_TABLE . "
405
		WHERE post_msg_id = $post_id
406
			AND in_message = 0
407
			AND is_orphan = 0
408
		ORDER BY filetime DESC";
409
	$result = $db->sql_query($sql);
410
	$message_parser->attachment_data = array_merge($message_parser->attachment_data, $db->sql_fetchrowset($result));
411
	$db->sql_freeresult($result);
412
}
413
414
if ($post_data['poster_id'] == ANONYMOUS)
415
{
416
	$post_data['username'] = ($mode == 'quote' || $mode == 'edit') ? trim($post_data['post_username']) : '';
417
}
418
else
419
{
420
	$post_data['username'] = ($mode == 'quote' || $mode == 'edit') ? trim($post_data['username']) : '';
421
}
422
423
$post_data['enable_urls'] = $post_data['enable_magic_url'];
424
425
if ($mode != 'edit')
426
{
427
	$post_data['enable_sig']		= ($config['allow_sig'] && $user->optionget('attachsig')) ? true: false;
428
	$post_data['enable_smilies']	= ($config['allow_smilies'] && $user->optionget('smilies')) ? true : false;
429
	$post_data['enable_bbcode']		= ($config['allow_bbcode'] && $user->optionget('bbcode')) ? true : false;
430
	$post_data['enable_urls']		= true;
431
}
432
433
$post_data['enable_magic_url'] = $post_data['drafts'] = false;
434
435
// User own some drafts?
436
if ($user->data['is_registered'] && $auth->acl_get('u_savedrafts') && ($mode == 'reply' || $mode == 'post' || $mode == 'quote'))
437
{
438
	$sql = 'SELECT draft_id
439
		FROM ' . DRAFTS_TABLE . '
440
		WHERE user_id = ' . $user->data['user_id'] .
441
			(($forum_id) ? ' AND forum_id = ' . (int) $forum_id : '') .
442
			(($topic_id) ? ' AND topic_id = ' . (int) $topic_id : '') .
443
			(($draft_id) ? " AND draft_id <> $draft_id" : '');
444
	$result = $db->sql_query_limit($sql, 1);
445
446
	if ($db->sql_fetchrow($result))
447
	{
448
		$post_data['drafts'] = true;
449
	}
450
	$db->sql_freeresult($result);
451
}
452
453
$check_value = (($post_data['enable_bbcode']+1) << 8) + (($post_data['enable_smilies']+1) << 4) + (($post_data['enable_urls']+1) << 2) + (($post_data['enable_sig']+1) << 1);
454
455
// Check if user is watching this topic
456
if ($mode != 'post' && $config['allow_topic_notify'] && $user->data['is_registered'])
457
{
458
	$sql = 'SELECT topic_id
459
		FROM ' . TOPICS_WATCH_TABLE . '
460
		WHERE topic_id = ' . $topic_id . '
461
			AND user_id = ' . $user->data['user_id'];
462
	$result = $db->sql_query($sql);
463
	$post_data['notify_set'] = (int) $db->sql_fetchfield('topic_id');
464
	$db->sql_freeresult($result);
465
}
466
467
// Do we want to edit our post ?
468
if ($mode == 'edit' && $post_data['bbcode_uid'])
469
{
470
	$message_parser->bbcode_uid = $post_data['bbcode_uid'];
471
}
472
473
// HTML, BBCode, Smilies, Images and Flash status
474
$bbcode_status	= ($config['allow_bbcode'] && $auth->acl_get('f_bbcode', $forum_id)) ? true : false;
475
$smilies_status	= ($bbcode_status && $config['allow_smilies'] && $auth->acl_get('f_smilies', $forum_id)) ? true : false;
476
$img_status		= ($bbcode_status && $auth->acl_get('f_img', $forum_id)) ? true : false;
477
$url_status		= ($config['allow_post_links']) ? true : false;
478
$flash_status	= ($bbcode_status && $auth->acl_get('f_flash', $forum_id) && $config['allow_post_flash']) ? true : false;
479
$quote_status	= ($auth->acl_get('f_reply', $forum_id)) ? true : false;
480
481
// Save Draft
482
if ($save && $user->data['is_registered'] && $auth->acl_get('u_savedrafts') && ($mode == 'reply' || $mode == 'post' || $mode == 'quote'))
483
{
484
	$subject = utf8_normalize_nfc(request_var('subject', '', true));
485
	$subject = (!$subject && $mode != 'post') ? $post_data['topic_title'] : $subject;
486
	$message = utf8_normalize_nfc(request_var('message', '', true));
487
488
	if ($subject && $message)
489
	{
490
		if (confirm_box(true))
491
		{
492
			$sql = 'INSERT INTO ' . DRAFTS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
493
				'user_id'		=> (int) $user->data['user_id'],
494
				'topic_id'		=> (int) $topic_id,
495
				'forum_id'		=> (int) $forum_id,
496
				'save_time'		=> (int) $current_time,
497
				'draft_subject'	=> (string) $subject,
498
				'draft_message'	=> (string) $message)
499
			);
500
			$db->sql_query($sql);
501
502
			$meta_info = ($mode == 'post') ? append_sid('viewforum', 'f=' . $forum_id) : append_sid('viewtopic', "f=$forum_id&amp;t=$topic_id");
503
504
			meta_refresh(3, $meta_info);
505
506
			$message = $user->lang['DRAFT_SAVED'] . '<br /><br />';
507
			$message .= ($mode != 'post') ? sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $meta_info . '">', '</a>') . '<br /><br />' : '';
508
			$message .= sprintf($user->lang['RETURN_FORUM'], '<a href="' . append_sid('viewforum', 'f=' . $forum_id) . '">', '</a>');
509
510
			trigger_error($message);
511
		}
512
		else
513
		{
514
			$s_hidden_fields = build_hidden_fields(array(
515
				'mode'		=> $mode,
516
				'save'		=> true,
517
				'f'			=> $forum_id,
518
				't'			=> $topic_id,
519
				'subject'	=> $subject,
520
				'message'	=> $message,
521
				'attachment_data' => $message_parser->attachment_data,
522
				)
523
			);
524
525
			confirm_box(false, 'SAVE_DRAFT', $s_hidden_fields);
526
		}
527
	}
528
	else
529
	{
530
		if (utf8_clean_string($subject) === '')
531
		{
532
			$error[] = $user->lang['EMPTY_SUBJECT'];
533
		}
534
535
		if (utf8_clean_string($message) === '')
536
		{
537
			$error[] = $user->lang['TOO_FEW_CHARS'];
538
		}
539
	}
540
	unset($subject, $message);
541
}
542
543
// Load requested Draft
544
if ($draft_id && ($mode == 'reply' || $mode == 'quote' || $mode == 'post') && $user->data['is_registered'] && $auth->acl_get('u_savedrafts'))
545
{
546
	$sql = 'SELECT draft_subject, draft_message
547
		FROM ' . DRAFTS_TABLE . "
548
		WHERE draft_id = $draft_id
549
			AND user_id = " . $user->data['user_id'];
550
	$result = $db->sql_query_limit($sql, 1);
551
	$row = $db->sql_fetchrow($result);
552
	$db->sql_freeresult($result);
553
554
	if ($row)
555
	{
556
		$post_data['post_subject'] = $row['draft_subject'];
557
		$message_parser->message = $row['draft_message'];
558
559
		$template->assign_var('S_DRAFT_LOADED', true);
560
	}
561
	else
562
	{
563
		$draft_id = 0;
564
	}
565
}
566
567
// Load draft overview
568
if ($load && ($mode == 'reply' || $mode == 'quote' || $mode == 'post') && $post_data['drafts'])
569
{
570
	load_drafts($topic_id, $forum_id);
571
}
572
573
$solved_captcha = false;
574
575
if ($submit || $preview || $refresh)
576
{
577
	$post_data['topic_cur_post_id']	= request_var('topic_cur_post_id', 0);
578
	$post_data['post_subject']		= utf8_normalize_nfc(request_var('subject', '', true));
579
	$message_parser->message		= utf8_normalize_nfc(request_var('message', '', true));
580
581
	$post_data['username']			= utf8_normalize_nfc(request_var('username', $post_data['username'], true));
582
	$post_data['post_edit_reason']	= (!empty($_POST['edit_reason']) && $mode == 'edit' && $auth->acl_get('m_edit', $forum_id)) ? utf8_normalize_nfc(request_var('edit_reason', '', true)) : '';
583
584
	$post_data['orig_topic_type']	= $post_data['topic_type'];
585
	$post_data['topic_type']		= request_var('topic_type', (($mode != 'post') ? (int) $post_data['topic_type'] : POST_NORMAL));
586
	$post_data['topic_time_limit']	= request_var('topic_time_limit', (($mode != 'post') ? (int) $post_data['topic_time_limit'] : 0));
587
	$post_data['icon_id']			= request_var('icon', 0);
588
589
	$post_data['enable_bbcode']		= (!$bbcode_status || isset($_POST['disable_bbcode'])) ? false : true;
590
	$post_data['enable_smilies']	= (!$smilies_status || isset($_POST['disable_smilies'])) ? false : true;
591
	$post_data['enable_urls']		= (isset($_POST['disable_magic_url'])) ? 0 : 1;
592
	$post_data['enable_sig']		= (!$config['allow_sig'] || !$auth->acl_get('f_sigs', $forum_id) || !$auth->acl_get('u_sig')) ? false : ((isset($_POST['attach_sig']) && $user->data['is_registered']) ? true : false);
593
594
	if ($config['allow_topic_notify'] && $user->data['is_registered'])
595
	{
596
		$notify = (isset($_POST['notify'])) ? true : false;
597
	}
598
	else
599
	{
600
		$notify = false;
601
	}
602
603
	$topic_lock			= (isset($_POST['lock_topic'])) ? true : false;
604
	$post_lock			= (isset($_POST['lock_post'])) ? true : false;
605
	$poll_delete		= (isset($_POST['poll_delete'])) ? true : false;
606
607
	if ($submit)
608
	{
609
		$status_switch = (($post_data['enable_bbcode']+1) << 8) + (($post_data['enable_smilies']+1) << 4) + (($post_data['enable_urls']+1) << 2) + (($post_data['enable_sig']+1) << 1);
610
		$status_switch = ($status_switch != $check_value);
611
	}
612
	else
613
	{
614
		$status_switch = 1;
615
	}
616
617
	// Delete Poll
618
	if ($poll_delete && $mode == 'edit' && sizeof($post_data['poll_options']) &&
619
		((!$post_data['poll_last_vote'] && $post_data['poster_id'] == $user->data['user_id'] && $auth->acl_get('f_delete', $forum_id)) || $auth->acl_get('m_delete', $forum_id)))
620
	{
621
		if ($submit && check_form_key('posting'))
622
		{
623
			$sql = 'DELETE FROM ' . POLL_OPTIONS_TABLE . "
624
				WHERE topic_id = $topic_id";
625
			$db->sql_query($sql);
626
627
			$sql = 'DELETE FROM ' . POLL_VOTES_TABLE . "
628
				WHERE topic_id = $topic_id";
629
			$db->sql_query($sql);
630
631
			$topic_sql = array(
632
				'poll_title'		=> '',
633
				'poll_start' 		=> 0,
634
				'poll_length'		=> 0,
635
				'poll_last_vote'	=> 0,
636
				'poll_max_options'	=> 0,
637
				'poll_vote_change'	=> 0
638
			);
639
640
			$sql = 'UPDATE ' . TOPICS_TABLE . '
641
				SET ' . $db->sql_build_array('UPDATE', $topic_sql) . "
642
				WHERE topic_id = $topic_id";
643
			$db->sql_query($sql);
644
		}
645
646
		$post_data['poll_title'] = $post_data['poll_option_text'] = '';
647
		$post_data['poll_vote_change'] = $post_data['poll_max_options'] = $post_data['poll_length'] = 0;
648
	}
649
	else
650
	{
651
		$post_data['poll_title']		= utf8_normalize_nfc(request_var('poll_title', '', true));
652
		$post_data['poll_length']		= request_var('poll_length', 0);
653
		$post_data['poll_option_text']	= utf8_normalize_nfc(request_var('poll_option_text', '', true));
654
		$post_data['poll_max_options']	= request_var('poll_max_options', 1);
655
		$post_data['poll_vote_change']	= ($auth->acl_get('f_votechg', $forum_id) && isset($_POST['poll_vote_change'])) ? 1 : 0;
656
	}
657
658
	// If replying/quoting and last post id has changed
659
	// give user option to continue submit or return to post
660
	// notify and show user the post made between his request and the final submit
661
	if (($mode == 'reply' || $mode == 'quote') && $post_data['topic_cur_post_id'] && $post_data['topic_cur_post_id'] != $post_data['topic_last_post_id'])
662
	{
663
		// Only do so if it is allowed forum-wide
664
		if ($post_data['forum_flags'] & FORUM_FLAG_POST_REVIEW)
665
		{
666
			if (topic_review($topic_id, $forum_id, 'post_review', $post_data['topic_cur_post_id']))
667
			{
668
				$template->assign_var('S_POST_REVIEW', true);
669
			}
670
671
			$submit = false;
672
			$refresh = true;
673
		}
674
	}
675
676
	// Parse Attachments - before checksum is calculated
677
	$message_parser->parse_attachments('fileupload', $mode, $forum_id, $submit, $preview, $refresh);
678
679
	// Grab md5 'checksum' of new message
680
	$message_md5 = md5($message_parser->message);
681
682
	// Check checksum ... don't re-parse message if the same
683
	$update_message = ($mode != 'edit' || $message_md5 != $post_data['post_checksum'] || $status_switch || strlen($post_data['bbcode_uid']) < BBCODE_UID_LEN) ? true : false;
684
685
	// Parse message
686
	if ($update_message)
687
	{
688
		if (sizeof($message_parser->warn_msg))
689
		{
690
			$error[] = implode('<br />', $message_parser->warn_msg);
691
			$message_parser->warn_msg = array();
692
		}
693
694
		$message_parser->parse($post_data['enable_bbcode'], ($config['allow_post_links']) ? $post_data['enable_urls'] : false, $post_data['enable_smilies'], $img_status, $flash_status, $quote_status, $config['allow_post_links']);
695
696
		// On a refresh we do not care about message parsing errors
697
		if (sizeof($message_parser->warn_msg) && $refresh)
698
		{
699
			$message_parser->warn_msg = array();
700
		}
701
	}
702
	else
703
	{
704
		$message_parser->bbcode_bitfield = $post_data['bbcode_bitfield'];
705
	}
706
707
	if ($mode != 'edit' && !$preview && !$refresh && $config['flood_interval'] && !$auth->acl_get('f_ignoreflood', $forum_id))
708
	{
709
		// Flood check
710
		$last_post_time = 0;
711
712
		if ($user->data['is_registered'])
713
		{
714
			$last_post_time = $user->data['user_lastpost_time'];
715
		}
716
		else
717
		{
718
			$sql = 'SELECT post_time AS last_post_time
719
				FROM ' . POSTS_TABLE . "
720
				WHERE poster_ip = '" . $user->ip . "'
721
					AND post_time > " . ($current_time - $config['flood_interval']);
722
			$result = $db->sql_query_limit($sql, 1);
723
			if ($row = $db->sql_fetchrow($result))
724
			{
725
				$last_post_time = $row['last_post_time'];
726
			}
727
			$db->sql_freeresult($result);
728
		}
729
730
		if ($last_post_time && ($current_time - $last_post_time) < intval($config['flood_interval']))
731
		{
732
			$error[] = $user->lang['FLOOD_ERROR'];
733
		}
734
	}
735
736
	// Validate username
737
	if (($post_data['username'] && !$user->data['is_registered']) || ($mode == 'edit' && $post_data['poster_id'] == ANONYMOUS && $post_data['username'] && $post_data['post_username'] && $post_data['post_username'] != $post_data['username']))
738
	{
739
		include(PHPBB_ROOT_PATH . 'includes/functions_user.' . PHP_EXT);
740
741
		if (($result = validate_username($post_data['username'], (!empty($post_data['post_username'])) ? $post_data['post_username'] : '')) !== false)
742
		{
743
			$user->add_lang('ucp');
744
			$error[] = $user->lang[$result . '_USERNAME'];
745
		}
746
	}
747
748
	if ($config['enable_post_confirm'] && !$user->data['is_registered'] && in_array($mode, array('quote', 'post', 'reply')))
749
	{
750
		$vc_response = $captcha->validate();
751
		if ($vc_response)
752
		{
753
			$error[] = $vc_response;
754
		}
755
		else
756
		{
757
			$solved_captcha = true;
758
		}
759
	}
760
761
	// check form
762
	if (($submit || $preview) && !check_form_key('posting'))
763
	{
764
		$error[] = $user->lang['FORM_INVALID'];
765
	}
766
767
	// Parse subject
768
	if (!$preview && !$refresh && utf8_clean_string($post_data['post_subject']) === '' && ($mode == 'post' || ($mode == 'edit' && $post_data['topic_first_post_id'] == $post_id)))
769
	{
770
		$error[] = $user->lang['EMPTY_SUBJECT'];
771
	}
772
773
	$post_data['poll_last_vote'] = (isset($post_data['poll_last_vote'])) ? $post_data['poll_last_vote'] : 0;
774
775
	if ($post_data['poll_option_text'] &&
776
		($mode == 'post' || ($mode == 'edit' && $post_id == $post_data['topic_first_post_id']/* && (!$post_data['poll_last_vote'] || $auth->acl_get('m_edit', $forum_id))*/))
777
		&& $auth->acl_get('f_poll', $forum_id))
778
	{
779
		$poll = array(
780
			'poll_title'		=> $post_data['poll_title'],
781
			'poll_length'		=> $post_data['poll_length'],
782
			'poll_max_options'	=> $post_data['poll_max_options'],
783
			'poll_option_text'	=> $post_data['poll_option_text'],
784
			'poll_start'		=> $post_data['poll_start'],
785
			'poll_last_vote'	=> $post_data['poll_last_vote'],
786
			'poll_vote_change'	=> $post_data['poll_vote_change'],
787
			'enable_bbcode'		=> $post_data['enable_bbcode'],
788
			'enable_urls'		=> $post_data['enable_urls'],
789
			'enable_smilies'	=> $post_data['enable_smilies'],
790
			'img_status'		=> $img_status
791
		);
792
793
		$message_parser->parse_poll($poll);
794
795
		$post_data['poll_options'] = (isset($poll['poll_options'])) ? $poll['poll_options'] : '';
796
		$post_data['poll_title'] = (isset($poll['poll_title'])) ? $poll['poll_title'] : '';
797
798
		/* We reset votes, therefore also allow removing options
799
		if ($post_data['poll_last_vote'] && ($poll['poll_options_size'] < $orig_poll_options_size))
800
		{
801
			$message_parser->warn_msg[] = $user->lang['NO_DELETE_POLL_OPTIONS'];
802
		}*/
803
	}
804
	else
805
	{
806
		$poll = array();
807
	}
808
809
	// Check topic type
810
	if ($post_data['topic_type'] != POST_NORMAL && ($mode == 'post' || ($mode == 'edit' && $post_data['topic_first_post_id'] == $post_id)))
811
	{
812
		switch ($post_data['topic_type'])
813
		{
814
			case POST_GLOBAL:
815
			case POST_ANNOUNCE:
816
				$auth_option = 'f_announce';
817
			break;
818
819
			case POST_STICKY:
820
				$auth_option = 'f_sticky';
821
			break;
822
823
			default:
824
				$auth_option = '';
825
			break;
826
		}
827
828
		if (!$auth->acl_get($auth_option, $forum_id))
829
		{
830
			// There is a special case where a user edits his post whereby the topic type got changed by an admin/mod.
831
			// Another case would be a mod not having sticky permissions for example but edit permissions.
832
			if ($mode == 'edit')
833
			{
834
				// To prevent non-authed users messing around with the topic type we reset it to the original one.
835
				$post_data['topic_type'] = $post_data['orig_topic_type'];
836
			}
837
			else
838
			{
839
				$error[] = $user->lang['CANNOT_POST_' . str_replace('F_', '', strtoupper($auth_option))];
840
			}
841
		}
842
	}
843
844
	if (sizeof($message_parser->warn_msg))
845
	{
846
		$error[] = implode('<br />', $message_parser->warn_msg);
847
	}
848
849
	// DNSBL check
850
	if ($config['check_dnsbl'] && !$refresh)
851
	{
852
		if (($dnsbl = $user->check_dnsbl('post')) !== false)
853
		{
854
			$error[] = sprintf($user->lang['IP_BLACKLISTED'], $user->ip, $dnsbl[1]);
855
		}
856
	}
857
858
	// Store message, sync counters
859
	if (!sizeof($error) && $submit)
860
	{
861
		// Check if we want to de-globalize the topic... and ask for new forum
862
		if ($post_data['topic_type'] != POST_GLOBAL)
863
		{
864
			$sql = 'SELECT topic_type, forum_id
865
				FROM ' . TOPICS_TABLE . "
866
				WHERE topic_id = $topic_id";
867
			$result = $db->sql_query($sql);
868
			$row = $db->sql_fetchrow($result);
869
			$db->sql_freeresult($result);
870
871
			if ($row && !$row['forum_id'] && $row['topic_type'] == POST_GLOBAL)
872
			{
873
				$to_forum_id = request_var('to_forum_id', 0);
874
875
				if ($to_forum_id)
876
				{
877
					$sql = 'SELECT forum_type
878
						FROM ' . FORUMS_TABLE . '
879
						WHERE forum_id = ' . $to_forum_id;
880
					$result = $db->sql_query($sql);
881
					$forum_type = (int) $db->sql_fetchfield('forum_type');
882
					$db->sql_freeresult($result);
883
884
					if ($forum_type != FORUM_POST || !$auth->acl_get('f_post', $to_forum_id))
885
					{
886
						$to_forum_id = 0;
887
					}