| @@ -1,0 +1,299 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * @package VikWP - Libraries | |
| 4 | + * @subpackage adapter.input | |
| 5 | + * @author E4J s.r.l. | |
| 6 | + * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. | |
| 7 | + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL | |
| 8 | + * @link https://vikwp.com | |
| 9 | + */ | |
| 10 | + | |
| 11 | +// No direct access | |
| 12 | +defined('ABSPATH') or die('No script kiddies please!'); | |
| 13 | + | |
| 14 | +JLoader::import('adapter.input.filter'); | |
| 15 | + | |
| 16 | +/** | |
| 17 | + * This is an abstracted input class used to handle retrieving data | |
| 18 | + * from the application environment. | |
| 19 | + * | |
| 20 | + * @since 10.0 | |
| 21 | + * | |
| 22 | + * @property-read Input $get | |
| 23 | + * @property-read Input $post | |
| 24 | + * @property-read Input $request | |
| 25 | + * @property-read Input $server | |
| 26 | + * @property-read Input $env | |
| 27 | + * @property-read Files $files | |
| 28 | + * @property-read Cookie $cookie | |
| 29 | + * | |
| 30 | + * @method integer getInt($name, $default = null) Get a signed integer. | |
| 31 | + * @method integer getUint($name, $default = null) Get an unsigned integer. | |
| 32 | + * @method float getFloat($name, $default = null) Get a floating-point number. | |
| 33 | + * @method boolean getBool($name, $default = null) Get a boolean value. | |
| 34 | + * @method string getWord($name, $default = null) Get a word. | |
| 35 | + * @method string getAlnum($name, $default = null) Get an alphanumeric string. | |
| 36 | + * @method string getCmd($name, $default = null) Get a CMD filtered string. | |
| 37 | + * @method string getBase64($name, $default = null) Get a base64 encoded string. | |
| 38 | + * @method string getString($name, $default = null) Get a string. | |
| 39 | + * @method string getHtml($name, $default = null) Get a HTML string. | |
| 40 | + * @method string getPath($name, $default = null) Get a file path. | |
| 41 | + * @method string getUsername($name, $default = null) Get a username. | |
| 42 | + */ | |
| 43 | +class JInput | |
| 44 | +{ | |
| 45 | + /** | |
| 46 | + * A list containing all the allowed superglobals. | |
| 47 | + * | |
| 48 | + * @var array | |
| 49 | + * @since 10.1.24 | |
| 50 | + */ | |
| 51 | + private static $allowedGlobals = array('REQUEST', 'GET', 'POST', 'FILES', 'SERVER', 'ENV'); | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * The input data source. | |
| 55 | + * | |
| 56 | + * @var array | |
| 57 | + */ | |
| 58 | + protected $data; | |
| 59 | + | |
| 60 | + /** | |
| 61 | + * An handler to filter data. | |
| 62 | + * | |
| 63 | + * @var JInputFilter | |
| 64 | + */ | |
| 65 | + protected $filter; | |
| 66 | + | |
| 67 | + /** | |
| 68 | + * An array of options. | |
| 69 | + * | |
| 70 | + * @var array | |
| 71 | + */ | |
| 72 | + protected $options; | |
| 73 | + | |
| 74 | + /** | |
| 75 | + * A list containing all the input loaded. | |
| 76 | + * | |
| 77 | + * @var array | |
| 78 | + */ | |
| 79 | + protected $inputs = array(); | |
| 80 | + | |
| 81 | + /** | |
| 82 | + * Class constructor. | |
| 83 | + * | |
| 84 | + * @param array $source Optional source data. If omitted, a copy of the server variable '_REQUEST' is used. | |
| 85 | + * @param array $options An optional associative array of configuration parameters. | |
| 86 | + * filter: An instance of JInputFilter. If omitted, a default filter is initialised. | |
| 87 | + */ | |
| 88 | + public function __construct(&$source = null, array $options = array()) | |
| 89 | + { | |
| 90 | + if (isset($options['filter']) && $options['filter'] instanceof JInputFilter) | |
| 91 | + { | |
| 92 | + $this->filter = $options['filter']; | |
| 93 | + } | |
| 94 | + else | |
| 95 | + { | |
| 96 | + $this->filter = new JInputFilter; | |
| 97 | + } | |
| 98 | + | |
| 99 | + if (is_null($source)) | |
| 100 | + { | |
| 101 | + $this->data = &$_REQUEST; | |
| 102 | + } | |
| 103 | + else | |
| 104 | + { | |
| 105 | + $this->data = &$source; | |
| 106 | + } | |
| 107 | + | |
| 108 | + $this->options = $options; | |
| 109 | + } | |
| 110 | + | |
| 111 | + /** | |
| 112 | + * Magic method to get an input object. | |
| 113 | + * | |
| 114 | + * @param mixed $name Name of the input object to retrieve. | |
| 115 | + * | |
| 116 | + * @return JInput The request input object. | |
| 117 | + */ | |
| 118 | + public function __get($name) | |
| 119 | + { | |
| 120 | + // always use UPPERCASE notation | |
| 121 | + $name = strtoupper($name); | |
| 122 | + | |
| 123 | + // check if the input has been already loaded | |
| 124 | + if (isset($this->inputs[$name])) | |
| 125 | + { | |
| 126 | + return $this->inputs[$name]; | |
| 127 | + } | |
| 128 | + | |
| 129 | + $className = 'JInput' . ucfirst($name); | |
| 130 | + | |
| 131 | + // otherwise try to load the file and check if the class handler exists | |
| 132 | + if (JLoader::import('adapter.input.classes.' . strtolower($name)) && class_exists($className)) | |
| 133 | + { | |
| 134 | + $this->inputs[$name] = new $className(null, $this->options); | |
| 135 | + | |
| 136 | + return $this->inputs[$name]; | |
| 137 | + } | |
| 138 | + | |
| 139 | + // otherwise check for an existing superglobal | |
| 140 | + $superGlobal = '_' . $name; | |
| 141 | + | |
| 142 | + /** | |
| 143 | + * Make sure the superglobal is allowed before accessing it. | |
| 144 | + * | |
| 145 | + * @since 10.1.24 | |
| 146 | + */ | |
| 147 | + if (in_array($name, static::$allowedGlobals, true) && isset($GLOBALS[$superGlobal])) | |
| 148 | + { | |
| 149 | + $this->inputs[$name] = new JInput($GLOBALS[$superGlobal], $this->options); | |
| 150 | + | |
| 151 | + return $this->inputs[$name]; | |
| 152 | + } | |
| 153 | + | |
| 154 | + throw new Exception('The input handler [' . $name . '] does not exist', 404); | |
| 155 | + } | |
| 156 | + | |
| 157 | + /** | |
| 158 | + * Magic method to get filtered input data. | |
| 159 | + * | |
| 160 | + * @param string $name Name of the filter type prefixed with 'get'. | |
| 161 | + * @param array $arguments [0] The name of the variable [1] The default value. | |
| 162 | + * | |
| 163 | + * @return mixed The filtered input value. | |
| 164 | + * | |
| 165 | + * @uses get() | |
| 166 | + */ | |
| 167 | + public function __call($name, $arguments) | |
| 168 | + { | |
| 169 | + if (substr($name, 0, 3) == 'get') | |
| 170 | + { | |
| 171 | + $filter = substr($name, 3); | |
| 172 | + | |
| 173 | + return $this->get($arguments[0], isset($arguments[1]) ? $arguments[1] : null, $filter); | |
| 174 | + } | |
| 175 | + | |
| 176 | + throw new Exception('Call to undefined method ' . __CLASS__ . '::' . $name . '()', 404); | |
| 177 | + } | |
| 178 | + | |
| 179 | + /** | |
| 180 | + * Get the number of variables. | |
| 181 | + * | |
| 182 | + * @return integer The number of variables in the input. | |
| 183 | + */ | |
| 184 | + public function count() | |
| 185 | + { | |
| 186 | + return count($this->data); | |
| 187 | + } | |
| 188 | + | |
| 189 | + /** | |
| 190 | + * Gets a value from the input data. | |
| 191 | + * | |
| 192 | + * @param string $name Name of the value to get. | |
| 193 | + * @param mixed $default Default value to return if variable does not exist. | |
| 194 | + * @param string $filter Filter to apply to the value. | |
| 195 | + * | |
| 196 | + * @return mixed The filtered input value. | |
| 197 | + */ | |
| 198 | + public function get($name, $default = null, $filter = 'cmd') | |
| 199 | + { | |
| 200 | + if (isset($this->data[$name])) | |
| 201 | + { | |
| 202 | + return $this->filter->clean($this->data[$name], $filter); | |
| 203 | + } | |
| 204 | + else if ($name == 'Itemid') | |
| 205 | + { | |
| 206 | + return (int) url_to_postid(JUri::current()); | |
| 207 | + } | |
| 208 | + | |
| 209 | + return $default; | |
| 210 | + } | |
| 211 | + | |
| 212 | + /** | |
| 213 | + * Gets the original array of values from the request. | |
| 214 | + * | |
| 215 | + * @return array The values array. | |
| 216 | + */ | |
| 217 | + public function getArray() | |
| 218 | + { | |
| 219 | + return $this->data; | |
| 220 | + } | |
| 221 | + | |
| 222 | + /** | |
| 223 | + * Sets a value into the input data. | |
| 224 | + * | |
| 225 | + * @param string $name Name of the value to set. | |
| 226 | + * @param mixed $value Value to assign to the input. | |
| 227 | + * | |
| 228 | + * @return void | |
| 229 | + */ | |
| 230 | + public function set($name, $value) | |
| 231 | + { | |
| 232 | + if ($value !== null) | |
| 233 | + { | |
| 234 | + $this->data[$name] = $value; | |
| 235 | + } | |
| 236 | + else | |
| 237 | + { | |
| 238 | + /** | |
| 239 | + * Delete argument directly when value is NULL. | |
| 240 | + * | |
| 241 | + * @since 10.1.24 | |
| 242 | + */ | |
| 243 | + $this->delete($name); | |
| 244 | + } | |
| 245 | + } | |
| 246 | + | |
| 247 | + /** | |
| 248 | + * Defines a value. The value will only be set if there's | |
| 249 | + * no value for the name or if it is null. | |
| 250 | + * | |
| 251 | + * @param string $name Name of the value to define. | |
| 252 | + * @param mixed $value Value to assign to the input. | |
| 253 | + * | |
| 254 | + * @return void | |
| 255 | + * | |
| 256 | + * @uses exists() | |
| 257 | + * @uses set() | |
| 258 | + */ | |
| 259 | + public function def($name, $value) | |
| 260 | + { | |
| 261 | + if (!$this->exists($name)) | |
| 262 | + { | |
| 263 | + $this->set($name, $value); | |
| 264 | + } | |
| 265 | + } | |
| 266 | + | |
| 267 | + /** | |
| 268 | + * Deletes a value from the input data, if any. | |
| 269 | + * | |
| 270 | + * NOTE: this method is not available in Joomla! | |
| 271 | + * | |
| 272 | + * @param string $name Name of the value to unset. | |
| 273 | + * | |
| 274 | + * @return void | |
| 275 | + * | |
| 276 | + * @uses exists() | |
| 277 | + * | |
| 278 | + * @since 10.1.24 | |
| 279 | + */ | |
| 280 | + public function delete($name) | |
| 281 | + { | |
| 282 | + if ($this->exists($name)) | |
| 283 | + { | |
| 284 | + unset($this->data[$name]); | |
| 285 | + } | |
| 286 | + } | |
| 287 | + | |
| 288 | + /** | |
| 289 | + * Checks if a value name exists. | |
| 290 | + * | |
| 291 | + * @param string $name The value name. | |
| 292 | + * | |
| 293 | + * @return boolean | |
| 294 | + */ | |
| 295 | + public function exists($name) | |
| 296 | + { | |
| 297 | + return isset($this->data[$name]); | |
| 298 | + } | |
| 299 | +} | |