PageRenderTime 95ms CodeModel.GetById 36ms RepoModel.GetById 6ms app.codeStats 0ms

/system/core/Security.php

https://bitbucket.org/naando_araujo/pagseguro
PHP | 820 lines | 661 code | 37 blank | 122 comment | 11 complexity | a53d132f68c5d669673d4822dae46086 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * Security Class
  18. *
  19. * @package CodeIgniter
  20. * @subpackage Libraries
  21. * @category Security
  22. * @author ExpressionEngine Dev Team
  23. * @link http://codeigniter.com/user_guide/libraries/security.html
  24. */
  25. class CI_Security {
  26. protected $_xss_hash = '';
  27. protected $_csrf_hash = '';
  28. protected $_csrf_expire = 7200; // Two hours (in seconds)
  29. protected $_csrf_token_name = 'ci_csrf_token';
  30. protected $_csrf_cookie_name = 'ci_csrf_token';
  31. /* never allowed, string replacement */
  32. protected $_never_allowed_str = array(
  33. 'document.cookie' => '[removed]',
  34. 'document.write' => '[removed]',
  35. '.parentNode' => '[removed]',
  36. '.innerHTML' => '[removed]',
  37. 'window.location' => '[removed]',
  38. '-moz-binding' => '[removed]',
  39. '<!--' => '&lt;!--',
  40. '-->' => '--&gt;',
  41. '<![CDATA[' => '&lt;![CDATA['
  42. );
  43. /* never allowed, regex replacement */
  44. protected $_never_allowed_regex = array(
  45. "javascript\s*:" => '[removed]',
  46. "expression\s*(\(|&\#40;)" => '[removed]', // CSS and IE
  47. "vbscript\s*:" => '[removed]', // IE, surprise!
  48. "Redirect\s+302" => '[removed]'
  49. );
  50. /**
  51. * Constructor
  52. */
  53. public function __construct()
  54. {
  55. // Append application specific cookie prefix to token name
  56. $this->_csrf_cookie_name = (config_item('cookie_prefix')) ? config_item('cookie_prefix').$this->_csrf_token_name : $this->_csrf_token_name;
  57. // Set the CSRF hash
  58. $this->_csrf_set_hash();
  59. log_message('debug', "Security Class Initialized");
  60. }
  61. // --------------------------------------------------------------------
  62. /**
  63. * Verify Cross Site Request Forgery Protection
  64. *
  65. * @return object
  66. */
  67. public function csrf_verify()
  68. {
  69. // If no POST data exists we will set the CSRF cookie
  70. if (count($_POST) == 0)
  71. {
  72. return $this->csrf_set_cookie();
  73. }
  74. // Do the tokens exist in both the _POST and _COOKIE arrays?
  75. if ( ! isset($_POST[$this->_csrf_token_name]) OR
  76. ! isset($_COOKIE[$this->_csrf_cookie_name]))
  77. {
  78. $this->csrf_show_error();
  79. }
  80. // Do the tokens match?
  81. if ($_POST[$this->_csrf_token_name] != $_COOKIE[$this->_csrf_cookie_name])
  82. {
  83. $this->csrf_show_error();
  84. }
  85. // We kill this since we're done and we don't want to
  86. // polute the _POST array
  87. unset($_POST[$this->_csrf_token_name]);
  88. // Nothing should last forever
  89. unset($_COOKIE[$this->_csrf_cookie_name]);
  90. $this->_csrf_set_hash();
  91. $this->csrf_set_cookie();
  92. log_message('debug', "CSRF token verified ");
  93. return $this;
  94. }
  95. // --------------------------------------------------------------------
  96. /**
  97. * Set Cross Site Request Forgery Protection Cookie
  98. *
  99. * @return object
  100. */
  101. public function csrf_set_cookie()
  102. {
  103. $expire = time() + $this->_csrf_expire;
  104. $secure_cookie = (config_item('cookie_secure') === TRUE) ? 1 : 0;
  105. if ($secure_cookie)
  106. {
  107. $req = isset($_SERVER['HTTPS']) ? $_SERVER['HTTPS'] : FALSE;
  108. if ( ! $req OR $req == 'off')
  109. {
  110. return FALSE;
  111. }
  112. }
  113. setcookie($this->_csrf_cookie_name, $this->_csrf_hash, $expire, config_item('cookie_path'), config_item('cookie_domain'), $secure_cookie);
  114. log_message('debug', "CRSF cookie Set");
  115. return $this;
  116. }
  117. // --------------------------------------------------------------------
  118. /**
  119. * Show CSRF Error
  120. *
  121. * @return void
  122. */
  123. public function csrf_show_error()
  124. {
  125. show_error('The action you have requested is not allowed.');
  126. }
  127. // --------------------------------------------------------------------
  128. /**
  129. * Get CSRF Hash
  130. *
  131. * Getter Method
  132. *
  133. * @return string self::_csrf_hash
  134. */
  135. public function get_csrf_hash()
  136. {
  137. return $this->_csrf_hash;
  138. }
  139. // --------------------------------------------------------------------
  140. /**
  141. * Get CSRF Token Name
  142. *
  143. * Getter Method
  144. *
  145. * @return string self::csrf_token_name
  146. */
  147. public function get_csrf_token_name()
  148. {
  149. return $this->_csrf_token_name;
  150. }
  151. // --------------------------------------------------------------------
  152. /**
  153. * XSS Clean
  154. *
  155. * Sanitizes data so that Cross Site Scripting Hacks can be
  156. * prevented. This function does a fair amount of work but
  157. * it is extremely thorough, designed to prevent even the
  158. * most obscure XSS attempts. Nothing is ever 100% foolproof,
  159. * of course, but I haven't been able to get anything passed
  160. * the filter.
  161. *
  162. * Note: This function should only be used to deal with data
  163. * upon submission. It's not something that should
  164. * be used for general runtime processing.
  165. *
  166. * This function was based in part on some code and ideas I
  167. * got from Bitflux: http://channel.bitflux.ch/wiki/XSS_Prevention
  168. *
  169. * To help develop this script I used this great list of
  170. * vulnerabilities along with a few other hacks I've
  171. * harvested from examining vulnerabilities in other programs:
  172. * http://ha.ckers.org/xss.html
  173. *
  174. * @param mixed string or array
  175. * @return string
  176. */
  177. public function xss_clean($str, $is_image = FALSE)
  178. {
  179. /*
  180. * Is the string an array?
  181. *
  182. */
  183. if (is_array($str))
  184. {
  185. while (list($key) = each($str))
  186. {
  187. $str[$key] = $this->xss_clean($str[$key]);
  188. }
  189. return $str;
  190. }
  191. /*
  192. * Remove Invisible Characters
  193. */
  194. $str = remove_invisible_characters($str);
  195. // Validate Entities in URLs
  196. $str = $this->_validate_entities($str);
  197. /*
  198. * URL Decode
  199. *
  200. * Just in case stuff like this is submitted:
  201. *
  202. * <a href="http://%77%77%77%2E%67%6F%6F%67%6C%65%2E%63%6F%6D">Google</a>
  203. *
  204. * Note: Use rawurldecode() so it does not remove plus signs
  205. *
  206. */
  207. $str = rawurldecode($str);
  208. /*
  209. * Convert character entities to ASCII
  210. *
  211. * This permits our tests below to work reliably.
  212. * We only convert entities that are within tags since
  213. * these are the ones that will pose security problems.
  214. *
  215. */
  216. $str = preg_replace_callback("/[a-z]+=([\'\"]).*?\\1/si", array($this, '_convert_attribute'), $str);
  217. $str = preg_replace_callback("/<\w+.*?(?=>|<|$)/si", array($this, '_decode_entity'), $str);
  218. /*
  219. * Remove Invisible Characters Again!
  220. */
  221. $str = remove_invisible_characters($str);
  222. /*
  223. * Convert all tabs to spaces
  224. *
  225. * This prevents strings like this: ja vascript
  226. * NOTE: we deal with spaces between characters later.
  227. * NOTE: preg_replace was found to be amazingly slow here on
  228. * large blocks of data, so we use str_replace.
  229. */
  230. if (strpos($str, "\t") !== FALSE)
  231. {
  232. $str = str_replace("\t", ' ', $str);
  233. }
  234. /*
  235. * Capture converted string for later comparison
  236. */
  237. $converted_string = $str;
  238. // Remove Strings that are never allowed
  239. $str = $this->_do_never_allowed($str);
  240. /*
  241. * Makes PHP tags safe
  242. *
  243. * Note: XML tags are inadvertently replaced too:
  244. *
  245. * <?xml
  246. *
  247. * But it doesn't seem to pose a problem.
  248. */
  249. if ($is_image === TRUE)
  250. {
  251. // Images have a tendency to have the PHP short opening and
  252. // closing tags every so often so we skip those and only
  253. // do the long opening tags.
  254. $str = preg_replace('/<\?(php)/i', "&lt;?\\1", $str);
  255. }
  256. else
  257. {
  258. $str = str_replace(array('<?', '?'.'>'), array('&lt;?', '?&gt;'), $str);
  259. }
  260. /*
  261. * Compact any exploded words
  262. *
  263. * This corrects words like: j a v a s c r i p t
  264. * These words are compacted back to their correct state.
  265. */
  266. $words = array(
  267. 'javascript', 'expression', 'vbscript', 'script',
  268. 'applet', 'alert', 'document', 'write', 'cookie', 'window'
  269. );
  270. foreach ($words as $word)
  271. {
  272. $temp = '';
  273. for ($i = 0, $wordlen = strlen($word); $i < $wordlen; $i++)
  274. {
  275. $temp .= substr($word, $i, 1)."\s*";
  276. }
  277. // We only want to do this when it is followed by a non-word character
  278. // That way valid stuff like "dealer to" does not become "dealerto"
  279. $str = preg_replace_callback('#('.substr($temp, 0, -3).')(\W)#is', array($this, '_compact_exploded_words'), $str);
  280. }
  281. /*
  282. * Remove disallowed Javascript in links or img tags
  283. * We used to do some version comparisons and use of stripos for PHP5,
  284. * but it is dog slow compared to these simplified non-capturing
  285. * preg_match(), especially if the pattern exists in the string
  286. */
  287. do
  288. {
  289. $original = $str;
  290. if (preg_match("/<a/i", $str))
  291. {
  292. $str = preg_replace_callback("#<a\s+([^>]*?)(>|$)#si", array($this, '_js_link_removal'), $str);
  293. }
  294. if (preg_match("/<img/i", $str))
  295. {
  296. $str = preg_replace_callback("#<img\s+([^>]*?)(\s?/?>|$)#si", array($this, '_js_img_removal'), $str);
  297. }
  298. if (preg_match("/script/i", $str) OR preg_match("/xss/i", $str))
  299. {
  300. $str = preg_replace("#<(/*)(script|xss)(.*?)\>#si", '[removed]', $str);
  301. }
  302. }
  303. while($original != $str);
  304. unset($original);
  305. // Remove evil attributes such as style, onclick and xmlns
  306. $str = $this->_remove_evil_attributes($str, $is_image);
  307. /*
  308. * Sanitize naughty HTML elements
  309. *
  310. * If a tag containing any of the words in the list
  311. * below is found, the tag gets converted to entities.
  312. *
  313. * So this: <blink>
  314. * Becomes: &lt;blink&gt;
  315. */
  316. $naughty = 'alert|applet|audio|basefont|base|behavior|bgsound|blink|body|embed|expression|form|frameset|frame|head|html|ilayer|iframe|input|isindex|layer|link|meta|object|plaintext|style|script|textarea|title|video|xml|xss';
  317. $str = preg_replace_callback('#<(/*\s*)('.$naughty.')([^><]*)([><]*)#is', array($this, '_sanitize_naughty_html'), $str);
  318. /*
  319. * Sanitize naughty scripting elements
  320. *
  321. * Similar to above, only instead of looking for
  322. * tags it looks for PHP and JavaScript commands
  323. * that are disallowed. Rather than removing the
  324. * code, it simply converts the parenthesis to entities
  325. * rendering the code un-executable.
  326. *
  327. * For example: eval('some code')
  328. * Becomes: eval&#40;'some code'&#41;
  329. */
  330. $str = preg_replace('#(alert|cmd|passthru|eval|exec|expression|system|fopen|fsockopen|file|file_get_contents|readfile|unlink)(\s*)\((.*?)\)#si', "\\1\\2&#40;\\3&#41;", $str);
  331. // Final clean up
  332. // This adds a bit of extra precaution in case
  333. // something got through the above filters
  334. $str = $this->_do_never_allowed($str);
  335. /*
  336. * Images are Handled in a Special Way
  337. * - Essentially, we want to know that after all of the character
  338. * conversion is done whether any unwanted, likely XSS, code was found.
  339. * If not, we return TRUE, as the image is clean.
  340. * However, if the string post-conversion does not matched the
  341. * string post-removal of XSS, then it fails, as there was unwanted XSS
  342. * code found and removed/changed during processing.
  343. */
  344. if ($is_image === TRUE)
  345. {
  346. return ($str == $converted_string) ? TRUE: FALSE;
  347. }
  348. log_message('debug', "XSS Filtering completed");
  349. return $str;
  350. }
  351. // --------------------------------------------------------------------
  352. /**
  353. * Random Hash for protecting URLs
  354. *
  355. * @return string
  356. */
  357. public function xss_hash()
  358. {
  359. if ($this->_xss_hash == '')
  360. {
  361. if (phpversion() >= 4.2)
  362. {
  363. mt_srand();
  364. }
  365. else
  366. {
  367. mt_srand(hexdec(substr(md5(microtime()), -8)) & 0x7fffffff);
  368. }
  369. $this->_xss_hash = md5(time() + mt_rand(0, 1999999999));
  370. }
  371. return $this->_xss_hash;
  372. }
  373. // --------------------------------------------------------------------
  374. /**
  375. * HTML Entities Decode
  376. *
  377. * This function is a replacement for html_entity_decode()
  378. *
  379. * In some versions of PHP the native function does not work
  380. * when UTF-8 is the specified character set, so this gives us
  381. * a work-around. More info here:
  382. * http://bugs.php.net/bug.php?id=25670
  383. *
  384. * NOTE: html_entity_decode() has a bug in some PHP versions when UTF-8 is the
  385. * character set, and the PHP developers said they were not back porting the
  386. * fix to versions other than PHP 5.x.
  387. *
  388. * @param string
  389. * @param string
  390. * @return string
  391. */
  392. public function entity_decode($str, $charset='UTF-8')
  393. {
  394. if (stristr($str, '&') === FALSE) return $str;
  395. // The reason we are not using html_entity_decode() by itself is because
  396. // while it is not technically correct to leave out the semicolon
  397. // at the end of an entity most browsers will still interpret the entity
  398. // correctly. html_entity_decode() does not convert entities without
  399. // semicolons, so we are left with our own little solution here. Bummer.
  400. if (function_exists('html_entity_decode') &&
  401. (strtolower($charset) != 'utf-8'))
  402. {
  403. $str = html_entity_decode($str, ENT_COMPAT, $charset);
  404. $str = preg_replace('~&#x(0*[0-9a-f]{2,5})~ei', 'chr(hexdec("\\1"))', $str);
  405. return preg_replace('~&#([0-9]{2,4})~e', 'chr(\\1)', $str);
  406. }
  407. // Numeric Entities
  408. $str = preg_replace('~&#x(0*[0-9a-f]{2,5});{0,1}~ei', 'chr(hexdec("\\1"))', $str);
  409. $str = preg_replace('~&#([0-9]{2,4});{0,1}~e', 'chr(\\1)', $str);
  410. // Literal Entities - Slightly slow so we do another check
  411. if (stristr($str, '&') === FALSE)
  412. {
  413. $str = strtr($str, array_flip(get_html_translation_table(HTML_ENTITIES)));
  414. }
  415. return $str;
  416. }
  417. // --------------------------------------------------------------------
  418. /**
  419. * Filename Security
  420. *
  421. * @param string
  422. * @return string
  423. */
  424. public function sanitize_filename($str, $relative_path = FALSE)
  425. {
  426. $bad = array(
  427. "../",
  428. "<!--",
  429. "-->",
  430. "<",
  431. ">",
  432. "'",
  433. '"',
  434. '&',
  435. '$',
  436. '#',
  437. '{',
  438. '}',
  439. '[',
  440. ']',
  441. '=',
  442. ';',
  443. '?',
  444. "%20",
  445. "%22",
  446. "%3c", // <
  447. "%253c", // <
  448. "%3e", // >
  449. "%0e", // >
  450. "%28", // (
  451. "%29", // )
  452. "%2528", // (
  453. "%26", // &
  454. "%24", // $
  455. "%3f", // ?
  456. "%3b", // ;
  457. "%3d" // =
  458. );
  459. if ( ! $relative_path)
  460. {
  461. $bad[] = './';
  462. $bad[] = '/';
  463. }
  464. $str = remove_invisible_characters($str, FALSE);
  465. return stripslashes(str_replace($bad, '', $str));
  466. }
  467. // ----------------------------------------------------------------
  468. /**
  469. * Compact Exploded Words
  470. *
  471. * Callback function for xss_clean() to remove whitespace from
  472. * things like j a v a s c r i p t
  473. *
  474. * @param type
  475. * @return type
  476. */
  477. protected function _compact_exploded_words($matches)
  478. {
  479. return preg_replace('/\s+/s', '', $matches[1]).$matches[2];
  480. }
  481. // --------------------------------------------------------------------
  482. /*
  483. * Remove Evil HTML Attributes (like evenhandlers and style)
  484. *
  485. * It removes the evil attribute and either:
  486. * - Everything up until a space
  487. * For example, everything between the pipes:
  488. * <a |style=document.write('hello');alert('world');| class=link>
  489. * - Everything inside the quotes
  490. * For example, everything between the pipes:
  491. * <a |style="document.write('hello'); alert('world');"| class="link">
  492. *
  493. * @param string $str The string to check
  494. * @param boolean $is_image TRUE if this is an image
  495. * @return string The string with the evil attributes removed
  496. */
  497. protected function _remove_evil_attributes($str, $is_image)
  498. {
  499. // All javascript event handlers (e.g. onload, onclick, onmouseover), style, and xmlns
  500. $evil_attributes = array('on\w*', 'style', 'xmlns');
  501. if ($is_image === TRUE)
  502. {
  503. /*
  504. * Adobe Photoshop puts XML metadata into JFIF images,
  505. * including namespacing, so we have to allow this for images.
  506. */
  507. unset($evil_attributes[array_search('xmlns', $evil_attributes)]);
  508. }
  509. do {
  510. $str = preg_replace(
  511. "#<(/?[^><]+?)([^A-Za-z\-])(".implode('|', $evil_attributes).")(\s*=\s*)([\"][^>]*?[\"]|[\'][^>]*?[\']|[^>]*?)([\s><])([><]*)#i",
  512. "<$1$6",
  513. $str, -1, $count
  514. );
  515. } while ($count);
  516. return $str;
  517. }
  518. // --------------------------------------------------------------------
  519. /**
  520. * Sanitize Naughty HTML
  521. *
  522. * Callback function for xss_clean() to remove naughty HTML elements
  523. *
  524. * @param array
  525. * @return string
  526. */
  527. protected function _sanitize_naughty_html($matches)
  528. {
  529. // encode opening brace
  530. $str = '&lt;'.$matches[1].$matches[2].$matches[3];
  531. // encode captured opening or closing brace to prevent recursive vectors
  532. $str .= str_replace(array('>', '<'), array('&gt;', '&lt;'),
  533. $matches[4]);
  534. return $str;
  535. }
  536. // --------------------------------------------------------------------
  537. /**
  538. * JS Link Removal
  539. *
  540. * Callback function for xss_clean() to sanitize links
  541. * This limits the PCRE backtracks, making it more performance friendly
  542. * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in
  543. * PHP 5.2+ on link-heavy strings
  544. *
  545. * @param array
  546. * @return string
  547. */
  548. protected function _js_link_removal($match)
  549. {
  550. $attributes = $this->_filter_attributes(str_replace(array('<', '>'), '', $match[1]));
  551. return str_replace($match[1], preg_replace("#href=.*?(alert\(|alert&\#40;|javascript\:|livescript\:|mocha\:|charset\=|window\.|document\.|\.cookie|<script|<xss|base64\s*,)#si", "", $attributes), $match[0]);
  552. }
  553. // --------------------------------------------------------------------
  554. /**
  555. * JS Image Removal
  556. *
  557. * Callback function for xss_clean() to sanitize image tags
  558. * This limits the PCRE backtracks, making it more performance friendly
  559. * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in
  560. * PHP 5.2+ on image tag heavy strings
  561. *
  562. * @param array
  563. * @return string
  564. */
  565. protected function _js_img_removal($match)
  566. {
  567. $attributes = $this->_filter_attributes(str_replace(array('<', '>'), '', $match[1]));
  568. return str_replace($match[1], preg_replace("#src=.*?(alert\(|alert&\#40;|javascript\:|livescript\:|mocha\:|charset\=|window\.|document\.|\.cookie|<script|<xss|base64\s*,)#si", "", $attributes), $match[0]);
  569. }
  570. // --------------------------------------------------------------------
  571. /**
  572. * Attribute Conversion
  573. *
  574. * Used as a callback for XSS Clean
  575. *
  576. * @param array
  577. * @return string
  578. */
  579. protected function _convert_attribute($match)
  580. {
  581. return str_replace(array('>', '<', '\\'), array('&gt;', '&lt;', '\\\\'), $match[0]);
  582. }
  583. // --------------------------------------------------------------------
  584. /**
  585. * Filter Attributes
  586. *
  587. * Filters tag attributes for consistency and safety
  588. *
  589. * @param string
  590. * @return string
  591. */
  592. protected function _filter_attributes($str)
  593. {
  594. $out = '';
  595. if (preg_match_all('#\s*[a-z\-]+\s*=\s*(\042|\047)([^\\1]*?)\\1#is', $str, $matches))
  596. {
  597. foreach ($matches[0] as $match)
  598. {
  599. $out .= preg_replace("#/\*.*?\*/#s", '', $match);
  600. }
  601. }
  602. return $out;
  603. }
  604. // --------------------------------------------------------------------
  605. /**
  606. * HTML Entity Decode Callback
  607. *
  608. * Used as a callback for XSS Clean
  609. *
  610. * @param array
  611. * @return string
  612. */
  613. protected function _decode_entity($match)
  614. {
  615. return $this->entity_decode($match[0], strtoupper(config_item('charset')));
  616. }
  617. // --------------------------------------------------------------------
  618. /**
  619. * Validate URL entities
  620. *
  621. * Called by xss_clean()
  622. *
  623. * @param string
  624. * @return string
  625. */
  626. protected function _validate_entities($str)
  627. {
  628. /*
  629. * Protect GET variables in URLs
  630. */
  631. // 901119URL5918AMP18930PROTECT8198
  632. $str = preg_replace('|\&([a-z\_0-9\-]+)\=([a-z\_0-9\-]+)|i', $this->xss_hash()."\\1=\\2", $str);
  633. /*
  634. * Validate standard character entities
  635. *
  636. * Add a semicolon if missing. We do this to enable
  637. * the conversion of entities to ASCII later.
  638. *
  639. */
  640. $str = preg_replace('#(&\#?[0-9a-z]{2,})([\x00-\x20])*;?#i', "\\1;\\2", $str);
  641. /*
  642. * Validate UTF16 two byte encoding (x00)
  643. *
  644. * Just as above, adds a semicolon if missing.
  645. *
  646. */
  647. $str = preg_replace('#(&\#x?)([0-9A-F]+);?#i',"\\1\\2;",$str);
  648. /*
  649. * Un-Protect GET variables in URLs
  650. */
  651. $str = str_replace($this->xss_hash(), '&', $str);
  652. return $str;
  653. }
  654. // ----------------------------------------------------------------------
  655. /**
  656. * Do Never Allowed
  657. *
  658. * A utility function for xss_clean()
  659. *
  660. * @param string
  661. * @return string
  662. */
  663. protected function _do_never_allowed($str)
  664. {
  665. foreach ($this->_never_allowed_str as $key => $val)
  666. {
  667. $str = str_replace($key, $val, $str);
  668. }
  669. foreach ($this->_never_allowed_regex as $key => $val)
  670. {
  671. $str = preg_replace("#".$key."#i", $val, $str);
  672. }
  673. return $str;
  674. }
  675. // --------------------------------------------------------------------
  676. /**
  677. * Set Cross Site Request Forgery Protection Cookie
  678. *
  679. * @return string
  680. */
  681. protected function _csrf_set_hash()
  682. {
  683. if ($this->_csrf_hash == '')
  684. {
  685. // If the cookie exists we will use it's value.
  686. // We don't necessarily want to regenerate it with
  687. // each page load since a page could contain embedded
  688. // sub-pages causing this feature to fail
  689. if (isset($_COOKIE[$this->_csrf_cookie_name]) &&
  690. $_COOKIE[$this->_csrf_cookie_name] != '')
  691. {
  692. return $this->_csrf_hash = $_COOKIE[$this->_csrf_cookie_name];
  693. }
  694. return $this->_csrf_hash = md5(uniqid(rand(), TRUE));
  695. }
  696. return $this->_csrf_hash;
  697. }
  698. }
  699. // END Security Class
  700. /* End of file Security.php */
  701. /* Location: ./system/libraries/Security.php */