PageRenderTime 44ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/sources/QueryString.php

https://github.com/Arantor/Elkarte
PHP | 632 lines | 312 code | 96 blank | 224 comment | 121 complexity | 37cf09dddcbc1621e927097920bc59df MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /**
  3. * @name ElkArte Forum
  4. * @copyright ElkArte Forum contributors
  5. * @license BSD http://opensource.org/licenses/BSD-3-Clause
  6. *
  7. * This software is a derived product, based on:
  8. *
  9. * Simple Machines Forum (SMF)
  10. * copyright: 2011 Simple Machines (http://www.simplemachines.org)
  11. * license: BSD, See included LICENSE.TXT for terms and conditions.
  12. *
  13. * @version 1.0 Alpha
  14. *
  15. * This file does a lot of important stuff. Mainly, this means it handles
  16. * the query string, request variables, and session management.
  17. *
  18. */
  19. if (!defined('ELKARTE'))
  20. die('No access...');
  21. /**
  22. * Clean the request variables - add html entities to GET and slashes if magic_quotes_gpc is Off.
  23. *
  24. * What it does:
  25. * - cleans the request variables (ENV, GET, POST, COOKIE, SERVER) and
  26. * makes sure the query string was parsed correctly.
  27. * - handles the URLs passed by the queryless URLs option.
  28. * - makes sure, regardless of php.ini, everything has slashes.
  29. * - sets up $board, $topic, and $scripturl and $_REQUEST['start'].
  30. * - determines, or rather tries to determine, the client's IP.
  31. */
  32. function cleanRequest()
  33. {
  34. global $board, $topic, $boardurl, $scripturl, $modSettings, $smcFunc;
  35. // Makes it easier to refer to things this way.
  36. $scripturl = $boardurl . '/index.php';
  37. // What function to use to reverse magic quotes - if sybase is on we assume that the database sensibly has the right unescape function!
  38. $removeMagicQuoteFunction = ini_get('magic_quotes_sybase') || strtolower(ini_get('magic_quotes_sybase')) == 'on' ? 'unescapestring__recursive' : 'stripslashes__recursive';
  39. // Save some memory.. (since we don't use these anyway.)
  40. unset($GLOBALS['HTTP_POST_VARS'], $GLOBALS['HTTP_POST_VARS']);
  41. unset($GLOBALS['HTTP_POST_FILES'], $GLOBALS['HTTP_POST_FILES']);
  42. // These keys shouldn't be set...ever.
  43. if (isset($_REQUEST['GLOBALS']) || isset($_COOKIE['GLOBALS']))
  44. die('Invalid request variable.');
  45. // Same goes for numeric keys.
  46. foreach (array_merge(array_keys($_POST), array_keys($_GET), array_keys($_FILES)) as $key)
  47. if (is_numeric($key))
  48. die('Numeric request keys are invalid.');
  49. // Numeric keys in cookies are less of a problem. Just unset those.
  50. foreach ($_COOKIE as $key => $value)
  51. if (is_numeric($key))
  52. unset($_COOKIE[$key]);
  53. // Get the correct query string. It may be in an environment variable...
  54. if (!isset($_SERVER['QUERY_STRING']))
  55. $_SERVER['QUERY_STRING'] = getenv('QUERY_STRING');
  56. // It seems that sticking a URL after the query string is mighty common, well, it's evil - don't.
  57. if (strpos($_SERVER['QUERY_STRING'], 'http') === 0)
  58. {
  59. header('HTTP/1.1 400 Bad Request');
  60. die;
  61. }
  62. // Are we going to need to parse the ; out?
  63. if (strpos(ini_get('arg_separator.input'), ';') === false && !empty($_SERVER['QUERY_STRING']))
  64. {
  65. // Get rid of the old one! You don't know where it's been!
  66. $_GET = array();
  67. // Was this redirected? If so, get the REDIRECT_QUERY_STRING.
  68. // Do not urldecode() the querystring, unless you so much wish to break OpenID implementation. :)
  69. $_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'], 0, 5) === 'url=/' ? $_SERVER['REDIRECT_QUERY_STRING'] : $_SERVER['QUERY_STRING'];
  70. // some german webmailers need a decoded string, so let's decode the string for action=activate and action=reminder
  71. if(strpos($_SERVER['QUERY_STRING'], 'activate') !== false || strpos($_SERVER['QUERY_STRING'], 'reminder') !== false)
  72. $_SERVER['QUERY_STRING'] = urldecode($_SERVER['QUERY_STRING']);
  73. // Replace ';' with '&' and '&something&' with '&something=&'. (this is done for compatibility...)
  74. parse_str(preg_replace('/&(\w+)(?=&|$)/', '&$1=', strtr($_SERVER['QUERY_STRING'], array(';?' => '&', ';' => '&', '%00' => '', "\0" => ''))), $_GET);
  75. // Magic quotes still applies with parse_str - so clean it up.
  76. if (function_exists('get_magic_quotes_gpc') && @get_magic_quotes_gpc() != 0 && empty($modSettings['integrate_magic_quotes']))
  77. $_GET = $removeMagicQuoteFunction($_GET);
  78. }
  79. elseif (strpos(ini_get('arg_separator.input'), ';') !== false)
  80. {
  81. if (function_exists('get_magic_quotes_gpc') && @get_magic_quotes_gpc() != 0 && empty($modSettings['integrate_magic_quotes']))
  82. $_GET = $removeMagicQuoteFunction($_GET);
  83. // Search engines will send action=profile%3Bu=1, which confuses PHP.
  84. foreach ($_GET as $k => $v)
  85. {
  86. if ((string) $v === $v && strpos($k, ';') !== false)
  87. {
  88. $temp = explode(';', $v);
  89. $_GET[$k] = $temp[0];
  90. for ($i = 1, $n = count($temp); $i < $n; $i++)
  91. {
  92. @list ($key, $val) = @explode('=', $temp[$i], 2);
  93. if (!isset($_GET[$key]))
  94. $_GET[$key] = $val;
  95. }
  96. }
  97. // This helps a lot with integration!
  98. if (strpos($k, '?') === 0)
  99. {
  100. $_GET[substr($k, 1)] = $v;
  101. unset($_GET[$k]);
  102. }
  103. }
  104. }
  105. // There's no query string, but there is a URL... try to get the data from there.
  106. if (!empty($_SERVER['REQUEST_URI']))
  107. {
  108. // Remove the .html, assuming there is one.
  109. if (substr($_SERVER['REQUEST_URI'], strrpos($_SERVER['REQUEST_URI'], '.'), 4) == '.htm')
  110. $request = substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], '.'));
  111. else
  112. $request = $_SERVER['REQUEST_URI'];
  113. // @todo smflib.
  114. // Replace 'index.php/a,b,c/d/e,f' with 'a=b,c&d=&e=f' and parse it into $_GET.
  115. if (strpos($request, basename($scripturl) . '/') !== false)
  116. {
  117. parse_str(substr(preg_replace('/&(\w+)(?=&|$)/', '&$1=', strtr(preg_replace('~/([^,/]+),~', '/$1=', substr($request, strpos($request, basename($scripturl)) + strlen(basename($scripturl)))), '/', '&')), 1), $temp);
  118. if (function_exists('get_magic_quotes_gpc') && @get_magic_quotes_gpc() != 0 && empty($modSettings['integrate_magic_quotes']))
  119. $temp = $removeMagicQuoteFunction($temp);
  120. $_GET += $temp;
  121. }
  122. }
  123. // If magic quotes is on we have some work...
  124. if (function_exists('get_magic_quotes_gpc') && @get_magic_quotes_gpc() != 0)
  125. {
  126. $_ENV = $removeMagicQuoteFunction($_ENV);
  127. $_POST = $removeMagicQuoteFunction($_POST);
  128. $_COOKIE = $removeMagicQuoteFunction($_COOKIE);
  129. foreach ($_FILES as $k => $dummy)
  130. if (isset($_FILES[$k]['name']))
  131. $_FILES[$k]['name'] = $removeMagicQuoteFunction($_FILES[$k]['name']);
  132. }
  133. // Add entities to GET. This is kinda like the slashes on everything else.
  134. $_GET = htmlspecialchars__recursive($_GET);
  135. // Let's not depend on the ini settings... why even have COOKIE in there, anyway?
  136. $_REQUEST = $_POST + $_GET;
  137. // Make sure $board and $topic are numbers.
  138. if (isset($_REQUEST['board']))
  139. {
  140. // Make sure its a string and not something else like an array
  141. $_REQUEST['board'] = (string) $_REQUEST['board'];
  142. // If there's a slash in it, we've got a start value! (old, compatible links.)
  143. if (strpos($_REQUEST['board'], '/') !== false)
  144. list ($_REQUEST['board'], $_REQUEST['start']) = explode('/', $_REQUEST['board']);
  145. // Same idea, but dots. This is the currently used format - ?board=1.0...
  146. elseif (strpos($_REQUEST['board'], '.') !== false)
  147. list ($_REQUEST['board'], $_REQUEST['start']) = explode('.', $_REQUEST['board']);
  148. // Now make absolutely sure it's a number.
  149. $board = (int) $_REQUEST['board'];
  150. $_REQUEST['start'] = isset($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0;
  151. // This is for "Who's Online" because it might come via POST - and it should be an int here.
  152. $_GET['board'] = $board;
  153. }
  154. // Well, $board is going to be a number no matter what.
  155. else
  156. $board = 0;
  157. // If there's a threadid, it's probably an old YaBB SE link. Flow with it.
  158. if (isset($_REQUEST['threadid']) && !isset($_REQUEST['topic']))
  159. $_REQUEST['topic'] = $_REQUEST['threadid'];
  160. // We've got topic!
  161. if (isset($_REQUEST['topic']))
  162. {
  163. // Make sure its a string and not something else like an array
  164. $_REQUEST['topic'] = (string) $_REQUEST['topic'];
  165. // Slash means old, beta style, formatting. That's okay though, the link should still work.
  166. if (strpos($_REQUEST['topic'], '/') !== false)
  167. list ($_REQUEST['topic'], $_REQUEST['start']) = explode('/', $_REQUEST['topic']);
  168. // Dots are useful and fun ;). This is ?topic=1.15.
  169. elseif (strpos($_REQUEST['topic'], '.') !== false)
  170. list ($_REQUEST['topic'], $_REQUEST['start']) = explode('.', $_REQUEST['topic']);
  171. $topic = (int) $_REQUEST['topic'];
  172. // Now make sure the online log gets the right number.
  173. $_GET['topic'] = $topic;
  174. }
  175. else
  176. $topic = 0;
  177. // There should be a $_REQUEST['start'], some at least. If you need to default to other than 0, use $_GET['start'].
  178. if (empty($_REQUEST['start']) || $_REQUEST['start'] < 0 || (int) $_REQUEST['start'] > 2147473647)
  179. $_REQUEST['start'] = 0;
  180. // The action needs to be a string and not an array or anything else
  181. if (isset($_REQUEST['action']))
  182. $_REQUEST['action'] = (string) $_REQUEST['action'];
  183. if (isset($_GET['action']))
  184. $_GET['action'] = (string) $_GET['action'];
  185. // Make sure we have a valid REMOTE_ADDR.
  186. if (!isset($_SERVER['REMOTE_ADDR']))
  187. {
  188. $_SERVER['REMOTE_ADDR'] = '';
  189. // A new magic variable to indicate we think this is command line.
  190. $_SERVER['is_cli'] = true;
  191. }
  192. // Perhaps we have a IPv6 address.
  193. elseif (!isValidIPv6($_SERVER['REMOTE_ADDR']) || preg_match('~::ffff:\d+\.\d+\.\d+\.\d+~', $_SERVER['REMOTE_ADDR']) !== 0)
  194. {
  195. $_SERVER['REMOTE_ADDR'] = preg_replace('~^::ffff:(\d+\.\d+\.\d+\.\d+)~', '\1', $_SERVER['REMOTE_ADDR']);
  196. // Just incase we have a legacy IPv4 address.
  197. // @ TODO: Convert to IPv6.
  198. 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['REMOTE_ADDR']) === 0)
  199. $_SERVER['REMOTE_ADDR'] = 'unknown';
  200. }
  201. // Try to calculate their most likely IP for those people behind proxies (And the like).
  202. $_SERVER['BAN_CHECK_IP'] = $_SERVER['REMOTE_ADDR'];
  203. // Find the user's IP address. (but don't let it give you 'unknown'!)
  204. // @ TODO: IPv6 really doesn't need this.
  205. 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|::1|fe80::|fc00::)~', $_SERVER['HTTP_CLIENT_IP']) == 0 || preg_match('~^((0|10|172\.(1[6-9]|2[0-9]|3[01])|192\.168|255|127)\.|unknown|::1|fe80::|fc00::)~', $_SERVER['REMOTE_ADDR']) != 0))
  206. {
  207. // We have both forwarded for AND client IP... check the first forwarded for as the block - only switch if it's better that way.
  208. 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))
  209. $_SERVER['BAN_CHECK_IP'] = implode('.', array_reverse(explode('.', $_SERVER['HTTP_CLIENT_IP'])));
  210. else
  211. $_SERVER['BAN_CHECK_IP'] = $_SERVER['HTTP_CLIENT_IP'];
  212. }
  213. if (!empty($_SERVER['HTTP_CLIENT_IP']) && (preg_match('~^((0|10|172\.(1[6-9]|2[0-9]|3[01])|192\.168|255|127)\.|unknown|::1|fe80::|fc00::)~', $_SERVER['HTTP_CLIENT_IP']) == 0 || preg_match('~^((0|10|172\.(1[6-9]|2[0-9]|3[01])|192\.168|255|127)\.|unknown|::1|fe80::|fc00::)~', $_SERVER['REMOTE_ADDR']) != 0))
  214. {
  215. // Since they are in different blocks, it's probably reversed.
  216. if (strtok($_SERVER['REMOTE_ADDR'], '.') != strtok($_SERVER['HTTP_CLIENT_IP'], '.'))
  217. $_SERVER['BAN_CHECK_IP'] = implode('.', array_reverse(explode('.', $_SERVER['HTTP_CLIENT_IP'])));
  218. else
  219. $_SERVER['BAN_CHECK_IP'] = $_SERVER['HTTP_CLIENT_IP'];
  220. }
  221. elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
  222. {
  223. // If there are commas, get the last one.. probably.
  224. if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ',') !== false)
  225. {
  226. $ips = array_reverse(explode(', ', $_SERVER['HTTP_X_FORWARDED_FOR']));
  227. // Go through each IP...
  228. foreach ($ips as $i => $ip)
  229. {
  230. // Make sure it's in a valid range...
  231. if (preg_match('~^((0|10|172\.(1[6-9]|2[0-9]|3[01])|192\.168|255|127)\.|unknown|::1|fe80::|fc00::)~', $ip) != 0 && preg_match('~^((0|10|172\.(1[6-9]|2[0-9]|3[01])|192\.168|255|127)\.|unknown|::1|fe80::|fc00::)~', $_SERVER['REMOTE_ADDR']) == 0)
  232. continue;
  233. // Otherwise, we've got an IP!
  234. $_SERVER['BAN_CHECK_IP'] = trim($ip);
  235. break;
  236. }
  237. }
  238. // Otherwise just use the only one.
  239. elseif (preg_match('~^((0|10|172\.(1[6-9]|2[0-9]|3[01])|192\.168|255|127)\.|unknown|::1|fe80::|fc00::)~', $_SERVER['HTTP_X_FORWARDED_FOR']) == 0 || preg_match('~^((0|10|172\.(1[6-9]|2[0-9]|3[01])|192\.168|255|127)\.|unknown|::1|fe80::|fc00::)~', $_SERVER['REMOTE_ADDR']) != 0)
  240. $_SERVER['BAN_CHECK_IP'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
  241. }
  242. // Make sure we know the URL of the current request.
  243. if (empty($_SERVER['REQUEST_URI']))
  244. $_SERVER['REQUEST_URL'] = $scripturl . (!empty($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : '');
  245. elseif (preg_match('~^([^/]+//[^/]+)~', $scripturl, $match) == 1)
  246. $_SERVER['REQUEST_URL'] = $match[1] . $_SERVER['REQUEST_URI'];
  247. else
  248. $_SERVER['REQUEST_URL'] = $_SERVER['REQUEST_URI'];
  249. // And make sure HTTP_USER_AGENT is set.
  250. $_SERVER['HTTP_USER_AGENT'] = isset($_SERVER['HTTP_USER_AGENT']) ? htmlspecialchars($smcFunc['db_unescape_string']($_SERVER['HTTP_USER_AGENT']), ENT_QUOTES) : '';
  251. // Some final checking.
  252. 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 || !isValidIPv6($_SERVER['BAN_CHECK_IP']))
  253. $_SERVER['BAN_CHECK_IP'] = '';
  254. if ($_SERVER['REMOTE_ADDR'] == 'unknown')
  255. $_SERVER['REMOTE_ADDR'] = '';
  256. }
  257. /**
  258. * Validates a IPv6 address. returns true if it is ipv6.
  259. *
  260. * @param string $ip ip address to be validated
  261. * @return boolean true|false
  262. */
  263. function isValidIPv6($ip)
  264. {
  265. if (preg_match('~^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))$~', $ip) === 0)
  266. return false;
  267. return true;
  268. }
  269. /**
  270. * Converts IPv6s to numbers. This makes ban checks much easier.
  271. *
  272. * @param string $ip ip address to be converted
  273. * @return array
  274. */
  275. function convertIPv6toInts($ip)
  276. {
  277. static $expanded = array();
  278. // Check if we have done this already.
  279. if (isset($expanded[$ip]))
  280. return $expanded[$ip];
  281. // Expand the IP out.
  282. $expanded_ip = explode(':', expandIPv6($ip));
  283. $new_ip = array();
  284. foreach ($expanded_ip as $int)
  285. $new_ip[] = hexdec($int);
  286. // Save this incase of repeated use.
  287. $expanded[$ip] = $new_ip;
  288. return $expanded[$ip];
  289. }
  290. /**
  291. * Expands a IPv6 address to its full form.
  292. *
  293. * @param type $addr
  294. * @param type $strict_check checks lenght to expaned address for compliance
  295. * @return boolean/string expanded ipv6 address.
  296. */
  297. function expandIPv6($addr, $strict_check = true)
  298. {
  299. static $converted = array();
  300. // Check if we have done this already.
  301. if (isset($converted[$addr]))
  302. return $converted[$addr];
  303. // Check if there are segments missing, insert if necessary.
  304. if (strpos($addr, '::') !== false)
  305. {
  306. $part = explode('::', $addr);
  307. $part[0] = explode(':', $part[0]);
  308. $part[1] = explode(':', $part[1]);
  309. $missing = array();
  310. for ($i = 0; $i < (8 - (count($part[0]) + count($part[1]))); $i++)
  311. array_push($missing, '0000');
  312. $part = array_merge($part[0], $missing, $part[1]);
  313. }
  314. else
  315. $part = explode(':', $addr);
  316. // Pad each segment until it has 4 digits.
  317. foreach ($part as &$p)
  318. while (strlen($p) < 4)
  319. $p = '0' . $p;
  320. unset($p);
  321. // Join segments.
  322. $result = implode(':', $part);
  323. // Save this incase of repeated use.
  324. $converted[$addr] = $result;
  325. // Quick check to make sure the length is as expected.
  326. if (!$strict_check || strlen($result) == 39)
  327. return $result;
  328. else
  329. return false;
  330. }
  331. /**
  332. * Adds slashes to the array/variable.
  333. * What it does:
  334. * - returns the var, as an array or string, with escapes as required.
  335. * - importantly escapes all keys and values!
  336. * - calls itself recursively if necessary.
  337. *
  338. * @param array|string $var
  339. * @return array|string
  340. */
  341. function escapestring__recursive($var)
  342. {
  343. global $smcFunc;
  344. if (!is_array($var))
  345. return $smcFunc['db_escape_string']($var);
  346. // Reindex the array with slashes.
  347. $new_var = array();
  348. // Add slashes to every element, even the indexes!
  349. foreach ($var as $k => $v)
  350. $new_var[$smcFunc['db_escape_string']($k)] = escapestring__recursive($v);
  351. return $new_var;
  352. }
  353. /**
  354. * Adds html entities to the array/variable. Uses two underscores to guard against overloading.
  355. * What it does:
  356. * - adds entities (&quot;, &lt;, &gt;) to the array or string var.
  357. * - importantly, does not effect keys, only values.
  358. * - calls itself recursively if necessary.
  359. *
  360. * @param array|string $var
  361. * @param int $level = 0
  362. * @return array|string
  363. */
  364. function htmlspecialchars__recursive($var, $level = 0)
  365. {
  366. global $smcFunc;
  367. if (!is_array($var))
  368. return isset($smcFunc['htmlspecialchars']) ? $smcFunc['htmlspecialchars']($var, ENT_QUOTES) : htmlspecialchars($var, ENT_QUOTES);
  369. // Add the htmlspecialchars to every element.
  370. foreach ($var as $k => $v)
  371. $var[$k] = $level > 25 ? null : htmlspecialchars__recursive($v, $level + 1);
  372. return $var;
  373. }
  374. /**
  375. * Removes url stuff from the array/variable. Uses two underscores to guard against overloading.
  376. * What it does:
  377. * - takes off url encoding (%20, etc.) from the array or string var.
  378. * - importantly, does it to keys too!
  379. * - calls itself recursively if there are any sub arrays.
  380. *
  381. * @param array|string $var
  382. * @param int $level = 0
  383. * @return array|string
  384. */
  385. function urldecode__recursive($var, $level = 0)
  386. {
  387. if (!is_array($var))
  388. return urldecode($var);
  389. // Reindex the array...
  390. $new_var = array();
  391. // Add the htmlspecialchars to every element.
  392. foreach ($var as $k => $v)
  393. $new_var[urldecode($k)] = $level > 25 ? null : urldecode__recursive($v, $level + 1);
  394. return $new_var;
  395. }
  396. /**
  397. * Unescapes any array or variable. Uses two underscores to guard against overloading.
  398. * What it does:
  399. * - unescapes, recursively, from the array or string var.
  400. * - effects both keys and values of arrays.
  401. * - calls itself recursively to handle arrays of arrays.
  402. *
  403. * @param array|string $var
  404. * @return array|string
  405. */
  406. function unescapestring__recursive($var)
  407. {
  408. global $smcFunc;
  409. if (!is_array($var))
  410. return $smcFunc['db_unescape_string']($var);
  411. // Reindex the array without slashes, this time.
  412. $new_var = array();
  413. // Strip the slashes from every element.
  414. foreach ($var as $k => $v)
  415. $new_var[$smcFunc['db_unescape_string']($k)] = unescapestring__recursive($v);
  416. return $new_var;
  417. }
  418. /**
  419. * Remove slashes recursively. Uses two underscores to guard against overloading.
  420. * What it does:
  421. * - removes slashes, recursively, from the array or string var.
  422. * - effects both keys and values of arrays.
  423. * - calls itself recursively to handle arrays of arrays.
  424. *
  425. * @param array|string $var
  426. * @param int $level = 0
  427. * @return array|string
  428. */
  429. function stripslashes__recursive($var, $level = 0)
  430. {
  431. if (!is_array($var))
  432. return stripslashes($var);
  433. // Reindex the array without slashes, this time.
  434. $new_var = array();
  435. // Strip the slashes from every element.
  436. foreach ($var as $k => $v)
  437. $new_var[stripslashes($k)] = $level > 25 ? null : stripslashes__recursive($v, $level + 1);
  438. return $new_var;
  439. }
  440. /**
  441. * Trim a string including the HTML space, character 160. Uses two underscores to guard against overloading.
  442. * What it does:
  443. * - trims a string or an the var array using html characters as well.
  444. * - does not effect keys, only values.
  445. * - may call itself recursively if needed.
  446. *
  447. * @param array|string $var
  448. * @param int $level = 0
  449. * @return array|string
  450. */
  451. function htmltrim__recursive($var, $level = 0)
  452. {
  453. global $smcFunc;
  454. // Remove spaces (32), tabs (9), returns (13, 10, and 11), nulls (0), and hard spaces. (160)
  455. if (!is_array($var))
  456. return isset($smcFunc) ? $smcFunc['htmltrim']($var) : trim($var, ' ' . "\t\n\r\x0B" . '\0' . "\xA0");
  457. // Go through all the elements and remove the whitespace.
  458. foreach ($var as $k => $v)
  459. $var[$k] = $level > 25 ? null : htmltrim__recursive($v, $level + 1);
  460. return $var;
  461. }
  462. /**
  463. * Clean up the XML to make sure it doesn't contain invalid characters.
  464. * What it does:
  465. * - removes invalid XML characters to assure the input string being
  466. * - parsed properly.
  467. *
  468. * @param string $string
  469. * @return string
  470. */
  471. function cleanXml($string)
  472. {
  473. global $context;
  474. // http://www.w3.org/TR/2000/REC-xml-20001006#NT-Char
  475. return preg_replace('~[\x00-\x08\x0B\x0C\x0E-\x19\x{FFFE}\x{FFFF}]~u', '', $string);
  476. }
  477. /**
  478. * Escapes (replaces) characters in strings to make them safe for use in javascript
  479. *
  480. * @param string $string
  481. * @return string
  482. */
  483. function JavaScriptEscape($string)
  484. {
  485. global $scripturl;
  486. return '\'' . strtr($string, array(
  487. "\r" => '',
  488. "\n" => '\\n',
  489. "\t" => '\\t',
  490. '\\' => '\\\\',
  491. '\'' => '\\\'',
  492. '</' => '<\' + \'/',
  493. '<script' => '<scri\'+\'pt',
  494. '<body>' => '<bo\'+\'dy>',
  495. '<a href' => '<a hr\'+\'ef',
  496. $scripturl => '\' + smf_scripturl + \'',
  497. )) . '\'';
  498. }
  499. /**
  500. * Rewrite URLs to include the session ID.
  501. * What it does:
  502. * - rewrites the URLs outputted to have the session ID, if the user
  503. * is not accepting cookies and is using a standard web browser.
  504. * - handles rewriting URLs for the queryless URLs option.
  505. * - can be turned off entirely by setting $scripturl to an empty
  506. * string, ''. (it wouldn't work well like that anyway.)
  507. * - because of bugs in certain builds of PHP, does not function in
  508. * versions lower than 4.3.0 - please upgrade if this hurts you.
  509. *
  510. * @param string $buffer
  511. * @return string
  512. */
  513. function ob_sessrewrite($buffer)
  514. {
  515. global $scripturl, $modSettings, $user_info, $context;
  516. // If $scripturl is set to nothing, or the SID is not defined (SSI?) just quit.
  517. if ($scripturl == '' || !defined('SID'))
  518. return $buffer;
  519. // 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.
  520. if (empty($_COOKIE) && SID != '' && !isBrowser('possibly_robot'))
  521. $buffer = preg_replace('/"' . preg_quote($scripturl, '/') . '(?!\?' . preg_quote(SID, '/') . ')\\??/', '"' . $scripturl . '?' . SID . '&amp;', $buffer);
  522. // Debugging templates, are we?
  523. elseif (isset($_GET['debug']))
  524. $buffer = preg_replace('/(?<!<link rel="canonical" href=)"' . preg_quote($scripturl, '/') . '\\??/', '"' . $scripturl . '?debug;', $buffer);
  525. // This should work even in 4.2.x, just not CGI without cgi.fix_pathinfo.
  526. if (!empty($modSettings['queryless_urls']) && (!$context['server']['is_cgi'] || ini_get('cgi.fix_pathinfo') == 1 || @get_cfg_var('cgi.fix_pathinfo') == 1) && ($context['server']['is_apache'] || $context['server']['is_lighttpd'] || $context['server']['is_litespeed']))
  527. {
  528. // Let's do something special for session ids!
  529. if (defined('SID') && SID != '')
  530. $buffer = preg_replace('/"' . preg_quote($scripturl, '/') . '\?(?:' . SID . '(?:;|&|&amp;))((?:board|topic)=[^#"]+?)(#[^"]*?)?"/e', "'\"' . \$scripturl . '/' . strtr('\$1', '&;=', '//,') . '.html?' . SID . '\$2\"'", $buffer);
  531. else
  532. $buffer = preg_replace('/"' . preg_quote($scripturl, '/') . '\?((?:board|topic)=[^#"]+?)(#[^"]*?)?"/e', "'\"' . \$scripturl . '/' . strtr('\$1', '&;=', '//,') . '.html\$2\"'", $buffer);
  533. }
  534. // Return the changed buffer.
  535. return $buffer;
  536. }