Exemplo n.º 1
1
 public static function isXliff($stringData = null, $fullPathToFile = null)
 {
     self::_reset();
     $info = array();
     if (!empty($stringData) && empty($fullPathToFile)) {
         $stringData = substr($stringData, 0, 1024);
     } elseif (empty($stringData) && !empty($fullPathToFile)) {
         $info = FilesStorage::pathinfo_fix($fullPathToFile);
         $file_pointer = fopen("{$fullPathToFile}", 'r');
         // Checking Requirements (By specs, I know that xliff version is in the first 1KB)
         $stringData = fread($file_pointer, 1024);
         fclose($file_pointer);
     } elseif (!empty($stringData) && !empty($fullPathToFile)) {
         //we want to check extension and content
         $info = FilesStorage::pathinfo_fix($fullPathToFile);
     }
     self::$fileType['info'] = $info;
     //we want to check extension also if file path is specified
     if (!empty($info) && !self::isXliffExtension()) {
         //THIS IS NOT an xliff
         return false;
     }
     //		preg_match( '|<xliff\s.*?version\s?=\s?["\'](.*?)["\'](.*?)>|si', $stringData, $tmp );
     if (!empty($stringData)) {
         return array($stringData);
     }
     return false;
 }
Exemplo n.º 2
0
 /**
  * When Called it perform the controller action to retrieve/manipulate data
  *
  * @return mixed
  */
 public function doAction()
 {
     //check if there was an error in constructor. If so, stop execution.
     if (!empty($this->result['errors'])) {
         $this->result['success'] = false;
         return false;
     }
     $this->result['errors'] = array();
     $this->TMService = new TMSService();
     $this->TMService->setTmKey($this->tm_key);
     try {
         if ($this->exec == "newTM") {
             $this->file = $this->TMService->uploadFile();
             foreach ($this->file as $fileInfo) {
                 if (FilesStorage::pathinfo_fix(strtolower($fileInfo->name), PATHINFO_EXTENSION) !== 'tmx') {
                     throw new Exception("Please upload a TMX.", -8);
                 }
                 $this->TMService->setName($fileInfo->name);
                 $this->TMService->addTmxInMyMemory();
             }
         } else {
             $this->TMService->setName($this->name);
             $status = $this->TMService->tmxUploadStatus();
             $this->result['data'] = $status['data'];
             $this->result['completed'] = $status['completed'];
         }
         $this->result['success'] = true;
     } catch (Exception $e) {
         $this->result['success'] = false;
         $this->result['errors'][] = array("code" => $e->getCode(), "message" => $e->getMessage());
     }
 }
 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);
     }
 }
Exemplo n.º 4
0
 public function doAction()
 {
     $this->result['code'] = 0;
     // No Good, Default
     if (empty($this->file_name)) {
         $this->result['code'] = -1;
         // No Good, Default
         $this->result['errors'][] = array("code" => -1, "message" => "Error: missing file name.");
         return false;
     }
     $ext = FilesStorage::pathinfo_fix($this->file_name, PATHINFO_EXTENSION);
     $conversionHandler = new ConversionHandler();
     $conversionHandler->setFileName($this->file_name);
     $conversionHandler->setSourceLang($this->source_lang);
     $conversionHandler->setTargetLang($this->target_lang);
     $conversionHandler->setSegmentationRule($this->segmentation_rule);
     $conversionHandler->setCookieDir($this->cookieDir);
     $conversionHandler->setIntDir($this->intDir);
     $conversionHandler->setErrDir($this->errDir);
     if ($ext == "zip") {
         if ($this->convertZipFile) {
             $this->handleZip($conversionHandler);
         } else {
             $this->result['errors'][] = array("code" => -2, "message" => "Nested zip files are not allowed");
             return false;
         }
     } else {
         $conversionHandler->doAction();
         $this->result = $conversionHandler->getResult();
     }
     isset($this->result['errors']) ? null : ($this->result['errors'] = array());
     if (count($this->result['errors']) == 0) {
         $this->result['code'] = 1;
     } else {
         $this->result['errors'] = array_values($this->result['errors']);
     }
 }
Exemplo n.º 5
0
 private function prependZipFileName($fName)
 {
     return FilesStorage::pathinfo_fix($this->filename, PATHINFO_BASENAME) . self::INTERNAL_SEPARATOR . $fName;
 }
Exemplo n.º 6
0
 public function moveFromCacheToFileDir($dateHashPath, $lang, $idFile, $newFileName = null)
 {
     list($datePath, $hash) = explode(DIRECTORY_SEPARATOR, $dateHashPath);
     $cacheTree = implode(DIRECTORY_SEPARATOR, $this->_composeCachePath($hash));
     //destination dir
     $fileDir = $this->filesDir . DIRECTORY_SEPARATOR . $datePath . DIRECTORY_SEPARATOR . $idFile;
     $cacheDir = $this->cacheDir . DIRECTORY_SEPARATOR . $cacheTree . "|" . $lang . DIRECTORY_SEPARATOR . "package";
     $res = true;
     //check if doesn't exist
     if (!is_dir($fileDir)) {
         //make files' directory structure
         $res &= mkdir($fileDir, 0755, true);
         $res &= mkdir($fileDir . DIRECTORY_SEPARATOR . "package");
         $res &= mkdir($fileDir . DIRECTORY_SEPARATOR . "package" . DIRECTORY_SEPARATOR . "orig");
         $res &= mkdir($fileDir . DIRECTORY_SEPARATOR . "package" . DIRECTORY_SEPARATOR . "work");
         $res &= mkdir($fileDir . DIRECTORY_SEPARATOR . "orig");
         $res &= mkdir($fileDir . DIRECTORY_SEPARATOR . "xliff");
     }
     //make links from cache to files
     //BUG: this stuff may not work if FILES and CACHES are on different filesystems
     //orig, suppress error because of xliff files have not original one
     $origFilePath = $this->getSingleFileInPath($cacheDir . DIRECTORY_SEPARATOR . "orig");
     $tmpOrigFileName = $origFilePath;
     if (is_file($origFilePath)) {
         /*
          * Force the new filename if it is provided
          */
         if (!empty($newFileName)) {
             $tmpOrigFileName = $newFileName;
         }
         $res &= $this->link($origFilePath, $fileDir . DIRECTORY_SEPARATOR . "orig" . DIRECTORY_SEPARATOR . FilesStorage::basename_fix($tmpOrigFileName));
     }
     //work
     /*
      * Force the new filename if it is provided
      */
     $convertedFilePath = $this->getSingleFileInPath($cacheDir . DIRECTORY_SEPARATOR . "work");
     $tmpConvertedFilePath = $convertedFilePath;
     if (!empty($newFileName)) {
         if (!DetectProprietaryXliff::isXliffExtension(FilesStorage::pathinfo_fix($newFileName))) {
             $convertedExtension = FilesStorage::pathinfo_fix($convertedFilePath, PATHINFO_EXTENSION);
             $tmpConvertedFilePath = $newFileName . "." . $convertedExtension;
         }
     }
     $res &= $this->link($convertedFilePath, $fileDir . DIRECTORY_SEPARATOR . "xliff" . DIRECTORY_SEPARATOR . FilesStorage::basename_fix($tmpConvertedFilePath));
     if (!$res) {
         throw new UnexpectedValueException('Internal Error: Failed to create/copy the file on disk from cache.', -13);
     }
 }
