1 <?php
2 /**
3 * @package FrameworkOnFramework
4 * @subpackage input
5 * @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
6 * @license GNU General Public License version 2 or later; see LICENSE.txt
7 */
8 // Protect from unauthorized access
9 defined('FOF_INCLUDED') or die;
10
11 if (version_compare(JVERSION, '1.7.0', 'lt'))
12 {
13 jimport('joomla.filter.input');
14 jimport('joomla.filter.filterinput');
15 jimport('joomla.base.object');
16
17 require_once __DIR__ . '/jinput/input.php';
18 require_once __DIR__ . '/jinput/cli.php';
19 require_once __DIR__ . '/jinput/cookie.php';
20 require_once __DIR__ . '/jinput/files.php';
21 require_once __DIR__ . '/jinput/json.php';
22 }
23 elseif (version_compare(JVERSION, '2.5.0', 'lt'))
24 {
25 jimport('joomla.application.input');
26 jimport('joomla.input.input');
27 }
28
29 /**
30 * FrameworkOnFramework input handling class. Extends upon the JInput class.
31 *
32 * @package FrameworkOnFramework
33 * @since 2.0
34 */
35 class FOFInput extends JInput
36 {
37 /**
38 * Public constructor. Overriden to allow specifying the global input array
39 * to use as a string and instantiate from an objetc holding variables.
40 *
41 * @param array|string|object|null $source Source data; set null to use $_REQUEST
42 * @param array $options Filter options
43 */
44 public function __construct($source = null, array $options = array())
45 {
46 $hash = null;
47
48 if (is_string($source))
49 {
50 $hash = strtoupper($source);
51
52 switch ($hash)
53 {
54 case 'GET':
55 $source = $_GET;
56 break;
57 case 'POST':
58 $source = $_POST;
59 break;
60 case 'FILES':
61 $source = $_FILES;
62 break;
63 case 'COOKIE':
64 $source = $_COOKIE;
65 break;
66 case 'ENV':
67 $source = $_ENV;
68 break;
69 case 'SERVER':
70 $source = $_SERVER;
71 break;
72 default:
73 $source = $_REQUEST;
74 $hash = 'REQUEST';
75 break;
76 }
77 }
78 elseif (is_object($source))
79 {
80 try
81 {
82 $source = (array) $source;
83 }
84 catch (Exception $exc)
85 {
86 $source = null;
87 }
88 }
89 elseif (is_array($source))
90 {
91 // Nothing, it's already an array
92 }
93 else
94 {
95 // Any other case
96 $source = $_REQUEST;
97 $hash = 'REQUEST';
98 }
99
100 // Magic quotes GPC handling (something JInput simply can't handle at all)
101
102 if (($hash == 'REQUEST') && get_magic_quotes_gpc() && class_exists('JRequest', true))
103 {
104 $source = JRequest::get('REQUEST', 2);
105 }
106
107 parent::__construct($source, $options);
108 }
109
110 /**
111 * Gets a value from the input data. Overriden to allow specifying a filter
112 * mask.
113 *
114 * @param string $name Name of the value to get.
115 * @param mixed $default Default value to return if variable does not exist.
116 * @param string $filter Filter to apply to the value.
117 * @param int $mask The filter mask
118 *
119 * @return mixed The filtered input value.
120 */
121 public function get($name, $default = null, $filter = 'cmd', $mask = 0)
122 {
123 if (isset($this->data[$name]))
124 {
125 return $this->_cleanVar($this->data[$name], $mask, $filter);
126 }
127
128 return $default;
129 }
130
131 /**
132 * Returns a copy of the raw data stored in the class
133 *
134 * @return array
135 */
136 public function getData()
137 {
138 return $this->data;
139 }
140
141 /**
142 * Old static methods are now deprecated. This magic method makes sure there
143 * is a continuity in our approach. The downside is that it's only compatible
144 * with PHP 5.3.0. Sorry!
145 *
146 * @param string $name Name of the method we're calling
147 * @param array $arguments The arguments passed to the method
148 *
149 * @return mixed
150 */
151 public static function __callStatic($name, $arguments)
152 {
153 FOFPlatform::getInstance()->logDeprecated('FOFInput: static getXXX() methods are deprecated. Use the input object\'s methods instead.');
154
155 if (substr($name, 0, 3) == 'get')
156 {
157 // Initialise arguments
158 $key = array_shift($arguments);
159 $default = array_shift($arguments);
160 $input = array_shift($arguments);
161 $type = 'none';
162 $mask = 0;
163
164 $type = strtolower(substr($name, 3));
165
166 if ($type == 'var')
167 {
168 $type = array_shift($arguments);
169 $mask = array_shift($arguments);
170 }
171
172 if (is_null($type))
173 {
174 $type = 'none';
175 }
176
177 if (is_null($mask))
178 {
179 $mask = 0;
180 }
181
182 if (!($input instanceof FOFInput) && !($input instanceof JInput))
183 {
184 $input = new FOFInput($input);
185 }
186
187 return $input->get($key, $default, $type, $mask);
188 }
189
190 return false;
191 }
192
193 /**
194 * Magic method to get filtered input data.
195 *
196 * @param mixed $name Name of the value to get.
197 * @param string $arguments Default value to return if variable does not exist.
198 *
199 * @return boolean The filtered boolean input value.
200 */
201 public function __call($name, $arguments)
202 {
203 if (substr($name, 0, 3) == 'get')
204 {
205 $filter = substr($name, 3);
206
207 $default = null;
208 $mask = 0;
209
210 if (isset($arguments[1]))
211 {
212 $default = $arguments[1];
213 }
214
215 if (isset($arguments[2]))
216 {
217 $mask = $arguments[2];
218 }
219
220 return $this->get($arguments[0], $default, $filter, $mask);
221 }
222 }
223
224 /**
225 * Sets an input variable. WARNING: IT SHOULD NO LONGER BE USED!
226 *
227 * @param string $name The name of the variable to set
228 * @param mixed $value The value to set it to
229 * @param array &$input The input array or FOFInput object
230 * @param boolean $overwrite Should I overwrite existing values (default: true)
231 *
232 * @return string Previous value
233 *
234 * @deprecated
235 */
236 public static function setVar($name, $value = null, &$input = array(), $overwrite = true)
237 {
238 FOFPlatform::getInstance()->logDeprecated('FOFInput::setVar() is deprecated. Use set() instead.');
239
240 if (empty($input))
241 {
242 return JRequest::setVar($name, $value, 'default', $overwrite);
243 }
244 elseif (is_string($input))
245 {
246 return JRequest::setVar($name, $value, $input, $overwrite);
247 }
248 else
249 {
250 if (!$overwrite && array_key_exists($name, $input))
251 {
252 return $input[$name];
253 }
254
255 $previous = array_key_exists($name, $input) ? $input[$name] : null;
256
257 if (is_array($input))
258 {
259 $input[$name] = $value;
260 }
261 elseif ($input instanceof FOFInput)
262 {
263 $input->set($name, $value);
264 }
265
266 return $previous;
267 }
268 }
269
270 /**
271 * Custom filter implementation. Works better with arrays and allows the use
272 * of a filter mask.
273 *
274 * @param mixed $var The variable (value) to clean
275 * @param integer $mask The clean mask
276 * @param string $type The variable type
277 *
278 * @return mixed
279 */
280 protected function _cleanVar($var, $mask = 0, $type = null)
281 {
282 if (is_array($var))
283 {
284 $temp = array();
285
286 foreach ($var as $k => $v)
287 {
288 $temp[$k] = self::_cleanVar($v, $mask);
289 }
290
291 return $temp;
292 }
293
294 // If the no trim flag is not set, trim the variable
295 if (!($mask & 1) && is_string($var))
296 {
297 $var = trim($var);
298 }
299
300 // Now we handle input filtering
301 if ($mask & 2)
302 {
303 // If the allow raw flag is set, do not modify the variable
304 $var = $var;
305 }
306 elseif ($mask & 4)
307 {
308 // If the allow HTML flag is set, apply a safe HTML filter to the variable
309 $safeHtmlFilter = JFilterInput::getInstance(null, null, 1, 1);
310 $var = $safeHtmlFilter->clean($var, $type);
311 }
312 else
313 {
314 $var = $this->filter->clean($var, $type);
315 }
316
317 return $var;
318 }
319 }
320