/src/libraries/fof/input/jinput/input.php

https://bitbucket.org/ke2083/transfans.co.uk-website · PHP · 383 lines · 165 code · 41 blank · 177 comment · 18 complexity · ecdc2c6fcd7b42ddaffba024f5e331aa MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Input
  5. *
  6. * @copyright Copyright (C) 2005-2016 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * Joomla! Input Base Class
  12. *
  13. * This is an abstracted input class used to manage retrieving data from the application environment.
  14. *
  15. * @since 11.1
  16. *
  17. * @property-read JInput $get
  18. * @property-read JInput $post
  19. * @property-read JInput $request
  20. * @property-read JInput $server
  21. * @property-read JInputFiles $files
  22. * @property-read JInputCookie $cookie
  23. *
  24. * @method integer getInt() getInt($name, $default = null) Get a signed integer.
  25. * @method integer getUint() getUint($name, $default = null) Get an unsigned integer.
  26. * @method float getFloat() getFloat($name, $default = null) Get a floating-point number.
  27. * @method boolean getBool() getBool($name, $default = null) Get a boolean.
  28. * @method string getWord() getWord($name, $default = null)
  29. * @method string getAlnum() getAlnum($name, $default = null)
  30. * @method string getCmd() getCmd($name, $default = null)
  31. * @method string getBase64() getBase64($name, $default = null)
  32. * @method string getString() getString($name, $default = null)
  33. * @method string getHtml() getHtml($name, $default = null)
  34. * @method string getPath() getPath($name, $default = null)
  35. * @method string getUsername() getUsername($name, $default = null)
  36. */
  37. class JInput implements Serializable, Countable
  38. {
  39. /**
  40. * Options array for the JInput instance.
  41. *
  42. * @var array
  43. * @since 11.1
  44. */
  45. protected $options = array();
  46. /**
  47. * Filter object to use.
  48. *
  49. * @var JFilterInput
  50. * @since 11.1
  51. */
  52. protected $filter = null;
  53. /**
  54. * Input data.
  55. *
  56. * @var array
  57. * @since 11.1
  58. */
  59. protected $data = array();
  60. /**
  61. * Input objects
  62. *
  63. * @var array
  64. * @since 11.1
  65. */
  66. protected $inputs = array();
  67. /**
  68. * Constructor.
  69. *
  70. * @param array $source Source data (Optional, default is $_REQUEST)
  71. * @param array $options Array of configuration parameters (Optional)
  72. *
  73. * @since 11.1
  74. */
  75. public function __construct($source = null, array $options = array())
  76. {
  77. if (isset($options['filter']))
  78. {
  79. $this->filter = $options['filter'];
  80. }
  81. else
  82. {
  83. $this->filter = JFilterInput::getInstance();
  84. }
  85. if (is_null($source))
  86. {
  87. $this->data = &$_REQUEST;
  88. }
  89. else
  90. {
  91. $this->data = $source;
  92. }
  93. // Set the options for the class.
  94. $this->options = $options;
  95. }
  96. /**
  97. * Magic method to get an input object
  98. *
  99. * @param mixed $name Name of the input object to retrieve.
  100. *
  101. * @return JInput The request input object
  102. *
  103. * @since 11.1
  104. */
  105. public function __get($name)
  106. {
  107. if (isset($this->inputs[$name]))
  108. {
  109. return $this->inputs[$name];
  110. }
  111. $className = 'JInput' . ucfirst($name);
  112. if (class_exists($className))
  113. {
  114. $this->inputs[$name] = new $className(null, $this->options);
  115. return $this->inputs[$name];
  116. }
  117. $superGlobal = '_' . strtoupper($name);
  118. if (isset($GLOBALS[$superGlobal]))
  119. {
  120. $this->inputs[$name] = new JInput($GLOBALS[$superGlobal], $this->options);
  121. return $this->inputs[$name];
  122. }
  123. // TODO throw an exception
  124. }
  125. /**
  126. * Get the number of variables.
  127. *
  128. * @return integer The number of variables in the input.
  129. *
  130. * @since 12.2
  131. * @see Countable::count()
  132. */
  133. public function count()
  134. {
  135. return count($this->data);
  136. }
  137. /**
  138. * Gets a value from the input data.
  139. *
  140. * @param string $name Name of the value to get.
  141. * @param mixed $default Default value to return if variable does not exist.
  142. * @param string $filter Filter to apply to the value.
  143. *
  144. * @return mixed The filtered input value.
  145. *
  146. * @since 11.1
  147. */
  148. public function get($name, $default = null, $filter = 'cmd')
  149. {
  150. if (isset($this->data[$name]))
  151. {
  152. return $this->filter->clean($this->data[$name], $filter);
  153. }
  154. return $default;
  155. }
  156. /**
  157. * Gets an array of values from the request.
  158. *
  159. * @param array $vars Associative array of keys and filter types to apply.
  160. * If empty and datasource is null, all the input data will be returned
  161. * but filtered using the default case in JFilterInput::clean.
  162. * @param mixed $datasource Array to retrieve data from, or null
  163. *
  164. * @return mixed The filtered input data.
  165. *
  166. * @since 11.1
  167. */
  168. public function getArray(array $vars = array(), $datasource = null)
  169. {
  170. if (empty($vars) && is_null($datasource))
  171. {
  172. $vars = $this->data;
  173. }
  174. $results = array();
  175. foreach ($vars as $k => $v)
  176. {
  177. if (is_array($v))
  178. {
  179. if (is_null($datasource))
  180. {
  181. $results[$k] = $this->getArray($v, $this->get($k, null, 'array'));
  182. }
  183. else
  184. {
  185. $results[$k] = $this->getArray($v, $datasource[$k]);
  186. }
  187. }
  188. else
  189. {
  190. if (is_null($datasource))
  191. {
  192. $results[$k] = $this->get($k, null, $v);
  193. }
  194. elseif (isset($datasource[$k]))
  195. {
  196. $results[$k] = $this->filter->clean($datasource[$k], $v);
  197. }
  198. else
  199. {
  200. $results[$k] = $this->filter->clean(null, $v);
  201. }
  202. }
  203. }
  204. return $results;
  205. }
  206. /**
  207. * Sets a value
  208. *
  209. * @param string $name Name of the value to set.
  210. * @param mixed $value Value to assign to the input.
  211. *
  212. * @return void
  213. *
  214. * @since 11.1
  215. */
  216. public function set($name, $value)
  217. {
  218. $this->data[$name] = $value;
  219. }
  220. /**
  221. * Define a value. The value will only be set if there's no value for the name or if it is null.
  222. *
  223. * @param string $name Name of the value to define.
  224. * @param mixed $value Value to assign to the input.
  225. *
  226. * @return void
  227. *
  228. * @since 12.1
  229. */
  230. public function def($name, $value)
  231. {
  232. if (isset($this->data[$name]))
  233. {
  234. return;
  235. }
  236. $this->data[$name] = $value;
  237. }
  238. /**
  239. * Magic method to get filtered input data.
  240. *
  241. * @param string $name Name of the filter type prefixed with 'get'.
  242. * @param array $arguments [0] The name of the variable [1] The default value.
  243. *
  244. * @return mixed The filtered input value.
  245. *
  246. * @since 11.1
  247. */
  248. public function __call($name, $arguments)
  249. {
  250. if (substr($name, 0, 3) == 'get')
  251. {
  252. $filter = substr($name, 3);
  253. $default = null;
  254. if (isset($arguments[1]))
  255. {
  256. $default = $arguments[1];
  257. }
  258. return $this->get($arguments[0], $default, $filter);
  259. }
  260. }
  261. /**
  262. * Gets the request method.
  263. *
  264. * @return string The request method.
  265. *
  266. * @since 11.1
  267. */
  268. public function getMethod()
  269. {
  270. $method = strtoupper($_SERVER['REQUEST_METHOD']);
  271. return $method;
  272. }
  273. /**
  274. * Method to serialize the input.
  275. *
  276. * @return string The serialized input.
  277. *
  278. * @since 12.1
  279. */
  280. public function serialize()
  281. {
  282. // Load all of the inputs.
  283. $this->loadAllInputs();
  284. // Remove $_ENV and $_SERVER from the inputs.
  285. $inputs = $this->inputs;
  286. unset($inputs['env']);
  287. unset($inputs['server']);
  288. // Serialize the options, data, and inputs.
  289. return serialize(array($this->options, $this->data, $inputs));
  290. }
  291. /**
  292. * Method to unserialize the input.
  293. *
  294. * @param string $input The serialized input.
  295. *
  296. * @return JInput The input object.
  297. *
  298. * @since 12.1
  299. */
  300. public function unserialize($input)
  301. {
  302. // Unserialize the options, data, and inputs.
  303. list($this->options, $this->data, $this->inputs) = unserialize($input);
  304. // Load the filter.
  305. if (isset($this->options['filter']))
  306. {
  307. $this->filter = $this->options['filter'];
  308. }
  309. else
  310. {
  311. $this->filter = JFilterInput::getInstance();
  312. }
  313. }
  314. /**
  315. * Method to load all of the global inputs.
  316. *
  317. * @return void
  318. *
  319. * @since 12.1
  320. */
  321. protected function loadAllInputs()
  322. {
  323. static $loaded = false;
  324. if (!$loaded)
  325. {
  326. // Load up all the globals.
  327. foreach ($GLOBALS as $global => $data)
  328. {
  329. // Check if the global starts with an underscore.
  330. if (strpos($global, '_') === 0)
  331. {
  332. // Convert global name to input name.
  333. $global = strtolower($global);
  334. $global = substr($global, 1);
  335. // Get the input.
  336. $this->$global;
  337. }
  338. }
  339. $loaded = true;
  340. }
  341. }
  342. }