includes/functions_posting.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Thu, 08 Mar 2012 01:33:19 +0400
changeset 11 0a7892c58331
parent 0 ceef6d64c21d
permissions -rw-r--r--
Style fix
indvdum@0
     1
<?php
indvdum@0
     2
/**
indvdum@0
     3
*
indvdum@0
     4
* @package phpBB3
indvdum@0
     5
* @version $Id$
indvdum@0
     6
* @copyright (c) 2005 phpBB Group
indvdum@0
     7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
indvdum@0
     8
*
indvdum@0
     9
*/
indvdum@0
    10
indvdum@0
    11
/**
indvdum@0
    12
* @ignore
indvdum@0
    13
*/
indvdum@0
    14
if (!defined('IN_PHPBB'))
indvdum@0
    15
{
indvdum@0
    16
	exit;
indvdum@0
    17
}
indvdum@0
    18
indvdum@0
    19
/**
indvdum@0
    20
* Fill smiley templates (or just the variables) with smilies, either in a window or inline
indvdum@0
    21
*/
indvdum@0
    22
function generate_smilies($mode, $forum_id)
indvdum@0
    23
{
indvdum@0
    24
	global $auth, $db, $user, $config, $template;
indvdum@0
    25
	global $phpEx, $phpbb_root_path;
indvdum@0
    26
indvdum@0
    27
	$start = request_var('start', 0);
indvdum@0
    28
indvdum@0
    29
	if ($mode == 'window')
indvdum@0
    30
	{
indvdum@0
    31
		if ($forum_id)
indvdum@0
    32
		{
indvdum@0
    33
			$sql = 'SELECT forum_style
indvdum@0
    34
				FROM ' . FORUMS_TABLE . "
indvdum@0
    35
				WHERE forum_id = $forum_id";
indvdum@0
    36
			$result = $db->sql_query_limit($sql, 1);
indvdum@0
    37
			$row = $db->sql_fetchrow($result);
indvdum@0
    38
			$db->sql_freeresult($result);
indvdum@0
    39
indvdum@0
    40
			$user->setup('posting', (int) $row['forum_style']);
indvdum@0
    41
		}
indvdum@0
    42
		else
indvdum@0
    43
		{
indvdum@0
    44
			$user->setup('posting');
indvdum@0
    45
		}
indvdum@0
    46
indvdum@0
    47
		page_header($user->lang['SMILIES']);
indvdum@0
    48
indvdum@0
    49
		$sql = 'SELECT COUNT(smiley_id) AS item_count
indvdum@0
    50
			FROM ' . SMILIES_TABLE . '
indvdum@0
    51
			GROUP BY smiley_url';
indvdum@0
    52
		$result = $db->sql_query($sql, 3600);
indvdum@0
    53
indvdum@0
    54
		$smiley_count = 0;
indvdum@0
    55
		while ($row = $db->sql_fetchrow($result))
indvdum@0
    56
		{
indvdum@0
    57
			++$smiley_count;
indvdum@0
    58
		}
indvdum@0
    59
		$db->sql_freeresult($result);
indvdum@0
    60
indvdum@0
    61
		$template->set_filenames(array(
indvdum@0
    62
			'body' => 'posting_smilies.html')
indvdum@0
    63
		);
indvdum@0
    64
indvdum@0
    65
		$template->assign_var('PAGINATION',
indvdum@0
    66
			generate_pagination(append_sid("{$phpbb_root_path}posting.$phpEx", 'mode=smilies&amp;f=' . $forum_id),
indvdum@0
    67
				$smiley_count, $config['smilies_per_page'], $start, true)
indvdum@0
    68
		);
indvdum@0
    69
	}
indvdum@0
    70
indvdum@0
    71
	$display_link = false;
indvdum@0
    72
	if ($mode == 'inline')
indvdum@0
    73
	{
indvdum@0
    74
		$sql = 'SELECT smiley_id
indvdum@0
    75
			FROM ' . SMILIES_TABLE . '
indvdum@0
    76
			WHERE display_on_posting = 0';
indvdum@0
    77
		$result = $db->sql_query_limit($sql, 1, 0, 3600);
indvdum@0
    78
indvdum@0
    79
		if ($row = $db->sql_fetchrow($result))
indvdum@0
    80
		{
indvdum@0
    81
			$display_link = true;
indvdum@0
    82
		}
indvdum@0
    83
		$db->sql_freeresult($result);
indvdum@0
    84
	}
indvdum@0
    85
indvdum@0
    86
	if ($mode == 'window')
indvdum@0
    87
	{
indvdum@0
    88
		$sql = 'SELECT smiley_url, MIN(emotion) as emotion, MIN(code) AS code, smiley_width, smiley_height, MIN(smiley_order) AS min_smiley_order
indvdum@0
    89
			FROM ' . SMILIES_TABLE . '
indvdum@0
    90
			GROUP BY smiley_url, smiley_width, smiley_height
indvdum@0
    91
			ORDER BY min_smiley_order';
indvdum@0
    92
		$result = $db->sql_query_limit($sql, $config['smilies_per_page'], $start, 3600);
indvdum@0
    93
	}
indvdum@0
    94
	else
indvdum@0
    95
	{
indvdum@0
    96
		$sql = 'SELECT *
indvdum@0
    97
			FROM ' . SMILIES_TABLE . '
indvdum@0
    98
			WHERE display_on_posting = 1
indvdum@0
    99
			ORDER BY smiley_order';
indvdum@0
   100
		$result = $db->sql_query($sql, 3600);
indvdum@0
   101
	}
indvdum@0
   102
indvdum@0
   103
	$smilies = array();
indvdum@0
   104
	while ($row = $db->sql_fetchrow($result))
indvdum@0
   105
	{
indvdum@0
   106
		if (empty($smilies[$row['smiley_url']]))
indvdum@0
   107
		{
indvdum@0
   108
			$smilies[$row['smiley_url']] = $row;
indvdum@0
   109
		}
indvdum@0
   110
	}
indvdum@0
   111
	$db->sql_freeresult($result);
indvdum@0
   112
indvdum@0
   113
	if (sizeof($smilies))
indvdum@0
   114
	{
indvdum@0
   115
		$root_path = (defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH) ? generate_board_url() . '/' : $phpbb_root_path;
indvdum@0
   116
indvdum@0
   117
		foreach ($smilies as $row)
indvdum@0
   118
		{
indvdum@0
   119
			$template->assign_block_vars('smiley', array(
indvdum@0
   120
				'SMILEY_CODE'	=> $row['code'],
indvdum@0
   121
				'A_SMILEY_CODE'	=> addslashes($row['code']),
indvdum@0
   122
				'SMILEY_IMG'	=> $root_path . $config['smilies_path'] . '/' . $row['smiley_url'],
indvdum@0
   123
				'SMILEY_WIDTH'	=> $row['smiley_width'],
indvdum@0
   124
				'SMILEY_HEIGHT'	=> $row['smiley_height'],
indvdum@0
   125
				'SMILEY_DESC'	=> $row['emotion'])
indvdum@0
   126
			);
indvdum@0
   127
		}
indvdum@0
   128
	}
indvdum@0
   129
indvdum@0
   130
	if ($mode == 'inline' && $display_link)
indvdum@0
   131
	{
indvdum@0
   132
		$template->assign_vars(array(
indvdum@0
   133
			'S_SHOW_SMILEY_LINK' 	=> true,
indvdum@0
   134
			'U_MORE_SMILIES' 		=> append_sid("{$phpbb_root_path}posting.$phpEx", 'mode=smilies&amp;f=' . $forum_id))
indvdum@0
   135
		);
indvdum@0
   136
	}
indvdum@0
   137
indvdum@0
   138
	if ($mode == 'window')
indvdum@0
   139
	{
indvdum@0
   140
		page_footer();
indvdum@0
   141
	}
indvdum@0
   142
}
indvdum@0
   143
indvdum@0
   144
/**
indvdum@0
   145
* Update last post information
indvdum@0
   146
* Should be used instead of sync() if only the last post information are out of sync... faster
indvdum@0
   147
*
indvdum@0
   148
* @param	string	$type				Can be forum|topic
indvdum@0
   149
* @param	mixed	$ids				topic/forum ids
indvdum@0
   150
* @param	bool	$return_update_sql	true: SQL query shall be returned, false: execute SQL
indvdum@0
   151
*/
indvdum@0
   152
function update_post_information($type, $ids, $return_update_sql = false)
indvdum@0
   153
{
indvdum@0
   154
	global $db;
indvdum@0
   155
indvdum@0
   156
	if (empty($ids))
indvdum@0
   157
	{
indvdum@0
   158
		return;
indvdum@0
   159
	}
indvdum@0
   160
	if (!is_array($ids))
indvdum@0
   161
	{
indvdum@0
   162
		$ids = array($ids);
indvdum@0
   163
	}
indvdum@0
   164
indvdum@0
   165
indvdum@0
   166
	$update_sql = $empty_forums = $not_empty_forums = array();
indvdum@0
   167
indvdum@0
   168
	if ($type != 'topic')
indvdum@0
   169
	{
indvdum@0
   170
		$topic_join = ', ' . TOPICS_TABLE . ' t';
indvdum@0
   171
		$topic_condition = 'AND t.topic_id = p.topic_id AND t.topic_approved = 1';
indvdum@0
   172
	}
indvdum@0
   173
	else
indvdum@0
   174
	{
indvdum@0
   175
		$topic_join = '';
indvdum@0
   176
		$topic_condition = '';
indvdum@0
   177
	}
indvdum@0
   178
indvdum@0
   179
	if (sizeof($ids) == 1)
indvdum@0
   180
	{
indvdum@0
   181
		$sql = 'SELECT MAX(p.post_id) as last_post_id
indvdum@0
   182
			FROM ' . POSTS_TABLE . " p $topic_join
indvdum@0
   183
			WHERE " . $db->sql_in_set('p.' . $type . '_id', $ids) . "
indvdum@0
   184
				$topic_condition
indvdum@0
   185
				AND p.post_approved = 1";
indvdum@0
   186
	}
indvdum@0
   187
	else
indvdum@0
   188
	{
indvdum@0
   189
		$sql = 'SELECT p.' . $type . '_id, MAX(p.post_id) as last_post_id
indvdum@0
   190
			FROM ' . POSTS_TABLE . " p $topic_join
indvdum@0
   191
			WHERE " . $db->sql_in_set('p.' . $type . '_id', $ids) . "
indvdum@0
   192
				$topic_condition
indvdum@0
   193
				AND p.post_approved = 1
indvdum@0
   194
			GROUP BY p.{$type}_id";
indvdum@0
   195
	}
indvdum@0
   196
	$result = $db->sql_query($sql);
indvdum@0
   197
indvdum@0
   198
	$last_post_ids = array();
indvdum@0
   199
	while ($row = $db->sql_fetchrow($result))
indvdum@0
   200
	{
indvdum@0
   201
		if (sizeof($ids) == 1)
indvdum@0
   202
		{
indvdum@0
   203
			$row[$type . '_id'] = $ids[0];
indvdum@0
   204
		}
indvdum@0
   205
indvdum@0
   206
		if ($type == 'forum')
indvdum@0
   207
		{
indvdum@0
   208
			$not_empty_forums[] = $row['forum_id'];
indvdum@0
   209
indvdum@0
   210
			if (empty($row['last_post_id']))
indvdum@0
   211
			{
indvdum@0
   212
				$empty_forums[] = $row['forum_id'];
indvdum@0
   213
			}
indvdum@0
   214
		}
indvdum@0
   215
indvdum@0
   216
		$last_post_ids[] = $row['last_post_id'];
indvdum@0
   217
	}
indvdum@0
   218
	$db->sql_freeresult($result);
indvdum@0
   219
indvdum@0
   220
	if ($type == 'forum')
indvdum@0
   221
	{
indvdum@0
   222
		$empty_forums = array_merge($empty_forums, array_diff($ids, $not_empty_forums));
indvdum@0
   223
indvdum@0
   224
		foreach ($empty_forums as $void => $forum_id)
indvdum@0
   225
		{
indvdum@0
   226
			$update_sql[$forum_id][] = 'forum_last_post_id = 0';
indvdum@0
   227
			$update_sql[$forum_id][] = "forum_last_post_subject = ''";
indvdum@0
   228
			$update_sql[$forum_id][] = 'forum_last_post_time = 0';
indvdum@0
   229
			$update_sql[$forum_id][] = 'forum_last_poster_id = 0';
indvdum@0
   230
			$update_sql[$forum_id][] = "forum_last_poster_name = ''";
indvdum@0
   231
			$update_sql[$forum_id][] = "forum_last_poster_colour = ''";
indvdum@0
   232
		}
indvdum@0
   233
	}
indvdum@0
   234
indvdum@0
   235
	if (sizeof($last_post_ids))
indvdum@0
   236
	{
indvdum@0
   237
		$sql = 'SELECT p.' . $type . '_id, p.post_id, p.post_subject, p.post_time, p.poster_id, p.post_username, u.user_id, u.username, u.user_colour
indvdum@0
   238
			FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
indvdum@0
   239
			WHERE p.poster_id = u.user_id
indvdum@0
   240
				AND ' . $db->sql_in_set('p.post_id', $last_post_ids);
indvdum@0
   241
		$result = $db->sql_query($sql);
indvdum@0
   242
indvdum@0
   243
		while ($row = $db->sql_fetchrow($result))
indvdum@0
   244
		{
indvdum@0
   245
			$update_sql[$row["{$type}_id"]][] = $type . '_last_post_id = ' . (int) $row['post_id'];
indvdum@0
   246
			$update_sql[$row["{$type}_id"]][] = "{$type}_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
indvdum@0
   247
			$update_sql[$row["{$type}_id"]][] = $type . '_last_post_time = ' . (int) $row['post_time'];
indvdum@0
   248
			$update_sql[$row["{$type}_id"]][] = $type . '_last_poster_id = ' . (int) $row['poster_id'];
indvdum@0
   249
			$update_sql[$row["{$type}_id"]][] = "{$type}_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
indvdum@0
   250
			$update_sql[$row["{$type}_id"]][] = "{$type}_last_poster_name = '" . (($row['poster_id'] == ANONYMOUS) ? $db->sql_escape($row['post_username']) : $db->sql_escape($row['username'])) . "'";
indvdum@0
   251
		}
indvdum@0
   252
		$db->sql_freeresult($result);
indvdum@0
   253
	}
indvdum@0
   254
	unset($empty_forums, $ids, $last_post_ids);
indvdum@0
   255
indvdum@0
   256
	if ($return_update_sql || !sizeof($update_sql))
indvdum@0
   257
	{
indvdum@0
   258
		return $update_sql;
indvdum@0
   259
	}
indvdum@0
   260
indvdum@0
   261
	$table = ($type == 'forum') ? FORUMS_TABLE : TOPICS_TABLE;
indvdum@0
   262
indvdum@0
   263
	foreach ($update_sql as $update_id => $update_sql_ary)
indvdum@0
   264
	{
indvdum@0
   265
		$sql = "UPDATE $table
indvdum@0
   266
			SET " . implode(', ', $update_sql_ary) . "
indvdum@0
   267
			WHERE {$type}_id = $update_id";
indvdum@0
   268
		$db->sql_query($sql);
indvdum@0
   269
	}
indvdum@0
   270
indvdum@0
   271
	return;
indvdum@0
   272
}
indvdum@0
   273
indvdum@0
   274
/**
indvdum@0
   275
* Generate Topic Icons for display
indvdum@0
   276
*/
indvdum@0
   277
function posting_gen_topic_icons($mode, $icon_id)
indvdum@0
   278
{
indvdum@0
   279
	global $phpbb_root_path, $config, $template, $cache;
indvdum@0
   280
indvdum@0
   281
	// Grab icons
indvdum@0
   282
	$icons = $cache->obtain_icons();
indvdum@0
   283
indvdum@0
   284
	if (!$icon_id)
indvdum@0
   285
	{
indvdum@0
   286
		$template->assign_var('S_NO_ICON_CHECKED', ' checked="checked"');
indvdum@0
   287
	}
indvdum@0
   288
indvdum@0
   289
	if (sizeof($icons))
indvdum@0
   290
	{
indvdum@0
   291
		foreach ($icons as $id => $data)
indvdum@0
   292
		{
indvdum@0
   293
			if ($data['display'])
indvdum@0
   294
			{
indvdum@0
   295
				$template->assign_block_vars('topic_icon', array(
indvdum@0
   296
					'ICON_ID'		=> $id,
indvdum@0
   297
					'ICON_IMG'		=> $phpbb_root_path . $config['icons_path'] . '/' . $data['img'],
indvdum@0
   298
					'ICON_WIDTH'	=> $data['width'],
indvdum@0
   299
					'ICON_HEIGHT'	=> $data['height'],
indvdum@0
   300
indvdum@0
   301
					'S_CHECKED'			=> ($id == $icon_id) ? true : false,
indvdum@0
   302
					'S_ICON_CHECKED'	=> ($id == $icon_id) ? ' checked="checked"' : '')
indvdum@0
   303
				);
indvdum@0
   304
			}
indvdum@0
   305
		}
indvdum@0
   306
indvdum@0
   307
		return true;
indvdum@0
   308
	}
indvdum@0
   309
indvdum@0
   310
	return false;
indvdum@0
   311
}
indvdum@0
   312
indvdum@0
   313
/**
indvdum@0
   314
* Build topic types able to be selected
indvdum@0
   315
*/
indvdum@0
   316
function posting_gen_topic_types($forum_id, $cur_topic_type = POST_NORMAL)
indvdum@0
   317
{
indvdum@0
   318
	global $auth, $user, $template, $topic_type;
indvdum@0
   319
indvdum@0
   320
	$toggle = false;
indvdum@0
   321
indvdum@0
   322
	$topic_types = array(
indvdum@0
   323
		'sticky'	=> array('const' => POST_STICKY, 'lang' => 'POST_STICKY'),
indvdum@0
   324
		'announce'	=> array('const' => POST_ANNOUNCE, 'lang' => 'POST_ANNOUNCEMENT'),
indvdum@0
   325
		'global'	=> array('const' => POST_GLOBAL, 'lang' => 'POST_GLOBAL')
indvdum@0
   326
	);
indvdum@0
   327
indvdum@0
   328
	$topic_type_array = array();
indvdum@0
   329
indvdum@0
   330
	foreach ($topic_types as $auth_key => $topic_value)
indvdum@0
   331
	{
indvdum@0
   332
		// We do not have a special post global announcement permission
indvdum@0
   333
		$auth_key = ($auth_key == 'global') ? 'announce' : $auth_key;
indvdum@0
   334
indvdum@0
   335
		if ($auth->acl_get('f_' . $auth_key, $forum_id))
indvdum@0
   336
		{
indvdum@0
   337
			$toggle = true;
indvdum@0
   338
indvdum@0
   339
			$topic_type_array[] = array(
indvdum@0
   340
				'VALUE'			=> $topic_value['const'],
indvdum@0
   341
				'S_CHECKED'		=> ($cur_topic_type == $topic_value['const'] || ($forum_id == 0 && $topic_value['const'] == POST_GLOBAL)) ? ' checked="checked"' : '',
indvdum@0
   342
				'L_TOPIC_TYPE'	=> $user->lang[$topic_value['lang']]
indvdum@0
   343
			);
indvdum@0
   344
		}
indvdum@0
   345
	}
indvdum@0
   346
indvdum@0
   347
	if ($toggle)
indvdum@0
   348
	{
indvdum@0
   349
		$topic_type_array = array_merge(array(0 => array(
indvdum@0
   350
			'VALUE'			=> POST_NORMAL,
indvdum@0
   351
			'S_CHECKED'		=> ($cur_topic_type == POST_NORMAL) ? ' checked="checked"' : '',
indvdum@0
   352
			'L_TOPIC_TYPE'	=> $user->lang['POST_NORMAL'])),
indvdum@0
   353
indvdum@0
   354
			$topic_type_array
indvdum@0
   355
		);
indvdum@0
   356
indvdum@0
   357
		foreach ($topic_type_array as $array)
indvdum@0
   358
		{
indvdum@0
   359
			$template->assign_block_vars('topic_type', $array);
indvdum@0
   360
		}
indvdum@0
   361
indvdum@0
   362
		$template->assign_vars(array(
indvdum@0
   363
			'S_TOPIC_TYPE_STICKY'	=> ($auth->acl_get('f_sticky', $forum_id)),
indvdum@0
   364
			'S_TOPIC_TYPE_ANNOUNCE'	=> ($auth->acl_get('f_announce', $forum_id)))
indvdum@0
   365
		);
indvdum@0
   366
	}
indvdum@0
   367
indvdum@0
   368
	return $toggle;
indvdum@0
   369
}
indvdum@0
   370
indvdum@0
   371
//
indvdum@0
   372
// Attachment related functions
indvdum@0
   373
//
indvdum@0
   374
indvdum@0
   375
/**
indvdum@0
   376
* Upload Attachment - filedata is generated here
indvdum@0
   377
* Uses upload class
indvdum@0
   378
*/
indvdum@0
   379
