2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * file upload functions
6 * @package PMA\libraries
8 namespace PMA\libraries
;
10 use PMA\libraries\config\ConfigFile
;
15 * @todo when uploading a file into a blob field, should we also consider using
16 * chunks like in import? UPDATE `table` SET `field` = `field` + [chunk]
18 * @package PMA\libraries
23 * @var string the temporary file name
29 * @var string the content
35 * @var Message|null the error message
38 var $_error_message = null;
41 * @var bool whether the file is temporary or not
44 var $_is_temp = false;
47 * @var string type of compression
50 var $_compression = null;
58 * @var integer size of chunk to read with every step
60 var $_chunk_size = 32768;
63 * @var resource file handle
68 * @var boolean whether to decompress content before returning
70 var $_decompress = false;
73 * @var string charset of file
80 * @param boolean|string $name file name or false
84 public function __construct($name = false)
86 if ($name && is_string($name)) {
87 $this->setName($name);
94 * @see File::cleanUp()
97 public function __destruct()
103 * deletes file if it is temporary, usually from a moved upload file
106 * @return boolean success
108 public function cleanUp()
110 if ($this->isTemp()) {
111 return $this->delete();
121 * @return boolean success
123 public function delete()
125 return unlink($this->getName());
129 * checks or sets the temp flag for this file
130 * file objects with temp flags are deleted with object destruction
132 * @param boolean $is_temp sets the temp flag
134 * @return boolean File::$_is_temp
137 public function isTemp($is_temp = null)
139 if (null !== $is_temp) {
140 $this->_is_temp
= (bool) $is_temp;
143 return $this->_is_temp
;
149 * @param string $name file name
154 public function setName($name)
156 $this->_name
= trim($name);
162 * @return string|false the binary file content,
163 * or false if no content
167 public function getRawContent()
169 if (null === $this->_content
) {
170 if ($this->isUploaded() && ! $this->checkUploadedFile()) {
174 if (! $this->isReadable()) {
178 if (function_exists('file_get_contents')) {
179 $this->_content
= file_get_contents($this->getName());
180 } elseif ($size = filesize($this->getName())) {
181 $handle = fopen($this->getName(), 'rb');
182 $this->_content
= fread($handle, $size);
187 return $this->_content
;
193 * @return string|false the binary file content as a string,
194 * or false if no content
198 public function getContent()
200 $result = $this->getRawContent();
201 if ($result === false) {
204 return '0x' . bin2hex($result);
208 * Whether file is uploaded.
214 public function isUploaded()
216 return is_uploaded_file($this->getName());
223 * @return string File::$_name
225 public function getName()
231 * Initializes object from uploaded file.
233 * @param string $name name of file uploaded
235 * @return boolean success
238 public function setUploadedFile($name)
240 $this->setName($name);
242 if (! $this->isUploaded()) {
243 $this->setName(null);
244 $this->_error_message
= Message
::error(__('File was not an uploaded file.'));
252 * Loads uploaded file from table change request.
254 * @param string $key the md5 hash of the column name
255 * @param string $rownumber number of row to process
257 * @return boolean success
260 public function setUploadedFromTblChangeRequest($key, $rownumber)
262 if (! isset($_FILES['fields_upload'])
263 ||
empty($_FILES['fields_upload']['name']['multi_edit'][$rownumber][$key])
267 $file = File
::fetchUploadedFromTblChangeRequestMultiple(
268 $_FILES['fields_upload'],
273 // check for file upload errors
274 switch ($file['error']) {
275 // we do not use the PHP constants here cause not all constants
276 // are defined in all versions of PHP - but the correct constants names
277 // are given as comment
278 case 0: //UPLOAD_ERR_OK:
279 return $this->setUploadedFile($file['tmp_name']);
280 case 4: //UPLOAD_ERR_NO_FILE:
282 case 1: //UPLOAD_ERR_INI_SIZE:
283 $this->_error_message
= Message
::error(__(
284 'The uploaded file exceeds the upload_max_filesize directive in '
288 case 2: //UPLOAD_ERR_FORM_SIZE:
289 $this->_error_message
= Message
::error(__(
290 'The uploaded file exceeds the MAX_FILE_SIZE directive that was '
291 . 'specified in the HTML form.'
294 case 3: //UPLOAD_ERR_PARTIAL:
295 $this->_error_message
= Message
::error(__(
296 'The uploaded file was only partially uploaded.'
299 case 6: //UPLOAD_ERR_NO_TMP_DIR:
300 $this->_error_message
= Message
::error(__('Missing a temporary folder.'));
302 case 7: //UPLOAD_ERR_CANT_WRITE:
303 $this->_error_message
= Message
::error(__('Failed to write file to disk.'));
305 case 8: //UPLOAD_ERR_EXTENSION:
306 $this->_error_message
= Message
::error(__('File upload stopped by extension.'));
309 $this->_error_message
= Message
::error(__('Unknown error in file upload.'));
316 * strips some dimension from the multi-dimensional array from $_FILES
319 * $file['name']['multi_edit'][$rownumber][$key] = [value]
320 * $file['type']['multi_edit'][$rownumber][$key] = [value]
321 * $file['size']['multi_edit'][$rownumber][$key] = [value]
322 * $file['tmp_name']['multi_edit'][$rownumber][$key] = [value]
323 * $file['error']['multi_edit'][$rownumber][$key] = [value]
327 * $file['name'] = [value]
328 * $file['type'] = [value]
329 * $file['size'] = [value]
330 * $file['tmp_name'] = [value]
331 * $file['error'] = [value]
334 * @param array $file the array
335 * @param string $rownumber number of row to process
336 * @param string $key key to process
342 public function fetchUploadedFromTblChangeRequestMultiple(
343 $file, $rownumber, $key
346 'name' => $file['name']['multi_edit'][$rownumber][$key],
347 'type' => $file['type']['multi_edit'][$rownumber][$key],
348 'size' => $file['size']['multi_edit'][$rownumber][$key],
349 'tmp_name' => $file['tmp_name']['multi_edit'][$rownumber][$key],
350 'error' => $file['error']['multi_edit'][$rownumber][$key],
357 * sets the name if the file to the one selected in the tbl_change form
359 * @param string $key the md5 hash of the column name
360 * @param string $rownumber number of row to process
362 * @return boolean success
365 public function setSelectedFromTblChangeRequest($key, $rownumber = null)
367 if (! empty($_REQUEST['fields_uploadlocal']['multi_edit'][$rownumber][$key])
368 && is_string($_REQUEST['fields_uploadlocal']['multi_edit'][$rownumber][$key])
370 // ... whether with multiple rows ...
371 return $this->setLocalSelectedFile(
372 $_REQUEST['fields_uploadlocal']['multi_edit'][$rownumber][$key]
380 * Returns possible error message.
383 * @return Message|null error message
385 public function getError()
387 return $this->_error_message
;
391 * Checks whether there was any error.
394 * @return boolean whether an error occurred or not
396 public function isError()
398 return ! is_null($this->_error_message
);
402 * checks the superglobals provided if the tbl_change form is submitted
403 * and uses the submitted/selected file
405 * @param string $key the md5 hash of the column name
406 * @param string $rownumber number of row to process
408 * @return boolean success
411 public function checkTblChangeForm($key, $rownumber)
413 if ($this->setUploadedFromTblChangeRequest($key, $rownumber)) {
415 $this->_error_message
= null;
417 } elseif ($this->setSelectedFromTblChangeRequest($key, $rownumber)) {
419 $this->_error_message
= null;
422 // all failed, whether just no file uploaded/selected or an error
428 * Sets named file to be read from UploadDir.
430 * @param string $name file name
432 * @return boolean success
435 public function setLocalSelectedFile($name)
437 if (empty($GLOBALS['cfg']['UploadDir'])) {
442 Util
::userDir($GLOBALS['cfg']['UploadDir']) . PMA_securePath($name)
444 if (@is_link
($this->getName())) {
445 $this->_error_message
= __('File is a symbolic link');
446 $this->setName(null);
449 if (! $this->isReadable()) {
450 $this->_error_message
= Message
::error(__('File could not be read!'));
451 $this->setName(null);
459 * Checks whether file can be read.
462 * @return boolean whether the file is readable or not
464 public function isReadable()
466 // suppress warnings from being displayed, but not from being logged
467 // any file access outside of open_basedir will issue a warning
468 return @is_readable
($this->getName());
472 * If we are on a server with open_basedir, we must move the file
473 * before opening it. The FAQ 1.11 explains how to create the "./tmp"
474 * directory - if needed
476 * @todo move check of $cfg['TempDir'] into Config?
478 * @return boolean whether uploaded file is fine or not
480 public function checkUploadedFile()
482 if ($this->isReadable()) {
486 $tmp_subdir = ConfigFile
::getDefaultTempDirectory();
487 if (@is_writable
($tmp_subdir)) {
488 // cannot create directory or access, point user to FAQ 1.11
489 $this->_error_message
= Message
::error(__(
490 'Error moving the uploaded file, see [doc@faq1-11]FAQ 1.11[/doc].'
495 $new_file_to_upload = tempnam(
496 realpath($tmp_subdir),
497 basename($this->getName())
500 // suppress warnings from being displayed, but not from being logged
501 // any file access outside of open_basedir will issue a warning
503 $move_uploaded_file_result = move_uploaded_file(
508 if (! $move_uploaded_file_result) {
509 $this->_error_message
= Message
::error(__('Error while moving uploaded file.'));
513 $this->setName($new_file_to_upload);
516 if (! $this->isReadable()) {
517 $this->_error_message
= Message
::error(__('Cannot read uploaded file.'));
525 * Detects what compression the file uses
527 * @todo move file read part into readChunk() or getChunk()
528 * @todo add support for compression plugins
530 * @return string|false false on error, otherwise string MIME type of
531 * compression, none for none
533 protected function detectCompression()
535 // suppress warnings from being displayed, but not from being logged
536 // f.e. any file access outside of open_basedir will issue a warning
538 $file = fopen($this->getName(), 'rb');
542 $this->_error_message
= Message
::error(__('File could not be read!'));
548 * get registered plugins for file compression
550 foreach (PMA_getPlugins($type = 'compression') as $plugin) {
551 if ($plugin['classname']::canHandle($this->getName())) {
552 $this->setCompressionPlugin($plugin);
558 $this->_compression
= Util
::getCompressionMimeType($file);
559 return $this->_compression
;
563 * Sets whether the content should be decompressed before returned
565 * @param boolean $decompress whether to decompress
569 public function setDecompressContent($decompress)
571 $this->_decompress
= (bool) $decompress;
575 * Returns the file handle
577 * @return resource file handle
579 public function getHandle()
581 if (null === $this->_handle
) {
584 return $this->_handle
;
588 * Sets the file handle
590 * @param object $handle file handle
594 public function setHandle($handle)
596 $this->_handle
= $handle;
601 * Sets error message for unsupported compression.
605 public function errorUnsupported()
607 $this->_error_message
= Message
::error(sprintf(
609 'You attempted to load file with unsupported compression (%s). '
610 . 'Either support for it is not implemented or disabled by your '
613 $this->getCompression()
618 * Attempts to open the file.
622 public function open()
624 if (! $this->_decompress
) {
625 $this->_handle
= @fopen
($this->getName(), 'r');
628 switch ($this->getCompression()) {
631 case 'application/bzip2':
632 if ($GLOBALS['cfg']['BZipDump'] && @function_exists
('bzopen')) {
633 $this->_handle
= @bzopen
($this->getName(), 'r');
635 $this->errorUnsupported();
639 case 'application/gzip':
640 if ($GLOBALS['cfg']['GZipDump'] && @function_exists
('gzopen')) {
641 $this->_handle
= @gzopen
($this->getName(), 'r');
643 $this->errorUnsupported();
647 case 'application/zip':
648 if ($GLOBALS['cfg']['ZipDump'] && @function_exists
('zip_open')) {
649 return $this->openZip();
651 $this->errorUnsupported();
655 $this->_handle
= @fopen
($this->getName(), 'r');
658 $this->errorUnsupported();
662 return ($this->_handle
!== false);
666 * Opens file from zip
668 * @param string|null $specific_entry Entry to open
672 public function openZip($specific_entry = null)
674 include_once './libraries/zip_extension.lib.php';
675 $result = PMA_getZipContents($this->getName(), $specific_entry);
676 if (! empty($result['error'])) {
677 $this->_error_message
= Message
::rawError($result['error']);
680 $this->_content
= $result['data'];
686 * Checks whether we've reached end of file
690 public function eof()
692 if (! is_null($this->_handle
)) {
693 return feof($this->_handle
);
695 return $this->_offset
== strlen($this->_content
);
703 public function close()
705 if (! is_null($this->_handle
)) {
706 fclose($this->_handle
);
707 $this->_handle
= null;
709 $this->_content
= '';
716 * Reads data from file
718 * @param int $size Number of bytes to read
722 public function read($size)
724 switch ($this->_compression
) {
725 case 'application/bzip2':
726 return bzread($this->_handle
, $size);
727 case 'application/gzip':
728 return gzread($this->_handle
, $size);
729 case 'application/zip':
730 $result = mb_strcut($this->_content
, $this->_offset
, $size);
731 $this->_offset +
= strlen($result);
735 return fread($this->_handle
, $size);
740 * Returns the character set of the file
742 * @return string character set of the file
744 public function getCharset()
746 return $this->_charset
;
750 * Sets the character set of the file
752 * @param string $charset character set of the file
756 public function setCharset($charset)
758 $this->_charset
= $charset;
762 * Returns compression used by file.
764 * @return string MIME type of compression, none for none
767 public function getCompression()
769 if (null === $this->_compression
) {
770 return $this->detectCompression();
773 return $this->_compression
;
779 * @return integer the offset
781 public function getOffset()
783 return $this->_offset
;
787 * Returns the chunk size
789 * @return integer the chunk size
791 public function getChunkSize()
793 return $this->_chunk_size
;
797 * Sets the chunk size
799 * @param integer $chunk_size the chunk size
803 public function setChunkSize($chunk_size)
805 $this->_chunk_size
= (int) $chunk_size;
809 * Returns the length of the content in the file
811 * @return integer the length of the file content
813 public function getContentLength()
815 return strlen($this->_content
);