Ejemplo 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;
 }
Ejemplo n.º 2
0
 public function doAction()
 {
     //get storage object
     $fs = new FilesStorage();
     $files_job = $fs->getOriginalFilesForJob($this->id_job, $this->id_file, $this->password);
     $output_content = array();
     foreach ($files_job as $file) {
         $id_file = $file['id_file'];
         $output_content[$id_file]['filename'] = $file['filename'];
         $output_content[$id_file]['contentPath'] = $file['originalFilePath'];
     }
     if ($this->download_type == 'all') {
         if (count($output_content) > 1) {
             $this->_filename = $this->fname;
             $pathinfo = pathinfo($this->fname);
             if ($pathinfo['extension'] != 'zip') {
                 $this->_filename = $pathinfo['basename'] . ".zip";
             }
             $this->content = $this->composeZip($output_content);
             //add zip archive content here;
         } elseif (count($output_content) == 1) {
             $this->setContent($output_content);
         }
     } else {
         $this->setContent($output_content);
     }
 }
 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.º 4
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());
     }
 }
Ejemplo n.º 5
0
 protected function _zipFileHandling($linkFiles)
 {
     //begin of zip hashes manipulation
     foreach ($linkFiles['zipHashes'] as $zipHash) {
         $result = $this->fileStorage->linkZipToProject($this->projectStructure['create_date'], $zipHash, $this->projectStructure['id_project']);
         if (!$result) {
             Log::doLog("Failed to store the Zip file {$zipHash} - \n");
             $this->projectStructure['result']['errors'][] = array("code" => -10, "message" => "Failed to store the original Zip {$zipHash} ");
             throw new Exception("Failed to store the original Zip {$zipHash} ");
             //Exit
         }
     }
     //end zip hashes manipulation
 }
Ejemplo n.º 6
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']);
     }
 }
Ejemplo n.º 7
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'];
 }
Ejemplo n.º 8
0
 /**
  * Creates a message from an HTML string, making modifications for inline images and backgrounds
  * and creates a plain-text version by converting the HTML
  * Overwrites any existing values in $this->Body and $this->AltBody
  * @access public
  * @param string $message HTML message string
  * @param string $basedir baseline directory for path
  * @param bool $advanced Whether to use the advanced HTML to text converter
  * @return string $message
  */
 public function MsgHTML($message, $basedir = '', $advanced = false)
 {
     preg_match_all("/(src|background)=[\"'](.*)[\"']/Ui", $message, $images);
     if (isset($images[2])) {
         foreach ($images[2] as $i => $url) {
             // do not change urls for absolute images (thanks to corvuscorax)
             if (!preg_match('#^[A-z]+://#', $url)) {
                 $filename = FilesStorage::basename_fix($url);
                 $directory = dirname($url);
                 if ($directory == '.') {
                     $directory = '';
                 }
                 $cid = md5($url) . '@phpmailer.0';
                 //RFC2392 S 2
                 if (strlen($basedir) > 1 && substr($basedir, -1) != '/') {
                     $basedir .= '/';
                 }
                 if (strlen($directory) > 1 && substr($directory, -1) != '/') {
                     $directory .= '/';
                 }
                 if ($this->AddEmbeddedImage($basedir . $directory . $filename, $cid, $filename, 'base64', self::_mime_types(self::mb_pathinfo($filename, PATHINFO_EXTENSION)))) {
                     $message = preg_replace("/" . $images[1][$i] . "=[\"']" . preg_quote($url, '/') . "[\"']/Ui", $images[1][$i] . "=\"cid:" . $cid . "\"", $message);
                 }
             }
         }
     }
     $this->IsHTML(true);
     if (empty($this->AltBody)) {
         $this->AltBody = 'To view this email message, open it in a program that understands HTML!' . "\n\n";
     }
     //Convert all message body line breaks to CRLF, makes quoted-printable encoding work much better
     $this->Body = $this->NormalizeBreaks($message);
     $this->AltBody = $this->NormalizeBreaks($this->html2text($message, $advanced));
     return $this->Body;
 }