function upload_attachment($form_name, $forum_id, $local = false, $local_storage = '', $is_message = false, $local_filedata = false)
indvdum@0
   380
{
indvdum@0
   381
	global $auth, $user, $config, $db, $cache;
indvdum@0
   382
	global $phpbb_root_path, $phpEx;
indvdum@0
   383
indvdum@0
   384
	$filedata = array(
indvdum@0
   385
		'error'	=> array()
indvdum@0
   386
	);
indvdum@0
   387
indvdum@0
   388
	include_once($phpbb_root_path . 'includes/functions_upload.' . $phpEx);
indvdum@0
   389
	$upload = new fileupload();
indvdum@0
   390
indvdum@0
   391
	if ($config['check_attachment_content'] && isset($config['mime_triggers']))
indvdum@0
   392
	{
indvdum@0
   393
		$upload->set_disallowed_content(explode('|', $config['mime_triggers']));
indvdum@0
   394
	}
indvdum@0
   395
indvdum@0
   396
	if (!$local)
indvdum@0
   397
	{
indvdum@0
   398
		$filedata['post_attach'] = ($upload->is_valid($form_name)) ? true : false;
indvdum@0
   399
	}
indvdum@0
   400
	else
indvdum@0
   401
	{
indvdum@0
   402
		$filedata['post_attach'] = true;
indvdum@0
   403
	}
indvdum@0
   404
indvdum@0
   405
	if (!$filedata['post_attach'])
indvdum@0
   406
	{
indvdum@0
   407
		$filedata['error'][] = $user->lang['NO_UPLOAD_FORM_FOUND'];
indvdum@0
   408
		return $filedata;
indvdum@0
   409
	}
indvdum@0
   410
indvdum@0
   411
	$extensions = $cache->obtain_attach_extensions((($is_message) ? false : (int) $forum_id));
indvdum@0
   412
	$upload->set_allowed_extensions(array_keys($extensions['_allowed_']));
indvdum@0
   413
indvdum@0
   414
	$file = ($local) ? $upload->local_upload($local_storage, $local_filedata) : $upload->form_upload($form_name);
indvdum@0
   415
indvdum@0
   416
	if ($file->init_error)
indvdum@0
   417
	{
indvdum@0
   418
		$filedata['post_attach'] = false;
indvdum@0
   419
		return $filedata;
indvdum@0
   420
	}
indvdum@0
   421
indvdum@0
   422
	$cat_id = (isset($extensions[$file->get('extension')]['display_cat'])) ? $extensions[$file->get('extension')]['display_cat'] : ATTACHMENT_CATEGORY_NONE;
indvdum@0
   423
indvdum@0
   424
	// Make sure the image category only holds valid images...
indvdum@0
   425
	if ($cat_id == ATTACHMENT_CATEGORY_IMAGE && !$file->is_image())
indvdum@0
   426
	{
indvdum@0
   427
		$file->remove();
indvdum@0
   428
indvdum@0
   429
		// If this error occurs a user tried to exploit an IE Bug by renaming extensions
indvdum@0
   430
		// Since the image category is displaying content inline we need to catch this.
indvdum@0
   431
		trigger_error($user->lang['ATTACHED_IMAGE_NOT_IMAGE']);
indvdum@0
   432
	}
indvdum@0
   433
indvdum@0
   434
	// Do we have to create a thumbnail?
indvdum@0
   435
	$filedata['thumbnail'] = ($cat_id == ATTACHMENT_CATEGORY_IMAGE && $config['img_create_thumbnail']) ? 1 : 0;
indvdum@0
   436
indvdum@0
   437
	// Check Image Size, if it is an image
indvdum@0
   438
	if (!$auth->acl_get('a_') && !$auth->acl_get('m_', $forum_id) && $cat_id == ATTACHMENT_CATEGORY_IMAGE)
indvdum@0
   439
	{
indvdum@0
   440
		$file->upload->set_allowed_dimensions(0, 0, $config['img_max_width'], $config['img_max_height']);
indvdum@0
   441
	}
indvdum@0
   442
indvdum@0
   443
	// Admins and mods are allowed to exceed the allowed filesize
indvdum@0
   444
	if (!$auth->acl_get('a_') && !$auth->acl_get('m_', $forum_id))
indvdum@0
   445
	{
indvdum@0
   446
		if (!empty($extensions[$file->get('extension')]['max_filesize']))
indvdum@0
   447
		{
indvdum@0
   448
			$allowed_filesize = $extensions[$file->get('extension')]['max_filesize'];
indvdum@0
   449
		}
indvdum@0
   450
		else
indvdum@0
   451
		{
indvdum@0
   452
			$allowed_filesize = ($is_message) ? $config['max_filesize_pm'] : $config['max_filesize'];
indvdum@0
   453
		}
indvdum@0
   454
indvdum@0
   455
		$file->upload->set_max_filesize($allowed_filesize);
indvdum@0
   456
	}
indvdum@0
   457
indvdum@0
   458
	$file->clean_filename('unique', $user->data['user_id'] . '_');
indvdum@0
   459
indvdum@0
   460
	// Are we uploading an image *and* this image being within the image category? Only then perform additional image checks.
indvdum@0
   461
	$no_image = ($cat_id == ATTACHMENT_CATEGORY_IMAGE) ? false : true;
indvdum@0
   462
indvdum@0
   463
	$file->move_file($config['upload_path'], false, $no_image);
indvdum@0
   464
indvdum@0
   465
	if (sizeof($file->error))
indvdum@0
   466
	{
indvdum@0
   467
		$file->remove();
indvdum@0
   468
		$filedata['error'] = array_merge($filedata['error'], $file->error);
indvdum@0
   469
		$filedata['post_attach'] = false;
indvdum@0
   470
indvdum@0
   471
		return $filedata;
indvdum@0
   472
	}
indvdum@0
   473
indvdum@0
   474
	$filedata['filesize'] = $file->get('filesize');
indvdum@0
   475
	$filedata['mimetype'] = $file->get('mimetype');
indvdum@0
   476
	$filedata['extension'] = $file->get('extension');
indvdum@0
   477
	$filedata['physical_filename'] = $file->get('realname');
indvdum@0
   478
	$filedata['real_filename'] = $file->get('uploadname');
indvdum@0
   479
	$filedata['filetime'] = time();
indvdum@0
   480
indvdum@0
   481
	// Check our complete quota
indvdum@0
   482
	if ($config['attachment_quota'])
indvdum@0
   483
	{
indvdum@0
   484
		if ($config['upload_dir_size'] + $file->get('filesize') > $config['attachment_quota'])
indvdum@0
   485
		{
indvdum@0
   486
			$filedata['error'][] = $user->lang['ATTACH_QUOTA_REACHED'];
indvdum@0
   487
			$filedata['post_attach'] = false;
indvdum@0
   488
indvdum@0
   489
			$file->remove();
indvdum@0
   490
indvdum@0
   491
			return $filedata;
indvdum@0
   492
		}
indvdum@0
   493
	}
indvdum@0
   494
indvdum@0
   495
	// Check free disk space
indvdum@0
   496
	if ($free_space = @disk_free_space($phpbb_root_path . $config['upload_path']))
indvdum@0
   497
	{
indvdum@0
   498
		if ($free_space <= $file->get('filesize'))
indvdum@0
   499
		{
indvdum@0
   500
			$filedata['error'][] = $user->lang['ATTACH_QUOTA_REACHED'];
indvdum@0
   501
			$filedata['post_attach'] = false;
indvdum@0
   502
indvdum@0
   503
			$file->remove();
indvdum@0
   504
indvdum@0
   505
			return $filedata;
indvdum@0
   506
		}
indvdum@0
   507
	}
indvdum@0
   508
indvdum@0
   509
	// Create Thumbnail
indvdum@0
   510
	if ($filedata['thumbnail'])
indvdum@0
   511
	{
indvdum@0
   512
		$source = $file->get('destination_file');
indvdum@0
   513
		$destination = $file->get('destination_path') . '/thumb_' . $file->get('realname');
indvdum@0
   514
indvdum@0
   515
		if (!create_thumbnail($source, $destination, $file->get('mimetype')))
indvdum@0
   516
		{
indvdum@0
   517
			$filedata['thumbnail'] = 0;
indvdum@0
   518
		}
indvdum@0
   519
	}
indvdum@0
   520
indvdum@0
   521
	return $filedata;
indvdum@0
   522
}
indvdum@0
   523
indvdum@0
   524
/**
indvdum@0
   525
* Calculate the needed size for Thumbnail
indvdum@0
   526
*/
indvdum@0
   527
function get_img_size_format($width, $height)
indvdum@0
   528
{
indvdum@0
   529
	global $config;
indvdum@0
   530
indvdum@0
   531
	// Maximum Width the Image can take
indvdum@0
   532
	$max_width = ($config['img_max_thumb_width']) ? $config['img_max_thumb_width'] : 400;
indvdum@0
   533
indvdum@0
   534
	if ($width > $height)
indvdum@0
   535
	{
indvdum@0
   536
		return array(
indvdum@0
   537
			round($width * ($max_width / $width)),
indvdum@0
   538
			round($height * ($max_width / $width))
indvdum@0
   539
		);
indvdum@0
   540
	}
indvdum@0
   541
	else
indvdum@0
   542
	{
indvdum@0
   543
		return array(
indvdum@0
   544
			round($width * ($max_width / $height)),
indvdum@0
   545
			round($height * ($max_width / $height))
indvdum@0
   546
		);
indvdum@0
   547
	}
indvdum@0
   548
}
indvdum@0
   549
indvdum@0
   550
/**
indvdum@0
   551
* Return supported image types
indvdum@0
   552
*/
indvdum@0
   553
function get_supported_image_types($type = false)
indvdum@0
   554
{
indvdum@0
   555
	if (@extension_loaded('gd'))
indvdum@0
   556
	{
indvdum@0
   557
		$format = imagetypes();
indvdum@0
   558
		$new_type = 0;
indvdum@0
   559
indvdum@0
   560
		if ($type !== false)
indvdum@0
   561
		{
indvdum@0
   562
			// Type is one of the IMAGETYPE constants - it is fetched from getimagesize()
indvdum@0
   563
			// We do not use the constants here, because some were not available in PHP 4.3.x
indvdum@0
   564
			switch ($type)
indvdum@0
   565
			{
indvdum@0
   566
				// GIF
indvdum@0
   567
				case 1:
indvdum@0
   568
					$new_type = ($format & IMG_GIF) ? IMG_GIF : false;
indvdum@0
   569
				break;
indvdum@0
   570
indvdum@0
   571
				// JPG, JPC, JP2
indvdum@0
   572
				case 2:
indvdum@0
   573
				case 9:
indvdum@0
   574
				case 10:
indvdum@0
   575
				case 11:
indvdum@0
   576
				case 12:
indvdum@0
   577
					$new_type = ($format & IMG_JPG) ? IMG_JPG : false;
indvdum@0
   578
				break;
indvdum@0
   579
indvdum@0
   580
				// PNG
indvdum@0
   581
				case 3:
indvdum@0
   582
					$new_type = ($format & IMG_PNG) ? IMG_PNG : false;
indvdum@0
   583
				break;
indvdum@0
   584
indvdum@0
   585
				// WBMP
indvdum@0
   586
				case 15:
indvdum@0
   587
					$new_type = ($format & IMG_WBMP) ? IMG_WBMP : false;
indvdum@0
   588
				break;
indvdum@0
   589
			}
indvdum@0
   590
		}
indvdum@0
   591
		else
indvdum@0
   592
		{
indvdum@0
   593
			$new_type = array();
indvdum@0
   594
			$go_through_types = array(IMG_GIF, IMG_JPG, IMG_PNG, IMG_WBMP);
indvdum@0
   595
indvdum@0
   596
			foreach ($go_through_types as $check_type)
indvdum@0
   597
			{
indvdum@0
   598
				if ($format & $check_type)
indvdum@0
   599
				{
indvdum@0
   600
					$new_type[] = $check_type;
indvdum@0
   601
				}
indvdum@0
   602
			}
indvdum@0
   603
		}
indvdum@0
   604
indvdum@0
   605
		return array(
indvdum@0
   606
			'gd'		=> ($new_type) ? true : false,
indvdum@0
   607
			'format'	=> $new_type,
indvdum@0
   608
			'version'	=> (function_exists('imagecreatetruecolor')) ? 2 : 1
indvdum@0
   609
		);
indvdum@0
   610
	}
indvdum@0
   611
indvdum@0
   612
	return array('gd' => false);
indvdum@0
   613
}
indvdum@0
   614
indvdum@0
   615
/**
indvdum@0
   616
* Create Thumbnail
indvdum@0
   617
*/
indvdum@0
   618
