public function doAction()
 {
     switch ($this->__postInput['exec']) {
         case 'stayAnonymous':
             unset($_SESSION['_anonym_pid']);
             unset($_SESSION['incomingUrl']);
             unset($_SESSION['_newProject']);
             break;
         case 'ping':
             $db = Database::obtain();
             $db->query("SELECT 1");
             $this->result['data'] = array("OK", time());
             break;
         case 'checkTMKey':
             //get MyMemory apiKey service
             $tmxHandler = new TMSService();
             $tmxHandler->setTmKey($this->__postInput['tm_key']);
             //validate the key
             try {
                 $keyExists = $tmxHandler->checkCorrectKey();
             } catch (Exception $e) {
                 /* PROVIDED KEY IS NOT VALID OR WRONG, $keyExists IS NOT SET */
                 Log::doLog($e->getMessage());
             }
             if (!isset($keyExists) || $keyExists === false) {
                 $this->result['errors'][] = array("code" => -9, "message" => "TM key is not valid.");
                 Log::doLog(__METHOD__ . " -> TM key is not valid.");
                 $this->result['success'] = false;
             } else {
                 $this->result['errors'] = array();
                 $this->result['success'] = true;
             }
             break;
     }
 }
Example #2
0
 /**
  * When Called it perform the controller action to retrieve/manipulate data
  *
  * @return mixed
  */
 function doAction()
 {
     if (count($this->errors) > 0) {
         return null;
     }
     //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->jobID, $this->jobPass);
     $pCheck = new AjaxPasswordCheck();
     //check for Password correctness
     if (empty($jobData) || !$pCheck->grantJobAccessByJobData($jobData, $this->jobPass)) {
         $msg = "Error : wrong password provided for download \n\n " . var_export($_POST, true) . "\n";
         Log::doLog($msg);
         Utils::sendErrMailReport($msg);
         return null;
     }
     $projectData = getProject($jobData['id_project']);
     $source = $jobData['source'];
     $target = $jobData['target'];
     $tmsService = new TMSService();
     /**
      * @var $tmx SplTempFileObject
      */
     $this->tmx = $tmsService->exportJobAsTMX($this->jobID, $this->jobPass, $source, $target);
     $this->fileName = $projectData[0]['name'] . "-" . $this->jobID . ".tmx";
 }
