Example #1
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 #2
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;
     }
 }
 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.");
     }
 }