function create_thumbnail($source, $destination, $mimetype)
indvdum@0
   619
{
indvdum@0
   620
	global $config;
indvdum@0
   621
indvdum@0
   622
	$min_filesize = (int) $config['img_min_thumb_filesize'];
indvdum@0
   623
	$img_filesize = (file_exists($source)) ? @filesize($source) : false;
indvdum@0
   624
indvdum@0
   625
	if (!$img_filesize || $img_filesize <= $min_filesize)
indvdum@0
   626
	{
indvdum@0
   627
		return false;
indvdum@0
   628
	}
indvdum@0
   629
indvdum@0
   630
	$dimension = @getimagesize($source);
indvdum@0
   631
indvdum@0
   632
	if ($dimension === false)
indvdum@0
   633
	{
indvdum@0
   634
		return false;
indvdum@0
   635
	}
indvdum@0
   636
indvdum@0
   637
	list($width, $height, $type, ) = $dimension;
indvdum@0
   638
indvdum@0
   639
	if (empty($width) || empty($height))
indvdum@0
   640
	{
indvdum@0
   641
		return false;
indvdum@0
   642
	}
indvdum@0
   643
indvdum@0
   644
	list($new_width, $new_height) = get_img_size_format($width, $height);
indvdum@0
   645
indvdum@0
   646
	// Do not create a thumbnail if the resulting width/height is bigger than the original one
indvdum@0
   647
	if ($new_width >= $width && $new_height >= $height)
indvdum@0
   648
	{
indvdum@0
   649
		return false;
indvdum@0
   650
	}
indvdum@0
   651
indvdum@0
   652
	$used_imagick = false;
indvdum@0
   653
indvdum@0
   654
	// Only use imagemagick if defined and the passthru function not disabled
indvdum@0
   655
	if ($config['img_imagick'] && function_exists('passthru'))
indvdum@0
   656
	{
indvdum@0
   657
		if (substr($config['img_imagick'], -1) !== '/')
indvdum@0
   658
		{
indvdum@0
   659
			$config['img_imagick'] .= '/';
indvdum@0
   660
		}
indvdum@0
   661
indvdum@0
   662
		@passthru(escapeshellcmd($config['img_imagick']) . 'convert' . ((defined('PHP_OS') && preg_match('#^win#i', PHP_OS)) ? '.exe' : '') . ' -quality 85 -geometry ' . $new_width . 'x' . $new_height . ' "' . str_replace('\\', '/', $source) . '" "' . str_replace('\\', '/', $destination) . '"');
indvdum@0
   663
indvdum@0
   664
		if (file_exists($destination))
indvdum@0
   665
		{
indvdum@0
   666
			$used_imagick = true;
indvdum@0
   667
		}
indvdum@0
   668
	}
indvdum@0
   669
indvdum@0
   670
	if (!$used_imagick)
indvdum@0
   671
	{
indvdum@0
   672
		$type = get_supported_image_types($type);
indvdum@0
   673
indvdum@0
   674
		if ($type['gd'])
indvdum@0
   675
		{
indvdum@0
   676
			// If the type is not supported, we are not able to create a thumbnail
indvdum@0
   677
			if ($type['format'] === false)
indvdum@0
   678
			{
indvdum@0
   679
				return false;
indvdum@0
   680
			}
indvdum@0
   681
indvdum@0
   682
			switch ($type['format'])
indvdum@0
   683
			{
indvdum@0
   684
				case IMG_GIF:
indvdum@0
   685
					$image = @imagecreatefromgif($source);
indvdum@0
   686
				break;
indvdum@0
   687
indvdum@0
   688
				case IMG_JPG:
indvdum@0
   689
					@ini_set('gd.jpeg_ignore_warning', 1);
indvdum@0
   690
					$image = @imagecreatefromjpeg($source);
indvdum@0
   691
				break;
indvdum@0
   692
indvdum@0
   693
				case IMG_PNG:
indvdum@0
   694
					$image = @imagecreatefrompng($source);
indvdum@0
   695
				break;
indvdum@0
   696
indvdum@0
   697
				case IMG_WBMP:
indvdum@0
   698
					$image = @imagecreatefromwbmp($source);
indvdum@0
   699
				break;
indvdum@0
   700
			}
indvdum@0
   701
indvdum@0
   702
			if (empty($image))
indvdum@0
   703
			{
indvdum@0
   704
				return false;
indvdum@0
   705
			}
indvdum@0
   706
indvdum@0
   707
			if ($type['version'] == 1)
indvdum@0
   708
			{
indvdum@0
   709
				$new_image = imagecreate($new_width, $new_height);
indvdum@0
   710
indvdum@0
   711
				if ($new_image === false)
indvdum@0
   712
				{
indvdum@0
   713
					return false;
indvdum@0
   714
				}
indvdum@0
   715
indvdum@0
   716
				imagecopyresized($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
indvdum@0
   717
			}
indvdum@0
   718
			else
indvdum@0
   719
			{
indvdum@0
   720
				$new_image = imagecreatetruecolor($new_width, $new_height);
indvdum@0
   721
indvdum@0
   722
				if ($new_image === false)
indvdum@0
   723
				{
indvdum@0
   724
					return false;
indvdum@0
   725
				}
indvdum@0
   726
indvdum@0
   727
				// Preserve alpha transparency (png for example)
indvdum@0
   728
				@imagealphablending($new_image, false);
indvdum@0
   729
				@imagesavealpha($new_image, true);
indvdum@0
   730
indvdum@0
   731
				imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
indvdum@0
   732
			}
indvdum@0
   733
indvdum@0
   734
			// If we are in safe mode create the destination file prior to using the gd functions to circumvent a PHP bug
indvdum@0
   735
			if (@ini_get('safe_mode') || @strtolower(ini_get('safe_mode')) == 'on')
indvdum@0
   736
			{
indvdum@0
   737
				@touch($destination);
indvdum@0
   738
			}
indvdum@0
   739
indvdum@0
   740
			switch ($type['format'])
indvdum@0
   741
			{
indvdum@0
   742
				case IMG_GIF:
indvdum@0
   743
					imagegif($new_image, $destination);
indvdum@0
   744
				break;
indvdum@0
   745
indvdum@0
   746
				case IMG_JPG:
indvdum@0
   747
					imagejpeg($new_image, $destination, 90);
indvdum@0
   748
				break;
indvdum@0
   749
indvdum@0
   750
				case IMG_PNG:
indvdum@0
   751
					imagepng($new_image, $destination);
indvdum@0
   752
				break;
indvdum@0
   753
indvdum@0
   754
				case IMG_WBMP:
indvdum@0
   755
					imagewbmp($new_image, $destination);
indvdum@0
   756
				break;
indvdum@0
   757
			}
indvdum@0
   758
indvdum@0
   759
			imagedestroy($new_image);
indvdum@0
   760
		}
indvdum@0
   761
		else
indvdum@0
   762
		{
indvdum@0
   763
			return false;
indvdum@0
   764
		}
indvdum@0
   765
	}
indvdum@0
   766
indvdum@0
   767
	if (!file_exists($destination))
indvdum@0
   768
	{
indvdum@0
   769
		return false;
indvdum@0
   770
	}
indvdum@0
   771
indvdum@0
   772
	phpbb_chmod($destination, CHMOD_READ | CHMOD_WRITE);
indvdum@0
   773
indvdum@0
   774
	return true;
indvdum@0
   775
}
indvdum@0
   776
indvdum@0
   777
/**
indvdum@0
   778
* Assign Inline attachments (build option fields)
indvdum@0
   779
*/
indvdum@0
   780
function posting_gen_inline_attachments(&$attachment_data)
indvdum@0
   781
{
indvdum@0
   782
	global $template;
indvdum@0
   783
indvdum@0
   784
	if (sizeof($attachment_data))
indvdum@0
   785
	{
indvdum@0
   786
		$s_inline_attachment_options = '';
indvdum@0
   787
indvdum@0
   788
		foreach ($attachment_data as $i => $attachment)
indvdum@0
   789
		{
indvdum@0
   790
			$s_inline_attachment_options .= '<option value="' . $i . '">' . utf8_basename($attachment['real_filename']) . '</option>';
indvdum@0
   791
		}
indvdum@0
   792
indvdum@0
   793
		$template->assign_var('S_INLINE_ATTACHMENT_OPTIONS', $s_inline_attachment_options);
indvdum@0
   794
indvdum@0
   795
		return true;
indvdum@0
   796
	}
indvdum@0
   797
indvdum@0
   798
	return false;
indvdum@0
   799
}
indvdum@0
   800
indvdum@0
   801
/**
indvdum@0
   802
* Generate inline attachment entry
indvdum@0
   803
*/
indvdum@0
   804
function posting_gen_attachment_entry($attachment_data, &$filename_data, $show_attach_box = true)
indvdum@0
   805
{
indvdum@0
   806
	global $template, $config, $phpbb_root_path, $phpEx, $user, $auth;
indvdum@0
   807
indvdum@0
   808
	// Some default template variables
indvdum@0
   809
	$template->assign_vars(array(
indvdum@0
   810
		'S_SHOW_ATTACH_BOX'	=> $show_attach_box,
indvdum@0
   811
		'S_HAS_ATTACHMENTS'	=> sizeof($attachment_data),
indvdum@0
   812
		'FILESIZE'			=> $config['max_filesize'],
indvdum@0
   813
		'FILE_COMMENT'		=> (isset($filename_data['filecomment'])) ? $filename_data['filecomment'] : '',
indvdum@0
   814
	));
indvdum@0
   815
indvdum@0
   816
	if (sizeof($attachment_data))
indvdum@0
   817
	{
indvdum@0
   818
		// We display the posted attachments within the desired order.
indvdum@0
   819
		($config['display_order']) ? krsort($attachment_data) : ksort($attachment_data);
indvdum@0
   820
indvdum@0
   821
		foreach ($attachment_data as $count => $attach_row)
indvdum@0
   822
		{
indvdum@0
   823
			$hidden = '';
indvdum@0
   824
			$attach_row['real_filename'] = utf8_basename($attach_row['real_filename']);
indvdum@0
   825
indvdum@0
   826
			foreach ($attach_row as $key => $value)
indvdum@0
   827
			{
indvdum@0
   828
				$hidden .= '<input type="hidden" name="attachment_data[' . $count . '][' . $key . ']" value="' . $value . '" />';
indvdum@0
   829
			}
indvdum@0
   830
indvdum@0
   831
			$download_link = append_sid("{$phpbb_root_path}download/file.$phpEx", 'mode=view&amp;id=' . (int) $attach_row['attach_id'], true, ($attach_row['is_orphan']) ? $user->session_id : false);
indvdum@0
   832
indvdum@0
   833
			$template->assign_block_vars('attach_row', array(
indvdum@0
   834
				'FILENAME'			=> utf8_basename($attach_row['real_filename']),
indvdum@0
   835
				'A_FILENAME'		=> addslashes(utf8_basename($attach_row['real_filename'])),
indvdum@0
   836
				'FILE_COMMENT'		=> $attach_row['attach_comment'],
indvdum@0
   837
				'ATTACH_ID'			=> $attach_row['attach_id'],
indvdum@0
   838
				'S_IS_ORPHAN'		=> $attach_row['is_orphan'],
indvdum@0
   839
				'ASSOC_INDEX'		=> $count,
indvdum@0
   840
indvdum@0
   841
				'U_VIEW_ATTACHMENT'	=> $download_link,
indvdum@0
   842
				'S_HIDDEN'			=> $hidden)
indvdum@0
   843
			);
indvdum@0
   844
		}
indvdum@0
   845
	}
indvdum@0
   846
indvdum@0
   847
	return sizeof($attachment_data);
indvdum@0
   848
}
indvdum@0
   849
indvdum@0
   850
//
indvdum@0
   851
// General Post functions
indvdum@0
   852
//
indvdum@0
   853
indvdum@0
   854
/**
indvdum@0
   855
* Load Drafts
indvdum@0
   856
*/
indvdum@0
   857
function load_drafts($topic_id = 0, $forum_id = 0, $id = 0, $pm_action = '', $msg_id = 0)
indvdum@0
   858
{
indvdum@0
   859
	global $user, $db, $template, $auth;
indvdum@0
   860
	global $phpbb_root_path, $phpEx;
indvdum@0
   861
indvdum@0
   862
	$topic_ids = $forum_ids = $draft_rows = array();
indvdum@0
   863
indvdum@0
   864
	// Load those drafts not connected to forums/topics
indvdum@0
   865
	// If forum_id == 0 AND topic_id == 0 then this is a PM draft
indvdum@0
   866
	if (!$topic_id && !$forum_id)
indvdum@0
   867
	{
indvdum@0
   868
		$sql_and = ' AND d.forum_id = 0 AND d.topic_id = 0';
indvdum@0
   869
	}
indvdum@0
   870
	else
indvdum@0
   871
	{
indvdum@0
   872
		$sql_and = '';
indvdum@0
   873
		$sql_and .= ($forum_id) ? ' AND d.forum_id = ' . (int) $forum_id : '';
indvdum@0
   874
		$sql_and .= ($topic_id) ? ' AND d.topic_id = ' . (int) $topic_id : '';
indvdum@0
   875
	}
indvdum@0
   876
indvdum@0
   877
	$sql = 'SELECT d.*, f.forum_id, f.forum_name
indvdum@0
   878
		FROM ' . DRAFTS_TABLE . ' d
indvdum@0
   879
		LEFT JOIN ' . FORUMS_TABLE . ' f ON (f.forum_id = d.forum_id)
indvdum@0
   880
			WHERE d.user_id = ' . $user->data['user_id'] . "
indvdum@0
   881
			$sql_and
indvdum@0
   882
		ORDER BY d.save_time DESC";
indvdum@0
   883
	$result = $db->sql_query($sql);
indvdum@0
   884
indvdum@0
   885
	while ($row = $db->sql_fetchrow($result))
indvdum@0
   886
	{
indvdum@0
   887
		if ($row['topic_id'])
indvdum@0
   888
		{
indvdum@0
   889
			$topic_ids[] = (int) $row['topic_id'];
indvdum@0
   890
		}
indvdum@0
   891
		$draft_rows[] = $row;
indvdum@0
   892
	}
indvdum@0
   893
	$db->sql_freeresult($result);
indvdum@0
   894
indvdum@0
   895
	if (!sizeof($draft_rows))
indvdum@0
   896
	{
indvdum@0
   897
		return;
indvdum@0
   898
	}
indvdum@0
   899
indvdum@0
   900
	$topic_rows = array();
indvdum@0
   901
	if (sizeof($topic_ids))
indvdum@0
   902
	{
indvdum@0
   903
		$sql = 'SELECT topic_id, forum_id, topic_title
indvdum@0
   904
			FROM ' . TOPICS_TABLE . '
indvdum@0
   905
			WHERE ' . $db->sql_in_set('topic_id', array_unique($topic_ids));
indvdum@0
   906
		$result = $db->sql_query($sql);
indvdum@0
   907
indvdum@0
   908
		while ($row = $db->sql_fetchrow($result))
indvdum@0
   909
		{
indvdum@0
   910
			$topic_rows[$row['topic_id']] = $row;
indvdum@0
   911
		}
indvdum@0
   912
		$db->sql_freeresult($result);
indvdum@0
   913
	}
indvdum@0
   914
	unset($topic_ids);
indvdum@0
   915
indvdum@0
   916
	$template->assign_var('S_SHOW_DRAFTS', true);
indvdum@0
   917
indvdum@0
   918
	foreach ($draft_rows as $draft)
indvdum@0
   919
	{
indvdum@0
   920
		$link_topic = $link_forum = $link_pm = false;
indvdum@0
   921
		$insert_url = $view_url = $title = '';
indvdum@0
   922
indvdum@0
   923
		if (isset($topic_rows[$draft['topic_id']])
indvdum@0
   924
			&& (
indvdum@0
   925
				($topic_rows[$draft['topic_id']]['forum_id'] && $auth->acl_get('f_read', $topic_rows[$draft['topic_id']]['forum_id']))
indvdum@0
   926
				||
indvdum@0
   927
				(!$topic_rows[$draft['topic_id']]['forum_id'] && $auth->acl_getf_global('f_read'))
indvdum@0
   928
			))
indvdum@0
   929
		{
indvdum@0
   930
			$topic_forum_id = ($topic_rows[$draft['topic_id']]['forum_id']) ? $topic_rows[$draft['topic_id']]['forum_id'] : $forum_id;
indvdum@0
   931
indvdum@0
   932
			$link_topic = true;
indvdum@0
   933
			$view_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $topic_forum_id . '&amp;t=' . $draft['topic_id']);
indvdum@0
   934
			$title = $topic_rows[$draft['topic_id']]['topic_title'];
indvdum@0
   935
indvdum@0
   936
			$insert_url = append_sid("{$phpbb_root_path}posting.$phpEx", 'f=' . $topic_forum_id . '&amp;t=' . $draft['topic_id'] . '&amp;mode=reply&amp;d=' . $draft['draft_id']);
indvdum@0
   937
		}
indvdum@0
   938
		else if ($draft['forum_id'] && $auth->acl_get('f_read', $draft['forum_id']))
indvdum@0
   939
		{
indvdum@0
   940
			$link_forum = true;
indvdum@0
   941
			$view_url = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $draft['forum_id']);
indvdum@0
   942
			$title = $draft['forum_name'];
indvdum@0
   943
indvdum@0
   944
			$insert_url = append_sid("{$phpbb_root_path}posting.$phpEx", 'f=' . $draft['forum_id'] . '&amp;mode=post&amp;d=' . $draft['draft_id']);
indvdum@0
   945
		}
indvdum@0
   946
		else
indvdum@0
   947
		{
indvdum@0
   948
			// Either display as PM draft if forum_id and topic_id are empty or if access to the forums has been denied afterwards...
indvdum@0
   949
			$link_pm = true;
indvdum@0
   950
			$insert_url = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=$id&amp;mode=compose&amp;d={$draft['draft_id']}" . (($pm_action) ? "&amp;action=$pm_action" : '') . (($msg_id) ? "&amp;p=$msg_id" : ''));
indvdum@0
   951
		}
indvdum@0
   952
indvdum@0
   953
		$template->assign_block_vars('draftrow', array(
indvdum@0
   954
			'DRAFT_ID'		=> $draft['draft_id'],
indvdum@0
   955
			'DATE'			=> $user->format_date($draft['save_time']),
indvdum@0
   956
			'DRAFT_SUBJECT'	=> $draft['draft_subject'],
indvdum@0
   957
indvdum@0
   958
			'TITLE'			=> $title,
indvdum@0
   959
			'U_VIEW'		=> $view_url,
indvdum@0
   960
			'U_INSERT'		=> $insert_url,
indvdum@0
   961
indvdum@0
   962
			'S_LINK_PM'		=> $link_pm,
indvdum@0
   963
			'S_LINK_TOPIC'	=> $link_topic,
indvdum@0
   964
			'S_LINK_FORUM'	=> $link_forum)
indvdum@0
   965
		);
indvdum@0
   966
	}
indvdum@0
   967
}
indvdum@0
   968
indvdum@0
   969
/**
indvdum@0
   970
* Topic Review
indvdum@0
   971
*/
indvdum@0
   972
function topic_review($topic_id, $forum_id, $mode = 'topic_review', $cur_post_id = 0, $show_quote_button = true)
indvdum@0
   973
{
indvdum@0
   974
	global $user, $auth, $db, $template, $bbcode, $cache;
indvdum@0
   975
	global $config, $phpbb_root_path, $phpEx;
indvdum@0
   976
indvdum@0
   977
	// Go ahead and pull all data for this topic
indvdum@0
   978
	$sql = 'SELECT p.post_id
indvdum@0
   979
		FROM ' . POSTS_TABLE . ' p' . "
indvdum@0
   980
		WHERE p.topic_id = $topic_id
indvdum@0
   981
			" . ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND p.post_approved = 1' : '') . '
indvdum@0
   982
			' . (($mode == 'post_review') ? " AND p.post_id > $cur_post_id" : '') . '
indvdum@0
   983
			' . (($mode == 'post_review_edit') ? " AND p.post_id = $cur_post_id" : '') . '
indvdum@0
   984
		ORDER BY p.post_time ';
indvdum@0
   985
	$sql .= ($mode == 'post_review') ? 'ASC' : 'DESC';
indvdum@0
   986
	$result = $db->sql_query_limit($sql, $config['posts_per_page']);
indvdum@0
   987
indvdum@0
   988
	$post_list = array();
indvdum@0
   989
indvdum@0
   990
	while ($row = $db->sql_fetchrow($result))
indvdum@0
   991
	{
indvdum@0
   992
		$post_list[] = $row['post_id'];
indvdum@0
   993
	}
indvdum@0
   994
indvdum@0
   995
	$db->sql_freeresult($result);
indvdum@0
   996
indvdum@0
   997
	if (!sizeof($post_list))
indvdum@0
   998
	{
indvdum@0
   999
		return false;
indvdum@0
  1000
	}
indvdum@0
  1001
indvdum@0
  1002
	// Handle 'post_review_edit' like 'post_review' from now on
indvdum@0
  1003
	if ($mode == 'post_review_edit')
indvdum@0
  1004
	{
indvdum@0
  1005
		$mode = 'post_review';
indvdum@0
  1006
	}
indvdum@0
  1007
indvdum@0
  1008
	$sql = $db->sql_build_query('SELECT', array(
indvdum@0
  1009
		'SELECT'	=> 'u.username, u.user_id, u.user_colour, p.*, z.friend, z.foe',
indvdum@0
  1010
indvdum@0
  1011
		'FROM'		=> array(
indvdum@0
  1012
			USERS_TABLE		=> 'u',
indvdum@0
  1013
			POSTS_TABLE		=> 'p',
indvdum@0
  1014
		),
indvdum@0
  1015
indvdum@0
  1016
		'LEFT_JOIN'	=> array(
indvdum@0
  1017
			array(
indvdum@0
  1018
				'FROM'	=> array(ZEBRA_TABLE => 'z'),
indvdum@0
  1019
				'ON'	=> 'z.user_id = ' . $user->data['user_id'] . ' AND z.zebra_id = p.poster_id'
indvdum@0
  1020
			)
indvdum@0
  1021
		),
indvdum@0
  1022
indvdum@0
  1023
		'WHERE'		=> $db->sql_in_set('p.post_id', $post_list) . '
indvdum@0
  1024
			AND u.user_id = p.poster_id'
indvdum@0
  1025
	));
indvdum@0
  1026
indvdum@0
  1027
	$result = $db->sql_query($sql);
indvdum@0
  1028
indvdum@0
  1029
	$bbcode_bitfield = '';
indvdum@0
  1030
	$rowset = array();
indvdum@0
  1031
	$has_attachments = false;
indvdum@0
  1032
	while ($row = $db->sql_fetchrow($result))
indvdum@0
  1033
	{
indvdum@0
  1034
		$rowset[$row['post_id']] = $row;
indvdum@0
  1035
		$bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
indvdum@0
  1036
indvdum@0
  1037
		if ($row['post_attachment'])
indvdum@0
  1038
		{
indvdum@0
  1039
			$has_attachments = true;
indvdum@0
  1040
		}
indvdum@0
  1041
	}
indvdum@0
  1042
	$db->sql_freeresult($result);
indvdum@0
  1043
indvdum@0
  1044
	// Instantiate BBCode class
indvdum@0
  1045
	if (!isset($bbcode) && $bbcode_bitfield !== '')
indvdum@0
  1046
	{
indvdum@0
  1047
		include_once($phpbb_root_path . 'includes/bbcode.' . $phpEx);
indvdum@0
  1048
		$bbcode = new bbcode(base64_encode($bbcode_bitfield));
indvdum@0
  1049
	}
indvdum@0
  1050
indvdum@0
  1051
	// Grab extensions
indvdum@0
  1052
	$extensions = $attachments = array();
indvdum@0
  1053
	if ($has_attachments && $auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id))
indvdum@0
  1054
	{
indvdum@0
  1055
		$extensions = $cache->obtain_attach_extensions($forum_id);
indvdum@0
  1056
indvdum@0
  1057
		// Get attachments...
indvdum@0
  1058
		$sql = 'SELECT *
indvdum@0
  1059
			FROM ' . ATTACHMENTS_TABLE . '
indvdum@0
  1060
			WHERE ' . $db->sql_in_set('post_msg_id', $post_list) . '
indvdum@0
  1061
				AND in_message = 0
indvdum@0
  1062
			ORDER BY filetime DESC, post_msg_id ASC';
indvdum@0
  1063
		$result = $db->sql_query($sql);
indvdum@0
  1064
indvdum@0
  1065
		while ($row = $db->sql_fetchrow($result))
indvdum@0
  1066
		{
indvdum@0
  1067
			$attachments[$row['post_msg_id']][] = $row;
indvdum@0
  1068
		}
indvdum@0
  1069
		$db->sql_freeresult($result);
indvdum@0
  1070
	}
indvdum@0
  1071
indvdum@0
  1072
	for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