Ejemplo n.º 9
0
 public function doAction()
 {
     $debug = array();
     $debug['total'][] = time();
     //get job language and data
     //Fixed Bug: need a specific job, because we need The target Language
     //Removed from within the foreach cycle, the job is always the same....
     $jobData = $this->jobInfo = getJobData($this->id_job, $this->password);
     $pCheck = new AjaxPasswordCheck();
     //check for Password correctness
     if (empty($jobData) || !$pCheck->grantJobAccessByJobData($jobData, $this->password)) {
         $msg = "Error : wrong password provided for download \n\n " . var_export($_POST, true) . "\n";
         Log::doLog($msg);
         Utils::sendErrMailReport($msg);
         return null;
     }
     $debug['get_file'][] = time();
     //get storage object
     $fs = new FilesStorage();
     $files_job = $fs->getFilesForJob($this->id_job, $this->id_file);
     $debug['get_file'][] = time();
     $nonew = 0;
     $output_content = array();
     /*
       the procedure:
       1)original xliff file is read directly from disk; a file handler is obtained
       2)the file is read chunk by chunk by a stream parser: for each trans-unit that is encountered, target is replaced (or added) with the corresponding translation obtained from the DB
       3)the parsed portion of xliff in the buffer is flushed on temporary file
       4)the temporary file is sent to the converter and an original file is obtained
       5)the temporary file is deleted
     */
     //file array is chuncked. Each chunk will be used for a parallel conversion request.
     $files_job = array_chunk($files_job, self::FILES_CHUNK_SIZE);
     foreach ($files_job as $chunk) {
         $converter = new FileFormatConverter();
         $files_buffer = array();
         foreach ($chunk as $file) {
             $mime_type = $file['mime_type'];
             $fileID = $file['id_file'];
             $current_filename = $file['filename'];
             //get path for the output file converted to know it's right extension
             $_fileName = explode(DIRECTORY_SEPARATOR, $file['xliffFilePath']);
             $outputPath = INIT::$TMP_DOWNLOAD . '/' . $this->id_job . '/' . $fileID . '/' . uniqid('', true) . "_.out." . array_pop($_fileName);
             //make dir if doesn't exist
             if (!file_exists(dirname($outputPath))) {
                 Log::doLog('Create Directory ' . escapeshellarg(dirname($outputPath)) . '');
                 mkdir(dirname($outputPath), 0775, true);
             }
             $debug['get_segments'][] = time();
             $data = getSegmentsDownload($this->id_job, $this->password, $fileID, $nonew);
             $debug['get_segments'][] = time();
             //prepare regexp for nest step
             $regexpEntity = '/&#x(0[0-8BCEF]|1[0-9A-F]|7F);/u';
             $regexpAscii = '/([\\x{00}-\\x{1F}\\x{7F}]{1})/u';
             foreach ($data as $i => $k) {
                 //create a secondary indexing mechanism on segments' array; this will be useful
                 //prepend a string so non-trans unit id ( ex: numerical ) are not overwritten
                 $data['matecat|' . $k['internal_id']][] = $i;
                 //FIXME: temporary patch
                 $data[$i]['translation'] = str_replace('<x id="nbsp"/>', '&#xA0;', $data[$i]['translation']);
                 $data[$i]['segment'] = str_replace('<x id="nbsp"/>', '&#xA0;', $data[$i]['segment']);
                 //remove binary chars in some xliff files
                 $sanitized_src = preg_replace($regexpAscii, '', $data[$i]['segment']);
                 $sanitized_trg = preg_replace($regexpAscii, '', $data[$i]['translation']);
                 //clean invalid xml entities ( charactes with ascii < 32 and different from 0A, 0D and 09
                 $sanitized_src = preg_replace($regexpEntity, '', $sanitized_src);
                 $sanitized_trg = preg_replace($regexpEntity, '', $sanitized_trg);
                 if ($sanitized_src != null) {
                     $data[$i]['segment'] = $sanitized_src;
                 }
                 if ($sanitized_trg != null) {
                     $data[$i]['translation'] = $sanitized_trg;
                 }
             }
             $debug['replace'][] = time();
             //instatiate parser
             $xsp = new XliffSAXTranslationReplacer($file['xliffFilePath'], $data, Langs_Languages::getInstance()->getLangRegionCode($jobData['target']), $outputPath);
             if ($this->download_type == 'omegat') {
                 $xsp->setSourceInTarget(true);
             }
             //run parsing
             Log::doLog("work on " . $fileID . " " . $current_filename);
             $xsp->replaceTranslation();
             //free memory
             unset($xsp);
             unset($data);
             $debug['replace'][] = time();
             $output_content[$fileID]['documentContent'] = file_get_contents($outputPath);
             $output_content[$fileID]['filename'] = $current_filename;
             if ($this->forceXliff) {
                 $file_info_details = pathinfo($output_content[$fileID]['filename']);
                 $output_content[$fileID]['filename'] = basename($outputPath);
             }
             /**
              * Conversion Enforce
              */
             $convertBackToOriginal = true;
             try {
                 //if it is a not converted file ( sdlxliff ) we have originalFile equals to xliffFile (it has just been copied)
                 $file['original_file'] = file_get_contents($file['originalFilePath']);
                 if (!INIT::$CONVERSION_ENABLED || ($file['originalFilePath'] == $file['xliffFilePath'] and $mime_type == 'sdlxliff') or $this->forceXliff) {
                     $convertBackToOriginal = false;
                     Log::doLog("SDLXLIFF: {$file['filename']} --- " . var_export($convertBackToOriginal, true));
                 } else {
                     //TODO: dos2unix ??? why??
                     //force unix type files
                     Log::doLog("NO SDLXLIFF, Conversion enforced: {$file['filename']} --- " . var_export($convertBackToOriginal, true));
                 }
             } catch (Exception $e) {
                 Log::doLog($e->getMessage());
             }
             if ($convertBackToOriginal) {
                 $output_content[$fileID]['out_xliff_name'] = $outputPath;
                 $output_content[$fileID]['source'] = $jobData['source'];
                 $output_content[$fileID]['target'] = $jobData['target'];
                 $files_buffer[$fileID] = $output_content[$fileID];
             } elseif ($this->forceXliff) {
                 $this->cleanFilePath($output_content[$fileID]['documentContent']);
             }
         }
         $debug['do_conversion'][] = time();
         $convertResult = $converter->multiConvertToOriginal($files_buffer, $chosen_machine = false);
         foreach (array_keys($files_buffer) as $fileID) {
             $output_content[$fileID]['documentContent'] = $this->ifGlobalSightXliffRemoveTargetMarks($convertResult[$fileID]['documentContent'], $files_buffer[$fileID]['filename']);
             //in case of .strings, they are required to be in UTF-16
             //get extension to perform file detection
             $extension = pathinfo($output_content[$fileID]['filename'], PATHINFO_EXTENSION);
             if (strtoupper($extension) == 'STRINGS') {
                 //use this function to convert stuff
                 $encodingConvertedFile = CatUtils::convertEncoding('UTF-16', $output_content[$fileID]['documentContent']);
                 //strip previously added BOM
                 $encodingConvertedFile[1] = $converter->stripBOM($encodingConvertedFile[1], 16);
                 //store new content
                 $output_content[$fileID]['documentContent'] = $encodingConvertedFile[1];
                 //trash temporary data
                 unset($encodingConvertedFile);
             }
         }
         //            $output_content[ $fileID ][ 'documentContent' ] = $convertResult[ 'documentContent' ];
         unset($convertResult);
         $debug['do_conversion'][] = time();
     }
     //set the file Name
     $pathinfo = pathinfo($this->fname);
     $this->_filename = $pathinfo['filename'] . "_" . $jobData['target'] . "." . $pathinfo['extension'];
     //qui prodest to check download type?
     if ($this->download_type == 'omegat') {
         $this->_filename .= ".zip";
         $tmsService = new TMSService();
         $tmsService->setOutputType('tm');
         /**
          * @var $tmFile SplTempFileObject
          */
         $tmFile = $tmsService->exportJobAsTMX($this->id_job, $this->password, $jobData['source'], $jobData['target']);
         $tmsService->setOutputType('mt');
         /**
          * @var $mtFile SplTempFileObject
          */
         $mtFile = $tmsService->exportJobAsTMX($this->id_job, $this->password, $jobData['source'], $jobData['target']);
         $tm_id = uniqid('tm');
         $mt_id = uniqid('mt');
         $output_content[$tm_id] = array('documentContent' => '', 'filename' => $pathinfo['filename'] . "_" . $jobData['target'] . "_TM . tmx");
         foreach ($tmFile as $lineNumber => $content) {
             $output_content[$tm_id]['documentContent'] .= $content;
         }
         $output_content[$mt_id] = array('documentContent' => '', 'filename' => $pathinfo['filename'] . "_" . $jobData['target'] . "_MT . tmx");
         foreach ($mtFile as $lineNumber => $content) {
             $output_content[$mt_id]['documentContent'] .= $content;
         }
         $this->createOmegaTZip($output_content, $jobData['source'], $jobData['target']);
         //add zip archive content here;
     } else {
         if (count($output_content) > 1) {
             if ($pathinfo['extension'] != 'zip') {
                 if ($this->forceXliff) {
                     $this->_filename = $this->id_job . ".zip";
                 } else {
                     $this->_filename = $pathinfo['basename'] . ".zip";
                 }
             }
             $this->composeZip($output_content, $jobData['source']);
             //add zip archive content here;
         } else {
             //always an array with 1 element, pop it, Ex: array( array() )
             $output_content = array_pop($output_content);
             $this->setContent($output_content);
         }
     }
     $debug['total'][] = time();
     Utils::deleteDir(INIT::$TMP_DOWNLOAD . '/' . $this->id_job . '/');
 }
