1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
5 * $Id: Input.php 3917 2009-01-21 03:06:22Z zombor $
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
14 // IP address of current user
18 protected static $instance;
21 * Retrieve a singleton instance of Input. This will always be the first
22 * created instance of this class.
26 public static function instance()
28 if (self
::$instance === NULL)
30 // Create a new instance
34 return self
::$instance;
38 * Sanitizes global GET, POST and COOKIE data. Also takes care of
39 * magic_quotes and register_globals, if they have been enabled.
43 public function __construct()
46 if (self
::$instance === NULL)
48 // magic_quotes_runtime is enabled
49 if (get_magic_quotes_runtime())
51 echo ('Disable magic_quotes_runtime! It is evil and deprecated: http://php.net/magic_quotes');
55 // magic_quotes_gpc is enabled
56 if (get_magic_quotes_gpc())
58 echo ('Disable magic_quotes_gpc! It is evil and deprecated: http://php.net/magic_quotes');
62 // register_globals is enabled
63 if (ini_get('register_globals'))
65 echo ('Disable register_globals! It is evil and deprecated: http://php.net/register_globals');
70 self
::$instance = $this;
75 * Fetch an item from the $_GET array.
77 * @param string key to find
78 * @param mixed default value
81 public function get($key = array(), $default = NULL)
83 return $this->search_array($_GET, $key, $default);
87 * Fetch an item from the $_POST array.
89 * @param string key to find
90 * @param mixed default value
93 public function post($key = array(), $default = NULL)
95 return $this->search_array($_POST, $key, $default);
99 * Fetch an item from the $_COOKIE array.
101 * @param string key to find
102 * @param mixed default value
105 public function cookie($key = array(), $default = NULL)
107 return $this->search_array($_COOKIE, $key, $default);
111 * Fetch an item from the $_SERVER array.
113 * @param string key to find
114 * @param mixed default value
117 public function server($key = array(), $default = NULL)
119 return $this->search_array($_SERVER, $key, $default);
123 * Fetch an item from a global array.
125 * @param array array to search
126 * @param string key to find
127 * @param mixed default value
130 protected function search_array($array, $key, $default = NULL)
132 if ($key === array())
135 if ( ! isset($array[$key]))
139 $value = $array[$key];
145 * Fetch the IP Address.
149 public function ip_address()
151 if ($this->ip_address
!== NULL)
152 return $this->ip_address
;
154 if ($ip = $this->server('HTTP_CLIENT_IP'))
156 $this->ip_address
= $ip;
158 elseif ($ip = $this->server('REMOTE_ADDR'))
160 $this->ip_address
= $ip;
162 elseif ($ip = $this->server('HTTP_X_FORWARDED_FOR'))
164 $this->ip_address
= $ip;
167 if ($comma = strrpos($this->ip_address
, ',') !== FALSE)
169 $this->ip_address
= substr($this->ip_address
, $comma +
1);
172 if ( ! valid
::ip($this->ip_address
))
175 $this->ip_address
= '0.0.0.0';
178 return $this->ip_address
;
182 * This is a helper method. It enforces W3C specifications for allowed
183 * key name strings, to prevent malicious exploitation.
185 * @param string string to clean
188 public function clean_input_keys($str)
190 $chars = PCRE_UNICODE_PROPERTIES ?
'\pL' : 'a-zA-Z';
192 if ( ! preg_match('#^['.$chars.'0-9:_.-]++$#uD', $str))
194 exit('Disallowed key characters in global data.');