indvdum@0
  1073
	{
indvdum@0
  1074
		// A non-existing rowset only happens if there was no user present for the entered poster_id
indvdum@0
  1075
		// This could be a broken posts table.
indvdum@0
  1076
		if (!isset($rowset[$post_list[$i]]))
indvdum@0
  1077
		{
indvdum@0
  1078
			continue;
indvdum@0
  1079
		}
indvdum@0
  1080
indvdum@0
  1081
		$row =& $rowset[$post_list[$i]];
indvdum@0
  1082
indvdum@0
  1083
		$poster_id		= $row['user_id'];
indvdum@0
  1084
		$post_subject	= $row['post_subject'];
indvdum@0
  1085
		$message		= censor_text($row['post_text']);
indvdum@0
  1086
indvdum@0
  1087
		$decoded_message = false;
indvdum@0
  1088
indvdum@0
  1089
		if ($show_quote_button && $auth->acl_get('f_reply', $forum_id))
indvdum@0
  1090
		{
indvdum@0
  1091
			$decoded_message = $message;
indvdum@0
  1092
			decode_message($decoded_message, $row['bbcode_uid']);
indvdum@0
  1093
indvdum@0
  1094
			$decoded_message = bbcode_nl2br($decoded_message);
indvdum@0
  1095
		}
indvdum@0
  1096
indvdum@0
  1097
		if ($row['bbcode_bitfield'])
indvdum@0
  1098
		{
indvdum@0
  1099
			$bbcode->bbcode_second_pass($message, $row['bbcode_uid'], $row['bbcode_bitfield']);
indvdum@0
  1100
		}
indvdum@0
  1101
indvdum@0
  1102
		$message = bbcode_nl2br($message);
indvdum@0
  1103
		$message = smiley_text($message, !$row['enable_smilies']);
indvdum@0
  1104
indvdum@0
  1105
		if (!empty($attachments[$row['post_id']]))
indvdum@0
  1106
		{
indvdum@0
  1107
			$update_count = array();
indvdum@0
  1108
			parse_attachments($forum_id, $message, $attachments[$row['post_id']], $update_count);
indvdum@0
  1109
		}
indvdum@0
  1110
indvdum@0
  1111
		$post_subject = censor_text($post_subject);
indvdum@0
  1112
indvdum@0
  1113
		$post_anchor = ($mode == 'post_review') ? 'ppr' . $row['post_id'] : 'pr' . $row['post_id'];
indvdum@0
  1114
		$u_show_post = append_sid($phpbb_root_path . 'viewtopic.' . $phpEx, "f=$forum_id&amp;t=$topic_id&amp;p={$row['post_id']}&amp;view=show#p{$row['post_id']}");
indvdum@0
  1115
indvdum@0
  1116
		$template->assign_block_vars($mode . '_row', array(
indvdum@0
  1117
			'POST_AUTHOR_FULL'		=> get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
indvdum@0
  1118
			'POST_AUTHOR_COLOUR'	=> get_username_string('colour', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
indvdum@0
  1119
			'POST_AUTHOR'			=> get_username_string('username', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
indvdum@0
  1120
			'U_POST_AUTHOR'			=> get_username_string('profile', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
indvdum@0
  1121
indvdum@0
  1122
			'S_HAS_ATTACHMENTS'	=> (!empty($attachments[$row['post_id']])) ? true : false,
indvdum@0
  1123
			'S_FRIEND'			=> ($row['friend']) ? true : false,
indvdum@0
  1124
			'S_IGNORE_POST'		=> ($row['foe']) ? true : false,
indvdum@0
  1125
			'L_IGNORE_POST'		=> ($row['foe']) ? sprintf($user->lang['POST_BY_FOE'], get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']), "<a href=\"{$u_show_post}\" onclick=\"dE('{$post_anchor}', 1); return false;\">", '</a>') : '',
indvdum@0
  1126
indvdum@0
  1127
			'POST_SUBJECT'		=> $post_subject,
indvdum@0
  1128
			'MINI_POST_IMG'		=> $user->img('icon_post_target', $user->lang['POST']),
indvdum@0
  1129
			'POST_DATE'			=> $user->format_date($row['post_time']),
indvdum@0
  1130
			'MESSAGE'			=> $message,
indvdum@0
  1131
			'DECODED_MESSAGE'	=> $decoded_message,
indvdum@0
  1132
			'POST_ID'			=> $row['post_id'],
indvdum@0
  1133
			'U_MINI_POST'		=> append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'p=' . $row['post_id']) . '#p' . $row['post_id'],
indvdum@0
  1134
			'U_MCP_DETAILS'		=> ($auth->acl_get('m_info', $forum_id)) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=main&amp;mode=post_details&amp;f=' . $forum_id . '&amp;p=' . $row['post_id'], true, $user->session_id) : '',
indvdum@0
  1135
			'POSTER_QUOTE'		=> ($show_quote_button && $auth->acl_get('f_reply', $forum_id)) ? addslashes(get_username_string('username', $poster_id, $row['username'], $row['user_colour'], $row['post_username'])) : '')
indvdum@0
  1136
		);
indvdum@0
  1137
indvdum@0
  1138
		// Display not already displayed Attachments for this post, we already parsed them. ;)
indvdum@0
  1139
		if (!empty($attachments[$row['post_id']]))
indvdum@0
  1140
		{
indvdum@0
  1141
			foreach ($attachments[$row['post_id']] as $attachment)
indvdum@0
  1142
			{
indvdum@0
  1143
				$template->assign_block_vars($mode . '_row.attachment', array(
indvdum@0
  1144
					'DISPLAY_ATTACHMENT'	=> $attachment)
indvdum@0
  1145
				);
indvdum@0
  1146
			}
indvdum@0
  1147
		}
indvdum@0
  1148
indvdum@0
  1149
		unset($rowset[$post_list[$i]]);
indvdum@0
  1150
	}
indvdum@0
  1151
indvdum@0
  1152
	if ($mode == 'topic_review')
indvdum@0
  1153
	{
indvdum@0
  1154
		$template->assign_var('QUOTE_IMG', $user->img('icon_post_quote', $user->lang['REPLY_WITH_QUOTE']));
indvdum@0
  1155
	}
indvdum@0
  1156
indvdum@0
  1157
	return true;
indvdum@0
  1158
}
indvdum@0
  1159
indvdum@0
  1160
/**
indvdum@0
  1161
* User Notification
indvdum@0
  1162
*/
indvdum@0
  1163
function user_notification($mode, $subject, $topic_title, $forum_name, $forum_id, $topic_id, $post_id)
indvdum@0
  1164
{
indvdum@0
  1165
	global $db, $user, $config, $phpbb_root_path, $phpEx, $auth;
indvdum@0
  1166
indvdum@0
  1167
	$topic_notification = ($mode == 'reply' || $mode == 'quote') ? true : false;
indvdum@0
  1168
	$forum_notification = ($mode == 'post') ? true : false;
indvdum@0
  1169
indvdum@0
  1170
	if (!$topic_notification && !$forum_notification)
indvdum@0
  1171
	{
indvdum@0
  1172
		trigger_error('NO_MODE');
indvdum@0
  1173
	}
indvdum@0
  1174
indvdum@0
  1175
	if (($topic_notification && !$config['allow_topic_notify']) || ($forum_notification && !$config['allow_forum_notify']))
indvdum@0
  1176
	{
indvdum@0
  1177
		return;
indvdum@0
  1178
	}
indvdum@0
  1179
indvdum@0
  1180
	$topic_title = ($topic_notification) ? $topic_title : $subject;
indvdum@0
  1181
	$topic_title = censor_text($topic_title);
indvdum@0
  1182
indvdum@0
  1183
	// Get banned User ID's
indvdum@0
  1184
	$sql = 'SELECT ban_userid
indvdum@0
  1185
		FROM ' . BANLIST_TABLE . '
indvdum@0
  1186
		WHERE ban_userid <> 0
indvdum@0
  1187
			AND ban_exclude <> 1';
indvdum@0
  1188
	$result = $db->sql_query($sql);
indvdum@0
  1189
indvdum@0
  1190
	$sql_ignore_users = ANONYMOUS . ', ' . $user->data['user_id'];
indvdum@0
  1191
	while ($row = $db->sql_fetchrow($result))
indvdum@0
  1192
	{
indvdum@0
  1193
		$sql_ignore_users .= ', ' . (int) $row['ban_userid'];
indvdum@0
  1194
	}
indvdum@0
  1195
	$db->sql_freeresult($result);
indvdum@0
  1196
indvdum@0
  1197
	$notify_rows = array();
indvdum@0
  1198
indvdum@0
  1199
	// -- get forum_userids	|| topic_userids
indvdum@0
  1200
	$sql = 'SELECT u.user_id, u.username, u.user_email, u.user_lang, u.user_notify_type, u.user_jabber
indvdum@0
  1201
		FROM ' . (($topic_notification) ? TOPICS_WATCH_TABLE : FORUMS_WATCH_TABLE) . ' w, ' . USERS_TABLE . ' u
indvdum@0
  1202
		WHERE w.' . (($topic_notification) ? 'topic_id' : 'forum_id') . ' = ' . (($topic_notification) ? $topic_id : $forum_id) . "
indvdum@0
  1203
			AND w.user_id NOT IN ($sql_ignore_users)
indvdum@0
  1204
			AND w.notify_status = " . NOTIFY_YES . '
indvdum@0
  1205
			AND u.user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')
indvdum@0
  1206
			AND u.user_id = w.user_id';
indvdum@0
  1207
	$result = $db->sql_query($sql);
indvdum@0
  1208
indvdum@0
  1209
	while ($row = $db->sql_fetchrow($result))
indvdum@0
  1210
	{
indvdum@0
  1211
		$notify_rows[$row['user_id']] = array(
indvdum@0
  1212
			'user_id'		=> $row['user_id'],
indvdum@0
  1213
			'username'		=> $row['username'],
indvdum@0
  1214
			'user_email'	=> $row['user_email'],
indvdum@0
  1215
			'user_jabber'	=> $row['user_jabber'],
indvdum@0
  1216
			'user_lang'		=> $row['user_lang'],
indvdum@0
  1217
			'notify_type'	=> ($topic_notification) ? 'topic' : 'forum',
indvdum@0
  1218
			'template'		=> ($topic_notification) ? 'topic_notify' : 'newtopic_notify',
indvdum@0
  1219
			'method'		=> $row['user_notify_type'],
indvdum@0
  1220
			'allowed'		=> false
indvdum@0
  1221
		);
indvdum@0
  1222
	}
indvdum@0
  1223
	$db->sql_freeresult($result);
indvdum@0
  1224
indvdum@0
  1225
	// forum notification is sent to those not already receiving topic notifications
indvdum@0
  1226
	if ($topic_notification)
indvdum@0
  1227
	{
indvdum@0
  1228
		if (sizeof($notify_rows))
indvdum@0
  1229
		{
indvdum@0
  1230
			$sql_ignore_users .= ', ' . implode(', ', array_keys($notify_rows));
indvdum@0
  1231
		}
indvdum@0
  1232
indvdum@0
  1233
		$sql = 'SELECT u.user_id, u.username, u.user_email, u.user_lang, u.user_notify_type, u.user_jabber
indvdum@0
  1234
			FROM ' . FORUMS_WATCH_TABLE . ' fw, ' . USERS_TABLE . " u
indvdum@0
  1235
			WHERE fw.forum_id = $forum_id
indvdum@0
  1236
				AND fw.user_id NOT IN ($sql_ignore_users)
indvdum@0
  1237
				AND fw.notify_status = " . NOTIFY_YES . '
indvdum@0
  1238
				AND u.user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')
indvdum@0
  1239
				AND u.user_id = fw.user_id';
indvdum@0
  1240
		$result = $db->sql_query($sql);
indvdum@0
  1241
indvdum@0
  1242
		while ($row = $db->sql_fetchrow($result))
indvdum@0
  1243
		{
indvdum@0
  1244
			$notify_rows[$row['user_id']] = array(
indvdum@0
  1245
				'user_id'		=> $row['user_id'],
indvdum@0
  1246
				'username'		=> $row['username'],
indvdum@0
  1247
				'user_email'	=> $row['user_email'],
indvdum@0
  1248
				'user_jabber'	=> $row['user_jabber'],
indvdum@0
  1249
				'user_lang'		=> $row['user_lang'],
indvdum@0
  1250
				'notify_type'	=> 'forum',
indvdum@0
  1251
				'template'		=> 'forum_notify',
indvdum@0
  1252
				'method'		=> $row['user_notify_type'],
indvdum@0
  1253
				'allowed'		=> false
indvdum@0
  1254
			);
indvdum@0
  1255
		}
indvdum@0
  1256
		$db->sql_freeresult($result);
indvdum@0
  1257
	}
indvdum@0
  1258
indvdum@0
  1259
	if (!sizeof($notify_rows))
indvdum@0
  1260
	{
indvdum@0
  1261
		return;
indvdum@0
  1262
	}
indvdum@0
  1263
indvdum@0
  1264
	// Make sure users are allowed to read the forum
indvdum@0
  1265
	foreach ($auth->acl_get_list(array_keys($notify_rows), 'f_read', $forum_id) as $forum_id => $forum_ary)
indvdum@0
  1266
	{
indvdum@0
  1267
		foreach ($forum_ary as $auth_option => $user_ary)
indvdum@0
  1268
		{
indvdum@0
  1269
			foreach ($user_ary as $user_id)
indvdum@0
  1270
			{
indvdum@0
  1271
				$notify_rows[$user_id]['allowed'] = true;
indvdum@0
  1272
			}
indvdum@0
  1273
		}
indvdum@0
  1274
	}
indvdum@0
  1275
indvdum@0
  1276
indvdum@0
  1277
	// Now, we have to do a little step before really sending, we need to distinguish our users a little bit. ;)
indvdum@0
  1278
	$msg_users = $delete_ids = $update_notification = array();
indvdum@0
  1279
	foreach ($notify_rows as $user_id => $row)
indvdum@0
  1280
	{
indvdum@0
  1281
		if (!$row['allowed'] || !trim($row['user_email']))
indvdum@0
  1282
		{
indvdum@0
  1283
			$delete_ids[$row['notify_type']][] = $row['user_id'];
indvdum@0
  1284
		}
indvdum@0
  1285
		else
indvdum@0
  1286
		{
indvdum@0
  1287
			$msg_users[] = $row;
indvdum@0
  1288
			$update_notification[$row['notify_type']][] = $row['user_id'];
indvdum@0
  1289
		}
indvdum@0
  1290
	}
indvdum@0
  1291
	unset($notify_rows);
indvdum@0
  1292
indvdum@0
  1293
	// Now, we are able to really send out notifications
indvdum@0
  1294
	if (sizeof($msg_users))
indvdum@0
  1295
	{
indvdum@0
  1296
		include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
indvdum@0
  1297
		$messenger = new messenger();
indvdum@0
  1298
indvdum@0
  1299
		$msg_list_ary = array();
indvdum@0
  1300
		foreach ($msg_users as $row)
indvdum@0
  1301
		{
indvdum@0
  1302
			$pos = (!isset($msg_list_ary[$row['template']])) ? 0 : sizeof($msg_list_ary[$row['template']]);
indvdum@0
  1303
indvdum@0
  1304
			$msg_list_ary[$row['template']][$pos]['method']	= $row['method'];
indvdum@0
  1305
			$msg_list_ary[$row['template']][$pos]['email']	= $row['user_email'];
indvdum@0
  1306
			$msg_list_ary[$row['template']][$pos]['jabber']	= $row['user_jabber'];
indvdum@0
  1307
			$msg_list_ary[$row['template']][$pos]['name']	= $row['username'];
indvdum@0
  1308
			$msg_list_ary[$row['template']][$pos]['lang']	= $row['user_lang'];
indvdum@0
  1309
			$msg_list_ary[$row['template']][$pos]['user_id']= $row['user_id'];
indvdum@0
  1310
		}
indvdum@0
  1311
		unset($msg_users);
indvdum@0
  1312
indvdum@0
  1313
		foreach ($msg_list_ary as $email_template => $email_list)
indvdum@0
  1314
		{
indvdum@0
  1315
			foreach ($email_list as $addr)
indvdum@0
  1316
			{
indvdum@0
  1317
				$messenger->template($email_template, $addr['lang']);
indvdum@0
  1318
indvdum@0
  1319
				$messenger->to($addr['email'], $addr['name']);
indvdum@0
  1320
				$messenger->im($addr['jabber'], $addr['name']);
indvdum@0
  1321
indvdum@0
  1322
				$messenger->assign_vars(array(
indvdum@0
  1323
					'USERNAME'		=> htmlspecialchars_decode($addr['name']),
indvdum@0
  1324
					'TOPIC_TITLE'	=> htmlspecialchars_decode($topic_title),
indvdum@0
  1325
					'FORUM_NAME'	=> htmlspecialchars_decode($forum_name),
indvdum@0
  1326
indvdum@0
  1327
					'U_FORUM'				=> generate_board_url() . "/viewforum.$phpEx?f=$forum_id",
indvdum@0
  1328
					'U_TOPIC'				=> generate_board_url() . "/viewtopic.$phpEx?f=$forum_id&t=$topic_id",
indvdum@0
  1329
					'U_NEWEST_POST'			=> generate_board_url() . "/viewtopic.$phpEx?f=$forum_id&t=$topic_id&p=$post_id&e=$post_id",
indvdum@0
  1330
					'U_STOP_WATCHING_TOPIC'	=> generate_board_url() . "/viewtopic.$phpEx?uid={$addr['user_id']}&f=$forum_id&t=$topic_id&unwatch=topic",
indvdum@0
  1331
					'U_STOP_WATCHING_FORUM'	=> generate_board_url() . "/viewforum.$phpEx?uid={$addr['user_id']}&f=$forum_id&unwatch=forum",
indvdum@0
  1332
				));
indvdum@0
  1333
indvdum@0
  1334
				$messenger->send($addr['method']);
indvdum@0
  1335
			}
indvdum@0
  1336
		}
indvdum@0
  1337
		unset($msg_list_ary);
indvdum@0
  1338
indvdum@0
  1339
		$messenger->save_queue();
indvdum@0
  1340
	}
indvdum@0
  1341
indvdum@0
  1342
	// Handle the DB updates
indvdum@0
  1343
	$db->sql_transaction('begin');
indvdum@0
  1344
indvdum@0
  1345
	if (!empty($update_notification['topic']))
indvdum@0
  1346
	{
indvdum@0
  1347
		$sql = 'UPDATE ' . TOPICS_WATCH_TABLE . '
indvdum@0
  1348
			SET notify_status = ' . NOTIFY_NO . "
indvdum@0
  1349
			WHERE topic_id = $topic_id
indvdum@0
  1350
				AND " . $db->sql_in_set('user_id', $update_notification['topic']);
indvdum@0
  1351
		$db->sql_query($sql);
indvdum@0
  1352
	}
indvdum@0
  1353
indvdum@0
  1354
	if (!empty($update_notification['forum']))
indvdum@0
  1355
	{
indvdum@0
  1356
		$sql = 'UPDATE ' . FORUMS_WATCH_TABLE . '
indvdum@0
  1357
			SET notify_status = ' . NOTIFY_NO . "
indvdum@0
  1358
			WHERE forum_id = $forum_id
indvdum@0
  1359
				AND " . $db->sql_in_set('user_id', $update_notification['forum']);
indvdum@0
  1360
		$db->sql_query($sql);
indvdum@0
  1361
	}
indvdum@0
  1362
indvdum@0
  1363
	// Now delete the user_ids not authorised to receive notifications on this topic/forum
indvdum@0
  1364
	if (!empty($delete_ids['topic']))
indvdum@0
  1365
	{
indvdum@0
  1366
		$sql = 'DELETE FROM ' . TOPICS_WATCH_TABLE . "
indvdum@0
  1367
			WHERE topic_id = $topic_id
indvdum@0
  1368
				AND " . $db->sql_in_set('user_id', $delete_ids['topic']);
indvdum@0
  1369
		$db->sql_query($sql);
indvdum@0
  1370
	}
indvdum@0
  1371
indvdum@0
  1372
	if (!empty($delete_ids['forum']))
indvdum@0
  1373
	{
indvdum@0
  1374
		$sql = 'DELETE FROM ' . FORUMS_WATCH_TABLE . "
indvdum@0
  1375
			WHERE forum_id = $forum_id
indvdum@0
  1376
				AND " . $db->sql_in_set('user_id', $delete_ids['forum']);
indvdum@0
  1377
		$db->sql_query($sql);
indvdum@0
  1378
	}
indvdum@0
  1379
indvdum@0
  1380
	$db->sql_transaction('commit');
indvdum@0
  1381
}
indvdum@0
  1382
indvdum@0
  1383
//
indvdum@0
  1384
// Post handling functions
indvdum@0
  1385
//
indvdum@0
  1386
indvdum@0
  1387
/**
indvdum@0
  1388
* Delete Post
indvdum@0
  1389
*/
indvdum@0
  1390
function delete_post($forum_id, $topic_id, $post_id, &$data)
indvdum@0
  1391
{
indvdum@0
  1392
	global $db, $user, $auth;
indvdum@0
  1393
	global $config, $phpEx, $phpbb_root_path;
indvdum@0
  1394
indvdum@0
  1395
	// Specify our post mode
indvdum@0
  1396
	$post_mode = 'delete';
indvdum@0
  1397
	if (($data['topic_first_post_id'] === $data['topic_last_post_id']) && $data['topic_replies_real'] == 0)
indvdum@0
  1398
	{
indvdum@0
  1399
		$post_mode = 'delete_topic';
indvdum@0
  1400
	}
indvdum@0
  1401
	else if ($data['topic_first_post_id'] == $post_id)
indvdum@0
  1402
	{
indvdum@0
  1403
		$post_mode = 'delete_first_post';
indvdum@0
  1404
	}
indvdum@0
  1405
	else if ($data['topic_last_post_id'] == $post_id)
indvdum@0
  1406
	{
indvdum@0
  1407
		$post_mode = 'delete_last_post';
indvdum@0
  1408
	}
indvdum@0
  1409
	$sql_data = array();
indvdum@0
  1410
	$next_post_id = false;
indvdum@0
  1411
indvdum@0
  1412
	include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
indvdum@0
  1413
indvdum@0
  1414
	$db->sql_transaction('begin');
indvdum@0
  1415
indvdum@0
  1416
	// we must make sure to update forums that contain the shadow'd topic
indvdum@0
  1417
	if ($post_mode == 'delete_topic')
indvdum@0
  1418
	{
indvdum@0
  1419
		$shadow_forum_ids = array();
indvdum@0
  1420
indvdum@0
  1421
		$sql = 'SELECT forum_id
indvdum@0
  1422
			FROM ' . TOPICS_TABLE . '
indvdum@0
  1423
			WHERE ' . $db->sql_in_set('topic_moved_id', $topic_id);
indvdum@0
  1424
		$result = $db->sql_query($sql);
indvdum@0
  1425
		while ($row = $db->sql_fetchrow($result))
indvdum@0
  1426
		{
indvdum@0
  1427
			if (!isset($shadow_forum_ids[(int) $row['forum_id']]))
indvdum@0
  1428
			{
indvdum@0
  1429
				$shadow_forum_ids[(int) $row['forum_id']] = 1;
indvdum@0
  1430
			}
indvdum@0
  1431
			else
indvdum@0
  1432
			{
indvdum@0
  1433
				$shadow_forum_ids[(int) $row['forum_id']]++;
indvdum@0
  1434
			}
indvdum@0
  1435
		}
indvdum@0
  1436
		$db->sql_freeresult($result);
indvdum@0
  1437
	}
indvdum@0
  1438
indvdum@0
  1439
	if (!delete_posts('post_id', array($post_id), false, false))
indvdum@0
  1440
	{
indvdum@0
  1441
		// Try to delete topic, we may had an previous error causing inconsistency
indvdum@0
  1442
		if ($post_mode == 'delete_topic')
indvdum@0
  1443
		{
indvdum@0
  1444
			delete_topics('topic_id', array($topic_id), false);
indvdum@0
  1445
		}
indvdum@0
  1446
		trigger_error('ALREADY_DELETED');
indvdum@0
  1447
	}
indvdum@0
  1448
indvdum@0
  1449
	$db->sql_transaction('commit');
indvdum@0
  1450
indvdum@0
  1451
	// Collect the necessary information for updating the tables
indvdum@0
  1452
	$sql_data[FORUMS_TABLE] = '';
indvdum@0
  1453
	switch ($post_mode)
indvdum@0
  1454
	{
indvdum@0
  1455
		case 'delete_topic':
indvdum@0
  1456
indvdum@0
  1457
			foreach ($shadow_forum_ids as $updated_forum => $topic_count)
indvdum@0
  1458
			{
indvdum@0
  1459
				// counting is fun! we only have to do sizeof($forum_ids) number of queries,
indvdum@0
  1460
				// even if the topic is moved back to where its shadow lives (we count how many times it is in a forum)
indvdum@0
  1461
				$db->sql_query('UPDATE ' . FORUMS_TABLE . ' SET forum_topics_real = forum_topics_real - ' . $topic_count . ', forum_topics = forum_topics - ' . $topic_count . ' WHERE forum_id = ' . $updated_forum);
indvdum@0
  1462
				update_post_information('forum', $updated_forum);
indvdum@0
  1463
			}
indvdum@0
  1464
indvdum@0
  1465
			delete_topics('topic_id', array($topic_id), false);
indvdum@0
  1466
indvdum@0
  1467
			if ($data['topic_type'] != POST_GLOBAL)
indvdum@0
  1468
			{
indvdum@0
  1469
				$sql_data[FORUMS_TABLE] .= 'forum_topics_real = forum_topics_real - 1';
indvdum@0
  1470
				$sql_data[FORUMS_TABLE] .= ($data['topic_approved']) ? ', forum_posts = forum_posts - 1, forum_topics = forum_topics - 1' : '';
indvdum@0
  1471
			}
indvdum@0
  1472
indvdum@0
  1473
			$update_sql = update_post_information('forum', $forum_id, true);
indvdum@0
  1474
			if (sizeof($update_sql))
indvdum@0
  1475
			{
indvdum@0
  1476
				$sql_data[FORUMS_TABLE] .= ($sql_data[FORUMS_TABLE]) ? ', ' : '';
indvdum@0
  1477
				$sql_data[FORUMS_TABLE] .= implode(', ', $update_sql[$forum_id]);
indvdum@0
  1478
			}
indvdum@0
  1479
		break;
indvdum@0
  1480
indvdum@0
  1481
		case 'delete_first_post':
indvdum@0
  1482
			$sql = 'SELECT p.post_id, p.poster_id, p.post_time, p.post_username, u.username, u.user_colour
indvdum@0
  1483
				FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . " u
indvdum@0
  1484
				WHERE p.topic_id = $topic_id
indvdum@0
  1485
					AND p.poster_id = u.user_id
indvdum@0
  1486
				ORDER BY p.post_time ASC";
indvdum@0
  1487
			$result = $db->sql_query_limit($sql, 1);
indvdum@0
  1488
			$row = $db->sql_fetchrow($result);
indvdum@0
  1489
			$db->sql_freeresult($result);
indvdum@0
  1490
indvdum@0
  1491
			if ($data['topic_type'] != POST_GLOBAL)
indvdum@0
  1492
			{
indvdum@0
  1493
				$sql_data[FORUMS_TABLE] = ($data['post_approved']) ? 'forum_posts = forum_posts - 1' : '';
indvdum@0
  1494
			}
indvdum@0
  1495
indvdum@0
  1496
			$sql_data[TOPICS_TABLE] = 'topic_poster = ' . intval($row['poster_id']) . ', topic_first_post_id = ' . intval($row['post_id']) . ", topic_first_poster_colour = '" . $db->sql_escape($row['user_colour']) . "', topic_first_poster_name = '" . (($row['poster_id'] == ANONYMOUS) ? $db->sql_escape($row['post_username']) : $db->sql_escape($row['username'])) . "', topic_time = " . (int) $row['post_time'];
indvdum@0
  1497
indvdum@0
  1498
			// Decrementing topic_replies here is fine because this case only happens if there is more than one post within the topic - basically removing one "reply"
indvdum@0
  1499
			$sql_data[TOPICS_TABLE] .= ', topic_replies_real = topic_replies_real - 1' . (($data['post_approved']) ? ', topic_replies = topic_replies - 1' : '');
indvdum@0
  1500
indvdum@0
  1501
			$next_post_id = (int) $row['post_id'];
indvdum@0
  1502
		break;
indvdum@0
  1503
indvdum@0
  1504
		case 'delete_last_post':
indvdum@0
  1505
			if ($data['topic_type'] != POST_GLOBAL)
indvdum@0
  1506
			{
indvdum@0
  1507
				$sql_data[FORUMS_TABLE] = ($data['post_approved']) ? 'forum_posts = forum_posts - 1' : '';
indvdum@0
  1508
			}
indvdum@0
  1509
indvdum@0
  1510
			$update_sql = update_post_information('forum', $forum_id, true);
indvdum@0
  1511
			if (sizeof($update_sql))
indvdum@0
  1512
			{
indvdum@0
  1513
				$sql_data[FORUMS_TABLE] .= ($sql_data[FORUMS_TABLE]) ? ', ' : '';
indvdum@0
  1514
				$sql_data[FORUMS_TABLE] .= implode(', ', $update_sql[$forum_id]);
indvdum@0
  1515
			}
indvdum@0
  1516
indvdum@0
  1517
			$sql_data[TOPICS_TABLE] = 'topic_bumped = 0, topic_bumper = 0, topic_replies_real = topic_replies_real - 1' . (($data['post_approved']) ? ', topic_replies = topic_replies - 1' : '');
indvdum@0
  1518
indvdum@0
  1519
			$update_sql = update_post_information('topic', $topic_id, true);
indvdum@0
  1520
			if (sizeof($update_sql))
indvdum@0
  1521
			{
indvdum@0
  1522
				$sql_data[TOPICS_TABLE] .= ', ' . implode(', ', $update_sql[$topic_id]);
indvdum@0
  1523
				$next_post_id = (int) str_replace('topic_last_post_id = ', '', $update_sql[$topic_id][0]);
indvdum@0
  1524
			}
indvdum@0
  1525
			else
indvdum@0
  1526
			{
indvdum@0
  1527
				$sql = 'SELECT MAX(post_id) as last_post_id
indvdum@0
  1528
					FROM ' . POSTS_TABLE . "
indvdum@0
  1529
					WHERE topic_id = $topic_id " .
indvdum@0
  1530
						((!$auth->acl_get('m_approve', $forum_id)) ? 'AND post_approved = 1' : '');
indvdum@0
  1531
				$result = $db->sql_query($sql);
indvdum@0
  1532
				$row = $db->sql_fetchrow($result);
indvdum@0
  1533
				$db->sql_freeresult($result);
indvdum@0
  1534
indvdum@0
  1535
				$next_post_id = (int) $row['last_post_id'];
indvdum@0
  1536
			}
indvdum@0
  1537
		break;
indvdum@0
  1538
indvdum@0
  1539
		case 'delete':
indvdum@0
  1540
			$sql = 'SELECT post_id
indvdum@0
  1541
				FROM ' . POSTS_TABLE . "
indvdum@0
  1542
				WHERE topic_id = $topic_id " .
indvdum@0
  1543
					((!$auth->acl_get('m_approve', $forum_id)) ? 'AND post_approved = 1' : '') . '
indvdum@0
  1544
					AND post_time > ' . $data['post_time'] . '
indvdum@0
  1545
				ORDER BY post_time ASC';
indvdum@0
  1546
			$result = $db->sql_query_limit($sql, 1);
indvdum@0
  1547
			$row = $db->sql_fetchrow($result);
indvdum@0
  1548
			$db->sql_freeresult($result);
indvdum@0
  1549
indvdum@0
  1550
			if ($data['topic_type'] != POST_GLOBAL)
indvdum@0
  1551
			{
indvdum@0
  1552
				$sql_data[FORUMS_TABLE] = ($data['post_approved']) ? 'forum_posts = forum_posts - 1' : '';
indvdum@0
  1553
			}
indvdum@0
  1554
indvdum@0
  1555
			$sql_data[TOPICS_TABLE] = 'topic_replies_real = topic_replies_real - 1' . (($data['post_approved']) ? ', topic_replies = topic_replies - 1' : '');
indvdum@0
  1556
			$next_post_id = (int) $row['post_id'];
indvdum@0
  1557
		break;
indvdum@0
  1558
	}
indvdum@0
  1559
indvdum@0
  1560
	if (($post_mode == 'delete') || ($post_mode == 'delete_last_post') || ($post_mode == 'delete_first_post'))
indvdum@0
  1561
	{
indvdum@0
  1562
		$sql = 'SELECT 1 AS has_attachments
indvdum@0
  1563
			FROM ' . ATTACHMENTS_TABLE . '
indvdum@0
  1564
			WHERE topic_id = ' . $topic_id;
indvdum@0
  1565
		$result = $db->sql_query_limit($sql, 1);
indvdum@0
  1566
		$has_attachments = (int) $db->sql_fetchfield('has_attachments');
indvdum@0
  1567
		$db->sql_freeresult($result);
indvdum@0
  1568
indvdum@0
  1569
		if (!$has_attachments)
indvdum@0
  1570
		{
indvdum@0
  1571
			$sql_data[TOPICS_TABLE] .= ', topic_attachment = 0';
indvdum@0
  1572
		}
indvdum@0
  1573
	}
indvdum@0
  1574
indvdum@0
  1575
//	$sql_data[USERS_TABLE] = ($data['post_postcount']) ? 'user_posts = user_posts - 1' : '';
indvdum@0
  1576
indvdum@0
  1577
	$db->sql_transaction('begin');
indvdum@0
  1578
indvdum@0
  1579
	$where_sql = array(
indvdum@0
  1580
		FORUMS_TABLE	=> "forum_id = $forum_id",
indvdum@0
  1581
		TOPICS_TABLE	=> "topic_id = $topic_id",
indvdum@0
  1582
		USERS_TABLE		=> 'user_id = ' . $data['poster_id']
indvdum@0
  1583
	);
indvdum@0
  1584
indvdum@0
  1585
	foreach ($sql_data as $table => $update_sql)
indvdum@0
  1586
	{
indvdum@0
  1587
		if ($update_sql)
indvdum@0
  1588
		{
indvdum@0
  1589
			$db->sql_query("UPDATE $table SET $update_sql WHERE " . $where_sql[$table]);
indvdum@0
  1590
		}
indvdum@0
  1591
	}
indvdum@0
  1592
indvdum@0
  1593
	// Adjust posted info for this user by looking for a post by him/her within this topic...
indvdum@0
  1594
	if ($post_mode != 'delete_topic' && $config['load_db_track'] && $data['poster_id'] != ANONYMOUS)
indvdum@0
  1595
	{
indvdum@0
  1596
		$sql = 'SELECT poster_id
indvdum@0
  1597
			FROM ' . POSTS_TABLE . '
indvdum@0
  1598
			WHERE topic_id = ' . $topic_id . '
indvdum@0
  1599
				AND poster_id = ' . $data['poster_id'];
indvdum@0
  1600
		$result = $db->sql_query_limit($sql, 1);
indvdum@0
  1601
		$poster_id = (int) $db->sql_fetchfield('poster_id');
indvdum@0
  1602
		$db->sql_freeresult($result);
indvdum@0
  1603
indvdum@0
  1604
		// The user is not having any more posts within this topic
indvdum@0
  1605
		if (!$poster_id)
indvdum@0
  1606
		{
indvdum@0
  1607
			$sql = 'DELETE FROM ' . TOPICS_POSTED_TABLE . '
indvdum@0
  1608
				WHERE topic_id = ' . $topic_id . '
indvdum@0
  1609
					AND user_id = ' . $data['poster_id'];
indvdum@0
  1610
			$db->sql_query($sql);
indvdum@0
  1611
		}
indvdum@0
  1612
	}
indvdum@0
  1613
indvdum@0
  1614
	$db->sql_transaction('commit');
indvdum@0
  1615
indvdum@0
  1616
	if ($data['post_reported'] && ($post_mode != 'delete_topic'))
indvdum@0
  1617
	{
indvdum@0
  1618
		sync('topic_reported', 'topic_id', array($topic_id));
indvdum@0
  1619
	}
indvdum@0
  1620
indvdum@0
  1621
	return $next_post_id;
indvdum@0
  1622
}
indvdum@0
  1623
indvdum@0
  1624
/**
indvdum@0
  1625
* Submit Post
indvdum@0
  1626
* @todo Split up and create lightweight, simple API for this.
indvdum@0
  1627
*/
indvdum@0
  1628
function submit_post($mode, $subject, $username, $topic_type, &$poll, &$data, $update_message = true, $update_search_index = true)
indvdum@0
  1629
{
indvdum@0
  1630
	global $db, $auth, $user, $config, $phpEx, $template, $phpbb_root_path;
indvdum@0
  1631
indvdum@0
  1632
	// We do not handle erasing posts here
indvdum@0
  1633
	if ($mode == 'delete')
indvdum@0
  1634
	{
indvdum@0
  1635
		return false;
indvdum@0
  1636
	}
indvdum@0
  1637
indvdum@0
  1638
	$current_time = time();
indvdum@0
  1639
indvdum@0
  1640
	if ($mode == 'post')
indvdum@0
  1641
	{
indvdum@0
  1642
		$post_mode = 'post';
indvdum@0
  1643
		$update_message = true;
indvdum@0
  1644
	}
indvdum@0
  1645
	else if ($mode != 'edit')
indvdum@0
  1646
	{
indvdum@0
  1647
		$post_mode = 'reply';
indvdum@0
  1648
		$update_message = true;
indvdum@0
  1649
	}
indvdum@0
  1650
	else if ($mode == 'edit')
indvdum@0
  1651
	{
indvdum@0
  1652
		$post_mode = ($data['topic_replies_real'] == 0) ? 'edit_topic' : (($data['topic_first_post_id'] == $data['post_id']) ? 'edit_first_post' : (($data['topic_last_post_id'] == $data['post_id']) ? 'edit_last_post' : 'edit'));
indvdum@0
  1653
	}
indvdum@0
  1654
indvdum@0
  1655
	// First of all make sure the subject and topic title are having the correct length.
indvdum@0
  1656
	// To achieve this without cutting off between special chars we convert to an array and then count the elements.
indvdum@0
  1657
	$subject = truncate_string($subject);
indvdum@0
  1658
	$data['topic_title'] = truncate_string($data['topic_title']);
indvdum@0
  1659
indvdum@0
  1660
	// Collect some basic information about which tables and which rows to update/insert
indvdum@0
  1661
	$sql_data = $topic_row = array();
indvdum@0
  1662
	$poster_id = ($mode == 'edit') ? $data['poster_id'] : (int) $user->data['user_id'];
indvdum@0
  1663
indvdum@0
  1664
	// Retrieve some additional information if not present
indvdum@0
  1665
	if ($mode == 'edit' && (!isset($data['post_approved']) || !isset($data['topic_approved']) || $data['post_approved'] === false || $data['topic_approved'] === false))
indvdum@0
  1666
	{
indvdum@0
  1667
		$sql = 'SELECT p.post_approved, t.topic_type, t.topic_replies, t.topic_replies_real, t.topic_approved
indvdum@0
  1668
			FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . ' p
indvdum@0
  1669
			WHERE t.topic_id = p.topic_id
indvdum@0
  1670
				AND p.post_id = ' . $data['post_id'];
indvdum@0
  1671
		$result = $db->sql_query($sql);
indvdum@0
  1672
		$topic_row = $db->sql_fetchrow($result);
indvdum@0
  1673
		$db->sql_freeresult($result);
indvdum@0
  1674
indvdum@0
  1675
		$data['topic_approved'] = $topic_row['topic_approved'];
indvdum@0
  1676
		$data['post_approved'] = $topic_row['post_approved'];
indvdum@0
  1677
	}
indvdum@0
  1678
indvdum@0
  1679
	// This variable indicates if the user is able to post or put into the queue - it is used later for all code decisions regarding approval
indvdum@0
  1680
	// The variable name should be $post_approved, because it indicates if the post is approved or not
indvdum@0
  1681
	$post_approval = 1;
indvdum@0
  1682
indvdum@0
  1683
	// Check the permissions for post approval. Moderators are not affected.
indvdum@0
  1684
	if (!$auth->acl_get('f_noapprove', $data['forum_id']) && !$auth->acl_get('m_approve', $data['forum_id']))
indvdum@0
  1685
	{
indvdum@0
  1686
		// Post not approved, but in queue
indvdum@0
  1687
		$post_approval = 0;
indvdum@0
  1688
	}
indvdum@0
  1689
indvdum@0
  1690
	// Mods are able to force approved/unapproved posts. True means the post is approved, false the post is unapproved
indvdum@0
  1691
	if (isset($data['force_approved_state']))
indvdum@0
  1692
	{
indvdum@0
  1693
		$post_approval = ($data['force_approved_state']) ? 1 : 0;
indvdum@0
  1694
	}
indvdum@0
  1695
indvdum@0
  1696
	// Start the transaction here
indvdum@0
  1697
	$db->sql_transaction('begin');
indvdum@0
  1698
indvdum@0
  1699
	// Collect Information
indvdum@0
  1700
	switch ($post_mode)
indvdum@0
  1701
	{
indvdum@0
  1702
		case 'post':
indvdum@0
  1703
		case 'reply':
indvdum@0
  1704
			$sql_data[POSTS_TABLE]['sql'] = array(
indvdum@0
  1705
				'forum_id'			=> ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
indvdum@0
  1706
				'poster_id'			=> (int) $user->data['user_id'],
indvdum@0
  1707
				'icon_id'			=> $data['icon_id'],
indvdum@0
  1708
				'poster_ip'			=> $user->ip,
indvdum@0
  1709
				'post_time'			=> $current_time,
indvdum@0
  1710
				'post_approved'		=> $post_approval,
indvdum@0
  1711
				'enable_bbcode'		=> $data['enable_bbcode'],
indvdum@0
  1712
				'enable_smilies'	=> $data['enable_smilies'],
indvdum@0
  1713
				'enable_magic_url'	=> $data['enable_urls'],
indvdum@0
  1714
				'enable_sig'		=> $data['enable_sig'],
indvdum@0
  1715
				'post_username'		=> (!$user->data['is_registered']) ? $username : '',
indvdum@0
  1716
				'post_subject'		=> $subject,
indvdum@0
  1717
				'post_text'			=> $data['message'],
indvdum@0
  1718
				'post_checksum'		=> $data['message_md5'],
indvdum@0
  1719
				'post_attachment'	=> (!empty($data['attachment_data'])) ? 1 : 0,
indvdum@0
  1720
				'bbcode_bitfield'	=> $data['bbcode_bitfield'],
indvdum@0
  1721
				'bbcode_uid'		=> $data['bbcode_uid'],
indvdum@0
  1722
				'post_postcount'	=> ($auth->acl_get('f_postcount', $data['forum_id'])) ? 1 : 0,
indvdum@0
  1723
				'post_edit_locked'	=> $data['post_edit_locked']
indvdum@0
  1724
			);
indvdum@0
  1725
		break;
indvdum@0
  1726
indvdum@0
  1727
		case 'edit_first_post':
indvdum@0
  1728
		case 'edit':
indvdum@0
  1729
indvdum@0
  1730
		case 'edit_last_post':
indvdum@0
  1731
		case 'edit_topic':
indvdum@0
  1732
indvdum@0
  1733
			// If edit reason is given always display edit info
indvdum@0
  1734
indvdum@0
  1735
			// If editing last post then display no edit info
indvdum@0
  1736
			// If m_edit permission then display no edit info
indvdum@0
  1737
			// If normal edit display edit info
indvdum@0
  1738
indvdum@0
  1739
			// Display edit info if edit reason given or user is editing his post, which is not the last within the topic.
indvdum@0
  1740
			if ($data['post_edit_reason'] || (!$auth->acl_get('m_edit', $data['forum_id']) && ($post_mode == 'edit' || $post_mode == 'edit_first_post')))
indvdum@0
  1741
			{
indvdum@0
  1742
				$data['post_edit_reason']		= truncate_string($data['post_edit_reason'], 255, 255, false);
indvdum@0
  1743
indvdum@0
  1744
				$sql_data[POSTS_TABLE]['sql']	= array(
indvdum@0
  1745
					'post_edit_time'	=> $current_time,
indvdum@0
  1746
					'post_edit_reason'	=> $data['post_edit_reason'],
indvdum@0
  1747
					'post_edit_user'	=> (int) $data['post_edit_user'],
indvdum@0
  1748
				);
indvdum@0
  1749
indvdum@0
  1750
				$sql_data[POSTS_TABLE]['stat'][] = 'post_edit_count = post_edit_count + 1';
indvdum@0
  1751
			}
indvdum@0
  1752
			else if (!$data['post_edit_reason'] && $mode == 'edit' && $auth->acl_get('m_edit', $data['forum_id']))
indvdum@0
  1753
			{
indvdum@0
  1754
				$sql_data[POSTS_TABLE]['sql'] = array(
indvdum@0
  1755
					'post_edit_reason'	=> '',
indvdum@0
  1756
				);
indvdum@0
  1757
			}
indvdum@0
  1758
indvdum@0
  1759
			// If the person editing this post is different to the one having posted then we will add a log entry stating the edit
indvdum@0
  1760
			// Could be simplified by only adding to the log if the edit is not tracked - but this may confuse admins/mods
indvdum@0
  1761
			if ($user->data['user_id'] != $poster_id)
indvdum@0
  1762
			{
indvdum@0
  1763
				$log_subject = ($subject) ? $subject : $data['topic_title'];
indvdum@0
  1764
				add_log('mod', $data['forum_id'], $data['topic_id'], 'LOG_POST_EDITED', $log_subject, (!empty($username)) ? $username : $user->lang['GUEST']);
indvdum@0
  1765
			}
indvdum@0
  1766
indvdum@0
  1767
			if (!isset($sql_data[POSTS_TABLE]['sql']))
indvdum@0
  1768
			{
indvdum@0
  1769
				$sql_data[POSTS_TABLE]['sql'] = array();
indvdum@0
  1770
			}
indvdum@0
  1771
indvdum@0
  1772
			$sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
indvdum@0
  1773
				'forum_id'			=> ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
indvdum@0
  1774
				'poster_id'			=> $data['poster_id'],
indvdum@0
  1775
				'icon_id'			=> $data['icon_id'],
indvdum@0
  1776
				'post_approved'		=> (!$post_approval) ? 0 : $data['post_approved'],
indvdum@0
  1777
				'enable_bbcode'		=> $data['enable_bbcode'],
indvdum@0
  1778
				'enable_smilies'	=> $data['enable_smilies'],
indvdum@0
  1779
				'enable_magic_url'	=> $data['enable_urls'],
indvdum@0
  1780
				'enable_sig'		=> $data['enable_sig'],
indvdum@0
  1781
				'post_username'		=> ($username && $data['poster_id'] == ANONYMOUS) ? $username : '',
indvdum@0
  1782
				'post_subject'		=> $subject,
indvdum@0
  1783
				'post_checksum'		=> $data['message_md5'],
indvdum@0
  1784
				'post_attachment'	=> (!empty($data['attachment_data'])) ? 1 : 0,
indvdum@0
  1785
				'bbcode_bitfield'	=> $data['bbcode_bitfield'],
indvdum@0
  1786
				'bbcode_uid'		=> $data['bbcode_uid'],
indvdum@0
  1787
				'post_edit_locked'	=> $data['post_edit_locked'])
indvdum@0
  1788
			);
indvdum@0
  1789
indvdum@0
  1790
			if ($update_message)
indvdum@0
  1791
			{
indvdum@0
  1792
				$sql_data[POSTS_TABLE]['sql']['post_text'] = $data['message'];
indvdum@0
  1793
			}
indvdum@0
  1794
indvdum@0
  1795
		break;
indvdum@0
  1796
	}
indvdum@0
  1797
indvdum@0
  1798
	$post_approved = $sql_data[POSTS_TABLE]['sql']['post_approved'];
indvdum@0
  1799
	$topic_row = array();
indvdum@0
  1800
indvdum@0
  1801
	// And the topic ladies and gentlemen
indvdum@0
  1802
	switch ($post_mode)
indvdum@0
  1803
	{
indvdum@0
  1804
		case 'post':
indvdum@0
  1805
			$sql_data[TOPICS_TABLE]['sql'] = array(
indvdum@0
  1806
				'topic_poster'				=> (int) $user->data['user_id'],
indvdum@0
  1807
				'topic_time'				=> $current_time,
indvdum@0
  1808
				'topic_last_view_time'		=> $current_time,
indvdum@0
  1809
				'forum_id'					=> ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
indvdum@0
  1810
				'icon_id'					=> $data['icon_id'],
indvdum@0
  1811
				'topic_approved'			=> $post_approval,
indvdum@0
  1812
				'topic_title'				=> $subject,
indvdum@0
  1813
				'topic_first_poster_name'	=> (!$user->data['is_registered'] && $username) ? $username : (($user->data['user_id'] != ANONYMOUS) ? $user->data['username'] : ''),
indvdum@0
  1814
				'topic_first_poster_colour'	=> $user->data['user_colour'],
indvdum@0
  1815
				'topic_type'				=> $topic_type,
indvdum@0
  1816
				'topic_time_limit'			=> ($topic_type == POST_STICKY || $topic_type == POST_ANNOUNCE) ? ($data['topic_time_limit'] * 86400) : 0,
indvdum@0
  1817
				'topic_attachment'			=> (!empty($data['attachment_data'])) ? 1 : 0,
indvdum@0
  1818
			);
indvdum@0
  1819
indvdum@0
  1820
			if (isset($poll['poll_options']) && !empty($poll['poll_options']))
indvdum@0
  1821
			{
indvdum@0
  1822
				$poll_start = ($poll['poll_start']) ? $poll['poll_start'] : $current_time;
indvdum@0
  1823
				$poll_length = $poll['poll_length'] * 86400;
indvdum@0
  1824
				if ($poll_length < 0)
indvdum@0
  1825
				{
indvdum@0
  1826
					$poll_start = $poll_start + $poll_length;
indvdum@0
  1827
					if ($poll_start < 0)
indvdum@0
  1828
					{
indvdum@0
  1829
						$poll_start = 0;
indvdum@0
  1830
					}
indvdum@0
  1831
					$poll_length = 1;
indvdum@0
  1832
				}
indvdum@0
  1833
indvdum@0
  1834
				$sql_data[TOPICS_TABLE]['sql'] = array_merge($sql_data[TOPICS_TABLE]['sql'], array(
indvdum@0
  1835
					'poll_title'		=> $poll['poll_title'],
indvdum@0
  1836
					'poll_start'		=> $poll_start,
indvdum@0
  1837
					'poll_max_options'	=> $poll['poll_max_options'],
indvdum@0
  1838
					'poll_length'		=> $poll_length,
indvdum@0
  1839
					'poll_vote_change'	=> $poll['poll_vote_change'])
indvdum@0
  1840
				);
indvdum@0
  1841
			}
indvdum@0
  1842
indvdum@0
  1843
			$sql_data[USERS_TABLE]['stat'][] = "user_lastpost_time = $current_time" . (($auth->acl_get('f_postcount', $data['forum_id']) && $post_approval) ? ', user_posts = user_posts + 1' : '');
indvdum@0
  1844
indvdum@0
  1845
			if ($topic_type != POST_GLOBAL)
indvdum@0
  1846
			{
indvdum@0
  1847
				if ($post_approval)
indvdum@0
  1848
				{
indvdum@0
  1849
					$sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts + 1';
indvdum@0
  1850
				}
indvdum@0
  1851
				$sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics_real = forum_topics_real + 1' . (($post_approval) ? ', forum_topics = forum_topics + 1' : '');
indvdum@0
  1852
			}
indvdum@0
  1853
		break;
indvdum@0
  1854
indvdum@0
  1855
		case 'reply':
indvdum@0
  1856
			$sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_view_time = ' . $current_time . ',
indvdum@0
  1857
				topic_replies_real = topic_replies_real + 1,
indvdum@0
  1858
				topic_bumped = 0,
indvdum@0
  1859
				topic_bumper = 0' .
indvdum@0
  1860
				(($post_approval) ? ', topic_replies = topic_replies + 1' : '') .
indvdum@0
  1861
				((!empty($data['attachment_data']) || (isset($data['topic_attachment']) && $data['topic_attachment'])) ? ', topic_attachment = 1' : '');
indvdum@0
  1862
indvdum@0
  1863
			$sql_data[USERS_TABLE]['stat'][] = "user_lastpost_time = $current_time" . (($auth->acl_get('f_postcount', $data['forum_id']) && $post_approval) ? ', user_posts = user_posts + 1' : '');
indvdum@0
  1864
indvdum@0
  1865
			if ($post_approval && $topic_type != POST_GLOBAL)
indvdum@0
  1866
			{
indvdum@0
  1867
				$sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts + 1';
indvdum@0
  1868
			}
indvdum@0
  1869
		break;
indvdum@0
  1870
indvdum@0
  1871
		case 'edit_topic':
indvdum@0
  1872
		case 'edit_first_post':
indvdum@0
  1873
			if (isset($poll['poll_options']))
indvdum@0
  1874
			{
indvdum@0
  1875
				$poll_start = ($poll['poll_start'] || empty($poll['poll_options'])) ? $poll['poll_start'] : $current_time;
indvdum@0
  1876
				$poll_length = $poll['poll_length'] * 86400;
indvdum@0
  1877
				if ($poll_length < 0)
indvdum@0
  1878
				{
indvdum@0
  1879
					$poll_start = $poll_start + $poll_length;
indvdum@0
  1880
					if ($poll_start < 0)
indvdum@0
  1881
					{
indvdum@0
  1882
						$poll_start = 0;
indvdum@0
  1883
					}
indvdum@0
  1884
					$poll_length = 1;
indvdum@0
  1885
				}
indvdum@0
  1886
			}
indvdum@0
  1887
indvdum@0
  1888
			$sql_data[TOPICS_TABLE]['sql'] = array(
indvdum@0
  1889
				'forum_id'					=> ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
indvdum@0
  1890
				'icon_id'					=> $data['icon_id'],
indvdum@0
  1891
				'topic_approved'			=> (!$post_approval) ? 0 : $data['topic_approved'],
indvdum@0
  1892
				'topic_title'				=> $subject,
indvdum@0
  1893
				'topic_first_poster_name'	=> $username,
indvdum@0
  1894
				'topic_type'				=> $topic_type,
indvdum@0
  1895
				'topic_time_limit'			=> ($topic_type == POST_STICKY || $topic_type == POST_ANNOUNCE) ? ($data['topic_time_limit'] * 86400) : 0,
indvdum@0
  1896
				'poll_title'				=> (isset($poll['poll_options'])) ? $poll['poll_title'] : '',
indvdum@0
  1897
				'poll_start'				=> (isset($poll['poll_options'])) ? $poll_start : 0,
indvdum@0
  1898
				'poll_max_options'			=> (isset($poll['poll_options'])) ? $poll['poll_max_options'] : 1,
indvdum@0
  1899
				'poll_length'				=> (isset($poll['poll_options'])) ? $poll_length : 0,
indvdum@0
  1900
				'poll_vote_change'			=> (isset($poll['poll_vote_change'])) ? $poll['poll_vote_change'] : 0,
indvdum@0
  1901
				'topic_last_view_time'		=> $current_time,
indvdum@0
  1902
indvdum@0
  1903
				'topic_attachment'			=> (!empty($data['attachment_data'])) ? 1 : (isset($data['topic_attachment']) ? $data['topic_attachment'] : 0),
indvdum@0
  1904
			);
indvdum@0
  1905
indvdum@0
  1906
			// Correctly set back the topic replies and forum posts... only if the topic was approved before and now gets disapproved
indvdum@0
  1907
			if (!$post_approval && $data['topic_approved'])
indvdum@0
  1908
			{
indvdum@0
  1909
				// Do we need to grab some topic informations?
indvdum@0
  1910
				if (!sizeof($topic_row))
indvdum@0
  1911
				{
indvdum@0
  1912
					$sql = 'SELECT topic_type, topic_replies, topic_replies_real, topic_approved
indvdum@0
  1913
						FROM ' . TOPICS_TABLE . '
indvdum@0
  1914
						WHERE topic_id = ' . $data['topic_id'];
indvdum@0
  1915
					$result = $db->sql_query($sql);
indvdum@0
  1916
					$topic_row = $db->sql_fetchrow($result);
indvdum@0
  1917
					$db->sql_freeresult($result);
indvdum@0
  1918
				}
indvdum@0
  1919
indvdum@0
  1920
				// If this is the only post remaining we do not need to decrement topic_replies.
indvdum@0
  1921
				// Also do not decrement if first post - then the topic_replies will not be adjusted if approving the topic again.
indvdum@0
  1922
indvdum@0
  1923
				// If this is an edited topic or the first post the topic gets completely disapproved later on...
indvdum@0
  1924
				$sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics = forum_topics - 1';
indvdum@0
  1925
				$sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts - ' . ($topic_row['topic_replies'] + 1);
indvdum@0
  1926
indvdum@0
  1927
				set_config_count('num_topics', -1, true);
indvdum@0
  1928
				set_config_count('num_posts', ($topic_row['topic_replies'] + 1) * (-1), true);
indvdum@0
  1929
indvdum@0
  1930
				// Only decrement this post, since this is the one non-approved now
indvdum@0
  1931
				if ($auth->acl_get('f_postcount', $data['forum_id']))
indvdum@0
  1932
				{
indvdum@0
  1933
					$sql_data[USERS_TABLE]['stat'][] = 'user_posts = user_posts - 1';
indvdum@0
  1934
				}
indvdum@0
  1935
			}
indvdum@0
  1936
indvdum@0
  1937
		break;
indvdum@0
  1938
indvdum@0
  1939
		case 'edit':
indvdum@0
  1940
		case 'edit_last_post':
indvdum@0
  1941
indvdum@0
  1942
			// Correctly set back the topic replies and forum posts... but only if the post was approved before.
indvdum@0
  1943
			if (!$post_approval && $data['post_approved'])
indvdum@0
  1944
			{
indvdum@0
  1945
				$sql_data[TOPICS_TABLE]['stat'][] = 'topic_replies = topic_replies - 1, topic_last_view_time = ' . $current_time;
indvdum@0
  1946
				$sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts - 1';
indvdum@0
  1947
indvdum@0
  1948
				set_config_count('num_posts', -1, true);
indvdum@0
  1949
indvdum@0
  1950
				if ($auth->acl_get('f_postcount', $data['forum_id']))
indvdum@0
  1951
				{
indvdum@0
  1952
					$sql_data[USERS_TABLE]['stat'][] = 'user_posts = user_posts - 1';
indvdum@0
  1953
				}
indvdum@0
  1954
			}
indvdum@0
  1955
indvdum@0
  1956
		break;
indvdum@0
  1957
	}
indvdum@0
  1958
indvdum@0
  1959
	// Submit new topic
indvdum@0
  1960
	if ($post_mode == 'post')
indvdum@0
  1961
	{
indvdum@0
  1962
		$sql = 'INSERT INTO ' . TOPICS_TABLE . ' ' .
indvdum@0
  1963
			$db->sql_build_array('INSERT', $sql_data[TOPICS_TABLE]['sql']);
indvdum@0
  1964
		$db->sql_query($sql);
indvdum@0
  1965
indvdum@0
  1966
		$data['topic_id'] = $db->sql_nextid();
indvdum@0
  1967
indvdum@0
  1968
		$sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
indvdum@0
  1969
			'topic_id' => $data['topic_id'])
indvdum@0
  1970
		);