Exemplo n.º 7
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;
 }
Exemplo n.º 8
0
 public function doAction()
 {
     $this->file_name = html_entity_decode($this->file_name, ENT_QUOTES);
     $file_path = $this->intDir . DIRECTORY_SEPARATOR . $this->file_name;
     if (!file_exists($file_path)) {
         $this->result['code'] = -6;
         // No Good, Default
         $this->result['errors'][] = array("code" => -6, "message" => "Error during upload. Please retry.", 'debug' => FilesStorage::basename_fix($this->file_name));
         return -1;
     }
     //XLIFF Conversion management
     //cyclomatic complexity 9999999 ..... but it works, for now.
     try {
         $fileType = DetectProprietaryXliff::getInfo($file_path);
         if (DetectProprietaryXliff::isXliffExtension() || DetectProprietaryXliff::getMemoryFileType()) {
             if (INIT::$CONVERSION_ENABLED) {
                 //conversion enforce
                 if (!INIT::$FORCE_XLIFF_CONVERSION) {
                     //if file is not proprietary AND Enforce is disabled
                     //we take it as is
                     if (!$fileType['proprietary'] || DetectProprietaryXliff::getMemoryFileType()) {
                         $this->result['code'] = 1;
                         // OK for client
                         //This file has to be linked to cache!
                         return 0;
                         //ok don't convert a standard sdlxliff
                     }
                 } else {
                     // if conversion enforce is active
                     // we force all xliff files but not files produced by
                     // SDL Studio or by the MateCAT converters, because we
                     // can handle them
                     if ($fileType['proprietary_short_name'] == 'matecat_converter' || $fileType['proprietary_short_name'] == 'trados' || DetectProprietaryXliff::getMemoryFileType()) {
                         $this->result['code'] = 1;
                         // OK for client
                         $this->result['errors'][] = array("code" => 0, "message" => "OK");
                         return 0;
                         //ok don't convert a standard sdlxliff
                     }
                 }
             } elseif ($fileType['proprietary']) {
                 unlink($file_path);
                 $this->result['code'] = -7;
                 // No Good, Default
                 $this->result['errors'][] = array("code" => -7, "message" => 'Matecat Open-Source does not support ' . ucwords($fileType['proprietary_name']) . '. Use MatecatPro.', 'debug' => FilesStorage::basename_fix($this->file_name));
                 return -1;
             } elseif (!$fileType['proprietary']) {
                 $this->result['code'] = 1;
                 // OK for client
                 $this->result['errors'][] = array("code" => 0, "message" => "OK");
                 return 0;
                 //ok don't convert a standard sdlxliff
             }
         }
     } catch (Exception $e) {
         //try catch not used because of exception no more raised
         $this->result['code'] = -8;
         // No Good, Default
         $this->result['errors'][] = array("code" => -8, "message" => $e->getMessage());
         Log::doLog($e->getMessage());
         return -1;
     }
     //compute hash to locate the file in the cache
     $sha1 = sha1_file($file_path);
     //initialize path variable
     $cachedXliffPath = false;
     //get storage object
     $fs = new FilesStorage();
     //TODO: REMOVE SET ENVIRONMENT FOR LEGACY CONVERSION INSTANCES
     if (INIT::$LEGACY_CONVERSION !== false) {
         INIT::$SAVE_SHASUM_FOR_FILES_LOADED = false;
     }
     //if already present in database cache get the converted without convert it again
     if (INIT::$SAVE_SHASUM_FOR_FILES_LOADED) {
         //move the file in the right directory from the packages to the file dir
         $cachedXliffPath = $fs->getXliffFromCache($sha1, $this->source_lang);
         if (!$cachedXliffPath) {
             Log::doLog("Failed to fetch xliff for {$sha1} from disk cache (is file there?)");
         }
     }
     //if invalid or no cached version
     if (!isset($cachedXliffPath) or empty($cachedXliffPath)) {
         //we have to convert it
         // By default, use always the new converters...
         $converterVersion = Constants_ConvertersVersions::LATEST;
         if ($this->segmentation_rule !== null) {
             // ...but new converters don't support custom segmentation rules.
             // if $this->segmentation_rule is set use the old ones.
             $converterVersion = Constants_ConvertersVersions::LEGACY;
         }
         //TODO: REMOVE SET ENVIRONMENT FOR LEGACY CONVERSION INSTANCES
         if (INIT::$LEGACY_CONVERSION !== false) {
             $converterVersion = Constants_ConvertersVersions::LEGACY;
         }
         $converter = new FileFormatConverter($converterVersion);
         if (strpos($this->target_lang, ',') !== false) {
             $single_language = explode(',', $this->target_lang);
             $single_language = $single_language[0];
         } else {
             $single_language = $this->target_lang;
         }
         $convertResult = $converter->convertToSdlxliff($file_path, $this->source_lang, $single_language, false, $this->segmentation_rule);
         if ($convertResult['isSuccess'] == 1) {
             /* try to back convert the file */
             $output_content = array();
             $output_content['out_xliff_name'] = $file_path . '.out.sdlxliff';
             $output_content['source'] = $this->source_lang;
             $output_content['target'] = $single_language;
             $output_content['content'] = $convertResult['xliffContent'];
             $output_content['filename'] = $this->file_name;
             $back_convertResult = $converter->convertToOriginal($output_content);
             /* try to back convert the file */
             if ($back_convertResult['isSuccess'] == false) {
                 //custom error message passed directly to javascript client and displayed as is
                 $convertResult['errorMessage'] = "Error: there is a problem with this file, it cannot be converted back to the original one.";
                 $this->result['code'] = -110;
                 $this->result['errors'][] = array("code" => -110, "message" => $convertResult['errorMessage'], 'debug' => FilesStorage::basename_fix($this->file_name));
                 return false;
             }
             //store converted content on a temporary path on disk (and off RAM)
             $cachedXliffPath = tempnam("/tmp", "MAT_XLF");
             file_put_contents($cachedXliffPath, $convertResult['xliffContent']);
             unset($convertResult['xliffContent']);
             /*
               store the converted file in the cache
               put a reference in the upload dir to the cache dir, so that from the UUID we can reach the converted file in the cache
               (this is independent by the "save xliff for caching" options, since we always end up storing original and xliff on disk)
             */
             //save in cache
             $res_insert = $fs->makeCachePackage($sha1, $this->source_lang, $file_path, $cachedXliffPath);
             if (!$res_insert) {
                 //custom error message passed directly to javascript client and displayed as is
                 $convertResult['errorMessage'] = "Error: File upload failed because you have MateCat running in multiple tabs. Please close all other MateCat tabs in your browser.";
                 $this->result['code'] = -103;
                 $this->result['errors'][] = array("code" => -103, "message" => $convertResult['errorMessage'], 'debug' => FilesStorage::basename_fix($this->file_name));
                 unset($cachedXliffPath);
                 return false;
             }
         } else {
             $file = FilesStorage::pathinfo_fix($this->file_name);
             switch ($file['extension']) {
                 case 'docx':
                     $defaultError = "Importing error. Try opening and saving the document with a new name. If this does not work, try converting to DOC.";
                     break;
                 case 'doc':
                 case 'rtf':
                     $defaultError = "Importing error. Try opening and saving the document with a new name. If this does not work, try converting to DOCX.";
                     break;
                 case 'inx':
                     $defaultError = "Importing Error. Try to commit changes in InDesign before importing.";
                     break;
                 case 'idml':
                     $defaultError = "Importing Error. MateCat does not support this version of InDesign, try converting it to a previous one.";
                     break;
                 default:
                     $defaultError = "Importing error. Try opening and saving the document with a new name.";
                     break;
             }
             if (stripos($convertResult['errorMessage'], "failed to create SDLXLIFF.") !== false || stripos($convertResult['errorMessage'], "COM target does not implement IDispatch") !== false) {
                 $convertResult['errorMessage'] = "Error: failed importing file.";
             } elseif (stripos($convertResult['errorMessage'], "Unable to open Excel file - it may be password protected") !== false) {
                 $convertResult['errorMessage'] = $convertResult['errorMessage'] . " Try to remove protection using the Unprotect Sheet command on Windows Excel.";
             } elseif (stripos($convertResult['errorMessage'], "The document contains unaccepted changes") !== false) {
                 $convertResult['errorMessage'] = "The document contains track changes. Accept all changes before uploading it.";
             } elseif (stripos($convertResult['errorMessage'], "Error: Could not find file") !== false || stripos($convertResult['errorMessage'], "tw4winMark") !== false) {
                 $convertResult['errorMessage'] = $defaultError;
             } elseif (stripos($convertResult['errorMessage'], "Attempted to read or write protected memory") !== false) {
                 $convertResult['errorMessage'] = $defaultError;
             } elseif (stripos($convertResult['errorMessage'], "The document was created in Microsoft Word 97 or earlier")) {
                 $convertResult['errorMessage'] = $defaultError;
             } elseif ($file['extension'] == 'csv' && empty($convertResult['errorMessage'])) {
                 $convertResult['errorMessage'] = "This CSV file is not eligible to be imported due internal wrong format. Try to convert in TXT using UTF8 encoding";
             } elseif (empty($convertResult['errorMessage'])) {
                 $convertResult['errorMessage'] = "Failed to convert file. Internal error. Please Try again.";
             } elseif (stripos($convertResult['errorMessage'], "DocumentFormat.OpenXml.dll") !== false) {
                 //this error is triggered on DOCX when converter's parser can't decode some regions of the file
                 $convertResult['errorMessage'] = "Conversion error. Try opening and saving the document with a new name. If this does not work, try converting to DOC.";
             } elseif ($file['extension'] == 'idml') {
                 $convertResult['errorMessage'] = $defaultError;
             } elseif (stripos($convertResult['errorMessage'], "Error: The source language of the file") !== false) {
                 //Error: The source language of the file (English (United States)) is different from the project source language.
                 //we take the error, is good
             } else {
                 $convertResult['errorMessage'] = "Import error. Try converting it to a compatible file format (e.g. doc > docx, xlsx > xls)";
             }
             //custom error message passed directly to javascript client and displayed as is
             $this->result['code'] = -100;
             $this->result['errors'][] = array("code" => -100, "message" => $convertResult['errorMessage'], "debug" => $file['basename']);
         }
     }
     //if everything went well and we've obtained a path toward a valid package (original+xliff), either via cache or conversion
     if (isset($cachedXliffPath) and !empty($cachedXliffPath)) {
         //FILE Found in cache, destroy the already present shasum for other languages ( if user swapped languages )
         $uploadDir = INIT::$UPLOAD_REPOSITORY . DIRECTORY_SEPARATOR . $this->cookieDir;
         $fs->deleteHashFromUploadDir($uploadDir, $sha1 . "|" . $this->source_lang);
         //put reference to cache in upload dir to link cache to session
         $fs->linkSessionToCache($sha1, $this->source_lang, $this->cookieDir, FilesStorage::basename_fix($file_path));
         //a usable package is available, give positive feedback
         $this->result['code'] = 1;
     }
     return 0;
 }
