indvdum@0: $v) indvdum@0: { indvdum@0: set_var($k, $k, $key_type); indvdum@0: if ($type == 'array' && is_array($v)) indvdum@0: { indvdum@0: foreach ($v as $_k => $_v) indvdum@0: { indvdum@0: if (is_array($_v)) indvdum@0: { indvdum@0: $_v = null; indvdum@0: } indvdum@0: set_var($_k, $_k, $sub_key_type, $multibyte); indvdum@0: set_var($var[$k][$_k], $_v, $sub_type, $multibyte); indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: if ($type == 'array' || is_array($v)) indvdum@0: { indvdum@0: $v = null; indvdum@0: } indvdum@0: set_var($var[$k], $v, $type, $multibyte); indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: set_var($var, $var, $type, $multibyte); indvdum@0: } indvdum@0: indvdum@0: return $var; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Set config value. Creates missing config entry. indvdum@0: */ indvdum@0: function set_config($config_name, $config_value, $is_dynamic = false) indvdum@0: { indvdum@0: global $db, $cache, $config; indvdum@0: indvdum@0: $sql = 'UPDATE ' . CONFIG_TABLE . " indvdum@0: SET config_value = '" . $db->sql_escape($config_value) . "' indvdum@0: WHERE config_name = '" . $db->sql_escape($config_name) . "'"; indvdum@0: $db->sql_query($sql); indvdum@0: indvdum@0: if (!$db->sql_affectedrows() && !isset($config[$config_name])) indvdum@0: { indvdum@0: $sql = 'INSERT INTO ' . CONFIG_TABLE . ' ' . $db->sql_build_array('INSERT', array( indvdum@0: 'config_name' => $config_name, indvdum@0: 'config_value' => $config_value, indvdum@0: 'is_dynamic' => ($is_dynamic) ? 1 : 0)); indvdum@0: $db->sql_query($sql); indvdum@0: } indvdum@0: indvdum@0: $config[$config_name] = $config_value; indvdum@0: indvdum@0: if (!$is_dynamic) indvdum@0: { indvdum@0: $cache->destroy('config'); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Set dynamic config value with arithmetic operation. indvdum@0: */ indvdum@0: function set_config_count($config_name, $increment, $is_dynamic = false) indvdum@0: { indvdum@0: global $db, $cache; indvdum@0: indvdum@0: switch ($db->sql_layer) indvdum@0: { indvdum@0: case 'firebird': indvdum@0: // Precision must be from 1 to 18 indvdum@0: $sql_update = 'CAST(CAST(config_value as DECIMAL(18, 0)) + ' . (int) $increment . ' as VARCHAR(255))'; indvdum@0: break; indvdum@0: indvdum@0: case 'postgres': indvdum@0: // Need to cast to text first for PostgreSQL 7.x indvdum@0: $sql_update = 'CAST(CAST(config_value::text as DECIMAL(255, 0)) + ' . (int) $increment . ' as VARCHAR(255))'; indvdum@0: break; indvdum@0: indvdum@0: // MySQL, SQlite, mssql, mssql_odbc, oracle indvdum@0: default: indvdum@0: $sql_update = 'config_value + ' . (int) $increment; indvdum@0: break; indvdum@0: } indvdum@0: indvdum@0: $db->sql_query('UPDATE ' . CONFIG_TABLE . ' SET config_value = ' . $sql_update . " WHERE config_name = '" . $db->sql_escape($config_name) . "'"); indvdum@0: indvdum@0: if (!$is_dynamic) indvdum@0: { indvdum@0: $cache->destroy('config'); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Generates an alphanumeric random string of given length indvdum@0: * indvdum@0: * @return string indvdum@0: */ indvdum@0: function gen_rand_string($num_chars = 8) indvdum@0: { indvdum@0: // [a, z] + [0, 9] = 36 indvdum@0: return substr(strtoupper(base_convert(unique_id(), 16, 36)), 0, $num_chars); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Generates a user-friendly alphanumeric random string of given length indvdum@0: * We remove 0 and O so users cannot confuse those in passwords etc. indvdum@0: * indvdum@0: * @return string indvdum@0: */ indvdum@0: function gen_rand_string_friendly($num_chars = 8) indvdum@0: { indvdum@0: $rand_str = unique_id(); indvdum@0: indvdum@0: // Remove Z and Y from the base_convert(), replace 0 with Z and O with Y indvdum@0: // [a, z] + [0, 9] - {z, y} = [a, z] + [0, 9] - {0, o} = 34 indvdum@0: $rand_str = str_replace(array('0', 'O'), array('Z', 'Y'), strtoupper(base_convert($rand_str, 16, 34))); indvdum@0: indvdum@0: return substr($rand_str, 0, $num_chars); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Return unique id indvdum@0: * @param string $extra additional entropy indvdum@0: */ indvdum@0: function unique_id($extra = 'c') indvdum@0: { indvdum@0: static $dss_seeded = false; indvdum@0: global $config; indvdum@0: indvdum@0: $val = $config['rand_seed'] . microtime(); indvdum@0: $val = md5($val); indvdum@0: $config['rand_seed'] = md5($config['rand_seed'] . $val . $extra); indvdum@0: indvdum@0: if ($dss_seeded !== true && ($config['rand_seed_last_update'] < time() - rand(1,10))) indvdum@0: { indvdum@0: set_config('rand_seed_last_update', time(), true); indvdum@0: set_config('rand_seed', $config['rand_seed'], true); indvdum@0: $dss_seeded = true; indvdum@0: } indvdum@0: indvdum@0: return substr($val, 4, 16); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Wrapper for mt_rand() which allows swapping $min and $max parameters. indvdum@0: * indvdum@0: * PHP does not allow us to swap the order of the arguments for mt_rand() anymore. indvdum@0: * (since PHP 5.3.4, see http://bugs.php.net/46587) indvdum@0: * indvdum@0: * @param int $min Lowest value to be returned indvdum@0: * @param int $max Highest value to be returned indvdum@0: * indvdum@0: * @return int Random integer between $min and $max (or $max and $min) indvdum@0: */ indvdum@0: function phpbb_mt_rand($min, $max) indvdum@0: { indvdum@0: return ($min > $max) ? mt_rand($max, $min) : mt_rand($min, $max); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Wrapper for getdate() which returns the equivalent array for UTC timestamps. indvdum@0: * indvdum@0: * @param int $time Unix timestamp (optional) indvdum@0: * indvdum@0: * @return array Returns an associative array of information related to the timestamp. indvdum@0: * See http://www.php.net/manual/en/function.getdate.php indvdum@0: */ indvdum@0: function phpbb_gmgetdate($time = false) indvdum@0: { indvdum@0: if ($time === false) indvdum@0: { indvdum@0: $time = time(); indvdum@0: } indvdum@0: indvdum@0: // getdate() interprets timestamps in local time. indvdum@0: // What follows uses the fact that getdate() and indvdum@0: // date('Z') balance each other out. indvdum@0: return getdate($time - date('Z')); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Return formatted string for filesizes indvdum@0: * indvdum@0: * @param int $value filesize in bytes indvdum@0: * @param bool $string_only true if language string should be returned indvdum@0: * @param array $allowed_units only allow these units (data array indexes) indvdum@0: * indvdum@0: * @return mixed data array if $string_only is false indvdum@0: * @author bantu indvdum@0: */ indvdum@0: function get_formatted_filesize($value, $string_only = true, $allowed_units = false) indvdum@0: { indvdum@0: global $user; indvdum@0: indvdum@0: $available_units = array( indvdum@0: 'gb' => array( indvdum@0: 'min' => 1073741824, // pow(2, 30) indvdum@0: 'index' => 3, indvdum@0: 'si_unit' => 'GB', indvdum@0: 'iec_unit' => 'GIB', indvdum@0: ), indvdum@0: 'mb' => array( indvdum@0: 'min' => 1048576, // pow(2, 20) indvdum@0: 'index' => 2, indvdum@0: 'si_unit' => 'MB', indvdum@0: 'iec_unit' => 'MIB', indvdum@0: ), indvdum@0: 'kb' => array( indvdum@0: 'min' => 1024, // pow(2, 10) indvdum@0: 'index' => 1, indvdum@0: 'si_unit' => 'KB', indvdum@0: 'iec_unit' => 'KIB', indvdum@0: ), indvdum@0: 'b' => array( indvdum@0: 'min' => 0, indvdum@0: 'index' => 0, indvdum@0: 'si_unit' => 'BYTES', // Language index indvdum@0: 'iec_unit' => 'BYTES', // Language index indvdum@0: ), indvdum@0: ); indvdum@0: indvdum@0: foreach ($available_units as $si_identifier => $unit_info) indvdum@0: { indvdum@0: if (!empty($allowed_units) && $si_identifier != 'b' && !in_array($si_identifier, $allowed_units)) indvdum@0: { indvdum@0: continue; indvdum@0: } indvdum@0: indvdum@0: if ($value >= $unit_info['min']) indvdum@0: { indvdum@0: $unit_info['si_identifier'] = $si_identifier; indvdum@0: indvdum@0: break; indvdum@0: } indvdum@0: } indvdum@0: unset($available_units); indvdum@0: indvdum@0: for ($i = 0; $i < $unit_info['index']; $i++) indvdum@0: { indvdum@0: $value /= 1024; indvdum@0: } indvdum@0: $value = round($value, 2); indvdum@0: indvdum@0: // Lookup units in language dictionary indvdum@0: $unit_info['si_unit'] = (isset($user->lang[$unit_info['si_unit']])) ? $user->lang[$unit_info['si_unit']] : $unit_info['si_unit']; indvdum@0: $unit_info['iec_unit'] = (isset($user->lang[$unit_info['iec_unit']])) ? $user->lang[$unit_info['iec_unit']] : $unit_info['iec_unit']; indvdum@0: indvdum@0: // Default to IEC indvdum@0: $unit_info['unit'] = $unit_info['iec_unit']; indvdum@0: indvdum@0: if (!$string_only) indvdum@0: { indvdum@0: $unit_info['value'] = $value; indvdum@0: indvdum@0: return $unit_info; indvdum@0: } indvdum@0: indvdum@0: return $value . ' ' . $unit_info['unit']; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Determine whether we are approaching the maximum execution time. Should be called once indvdum@0: * at the beginning of the script in which it's used. indvdum@0: * @return bool Either true if the maximum execution time is nearly reached, or false indvdum@0: * if some time is still left. indvdum@0: */ indvdum@0: function still_on_time($extra_time = 15) indvdum@0: { indvdum@0: static $max_execution_time, $start_time; indvdum@0: indvdum@0: $time = explode(' ', microtime()); indvdum@0: $current_time = $time[0] + $time[1]; indvdum@0: indvdum@0: if (empty($max_execution_time)) indvdum@0: { indvdum@0: $max_execution_time = (function_exists('ini_get')) ? (int) @ini_get('max_execution_time') : (int) @get_cfg_var('max_execution_time'); indvdum@0: indvdum@0: // If zero, then set to something higher to not let the user catch the ten seconds barrier. indvdum@0: if ($max_execution_time === 0) indvdum@0: { indvdum@0: $max_execution_time = 50 + $extra_time; indvdum@0: } indvdum@0: indvdum@0: $max_execution_time = min(max(10, ($max_execution_time - $extra_time)), 50); indvdum@0: indvdum@0: // For debugging purposes indvdum@0: // $max_execution_time = 10; indvdum@0: indvdum@0: global $starttime; indvdum@0: $start_time = (empty($starttime)) ? $current_time : $starttime; indvdum@0: } indvdum@0: indvdum@0: return (ceil($current_time - $start_time) < $max_execution_time) ? true : false; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * indvdum@0: * @version Version 0.1 / slightly modified for phpBB 3.0.x (using $H$ as hash type identifier) indvdum@0: * indvdum@0: * Portable PHP password hashing framework. indvdum@0: * indvdum@0: * Written by Solar Designer in 2004-2006 and placed in indvdum@0: * the public domain. indvdum@0: * indvdum@0: * There's absolutely no warranty. indvdum@0: * indvdum@0: * The homepage URL for this framework is: indvdum@0: * indvdum@0: * http://www.openwall.com/phpass/ indvdum@0: * indvdum@0: * Please be sure to update the Version line if you edit this file in any way. indvdum@0: * It is suggested that you leave the main version number intact, but indicate indvdum@0: * your project name (after the slash) and add your own revision information. indvdum@0: * indvdum@0: * Please do not change the "private" password hashing method implemented in indvdum@0: * here, thereby making your hashes incompatible. However, if you must, please indvdum@0: * change the hash type identifier (the "$P$") to something different. indvdum@0: * indvdum@0: * Obviously, since this code is in the public domain, the above are not indvdum@0: * requirements (there can be none), but merely suggestions. indvdum@0: * indvdum@0: * indvdum@0: * Hash the password indvdum@0: */ indvdum@0: function phpbb_hash($password) indvdum@0: { indvdum@5: /* indvdum@0: $itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; indvdum@0: indvdum@0: $random_state = unique_id(); indvdum@0: $random = ''; indvdum@0: $count = 6; indvdum@0: indvdum@0: if (($fh = @fopen('/dev/urandom', 'rb'))) indvdum@0: { indvdum@0: $random = fread($fh, $count); indvdum@0: fclose($fh); indvdum@0: } indvdum@0: indvdum@0: if (strlen($random) < $count) indvdum@0: { indvdum@0: $random = ''; indvdum@0: indvdum@0: for ($i = 0; $i < $count; $i += 16) indvdum@0: { indvdum@0: $random_state = md5(unique_id() . $random_state); indvdum@0: $random .= pack('H*', md5($random_state)); indvdum@0: } indvdum@0: $random = substr($random, 0, $count); indvdum@0: } indvdum@0: indvdum@0: $hash = _hash_crypt_private($password, _hash_gensalt_private($random, $itoa64), $itoa64); indvdum@0: indvdum@0: if (strlen($hash) == 34) indvdum@0: { indvdum@0: return $hash; indvdum@0: } indvdum@5: */ indvdum@0: indvdum@0: return md5($password); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Check for correct password indvdum@0: * indvdum@0: * @param string $password The password in plain text indvdum@0: * @param string $hash The stored password hash indvdum@0: * indvdum@0: * @return bool Returns true if the password is correct, false if not. indvdum@0: */ indvdum@0: function phpbb_check_hash($password, $hash) indvdum@0: { indvdum@5: /* indvdum@0: $itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; indvdum@0: if (strlen($hash) == 34) indvdum@0: { indvdum@0: return (_hash_crypt_private($password, $hash, $itoa64) === $hash) ? true : false; indvdum@0: } indvdum@5: */ indvdum@0: indvdum@0: return (md5($password) === $hash) ? true : false; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Generate salt for hash generation indvdum@0: */ indvdum@0: function _hash_gensalt_private($input, &$itoa64, $iteration_count_log2 = 6) indvdum@0: { indvdum@0: if ($iteration_count_log2 < 4 || $iteration_count_log2 > 31) indvdum@0: { indvdum@0: $iteration_count_log2 = 8; indvdum@0: } indvdum@0: indvdum@0: $output = '$H$'; indvdum@0: $output .= $itoa64[min($iteration_count_log2 + ((PHP_VERSION >= 5) ? 5 : 3), 30)]; indvdum@0: $output .= _hash_encode64($input, 6, $itoa64); indvdum@0: indvdum@0: return $output; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Encode hash indvdum@0: */ indvdum@0: function _hash_encode64($input, $count, &$itoa64) indvdum@0: { indvdum@0: $output = ''; indvdum@0: $i = 0; indvdum@0: indvdum@0: do indvdum@0: { indvdum@0: $value = ord($input[$i++]); indvdum@0: $output .= $itoa64[$value & 0x3f]; indvdum@0: indvdum@0: if ($i < $count) indvdum@0: { indvdum@0: $value |= ord($input[$i]) << 8; indvdum@0: } indvdum@0: indvdum@0: $output .= $itoa64[($value >> 6) & 0x3f]; indvdum@0: indvdum@0: if ($i++ >= $count) indvdum@0: { indvdum@0: break; indvdum@0: } indvdum@0: indvdum@0: if ($i < $count) indvdum@0: { indvdum@0: $value |= ord($input[$i]) << 16; indvdum@0: } indvdum@0: indvdum@0: $output .= $itoa64[($value >> 12) & 0x3f]; indvdum@0: indvdum@0: if ($i++ >= $count) indvdum@0: { indvdum@0: break; indvdum@0: } indvdum@0: indvdum@0: $output .= $itoa64[($value >> 18) & 0x3f]; indvdum@0: } indvdum@0: while ($i < $count); indvdum@0: indvdum@0: return $output; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * The crypt function/replacement indvdum@0: */ indvdum@0: function _hash_crypt_private($password, $setting, &$itoa64) indvdum@0: { indvdum@0: $output = '*'; indvdum@0: indvdum@0: // Check for correct hash indvdum@0: if (substr($setting, 0, 3) != '$H$' && substr($setting, 0, 3) != '$P$') indvdum@0: { indvdum@0: return $output; indvdum@0: } indvdum@0: indvdum@0: $count_log2 = strpos($itoa64, $setting[3]); indvdum@0: indvdum@0: if ($count_log2 < 7 || $count_log2 > 30) indvdum@0: { indvdum@0: return $output; indvdum@0: } indvdum@0: indvdum@0: $count = 1 << $count_log2; indvdum@0: $salt = substr($setting, 4, 8); indvdum@0: indvdum@0: if (strlen($salt) != 8) indvdum@0: { indvdum@0: return $output; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * We're kind of forced to use MD5 here since it's the only indvdum@0: * cryptographic primitive available in all versions of PHP indvdum@0: * currently in use. To implement our own low-level crypto indvdum@0: * in PHP would result in much worse performance and indvdum@0: * consequently in lower iteration counts and hashes that are indvdum@0: * quicker to crack (by non-PHP code). indvdum@0: */ indvdum@0: if (PHP_VERSION >= 5) indvdum@0: { indvdum@0: $hash = md5($salt . $password, true); indvdum@0: do indvdum@0: { indvdum@0: $hash = md5($hash . $password, true); indvdum@0: } indvdum@0: while (--$count); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $hash = pack('H*', md5($salt . $password)); indvdum@0: do indvdum@0: { indvdum@0: $hash = pack('H*', md5($hash . $password)); indvdum@0: } indvdum@0: while (--$count); indvdum@0: } indvdum@0: indvdum@0: $output = substr($setting, 0, 12); indvdum@0: $output .= _hash_encode64($hash, 16, $itoa64); indvdum@0: indvdum@0: return $output; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Hashes an email address to a big integer indvdum@0: * indvdum@0: * @param string $email Email address indvdum@0: * indvdum@0: * @return string Unsigned Big Integer indvdum@0: */ indvdum@0: function phpbb_email_hash($email) indvdum@0: { indvdum@0: return sprintf('%u', crc32(strtolower($email))) . strlen($email); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Wrapper for version_compare() that allows using uppercase A and B indvdum@0: * for alpha and beta releases. indvdum@0: * indvdum@0: * See http://www.php.net/manual/en/function.version-compare.php indvdum@0: * indvdum@0: * @param string $version1 First version number indvdum@0: * @param string $version2 Second version number indvdum@0: * @param string $operator Comparison operator (optional) indvdum@0: * indvdum@0: * @return mixed Boolean (true, false) if comparison operator is specified. indvdum@0: * Integer (-1, 0, 1) otherwise. indvdum@0: */ indvdum@0: function phpbb_version_compare($version1, $version2, $operator = null) indvdum@0: { indvdum@0: $version1 = strtolower($version1); indvdum@0: $version2 = strtolower($version2); indvdum@0: indvdum@0: if (is_null($operator)) indvdum@0: { indvdum@0: return version_compare($version1, $version2); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: return version_compare($version1, $version2, $operator); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Global function for chmodding directories and files for internal use indvdum@0: * indvdum@0: * This function determines owner and group whom the file belongs to and user and group of PHP and then set safest possible file permissions. indvdum@0: * The function determines owner and group from common.php file and sets the same to the provided file. indvdum@0: * The function uses bit fields to build the permissions. indvdum@0: * The function sets the appropiate execute bit on directories. indvdum@0: * indvdum@0: * Supported constants representing bit fields are: indvdum@0: * indvdum@0: * CHMOD_ALL - all permissions (7) indvdum@0: * CHMOD_READ - read permission (4) indvdum@0: * CHMOD_WRITE - write permission (2) indvdum@0: * CHMOD_EXECUTE - execute permission (1) indvdum@0: * indvdum@0: * NOTE: The function uses POSIX extension and fileowner()/filegroup() functions. If any of them is disabled, this function tries to build proper permissions, by calling is_readable() and is_writable() functions. indvdum@0: * indvdum@0: * @param string $filename The file/directory to be chmodded indvdum@0: * @param int $perms Permissions to set indvdum@0: * indvdum@0: * @return bool true on success, otherwise false indvdum@0: * @author faw, phpBB Group indvdum@0: */ indvdum@0: function phpbb_chmod($filename, $perms = CHMOD_READ) indvdum@0: { indvdum@0: static $_chmod_info; indvdum@0: indvdum@0: // Return if the file no longer exists. indvdum@0: if (!file_exists($filename)) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: // Determine some common vars indvdum@0: if (empty($_chmod_info)) indvdum@0: { indvdum@0: if (!function_exists('fileowner') || !function_exists('filegroup')) indvdum@0: { indvdum@0: // No need to further determine owner/group - it is unknown indvdum@0: $_chmod_info['process'] = false; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: global $phpbb_root_path, $phpEx; indvdum@0: indvdum@0: // Determine owner/group of common.php file and the filename we want to change here indvdum@0: $common_php_owner = @fileowner($phpbb_root_path . 'common.' . $phpEx); indvdum@0: $common_php_group = @filegroup($phpbb_root_path . 'common.' . $phpEx); indvdum@0: indvdum@0: // And the owner and the groups PHP is running under. indvdum@0: $php_uid = (function_exists('posix_getuid')) ? @posix_getuid() : false; indvdum@0: $php_gids = (function_exists('posix_getgroups')) ? @posix_getgroups() : false; indvdum@0: indvdum@0: // If we are unable to get owner/group, then do not try to set them by guessing indvdum@0: if (!$php_uid || empty($php_gids) || !$common_php_owner || !$common_php_group) indvdum@0: { indvdum@0: $_chmod_info['process'] = false; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $_chmod_info = array( indvdum@0: 'process' => true, indvdum@0: 'common_owner' => $common_php_owner, indvdum@0: 'common_group' => $common_php_group, indvdum@0: 'php_uid' => $php_uid, indvdum@0: 'php_gids' => $php_gids, indvdum@0: ); indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: if ($_chmod_info['process']) indvdum@0: { indvdum@0: $file_uid = @fileowner($filename); indvdum@0: $file_gid = @filegroup($filename); indvdum@0: indvdum@0: // Change owner indvdum@0: if (@chown($filename, $_chmod_info['common_owner'])) indvdum@0: { indvdum@0: clearstatcache(); indvdum@0: $file_uid = @fileowner($filename); indvdum@0: } indvdum@0: indvdum@0: // Change group indvdum@0: if (@chgrp($filename, $_chmod_info['common_group'])) indvdum@0: { indvdum@0: clearstatcache(); indvdum@0: $file_gid = @filegroup($filename); indvdum@0: } indvdum@0: indvdum@0: // If the file_uid/gid now match the one from common.php we can process further, else we are not able to change something indvdum@0: if ($file_uid != $_chmod_info['common_owner'] || $file_gid != $_chmod_info['common_group']) indvdum@0: { indvdum@0: $_chmod_info['process'] = false; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // Still able to process? indvdum@0: if ($_chmod_info['process']) indvdum@0: { indvdum@0: if ($file_uid == $_chmod_info['php_uid']) indvdum@0: { indvdum@0: $php = 'owner'; indvdum@0: } indvdum@0: else if (in_array($file_gid, $_chmod_info['php_gids'])) indvdum@0: { indvdum@0: $php = 'group'; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // Since we are setting the everyone bit anyway, no need to do expensive operations indvdum@0: $_chmod_info['process'] = false; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // We are not able to determine or change something indvdum@0: if (!$_chmod_info['process']) indvdum@0: { indvdum@0: $php = 'other'; indvdum@0: } indvdum@0: indvdum@0: // Owner always has read/write permission indvdum@0: $owner = CHMOD_READ | CHMOD_WRITE; indvdum@0: if (is_dir($filename)) indvdum@0: { indvdum@0: $owner |= CHMOD_EXECUTE; indvdum@0: indvdum@0: // Only add execute bit to the permission if the dir needs to be readable indvdum@0: if ($perms & CHMOD_READ) indvdum@0: { indvdum@0: $perms |= CHMOD_EXECUTE; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: switch ($php) indvdum@0: { indvdum@0: case 'owner': indvdum@0: $result = @chmod($filename, ($owner << 6) + (0 << 3) + (0 << 0)); indvdum@0: indvdum@0: clearstatcache(); indvdum@0: indvdum@0: if (is_readable($filename) && phpbb_is_writable($filename)) indvdum@0: { indvdum@0: break; indvdum@0: } indvdum@0: indvdum@0: case 'group': indvdum@0: $result = @chmod($filename, ($owner << 6) + ($perms << 3) + (0 << 0)); indvdum@0: indvdum@0: clearstatcache(); indvdum@0: indvdum@0: if ((!($perms & CHMOD_READ) || is_readable($filename)) && (!($perms & CHMOD_WRITE) || phpbb_is_writable($filename))) indvdum@0: { indvdum@0: break; indvdum@0: } indvdum@0: indvdum@0: case 'other': indvdum@0: $result = @chmod($filename, ($owner << 6) + ($perms << 3) + ($perms << 0)); indvdum@0: indvdum@0: clearstatcache(); indvdum@0: indvdum@0: if ((!($perms & CHMOD_READ) || is_readable($filename)) && (!($perms & CHMOD_WRITE) || phpbb_is_writable($filename))) indvdum@0: { indvdum@0: break; indvdum@0: } indvdum@0: indvdum@0: default: indvdum@0: return false; indvdum@0: break; indvdum@0: } indvdum@0: indvdum@0: return $result; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Test if a file/directory is writable indvdum@0: * indvdum@0: * This function calls the native is_writable() when not running under indvdum@0: * Windows and it is not disabled. indvdum@0: * indvdum@0: * @param string $file Path to perform write test on indvdum@0: * @return bool True when the path is writable, otherwise false. indvdum@0: */ indvdum@0: function phpbb_is_writable($file) indvdum@0: { indvdum@0: if (strtolower(substr(PHP_OS, 0, 3)) === 'win' || !function_exists('is_writable')) indvdum@0: { indvdum@0: if (file_exists($file)) indvdum@0: { indvdum@0: // Canonicalise path to absolute path indvdum@0: $file = phpbb_realpath($file); indvdum@0: indvdum@0: if (is_dir($file)) indvdum@0: { indvdum@0: // Test directory by creating a file inside the directory indvdum@0: $result = @tempnam($file, 'i_w'); indvdum@0: indvdum@0: if (is_string($result) && file_exists($result)) indvdum@0: { indvdum@0: unlink($result); indvdum@0: indvdum@0: // Ensure the file is actually in the directory (returned realpathed) indvdum@0: return (strpos($result, $file) === 0) ? true : false; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $handle = @fopen($file, 'r+'); indvdum@0: indvdum@0: if (is_resource($handle)) indvdum@0: { indvdum@0: fclose($handle); indvdum@0: return true; indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // file does not exist test if we can write to the directory indvdum@0: $dir = dirname($file); indvdum@0: indvdum@0: if (file_exists($dir) && is_dir($dir) && phpbb_is_writable($dir)) indvdum@0: { indvdum@0: return true; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: return false; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: return is_writable($file); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // Compatibility functions indvdum@0: indvdum@0: if (!function_exists('array_combine')) indvdum@0: { indvdum@0: /** indvdum@0: * A wrapper for the PHP5 function array_combine() indvdum@0: * @param array $keys contains keys for the resulting array indvdum@0: * @param array $values contains values for the resulting array indvdum@0: * indvdum@0: * @return Returns an array by using the values from the keys array as keys and the indvdum@0: * values from the values array as the corresponding values. Returns false if the indvdum@0: * number of elements for each array isn't equal or if the arrays are empty. indvdum@0: */ indvdum@0: function array_combine($keys, $values) indvdum@0: { indvdum@0: $keys = array_values($keys); indvdum@0: $values = array_values($values); indvdum@0: indvdum@0: $n = sizeof($keys); indvdum@0: $m = sizeof($values); indvdum@0: if (!$n || !$m || ($n != $m)) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: $combined = array(); indvdum@0: for ($i = 0; $i < $n; $i++) indvdum@0: { indvdum@0: $combined[$keys[$i]] = $values[$i]; indvdum@0: } indvdum@0: return $combined; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: if (!function_exists('str_split')) indvdum@0: { indvdum@0: /** indvdum@0: * A wrapper for the PHP5 function str_split() indvdum@0: * @param array $string contains the string to be converted indvdum@0: * @param array $split_length contains the length of each chunk indvdum@0: * indvdum@0: * @return Converts a string to an array. If the optional split_length parameter is specified, indvdum@0: * the returned array will be broken down into chunks with each being split_length in length, indvdum@0: * otherwise each chunk will be one character in length. FALSE is returned if split_length is indvdum@0: * less than 1. If the split_length length exceeds the length of string, the entire string is indvdum@0: * returned as the first (and only) array element. indvdum@0: */ indvdum@0: function str_split($string, $split_length = 1) indvdum@0: { indvdum@0: if ($split_length < 1) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: else if ($split_length >= strlen($string)) indvdum@0: { indvdum@0: return array($string); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: preg_match_all('#.{1,' . $split_length . '}#s', $string, $matches); indvdum@0: return $matches[0]; indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: if (!function_exists('stripos')) indvdum@0: { indvdum@0: /** indvdum@0: * A wrapper for the PHP5 function stripos indvdum@0: * Find position of first occurrence of a case-insensitive string indvdum@0: * indvdum@0: * @param string $haystack is the string to search in indvdum@0: * @param string $needle is the string to search for indvdum@0: * indvdum@0: * @return mixed Returns the numeric position of the first occurrence of needle in the haystack string. Unlike strpos(), stripos() is case-insensitive. indvdum@0: * Note that the needle may be a string of one or more characters. indvdum@0: * If needle is not found, stripos() will return boolean FALSE. indvdum@0: */ indvdum@0: function stripos($haystack, $needle) indvdum@0: { indvdum@0: if (preg_match('#' . preg_quote($needle, '#') . '#i', $haystack, $m)) indvdum@0: { indvdum@0: return strpos($haystack, $m[0]); indvdum@0: } indvdum@0: indvdum@0: return false; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Checks if a path ($path) is absolute or relative indvdum@0: * indvdum@0: * @param string $path Path to check absoluteness of indvdum@0: * @return boolean indvdum@0: */ indvdum@0: function is_absolute($path) indvdum@0: { indvdum@0: return ($path[0] == '/' || (DIRECTORY_SEPARATOR == '\\' && preg_match('#^[a-z]:[/\\\]#i', $path))) ? true : false; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * @author Chris Smith indvdum@0: * @copyright 2006 Project Minerva Team indvdum@0: * @param string $path The path which we should attempt to resolve. indvdum@0: * @return mixed indvdum@0: */ indvdum@0: function phpbb_own_realpath($path) indvdum@0: { indvdum@0: // Now to perform funky shizzle indvdum@0: indvdum@0: // Switch to use UNIX slashes indvdum@0: $path = str_replace(DIRECTORY_SEPARATOR, '/', $path); indvdum@0: $path_prefix = ''; indvdum@0: indvdum@0: // Determine what sort of path we have indvdum@0: if (is_absolute($path)) indvdum@0: { indvdum@0: $absolute = true; indvdum@0: indvdum@0: if ($path[0] == '/') indvdum@0: { indvdum@0: // Absolute path, *NIX style indvdum@0: $path_prefix = ''; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // Absolute path, Windows style indvdum@0: // Remove the drive letter and colon indvdum@0: $path_prefix = $path[0] . ':'; indvdum@0: $path = substr($path, 2); indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // Relative Path indvdum@0: // Prepend the current working directory indvdum@0: if (function_exists('getcwd')) indvdum@0: { indvdum@0: // This is the best method, hopefully it is enabled! indvdum@0: $path = str_replace(DIRECTORY_SEPARATOR, '/', getcwd()) . '/' . $path; indvdum@0: $absolute = true; indvdum@0: if (preg_match('#^[a-z]:#i', $path)) indvdum@0: { indvdum@0: $path_prefix = $path[0] . ':'; indvdum@0: $path = substr($path, 2); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $path_prefix = ''; indvdum@0: } indvdum@0: } indvdum@0: else if (isset($_SERVER['SCRIPT_FILENAME']) && !empty($_SERVER['SCRIPT_FILENAME'])) indvdum@0: { indvdum@0: // Warning: If chdir() has been used this will lie! indvdum@0: // Warning: This has some problems sometime (CLI can create them easily) indvdum@0: $path = str_replace(DIRECTORY_SEPARATOR, '/', dirname($_SERVER['SCRIPT_FILENAME'])) . '/' . $path; indvdum@0: $absolute = true; indvdum@0: $path_prefix = ''; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // We have no way of getting the absolute path, just run on using relative ones. indvdum@0: $absolute = false; indvdum@0: $path_prefix = '.'; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // Remove any repeated slashes indvdum@0: $path = preg_replace('#/{2,}#', '/', $path); indvdum@0: indvdum@0: // Remove the slashes from the start and end of the path indvdum@0: $path = trim($path, '/'); indvdum@0: indvdum@0: // Break the string into little bits for us to nibble on indvdum@0: $bits = explode('/', $path); indvdum@0: indvdum@0: // Remove any . in the path, renumber array for the loop below indvdum@0: $bits = array_values(array_diff($bits, array('.'))); indvdum@0: indvdum@0: // Lets get looping, run over and resolve any .. (up directory) indvdum@0: for ($i = 0, $max = sizeof($bits); $i < $max; $i++) indvdum@0: { indvdum@0: // @todo Optimise indvdum@0: if ($bits[$i] == '..' ) indvdum@0: { indvdum@0: if (isset($bits[$i - 1])) indvdum@0: { indvdum@0: if ($bits[$i - 1] != '..') indvdum@0: { indvdum@0: // We found a .. and we are able to traverse upwards, lets do it! indvdum@0: unset($bits[$i]); indvdum@0: unset($bits[$i - 1]); indvdum@0: $i -= 2; indvdum@0: $max -= 2; indvdum@0: $bits = array_values($bits); indvdum@0: } indvdum@0: } indvdum@0: else if ($absolute) // ie. !isset($bits[$i - 1]) && $absolute indvdum@0: { indvdum@0: // We have an absolute path trying to descend above the root of the filesystem indvdum@0: // ... Error! indvdum@0: return false; indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // Prepend the path prefix indvdum@0: array_unshift($bits, $path_prefix); indvdum@0: indvdum@0: $resolved = ''; indvdum@0: indvdum@0: $max = sizeof($bits) - 1; indvdum@0: indvdum@0: // Check if we are able to resolve symlinks, Windows cannot. indvdum@0: $symlink_resolve = (function_exists('readlink')) ? true : false; indvdum@0: indvdum@0: foreach ($bits as $i => $bit) indvdum@0: { indvdum@0: if (@is_dir("$resolved/$bit") || ($i == $max && @is_file("$resolved/$bit"))) indvdum@0: { indvdum@0: // Path Exists indvdum@0: if ($symlink_resolve && is_link("$resolved/$bit") && ($link = readlink("$resolved/$bit"))) indvdum@0: { indvdum@0: // Resolved a symlink. indvdum@0: $resolved = $link . (($i == $max) ? '' : '/'); indvdum@0: continue; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // Something doesn't exist here! indvdum@0: // This is correct realpath() behaviour but sadly open_basedir and safe_mode make this problematic indvdum@0: // return false; indvdum@0: } indvdum@0: $resolved .= $bit . (($i == $max) ? '' : '/'); indvdum@0: } indvdum@0: indvdum@0: // @todo If the file exists fine and open_basedir only has one path we should be able to prepend it indvdum@0: // because we must be inside that basedir, the question is where... indvdum@0: // @internal The slash in is_dir() gets around an open_basedir restriction indvdum@0: if (!@file_exists($resolved) || (!@is_dir($resolved . '/') && !is_file($resolved))) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: // Put the slashes back to the native operating systems slashes indvdum@0: $resolved = str_replace('/', DIRECTORY_SEPARATOR, $resolved); indvdum@0: indvdum@0: // Check for DIRECTORY_SEPARATOR at the end (and remove it!) indvdum@0: if (substr($resolved, -1) == DIRECTORY_SEPARATOR) indvdum@0: { indvdum@0: return substr($resolved, 0, -1); indvdum@0: } indvdum@0: indvdum@0: return $resolved; // We got here, in the end! indvdum@0: } indvdum@0: indvdum@0: if (!function_exists('realpath')) indvdum@0: { indvdum@0: /** indvdum@0: * A wrapper for realpath indvdum@0: * @ignore indvdum@0: */ indvdum@0: function phpbb_realpath($path) indvdum@0: { indvdum@0: return phpbb_own_realpath($path); indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: /** indvdum@0: * A wrapper for realpath indvdum@0: */ indvdum@0: function phpbb_realpath($path) indvdum@0: { indvdum@0: $realpath = realpath($path); indvdum@0: indvdum@0: // Strangely there are provider not disabling realpath but returning strange values. :o indvdum@0: // We at least try to cope with them. indvdum@0: if ($realpath === $path || $realpath === false) indvdum@0: { indvdum@0: return phpbb_own_realpath($path); indvdum@0: } indvdum@0: indvdum@0: // Check for DIRECTORY_SEPARATOR at the end (and remove it!) indvdum@0: if (substr($realpath, -1) == DIRECTORY_SEPARATOR) indvdum@0: { indvdum@0: $realpath = substr($realpath, 0, -1); indvdum@0: } indvdum@0: indvdum@0: return $realpath; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: if (!function_exists('htmlspecialchars_decode')) indvdum@0: { indvdum@0: /** indvdum@0: * A wrapper for htmlspecialchars_decode indvdum@0: * @ignore indvdum@0: */ indvdum@0: function htmlspecialchars_decode($string, $quote_style = ENT_COMPAT) indvdum@0: { indvdum@0: return strtr($string, array_flip(get_html_translation_table(HTML_SPECIALCHARS, $quote_style))); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // functions used for building option fields indvdum@0: indvdum@0: /** indvdum@0: * Pick a language, any language ... indvdum@0: */ indvdum@0: function language_select($default = '') indvdum@0: { indvdum@0: global $db; indvdum@0: indvdum@0: $sql = 'SELECT lang_iso, lang_local_name indvdum@0: FROM ' . LANG_TABLE . ' indvdum@0: ORDER BY lang_english_name'; indvdum@0: $result = $db->sql_query($sql); indvdum@0: indvdum@0: $lang_options = ''; indvdum@0: while ($row = $db->sql_fetchrow($result)) indvdum@0: { indvdum@0: $selected = ($row['lang_iso'] == $default) ? ' selected="selected"' : ''; indvdum@0: $lang_options .= ''; indvdum@0: } indvdum@0: $db->sql_freeresult($result); indvdum@0: indvdum@0: return $lang_options; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Pick a template/theme combo, indvdum@0: */ indvdum@0: function style_select($default = '', $all = false) indvdum@0: { indvdum@0: global $db; indvdum@0: indvdum@0: $sql_where = (!$all) ? 'WHERE style_active = 1 ' : ''; indvdum@0: $sql = 'SELECT style_id, style_name indvdum@0: FROM ' . STYLES_TABLE . " indvdum@0: $sql_where indvdum@0: ORDER BY style_name"; indvdum@0: $result = $db->sql_query($sql); indvdum@0: indvdum@0: $style_options = ''; indvdum@0: while ($row = $db->sql_fetchrow($result)) indvdum@0: { indvdum@0: $selected = ($row['style_id'] == $default) ? ' selected="selected"' : ''; indvdum@0: $style_options .= ''; indvdum@0: } indvdum@0: $db->sql_freeresult($result); indvdum@0: indvdum@0: return $style_options; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Pick a timezone indvdum@0: */ indvdum@0: function tz_select($default = '', $truncate = false) indvdum@0: { indvdum@0: global $user; indvdum@0: indvdum@0: $tz_select = ''; indvdum@0: foreach ($user->lang['tz_zones'] as $offset => $zone) indvdum@0: { indvdum@0: if ($truncate) indvdum@0: { indvdum@0: $zone_trunc = truncate_string($zone, 50, 255, false, '...'); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $zone_trunc = $zone; indvdum@0: } indvdum@0: indvdum@0: if (is_numeric($offset)) indvdum@0: { indvdum@0: $selected = ($offset == $default) ? ' selected="selected"' : ''; indvdum@0: $tz_select .= ''; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: return $tz_select; indvdum@0: } indvdum@0: indvdum@0: // Functions handling topic/post tracking/marking indvdum@0: indvdum@0: /** indvdum@0: * Marks a topic/forum as read indvdum@0: * Marks a topic as posted to indvdum@0: * indvdum@0: * @param int $user_id can only be used with $mode == 'post' indvdum@0: */ indvdum@0: function markread($mode, $forum_id = false, $topic_id = false, $post_time = 0, $user_id = 0) indvdum@0: { indvdum@0: global $db, $user, $config; indvdum@0: indvdum@0: if ($mode == 'all') indvdum@0: { indvdum@0: if ($forum_id === false || !sizeof($forum_id)) indvdum@0: { indvdum@0: if ($config['load_db_lastread'] && $user->data['is_registered']) indvdum@0: { indvdum@0: // Mark all forums read (index page) indvdum@0: $db->sql_query('DELETE FROM ' . TOPICS_TRACK_TABLE . " WHERE user_id = {$user->data['user_id']}"); indvdum@0: $db->sql_query('DELETE FROM ' . FORUMS_TRACK_TABLE . " WHERE user_id = {$user->data['user_id']}"); indvdum@0: $db->sql_query('UPDATE ' . USERS_TABLE . ' SET user_lastmark = ' . time() . " WHERE user_id = {$user->data['user_id']}"); indvdum@0: } indvdum@0: else if ($config['load_anon_lastread'] || $user->data['is_registered']) indvdum@0: { indvdum@0: $tracking_topics = (isset($_COOKIE[$config['cookie_name'] . '_track'])) ? ((STRIP) ? stripslashes($_COOKIE[$config['cookie_name'] . '_track']) : $_COOKIE[$config['cookie_name'] . '_track']) : ''; indvdum@0: $tracking_topics = ($tracking_topics) ? tracking_unserialize($tracking_topics) : array(); indvdum@0: indvdum@0: unset($tracking_topics['tf']); indvdum@0: unset($tracking_topics['t']); indvdum@0: unset($tracking_topics['f']); indvdum@0: $tracking_topics['l'] = base_convert(time() - $config['board_startdate'], 10, 36); indvdum@0: indvdum@0: $user->set_cookie('track', tracking_serialize($tracking_topics), time() + 31536000); indvdum@0: $_COOKIE[$config['cookie_name'] . '_track'] = (STRIP) ? addslashes(tracking_serialize($tracking_topics)) : tracking_serialize($tracking_topics); indvdum@0: indvdum@0: unset($tracking_topics); indvdum@0: indvdum@0: if ($user->data['is_registered']) indvdum@0: { indvdum@0: $db->sql_query('UPDATE ' . USERS_TABLE . ' SET user_lastmark = ' . time() . " WHERE user_id = {$user->data['user_id']}"); indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: return; indvdum@0: } indvdum@0: else if ($mode == 'topics') indvdum@0: { indvdum@0: // Mark all topics in forums read indvdum@0: if (!is_array($forum_id)) indvdum@0: { indvdum@0: $forum_id = array($forum_id); indvdum@0: } indvdum@0: indvdum@0: // Add 0 to forums array to mark global announcements correctly indvdum@0: // $forum_id[] = 0; indvdum@0: indvdum@0: if ($config['load_db_lastread'] && $user->data['is_registered']) indvdum@0: { indvdum@0: $sql = 'DELETE FROM ' . TOPICS_TRACK_TABLE . " indvdum@0: WHERE user_id = {$user->data['user_id']} indvdum@0: AND " . $db->sql_in_set('forum_id', $forum_id); indvdum@0: $db->sql_query($sql); indvdum@0: indvdum@0: $sql = 'SELECT forum_id indvdum@0: FROM ' . FORUMS_TRACK_TABLE . " indvdum@0: WHERE user_id = {$user->data['user_id']} indvdum@0: AND " . $db->sql_in_set('forum_id', $forum_id); indvdum@0: $result = $db->sql_query($sql); indvdum@0: indvdum@0: $sql_update = array(); indvdum@0: while ($row = $db->sql_fetchrow($result)) indvdum@0: { indvdum@0: $sql_update[] = (int) $row['forum_id']; indvdum@0: } indvdum@0: $db->sql_freeresult($result); indvdum@0: indvdum@0: if (sizeof($sql_update)) indvdum@0: { indvdum@0: $sql = 'UPDATE ' . FORUMS_TRACK_TABLE . ' indvdum@0: SET mark_time = ' . time() . " indvdum@0: WHERE user_id = {$user->data['user_id']} indvdum@0: AND " . $db->sql_in_set('forum_id', $sql_update); indvdum@0: $db->sql_query($sql); indvdum@0: } indvdum@0: indvdum@0: if ($sql_insert = array_diff($forum_id, $sql_update)) indvdum@0: { indvdum@0: $sql_ary = array(); indvdum@0: foreach ($sql_insert as $f_id) indvdum@0: { indvdum@0: $sql_ary[] = array( indvdum@0: 'user_id' => (int) $user->data['user_id'], indvdum@0: 'forum_id' => (int) $f_id, indvdum@0: 'mark_time' => time() indvdum@0: ); indvdum@0: } indvdum@0: indvdum@0: $db->sql_multi_insert(FORUMS_TRACK_TABLE, $sql_ary); indvdum@0: } indvdum@0: } indvdum@0: else if ($config['load_anon_lastread'] || $user->data['is_registered']) indvdum@0: { indvdum@0: $tracking = (isset($_COOKIE[$config['cookie_name'] . '_track'])) ? ((STRIP) ? stripslashes($_COOKIE[$config['cookie_name'] . '_track']) : $_COOKIE[$config['cookie_name'] . '_track']) : ''; indvdum@0: $tracking = ($tracking) ? tracking_unserialize($tracking) : array(); indvdum@0: indvdum@0: foreach ($forum_id as $f_id) indvdum@0: { indvdum@0: $topic_ids36 = (isset($tracking['tf'][$f_id])) ? $tracking['tf'][$f_id] : array(); indvdum@0: indvdum@0: if (isset($tracking['tf'][$f_id])) indvdum@0: { indvdum@0: unset($tracking['tf'][$f_id]); indvdum@0: } indvdum@0: indvdum@0: foreach ($topic_ids36 as $topic_id36) indvdum@0: { indvdum@0: unset($tracking['t'][$topic_id36]); indvdum@0: } indvdum@0: indvdum@0: if (isset($tracking['f'][$f_id])) indvdum@0: { indvdum@0: unset($tracking['f'][$f_id]); indvdum@0: } indvdum@0: indvdum@0: $tracking['f'][$f_id] = base_convert(time() - $config['board_startdate'], 10, 36); indvdum@0: } indvdum@0: indvdum@0: if (isset($tracking['tf']) && empty($tracking['tf'])) indvdum@0: { indvdum@0: unset($tracking['tf']); indvdum@0: } indvdum@0: indvdum@0: $user->set_cookie('track', tracking_serialize($tracking), time() + 31536000); indvdum@0: $_COOKIE[$config['cookie_name'] . '_track'] = (STRIP) ? addslashes(tracking_serialize($tracking)) : tracking_serialize($tracking); indvdum@0: indvdum@0: unset($tracking); indvdum@0: } indvdum@0: indvdum@0: return; indvdum@0: } indvdum@0: else if ($mode == 'topic') indvdum@0: { indvdum@0: if ($topic_id === false || $forum_id === false) indvdum@0: { indvdum@0: return; indvdum@0: } indvdum@0: indvdum@0: if ($config['load_db_lastread'] && $user->data['is_registered']) indvdum@0: { indvdum@0: $sql = 'UPDATE ' . TOPICS_TRACK_TABLE . ' indvdum@0: SET mark_time = ' . (($post_time) ? $post_time : time()) . " indvdum@0: WHERE user_id = {$user->data['user_id']} indvdum@0: AND topic_id = $topic_id"; indvdum@0: $db->sql_query($sql); indvdum@0: indvdum@0: // insert row indvdum@0: if (!$db->sql_affectedrows()) indvdum@0: { indvdum@0: $db->sql_return_on_error(true); indvdum@0: indvdum@0: $sql_ary = array( indvdum@0: 'user_id' => (int) $user->data['user_id'], indvdum@0: 'topic_id' => (int) $topic_id, indvdum@0: 'forum_id' => (int) $forum_id, indvdum@0: 'mark_time' => ($post_time) ? (int) $post_time : time(), indvdum@0: ); indvdum@0: indvdum@0: $db->sql_query('INSERT INTO ' . TOPICS_TRACK_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary)); indvdum@0: indvdum@0: $db->sql_return_on_error(false); indvdum@0: } indvdum@0: } indvdum@0: else if ($config['load_anon_lastread'] || $user->data['is_registered']) indvdum@0: { indvdum@0: $tracking = (isset($_COOKIE[$config['cookie_name'] . '_track'])) ? ((STRIP) ? stripslashes($_COOKIE[$config['cookie_name'] . '_track']) : $_COOKIE[$config['cookie_name'] . '_track']) : ''; indvdum@0: $tracking = ($tracking) ? tracking_unserialize($tracking) : array(); indvdum@0: indvdum@0: $topic_id36 = base_convert($topic_id, 10, 36); indvdum@0: indvdum@0: if (!isset($tracking['t'][$topic_id36])) indvdum@0: { indvdum@0: $tracking['tf'][$forum_id][$topic_id36] = true; indvdum@0: } indvdum@0: indvdum@0: $post_time = ($post_time) ? $post_time : time(); indvdum@0: $tracking['t'][$topic_id36] = base_convert($post_time - $config['board_startdate'], 10, 36); indvdum@0: indvdum@0: // If the cookie grows larger than 10000 characters we will remove the smallest value indvdum@0: // This can result in old topics being unread - but most of the time it should be accurate... indvdum@0: if (isset($_COOKIE[$config['cookie_name'] . '_track']) && strlen($_COOKIE[$config['cookie_name'] . '_track']) > 10000) indvdum@0: { indvdum@0: //echo 'Cookie grown too large' . print_r($tracking, true); indvdum@0: indvdum@0: // We get the ten most minimum stored time offsets and its associated topic ids indvdum@0: $time_keys = array(); indvdum@0: for ($i = 0; $i < 10 && sizeof($tracking['t']); $i++) indvdum@0: { indvdum@0: $min_value = min($tracking['t']); indvdum@0: $m_tkey = array_search($min_value, $tracking['t']); indvdum@0: unset($tracking['t'][$m_tkey]); indvdum@0: indvdum@0: $time_keys[$m_tkey] = $min_value; indvdum@0: } indvdum@0: indvdum@0: // Now remove the topic ids from the array... indvdum@0: foreach ($tracking['tf'] as $f_id => $topic_id_ary) indvdum@0: { indvdum@0: foreach ($time_keys as $m_tkey => $min_value) indvdum@0: { indvdum@0: if (isset($topic_id_ary[$m_tkey])) indvdum@0: { indvdum@0: $tracking['f'][$f_id] = $min_value; indvdum@0: unset($tracking['tf'][$f_id][$m_tkey]); indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: if ($user->data['is_registered']) indvdum@0: { indvdum@0: $user->data['user_lastmark'] = intval(base_convert(max($time_keys) + $config['board_startdate'], 36, 10)); indvdum@0: $db->sql_query('UPDATE ' . USERS_TABLE . ' SET user_lastmark = ' . $user->data['user_lastmark'] . " WHERE user_id = {$user->data['user_id']}"); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $tracking['l'] = max($time_keys); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $user->set_cookie('track', tracking_serialize($tracking), time() + 31536000); indvdum@0: $_COOKIE[$config['cookie_name'] . '_track'] = (STRIP) ? addslashes(tracking_serialize($tracking)) : tracking_serialize($tracking); indvdum@0: } indvdum@0: indvdum@0: return; indvdum@0: } indvdum@0: else if ($mode == 'post') indvdum@0: { indvdum@0: if ($topic_id === false) indvdum@0: { indvdum@0: return; indvdum@0: } indvdum@0: indvdum@0: $use_user_id = (!$user_id) ? $user->data['user_id'] : $user_id; indvdum@0: indvdum@0: if ($config['load_db_track'] && $use_user_id != ANONYMOUS) indvdum@0: { indvdum@0: $db->sql_return_on_error(true); indvdum@0: indvdum@0: $sql_ary = array( indvdum@0: 'user_id' => (int) $use_user_id, indvdum@0: 'topic_id' => (int) $topic_id, indvdum@0: 'topic_posted' => 1 indvdum@0: ); indvdum@0: indvdum@0: $db->sql_query('INSERT INTO ' . TOPICS_POSTED_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary)); indvdum@0: indvdum@0: $db->sql_return_on_error(false); indvdum@0: } indvdum@0: indvdum@0: return; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Get topic tracking info by using already fetched info indvdum@0: */ indvdum@0: function get_topic_tracking($forum_id, $topic_ids, &$rowset, $forum_mark_time, $global_announce_list = false) indvdum@0: { indvdum@0: global $config, $user; indvdum@0: indvdum@0: $last_read = array(); indvdum@0: indvdum@0: if (!is_array($topic_ids)) indvdum@0: { indvdum@0: $topic_ids = array($topic_ids); indvdum@0: } indvdum@0: indvdum@0: foreach ($topic_ids as $topic_id) indvdum@0: { indvdum@0: if (!empty($rowset[$topic_id]['mark_time'])) indvdum@0: { indvdum@0: $last_read[$topic_id] = $rowset[$topic_id]['mark_time']; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $topic_ids = array_diff($topic_ids, array_keys($last_read)); indvdum@0: indvdum@0: if (sizeof($topic_ids)) indvdum@0: { indvdum@0: $mark_time = array(); indvdum@0: indvdum@0: // Get global announcement info indvdum@0: if ($global_announce_list && sizeof($global_announce_list)) indvdum@0: { indvdum@0: if (!isset($forum_mark_time[0])) indvdum@0: { indvdum@0: global $db; indvdum@0: indvdum@0: $sql = 'SELECT mark_time indvdum@0: FROM ' . FORUMS_TRACK_TABLE . " indvdum@0: WHERE user_id = {$user->data['user_id']} indvdum@0: AND forum_id = 0"; indvdum@0: $result = $db->sql_query($sql); indvdum@0: $row = $db->sql_fetchrow($result); indvdum@0: $db->sql_freeresult($result); indvdum@0: indvdum@0: if ($row) indvdum@0: { indvdum@0: $mark_time[0] = $row['mark_time']; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: if ($forum_mark_time[0] !== false) indvdum@0: { indvdum@0: $mark_time[0] = $forum_mark_time[0]; indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: if (!empty($forum_mark_time[$forum_id]) && $forum_mark_time[$forum_id] !== false) indvdum@0: { indvdum@0: $mark_time[$forum_id] = $forum_mark_time[$forum_id]; indvdum@0: } indvdum@0: indvdum@0: $user_lastmark = (isset($mark_time[$forum_id])) ? $mark_time[$forum_id] : $user->data['user_lastmark']; indvdum@0: indvdum@0: foreach ($topic_ids as $topic_id) indvdum@0: { indvdum@0: if ($global_announce_list && isset($global_announce_list[$topic_id])) indvdum@0: { indvdum@0: $last_read[$topic_id] = (isset($mark_time[0])) ? $mark_time[0] : $user_lastmark; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $last_read[$topic_id] = $user_lastmark; indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: return $last_read; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Get topic tracking info from db (for cookie based tracking only this function is used) indvdum@0: */ indvdum@0: function get_complete_topic_tracking($forum_id, $topic_ids, $global_announce_list = false) indvdum@0: { indvdum@0: global $config, $user; indvdum@0: indvdum@0: $last_read = array(); indvdum@0: indvdum@0: if (!is_array($topic_ids)) indvdum@0: { indvdum@0: $topic_ids = array($topic_ids); indvdum@0: } indvdum@0: indvdum@0: if ($config['load_db_lastread'] && $user->data['is_registered']) indvdum@0: { indvdum@0: global $db; indvdum@0: indvdum@0: $sql = 'SELECT topic_id, mark_time indvdum@0: FROM ' . TOPICS_TRACK_TABLE . " indvdum@0: WHERE user_id = {$user->data['user_id']} indvdum@0: AND " . $db->sql_in_set('topic_id', $topic_ids); indvdum@0: $result = $db->sql_query($sql); indvdum@0: indvdum@0: while ($row = $db->sql_fetchrow($result)) indvdum@0: { indvdum@0: $last_read[$row['topic_id']] = $row['mark_time']; indvdum@0: } indvdum@0: $db->sql_freeresult($result); indvdum@0: indvdum@0: $topic_ids = array_diff($topic_ids, array_keys($last_read)); indvdum@0: indvdum@0: if (sizeof($topic_ids)) indvdum@0: { indvdum@0: $sql = 'SELECT forum_id, mark_time indvdum@0: FROM ' . FORUMS_TRACK_TABLE . " indvdum@0: WHERE user_id = {$user->data['user_id']} indvdum@0: AND forum_id " . indvdum@0: (($global_announce_list && sizeof($global_announce_list)) ? "IN (0, $forum_id)" : "= $forum_id"); indvdum@0: $result = $db->sql_query($sql); indvdum@0: indvdum@0: $mark_time = array(); indvdum@0: while ($row = $db->sql_fetchrow($result)) indvdum@0: { indvdum@0: $mark_time[$row['forum_id']] = $row['mark_time']; indvdum@0: } indvdum@0: $db->sql_freeresult($result); indvdum@0: indvdum@0: $user_lastmark = (isset($mark_time[$forum_id])) ? $mark_time[$forum_id] : $user->data['user_lastmark']; indvdum@0: indvdum@0: foreach ($topic_ids as $topic_id) indvdum@0: { indvdum@0: if ($global_announce_list && isset($global_announce_list[$topic_id])) indvdum@0: { indvdum@0: $last_read[$topic_id] = (isset($mark_time[0])) ? $mark_time[0] : $user_lastmark; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $last_read[$topic_id] = $user_lastmark; indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: else if ($config['load_anon_lastread'] || $user->data['is_registered']) indvdum@0: { indvdum@0: global $tracking_topics; indvdum@0: indvdum@0: if (!isset($tracking_topics) || !sizeof($tracking_topics)) indvdum@0: { indvdum@0: $tracking_topics = (isset($_COOKIE[$config['cookie_name'] . '_track'])) ? ((STRIP) ? stripslashes($_COOKIE[$config['cookie_name'] . '_track']) : $_COOKIE[$config['cookie_name'] . '_track']) : ''; indvdum@0: $tracking_topics = ($tracking_topics) ? tracking_unserialize($tracking_topics) : array(); indvdum@0: } indvdum@0: indvdum@0: if (!$user->data['is_registered']) indvdum@0: { indvdum@0: $user_lastmark = (isset($tracking_topics['l'])) ? base_convert($tracking_topics['l'], 36, 10) + $config['board_startdate'] : 0; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $user_lastmark = $user->data['user_lastmark']; indvdum@0: } indvdum@0: indvdum@0: foreach ($topic_ids as $topic_id) indvdum@0: { indvdum@0: $topic_id36 = base_convert($topic_id, 10, 36); indvdum@0: indvdum@0: if (isset($tracking_topics['t'][$topic_id36])) indvdum@0: { indvdum@0: $last_read[$topic_id] = base_convert($tracking_topics['t'][$topic_id36], 36, 10) + $config['board_startdate']; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $topic_ids = array_diff($topic_ids, array_keys($last_read)); indvdum@0: indvdum@0: if (sizeof($topic_ids)) indvdum@0: { indvdum@0: $mark_time = array(); indvdum@0: if ($global_announce_list && sizeof($global_announce_list)) indvdum@0: { indvdum@0: if (isset($tracking_topics['f'][0])) indvdum@0: { indvdum@0: $mark_time[0] = base_convert($tracking_topics['f'][0], 36, 10) + $config['board_startdate']; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: if (isset($tracking_topics['f'][$forum_id])) indvdum@0: { indvdum@0: $mark_time[$forum_id] = base_convert($tracking_topics['f'][$forum_id], 36, 10) + $config['board_startdate']; indvdum@0: } indvdum@0: indvdum@0: $user_lastmark = (isset($mark_time[$forum_id])) ? $mark_time[$forum_id] : $user_lastmark; indvdum@0: indvdum@0: foreach ($topic_ids as $topic_id) indvdum@0: { indvdum@0: if ($global_announce_list && isset($global_announce_list[$topic_id])) indvdum@0: { indvdum@0: $last_read[$topic_id] = (isset($mark_time[0])) ? $mark_time[0] : $user_lastmark; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $last_read[$topic_id] = $user_lastmark; indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: return $last_read; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Get list of unread topics indvdum@0: * indvdum@0: * @param int $user_id User ID (or false for current user) indvdum@0: * @param string $sql_extra Extra WHERE SQL statement indvdum@0: * @param string $sql_sort ORDER BY SQL sorting statement indvdum@0: * @param string $sql_limit Limits the size of unread topics list, 0 for unlimited query indvdum@0: * @param string $sql_limit_offset Sets the offset of the first row to search, 0 to search from the start indvdum@0: * indvdum@0: * @return array[int][int] Topic ids as keys, mark_time of topic as value indvdum@0: */ indvdum@0: function get_unread_topics($user_id = false, $sql_extra = '', $sql_sort = '', $sql_limit = 1001, $sql_limit_offset = 0) indvdum@0: { indvdum@0: global $config, $db, $user; indvdum@0: indvdum@0: $user_id = ($user_id === false) ? (int) $user->data['user_id'] : (int) $user_id; indvdum@0: indvdum@0: // Data array we're going to return indvdum@0: $unread_topics = array(); indvdum@0: indvdum@0: if (empty($sql_sort)) indvdum@0: { indvdum@0: $sql_sort = 'ORDER BY t.topic_last_post_time DESC'; indvdum@0: } indvdum@0: indvdum@0: if ($config['load_db_lastread'] && $user->data['is_registered']) indvdum@0: { indvdum@0: // Get list of the unread topics indvdum@0: $last_mark = (int) $user->data['user_lastmark']; indvdum@0: indvdum@0: $sql_array = array( indvdum@0: 'SELECT' => 't.topic_id, t.topic_last_post_time, tt.mark_time as topic_mark_time, ft.mark_time as forum_mark_time', indvdum@0: indvdum@0: 'FROM' => array(TOPICS_TABLE => 't'), indvdum@0: indvdum@0: 'LEFT_JOIN' => array( indvdum@0: array( indvdum@0: 'FROM' => array(TOPICS_TRACK_TABLE => 'tt'), indvdum@0: 'ON' => "tt.user_id = $user_id AND t.topic_id = tt.topic_id", indvdum@0: ), indvdum@0: array( indvdum@0: 'FROM' => array(FORUMS_TRACK_TABLE => 'ft'), indvdum@0: 'ON' => "ft.user_id = $user_id AND t.forum_id = ft.forum_id", indvdum@0: ), indvdum@0: ), indvdum@0: indvdum@0: 'WHERE' => " indvdum@0: t.topic_last_post_time > $last_mark AND indvdum@0: ( indvdum@0: (tt.mark_time IS NOT NULL AND t.topic_last_post_time > tt.mark_time) OR indvdum@0: (tt.mark_time IS NULL AND ft.mark_time IS NOT NULL AND t.topic_last_post_time > ft.mark_time) OR indvdum@0: (tt.mark_time IS NULL AND ft.mark_time IS NULL) indvdum@0: ) indvdum@0: $sql_extra indvdum@0: $sql_sort", indvdum@0: ); indvdum@0: indvdum@0: $sql = $db->sql_build_query('SELECT', $sql_array); indvdum@0: $result = $db->sql_query_limit($sql, $sql_limit, $sql_limit_offset); indvdum@0: indvdum@0: while ($row = $db->sql_fetchrow($result)) indvdum@0: { indvdum@0: $topic_id = (int) $row['topic_id']; indvdum@0: $unread_topics[$topic_id] = ($row['topic_mark_time']) ? (int) $row['topic_mark_time'] : (($row['forum_mark_time']) ? (int) $row['forum_mark_time'] : $last_mark); indvdum@0: } indvdum@0: $db->sql_freeresult($result); indvdum@0: } indvdum@0: else if ($config['load_anon_lastread'] || $user->data['is_registered']) indvdum@0: { indvdum@0: global $tracking_topics; indvdum@0: indvdum@0: if (empty($tracking_topics)) indvdum@0: { indvdum@0: $tracking_topics = request_var($config['cookie_name'] . '_track', '', false, true); indvdum@0: $tracking_topics = ($tracking_topics) ? tracking_unserialize($tracking_topics) : array(); indvdum@0: } indvdum@0: indvdum@0: if (!$user->data['is_registered']) indvdum@0: { indvdum@0: $user_lastmark = (isset($tracking_topics['l'])) ? base_convert($tracking_topics['l'], 36, 10) + $config['board_startdate'] : 0; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $user_lastmark = (int) $user->data['user_lastmark']; indvdum@0: } indvdum@0: indvdum@0: $sql = 'SELECT t.topic_id, t.forum_id, t.topic_last_post_time indvdum@0: FROM ' . TOPICS_TABLE . ' t indvdum@0: WHERE t.topic_last_post_time > ' . $user_lastmark . " indvdum@0: $sql_extra indvdum@0: $sql_sort"; indvdum@0: $result = $db->sql_query_limit($sql, $sql_limit, $sql_limit_offset); indvdum@0: indvdum@0: while ($row = $db->sql_fetchrow($result)) indvdum@0: { indvdum@0: $forum_id = (int) $row['forum_id']; indvdum@0: $topic_id = (int) $row['topic_id']; indvdum@0: $topic_id36 = base_convert($topic_id, 10, 36); indvdum@0: indvdum@0: if (isset($tracking_topics['t'][$topic_id36])) indvdum@0: { indvdum@0: $last_read = base_convert($tracking_topics['t'][$topic_id36], 36, 10) + $config['board_startdate']; indvdum@0: indvdum@0: if ($row['topic_last_post_time'] > $last_read) indvdum@0: { indvdum@0: $unread_topics[$topic_id] = $last_read; indvdum@0: } indvdum@0: } indvdum@0: else if (isset($tracking_topics['f'][$forum_id])) indvdum@0: { indvdum@0: $mark_time = base_convert($tracking_topics['f'][$forum_id], 36, 10) + $config['board_startdate']; indvdum@0: indvdum@0: if ($row['topic_last_post_time'] > $mark_time) indvdum@0: { indvdum@0: $unread_topics[$topic_id] = $mark_time; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $unread_topics[$topic_id] = $user_lastmark; indvdum@0: } indvdum@0: } indvdum@0: $db->sql_freeresult($result); indvdum@0: } indvdum@0: indvdum@0: return $unread_topics; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Check for read forums and update topic tracking info accordingly indvdum@0: * indvdum@0: * @param int $forum_id the forum id to check indvdum@0: * @param int $forum_last_post_time the forums last post time indvdum@0: * @param int $f_mark_time the forums last mark time if user is registered and load_db_lastread enabled indvdum@0: * @param int $mark_time_forum false if the mark time needs to be obtained, else the last users forum mark time indvdum@0: * indvdum@0: * @return true if complete forum got marked read, else false. indvdum@0: */ indvdum@0: function update_forum_tracking_info($forum_id, $forum_last_post_time, $f_mark_time = false, $mark_time_forum = false) indvdum@0: { indvdum@0: global $db, $tracking_topics, $user, $config, $auth; indvdum@0: indvdum@0: // Determine the users last forum mark time if not given. indvdum@0: if ($mark_time_forum === false) indvdum@0: { indvdum@0: if ($config['load_db_lastread'] && $user->data['is_registered']) indvdum@0: { indvdum@0: $mark_time_forum = (!empty($f_mark_time)) ? $f_mark_time : $user->data['user_lastmark']; indvdum@0: } indvdum@0: else if ($config['load_anon_lastread'] || $user->data['is_registered']) indvdum@0: { indvdum@0: $tracking_topics = (isset($_COOKIE[$config['cookie_name'] . '_track'])) ? ((STRIP) ? stripslashes($_COOKIE[$config['cookie_name'] . '_track']) : $_COOKIE[$config['cookie_name'] . '_track']) : ''; indvdum@0: $tracking_topics = ($tracking_topics) ? tracking_unserialize($tracking_topics) : array(); indvdum@0: indvdum@0: if (!$user->data['is_registered']) indvdum@0: { indvdum@0: $user->data['user_lastmark'] = (isset($tracking_topics['l'])) ? (int) (base_convert($tracking_topics['l'], 36, 10) + $config['board_startdate']) : 0; indvdum@0: } indvdum@0: indvdum@0: $mark_time_forum = (isset($tracking_topics['f'][$forum_id])) ? (int) (base_convert($tracking_topics['f'][$forum_id], 36, 10) + $config['board_startdate']) : $user->data['user_lastmark']; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // Handle update of unapproved topics info. indvdum@0: // Only update for moderators having m_approve permission for the forum. indvdum@0: $sql_update_unapproved = ($auth->acl_get('m_approve', $forum_id)) ? '': 'AND t.topic_approved = 1'; indvdum@0: indvdum@0: // Check the forum for any left unread topics. indvdum@0: // If there are none, we mark the forum as read. indvdum@0: if ($config['load_db_lastread'] && $user->data['is_registered']) indvdum@0: { indvdum@0: if ($mark_time_forum >= $forum_last_post_time) indvdum@0: { indvdum@0: // We do not need to mark read, this happened before. Therefore setting this to true indvdum@0: $row = true; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $sql = 'SELECT t.forum_id FROM ' . TOPICS_TABLE . ' t indvdum@0: LEFT JOIN ' . TOPICS_TRACK_TABLE . ' tt ON (tt.topic_id = t.topic_id AND tt.user_id = ' . $user->data['user_id'] . ') indvdum@0: WHERE t.forum_id = ' . $forum_id . ' indvdum@0: AND t.topic_last_post_time > ' . $mark_time_forum . ' indvdum@0: AND t.topic_moved_id = 0 ' . indvdum@0: $sql_update_unapproved . ' indvdum@0: AND (tt.topic_id IS NULL OR tt.mark_time < t.topic_last_post_time) indvdum@0: GROUP BY t.forum_id'; indvdum@0: $result = $db->sql_query_limit($sql, 1); indvdum@0: $row = $db->sql_fetchrow($result); indvdum@0: $db->sql_freeresult($result); indvdum@0: } indvdum@0: } indvdum@0: else if ($config['load_anon_lastread'] || $user->data['is_registered']) indvdum@0: { indvdum@0: // Get information from cookie indvdum@0: $row = false; indvdum@0: indvdum@0: if (!isset($tracking_topics['tf'][$forum_id])) indvdum@0: { indvdum@0: // We do not need to mark read, this happened before. Therefore setting this to true indvdum@0: $row = true; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $sql = 'SELECT t.topic_id indvdum@0: FROM ' . TOPICS_TABLE . ' t indvdum@0: WHERE t.forum_id = ' . $forum_id . ' indvdum@0: AND t.topic_last_post_time > ' . $mark_time_forum . ' indvdum@0: AND t.topic_moved_id = 0 ' . indvdum@0: $sql_update_unapproved; indvdum@0: $result = $db->sql_query($sql); indvdum@0: indvdum@0: $check_forum = $tracking_topics['tf'][$forum_id]; indvdum@0: $unread = false; indvdum@0: indvdum@0: while ($row = $db->sql_fetchrow($result)) indvdum@0: { indvdum@0: if (!isset($check_forum[base_convert($row['topic_id'], 10, 36)])) indvdum@0: { indvdum@0: $unread = true; indvdum@0: break; indvdum@0: } indvdum@0: } indvdum@0: $db->sql_freeresult($result); indvdum@0: indvdum@0: $row = $unread; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $row = true; indvdum@0: } indvdum@0: indvdum@0: if (!$row) indvdum@0: { indvdum@0: markread('topics', $forum_id); indvdum@0: return true; indvdum@0: } indvdum@0: indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Transform an array into a serialized format indvdum@0: */ indvdum@0: function tracking_serialize($input) indvdum@0: { indvdum@0: $out = ''; indvdum@0: foreach ($input as $key => $value) indvdum@0: { indvdum@0: if (is_array($value)) indvdum@0: { indvdum@0: $out .= $key . ':(' . tracking_serialize($value) . ');'; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $out .= $key . ':' . $value . ';'; indvdum@0: } indvdum@0: } indvdum@0: return $out; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Transform a serialized array into an actual array indvdum@0: */ indvdum@0: function tracking_unserialize($string, $max_depth = 3) indvdum@0: { indvdum@0: $n = strlen($string); indvdum@0: if ($n > 10010) indvdum@0: { indvdum@0: die('Invalid data supplied'); indvdum@0: } indvdum@0: $data = $stack = array(); indvdum@0: $key = ''; indvdum@0: $mode = 0; indvdum@0: $level = &$data; indvdum@0: for ($i = 0; $i < $n; ++$i) indvdum@0: { indvdum@0: switch ($mode) indvdum@0: { indvdum@0: case 0: indvdum@0: switch ($string[$i]) indvdum@0: { indvdum@0: case ':': indvdum@0: $level[$key] = 0; indvdum@0: $mode = 1; indvdum@0: break; indvdum@0: case ')': indvdum@0: unset($level); indvdum@0: $level = array_pop($stack); indvdum@0: $mode = 3; indvdum@0: break; indvdum@0: default: indvdum@0: $key .= $string[$i]; indvdum@0: } indvdum@0: break; indvdum@0: indvdum@0: case 1: indvdum@0: switch ($string[$i]) indvdum@0: { indvdum@0: case '(': indvdum@0: if (sizeof($stack) >= $max_depth) indvdum@0: { indvdum@0: die('Invalid data supplied'); indvdum@0: } indvdum@0: $stack[] = &$level; indvdum@0: $level[$key] = array(); indvdum@0: $level = &$level[$key]; indvdum@0: $key = ''; indvdum@0: $mode = 0; indvdum@0: break; indvdum@0: default: indvdum@0: $level[$key] = $string[$i]; indvdum@0: $mode = 2; indvdum@0: break; indvdum@0: } indvdum@0: break; indvdum@0: indvdum@0: case 2: indvdum@0: switch ($string[$i]) indvdum@0: { indvdum@0: case ')': indvdum@0: unset($level); indvdum@0: $level = array_pop($stack); indvdum@0: $mode = 3; indvdum@0: break; indvdum@0: case ';': indvdum@0: $key = ''; indvdum@0: $mode = 0; indvdum@0: break; indvdum@0: default: indvdum@0: $level[$key] .= $string[$i]; indvdum@0: break; indvdum@0: } indvdum@0: break; indvdum@0: indvdum@0: case 3: indvdum@0: switch ($string[$i]) indvdum@0: { indvdum@0: case ')': indvdum@0: unset($level); indvdum@0: $level = array_pop($stack); indvdum@0: break; indvdum@0: case ';': indvdum@0: $key = ''; indvdum@0: $mode = 0; indvdum@0: break; indvdum@0: default: indvdum@0: die('Invalid data supplied'); indvdum@0: break; indvdum@0: } indvdum@0: break; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: if (sizeof($stack) != 0 || ($mode != 0 && $mode != 3)) indvdum@0: { indvdum@0: die('Invalid data supplied'); indvdum@0: } indvdum@0: indvdum@0: return $level; indvdum@0: } indvdum@0: indvdum@0: // Pagination functions indvdum@0: indvdum@0: /** indvdum@0: * Pagination routine, generates page number sequence indvdum@0: * tpl_prefix is for using different pagination blocks at one page indvdum@0: */ indvdum@0: function generate_pagination($base_url, $num_items, $per_page, $start_item, $add_prevnext_text = false, $tpl_prefix = '') indvdum@0: { indvdum@0: global $template, $user; indvdum@0: indvdum@0: // Make sure $per_page is a valid value indvdum@0: $per_page = ($per_page <= 0) ? 1 : $per_page; indvdum@0: indvdum@0: $seperator = '' . $user->lang['COMMA_SEPARATOR'] . ''; indvdum@0: $total_pages = ceil($num_items / $per_page); indvdum@0: indvdum@0: if ($total_pages == 1 || !$num_items) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: $on_page = floor($start_item / $per_page) + 1; indvdum@0: $url_delim = (strpos($base_url, '?') === false) ? '?' : ((strpos($base_url, '?') === strlen($base_url) - 1) ? '' : '&'); indvdum@0: indvdum@0: $page_string = ($on_page == 1) ? '1' : '1'; indvdum@0: indvdum@0: if ($total_pages > 5) indvdum@0: { indvdum@0: $start_cnt = min(max(1, $on_page - 4), $total_pages - 5); indvdum@0: $end_cnt = max(min($total_pages, $on_page + 4), 6); indvdum@0: indvdum@0: $page_string .= ($start_cnt > 1) ? ' ... ' : $seperator; indvdum@0: indvdum@0: for ($i = $start_cnt + 1; $i < $end_cnt; $i++) indvdum@0: { indvdum@0: $page_string .= ($i == $on_page) ? '' . $i . '' : '' . $i . ''; indvdum@0: if ($i < $end_cnt - 1) indvdum@0: { indvdum@0: $page_string .= $seperator; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $page_string .= ($end_cnt < $total_pages) ? ' ... ' : $seperator; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $page_string .= $seperator; indvdum@0: indvdum@0: for ($i = 2; $i < $total_pages; $i++) indvdum@0: { indvdum@0: $page_string .= ($i == $on_page) ? '' . $i . '' : '' . $i . ''; indvdum@0: if ($i < $total_pages) indvdum@0: { indvdum@0: $page_string .= $seperator; indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $page_string .= ($on_page == $total_pages) ? '' . $total_pages . '' : '' . $total_pages . ''; indvdum@0: indvdum@0: if ($add_prevnext_text) indvdum@0: { indvdum@0: if ($on_page != 1) indvdum@0: { indvdum@0: $page_string = '' . $user->lang['PREVIOUS'] . '  ' . $page_string; indvdum@0: } indvdum@0: indvdum@0: if ($on_page != $total_pages) indvdum@0: { indvdum@0: $page_string .= '  ' . $user->lang['NEXT'] . ''; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $template->assign_vars(array( indvdum@0: $tpl_prefix . 'BASE_URL' => $base_url, indvdum@0: 'A_' . $tpl_prefix . 'BASE_URL' => addslashes($base_url), indvdum@0: $tpl_prefix . 'PER_PAGE' => $per_page, indvdum@0: indvdum@0: $tpl_prefix . 'PREVIOUS_PAGE' => ($on_page == 1) ? '' : $base_url . "{$url_delim}start=" . (($on_page - 2) * $per_page), indvdum@0: $tpl_prefix . 'NEXT_PAGE' => ($on_page == $total_pages) ? '' : $base_url . "{$url_delim}start=" . ($on_page * $per_page), indvdum@0: $tpl_prefix . 'TOTAL_PAGES' => $total_pages, indvdum@0: )); indvdum@0: indvdum@0: return $page_string; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Return current page (pagination) indvdum@0: */ indvdum@0: function on_page($num_items, $per_page, $start) indvdum@0: { indvdum@0: global $template, $user; indvdum@0: indvdum@0: // Make sure $per_page is a valid value indvdum@0: $per_page = ($per_page <= 0) ? 1 : $per_page; indvdum@0: indvdum@0: $on_page = floor($start / $per_page) + 1; indvdum@0: indvdum@0: $template->assign_vars(array( indvdum@0: 'ON_PAGE' => $on_page) indvdum@0: ); indvdum@0: indvdum@0: return sprintf($user->lang['PAGE_OF'], $on_page, max(ceil($num_items / $per_page), 1)); indvdum@0: } indvdum@0: indvdum@0: // Server functions (building urls, redirecting...) indvdum@0: indvdum@0: /** indvdum@0: * Append session id to url. indvdum@0: * This function supports hooks. indvdum@0: * indvdum@0: * @param string $url The url the session id needs to be appended to (can have params) indvdum@0: * @param mixed $params String or array of additional url parameters indvdum@0: * @param bool $is_amp Is url using & (true) or & (false) indvdum@0: * @param string $session_id Possibility to use a custom session id instead of the global one indvdum@0: * indvdum@0: * Examples: indvdum@0: * indvdum@0: * append_sid("{$phpbb_root_path}viewtopic.$phpEx?t=1&f=2"); indvdum@0: * append_sid("{$phpbb_root_path}viewtopic.$phpEx", 't=1&f=2'); indvdum@0: * append_sid("{$phpbb_root_path}viewtopic.$phpEx", 't=1&f=2', false); indvdum@0: * append_sid("{$phpbb_root_path}viewtopic.$phpEx", array('t' => 1, 'f' => 2)); indvdum@0: * indvdum@0: * indvdum@0: */ indvdum@0: function append_sid($url, $params = false, $is_amp = true, $session_id = false) indvdum@0: { indvdum@0: global $_SID, $_EXTRA_URL, $phpbb_hook; indvdum@0: indvdum@0: if ($params === '' || (is_array($params) && empty($params))) indvdum@0: { indvdum@0: // Do not append the ? if the param-list is empty anyway. indvdum@0: $params = false; indvdum@0: } indvdum@0: indvdum@0: // Developers using the hook function need to globalise the $_SID and $_EXTRA_URL on their own and also handle it appropriately. indvdum@0: // They could mimic most of what is within this function indvdum@0: if (!empty($phpbb_hook) && $phpbb_hook->call_hook(__FUNCTION__, $url, $params, $is_amp, $session_id)) indvdum@0: { indvdum@0: if ($phpbb_hook->hook_return(__FUNCTION__)) indvdum@0: { indvdum@0: return $phpbb_hook->hook_return_result(__FUNCTION__); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $params_is_array = is_array($params); indvdum@0: indvdum@0: // Get anchor indvdum@0: $anchor = ''; indvdum@0: if (strpos($url, '#') !== false) indvdum@0: { indvdum@0: list($url, $anchor) = explode('#', $url, 2); indvdum@0: $anchor = '#' . $anchor; indvdum@0: } indvdum@0: else if (!$params_is_array && strpos($params, '#') !== false) indvdum@0: { indvdum@0: list($params, $anchor) = explode('#', $params, 2); indvdum@0: $anchor = '#' . $anchor; indvdum@0: } indvdum@0: indvdum@0: // Handle really simple cases quickly indvdum@0: if ($_SID == '' && $session_id === false && empty($_EXTRA_URL) && !$params_is_array && !$anchor) indvdum@0: { indvdum@0: if ($params === false) indvdum@0: { indvdum@0: return $url; indvdum@0: } indvdum@0: indvdum@0: $url_delim = (strpos($url, '?') === false) ? '?' : (($is_amp) ? '&' : '&'); indvdum@0: return $url . ($params !== false ? $url_delim. $params : ''); indvdum@0: } indvdum@0: indvdum@0: // Assign sid if session id is not specified indvdum@0: if ($session_id === false) indvdum@0: { indvdum@0: $session_id = $_SID; indvdum@0: } indvdum@0: indvdum@0: $amp_delim = ($is_amp) ? '&' : '&'; indvdum@0: $url_delim = (strpos($url, '?') === false) ? '?' : $amp_delim; indvdum@0: indvdum@0: // Appending custom url parameter? indvdum@0: $append_url = (!empty($_EXTRA_URL)) ? implode($amp_delim, $_EXTRA_URL) : ''; indvdum@0: indvdum@0: // Use the short variant if possible ;) indvdum@0: if ($params === false) indvdum@0: { indvdum@0: // Append session id indvdum@0: if (!$session_id) indvdum@0: { indvdum@0: return $url . (($append_url) ? $url_delim . $append_url : '') . $anchor; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: return $url . (($append_url) ? $url_delim . $append_url . $amp_delim : $url_delim) . 'sid=' . $session_id . $anchor; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // Build string if parameters are specified as array indvdum@0: if (is_array($params)) indvdum@0: { indvdum@0: $output = array(); indvdum@0: indvdum@0: foreach ($params as $key => $item) indvdum@0: { indvdum@0: if ($item === NULL) indvdum@0: { indvdum@0: continue; indvdum@0: } indvdum@0: indvdum@0: if ($key == '#') indvdum@0: { indvdum@0: $anchor = '#' . $item; indvdum@0: continue; indvdum@0: } indvdum@0: indvdum@0: $output[] = $key . '=' . $item; indvdum@0: } indvdum@0: indvdum@0: $params = implode($amp_delim, $output); indvdum@0: } indvdum@0: indvdum@0: // Append session id and parameters (even if they are empty) indvdum@0: // If parameters are empty, the developer can still append his/her parameters without caring about the delimiter indvdum@0: return $url . (($append_url) ? $url_delim . $append_url . $amp_delim : $url_delim) . $params . ((!$session_id) ? '' : $amp_delim . 'sid=' . $session_id) . $anchor; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Generate board url (example: http://www.example.com/phpBB) indvdum@0: * indvdum@0: * @param bool $without_script_path if set to true the script path gets not appended (example: http://www.example.com) indvdum@0: * indvdum@0: * @return string the generated board url indvdum@0: */ indvdum@0: function generate_board_url($without_script_path = false) indvdum@0: { indvdum@0: global $config, $user; indvdum@0: indvdum@0: $server_name = $user->host; indvdum@0: $server_port = (!empty($_SERVER['SERVER_PORT'])) ? (int) $_SERVER['SERVER_PORT'] : (int) getenv('SERVER_PORT'); indvdum@0: indvdum@0: // Forcing server vars is the only way to specify/override the protocol indvdum@0: if ($config['force_server_vars'] || !$server_name) indvdum@0: { indvdum@0: $server_protocol = ($config['server_protocol']) ? $config['server_protocol'] : (($config['cookie_secure']) ? 'https://' : 'http://'); indvdum@0: $server_name = $config['server_name']; indvdum@0: $server_port = (int) $config['server_port']; indvdum@0: $script_path = $config['script_path']; indvdum@0: indvdum@0: $url = $server_protocol . $server_name; indvdum@0: $cookie_secure = $config['cookie_secure']; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // Do not rely on cookie_secure, users seem to think that it means a secured cookie instead of an encrypted connection indvdum@0: $cookie_secure = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 1 : 0; indvdum@0: $url = (($cookie_secure) ? 'https://' : 'http://') . $server_name; indvdum@0: indvdum@0: $script_path = $user->page['root_script_path']; indvdum@0: } indvdum@0: indvdum@0: if ($server_port && (($cookie_secure && $server_port <> 443) || (!$cookie_secure && $server_port <> 80))) indvdum@0: { indvdum@0: // HTTP HOST can carry a port number (we fetch $user->host, but for old versions this may be true) indvdum@0: if (strpos($server_name, ':') === false) indvdum@0: { indvdum@0: $url .= ':' . $server_port; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: if (!$without_script_path) indvdum@0: { indvdum@0: $url .= $script_path; indvdum@0: } indvdum@0: indvdum@0: // Strip / from the end indvdum@0: if (substr($url, -1, 1) == '/') indvdum@0: { indvdum@0: $url = substr($url, 0, -1); indvdum@0: } indvdum@0: indvdum@0: return $url; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Redirects the user to another page then exits the script nicely indvdum@0: * This function is intended for urls within the board. It's not meant to redirect to cross-domains. indvdum@0: * indvdum@0: * @param string $url The url to redirect to indvdum@0: * @param bool $return If true, do not redirect but return the sanitized URL. Default is no return. indvdum@0: * @param bool $disable_cd_check If true, redirect() will redirect to an external domain. If false, the redirect point to the boards url if it does not match the current domain. Default is false. indvdum@0: */ indvdum@0: function redirect($url, $return = false, $disable_cd_check = false) indvdum@0: { indvdum@0: global $db, $cache, $config, $user, $phpbb_root_path; indvdum@0: indvdum@0: $failover_flag = false; indvdum@0: indvdum@0: if (empty($user->lang)) indvdum@0: { indvdum@0: $user->add_lang('common'); indvdum@0: } indvdum@0: indvdum@0: if (!$return) indvdum@0: { indvdum@0: garbage_collection(); indvdum@0: } indvdum@0: indvdum@0: // Make sure no &'s are in, this will break the redirect indvdum@0: $url = str_replace('&', '&', $url); indvdum@0: indvdum@0: // Determine which type of redirect we need to handle... indvdum@0: $url_parts = @parse_url($url); indvdum@0: indvdum@0: if ($url_parts === false) indvdum@0: { indvdum@0: // Malformed url, redirect to current page... indvdum@0: $url = generate_board_url() . '/' . $user->page['page']; indvdum@0: } indvdum@0: else if (!empty($url_parts['scheme']) && !empty($url_parts['host'])) indvdum@0: { indvdum@0: // Attention: only able to redirect within the same domain if $disable_cd_check is false (yourdomain.com -> www.yourdomain.com will not work) indvdum@0: if (!$disable_cd_check && $url_parts['host'] !== $user->host) indvdum@0: { indvdum@0: $url = generate_board_url(); indvdum@0: } indvdum@0: } indvdum@0: else if ($url[0] == '/') indvdum@0: { indvdum@0: // Absolute uri, prepend direct url... indvdum@0: $url = generate_board_url(true) . $url; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // Relative uri indvdum@0: $pathinfo = pathinfo($url); indvdum@0: indvdum@0: if (!$disable_cd_check && !file_exists($pathinfo['dirname'] . '/')) indvdum@0: { indvdum@0: $url = str_replace('../', '', $url); indvdum@0: $pathinfo = pathinfo($url); indvdum@0: indvdum@0: if (!file_exists($pathinfo['dirname'] . '/')) indvdum@0: { indvdum@0: // fallback to "last known user page" indvdum@0: // at least this way we know the user does not leave the phpBB root indvdum@0: $url = generate_board_url() . '/' . $user->page['page']; indvdum@0: $failover_flag = true; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: if (!$failover_flag) indvdum@0: { indvdum@0: // Is the uri pointing to the current directory? indvdum@0: if ($pathinfo['dirname'] == '.') indvdum@0: { indvdum@0: $url = str_replace('./', '', $url); indvdum@0: indvdum@0: // Strip / from the beginning indvdum@0: if ($url && substr($url, 0, 1) == '/') indvdum@0: { indvdum@0: $url = substr($url, 1); indvdum@0: } indvdum@0: indvdum@0: if ($user->page['page_dir']) indvdum@0: { indvdum@0: $url = generate_board_url() . '/' . $user->page['page_dir'] . '/' . $url; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $url = generate_board_url() . '/' . $url; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // Used ./ before, but $phpbb_root_path is working better with urls within another root path indvdum@0: $root_dirs = explode('/', str_replace('\\', '/', phpbb_realpath($phpbb_root_path))); indvdum@0: $page_dirs = explode('/', str_replace('\\', '/', phpbb_realpath($pathinfo['dirname']))); indvdum@0: $intersection = array_intersect_assoc($root_dirs, $page_dirs); indvdum@0: indvdum@0: $root_dirs = array_diff_assoc($root_dirs, $intersection); indvdum@0: $page_dirs = array_diff_assoc($page_dirs, $intersection); indvdum@0: indvdum@0: $dir = str_repeat('../', sizeof($root_dirs)) . implode('/', $page_dirs); indvdum@0: indvdum@0: // Strip / from the end indvdum@0: if ($dir && substr($dir, -1, 1) == '/') indvdum@0: { indvdum@0: $dir = substr($dir, 0, -1); indvdum@0: } indvdum@0: indvdum@0: // Strip / from the beginning indvdum@0: if ($dir && substr($dir, 0, 1) == '/') indvdum@0: { indvdum@0: $dir = substr($dir, 1); indvdum@0: } indvdum@0: indvdum@0: $url = str_replace($pathinfo['dirname'] . '/', '', $url); indvdum@0: indvdum@0: // Strip / from the beginning indvdum@0: if (substr($url, 0, 1) == '/') indvdum@0: { indvdum@0: $url = substr($url, 1); indvdum@0: } indvdum@0: indvdum@0: $url = (!empty($dir) ? $dir . '/' : '') . $url; indvdum@0: $url = generate_board_url() . '/' . $url; indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // Make sure no linebreaks are there... to prevent http response splitting for PHP < 4.4.2 indvdum@0: if (strpos(urldecode($url), "\n") !== false || strpos(urldecode($url), "\r") !== false || strpos($url, ';') !== false) indvdum@0: { indvdum@0: trigger_error('Tried to redirect to potentially insecure url.', E_USER_ERROR); indvdum@0: } indvdum@0: indvdum@0: // Now, also check the protocol and for a valid url the last time... indvdum@0: $allowed_protocols = array('http', 'https', 'ftp', 'ftps'); indvdum@0: $url_parts = parse_url($url); indvdum@0: indvdum@0: if ($url_parts === false || empty($url_parts['scheme']) || !in_array($url_parts['scheme'], $allowed_protocols)) indvdum@0: { indvdum@0: trigger_error('Tried to redirect to potentially insecure url.', E_USER_ERROR); indvdum@0: } indvdum@0: indvdum@0: if ($return) indvdum@0: { indvdum@0: return $url; indvdum@0: } indvdum@0: indvdum@0: // Redirect via an HTML form for PITA webservers indvdum@0: if (@preg_match('#Microsoft|WebSTAR|Xitami#', getenv('SERVER_SOFTWARE'))) indvdum@0: { indvdum@0: header('Refresh: 0; URL=' . $url); indvdum@0: indvdum@0: echo ''; indvdum@0: echo ''; indvdum@0: echo ''; indvdum@0: echo ''; indvdum@0: echo ''; indvdum@0: echo '' . $user->lang['REDIRECT'] . ''; indvdum@0: echo ''; indvdum@0: echo ''; indvdum@0: echo '
' . sprintf($user->lang['URL_REDIRECT'], '', '') . '
'; indvdum@0: echo ''; indvdum@0: echo ''; indvdum@0: indvdum@0: exit; indvdum@0: } indvdum@0: indvdum@0: // Behave as per HTTP/1.1 spec for others indvdum@0: header('Location: ' . $url); indvdum@0: exit; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Re-Apply session id after page reloads indvdum@0: */ indvdum@0: function reapply_sid($url) indvdum@0: { indvdum@0: global $phpEx, $phpbb_root_path; indvdum@0: indvdum@0: if ($url === "index.$phpEx") indvdum@0: { indvdum@0: return append_sid("index.$phpEx"); indvdum@0: } indvdum@0: else if ($url === "{$phpbb_root_path}index.$phpEx") indvdum@0: { indvdum@0: return append_sid("{$phpbb_root_path}index.$phpEx"); indvdum@0: } indvdum@0: indvdum@0: // Remove previously added sid indvdum@0: if (strpos($url, 'sid=') !== false) indvdum@0: { indvdum@0: // All kind of links indvdum@0: $url = preg_replace('/(\?)?(&|&)?sid=[a-z0-9]+/', '', $url); indvdum@0: // if the sid was the first param, make the old second as first ones indvdum@0: $url = preg_replace("/$phpEx(&|&)+?/", "$phpEx?", $url); indvdum@0: } indvdum@0: indvdum@0: return append_sid($url); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Returns url from the session/current page with an re-appended SID with optionally stripping vars from the url indvdum@0: */ indvdum@0: function build_url($strip_vars = false) indvdum@0: { indvdum@0: global $user, $phpbb_root_path; indvdum@0: indvdum@0: // Append SID indvdum@0: $redirect = append_sid($user->page['page'], false, false); indvdum@0: indvdum@0: // Add delimiter if not there... indvdum@0: if (strpos($redirect, '?') === false) indvdum@0: { indvdum@0: $redirect .= '?'; indvdum@0: } indvdum@0: indvdum@0: // Strip vars... indvdum@0: if ($strip_vars !== false && strpos($redirect, '?') !== false) indvdum@0: { indvdum@0: if (!is_array($strip_vars)) indvdum@0: { indvdum@0: $strip_vars = array($strip_vars); indvdum@0: } indvdum@0: indvdum@0: $query = $_query = array(); indvdum@0: indvdum@0: $args = substr($redirect, strpos($redirect, '?') + 1); indvdum@0: $args = ($args) ? explode('&', $args) : array(); indvdum@0: $redirect = substr($redirect, 0, strpos($redirect, '?')); indvdum@0: indvdum@0: foreach ($args as $argument) indvdum@0: { indvdum@0: $arguments = explode('=', $argument); indvdum@0: $key = $arguments[0]; indvdum@0: unset($arguments[0]); indvdum@0: indvdum@0: if ($key === '') indvdum@0: { indvdum@0: continue; indvdum@0: } indvdum@0: indvdum@0: $query[$key] = implode('=', $arguments); indvdum@0: } indvdum@0: indvdum@0: // Strip the vars off indvdum@0: foreach ($strip_vars as $strip) indvdum@0: { indvdum@0: if (isset($query[$strip])) indvdum@0: { indvdum@0: unset($query[$strip]); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // Glue the remaining parts together... already urlencoded indvdum@0: foreach ($query as $key => $value) indvdum@0: { indvdum@0: $_query[] = $key . '=' . $value; indvdum@0: } indvdum@0: $query = implode('&', $_query); indvdum@0: indvdum@0: $redirect .= ($query) ? '?' . $query : ''; indvdum@0: } indvdum@0: indvdum@0: // We need to be cautious here. indvdum@0: // On some situations, the redirect path is an absolute URL, sometimes a relative path indvdum@0: // For a relative path, let's prefix it with $phpbb_root_path to point to the correct location, indvdum@0: // else we use the URL directly. indvdum@0: $url_parts = @parse_url($redirect); indvdum@0: indvdum@0: // URL indvdum@0: if ($url_parts !== false && !empty($url_parts['scheme']) && !empty($url_parts['host'])) indvdum@0: { indvdum@0: return str_replace('&', '&', $redirect); indvdum@0: } indvdum@0: indvdum@0: return $phpbb_root_path . str_replace('&', '&', $redirect); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Meta refresh assignment indvdum@0: * Adds META template variable with meta http tag. indvdum@0: * indvdum@0: * @param int $time Time in seconds for meta refresh tag indvdum@0: * @param string $url URL to redirect to. The url will go through redirect() first before the template variable is assigned indvdum@0: * @param bool $disable_cd_check If true, meta_refresh() will redirect to an external domain. If false, the redirect point to the boards url if it does not match the current domain. Default is false. indvdum@0: */ indvdum@0: function meta_refresh($time, $url, $disable_cd_check = false) indvdum@0: { indvdum@0: global $template; indvdum@0: indvdum@0: $url = redirect($url, true, $disable_cd_check); indvdum@0: $url = str_replace('&', '&', $url); indvdum@0: indvdum@0: // For XHTML compatibility we change back & to & indvdum@0: $template->assign_vars(array( indvdum@0: 'META' => '') indvdum@0: ); indvdum@0: indvdum@0: return $url; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Outputs correct status line header. indvdum@0: * indvdum@0: * Depending on php sapi one of the two following forms is used: indvdum@0: * indvdum@0: * Status: 404 Not Found indvdum@0: * indvdum@0: * HTTP/1.x 404 Not Found indvdum@0: * indvdum@0: * HTTP version is taken from HTTP_VERSION environment variable, indvdum@0: * and defaults to 1.0. indvdum@0: * indvdum@0: * Sample usage: indvdum@0: * indvdum@0: * send_status_line(404, 'Not Found'); indvdum@0: * indvdum@0: * @param int $code HTTP status code indvdum@0: * @param string $message Message for the status code indvdum@0: * @return void indvdum@0: */ indvdum@0: function send_status_line($code, $message) indvdum@0: { indvdum@0: if (substr(strtolower(@php_sapi_name()), 0, 3) === 'cgi') indvdum@0: { indvdum@0: // in theory, we shouldn't need that due to php doing it. Reality offers a differing opinion, though indvdum@0: header("Status: $code $message", true, $code); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: if (!empty($_SERVER['SERVER_PROTOCOL'])) indvdum@0: { indvdum@0: $version = $_SERVER['SERVER_PROTOCOL']; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $version = 'HTTP/1.0'; indvdum@0: } indvdum@0: header("$version $code $message", true, $code); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: //Form validation indvdum@0: indvdum@0: indvdum@0: /** indvdum@0: * Add a secret hash for use in links/GET requests indvdum@0: * @param string $link_name The name of the link; has to match the name used in check_link_hash, otherwise no restrictions apply indvdum@0: * @return string the hash indvdum@0: indvdum@0: */ indvdum@0: function generate_link_hash($link_name) indvdum@0: { indvdum@0: global $user; indvdum@0: indvdum@0: if (!isset($user->data["hash_$link_name"])) indvdum@0: { indvdum@0: $user->data["hash_$link_name"] = substr(sha1($user->data['user_form_salt'] . $link_name), 0, 8); indvdum@0: } indvdum@0: indvdum@0: return $user->data["hash_$link_name"]; indvdum@0: } indvdum@0: indvdum@0: indvdum@0: /** indvdum@0: * checks a link hash - for GET requests indvdum@0: * @param string $token the submitted token indvdum@0: * @param string $link_name The name of the link indvdum@0: * @return boolean true if all is fine indvdum@0: */ indvdum@0: function check_link_hash($token, $link_name) indvdum@0: { indvdum@0: return $token === generate_link_hash($link_name); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Add a secret token to the form (requires the S_FORM_TOKEN template variable) indvdum@0: * @param string $form_name The name of the form; has to match the name used in check_form_key, otherwise no restrictions apply indvdum@0: */ indvdum@0: function add_form_key($form_name) indvdum@0: { indvdum@0: global $config, $template, $user; indvdum@0: indvdum@0: $now = time(); indvdum@0: $token_sid = ($user->data['user_id'] == ANONYMOUS && !empty($config['form_token_sid_guests'])) ? $user->session_id : ''; indvdum@0: $token = sha1($now . $user->data['user_form_salt'] . $form_name . $token_sid); indvdum@0: indvdum@0: $s_fields = build_hidden_fields(array( indvdum@0: 'creation_time' => $now, indvdum@0: 'form_token' => $token, indvdum@0: )); indvdum@0: indvdum@0: $template->assign_vars(array( indvdum@0: 'S_FORM_TOKEN' => $s_fields, indvdum@0: )); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Check the form key. Required for all altering actions not secured by confirm_box indvdum@0: * @param string $form_name The name of the form; has to match the name used in add_form_key, otherwise no restrictions apply indvdum@0: * @param int $timespan The maximum acceptable age for a submitted form in seconds. Defaults to the config setting. indvdum@0: * @param string $return_page The address for the return link indvdum@0: * @param bool $trigger If true, the function will triger an error when encountering an invalid form indvdum@0: */ indvdum@0: function check_form_key($form_name, $timespan = false, $return_page = '', $trigger = false) indvdum@0: { indvdum@0: global $config, $user; indvdum@0: indvdum@0: if ($timespan === false) indvdum@0: { indvdum@0: // we enforce a minimum value of half a minute here. indvdum@0: $timespan = ($config['form_token_lifetime'] == -1) ? -1 : max(30, $config['form_token_lifetime']); indvdum@0: } indvdum@0: indvdum@0: if (isset($_POST['creation_time']) && isset($_POST['form_token'])) indvdum@0: { indvdum@0: $creation_time = abs(request_var('creation_time', 0)); indvdum@0: $token = request_var('form_token', ''); indvdum@0: indvdum@0: $diff = time() - $creation_time; indvdum@0: indvdum@0: // If creation_time and the time() now is zero we can assume it was not a human doing this (the check for if ($diff)... indvdum@0: if ($diff && ($diff <= $timespan || $timespan === -1)) indvdum@0: { indvdum@0: $token_sid = ($user->data['user_id'] == ANONYMOUS && !empty($config['form_token_sid_guests'])) ? $user->session_id : ''; indvdum@0: $key = sha1($creation_time . $user->data['user_form_salt'] . $form_name . $token_sid); indvdum@0: indvdum@0: if ($key === $token) indvdum@0: { indvdum@0: return true; indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: if ($trigger) indvdum@0: { indvdum@0: trigger_error($user->lang['FORM_INVALID'] . $return_page); indvdum@0: } indvdum@0: indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: // Message/Login boxes indvdum@0: indvdum@0: /** indvdum@0: * Build Confirm box indvdum@0: * @param boolean $check True for checking if confirmed (without any additional parameters) and false for displaying the confirm box indvdum@0: * @param string $title Title/Message used for confirm box. indvdum@0: * message text is _CONFIRM appended to title. indvdum@0: * If title cannot be found in user->lang a default one is displayed indvdum@0: * If title_CONFIRM cannot be found in user->lang the text given is used. indvdum@0: * @param string $hidden Hidden variables indvdum@0: * @param string $html_body Template used for confirm box indvdum@0: * @param string $u_action Custom form action indvdum@0: */ indvdum@0: function confirm_box($check, $title = '', $hidden = '', $html_body = 'confirm_body.html', $u_action = '') indvdum@0: { indvdum@0: global $user, $template, $db; indvdum@0: global $phpEx, $phpbb_root_path; indvdum@0: indvdum@0: if (isset($_POST['cancel'])) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: $confirm = false; indvdum@0: if (isset($_POST['confirm'])) indvdum@0: { indvdum@0: // language frontier indvdum@0: if ($_POST['confirm'] === $user->lang['YES']) indvdum@0: { indvdum@0: $confirm = true; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: if ($check && $confirm) indvdum@0: { indvdum@0: $user_id = request_var('confirm_uid', 0); indvdum@0: $session_id = request_var('sess', ''); indvdum@0: $confirm_key = request_var('confirm_key', ''); indvdum@0: indvdum@0: if ($user_id != $user->data['user_id'] || $session_id != $user->session_id || !$confirm_key || !$user->data['user_last_confirm_key'] || $confirm_key != $user->data['user_last_confirm_key']) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: // Reset user_last_confirm_key indvdum@0: $sql = 'UPDATE ' . USERS_TABLE . " SET user_last_confirm_key = '' indvdum@0: WHERE user_id = " . $user->data['user_id']; indvdum@0: $db->sql_query($sql); indvdum@0: indvdum@0: return true; indvdum@0: } indvdum@0: else if ($check) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: $s_hidden_fields = build_hidden_fields(array( indvdum@0: 'confirm_uid' => $user->data['user_id'], indvdum@0: 'sess' => $user->session_id, indvdum@0: 'sid' => $user->session_id, indvdum@0: )); indvdum@0: indvdum@0: // generate activation key indvdum@0: $confirm_key = gen_rand_string(10); indvdum@0: indvdum@0: if (defined('IN_ADMIN') && isset($user->data['session_admin']) && $user->data['session_admin']) indvdum@0: { indvdum@0: adm_page_header((!isset($user->lang[$title])) ? $user->lang['CONFIRM'] : $user->lang[$title]); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: page_header(((!isset($user->lang[$title])) ? $user->lang['CONFIRM'] : $user->lang[$title]), false); indvdum@0: } indvdum@0: indvdum@0: $template->set_filenames(array( indvdum@0: 'body' => $html_body) indvdum@0: ); indvdum@0: indvdum@0: // If activation key already exist, we better do not re-use the key (something very strange is going on...) indvdum@0: if (request_var('confirm_key', '')) indvdum@0: { indvdum@0: // This should not occur, therefore we cancel the operation to safe the user indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: // re-add sid / transform & to & for user->page (user->page is always using &) indvdum@0: $use_page = ($u_action) ? $phpbb_root_path . $u_action : $phpbb_root_path . str_replace('&', '&', $user->page['page']); indvdum@0: $u_action = reapply_sid($use_page); indvdum@0: $u_action .= ((strpos($u_action, '?') === false) ? '?' : '&') . 'confirm_key=' . $confirm_key; indvdum@0: indvdum@0: $template->assign_vars(array( indvdum@0: 'MESSAGE_TITLE' => (!isset($user->lang[$title])) ? $user->lang['CONFIRM'] : $user->lang[$title], indvdum@0: 'MESSAGE_TEXT' => (!isset($user->lang[$title . '_CONFIRM'])) ? $title : $user->lang[$title . '_CONFIRM'], indvdum@0: indvdum@0: 'YES_VALUE' => $user->lang['YES'], indvdum@0: 'S_CONFIRM_ACTION' => $u_action, indvdum@0: 'S_HIDDEN_FIELDS' => $hidden . $s_hidden_fields) indvdum@0: ); indvdum@0: indvdum@0: $sql = 'UPDATE ' . USERS_TABLE . " SET user_last_confirm_key = '" . $db->sql_escape($confirm_key) . "' indvdum@0: WHERE user_id = " . $user->data['user_id']; indvdum@0: $db->sql_query($sql); indvdum@0: indvdum@0: if (defined('IN_ADMIN') && isset($user->data['session_admin']) && $user->data['session_admin']) indvdum@0: { indvdum@0: adm_page_footer(); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: page_footer(); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Generate login box or verify password indvdum@0: */ indvdum@0: function login_box($redirect = '', $l_explain = '', $l_success = '', $admin = false, $s_display = true) indvdum@0: { indvdum@0: global $db, $user, $template, $auth, $phpEx, $phpbb_root_path, $config; indvdum@0: indvdum@0: if (!class_exists('phpbb_captcha_factory')) indvdum@0: { indvdum@0: include($phpbb_root_path . 'includes/captcha/captcha_factory.' . $phpEx); indvdum@0: } indvdum@0: indvdum@0: $err = ''; indvdum@0: indvdum@0: // Make sure user->setup() has been called indvdum@0: if (empty($user->lang)) indvdum@0: { indvdum@0: $user->setup(); indvdum@0: } indvdum@0: indvdum@0: // Print out error if user tries to authenticate as an administrator without having the privileges... indvdum@0: if ($admin && !$auth->acl_get('a_')) indvdum@0: { indvdum@0: // Not authd indvdum@0: // anonymous/inactive users are never able to go to the ACP even if they have the relevant permissions indvdum@0: if ($user->data['is_registered']) indvdum@0: { indvdum@0: add_log('admin', 'LOG_ADMIN_AUTH_FAIL'); indvdum@0: } indvdum@0: trigger_error('NO_AUTH_ADMIN'); indvdum@0: } indvdum@0: indvdum@0: if (isset($_POST['login'])) indvdum@0: { indvdum@0: // Get credential indvdum@0: if ($admin) indvdum@0: { indvdum@0: $credential = request_var('credential', ''); indvdum@0: indvdum@0: if (strspn($credential, 'abcdef0123456789') !== strlen($credential) || strlen($credential) != 32) indvdum@0: { indvdum@0: if ($user->data['is_registered']) indvdum@0: { indvdum@0: add_log('admin', 'LOG_ADMIN_AUTH_FAIL'); indvdum@0: } indvdum@0: trigger_error('NO_AUTH_ADMIN'); indvdum@0: } indvdum@0: indvdum@0: $password = request_var('password_' . $credential, '', true); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $password = request_var('password', '', true); indvdum@0: } indvdum@0: indvdum@0: $username = request_var('username', '', true); indvdum@0: $autologin = (!empty($_POST['autologin'])) ? true : false; indvdum@0: $viewonline = (!empty($_POST['viewonline'])) ? 0 : 1; indvdum@0: $admin = ($admin) ? 1 : 0; indvdum@0: $viewonline = ($admin) ? $user->data['session_viewonline'] : $viewonline; indvdum@0: indvdum@0: // Check if the supplied username is equal to the one stored within the database if re-authenticating indvdum@0: if ($admin && utf8_clean_string($username) != utf8_clean_string($user->data['username'])) indvdum@0: { indvdum@0: // We log the attempt to use a different username... indvdum@0: add_log('admin', 'LOG_ADMIN_AUTH_FAIL'); indvdum@0: trigger_error('NO_AUTH_ADMIN_USER_DIFFER'); indvdum@0: } indvdum@0: indvdum@0: // If authentication is successful we redirect user to previous page indvdum@0: $result = $auth->login($username, $password, $autologin, $viewonline, $admin); indvdum@0: indvdum@0: // If admin authentication and login, we will log if it was a success or not... indvdum@0: // We also break the operation on the first non-success login - it could be argued that the user already knows indvdum@0: if ($admin) indvdum@0: { indvdum@0: if ($result['status'] == LOGIN_SUCCESS) indvdum@0: { indvdum@0: add_log('admin', 'LOG_ADMIN_AUTH_SUCCESS'); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // Only log the failed attempt if a real user tried to. indvdum@0: // anonymous/inactive users are never able to go to the ACP even if they have the relevant permissions indvdum@0: if ($user->data['is_registered']) indvdum@0: { indvdum@0: add_log('admin', 'LOG_ADMIN_AUTH_FAIL'); indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // The result parameter is always an array, holding the relevant information... indvdum@0: if ($result['status'] == LOGIN_SUCCESS) indvdum@0: { indvdum@0: $redirect = request_var('redirect', "{$phpbb_root_path}index.$phpEx"); indvdum@0: $message = ($l_success) ? $l_success : $user->lang['LOGIN_REDIRECT']; indvdum@0: $l_redirect = ($admin) ? $user->lang['PROCEED_TO_ACP'] : (($redirect === "{$phpbb_root_path}index.$phpEx" || $redirect === "index.$phpEx") ? $user->lang['RETURN_INDEX'] : $user->lang['RETURN_PAGE']); indvdum@0: indvdum@0: // append/replace SID (may change during the session for AOL users) indvdum@0: $redirect = reapply_sid($redirect); indvdum@0: indvdum@0: // Special case... the user is effectively banned, but we allow founders to login indvdum@0: if (defined('IN_CHECK_BAN') && $result['user_row']['user_type'] != USER_FOUNDER) indvdum@0: { indvdum@0: return; indvdum@0: } indvdum@0: indvdum@0: $redirect = meta_refresh(3, $redirect); indvdum@0: trigger_error($message . '

' . sprintf($l_redirect, '', '')); indvdum@0: } indvdum@0: indvdum@0: // Something failed, determine what... indvdum@0: if ($result['status'] == LOGIN_BREAK) indvdum@0: { indvdum@0: trigger_error($result['error_msg']); indvdum@0: } indvdum@0: indvdum@0: // Special cases... determine indvdum@0: switch ($result['status']) indvdum@0: { indvdum@0: case LOGIN_ERROR_ATTEMPTS: indvdum@0: indvdum@0: $captcha = phpbb_captcha_factory::get_instance($config['captcha_plugin']); indvdum@0: $captcha->init(CONFIRM_LOGIN); indvdum@0: // $captcha->reset(); indvdum@0: indvdum@0: $template->assign_vars(array( indvdum@0: 'CAPTCHA_TEMPLATE' => $captcha->get_template(), indvdum@0: )); indvdum@0: indvdum@0: $err = $user->lang[$result['error_msg']]; indvdum@0: break; indvdum@0: indvdum@0: case LOGIN_ERROR_PASSWORD_CONVERT: indvdum@0: $err = sprintf( indvdum@0: $user->lang[$result['error_msg']], indvdum@0: ($config['email_enable']) ? '' : '', indvdum@0: ($config['email_enable']) ? '' : '', indvdum@0: ($config['board_contact']) ? '' : '', indvdum@0: ($config['board_contact']) ? '' : '' indvdum@0: ); indvdum@0: break; indvdum@0: indvdum@0: // Username, password, etc... indvdum@0: default: indvdum@0: $err = $user->lang[$result['error_msg']]; indvdum@0: indvdum@0: // Assign admin contact to some error messages indvdum@0: if ($result['error_msg'] == 'LOGIN_ERROR_USERNAME' || $result['error_msg'] == 'LOGIN_ERROR_PASSWORD') indvdum@0: { indvdum@0: $err = (!$config['board_contact']) ? sprintf($user->lang[$result['error_msg']], '', '') : sprintf($user->lang[$result['error_msg']], '', ''); indvdum@0: } indvdum@0: indvdum@0: break; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // Assign credential for username/password pair indvdum@0: $credential = ($admin) ? md5(unique_id()) : false; indvdum@0: indvdum@0: $s_hidden_fields = array( indvdum@0: 'sid' => $user->session_id, indvdum@0: ); indvdum@0: indvdum@0: if ($redirect) indvdum@0: { indvdum@0: $s_hidden_fields['redirect'] = $redirect; indvdum@0: } indvdum@0: indvdum@0: if ($admin) indvdum@0: { indvdum@0: $s_hidden_fields['credential'] = $credential; indvdum@0: } indvdum@0: indvdum@0: $s_hidden_fields = build_hidden_fields($s_hidden_fields); indvdum@0: indvdum@0: $template->assign_vars(array( indvdum@0: 'LOGIN_ERROR' => $err, indvdum@0: 'LOGIN_EXPLAIN' => $l_explain, indvdum@0: indvdum@0: 'U_SEND_PASSWORD' => ($config['email_enable']) ? append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=sendpassword') : '', indvdum@0: 'U_RESEND_ACTIVATION' => ($config['require_activation'] == USER_ACTIVATION_SELF && $config['email_enable']) ? append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=resend_act') : '', indvdum@0: 'U_TERMS_USE' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=terms'), indvdum@0: 'U_PRIVACY' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=privacy'), indvdum@0: indvdum@0: 'S_DISPLAY_FULL_LOGIN' => ($s_display) ? true : false, indvdum@0: 'S_HIDDEN_FIELDS' => $s_hidden_fields, indvdum@0: indvdum@0: 'S_ADMIN_AUTH' => $admin, indvdum@0: 'USERNAME' => ($admin) ? $user->data['username'] : '', indvdum@0: indvdum@0: 'USERNAME_CREDENTIAL' => 'username', indvdum@0: 'PASSWORD_CREDENTIAL' => ($admin) ? 'password_' . $credential : 'password', indvdum@0: )); indvdum@0: indvdum@0: page_header($user->lang['LOGIN'], false); indvdum@0: indvdum@0: $template->set_filenames(array( indvdum@0: 'body' => 'login_body.html') indvdum@0: ); indvdum@0: make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx")); indvdum@0: indvdum@0: page_footer(); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Generate forum login box indvdum@0: */ indvdum@0: function login_forum_box($forum_data) indvdum@0: { indvdum@0: global $db, $config, $user, $template, $phpEx; indvdum@0: indvdum@0: $password = request_var('password', '', true); indvdum@0: indvdum@0: $sql = 'SELECT forum_id indvdum@0: FROM ' . FORUMS_ACCESS_TABLE . ' indvdum@0: WHERE forum_id = ' . $forum_data['forum_id'] . ' indvdum@0: AND user_id = ' . $user->data['user_id'] . " indvdum@0: AND session_id = '" . $db->sql_escape($user->session_id) . "'"; indvdum@0: $result = $db->sql_query($sql); indvdum@0: $row = $db->sql_fetchrow($result); indvdum@0: $db->sql_freeresult($result); indvdum@0: indvdum@0: if ($row) indvdum@0: { indvdum@0: return true; indvdum@0: } indvdum@0: indvdum@0: if ($password) indvdum@0: { indvdum@0: // Remove expired authorised sessions indvdum@0: $sql = 'SELECT f.session_id indvdum@0: FROM ' . FORUMS_ACCESS_TABLE . ' f indvdum@0: LEFT JOIN ' . SESSIONS_TABLE . ' s ON (f.session_id = s.session_id) indvdum@0: WHERE s.session_id IS NULL'; indvdum@0: $result = $db->sql_query($sql); indvdum@0: indvdum@0: if ($row = $db->sql_fetchrow($result)) indvdum@0: { indvdum@0: $sql_in = array(); indvdum@0: do indvdum@0: { indvdum@0: $sql_in[] = (string) $row['session_id']; indvdum@0: } indvdum@0: while ($row = $db->sql_fetchrow($result)); indvdum@0: indvdum@0: // Remove expired sessions indvdum@0: $sql = 'DELETE FROM ' . FORUMS_ACCESS_TABLE . ' indvdum@0: WHERE ' . $db->sql_in_set('session_id', $sql_in); indvdum@0: $db->sql_query($sql); indvdum@0: } indvdum@0: $db->sql_freeresult($result); indvdum@0: indvdum@0: if (phpbb_check_hash($password, $forum_data['forum_password'])) indvdum@0: { indvdum@0: $sql_ary = array( indvdum@0: 'forum_id' => (int) $forum_data['forum_id'], indvdum@0: 'user_id' => (int) $user->data['user_id'], indvdum@0: 'session_id' => (string) $user->session_id, indvdum@0: ); indvdum@0: indvdum@0: $db->sql_query('INSERT INTO ' . FORUMS_ACCESS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary)); indvdum@0: indvdum@0: return true; indvdum@0: } indvdum@0: indvdum@0: $template->assign_var('LOGIN_ERROR', $user->lang['WRONG_PASSWORD']); indvdum@0: } indvdum@0: indvdum@0: page_header($user->lang['LOGIN'], false); indvdum@0: indvdum@0: $template->assign_vars(array( indvdum@0: 'S_LOGIN_ACTION' => build_url(array('f')), indvdum@0: 'S_HIDDEN_FIELDS' => build_hidden_fields(array('f' => $forum_data['forum_id']))) indvdum@0: ); indvdum@0: indvdum@0: $template->set_filenames(array( indvdum@0: 'body' => 'login_forum.html') indvdum@0: ); indvdum@0: indvdum@0: page_footer(); indvdum@0: } indvdum@0: indvdum@0: // Little helpers indvdum@0: indvdum@0: /** indvdum@0: * Little helper for the build_hidden_fields function indvdum@0: */ indvdum@0: function _build_hidden_fields($key, $value, $specialchar, $stripslashes) indvdum@0: { indvdum@0: $hidden_fields = ''; indvdum@0: indvdum@0: if (!is_array($value)) indvdum@0: { indvdum@0: $value = ($stripslashes) ? stripslashes($value) : $value; indvdum@0: $value = ($specialchar) ? htmlspecialchars($value, ENT_COMPAT, 'UTF-8') : $value; indvdum@0: indvdum@0: $hidden_fields .= '' . "\n"; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: foreach ($value as $_key => $_value) indvdum@0: { indvdum@0: $_key = ($stripslashes) ? stripslashes($_key) : $_key; indvdum@0: $_key = ($specialchar) ? htmlspecialchars($_key, ENT_COMPAT, 'UTF-8') : $_key; indvdum@0: indvdum@0: $hidden_fields .= _build_hidden_fields($key . '[' . $_key . ']', $_value, $specialchar, $stripslashes); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: return $hidden_fields; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Build simple hidden fields from array indvdum@0: * indvdum@0: * @param array $field_ary an array of values to build the hidden field from indvdum@0: * @param bool $specialchar if true, keys and values get specialchared indvdum@0: * @param bool $stripslashes if true, keys and values get stripslashed indvdum@0: * indvdum@0: * @return string the hidden fields indvdum@0: */ indvdum@0: function build_hidden_fields($field_ary, $specialchar = false, $stripslashes = false) indvdum@0: { indvdum@0: $s_hidden_fields = ''; indvdum@0: indvdum@0: foreach ($field_ary as $name => $vars) indvdum@0: { indvdum@0: $name = ($stripslashes) ? stripslashes($name) : $name; indvdum@0: $name = ($specialchar) ? htmlspecialchars($name, ENT_COMPAT, 'UTF-8') : $name; indvdum@0: indvdum@0: $s_hidden_fields .= _build_hidden_fields($name, $vars, $specialchar, $stripslashes); indvdum@0: } indvdum@0: indvdum@0: return $s_hidden_fields; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Parse cfg file indvdum@0: */ indvdum@0: function parse_cfg_file($filename, $lines = false) indvdum@0: { indvdum@0: $parsed_items = array(); indvdum@0: indvdum@0: if ($lines === false) indvdum@0: { indvdum@0: $lines = file($filename); indvdum@0: } indvdum@0: indvdum@0: foreach ($lines as $line) indvdum@0: { indvdum@0: $line = trim($line); indvdum@0: indvdum@0: if (!$line || $line[0] == '#' || ($delim_pos = strpos($line, '=')) === false) indvdum@0: { indvdum@0: continue; indvdum@0: } indvdum@0: indvdum@0: // Determine first occurrence, since in values the equal sign is allowed indvdum@0: $key = strtolower(trim(substr($line, 0, $delim_pos))); indvdum@0: $value = trim(substr($line, $delim_pos + 1)); indvdum@0: indvdum@0: if (in_array($value, array('off', 'false', '0'))) indvdum@0: { indvdum@0: $value = false; indvdum@0: } indvdum@0: else if (in_array($value, array('on', 'true', '1'))) indvdum@0: { indvdum@0: $value = true; indvdum@0: } indvdum@0: else if (!trim($value)) indvdum@0: { indvdum@0: $value = ''; indvdum@0: } indvdum@0: else if (($value[0] == "'" && $value[sizeof($value) - 1] == "'") || ($value[0] == '"' && $value[sizeof($value) - 1] == '"')) indvdum@0: { indvdum@0: $value = substr($value, 1, sizeof($value)-2); indvdum@0: } indvdum@0: indvdum@0: $parsed_items[$key] = $value; indvdum@0: } indvdum@0: indvdum@0: return $parsed_items; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Add log event indvdum@0: */ indvdum@0: function add_log() indvdum@0: { indvdum@0: global $db, $user; indvdum@0: indvdum@0: // In phpBB 3.1.x i want to have logging in a class to be able to control it indvdum@0: // For now, we need a quite hakish approach to circumvent logging for some actions indvdum@0: // @todo implement cleanly indvdum@0: if (!empty($GLOBALS['skip_add_log'])) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: $args = func_get_args(); indvdum@0: indvdum@0: $mode = array_shift($args); indvdum@0: $reportee_id = ($mode == 'user') ? intval(array_shift($args)) : ''; indvdum@0: $forum_id = ($mode == 'mod') ? intval(array_shift($args)) : ''; indvdum@0: $topic_id = ($mode == 'mod') ? intval(array_shift($args)) : ''; indvdum@0: $action = array_shift($args); indvdum@0: $data = (!sizeof($args)) ? '' : serialize($args); indvdum@0: indvdum@0: $sql_ary = array( indvdum@0: 'user_id' => (empty($user->data)) ? ANONYMOUS : $user->data['user_id'], indvdum@0: 'log_ip' => $user->ip, indvdum@0: 'log_time' => time(), indvdum@0: 'log_operation' => $action, indvdum@0: 'log_data' => $data, indvdum@0: ); indvdum@0: indvdum@0: switch ($mode) indvdum@0: { indvdum@0: case 'admin': indvdum@0: $sql_ary['log_type'] = LOG_ADMIN; indvdum@0: break; indvdum@0: indvdum@0: case 'mod': indvdum@0: $sql_ary += array( indvdum@0: 'log_type' => LOG_MOD, indvdum@0: 'forum_id' => $forum_id, indvdum@0: 'topic_id' => $topic_id indvdum@0: ); indvdum@0: break; indvdum@0: indvdum@0: case 'user': indvdum@0: $sql_ary += array( indvdum@0: 'log_type' => LOG_USERS, indvdum@0: 'reportee_id' => $reportee_id indvdum@0: ); indvdum@0: break; indvdum@0: indvdum@0: case 'critical': indvdum@0: $sql_ary['log_type'] = LOG_CRITICAL; indvdum@0: break; indvdum@0: indvdum@0: default: indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: $db->sql_query('INSERT INTO ' . LOG_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary)); indvdum@0: indvdum@0: return $db->sql_nextid(); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Return a nicely formatted backtrace. indvdum@0: * indvdum@0: * Turns the array returned by debug_backtrace() into HTML markup. indvdum@0: * Also filters out absolute paths to phpBB root. indvdum@0: * indvdum@0: * @return string HTML markup indvdum@0: */ indvdum@0: function get_backtrace() indvdum@0: { indvdum@0: $output = '
'; indvdum@0: $backtrace = debug_backtrace(); indvdum@0: indvdum@0: // We skip the first one, because it only shows this file/function indvdum@0: unset($backtrace[0]); indvdum@0: indvdum@0: foreach ($backtrace as $trace) indvdum@0: { indvdum@0: // Strip the current directory from path indvdum@0: $trace['file'] = (empty($trace['file'])) ? '(not given by php)' : htmlspecialchars(phpbb_filter_root_path($trace['file'])); indvdum@0: $trace['line'] = (empty($trace['line'])) ? '(not given by php)' : $trace['line']; indvdum@0: indvdum@0: // Only show function arguments for include etc. indvdum@0: // Other parameters may contain sensible information indvdum@0: $argument = ''; indvdum@0: if (!empty($trace['args'][0]) && in_array($trace['function'], array('include', 'require', 'include_once', 'require_once'))) indvdum@0: { indvdum@0: $argument = htmlspecialchars(phpbb_filter_root_path($trace['args'][0])); indvdum@0: } indvdum@0: indvdum@0: $trace['class'] = (!isset($trace['class'])) ? '' : $trace['class']; indvdum@0: $trace['type'] = (!isset($trace['type'])) ? '' : $trace['type']; indvdum@0: indvdum@0: $output .= '
'; indvdum@0: $output .= 'FILE: ' . $trace['file'] . '
'; indvdum@0: $output .= 'LINE: ' . ((!empty($trace['line'])) ? $trace['line'] : '') . '
'; indvdum@0: indvdum@0: $output .= 'CALL: ' . htmlspecialchars($trace['class'] . $trace['type'] . $trace['function']); indvdum@0: $output .= '(' . (($argument !== '') ? "'$argument'" : '') . ')
'; indvdum@0: } indvdum@0: $output .= '
'; indvdum@0: return $output; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * This function returns a regular expression pattern for commonly used expressions indvdum@0: * Use with / as delimiter for email mode and # for url modes indvdum@0: * mode can be: email|bbcode_htm|url|url_inline|www_url|www_url_inline|relative_url|relative_url_inline|ipv4|ipv6 indvdum@0: */ indvdum@0: function get_preg_expression($mode) indvdum@0: { indvdum@0: switch ($mode) indvdum@0: { indvdum@0: case 'email': indvdum@0: // Regex written by James Watts and Francisco Jose Martin Moreno indvdum@0: // http://fightingforalostcause.net/misc/2006/compare-email-regex.php indvdum@0: return '([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*(?:[\w\!\#$\%\'\*\+\-\/\=\?\^\`{\|\}\~]|&)+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)'; indvdum@0: break; indvdum@0: indvdum@0: case 'bbcode_htm': indvdum@0: return array( indvdum@0: '#.*?#', indvdum@0: '#.*?#', indvdum@0: '#.*?#', indvdum@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; indvdum@0: } indvdum@0: indvdum@0: // Unescape the asterisk to simplify further conversions indvdum@0: $word = str_replace('\*', '*', preg_quote($word, '#')); indvdum@0: indvdum@0: if ($use_unicode && $unicode_support) indvdum@0: { indvdum@0: // Replace asterisk(s) inside the pattern, at the start and at the end of it with regexes indvdum@0: $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); indvdum@0: indvdum@0: // Generate the final substitution indvdum@0: $preg_expr = '#(?=')) && indvdum@0: ($type != 'AAAA' || version_compare(PHP_VERSION, '5.0.0', '>=')) && indvdum@0: ($type != 'TXT' || version_compare(PHP_VERSION, '5.2.4', '>=')) && indvdum@0: function_exists('checkdnsrr') indvdum@0: ) indvdum@0: { indvdum@0: return checkdnsrr($host_fqdn, $type); indvdum@0: } indvdum@0: indvdum@0: // dns_get_record() is available since PHP 5; since PHP 5.3 also on Windows, indvdum@0: // but on Windows it does not work reliable for AAAA records before PHP 5.3.1 indvdum@0: indvdum@0: // Call dns_get_record() if indvdum@0: // we're not looking for an AAAA record or indvdum@0: // we're not on Windows or indvdum@0: // we're running a PHP version where AAAA lookups work reliable indvdum@0: if ( indvdum@0: ($type != 'AAAA' || DIRECTORY_SEPARATOR != '\\' || version_compare(PHP_VERSION, '5.3.1', '>=')) && indvdum@0: function_exists('dns_get_record') indvdum@0: ) indvdum@0: { indvdum@0: // dns_get_record() expects an integer as second parameter indvdum@0: // We have to convert the string $type to the corresponding integer constant. indvdum@0: $type_constant = 'DNS_' . $type; indvdum@0: $type_param = (defined($type_constant)) ? constant($type_constant) : DNS_ANY; indvdum@0: indvdum@0: // dns_get_record() might throw E_WARNING and return false for records that do not exist indvdum@0: $resultset = @dns_get_record($host_fqdn, $type_param); indvdum@0: indvdum@0: if (empty($resultset) || !is_array($resultset)) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: else if ($type_param == DNS_ANY) indvdum@0: { indvdum@0: // $resultset is a non-empty array indvdum@0: return true; indvdum@0: } indvdum@0: indvdum@0: foreach ($resultset as $result) indvdum@0: { indvdum@0: if ( indvdum@0: isset($result['host']) && $result['host'] == $host && indvdum@0: isset($result['type']) && $result['type'] == $type indvdum@0: ) indvdum@0: { indvdum@0: return true; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: // If we're on Windows we can still try to call nslookup via exec() as a last resort indvdum@0: if (DIRECTORY_SEPARATOR == '\\' && function_exists('exec')) indvdum@0: { indvdum@0: @exec('nslookup -type=' . escapeshellarg($type) . ' ' . escapeshellarg($host_fqdn), $output); indvdum@0: indvdum@0: // If output is empty, the nslookup failed indvdum@0: if (empty($output)) indvdum@0: { indvdum@0: return NULL; indvdum@0: } indvdum@0: indvdum@0: foreach ($output as $line) indvdum@0: { indvdum@0: $line = trim($line); indvdum@0: indvdum@0: if (empty($line)) indvdum@0: { indvdum@0: continue; indvdum@0: } indvdum@0: indvdum@0: // Squash tabs and multiple whitespaces to a single whitespace. indvdum@0: $line = preg_replace('/\s+/', ' ', $line); indvdum@0: indvdum@0: switch ($type) indvdum@0: { indvdum@0: case 'MX': indvdum@0: if (stripos($line, "$host MX") === 0) indvdum@0: { indvdum@0: return true; indvdum@0: } indvdum@0: break; indvdum@0: indvdum@0: case 'NS': indvdum@0: if (stripos($line, "$host nameserver") === 0) indvdum@0: { indvdum@0: return true; indvdum@0: } indvdum@0: break; indvdum@0: indvdum@0: case 'TXT': indvdum@0: if (stripos($line, "$host text") === 0) indvdum@0: { indvdum@0: return true; indvdum@0: } indvdum@0: break; indvdum@0: indvdum@0: case 'CNAME': indvdum@0: if (stripos($line, "$host canonical name") === 0) indvdum@0: { indvdum@0: return true; indvdum@0: } indvdum@0: break; indvdum@0: indvdum@0: default: indvdum@0: case 'AAAA': indvdum@0: // AAAA records returned by nslookup on Windows XP/2003 have this format. indvdum@0: // Later Windows versions use the A record format below for AAAA records. indvdum@0: if (stripos($line, "$host AAAA IPv6 address") === 0) indvdum@0: { indvdum@0: return true; indvdum@0: } indvdum@0: // No break indvdum@0: indvdum@0: case 'A': indvdum@0: if (!empty($host_matches)) indvdum@0: { indvdum@0: // Second line indvdum@0: if (stripos($line, "Address: ") === 0) indvdum@0: { indvdum@0: return true; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $host_matches = false; indvdum@0: } indvdum@0: } indvdum@0: else if (stripos($line, "Name: $host") === 0) indvdum@0: { indvdum@0: // First line indvdum@0: $host_matches = true; indvdum@0: } indvdum@0: break; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: return NULL; indvdum@0: } indvdum@0: indvdum@0: // Handler, header and footer indvdum@0: indvdum@0: /** indvdum@0: * Error and message handler, call with trigger_error if reqd indvdum@0: */ indvdum@0: function msg_handler($errno, $msg_text, $errfile, $errline) indvdum@0: { indvdum@0: global $cache, $db, $auth, $template, $config, $user; indvdum@0: global $phpEx, $phpbb_root_path, $msg_title, $msg_long_text; indvdum@0: indvdum@0: // Do not display notices if we suppress them via @ indvdum@0: if (error_reporting() == 0 && $errno != E_USER_ERROR && $errno != E_USER_WARNING && $errno != E_USER_NOTICE) indvdum@0: { indvdum@0: return; indvdum@0: } indvdum@0: indvdum@0: // Message handler is stripping text. In case we need it, we are possible to define long text... indvdum@0: if (isset($msg_long_text) && $msg_long_text && !$msg_text) indvdum@0: { indvdum@0: $msg_text = $msg_long_text; indvdum@0: } indvdum@0: indvdum@0: if (!defined('E_DEPRECATED')) indvdum@0: { indvdum@0: define('E_DEPRECATED', 8192); indvdum@0: } indvdum@0: indvdum@0: switch ($errno) indvdum@0: { indvdum@0: case E_NOTICE: indvdum@0: case E_WARNING: indvdum@0: indvdum@0: // Check the error reporting level and return if the error level does not match indvdum@0: // If DEBUG is defined the default level is E_ALL indvdum@0: if (($errno & ((defined('DEBUG')) ? E_ALL : error_reporting())) == 0) indvdum@0: { indvdum@0: return; indvdum@0: } indvdum@0: indvdum@0: if (strpos($errfile, 'cache') === false && strpos($errfile, 'template.') === false) indvdum@0: { indvdum@0: $errfile = phpbb_filter_root_path($errfile); indvdum@0: $msg_text = phpbb_filter_root_path($msg_text); indvdum@0: $error_name = ($errno === E_WARNING) ? 'PHP Warning' : 'PHP Notice'; indvdum@0: echo '[phpBB Debug] ' . $error_name . ': in file ' . $errfile . ' on line ' . $errline . ': ' . $msg_text . '
' . "\n"; indvdum@0: indvdum@0: // we are writing an image - the user won't see the debug, so let's place it in the log indvdum@0: if (defined('IMAGE_OUTPUT') || defined('IN_CRON')) indvdum@0: { indvdum@0: add_log('critical', 'LOG_IMAGE_GENERATION_ERROR', $errfile, $errline, $msg_text); indvdum@0: } indvdum@0: // echo '

BACKTRACE
' . get_backtrace() . '
' . "\n"; indvdum@0: } indvdum@0: indvdum@0: return; indvdum@0: indvdum@0: break; indvdum@0: indvdum@0: case E_USER_ERROR: indvdum@0: indvdum@0: if (!empty($user) && !empty($user->lang)) indvdum@0: { indvdum@0: $msg_text = (!empty($user->lang[$msg_text])) ? $user->lang[$msg_text] : $msg_text; indvdum@0: $msg_title = (!isset($msg_title)) ? $user->lang['GENERAL_ERROR'] : ((!empty($user->lang[$msg_title])) ? $user->lang[$msg_title] : $msg_title); indvdum@0: indvdum@0: $l_return_index = sprintf($user->lang['RETURN_INDEX'], '', ''); indvdum@0: $l_notify = ''; indvdum@0: indvdum@0: if (!empty($config['board_contact'])) indvdum@0: { indvdum@0: $l_notify = '

' . sprintf($user->lang['NOTIFY_ADMIN_EMAIL'], $config['board_contact']) . '

'; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $msg_title = 'General Error'; indvdum@0: $l_return_index = 'Return to index page'; indvdum@0: $l_notify = ''; indvdum@0: indvdum@0: if (!empty($config['board_contact'])) indvdum@0: { indvdum@0: $l_notify = '

Please notify the board administrator or webmaster: ' . $config['board_contact'] . '

'; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: if ((defined('DEBUG') || defined('IN_CRON') || defined('IMAGE_OUTPUT')) && isset($db)) indvdum@0: { indvdum@0: // let's avoid loops indvdum@0: $db->sql_return_on_error(true); indvdum@0: add_log('critical', 'LOG_GENERAL_ERROR', $msg_title, $msg_text); indvdum@0: $db->sql_return_on_error(false); indvdum@0: } indvdum@0: indvdum@0: // Do not send 200 OK, but service unavailable on errors indvdum@0: send_status_line(503, 'Service Unavailable'); indvdum@0: indvdum@0: garbage_collection(); indvdum@0: indvdum@0: // Try to not call the adm page data... indvdum@0: indvdum@0: echo ''; indvdum@0: echo ''; indvdum@0: echo ''; indvdum@0: echo ''; indvdum@0: echo '' . $msg_title . ''; indvdum@0: echo ''; indvdum@0: echo ''; indvdum@0: echo ''; indvdum@0: echo '
'; indvdum@0: echo ' '; indvdum@0: echo '
'; indvdum@0: echo '
'; indvdum@0: echo '
'; indvdum@0: echo '

' . $msg_title . '

'; indvdum@0: indvdum@0: echo '
' . $msg_text . '
'; indvdum@0: indvdum@0: echo $l_notify; indvdum@0: indvdum@0: echo '
'; indvdum@0: echo '
'; indvdum@0: echo '
'; indvdum@0: echo ' '; indvdum@0: echo '
'; indvdum@0: echo ''; indvdum@0: echo ''; indvdum@0: indvdum@0: exit_handler(); indvdum@0: indvdum@0: // On a fatal error (and E_USER_ERROR *is* fatal) we never want other scripts to continue and force an exit here. indvdum@0: exit; indvdum@0: break; indvdum@0: indvdum@0: case E_USER_WARNING: indvdum@0: case E_USER_NOTICE: indvdum@0: indvdum@0: define('IN_ERROR_HANDLER', true); indvdum@0: indvdum@0: if (empty($user->data)) indvdum@0: { indvdum@0: $user->session_begin(); indvdum@0: } indvdum@0: indvdum@0: // We re-init the auth array to get correct results on login/logout indvdum@0: $auth->acl($user->data); indvdum@0: indvdum@0: if (empty($user->lang)) indvdum@0: { indvdum@0: $user->setup(); indvdum@0: } indvdum@0: indvdum@0: if ($msg_text == 'ERROR_NO_ATTACHMENT' || $msg_text == 'NO_FORUM' || $msg_text == 'NO_TOPIC' || $msg_text == 'NO_USER') indvdum@0: { indvdum@0: send_status_line(404, 'Not Found'); indvdum@0: } indvdum@0: indvdum@0: $msg_text = (!empty($user->lang[$msg_text])) ? $user->lang[$msg_text] : $msg_text; indvdum@0: $msg_title = (!isset($msg_title)) ? $user->lang['INFORMATION'] : ((!empty($user->lang[$msg_title])) ? $user->lang[$msg_title] : $msg_title); indvdum@0: indvdum@0: if (!defined('HEADER_INC')) indvdum@0: { indvdum@0: if (defined('IN_ADMIN') && isset($user->data['session_admin']) && $user->data['session_admin']) indvdum@0: { indvdum@0: adm_page_header($msg_title); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: page_header($msg_title, false); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $template->set_filenames(array( indvdum@0: 'body' => 'message_body.html') indvdum@0: ); indvdum@0: indvdum@0: $template->assign_vars(array( indvdum@0: 'MESSAGE_TITLE' => $msg_title, indvdum@0: 'MESSAGE_TEXT' => $msg_text, indvdum@0: 'S_USER_WARNING' => ($errno == E_USER_WARNING) ? true : false, indvdum@0: 'S_USER_NOTICE' => ($errno == E_USER_NOTICE) ? true : false) indvdum@0: ); indvdum@0: indvdum@0: // We do not want the cron script to be called on error messages indvdum@0: define('IN_CRON', true); indvdum@0: indvdum@0: if (defined('IN_ADMIN') && isset($user->data['session_admin']) && $user->data['session_admin']) indvdum@0: { indvdum@0: adm_page_footer(); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: page_footer(); indvdum@0: } indvdum@0: indvdum@0: exit_handler(); indvdum@0: break; indvdum@0: indvdum@0: // PHP4 compatibility indvdum@0: case E_DEPRECATED: indvdum@0: return true; indvdum@0: break; indvdum@0: } indvdum@0: indvdum@0: // If we notice an error not handled here we pass this back to PHP by returning false indvdum@0: // This may not work for all php versions indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Removes absolute path to phpBB root directory from error messages indvdum@0: * and converts backslashes to forward slashes. indvdum@0: * indvdum@0: * @param string $errfile Absolute file path indvdum@0: * (e.g. /var/www/phpbb3/phpBB/includes/functions.php) indvdum@0: * Please note that if $errfile is outside of the phpBB root, indvdum@0: * the root path will not be found and can not be filtered. indvdum@0: * @return string Relative file path indvdum@0: * (e.g. /includes/functions.php) indvdum@0: */ indvdum@0: function phpbb_filter_root_path($errfile) indvdum@0: { indvdum@0: static $root_path; indvdum@0: indvdum@0: if (empty($root_path)) indvdum@0: { indvdum@0: $root_path = phpbb_realpath(dirname(__FILE__) . '/../'); indvdum@0: } indvdum@0: indvdum@0: return str_replace(array($root_path, '\\'), array('[ROOT]', '/'), $errfile); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Queries the session table to get information about online guests indvdum@0: * @param int $item_id Limits the search to the item with this id indvdum@0: * @param string $item The name of the item which is stored in the session table as session_{$item}_id indvdum@0: * @return int The number of active distinct guest sessions indvdum@0: */ indvdum@0: function obtain_guest_count($item_id = 0, $item = 'forum') indvdum@0: { indvdum@0: global $db, $config; indvdum@0: indvdum@0: if ($item_id) indvdum@0: { indvdum@0: $reading_sql = ' AND s.session_' . $item . '_id = ' . (int) $item_id; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $reading_sql = ''; indvdum@0: } indvdum@0: $time = (time() - (intval($config['load_online_time']) * 60)); indvdum@0: indvdum@0: // Get number of online guests indvdum@0: indvdum@0: if ($db->sql_layer === 'sqlite') indvdum@0: { indvdum@0: $sql = 'SELECT COUNT(session_ip) as num_guests indvdum@0: FROM ( indvdum@0: SELECT DISTINCT s.session_ip indvdum@0: FROM ' . SESSIONS_TABLE . ' s indvdum@0: WHERE s.session_user_id = ' . ANONYMOUS . ' indvdum@0: AND s.session_time >= ' . ($time - ((int) ($time % 60))) . indvdum@0: $reading_sql . indvdum@0: ')'; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $sql = 'SELECT COUNT(DISTINCT s.session_ip) as num_guests indvdum@0: FROM ' . SESSIONS_TABLE . ' s indvdum@0: WHERE s.session_user_id = ' . ANONYMOUS . ' indvdum@0: AND s.session_time >= ' . ($time - ((int) ($time % 60))) . indvdum@0: $reading_sql; indvdum@0: } indvdum@0: $result = $db->sql_query($sql); indvdum@0: $guests_online = (int) $db->sql_fetchfield('num_guests'); indvdum@0: $db->sql_freeresult($result); indvdum@0: indvdum@0: return $guests_online; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Queries the session table to get information about online users indvdum@0: * @param int $item_id Limits the search to the item with this id indvdum@0: * @param string $item The name of the item which is stored in the session table as session_{$item}_id indvdum@0: * @return array An array containing the ids of online, hidden and visible users, as well as statistical info indvdum@0: */ indvdum@0: function obtain_users_online($item_id = 0, $item = 'forum') indvdum@0: { indvdum@0: global $db, $config, $user; indvdum@0: indvdum@0: $reading_sql = ''; indvdum@0: if ($item_id !== 0) indvdum@0: { indvdum@0: $reading_sql = ' AND s.session_' . $item . '_id = ' . (int) $item_id; indvdum@0: } indvdum@0: indvdum@0: $online_users = array( indvdum@0: 'online_users' => array(), indvdum@0: 'hidden_users' => array(), indvdum@0: 'total_online' => 0, indvdum@0: 'visible_online' => 0, indvdum@0: 'hidden_online' => 0, indvdum@0: 'guests_online' => 0, indvdum@0: ); indvdum@0: indvdum@0: if ($config['load_online_guests']) indvdum@0: { indvdum@0: $online_users['guests_online'] = obtain_guest_count($item_id, $item); indvdum@0: } indvdum@0: indvdum@0: // a little discrete magic to cache this for 30 seconds indvdum@0: $time = (time() - (intval($config['load_online_time']) * 60)); indvdum@0: indvdum@0: $sql = 'SELECT s.session_user_id, s.session_ip, s.session_viewonline indvdum@0: FROM ' . SESSIONS_TABLE . ' s indvdum@0: WHERE s.session_time >= ' . ($time - ((int) ($time % 30))) . indvdum@0: $reading_sql . indvdum@0: ' AND s.session_user_id <> ' . ANONYMOUS; indvdum@0: $result = $db->sql_query($sql); indvdum@0: indvdum@0: while ($row = $db->sql_fetchrow($result)) indvdum@0: { indvdum@0: // Skip multiple sessions for one user indvdum@0: if (!isset($online_users['online_users'][$row['session_user_id']])) indvdum@0: { indvdum@0: $online_users['online_users'][$row['session_user_id']] = (int) $row['session_user_id']; indvdum@0: if ($row['session_viewonline']) indvdum@0: { indvdum@0: $online_users['visible_online']++; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $online_users['hidden_users'][$row['session_user_id']] = (int) $row['session_user_id']; indvdum@0: $online_users['hidden_online']++; indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: $online_users['total_online'] = $online_users['guests_online'] + $online_users['visible_online'] + $online_users['hidden_online']; indvdum@0: $db->sql_freeresult($result); indvdum@0: indvdum@0: return $online_users; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Uses the result of obtain_users_online to generate a localized, readable representation. indvdum@0: * @param mixed $online_users result of obtain_users_online - array with user_id lists for total, hidden and visible users, and statistics indvdum@0: * @param int $item_id Indicate that the data is limited to one item and not global indvdum@0: * @param string $item The name of the item which is stored in the session table as session_{$item}_id indvdum@0: * @return array An array containing the string for output to the template indvdum@0: */ indvdum@0: function obtain_users_online_string($online_users, $item_id = 0, $item = 'forum') indvdum@0: { indvdum@0: global $config, $db, $user, $auth; indvdum@0: indvdum@0: $user_online_link = $online_userlist = ''; indvdum@0: // Need caps version of $item for language-strings indvdum@0: $item_caps = strtoupper($item); indvdum@0: indvdum@0: if (sizeof($online_users['online_users'])) indvdum@0: { indvdum@0: $sql = 'SELECT username, username_clean, user_id, user_type, user_allow_viewonline, user_colour indvdum@0: FROM ' . USERS_TABLE . ' indvdum@0: WHERE ' . $db->sql_in_set('user_id', $online_users['online_users']) . ' indvdum@0: ORDER BY username_clean ASC'; indvdum@0: $result = $db->sql_query($sql); indvdum@0: indvdum@0: while ($row = $db->sql_fetchrow($result)) indvdum@0: { indvdum@0: // User is logged in and therefore not a guest indvdum@0: if ($row['user_id'] != ANONYMOUS) indvdum@0: { indvdum@0: if (isset($online_users['hidden_users'][$row['user_id']])) indvdum@0: { indvdum@0: $row['username'] = '' . $row['username'] . ''; indvdum@0: } indvdum@0: indvdum@0: if (!isset($online_users['hidden_users'][$row['user_id']]) || $auth->acl_get('u_viewonline')) indvdum@0: { indvdum@0: $user_online_link = get_username_string(($row['user_type'] <> USER_IGNORE) ? 'full' : 'no_profile', $row['user_id'], $row['username'], $row['user_colour']); indvdum@0: $online_userlist .= ($online_userlist != '') ? ', ' . $user_online_link : $user_online_link; indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: $db->sql_freeresult($result); indvdum@0: } indvdum@0: indvdum@0: if (!$online_userlist) indvdum@0: { indvdum@0: $online_userlist = $user->lang['NO_ONLINE_USERS']; indvdum@0: } indvdum@0: indvdum@0: if ($item_id === 0) indvdum@0: { indvdum@0: $online_userlist = $user->lang['REGISTERED_USERS'] . ' ' . $online_userlist; indvdum@0: } indvdum@0: else if ($config['load_online_guests']) indvdum@0: { indvdum@0: $l_online = ($online_users['guests_online'] === 1) ? $user->lang['BROWSING_' . $item_caps . '_GUEST'] : $user->lang['BROWSING_' . $item_caps . '_GUESTS']; indvdum@0: $online_userlist = sprintf($l_online, $online_userlist, $online_users['guests_online']); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $online_userlist = sprintf($user->lang['BROWSING_' . $item_caps], $online_userlist); indvdum@0: } indvdum@0: // Build online listing indvdum@0: $vars_online = array( indvdum@0: 'ONLINE' => array('total_online', 'l_t_user_s', 0), indvdum@0: 'REG' => array('visible_online', 'l_r_user_s', !$config['load_online_guests']), indvdum@0: 'HIDDEN' => array('hidden_online', 'l_h_user_s', $config['load_online_guests']), indvdum@0: 'GUEST' => array('guests_online', 'l_g_user_s', 0) indvdum@0: ); indvdum@0: indvdum@0: foreach ($vars_online as $l_prefix => $var_ary) indvdum@0: { indvdum@0: if ($var_ary[2]) indvdum@0: { indvdum@0: $l_suffix = '_AND'; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $l_suffix = ''; indvdum@0: } indvdum@0: switch ($online_users[$var_ary[0]]) indvdum@0: { indvdum@0: case 0: indvdum@0: ${$var_ary[1]} = $user->lang[$l_prefix . '_USERS_ZERO_TOTAL' . $l_suffix]; indvdum@0: break; indvdum@0: indvdum@0: case 1: indvdum@0: ${$var_ary[1]} = $user->lang[$l_prefix . '_USER_TOTAL' . $l_suffix]; indvdum@0: break; indvdum@0: indvdum@0: default: indvdum@0: ${$var_ary[1]} = $user->lang[$l_prefix . '_USERS_TOTAL' . $l_suffix]; indvdum@0: break; indvdum@0: } indvdum@0: } indvdum@0: unset($vars_online); indvdum@0: indvdum@0: $l_online_users = sprintf($l_t_user_s, $online_users['total_online']); indvdum@0: $l_online_users .= sprintf($l_r_user_s, $online_users['visible_online']); indvdum@0: $l_online_users .= sprintf($l_h_user_s, $online_users['hidden_online']); indvdum@0: indvdum@0: if ($config['load_online_guests']) indvdum@0: { indvdum@0: $l_online_users .= sprintf($l_g_user_s, $online_users['guests_online']); indvdum@0: } indvdum@0: indvdum@0: indvdum@0: indvdum@0: return array( indvdum@0: 'online_userlist' => $online_userlist, indvdum@0: 'l_online_users' => $l_online_users, indvdum@0: ); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Get option bitfield from custom data indvdum@0: * indvdum@0: * @param int $bit The bit/value to get indvdum@0: * @param int $data Current bitfield to check indvdum@0: * @return bool Returns true if value of constant is set in bitfield, else false indvdum@0: */ indvdum@0: function phpbb_optionget($bit, $data) indvdum@0: { indvdum@0: return ($data & 1 << (int) $bit) ? true : false; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Set option bitfield indvdum@0: * indvdum@0: * @param int $bit The bit/value to set/unset indvdum@0: * @param bool $set True if option should be set, false if option should be unset. indvdum@0: * @param int $data Current bitfield to change indvdum@0: * indvdum@0: * @return int The new bitfield indvdum@0: */ indvdum@0: function phpbb_optionset($bit, $set, $data) indvdum@0: { indvdum@0: if ($set && !($data & 1 << $bit)) indvdum@0: { indvdum@0: $data += 1 << $bit; indvdum@0: } indvdum@0: else if (!$set && ($data & 1 << $bit)) indvdum@0: { indvdum@0: $data -= 1 << $bit; indvdum@0: } indvdum@0: indvdum@0: return $data; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Login using http authenticate. indvdum@0: * indvdum@0: * @param array $param Parameter array, see $param_defaults array. indvdum@0: * indvdum@0: * @return void indvdum@0: */ indvdum@0: function phpbb_http_login($param) indvdum@0: { indvdum@0: global $auth, $user; indvdum@0: global $config; indvdum@0: indvdum@0: $param_defaults = array( indvdum@0: 'auth_message' => '', indvdum@0: indvdum@0: 'autologin' => false, indvdum@0: 'viewonline' => true, indvdum@0: 'admin' => false, indvdum@0: ); indvdum@0: indvdum@0: // Overwrite default values with passed values indvdum@0: $param = array_merge($param_defaults, $param); indvdum@0: indvdum@0: // User is already logged in indvdum@0: // We will not overwrite his session indvdum@0: if (!empty($user->data['is_registered'])) indvdum@0: { indvdum@0: return; indvdum@0: } indvdum@0: indvdum@0: // $_SERVER keys to check indvdum@0: $username_keys = array( indvdum@0: 'PHP_AUTH_USER', indvdum@0: 'Authorization', indvdum@0: 'REMOTE_USER', 'REDIRECT_REMOTE_USER', indvdum@0: 'HTTP_AUTHORIZATION', 'REDIRECT_HTTP_AUTHORIZATION', indvdum@0: 'REMOTE_AUTHORIZATION', 'REDIRECT_REMOTE_AUTHORIZATION', indvdum@0: 'AUTH_USER', indvdum@0: ); indvdum@0: indvdum@0: $password_keys = array( indvdum@0: 'PHP_AUTH_PW', indvdum@0: 'REMOTE_PASSWORD', indvdum@0: 'AUTH_PASSWORD', indvdum@0: ); indvdum@0: indvdum@0: $username = null; indvdum@0: foreach ($username_keys as $k) indvdum@0: { indvdum@0: if (isset($_SERVER[$k])) indvdum@0: { indvdum@0: $username = $_SERVER[$k]; indvdum@0: break; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $password = null; indvdum@0: foreach ($password_keys as $k) indvdum@0: { indvdum@0: if (isset($_SERVER[$k])) indvdum@0: { indvdum@0: $password = $_SERVER[$k]; indvdum@0: break; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // Decode encoded information (IIS, CGI, FastCGI etc.) indvdum@0: if (!is_null($username) && is_null($password) && strpos($username, 'Basic ') === 0) indvdum@0: { indvdum@0: list($username, $password) = explode(':', base64_decode(substr($username, 6)), 2); indvdum@0: } indvdum@0: indvdum@0: if (!is_null($username) && !is_null($password)) indvdum@0: { indvdum@0: set_var($username, $username, 'string', true); indvdum@0: set_var($password, $password, 'string', true); indvdum@0: indvdum@0: $auth_result = $auth->login($username, $password, $param['autologin'], $param['viewonline'], $param['admin']); indvdum@0: indvdum@0: if ($auth_result['status'] == LOGIN_SUCCESS) indvdum@0: { indvdum@0: return; indvdum@0: } indvdum@0: else if ($auth_result['status'] == LOGIN_ERROR_ATTEMPTS) indvdum@0: { indvdum@0: send_status_line(401, 'Unauthorized'); indvdum@0: indvdum@0: trigger_error('NOT_AUTHORISED'); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // Prepend sitename to auth_message indvdum@0: $param['auth_message'] = ($param['auth_message'] === '') ? $config['sitename'] : $config['sitename'] . ' - ' . $param['auth_message']; indvdum@0: indvdum@0: // We should probably filter out non-ASCII characters - RFC2616 indvdum@0: $param['auth_message'] = preg_replace('/[\x80-\xFF]/', '?', $param['auth_message']); indvdum@0: indvdum@0: header('WWW-Authenticate: Basic realm="' . $param['auth_message'] . '"'); indvdum@0: send_status_line(401, 'Unauthorized'); indvdum@0: indvdum@0: trigger_error('NOT_AUTHORISED'); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Generate page header indvdum@0: */ indvdum@0: function page_header($page_title = '', $display_online_list = true, $item_id = 0, $item = 'forum') indvdum@0: { indvdum@0: global $db, $config, $template, $SID, $_SID, $_EXTRA_URL, $user, $auth, $phpEx, $phpbb_root_path; indvdum@0: indvdum@0: if (defined('HEADER_INC')) indvdum@0: { indvdum@0: return; indvdum@0: } indvdum@0: indvdum@0: define('HEADER_INC', true); indvdum@0: indvdum@0: // gzip_compression indvdum@0: if ($config['gzip_compress']) indvdum@0: { indvdum@0: // to avoid partially compressed output resulting in blank pages in indvdum@0: // the browser or error messages, compression is disabled in a few cases: indvdum@0: // indvdum@0: // 1) if headers have already been sent, this indicates plaintext output indvdum@0: // has been started so further content must not be compressed indvdum@0: // 2) the length of the current output buffer is non-zero. This means indvdum@0: // there is already some uncompressed content in this output buffer indvdum@0: // so further output must not be compressed indvdum@0: // 3) if more than one level of output buffering is used because we indvdum@0: // cannot test all output buffer level content lengths. One level indvdum@0: // could be caused by php.ini output_buffering. Anything indvdum@0: // beyond that is manual, so the code wrapping phpBB in output buffering indvdum@0: // can easily compress the output itself. indvdum@0: // indvdum@0: if (@extension_loaded('zlib') && !headers_sent() && ob_get_level() <= 1 && ob_get_length() == 0) indvdum@0: { indvdum@0: ob_start('ob_gzhandler'); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // Generate logged in/logged out status indvdum@0: if ($user->data['user_id'] != ANONYMOUS) indvdum@0: { indvdum@0: $u_login_logout = append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=logout', true, $user->session_id); indvdum@0: $l_login_logout = sprintf($user->lang['LOGOUT_USER'], $user->data['username']); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $u_login_logout = append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=login'); indvdum@0: $l_login_logout = $user->lang['LOGIN']; indvdum@0: } indvdum@0: indvdum@0: // Last visit date/time indvdum@0: $s_last_visit = ($user->data['user_id'] != ANONYMOUS) ? $user->format_date($user->data['session_last_visit']) : ''; indvdum@0: indvdum@0: // Get users online list ... if required indvdum@0: $l_online_users = $online_userlist = $l_online_record = $l_online_time = ''; indvdum@0: indvdum@0: if ($config['load_online'] && $config['load_online_time'] && $display_online_list) indvdum@0: { indvdum@0: /** indvdum@0: * Load online data: indvdum@0: * For obtaining another session column use $item and $item_id in the function-parameter, whereby the column is session_{$item}_id. indvdum@0: */ indvdum@0: $item_id = max($item_id, 0); indvdum@0: indvdum@0: $online_users = obtain_users_online($item_id, $item); indvdum@0: $user_online_strings = obtain_users_online_string($online_users, $item_id, $item); indvdum@0: indvdum@0: $l_online_users = $user_online_strings['l_online_users']; indvdum@0: $online_userlist = $user_online_strings['online_userlist']; indvdum@0: $total_online_users = $online_users['total_online']; indvdum@0: indvdum@0: if ($total_online_users > $config['record_online_users']) indvdum@0: { indvdum@0: set_config('record_online_users', $total_online_users, true); indvdum@0: set_config('record_online_date', time(), true); indvdum@0: } indvdum@0: indvdum@0: $l_online_record = sprintf($user->lang['RECORD_ONLINE_USERS'], $config['record_online_users'], $user->format_date($config['record_online_date'], false, true)); indvdum@0: indvdum@0: $l_online_time = ($config['load_online_time'] == 1) ? 'VIEW_ONLINE_TIME' : 'VIEW_ONLINE_TIMES'; indvdum@0: $l_online_time = sprintf($user->lang[$l_online_time], $config['load_online_time']); indvdum@0: } indvdum@0: indvdum@0: $l_privmsgs_text = $l_privmsgs_text_unread = ''; indvdum@0: $s_privmsg_new = false; indvdum@0: indvdum@0: // Obtain number of new private messages if user is logged in indvdum@0: if (!empty($user->data['is_registered'])) indvdum@0: { indvdum@0: if ($user->data['user_new_privmsg']) indvdum@0: { indvdum@0: $l_message_new = ($user->data['user_new_privmsg'] == 1) ? $user->lang['NEW_PM'] : $user->lang['NEW_PMS']; indvdum@0: $l_privmsgs_text = sprintf($l_message_new, $user->data['user_new_privmsg']); indvdum@0: indvdum@0: if (!$user->data['user_last_privmsg'] || $user->data['user_last_privmsg'] > $user->data['session_last_visit']) indvdum@0: { indvdum@0: $sql = 'UPDATE ' . USERS_TABLE . ' indvdum@0: SET user_last_privmsg = ' . $user->data['session_last_visit'] . ' indvdum@0: WHERE user_id = ' . $user->data['user_id']; indvdum@0: $db->sql_query($sql); indvdum@0: indvdum@0: $s_privmsg_new = true; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $s_privmsg_new = false; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $l_privmsgs_text = $user->lang['NO_NEW_PM']; indvdum@0: $s_privmsg_new = false; indvdum@0: } indvdum@0: indvdum@0: $l_privmsgs_text_unread = ''; indvdum@0: indvdum@0: if ($user->data['user_unread_privmsg'] && $user->data['user_unread_privmsg'] != $user->data['user_new_privmsg']) indvdum@0: { indvdum@0: $l_message_unread = ($user->data['user_unread_privmsg'] == 1) ? $user->lang['UNREAD_PM'] : $user->lang['UNREAD_PMS']; indvdum@0: $l_privmsgs_text_unread = sprintf($l_message_unread, $user->data['user_unread_privmsg']); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $forum_id = request_var('f', 0); indvdum@0: $topic_id = request_var('t', 0); indvdum@0: indvdum@0: $s_feed_news = false; indvdum@0: indvdum@0: // Get option for news indvdum@0: if ($config['feed_enable']) indvdum@0: { indvdum@0: $sql = 'SELECT forum_id indvdum@0: FROM ' . FORUMS_TABLE . ' indvdum@0: WHERE ' . $db->sql_bit_and('forum_options', FORUM_OPTION_FEED_NEWS, '<> 0'); indvdum@0: $result = $db->sql_query_limit($sql, 1, 0, 600); indvdum@0: $s_feed_news = (int) $db->sql_fetchfield('forum_id'); indvdum@0: $db->sql_freeresult($result); indvdum@0: } indvdum@0: indvdum@0: // Determine board url - we may need it later indvdum@0: $board_url = generate_board_url() . '/'; indvdum@0: $web_path = (defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH) ? $board_url : $phpbb_root_path; indvdum@0: indvdum@0: // Which timezone? indvdum@0: $tz = ($user->data['user_id'] != ANONYMOUS) ? strval(doubleval($user->data['user_timezone'])) : strval(doubleval($config['board_timezone'])); indvdum@0: indvdum@0: // Send a proper content-language to the output indvdum@0: $user_lang = $user->lang['USER_LANG']; indvdum@0: if (strpos($user_lang, '-x-') !== false) indvdum@0: { indvdum@0: $user_lang = substr($user_lang, 0, strpos($user_lang, '-x-')); indvdum@0: } indvdum@0: indvdum@0: $s_search_hidden_fields = array(); indvdum@0: if ($_SID) indvdum@0: { indvdum@0: $s_search_hidden_fields['sid'] = $_SID; indvdum@0: } indvdum@0: indvdum@0: if (!empty($_EXTRA_URL)) indvdum@0: { indvdum@0: foreach ($_EXTRA_URL as $url_param) indvdum@0: { indvdum@0: $url_param = explode('=', $url_param, 2); indvdum@0: $s_hidden_fields[$url_param[0]] = $url_param[1]; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // The following assigns all _common_ variables that may be used at any point in a template. indvdum@0: $template->assign_vars(array( indvdum@0: 'SITENAME' => $config['sitename'], indvdum@0: 'SITE_DESCRIPTION' => $config['site_desc'], indvdum@0: 'PAGE_TITLE' => $page_title, indvdum@0: 'SCRIPT_NAME' => str_replace('.' . $phpEx, '', $user->page['page_name']), indvdum@0: 'LAST_VISIT_DATE' => sprintf($user->lang['YOU_LAST_VISIT'], $s_last_visit), indvdum@0: 'LAST_VISIT_YOU' => $s_last_visit, indvdum@0: 'CURRENT_TIME' => sprintf($user->lang['CURRENT_TIME'], $user->format_date(time(), false, true)), indvdum@0: 'TOTAL_USERS_ONLINE' => $l_online_users, indvdum@0: 'LOGGED_IN_USER_LIST' => $online_userlist, indvdum@0: 'RECORD_USERS' => $l_online_record, indvdum@0: 'PRIVATE_MESSAGE_INFO' => $l_privmsgs_text, indvdum@0: 'PRIVATE_MESSAGE_INFO_UNREAD' => $l_privmsgs_text_unread, indvdum@0: indvdum@0: 'S_USER_NEW_PRIVMSG' => $user->data['user_new_privmsg'], indvdum@0: 'S_USER_UNREAD_PRIVMSG' => $user->data['user_unread_privmsg'], indvdum@0: 'S_USER_NEW' => $user->data['user_new'], indvdum@0: indvdum@0: 'SID' => $SID, indvdum@0: '_SID' => $_SID, indvdum@0: 'SESSION_ID' => $user->session_id, indvdum@0: 'ROOT_PATH' => $phpbb_root_path, indvdum@0: 'BOARD_URL' => $board_url, indvdum@0: indvdum@0: 'L_LOGIN_LOGOUT' => $l_login_logout, indvdum@0: 'L_INDEX' => $user->lang['FORUM_INDEX'], indvdum@0: 'L_ONLINE_EXPLAIN' => $l_online_time, indvdum@0: indvdum@0: 'U_PRIVATEMSGS' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&folder=inbox'), indvdum@0: 'U_RETURN_INBOX' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&folder=inbox'), indvdum@0: 'U_POPUP_PM' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&mode=popup'), indvdum@0: 'UA_POPUP_PM' => addslashes(append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&mode=popup')), indvdum@0: 'U_MEMBERLIST' => append_sid("{$phpbb_root_path}memberlist.$phpEx"), indvdum@0: 'U_VIEWONLINE' => ($auth->acl_gets('u_viewprofile', 'a_user', 'a_useradd', 'a_userdel')) ? append_sid("{$phpbb_root_path}viewonline.$phpEx") : '', indvdum@0: 'U_LOGIN_LOGOUT' => $u_login_logout, indvdum@0: 'U_INDEX' => append_sid("{$phpbb_root_path}index.$phpEx"), indvdum@0: 'U_SEARCH' => append_sid("{$phpbb_root_path}search.$phpEx"), indvdum@0: 'U_REGISTER' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=register'), indvdum@0: 'U_PROFILE' => append_sid("{$phpbb_root_path}ucp.$phpEx"), indvdum@0: 'U_MODCP' => append_sid("{$phpbb_root_path}mcp.$phpEx", false, true, $user->session_id), indvdum@0: 'U_FAQ' => append_sid("{$phpbb_root_path}faq.$phpEx"), indvdum@0: 'U_SEARCH_SELF' => append_sid("{$phpbb_root_path}search.$phpEx", 'search_id=egosearch'), indvdum@0: 'U_SEARCH_NEW' => append_sid("{$phpbb_root_path}search.$phpEx", 'search_id=newposts'), indvdum@0: 'U_SEARCH_UNANSWERED' => append_sid("{$phpbb_root_path}search.$phpEx", 'search_id=unanswered'), indvdum@0: 'U_SEARCH_UNREAD' => append_sid("{$phpbb_root_path}search.$phpEx", 'search_id=unreadposts'), indvdum@0: 'U_SEARCH_ACTIVE_TOPICS'=> append_sid("{$phpbb_root_path}search.$phpEx", 'search_id=active_topics'), indvdum@0: 'U_DELETE_COOKIES' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=delete_cookies'), indvdum@0: 'U_TEAM' => ($user->data['user_id'] != ANONYMOUS && !$auth->acl_get('u_viewprofile')) ? '' : append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=leaders'), indvdum@0: 'U_TERMS_USE' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=terms'), indvdum@0: 'U_PRIVACY' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=privacy'), indvdum@0: 'U_RESTORE_PERMISSIONS' => ($user->data['user_perm_from'] && $auth->acl_get('a_switchperm')) ? append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=restore_perm') : '', indvdum@0: 'U_FEED' => generate_board_url() . "/feed.$phpEx", indvdum@0: indvdum@0: 'S_USER_LOGGED_IN' => ($user->data['user_id'] != ANONYMOUS) ? true : false, indvdum@0: 'S_AUTOLOGIN_ENABLED' => ($config['allow_autologin']) ? true : false, indvdum@0: 'S_BOARD_DISABLED' => ($config['board_disable']) ? true : false, indvdum@0: 'S_REGISTERED_USER' => (!empty($user->data['is_registered'])) ? true : false, indvdum@0: 'S_IS_BOT' => (!empty($user->data['is_bot'])) ? true : false, indvdum@0: 'S_USER_PM_POPUP' => $user->optionget('popuppm'), indvdum@0: 'S_USER_LANG' => $user_lang, indvdum@0: 'S_USER_BROWSER' => (isset($user->data['session_browser'])) ? $user->data['session_browser'] : $user->lang['UNKNOWN_BROWSER'], indvdum@0: 'S_USERNAME' => $user->data['username'], indvdum@0: 'S_CONTENT_DIRECTION' => $user->lang['DIRECTION'], indvdum@0: 'S_CONTENT_FLOW_BEGIN' => ($user->lang['DIRECTION'] == 'ltr') ? 'left' : 'right', indvdum@0: 'S_CONTENT_FLOW_END' => ($user->lang['DIRECTION'] == 'ltr') ? 'right' : 'left', indvdum@0: 'S_CONTENT_ENCODING' => 'UTF-8', indvdum@0: 'S_TIMEZONE' => ($user->data['user_dst'] || ($user->data['user_id'] == ANONYMOUS && $config['board_dst'])) ? sprintf($user->lang['ALL_TIMES'], $user->lang['tz'][$tz], $user->lang['tz']['dst']) : sprintf($user->lang['ALL_TIMES'], $user->lang['tz'][$tz], ''), indvdum@0: 'S_DISPLAY_ONLINE_LIST' => ($l_online_time) ? 1 : 0, indvdum@0: 'S_DISPLAY_SEARCH' => (!$config['load_search']) ? 0 : (isset($auth) ? ($auth->acl_get('u_search') && $auth->acl_getf_global('f_search')) : 1), indvdum@0: 'S_DISPLAY_PM' => ($config['allow_privmsg'] && !empty($user->data['is_registered']) && ($auth->acl_get('u_readpm') || $auth->acl_get('u_sendpm'))) ? true : false, indvdum@0: 'S_DISPLAY_MEMBERLIST' => (isset($auth)) ? $auth->acl_get('u_viewprofile') : 0, indvdum@0: 'S_NEW_PM' => ($s_privmsg_new) ? 1 : 0, indvdum@0: 'S_REGISTER_ENABLED' => ($config['require_activation'] != USER_ACTIVATION_DISABLE) ? true : false, indvdum@0: 'S_FORUM_ID' => $forum_id, indvdum@0: 'S_TOPIC_ID' => $topic_id, indvdum@0: indvdum@0: 'S_LOGIN_ACTION' => ((!defined('ADMIN_START')) ? append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=login') : append_sid("index.$phpEx", false, true, $user->session_id)), indvdum@0: 'S_LOGIN_REDIRECT' => build_hidden_fields(array('redirect' => build_url())), indvdum@0: indvdum@0: 'S_ENABLE_FEEDS' => ($config['feed_enable']) ? true : false, indvdum@0: 'S_ENABLE_FEEDS_OVERALL' => ($config['feed_overall']) ? true : false, indvdum@0: 'S_ENABLE_FEEDS_FORUMS' => ($config['feed_overall_forums']) ? true : false, indvdum@0: 'S_ENABLE_FEEDS_TOPICS' => ($config['feed_topics_new']) ? true : false, indvdum@0: 'S_ENABLE_FEEDS_TOPICS_ACTIVE' => ($config['feed_topics_active']) ? true : false, indvdum@0: 'S_ENABLE_FEEDS_NEWS' => ($s_feed_news) ? true : false, indvdum@0: indvdum@0: 'S_LOAD_UNREADS' => ($config['load_unreads_search'] && ($config['load_anon_lastread'] || $user->data['is_registered'])) ? true : false, indvdum@0: indvdum@0: 'S_SEARCH_HIDDEN_FIELDS' => build_hidden_fields($s_search_hidden_fields), indvdum@0: indvdum@0: 'T_THEME_PATH' => "{$web_path}styles/" . $user->theme['theme_path'] . '/theme', indvdum@0: 'T_TEMPLATE_PATH' => "{$web_path}styles/" . $user->theme['template_path'] . '/template', indvdum@0: 'T_SUPER_TEMPLATE_PATH' => (isset($user->theme['template_inherit_path']) && $user->theme['template_inherit_path']) ? "{$web_path}styles/" . $user->theme['template_inherit_path'] . '/template' : "{$web_path}styles/" . $user->theme['template_path'] . '/template', indvdum@0: 'T_IMAGESET_PATH' => "{$web_path}styles/" . $user->theme['imageset_path'] . '/imageset', indvdum@0: 'T_IMAGESET_LANG_PATH' => "{$web_path}styles/" . $user->theme['imageset_path'] . '/imageset/' . $user->lang_name, indvdum@0: 'T_IMAGES_PATH' => "{$web_path}images/", indvdum@0: 'T_SMILIES_PATH' => "{$web_path}{$config['smilies_path']}/", indvdum@0: 'T_AVATAR_PATH' => "{$web_path}{$config['avatar_path']}/", indvdum@0: 'T_AVATAR_GALLERY_PATH' => "{$web_path}{$config['avatar_gallery_path']}/", indvdum@0: 'T_ICONS_PATH' => "{$web_path}{$config['icons_path']}/", indvdum@0: 'T_RANKS_PATH' => "{$web_path}{$config['ranks_path']}/", indvdum@0: 'T_UPLOAD_PATH' => "{$web_path}{$config['upload_path']}/", indvdum@0: 'T_STYLESHEET_LINK' => (!$user->theme['theme_storedb']) ? "{$web_path}styles/" . $user->theme['theme_path'] . '/theme/stylesheet.css' : append_sid("{$phpbb_root_path}style.$phpEx", 'id=' . $user->theme['style_id'] . '&lang=' . $user->lang_name), indvdum@0: 'T_STYLESHEET_NAME' => $user->theme['theme_name'], indvdum@0: indvdum@0: 'T_THEME_NAME' => $user->theme['theme_path'], indvdum@0: 'T_TEMPLATE_NAME' => $user->theme['template_path'], indvdum@0: 'T_SUPER_TEMPLATE_NAME' => (isset($user->theme['template_inherit_path']) && $user->theme['template_inherit_path']) ? $user->theme['template_inherit_path'] : $user->theme['template_path'], indvdum@0: 'T_IMAGESET_NAME' => $user->theme['imageset_path'], indvdum@0: 'T_IMAGESET_LANG_NAME' => $user->data['user_lang'], indvdum@0: 'T_IMAGES' => 'images', indvdum@0: 'T_SMILIES' => $config['smilies_path'], indvdum@0: 'T_AVATAR' => $config['avatar_path'], indvdum@0: 'T_AVATAR_GALLERY' => $config['avatar_gallery_path'], indvdum@0: 'T_ICONS' => $config['icons_path'], indvdum@0: 'T_RANKS' => $config['ranks_path'], indvdum@0: 'T_UPLOAD' => $config['upload_path'], indvdum@0: indvdum@0: 'SITE_LOGO_IMG' => $user->img('site_logo'), indvdum@0: indvdum@0: 'A_COOKIE_SETTINGS' => addslashes('; path=' . $config['cookie_path'] . ((!$config['cookie_domain'] || $config['cookie_domain'] == 'localhost' || $config['cookie_domain'] == '127.0.0.1') ? '' : '; domain=' . $config['cookie_domain']) . ((!$config['cookie_secure']) ? '' : '; secure')), indvdum@0: )); indvdum@0: indvdum@0: // application/xhtml+xml not used because of IE indvdum@0: header('Content-type: text/html; charset=UTF-8'); indvdum@0: indvdum@0: header('Cache-Control: private, no-cache="set-cookie"'); indvdum@0: header('Expires: 0'); indvdum@0: header('Pragma: no-cache'); indvdum@0: indvdum@0: if (!empty($user->data['is_bot'])) indvdum@0: { indvdum@0: // Let reverse proxies know we detected a bot. indvdum@0: header('X-PHPBB-IS-BOT: yes'); indvdum@0: } indvdum@0: indvdum@0: return; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Generate page footer indvdum@0: */ indvdum@0: function page_footer($run_cron = true) indvdum@0: { indvdum@0: global $db, $config, $template, $user, $auth, $cache, $starttime, $phpbb_root_path, $phpEx; indvdum@0: indvdum@0: // Output page creation time indvdum@0: if (defined('DEBUG')) indvdum@0: { indvdum@0: $mtime = explode(' ', microtime()); indvdum@0: $totaltime = $mtime[0] + $mtime[1] - $starttime; indvdum@0: indvdum@0: if (!empty($_REQUEST['explain']) && $auth->acl_get('a_') && defined('DEBUG_EXTRA') && method_exists($db, 'sql_report')) indvdum@0: { indvdum@0: $db->sql_report('display'); indvdum@0: } indvdum@0: indvdum@0: $debug_output = sprintf('Time : %.3fs | ' . $db->sql_num_queries() . ' Queries | GZIP : ' . (($config['gzip_compress'] && @extension_loaded('zlib')) ? 'On' : 'Off') . (($user->load) ? ' | Load : ' . $user->load : ''), $totaltime); indvdum@0: indvdum@0: if ($auth->acl_get('a_') && defined('DEBUG_EXTRA')) indvdum@0: { indvdum@0: if (function_exists('memory_get_usage')) indvdum@0: { indvdum@0: if ($memory_usage = memory_get_usage()) indvdum@0: { indvdum@0: global $base_memory_usage; indvdum@0: $memory_usage -= $base_memory_usage; indvdum@0: $memory_usage = get_formatted_filesize($memory_usage); indvdum@0: indvdum@0: $debug_output .= ' | Memory Usage: ' . $memory_usage; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $debug_output .= ' | Explain'; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $template->assign_vars(array( indvdum@0: 'DEBUG_OUTPUT' => (defined('DEBUG')) ? $debug_output : '', indvdum@0: 'TRANSLATION_INFO' => (!empty($user->lang['TRANSLATION_INFO'])) ? $user->lang['TRANSLATION_INFO'] : '', indvdum@0: indvdum@0: 'U_ACP' => ($auth->acl_get('a_') && !empty($user->data['is_registered'])) ? append_sid("{$phpbb_root_path}adm/index.$phpEx", false, true, $user->session_id) : '') indvdum@0: ); indvdum@0: indvdum@0: // Call cron-type script indvdum@0: $call_cron = false; indvdum@0: if (!defined('IN_CRON') && $run_cron && !$config['board_disable'] && !$user->data['is_bot']) indvdum@0: { indvdum@0: $call_cron = true; indvdum@0: $time_now = (!empty($user->time_now) && is_int($user->time_now)) ? $user->time_now : time(); indvdum@0: indvdum@0: // Any old lock present? indvdum@0: if (!empty($config['cron_lock'])) indvdum@0: { indvdum@0: $cron_time = explode(' ', $config['cron_lock']); indvdum@0: indvdum@0: // If 1 hour lock is present we do not call cron.php indvdum@0: if ($cron_time[0] + 3600 >= $time_now) indvdum@0: { indvdum@0: $call_cron = false; indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // Call cron job? indvdum@0: if ($call_cron) indvdum@0: { indvdum@0: $cron_type = ''; indvdum@0: indvdum@0: if ($time_now - $config['queue_interval'] > $config['last_queue_run'] && !defined('IN_ADMIN') && file_exists($phpbb_root_path . 'cache/queue.' . $phpEx)) indvdum@0: { indvdum@0: // Process email queue indvdum@0: $cron_type = 'queue'; indvdum@0: } indvdum@0: else if (method_exists($cache, 'tidy') && $time_now - $config['cache_gc'] > $config['cache_last_gc']) indvdum@0: { indvdum@0: // Tidy the cache indvdum@0: $cron_type = 'tidy_cache'; indvdum@0: } indvdum@0: else if ($config['warnings_expire_days'] && ($time_now - $config['warnings_gc'] > $config['warnings_last_gc'])) indvdum@0: { indvdum@0: $cron_type = 'tidy_warnings'; indvdum@0: } indvdum@0: else if ($time_now - $config['database_gc'] > $config['database_last_gc']) indvdum@0: { indvdum@0: // Tidy the database indvdum@0: $cron_type = 'tidy_database'; indvdum@0: } indvdum@0: else if ($time_now - $config['search_gc'] > $config['search_last_gc']) indvdum@0: { indvdum@0: // Tidy the search indvdum@0: $cron_type = 'tidy_search'; indvdum@0: } indvdum@0: else if ($time_now - $config['session_gc'] > $config['session_last_gc']) indvdum@0: { indvdum@0: $cron_type = 'tidy_sessions'; indvdum@0: } indvdum@0: indvdum@0: if ($cron_type) indvdum@0: { indvdum@0: $template->assign_var('RUN_CRON_TASK', 'cron'); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $template->display('body'); indvdum@0: indvdum@0: garbage_collection(); indvdum@0: exit_handler(); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Closing the cache object and the database indvdum@0: * Cool function name, eh? We might want to add operations to it later indvdum@0: */ indvdum@0: function garbage_collection() indvdum@0: { indvdum@0: global $cache, $db; indvdum@0: indvdum@0: // Unload cache, must be done before the DB connection if closed indvdum@0: if (!empty($cache)) indvdum@0: { indvdum@0: $cache->unload(); indvdum@0: } indvdum@0: indvdum@0: // Close our DB connection. indvdum@0: if (!empty($db)) indvdum@0: { indvdum@0: $db->sql_close(); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Handler for exit calls in phpBB. indvdum@0: * This function supports hooks. indvdum@0: * indvdum@0: * Note: This function is called after the template has been outputted. indvdum@0: */ indvdum@0: function exit_handler() indvdum@0: { indvdum@0: global $phpbb_hook, $config; indvdum@0: indvdum@0: if (!empty($phpbb_hook) && $phpbb_hook->call_hook(__FUNCTION__)) indvdum@0: { indvdum@0: if ($phpbb_hook->hook_return(__FUNCTION__)) indvdum@0: { indvdum@0: return $phpbb_hook->hook_return_result(__FUNCTION__); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // As a pre-caution... some setups display a blank page if the flush() is not there. indvdum@0: (ob_get_level() > 0) ? @ob_flush() : @flush(); indvdum@0: indvdum@0: exit; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Handler for init calls in phpBB. This function is called in user::setup(); indvdum@0: * This function supports hooks. indvdum@0: */ indvdum@0: function phpbb_user_session_handler() indvdum@0: { indvdum@0: global $phpbb_hook; indvdum@0: indvdum@0: if (!empty($phpbb_hook) && $phpbb_hook->call_hook(__FUNCTION__)) indvdum@0: { indvdum@0: if ($phpbb_hook->hook_return(__FUNCTION__)) indvdum@0: { indvdum@0: return $phpbb_hook->hook_return_result(__FUNCTION__); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: return; indvdum@0: } indvdum@0: indvdum@5: ?>