indvdum@0
  1971
		unset($sql_data[TOPICS_TABLE]['sql']);
indvdum@0
  1972
	}
indvdum@0
  1973
indvdum@0
  1974
	// Submit new post
indvdum@0
  1975
	if ($post_mode == 'post' || $post_mode == 'reply')
indvdum@0
  1976
	{
indvdum@0
  1977
		if ($post_mode == 'reply')
indvdum@0
  1978
		{
indvdum@0
  1979
			$sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
indvdum@0
  1980
				'topic_id' => $data['topic_id'])
indvdum@0
  1981
			);
indvdum@0
  1982
		}
indvdum@0
  1983
indvdum@0
  1984
		$sql = 'INSERT INTO ' . POSTS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_data[POSTS_TABLE]['sql']);
indvdum@0
  1985
		$db->sql_query($sql);
indvdum@0
  1986
		$data['post_id'] = $db->sql_nextid();
indvdum@0
  1987
indvdum@0
  1988
		if ($post_mode == 'post')
indvdum@0
  1989
		{
indvdum@0
  1990
			$sql_data[TOPICS_TABLE]['sql'] = array(
indvdum@0
  1991
				'topic_first_post_id'		=> $data['post_id'],
indvdum@0
  1992
				'topic_last_post_id'		=> $data['post_id'],
indvdum@0
  1993
				'topic_last_post_time'		=> $current_time,
indvdum@0
  1994
				'topic_last_poster_id'		=> (int) $user->data['user_id'],
indvdum@0
  1995
				'topic_last_poster_name'	=> (!$user->data['is_registered'] && $username) ? $username : (($user->data['user_id'] != ANONYMOUS) ? $user->data['username'] : ''),
indvdum@0
  1996
				'topic_last_poster_colour'	=> $user->data['user_colour'],
indvdum@0
  1997
				'topic_last_post_subject'	=> (string) $subject,
indvdum@0
  1998
			);
indvdum@0
  1999
		}