Exemplo n.º 9
0
 public function doAction()
 {
     //check for errors. If there are, stop execution and return errors.
     if (count(@$this->result['errors'])) {
         return false;
     }
     $arFiles = explode('@@SEP@@', html_entity_decode($this->file_name, ENT_QUOTES, 'UTF-8'));
     $default_project_name = $arFiles[0];
     if (count($arFiles) > 1) {
         $default_project_name = "MATECAT_PROJ-" . date("Ymdhi");
     }
     if (empty($this->project_name)) {
         $this->project_name = $default_project_name;
     }
     $sourceLangHistory = $_COOKIE["sourceLang"];
     $targetLangHistory = $_COOKIE["targetLang"];
     // SET SOURCE COOKIE
     if ($sourceLangHistory == '_EMPTY_') {
         $sourceLangHistory = "";
     }
     $sourceLangAr = explode('||', urldecode($sourceLangHistory));
     if (($key = array_search($this->source_language, $sourceLangAr)) !== false) {
         unset($sourceLangAr[$key]);
     }
     array_unshift($sourceLangAr, $this->source_language);
     if ($sourceLangAr == '_EMPTY_') {
         $sourceLangAr = "";
     }
     $newCookieVal = "";
     $sourceLangAr = array_slice($sourceLangAr, 0, 3);
     $sourceLangAr = array_reverse($sourceLangAr);
     foreach ($sourceLangAr as $key => $link) {
         if ($sourceLangAr[$key] == '') {
             unset($sourceLangAr[$key]);
         }
     }
     foreach ($sourceLangAr as $lang) {
         if ($lang != "") {
             $newCookieVal = $lang . "||" . $newCookieVal;
         }
     }
     setcookie("sourceLang", $newCookieVal, time() + 86400 * 365);
     // SET TARGET COOKIE
     if ($targetLangHistory == '_EMPTY_') {
         $targetLangHistory = "";
     }
     $targetLangAr = explode('||', urldecode($targetLangHistory));
     if (($key = array_search($this->target_language, $targetLangAr)) !== false) {
         unset($targetLangAr[$key]);
     }
     array_unshift($targetLangAr, $this->target_language);
     if ($targetLangAr == '_EMPTY_') {
         $targetLangAr = "";
     }
     $newCookieVal = "";
     $targetLangAr = array_slice($targetLangAr, 0, 3);
     $targetLangAr = array_reverse($targetLangAr);
     foreach ($targetLangAr as $key => $link) {
         if ($targetLangAr[$key] == '') {
             unset($targetLangAr[$key]);
         }
     }
     foreach ($targetLangAr as $lang) {
         if ($lang != "") {
             $newCookieVal = $lang . "||" . $newCookieVal;
         }
     }
     setcookie("targetLang", $newCookieVal, time() + 86400 * 365);
     //search in fileNames if there's a zip file. If it's present, get filenames and add the instead of the zip file.
     $uploadDir = INIT::$UPLOAD_REPOSITORY . DIRECTORY_SEPARATOR . $_COOKIE['upload_session'];
     $newArFiles = array();
     foreach ($arFiles as $__fName) {
         if ('zip' == FilesStorage::pathinfo_fix($__fName, PATHINFO_EXTENSION)) {
             $fs = new FilesStorage();
             $fs->cacheZipArchive(sha1_file($uploadDir . DIRECTORY_SEPARATOR . $__fName), $uploadDir . DIRECTORY_SEPARATOR . $__fName);
             $linkFiles = scandir($uploadDir);
             //fetch cache links, created by converter, from upload directory
             foreach ($linkFiles as $storedFileName) {
                 //check if file begins with the name of the zip file.
                 // If so, then it was stored in the zip file.
                 if (strpos($storedFileName, $__fName) !== false && substr($storedFileName, 0, strlen($__fName)) == $__fName) {
                     //add file name to the files array
                     $newArFiles[] = $storedFileName;
                 }
             }
         } else {
             //this file was not in a zip. Add it normally
             if (file_exists($uploadDir . DIRECTORY_SEPARATOR . $__fName)) {
                 $newArFiles[] = $__fName;
             }
         }
     }
     $arFiles = $newArFiles;
     $projectManager = new ProjectManager();
     $projectStructure = $projectManager->getProjectStructure();
     $projectStructure['project_name'] = $this->project_name;
     $projectStructure['result'] = $this->result;
     $projectStructure['private_tm_key'] = $this->private_tm_key;
     $projectStructure['private_tm_user'] = $this->private_tm_user;
     $projectStructure['private_tm_pass'] = $this->private_tm_pass;
     $projectStructure['uploadToken'] = $_COOKIE['upload_session'];
     $projectStructure['array_files'] = $arFiles;
     //list of file name
     $projectStructure['source_language'] = $this->source_language;
     $projectStructure['target_language'] = explode(',', $this->target_language);
     $projectStructure['job_subject'] = $this->job_subject;
     $projectStructure['mt_engine'] = $this->mt_engine;
     $projectStructure['tms_engine'] = $this->tms_engine;
     $projectStructure['status'] = Constants_ProjectStatus::STATUS_NOT_READY_FOR_ANALYSIS;
     $projectStructure['lang_detect_files'] = $this->lang_detect_files;
     $projectStructure['skip_lang_validation'] = true;
     $projectStructure['pretranslate_100'] = $this->pretranslate_100;
     if (INIT::$DQF_ENABLED) {
         $projectStructure['dqf_key'] = $this->dqf_key;
     }
     //if user is logged in, set the uid and the userIsLogged flag
     $this->checkLogin(false);
     if ($this->userIsLogged) {
         $projectStructure['userIsLogged'] = true;
         $projectStructure['uid'] = $this->uid;
     }
     $projectManager = new ProjectManager($projectStructure);
     $projectManager->createProject();
     $this->result = $projectStructure['result'];
 }