Ejemplo n.º 10
0
 public static function isJobBasedOnMateCatFilters($jobId)
 {
     $fs = new FilesStorage();
     $files = $fs->getFilesForJob($jobId, null);
     foreach ($files as $file) {
         $fileType = DetectProprietaryXliff::getInfo($files[0]['xliffFilePath']);
         if ($fileType['proprietary_short_name'] !== 'matecat_converter') {
             // If only one XLIFF is not created with MateCat Filters, we can't say
             // that the project is entirely based on new Filters
             return false;
         }
     }
     // If the flow arrives here, all the files' XLIFFs are based on new Filters
     return true;
 }
 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;
     }
 }
Ejemplo n.º 12
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);
     }
 }
Ejemplo 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;
 }
Ejemplo 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'];
     }
 }
Ejemplo n.º 15
0
 public function createProject()
 {
     // project name sanitize
     $oldName = $this->projectStructure['project_name'];
     $this->projectStructure['project_name'] = $this->_sanitizeName($this->projectStructure['project_name']);
     if ($this->projectStructure['project_name'] == false) {
         $this->projectStructure['result']['errors'][] = array("code" => -5, "message" => "Invalid Project Name " . $oldName . ": it should only contain numbers and letters!");
         return false;
     }
     // create project
     $this->projectStructure['ppassword'] = $this->_generatePassword();
     $this->projectStructure['user_ip'] = Utils::getRealIpAddr();
     $this->projectStructure['id_customer'] = 'translated_user';
     $this->projectStructure['id_project'] = insertProject($this->projectStructure);
     //create user (Massidda 2013-01-24)
     //check if all the keys are valid MyMemory keys
     if (!empty($this->projectStructure['private_tm_key'])) {
         foreach ($this->projectStructure['private_tm_key'] as $i => $_tmKey) {
             $this->tmxServiceWrapper->setTmKey($_tmKey['key']);
             try {
                 $keyExists = $this->tmxServiceWrapper->checkCorrectKey();
                 if (!isset($keyExists) || $keyExists === false) {
                     Log::doLog(__METHOD__ . " -> TM key is not valid.");
                     throw new Exception("TM key is not valid.", -4);
                 }
             } catch (Exception $e) {
                 $this->projectStructure['result']['errors'][] = array("code" => $e->getCode(), "message" => $e->getMessage());
                 return false;
             }
             //set the first key as primary
             $this->tmxServiceWrapper->setTmKey($this->projectStructure['private_tm_key'][0]['key']);
         }
         //check if the MyMemory keys provided by the user are already associated to him.
         if ($this->projectStructure['userIsLogged']) {
             $mkDao = new TmKeyManagement_MemoryKeyDao($this->dbHandler);
             $searchMemoryKey = new TmKeyManagement_MemoryKeyStruct();
             $searchMemoryKey->uid = $this->projectStructure['uid'];
             $userMemoryKeys = $mkDao->read($searchMemoryKey);
             $userTmKeys = array();
             $memoryKeysToBeInserted = array();
             //extract user tm keys
             foreach ($userMemoryKeys as $_memoKey) {
                 /**
                  * @var $_memoKey TmKeyManagement_MemoryKeyStruct
                  */
                 $userTmKeys[] = $_memoKey->tm_key->key;
             }
             foreach ($this->projectStructure['private_tm_key'] as $_tmKey) {
                 if (!in_array($_tmKey['key'], $userTmKeys)) {
                     $newMemoryKey = new TmKeyManagement_MemoryKeyStruct();
                     $newTmKey = new TmKeyManagement_TmKeyStruct();
                     $newTmKey->key = $_tmKey['key'];
                     $newTmKey->tm = true;
                     $newTmKey->glos = true;
                     //TODO: take this from input
                     $newTmKey->name = $_tmKey['name'];
                     $newMemoryKey->tm_key = $newTmKey;
                     $newMemoryKey->uid = $this->projectStructure['uid'];
                     $memoryKeysToBeInserted[] = $newMemoryKey;
                 } else {
                     Log::doLog('skip insertion');
                 }
             }
             try {
                 $mkDao->createList($memoryKeysToBeInserted);
             } catch (Exception $e) {
                 Log::doLog($e->getMessage());
                 # Here we handle the error, displaying HTML, logging, ...
                 $output = "<pre>\n";
                 $output .= $e->getMessage() . "\n\t";
                 $output .= "</pre>";
                 Utils::sendErrMailReport($output);
             }
         }
         //the base case is when the user clicks on "generate private TM" button:
         //a (user, pass, key) tuple is generated and can be inserted
         //if it comes with it's own key without querying the creation API, create a (key,key,key) user
         if (empty($this->projectStructure['private_tm_user'])) {
             $this->projectStructure['private_tm_user'] = $this->projectStructure['private_tm_key'][0]['key'];
             $this->projectStructure['private_tm_pass'] = $this->projectStructure['private_tm_key'][0]['key'];
         }
         insertTranslator($this->projectStructure);
     }
     //sort files in order to process TMX first
     $sortedFiles = array();
     foreach ($this->projectStructure['array_files'] as $fileName) {
         if ('tmx' == pathinfo($fileName, PATHINFO_EXTENSION)) {
             //found TMX, enable language checking routines
             $this->checkTMX = 1;
             array_unshift($sortedFiles, $fileName);
         } else {
             array_push($sortedFiles, $fileName);
         }
     }
     $this->projectStructure['array_files'] = $sortedFiles;
     unset($sortedFiles);
     $uploadDir = INIT::$UPLOAD_REPOSITORY . DIRECTORY_SEPARATOR . $this->projectStructure['uploadToken'];
     //fetch cache links, created by converter, from upload directory
     $linkFiles = scandir($uploadDir);
     //remove dir hardlinks, as uninteresting, as weel as regular files; only hash-links
     foreach ($linkFiles as $k => $linkFile) {
         if (strpos($linkFile, '.') !== false or strpos($linkFile, '|') === false) {
             unset($linkFiles[$k]);
         }
     }
     /*
        loop through all input files to
        1)upload TMX
        2)convert, in case, non standard XLIFF files to a format that Matecat understands
     
        Note that XLIFF that don't need conversion are moved anyway as they are to cache in order not to alter the workflow
     */
     foreach ($this->projectStructure['array_files'] as $fileName) {
         //if TMX,
         if ('tmx' == pathinfo($fileName, PATHINFO_EXTENSION)) {
             //load it into MyMemory; we'll check later on how it went
             $file = new stdClass();
             $file->file_path = "{$uploadDir}/{$fileName}";
             $this->tmxServiceWrapper->setName($fileName);
             $this->tmxServiceWrapper->setFile(array($file));
             try {
                 $this->tmxServiceWrapper->addTmxInMyMemory();
             } catch (Exception $e) {
                 $this->projectStructure['result']['errors'][] = array("code" => $e->getCode(), "message" => $e->getMessage());
                 return false;
             }
             //in any case, skip the rest of the loop, go to the next file
             continue;
         }
         /*
           Conversion Enforce
           Checking Extension is no more sufficient, we want check content if this is an idiom xlf file type, conversion are enforced
           $enforcedConversion = true; //( if conversion is enabled )
         */
         $isAnXliffToConvert = $this->isConversionToEnforce($fileName);
         //we are going to access the storage, get model object to manipulate it
         $fs = new FilesStorage();
         //if it's one of the listed formats or conversion is not enabled in first place
         if (!$isAnXliffToConvert) {
             /*
               filename is already an xliff and it's in upload directory
               we have to make a cache package from it to avoid altering the original path
             */
             //get file
             $filePathName = "{$uploadDir}/{$fileName}";
             //calculate hash + add the fileName, if i load 3 equal files with the same content
             // they will be squashed to the last one
             $sha1 = sha1(file_get_contents($filePathName) . $filePathName);
             //make a cache package (with work/ only, emtpy orig/)
             $fs->makeCachePackage($sha1, $this->projectStructure['source_language'], false, $filePathName);
             //put reference to cache in upload dir to link cache to session
             $fs->linkSessionToCache($sha1, $this->projectStructure['source_language'], $this->projectStructure['uploadToken']);
             //add newly created link to list
             $linkFiles[] = $sha1 . "|" . $this->projectStructure['source_language'];
             unset($sha1);
         }
     }
     //now, upload dir contains only hash-links
     //we start copying files to "file" dir, inserting metadata in db and extracting segments
     foreach ($linkFiles as $linkFile) {
         //converted file is inside cache directory
         //get hash from file name inside UUID dir
         $hashFile = basename($linkFile);
         $hashFile = explode('|', $hashFile);
         //use hash and lang to fetch file from package
         $xliffFilePathName = $fs->getXliffFromCache($hashFile[0], $hashFile[1]);
         //get sha
         $sha1_original = $hashFile[0];
         //get original file name
         $originalFilePathName = $fs->getOriginalFromCache($hashFile[0], $hashFile[1]);
         $raw_originalFilePathName = explode(DIRECTORY_SEPARATOR, $originalFilePathName);
         $fileName = array_pop($raw_originalFilePathName);
         unset($hashFile);
         if (!file_exists($xliffFilePathName)) {
             $this->projectStructure['result']['errors'][] = array("code" => -6, "message" => "File not found on server after upload.");
         }
         try {
             $info = pathinfo($xliffFilePathName);
             if (!in_array($info['extension'], array('xliff', 'sdlxliff', 'xlf'))) {
                 throw new Exception("Failed to find Xliff - no segments found", -3);
             }
             $mimeType = pathinfo($fileName, PATHINFO_EXTENSION);
             $yearMonthPath = date_create()->format("Ymd");
             $fileDateSha1Path = $yearMonthPath . DIRECTORY_SEPARATOR . $sha1_original;
             $fid = insertFile($this->projectStructure, $fileName, $mimeType, $fileDateSha1Path);
             //move the file in the right directory from the packages to the file dir
             $fs->moveFromCacheToFileDir($fileDateSha1Path, $this->projectStructure['source_language'], $fid);
             $this->projectStructure['file_id_list']->append($fid);
             $this->_extractSegments(file_get_contents($xliffFilePathName), $fid);
         } catch (Exception $e) {
             if ($e->getCode() == -1) {
                 $this->projectStructure['result']['errors'][] = array("code" => -1, "message" => "No text to translate in the file {$fileName}.");
                 $fs->deleteHashFromUploadDir($uploadDir, $linkFile);
             } elseif ($e->getCode() == -2) {
                 $this->projectStructure['result']['errors'][] = array("code" => -7, "message" => "Failed to store segments in database for {$fileName}");
             } elseif ($e->getCode() == -3) {
                 $this->projectStructure['result']['errors'][] = array("code" => -7, "message" => "File {$fileName} not found. Failed to save XLIFF conversion on disk");
             } elseif ($e->getCode() == -4) {
                 $this->projectStructure['result']['errors'][] = array("code" => -7, "message" => "Internal Error. Xliff Import: Error parsing. ( {$fileName} )");
             } elseif ($e->getCode() == -11) {
                 $this->projectStructure['result']['errors'][] = array("code" => -7, "message" => "Failed to store reference files on disk. Permission denied");
             } elseif ($e->getCode() == -12) {
                 $this->projectStructure['result']['errors'][] = array("code" => -7, "message" => "Failed to store reference files in database");
             } elseif ($e->getCode() == -13) {
                 $this->projectStructure['result']['errors'][] = array("code" => -13, "message" => $e->getMessage());
                 Log::doLog($e->getMessage());
                 return null;
                 // SEVERE EXCEPTION we can not write to disk!! Break project creation
             } else {
                 //mysql insert Blob Error
                 $this->projectStructure['result']['errors'][] = array("code" => -7, "message" => "Failed to create project. Database Error on {$fileName}. Please try again.");
             }
             Log::doLog($e->getMessage());
         }
     }
     //end of hash-link loop
     //check if the files language equals the source language. If not, set an error message.
     if (!$this->projectStructure['skip_lang_validation']) {
         $this->validateFilesLanguages();
     }
     /****************/
     //loop again through files to check to check for TMX loading
     foreach ($this->projectStructure['array_files'] as $fileName) {
         //if TMX,
         if ('tmx' == pathinfo($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
                     return false;
                 }
             }
             //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']);
                 }
             }
         }
     }
     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']);
         return false;
     }
     if (!empty($this->projectStructure['result']['errors'])) {
         Log::doLog("Project Creation Failed. Sent to Output all errors.");
         Log::doLog($this->projectStructure['result']['errors']);
         return false;
     }
     //Log::doLog( array_pop( array_chunk( $SegmentTranslations[$fid], 25, true ) ) );
     //create job
     if (isset($_SESSION['cid']) and !empty($_SESSION['cid'])) {
         $owner = $_SESSION['cid'];
     } else {
         $_SESSION['_anonym_pid'] = $this->projectStructure['id_project'];
         //default user
         $owner = '';
     }
     $isEmptyProject = false;
     //Throws exception
     try {
         $this->_createJobs($this->projectStructure, $owner);
         //FIXME for project with pre translation this query is not enough,
         //we need compare the number of segments with translations, but take an eye to the opensource
         $query_visible_segments = "SELECT count(*) as cattool_segments\n\t\t\t\tFROM segments WHERE id_file IN ( %s ) and show_in_cattool = 1";
         $string_file_list = implode(",", $this->projectStructure['file_id_list']->getArrayCopy());
         $query_visible_segments = sprintf($query_visible_segments, $string_file_list);
         $rows = $this->dbHandler->fetch_array($query_visible_segments);
         if (!$rows) {
             Log::doLog("Segment Search: Failed Retrieve min_segment/max_segment for files ( {$string_file_list} ) - DB Error: " . var_export($this->dbHandler->get_error(), true) . " - \n");
             throw new Exception("Segment Search: Failed Retrieve min_segment/max_segment for job", -5);
         }
         if ($rows[0]['cattool_segments'] == 0) {
             Log::doLog("Segment Search: No segments in this project - \n");
             $isEmptyProject = true;
         }
     } catch (Exception $ex) {
         $this->projectStructure['result']['errors'][] = array("code" => -9, "message" => "Fail to create Job. ( {$ex->getMessage()} )");
         return false;
     }
     try {
         Utils::deleteDir($uploadDir);
         if (is_dir($uploadDir . '_converted')) {
             Utils::deleteDir($uploadDir . '_converted');
         }
     } catch (Exception $e) {
         $output = "<pre>\n";
         $output .= " - Exception: " . print_r($e->getMessage(), true) . "\n";
         $output .= " - REQUEST URI: " . print_r(@$_SERVER['REQUEST_URI'], true) . "\n";
         $output .= " - REQUEST Message: " . print_r($_REQUEST, true) . "\n";
         $output .= " - Trace: \n" . print_r($e->getTraceAsString(), true) . "\n";
         $output .= "\n\t";
         $output .= "Aborting...\n";
         $output .= "</pre>";
         Log::doLog($output);
         Utils::sendErrMailReport($output, $e->getMessage());
     }
     $this->projectStructure['status'] = INIT::$VOLUME_ANALYSIS_ENABLED ? Constants_ProjectStatus::STATUS_NEW : Constants_ProjectStatus::STATUS_NOT_TO_ANALYZE;
     if ($isEmptyProject) {
         $this->projectStructure['status'] = Constants_ProjectStatus::STATUS_EMPTY;
     }
     $this->projectStructure['result']['code'] = 1;
     $this->projectStructure['result']['data'] = "OK";
     $this->projectStructure['result']['ppassword'] = $this->projectStructure['ppassword'];
     $this->projectStructure['result']['password'] = $this->projectStructure['array_jobs']['job_pass'];
     $this->projectStructure['result']['id_job'] = $this->projectStructure['array_jobs']['job_list'];
     $this->projectStructure['result']['job_segments'] = $this->projectStructure['array_jobs']['job_segments'];
     $this->projectStructure['result']['id_project'] = $this->projectStructure['id_project'];
     $this->projectStructure['result']['project_name'] = $this->projectStructure['project_name'];
     $this->projectStructure['result']['source_language'] = $this->projectStructure['source_language'];
     $this->projectStructure['result']['target_language'] = $this->projectStructure['target_language'];
     $this->projectStructure['result']['status'] = $this->projectStructure['status'];
     $this->projectStructure['result']['lang_detect'] = $this->projectStructure['lang_detect_files'];
     $query_project_summary = "\n            SELECT\n                 COUNT( s.id ) AS project_segments,\n                 SUM( IF( IFNULL( st.eq_word_count, -1 ) = -1, s.raw_word_count, st.eq_word_count ) ) AS project_raw_wordcount\n            FROM segments s\n            INNER JOIN files_job fj ON fj.id_file = s.id_file\n            INNER JOIN jobs j ON j.id= fj.id_job\n            LEFT JOIN segment_translations st ON s.id = st.id_segment\n            WHERE j.id_project = %u\n        ";
     $query_project_summary = sprintf($query_project_summary, $this->projectStructure['id_project']);
     $project_summary = $this->dbHandler->fetch_array($query_project_summary);
     $update_project_count = "\n            UPDATE projects\n              SET\n                standard_analysis_wc = %.2F,\n                status_analysis = '%s'\n            WHERE id = %u\n        ";
     $update_project_count = sprintf($update_project_count, $project_summary[0]['project_raw_wordcount'], $this->projectStructure['status'], $this->projectStructure['id_project']);
     $this->dbHandler->query($update_project_count);
     //        Log::doLog( $this->projectStructure );
     //create Project into DQF queue
     if (INIT::$DQF_ENABLED && !empty($this->projectStructure['dqf_key'])) {
         $dqfProjectStruct = DQF_DqfProjectStruct::getStruct();
         $dqfProjectStruct->api_key = $this->projectStructure['dqf_key'];
         $dqfProjectStruct->project_id = $this->projectStructure['id_project'];
         $dqfProjectStruct->name = $this->projectStructure['project_name'];
         $dqfProjectStruct->source_language = $this->projectStructure['source_language'];
         $dqfQueue = new Analysis_DqfQueueHandler();
         try {
             $dqfQueue->createProject($dqfProjectStruct);
             //for each job, push a task into AMQ's DQF queue
             foreach ($this->projectStructure['array_jobs']['job_list'] as $i => $jobID) {
                 /**
                  * @var $dqfTaskStruct DQF_DqfTaskStruct
                  */
                 $dqfTaskStruct = DQF_DqfTaskStruct::getStruct();
                 $dqfTaskStruct->api_key = $this->projectStructure['dqf_key'];
                 $dqfTaskStruct->project_id = $this->projectStructure['id_project'];
                 $dqfTaskStruct->task_id = $jobID;
                 $dqfTaskStruct->target_language = $this->projectStructure['target_language'][$i];
                 $dqfTaskStruct->file_name = uniqid('', true) . $this->projectStructure['project_name'];
                 $dqfQueue->createTask($dqfTaskStruct);
             }
         } catch (Exception $exn) {
             $output = __METHOD__ . " (code " . $exn->getCode() . " ) - " . $exn->getMessage();
             Log::doLog($output);
             Utils::sendErrMailReport($output, $exn->getMessage());
         }
     }
 }
