PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/engine/lib/input.php

https://github.com/masuman/elgg-1
PHP | 524 lines | 439 code | 41 blank | 44 comment | 26 complexity | d45083044101a5e897063343bd7694a7 MD5 | raw file
  1. <?php
  2. /**
  3. * Parameter input functions.
  4. * This file contains functions for getting input from get/post variables.
  5. *
  6. * @package Elgg
  7. * @subpackage Core
  8. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
  9. * @author Curverider Ltd <info@elgg.com>
  10. * @copyright Curverider Ltd 2008-2009
  11. * @link http://elgg.org/
  12. */
  13. /**
  14. * Get some input from variables passed on the GET or POST line.
  15. *
  16. * @param $variable string The variable we want to return.
  17. * @param $default mixed A default value for the variable if it is not found.
  18. * @param $filter_result If true then the result is filtered for bad tags.
  19. */
  20. function get_input($variable, $default = "", $filter_result = true)
  21. {
  22. global $CONFIG;
  23. if (isset($CONFIG->input[$variable]))
  24. return $CONFIG->input[$variable];
  25. if (isset($_REQUEST[$variable])) {
  26. if (is_array($_REQUEST[$variable])) {
  27. $var = $_REQUEST[$variable];
  28. } else {
  29. $var = trim($_REQUEST[$variable]);
  30. }
  31. if ($filter_result)
  32. $var = filter_tags($var);
  33. return $var;
  34. }
  35. return $default;
  36. }
  37. /**
  38. * Sets an input value that may later be retrieved by get_input
  39. *
  40. * @param string $variable The name of the variable
  41. * @param string $value The value of the variable
  42. */
  43. function set_input($variable, $value) {
  44. global $CONFIG;
  45. if (!isset($CONFIG->input))
  46. $CONFIG->input = array();
  47. if (is_array($value))
  48. {
  49. foreach ($value as $key => $val)
  50. $value[$key] = trim($val);
  51. $CONFIG->input[trim($variable)] = $value;
  52. }
  53. else
  54. $CONFIG->input[trim($variable)] = trim($value);
  55. }
  56. /**
  57. * Kses filtering of tags, called on a plugin hook
  58. *
  59. * @param mixed $var Variable to filter
  60. * @return mixed
  61. */
  62. function kses_filter_tags($hook, $entity_type, $returnvalue, $params)
  63. {
  64. $return = $returnvalue;
  65. $var = $returnvalue;
  66. if (@include_once(dirname(dirname(dirname(__FILE__)))) . "/vendors/kses/kses.php") {
  67. global $CONFIG;
  68. $allowedtags = $CONFIG->allowedtags;
  69. $allowedprotocols = $CONFIG->allowedprotocols;
  70. if (!is_array($var)) {
  71. $return = "";
  72. $return = kses($var, $allowedtags, $allowedprotocols);
  73. } else {
  74. $return = array();
  75. foreach($var as $key => $el) {
  76. $return[$key] = kses($el, $allowedtags, $allowedprotocols);
  77. }
  78. }
  79. }
  80. return $return;
  81. }
  82. /**
  83. * Filter tags from a given string based on registered hooks.
  84. * @param $var
  85. * @return mixed The filtered result
  86. */
  87. function filter_tags($var)
  88. {
  89. return trigger_plugin_hook('validate', 'input', null, $var);
  90. }
  91. /**
  92. * Sanitise file paths for input, ensuring that they begin and end with slashes etc.
  93. *
  94. * @param string $path The path
  95. * @return string
  96. */
  97. function sanitise_filepath($path)
  98. {
  99. // Convert to correct UNIX paths
  100. $path = str_replace('\\', '/', $path);
  101. // Sort trailing slash
  102. $path = trim($path);
  103. $path = rtrim($path, " /");
  104. $path = $path . "/";
  105. return $path;
  106. }
  107. /**
  108. * Takes a string and turns any URLs into formatted links
  109. *
  110. * @param string $text The input string
  111. * @return string The output stirng with formatted links
  112. **/
  113. function parse_urls($text) {
  114. return preg_replace_callback('/(?<!=["\'])((ht|f)tps?:\/\/[^\s\r\n\t<>"\'\!\(\)]+)/i',
  115. create_function(
  116. '$matches',
  117. '
  118. $url = $matches[1];
  119. $urltext = str_replace("/", "/<wbr />", $url);
  120. return "<a href=\"$url\" style=\"text-decoration:underline;\">$urltext</a>";
  121. '
  122. ), $text);
  123. }
  124. function autop($pee, $br = 1) {
  125. $pee = $pee . "\n"; // just to make things a little easier, pad the end
  126. $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
  127. // Space things out a little
  128. $allblocks = '(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|map|area|blockquote|address|math|style|input|p|h[1-6]|hr)';
  129. $pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee);
  130. $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
  131. $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
  132. if ( strpos($pee, '<object') !== false ) {
  133. $pee = preg_replace('|\s*<param([^>]*)>\s*|', "<param$1>", $pee); // no pee inside object/embed
  134. $pee = preg_replace('|\s*</embed>\s*|', '</embed>', $pee);
  135. }
  136. $pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
  137. $pee = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "<p>$1</p>\n", $pee); // make paragraphs, including one at the end
  138. $pee = preg_replace('|<p>\s*?</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
  139. $pee = preg_replace('!<p>([^<]+)\s*?(</(?:div|address|form)[^>]*>)!', "<p>$1</p>$2", $pee);
  140. $pee = preg_replace( '|<p>|', "$1<p>", $pee );
  141. $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
  142. $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
  143. $pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
  144. $pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
  145. $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
  146. $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
  147. if ($br) {
  148. $pee = preg_replace_callback('/<(script|style).*?<\/\\1>/s', create_function('$matches', 'return str_replace("\n", "<WPPreserveNewline />", $matches[0]);'), $pee);
  149. $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
  150. $pee = str_replace('<WPPreserveNewline />', "\n", $pee);
  151. }
  152. $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);
  153. $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
  154. if (strpos($pee, '<pre') !== false)
  155. $pee = preg_replace_callback('!(<pre.*?>)(.*?)</pre>!is', 'clean_pre', $pee );
  156. $pee = preg_replace( "|\n</p>$|", '</p>', $pee );
  157. return $pee;
  158. }
  159. function input_init() {
  160. if (ini_get_bool('magic_quotes_gpc') ) {
  161. //do keys as well, cos array_map ignores them
  162. function stripslashes_arraykeys($array) {
  163. if (is_array($array)) {
  164. $array2 = array();
  165. foreach ($array as $key => $data) {
  166. if ($key != stripslashes($key)) {
  167. $array2[stripslashes($key)] = $data;
  168. } else {
  169. $array2[$key] = $data;
  170. }
  171. }
  172. return $array2;
  173. } else {
  174. return $array;
  175. }
  176. }
  177. function stripslashes_deep($value) {
  178. if (is_array($value)) {
  179. $value = stripslashes_arraykeys($value);
  180. $value = array_map('stripslashes_deep', $value);
  181. } else {
  182. $value = stripslashes($value);
  183. }
  184. return $value;
  185. }
  186. $_POST = stripslashes_arraykeys($_POST);
  187. $_GET = stripslashes_arraykeys($_GET);
  188. $_COOKIE = stripslashes_arraykeys($_COOKIE);
  189. $_REQUEST = stripslashes_arraykeys($_REQUEST);
  190. $_POST = array_map('stripslashes_deep', $_POST);
  191. $_GET = array_map('stripslashes_deep', $_GET);
  192. $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
  193. $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
  194. if (!empty($_SERVER['REQUEST_URI'])) {
  195. $_SERVER['REQUEST_URI'] = stripslashes($_SERVER['REQUEST_URI']);
  196. }
  197. if (!empty($_SERVER['QUERY_STRING'])) {
  198. $_SERVER['QUERY_STRING'] = stripslashes($_SERVER['QUERY_STRING']);
  199. }
  200. if (!empty($_SERVER['HTTP_REFERER'])) {
  201. $_SERVER['HTTP_REFERER'] = stripslashes($_SERVER['HTTP_REFERER']);
  202. }
  203. if (!empty($_SERVER['PATH_INFO'])) {
  204. $_SERVER['PATH_INFO'] = stripslashes($_SERVER['PATH_INFO']);
  205. }
  206. if (!empty($_SERVER['PHP_SELF'])) {
  207. $_SERVER['PHP_SELF'] = stripslashes($_SERVER['PHP_SELF']);
  208. }
  209. if (!empty($_SERVER['PATH_TRANSLATED'])) {
  210. $_SERVER['PATH_TRANSLATED'] = stripslashes($_SERVER['PATH_TRANSLATED']);
  211. }
  212. }
  213. global $CONFIG;
  214. $CONFIG->allowedtags = array(
  215. 'address' => array(),
  216. 'a' => array(
  217. 'class' => array (),
  218. 'href' => array (),
  219. 'id' => array (),
  220. 'title' => array (),
  221. 'rel' => array (),
  222. 'rev' => array (),
  223. 'name' => array (),
  224. 'target' => array()),
  225. 'abbr' => array(
  226. 'class' => array (),
  227. 'title' => array ()),
  228. 'acronym' => array(
  229. 'title' => array ()),
  230. 'b' => array(),
  231. 'big' => array(),
  232. 'blockquote' => array(
  233. 'id' => array (),
  234. 'cite' => array (),
  235. 'class' => array(),
  236. 'lang' => array(),
  237. 'xml:lang' => array()),
  238. 'br' => array (
  239. 'class' => array ()),
  240. 'button' => array(
  241. 'disabled' => array (),
  242. 'name' => array (),
  243. 'type' => array (),
  244. 'value' => array ()),
  245. 'caption' => array(
  246. 'align' => array (),
  247. 'class' => array ()),
  248. 'cite' => array (
  249. 'class' => array(),
  250. 'dir' => array(),
  251. 'lang' => array(),
  252. 'title' => array ()),
  253. 'code' => array (
  254. 'style' => array()),
  255. 'col' => array(
  256. 'align' => array (),
  257. 'char' => array (),
  258. 'charoff' => array (),
  259. 'span' => array (),
  260. 'dir' => array(),
  261. 'style' => array (),
  262. 'valign' => array (),
  263. 'width' => array ()),
  264. 'del' => array(
  265. 'datetime' => array ()),
  266. 'dd' => array(),
  267. 'div' => array(
  268. 'align' => array (),
  269. 'class' => array (),
  270. 'id' => array(),
  271. 'dir' => array (),
  272. 'lang' => array(),
  273. 'style' => array (),
  274. 'xml:lang' => array()),
  275. 'dl' => array(),
  276. 'dt' => array(),
  277. 'em' => array(),
  278. 'fieldset' => array(),
  279. 'font' => array(
  280. 'color' => array (),
  281. 'face' => array (),
  282. 'size' => array ()),
  283. 'form' => array(
  284. 'action' => array (),
  285. 'accept' => array (),
  286. 'accept-charset' => array (),
  287. 'enctype' => array (),
  288. 'method' => array (),
  289. 'name' => array (),
  290. 'target' => array ()),
  291. 'h1' => array(
  292. 'align' => array (),
  293. 'class' => array ()),
  294. 'h2' => array(
  295. 'align' => array (),
  296. 'class' => array ()),
  297. 'h3' => array(
  298. 'align' => array (),
  299. 'class' => array ()),
  300. 'h4' => array(
  301. 'align' => array (),
  302. 'class' => array ()),
  303. 'h5' => array(
  304. 'align' => array (),
  305. 'class' => array ()),
  306. 'h6' => array(
  307. 'align' => array (),
  308. 'class' => array ()),
  309. 'input' => array(
  310. 'name' => array(),
  311. 'type' => array(),
  312. 'value' => array(),
  313. 'alt' => array(),
  314. 'src' => array()
  315. ),
  316. 'hr' => array(
  317. 'align' => array (),
  318. 'class' => array (),
  319. 'noshade' => array (),
  320. 'size' => array (),
  321. 'width' => array ()),
  322. 'i' => array(),
  323. 'img' => array(
  324. 'alt' => array (),
  325. 'align' => array (),
  326. 'border' => array (),
  327. 'class' => array (),
  328. 'height' => array (),
  329. 'hspace' => array (),
  330. 'longdesc' => array (),
  331. 'vspace' => array (),
  332. 'src' => array (),
  333. 'style' => array (),
  334. 'width' => array ()),
  335. 'ins' => array(
  336. 'datetime' => array (),
  337. 'cite' => array ()),
  338. 'kbd' => array(),
  339. 'label' => array(
  340. 'for' => array ()),
  341. 'legend' => array(
  342. 'align' => array ()),
  343. 'li' => array (
  344. 'align' => array (),
  345. 'class' => array ()),
  346. 'p' => array(
  347. 'class' => array (),
  348. 'align' => array (),
  349. 'dir' => array(),
  350. 'lang' => array(),
  351. 'style' => array (),
  352. 'xml:lang' => array()),
  353. 'pre' => array(
  354. 'style' => array(),
  355. 'width' => array ()),
  356. 'q' => array(
  357. 'cite' => array ()),
  358. 's' => array(),
  359. 'span' => array (
  360. 'class' => array (),
  361. 'dir' => array (),
  362. 'align' => array (),
  363. 'lang' => array (),
  364. 'style' => array (),
  365. 'title' => array (),
  366. 'xml:lang' => array()),
  367. 'strike' => array(),
  368. 'strong' => array(),
  369. 'sub' => array(),
  370. 'sup' => array(),
  371. 'table' => array(
  372. 'align' => array (),
  373. 'bgcolor' => array (),
  374. 'border' => array (),
  375. 'cellpadding' => array (),
  376. 'cellspacing' => array (),
  377. 'class' => array (),
  378. 'dir' => array(),
  379. 'id' => array(),
  380. 'rules' => array (),
  381. 'style' => array (),
  382. 'summary' => array (),
  383. 'width' => array ()),
  384. 'tbody' => array(
  385. 'align' => array (),
  386. 'char' => array (),
  387. 'charoff' => array (),
  388. 'valign' => array ()),
  389. 'td' => array(
  390. 'abbr' => array (),
  391. 'align' => array (),
  392. 'axis' => array (),
  393. 'bgcolor' => array (),
  394. 'char' => array (),
  395. 'charoff' => array (),
  396. 'class' => array (),
  397. 'colspan' => array (),
  398. 'dir' => array(),
  399. 'headers' => array (),
  400. 'height' => array (),
  401. 'nowrap' => array (),
  402. 'rowspan' => array (),
  403. 'scope' => array (),
  404. 'style' => array (),
  405. 'valign' => array (),
  406. 'width' => array ()),
  407. 'textarea' => array(
  408. 'cols' => array (),
  409. 'rows' => array (),
  410. 'disabled' => array (),
  411. 'name' => array (),
  412. 'readonly' => array ()),
  413. 'tfoot' => array(
  414. 'align' => array (),
  415. 'char' => array (),
  416. 'class' => array (),
  417. 'charoff' => array (),
  418. 'valign' => array ()),
  419. 'th' => array(
  420. 'abbr' => array (),
  421. 'align' => array (),
  422. 'axis' => array (),
  423. 'bgcolor' => array (),
  424. 'char' => array (),
  425. 'charoff' => array (),
  426. 'class' => array (),
  427. 'colspan' => array (),
  428. 'headers' => array (),
  429. 'height' => array (),
  430. 'nowrap' => array (),
  431. 'rowspan' => array (),
  432. 'scope' => array (),
  433. 'valign' => array (),
  434. 'width' => array ()),
  435. 'thead' => array(
  436. 'align' => array (),
  437. 'char' => array (),
  438. 'charoff' => array (),
  439. 'class' => array (),
  440. 'valign' => array ()),
  441. 'title' => array(),
  442. 'tr' => array(
  443. 'align' => array (),
  444. 'bgcolor' => array (),
  445. 'char' => array (),
  446. 'charoff' => array (),
  447. 'class' => array (),
  448. 'style' => array (),
  449. 'valign' => array ()),
  450. 'tt' => array(),
  451. 'u' => array(),
  452. 'ul' => array (
  453. 'id' => array(),
  454. 'class' => array (),
  455. 'style' => array (),
  456. 'type' => array ()),
  457. 'ol' => array (
  458. 'class' => array (),
  459. 'start' => array (),
  460. 'style' => array (),
  461. 'type' => array ()),
  462. 'var' => array (),
  463. 'object' => array('height'=>array(), 'width'=>array()),
  464. 'param' => array('name'=>array(), 'value'=>array()),
  465. 'embed' => array('allowfullscreen'=>array(), 'allowscriptaccess'=>array(),
  466. 'height'=>array(), 'src' => array(), 'type'=>array(), 'width'=>array()),
  467. 'iframe' => array('frameborder'=>array(), 'scrolling'=>array(), 'style'=>array(),
  468. 'height'=>array(), 'src' => array(), 'type'=>array(), 'width'=>array())
  469. );
  470. $CONFIG->allowedprotocols = array('http', 'https', 'ftp', 'news', 'mailto', 'rtsp', 'teamspeak', 'gopher', 'mms',
  471. 'color', 'callto', 'cursor', 'text-align', 'font-size', 'font-weight', 'font-style',
  472. 'border', 'margin', 'padding', 'float');
  473. // For now, register the kses for processing
  474. register_plugin_hook('validate', 'input', 'kses_filter_tags', 1);
  475. }
  476. register_elgg_event_handler('init','system','input_init');
  477. ?>