Exemplo n.º 10
0
 public function multiConvertToOriginal($xliffVector_array, $chosen_by_user_machine = false)
 {
     if (empty($xliffVector_array)) {
         return array();
     }
     $multiCurlObj = new MultiCurlHandler();
     $conversionObjects = array();
     $temporary_files = array();
     //iterate files.
     //For each file prepare a curl resource
     foreach ($xliffVector_array as $id_file => $xliffVector) {
         $xliffContent = $xliffVector['document_content'];
         //assign converter
         if (!$chosen_by_user_machine) {
             $this->ip = $this->pickRandConverter();
             $storage = $this->getValidStorage();
             //add replace/regexp pattern because we have more than 1 replacement
             //http://stackoverflow.com/questions/2222643/php-preg-replace
             $xliffContent = self::replacedAddress($storage, $xliffContent);
         } else {
             $this->ip = $chosen_by_user_machine;
         }
         $url = "{$this->ip}:{$this->port}/{$this->fromXliffFunction}";
         $uid_ext = $this->extractUidandExt($xliffContent);
         $data['uid'] = $uid_ext[0];
         //get random name for temporary location
         $tmp_name = tempnam("/tmp", "MAT_BW");
         $temporary_files[] = $tmp_name;
         //write encoded file to temporary location
         $fileSize = file_put_contents($tmp_name, $xliffContent);
         $data['xliffContent'] = "@{$tmp_name}";
         $xliffName = $xliffVector['out_xliff_name'];
         //prepare conversion object
         $this->conversionObject->ip_machine = $this->ip;
         $this->conversionObject->ip_client = Utils::getRealIpAddr();
         $this->conversionObject->path_name = $xliffVector['out_xliff_name'];
         $this->conversionObject->file_name = FilesStorage::pathinfo_fix($xliffName, PATHINFO_BASENAME);
         $this->conversionObject->direction = 'bw';
         $this->conversionObject->src_lang = $this->lang_handler->getLangRegionCode($xliffVector['source']);
         $this->conversionObject->trg_lang = $this->lang_handler->getLangRegionCode($xliffVector['target']);
         $this->conversionObject->file_size = $fileSize;
         $conversionObjects[$id_file] = clone $this->conversionObject;
         $options = array(CURLOPT_URL => $url, CURLOPT_HEADER => 0, CURLOPT_USERAGENT => INIT::MATECAT_USER_AGENT . INIT::$BUILD_NUMBER, CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $data, CURLOPT_HTTPHEADER => $this->opt['httpheader']);
         $multiCurlObj->createResource($url, $options, $id_file);
     }
     //Perform curl
     Log::doLog("multicurl_start");
     $multiCurlObj->multiExec();
     Log::doLog("multicurl_end");
     $multiInfo = $multiCurlObj->getAllInfo();
     $multiResponses = $multiCurlObj->getAllContents();
     //decode response and return the result
     foreach ($multiResponses as $hash => $json) {
         $multiResponses[$hash] = json_decode($json, true);
         $conversionObjects[$hash]->conversion_time = $multiInfo[$hash]['curlinfo_total_time'];
         $multiResponses[$hash] = $this->__parseOutput($multiResponses[$hash], $conversionObjects[$hash]);
     }
     //remove temporary files
     foreach ($temporary_files as $temp_name) {
         unlink($temp_name);
     }
     return $multiResponses;
 }