Example #3
0
 /**
  * When Called it perform the controller action to retrieve/manipulate data
  *
  * @return void
  */
 function doAction()
 {
     //if some error occured, stop execution.
     if (count(@$this->result['errors'])) {
         return;
     }
     try {
         $tmService = new TMSService();
         $tmService->setTmKey($this->key);
         //validate the key
         try {
             $keyExists = $tmService->checkCorrectKey();
         } catch (Exception $e) {
             /* PROVIDED KEY IS NOT VALID OR WRONG, $keyExists IS NOT SET */
             Log::doLog($e->getMessage());
         }
         if (!isset($keyExists) || $keyExists === false) {
             Log::doLog(__METHOD__ . " -> TM key is not valid.");
             throw new Exception("TM key is not valid.", -4);
         }
         $tmKeyStruct = new TmKeyManagement_TmKeyStruct();
         $tmKeyStruct->key = $this->key;
         $tmKeyStruct->name = $this->description;
         $tmKeyStruct->tm = true;
         $tmKeyStruct->glos = true;
         $mkDao = new TmKeyManagement_MemoryKeyDao(Database::obtain());
         $memoryKeyToUpdate = new TmKeyManagement_MemoryKeyStruct();
         $memoryKeyToUpdate->uid = $this->uid;
         $memoryKeyToUpdate->tm_key = $tmKeyStruct;
         switch ($this->exec) {
             case 'delete':
                 $userMemoryKeys = $mkDao->disable($memoryKeyToUpdate);
                 break;
             case 'update':
                 $userMemoryKeys = $mkDao->update($memoryKeyToUpdate);
                 break;
             case 'newKey':
                 $userMemoryKeys = $mkDao->create($memoryKeyToUpdate);
                 break;
             default:
                 throw new Exception("Unexpected Exception", -4);
         }
         if (!$userMemoryKeys) {
             throw new Exception("This key wasn't found in your keyring.", -3);
         }
     } catch (Exception $e) {
         $this->result['data'] = 'KO';
         $this->result['errors'][] = array("code" => $e->getCode(), "message" => $e->getMessage());
     }
 }
 public function doAction()
 {
     switch ($this->__postInput['exec']) {
         case 'stayAnonymous':
             unset($_SESSION['_anonym_pid']);
             unset($_SESSION['incomingUrl']);
             unset($_SESSION['_newProject']);
             break;
         case 'ping':
             $db = Database::obtain();
             $db->query("SELECT 1");
             $this->result['data'] = array("OK", time());
             break;
         case 'checkTMKey':
             //get MyMemory apiKey service
             $tmxHandler = new TMSService();
             $tmxHandler->setTmKey($this->__postInput['tm_key']);
             //validate the key
             try {
                 $keyExists = $tmxHandler->checkCorrectKey();
             } catch (Exception $e) {
                 /* PROVIDED KEY IS NOT VALID OR WRONG, $keyExists IS NOT SET */
                 Log::doLog($e->getMessage());
             }
             if (!isset($keyExists) || $keyExists === false) {
                 $this->result['errors'][] = array("code" => -9, "message" => "TM key is not valid.");
                 Log::doLog(__METHOD__ . " -> TM key is not valid.");
                 $this->result['success'] = false;
             } else {
                 $this->result['errors'] = array();
                 $this->result['success'] = true;
             }
             break;
         case 'clearNotCompletedUploads':
             try {
                 Utils::deleteDir(INIT::$UPLOAD_REPOSITORY . '/' . $_COOKIE['upload_session'] . '/');
             } catch (Exception $e) {
                 Log::doLog("ajaxUtils::clearNotCompletedUploads : " . $e->getMessage());
             }
             setcookie("upload_session", null, -1, '/');
             unset($_COOKIE['upload_session']);
             break;
     }
 }
 /**
  * When Called it perform the controller action to retrieve/manipulate data
  *
  * @return mixed
  */
 function doAction()
 {
     try {
         $this->streamFilePointer = $this->tmxHandler->downloadTMX();
     } catch (Exception $e) {
         $r = "<pre>";
         $r .= print_r("User Email: " . $this->userMail, true);
         $r .= print_r("User ID: " . $this->uid, true);
         $r .= print_r($e->getMessage(), true);
         $r .= print_r($e->getTraceAsString(), true);
         $r .= "\n\n";
         $r .= " - REQUEST URI: " . print_r(@$_SERVER['REQUEST_URI'], true) . "\n";
         $r .= " - REQUEST Message: " . print_r($_REQUEST, true) . "\n";
         $r .= "\n\n\n";
         $r .= "</pre>";
         Log::$fileName = 'php_errors.txt';
         Log::doLog($r);
         Utils::sendErrMailReport($r, "Download TMX Error: " . $e->getMessage());
         $this->unlockToken();
         echo $e->getMessage();
         exit;
     }
 }
 public function doAction()
 {
     //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;
     }
     //get storage object
     $fs = new FilesStorage();
     $files_job = $fs->getFilesForJob($this->id_job, $this->id_file);
     $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
     */
     // This array will contain all the files of $files_job split by
     // converter version.
     $files_job_by_converter_version = array();
     // Detect the converter's version to use for each file, then store
     // file info accordingly.
     foreach ($files_job as $file) {
         $fileType = DetectProprietaryXliff::getInfo($file['xliffFilePath']);
         $files_job_by_converter_version[$fileType['converter_version']][] = $file;
     }
     // Process files according to the converters' versions, one version
     // at a time
     foreach ($files_job_by_converter_version as $converter_version => $files_job) {
         //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($converter_version);
             $files_to_be_converted = 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);
                 }
                 $data = getSegmentsDownload($this->id_job, $this->password, $fileID, $nonew);
                 //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;
                     }
                 }
                 //instatiate parser
                 $xsp = new SdlXliffSAXTranslationReplacer($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);
                 $output_content[$fileID]['document_content'] = file_get_contents($outputPath);
                 $output_content[$fileID]['output_filename'] = $current_filename;
                 $fileType = DetectProprietaryXliff::getInfo($file['xliffFilePath']);
                 if ($this->forceXliff) {
                     //clean the output filename by removing
                     // the unique hash identifier 55e5739b467109.05614837_.out.Test_English.doc.sdlxliff
                     $output_content[$fileID]['output_filename'] = preg_replace('#[0-9a-f]+\\.[0-9_]+\\.out\\.#i', '', FilesStorage::basename_fix($outputPath));
                     if ($fileType['proprietary_short_name'] === 'matecat_converter') {
                         // Set the XLIFF extension to .xlf
                         // Internally, MateCat continues using .sdlxliff as default
                         // extension for the XLIFF behind the projects.
                         // Changing this behavior requires a huge refactoring that
                         // it's scheduled for future versions.
                         // We quickly fixed the behaviour from the user standpoint
                         // using the following line of code, that changes the XLIFF's
                         // extension just a moment before it is downloaded by the user.
                         $output_content[$fileID]['output_filename'] = preg_replace("|\\.sdlxliff\$|i", ".xlf", $output_content[$fileID]['output_filename']);
                     }
                 }
                 /**
                  * Conversion Enforce
                  */
                 $convertBackToOriginal = true;
                 //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']);
                 // When the 'proprietary' flag is set to false, the xliff
                 // is not passed to any converter, because is handled
                 // directly inside MateCAT.
                 $xliffWasNotConverted = $fileType['proprietary'] === false;
                 if (!INIT::$CONVERSION_ENABLED || ($file['originalFilePath'] == $file['xliffFilePath'] and $xliffWasNotConverted) 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));
                 }
                 if ($convertBackToOriginal) {
                     $output_content[$fileID]['out_xliff_name'] = $outputPath;
                     $output_content[$fileID]['source'] = $jobData['source'];
                     $output_content[$fileID]['target'] = $jobData['target'];
                     $files_to_be_converted[$fileID] = $output_content[$fileID];
                 } elseif ($this->forceXliff) {
                     $this->cleanFilePath($output_content[$fileID]['document_content']);
                 }
             }
             $convertResult = $converter->multiConvertToOriginal($files_to_be_converted, $chosen_machine = false);
             foreach (array_keys($files_to_be_converted) as $fileID) {
                 $output_content[$fileID]['document_content'] = $this->ifGlobalSightXliffRemoveTargetMarks($convertResult[$fileID]['document_content'], $files_to_be_converted[$fileID]['output_filename']);
                 //in case of .strings, they are required to be in UTF-16
                 //get extension to perform file detection
                 $extension = FilesStorage::pathinfo_fix($output_content[$fileID]['output_filename'], PATHINFO_EXTENSION);
                 if (strtoupper($extension) == 'STRINGS') {
                     //use this function to convert stuff
                     $encodingConvertedFile = CatUtils::convertEncoding('UTF-16', $output_content[$fileID]['document_content']);
                     //strip previously added BOM
                     $encodingConvertedFile[1] = $converter->stripBOM($encodingConvertedFile[1], 16);
                     //store new content
                     $output_content[$fileID]['document_content'] = $encodingConvertedFile[1];
                     //trash temporary data
                     unset($encodingConvertedFile);
                 }
             }
             unset($convertResult);
         }
     }
     foreach ($output_content as $idFile => $fileInformations) {
         $zipPathInfo = ZipArchiveExtended::zipPathInfo($output_content[$idFile]['output_filename']);
         if (is_array($zipPathInfo)) {
             $output_content[$idFile]['zipfilename'] = $zipPathInfo['zipfilename'];
             $output_content[$idFile]['zipinternalPath'] = $zipPathInfo['dirname'];
             $output_content[$idFile]['output_filename'] = $zipPathInfo['basename'];
         }
     }
     //set the file Name
     $pathinfo = FilesStorage::pathinfo_fix($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('document_content' => '', 'output_filename' => $pathinfo['filename'] . "_" . $jobData['target'] . "_TM . tmx");
         foreach ($tmFile as $lineNumber => $content) {
             $output_content[$tm_id]['document_content'] .= $content;
         }
         $output_content[$mt_id] = array('document_content' => '', 'output_filename' => $pathinfo['filename'] . "_" . $jobData['target'] . "_MT . tmx");
         foreach ($mtFile as $lineNumber => $content) {
             $output_content[$mt_id]['document_content'] .= $content;
         }
         $this->createOmegaTZip($output_content, $jobData['source'], $jobData['target']);
         //add zip archive content here;
     } else {
         try {
             $output_content = $this->getOutputContentsWithZipFiles($output_content);
             if (count($output_content) > 1) {
                 //cast $output_content elements to ZipContentObject
                 foreach ($output_content as $key => $__output_content_elem) {
                     $output_content[$key] = new ZipContentObject($__output_content_elem);
                 }
                 if ($pathinfo['extension'] != 'zip') {
                     if ($this->forceXliff) {
                         $this->_filename = $this->id_job . ".zip";
                     } else {
                         $this->_filename = $pathinfo['basename'] . ".zip";
                     }
                 }
                 $this->content = self::composeZip($output_content);
                 //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);
             }
         } catch (Exception $e) {
             $msg = "\n\n Error retrieving file content, Conversion failed??? \n\n Error: {$e->getMessage()} \n\n" . var_export($e->getTraceAsString(), true);
             $msg .= "\n\n Request: " . var_export($_REQUEST, true);
             Log::$fileName = 'fatal_errors.txt';
             Log::doLog($msg);
             Utils::sendErrMailReport($msg);
             $this->unlockToken(array("code" => -110, "message" => "Download failed. Please contact " . INIT::$SUPPORT_MAIL));
             throw $e;
             // avoid sent Headers and empty file content with finalize method
         }
     }
     try {
         Utils::deleteDir(INIT::$TMP_DOWNLOAD . '/' . $this->id_job . '/');
     } catch (Exception $e) {
         Log::doLog('Failed to delete dir:' . $e->getMessage());
     }
 }
 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();
     $files_job = getFilesForJob($this->id_job, $this->id_file);
     $debug['get_file'][] = time();
     $nonew = 0;
     $output_content = array();
     /*
      * the procedure is now as follows:
      * 1)original file is loaded from DB into RAM and the flushed in a temp file on disk; a file handler is obtained
      * 2)RAM gets freed from original content
      * 3)the file is read chunk by chunk by a stream parser: for each tran-unit that is encountered,
      *     target is replaced (or added) with the corresponding translation among segments
      *     the current string in the buffer is flushed on standard output
      * 4)the temporary file is deleted by another process after some time
      *
      */
     //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'];
             $original_xliff = $file['xliff_file'];
             //get path
             $path = INIT::$TMP_DOWNLOAD . '/' . $this->id_job . '/' . $fileID . '/' . $current_filename . "_" . uniqid('', true) . '.sdlxliff';
             //make dir if doesn't exist
             if (!file_exists(dirname($path))) {
                 Log::doLog('exec ("chmod 666 ' . escapeshellarg($path) . '");');
                 mkdir(dirname($path), 0777, true);
                 exec("chmod 666 " . escapeshellarg($path));
             }
             //create file
             $fp = fopen($path, 'w+');
             //flush file to disk
             fwrite($fp, $original_xliff);
             //free memory, as we can work with file on disk now
             unset($original_xliff);
             $debug['get_segments'][] = time();
             $data = getSegmentsDownload($this->id_job, $this->password, $fileID, $nonew);
             $debug['get_segments'][] = time();
             //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
             //clean also not valid xml entities ( charactes with ascii < 32 and different from 0A, 0D and 09
             $regexpEntity = '/&#x(0[0-8BCEF]|1[0-9A-F]|7F);/u';
             //remove binary chars in some xliff files
             $regexpAscii = '/([\\x{00}-\\x{1F}\\x{7F}]{1})/u';
             foreach ($data as $i => $k) {
                 $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']);
                 $sanitized_src = preg_replace($regexpAscii, '', $data[$i]['segment']);
                 $sanitized_trg = preg_replace($regexpAscii, '', $data[$i]['translation']);
                 $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($path, $data, Langs_Languages::getInstance()->getLangRegionCode($jobData['target']), $fp);
             if ($this->download_type == 'omegat') {
                 $xsp->setSourceInTarget(true);
             }
             //run parsing
             Log::doLog("work on " . $fileID . " " . $current_filename);
             $xsp->replaceTranslation();
             fclose($fp);
             unset($xsp);
             $debug['replace'][] = time();
             $output_xliff = file_get_contents($path . '.out.sdlxliff');
             $output_content[$fileID]['documentContent'] = $output_xliff;
             $output_content[$fileID]['filename'] = $current_filename;
             unset($output_xliff);
             if ($this->forceXliff) {
                 $file_info_details = pathinfo($output_content[$fileID]['filename']);
                 $output_content[$fileID]['filename'] = $file_info_details['filename'] . ".out.sdlxliff";
             }
             //TODO set a flag in database when file uploaded to know if this file is a proprietary xlf converted
             //TODO so we can load from database the original file blob ONLY when needed
             /**
              * Conversion Enforce
              */
             $convertBackToOriginal = true;
             try {
                 //if it is a not converted file ( sdlxliff ) we have an empty field original_file
                 //so we can simplify all the logic with:
                 // is empty original_file? if it is, we don't need conversion back because
                 // we already have an sdlxliff or an accepted file
                 $file['original_file'] = @gzinflate($file['original_file']);
                 if (!INIT::$CONVERSION_ENABLED || empty($file['original_file']) && $mime_type == 'sdlxliff' || $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'] = $path . '.out.sdlxliff';
                 $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->removeTargetMarks($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 . '/');
 }
Example #8
0
 public function __construct()
 {
     //limit execution time to 300 seconds
     set_time_limit(300);
     parent::__construct();
     //force client to close connection, avoid UPLOAD_ERR_PARTIAL for keep-alive connections
     header("Connection: close");
     $filterArgs = array('project_name' => array('filter' => FILTER_SANITIZE_STRING, 'flags' => FILTER_FLAG_STRIP_LOW), 'source_lang' => array('filter' => FILTER_SANITIZE_STRING, 'flags' => FILTER_FLAG_STRIP_LOW), 'target_lang' => array('filter' => FILTER_SANITIZE_STRING, 'flags' => FILTER_FLAG_STRIP_LOW), 'tms_engine' => array('filter' => FILTER_VALIDATE_INT, 'flags' => FILTER_REQUIRE_SCALAR, 'options' => array('default' => 1, 'min_range' => 0)), 'mt_engine' => array('filter' => FILTER_VALIDATE_INT, 'flags' => FILTER_REQUIRE_SCALAR, 'options' => array('default' => 1, 'min_range' => 0)), 'private_tm_key' => array('filter' => FILTER_SANITIZE_STRING, 'flags' => FILTER_FLAG_STRIP_LOW));
     $__postInput = filter_input_array(INPUT_POST, $filterArgs);
     if (!isset($__postInput['tms_engine']) || is_null($__postInput['tms_engine'])) {
         $__postInput['tms_engine'] = 1;
     }
     if (!isset($__postInput['mt_engine']) || is_null($__postInput['mt_engine'])) {
         $__postInput['mt_engine'] = 1;
     }
     foreach ($__postInput as $key => $val) {
         $__postInput[$key] = urldecode($val);
     }
     //NOTE: This is for debug purpose only,
     //NOTE: Global $_POST Overriding from CLI
     //$__postInput = filter_var_array( $_POST, $filterArgs );
     $this->project_name = $__postInput['project_name'];
     $this->source_lang = $__postInput['source_lang'];
     $this->target_lang = $__postInput['target_lang'];
     $this->tms_engine = $__postInput['tms_engine'];
     // Default 1 MyMemory
     $this->mt_engine = $__postInput['mt_engine'];
     // Default 1 MyMemory
     $this->private_tm_key = $__postInput['private_tm_key'];
     try {
         if ($this->tms_engine != 0) {
             $test_valid_TMS = Engine::getInstance($this->tms_engine);
         }
         if ($this->mt_engine != 0 && $this->mt_engine != 1) {
             $test_valid_MT = Engine::getInstance($this->mt_engine);
         }
     } catch (Exception $ex) {
         $this->api_output['message'] = $ex->getMessage();
         Log::doLog($ex->getMessage());
         return -1;
     }
     //from api a key is sent and the value is 'new'
     if ($this->private_tm_key == 'new') {
         try {
             $APIKeySrv = new TMSService();
             $newUser = $APIKeySrv->createMyMemoryKey();
             $this->private_tm_user = $newUser->id;
             $this->private_tm_pass = $newUser->pass;
             $this->private_tm_key = array(array('key' => $newUser->key, 'name' => null, 'r' => true, 'w' => true));
         } catch (Exception $e) {
             $this->api_output['message'] = 'Project Creation Failure';
             $this->api_output['debug'] = array("code" => $e->getCode(), "message" => $e->getMessage());
             return -1;
         }
     } else {
         //if a string is sent, transform it into a valid array
         if (!empty($this->private_tm_key)) {
             $this->private_tm_key = array(array('key' => $this->private_tm_key, 'name' => null, 'r' => true, 'w' => true));
         } else {
             $this->private_tm_key = array();
         }
     }
     //This is only an element, this seems redundant,
     // but if we need more than a key in the next api version we can easily handle them here
     $this->private_tm_key = array_filter($this->private_tm_key, array("self", "sanitizeTmKeyArr"));
     if (empty($_FILES)) {
         $this->result['errors'][] = array("code" => -1, "message" => "Missing file. Not Sent.");
         return -1;
     }
 }
Example #9
0
 public function __construct()
 {
     //limit execution time to 300 seconds
     set_time_limit(300);
     parent::__construct();
     //force client to close connection, avoid UPLOAD_ERR_PARTIAL for keep-alive connections
     header("Connection: close");
     $filterArgs = array('project_name' => array('filter' => FILTER_SANITIZE_STRING, 'flags' => FILTER_FLAG_STRIP_LOW), 'source_lang' => array('filter' => FILTER_SANITIZE_STRING, 'flags' => FILTER_FLAG_STRIP_LOW), 'target_lang' => array('filter' => FILTER_SANITIZE_STRING, 'flags' => FILTER_FLAG_STRIP_LOW), 'tms_engine' => array('filter' => FILTER_VALIDATE_INT, 'flags' => FILTER_REQUIRE_SCALAR, 'options' => array('default' => 1, 'min_range' => 0)), 'mt_engine' => array('filter' => FILTER_VALIDATE_INT, 'flags' => FILTER_REQUIRE_SCALAR, 'options' => array('default' => 1, 'min_range' => 0)), 'private_tm_key' => array('filter' => FILTER_SANITIZE_STRING, 'flags' => FILTER_FLAG_STRIP_LOW), 'subject' => array('filter' => FILTER_SANITIZE_STRING, 'flags' => FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH), 'segmentation_rule' => array('filter' => FILTER_SANITIZE_STRING, 'flags' => FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH), 'owner_email' => array('filter' => FILTER_VALIDATE_EMAIL));
     $__postInput = filter_input_array(INPUT_POST, $filterArgs);
     if (!isset($__postInput['tms_engine']) || is_null($__postInput['tms_engine'])) {
         $__postInput['tms_engine'] = 1;
     }
     if (!isset($__postInput['mt_engine']) || is_null($__postInput['mt_engine'])) {
         $__postInput['mt_engine'] = 1;
     }
     foreach ($__postInput as $key => $val) {
         $__postInput[$key] = urldecode($val);
     }
     //NOTE: This is for debug purpose only,
     //NOTE: Global $_POST Overriding from CLI
     //$__postInput = filter_var_array( $_POST, $filterArgs );
     $this->project_name = $__postInput['project_name'];
     $this->source_lang = $__postInput['source_lang'];
     $this->target_lang = $__postInput['target_lang'];
     $this->tms_engine = $__postInput['tms_engine'];
     // Default 1 MyMemory
     $this->mt_engine = $__postInput['mt_engine'];
     // Default 1 MyMemory
     $this->seg_rule = !empty($__postInput['segmentation_rule']) ? $__postInput['segmentation_rule'] : '';
     $this->subject = !empty($__postInput['subject']) ? $__postInput['subject'] : 'general';
     $this->owner = $__postInput['owner_email'];
     try {
         $this->private_tm_key = array_map('self::parseTmKeyInput', explode(",", $__postInput['private_tm_key']));
     } catch (Exception $e) {
         $this->api_output['message'] = $e->getMessage();
         $this->api_output['debug'] = $e->getMessage();
         Log::doLog($e->getMessage());
         return -6;
     }
     if ($this->owner === false) {
         $this->api_output['message'] = "Project Creation Failure";
         $this->api_output['debug'] = "Email is not valid";
         Log::doLog("Email is not valid");
         return -5;
     } else {
         if (!is_null($this->owner) && !empty($this->owner)) {
             $domain = explode("@", $this->owner);
             $domain = $domain[1];
             if (!checkdnsrr($domain)) {
                 $this->api_output['message'] = "Project Creation Failure";
                 $this->api_output['debug'] = "Email is not valid";
                 Log::doLog("Email is not valid");
                 return -5;
             }
         }
     }
     try {
         $this->validateEngines();
     } catch (Exception $ex) {
         $this->api_output['message'] = $ex->getMessage();
         Log::doLog($ex->getMessage());
         return -1;
     }
     if (count($this->private_tm_key) > self::MAX_NUM_KEYS) {
         $this->api_output['message'] = "Project Creation Failure";
         $this->api_output['debug'] = "Too much keys provided. Max number of keys is " . self::MAX_NUM_KEYS;
         Log::doLog("Too much keys provided. Max number of keys is " . self::MAX_NUM_KEYS);
         return -2;
     }
     $langDomains = Langs_LanguageDomains::getInstance();
     $subjectList = $langDomains::getEnabledDomains();
     // In this list there is an item whose key is "----".
     // It is useful for UI purposes, but not here. So we unset it
     foreach ($subjectList as $idx => $subject) {
         if ($subject['key'] == '----') {
             unset($subjectList[$idx]);
             break;
         }
     }
     //Array_column() is not supported on PHP 5.4, so i'll rewrite it
     if (!function_exists('array_column')) {
         $subjectList = Utils::array_column($subjectList, 'key');
     } else {
         $subjectList = array_column($subjectList, 'key');
     }
     if (!in_array($this->subject, $subjectList)) {
         $this->api_output['message'] = "Project Creation Failure";
         $this->api_output['debug'] = "Subject not allowed: " . $this->subject;
         Log::doLog("Subject not allowed: " . $this->subject);
         return -3;
     }
     if (!in_array($this->seg_rule, self::$allowed_seg_rules)) {
         $this->api_output['message'] = "Project Creation Failure";
         $this->api_output['debug'] = "Segmentation rule not allowed: " . $this->seg_rule;
         Log::doLog("Segmentation rule not allowed: " . $this->seg_rule);
         return -4;
     }
     //normalize segmentation rule to what it's used internally
     if ($this->seg_rule == 'standard' || $this->seg_rule == '') {
         $this->seg_rule = null;
     }
     if (empty($_FILES)) {
         $this->result['errors'][] = array("code" => -1, "message" => "Missing file. Not Sent.");
         return -1;
     }
     $this->private_tm_key = array_values(array_filter($this->private_tm_key));
     //If a TMX file has been uploaded and no key was provided, create a new key.
     if (empty($this->private_tm_key)) {
         foreach ($_FILES as $_fileinfo) {
             $pathinfo = FilesStorage::pathinfo_fix($_fileinfo['name']);
             if ($pathinfo['extension'] == 'tmx') {
                 $this->private_tm_key[] = 'new';
                 break;
             }
         }
     }
     //remove all empty entries
     foreach ($this->private_tm_key as $__key_idx => $tm_key) {
         //from api a key is sent and the value is 'new'
         if ($tm_key['key'] == 'new') {
             try {
                 $APIKeySrv = new TMSService();
                 $newUser = $APIKeySrv->createMyMemoryKey();
                 //TODO: i need to store an array of these
                 $this->private_tm_user = $newUser->id;
                 $this->private_tm_pass = $newUser->pass;
                 $this->private_tm_key[$__key_idx] = array('key' => $newUser->key, 'name' => null, 'r' => $tm_key['r'], 'w' => $tm_key['w']);
                 $this->new_keys[] = $newUser->key;
             } catch (Exception $e) {
                 $this->api_output['message'] = 'Project Creation Failure';
                 $this->api_output['debug'] = array("code" => $e->getCode(), "message" => $e->getMessage());
                 return -1;
             }
         } else {
             if (!empty($tm_key)) {
                 $this->private_tm_key[$__key_idx] = array('key' => $tm_key['key'], 'name' => null, 'r' => $tm_key['r'], 'w' => $tm_key['w']);
             }
         }
         $this->private_tm_key[$__key_idx] = array_filter($this->private_tm_key[$__key_idx], array("self", "sanitizeTmKeyArr"));
     }
 }
Example #10
0
 protected function _set($config)
 {
     $this->result['errors'] = array();
     $tm_keys = $this->job_info['tm_keys'];
     if (self::isRevision()) {
         $this->userRole = TmKeyManagement_Filter::ROLE_REVISOR;
     }
     //get TM keys with read grants
     $tm_keys = TmKeyManagement_TmKeyManagement::getJobTmKeys($tm_keys, 'w', 'glos', $this->uid, $this->userRole);
     if (empty($tm_keys)) {
         $APIKeySrv = new TMSService();
         $newUser = (object) $APIKeySrv->createMyMemoryKey();
         //throws exception
         //TODO take only for hystorical reason
         updateTranslatorJob($this->id_job, $newUser);
         //fallback
         $config['id_user'] = $newUser->id;
         $new_key = TmKeyManagement_TmKeyManagement::getTmKeyStructure();
         $new_key->tm = 1;
         $new_key->glos = 1;
         $new_key->key = $newUser->key;
         $new_key->owner = $this->userMail == $this->job_info['owner'];
         if (!$new_key->owner) {
             $new_key->{TmKeyManagement_Filter::$GRANTS_MAP[$this->userRole]['r']} = 1;
             $new_key->{TmKeyManagement_Filter::$GRANTS_MAP[$this->userRole]['w']} = 1;
         } else {
             $new_key->r = 1;
             $new_key->w = 1;
         }
         if ($new_key->owner) {
             //do nothing, this is a greedy if
         } elseif ($this->userRole == TmKeyManagement_Filter::ROLE_TRANSLATOR) {
             $new_key->uid_transl = $this->uid;
         } elseif ($this->userRole == TmKeyManagement_Filter::ROLE_REVISOR) {
             $new_key->uid_rev = $this->uid;
         }
         //create an empty array
         $tm_keys = array();
         //append new key
         $tm_keys[] = $new_key;
         //put the key in the job
         TmKeyManagement_TmKeyManagement::setJobTmKeys($this->id_job, $this->password, $tm_keys);
         //put the key in the user keiring
         if ($this->userIsLogged) {
             $newMemoryKey = new TmKeyManagement_MemoryKeyStruct();
             $newMemoryKey->tm_key = $new_key;
             $newMemoryKey->uid = $this->uid;
             $mkDao = new TmKeyManagement_MemoryKeyDao(Database::obtain());
             $mkDao->create($newMemoryKey);
         }
     }
     $config['segment'] = CatUtils::view2rawxliff($config['segment']);
     $config['translation'] = CatUtils::view2rawxliff($config['translation']);
     $config['prop'] = json_encode(CatUtils::getTMProps($this->job_info));
     //prepare the error report
     $set_code = array();
     //set the glossary entry for each key with write grants
     if (count($tm_keys)) {
         /**
          * @var $tm_keys TmKeyManagement_TmKeyStruct[]
          */
         foreach ($tm_keys as $tm_key) {
             $config['id_user'] = $tm_key->key;
             $TMS_RESULT = $this->_TMS->set($config);
             $set_code[] = $TMS_RESULT;
         }
     }
     $set_successful = true;
     if (array_search(false, $set_code, true)) {
         //There's an error, for now skip, let's assume that are not errors
         $set_successful = false;
     }
     if ($set_successful) {
         //          Often the get method after a set is not in real time, so return the same values ( FAKE )
         //          $TMS_GET_RESULT = $this->_TMS->get($config)->get_glossary_matches_as_array();
         //          $this->result['data']['matches'] = $TMS_GET_RESULT;
         $this->result['data']['matches'] = array($config['segment'] => array(array('segment' => $config['segment'], 'translation' => $config['translation'], 'last_update_date' => date_create()->format('Y-m-d H:i:m'), 'last_updated_by' => "Matecat user", 'created_by' => "Matecat user", 'target_note' => $config['tnote'])));
         if (isset($new_key)) {
             $this->result['data']['created_tm_key'] = true;
         }
     } else {
         $this->result['errors'][] = array("code" => -1, "message" => "We got an error, please try again.");
     }
 }