Ejemplo n.º 16
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;
 }
Ejemplo n.º 17
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;
 }
Ejemplo n.º 18
0
 /**
  * Avoid race conditions by javascript multiple calls
  *
  * @param $file_path
  */
 private function deleteSha($file_path)
 {
     $sha1 = sha1_file($file_path);
     if (!$sha1) {
         return;
     }
     //can be present more than one file with the same sha
     //so in the sha1 file there could be more than one row
     $file_sha = glob($this->options['upload_dir'] . $sha1 . "*");
     //delete sha1 also
     $fp = fopen($file_sha[0], "r+");
     // no file found
     if (!$fp) {
         return;
     }
     $i = 0;
     while (!flock($fp, LOCK_EX | LOCK_NB)) {
         // acquire an exclusive lock
         $i++;
         if ($i == 40) {
             return;
         }
         //exit the loop after 2 seconds, can not acquire the lock
         usleep(50000);
         continue;
     }
     $file_content = fread($fp, filesize($file_sha[0]));
     $file_content_array = explode("\n", $file_content);
     //remove the last line ( is an empty string )
     array_pop($file_content_array);
     $fileName = FilesStorage::basename_fix($file_path);
     $key = array_search($fileName, $file_content_array);
     unset($file_content_array[$key]);
     if (!empty($file_content_array)) {
         fseek($fp, 0);
         //rewind
         ftruncate($fp, 0);
         //truncate to zero bytes length
         fwrite($fp, implode("\n", $file_content_array) . "\n");
         fflush($fp);
         flock($fp, LOCK_UN);
         // release the lock
         fclose($fp);
     } else {
         flock($fp, LOCK_UN);
         // release the lock
         fclose($fp);
         @unlink(@$file_sha[0]);
     }
 }
Ejemplo n.º 19
0
 private function prependZipFileName($fName)
 {
     return FilesStorage::pathinfo_fix($this->filename, PATHINFO_BASENAME) . self::INTERNAL_SEPARATOR . $fName;
 }
Ejemplo n.º 20
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;
 }
Ejemplo n.º 21
0
 protected static function sanitizeFileExtension($filename)
 {
     $pathinfo = FilesStorage::pathinfo_fix($filename);
     if (strtolower($pathinfo['extension']) == 'pdf') {
         $filename = $pathinfo['basename'] . ".docx";
     }
     return $filename;
 }
Ejemplo n.º 22
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']);
     }
 }