2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * file upload functions
8 if (! defined('PHPMYADMIN')) {
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]
23 * @var string the temporary file name
29 * @var string the content
35 * @var string the error message
38 var $_error_message = '';
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 PMA_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 PMA_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 as a string,
163 * or false if no content
167 public function getContent()
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 $this->_content
= fread(fopen($this->getName(), 'rb'), $size);
185 return '0x' . bin2hex($this->_content
);
189 * Whether file is uploaded.
195 public function isUploaded()
197 return is_uploaded_file($this->getName());
204 * @return string PMA_File::$_name
206 public function getName()
212 * Initializes object from uploaded file.
214 * @param string $name name of file uploaded
216 * @return boolean success
219 public function setUploadedFile($name)
221 $this->setName($name);
223 if (! $this->isUploaded()) {
224 $this->setName(null);
225 $this->_error_message
= __('File was not an uploaded file.');
233 * Loads uploaded file from table change request.
235 * @param string $key the md5 hash of the column name
236 * @param string $rownumber number of row to process
238 * @return boolean success
241 public function setUploadedFromTblChangeRequest($key, $rownumber)
243 if (! isset($_FILES['fields_upload'])
244 ||
empty($_FILES['fields_upload']['name']['multi_edit'][$rownumber][$key])
248 $file = PMA_File
::fetchUploadedFromTblChangeRequestMultiple(
249 $_FILES['fields_upload'],
254 // check for file upload errors
255 switch ($file['error']) {
256 // we do not use the PHP constants here cause not all constants
257 // are defined in all versions of PHP - but the correct constants names
258 // are given as comment
259 case 0: //UPLOAD_ERR_OK:
260 return $this->setUploadedFile($file['tmp_name']);
261 case 4: //UPLOAD_ERR_NO_FILE:
263 case 1: //UPLOAD_ERR_INI_SIZE:
264 $this->_error_message
= __(
265 'The uploaded file exceeds the upload_max_filesize directive in '
269 case 2: //UPLOAD_ERR_FORM_SIZE:
270 $this->_error_message
= __(
271 'The uploaded file exceeds the MAX_FILE_SIZE directive that was '
272 . 'specified in the HTML form.'
275 case 3: //UPLOAD_ERR_PARTIAL:
276 $this->_error_message
= __(
277 'The uploaded file was only partially uploaded.'
280 case 6: //UPLOAD_ERR_NO_TMP_DIR:
281 $this->_error_message
= __('Missing a temporary folder.');
283 case 7: //UPLOAD_ERR_CANT_WRITE:
284 $this->_error_message
= __('Failed to write file to disk.');
286 case 8: //UPLOAD_ERR_EXTENSION:
287 $this->_error_message
= __('File upload stopped by extension.');
290 $this->_error_message
= __('Unknown error in file upload.');
297 * strips some dimension from the multi-dimensional array from $_FILES
300 * $file['name']['multi_edit'][$rownumber][$key] = [value]
301 * $file['type']['multi_edit'][$rownumber][$key] = [value]
302 * $file['size']['multi_edit'][$rownumber][$key] = [value]
303 * $file['tmp_name']['multi_edit'][$rownumber][$key] = [value]
304 * $file['error']['multi_edit'][$rownumber][$key] = [value]
308 * $file['name'] = [value]
309 * $file['type'] = [value]
310 * $file['size'] = [value]
311 * $file['tmp_name'] = [value]
312 * $file['error'] = [value]
315 * @param array $file the array
316 * @param string $rownumber number of row to process
317 * @param string $key key to process
323 public function fetchUploadedFromTblChangeRequestMultiple(
324 $file, $rownumber, $key
327 'name' => $file['name']['multi_edit'][$rownumber][$key],
328 'type' => $file['type']['multi_edit'][$rownumber][$key],
329 'size' => $file['size']['multi_edit'][$rownumber][$key],
330 'tmp_name' => $file['tmp_name']['multi_edit'][$rownumber][$key],
331 'error' => $file['error']['multi_edit'][$rownumber][$key],
338 * sets the name if the file to the one selected in the tbl_change form
340 * @param string $key the md5 hash of the column name
341 * @param string $rownumber number of row to process
343 * @return boolean success
346 public function setSelectedFromTblChangeRequest($key, $rownumber = null)
348 if (! empty($_REQUEST['fields_uploadlocal']['multi_edit'][$rownumber][$key])
349 && is_string($_REQUEST['fields_uploadlocal']['multi_edit'][$rownumber][$key])
351 // ... whether with multiple rows ...
352 return $this->setLocalSelectedFile(
353 $_REQUEST['fields_uploadlocal']['multi_edit'][$rownumber][$key]
361 * Returns possible error message.
364 * @return string error message
366 public function getError()
368 return $this->_error_message
;
372 * Checks whether there was any error.
375 * @return boolean whether an error occurred or not
377 public function isError()
379 return ! empty($this->_error_message
);
383 * checks the superglobals provided if the tbl_change form is submitted
384 * and uses the submitted/selected file
386 * @param string $key the md5 hash of the column name
387 * @param string $rownumber number of row to process
389 * @return boolean success
392 public function checkTblChangeForm($key, $rownumber)
394 if ($this->setUploadedFromTblChangeRequest($key, $rownumber)) {
396 $this->_error_message
= '';
398 } elseif ($this->setSelectedFromTblChangeRequest($key, $rownumber)) {
400 $this->_error_message
= '';
403 // all failed, whether just no file uploaded/selected or an error
409 * Sets named file to be read from UploadDir.
411 * @param string $name file name
413 * @return boolean success
416 public function setLocalSelectedFile($name)
418 if (empty($GLOBALS['cfg']['UploadDir'])) {
423 PMA_Util
::userDir($GLOBALS['cfg']['UploadDir']) . PMA_securePath($name)
425 if (! $this->isReadable()) {
426 $this->_error_message
= __('File could not be read!');
427 $this->setName(null);
435 * Checks whether file can be read.
438 * @return boolean whether the file is readable or not
440 public function isReadable()
442 // suppress warnings from being displayed, but not from being logged
443 // any file access outside of open_basedir will issue a warning
445 $is_readable = is_readable($this->getName());
451 * If we are on a server with open_basedir, we must move the file
452 * before opening it. The FAQ 1.11 explains how to create the "./tmp"
453 * directory - if needed
455 * @todo move check of $cfg['TempDir'] into PMA_Config?
457 * @return boolean whether uploaded file is fine or not
459 public function checkUploadedFile()
461 if ($this->isReadable()) {
465 if (empty($GLOBALS['cfg']['TempDir'])
466 ||
! is_writable($GLOBALS['cfg']['TempDir'])
468 // cannot create directory or access, point user to FAQ 1.11
469 $this->_error_message
= __(
470 'Error moving the uploaded file, see [doc@faq1-11]FAQ 1.11[/doc].'
475 $new_file_to_upload = tempnam(
476 realpath($GLOBALS['cfg']['TempDir']),
477 basename($this->getName())
480 // suppress warnings from being displayed, but not from being logged
481 // any file access outside of open_basedir will issue a warning
483 $move_uploaded_file_result = move_uploaded_file(
488 if (! $move_uploaded_file_result) {
489 $this->_error_message
= __('Error while moving uploaded file.');
493 $this->setName($new_file_to_upload);
496 if (! $this->isReadable()) {
497 $this->_error_message
= __('Cannot read uploaded file.');
505 * Detects what compression the file uses
507 * @todo move file read part into readChunk() or getChunk()
508 * @todo add support for compression plugins
510 * @return string|false false on error, otherwise string MIME type of
511 * compression, none for none
513 protected function detectCompression()
515 // suppress warnings from being displayed, but not from being logged
516 // f.e. any file access outside of open_basedir will issue a warning
518 $file = fopen($this->getName(), 'rb');
522 $this->_error_message
= __('File could not be read!');
528 * get registered plugins for file compression
530 foreach (PMA_getPlugins($type = 'compression') as $plugin) {
531 if ($plugin['classname']::canHandle($this->getName())) {
532 $this->setCompressionPlugin($plugin);
538 $this->_compression
= PMA_Util
::getCompressionMimeType($file);
539 return $this->_compression
;
543 * Sets whether the content should be decompressed before returned
545 * @param boolean $decompress whether to decompress
549 public function setDecompressContent($decompress)
551 $this->_decompress
= (bool) $decompress;
555 * Returns the file handle
557 * @return resource file handle
559 public function getHandle()
561 if (null === $this->_handle
) {
564 return $this->_handle
;
568 * Sets the file handle
570 * @param object $handle file handle
574 public function setHandle($handle)
576 $this->_handle
= $handle;
581 * Sets error message for unsupported compression.
585 public function errorUnsupported()
587 $this->_error_message
= sprintf(
589 'You attempted to load file with unsupported compression (%s). '
590 . 'Either support for it is not implemented or disabled by your '
593 $this->getCompression()
598 * Attempts to open the file.
602 public function open()
604 if (! $this->_decompress
) {
605 $this->_handle
= @fopen
($this->getName(), 'r');
608 switch ($this->getCompression()) {
611 case 'application/bzip2':
612 if ($GLOBALS['cfg']['BZipDump'] && @function_exists
('bzopen')) {
613 $this->_handle
= @bzopen
($this->getName(), 'r');
615 $this->errorUnsupported();
619 case 'application/gzip':
620 if ($GLOBALS['cfg']['GZipDump'] && @function_exists
('gzopen')) {
621 $this->_handle
= @gzopen
($this->getName(), 'r');
623 $this->errorUnsupported();
627 case 'application/zip':
628 if ($GLOBALS['cfg']['ZipDump'] && @function_exists
('zip_open')) {
629 include_once './libraries/zip_extension.lib.php';
630 $result = PMA_getZipContents($this->getName());
631 if (! empty($result['error'])) {
632 $this->_error_message
= PMA_Message
::rawError($result['error']);
637 $this->errorUnsupported();
642 $this->_handle
= @fopen
($this->getName(), 'r');
645 $this->errorUnsupported();
653 * Returns the character set of the file
655 * @return string character set of the file
657 public function getCharset()
659 return $this->_charset
;
663 * Sets the character set of the file
665 * @param string $charset character set of the file
669 public function setCharset($charset)
671 $this->_charset
= $charset;
675 * Returns compression used by file.
677 * @return string MIME type of compression, none for none
680 public function getCompression()
682 if (null === $this->_compression
) {
683 return $this->detectCompression();
686 return $this->_compression
;
692 * @return integer the offset
694 public function getOffset()
696 return $this->_offset
;
700 * Returns the chunk size
702 * @return integer the chunk size
704 public function getChunkSize()
706 return $this->_chunk_size
;
710 * Sets the chunk size
712 * @param integer $chunk_size the chunk size
716 public function setChunkSize($chunk_size)
718 $this->_chunk_size
= (int) $chunk_size;
722 * Returns the length of the content in the file
724 * @return integer the length of the file content
726 public function getContentLength()
728 return /*overload*/mb_strlen($this->_content
);
732 * Returns whether the end of the file has been reached
734 * @return boolean whether the end of the file has been reached
736 public function eof()
738 if ($this->getHandle()) {
739 return feof($this->getHandle());
741 return ($this->getOffset() >= $this->getContentLength());