indvdum@0
  2000
indvdum@0
  2001
		unset($sql_data[POSTS_TABLE]['sql']);
indvdum@0
  2002
	}
indvdum@0
  2003
indvdum@0
  2004
	$make_global = false;
indvdum@0
  2005
indvdum@0
  2006
	// Are we globalising or unglobalising?
indvdum@0
  2007
	if ($post_mode == 'edit_first_post' || $post_mode == 'edit_topic')
indvdum@0
  2008
	{
indvdum@0
  2009
		if (!sizeof($topic_row))
indvdum@0
  2010
		{
indvdum@0
  2011
			$sql = 'SELECT topic_type, topic_replies, topic_replies_real, topic_approved, topic_last_post_id
indvdum@0
  2012
				FROM ' . TOPICS_TABLE . '
indvdum@0
  2013
				WHERE topic_id = ' . $data['topic_id'];
indvdum@0
  2014
			$result = $db->sql_query($sql);
indvdum@0
  2015
			$topic_row = $db->sql_fetchrow($result);
indvdum@0
  2016
			$db->sql_freeresult($result);
indvdum@0
  2017
		}
indvdum@0
  2018
indvdum@0
  2019
		// globalise/unglobalise?
indvdum@0
  2020
		if (($topic_row['topic_type'] != POST_GLOBAL && $topic_type == POST_GLOBAL) || ($topic_row['topic_type'] == POST_GLOBAL && $topic_type != POST_GLOBAL))
indvdum@0
  2021
		{
indvdum@0
  2022
			if (!empty($sql_data[FORUMS_TABLE]['stat']) && implode('', $sql_data[FORUMS_TABLE]['stat']))
indvdum@0
  2023
			{
indvdum@0
  2024
				$db->sql_query('UPDATE ' . FORUMS_TABLE . ' SET ' . implode(', ', $sql_data[FORUMS_TABLE]['stat']) . ' WHERE forum_id = ' . $data['forum_id']);
indvdum@0
  2025
			}
indvdum@0
  2026
indvdum@0
  2027
			$make_global = true;
indvdum@0
  2028
			$sql_data[FORUMS_TABLE]['stat'] = array();
indvdum@0
  2029
		}
indvdum@0
  2030
indvdum@0
  2031
		// globalise
indvdum@0
  2032
		if ($topic_row['topic_type'] != POST_GLOBAL && $topic_type == POST_GLOBAL)
indvdum@0
  2033
		{
indvdum@0
  2034
			// Decrement topic/post count
indvdum@0
  2035
			$sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts - ' . ($topic_row['topic_replies_real'] + 1);
indvdum@0
  2036
			$sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics_real = forum_topics_real - 1' . (($topic_row['topic_approved']) ? ', forum_topics = forum_topics - 1' : '');
indvdum@0
  2037
indvdum@0
  2038
			// Update forum_ids for all posts
indvdum@0
  2039
			$sql = 'UPDATE ' . POSTS_TABLE . '
indvdum@0
  2040
				SET forum_id = 0
indvdum@0
  2041
				WHERE topic_id = ' . $data['topic_id'];
indvdum@0
  2042
			$db->sql_query($sql);
indvdum@0
  2043
		}
indvdum@0
  2044
		// unglobalise
indvdum@0
  2045
		else if ($topic_row['topic_type'] == POST_GLOBAL && $topic_type != POST_GLOBAL)
indvdum@0
  2046
		{
indvdum@0
  2047
			// Increment topic/post count
indvdum@0
  2048
			$sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts + ' . ($topic_row['topic_replies_real'] + 1);
indvdum@0
  2049
			$sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics_real = forum_topics_real + 1' . (($topic_row['topic_approved']) ? ', forum_topics = forum_topics + 1' : '');
indvdum@0
  2050
indvdum@0
  2051
			// Update forum_ids for all posts
indvdum@0
  2052
			$sql = 'UPDATE ' . POSTS_TABLE . '
indvdum@0
  2053
				SET forum_id = ' . $data['forum_id'] . '
indvdum@0
  2054
				WHERE topic_id = ' . $data['topic_id'];
indvdum@0
  2055
			$db->sql_query($sql);
indvdum@0
  2056
		}
indvdum@0
  2057
	}
indvdum@0
  2058
indvdum@0
  2059
	// Update the topics table
indvdum@0
  2060
	if (isset($sql_data[TOPICS_TABLE]['sql']))
indvdum@0
  2061
	{
indvdum@0
  2062
		$sql = 'UPDATE ' . TOPICS_TABLE . '
indvdum@0
  2063
			SET ' . $db->sql_build_array('UPDATE', $sql_data[TOPICS_TABLE]['sql']) . '
indvdum@0
  2064
			WHERE topic_id = ' . $data['topic_id'];
indvdum@0
  2065
		$db->sql_query($sql);
indvdum@0
  2066
	}
indvdum@0
  2067
indvdum@0
  2068
	// Update the posts table
indvdum@0
  2069
	if (isset($sql_data[POSTS_TABLE]['sql']))
indvdum@0
  2070
	{
indvdum@0
  2071
		$sql = 'UPDATE ' . POSTS_TABLE . '
indvdum@0
  2072
			SET ' . $db->sql_build_array('UPDATE', $sql_data[POSTS_TABLE]['sql']) . '
indvdum@0
  2073
			WHERE post_id = ' . $data['post_id'];
indvdum@0
  2074
		$db->sql_query($sql);
indvdum@0
  2075
	}
