Http.php
15.2 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_File_Transfer
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Http.php 23775 2011-03-01 17:25:24Z ralph $
*/
/**
* @see Zend_File_Transfer_Adapter_Abstract
*/
require_once 'Zend/File/Transfer/Adapter/Abstract.php';
/**
* File transfer adapter class for the HTTP protocol
*
* @category Zend
* @package Zend_File_Transfer
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_File_Transfer_Adapter_Http extends Zend_File_Transfer_Adapter_Abstract
{
protected static $_callbackApc = 'apc_fetch';
protected static $_callbackUploadProgress = 'uploadprogress_get_info';
/**
* Constructor for Http File Transfers
*
* @param array $options OPTIONAL Options to set
*/
public function __construct($options = array())
{
if (ini_get('file_uploads') == false) {
require_once 'Zend/File/Transfer/Exception.php';
throw new Zend_File_Transfer_Exception('File uploads are not allowed in your php config!');
}
$this->setOptions($options);
$this->_prepareFiles();
$this->addValidator('Upload', false, $this->_files);
}
/**
* Sets a validator for the class, erasing all previous set
*
* @param string|array $validator Validator to set
* @param string|array $files Files to limit this validator to
* @return Zend_File_Transfer_Adapter
*/
public function setValidators(array $validators, $files = null)
{
$this->clearValidators();
return $this->addValidators($validators, $files);
}
/**
* Remove an individual validator
*
* @param string $name
* @return Zend_File_Transfer_Adapter_Abstract
*/
public function removeValidator($name)
{
if ($name == 'Upload') {
return $this;
}
return parent::removeValidator($name);
}
/**
* Remove an individual validator
*
* @param string $name
* @return Zend_File_Transfer_Adapter_Abstract
*/
public function clearValidators()
{
parent::clearValidators();
$this->addValidator('Upload', false, $this->_files);
return $this;
}
/**
* Send the file to the client (Download)
*
* @param string|array $options Options for the file(s) to send
* @return void
* @throws Zend_File_Transfer_Exception Not implemented
*/
public function send($options = null)
{
require_once 'Zend/File/Transfer/Exception.php';
throw new Zend_File_Transfer_Exception('Method not implemented');
}
/**
* Checks if the files are valid
*
* @param string|array $files (Optional) Files to check
* @return boolean True if all checks are valid
*/
public function isValid($files = null)
{
// Workaround for WebServer not conforming HTTP and omitting CONTENT_LENGTH
$content = 0;
if (isset($_SERVER['CONTENT_LENGTH'])) {
$content = $_SERVER['CONTENT_LENGTH'];
} else if (!empty($_POST)) {
$content = serialize($_POST);
}
// Workaround for a PHP error returning empty $_FILES when form data exceeds php settings
if (empty($this->_files) && ($content > 0)) {
if (is_array($files)) {
$files = current($files);
}
$temp = array($files => array(
'name' => $files,
'error' => 1));
$validator = $this->_validators['Zend_Validate_File_Upload'];
$validator->setFiles($temp)
->isValid($files, null);
$this->_messages += $validator->getMessages();
return false;
}
return parent::isValid($files);
}
/**
* Receive the file from the client (Upload)
*
* @param string|array $files (Optional) Files to receive
* @return bool
*/
public function receive($files = null)
{
if (!$this->isValid($files)) {
return false;
}
$check = $this->_getFiles($files);
foreach ($check as $file => $content) {
if (!$content['received']) {
$directory = '';
$destination = $this->getDestination($file);
if ($destination !== null) {
$directory = $destination . DIRECTORY_SEPARATOR;
}
$filename = $directory . $content['name'];
$rename = $this->getFilter('Rename');
if ($rename !== null) {
$tmp = $rename->getNewName($content['tmp_name']);
if ($tmp != $content['tmp_name']) {
$filename = $tmp;
}
if (dirname($filename) == '.') {
$filename = $directory . $filename;
}
$key = array_search(get_class($rename), $this->_files[$file]['filters']);
unset($this->_files[$file]['filters'][$key]);
}
// Should never return false when it's tested by the upload validator
if (!move_uploaded_file($content['tmp_name'], $filename)) {
if ($content['options']['ignoreNoFile']) {
$this->_files[$file]['received'] = true;
$this->_files[$file]['filtered'] = true;
continue;
}
$this->_files[$file]['received'] = false;
return false;
}
if ($rename !== null) {
$this->_files[$file]['destination'] = dirname($filename);
$this->_files[$file]['name'] = basename($filename);
}
$this->_files[$file]['tmp_name'] = $filename;
$this->_files[$file]['received'] = true;
}
if (!$content['filtered']) {
if (!$this->_filter($file)) {
$this->_files[$file]['filtered'] = false;
return false;
}
$this->_files[$file]['filtered'] = true;
}
}
return true;
}
/**
* Checks if the file was already sent
*
* @param string|array $file Files to check
* @return bool
* @throws Zend_File_Transfer_Exception Not implemented
*/
public function isSent($files = null)
{
require_once 'Zend/File/Transfer/Exception.php';
throw new Zend_File_Transfer_Exception('Method not implemented');
}
/**
* Checks if the file was already received
*
* @param string|array $files (Optional) Files to check
* @return bool
*/
public function isReceived($files = null)
{
$files = $this->_getFiles($files, false, true);
if (empty($files)) {
return false;
}
foreach ($files as $content) {
if ($content['received'] !== true) {
return false;
}
}
return true;
}
/**
* Checks if the file was already filtered
*
* @param string|array $files (Optional) Files to check
* @return bool
*/
public function isFiltered($files = null)
{
$files = $this->_getFiles($files, false, true);
if (empty($files)) {
return false;
}
foreach ($files as $content) {
if ($content['filtered'] !== true) {
return false;
}
}
return true;
}
/**
* Has a file been uploaded ?
*
* @param array|string|null $file
* @return bool
*/
public function isUploaded($files = null)
{
$files = $this->_getFiles($files, false, true);
if (empty($files)) {
return false;
}
foreach ($files as $file) {
if (empty($file['name'])) {
return false;
}
}
return true;
}
/**
* Returns the actual progress of file up-/downloads
*
* @param string $id The upload to get the progress for
* @return array|null
*/
public static function getProgress($id = null)
{
if (!function_exists('apc_fetch') and !function_exists('uploadprogress_get_info')) {
require_once 'Zend/File/Transfer/Exception.php';
throw new Zend_File_Transfer_Exception('Neither APC nor uploadprogress extension installed');
}
$session = 'Zend_File_Transfer_Adapter_Http_ProgressBar';
$status = array(
'total' => 0,
'current' => 0,
'rate' => 0,
'message' => '',
'done' => false
);
if (is_array($id)) {
if (isset($id['progress'])) {
$adapter = $id['progress'];
}
if (isset($id['session'])) {
$session = $id['session'];
}
if (isset($id['id'])) {
$id = $id['id'];
} else {
unset($id);
}
}
if (!empty($id) && (($id instanceof Zend_ProgressBar_Adapter) || ($id instanceof Zend_ProgressBar))) {
$adapter = $id;
unset($id);
}
if (empty($id)) {
if (!isset($_GET['progress_key'])) {
$status['message'] = 'No upload in progress';
$status['done'] = true;
} else {
$id = $_GET['progress_key'];
}
}
if (!empty($id)) {
if (self::isApcAvailable()) {
$call = call_user_func(self::$_callbackApc, ini_get('apc.rfc1867_prefix') . $id);
if (is_array($call)) {
$status = $call + $status;
}
} else if (self::isUploadProgressAvailable()) {
$call = call_user_func(self::$_callbackUploadProgress, $id);
if (is_array($call)) {
$status = $call + $status;
$status['total'] = $status['bytes_total'];
$status['current'] = $status['bytes_uploaded'];
$status['rate'] = $status['speed_average'];
if ($status['total'] == $status['current']) {
$status['done'] = true;
}
}
}
if (!is_array($call)) {
$status['done'] = true;
$status['message'] = 'Failure while retrieving the upload progress';
} else if (!empty($status['cancel_upload'])) {
$status['done'] = true;
$status['message'] = 'The upload has been canceled';
} else {
$status['message'] = self::_toByteString($status['current']) . " - " . self::_toByteString($status['total']);
}
$status['id'] = $id;
}
if (isset($adapter) && isset($status['id'])) {
if ($adapter instanceof Zend_ProgressBar_Adapter) {
require_once 'Zend/ProgressBar.php';
$adapter = new Zend_ProgressBar($adapter, 0, $status['total'], $session);
}
if (!($adapter instanceof Zend_ProgressBar)) {
require_once 'Zend/File/Transfer/Exception.php';
throw new Zend_File_Transfer_Exception('Unknown Adapter given');
}
if ($status['done']) {
$adapter->finish();
} else {
$adapter->update($status['current'], $status['message']);
}
$status['progress'] = $adapter;
}
return $status;
}
/**
* Checks the APC extension for progress information
*
* @return boolean
*/
public static function isApcAvailable()
{
return (bool) ini_get('apc.enabled') && (bool) ini_get('apc.rfc1867') && is_callable(self::$_callbackApc);
}
/**
* Checks the UploadProgress extension for progress information
*
* @return boolean
*/
public static function isUploadProgressAvailable()
{
return is_callable(self::$_callbackUploadProgress);
}
/**
* Prepare the $_FILES array to match the internal syntax of one file per entry
*
* @param array $files
* @return array
*/
protected function _prepareFiles()
{
$this->_files = array();
foreach ($_FILES as $form => $content) {
if (is_array($content['name'])) {
foreach ($content as $param => $file) {
foreach ($file as $number => $target) {
$this->_files[$form . '_' . $number . '_'][$param] = $target;
$this->_files[$form]['multifiles'][$number] = $form . '_' . $number . '_';
}
}
$this->_files[$form]['name'] = $form;
foreach($this->_files[$form]['multifiles'] as $key => $value) {
$this->_files[$value]['options'] = $this->_options;
$this->_files[$value]['validated'] = false;
$this->_files[$value]['received'] = false;
$this->_files[$value]['filtered'] = false;
$mimetype = $this->_detectMimeType($this->_files[$value]);
$this->_files[$value]['type'] = $mimetype;
$filesize = $this->_detectFileSize($this->_files[$value]);
$this->_files[$value]['size'] = $filesize;
if ($this->_options['detectInfos']) {
$_FILES[$form]['type'][$key] = $mimetype;
$_FILES[$form]['size'][$key] = $filesize;
}
}
} else {
$this->_files[$form] = $content;
$this->_files[$form]['options'] = $this->_options;
$this->_files[$form]['validated'] = false;
$this->_files[$form]['received'] = false;
$this->_files[$form]['filtered'] = false;
$mimetype = $this->_detectMimeType($this->_files[$form]);
$this->_files[$form]['type'] = $mimetype;
$filesize = $this->_detectFileSize($this->_files[$form]);
$this->_files[$form]['size'] = $filesize;
if ($this->_options['detectInfos']) {
$_FILES[$form]['type'] = $mimetype;
$_FILES[$form]['size'] = $filesize;
}
}
}
return $this;
}
}