Chris@76: $value)
Chris@76: if (is_numeric($key))
Chris@76: unset($_COOKIE[$key]);
Chris@76:
Chris@76: // Get the correct query string. It may be in an environment variable...
Chris@76: if (!isset($_SERVER['QUERY_STRING']))
Chris@76: $_SERVER['QUERY_STRING'] = getenv('QUERY_STRING');
Chris@76:
Chris@76: // It seems that sticking a URL after the query string is mighty common, well, it's evil - don't.
Chris@76: if (strpos($_SERVER['QUERY_STRING'], 'http') === 0)
Chris@76: {
Chris@76: header('HTTP/1.1 400 Bad Request');
Chris@76: die;
Chris@76: }
Chris@76:
Chris@76: // Are we going to need to parse the ; out?
Chris@76: if ((strpos(@ini_get('arg_separator.input'), ';') === false || @version_compare(PHP_VERSION, '4.2.0') == -1) && !empty($_SERVER['QUERY_STRING']))
Chris@76: {
Chris@76: // Get rid of the old one! You don't know where it's been!
Chris@76: $_GET = array();
Chris@76:
Chris@76: // Was this redirected? If so, get the REDIRECT_QUERY_STRING.
Chris@76: // Do not urldecode() the querystring, unless you so much wish to break OpenID implementation. :)
Chris@76: $_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'], 0, 5) === 'url=/' ? $_SERVER['REDIRECT_QUERY_STRING'] : $_SERVER['QUERY_STRING'];
Chris@76:
Chris@76: // Replace ';' with '&' and '&something&' with '&something=&'. (this is done for compatibility...)
Chris@76: // !!! smflib
Chris@76: parse_str(preg_replace('/&(\w+)(?=&|$)/', '&$1=', strtr($_SERVER['QUERY_STRING'], array(';?' => '&', ';' => '&', '%00' => '', "\0" => ''))), $_GET);
Chris@76:
Chris@76: // Magic quotes still applies with parse_str - so clean it up.
Chris@76: if (function_exists('get_magic_quotes_gpc') && @get_magic_quotes_gpc() != 0 && empty($modSettings['integrate_magic_quotes']))
Chris@76: $_GET = $removeMagicQuoteFunction($_GET);
Chris@76: }
Chris@76: elseif (strpos(@ini_get('arg_separator.input'), ';') !== false)
Chris@76: {
Chris@76: if (function_exists('get_magic_quotes_gpc') && @get_magic_quotes_gpc() != 0 && empty($modSettings['integrate_magic_quotes']))
Chris@76: $_GET = $removeMagicQuoteFunction($_GET);
Chris@76:
Chris@76: // Search engines will send action=profile%3Bu=1, which confuses PHP.
Chris@76: foreach ($_GET as $k => $v)
Chris@76: {
Chris@76: if (is_string($v) && strpos($k, ';') !== false)
Chris@76: {
Chris@76: $temp = explode(';', $v);
Chris@76: $_GET[$k] = $temp[0];
Chris@76:
Chris@76: for ($i = 1, $n = count($temp); $i < $n; $i++)
Chris@76: {
Chris@76: @list ($key, $val) = @explode('=', $temp[$i], 2);
Chris@76: if (!isset($_GET[$key]))
Chris@76: $_GET[$key] = $val;
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: // This helps a lot with integration!
Chris@76: if (strpos($k, '?') === 0)
Chris@76: {
Chris@76: $_GET[substr($k, 1)] = $v;
Chris@76: unset($_GET[$k]);
Chris@76: }
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: // There's no query string, but there is a URL... try to get the data from there.
Chris@76: if (!empty($_SERVER['REQUEST_URI']))
Chris@76: {
Chris@76: // Remove the .html, assuming there is one.
Chris@76: if (substr($_SERVER['REQUEST_URI'], strrpos($_SERVER['REQUEST_URI'], '.'), 4) == '.htm')
Chris@76: $request = substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], '.'));
Chris@76: else
Chris@76: $request = $_SERVER['REQUEST_URI'];
Chris@76:
Chris@76: // !!! smflib.
Chris@76: // Replace 'index.php/a,b,c/d/e,f' with 'a=b,c&d=&e=f' and parse it into $_GET.
Chris@76: if (strpos($request, basename($scripturl) . '/') !== false)
Chris@76: {
Chris@76: parse_str(substr(preg_replace('/&(\w+)(?=&|$)/', '&$1=', strtr(preg_replace('~/([^,/]+),~', '/$1=', substr($request, strpos($request, basename($scripturl)) + strlen(basename($scripturl)))), '/', '&')), 1), $temp);
Chris@76: if (function_exists('get_magic_quotes_gpc') && @get_magic_quotes_gpc() != 0 && empty($modSettings['integrate_magic_quotes']))
Chris@76: $temp = $removeMagicQuoteFunction($temp);
Chris@76: $_GET += $temp;
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: // If magic quotes is on we have some work...
Chris@76: if (function_exists('get_magic_quotes_gpc') && @get_magic_quotes_gpc() != 0)
Chris@76: {
Chris@76: $_ENV = $removeMagicQuoteFunction($_ENV);
Chris@76: $_POST = $removeMagicQuoteFunction($_POST);
Chris@76: $_COOKIE = $removeMagicQuoteFunction($_COOKIE);
Chris@76: foreach ($_FILES as $k => $dummy)
Chris@76: if (isset($_FILES[$k]['name']))
Chris@76: $_FILES[$k]['name'] = $removeMagicQuoteFunction($_FILES[$k]['name']);
Chris@76: }
Chris@76:
Chris@76: // Add entities to GET. This is kinda like the slashes on everything else.
Chris@76: $_GET = htmlspecialchars__recursive($_GET);
Chris@76:
Chris@76: // Let's not depend on the ini settings... why even have COOKIE in there, anyway?
Chris@76: $_REQUEST = $_POST + $_GET;
Chris@76:
Chris@76: // Make sure $board and $topic are numbers.
Chris@76: if (isset($_REQUEST['board']))
Chris@76: {
Chris@76: // Make sure its a string and not something else like an array
Chris@76: $_REQUEST['board'] = (string) $_REQUEST['board'];
Chris@76:
Chris@76: // If there's a slash in it, we've got a start value! (old, compatible links.)
Chris@76: if (strpos($_REQUEST['board'], '/') !== false)
Chris@76: list ($_REQUEST['board'], $_REQUEST['start']) = explode('/', $_REQUEST['board']);
Chris@76: // Same idea, but dots. This is the currently used format - ?board=1.0...
Chris@76: elseif (strpos($_REQUEST['board'], '.') !== false)
Chris@76: list ($_REQUEST['board'], $_REQUEST['start']) = explode('.', $_REQUEST['board']);
Chris@76: // Now make absolutely sure it's a number.
Chris@76: $board = (int) $_REQUEST['board'];
Chris@76: $_REQUEST['start'] = isset($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0;
Chris@76:
Chris@76: // This is for "Who's Online" because it might come via POST - and it should be an int here.
Chris@76: $_GET['board'] = $board;
Chris@76: }
Chris@76: // Well, $board is going to be a number no matter what.
Chris@76: else
Chris@76: $board = 0;
Chris@76:
Chris@76: // If there's a threadid, it's probably an old YaBB SE link. Flow with it.
Chris@76: if (isset($_REQUEST['threadid']) && !isset($_REQUEST['topic']))
Chris@76: $_REQUEST['topic'] = $_REQUEST['threadid'];
Chris@76:
Chris@76: // We've got topic!
Chris@76: if (isset($_REQUEST['topic']))
Chris@76: {
Chris@76: // Make sure its a string and not something else like an array
Chris@76: $_REQUEST['topic'] = (string) $_REQUEST['topic'];
Chris@76:
Chris@76: // Slash means old, beta style, formatting. That's okay though, the link should still work.
Chris@76: if (strpos($_REQUEST['topic'], '/') !== false)
Chris@76: list ($_REQUEST['topic'], $_REQUEST['start']) = explode('/', $_REQUEST['topic']);
Chris@76: // Dots are useful and fun ;). This is ?topic=1.15.
Chris@76: elseif (strpos($_REQUEST['topic'], '.') !== false)
Chris@76: list ($_REQUEST['topic'], $_REQUEST['start']) = explode('.', $_REQUEST['topic']);
Chris@76:
Chris@76: $topic = (int) $_REQUEST['topic'];
Chris@76:
Chris@76: // Now make sure the online log gets the right number.
Chris@76: $_GET['topic'] = $topic;
Chris@76: }
Chris@76: else
Chris@76: $topic = 0;
Chris@76:
Chris@76: // There should be a $_REQUEST['start'], some at least. If you need to default to other than 0, use $_GET['start'].
Chris@76: if (empty($_REQUEST['start']) || $_REQUEST['start'] < 0 || (int) $_REQUEST['start'] > 2147473647)
Chris@76: $_REQUEST['start'] = 0;
Chris@76:
Chris@76: // The action needs to be a string and not an array or anything else
Chris@76: if (isset($_REQUEST['action']))
Chris@76: $_REQUEST['action'] = (string) $_REQUEST['action'];
Chris@76: if (isset($_GET['action']))
Chris@76: $_GET['action'] = (string) $_GET['action'];
Chris@76:
Chris@76: // Make sure we have a valid REMOTE_ADDR.
Chris@76: if (!isset($_SERVER['REMOTE_ADDR']))
Chris@76: {
Chris@76: $_SERVER['REMOTE_ADDR'] = '';
Chris@76: // A new magic variable to indicate we think this is command line.
Chris@76: $_SERVER['is_cli'] = true;
Chris@76: }
Chris@76: elseif (preg_match('~^((([1]?\d)?\d|2[0-4]\d|25[0-5])\.){3}(([1]?\d)?\d|2[0-4]\d|25[0-5])$~', $_SERVER['REMOTE_ADDR']) === 0)
Chris@76: $_SERVER['REMOTE_ADDR'] = 'unknown';
Chris@76:
Chris@76: // Try to calculate their most likely IP for those people behind proxies (And the like).
Chris@76: $_SERVER['BAN_CHECK_IP'] = $_SERVER['REMOTE_ADDR'];
Chris@76:
Chris@76: // Find the user's IP address. (but don't let it give you 'unknown'!)
Chris@76: if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_CLIENT_IP']) && (preg_match('~^((0|10|172\.(1[6-9]|2[0-9]|3[01])|192\.168|255|127)\.|unknown)~', $_SERVER['HTTP_CLIENT_IP']) == 0 || preg_match('~^((0|10|172\.(1[6-9]|2[0-9]|3[01])|192\.168|255|127)\.|unknown)~', $_SERVER['REMOTE_ADDR']) != 0))
Chris@76: {
Chris@76: // We have both forwarded for AND client IP... check the first forwarded for as the block - only switch if it's better that way.
Chris@76: if (strtok($_SERVER['HTTP_X_FORWARDED_FOR'], '.') != strtok($_SERVER['HTTP_CLIENT_IP'], '.') && '.' . strtok($_SERVER['HTTP_X_FORWARDED_FOR'], '.') == strrchr($_SERVER['HTTP_CLIENT_IP'], '.') && (preg_match('~^((0|10|172\.(1[6-9]|2[0-9]|3[01])|192\.168|255|127)\.|unknown)~', $_SERVER['HTTP_X_FORWARDED_FOR']) == 0 || preg_match('~^((0|10|172\.(1[6-9]|2[0-9]|3[01])|192\.168|255|127)\.|unknown)~', $_SERVER['REMOTE_ADDR']) != 0))
Chris@76: $_SERVER['BAN_CHECK_IP'] = implode('.', array_reverse(explode('.', $_SERVER['HTTP_CLIENT_IP'])));
Chris@76: else
Chris@76: $_SERVER['BAN_CHECK_IP'] = $_SERVER['HTTP_CLIENT_IP'];
Chris@76: }
Chris@76: if (!empty($_SERVER['HTTP_CLIENT_IP']) && (preg_match('~^((0|10|172\.(1[6-9]|2[0-9]|3[01])|192\.168|255|127)\.|unknown)~', $_SERVER['HTTP_CLIENT_IP']) == 0 || preg_match('~^((0|10|172\.(1[6-9]|2[0-9]|3[01])|192\.168|255|127)\.|unknown)~', $_SERVER['REMOTE_ADDR']) != 0))
Chris@76: {
Chris@76: // Since they are in different blocks, it's probably reversed.
Chris@76: if (strtok($_SERVER['REMOTE_ADDR'], '.') != strtok($_SERVER['HTTP_CLIENT_IP'], '.'))
Chris@76: $_SERVER['BAN_CHECK_IP'] = implode('.', array_reverse(explode('.', $_SERVER['HTTP_CLIENT_IP'])));
Chris@76: else
Chris@76: $_SERVER['BAN_CHECK_IP'] = $_SERVER['HTTP_CLIENT_IP'];
Chris@76: }
Chris@76: elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
Chris@76: {
Chris@76: // If there are commas, get the last one.. probably.
Chris@76: if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ',') !== false)
Chris@76: {
Chris@76: $ips = array_reverse(explode(', ', $_SERVER['HTTP_X_FORWARDED_FOR']));
Chris@76:
Chris@76: // Go through each IP...
Chris@76: foreach ($ips as $i => $ip)
Chris@76: {
Chris@76: // Make sure it's in a valid range...
Chris@76: if (preg_match('~^((0|10|172\.(1[6-9]|2[0-9]|3[01])|192\.168|255|127)\.|unknown)~', $ip) != 0 && preg_match('~^((0|10|172\.(1[6-9]|2[0-9]|3[01])|192\.168|255|127)\.|unknown)~', $_SERVER['REMOTE_ADDR']) == 0)
Chris@76: continue;
Chris@76:
Chris@76: // Otherwise, we've got an IP!
Chris@76: $_SERVER['BAN_CHECK_IP'] = trim($ip);
Chris@76: break;
Chris@76: }
Chris@76: }
Chris@76: // Otherwise just use the only one.
Chris@76: elseif (preg_match('~^((0|10|172\.(1[6-9]|2[0-9]|3[01])|192\.168|255|127)\.|unknown)~', $_SERVER['HTTP_X_FORWARDED_FOR']) == 0 || preg_match('~^((0|10|172\.(1[6-9]|2[0-9]|3[01])|192\.168|255|127)\.|unknown)~', $_SERVER['REMOTE_ADDR']) != 0)
Chris@76: $_SERVER['BAN_CHECK_IP'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
Chris@76: }
Chris@76:
Chris@76: // Make sure we know the URL of the current request.
Chris@76: if (empty($_SERVER['REQUEST_URI']))
Chris@76: $_SERVER['REQUEST_URL'] = $scripturl . (!empty($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : '');
Chris@76: elseif (preg_match('~^([^/]+//[^/]+)~', $scripturl, $match) == 1)
Chris@76: $_SERVER['REQUEST_URL'] = $match[1] . $_SERVER['REQUEST_URI'];
Chris@76: else
Chris@76: $_SERVER['REQUEST_URL'] = $_SERVER['REQUEST_URI'];
Chris@76:
Chris@76: // And make sure HTTP_USER_AGENT is set.
Chris@76: $_SERVER['HTTP_USER_AGENT'] = isset($_SERVER['HTTP_USER_AGENT']) ? htmlspecialchars($smcFunc['db_unescape_string']($_SERVER['HTTP_USER_AGENT']), ENT_QUOTES) : '';
Chris@76:
Chris@76: // Some final checking.
Chris@76: if (preg_match('~^((([1]?\d)?\d|2[0-4]\d|25[0-5])\.){3}(([1]?\d)?\d|2[0-4]\d|25[0-5])$~', $_SERVER['BAN_CHECK_IP']) === 0)
Chris@76: $_SERVER['BAN_CHECK_IP'] = '';
Chris@76: if ($_SERVER['REMOTE_ADDR'] == 'unknown')
Chris@76: $_SERVER['REMOTE_ADDR'] = '';
Chris@76: }
Chris@76:
Chris@76: // Adds slashes to the array/variable. Uses two underscores to guard against overloading.
Chris@76: function escapestring__recursive($var)
Chris@76: {
Chris@76: global $smcFunc;
Chris@76:
Chris@76: if (!is_array($var))
Chris@76: return $smcFunc['db_escape_string']($var);
Chris@76:
Chris@76: // Reindex the array with slashes.
Chris@76: $new_var = array();
Chris@76:
Chris@76: // Add slashes to every element, even the indexes!
Chris@76: foreach ($var as $k => $v)
Chris@76: $new_var[$smcFunc['db_escape_string']($k)] = escapestring__recursive($v);
Chris@76:
Chris@76: return $new_var;
Chris@76: }
Chris@76:
Chris@76: // Adds html entities to the array/variable. Uses two underscores to guard against overloading.
Chris@76: function htmlspecialchars__recursive($var, $level = 0)
Chris@76: {
Chris@76: global $smcFunc;
Chris@76:
Chris@76: if (!is_array($var))
Chris@76: return isset($smcFunc['htmlspecialchars']) ? $smcFunc['htmlspecialchars']($var, ENT_QUOTES) : htmlspecialchars($var, ENT_QUOTES);
Chris@76:
Chris@76: // Add the htmlspecialchars to every element.
Chris@76: foreach ($var as $k => $v)
Chris@76: $var[$k] = $level > 25 ? null : htmlspecialchars__recursive($v, $level + 1);
Chris@76:
Chris@76: return $var;
Chris@76: }
Chris@76:
Chris@76: // Removes url stuff from the array/variable. Uses two underscores to guard against overloading.
Chris@76: function urldecode__recursive($var, $level = 0)
Chris@76: {
Chris@76: if (!is_array($var))
Chris@76: return urldecode($var);
Chris@76:
Chris@76: // Reindex the array...
Chris@76: $new_var = array();
Chris@76:
Chris@76: // Add the htmlspecialchars to every element.
Chris@76: foreach ($var as $k => $v)
Chris@76: $new_var[urldecode($k)] = $level > 25 ? null : urldecode__recursive($v, $level + 1);
Chris@76:
Chris@76: return $new_var;
Chris@76: }
Chris@76: // Unescapes any array or variable. Two underscores for the normal reason.
Chris@76: function unescapestring__recursive($var)
Chris@76: {
Chris@76: global $smcFunc;
Chris@76:
Chris@76: if (!is_array($var))
Chris@76: return $smcFunc['db_unescape_string']($var);
Chris@76:
Chris@76: // Reindex the array without slashes, this time.
Chris@76: $new_var = array();
Chris@76:
Chris@76: // Strip the slashes from every element.
Chris@76: foreach ($var as $k => $v)
Chris@76: $new_var[$smcFunc['db_unescape_string']($k)] = unescapestring__recursive($v);
Chris@76:
Chris@76: return $new_var;
Chris@76: }
Chris@76:
Chris@76: // Remove slashes recursively...
Chris@76: function stripslashes__recursive($var, $level = 0)
Chris@76: {
Chris@76: if (!is_array($var))
Chris@76: return stripslashes($var);
Chris@76:
Chris@76: // Reindex the array without slashes, this time.
Chris@76: $new_var = array();
Chris@76:
Chris@76: // Strip the slashes from every element.
Chris@76: foreach ($var as $k => $v)
Chris@76: $new_var[stripslashes($k)] = $level > 25 ? null : stripslashes__recursive($v, $level + 1);
Chris@76:
Chris@76: return $new_var;
Chris@76: }
Chris@76:
Chris@76: // Trim a string including the HTML space, character 160.
Chris@76: function htmltrim__recursive($var, $level = 0)
Chris@76: {
Chris@76: global $smcFunc;
Chris@76:
Chris@76: // Remove spaces (32), tabs (9), returns (13, 10, and 11), nulls (0), and hard spaces. (160)
Chris@76: if (!is_array($var))
Chris@76: return isset($smcFunc) ? $smcFunc['htmltrim']($var) : trim($var, ' ' . "\t\n\r\x0B" . '\0' . "\xA0");
Chris@76:
Chris@76: // Go through all the elements and remove the whitespace.
Chris@76: foreach ($var as $k => $v)
Chris@76: $var[$k] = $level > 25 ? null : htmltrim__recursive($v, $level + 1);
Chris@76:
Chris@76: return $var;
Chris@76: }
Chris@76:
Chris@76: // Clean up the XML to make sure it doesn't contain invalid characters.
Chris@76: function cleanXml($string)
Chris@76: {
Chris@76: global $context;
Chris@76:
Chris@76: // http://www.w3.org/TR/2000/REC-xml-20001006#NT-Char
Chris@76: return preg_replace('~[\x00-\x08\x0B\x0C\x0E-\x19' . ($context['utf8'] ? (@version_compare(PHP_VERSION, '4.3.3') != -1 ? '\x{FFFE}\x{FFFF}' : "\xED\xA0\x80-\xED\xBF\xBF\xEF\xBF\xBE\xEF\xBF\xBF") : '') . ']~' . ($context['utf8'] ? 'u' : ''), '', $string);
Chris@76: }
Chris@76:
Chris@76: function JavaScriptEscape($string)
Chris@76: {
Chris@76: global $scripturl;
Chris@76:
Chris@76: return '\'' . strtr($string, array(
Chris@76: "\r" => '',
Chris@76: "\n" => '\\n',
Chris@76: "\t" => '\\t',
Chris@76: '\\' => '\\\\',
Chris@76: '\'' => '\\\'',
Chris@76: '' => '<\' + \'/',
Chris@76: 'script' => 'scri\'+\'pt',
Chris@76: ' ' $scripturl . '\'+\'',
Chris@76: )) . '\'';
Chris@76: }
Chris@76:
Chris@76: // Rewrite URLs to include the session ID.
Chris@76: function ob_sessrewrite($buffer)
Chris@76: {
Chris@76: global $scripturl, $modSettings, $user_info, $context;
Chris@76:
Chris@76: // If $scripturl is set to nothing, or the SID is not defined (SSI?) just quit.
Chris@76: if ($scripturl == '' || !defined('SID'))
Chris@76: return $buffer;
Chris@76:
Chris@76: // Do nothing if the session is cookied, or they are a crawler - guests are caught by redirectexit(). This doesn't work below PHP 4.3.0, because it makes the output buffer bigger.
Chris@76: // !!! smflib
Chris@76: if (empty($_COOKIE) && SID != '' && empty($context['browser']['possibly_robot']) && @version_compare(PHP_VERSION, '4.3.0') != -1)
Chris@76: $buffer = preg_replace('/"' . preg_quote($scripturl, '/') . '(?!\?' . preg_quote(SID, '/') . ')\\??/', '"' . $scripturl . '?' . SID . '&', $buffer);
Chris@76: // Debugging templates, are we?
Chris@76: elseif (isset($_GET['debug']))
Chris@76: $buffer = preg_replace('/(?