/** * 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()); } }
/** * Merge the keys from CLIENT with those from DATABASE ( jobData ) * * @param string $Json_clientKeys A json_encoded array of objects having the following structure:<br /> * <pre> * array( * 'key' => <private_tm_key>, * 'name' => <tm_name>, * 'r' => true, * 'w' => true * ) * </pre> * @param string $Json_jobKeys A json_encoded array of TmKeyManagement_TmKeyStruct objects * @param string $userRole One of the following strings: "owner", "translator", "revisor" * @param int $uid * * @see TmKeyManagement_TmKeyStruct * * @return array TmKeyManagement_TmKeyStruct[] * * @throws Exception */ public static function mergeJsonKeys($Json_clientKeys, $Json_jobKeys, $userRole = TmKeyManagement_Filter::ROLE_TRANSLATOR, $uid = null) { //we put the already present job keys so they can be checked against the client keys when cycle advances //( jobs has more elements than the client objects ) $clientDecodedJson = json_decode($Json_clientKeys, true); Utils::raiseJsonExceptionError(); $serverDecodedJson = json_decode($Json_jobKeys, true); Utils::raiseJsonExceptionError(); if (!array_key_exists($userRole, TmKeyManagement_Filter::$GRANTS_MAP)) { throw new Exception("Invalid Role Type string.", 4); } $client_tm_keys = array_map(array('self', 'getTmKeyStructure'), $clientDecodedJson); $client_tm_keys = array_map(array('self', 'sanitize'), $client_tm_keys); $job_tm_keys = array_map(array('self', 'getTmKeyStructure'), $serverDecodedJson); $server_reorder_position = array(); $reverse_lookup_client_json = array('pos' => array(), 'elements' => array(), 'unique' => array()); foreach ($client_tm_keys as $_j => $_client_tm_key) { /** * @var $_client_tm_key TmKeyManagement_TmKeyStruct */ //create a reverse lookup $reverse_lookup_client_json['pos'][$_j] = $_client_tm_key->key; $reverse_lookup_client_json['elements'][$_j] = $_client_tm_key; $reverse_lookup_client_json['unique'][$_j] = $_client_tm_key->getCrypt(); if (empty($_client_tm_key->r) && empty($_client_tm_key->w)) { throw new Exception("Please, select Lookup and/or Update to activate your TM in this project", 4); } if (empty($_client_tm_key->key)) { throw new Exception("Invalid Key Provided", 5); } } $uniq_num = count(array_unique($reverse_lookup_client_json['unique'])); if ($uniq_num != count($reverse_lookup_client_json['pos'])) { throw new Exception("A key is already present in this project.", 5); } //update existing job keys foreach ($job_tm_keys as $i => $_job_Key) { /** * @var $_job_Key TmKeyManagement_TmKeyStruct */ $_index_position = array_search($_job_Key->key, $reverse_lookup_client_json['pos']); if (array_search($_job_Key->getCrypt(), $reverse_lookup_client_json['pos']) !== false) { //DO NOTHING //reduce the stack $hashPosition = array_search($_job_Key->getCrypt(), $reverse_lookup_client_json['pos']); unset($reverse_lookup_client_json['pos'][$hashPosition]); unset($reverse_lookup_client_json['elements'][$hashPosition]); //PASS //take the new order $server_reorder_position[$hashPosition] = $_job_Key; } elseif ($_index_position !== false) { // so, here the key exists in client //this is an anonymous user, and a key exists in job if ($_index_position !== false && $uid == null) { //check anonymous user, an anonymous user can not change a not anonymous key if ($userRole == TmKeyManagement_Filter::ROLE_TRANSLATOR) { if ($_job_Key->uid_transl != null) { throw new Exception("Anonymous user can not modify existent keys.", 1); } } elseif ($userRole == TmKeyManagement_Filter::ROLE_REVISOR) { if ($_job_Key->uid_rev != null) { throw new Exception("Anonymous user can not modify existent keys.", 2); } } else { if ($uid == null) { throw new Exception("Anonymous user can not be OWNER", 3); } } } //override the static values $_job_Key->tm = filter_var($reverse_lookup_client_json['elements'][$_index_position]->tm, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); $_job_Key->glos = filter_var($reverse_lookup_client_json['elements'][$_index_position]->glos, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); if ($userRole == TmKeyManagement_Filter::OWNER) { //override grants $_job_Key->r = filter_var($reverse_lookup_client_json['elements'][$_index_position]->r, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); $_job_Key->w = filter_var($reverse_lookup_client_json['elements'][$_index_position]->w, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); } elseif ($userRole == TmKeyManagement_Filter::ROLE_REVISOR || $userRole == TmKeyManagement_Filter::ROLE_TRANSLATOR) { //override role specific grants $_job_Key->{TmKeyManagement_Filter::$GRANTS_MAP[$userRole]['r']} = filter_var($reverse_lookup_client_json['elements'][$_index_position]->r, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); $_job_Key->{TmKeyManagement_Filter::$GRANTS_MAP[$userRole]['w']} = filter_var($reverse_lookup_client_json['elements'][$_index_position]->w, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); } //change name if modified if ($_job_Key->name != $reverse_lookup_client_json['elements'][$_index_position]->name) { $_job_Key->name = $reverse_lookup_client_json['elements'][$_index_position]->name; } //set as owner if it is but should be already set // $_job_Key->owner = ( $userRole == TmKeyManagement_Filter::OWNER ); //reduce the stack unset($reverse_lookup_client_json['pos'][$_index_position]); unset($reverse_lookup_client_json['elements'][$_index_position]); //take the new order $server_reorder_position[$_index_position] = $_job_Key; } else { //the key must be deleted if ($userRole == TmKeyManagement_Filter::OWNER) { //override grants $_job_Key->r = null; $_job_Key->w = null; $_job_Key->owner = false; } elseif ($userRole == TmKeyManagement_Filter::ROLE_REVISOR || $userRole == TmKeyManagement_Filter::ROLE_TRANSLATOR) { //override role specific grants $_job_Key->{TmKeyManagement_Filter::$GRANTS_MAP[$userRole]['r']} = null; $_job_Key->{TmKeyManagement_Filter::$GRANTS_MAP[$userRole]['w']} = null; } //remove the uid property if ($userRole == TmKeyManagement_Filter::ROLE_TRANSLATOR) { $_job_Key->uid_transl = null; } elseif ($userRole == TmKeyManagement_Filter::ROLE_REVISOR) { $_job_Key->uid_rev = null; } //if the key is no more linked to someone, don't add to the resultset, else reorder if it is not an owner key. if ($_job_Key->owner || !is_null($_job_Key->uid_transl) || !is_null($_job_Key->uid_rev)) { if (!$_job_Key->owner) { //take the new order, put the deleted key at the end of the array //a position VERY LOW ( 1 Million ) $server_reorder_position[1000000 + $i] = $_job_Key; } else { if ($userRole != TmKeyManagement_Filter::OWNER) { //place on top of the owner keys, preserve the order of owner keys by adding it's normal index position $server_reorder_position[-1000000 + $i] = $_job_Key; } else { // Remove the key!!! //only the owner can remove it's keys } } } } } /* * There are some new keys from client? Add them */ if (!empty($reverse_lookup_client_json['pos'])) { $justCreatedKey = new TmKeyManagement_TmKeyStruct(); foreach ($reverse_lookup_client_json['elements'] as $_pos => $newClientKey) { /** * @var $newClientKey TmKeyManagement_TmKeyStruct */ //set the key value $justCreatedKey->key = $newClientKey->key; //override the static values $justCreatedKey->tm = filter_var($newClientKey->tm, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); $justCreatedKey->glos = filter_var($newClientKey->glos, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); if ($userRole != TmKeyManagement_Filter::OWNER) { $justCreatedKey->{TmKeyManagement_Filter::$GRANTS_MAP[$userRole]['r']} = filter_var($newClientKey->r, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); $justCreatedKey->{TmKeyManagement_Filter::$GRANTS_MAP[$userRole]['w']} = filter_var($newClientKey->w, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); } else { //override grants $justCreatedKey->r = filter_var($newClientKey->r, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); $justCreatedKey->w = filter_var($newClientKey->w, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); } //set the uid property if ($userRole == TmKeyManagement_Filter::ROLE_TRANSLATOR) { $justCreatedKey->uid_transl = $uid; } elseif ($userRole == TmKeyManagement_Filter::ROLE_REVISOR) { $justCreatedKey->uid_rev = $uid; } //choose a name instead of null $justCreatedKey->name = $newClientKey->name; //choose an owner instead of null $justCreatedKey->owner = $userRole == TmKeyManagement_Filter::OWNER; //finally append to the job keys!! //take the new order, put the new key at the end of the array //a position VERY LOW, but BEFORE the deleted keys, so it goes not to the end ( 100 hundred thousand ) $server_reorder_position[100000 + $_pos] = $justCreatedKey; if ($uid != null) { //if uid is provided, check for key and try to add to it's memory key ring try { /* * Take the keys of the user */ $_keyDao = new TmKeyManagement_MemoryKeyDao(Database::obtain()); $dh = new TmKeyManagement_MemoryKeyStruct(array('uid' => $uid, 'tm_key' => new TmKeyManagement_TmKeyStruct(array('key' => $justCreatedKey->key)))); $keyList = $_keyDao->read($dh); if (empty($keyList)) { // add the key to a new row struct $dh->uid = $uid; $dh->tm_key = $justCreatedKey; $_keyDao->create($dh); } } catch (Exception $e) { Log::doLog($e->getMessage()); } } } } ksort($server_reorder_position, SORT_NUMERIC); return array_values($server_reorder_position); }
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."); } }