indvdum@0
  2076
indvdum@0
  2077
	// Update Poll Tables
indvdum@0
  2078
	if (isset($poll['poll_options']))
indvdum@0
  2079
	{
indvdum@0
  2080
		$cur_poll_options = array();
indvdum@0
  2081
indvdum@0
  2082
		if ($mode == 'edit')
indvdum@0
  2083
		{
indvdum@0
  2084
			$sql = 'SELECT *
indvdum@0
  2085
				FROM ' . POLL_OPTIONS_TABLE . '
indvdum@0
  2086
				WHERE topic_id = ' . $data['topic_id'] . '
indvdum@0
  2087
				ORDER BY poll_option_id';
indvdum@0
  2088
			$result = $db->sql_query($sql);
indvdum@0
  2089
indvdum@0
  2090
			$cur_poll_options = array();
indvdum@0
  2091
			while ($row = $db->sql_fetchrow($result))
indvdum@0
  2092
			{
indvdum@0
  2093
				$cur_poll_options[] = $row;
indvdum@0
  2094
			}
indvdum@0
  2095
			$db->sql_freeresult($result);
indvdum@0
  2096
		}
indvdum@0
  2097
indvdum@0
  2098
		$sql_insert_ary = array();
indvdum@0
  2099
indvdum@0
  2100
		for ($i = 0, $size = sizeof($poll['poll_options']); $i < $size; $i++)
indvdum@0
  2101
		{
indvdum@0
  2102
			if (strlen(trim($poll['poll_options'][$i])))
indvdum@0
  2103
			{
indvdum@0
  2104
				if (empty($cur_poll_options[$i]))
indvdum@0
  2105
				{
indvdum@0
  2106
					// If we add options we need to put them to the end to be able to preserve votes...
indvdum@0
  2107
					$sql_insert_ary[] = array(
indvdum@0
  2108
						'poll_option_id'	=> (int) sizeof($cur_poll_options) + 1 + sizeof($sql_insert_ary),
indvdum@0
  2109
						'topic_id'			=> (int) $data['topic_id'],
indvdum@0
  2110
						'poll_option_text'	=> (string) $poll['poll_options'][$i]
indvdum@0
  2111
					);
indvdum@0
  2112
				}
indvdum@0
  2113
				else if ($poll['poll_options'][$i] != $cur_poll_options[$i])
indvdum@0
  2114
				{
indvdum@0
  2115
					$sql = 'UPDATE ' . POLL_OPTIONS_TABLE . "
indvdum@0
  2116
						SET poll_option_text = '" . $db->sql_escape($poll['poll_options'][$i]) . "'
indvdum@0
  2117
						WHERE poll_option_id = " . $cur_poll_options[$i]['poll_option_id'] . '
indvdum@0
  2118
							AND topic_id = ' . $data['topic_id'];
indvdum@0
  2119
					$db->sql_query($sql);
indvdum@0
  2120
				}
indvdum@0
  2121
			}
indvdum@0
  2122
		}
indvdum@0
  2123
indvdum@0
  2124
		$db->sql_multi_insert(POLL_OPTIONS_TABLE, $sql_insert_ary);
indvdum@0
  2125
indvdum@0
  2126
		if (sizeof($poll['poll_options']) < sizeof($cur_poll_options))
indvdum@0
  2127
		{
indvdum@0
  2128
			$sql = 'DELETE FROM ' . POLL_OPTIONS_TABLE . '
indvdum@0
  2129
				WHERE poll_option_id > ' . sizeof($poll['poll_options']) . '
indvdum@0
  2130
					AND topic_id = ' . $data['topic_id'];
indvdum@0
  2131
			$db->sql_query($sql);
indvdum@0
  2132
		}
indvdum@0
  2133
indvdum@0
  2134
		// If edited, we would need to reset votes (since options can be re-ordered above, you can't be sure if the change is for changing the text or adding an option
indvdum@0
  2135
		if ($mode == 'edit' && sizeof($poll['poll_options']) != sizeof($cur_poll_options))
indvdum@0
  2136
		{
indvdum@0
  2137
			$db->sql_query('DELETE FROM ' . POLL_VOTES_TABLE . ' WHERE topic_id = ' . $data['topic_id']);
indvdum@0
  2138
			$db->sql_query('UPDATE ' . POLL_OPTIONS_TABLE . ' SET poll_option_total = 0 WHERE topic_id = ' . $data['topic_id']);
indvdum@0
  2139
		}
indvdum@0
  2140
	}
indvdum@0
  2141
indvdum@0
  2142
	// Submit Attachments
indvdum@0
  2143
	if (!empty($data['attachment_data']) && $data['post_id'] && in_array($mode, array('post', 'reply', 'quote', 'edit')))
indvdum@0
  2144
	{
indvdum@0
  2145
		$space_taken = $files_added = 0;
indvdum@0
  2146
		$orphan_rows = array();
indvdum@0
  2147
indvdum@0
  2148
		foreach ($data['attachment_data'] as $pos => $attach_row)
indvdum@0
  2149
		{
indvdum@0
  2150
			$orphan_rows[(int) $attach_row['attach_id']] = array();
indvdum@0
  2151
		}
indvdum@0
  2152
indvdum@0
  2153
		if (sizeof($orphan_rows))
indvdum@0
  2154
		{
indvdum@0
  2155
			$sql = 'SELECT attach_id, filesize, physical_filename
indvdum@0
  2156
				FROM ' . ATTACHMENTS_TABLE . '
indvdum@0
  2157
				WHERE ' . $db->sql_in_set('attach_id', array_keys($orphan_rows)) . '
indvdum@0
  2158
					AND is_orphan = 1
indvdum@0
  2159
					AND poster_id = ' . $user->data['user_id'];
indvdum@0
  2160
			$result = $db->sql_query($sql);
indvdum@0
  2161
indvdum@0
  2162
			$orphan_rows = array();
indvdum@0
  2163
			while ($row = $db->sql_fetchrow($result))
indvdum@0
  2164
			{
indvdum@0
  2165
				$orphan_rows[$row['attach_id']] = $row;
indvdum@0
  2166
			}
indvdum@0
  2167
			$db->sql_freeresult($result);
indvdum@0
  2168
		}
indvdum@0
  2169
indvdum@0
  2170
		foreach ($data['attachment_data'] as $pos => $attach_row)
indvdum@0
  2171
		{
indvdum@0
  2172
			if ($attach_row['is_orphan'] && !isset($orphan_rows[$attach_row['attach_id']]))
indvdum@0
  2173
			{
indvdum@0
  2174
				continue;
indvdum@0
  2175
			}
indvdum@0
  2176
indvdum@0
  2177
			if (!$attach_row['is_orphan'])
indvdum@0
  2178
			{
indvdum@0
  2179
				// update entry in db if attachment already stored in db and filespace
indvdum@0
  2180
				$sql = 'UPDATE ' . ATTACHMENTS_TABLE . "
indvdum@0
  2181
					SET attach_comment = '" . $db->sql_escape($attach_row['attach_comment']) . "'
indvdum@0
  2182
					WHERE attach_id = " . (int) $attach_row['attach_id'] . '
indvdum@0
  2183
						AND is_orphan = 0';
indvdum@0
  2184
				$db->sql_query($sql);
indvdum@0
  2185
			}
indvdum@0
  2186
			else
indvdum@0
  2187
			{
indvdum@0
  2188
				// insert attachment into db
indvdum@0
  2189
				if (!@file_exists($phpbb_root_path . $config['upload_path'] . '/' . utf8_basename($orphan_rows[$attach_row['attach_id']]['physical_filename'])))
indvdum@0
  2190
				{
indvdum@0
  2191
					continue;
indvdum@0
  2192
				}
indvdum@0
  2193
indvdum@0
  2194
				$space_taken += $orphan_rows[$attach_row['attach_id']]['filesize'];
indvdum@0
  2195
				$files_added++;
indvdum@0
  2196
indvdum@0
  2197
				$attach_sql = array(
indvdum@0
  2198
					'post_msg_id'		=> $data['post_id'],
indvdum@0
  2199
					'topic_id'			=> $data['topic_id'],
indvdum@0
  2200
					'is_orphan'			=> 0,
indvdum@0
  2201
					'poster_id'			=> $poster_id,
indvdum@0
  2202
					'attach_comment'	=> $attach_row['attach_comment'],
indvdum@0
  2203
				);
indvdum@0
  2204
indvdum@0
  2205
				$sql = 'UPDATE ' . ATTACHMENTS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $attach_sql) . '
indvdum@0
  2206
					WHERE attach_id = ' . $attach_row['attach_id'] . '
indvdum@0
  2207
						AND is_orphan = 1
indvdum@0
  2208
						AND poster_id = ' . $user->data['user_id'];
indvdum@0
  2209
				$db->sql_query($sql);
indvdum@0
  2210
			}
indvdum@0
  2211
		}
indvdum@0
  2212
indvdum@0
  2213
		if ($space_taken && $files_added)
indvdum@0
  2214
		{
indvdum@0
  2215
			set_config_count('upload_dir_size', $space_taken, true);
indvdum@0
  2216
			set_config_count('num_files', $files_added, true);
indvdum@0
  2217
		}
indvdum@0
  2218
	}
indvdum@0
  2219
indvdum@0
  2220
	// we need to update the last forum information
indvdum@0
  2221
	// only applicable if the topic is not global and it is approved
indvdum@0
  2222
	// we also check to make sure we are not dealing with globaling the latest topic (pretty rare but still needs to be checked)
indvdum@0
  2223
	if ($topic_type != POST_GLOBAL && !$make_global && ($post_approved || !$data['post_approved']))
