Merge pull request #3024 from splitbrain/cookieupdate
[dokuwiki.git] / inc / Input / Input.php
blob3d2426bcc9424ad6c4c4c797e94cb555a15ef783
1 <?php
3 namespace dokuwiki\Input;
5 /**
6 * Encapsulates access to the $_REQUEST array, making sure used parameters are initialized and
7 * have the correct type.
9 * All function access the $_REQUEST array by default, if you want to access $_POST or $_GET
10 * explicitly use the $post and $get members.
12 * @author Andreas Gohr <andi@splitbrain.org>
14 class Input
17 /** @var Post Access $_POST parameters */
18 public $post;
19 /** @var Get Access $_GET parameters */
20 public $get;
21 /** @var Server Access $_SERVER parameters */
22 public $server;
24 protected $access;
26 /**
27 * @var Callable
29 protected $filter;
31 /**
32 * Intilizes the dokuwiki\Input\Input class and it subcomponents
34 public function __construct()
36 $this->access = &$_REQUEST;
37 $this->post = new Post();
38 $this->get = new Get();
39 $this->server = new Server();
42 /**
43 * Apply the set filter to the given value
45 * @param string $data
46 * @return string
48 protected function applyfilter($data)
50 if (!$this->filter) return $data;
51 return call_user_func($this->filter, $data);
54 /**
55 * Return a filtered copy of the input object
57 * Expects a callable that accepts one string parameter and returns a filtered string
59 * @param Callable|string $filter
60 * @return Input
62 public function filter($filter = 'stripctl')
64 $this->filter = $filter;
65 $clone = clone $this;
66 $this->filter = '';
67 return $clone;
70 /**
71 * Check if a parameter was set
73 * Basically a wrapper around isset. When called on the $post and $get subclasses,
74 * the parameter is set to $_POST or $_GET and to $_REQUEST
76 * @see isset
77 * @param string $name Parameter name
78 * @return bool
80 public function has($name)
82 return isset($this->access[$name]);
85 /**
86 * Remove a parameter from the superglobals
88 * Basically a wrapper around unset. When NOT called on the $post and $get subclasses,
89 * the parameter will also be removed from $_POST or $_GET
91 * @see isset
92 * @param string $name Parameter name
94 public function remove($name)
96 if (isset($this->access[$name])) {
97 unset($this->access[$name]);
99 // also remove from sub classes
100 if (isset($this->post) && isset($_POST[$name])) {
101 unset($_POST[$name]);
103 if (isset($this->get) && isset($_GET[$name])) {
104 unset($_GET[$name]);
109 * Access a request parameter without any type conversion
111 * @param string $name Parameter name
112 * @param mixed $default Default to return if parameter isn't set
113 * @param bool $nonempty Return $default if parameter is set but empty()
114 * @return mixed
116 public function param($name, $default = null, $nonempty = false)
118 if (!isset($this->access[$name])) return $default;
119 $value = $this->applyfilter($this->access[$name]);
120 if ($nonempty && empty($value)) return $default;
121 return $value;
125 * Sets a parameter
127 * @param string $name Parameter name
128 * @param mixed $value Value to set
130 public function set($name, $value)
132 $this->access[$name] = $value;
136 * Get a reference to a request parameter
138 * This avoids copying data in memory, when the parameter is not set it will be created
139 * and intialized with the given $default value before a reference is returned
141 * @param string $name Parameter name
142 * @param mixed $default If parameter is not set, initialize with this value
143 * @param bool $nonempty Init with $default if parameter is set but empty()
144 * @return mixed (reference)
146 public function &ref($name, $default = '', $nonempty = false)
148 if (!isset($this->access[$name]) || ($nonempty && empty($this->access[$name]))) {
149 $this->set($name, $default);
152 return $this->access[$name];
156 * Access a request parameter as int
158 * @param string $name Parameter name
159 * @param int $default Default to return if parameter isn't set or is an array
160 * @param bool $nonempty Return $default if parameter is set but empty()
161 * @return int
163 public function int($name, $default = 0, $nonempty = false)
165 if (!isset($this->access[$name])) return $default;
166 if (is_array($this->access[$name])) return $default;
167 $value = $this->applyfilter($this->access[$name]);
168 if ($value === '') return $default;
169 if ($nonempty && empty($value)) return $default;
171 return (int)$value;
175 * Access a request parameter as string
177 * @param string $name Parameter name
178 * @param string $default Default to return if parameter isn't set or is an array
179 * @param bool $nonempty Return $default if parameter is set but empty()
180 * @return string
182 public function str($name, $default = '', $nonempty = false)
184 if (!isset($this->access[$name])) return $default;
185 if (is_array($this->access[$name])) return $default;
186 $value = $this->applyfilter($this->access[$name]);
187 if ($nonempty && empty($value)) return $default;
189 return (string)$value;
193 * Access a request parameter and make sure it is has a valid value
195 * Please note that comparisons to the valid values are not done typesafe (request vars
196 * are always strings) however the function will return the correct type from the $valids
197 * array when an match was found.
199 * @param string $name Parameter name
200 * @param array $valids Array of valid values
201 * @param mixed $default Default to return if parameter isn't set or not valid
202 * @return null|mixed
204 public function valid($name, $valids, $default = null)
206 if (!isset($this->access[$name])) return $default;
207 if (is_array($this->access[$name])) return $default; // we don't allow arrays
208 $value = $this->applyfilter($this->access[$name]);
209 $found = array_search($value, $valids);
210 if ($found !== false) return $valids[$found]; // return the valid value for type safety
211 return $default;
215 * Access a request parameter as bool
217 * Note: $nonempty is here for interface consistency and makes not much sense for booleans
219 * @param string $name Parameter name
220 * @param mixed $default Default to return if parameter isn't set
221 * @param bool $nonempty Return $default if parameter is set but empty()
222 * @return bool
224 public function bool($name, $default = false, $nonempty = false)
226 if (!isset($this->access[$name])) return $default;
227 if (is_array($this->access[$name])) return $default;
228 $value = $this->applyfilter($this->access[$name]);
229 if ($value === '') return $default;
230 if ($nonempty && empty($value)) return $default;
232 return (bool)$value;
236 * Access a request parameter as array
238 * @param string $name Parameter name
239 * @param mixed $default Default to return if parameter isn't set
240 * @param bool $nonempty Return $default if parameter is set but empty()
241 * @return array
243 public function arr($name, $default = array(), $nonempty = false)
245 if (!isset($this->access[$name])) return $default;
246 if (!is_array($this->access[$name])) return $default;
247 if ($nonempty && empty($this->access[$name])) return $default;
249 return (array)$this->access[$name];
253 * Create a simple key from an array key
255 * This is useful to access keys where the information is given as an array key or as a single array value.
256 * For example when the information was submitted as the name of a submit button.
258 * This function directly changes the access array.
260 * Eg. $_REQUEST['do']['save']='Speichern' becomes $_REQUEST['do'] = 'save'
262 * This function returns the $INPUT object itself for easy chaining
264 * @param string $name
265 * @return Input
267 public function extract($name)
269 if (!isset($this->access[$name])) return $this;
270 if (!is_array($this->access[$name])) return $this;
271 $keys = array_keys($this->access[$name]);
272 if (!$keys) {
273 // this was an empty array
274 $this->remove($name);
275 return $this;
277 // get the first key
278 $value = array_shift($keys);
279 if ($value === 0) {
280 // we had a numeric array, assume the value is not in the key
281 $value = array_shift($this->access[$name]);
284 $this->set($name, $value);
285 return $this;