punbb/include/functions.php
changeset 2 a8a21e1c7afa
parent 0 f9ffdbd96607
child 3 c0c445d4a13e
equal deleted inserted replaced
1:8f6143115bf5 2:a8a21e1c7afa
    25 //
    25 //
    26 // Cookie stuff!
    26 // Cookie stuff!
    27 //
    27 //
    28 function check_cookie(&$pun_user)
    28 function check_cookie(&$pun_user)
    29 {
    29 {
    30 	global $db, $db_type, $pun_config, $cookie_name, $cookie_seed;
    30   // Import Enano
    31 
    31   global $db, $session, $paths, $template, $plugins; // Common objects
    32 	$now = time();
    32   
    33 	$expire = $now + 31536000;	// The cookie expires after a year
    33   // Import PunBB
    34 
    34 	global $pun_db, $db_type, $pun_config, $cookie_name, $cookie_seed;
    35 	// We assume it's a guest
    35   
    36 	$cookie = array('user_id' => 1, 'password_hash' => 'Guest');
    36   $now = time();
    37 
    37   
    38 	// If a cookie is set, we get the user_id and password hash from it
    38   if(!$session->started)
    39 	if (isset($_COOKIE[$cookie_name]))
    39     $session->start();
    40 		list($cookie['user_id'], $cookie['password_hash']) = @unserialize($_COOKIE[$cookie_name]);
    40   
    41 
    41   if($session->user_logged_in)
    42 	if ($cookie['user_id'] > 1)
    42   {
    43 	{
    43     $result = $pun_db->query(
    44 		// Check if there's a user with the user ID and password hash from the cookie
    44       'SELECT eu.*, u.*, eu.real_name AS realname, eu.user_level AS g_id, g.*, o.logged, o.idle
    45 		$result = $db->query('SELECT u.*, g.*, o.logged, o.idle FROM '.$db->prefix.'users AS u INNER JOIN '.$db->prefix.'groups AS g ON u.group_id=g.g_id LEFT JOIN '.$db->prefix.'online AS o ON o.user_id=u.id WHERE u.id='.intval($cookie['user_id'])) or error('Unable to fetch user information', __FILE__, __LINE__, $db->error());
    45         FROM '.$pun_db->prefix.'users AS u
    46 		$pun_user = $db->fetch_assoc($result);
    46         LEFT JOIN '.table_prefix.'users AS eu
    47 
    47           ON eu.user_id=u.id
    48 		// If user authorisation failed
    48         INNER JOIN '.$pun_db->prefix.'groups AS g
    49 		if (!isset($pun_user['id']) || md5($cookie_seed.$pun_user['password']) !== $cookie['password_hash'])
    49           ON u.group_id=g.g_id 
    50 		{
    50         LEFT JOIN '.$pun_db->prefix.'online AS o
    51 			pun_setcookie(0, random_pass(8), $expire);
    51           ON o.user_id=u.id
    52 			set_default_user();
    52         WHERE u.id='.intval($session->user_id))
    53 
    53       or error('Unable to fetch user information', __FILE__, __LINE__, $pun_db->error());
    54 			return;
    54 		$pun_user = $pun_db->fetch_assoc($result);
    55 		}
    55     // Set a default language if the user selected language no longer exists
    56 
       
    57 		// Set a default language if the user selected language no longer exists
       
    58 		if (!@file_exists(PUN_ROOT.'lang/'.$pun_user['language']))
    56 		if (!@file_exists(PUN_ROOT.'lang/'.$pun_user['language']))
    59 			$pun_user['language'] = $pun_config['o_default_lang'];
    57 			$pun_user['language'] = $pun_config['o_default_lang'];
    60 
    58 
    61 		// Set a default style if the user selected style no longer exists
    59 		// Set a default style if the user selected style no longer exists
    62 		if (!@file_exists(PUN_ROOT.'style/'.$pun_user['style'].'.css'))
    60 		// if (!@file_exists(PUN_ROOT.'style/'.$pun_user['style'].'.css'))
    63 			$pun_user['style'] = $pun_config['o_default_style'];
    61 		// 	$pun_user['style'] = $pun_config['o_default_style'];
    64 
    62 
    65 		if (!$pun_user['disp_topics'])
    63 		if (!$pun_user['disp_topics'])
    66 			$pun_user['disp_topics'] = $pun_config['o_disp_topics_default'];
    64 			$pun_user['disp_topics'] = $pun_config['o_disp_topics_default'];
    67 		if (!$pun_user['disp_posts'])
    65 		if (!$pun_user['disp_posts'])
    68 			$pun_user['disp_posts'] = $pun_config['o_disp_posts_default'];
    66 			$pun_user['disp_posts'] = $pun_config['o_disp_posts_default'];
    73 		// Define this if you want this visit to affect the online list and the users last visit data
    71 		// Define this if you want this visit to affect the online list and the users last visit data
    74 		if (!defined('PUN_QUIET_VISIT'))
    72 		if (!defined('PUN_QUIET_VISIT'))
    75 		{
    73 		{
    76 			// Update the online list
    74 			// Update the online list
    77 			if (!$pun_user['logged'])
    75 			if (!$pun_user['logged'])
    78 			{
    76 				$pun_db->query('INSERT INTO '.$pun_db->prefix.'online (user_id, ident, logged) VALUES('.$pun_user['id'].', \''.$pun_db->escape($pun_user['username']).'\', '.$now.')') or error('Unable to insert into online list', __FILE__, __LINE__, $pun_db->error());
    79 				$pun_user['logged'] = $now;
       
    80 
       
    81 				// With MySQL/MySQLi, REPLACE INTO avoids a user having two rows in the online table
       
    82 				switch ($db_type)
       
    83 				{
       
    84 					case 'mysql':
       
    85 					case 'mysqli':
       
    86 						$db->query('REPLACE INTO '.$db->prefix.'online (user_id, ident, logged) VALUES('.$pun_user['id'].', \''.$db->escape($pun_user['username']).'\', '.$pun_user['logged'].')') or error('Unable to insert into online list', __FILE__, __LINE__, $db->error());
       
    87 						break;
       
    88 
       
    89 					default:
       
    90 						$db->query('INSERT INTO '.$db->prefix.'online (user_id, ident, logged) VALUES('.$pun_user['id'].', \''.$db->escape($pun_user['username']).'\', '.$pun_user['logged'].')') or error('Unable to insert into online list', __FILE__, __LINE__, $db->error());
       
    91 						break;
       
    92 				}
       
    93 			}
       
    94 			else
    77 			else
    95 			{
    78 			{
    96 				// Special case: We've timed out, but no other user has browsed the forums since we timed out
    79 				// Special case: We've timed out, but no other user has browsed the forums since we timed out
    97 				if ($pun_user['logged'] < ($now-$pun_config['o_timeout_visit']))
    80 				if ($pun_user['logged'] < ($now-$pun_config['o_timeout_visit']))
    98 				{
    81 				{
    99 					$db->query('UPDATE '.$db->prefix.'users SET last_visit='.$pun_user['logged'].' WHERE id='.$pun_user['id']) or error('Unable to update user visit data', __FILE__, __LINE__, $db->error());
    82 					$pun_db->query('UPDATE '.$pun_db->prefix.'users SET last_visit='.$pun_user['logged'].' WHERE id='.$pun_user['id']) or error('Unable to update user visit data', __FILE__, __LINE__, $pun_db->error());
   100 					$pun_user['last_visit'] = $pun_user['logged'];
    83 					$pun_user['last_visit'] = $pun_user['logged'];
   101 				}
    84 				}
   102 
    85 
   103 				$idle_sql = ($pun_user['idle'] == '1') ? ', idle=0' : '';
    86 				$idle_sql = ($pun_user['idle'] == '1') ? ', idle=0' : '';
   104 				$db->query('UPDATE '.$db->prefix.'online SET logged='.$now.$idle_sql.' WHERE user_id='.$pun_user['id']) or error('Unable to update online list', __FILE__, __LINE__, $db->error());
    87 				$pun_db->query('UPDATE '.$pun_db->prefix.'online SET logged='.$now.$idle_sql.' WHERE user_id='.$pun_user['id']) or error('Unable to update online list', __FILE__, __LINE__, $pun_db->error());
   105 			}
    88 			}
   106 		}
    89 		}
   107 
    90 
   108 		$pun_user['is_guest'] = false;
    91 		$pun_user['is_guest'] = false;
   109 	}
    92   }
   110 	else
    93   else
   111 		set_default_user();
    94   {
       
    95     set_default_user();
       
    96   }
   112 }
    97 }
   113 
    98 
   114 
    99 
   115 //
   100 //
   116 // Fill $pun_user with default values (for guests)
   101 // Fill $pun_user with default values (for guests)
   117 //
   102 //
   118 function set_default_user()
   103 function set_default_user()
   119 {
   104 {
   120 	global $db, $db_type, $pun_user, $pun_config;
   105 	global $pun_db, $pun_user, $pun_config;
   121 
   106 
   122 	$remote_addr = get_remote_address();
   107 	$remote_addr = get_remote_address();
   123 
   108 
   124 	// Fetch guest user
   109 	// Fetch guest user
   125 	$result = $db->query('SELECT u.*, g.*, o.logged FROM '.$db->prefix.'users AS u INNER JOIN '.$db->prefix.'groups AS g ON u.group_id=g.g_id LEFT JOIN '.$db->prefix.'online AS o ON o.ident=\''.$remote_addr.'\' WHERE u.id=1') or error('Unable to fetch guest information', __FILE__, __LINE__, $db->error());
   110 	$result = $pun_db->query('SELECT u.*, g.*, o.logged FROM '.$pun_db->prefix.'users AS u INNER JOIN '.$pun_db->prefix.'groups AS g ON u.group_id=g.g_id LEFT JOIN '.$pun_db->prefix.'online AS o ON o.ident=\''.$remote_addr.'\' WHERE u.id=1') or error('Unable to fetch guest information', __FILE__, __LINE__, $pun_db->error());
   126 	if (!$db->num_rows($result))
   111 	if (!$pun_db->num_rows($result))
   127 		exit('Unable to fetch guest information. The table \''.$db->prefix.'users\' must contain an entry with id = 1 that represents anonymous users.');
   112 		exit('Unable to fetch guest information. The table \''.$pun_db->prefix.'users\' must contain an entry with id = 1 that represents anonymous users.');
   128 
   113 
   129 	$pun_user = $db->fetch_assoc($result);
   114 	$pun_user = $pun_db->fetch_assoc($result);
   130 
   115 
   131 	// Update online list
   116 	// Update online list
   132 	if (!$pun_user['logged'])
   117 	if (!$pun_user['logged'])
   133 	{
   118 		$pun_db->query('INSERT INTO '.$pun_db->prefix.'online (user_id, ident, logged) VALUES(1, \''.$pun_db->escape($remote_addr).'\', '.time().')') or error('Unable to insert into online list', __FILE__, __LINE__, $pun_db->error());
   134 		$pun_user['logged'] = time();
       
   135 
       
   136 		// With MySQL/MySQLi, REPLACE INTO avoids a user having two rows in the online table
       
   137 		switch ($db_type)
       
   138 		{
       
   139 			case 'mysql':
       
   140 			case 'mysqli':
       
   141 				$db->query('REPLACE INTO '.$db->prefix.'online (user_id, ident, logged) VALUES(1, \''.$db->escape($remote_addr).'\', '.$pun_user['logged'].')') or error('Unable to insert into online list', __FILE__, __LINE__, $db->error());
       
   142 				break;
       
   143 
       
   144 			default:
       
   145 				$db->query('INSERT INTO '.$db->prefix.'online (user_id, ident, logged) VALUES(1, \''.$db->escape($remote_addr).'\', '.$pun_user['logged'].')') or error('Unable to insert into online list', __FILE__, __LINE__, $db->error());
       
   146 				break;
       
   147 		}
       
   148 	}
       
   149 	else
   119 	else
   150 		$db->query('UPDATE '.$db->prefix.'online SET logged='.time().' WHERE ident=\''.$db->escape($remote_addr).'\'') or error('Unable to update online list', __FILE__, __LINE__, $db->error());
   120 		$pun_db->query('UPDATE '.$pun_db->prefix.'online SET logged='.time().' WHERE ident=\''.$pun_db->escape($remote_addr).'\'') or error('Unable to update online list', __FILE__, __LINE__, $pun_db->error());
   151 
   121 
   152 	$pun_user['disp_topics'] = $pun_config['o_disp_topics_default'];
   122 	$pun_user['disp_topics'] = $pun_config['o_disp_topics_default'];
   153 	$pun_user['disp_posts'] = $pun_config['o_disp_posts_default'];
   123 	$pun_user['disp_posts'] = $pun_config['o_disp_posts_default'];
   154 	$pun_user['timezone'] = $pun_config['o_server_timezone'];
   124 	$pun_user['timezone'] = $pun_config['o_server_timezone'];
   155 	$pun_user['language'] = $pun_config['o_default_lang'];
   125 	$pun_user['language'] = $pun_config['o_default_lang'];
   178 //
   148 //
   179 // Check whether the connecting user is banned (and delete any expired bans while we're at it)
   149 // Check whether the connecting user is banned (and delete any expired bans while we're at it)
   180 //
   150 //
   181 function check_bans()
   151 function check_bans()
   182 {
   152 {
   183 	global $db, $pun_config, $lang_common, $pun_user, $pun_bans;
   153 	global $pun_db, $pun_config, $lang_common, $pun_user, $pun_bans;
   184 
   154 
   185 	// Admins aren't affected
   155 	// Admins aren't affected
   186 	if ($pun_user['g_id'] == PUN_ADMIN || !$pun_bans)
   156 	if ($pun_user['g_id'] == PUN_ADMIN || !$pun_bans)
   187 		return;
   157 		return;
   188 
   158 
   193 	foreach ($pun_bans as $cur_ban)
   163 	foreach ($pun_bans as $cur_ban)
   194 	{
   164 	{
   195 		// Has this ban expired?
   165 		// Has this ban expired?
   196 		if ($cur_ban['expire'] != '' && $cur_ban['expire'] <= time())
   166 		if ($cur_ban['expire'] != '' && $cur_ban['expire'] <= time())
   197 		{
   167 		{
   198 			$db->query('DELETE FROM '.$db->prefix.'bans WHERE id='.$cur_ban['id']) or error('Unable to delete expired ban', __FILE__, __LINE__, $db->error());
   168 			$pun_db->query('DELETE FROM '.$pun_db->prefix.'bans WHERE id='.$cur_ban['id']) or error('Unable to delete expired ban', __FILE__, __LINE__, $pun_db->error());
   199 			$bans_altered = true;
   169 			$bans_altered = true;
   200 			continue;
   170 			continue;
   201 		}
   171 		}
   202 
   172 
   203 		if ($cur_ban['username'] != '' && !strcasecmp($pun_user['username'], $cur_ban['username']))
   173 		if ($cur_ban['username'] != '' && !strcasecmp($pun_user['username'], $cur_ban['username']))
   204 		{
   174 		{
   205 			$db->query('DELETE FROM '.$db->prefix.'online WHERE ident=\''.$db->escape($pun_user['username']).'\'') or error('Unable to delete from online list', __FILE__, __LINE__, $db->error());
   175 			$pun_db->query('DELETE FROM '.$pun_db->prefix.'online WHERE ident=\''.$pun_db->escape($pun_user['username']).'\'') or error('Unable to delete from online list', __FILE__, __LINE__, $pun_db->error());
   206 			message($lang_common['Ban message'].' '.(($cur_ban['expire'] != '') ? $lang_common['Ban message 2'].' '.strtolower(format_time($cur_ban['expire'], true)).'. ' : '').(($cur_ban['message'] != '') ? $lang_common['Ban message 3'].'<br /><br /><strong>'.pun_htmlspecialchars($cur_ban['message']).'</strong><br /><br />' : '<br /><br />').$lang_common['Ban message 4'].' <a href="mailto:'.$pun_config['o_admin_email'].'">'.$pun_config['o_admin_email'].'</a>.', true);
   176 			message($lang_common['Ban message'].' '.(($cur_ban['expire'] != '') ? $lang_common['Ban message 2'].' '.strtolower(format_time($cur_ban['expire'], true)).'. ' : '').(($cur_ban['message'] != '') ? $lang_common['Ban message 3'].'<br /><br /><strong>'.pun_htmlspecialchars($cur_ban['message']).'</strong><br /><br />' : '<br /><br />').$lang_common['Ban message 4'].' <a href="mailto:'.$pun_config['o_admin_email'].'">'.$pun_config['o_admin_email'].'</a>.', true);
   207 		}
   177 		}
   208 
   178 
   209 		if ($cur_ban['ip'] != '')
   179 		if ($cur_ban['ip'] != '')
   210 		{
   180 		{
   214 			{
   184 			{
   215 				$cur_ban_ips[$i] = $cur_ban_ips[$i].'.';
   185 				$cur_ban_ips[$i] = $cur_ban_ips[$i].'.';
   216 
   186 
   217 				if (substr($user_ip, 0, strlen($cur_ban_ips[$i])) == $cur_ban_ips[$i])
   187 				if (substr($user_ip, 0, strlen($cur_ban_ips[$i])) == $cur_ban_ips[$i])
   218 				{
   188 				{
   219 					$db->query('DELETE FROM '.$db->prefix.'online WHERE ident=\''.$db->escape($pun_user['username']).'\'') or error('Unable to delete from online list', __FILE__, __LINE__, $db->error());
   189 					$pun_db->query('DELETE FROM '.$pun_db->prefix.'online WHERE ident=\''.$pun_db->escape($pun_user['username']).'\'') or error('Unable to delete from online list', __FILE__, __LINE__, $pun_db->error());
   220 					message($lang_common['Ban message'].' '.(($cur_ban['expire'] != '') ? $lang_common['Ban message 2'].' '.strtolower(format_time($cur_ban['expire'], true)).'. ' : '').(($cur_ban['message'] != '') ? $lang_common['Ban message 3'].'<br /><br /><strong>'.pun_htmlspecialchars($cur_ban['message']).'</strong><br /><br />' : '<br /><br />').$lang_common['Ban message 4'].' <a href="mailto:'.$pun_config['o_admin_email'].'">'.$pun_config['o_admin_email'].'</a>.', true);
   190 					message($lang_common['Ban message'].' '.(($cur_ban['expire'] != '') ? $lang_common['Ban message 2'].' '.strtolower(format_time($cur_ban['expire'], true)).'. ' : '').(($cur_ban['message'] != '') ? $lang_common['Ban message 3'].'<br /><br /><strong>'.pun_htmlspecialchars($cur_ban['message']).'</strong><br /><br />' : '<br /><br />').$lang_common['Ban message 4'].' <a href="mailto:'.$pun_config['o_admin_email'].'">'.$pun_config['o_admin_email'].'</a>.', true);
   221 				}
   191 				}
   222 			}
   192 			}
   223 		}
   193 		}
   224 	}
   194 	}
   235 //
   205 //
   236 // Update "Users online"
   206 // Update "Users online"
   237 //
   207 //
   238 function update_users_online()
   208 function update_users_online()
   239 {
   209 {
   240 	global $db, $pun_config, $pun_user;
   210 	global $pun_db, $pun_config, $pun_user;
   241 
   211 
   242 	$now = time();
   212 	$now = time();
   243 
   213 
   244 	// Fetch all online list entries that are older than "o_timeout_online"
   214 	// Fetch all online list entries that are older than "o_timeout_online"
   245 	$result = $db->query('SELECT * FROM '.$db->prefix.'online WHERE logged<'.($now-$pun_config['o_timeout_online'])) or error('Unable to fetch old entries from online list', __FILE__, __LINE__, $db->error());
   215 	$result = $pun_db->query('SELECT * FROM '.$pun_db->prefix.'online WHERE logged<'.($now-$pun_config['o_timeout_online'])) or error('Unable to fetch old entries from online list', __FILE__, __LINE__, $pun_db->error());
   246 	while ($cur_user = $db->fetch_assoc($result))
   216 	while ($cur_user = $pun_db->fetch_assoc($result))
   247 	{
   217 	{
   248 		// If the entry is a guest, delete it
   218 		// If the entry is a guest, delete it
   249 		if ($cur_user['user_id'] == '1')
   219 		if ($cur_user['user_id'] == '1')
   250 			$db->query('DELETE FROM '.$db->prefix.'online WHERE ident=\''.$db->escape($cur_user['ident']).'\'') or error('Unable to delete from online list', __FILE__, __LINE__, $db->error());
   220 			$pun_db->query('DELETE FROM '.$pun_db->prefix.'online WHERE ident=\''.$pun_db->escape($cur_user['ident']).'\'') or error('Unable to delete from online list', __FILE__, __LINE__, $pun_db->error());
   251 		else
   221 		else
   252 		{
   222 		{
   253 			// If the entry is older than "o_timeout_visit", update last_visit for the user in question, then delete him/her from the online list
   223 			// If the entry is older than "o_timeout_visit", update last_visit for the user in question, then delete him/her from the online list
   254 			if ($cur_user['logged'] < ($now-$pun_config['o_timeout_visit']))
   224 			if ($cur_user['logged'] < ($now-$pun_config['o_timeout_visit']))
   255 			{
   225 			{
   256 				$db->query('UPDATE '.$db->prefix.'users SET last_visit='.$cur_user['logged'].' WHERE id='.$cur_user['user_id']) or error('Unable to update user visit data', __FILE__, __LINE__, $db->error());
   226 				$pun_db->query('UPDATE '.$pun_db->prefix.'users SET last_visit='.$cur_user['logged'].' WHERE id='.$cur_user['user_id']) or error('Unable to update user visit data', __FILE__, __LINE__, $pun_db->error());
   257 				$db->query('DELETE FROM '.$db->prefix.'online WHERE user_id='.$cur_user['user_id']) or error('Unable to delete from online list', __FILE__, __LINE__, $db->error());
   227 				$pun_db->query('DELETE FROM '.$pun_db->prefix.'online WHERE user_id='.$cur_user['user_id']) or error('Unable to delete from online list', __FILE__, __LINE__, $pun_db->error());
   258 			}
   228 			}
   259 			else if ($cur_user['idle'] == '0')
   229 			else if ($cur_user['idle'] == '0')
   260 				$db->query('UPDATE '.$db->prefix.'online SET idle=1 WHERE user_id='.$cur_user['user_id']) or error('Unable to insert into online list', __FILE__, __LINE__, $db->error());
   230 				$pun_db->query('UPDATE '.$pun_db->prefix.'online SET idle=1 WHERE user_id='.$cur_user['user_id']) or error('Unable to insert into online list', __FILE__, __LINE__, $pun_db->error());
   261 		}
   231 		}
   262 	}
   232 	}
   263 }
   233 }
   264 
       
   265 
   234 
   266 //
   235 //
   267 // Generate the "navigator" that appears at the top of every page
   236 // Generate the "navigator" that appears at the top of every page
   268 //
   237 //
   269 function generate_navlinks()
   238 function generate_navlinks()
   281 	{
   250 	{
   282 		if ($pun_user['g_search'] == '1')
   251 		if ($pun_user['g_search'] == '1')
   283 			$links[] = '<li id="navsearch"><a href="search.php">'.$lang_common['Search'].'</a>';
   252 			$links[] = '<li id="navsearch"><a href="search.php">'.$lang_common['Search'].'</a>';
   284 
   253 
   285 		$links[] = '<li id="navregister"><a href="register.php">'.$lang_common['Register'].'</a>';
   254 		$links[] = '<li id="navregister"><a href="register.php">'.$lang_common['Register'].'</a>';
   286 		$links[] = '<li id="navlogin"><a href="login.php">'.$lang_common['Login'].'</a>';
   255 		// $links[] = '<li id="navlogin"><a href="login.php">'.$lang_common['Login'].'</a>';
   287 
   256 
   288 		$info = $lang_common['Not logged in'];
   257 		$info = $lang_common['Not logged in'];
   289 	}
   258 	}
   290 	else
   259 	else
   291 	{
   260 	{
   292 		if ($pun_user['g_id'] > PUN_MOD)
   261 		if ($pun_user['g_id'] < PUN_MOD)
   293 		{
   262 		{
   294 			if ($pun_user['g_search'] == '1')
   263 			if ($pun_user['g_search'] == '1')
   295 				$links[] = '<li id="navsearch"><a href="search.php">'.$lang_common['Search'].'</a>';
   264 				$links[] = '<li id="navsearch"><a href="search.php">'.$lang_common['Search'].'</a>';
   296 
   265 
   297 			$links[] = '<li id="navprofile"><a href="profile.php?id='.$pun_user['id'].'">'.$lang_common['Profile'].'</a>';
   266 			$links[] = '<li id="navprofile"><a href="profile.php?id='.$pun_user['id'].'">'.$lang_common['Profile'].'</a>';
   298 			$links[] = '<li id="navlogout"><a href="login.php?action=out&amp;id='.$pun_user['id'].'">'.$lang_common['Logout'].'</a>';
   267 			// $links[] = '<li id="navlogout"><a href="login.php?action=out&amp;id='.$pun_user['id'].'">'.$lang_common['Logout'].'</a>';
   299 		}
   268 		}
   300 		else
   269 		else
   301 		{
   270 		{
   302 			$links[] = '<li id="navsearch"><a href="search.php">'.$lang_common['Search'].'</a>';
   271 			$links[] = '<li id="navsearch"><a href="search.php">'.$lang_common['Search'].'</a>';
   303 			$links[] = '<li id="navprofile"><a href="profile.php?id='.$pun_user['id'].'">'.$lang_common['Profile'].'</a>';
   272 			$links[] = '<li id="navprofile"><a href="profile.php?id='.$pun_user['id'].'">'.$lang_common['Profile'].'</a>';
   304 			$links[] = '<li id="navadmin"><a href="admin_index.php">'.$lang_common['Admin'].'</a>';
   273 			$links[] = '<li id="navadmin"><a href="admin_index.php">'.$lang_common['Admin'].'</a>';
   305 			$links[] = '<li id="navlogout"><a href="login.php?action=out&amp;id='.$pun_user['id'].'">'.$lang_common['Logout'].'</a>';
   274 			// $links[] = '<li id="navlogout"><a href="login.php?action=out&amp;id='.$pun_user['id'].'">'.$lang_common['Logout'].'</a>';
   306 		}
   275 		}
   307 	}
   276 	}
   308 
   277 
   309 	// Are there any additional navlinks we should insert into the array before imploding it?
   278 	// Are there any additional navlinks we should insert into the array before imploding it?
   310 	if ($pun_config['o_additional_navlinks'] != '')
   279 	if ($pun_config['o_additional_navlinks'] != '')
   324 //
   293 //
   325 // Display the profile navigation menu
   294 // Display the profile navigation menu
   326 //
   295 //
   327 function generate_profile_menu($page = '')
   296 function generate_profile_menu($page = '')
   328 {
   297 {
   329 	global $lang_profile, $pun_config, $pun_user, $id;
   298 	global $lang_profile, $pun_config, $pun_user, $id, $lang_common;
   330 
   299 
   331 ?>
   300 ?>
   332 <div id="profile" class="block2col">
   301 <div id="profile" class="block2col">
   333 	<div class="blockmenu">
   302 	<div class="blockmenu">
   334 		<h2><span><?php echo $lang_profile['Profile menu'] ?></span></h2>
   303 		<h2><span><?php echo $lang_profile['Profile menu'] ?></span></h2>
   354 //
   323 //
   355 // Update posts, topics, last_post, last_post_id and last_poster for a forum (redirect topics are not included)
   324 // Update posts, topics, last_post, last_post_id and last_poster for a forum (redirect topics are not included)
   356 //
   325 //
   357 function update_forum($forum_id)
   326 function update_forum($forum_id)
   358 {
   327 {
   359 	global $db;
   328 	global $pun_db;
   360 
   329 
   361 	$result = $db->query('SELECT COUNT(id), SUM(num_replies) FROM '.$db->prefix.'topics WHERE moved_to IS NULL AND forum_id='.$forum_id) or error('Unable to fetch forum topic count', __FILE__, __LINE__, $db->error());
   330 	$result = $pun_db->query('SELECT COUNT(id), SUM(num_replies) FROM '.$pun_db->prefix.'topics WHERE moved_to IS NULL AND forum_id='.$forum_id) or error('Unable to fetch forum topic count', __FILE__, __LINE__, $pun_db->error());
   362 	list($num_topics, $num_posts) = $db->fetch_row($result);
   331 	list($num_topics, $num_posts) = $pun_db->fetch_row($result);
   363 
   332 
   364 	$num_posts = $num_posts + $num_topics;		// $num_posts is only the sum of all replies (we have to add the topic posts)
   333 	$num_posts = $num_posts + $num_topics;		// $num_posts is only the sum of all replies (we have to add the topic posts)
   365 
   334 
   366 	$result = $db->query('SELECT last_post, last_post_id, last_poster FROM '.$db->prefix.'topics WHERE forum_id='.$forum_id.' AND moved_to IS NULL ORDER BY last_post DESC LIMIT 1') or error('Unable to fetch last_post/last_post_id/last_poster', __FILE__, __LINE__, $db->error());
   335 	$result = $pun_db->query('SELECT last_post, last_post_id, last_poster FROM '.$pun_db->prefix.'topics WHERE forum_id='.$forum_id.' AND moved_to IS NULL ORDER BY last_post DESC LIMIT 1') or error('Unable to fetch last_post/last_post_id/last_poster', __FILE__, __LINE__, $pun_db->error());
   367 	if ($db->num_rows($result))		// There are topics in the forum
   336 	if ($pun_db->num_rows($result))		// There are topics in the forum
   368 	{
   337 	{
   369 		list($last_post, $last_post_id, $last_poster) = $db->fetch_row($result);
   338 		list($last_post, $last_post_id, $last_poster) = $pun_db->fetch_row($result);
   370 
   339 
   371 		$db->query('UPDATE '.$db->prefix.'forums SET num_topics='.$num_topics.', num_posts='.$num_posts.', last_post='.$last_post.', last_post_id='.$last_post_id.', last_poster=\''.$db->escape($last_poster).'\' WHERE id='.$forum_id) or error('Unable to update last_post/last_post_id/last_poster', __FILE__, __LINE__, $db->error());
   340 		$pun_db->query('UPDATE '.$pun_db->prefix.'forums SET num_topics='.$num_topics.', num_posts='.$num_posts.', last_post='.$last_post.', last_post_id='.$last_post_id.', last_poster=\''.$pun_db->escape($last_poster).'\' WHERE id='.$forum_id) or error('Unable to update last_post/last_post_id/last_poster', __FILE__, __LINE__, $pun_db->error());
   372 	}
   341 	}
   373 	else	// There are no topics
   342 	else	// There are no topics
   374 		$db->query('UPDATE '.$db->prefix.'forums SET num_topics=0, num_posts=0, last_post=NULL, last_post_id=NULL, last_poster=NULL WHERE id='.$forum_id) or error('Unable to update last_post/last_post_id/last_poster', __FILE__, __LINE__, $db->error());
   343 		$pun_db->query('UPDATE '.$pun_db->prefix.'forums SET num_topics=0, num_posts=0, last_post=NULL, last_post_id=NULL, last_poster=NULL WHERE id='.$forum_id) or error('Unable to update last_post/last_post_id/last_poster', __FILE__, __LINE__, $pun_db->error());
   375 }
   344 }
   376 
   345 
   377 
   346 
   378 //
   347 //
   379 // Delete a topic and all of it's posts
   348 // Delete a topic and all of it's posts
   380 //
   349 //
   381 function delete_topic($topic_id)
   350 function delete_topic($topic_id)
   382 {
   351 {
   383 	global $db;
   352 	global $pun_db;
   384 
   353 
   385 	// Delete the topic and any redirect topics
   354 	// Delete the topic and any redirect topics
   386 	$db->query('DELETE FROM '.$db->prefix.'topics WHERE id='.$topic_id.' OR moved_to='.$topic_id) or error('Unable to delete topic', __FILE__, __LINE__, $db->error());
   355 	$pun_db->query('DELETE FROM '.$pun_db->prefix.'topics WHERE id='.$topic_id.' OR moved_to='.$topic_id) or error('Unable to delete topic', __FILE__, __LINE__, $pun_db->error());
   387 
   356 
   388 	// Create a list of the post ID's in this topic
   357 	// Create a list of the post ID's in this topic
   389 	$post_ids = '';
   358 	$post_ids = '';
   390 	$result = $db->query('SELECT id FROM '.$db->prefix.'posts WHERE topic_id='.$topic_id) or error('Unable to fetch posts', __FILE__, __LINE__, $db->error());
   359 	$result = $pun_db->query('SELECT id FROM '.$pun_db->prefix.'posts WHERE topic_id='.$topic_id) or error('Unable to fetch posts', __FILE__, __LINE__, $pun_db->error());
   391 	while ($row = $db->fetch_row($result))
   360 	while ($row = $pun_db->fetch_row($result))
   392 		$post_ids .= ($post_ids != '') ? ','.$row[0] : $row[0];
   361 		$post_ids .= ($post_ids != '') ? ','.$row[0] : $row[0];
   393 
   362 
   394 	// Make sure we have a list of post ID's
   363 	// Make sure we have a list of post ID's
   395 	if ($post_ids != '')
   364 	if ($post_ids != '')
   396 	{
   365 	{
   397 		strip_search_index($post_ids);
   366 		strip_search_index($post_ids);
   398 
   367 
   399 		// Delete posts in topic
   368 		// Delete posts in topic
   400 		$db->query('DELETE FROM '.$db->prefix.'posts WHERE topic_id='.$topic_id) or error('Unable to delete posts', __FILE__, __LINE__, $db->error());
   369 		$pun_db->query('DELETE FROM '.$pun_db->prefix.'posts WHERE topic_id='.$topic_id) or error('Unable to delete posts', __FILE__, __LINE__, $pun_db->error());
   401 	}
   370 	}
   402 
   371 
   403 	// Delete any subscriptions for this topic
   372 	// Delete any subscriptions for this topic
   404 	$db->query('DELETE FROM '.$db->prefix.'subscriptions WHERE topic_id='.$topic_id) or error('Unable to delete subscriptions', __FILE__, __LINE__, $db->error());
   373 	$pun_db->query('DELETE FROM '.$pun_db->prefix.'subscriptions WHERE topic_id='.$topic_id) or error('Unable to delete subscriptions', __FILE__, __LINE__, $pun_db->error());
   405 }
   374 }
   406 
   375 
   407 
   376 
   408 //
   377 //
   409 // Delete a single post
   378 // Delete a single post
   410 //
   379 //
   411 function delete_post($post_id, $topic_id)
   380 function delete_post($post_id, $topic_id)
   412 {
   381 {
   413 	global $db;
   382 	global $pun_db;
   414 
   383 
   415 	$result = $db->query('SELECT id, poster, posted FROM '.$db->prefix.'posts WHERE topic_id='.$topic_id.' ORDER BY id DESC LIMIT 2') or error('Unable to fetch post info', __FILE__, __LINE__, $db->error());
   384 	$result = $pun_db->query('SELECT id, poster, posted FROM '.$pun_db->prefix.'posts WHERE topic_id='.$topic_id.' ORDER BY id DESC LIMIT 2') or error('Unable to fetch post info', __FILE__, __LINE__, $pun_db->error());
   416 	list($last_id, ,) = $db->fetch_row($result);
   385 	list($last_id, ,) = $pun_db->fetch_row($result);
   417 	list($second_last_id, $second_poster, $second_posted) = $db->fetch_row($result);
   386 	list($second_last_id, $second_poster, $second_posted) = $pun_db->fetch_row($result);
   418 
   387 
   419 	// Delete the post
   388 	// Delete the post
   420 	$db->query('DELETE FROM '.$db->prefix.'posts WHERE id='.$post_id) or error('Unable to delete post', __FILE__, __LINE__, $db->error());
   389 	$pun_db->query('DELETE FROM '.$pun_db->prefix.'posts WHERE id='.$post_id) or error('Unable to delete post', __FILE__, __LINE__, $pun_db->error());
   421 
   390 
   422 	strip_search_index($post_id);
   391 	strip_search_index($post_id);
   423 
   392 
   424 	// Count number of replies in the topic
   393 	// Count number of replies in the topic
   425 	$result = $db->query('SELECT COUNT(id) FROM '.$db->prefix.'posts WHERE topic_id='.$topic_id) or error('Unable to fetch post count for topic', __FILE__, __LINE__, $db->error());
   394 	$result = $pun_db->query('SELECT COUNT(id) FROM '.$pun_db->prefix.'posts WHERE topic_id='.$topic_id) or error('Unable to fetch post count for topic', __FILE__, __LINE__, $pun_db->error());
   426 	$num_replies = $db->result($result, 0) - 1;
   395 	$num_replies = $pun_db->result($result, 0) - 1;
   427 
   396 
   428 	// If the message we deleted is the most recent in the topic (at the end of the topic)
   397 	// If the message we deleted is the most recent in the topic (at the end of the topic)
   429 	if ($last_id == $post_id)
   398 	if ($last_id == $post_id)
   430 	{
   399 	{
   431 		// If there is a $second_last_id there is more than 1 reply to the topic
   400 		// If there is a $second_last_id there is more than 1 reply to the topic
   432 		if (!empty($second_last_id))
   401 		if (!empty($second_last_id))
   433 			$db->query('UPDATE '.$db->prefix.'topics SET last_post='.$second_posted.', last_post_id='.$second_last_id.', last_poster=\''.$db->escape($second_poster).'\', num_replies='.$num_replies.' WHERE id='.$topic_id) or error('Unable to update topic', __FILE__, __LINE__, $db->error());
   402 			$pun_db->query('UPDATE '.$pun_db->prefix.'topics SET last_post='.$second_posted.', last_post_id='.$second_last_id.', last_poster=\''.$pun_db->escape($second_poster).'\', num_replies='.$num_replies.' WHERE id='.$topic_id) or error('Unable to update topic', __FILE__, __LINE__, $pun_db->error());
   434 		else
   403 		else
   435 			// We deleted the only reply, so now last_post/last_post_id/last_poster is posted/id/poster from the topic itself
   404 			// We deleted the only reply, so now last_post/last_post_id/last_poster is posted/id/poster from the topic itself
   436 			$db->query('UPDATE '.$db->prefix.'topics SET last_post=posted, last_post_id=id, last_poster=poster, num_replies='.$num_replies.' WHERE id='.$topic_id) or error('Unable to update topic', __FILE__, __LINE__, $db->error());
   405 			$pun_db->query('UPDATE '.$pun_db->prefix.'topics SET last_post=posted, last_post_id=id, last_poster=poster, num_replies='.$num_replies.' WHERE id='.$topic_id) or error('Unable to update topic', __FILE__, __LINE__, $pun_db->error());
   437 	}
   406 	}
   438 	else
   407 	else
   439 		// Otherwise we just decrement the reply counter
   408 		// Otherwise we just decrement the reply counter
   440 		$db->query('UPDATE '.$db->prefix.'topics SET num_replies='.$num_replies.' WHERE id='.$topic_id) or error('Unable to update topic', __FILE__, __LINE__, $db->error());
   409 		$pun_db->query('UPDATE '.$pun_db->prefix.'topics SET num_replies='.$num_replies.' WHERE id='.$topic_id) or error('Unable to update topic', __FILE__, __LINE__, $pun_db->error());
   441 }
   410 }
   442 
   411 
   443 
   412 
   444 //
   413 //
   445 // Replace censored words in $text
   414 // Replace censored words in $text
   446 //
   415 //
   447 function censor_words($text)
   416 function censor_words($text)
   448 {
   417 {
   449 	global $db;
   418 	global $pun_db;
   450 	static $search_for, $replace_with;
   419 	static $search_for, $replace_with;
   451 
   420 
   452 	// If not already built in a previous call, build an array of censor words and their replacement text
   421 	// If not already built in a previous call, build an array of censor words and their replacement text
   453 	if (!isset($search_for))
   422 	if (!isset($search_for))
   454 	{
   423 	{
   455 		$result = $db->query('SELECT search_for, replace_with FROM '.$db->prefix.'censoring') or error('Unable to fetch censor word list', __FILE__, __LINE__, $db->error());
   424 		$result = $pun_db->query('SELECT search_for, replace_with FROM '.$pun_db->prefix.'censoring') or error('Unable to fetch censor word list', __FILE__, __LINE__, $pun_db->error());
   456 		$num_words = $db->num_rows($result);
   425 		$num_words = $pun_db->num_rows($result);
   457 
   426 
   458 		$search_for = array();
   427 		$search_for = array();
   459 		for ($i = 0; $i < $num_words; ++$i)
   428 		for ($i = 0; $i < $num_words; ++$i)
   460 		{
   429 		{
   461 			list($search_for[$i], $replace_with[$i]) = $db->fetch_row($result);
   430 			list($search_for[$i], $replace_with[$i]) = $pun_db->fetch_row($result);
   462 			$search_for[$i] = '/\b('.str_replace('\*', '\w*?', preg_quote($search_for[$i], '/')).')\b/i';
   431 			$search_for[$i] = '/\b('.str_replace('\*', '\w*?', preg_quote($search_for[$i], '/')).')\b/i';
   463 		}
   432 		}
   464 	}
   433 	}
   465 
   434 
   466 	if (!empty($search_for))
   435 	if (!empty($search_for))
   474 // Determines the correct title for $user
   443 // Determines the correct title for $user
   475 // $user must contain the elements 'username', 'title', 'posts', 'g_id' and 'g_user_title'
   444 // $user must contain the elements 'username', 'title', 'posts', 'g_id' and 'g_user_title'
   476 //
   445 //
   477 function get_title($user)
   446 function get_title($user)
   478 {
   447 {
   479 	global $db, $pun_config, $pun_bans, $lang_common;
   448 	global $pun_db, $pun_config, $pun_bans, $lang_common;
   480 	static $ban_list, $pun_ranks;
   449 	static $ban_list, $pun_ranks;
   481 
   450 
   482 	// If not already built in a previous call, build an array of lowercase banned usernames
   451 	// If not already built in a previous call, build an array of lowercase banned usernames
   483 	if (empty($ban_list))
   452 	if (empty($ban_list))
   484 	{
   453 	{
   535 
   504 
   536 
   505 
   537 //
   506 //
   538 // Generate a string with numbered links (for multipage scripts)
   507 // Generate a string with numbered links (for multipage scripts)
   539 //
   508 //
   540 function paginate($num_pages, $cur_page, $link_to)
   509 function pun_paginate($num_pages, $cur_page, $link_to)
   541 {
   510 {
   542 	$pages = array();
   511 	$pages = array();
   543 	$link_to_all = false;
   512 	$link_to_all = false;
   544 
   513 
   545 	// If $cur_page == -1, we link to all pages (used in viewforum.php)
   514 	// If $cur_page == -1, we link to all pages (used in viewforum.php)
   588 //
   557 //
   589 // Display a message
   558 // Display a message
   590 //
   559 //
   591 function message($message, $no_back_link = false)
   560 function message($message, $no_back_link = false)
   592 {
   561 {
   593 	global $db, $lang_common, $pun_config, $pun_start, $tpl_main;
   562 	global $pun_db, $lang_common, $pun_config, $pun_start, $tpl_main;
   594 
   563 
   595 	if (!defined('PUN_HEADER'))
   564 	if (!defined('PUN_HEADER'))
   596 	{
   565 	{
   597 		global $pun_user;
   566 		global $pun_user;
   598 
   567 
   768 //
   737 //
   769 // Display a message when board is in maintenance mode
   738 // Display a message when board is in maintenance mode
   770 //
   739 //
   771 function maintenance_message()
   740 function maintenance_message()
   772 {
   741 {
   773 	global $db, $pun_config, $lang_common, $pun_user;
   742 	global $pun_db, $pun_config, $lang_common, $pun_user;
   774 
   743 
   775 	// Deal with newlines, tabs and multiple spaces
   744 	// Deal with newlines, tabs and multiple spaces
   776 	$pattern = array("\t", '  ', '  ');
   745 	$pattern = array("\t", '  ', '  ');
   777 	$replace = array('&nbsp; &nbsp; ', '&nbsp; ', ' &nbsp;');
   746 	$replace = array('&nbsp; &nbsp; ', '&nbsp; ', ' &nbsp;');
   778 	$message = str_replace($pattern, $replace, $pun_config['o_maintenance_message']);
   747 	$message = str_replace($pattern, $replace, $pun_config['o_maintenance_message']);
   830 	$tpl_maint = str_replace('<pun_maint_message>', $message, $tpl_maint);
   799 	$tpl_maint = str_replace('<pun_maint_message>', $message, $tpl_maint);
   831 	// END SUBST - <pun_maint_message>
   800 	// END SUBST - <pun_maint_message>
   832 
   801 
   833 
   802 
   834 	// End the transaction
   803 	// End the transaction
   835 	$db->end_transaction();
   804 	$pun_db->end_transaction();
   836 
   805 
   837 
   806 
   838 	// Close the db connection (and free up any result data)
   807 	// Close the db connection (and free up any result data)
   839 	$db->close();
   808 	$pun_db->close();
   840 
   809 
   841 	exit($tpl_maint);
   810 	exit($tpl_maint);
   842 }
   811 }
   843 
   812 
   844 
   813 
   845 //
   814 //
   846 // Display $message and redirect user to $destination_url
   815 // Display $message and redirect user to $destination_url
   847 //
   816 //
   848 function redirect($destination_url, $message)
   817 function pun_redirect($destination_url, $message)
   849 {
   818 {
   850 	global $db, $pun_config, $lang_common, $pun_user;
   819 	global $pun_db, $pun_config, $lang_common, $pun_user;
   851 
   820 
   852 	if ($destination_url == '')
   821 	if ($destination_url == '')
   853 		$destination_url = 'index.php';
   822 		$destination_url = 'index.php';
   854 
   823 
   855 	// If the delay is 0 seconds, we might as well skip the redirect all together
   824 	// If the delay is 0 seconds, we might as well skip the redirect all together
   890 	ob_start();
   859 	ob_start();
   891 
   860 
   892 ?>
   861 ?>
   893 <meta http-equiv="refresh" content="<?php echo $pun_config['o_redirect_delay'] ?>;URL=<?php echo str_replace(array('<', '>', '"'), array('&lt;', '&gt;', '&quot;'), $destination_url) ?>" />
   862 <meta http-equiv="refresh" content="<?php echo $pun_config['o_redirect_delay'] ?>;URL=<?php echo str_replace(array('<', '>', '"'), array('&lt;', '&gt;', '&quot;'), $destination_url) ?>" />
   894 <title><?php echo pun_htmlspecialchars($pun_config['o_board_title']).' / '.$lang_common['Redirecting'] ?></title>
   863 <title><?php echo pun_htmlspecialchars($pun_config['o_board_title']).' / '.$lang_common['Redirecting'] ?></title>
   895 <link rel="stylesheet" type="text/css" href="style/<?php echo $pun_user['style'].'.css' ?>" />
   864 <link rel="stylesheet" type="text/css" href="<?php echo scriptPath; ?>/punbb/style/<?php echo $pun_user['style'].'.css' ?>" />
   896 <?php
   865 <?php
   897 
   866 
   898 	$tpl_temp = trim(ob_get_contents());
   867 	$tpl_temp = trim(ob_get_contents());
   899 	$tpl_redir = str_replace('<pun_head>', $tpl_temp, $tpl_redir);
   868 	$tpl_redir = str_replace('<pun_head>', $tpl_temp, $tpl_redir);
   900 	ob_end_clean();
   869 	ob_end_clean();
   914 
   883 
   915 	// START SUBST - <pun_footer>
   884 	// START SUBST - <pun_footer>
   916 	ob_start();
   885 	ob_start();
   917 
   886 
   918 	// End the transaction
   887 	// End the transaction
   919 	$db->end_transaction();
   888 	$pun_db->end_transaction();
   920 
   889 
   921 	// Display executed queries (if enabled)
   890 	// Display executed queries (if enabled)
   922 	if (defined('PUN_SHOW_QUERIES'))
   891 	if (defined('PUN_SHOW_QUERIES'))
   923 		display_saved_queries();
   892 		display_saved_queries();
   924 
   893 
   927 	ob_end_clean();
   896 	ob_end_clean();
   928 	// END SUBST - <pun_footer>
   897 	// END SUBST - <pun_footer>
   929 
   898 
   930 
   899 
   931 	// Close the db connection (and free up any result data)
   900 	// Close the db connection (and free up any result data)
   932 	$db->close();
   901 	$pun_db->close();
   933 
   902 
   934 	exit($tpl_redir);
   903 	exit($tpl_redir);
   935 }
   904 }
   936 
   905 
   937 
   906 
  1010 //
   979 //
  1011 // Display executed queries (if enabled)
   980 // Display executed queries (if enabled)
  1012 //
   981 //
  1013 function display_saved_queries()
   982 function display_saved_queries()
  1014 {
   983 {
  1015 	global $db, $lang_common;
   984 	global $pun_db, $lang_common;
  1016 
   985 
  1017 	// Get the queries so that we can print them out
   986 	// Get the queries so that we can print them out
  1018 	$saved_queries = $db->get_saved_queries();
   987 	$saved_queries = $pun_db->get_saved_queries();
  1019 
   988 
  1020 ?>
   989 ?>
  1021 
   990 
  1022 <div id="debug" class="blocktable">
   991 <div id="debug" class="blocktable">
  1023 	<h2><span><?php echo $lang_common['Debug table'] ?></span></h2>
   992 	<h2><span><?php echo $lang_common['Debug table'] ?></span></h2>