1 <?php
2 /**
3 * Part of the Joomla Framework Input Package
4 *
5 * @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
6 * @license GNU General Public License version 2 or later; see LICENSE
7 */
8
9 namespace Joomla\Input;
10
11 use Joomla\Filter;
12
13 /**
14 * Joomla! Input Files Class
15 *
16 * @since 1.0
17 */
18 class Files extends Input
19 {
20 /**
21 * The pivoted data from a $_FILES or compatible array.
22 *
23 * @var array
24 * @since 1.0
25 */
26 protected $decodedData = array();
27
28 /**
29 * The class constructor.
30 *
31 * @param array $source The source argument is ignored. $_FILES is always used.
32 * @param array $options An optional array of configuration options:
33 * filter : a custom JFilterInput object.
34 *
35 * @since 1.0
36 */
37 public function __construct(array $source = null, array $options = array())
38 {
39 if (isset($options['filter']))
40 {
41 $this->filter = $options['filter'];
42 }
43 else
44 {
45 $this->filter = new Filter\InputFilter;
46 }
47
48 // Set the data source.
49 $this->data = & $_FILES;
50
51 // Set the options for the class.
52 $this->options = $options;
53 }
54
55 /**
56 * Gets a value from the input data.
57 *
58 * @param string $name The name of the input property (usually the name of the files INPUT tag) to get.
59 * @param mixed $default The default value to return if the named property does not exist.
60 * @param string $filter The filter to apply to the value.
61 *
62 * @return mixed The filtered input value.
63 *
64 * @see \Joomla\Filter\InputFilter::clean()
65 * @since 1.0
66 */
67 public function get($name, $default = null, $filter = 'cmd')
68 {
69 if (isset($this->data[$name]))
70 {
71 $results = $this->decodeData(
72 array(
73 $this->data[$name]['name'],
74 $this->data[$name]['type'],
75 $this->data[$name]['tmp_name'],
76 $this->data[$name]['error'],
77 $this->data[$name]['size']
78 )
79 );
80
81 return $results;
82 }
83
84 return $default;
85 }
86
87 /**
88 * Method to decode a data array.
89 *
90 * @param array $data The data array to decode.
91 *
92 * @return array
93 *
94 * @since 1.0
95 */
96 protected function decodeData(array $data)
97 {
98 $result = array();
99
100 if (is_array($data[0]))
101 {
102 foreach ($data[0] as $k => $v)
103 {
104 $result[$k] = $this->decodeData(array($data[0][$k], $data[1][$k], $data[2][$k], $data[3][$k], $data[4][$k]));
105 }
106
107 return $result;
108 }
109
110 return array('name' => $data[0], 'type' => $data[1], 'tmp_name' => $data[2], 'error' => $data[3], 'size' => $data[4]);
111 }
112
113 /**
114 * Sets a value.
115 *
116 * @param string $name The name of the input property to set.
117 * @param mixed $value The value to assign to the input property.
118 *
119 * @return void
120 *
121 * @since 1.0
122 */
123 public function set($name, $value)
124 {
125 // Restricts the usage of parent's set method.
126 }
127 }
128