Exemplo n.º 11
0
 public function delete()
 {
     /*
      * BUG FIXED: UTF16 / UTF8 File name conversion related
      *
      * $file_name = isset($_REQUEST['file']) ? basename(stripslashes($_REQUEST['file'])) : null;
      *
      * ----> basename is NOT UTF8 compliant
      */
     $file_name = null;
     if (isset($_REQUEST['file'])) {
         $raw_file = explode(DIRECTORY_SEPARATOR, $_REQUEST['file']);
         $file_name = array_pop($raw_file);
     }
     $file_info = FilesStorage::pathinfo_fix($file_name);
     //if it's a zip file, delete it and all its contained files.
     if ($file_info['extension'] == 'zip') {
         $success = $this->zipFileDelete($file_name);
     } elseif (preg_match('#^[^\\.]*\\.zip/#', $_REQUEST['file'])) {
         $file_name = ZipArchiveExtended::getInternalFileName($_REQUEST['file']);
         $success = $this->zipInternalFileDelete($file_name);
     } else {
         $success = $this->normalFileDelete($file_name);
     }
     header('Content-type: application/json');
     echo json_encode($success);
 }
Exemplo n.º 12
0
 /**
  * @throws Exception
  */
 protected function _loopForTMXLoadStatus()
 {
     //TMX Management
     /****************/
     //loop again through files to check to check for TMX loading
     foreach ($this->projectStructure['array_files'] as $kname => $fileName) {
         //if TMX,
         if ('tmx' == FilesStorage::pathinfo_fix($fileName, PATHINFO_EXTENSION)) {
             $this->tmxServiceWrapper->setName($fileName);
             $result = array();
             //is the TM loaded?
             //wait until current TMX is loaded
             while (true) {
                 try {
                     $result = $this->tmxServiceWrapper->tmxUploadStatus();
                     if ($result['completed']) {
                         //"$fileName" has been loaded into MyMemory"
                         //exit the loop
                         break;
                     }
                     //"waiting for "$fileName" to be loaded into MyMemory"
                     sleep(3);
                 } catch (Exception $e) {
                     $this->projectStructure['result']['errors'][] = array("code" => $e->getCode(), "message" => $e->getMessage());
                     Log::doLog($e->getMessage() . "\n" . $e->getTraceAsString());
                     //exit project creation
                     throw new Exception($e);
                 }
             }
             //once the language is loaded, check if language is compliant (unless something useful has already been found)
             if (1 == $this->checkTMX) {
                 //get localized target languages of TM (in case it's a multilingual TM)
                 $tmTargets = explode(';', $result['data']['target_lang']);
                 //indicates if something has been found for current memory
                 $found = false;
                 //compare localized target languages array (in case it's a multilingual project) to the TM supplied
                 //if nothing matches, then the TM supplied can't have matches for this project
                 //create an empty var and add the source language too
                 $project_languages = array_merge((array) $this->projectStructure['target_language'], (array) $this->projectStructure['source_language']);
                 foreach ($project_languages as $projectTarget) {
                     if (in_array($this->langService->getLocalizedName($projectTarget), $tmTargets)) {
                         $found = true;
                         break;
                     }
                 }
                 //if this TM matches the project lagpair and something has been found
                 if ($found and $result['data']['source_lang'] == $this->langService->getLocalizedName($this->projectStructure['source_language'])) {
                     //the TMX is good to go
                     $this->checkTMX = 0;
                 } elseif ($found and $result['data']['target_lang'] == $this->langService->getLocalizedName($this->projectStructure['source_language'])) {
                     /*
                      * This means that the TMX has a srclang as specification in the header. Warn the user.
                      * Ex:
                      * <header creationtool="SDL Language Platform"
                      *      creationtoolversion="8.0"
                      *      datatype="rtf"
                      *      segtype="sentence"
                      *      adminlang="DE-DE"
                      *      srclang="DE-DE" />
                      */
                     $this->projectStructure['result']['errors'][] = array("code" => -16, "message" => "The TMX you provided explicitly specifies {$result['data']['source_lang']} as source language. Check that the specified language source in the TMX file match the language source of your project or remove that specification in TMX file.");
                     $this->checkTMX = 0;
                     Log::doLog($this->projectStructure['result']);
                 }
             }
             unset($this->projectStructure['array_files'][$kname]);
         }
     }
     if (1 == $this->checkTMX) {
         //this means that noone of uploaded TMX were usable for this project. Warn the user.
         $this->projectStructure['result']['errors'][] = array("code" => -16, "message" => "The TMX did not contain any usable segment. Check that the languages in the TMX file match the languages of your project.");
         Log::doLog($this->projectStructure['result']);
         throw new Exception("The TMX did not contain any usable segment. Check that the languages in the TMX file match the languages of your project.");
     }
 }
