/libraries/joomla/input/input.php

https://github.com/dextercowley/joomla-platform · PHP · 348 lines · 153 code · 33 blank · 162 comment · 16 complexity · dd1d043deae0cd90bf85beb2879ddec2 MD5 · raw file

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