PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/sp_1/system/core/Input.php

https://gitlab.com/rezaul007/Hospital-information-bank
PHP | 887 lines | 424 code | 111 blank | 352 comment | 65 complexity | 331c6f7252e3c0d76cf51f55523ef2c7 MD5 | raw file
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2015, British Columbia Institute of Technology
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * @package CodeIgniter
  30. * @author EllisLab Dev Team
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://opensource.org/licenses/MIT MIT License
  34. * @link http://codeigniter.com
  35. * @since Version 1.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * Input Class
  41. *
  42. * Pre-processes global input data for security
  43. *
  44. * @package CodeIgniter
  45. * @subpackage Libraries
  46. * @category Input
  47. * @author EllisLab Dev Team
  48. * @link http://codeigniter.com/user_guide/libraries/input.html
  49. */
  50. class CI_Input {
  51. /**
  52. * IP address of the current user
  53. *
  54. * @var string
  55. */
  56. protected $ip_address = FALSE;
  57. /**
  58. * Allow GET array flag
  59. *
  60. * If set to FALSE, then $_GET will be set to an empty array.
  61. *
  62. * @var bool
  63. */
  64. protected $_allow_get_array = TRUE;
  65. /**
  66. * Standardize new lines flag
  67. *
  68. * If set to TRUE, then newlines are standardized.
  69. *
  70. * @var bool
  71. */
  72. protected $_standardize_newlines;
  73. /**
  74. * Enable XSS flag
  75. *
  76. * Determines whether the XSS filter is always active when
  77. * GET, POST or COOKIE data is encountered.
  78. * Set automatically based on config setting.
  79. *
  80. * @var bool
  81. */
  82. protected $_enable_xss = FALSE;
  83. /**
  84. * Enable CSRF flag
  85. *
  86. * Enables a CSRF cookie token to be set.
  87. * Set automatically based on config setting.
  88. *
  89. * @var bool
  90. */
  91. protected $_enable_csrf = FALSE;
  92. /**
  93. * List of all HTTP request headers
  94. *
  95. * @var array
  96. */
  97. protected $headers = array();
  98. /**
  99. * Raw input stream data
  100. *
  101. * Holds a cache of php://input contents
  102. *
  103. * @var string
  104. */
  105. protected $_raw_input_stream;
  106. /**
  107. * Parsed input stream data
  108. *
  109. * Parsed from php://input at runtime
  110. *
  111. * @see CI_Input::input_stream()
  112. * @var array
  113. */
  114. protected $_input_stream;
  115. protected $security;
  116. protected $uni;
  117. // --------------------------------------------------------------------
  118. /**
  119. * Class constructor
  120. *
  121. * Determines whether to globally enable the XSS processing
  122. * and whether to allow the $_GET array.
  123. *
  124. * @return void
  125. */
  126. public function __construct()
  127. {
  128. $this->_allow_get_array = (config_item('allow_get_array') === TRUE);
  129. $this->_enable_xss = (config_item('global_xss_filtering') === TRUE);
  130. $this->_enable_csrf = (config_item('csrf_protection') === TRUE);
  131. $this->_standardize_newlines = (bool) config_item('standardize_newlines');
  132. $this->security =& load_class('Security', 'core');
  133. // Do we need the UTF-8 class?
  134. if (UTF8_ENABLED === TRUE)
  135. {
  136. $this->uni =& load_class('Utf8', 'core');
  137. }
  138. // Sanitize global arrays
  139. $this->_sanitize_globals();
  140. log_message('info', 'Input Class Initialized');
  141. }
  142. // --------------------------------------------------------------------
  143. /**
  144. * Fetch from array
  145. *
  146. * Internal method used to retrieve values from global arrays.
  147. *
  148. * @param array &$array $_GET, $_POST, $_COOKIE, $_SERVER, etc.
  149. * @param mixed $index Index for item to be fetched from $array
  150. * @param bool $xss_clean Whether to apply XSS filtering
  151. * @return mixed
  152. */
  153. protected function _fetch_from_array(&$array, $index = NULL, $xss_clean = NULL)
  154. {
  155. is_bool($xss_clean) OR $xss_clean = $this->_enable_xss;
  156. // If $index is NULL, it means that the whole $array is requested
  157. isset($index) OR $index = array_keys($array);
  158. // allow fetching multiple keys at once
  159. if (is_array($index))
  160. {
  161. $output = array();
  162. foreach ($index as $key)
  163. {
  164. $output[$key] = $this->_fetch_from_array($array, $key, $xss_clean);
  165. }
  166. return $output;
  167. }
  168. if (isset($array[$index]))
  169. {
  170. $value = $array[$index];
  171. }
  172. elseif (($count = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $index, $matches)) > 1) // Does the index contain array notation
  173. {
  174. $value = $array;
  175. for ($i = 0; $i < $count; $i++)
  176. {
  177. $key = trim($matches[0][$i], '[]');
  178. if ($key === '') // Empty notation will return the value as array
  179. {
  180. break;
  181. }
  182. if (isset($value[$key]))
  183. {
  184. $value = $value[$key];
  185. }
  186. else
  187. {
  188. return NULL;
  189. }
  190. }
  191. }
  192. else
  193. {
  194. return NULL;
  195. }
  196. return ($xss_clean === TRUE)
  197. ? $this->security->xss_clean($value)
  198. : $value;
  199. }
  200. // --------------------------------------------------------------------
  201. /**
  202. * Fetch an item from the GET array
  203. *
  204. * @param mixed $index Index for item to be fetched from $_GET
  205. * @param bool $xss_clean Whether to apply XSS filtering
  206. * @return mixed
  207. */
  208. public function get($index = NULL, $xss_clean = NULL)
  209. {
  210. return $this->_fetch_from_array($_GET, $index, $xss_clean);
  211. }
  212. // --------------------------------------------------------------------
  213. /**
  214. * Fetch an item from the POST array
  215. *
  216. * @param mixed $index Index for item to be fetched from $_POST
  217. * @param bool $xss_clean Whether to apply XSS filtering
  218. * @return mixed
  219. */
  220. public function post($index = NULL, $xss_clean = NULL)
  221. {
  222. return $this->_fetch_from_array($_POST, $index, $xss_clean);
  223. }
  224. // --------------------------------------------------------------------
  225. /**
  226. * Fetch an item from POST data with fallback to GET
  227. *
  228. * @param string $index Index for item to be fetched from $_POST or $_GET
  229. * @param bool $xss_clean Whether to apply XSS filtering
  230. * @return mixed
  231. */
  232. public function post_get($index, $xss_clean = NULL)
  233. {
  234. return isset($_POST[$index])
  235. ? $this->post($index, $xss_clean)
  236. : $this->get($index, $xss_clean);
  237. }
  238. // --------------------------------------------------------------------
  239. /**
  240. * Fetch an item from GET data with fallback to POST
  241. *
  242. * @param string $index Index for item to be fetched from $_GET or $_POST
  243. * @param bool $xss_clean Whether to apply XSS filtering
  244. * @return mixed
  245. */
  246. public function get_post($index, $xss_clean = NULL)
  247. {
  248. return isset($_GET[$index])
  249. ? $this->get($index, $xss_clean)
  250. : $this->post($index, $xss_clean);
  251. }
  252. // --------------------------------------------------------------------
  253. /**
  254. * Fetch an item from the COOKIE array
  255. *
  256. * @param mixed $index Index for item to be fetched from $_COOKIE
  257. * @param bool $xss_clean Whether to apply XSS filtering
  258. * @return mixed
  259. */
  260. public function cookie($index = NULL, $xss_clean = NULL)
  261. {
  262. return $this->_fetch_from_array($_COOKIE, $index, $xss_clean);
  263. }
  264. // --------------------------------------------------------------------
  265. /**
  266. * Fetch an item from the SERVER array
  267. *
  268. * @param mixed $index Index for item to be fetched from $_SERVER
  269. * @param bool $xss_clean Whether to apply XSS filtering
  270. * @return mixed
  271. */
  272. public function server($index, $xss_clean = NULL)
  273. {
  274. return $this->_fetch_from_array($_SERVER, $index, $xss_clean);
  275. }
  276. // ------------------------------------------------------------------------
  277. /**
  278. * Fetch an item from the php://input stream
  279. *
  280. * Useful when you need to access PUT, DELETE or PATCH request data.
  281. *
  282. * @param string $index Index for item to be fetched
  283. * @param bool $xss_clean Whether to apply XSS filtering
  284. * @return mixed
  285. */
  286. public function input_stream($index = NULL, $xss_clean = NULL)
  287. {
  288. // Prior to PHP 5.6, the input stream can only be read once,
  289. // so we'll need to check if we have already done that first.
  290. if ( ! is_array($this->_input_stream))
  291. {
  292. // $this->raw_input_stream will trigger __get().
  293. parse_str($this->raw_input_stream, $this->_input_stream);
  294. is_array($this->_input_stream) OR $this->_input_stream = array();
  295. }
  296. return $this->_fetch_from_array($this->_input_stream, $index, $xss_clean);
  297. }
  298. // ------------------------------------------------------------------------
  299. /**
  300. * Set cookie
  301. *
  302. * Accepts an arbitrary number of parameters (up to 7) or an associative
  303. * array in the first parameter containing all the values.
  304. *
  305. * @param string|mixed[] $name Cookie name or an array containing parameters
  306. * @param string $value Cookie value
  307. * @param int $expire Cookie expiration time in seconds
  308. * @param string $domain Cookie domain (e.g.: '.yourdomain.com')
  309. * @param string $path Cookie path (default: '/')
  310. * @param string $prefix Cookie name prefix
  311. * @param bool $secure Whether to only transfer cookies via SSL
  312. * @param bool $httponly Whether to only makes the cookie accessible via HTTP (no javascript)
  313. * @return void
  314. */
  315. public function set_cookie($name, $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = FALSE, $httponly = FALSE)
  316. {
  317. if (is_array($name))
  318. {
  319. // always leave 'name' in last place, as the loop will break otherwise, due to $$item
  320. foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'secure', 'httponly', 'name') as $item)
  321. {
  322. if (isset($name[$item]))
  323. {
  324. $$item = $name[$item];
  325. }
  326. }
  327. }
  328. if ($prefix === '' && config_item('cookie_prefix') !== '')
  329. {
  330. $prefix = config_item('cookie_prefix');
  331. }
  332. if ($domain == '' && config_item('cookie_domain') != '')
  333. {
  334. $domain = config_item('cookie_domain');
  335. }
  336. if ($path === '/' && config_item('cookie_path') !== '/')
  337. {
  338. $path = config_item('cookie_path');
  339. }
  340. if ($secure === FALSE && config_item('cookie_secure') === TRUE)
  341. {
  342. $secure = config_item('cookie_secure');
  343. }
  344. if ($httponly === FALSE && config_item('cookie_httponly') !== FALSE)
  345. {
  346. $httponly = config_item('cookie_httponly');
  347. }
  348. if ( ! is_numeric($expire))
  349. {
  350. $expire = time() - 86500;
  351. }
  352. else
  353. {
  354. $expire = ($expire > 0) ? time() + $expire : 0;
  355. }
  356. setcookie($prefix.$name, $value, $expire, $path, $domain, $secure, $httponly);
  357. }
  358. // --------------------------------------------------------------------
  359. /**
  360. * Fetch the IP Address
  361. *
  362. * Determines and validates the visitor's IP address.
  363. *
  364. * @return string IP address
  365. */
  366. public function ip_address()
  367. {
  368. if ($this->ip_address !== FALSE)
  369. {
  370. return $this->ip_address;
  371. }
  372. $proxy_ips = config_item('proxy_ips');
  373. if ( ! empty($proxy_ips) && ! is_array($proxy_ips))
  374. {
  375. $proxy_ips = explode(',', str_replace(' ', '', $proxy_ips));
  376. }
  377. $this->ip_address = $this->server('REMOTE_ADDR');
  378. if ($proxy_ips)
  379. {
  380. foreach (array('HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'HTTP_X_CLIENT_IP', 'HTTP_X_CLUSTER_CLIENT_IP') as $header)
  381. {
  382. if (($spoof = $this->server($header)) !== NULL)
  383. {
  384. // Some proxies typically list the whole chain of IP
  385. // addresses through which the client has reached us.
  386. // e.g. client_ip, proxy_ip1, proxy_ip2, etc.
  387. sscanf($spoof, '%[^,]', $spoof);
  388. if ( ! $this->valid_ip($spoof))
  389. {
  390. $spoof = NULL;
  391. }
  392. else
  393. {
  394. break;
  395. }
  396. }
  397. }
  398. if ($spoof)
  399. {
  400. for ($i = 0, $c = count($proxy_ips); $i < $c; $i++)
  401. {
  402. // Check if we have an IP address or a subnet
  403. if (strpos($proxy_ips[$i], '/') === FALSE)
  404. {
  405. // An IP address (and not a subnet) is specified.
  406. // We can compare right away.
  407. if ($proxy_ips[$i] === $this->ip_address)
  408. {
  409. $this->ip_address = $spoof;
  410. break;
  411. }
  412. continue;
  413. }
  414. // We have a subnet ... now the heavy lifting begins
  415. isset($separator) OR $separator = $this->valid_ip($this->ip_address, 'ipv6') ? ':' : '.';
  416. // If the proxy entry doesn't match the IP protocol - skip it
  417. if (strpos($proxy_ips[$i], $separator) === FALSE)
  418. {
  419. continue;
  420. }
  421. // Convert the REMOTE_ADDR IP address to binary, if needed
  422. if ( ! isset($ip, $sprintf))
  423. {
  424. if ($separator === ':')
  425. {
  426. // Make sure we're have the "full" IPv6 format
  427. $ip = explode(':',
  428. str_replace('::',
  429. str_repeat(':', 9 - substr_count($this->ip_address, ':')),
  430. $this->ip_address
  431. )
  432. );
  433. for ($j = 0; $j < 8; $j++)
  434. {
  435. $ip[$j] = intval($ip[$j], 16);
  436. }
  437. $sprintf = '%016b%016b%016b%016b%016b%016b%016b%016b';
  438. }
  439. else
  440. {
  441. $ip = explode('.', $this->ip_address);
  442. $sprintf = '%08b%08b%08b%08b';
  443. }
  444. $ip = vsprintf($sprintf, $ip);
  445. }
  446. // Split the netmask length off the network address
  447. sscanf($proxy_ips[$i], '%[^/]/%d', $netaddr, $masklen);
  448. // Again, an IPv6 address is most likely in a compressed form
  449. if ($separator === ':')
  450. {
  451. $netaddr = explode(':', str_replace('::', str_repeat(':', 9 - substr_count($netaddr, ':')), $netaddr));
  452. for ($i = 0; $i < 8; $i++)
  453. {
  454. $netaddr[$i] = intval($netaddr[$i], 16);
  455. }
  456. }
  457. else
  458. {
  459. $netaddr = explode('.', $netaddr);
  460. }
  461. // Convert to binary and finally compare
  462. if (strncmp($ip, vsprintf($sprintf, $netaddr), $masklen) === 0)
  463. {
  464. $this->ip_address = $spoof;
  465. break;
  466. }
  467. }
  468. }
  469. }
  470. if ( ! $this->valid_ip($this->ip_address))
  471. {
  472. return $this->ip_address = '0.0.0.0';
  473. }
  474. return $this->ip_address;
  475. }
  476. // --------------------------------------------------------------------
  477. /**
  478. * Validate IP Address
  479. *
  480. * @param string $ip IP address
  481. * @param string $which IP protocol: 'ipv4' or 'ipv6'
  482. * @return bool
  483. */
  484. public function valid_ip($ip, $which = '')
  485. {
  486. switch (strtolower($which))
  487. {
  488. case 'ipv4':
  489. $which = FILTER_FLAG_IPV4;
  490. break;
  491. case 'ipv6':
  492. $which = FILTER_FLAG_IPV6;
  493. break;
  494. default:
  495. $which = NULL;
  496. break;
  497. }
  498. return (bool) filter_var($ip, FILTER_VALIDATE_IP, $which);
  499. }
  500. // --------------------------------------------------------------------
  501. /**
  502. * Fetch User Agent string
  503. *
  504. * @return string|null User Agent string or NULL if it doesn't exist
  505. */
  506. public function user_agent($xss_clean = NULL)
  507. {
  508. return $this->_fetch_from_array($_SERVER, 'HTTP_USER_AGENT', $xss_clean);
  509. }
  510. // --------------------------------------------------------------------
  511. /**
  512. * Sanitize Globals
  513. *
  514. * Internal method serving for the following purposes:
  515. *
  516. * - Unsets $_GET data, if query strings are not enabled
  517. * - Cleans POST, COOKIE and SERVER data
  518. * - Standardizes newline characters to PHP_EOL
  519. *
  520. * @return void
  521. */
  522. protected function _sanitize_globals()
  523. {
  524. // Is $_GET data allowed? If not we'll set the $_GET to an empty array
  525. if ($this->_allow_get_array === FALSE)
  526. {
  527. $_GET = array();
  528. }
  529. elseif (is_array($_GET) && count($_GET) > 0)
  530. {
  531. foreach ($_GET as $key => $val)
  532. {
  533. $_GET[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
  534. }
  535. }
  536. // Clean $_POST Data
  537. if (is_array($_POST) && count($_POST) > 0)
  538. {
  539. foreach ($_POST as $key => $val)
  540. {
  541. $_POST[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
  542. }
  543. }
  544. // Clean $_COOKIE Data
  545. if (is_array($_COOKIE) && count($_COOKIE) > 0)
  546. {
  547. // Also get rid of specially treated cookies that might be set by a server
  548. // or silly application, that are of no use to a CI application anyway
  549. // but that when present will trip our 'Disallowed Key Characters' alarm
  550. // http://www.ietf.org/rfc/rfc2109.txt
  551. // note that the key names below are single quoted strings, and are not PHP variables
  552. unset(
  553. $_COOKIE['$Version'],
  554. $_COOKIE['$Path'],
  555. $_COOKIE['$Domain']
  556. );
  557. foreach ($_COOKIE as $key => $val)
  558. {
  559. if (($cookie_key = $this->_clean_input_keys($key)) !== FALSE)
  560. {
  561. $_COOKIE[$cookie_key] = $this->_clean_input_data($val);
  562. }
  563. else
  564. {
  565. unset($_COOKIE[$key]);
  566. }
  567. }
  568. }
  569. // Sanitize PHP_SELF
  570. $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
  571. // CSRF Protection check
  572. if ($this->_enable_csrf === TRUE && ! is_cli())
  573. {
  574. $this->security->csrf_verify();
  575. }
  576. log_message('debug', 'Global POST, GET and COOKIE data sanitized');
  577. }
  578. // --------------------------------------------------------------------
  579. /**
  580. * Clean Input Data
  581. *
  582. * Internal method that aids in escaping data and
  583. * standardizing newline characters to PHP_EOL.
  584. *
  585. * @param string|string[] $str Input string(s)
  586. * @return string
  587. */
  588. protected function _clean_input_data($str)
  589. {
  590. if (is_array($str))
  591. {
  592. $new_array = array();
  593. foreach (array_keys($str) as $key)
  594. {
  595. $new_array[$this->_clean_input_keys($key)] = $this->_clean_input_data($str[$key]);
  596. }
  597. return $new_array;
  598. }
  599. /* We strip slashes if magic quotes is on to keep things consistent
  600. NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
  601. it will probably not exist in future versions at all.
  602. */
  603. if ( ! is_php('5.4') && get_magic_quotes_gpc())
  604. {
  605. $str = stripslashes($str);
  606. }
  607. // Clean UTF-8 if supported
  608. if (UTF8_ENABLED === TRUE)
  609. {
  610. $str = $this->uni->clean_string($str);
  611. }
  612. // Remove control characters
  613. $str = remove_invisible_characters($str, FALSE);
  614. // Standardize newlines if needed
  615. if ($this->_standardize_newlines === TRUE)
  616. {
  617. return preg_replace('/(?:\r\n|[\r\n])/', PHP_EOL, $str);
  618. }
  619. return $str;
  620. }
  621. // --------------------------------------------------------------------
  622. /**
  623. * Clean Keys
  624. *
  625. * Internal method that helps to prevent malicious users
  626. * from trying to exploit keys we make sure that keys are
  627. * only named with alpha-numeric text and a few other items.
  628. *
  629. * @param string $str Input string
  630. * @param bool $fatal Whether to terminate script exection
  631. * or to return FALSE if an invalid
  632. * key is encountered
  633. * @return string|bool
  634. */
  635. protected function _clean_input_keys($str, $fatal = TRUE)
  636. {
  637. if ( ! preg_match('/^[a-z0-9:_\/|-]+$/i', $str))
  638. {
  639. if ($fatal === TRUE)
  640. {
  641. return FALSE;
  642. }
  643. else
  644. {
  645. set_status_header(503);
  646. echo 'Disallowed Key Characters.';
  647. exit(7); // EXIT_USER_INPUT
  648. }
  649. }
  650. // Clean UTF-8 if supported
  651. if (UTF8_ENABLED === TRUE)
  652. {
  653. return $this->uni->clean_string($str);
  654. }
  655. return $str;
  656. }
  657. // --------------------------------------------------------------------
  658. /**
  659. * Request Headers
  660. *
  661. * @param bool $xss_clean Whether to apply XSS filtering
  662. * @return array
  663. */
  664. public function request_headers($xss_clean = FALSE)
  665. {
  666. // If header is already defined, return it immediately
  667. if ( ! empty($this->headers))
  668. {
  669. return $this->headers;
  670. }
  671. // In Apache, you can simply call apache_request_headers()
  672. if (function_exists('apache_request_headers'))
  673. {
  674. return $this->headers = apache_request_headers();
  675. }
  676. $this->headers['Content-Type'] = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : @getenv('CONTENT_TYPE');
  677. foreach ($_SERVER as $key => $val)
  678. {
  679. if (sscanf($key, 'HTTP_%s', $header) === 1)
  680. {
  681. // take SOME_HEADER and turn it into Some-Header
  682. $header = str_replace('_', ' ', strtolower($header));
  683. $header = str_replace(' ', '-', ucwords($header));
  684. $this->headers[$header] = $this->_fetch_from_array($_SERVER, $key, $xss_clean);
  685. }
  686. }
  687. return $this->headers;
  688. }
  689. // --------------------------------------------------------------------
  690. /**
  691. * Get Request Header
  692. *
  693. * Returns the value of a single member of the headers class member
  694. *
  695. * @param string $index Header name
  696. * @param bool $xss_clean Whether to apply XSS filtering
  697. * @return string|null The requested header on success or NULL on failure
  698. */
  699. public function get_request_header($index, $xss_clean = FALSE)
  700. {
  701. if (empty($this->headers))
  702. {
  703. $this->request_headers();
  704. }
  705. if ( ! isset($this->headers[$index]))
  706. {
  707. return NULL;
  708. }
  709. return ($xss_clean === TRUE)
  710. ? $this->security->xss_clean($this->headers[$index])
  711. : $this->headers[$index];
  712. }
  713. // --------------------------------------------------------------------
  714. /**
  715. * Is AJAX request?
  716. *
  717. * Test to see if a request contains the HTTP_X_REQUESTED_WITH header.
  718. *
  719. * @return bool
  720. */
  721. public function is_ajax_request()
  722. {
  723. return ( ! empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
  724. }
  725. // --------------------------------------------------------------------
  726. /**
  727. * Is CLI request?
  728. *
  729. * Test to see if a request was made from the command line.
  730. *
  731. * @deprecated 3.0.0 Use is_cli() instead
  732. * @return bool
  733. */
  734. public function is_cli_request()
  735. {
  736. return is_cli();
  737. }
  738. // --------------------------------------------------------------------
  739. /**
  740. * Get Request Method
  741. *
  742. * Return the request method
  743. *
  744. * @param bool $upper Whether to return in upper or lower case
  745. * (default: FALSE)
  746. * @return string
  747. */
  748. public function method($upper = FALSE)
  749. {
  750. return ($upper)
  751. ? strtoupper($this->server('REQUEST_METHOD'))
  752. : strtolower($this->server('REQUEST_METHOD'));
  753. }
  754. // ------------------------------------------------------------------------
  755. /**
  756. * Magic __get()
  757. *
  758. * Allows read access to protected properties
  759. *
  760. * @param string $name
  761. * @return mixed
  762. */
  763. public function __get($name)
  764. {
  765. if ($name === 'raw_input_stream')
  766. {
  767. isset($this->_raw_input_stream) OR $this->_raw_input_stream = file_get_contents('php://input');
  768. return $this->_raw_input_stream;
  769. }
  770. elseif ($name === 'ip_address')
  771. {
  772. return $this->ip_address;
  773. }
  774. }
  775. }