[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Part of the Joomla Framework Input Package 4 * 5 * @copyright Copyright (C) 2005 - 2022 Open Source Matters, Inc. All rights reserved. 6 * @license GNU General Public License version 2 or later; see LICENSE 7 */ 8 9 namespace Joomla\Input; 10 11 use Joomla\Filter; 12 13 /** 14 * Joomla! Input Base Class 15 * 16 * This is an abstracted input class used to manage retrieving data from the application environment. 17 * 18 * @since 1.0 19 * 20 * @property-read Input $get 21 * @property-read Input $post 22 * @property-read Input $request 23 * @property-read Input $server 24 * @property-read Input $env 25 * @property-read Files $files 26 * @property-read Cookie $cookie 27 * 28 * @method integer getInt($name, $default = null) Get a signed integer. 29 * @method integer getUint($name, $default = null) Get an unsigned integer. 30 * @method float getFloat($name, $default = null) Get a floating-point number. 31 * @method boolean getBool($name, $default = null) Get a boolean value. 32 * @method string getWord($name, $default = null) Get a word. 33 * @method string getAlnum($name, $default = null) Get an alphanumeric string. 34 * @method string getCmd($name, $default = null) Get a CMD filtered string. 35 * @method string getBase64($name, $default = null) Get a base64 encoded string. 36 * @method string getString($name, $default = null) Get a string. 37 * @method string getHtml($name, $default = null) Get a HTML string. 38 * @method string getPath($name, $default = null) Get a file path. 39 * @method string getUsername($name, $default = null) Get a username. 40 * @method mixed getRaw($name, $default = null) Get an unfiltered value. 41 */ 42 class Input implements \Countable 43 { 44 /** 45 * Container with allowed superglobals 46 * 47 * @var array 48 * @since 1.3.0 49 */ 50 private const ALLOWED_GLOBALS = ['REQUEST', 'GET', 'POST', 'FILES', 'SERVER', 'ENV']; 51 52 /** 53 * Options array for the Input instance. 54 * 55 * @var array 56 * @since 1.0 57 */ 58 protected $options = []; 59 60 /** 61 * Filter object to use. 62 * 63 * @var Filter\InputFilter 64 * @since 1.0 65 */ 66 protected $filter; 67 68 /** 69 * Input data. 70 * 71 * @var array 72 * @since 1.0 73 */ 74 protected $data = []; 75 76 /** 77 * Input objects 78 * 79 * @var Input[] 80 * @since 1.0 81 */ 82 protected $inputs = []; 83 84 /** 85 * Constructor. 86 * 87 * @param array|null $source Optional source data. If omitted, a copy of the server variable '_REQUEST' is used. 88 * @param array $options An optional associative array of configuration parameters: 89 * filter: An instance of Filter\Input. If omitted, a default filter is initialised. 90 * 91 * @since 1.0 92 */ 93 public function __construct($source = null, array $options = []) 94 { 95 $this->data = $source ?? $_REQUEST; 96 $this->filter = $options['filter'] ?? new Filter\InputFilter; 97 $this->options = $options; 98 } 99 100 /** 101 * Magic method to get an input object 102 * 103 * @param mixed $name Name of the input object to retrieve. 104 * 105 * @return Input The request input object 106 * 107 * @since 1.0 108 */ 109 public function __get($name) 110 { 111 if (isset($this->inputs[$name])) 112 { 113 return $this->inputs[$name]; 114 } 115 116 $className = __NAMESPACE__ . '\\' . ucfirst($name); 117 118 if (class_exists($className)) 119 { 120 $this->inputs[$name] = new $className(null, $this->options); 121 122 return $this->inputs[$name]; 123 } 124 125 $superGlobal = '_' . strtoupper($name); 126 127 if (\in_array(strtoupper($name), self::ALLOWED_GLOBALS, true) && isset($GLOBALS[$superGlobal])) 128 { 129 $this->inputs[$name] = new self($GLOBALS[$superGlobal], $this->options); 130 131 return $this->inputs[$name]; 132 } 133 134 $trace = debug_backtrace(); 135 trigger_error( 136 'Undefined property via __get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], 137 E_USER_NOTICE 138 ); 139 } 140 141 /** 142 * Get the number of variables. 143 * 144 * @return integer The number of variables in the input. 145 * 146 * @since 1.0 147 * @see Countable::count() 148 */ 149 #[\ReturnTypeWillChange] 150 public function count() 151 { 152 return \count($this->data); 153 } 154 155 /** 156 * Gets a value from the input data. 157 * 158 * @param string $name Name of the value to get. 159 * @param mixed $default Default value to return if variable does not exist. 160 * @param string $filter Filter to apply to the value. 161 * 162 * @return mixed The filtered input value. 163 * 164 * @see \Joomla\Filter\InputFilter::clean() 165 * @since 1.0 166 */ 167 public function get($name, $default = null, $filter = 'cmd') 168 { 169 if ($this->exists($name)) 170 { 171 return $this->filter->clean($this->data[$name], $filter); 172 } 173 174 return $default; 175 } 176 177 /** 178 * Gets an array of values from the request. 179 * 180 * @param array $vars Associative array of keys and filter types to apply. 181 * If empty and datasource is null, all the input data will be returned 182 * but filtered using the default case in InputFilter::clean. 183 * @param mixed $datasource Array to retrieve data from, or null 184 * 185 * @return mixed The filtered input data. 186 * 187 * @since 1.0 188 */ 189 public function getArray(array $vars = [], $datasource = null) 190 { 191 if (empty($vars) && $datasource === null) 192 { 193 $vars = $this->data; 194 } 195 196 $results = []; 197 198 foreach ($vars as $k => $v) 199 { 200 if (\is_array($v)) 201 { 202 if ($datasource === null) 203 { 204 $results[$k] = $this->getArray($v, $this->get($k, null, 'array')); 205 } 206 else 207 { 208 $results[$k] = $this->getArray($v, $datasource[$k]); 209 } 210 } 211 else 212 { 213 if ($datasource === null) 214 { 215 $results[$k] = $this->get($k, null, $v); 216 } 217 elseif (isset($datasource[$k])) 218 { 219 $results[$k] = $this->filter->clean($datasource[$k], $v); 220 } 221 else 222 { 223 $results[$k] = $this->filter->clean(null, $v); 224 } 225 } 226 } 227 228 return $results; 229 } 230 231 /** 232 * Get the Input instance holding the data for the current request method 233 * 234 * @return Input 235 * 236 * @since 1.3.0 237 */ 238 public function getInputForRequestMethod() 239 { 240 switch (strtoupper($this->getMethod())) 241 { 242 case 'GET': 243 return $this->get; 244 245 case 'POST': 246 return $this->post; 247 248 default: 249 // PUT, PATCH, etc. don't have superglobals 250 return $this; 251 } 252 } 253 254 /** 255 * Sets a value 256 * 257 * @param string $name Name of the value to set. 258 * @param mixed $value Value to assign to the input. 259 * 260 * @return void 261 * 262 * @since 1.0 263 */ 264 public function set($name, $value) 265 { 266 $this->data[$name] = $value; 267 } 268 269 /** 270 * Define a value. The value will only be set if there's no value for the name or if it is null. 271 * 272 * @param string $name Name of the value to define. 273 * @param mixed $value Value to assign to the input. 274 * 275 * @return void 276 * 277 * @since 1.0 278 */ 279 public function def($name, $value) 280 { 281 if (isset($this->data[$name])) 282 { 283 return; 284 } 285 286 $this->data[$name] = $value; 287 } 288 289 /** 290 * Check if a value name exists. 291 * 292 * @param string $name Value name 293 * 294 * @return boolean 295 * 296 * @since 1.2.0 297 */ 298 public function exists($name) 299 { 300 return isset($this->data[$name]); 301 } 302 303 /** 304 * Magic method to get filtered input data. 305 * 306 * @param string $name Name of the filter type prefixed with 'get'. 307 * @param array $arguments [0] The name of the variable [1] The default value. 308 * 309 * @return mixed The filtered input value. 310 * 311 * @since 1.0 312 */ 313 public function __call($name, $arguments) 314 { 315 if (substr($name, 0, 3) == 'get') 316 { 317 $filter = substr($name, 3); 318 319 $default = null; 320 321 if (isset($arguments[1])) 322 { 323 $default = $arguments[1]; 324 } 325 326 return $this->get($arguments[0], $default, $filter); 327 } 328 329 $trace = debug_backtrace(); 330 trigger_error( 331 'Call to undefined method via call(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], 332 E_USER_ERROR 333 ); 334 } 335 336 /** 337 * Gets the request method. 338 * 339 * @return string The request method. 340 * 341 * @since 1.0 342 */ 343 public function getMethod() 344 { 345 return strtoupper($this->server->getCmd('REQUEST_METHOD')); 346 } 347 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |