-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form.Field.File.class.php
309 lines (270 loc) · 6.3 KB
/
Form.Field.File.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
require_once dirname(__FILE__) . "/Form.class.php";
/**
*
* FORM_FILE
*
*
* An HTML file input
*
*/
class FORM_FILE extends FORM_FIELD {
/**
* Keys found in a $_FILES entry, for reference
*
* @var array
*/
public static $infokeys = array('name', 'type', 'size', 'tmp_name', 'error');
/**
* The $_FILES entry associated to this file field, if any
*
* @var array
*/
protected $file_info;
/**
* The class.upload.php object set when a file is uploaded
*
* @var upload
*/
protected $handle;
/**
* The list of class.upload.php options to set
*
* Needed since options are most likely set before the handle is created,
* notice that initHandle() will copy these options when creating the handle
*
* @var array
*/
protected $options = array();
/**
* The target directory to be passed to class.upload.php's process()
*
* @var string
*/
protected $target_dir;
/**
* Optional user defined processor, has keys 'func' and 'args'
*
* @var array
*/
protected $processor;
/*******************
** CONSTRUCTOR **
*******************/
/**
* Constructor
*
* @param FORM_FIELDSET $parent
* @param string $name
* @param array|string $labels
* @param string $target_dir
* @param array $options
* @return FORM_FILE
*/
public function __construct(&$parent, $name, $labels=null, $target_dir=null, array $options=array()) {
parent::__construct($parent, $name, $labels);
$this->setTargetDir($target_dir);
$this->setProcessingOptionsArray($options);
}
/***************
** OPTIONS **
***************/
/**
* Sets uploaded file information (an entry from $_FILES) and initializes a handle with it
*
* @param array $file_info An entry from $_FILES
* @return FORM_FILE
*/
public function setUploadedFileInfo(array $file_info) {
$this->file_info = $file_info;
$this->initHandle();
return $this;
}
/**
* Return the file information array,
* or a specific item is $arg is provided
*
* @param string $arg
* @return array|string
*/
public function getFileInfo($arg=null) {
$info = $this->file_info;
return $arg ? $info[$arg] : $info;
}
/**
* Creates a new handle and copies
* over previously set options
*
* @return FORM_FILE
*/
public function initHandle() {
$this->handle = new upload($this->file_info);
foreach ($this->options as $opt => $val) {
$this->handle->{$opt} = $val;
}
return $this;
}
/**
* Get the handle, which is possibly null
*
* @return UPLOAD
*/
public function getHandle() {
return $this->handle;
}
/**
* Whether a file was sucessfully uploaded
* (note: not necessarily processed)
*
* @return boolean
*/
public function hasUploadedFile() {
$handle = $this->getHandle();
return ($handle && $handle->uploaded);
}
/**
* Set the target directory, which will be passed
* to class.upload.php's process()
*
* @param string $dir
* @return FORM_FILE
*/
public function setTargetDir($dir) {
$this->target_dir = $dir;
return $this;
}
/**
* Return the currently set target directory
*
* @return string
*/
public function getTargetDir() {
return $this->target_dir;
}
/**
* Set an array's worth or processing options,
* each key should be a valid class.upload.php option
*
* @param array $options
* @return FORM_FILE
*/
public function setProcessingOptionsArray(array $options) {
foreach ($options as $opt => $val) {
$this->setProcessingOption($opt, $val);
}
return $this;
}
/**
* Set an individual processing option,
* the option name should be a valid class.upload.php option
*
* @param string $opt
* @param mixed $val
* @return FORM_FILE
*/
public function setProcessingOption($opt, $val) {
$this->options[$opt] = $val;
if ($this->getHandle()) {
$this->getHandle()->{$opt} = $val;
}
return $this;
}
/**
* Clear all previously set processing options and loading defaults
*
* @return FORM_FILE
*/
public function clearProcessingOptions() {
$this->options = array();
if ($this->getHandle()) {
$this->getHandle()->init();
}
return $this;
}
/**
* Get a specific processing option,
* the option name should be a valid class.upload.php option
*
* @param string $opt
* @return mixed
*/
public function getProcessingOption($opt) {
$handle = $this->getHandle();
return $handle ? $handle->{$opt} : $this->options[$opt];
}
/******************
** PROCESSING **
******************/
/**
* Set a processor, with optiona argument array that will be passed to it
*
* @param callable $processor
* @param array $args
* @return FORM_FILE
*/
public function setProcesssor($processor, array $args=array()) {
$this->processor = array(
'func' => $processor,
'args' => $args
);
return $this;
}
/**
* Get the currently set processor, which will be an array
* with the keys 'func' and 'args', or null is none set
*
* @return array
*/
public function getProcessor() {
return $this->processor;
}
/**
* Remove the currently set processor
*
* @return FORM_FILE
*/
public function clearProcessor() {
unset($this->processor);
return $this;
}
/**
* Process the uploaded file by calling the
* processor, or, if none are set, the default processor
*
* @return FORM_FILE
*/
public function process() {
if ($this->hasUploadedFile()) {
$processor = $this->getProcessor();
if (empty($processor)) {
$processor = array(
'func' => array("FORM_FILE", "default_processor"),
'args' => array()
);
}
if (!is_callable($processor['func'])) {
throw new FormInvalidProcessor("The processor is not a callable", $this, $processor);
}
$arguments = array_merge(array($this, $this->getTargetDir(), $this->getHandle()), (array)$processor['args']);
$error = call_user_func_array($processor['func'], $arguments);
if (!empty($error)) {
$error = $this->name() . ": " . $error;
throw new FormFileUploadError($error, $this);
}
}
return $this;
}
/**
* The default processor to run when there is no other processor set,
* returns the error text, if any
*
* @param FORM_FILE $elem
* @param string $target_dir
* @param UPLOAD $handle
* @return string
*/
public static function default_processor($elem, $target_dir, $handle) {
$handle->process($target_dir);
return $handle->processed ? "" : $handle->error;
}
}