diff options
Diffstat (limited to 'phpBB/includes/functions.php')
| -rw-r--r-- | phpBB/includes/functions.php | 707 | 
1 files changed, 317 insertions, 390 deletions
| diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index b8ea80ad4a..63d0f3387a 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -17,184 +17,126 @@ if (!defined('IN_PHPBB'))  }  // Common global functions -  /** -* set_var -* -* Set variable, used by {@link request_var the request_var function} +* Casts a variable to the given type.  * -* @access private +* @deprecated  */  function set_var(&$result, $var, $type, $multibyte = false)  { -	settype($var, $type); -	$result = $var; - -	if ($type == 'string') -	{ -		$result = trim(htmlspecialchars(str_replace(array("\r\n", "\r", "\0"), array("\n", "\n", ''), $result), ENT_COMPAT, 'UTF-8')); - -		if (!empty($result)) -		{ -			// Make sure multibyte characters are wellformed -			if ($multibyte) -			{ -				if (!preg_match('/^./u', $result)) -				{ -					$result = ''; -				} -			} -			else -			{ -				// no multibyte, allow only ASCII (0-127) -				$result = preg_replace('/[\x80-\xFF]/', '?', $result); -			} -		} - -		$result = (STRIP) ? stripslashes($result) : $result; -	} +	// no need for dependency injection here, if you have the object, call the method yourself! +	$type_cast_helper = new phpbb_request_type_cast_helper(); +	$type_cast_helper->set_var($result, $var, $type, $multibyte);  }  /** -* request_var +* Wrapper function of phpbb_request::variable which exists for backwards compatability. +* See {@link phpbb_request_interface::variable phpbb_request_interface::variable} for +* documentation of this function's use.  * -* Used to get passed variable +* @deprecated +* @param	mixed			$var_name	The form variable's name from which data shall be retrieved. +* 										If the value is an array this may be an array of indizes which will give +* 										direct access to a value at any depth. E.g. if the value of "var" is array(1 => "a") +* 										then specifying array("var", 1) as the name will return "a". +* 										If you pass an instance of {@link phpbb_request_interface phpbb_request_interface} +* 										as this parameter it will overwrite the current request class instance. If you do +* 										not do so, it will create its own instance (but leave superglobals enabled). +* @param	mixed			$default	A default value that is returned if the variable was not set. +* 										This function will always return a value of the same type as the default. +* @param	bool			$multibyte	If $default is a string this paramater has to be true if the variable may contain any UTF-8 characters +*										Default is false, causing all bytes outside the ASCII range (0-127) to be replaced with question marks +* @param	bool			$cookie		This param is mapped to phpbb_request_interface::COOKIE as the last param for +* 										phpbb_request_interface::variable for backwards compatability reasons. +* @param	phpbb_request_interface|null|false	If an instance of phpbb_request_interface is given the instance is stored in +*										a static variable and used for all further calls where this parameters is null. Until +*										the function is called with an instance it automatically creates a new phpbb_request +*										instance on every call. By passing false this per-call instantiation can be restored +*										after having passed in a phpbb_request_interface instance. +* +* @return	mixed	The value of $_REQUEST[$var_name] run through {@link set_var set_var} to ensure that the type is the +* 					the same as that of $default. If the variable is not set $default is returned.  */ -function request_var($var_name, $default, $multibyte = false, $cookie = false) +function request_var($var_name, $default, $multibyte = false, $cookie = false, $request = null)  { -	if (!$cookie && isset($_COOKIE[$var_name])) -	{ -		if (!isset($_GET[$var_name]) && !isset($_POST[$var_name])) -		{ -			return (is_array($default)) ? array() : $default; -		} -		$_REQUEST[$var_name] = isset($_POST[$var_name]) ? $_POST[$var_name] : $_GET[$var_name]; -	} +	// This is all just an ugly hack to add "Dependency Injection" to a function +	// the only real code is the function call which maps this function to a method. +	static $static_request = null; -	$super_global = ($cookie) ? '_COOKIE' : '_REQUEST'; -	if (!isset($GLOBALS[$super_global][$var_name]) || is_array($GLOBALS[$super_global][$var_name]) != is_array($default)) +	if ($request instanceof phpbb_request_interface)  	{ -		return (is_array($default)) ? array() : $default; -	} +		$static_request = $request; -	$var = $GLOBALS[$super_global][$var_name]; -	if (!is_array($default)) -	{ -		$type = gettype($default); -	} -	else -	{ -		list($key_type, $type) = each($default); -		$type = gettype($type); -		$key_type = gettype($key_type); -		if ($type == 'array') +		if (empty($var_name))  		{ -			reset($default); -			$default = current($default); -			list($sub_key_type, $sub_type) = each($default); -			$sub_type = gettype($sub_type); -			$sub_type = ($sub_type == 'array') ? 'NULL' : $sub_type; -			$sub_key_type = gettype($sub_key_type); +			return;  		}  	} - -	if (is_array($var)) +	else if ($request === false)  	{ -		$_var = $var; -		$var = array(); +		$static_request = null; -		foreach ($_var as $k => $v) +		if (empty($var_name))  		{ -			set_var($k, $k, $key_type); -			if ($type == 'array' && is_array($v)) -			{ -				foreach ($v as $_k => $_v) -				{ -					if (is_array($_v)) -					{ -						$_v = null; -					} -					set_var($_k, $_k, $sub_key_type, $multibyte); -					set_var($var[$k][$_k], $_v, $sub_type, $multibyte); -				} -			} -			else -			{ -				if ($type == 'array' || is_array($v)) -				{ -					$v = null; -				} -				set_var($var[$k], $v, $type, $multibyte); -			} +			return;  		}  	} -	else + +	$tmp_request = $static_request; + +	// no request class set, create a temporary one ourselves to keep backwards compatability +	if ($tmp_request === null)  	{ -		set_var($var, $var, $type, $multibyte); +		// false param: enable super globals, so the created request class does not +		// make super globals inaccessible everywhere outside this function. +		$tmp_request = new phpbb_request(new phpbb_request_type_cast_helper(), false);  	} -	return $var; +	return $tmp_request->variable($var_name, $default, $multibyte, ($cookie) ? phpbb_request_interface::COOKIE : phpbb_request_interface::REQUEST);  }  /**  * Set config value. Creates missing config entry. +* +* @deprecated  */ -function set_config($config_name, $config_value, $is_dynamic = false) +function set_config($config_name, $config_value, $is_dynamic = false, phpbb_config $set_config = null)  { -	global $db, $cache, $config; - -	$sql = 'UPDATE ' . CONFIG_TABLE . " -		SET config_value = '" . $db->sql_escape($config_value) . "' -		WHERE config_name = '" . $db->sql_escape($config_name) . "'"; -	$db->sql_query($sql); +	static $config = null; -	if (!$db->sql_affectedrows() && !isset($config[$config_name])) +	if ($set_config !== null)  	{ -		$sql = 'INSERT INTO ' . CONFIG_TABLE . ' ' . $db->sql_build_array('INSERT', array( -			'config_name'	=> $config_name, -			'config_value'	=> $config_value, -			'is_dynamic'	=> ($is_dynamic) ? 1 : 0)); -		$db->sql_query($sql); -	} +		$config = $set_config; -	$config[$config_name] = $config_value; - -	if (!$is_dynamic) -	{ -		$cache->destroy('config'); +		if (empty($config_name)) +		{ +			return; +		}  	} + +	$config->set($config_name, $config_value, !$is_dynamic);  }  /**  * Set dynamic config value with arithmetic operation. +* +* @deprecated  */ -function set_config_count($config_name, $increment, $is_dynamic = false) +function set_config_count($config_name, $increment, $is_dynamic = false, phpbb_config $set_config = null)  { -	global $db, $cache; +	static $config = null; -	switch ($db->sql_layer) +	if ($set_config !== null)  	{ -		case 'firebird': -			$sql_update = 'CAST(CAST(config_value as DECIMAL(255, 0)) + ' . (int) $increment . ' as VARCHAR(255))'; -		break; +		$config = $set_config; -		case 'postgres': -			// Need to cast to text first for PostgreSQL 7.x -			$sql_update = 'CAST(CAST(config_value::text as DECIMAL(255, 0)) + ' . (int) $increment . ' as VARCHAR(255))'; -		break; - -		// MySQL, SQlite, mssql, mssql_odbc, oracle -		default: -			$sql_update = 'config_value + ' . (int) $increment; -		break; +		if (empty($config_name)) +		{ +			return; +		}  	} -	$db->sql_query('UPDATE ' . CONFIG_TABLE . ' SET config_value = ' . $sql_update . " WHERE config_name = '" . $db->sql_escape($config_name) . "'"); - -	if (!$is_dynamic) -	{ -		$cache->destroy('config'); -	} +	$config->increment($config_name, $increment, !$is_dynamic);  }  /** @@ -458,7 +400,7 @@ function _hash_gensalt_private($input, &$itoa64, $iteration_count_log2 = 6)  	}  	$output = '$H$'; -	$output .= $itoa64[min($iteration_count_log2 + ((PHP_VERSION >= 5) ? 5 : 3), 30)]; +	$output .= $itoa64[min($iteration_count_log2 + 5, 30)];  	$output .= _hash_encode64($input, 6, $itoa64);  	return $output; @@ -544,24 +486,12 @@ function _hash_crypt_private($password, $setting, &$itoa64)  	* consequently in lower iteration counts and hashes that are  	* quicker to crack (by non-PHP code).  	*/ -	if (PHP_VERSION >= 5) +	$hash = md5($salt . $password, true); +	do  	{ -		$hash = md5($salt . $password, true); -		do -		{ -			$hash = md5($hash . $password, true); -		} -		while (--$count); -	} -	else -	{ -		$hash = pack('H*', md5($salt . $password)); -		do -		{ -			$hash = pack('H*', md5($hash . $password)); -		} -		while (--$count); +		$hash = md5($hash . $password, true);  	} +	while (--$count);  	$output = substr($setting, 0, 12);  	$output .= _hash_encode64($hash, 16, $itoa64); @@ -816,95 +746,6 @@ function phpbb_is_writable($file)  	}  } -// Compatibility functions - -if (!function_exists('array_combine')) -{ -	/** -	* A wrapper for the PHP5 function array_combine() -	* @param array $keys contains keys for the resulting array -	* @param array $values contains values for the resulting array -	* -	* @return Returns an array by using the values from the keys array as keys and the -	* 	values from the values array as the corresponding values. Returns false if the -	* 	number of elements for each array isn't equal or if the arrays are empty. -	*/ -	function array_combine($keys, $values) -	{ -		$keys = array_values($keys); -		$values = array_values($values); - -		$n = sizeof($keys); -		$m = sizeof($values); -		if (!$n || !$m || ($n != $m)) -		{ -			return false; -		} - -		$combined = array(); -		for ($i = 0; $i < $n; $i++) -		{ -			$combined[$keys[$i]] = $values[$i]; -		} -		return $combined; -	} -} - -if (!function_exists('str_split')) -{ -	/** -	* A wrapper for the PHP5 function str_split() -	* @param array $string contains the string to be converted -	* @param array $split_length contains the length of each chunk -	* -	* @return  Converts a string to an array. If the optional split_length parameter is specified, -	*  	the returned array will be broken down into chunks with each being split_length in length, -	*  	otherwise each chunk will be one character in length. FALSE is returned if split_length is -	*  	less than 1. If the split_length length exceeds the length of string, the entire string is -	*  	returned as the first (and only) array element. -	*/ -	function str_split($string, $split_length = 1) -	{ -		if ($split_length < 1) -		{ -			return false; -		} -		else if ($split_length >= strlen($string)) -		{ -			return array($string); -		} -		else -		{ -			preg_match_all('#.{1,' . $split_length . '}#s', $string, $matches); -			return $matches[0]; -		} -	} -} - -if (!function_exists('stripos')) -{ -	/** -	* A wrapper for the PHP5 function stripos -	* Find position of first occurrence of a case-insensitive string -	* -	* @param string $haystack is the string to search in -	* @param string $needle is the string to search for -	* -	* @return mixed Returns the numeric position of the first occurrence of needle in the haystack string. Unlike strpos(), stripos() is case-insensitive. -	* Note that the needle may be a string of one or more characters. -	* If needle is not found, stripos() will return boolean FALSE. -	*/ -	function stripos($haystack, $needle) -	{ -		if (preg_match('#' . preg_quote($needle, '#') . '#i', $haystack, $m)) -		{ -			return strpos($haystack, $m[0]); -		} - -		return false; -	} -} -  /**  * Checks if a path ($path) is absolute or relative  * @@ -1110,18 +951,6 @@ else  	}  } -if (!function_exists('htmlspecialchars_decode')) -{ -	/** -	* A wrapper for htmlspecialchars_decode -	* @ignore -	*/ -	function htmlspecialchars_decode($string, $quote_style = ENT_COMPAT) -	{ -		return strtr($string, array_flip(get_html_translation_table(HTML_SPECIALCHARS, $quote_style))); -	} -} -  // functions used for building option fields  /** @@ -1212,6 +1041,7 @@ function tz_select($default = '', $truncate = false)  function markread($mode, $forum_id = false, $topic_id = false, $post_time = 0, $user_id = 0)  {  	global $db, $user, $config; +	global $request;  	if ($mode == 'all')  	{ @@ -1226,7 +1056,7 @@ function markread($mode, $forum_id = false, $topic_id = false, $post_time = 0, $  			}  			else if ($config['load_anon_lastread'] || $user->data['is_registered'])  			{ -				$tracking_topics = (isset($_COOKIE[$config['cookie_name'] . '_track'])) ? ((STRIP) ? stripslashes($_COOKIE[$config['cookie_name'] . '_track']) : $_COOKIE[$config['cookie_name'] . '_track']) : ''; +				$tracking_topics = $request->variable($config['cookie_name'] . '_track', '', true, phpbb_request_interface::COOKIE);  				$tracking_topics = ($tracking_topics) ? tracking_unserialize($tracking_topics) : array();  				unset($tracking_topics['tf']); @@ -1235,7 +1065,7 @@ function markread($mode, $forum_id = false, $topic_id = false, $post_time = 0, $  				$tracking_topics['l'] = base_convert(time() - $config['board_startdate'], 10, 36);  				$user->set_cookie('track', tracking_serialize($tracking_topics), time() + 31536000); -				$_COOKIE[$config['cookie_name'] . '_track'] = (STRIP) ? addslashes(tracking_serialize($tracking_topics)) : tracking_serialize($tracking_topics); +				$request->overwrite($config['cookie_name'] . '_track', tracking_serialize($tracking_topics), phpbb_request_interface::COOKIE);  				unset($tracking_topics); @@ -1305,7 +1135,7 @@ function markread($mode, $forum_id = false, $topic_id = false, $post_time = 0, $  		}  		else if ($config['load_anon_lastread'] || $user->data['is_registered'])  		{ -			$tracking = (isset($_COOKIE[$config['cookie_name'] . '_track'])) ? ((STRIP) ? stripslashes($_COOKIE[$config['cookie_name'] . '_track']) : $_COOKIE[$config['cookie_name'] . '_track']) : ''; +			$tracking = $request->variable($config['cookie_name'] . '_track', '', true, phpbb_request_interface::COOKIE);  			$tracking = ($tracking) ? tracking_unserialize($tracking) : array();  			foreach ($forum_id as $f_id) @@ -1336,7 +1166,7 @@ function markread($mode, $forum_id = false, $topic_id = false, $post_time = 0, $  			}  			$user->set_cookie('track', tracking_serialize($tracking), time() + 31536000); -			$_COOKIE[$config['cookie_name'] . '_track'] = (STRIP) ? addslashes(tracking_serialize($tracking)) : tracking_serialize($tracking); +			$request->overwrite($config['cookie_name'] . '_track', tracking_serialize($tracking), phpbb_request_interface::COOKIE);  			unset($tracking);  		} @@ -1377,7 +1207,7 @@ function markread($mode, $forum_id = false, $topic_id = false, $post_time = 0, $  		}  		else if ($config['load_anon_lastread'] || $user->data['is_registered'])  		{ -			$tracking = (isset($_COOKIE[$config['cookie_name'] . '_track'])) ? ((STRIP) ? stripslashes($_COOKIE[$config['cookie_name'] . '_track']) : $_COOKIE[$config['cookie_name'] . '_track']) : ''; +			$tracking = $request->variable($config['cookie_name'] . '_track', '', true, phpbb_request_interface::COOKIE);  			$tracking = ($tracking) ? tracking_unserialize($tracking) : array();  			$topic_id36 = base_convert($topic_id, 10, 36); @@ -1392,7 +1222,7 @@ function markread($mode, $forum_id = false, $topic_id = false, $post_time = 0, $  			// If the cookie grows larger than 10000 characters we will remove the smallest value  			// This can result in old topics being unread - but most of the time it should be accurate... -			if (isset($_COOKIE[$config['cookie_name'] . '_track']) && strlen($_COOKIE[$config['cookie_name'] . '_track']) > 10000) +			if (strlen($request->variable($config['cookie_name'] . '_track', '', true, phpbb_request_interface::COOKIE)) > 10000)  			{  				//echo 'Cookie grown too large' . print_r($tracking, true); @@ -1432,7 +1262,7 @@ function markread($mode, $forum_id = false, $topic_id = false, $post_time = 0, $  			}  			$user->set_cookie('track', tracking_serialize($tracking), time() + 31536000); -			$_COOKIE[$config['cookie_name'] . '_track'] = (STRIP) ? addslashes(tracking_serialize($tracking)) : tracking_serialize($tracking); +			$request->overwrite($config['cookie_name'] . '_track', tracking_serialize($tracking), phpbb_request_interface::COOKIE);  		}  		return; @@ -1493,35 +1323,6 @@ function get_topic_tracking($forum_id, $topic_ids, &$rowset, $forum_mark_time, $  	{  		$mark_time = array(); -		// Get global announcement info -		if ($global_announce_list && sizeof($global_announce_list)) -		{ -			if (!isset($forum_mark_time[0])) -			{ -				global $db; - -				$sql = 'SELECT mark_time -					FROM ' . FORUMS_TRACK_TABLE . " -					WHERE user_id = {$user->data['user_id']} -						AND forum_id = 0"; -				$result = $db->sql_query($sql); -				$row = $db->sql_fetchrow($result); -				$db->sql_freeresult($result); - -				if ($row) -				{ -					$mark_time[0] = $row['mark_time']; -				} -			} -			else -			{ -				if ($forum_mark_time[0] !== false) -				{ -					$mark_time[0] = $forum_mark_time[0]; -				} -			} -		} -  		if (!empty($forum_mark_time[$forum_id]) && $forum_mark_time[$forum_id] !== false)  		{  			$mark_time[$forum_id] = $forum_mark_time[$forum_id]; @@ -1531,14 +1332,7 @@ function get_topic_tracking($forum_id, $topic_ids, &$rowset, $forum_mark_time, $  		foreach ($topic_ids as $topic_id)  		{ -			if ($global_announce_list && isset($global_announce_list[$topic_id])) -			{ -				$last_read[$topic_id] = (isset($mark_time[0])) ? $mark_time[0] : $user_lastmark; -			} -			else -			{ -				$last_read[$topic_id] = $user_lastmark; -			} +			$last_read[$topic_id] = $user_lastmark;  		}  	} @@ -1550,7 +1344,7 @@ function get_topic_tracking($forum_id, $topic_ids, &$rowset, $forum_mark_time, $  */  function get_complete_topic_tracking($forum_id, $topic_ids, $global_announce_list = false)  { -	global $config, $user; +	global $config, $user, $request;  	$last_read = array(); @@ -1582,8 +1376,7 @@ function get_complete_topic_tracking($forum_id, $topic_ids, $global_announce_lis  			$sql = 'SELECT forum_id, mark_time  				FROM ' . FORUMS_TRACK_TABLE . "  				WHERE user_id = {$user->data['user_id']} -					AND forum_id " . -					(($global_announce_list && sizeof($global_announce_list)) ? "IN (0, $forum_id)" : "= $forum_id"); +					AND forum_id = $forum_id";  			$result = $db->sql_query($sql);  			$mark_time = array(); @@ -1597,14 +1390,7 @@ function get_complete_topic_tracking($forum_id, $topic_ids, $global_announce_lis  			foreach ($topic_ids as $topic_id)  			{ -				if ($global_announce_list && isset($global_announce_list[$topic_id])) -				{ -					$last_read[$topic_id] = (isset($mark_time[0])) ? $mark_time[0] : $user_lastmark; -				} -				else -				{ -					$last_read[$topic_id] = $user_lastmark; -				} +				$last_read[$topic_id] = $user_lastmark;  			}  		}  	} @@ -1614,7 +1400,7 @@ function get_complete_topic_tracking($forum_id, $topic_ids, $global_announce_lis  		if (!isset($tracking_topics) || !sizeof($tracking_topics))  		{ -			$tracking_topics = (isset($_COOKIE[$config['cookie_name'] . '_track'])) ? ((STRIP) ? stripslashes($_COOKIE[$config['cookie_name'] . '_track']) : $_COOKIE[$config['cookie_name'] . '_track']) : ''; +			$tracking_topics = $request->variable($config['cookie_name'] . '_track', '', true, phpbb_request_interface::COOKIE);  			$tracking_topics = ($tracking_topics) ? tracking_unserialize($tracking_topics) : array();  		} @@ -1642,13 +1428,6 @@ function get_complete_topic_tracking($forum_id, $topic_ids, $global_announce_lis  		if (sizeof($topic_ids))  		{  			$mark_time = array(); -			if ($global_announce_list && sizeof($global_announce_list)) -			{ -				if (isset($tracking_topics['f'][0])) -				{ -					$mark_time[0] = base_convert($tracking_topics['f'][0], 36, 10) + $config['board_startdate']; -				} -			}  			if (isset($tracking_topics['f'][$forum_id]))  			{ @@ -1659,14 +1438,7 @@ function get_complete_topic_tracking($forum_id, $topic_ids, $global_announce_lis  			foreach ($topic_ids as $topic_id)  			{ -				if ($global_announce_list && isset($global_announce_list[$topic_id])) -				{ -					$last_read[$topic_id] = (isset($mark_time[0])) ? $mark_time[0] : $user_lastmark; -				} -				else -				{ -					$last_read[$topic_id] = $user_lastmark; -				} +				$last_read[$topic_id] = $user_lastmark;  			}  		}  	} @@ -1814,7 +1586,7 @@ function get_unread_topics($user_id = false, $sql_extra = '', $sql_sort = '', $s  */  function update_forum_tracking_info($forum_id, $forum_last_post_time, $f_mark_time = false, $mark_time_forum = false)  { -	global $db, $tracking_topics, $user, $config; +	global $db, $tracking_topics, $user, $config, $request;  	// Determine the users last forum mark time if not given.  	if ($mark_time_forum === false) @@ -1825,7 +1597,7 @@ function update_forum_tracking_info($forum_id, $forum_last_post_time, $f_mark_ti  		}  		else if ($config['load_anon_lastread'] || $user->data['is_registered'])  		{ -			$tracking_topics = (isset($_COOKIE[$config['cookie_name'] . '_track'])) ? ((STRIP) ? stripslashes($_COOKIE[$config['cookie_name'] . '_track']) : $_COOKIE[$config['cookie_name'] . '_track']) : ''; +			$tracking_topics = $request->variable($config['cookie_name'] . '_track', '', true, phpbb_request_interface::COOKIE);  			$tracking_topics = ($tracking_topics) ? tracking_unserialize($tracking_topics) : array();  			if (!$user->data['is_registered']) @@ -2043,7 +1815,7 @@ function generate_pagination($base_url, $num_items, $per_page, $start_item, $add  	// Make sure $per_page is a valid value  	$per_page = ($per_page <= 0) ? 1 : $per_page; -	$seperator = '<span class="page-sep">' . $user->lang['COMMA_SEPARATOR'] . '</span>'; +	$separator = '<span class="page-sep">' . $user->lang['COMMA_SEPARATOR'] . '</span>';  	$total_pages = ceil($num_items / $per_page);  	if ($total_pages == 1 || !$num_items) @@ -2061,29 +1833,29 @@ function generate_pagination($base_url, $num_items, $per_page, $start_item, $add  		$start_cnt = min(max(1, $on_page - 4), $total_pages - 5);  		$end_cnt = max(min($total_pages, $on_page + 4), 6); -		$page_string .= ($start_cnt > 1) ? ' ... ' : $seperator; +		$page_string .= ($start_cnt > 1) ? ' ... ' : $separator;  		for ($i = $start_cnt + 1; $i < $end_cnt; $i++)  		{  			$page_string .= ($i == $on_page) ? '<strong>' . $i . '</strong>' : '<a href="' . $base_url . "{$url_delim}start=" . (($i - 1) * $per_page) . '">' . $i . '</a>';  			if ($i < $end_cnt - 1)  			{ -				$page_string .= $seperator; +				$page_string .= $separator;  			}  		} -		$page_string .= ($end_cnt < $total_pages) ? ' ... ' : $seperator; +		$page_string .= ($end_cnt < $total_pages) ? ' ... ' : $separator;  	}  	else  	{ -		$page_string .= $seperator; +		$page_string .= $separator;  		for ($i = 2; $i < $total_pages; $i++)  		{  			$page_string .= ($i == $on_page) ? '<strong>' . $i . '</strong>' : '<a href="' . $base_url . "{$url_delim}start=" . (($i - 1) * $per_page) . '">' . $i . '</a>';  			if ($i < $total_pages)  			{ -				$page_string .= $seperator; +				$page_string .= $separator;  			}  		}  	} @@ -2772,22 +2544,14 @@ function check_form_key($form_name, $timespan = false, $return_page = '', $trigg  function confirm_box($check, $title = '', $hidden = '', $html_body = 'confirm_body.html', $u_action = '')  {  	global $user, $template, $db; -	global $phpEx, $phpbb_root_path; +	global $phpEx, $phpbb_root_path, $request;  	if (isset($_POST['cancel']))  	{  		return false;  	} -	$confirm = false; -	if (isset($_POST['confirm'])) -	{ -		// language frontier -		if ($_POST['confirm'] === $user->lang['YES']) -		{ -			$confirm = true; -		} -	} +	$confirm = ($user->lang['YES'] === $request->variable('confirm', '', true, phpbb_request_interface::POST));  	if ($check && $confirm)  	{ @@ -2875,8 +2639,9 @@ function confirm_box($check, $title = '', $hidden = '', $html_body = 'confirm_bo  function login_box($redirect = '', $l_explain = '', $l_success = '', $admin = false, $s_display = true)  {  	global $db, $user, $template, $auth, $phpEx, $phpbb_root_path, $config; +	global $request; -	if (!class_exists('phpbb_captcha_factory')) +	if (!class_exists('phpbb_captcha_factory', false))  	{  		include($phpbb_root_path . 'includes/captcha/captcha_factory.' . $phpEx);  	} @@ -2925,8 +2690,8 @@ function login_box($redirect = '', $l_explain = '', $l_success = '', $admin = fa  		}  		$username	= request_var('username', '', true); -		$autologin	= (!empty($_POST['autologin'])) ? true : false; -		$viewonline = (!empty($_POST['viewonline'])) ? 0 : 1; +		$autologin	= $request->is_set_post('autologin'); +		$viewonline = (int) !$request->is_set_post('viewonline');  		$admin 		= ($admin) ? 1 : 0;  		$viewonline = ($admin) ? $user->data['session_viewonline'] : $viewonline; @@ -3447,24 +3212,16 @@ function get_preg_expression($mode)  * Depends on whether installed PHP version supports unicode properties  *  * @param string	$word			word template to be replaced -* @param bool	$use_unicode	whether or not to take advantage of PCRE supporting unicode  +* @param bool	$use_unicode	whether or not to take advantage of PCRE supporting unicode  *  * @return string $preg_expr		regex to use with word censor  */  function get_censor_preg_expression($word, $use_unicode = true)  { -	static $unicode_support = null; - -	// Check whether PHP version supports unicode properties -	if (is_null($unicode_support)) -	{ -		$unicode_support = ((version_compare(PHP_VERSION, '5.1.0', '>=') || (version_compare(PHP_VERSION, '5.0.0-dev', '<=') && version_compare(PHP_VERSION, '4.4.0', '>='))) && @preg_match('/\p{L}/u', 'a') !== false) ? true : false; -	} -  	// Unescape the asterisk to simplify further conversions  	$word = str_replace('\*', '*', preg_quote($word, '#')); -	if ($use_unicode && $unicode_support) +	if ($use_unicode && pcre_utf8_support())  	{  		// Replace asterisk(s) inside the pattern, at the start and at the end of it with regexes  		$word = preg_replace(array('#(?<=[\p{Nd}\p{L}_])\*+(?=[\p{Nd}\p{L}_])#iu', '#^\*+#', '#\*+$#'), array('([\x20]*?|[\p{Nd}\p{L}_-]*?)', '[\p{Nd}\p{L}_-]*?', '[\p{Nd}\p{L}_-]*?'), $word); @@ -3516,6 +3273,188 @@ function short_ipv6($ip, $length)  }  /** +* Normalises an internet protocol address, +* also checks whether the specified address is valid. +* +* IPv4 addresses are returned 'as is'. +* +* IPv6 addresses are normalised according to +*	A Recommendation for IPv6 Address Text Representation +*	http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-07 +* +* @param string $address	IP address +* +* @return mixed		false if specified address is not valid, +*					string otherwise +* +* @author bantu +*/ +function phpbb_ip_normalise($address) +{ +	$address = trim($address); + +	if (empty($address) || !is_string($address)) +	{ +		return false; +	} + +	if (preg_match(get_preg_expression('ipv4'), $address)) +	{ +		return $address; +	} + +	return phpbb_inet_ntop(phpbb_inet_pton($address)); +} + +/** +* Wrapper for inet_ntop() +* +* Converts a packed internet address to a human readable representation +* inet_ntop() is supported by PHP since 5.1.0, since 5.3.0 also on Windows. +* +* @param string $in_addr	A 32bit IPv4, or 128bit IPv6 address. +* +* @return mixed		false on failure, +*					string otherwise +* +* @author APTX +*/ +function phpbb_inet_ntop($in_addr) +{ +	$in_addr = bin2hex($in_addr); + +	switch (strlen($in_addr)) +	{ +		case 8: +			return implode('.', array_map('hexdec', str_split($in_addr, 2))); + +		case 32: +			if (substr($in_addr, 0, 24) === '00000000000000000000ffff') +			{ +				return phpbb_inet_ntop(pack('H*', substr($in_addr, 24))); +			} + +			$parts = str_split($in_addr, 4); +			$parts = preg_replace('/^0+(?!$)/', '', $parts); +			$ret = implode(':', $parts); + +			$matches = array(); +			preg_match_all('/(?<=:|^)(?::?0){2,}/', $ret, $matches, PREG_OFFSET_CAPTURE); +			$matches = $matches[0]; + +			if (empty($matches)) +			{ +				return $ret; +			} + +			$longest_match = ''; +			$longest_match_offset = 0; +			foreach ($matches as $match) +			{ +				if (strlen($match[0]) > strlen($longest_match)) +				{ +					$longest_match = $match[0]; +					$longest_match_offset = $match[1]; +				} +			} + +			$ret = substr_replace($ret, '', $longest_match_offset, strlen($longest_match)); + +			if ($longest_match_offset == strlen($ret)) +			{ +				$ret .= ':'; +			} + +			if ($longest_match_offset == 0) +			{ +				$ret = ':' . $ret; +			} + +			return $ret; + +		default: +			return false; +	} +} + +/** +* Wrapper for inet_pton() +* +* Converts a human readable IP address to its packed in_addr representation +* inet_pton() is supported by PHP since 5.1.0, since 5.3.0 also on Windows. +* +* @param string $address	A human readable IPv4 or IPv6 address. +* +* @return mixed		false if address is invalid, +*					in_addr representation of the given address otherwise (string) +* +* @author APTX +*/ +function phpbb_inet_pton($address) +{ +	$ret = ''; +	if (preg_match(get_preg_expression('ipv4'), $address)) +	{ +		foreach (explode('.', $address) as $part) +		{ +			$ret .= ($part <= 0xF ? '0' : '') . dechex($part); +		} + +		return pack('H*', $ret); +	} + +	if (preg_match(get_preg_expression('ipv6'), $address)) +	{ +		$parts = explode(':', $address); +		$missing_parts = 8 - sizeof($parts) + 1; + +		if (substr($address, 0, 2) === '::') +		{ +			++$missing_parts; +		} + +		if (substr($address, -2) === '::') +		{ +			++$missing_parts; +		} + +		$embedded_ipv4 = false; +		$last_part = end($parts); + +		if (preg_match(get_preg_expression('ipv4'), $last_part)) +		{ +			$parts[sizeof($parts) - 1] = ''; +			$last_part = phpbb_inet_pton($last_part); +			$embedded_ipv4 = true; +			--$missing_parts; +		} + +		foreach ($parts as $i => $part) +		{ +			if (strlen($part)) +			{ +				$ret .= str_pad($part, 4, '0', STR_PAD_LEFT); +			} +			else if ($i && $i < sizeof($parts) - 1) +			{ +				$ret .= str_repeat('0000', $missing_parts); +			} +		} + +		$ret = pack('H*', $ret); + +		if ($embedded_ipv4) +		{ +			$ret .= $last_part; +		} + +		return $ret; +	} + +	return false; +} + +/**  * Wrapper for php's checkdnsrr function.  *  * @param string $host	Fully-Qualified Domain Name @@ -3557,7 +3496,7 @@ function phpbb_checkdnsrr($host, $type = 'MX')  	// but until 5.3.3 it only works for MX records  	// See: http://bugs.php.net/bug.php?id=51844 -	// Call checkdnsrr() if  +	// Call checkdnsrr() if  	// we're looking for an MX record or  	// we're not on Windows or  	// we're running a PHP version where #51844 has been fixed @@ -3577,7 +3516,7 @@ function phpbb_checkdnsrr($host, $type = 'MX')  	// dns_get_record() is available since PHP 5; since PHP 5.3 also on Windows,  	// but on Windows it does not work reliable for AAAA records before PHP 5.3.1 -	// Call dns_get_record() if  +	// Call dns_get_record() if  	// we're not looking for an AAAA record or  	// we're not on Windows or  	// we're running a PHP version where AAAA lookups work reliable @@ -3607,7 +3546,7 @@ function phpbb_checkdnsrr($host, $type = 'MX')  		foreach ($resultset as $result)  		{  			if ( -				isset($result['host']) && $result['host'] == $host &&  +				isset($result['host']) && $result['host'] == $host &&  				isset($result['type']) && $result['type'] == $type  			)  			{ @@ -4573,6 +4512,7 @@ function page_header($page_title = '', $display_online_list = true, $item_id = 0  function page_footer($run_cron = true)  {  	global $db, $config, $template, $user, $auth, $cache, $starttime, $phpbb_root_path, $phpEx; +	global $request;  	// Output page creation time  	if (defined('DEBUG')) @@ -4580,7 +4520,7 @@ function page_footer($run_cron = true)  		$mtime = explode(' ', microtime());  		$totaltime = $mtime[0] + $mtime[1] - $starttime; -		if (!empty($_REQUEST['explain']) && $auth->acl_get('a_') && defined('DEBUG_EXTRA') && method_exists($db, 'sql_report')) +		if ($request->variable('explain', false) && $auth->acl_get('a_') && defined('DEBUG_EXTRA') && method_exists($db, 'sql_report'))  		{  			$db->sql_report('display');  		} @@ -4614,7 +4554,7 @@ function page_footer($run_cron = true)  	// Call cron-type script  	$call_cron = false; -	if (!defined('IN_CRON') && $run_cron && !$config['board_disable'] && !$user->data['is_bot']) +	if (!defined('IN_CRON') && !$config['use_system_cron'] && $run_cron && !$config['board_disable'] && !$user->data['is_bot'])  	{  		$call_cron = true;  		$time_now = (!empty($user->time_now) && is_int($user->time_now)) ? $user->time_now : time(); @@ -4635,40 +4575,13 @@ function page_footer($run_cron = true)  	// Call cron job?  	if ($call_cron)  	{ -		$cron_type = ''; +		global $cron; +		$task = $cron->find_one_ready_task(); -		if ($time_now - $config['queue_interval'] > $config['last_queue_run'] && !defined('IN_ADMIN') && file_exists($phpbb_root_path . 'cache/queue.' . $phpEx)) -		{ -			// Process email queue -			$cron_type = 'queue'; -		} -		else if (method_exists($cache, 'tidy') && $time_now - $config['cache_gc'] > $config['cache_last_gc']) +		if ($task)  		{ -			// Tidy the cache -			$cron_type = 'tidy_cache'; -		} -		else if ($config['warnings_expire_days'] && ($time_now - $config['warnings_gc'] > $config['warnings_last_gc'])) -		{ -			$cron_type = 'tidy_warnings'; -		} -		else if ($time_now - $config['database_gc'] > $config['database_last_gc']) -		{ -			// Tidy the database -			$cron_type = 'tidy_database'; -		} -		else if ($time_now - $config['search_gc'] > $config['search_last_gc']) -		{ -			// Tidy the search -			$cron_type = 'tidy_search'; -		} -		else if ($time_now - $config['session_gc'] > $config['session_last_gc']) -		{ -			$cron_type = 'tidy_sessions'; -		} - -		if ($cron_type) -		{ -			$template->assign_var('RUN_CRON_TASK', '<img src="' . append_sid($phpbb_root_path . 'cron.' . $phpEx, 'cron_type=' . $cron_type) . '" width="1" height="1" alt="cron" />'); +			$url = $task->get_url(); +			$template->assign_var('RUN_CRON_TASK', '<img src="' . $url . '" width="1" height="1" alt="cron" />');  		}  	} @@ -4742,4 +4655,18 @@ function phpbb_user_session_handler()  	return;  } -?>
\ No newline at end of file +/** +* Check if PCRE has UTF-8 support +* PHP may not be linked with the bundled PCRE lib and instead with an older version +* +* @return bool	Returns true if PCRE (the regular expressions library) supports UTF-8 encoding +*/ +function pcre_utf8_support() +{ +	static $utf8_pcre_properties = null; +	if (is_null($utf8_pcre_properties)) +	{ +		$utf8_pcre_properties = (@preg_match('/\p{L}/u', 'a') !== false); +	} +	return $utf8_pcre_properties; +} | 
