/wire/core/Input.php

https://github.com/fluctusz/P21 · PHP · 284 lines · 123 code · 40 blank · 121 comment · 24 complexity · 39bd8357321c3cac742c899a4ba0e6fb MD5 · raw file

  1. <?php
  2. /**
  3. * ProcessWire WireInputData and WireInput
  4. *
  5. * WireInputData and the WireInput class together form a simple
  6. * front end to PHP's $_GET, $_POST, and $_COOKIE superglobals.
  7. *
  8. * ProcessWire 2.x
  9. * Copyright (C) 2010 by Ryan Cramer
  10. * Licensed under GNU/GPL v2, see LICENSE.TXT
  11. *
  12. * http://www.processwire.com
  13. * http://www.ryancramer.com
  14. *
  15. */
  16. /**
  17. * WireInputData manages one of GET, POST, COOKIE, or whitelist
  18. *
  19. * Vars retrieved from here will not have to consider magic_quotes.
  20. * No sanitization or filtering is done, other than disallowing multi-dimensional arrays in input.
  21. *
  22. * WireInputData specifically manages one of: get, post, cookie or whitelist, whereas the Input class
  23. * provides access to the 3 InputData instances.
  24. *
  25. * Each WireInputData is not instantiated unless specifically asked for.
  26. *
  27. */
  28. class WireInputData implements ArrayAccess, IteratorAggregate, Countable {
  29. protected $stripSlashes = false;
  30. protected $data = array();
  31. public function __construct(array $input = array()) {
  32. $this->stripSlashes = get_magic_quotes_gpc();
  33. $this->setArray($input);
  34. }
  35. public function setArray(array $input) {
  36. foreach($input as $key => $value) $this->__set($key, $value);
  37. return $this;
  38. }
  39. public function getArray() {
  40. return $this->data;
  41. }
  42. public function __set($key, $value) {
  43. if(is_string($value) && $this->stripSlashes) $value = stripslashes($value);
  44. if(is_array($value)) $value = $this->cleanArray($value);
  45. $this->data[$key] = $value;
  46. }
  47. protected function cleanArray(array $a) {
  48. $clean = array();
  49. foreach($a as $key => $value) {
  50. if(is_array($value)) continue; // we only allow one dimensional arrays
  51. if(is_string($value) && $this->stripSlashes) $value = stripslashes($value);
  52. $clean[$key] = $value;
  53. }
  54. return $clean;
  55. }
  56. public function setStripSlashes($stripSlashes) {
  57. $this->stripSlashes = $stripSlashes ? true : false;
  58. }
  59. public function __get($key) {
  60. if($key == 'whitelist') return $this->whitelist;
  61. return isset($this->data[$key]) ? $this->data[$key] : null;
  62. }
  63. public function getIterator() {
  64. return new ArrayObject($this->data);
  65. }
  66. public function offsetExists($key) {
  67. return isset($this->data[$key]);
  68. }
  69. public function offsetGet($key) {
  70. return $this->__get($key);
  71. }
  72. public function offsetSet($key, $value) {
  73. $this->__set($key, $value);
  74. }
  75. public function offsetUnset($key) {
  76. unset($this->data[$key]);
  77. }
  78. public function count() {
  79. return count($this->data);
  80. }
  81. public function removeAll() {
  82. $this->data = array();
  83. }
  84. public function __isset($key) {
  85. return $this->offsetExists($key);
  86. }
  87. public function __unset($key) {
  88. return $this->offsetUnset($key);
  89. }
  90. }
  91. /**
  92. * Manages the group of GET, POST, COOKIE and whitelist vars, each of which is a WireInputData object.
  93. *
  94. */
  95. class WireInput {
  96. protected $getVars = null;
  97. protected $postVars = null;
  98. protected $cookieVars = null;
  99. protected $whitelist = null;
  100. protected $urlSegments = array();
  101. protected $pageNum = 1;
  102. /**
  103. * Retrieve a GET value or all GET values
  104. *
  105. * @param blank|string
  106. * If populated, returns the value corresponding to the key or NULL if it doesn't exist.
  107. * If blank, returns reference to the WireDataInput containing all GET vars.
  108. * @return null|mixed|WireDataInput
  109. *
  110. */
  111. public function get($key = '') {
  112. if(is_null($this->getVars)) $this->getVars = new WireInputData($_GET);
  113. return $key ? $this->getVars->__get($key) : $this->getVars;
  114. }
  115. /**
  116. * Retrieve a POST value or all POST values
  117. *
  118. * @param blank|string
  119. * If populated, returns the value corresponding to the key or NULL if it doesn't exist.
  120. * If blank, returns reference to the WireDataInput containing all POST vars.
  121. * @return null|mixed|WireDataInput
  122. *
  123. */
  124. public function post($key = '') {
  125. if(is_null($this->postVars)) $this->postVars = new WireInputData($_POST);
  126. return $key ? $this->postVars->__get($key) : $this->postVars;
  127. }
  128. /**
  129. * Retrieve a COOKIE value or all COOKIE values
  130. *
  131. * @param blank|string
  132. * If populated, returns the value corresponding to the key or NULL if it doesn't exist.
  133. * If blank, returns reference to the WireDataInput containing all COOKIE vars.
  134. * @return null|mixed|WireDataInput
  135. *
  136. */
  137. public function cookie($key = '') {
  138. if(is_null($this->cookieVars)) $this->cookieVars = new WireInputData($_COOKIE);
  139. return $key ? $this->cookieVars->get($key) : $this->cookieVars;
  140. }
  141. /**
  142. * Get or set a whitelist var
  143. *
  144. * Whitelist vars are used by modules and templates and assumed to be clean.
  145. *
  146. * The whitelist is a list of variables specifically set by the application as clean for use elsewhere in the application.
  147. * Only the version returned from this method should be considered clean.
  148. * This whitelist is not specifically used by ProcessWire unless you populate it from your templates or the API.
  149. *
  150. * @param string $key
  151. * If $key is blank, it assumes you are asking to return the entire whitelist.
  152. * If $key and $value are populated, it adds the value to the whitelist.
  153. * If $key is an array, it adds all the values present in the array to the whitelist.
  154. * If $value is ommited, it assumes you are asking for a value with $key, in which case it returns it.
  155. * @param mixed $value
  156. * See explanation for the $key param
  157. * @return null|mixed|WireDataInput
  158. * See explanation for the $key param
  159. *
  160. */
  161. public function whitelist($key = '', $value = null) {
  162. if(is_null($this->whitelist)) $this->whitelist = new WireInputData();
  163. if(!$key) return $this->whitelist;
  164. if(is_array($key)) return $this->whitelist->setArray($key);
  165. if(is_null($value)) return $this->whitelist->__get($key);
  166. $this->whitelist->__set($key, $value);
  167. return $this->whitelist;
  168. }
  169. /**
  170. * Retrieve the URL segment with index $num
  171. *
  172. * Note that the index is 1 based (not 0 based)
  173. *
  174. * Returns a blank string if the specified index is not found.
  175. *
  176. * @param int $num
  177. * @return string
  178. *
  179. */
  180. public function urlSegment($num = 1) {
  181. if($num < 1) $num = 1;
  182. return isset($this->urlSegments[$num]) ? $this->urlSegments[$num] : '';
  183. }
  184. /**
  185. * Set a URL segment value
  186. *
  187. * @param int $num Number of this URL segment (1 based)
  188. * @param string $value
  189. *
  190. */
  191. public function setUrlSegment($num, $value) {
  192. $this->urlSegments[(int)$num] = (string) $value;
  193. }
  194. /**
  195. * Return the current page number.
  196. *
  197. * First page number is 1 (not 0).
  198. *
  199. * @return int
  200. *
  201. */
  202. public function pageNum() {
  203. return $this->pageNum;
  204. }
  205. /**
  206. * Set the current page number.
  207. *
  208. * Note that the first page should be 1 (not 0).
  209. *
  210. * @param int $num
  211. *
  212. */
  213. public function setPageNum($num) {
  214. $this->pageNum = (int) $num;
  215. }
  216. /**
  217. * Retrieve the get, post, cookie or whitelist vars using a direct reference, i.e. $input->cookie
  218. *
  219. * Can also be used with URL segments, i.e. $input->urlSegment1, $input->urlSegment2, $input->urlSegment3, etc.
  220. * And can also be used for $input->pageNum.
  221. *
  222. * @param string $key
  223. * @return string|int|null
  224. *
  225. */
  226. public function __get($key) {
  227. if($key == 'pageNum') return $this->pageNum;
  228. if($key == 'urlSegments') return $this->urlSegments;
  229. if(strpos($key, 'urlSegment') === 0) {
  230. if(strlen($key) > 10) $num = (int) substr($key, 10);
  231. else $num = 1;
  232. return $this->urlSegment($num);
  233. }
  234. $value = null;
  235. $gpc = array('get', 'post', 'cookie', 'whitelist');
  236. if(in_array($key, $gpc)) {
  237. $value = $this->$key();
  238. }
  239. return $value;
  240. }
  241. public function __isset($key) {
  242. return $this->__get($key) !== null;
  243. }
  244. }