Exemplo n.º 13
0
 protected static function sanitizeFileExtension($filename)
 {
     $pathinfo = FilesStorage::pathinfo_fix($filename);
     switch (strtolower($pathinfo['extension'])) {
         case 'pdf':
         case 'bmp':
         case 'png':
         case 'gif':
         case 'jpg':
         case 'jpeg':
         case 'tiff':
         case 'tif':
             $filename = $pathinfo['basename'] . ".docx";
             break;
     }
     return $filename;
 }
Exemplo n.º 14
0
 public function doAction()
 {
     if (!$this->validateAuthHeader()) {
         header('HTTP/1.0 401 Unauthorized');
         $this->api_output['message'] = 'Authentication failed';
         return -1;
     }
     if (@count($this->api_output['debug']) > 0) {
         return;
     }
     $uploadFile = new Upload();
     try {
         $stdResult = $uploadFile->uploadFiles($_FILES);
     } catch (Exception $e) {
         $stdResult = array();
         $this->result = array('errors' => array(array("code" => -1, "message" => $e->getMessage())));
         $this->api_output['message'] = $e->getMessage();
     }
     $arFiles = array();
     foreach ($stdResult as $input_name => $input_value) {
         $arFiles[] = $input_value->name;
     }
     //if fileupload was failed this index ( 0 = does not exists )
     $default_project_name = @$arFiles[0];
     if (count($arFiles) > 1) {
         $default_project_name = "MATECAT_PROJ-" . date("Ymdhi");
     }
     if (empty($this->project_name)) {
         $this->project_name = $default_project_name;
         //'NO_NAME'.$this->create_project_name();
     }
     if (empty($this->source_lang)) {
         $this->api_output['message'] = "Missing source language.";
         $this->result['errors'][] = array("code" => -3, "message" => "Missing source language.");
     }
     if (empty($this->target_lang)) {
         $this->api_output['message'] = "Missing target language.";
         $this->result['errors'][] = array("code" => -4, "message" => "Missing target language.");
     }
     //ONE OR MORE ERRORS OCCURRED : EXITING
     //for now we sent to api output only the LAST error message, but we log all
     if (!empty($this->result['errors'])) {
         $msg = "Error \n\n " . var_export(array_merge($this->result, $_POST), true);
         Log::doLog($msg);
         Utils::sendErrMailReport($msg);
         return -1;
         //exit code
     }
     $cookieDir = $uploadFile->getDirUploadToken();
     $intDir = INIT::$UPLOAD_REPOSITORY . DIRECTORY_SEPARATOR . $cookieDir;
     $errDir = INIT::$STORAGE_DIR . DIRECTORY_SEPARATOR . 'conversion_errors' . DIRECTORY_SEPARATOR . $cookieDir;
     $response_stack = array();
     foreach ($arFiles as $file_name) {
         $ext = FilesStorage::pathinfo_fix($file_name, PATHINFO_EXTENSION);
         $conversionHandler = new ConversionHandler();
         $conversionHandler->setFileName($file_name);
         $conversionHandler->setSourceLang($this->source_lang);
         $conversionHandler->setTargetLang($this->target_lang);
         $conversionHandler->setSegmentationRule($this->seg_rule);
         $conversionHandler->setCookieDir($cookieDir);
         $conversionHandler->setIntDir($intDir);
         $conversionHandler->setErrDir($errDir);
         $status = array();
         if ($ext == "zip") {
             // this makes the conversionhandler accumulate eventual errors on files and continue
             $conversionHandler->setStopOnFileException(false);
             $fileObjects = $conversionHandler->extractZipFile();
             //call convertFileWrapper and start conversions for each file
             if ($conversionHandler->uploadError) {
                 $fileErrors = $conversionHandler->getUploadedFiles();
                 foreach ($fileErrors as $fileError) {
                     if (count($fileError->error) == 0) {
                         continue;
                     }
                     $brokenFileName = ZipArchiveExtended::getFileName($fileError->name);
                     /*
                      * TODO
                      * return error code is 2 because
                      *      <=0 is for errors
                      *      1   is OK
                      *
                      * In this case, we raise warnings, hence the return code must be a new code
                      */
                     $this->result['code'] = 2;
                     $this->result['errors'][$brokenFileName] = array('code' => $fileError->error['code'], 'message' => $fileError->error['message'], 'debug' => $brokenFileName);
                 }
             }
             $realFileObjectInfo = $fileObjects;
             $realFileObjectNames = array_map(array('ZipArchiveExtended', 'getFileName'), $fileObjects);
             foreach ($realFileObjectNames as $i => &$fileObject) {
                 $__fileName = $fileObject;
                 $__realFileName = $realFileObjectInfo[$i];
                 $filesize = filesize($intDir . DIRECTORY_SEPARATOR . $__realFileName);
                 $fileObject = array('name' => $__fileName, 'size' => $filesize);
                 $realFileObjectInfo[$i] = $fileObject;
             }
             $this->result['data'][$file_name] = json_encode($realFileObjectNames);
             $stdFileObjects = array();
             if ($fileObjects !== null) {
                 foreach ($fileObjects as $fName) {
                     if (isset($fileErrors) && isset($fileErrors->{$fName}) && !empty($fileErrors->{$fName}->error)) {
                         continue;
                     }
                     $newStdFile = new stdClass();
                     $newStdFile->name = $fName;
                     $stdFileObjects[] = $newStdFile;
                 }
             } else {
                 $errors = $conversionHandler->getResult();
                 $errors = array_map(array('Upload', 'formatExceptionMessage'), $errors['errors']);
                 $this->result['errors'] = array_merge($this->result['errors'], $errors);
                 $this->api_output['message'] = "Zip Error";
                 $this->api_output['debug'] = $this->result['errors'];
                 return false;
             }
             /* Do conversions here */
             $converter = new ConvertFileWrapper($stdFileObjects, false);
             $converter->intDir = $intDir;
             $converter->errDir = $errDir;
             $converter->cookieDir = $cookieDir;
             $converter->source_lang = $this->source_lang;
             $converter->target_lang = $this->target_lang;
             $converter->doAction();
             $status = $errors = $converter->checkResult();
             if (count($errors) > 0) {
                 //                    $this->result[ 'errors' ] = array_merge( $this->result[ 'errors' ], $errors );
                 $this->result['code'] = 2;
                 foreach ($errors as $__err) {
                     $brokenFileName = ZipArchiveExtended::getFileName($__err['debug']);
                     if (!isset($this->result['errors'][$brokenFileName])) {
                         $this->result['errors'][$brokenFileName] = array('code' => $__err['code'], 'message' => $__err['message'], 'debug' => $brokenFileName);
                     }
                 }
             }
         } else {
             $conversionHandler->doAction();
             $this->result = $conversionHandler->getResult();
             if ($this->result['code'] > 0) {
                 $this->result = array();
             }
         }
     }
     $status = array_values($status);
     if (!empty($status)) {
         $this->api_output['message'] = 'Project Conversion Failure';
         $this->api_output['debug'] = $status;
         $this->result['errors'] = $status;
         Log::doLog($status);
         return -1;
     }
     /* Do conversions here */
     if (isset($this->result['data']) && !empty($this->result['data'])) {
         foreach ($this->result['data'] as $zipFileName => $zipFiles) {
             $zipFiles = json_decode($zipFiles, true);
             $fileNames = Utils::array_column($zipFiles, 'name');
             $arFiles = array_merge($arFiles, $fileNames);
         }
     }
     $newArFiles = array();
     $linkFiles = scandir($intDir);
     foreach ($arFiles as $__fName) {
         if ('zip' == FilesStorage::pathinfo_fix($__fName, PATHINFO_EXTENSION)) {
             $fs = new FilesStorage();
             $fs->cacheZipArchive(sha1_file($intDir . DIRECTORY_SEPARATOR . $__fName), $intDir . DIRECTORY_SEPARATOR . $__fName);
             $linkFiles = scandir($intDir);
             //fetch cache links, created by converter, from upload directory
             foreach ($linkFiles as $storedFileName) {
                 //check if file begins with the name of the zip file.
                 // If so, then it was stored in the zip file.
                 if (strpos($storedFileName, $__fName) !== false && substr($storedFileName, 0, strlen($__fName)) == $__fName) {
                     //add file name to the files array
                     $newArFiles[] = $storedFileName;
                 }
             }
         } else {
             //this file was not in a zip. Add it normally
             if (file_exists($intDir . DIRECTORY_SEPARATOR . $__fName)) {
                 $newArFiles[] = $__fName;
             }
         }
     }
     $arFiles = $newArFiles;
     $projectManager = new ProjectManager();
     $projectStructure = $projectManager->getProjectStructure();
     $projectStructure['project_name'] = $this->project_name;
     $projectStructure['job_subject'] = $this->subject;
     $projectStructure['result'] = $this->result;
     $projectStructure['private_tm_key'] = $this->private_tm_key;
     $projectStructure['private_tm_user'] = $this->private_tm_user;
     $projectStructure['private_tm_pass'] = $this->private_tm_pass;
     $projectStructure['uploadToken'] = $uploadFile->getDirUploadToken();
     $projectStructure['array_files'] = $arFiles;
     //list of file name
     $projectStructure['source_language'] = $this->source_lang;
     $projectStructure['target_language'] = explode(',', $this->target_lang);
     $projectStructure['mt_engine'] = $this->mt_engine;
     $projectStructure['tms_engine'] = $this->tms_engine;
     $projectStructure['status'] = Constants_ProjectStatus::STATUS_NOT_READY_FOR_ANALYSIS;
     $projectStructure['skip_lang_validation'] = true;
     $projectStructure['owner'] = $this->owner;
     if ($this->current_user != null) {
         $projectStructure['id_customer'] = $this->current_user->getEmail();
     }
     if ($this->current_user != null) {
         $projectStructure['id_customer'] = $this->current_user->getEmail();
     }
     $projectManager = new ProjectManager($projectStructure);
     $projectManager->createProject();
     $this->result = $projectStructure['result'];
     if (!empty($projectStructure['result']['errors'])) {
         //errors already logged
         $this->api_output['message'] = 'Project Creation Failure';
         $this->api_output['debug'] = array_values($projectStructure['result']['errors']);
     } else {
         //everything ok
         $this->api_output['status'] = 'OK';
         $this->api_output['message'] = 'Success';
         $this->api_output['id_project'] = $projectStructure['result']['id_project'];
         $this->api_output['project_pass'] = $projectStructure['result']['ppassword'];
         $this->api_output['new_keys'] = $this->new_keys;
         $this->api_output['analyze_url'] = INIT::$HTTPHOST . "/analyze/" . $projectStructure['project_name'] . "/" . $projectStructure['result']['id_project'] . "-" . $projectStructure['result']['ppassword'];
     }
 }
 protected function _performTestConversion($ip_converter)
 {
     self::_prettyEcho("> Attempt to convert files to detect service failures", 4);
     //get files for test
     $iterator = new DirectoryIterator("{$this->path}/{$this->original_dir}/");
     /**
      * @var $fileInfo DirectoryIterator
      */
     foreach ($iterator as $fileInfo) {
         if ($fileInfo->isDot() || $fileInfo->isDir()) {
             continue;
         }
         self::_prettyEcho("> Sending " . $fileInfo->getPathname(), 4);
         $t_start = microtime(true);
         //get extension
         //PHP 5.2 LACK THIS METHOD .... ADDED IN PHP 5.3.6 ....
         //$ext = $fileInfo->getExtension();
         $ext = FilesStorage::pathinfo_fix($fileInfo->getFilename(), PATHINFO_EXTENSION);
         //get path
         $file_path = $fileInfo->getPathname();
         $this->converterFactory->sendErrorReport = false;
         //10 seconds of timeout, average time is less than 5 seconds
         $this->converterFactory->setCurlOpt(array('CURLOPT_TIMEOUT' => 20));
         self::_prettyEcho("> Trying conversion on " . $ip_converter, 4);
         $convertResult = $this->converterFactory->convertToSdlxliff($file_path, $this->source_lang, $this->target_language, $ip_converter);
         $elapsed_time = microtime(true) - $t_start;
         if (!$convertResult['isSuccess']) {
             self::_prettyEcho("> Conversion failed in " . $elapsed_time . " seconds because of " . $convertResult['errorMessage'], 4);
             return $ip_converter;
         }
         self::_prettyEcho("> Done in " . $elapsed_time . " seconds", 4);
         self::_prettyEcho("> Performing content check", 4);
         if (!file_exists("{$this->path}/{$this->converted_dir}/{$ip_converter}")) {
             mkdir("{$this->path}/{$this->converted_dir}/{$ip_converter}", 0777, true);
         }
         //now not needed
         //file_put_contents( "$this->path/$this->converted_dir/$ip_converter/{$fileInfo->getFilename()}.sdlxliff", $convertResult[ 'xliffContent' ] );
         $template_xliffContent = file_get_contents("{$this->path}/{$this->template_dir}/{$fileInfo->getFilename()}.sdlxliff");
         //compare files: file lenght is weak, but necessary (because transunit-ids change and file blob changes from machine to machine) and sufficient (the text should really be the same)
         //get lenghts
         $template_len = strlen($template_xliffContent);
         $converted_len = strlen($convertResult['xliffContent']);
         self::_prettyEcho("> Original " . $fileInfo->getFilename() . ".sdlxliff Expected Length: " . $template_len, 4);
         self::_prettyEcho("> Received " . $fileInfo->getFilename() . ".sdlxliff Obtained Length: " . $converted_len, 4);
         $max = max($template_len, $converted_len);
         $min = min($template_len, $converted_len);
         //compare distance
         $diff = $min / $max;
         if ($diff < 0.97) {
             self::_prettyEcho("> Size Mismatch, conversion is failed...", 4);
             //file differ too much, conversion failed
             return $ip_converter;
         }
         return null;
     }
 }
