public function doAction()
 {
     //get storage object
     $fs = new FilesStorage();
     $files_job = $fs->getOriginalFilesForJob($this->id_job, $this->id_file, $this->password);
     //take the project ID and creation date, array index zero is good, all id are equals
     $this->id_project = $files_job[0]['id_project'];
     $this->project_date = $files_job[0]['create_date'];
     $output_content = array();
     foreach ($files_job as $file) {
         $id_file = $file['id_file'];
         $zipPathInfo = ZipArchiveExtended::zipPathInfo($file['filename']);
         if (is_array($zipPathInfo)) {
             $output_content[$id_file]['output_filename'] = $zipPathInfo['zipfilename'];
             $output_content[$id_file]['input_filename'] = $fs->getOriginalZipPath($this->project_date, $this->id_project, $zipPathInfo['zipfilename']);
         } else {
             $output_content[$id_file]['output_filename'] = $file['filename'];
             $output_content[$id_file]['input_filename'] = $file['originalFilePath'];
         }
     }
     /*
      * get Unique file zip because there are more than one file in the zip
      * array_unique compares items using a string comparison.
      *
      * From the docs:
      * Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2.
      * In words: when the string representation is the same. The first element will be used.
      */
     $output_content = array_map('unserialize', array_unique(array_map('serialize', $output_content)));
     foreach ($output_content as $key => $iFile) {
         $output_content[$key] = new ZipContentObject($iFile);
     }
     if ($this->download_type == 'all') {
         if (count($output_content) > 1) {
             $this->_filename = $this->fname;
             $pathInfo = FilesStorage::pathinfo_fix($this->fname);
             if ($pathInfo['extension'] != 'zip') {
                 $this->_filename = $pathInfo['basename'] . ".zip";
             }
             $this->content = self::composeZip($output_content);
             //add zip archive content here;
         } elseif (count($output_content) == 1) {
             $this->setContent($output_content);
         }
     } else {
         $this->setContent($output_content);
     }
 }
Ejemplo n.º 2
0
 /**
  * @param $zipFileName
  * @param $internalFiles ZipContentObject[]
  *
  * @return string
  */
 public function reBuildZipContent($zipFileName, $internalFiles)
 {
     $fs = new FilesStorage();
     $zipFile = $fs->getOriginalZipPath($this->jobInfo['create_date'], $this->jobInfo['id_project'], $zipFileName);
     $tmpFName = tempnam(INIT::$TMP_DOWNLOAD . '/' . $this->id_job . '/', "ZIP");
     copy($zipFile, $tmpFName);
     $zip = new ZipArchiveExtended();
     if ($zip->open($tmpFName)) {
         $zip->createTree();
         //rebuild the real name of files in the zip archive
         foreach ($zip->treeList as $filePath) {
             $realPath = str_replace(array(ZipArchiveExtended::INTERNAL_SEPARATOR, FilesStorage::pathinfo_fix($tmpFName, PATHINFO_BASENAME)), array(DIRECTORY_SEPARATOR, ""), $filePath);
             $realPath = ltrim($realPath, "/");
             //remove the tmx from the original zip ( we want not to be exported as preview )
             if (FilesStorage::pathinfo_fix($realPath, PATHINFO_EXTENSION) == 'tmx') {
                 $zip->deleteName($realPath);
                 continue;
             }
             //fix the file names inside the zip file, so we compare with our files
             // and if matches we can substitute them with the converted ones
             //                $fileName_fixed = array_pop( explode( DIRECTORY_SEPARATOR, str_replace( " ", "_", $realPath ) ) );
             foreach ($internalFiles as $index => $internalFile) {
                 //                    $__ourFileName = array_pop( explode( DIRECTORY_SEPARATOR, $internalFile->output_filename ) );
                 $_tmpRealPath = str_replace(array(" ", " "), "_", $realPath);
                 if ($internalFile->output_filename == $_tmpRealPath) {
                     $zip->deleteName($realPath);
                     if (FilesStorage::pathinfo_fix($realPath, PATHINFO_EXTENSION) == 'pdf') {
                         $realPath .= '.docx';
                     }
                     $zip->addFromString($realPath, $internalFile->getContent());
                 }
             }
         }
         $zip->close();
     }
     return $tmpFName;
 }