Ejemplo n.º 1
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.º 2
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'];
     }
 }