Exemple #1
0
 /**
  * Extract archive Files
  *
  * @access  public
  * @param   array   $files        $_FILES array
  * @param   string  $dest         Destination directory(include end directory separator)
  * @param   bool    $extractToDir Create separate directory for extracted files
  * @param   bool    $overwrite    Overwrite directory if exist
  * @param   int     $max_size     Max size of file
  * @return  bool    Returns TRUE on success or FALSE on failure
  */
 static function ExtractFiles($files, $dest, $extractToDir = true, $overwrite = true, $max_size = null)
 {
     if (empty($files) || !is_array($files)) {
         return new Jaws_Error(_t('GLOBAL_ERROR_UPLOAD'), __FUNCTION__);
     }
     if (isset($files['name'])) {
         $files = array($files);
     }
     require_once PEAR_PATH . 'File/Archive.php';
     foreach ($files as $key => $file) {
         if (isset($file['error']) && !empty($file['error']) || !isset($file['name'])) {
             return new Jaws_Error(_t('GLOBAL_ERROR_UPLOAD_' . $file['error']), __FUNCTION__);
         }
         if (empty($file['tmp_name'])) {
             continue;
         }
         $ext = strrchr($file['name'], '.');
         $filename = substr($file['name'], 0, -strlen($ext));
         if (false !== stristr($filename, '.tar')) {
             $filename = substr($filename, 0, strrpos($filename, '.'));
             switch ($ext) {
                 case '.gz':
                     $ext = '.tgz';
                     break;
                 case '.bz2':
                 case '.bzip2':
                     $ext = '.tbz';
                     break;
                 default:
                     $ext = '.tar' . $ext;
             }
         }
         $ext = strtolower(substr($ext, 1));
         if (!File_Archive::isKnownExtension($ext)) {
             return new Jaws_Error(_t('GLOBAL_ERROR_UPLOAD_INVALID_FORMAT', $file['name']), __FUNCTION__);
         }
         if ($extractToDir) {
             $dest = $dest . $filename;
         }
         if ($extractToDir && !Jaws_Utils::mkdir($dest)) {
             return new Jaws_Error(_t('GLOBAL_ERROR_FAILED_CREATING_DIR', $dest), __FUNCTION__);
         }
         if (!Jaws_Utils::is_writable($dest)) {
             return new Jaws_Error(_t('GLOBAL_ERROR_FAILED_DIRECTORY_UNWRITABLE', $dest), __FUNCTION__);
         }
         $archive = File_Archive::readArchive($ext, $file['tmp_name']);
         if (PEAR::isError($archive)) {
             return new Jaws_Error($archive->getMessage(), __FUNCTION__);
         }
         $writer = File_Archive::_convertToWriter($dest);
         $result = $archive->extract($writer);
         if (PEAR::isError($result)) {
             return new Jaws_Error($result->getMessage(), __FUNCTION__);
         }
         //@unlink($file['tmp_name']);
     }
     return true;
 }
Exemple #2
0
 /**
  * File_Archive::extract($source, $dest) is equivalent to $source->extract($dest)
  * If $source is a PEAR error, the error will be returned
  * It is thus easier to use this function than $source->extract, since it reduces the number of
  * error checking and doesn't force you to define a variable $source
  *
  * You may use strings as source and dest. In that case the source is automatically
  * converted to a reader using File_Archive::read and the dest is converted to a
  * writer using File_Archive::appender
  * Since PHP doesn't allow to pass literal strings by ref, you will have to use temporary
  * variables.
  * File_Archive::extract($src = 'archive.zip/', $dest = 'dir') will extract the archive to 'dir'
  * It is the same as
  * File_Archive::extract(
  *    File_Archive::read('archive.zip/'),
  *    File_Archive::appender('dir')
  * );
  * You may use any variable in the extract function ($from/$to, $a/$b...).
  *
  * @param File_Archive_Reader $source The source that will be read
  * @param File_Archive_Writer $dest Where to copy $source files
  * @param bool $autoClose if true (default), $dest will be closed after the extraction
  * @param int $bufferSize Size of the buffer to use to move data from the reader to the buffer
  *        If $bufferSize <= 0 (default), the blockSize option is used
  *        You shouldn't need to change that
  * @return null or a PEAR error if an error occured
  */
 function extract(&$sourceToConvert, &$destToConvert, $autoClose = true, $bufferSize = 0)
 {
     $source =& File_Archive::_convertToReader($sourceToConvert);
     if (PEAR::isError($source)) {
         return $source;
     }
     $dest =& File_Archive::_convertToWriter($destToConvert);
     return $source->extract($dest, $autoClose, $bufferSize);
 }