Exemplo n.º 16
0
 protected static function sanitizeFileExtension($filename)
 {
     $pathinfo = FilesStorage::pathinfo_fix($filename);
     if (strtolower($pathinfo['extension']) == 'pdf') {
         $filename = $pathinfo['basename'] . ".docx";
     }
     return $filename;
 }
Exemplo n.º 17
0
 public function doAction()
 {
     $this->result['code'] = 0;
     // No Good, Default
     if (empty($this->file_name)) {
         $this->result['code'] = -1;
         // No Good, Default
         $this->result['errors'][] = array("code" => -1, "message" => "Error: missing file name.");
         return false;
     }
     $ext = FilesStorage::pathinfo_fix($this->file_name, PATHINFO_EXTENSION);
     $conversionHandler = new ConversionHandler();
     $conversionHandler->setFileName($this->file_name);
     $conversionHandler->setSourceLang($this->source_lang);
     $conversionHandler->setTargetLang($this->target_lang);
     $conversionHandler->setSegmentationRule($this->segmentation_rule);
     $conversionHandler->setCookieDir($this->cookieDir);
     $conversionHandler->setIntDir($this->intDir);
     $conversionHandler->setErrDir($this->errDir);
     if ($ext == "zip") {
         if ($this->convertZipFile) {
             // this makes the conversionhandler accumulate eventual errors on files and continue
             $conversionHandler->setStopOnFileException(false);
             $internalZipFileNames = $conversionHandler->extractZipFile();
             //call convertFileWrapper and start conversions for each file
             if ($conversionHandler->uploadError) {
                 $fileErrors = $conversionHandler->getUploadedFiles();
                 foreach ($fileErrors as $fileError) {
                     if (count($fileError->error) == 0) {
                         continue;
                     }
                     $brokenFileName = ZipArchiveExtended::getFileName($fileError->name);
                     /*
                      * TODO
                      * return error code is 2 because
                      *      <=0 is for errors
                      *      1   is OK
                      *
                      * In this case, we raise warnings, hence the return code must be a new code
                      */
                     $this->result['code'] = 2;
                     $this->result['errors'][$brokenFileName] = array('code' => $fileError->error['code'], 'message' => $fileError->error['message'], 'debug' => $brokenFileName);
                 }
             }
             $realFileNames = array_map(array('ZipArchiveExtended', 'getFileName'), $internalZipFileNames);
             foreach ($realFileNames as $i => &$fileObject) {
                 $fileObject = array('name' => $fileObject, 'size' => filesize($this->intDir . DIRECTORY_SEPARATOR . $internalZipFileNames[$i]));
             }
             $this->result['data']['zipFiles'] = json_encode($realFileNames);
             $stdFileObjects = array();
             if ($internalZipFileNames !== null) {
                 foreach ($internalZipFileNames as $fName) {
                     $newStdFile = new stdClass();
                     $newStdFile->name = $fName;
                     $stdFileObjects[] = $newStdFile;
                 }
             } else {
                 $errors = $conversionHandler->getResult();
                 $errors = array_map(array('Upload', 'formatExceptionMessage'), $errors['errors']);
                 $this->result['errors'] = array_merge($this->result['errors'], $errors);
                 return false;
             }
             /* Do conversions here */
             $converter = new ConvertFileWrapper($stdFileObjects, false);
             $converter->intDir = $this->intDir;
             $converter->errDir = $this->errDir;
             $converter->cookieDir = $this->cookieDir;
             $converter->source_lang = $this->source_lang;
             $converter->target_lang = $this->target_lang;
             $converter->doAction();
             $errors = $converter->checkResult();
             if (count($errors) > 0) {
                 $this->result['code'] = 2;
                 foreach ($errors as $__err) {
                     $brokenFileName = ZipArchiveExtended::getFileName($__err['debug']);
                     if (!isset($this->result['errors'][$brokenFileName])) {
                         $this->result['errors'][$brokenFileName] = array('code' => $__err['code'], 'message' => $__err['message'], 'debug' => $brokenFileName);
                     }
                 }
             }
         } else {
             $this->result['errors'][] = array("code" => -2, "message" => "Nested zip files are not allowed");
             return false;
         }
     } else {
         $conversionHandler->doAction();
         $this->result = $conversionHandler->getResult();
     }
     isset($this->result['errors']) ? null : ($this->result['errors'] = array());
     if (count($this->result['errors']) == 0) {
         $this->result['code'] = 1;
     } else {
         $this->result['errors'] = array_values($this->result['errors']);
     }
 }