indvdum@0
  2224
	{
indvdum@0
  2225
		// the last post makes us update the forum table. This can happen if...
indvdum@0
  2226
		// We make a new topic
indvdum@0
  2227
		// We reply to a topic
indvdum@0
  2228
		// We edit the last post in a topic and this post is the latest in the forum (maybe)
indvdum@0
  2229
		// We edit the only post in the topic
indvdum@0
  2230
		// We edit the first post in the topic and all the other posts are not approved
indvdum@0
  2231
		if (($post_mode == 'post' || $post_mode == 'reply') && $post_approved)
indvdum@0
  2232
		{
indvdum@0
  2233
			$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = ' . $data['post_id'];
indvdum@0
  2234
			$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($subject) . "'";
indvdum@0
  2235
			$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = ' . $current_time;
indvdum@0
  2236
			$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = ' . (int) $user->data['user_id'];
indvdum@0
  2237
			$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape((!$user->data['is_registered'] && $username) ? $username : (($user->data['user_id'] != ANONYMOUS) ? $user->data['username'] : '')) . "'";
indvdum@0
  2238
			$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = '" . $db->sql_escape($user->data['user_colour']) . "'";
indvdum@0
  2239
		}
indvdum@0
  2240
		else if ($post_mode == 'edit_last_post' || $post_mode == 'edit_topic' || ($post_mode == 'edit_first_post' && !$data['topic_replies']))
indvdum@0
  2241
		{
indvdum@0
  2242
			// this does not _necessarily_ mean that we must update the info again,
indvdum@0
  2243
			// it just means that we might have to
indvdum@0
  2244
			$sql = 'SELECT forum_last_post_id, forum_last_post_subject
indvdum@0
  2245
				FROM ' . FORUMS_TABLE . '
indvdum@0
  2246
				WHERE forum_id = ' . (int) $data['forum_id'];
indvdum@0
  2247
			$result = $db->sql_query($sql);
indvdum@0
  2248
			$row = $db->sql_fetchrow($result);
indvdum@0
  2249
			$db->sql_freeresult($result);
indvdum@0
  2250
indvdum@0
  2251
			// this post is the latest post in the forum, better update
indvdum@0
  2252
			if ($row['forum_last_post_id'] == $data['post_id'])
indvdum@0
  2253
			{
indvdum@0
  2254
				// If post approved and subject changed, or poster is anonymous, we need to update the forum_last* rows
indvdum@0
  2255
				if ($post_approved && ($row['forum_last_post_subject'] !== $subject || $data['poster_id'] == ANONYMOUS))
indvdum@0
  2256
				{
indvdum@0
  2257
					// the post's subject changed
indvdum@0
  2258
					if ($row['forum_last_post_subject'] !== $subject)
indvdum@0
  2259
					{
indvdum@0
  2260
						$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_subject = \'' . $db->sql_escape($subject) . '\'';
indvdum@0
  2261
					}
indvdum@0
  2262
indvdum@0
  2263
					// Update the user name if poster is anonymous... just in case an admin changed it
indvdum@0
  2264
					if ($data['poster_id'] == ANONYMOUS)
indvdum@0
  2265
					{
indvdum@0
  2266
						$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape($username) . "'";
indvdum@0
  2267
					}
indvdum@0
  2268
				}
indvdum@0
  2269
				else if ($data['post_approved'] !== $post_approved)
indvdum@0
  2270
				{
indvdum@0
  2271
					// we need a fresh change of socks, everything has become invalidated
indvdum@0
  2272
					$sql = 'SELECT MAX(topic_last_post_id) as last_post_id
indvdum@0
  2273
						FROM ' . TOPICS_TABLE . '
indvdum@0
  2274
						WHERE forum_id = ' . (int) $data['forum_id'] . '
indvdum@0
  2275
							AND topic_approved = 1';
indvdum@0
  2276
					$result = $db->sql_query($sql);
indvdum@0
  2277
					$row = $db->sql_fetchrow($result);
indvdum@0
  2278
					$db->sql_freeresult($result);
indvdum@0
  2279
indvdum@0
  2280
					// any posts left in this forum?
indvdum@0
  2281
					if (!empty($row['last_post_id']))
indvdum@0
  2282
					{
indvdum@0
  2283
						$sql = 'SELECT p.post_id, p.post_subject, p.post_time, p.poster_id, p.post_username, u.user_id, u.username, u.user_colour
indvdum@0
  2284
							FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
indvdum@0
  2285
							WHERE p.poster_id = u.user_id
indvdum@0
  2286
								AND p.post_id = ' . (int) $row['last_post_id'];
indvdum@0
  2287
						$result = $db->sql_query($sql);
indvdum@0
  2288
						$row = $db->sql_fetchrow($result);
indvdum@0
  2289
						$db->sql_freeresult($result);
indvdum@0
  2290
indvdum@0
  2291
						// salvation, a post is found! jam it into the forums table
indvdum@0
  2292
						$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = ' . (int) $row['post_id'];
indvdum@0
  2293
						$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
indvdum@0
  2294
						$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = ' . (int) $row['post_time'];
indvdum@0
  2295
						$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = ' . (int) $row['poster_id'];
indvdum@0
  2296
						$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape(($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username']) . "'";
indvdum@0
  2297
						$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
indvdum@0
  2298
					}
indvdum@0
  2299
					else
indvdum@0
  2300
					{
indvdum@0
  2301
						// just our luck, the last topic in the forum has just been turned unapproved...
indvdum@0
  2302
						$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = 0';
indvdum@0
  2303
						$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = ''";
indvdum@0
  2304
						$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = 0';
indvdum@0
  2305
						$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = 0';
indvdum@0
  2306
						$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = ''";
indvdum@0
  2307
						$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = ''";
indvdum@0
  2308
					}
indvdum@0
  2309
				}
indvdum@0
  2310
			}
indvdum@0
  2311
		}
indvdum@0
  2312
	}
indvdum@0
  2313
	else if ($make_global)
indvdum@0
  2314
	{
indvdum@0
  2315
		// somebody decided to be a party pooper, we must recalculate the whole shebang (maybe)
indvdum@0
  2316
		$sql = 'SELECT forum_last_post_id
indvdum@0
  2317
			FROM ' . FORUMS_TABLE . '
indvdum@0
  2318
			WHERE forum_id = ' . (int) $data['forum_id'];
indvdum@0
  2319
		$result = $db->sql_query($sql);
indvdum@0
  2320
		$forum_row = $db->sql_fetchrow($result);
indvdum@0
  2321
		$db->sql_freeresult($result);
indvdum@0
  2322
indvdum@0
  2323
		// we made a topic global, go get new data
indvdum@0
  2324
		if ($topic_row['topic_type'] != POST_GLOBAL && $topic_type == POST_GLOBAL && $forum_row['forum_last_post_id'] == $topic_row['topic_last_post_id'])
indvdum@0
  2325
		{
indvdum@0
  2326
			// we need a fresh change of socks, everything has become invalidated
indvdum@0
  2327
			$sql = 'SELECT MAX(topic_last_post_id) as last_post_id
indvdum@0
  2328
				FROM ' . TOPICS_TABLE . '
indvdum@0
  2329
				WHERE forum_id = ' . (int) $data['forum_id'] . '
indvdum@0
  2330
					AND topic_approved = 1';
indvdum@0
  2331
			$result = $db->sql_query($sql);
indvdum@0
  2332
			$row = $db->sql_fetchrow($result);
indvdum@0
  2333
			$db->sql_freeresult($result);
indvdum@0
  2334
indvdum@0
  2335
			// any posts left in this forum?
indvdum@0
  2336
			if (!empty($row['last_post_id']))
indvdum@0
  2337
			{
indvdum@0
  2338
				$sql = 'SELECT p.post_id, p.post_subject, p.post_time, p.poster_id, p.post_username, u.user_id, u.username, u.user_colour
indvdum@0
  2339
					FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
indvdum@0
  2340
					WHERE p.poster_id = u.user_id
indvdum@0
  2341
						AND p.post_id = ' . (int) $row['last_post_id'];
indvdum@0
  2342
				$result = $db->sql_query($sql);
indvdum@0
  2343
				$row = $db->sql_fetchrow($result);
indvdum@0
  2344
				$db->sql_freeresult($result);
indvdum@0
  2345
indvdum@0
  2346
				// salvation, a post is found! jam it into the forums table
indvdum@0
  2347
				$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = ' . (int) $row['post_id'];
indvdum@0
  2348
				$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
indvdum@0
  2349
				$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = ' . (int) $row['post_time'];
indvdum@0
  2350
				$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = ' . (int) $row['poster_id'];
indvdum@0
  2351
				$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape(($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username']) . "'";
indvdum@0
  2352
				$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
indvdum@0
  2353
			}
indvdum@0
  2354
			else
indvdum@0
  2355
			{
indvdum@0
  2356
				// just our luck, the last topic in the forum has just been globalized...
indvdum@0
  2357
				$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = 0';
indvdum@0
  2358
				$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = ''";
indvdum@0
  2359
				$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = 0';
indvdum@0
  2360
				$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = 0';
indvdum@0
  2361
				$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = ''";
indvdum@0
  2362
				$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = ''";
indvdum@0
  2363
			}
indvdum@0
  2364
		}
indvdum@0
  2365
		else if ($topic_row['topic_type'] == POST_GLOBAL && $topic_type != POST_GLOBAL && $forum_row['forum_last_post_id'] < $topic_row['topic_last_post_id'])
indvdum@0
  2366
		{
indvdum@0
  2367
			// this post has a higher id, it is newer
indvdum@0
  2368
			$sql = 'SELECT p.post_id, p.post_subject, p.post_time, p.poster_id, p.post_username, u.user_id, u.username, u.user_colour
indvdum@0
  2369
				FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
indvdum@0
  2370
				WHERE p.poster_id = u.user_id
indvdum@0
  2371
					AND p.post_id = ' . (int) $topic_row['topic_last_post_id'];
indvdum@0
  2372
			$result = $db->sql_query($sql);
indvdum@0
  2373
			$row = $db->sql_fetchrow($result);
indvdum@0
  2374
			$db->sql_freeresult($result);
indvdum@0
  2375
indvdum@0
  2376
			// salvation, a post is found! jam it into the forums table
indvdum@0
  2377
			$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = ' . (int) $row['post_id'];
indvdum@0
  2378
			$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
indvdum@0
  2379
			$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = ' . (int) $row['post_time'];
indvdum@0
  2380
			$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = ' . (int) $row['poster_id'];
indvdum@0
  2381
			$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape(($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username']) . "'";
indvdum@0
  2382
			$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
indvdum@0
  2383
		}
indvdum@0
  2384
	}
indvdum@0
  2385
indvdum@0
  2386
	// topic sync time!
indvdum@0
  2387
	// simply, we update if it is a reply or the last post is edited
indvdum@0
  2388
	if ($post_approved)
indvdum@0
  2389
	{
indvdum@0
  2390
		// reply requires the whole thing
indvdum@0
  2391
		if ($post_mode == 'reply')
indvdum@0
  2392
		{
indvdum@0
  2393
			$sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_post_id = ' . (int) $data['post_id'];
indvdum@0
  2394
			$sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_poster_id = ' . (int) $user->data['user_id'];
indvdum@0
  2395
			$sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_name = '" . $db->sql_escape((!$user->data['is_registered'] && $username) ? $username : (($user->data['user_id'] != ANONYMOUS) ? $user->data['username'] : '')) . "'";
indvdum@0
  2396
			$sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_colour = '" . (($user->data['user_id'] != ANONYMOUS) ? $db->sql_escape($user->data['user_colour']) : '') . "'";
indvdum@0
  2397
			$sql_data[TOPICS_TABLE]['stat'][] = "topic_last_post_subject = '" . $db->sql_escape($subject) . "'";
indvdum@0
  2398
			$sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_post_time = ' . (int) $current_time;
indvdum@0
  2399
		}
indvdum@0
  2400
		else if ($post_mode == 'edit_last_post' || $post_mode == 'edit_topic' || ($post_mode == 'edit_first_post' && !$data['topic_replies']))
indvdum@0
  2401
		{
indvdum@0
  2402
			// only the subject can be changed from edit
indvdum@0
  2403
			$sql_data[TOPICS_TABLE]['stat'][] = "topic_last_post_subject = '" . $db->sql_escape($subject) . "'";
indvdum@0
  2404
indvdum@0
  2405
			// Maybe not only the subject, but also changing anonymous usernames. ;)
indvdum@0
  2406
			if ($data['poster_id'] == ANONYMOUS)
indvdum@0
  2407
			{
indvdum@0
  2408
				$sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_name = '" . $db->sql_escape($username) . "'";
indvdum@0
  2409
			}
indvdum@0
  2410
		}
indvdum@0
  2411
	}
indvdum@0
  2412
	else if (!$data['post_approved'] && ($post_mode == 'edit_last_post' || $post_mode == 'edit_topic' || ($post_mode == 'edit_first_post' && !$data['topic_replies'])))
indvdum@0
  2413
	{
indvdum@0
  2414
		// like having the rug pulled from under us
indvdum@0
  2415
		$sql = 'SELECT MAX(post_id) as last_post_id
indvdum@0
  2416
			FROM ' . POSTS_TABLE . '
indvdum@0
  2417
			WHERE topic_id = ' . (int) $data['topic_id'] . '
indvdum@0
  2418
				AND post_approved = 1';
indvdum@0
  2419
		$result = $db->sql_query($sql);
indvdum@0
  2420
		$row = $db->sql_fetchrow($result);
indvdum@0
  2421
		$db->sql_freeresult($result);
indvdum@0
  2422
indvdum@0
  2423
		// any posts left in this forum?
indvdum@0
  2424
		if (!empty($row['last_post_id']))
indvdum@0
  2425
		{
indvdum@0
  2426
			$sql = 'SELECT p.post_id, p.post_subject, p.post_time, p.poster_id, p.post_username, u.user_id, u.username, u.user_colour
indvdum@0
  2427
				FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
indvdum@0
  2428
				WHERE p.poster_id = u.user_id
indvdum@0
  2429
					AND p.post_id = ' . (int) $row['last_post_id'];
indvdum@0
  2430
			$result = $db->sql_query($sql);
indvdum@0
  2431
			$row = $db->sql_fetchrow($result);
indvdum@0
  2432
			$db->sql_freeresult($result);
indvdum@0
  2433
indvdum@0
  2434
			// salvation, a post is found! jam it into the topics table
indvdum@0
  2435
			$sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_post_id = ' . (int) $row['post_id'];
indvdum@0
  2436
			$sql_data[TOPICS_TABLE]['stat'][] = "topic_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
indvdum@0
  2437
			$sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_post_time = ' . (int) $row['post_time'];
indvdum@0
  2438
			$sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_poster_id = ' . (int) $row['poster_id'];
indvdum@0
  2439
			$sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_name = '" . $db->sql_escape(($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username']) . "'";
indvdum@0
  2440
			$sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
indvdum@0
  2441
		}
indvdum@0
  2442
	}
indvdum@0
  2443
indvdum@0
  2444
	// Update total post count, do not consider moderated posts/topics
indvdum@0
  2445
	if ($post_approval)
indvdum@0
  2446
	{
indvdum@0
  2447
		if ($post_mode == 'post')
indvdum@0
  2448
		{
indvdum@0
  2449
			set_config_count('num_topics', 1, true);
indvdum@0
  2450
			set_config_count('num_posts', 1, true);
indvdum@0
  2451
		}
indvdum@0
  2452
indvdum@0
  2453
		if ($post_mode == 'reply')
indvdum@0
  2454
		{
indvdum@0
  2455
			set_config_count('num_posts', 1, true);
indvdum@0
  2456
		}
indvdum@0
  2457
	}
indvdum@0
  2458
indvdum@0
  2459
	// Update forum stats
indvdum@0
  2460
	$where_sql = array(POSTS_TABLE => 'post_id = ' . $data['post_id'], TOPICS_TABLE => 'topic_id = ' . $data['topic_id'], FORUMS_TABLE => 'forum_id = ' . $data['forum_id'], USERS_TABLE => 'user_id = ' . $poster_id);
indvdum@0
  2461
indvdum@0
  2462
	foreach ($sql_data as $table => $update_ary)
indvdum@0
  2463
	{
indvdum@0
  2464
		if (isset($update_ary['stat']) && implode('', $update_ary['stat']))
indvdum@0
  2465
		{
indvdum@0
  2466
			$sql = "UPDATE $table SET " . implode(', ', $update_ary['stat']) . ' WHERE ' . $where_sql[$table];
indvdum@0
  2467
			$db->sql_query($sql);
indvdum@0
  2468
		}
indvdum@0
  2469
	}
indvdum@0
  2470
indvdum@0
  2471
	// Delete topic shadows (if any exist). We do not need a shadow topic for an global announcement
indvdum@0
  2472
	if ($make_global)
indvdum@0
  2473
	{
indvdum@0
  2474
		$sql = 'DELETE FROM ' . TOPICS_TABLE . '
indvdum@0
  2475
			WHERE topic_moved_id = ' . $data['topic_id'];
indvdum@0
  2476
		$db->sql_query($sql);
indvdum@0
  2477
	}
indvdum@0
  2478
indvdum@0
  2479
	// Committing the transaction before updating search index
indvdum@0
  2480
	$db->sql_transaction('commit');
indvdum@0
  2481
indvdum@0
  2482
	// Delete draft if post was loaded...
indvdum@0
  2483
	$draft_id = request_var('draft_loaded', 0);
indvdum@0
  2484
	if ($draft_id)
indvdum@0
  2485
	{
indvdum@0
  2486
		$sql = 'DELETE FROM ' . DRAFTS_TABLE . "
indvdum@0
  2487
			WHERE draft_id = $draft_id
indvdum@0
  2488
				AND user_id = {$user->data['user_id']}";
indvdum@0
  2489
		$db->sql_query($sql);
indvdum@0
  2490
	}
indvdum@0
  2491
indvdum@0
  2492
	// Index message contents
indvdum@0
  2493
	if ($update_search_index && $data['enable_indexing'])
indvdum@0
  2494
	{
indvdum@0
  2495
		// Select the search method and do some additional checks to ensure it can actually be utilised
indvdum@0
  2496
		$search_type = basename($config['search_type']);
indvdum@0
  2497
indvdum@0
  2498
		if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx))
indvdum@0
  2499
		{
indvdum@0
  2500
			trigger_error('NO_SUCH_SEARCH_MODULE');
indvdum@0
  2501
		}
indvdum@0
  2502
indvdum@0
  2503
		if (!class_exists($search_type))
indvdum@0
  2504
		{
indvdum@0
  2505
			include("{$phpbb_root_path}includes/search/$search_type.$phpEx");
indvdum@0
  2506
		}
indvdum@0
  2507
indvdum@0
  2508
		$error = false;
indvdum@0
  2509
		$search = new $search_type($error);
indvdum@0
  2510
indvdum@0
  2511
		if ($error)
indvdum@0
  2512
		{
indvdum@0
  2513
			trigger_error($error);
indvdum@0
  2514
		}
indvdum@0
  2515
indvdum@0
  2516
		$search->index($mode, $data['post_id'], $data['message'], $subject, $poster_id, ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id']);
indvdum@0
  2517
	}
indvdum@0
  2518
indvdum@0
  2519
	// Topic Notification, do not change if moderator is changing other users posts...
indvdum@0
  2520
	if ($user->data['user_id'] == $poster_id)
indvdum@0
  2521
	{
indvdum@0
  2522
		if (!$data['notify_set'] && $data['notify'])
indvdum@0
  2523
		{
indvdum@0
  2524
			$sql = 'INSERT INTO ' . TOPICS_WATCH_TABLE . ' (user_id, topic_id)
indvdum@0
  2525
				VALUES (' . $user->data['user_id'] . ', ' . $data['topic_id'] . ')';
indvdum@0
  2526
			$db->sql_query($sql);
indvdum@0
  2527
		}
indvdum@0
  2528
		else if (($config['email_enable'] || $config['jab_enable']) && $data['notify_set'] && !$data['notify'])
indvdum@0
  2529
		{
indvdum@0
  2530
			$sql = 'DELETE FROM ' . TOPICS_WATCH_TABLE . '
indvdum@0
  2531
				WHERE user_id = ' . $user->data['user_id'] . '
indvdum@0
  2532
					AND topic_id = ' . $data['topic_id'];
indvdum@0
  2533
			$db->sql_query($sql);
indvdum@0
  2534
		}
indvdum@0
  2535
	}
indvdum@0
  2536
indvdum@0
  2537
	if ($mode == 'post' || $mode == 'reply' || $mode == 'quote')
indvdum@0
  2538
	{
indvdum@0
  2539
		// Mark this topic as posted to
indvdum@0
  2540
		markread('post', $data['forum_id'], $data['topic_id']);
indvdum@0
  2541
	}
indvdum@0
  2542
indvdum@0
  2543
	// Mark this topic as read
indvdum@0
  2544
	// We do not use post_time here, this is intended (post_time can have a date in the past if editing a message)
indvdum@0
  2545
	markread('topic', (($topic_type == POST_GLOBAL) ? 0 : $data['forum_id']), $data['topic_id'], time());
indvdum@0
  2546
indvdum@0
  2547
	//
indvdum@0
  2548
	if ($config['load_db_lastread'] && $user->data['is_registered'])
indvdum@0
  2549
	{
indvdum@0
  2550
		$sql = 'SELECT mark_time
indvdum@0
  2551
			FROM ' . FORUMS_TRACK_TABLE . '
indvdum@0
  2552
			WHERE user_id = ' . $user->data['user_id'] . '
indvdum@0
  2553
				AND forum_id = ' . (($topic_type == POST_GLOBAL) ? 0 : $data['forum_id']);
indvdum@0
  2554
		$result = $db->sql_query($sql);
indvdum@0
  2555
		$f_mark_time = (int) $db->sql_fetchfield('mark_time');
indvdum@0
  2556
		$db->sql_freeresult($result);
indvdum@0
  2557
	}
indvdum@0
  2558
	else if ($config['load_anon_lastread'] || $user->data['is_registered'])
indvdum@0
  2559
	{
indvdum@0
  2560
		$f_mark_time = false;
indvdum@0
  2561
	}
indvdum@0
  2562
indvdum@0
  2563
	if (($config['load_db_lastread'] && $user->data['is_registered']) || $config['load_anon_lastread'] || $user->data['is_registered'])
indvdum@0
  2564
	{
indvdum@0
  2565
		// Update forum info
indvdum@0
  2566
		if ($topic_type == POST_GLOBAL)
indvdum@0
  2567
		{
indvdum@0
  2568
			$sql = 'SELECT MAX(topic_last_post_time) as forum_last_post_time
indvdum@0
  2569
				FROM ' . TOPICS_TABLE . '
indvdum@0
  2570
				WHERE forum_id = 0';
indvdum@0
  2571
		}
indvdum@0
  2572
		else
indvdum@0
  2573
		{
indvdum@0
  2574
			$sql = 'SELECT forum_last_post_time
indvdum@0
  2575
				FROM ' . FORUMS_TABLE . '
indvdum@0
  2576
				WHERE forum_id = ' . $data['forum_id'];
indvdum@0
  2577
		}
indvdum@0
  2578
		$result = $db->sql_query($sql);
indvdum@0
  2579
		$forum_last_post_time = (int) $db->sql_fetchfield('forum_last_post_time');
indvdum@0
  2580
		$db->sql_freeresult($result);
indvdum@0
  2581
indvdum@0
  2582
		update_forum_tracking_info((($topic_type == POST_GLOBAL) ? 0 : $data['forum_id']), $forum_last_post_time, $f_mark_time, false);
indvdum@0
  2583
	}
indvdum@0
  2584
indvdum@0
  2585
	// Send Notifications
indvdum@0
  2586
	if (($mode == 'reply' || $mode == 'quote' || $mode == 'post') && $post_approval)
indvdum@0
  2587
	{
indvdum@0
  2588
		user_notification($mode, $subject, $data['topic_title'], $data['forum_name'], $data['forum_id'], $data['topic_id'], $data['post_id']);
indvdum@0
  2589
	}
indvdum@0
  2590
indvdum@0
  2591
	$params = $add_anchor = '';
indvdum@0
  2592
indvdum@0
  2593
	if ($post_approval)
indvdum@0
  2594
	{
indvdum@0
  2595
		$params .= '&amp;t=' . $data['topic_id'];
indvdum@0
  2596
indvdum@0
  2597
		if ($mode != 'post')
indvdum@0
  2598
		{
indvdum@0
  2599
			$params .= '&amp;p=' . $data['post_id'];
indvdum@0
  2600
			$add_anchor = '#p' . $data['post_id'];
indvdum@0
  2601
		}
indvdum@0
  2602
	}
indvdum@0
  2603
	else if ($mode != 'post' && $post_mode != 'edit_first_post' && $post_mode != 'edit_topic')
indvdum@0
  2604
	{
indvdum@0
  2605
		$params .= '&amp;t=' . $data['topic_id'];
indvdum@0
  2606
	}
indvdum@0
  2607
indvdum@0
  2608
	$url = (!$params) ? "{$phpbb_root_path}viewforum.$phpEx" : "{$phpbb_root_path}viewtopic.$phpEx";
indvdum@0
  2609
	$url = append_sid($url, 'f=' . $data['forum_id'] . $params) . $add_anchor;
indvdum@0
  2610
indvdum@0
  2611
	return $url;
indvdum@0
  2612
}
indvdum@0
  2613
indvdum@0
  2614
/**
indvdum@0
  2615
* Handle topic bumping
indvdum@0
  2616
* @param int $forum_id The ID of the forum the topic is being bumped belongs to
indvdum@0
  2617
* @param int $topic_id The ID of the topic is being bumping
indvdum@0
  2618
* @param array $post_data Passes some topic parameters:
indvdum@0
  2619
*				- 'topic_title'
indvdum@0
  2620
*				- 'topic_last_post_id'
indvdum@0
  2621
*				- 'topic_last_poster_id'
indvdum@0
  2622
*				- 'topic_last_post_subject'
indvdum@0
  2623
*				- 'topic_last_poster_name'
indvdum@0
  2624
*				- 'topic_last_poster_colour'
indvdum@0
  2625
* @param int $bump_time The time at which topic was bumped, usually it is a current time as obtained via time(). 
indvdum@0
  2626
* @return string An URL to the bumped topic, example: ./viewtopic.php?forum_id=1&amptopic_id=2&ampp=3#p3
indvdum@0
  2627
*/
indvdum@0
  2628
function phpbb_bump_topic($forum_id, $topic_id, $post_data, $bump_time = false)
indvdum@0
  2629
{
indvdum@0
  2630
	global $config, $db, $user, $phpEx, $phpbb_root_path;
indvdum@0
  2631
indvdum@0
  2632
	if ($bump_time === false)
indvdum@0
  2633
	{
indvdum@0
  2634
		$bump_time = time();
indvdum@0
  2635
	}
indvdum@0
  2636
indvdum@0
  2637
	// Begin bumping
indvdum@0
  2638
	$db->sql_transaction('begin');
indvdum@0
  2639
indvdum@0
  2640
	// Update the topic's last post post_time
indvdum@0
  2641
	$sql = 'UPDATE ' . POSTS_TABLE . "
indvdum@0
  2642
		SET post_time = $bump_time
indvdum@0
  2643
		WHERE post_id = {$post_data['topic_last_post_id']}
indvdum@0
  2644
			AND topic_id = $topic_id";
indvdum@0
  2645
	$db->sql_query($sql);
indvdum@0
  2646
indvdum@0
  2647
	// Sync the topic's last post time, the rest of the topic's last post data isn't changed
indvdum@0
  2648
	$sql = 'UPDATE ' . TOPICS_TABLE . "
indvdum@0
  2649
		SET topic_last_post_time = $bump_time,
indvdum@0
  2650
			topic_bumped = 1,
indvdum@0
  2651
			topic_bumper = " . $user->data['user_id'] . "
indvdum@0
  2652
		WHERE topic_id = $topic_id";
indvdum@0
  2653
	$db->sql_query($sql);
indvdum@0
  2654
indvdum@0
  2655
	// Update the forum's last post info
indvdum@0
  2656
	$sql = 'UPDATE ' . FORUMS_TABLE . "
indvdum@0
  2657
		SET forum_last_post_id = " . $post_data['topic_last_post_id'] . ",
indvdum@0
  2658
			forum_last_poster_id = " . $post_data['topic_last_poster_id'] . ",
indvdum@0
  2659
			forum_last_post_subject = '" . $db->sql_escape($post_data['topic_last_post_subject']) . "',
indvdum@0
  2660
			forum_last_post_time = $bump_time,
indvdum@0
  2661
			forum_last_poster_name = '" . $db->sql_escape($post_data['topic_last_poster_name']) . "',
indvdum@0
  2662
			forum_last_poster_colour = '" . $db->sql_escape($post_data['topic_last_poster_colour']) . "'
indvdum@0
  2663
		WHERE forum_id = $forum_id";
indvdum@0
  2664
	$db->sql_query($sql);
indvdum@0
  2665
indvdum@0
  2666
	// Update bumper's time of the last posting to prevent flood
indvdum@0
  2667
	$sql = 'UPDATE ' . USERS_TABLE . "
indvdum@0
  2668
		SET user_lastpost_time = $bump_time
indvdum@0
  2669
		WHERE user_id = " . $user->data['user_id'];
indvdum@0
  2670
	$db->sql_query($sql);
indvdum@0
  2671
indvdum@0
  2672
	$db->sql_transaction('commit');
indvdum@0
  2673
indvdum@0
  2674
	// Mark this topic as posted to
indvdum@0
  2675
	markread('post', $forum_id, $topic_id, $bump_time);
indvdum@0
  2676
indvdum@0
  2677
	// Mark this topic as read
indvdum@0
  2678
	markread('topic', $forum_id, $topic_id, $bump_time);
indvdum@0
  2679
indvdum@0
  2680
	// Update forum tracking info
indvdum@0
  2681
	if ($config['load_db_lastread'] && $user->data['is_registered'])
indvdum@0
  2682
	{
indvdum@0
  2683
		$sql = 'SELECT mark_time
indvdum@0
  2684
			FROM ' . FORUMS_TRACK_TABLE . '
indvdum@0
  2685
			WHERE user_id = ' . $user->data['user_id'] . '
indvdum@0
  2686
				AND forum_id = ' . $forum_id;
indvdum@0
  2687
		$result = $db->sql_query($sql);
indvdum@0
  2688
		$f_mark_time = (int) $db->sql_fetchfield('mark_time');
indvdum@0
  2689
		$db->sql_freeresult($result);
indvdum@0
  2690
	}
indvdum@0
  2691
	else if ($config['load_anon_lastread'] || $user->data['is_registered'])
indvdum@0
  2692
	{
indvdum@0
  2693
		$f_mark_time = false;
indvdum@0
  2694
	}
indvdum@0
  2695
indvdum@0
  2696
	if (($config['load_db_lastread'] && $user->data['is_registered']) || $config['load_anon_lastread'] || $user->data['is_registered'])
indvdum@0
  2697
	{
indvdum@0
  2698
		// Update forum info
indvdum@0
  2699
		$sql = 'SELECT forum_last_post_time
indvdum@0
  2700
			FROM ' . FORUMS_TABLE . '
indvdum@0
  2701
			WHERE forum_id = ' . $forum_id;
indvdum@0
  2702
		$result = $db->sql_query($sql);
indvdum@0
  2703
		$forum_last_post_time = (int) $db->sql_fetchfield('forum_last_post_time');
indvdum@0
  2704
		$db->sql_freeresult($result);
indvdum@0
  2705
indvdum@0
  2706
		update_forum_tracking_info($forum_id, $forum_last_post_time, $f_mark_time, false);
indvdum@0
  2707
	}
indvdum@0
  2708
indvdum@0
  2709
	add_log('mod', $forum_id, $topic_id, 'LOG_BUMP_TOPIC', $post_data['topic_title']);
indvdum@0
  2710
indvdum@0
  2711
	$url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;p={$post_data['topic_last_post_id']}") . "#p{$post_data['topic_last_post_id']}";
indvdum@0
  2712
indvdum@0
  2713
	return $url;
indvdum@0
  2714
}
indvdum@0
  2715
indvdum@0
  2716
?>