public function doAction() { //get Job Infos $job_data = getJobData($this->jid); $pCheck = new AjaxPasswordCheck(); //check for Password correctness if (!$pCheck->grantJobAccessByJobData($job_data, $this->password)) { $this->result['errors'][] = array("code" => -10, "message" => "wrong password"); return; } $lang_handler = Langs_Languages::getInstance(); if ($this->ref_segment == '') { $this->ref_segment = 0; } $data = getMoreSegments($this->jid, $this->password, $this->step, $this->ref_segment, $this->where); $this->prepareNotes($data); foreach ($data as $i => $seg) { if ($this->where == 'before') { if ((double) $seg['sid'] >= (double) $this->ref_segment) { break; } } if (empty($this->pname)) { $this->pname = $seg['pname']; } if (empty($this->last_opened_segment)) { $this->last_opened_segment = $seg['last_opened_segment']; } if (empty($this->cid)) { $this->cid = $seg['cid']; } if (empty($this->pid)) { $this->pid = $seg['pid']; } if (empty($this->tid)) { $this->tid = $seg['tid']; } if (empty($this->create_date)) { $this->create_date = $seg['create_date']; } if (empty($this->source_code)) { $this->source_code = $seg['source']; } if (empty($this->target_code)) { $this->target_code = $seg['target']; } if (empty($this->source)) { $s = explode("-", $seg['source']); $source = strtoupper($s[0]); $this->source = $source; } if (empty($this->target)) { $t = explode("-", $seg['target']); $target = strtoupper($t[0]); $this->target = $target; } if (empty($this->err)) { $this->err = $seg['serialized_errors_list']; } $id_file = $seg['id_file']; if (!isset($this->data["{$id_file}"])) { $this->data["{$id_file}"]['jid'] = $seg['jid']; $this->data["{$id_file}"]["filename"] = ZipArchiveExtended::getFileName($seg['filename']); $this->data["{$id_file}"]["mime_type"] = $seg['mime_type']; $this->data["{$id_file}"]['source'] = $lang_handler->getLocalizedName($seg['source']); $this->data["{$id_file}"]['target'] = $lang_handler->getLocalizedName($seg['target']); $this->data["{$id_file}"]['source_code'] = $seg['source']; $this->data["{$id_file}"]['target_code'] = $seg['target']; $this->data["{$id_file}"]['segments'] = array(); } unset($seg['id_file']); unset($seg['source']); unset($seg['target']); unset($seg['source_code']); unset($seg['target_code']); unset($seg['mime_type']); unset($seg['filename']); unset($seg['jid']); unset($seg['pid']); unset($seg['cid']); unset($seg['tid']); unset($seg['pname']); unset($seg['create_date']); unset($seg['id_segment_end']); unset($seg['id_segment_start']); unset($seg['serialized_errors_list']); $seg['parsed_time_to_edit'] = CatUtils::parse_time_to_edit($seg['time_to_edit']); $seg['source_chunk_lengths'] === null ? $seg['source_chunk_lengths'] = '[]' : null; $seg['target_chunk_lengths'] === null ? $seg['target_chunk_lengths'] = '{"len":[0],"statuses":["DRAFT"]}' : null; $seg['source_chunk_lengths'] = json_decode($seg['source_chunk_lengths'], true); $seg['target_chunk_lengths'] = json_decode($seg['target_chunk_lengths'], true); $seg['segment'] = CatUtils::rawxliff2view(CatUtils::reApplySegmentSplit($seg['segment'], $seg['source_chunk_lengths'])); $seg['translation'] = CatUtils::rawxliff2view(CatUtils::reApplySegmentSplit($seg['translation'], $seg['target_chunk_lengths']['len'])); $this->attachNotes($seg); $this->data["{$id_file}"]['segments'][] = $seg; } $this->result['data']['files'] = $this->data; $this->result['data']['where'] = $this->where; }
public function doAction() { $project_by_jobs_data = getProjectData($this->pid, $this->ppassword, $this->jid, $this->jpassword); $lang_handler = Langs_Languages::getInstance(); if (empty($project_by_jobs_data)) { $this->project_not_found = true; } //pick the project subject from the first job if (count($project_by_jobs_data) > 0) { $this->subject = $project_by_jobs_data[0]['subject']; } foreach ($project_by_jobs_data as &$p_jdata) { $p_jdata['filename'] = ZipArchiveExtended::getFileName($p_jdata['filename']); //json_decode payable rates $p_jdata['payable_rates'] = json_decode($p_jdata['payable_rates'], true); $this->num_segments += $p_jdata['total_segments']; if (empty($this->pname)) { $this->pname = $p_jdata['name']; } if (empty($this->project_status)) { $this->project_status = $p_jdata['status_analysis']; if ($this->standard_analysis_wc == 0) { $this->standard_analysis_wc = $p_jdata['standard_analysis_wc']; } } //equivalent word count global if ($this->tm_analysis_wc == 0) { $this->tm_analysis_wc = $p_jdata['tm_analysis_wc']; } if ($this->tm_analysis_wc == 0) { $this->tm_analysis_wc = $p_jdata['fast_analysis_wc']; } $this->tm_analysis_wc_print = number_format($this->tm_analysis_wc, 0, ".", ","); if ($this->fast_analysis_wc == 0) { $this->fast_analysis_wc = $p_jdata['fast_analysis_wc']; $this->fast_analysis_wc_print = number_format($this->fast_analysis_wc, 0, ".", ","); } // if zero then print empty instead of 0 if ($this->standard_analysis_wc == 0) { $this->standard_analysis_wc_print = ""; } if ($this->fast_analysis_wc == 0) { $this->fast_analysis_wc_print = ""; } if ($this->tm_analysis_wc == 0) { $this->tm_analysis_wc_print = ""; } $this->total_raw_word_count += $p_jdata['file_raw_word_count']; $source = $lang_handler->getLocalizedName($p_jdata['source']); $target = $lang_handler->getLocalizedName($p_jdata['target']); if (!isset($this->jobs[$p_jdata['jid']])) { if (!isset($this->jobs[$p_jdata['jid']]['splitted'])) { $this->jobs[$p_jdata['jid']]['splitted'] = ''; } $this->jobs[$p_jdata['jid']]['jid'] = $p_jdata['jid']; $this->jobs[$p_jdata['jid']]['source'] = $source; $this->jobs[$p_jdata['jid']]['target'] = $target; } $source_short = $p_jdata['source']; $target_short = $p_jdata['target']; $password = $p_jdata['jpassword']; unset($p_jdata['name']); unset($p_jdata['source']); unset($p_jdata['target']); unset($p_jdata['jpassword']); unset($p_jdata['fast_analysis_wc']); unset($p_jdata['tm_analysis_wc']); unset($p_jdata['standard_analysis_wc']); if (!isset($this->jobs[$p_jdata['jid']]['chunks'][$password])) { $this->jobs[$p_jdata['jid']]['chunks'][$password] = array(); $this->jobs[$p_jdata['jid']]['chunks'][$password]['jid'] = $p_jdata['jid']; $this->jobs[$p_jdata['jid']]['chunks'][$password]['source'] = $source; $this->jobs[$p_jdata['jid']]['chunks'][$password]['target'] = $target; $this->jobs[$p_jdata['jid']]['chunks'][$password]['jpassword'] = $password; $this->jobs[$p_jdata['jid']]['chunks'][$password]['source_short'] = $source_short; $this->jobs[$p_jdata['jid']]['chunks'][$password]['target_short'] = $target_short; $this->jobs[$p_jdata['jid']]['rates'] = $p_jdata['payable_rates']; if (!array_key_exists("total_raw_word_count", $this->jobs[$p_jdata['jid']])) { $this->jobs[$p_jdata['jid']]['chunks'][$password]['total_raw_word_count'] = 0; } if (!array_key_exists("total_eq_word_count", $this->jobs[$p_jdata['jid']])) { $this->jobs[$p_jdata['jid']]['chunks'][$password]['total_eq_word_count'] = 0; } } //calculate total word counts per job (summing different files) $this->jobs[$p_jdata['jid']]['chunks'][$password]['total_raw_word_count'] += $p_jdata['file_raw_word_count']; //format the total (yeah, it's ugly doing it every cycle) $this->jobs[$p_jdata['jid']]['chunks'][$password]['total_raw_word_count_print'] = number_format($this->jobs[$p_jdata['jid']]['chunks'][$password]['total_raw_word_count'], 0, ".", ","); $this->jobs[$p_jdata['jid']]['chunks'][$password]['total_eq_word_count'] += $p_jdata['file_eq_word_count']; $this->jobs[$p_jdata['jid']]['chunks'][$password]['total_eq_word_count_print'] = number_format($this->jobs[$p_jdata['jid']]['chunks'][$password]['total_eq_word_count'], 0, ".", ","); $p_jdata['file_eq_word_count'] = number_format($p_jdata['file_eq_word_count'], 0, ".", ","); $p_jdata['file_raw_word_count'] = number_format($p_jdata['file_raw_word_count'], 0, ".", ","); $this->jobs[$p_jdata['jid']]['chunks'][$password]['files'][$p_jdata['id_file']] = $p_jdata; $this->jobs[$p_jdata['jid']]['splitted'] = count($this->jobs[$p_jdata['jid']]['chunks']) > 1 ? 'splitted' : ''; } $raw_wc_time = $this->total_raw_word_count / INIT::$ANALYSIS_WORDS_PER_DAYS; $tm_wc_time = $this->tm_analysis_wc / INIT::$ANALYSIS_WORDS_PER_DAYS; $fast_wc_time = $this->fast_analysis_wc / INIT::$ANALYSIS_WORDS_PER_DAYS; $raw_wc_unit = 'day'; $tm_wc_unit = 'day'; $fast_wc_unit = 'day'; if ($raw_wc_time > 0 and $raw_wc_time < 1) { $raw_wc_time *= 8; //convert to hours (1 work day = 8 hours) $raw_wc_unit = 'hour'; } if ($raw_wc_time > 0 and $raw_wc_time < 1) { $raw_wc_time *= 60; //convert to minutes $raw_wc_unit = 'minute'; } if ($raw_wc_time > 1) { $raw_wc_unit .= 's'; } if ($tm_wc_time > 0 and $tm_wc_time < 1) { $tm_wc_time *= 8; //convert to hours (1 work day = 8 hours) $tm_wc_unit = 'hour'; } if ($tm_wc_time > 0 and $tm_wc_time < 1) { $tm_wc_time *= 60; //convert to minutes $tm_wc_unit = 'minute'; } if ($tm_wc_time > 1) { $tm_wc_unit .= 's'; } if ($fast_wc_time > 0 and $fast_wc_time < 1) { $fast_wc_time *= 8; //convert to hours (1 work day = 8 hours) $fast_wc_unit = 'hour'; } if ($fast_wc_time > 0 and $fast_wc_time < 1) { $fast_wc_time *= 60; //convert to minutes $fast_wc_unit = 'minute'; } if ($fast_wc_time > 1) { $fast_wc_unit .= 's'; } $this->raw_wc_time = ceil($raw_wc_time); $this->fast_wc_time = ceil($fast_wc_time); $this->tm_wc_time = ceil($tm_wc_time); $this->raw_wc_unit = $raw_wc_unit; $this->tm_wc_unit = $tm_wc_unit; $this->fast_wc_unit = $fast_wc_unit; if ($this->raw_wc_time == 8 and $this->raw_wc_unit == "hours") { $this->raw_wc_time = 1; $this->raw_wc_unit = "day"; } if ($this->raw_wc_time == 60 and $this->raw_wc_unit == "minutes") { $this->raw_wc_time = 1; $this->raw_wc_unit = "hour"; } if ($this->fast_wc_time == 8 and $this->fast_wc_time == "hours") { $this->fast_wc_time = 1; $this->fast_wc_time = "day"; } if ($this->tm_wc_time == 60 and $this->tm_wc_time == "minutes") { $this->tm_wc_time = 1; $this->tm_wc_time = "hour"; } if ($this->total_raw_word_count == 0) { $this->total_raw_word_count_print = ""; } else { $this->total_raw_word_count_print = number_format($this->total_raw_word_count, 0, ".", ","); } // echo "<pre>" . print_r ( $this->jobs, true ) . "</pre>"; exit; }
public function doAction() { $files_found = array(); $lang_handler = Langs_Languages::getInstance(); $data = getSegmentsInfo($this->jid, $this->password); if (empty($data) or $data < 0) { $this->job_not_found = true; //stop execution return; } //retrieve job owner. It will be useful also if the job is archived or cancelled $this->job_owner = $data[0]['job_owner'] != "" ? $data[0]['job_owner'] : "*****@*****.**"; if ($data[0]['status'] == Constants_JobStatus::STATUS_CANCELLED) { $this->job_cancelled = true; //stop execution return; } if ($data[0]['status'] == Constants_JobStatus::STATUS_ARCHIVED) { $this->job_archived = true; //stop execution return; } /* * I prefer to use a programmatic approach to the check for the archive date instead of a pure query * because the query to check "Utils::getArchivableJobs($this->jid)" should be * executed every time a job is loaded ( F5 or CTRL+R on browser ) and it cost some milliseconds ( ~0.1s ) * and it is little heavy for the database. * We use the data we already have from last query and perform * the check on the last translation only if the job is older than 30 days * */ $lastUpdate = new DateTime($data[0]['last_update']); $oneMonthAgo = new DateTime(); $oneMonthAgo->modify('-' . INIT::JOB_ARCHIVABILITY_THRESHOLD . ' days'); if ($lastUpdate < $oneMonthAgo && !$this->job_cancelled) { $lastTranslationInJob = new Datetime(getLastTranslationDate($this->jid)); if ($lastTranslationInJob < $oneMonthAgo) { $res = "job"; $new_status = Constants_JobStatus::STATUS_ARCHIVED; updateJobsStatus($res, $this->jid, $new_status, null, null, $this->password); $this->job_archived = true; } } foreach ($data as $i => $job) { $this->project_status = $job; // get one row values for the project are the same for every row if (empty($this->pname)) { $this->pname = $job['pname']; $this->downloadFileName = $job['pname'] . ".zip"; // will be overwritten below in case of one file job } if (empty($this->last_opened_segment)) { $this->last_opened_segment = $job['last_opened_segment']; } if (empty($this->cid)) { $this->cid = $job['cid']; } if (empty($this->pid)) { $this->pid = $job['pid']; } if (empty($this->create_date)) { $this->create_date = $job['create_date']; } if (empty($this->source_code)) { $this->source_code = $job['source']; } if (empty($this->target_code)) { $this->target_code = $job['target']; } if (empty($this->source)) { $s = explode("-", $job['source']); $source = strtoupper($s[0]); $this->source = $source; $this->source_rtl = $lang_handler->isRTL(strtolower($this->source)) ? ' rtl-source' : ''; } if (empty($this->target)) { $t = explode("-", $job['target']); $target = strtoupper($t[0]); $this->target = $target; $this->target_rtl = $lang_handler->isRTL(strtolower($this->target)) ? ' rtl-target' : ''; } //check if language belongs to supported right-to-left languages if ($job['status'] == Constants_JobStatus::STATUS_ARCHIVED) { $this->job_archived = true; $this->job_owner = $data[0]['job_owner']; } $id_file = $job['id_file']; if (!isset($this->data["{$id_file}"])) { $files_found[] = $job['filename']; } $wStruct = new WordCount_Struct(); $wStruct->setIdJob($this->jid); $wStruct->setJobPassword($this->password); $wStruct->setNewWords($job['new_words']); $wStruct->setDraftWords($job['draft_words']); $wStruct->setTranslatedWords($job['translated_words']); $wStruct->setApprovedWords($job['approved_words']); $wStruct->setRejectedWords($job['rejected_words']); unset($job['id_file']); unset($job['source']); unset($job['target']); unset($job['source_code']); unset($job['target_code']); unset($job['mime_type']); unset($job['filename']); unset($job['jid']); unset($job['pid']); unset($job['cid']); unset($job['tid']); unset($job['pname']); unset($job['create_date']); unset($job['owner']); unset($job['last_opened_segment']); unset($job['new_words']); unset($job['draft_words']); unset($job['translated_words']); unset($job['approved_words']); unset($job['rejected_words']); //For projects created with No tm analysis enabled if ($wStruct->getTotal() == 0 && ($job['status_analysis'] == Constants_ProjectStatus::STATUS_DONE || $job['status_analysis'] == Constants_ProjectStatus::STATUS_NOT_TO_ANALYZE)) { $wCounter = new WordCount_Counter(); $wStruct = $wCounter->initializeJobWordCount($this->jid, $this->password); Log::doLog("BackWard compatibility set Counter."); } $this->job_stats = CatUtils::getFastStatsForJob($wStruct); } //Needed because a just created job has last_opened segment NULL if (empty($this->last_opened_segment)) { $this->last_opened_segment = getFirstSegmentId($this->jid, $this->password); } $this->first_job_segment = $this->project_status['job_first_segment']; $this->last_job_segment = $this->project_status['job_last_segment']; if (count($files_found) == 1) { $this->downloadFileName = $files_found[0]; } /** * get first segment of every file */ $fileInfo = getFirstSegmentOfFilesInJob($this->jid); $TotalPayable = array(); foreach ($fileInfo as &$file) { $file['file_name'] = ZipArchiveExtended::getFileName($file['file_name']); $TotalPayable[$file['id_file']]['TOTAL_FORMATTED'] = $file['TOTAL_FORMATTED']; } $this->firstSegmentOfFiles = json_encode($fileInfo); $this->fileCounter = json_encode($TotalPayable); list($uid, $user_email) = $this->getLoginUserParams(); if (self::isRevision()) { $this->userRole = TmKeyManagement_Filter::ROLE_REVISOR; } elseif ($user_email == $data[0]['job_owner']) { $this->userRole = TmKeyManagement_Filter::OWNER; } else { $this->userRole = TmKeyManagement_Filter::ROLE_TRANSLATOR; } /* * Take the keys of the user */ try { $_keyList = new TmKeyManagement_MemoryKeyDao(Database::obtain()); $dh = new TmKeyManagement_MemoryKeyStruct(array('uid' => $uid)); $keyList = $_keyList->read($dh); } catch (Exception $e) { $keyList = array(); Log::doLog($e->getMessage()); } $reverse_lookup_user_personal_keys = array('pos' => array(), 'elements' => array()); /** * Set these keys as editable for the client * * @var $keyList TmKeyManagement_MemoryKeyStruct[] */ foreach ($keyList as $_j => $key) { /** * @var $_client_tm_key TmKeyManagement_TmKeyStruct */ //create a reverse lookup $reverse_lookup_user_personal_keys['pos'][$_j] = $key->tm_key->key; $reverse_lookup_user_personal_keys['elements'][$_j] = $key; $this->_keyList['totals'][$_j] = new TmKeyManagement_ClientTmKeyStruct($key->tm_key); } /* * Now take the JOB keys */ $job_keyList = json_decode($data[0]['tm_keys'], true); $this->tid = count($job_keyList) > 0; /** * Start this N^2 cycle from keys of the job, * these should be statistically lesser than the keys of the user * * @var $keyList array */ foreach ($job_keyList as $jobKey) { $jobKey = new TmKeyManagement_ClientTmKeyStruct($jobKey); if ($this->isLoggedIn() && count($reverse_lookup_user_personal_keys['pos'])) { /* * If user has some personal keys, check for the job keys if they are present, and obfuscate * when they are not */ $_index_position = array_search($jobKey->key, $reverse_lookup_user_personal_keys['pos']); if ($_index_position !== false) { //i found a key in the job that is present in my database //i'm owner?? and the key is an owner type key? if (!$jobKey->owner && $this->userRole != TmKeyManagement_Filter::OWNER) { $jobKey->r = $jobKey->{TmKeyManagement_Filter::$GRANTS_MAP[$this->userRole]['r']}; $jobKey->w = $jobKey->{TmKeyManagement_Filter::$GRANTS_MAP[$this->userRole]['w']}; $jobKey = $jobKey->hideKey($uid); } else { if ($jobKey->owner && $this->userRole != TmKeyManagement_Filter::OWNER) { // I'm not the job owner, but i know the key because it is in my keyring // so, i can upload and download TMX, but i don't want it to be removed from job // in tm.html relaxed the control to "key.edit" to enable buttons // $jobKey = $jobKey->hideKey( $uid ); // enable editing } else { if ($jobKey->owner && $this->userRole == TmKeyManagement_Filter::OWNER) { //do Nothing } } } unset($this->_keyList['totals'][$_index_position]); } else { /* * This is not a key of that user, set right and obfuscate */ $jobKey->r = true; $jobKey->w = true; $jobKey = $jobKey->hideKey(-1); } $this->_keyList['job_keys'][] = $jobKey; } else { /* * This user is anonymous or it has no keys in its keyring, obfuscate all */ $jobKey->r = true; $jobKey->w = true; $this->_keyList['job_keys'][] = $jobKey->hideKey(-1); } } //clean unordered keys $this->_keyList['totals'] = array_values($this->_keyList['totals']); /** * Retrieve information about job errors * ( Note: these information are fed by the revision process ) * @see setRevisionController */ $jobQA = new Revise_JobQA($this->jid, $this->password, $wStruct->getTotal()); $jobQA->retrieveJobErrorTotals(); $jobVote = $jobQA->evalJobVote(); $this->qa_data = json_encode($jobQA->getQaData()); $this->qa_overall = $jobVote['minText']; $engine = new EnginesModel_EngineDAO(Database::obtain()); //this gets all engines of the user if ($this->isLoggedIn()) { $engineQuery = new EnginesModel_EngineStruct(); $engineQuery->type = 'MT'; $engineQuery->uid = $uid; $engineQuery->active = 1; $mt_engines = $engine->read($engineQuery); } else { $mt_engines = array(); } // this gets MyMemory $engineQuery = new EnginesModel_EngineStruct(); $engineQuery->type = 'TM'; $engineQuery->active = 1; $tms_engine = $engine->setCacheTTL(3600 * 24 * 30)->read($engineQuery); //this gets MT engine active for the job $engineQuery = new EnginesModel_EngineStruct(); $engineQuery->id = $this->project_status['id_mt_engine']; $engineQuery->active = 1; $active_mt_engine = $engine->setCacheTTL(60 * 10)->read($engineQuery); /* * array_unique cast EnginesModel_EngineStruct to string * * EnginesModel_EngineStruct implements __toString method * */ $this->translation_engines = array_unique(array_merge($active_mt_engine, $tms_engine, $mt_engines)); }
/** * Upload File from $_FILES * $RegistryKeyIndex MUST BE form name Element * * @param $fileUp * * @return object * @throws Exception */ protected function _uploadFile($fileUp) { $mod_name = null; if (empty($fileUp)) { throw new Exception(__METHOD__ . " -> File Not Found In Registry Instance."); } $fileName = $fileUp['name']; $fileTmpName = $fileUp['tmp_name']; // $fileType = $fileUp[ 'type' ]; $fileError = $fileUp['error']; $fileSize = $fileUp['size']; $fileUp = (object) $fileUp; if (!empty($fileError)) { switch ($fileError) { case 1: //UPLOAD_ERR_INI_SIZE $this->setObjectErrorOrThrowException($fileUp, new Exception(__METHOD__ . " -> The file '{$fileName}' is bigger than this PHP installation allows.")); break; case 2: //UPLOAD_ERR_FORM_SIZE $this->setObjectErrorOrThrowException($fileUp, new Exception(__METHOD__ . " -> The file '{$fileName}' is bigger than this form allows.")); break; case 3: //UPLOAD_ERR_PARTIAL $this->setObjectErrorOrThrowException($fileUp, new Exception(__METHOD__ . " -> Only part of the file '{$fileName}' was uploaded.")); break; case 4: //UPLOAD_ERR_NO_FILE $this->setObjectErrorOrThrowException($fileUp, new Exception(__METHOD__ . " -> No file was uploaded.")); break; case 6: //UPLOAD_ERR_NO_TMP_DIR $this->setObjectErrorOrThrowException($fileUp, new Exception(__METHOD__ . " -> Missing a temporary folder. ")); break; case 7: //UPLOAD_ERR_CANT_WRITE $this->setObjectErrorOrThrowException($fileUp, new Exception(__METHOD__ . " -> Failed to write file to disk.")); break; case 8: //UPLOAD_ERR_EXTENSION $this->setObjectErrorOrThrowException($fileUp, new Exception(__METHOD__ . " -> A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help.")); break; default: $this->setObjectErrorOrThrowException($fileUp, new Exception(__METHOD__ . " -> Unknown Error: {$fileError}")); break; } } else { $out_filename = ZipArchiveExtended::getFileName($fileName); if (!$this->_isRightExtension($fileUp) && (!isset($fileUp->error) || empty($fileUp->error))) { $this->setObjectErrorOrThrowException($fileUp, new Exception(__METHOD__ . " -> File Extension Not Allowed. '" . $out_filename . "'")); } if (!$this->_isRightMime($fileUp) && (!isset($fileUp->error) || empty($fileUp->error))) { $this->setObjectErrorOrThrowException($fileUp, new Exception(__METHOD__ . " -> File Mime Not Allowed. '{$out_filename}'")); } // NOTE FOR ZIP FILES //This exception is already raised by ZipArchiveExtended when file is unzipped. if ($fileSize >= INIT::$MAX_UPLOAD_FILE_SIZE && (!isset($fileUp->error) || empty($fileUp->error))) { $this->setObjectErrorOrThrowException($fileUp, new Exception(__METHOD__ . " -> File Dimensions Not Allowed. '{$out_filename}'")); } //All Right!!! GO!!! $mod_name = self::fixFileName($fileName); if ((!isset($fileUp->error) || empty($fileUp->error)) && !copy($fileTmpName, $this->dirUpload . DIRECTORY_SEPARATOR . $mod_name)) { $this->setObjectErrorOrThrowException($fileUp, new Exception(__METHOD__ . " -> Failed To Store File '{$out_filename}' On Server.")); } //In Unix you can't rename or move between filesystems, //Instead you must copy the file from one source location to the destination location, then delete the source. @unlink($fileTmpName); // octal; changing mode if ((!isset($fileUp->error) || empty($fileUp->error)) && !chmod($this->dirUpload . DIRECTORY_SEPARATOR . $mod_name, 0664)) { $this->setObjectErrorOrThrowException($fileUp, new Exception(__METHOD__ . " -> Failed To Set Permissions On File. '{$out_filename}'")); } } $fileUp->name = $mod_name; $fileUp->file_path = $this->dirUpload . DIRECTORY_SEPARATOR . $mod_name; unset($fileUp->tmp_name); return $fileUp; }
private function zipInternalFileDelete($file_name) { $file_path = $this->options['upload_dir'] . $file_name; $this->deleteSha($file_path); $out_file_name = ZipArchiveExtended::getFileName($file_name); $success[$out_file_name] = is_file($file_path) && $file_name[0] !== '.' && unlink($file_path); if ($success[$out_file_name]) { $this->deleteThumbnails($file_name); } return $success; }
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']; } }
private function handleZip($conversionHandler) { // this makes the conversionhandler accumulate eventual errors on files and continue $conversionHandler->setStopOnFileException(false); $internalZipFileNames = $conversionHandler->extractZipFile(); //call convertFileWrapper and start conversions for each file if ($conversionHandler->uploadError) { $fileErrors = $conversionHandler->getUploadedFiles(); foreach ($fileErrors as $fileError) { if (count($fileError->error) == 0) { continue; } $brokenFileName = ZipArchiveExtended::getFileName($fileError->name); /* * TODO * return error code is 2 because * <=0 is for errors * 1 is OK * * In this case, we raise warnings, hence the return code must be a new code */ $this->result['code'] = 2; $this->result['errors'][$brokenFileName] = array('code' => $fileError->error['code'], 'message' => $fileError->error['message'], 'debug' => $brokenFileName); } } $realFileNames = array_map(array('ZipArchiveExtended', 'getFileName'), $internalZipFileNames); foreach ($realFileNames as $i => &$fileObject) { $fileObject = array('name' => $fileObject, 'size' => filesize($this->intDir . DIRECTORY_SEPARATOR . $internalZipFileNames[$i])); } $this->result['data']['zipFiles'] = json_encode($realFileNames); $stdFileObjects = array(); if ($internalZipFileNames !== null) { foreach ($internalZipFileNames as $fName) { $newStdFile = new stdClass(); $newStdFile->name = $fName; $stdFileObjects[] = $newStdFile; } } else { $errors = $conversionHandler->getResult(); $errors = array_map(array('Upload', 'formatExceptionMessage'), $errors['errors']); $this->result['errors'] = array_merge($this->result['errors'], $errors); return false; } /* Do conversions here */ $converter = new ConvertFileWrapper($stdFileObjects, false); $converter->intDir = $this->intDir; $converter->errDir = $this->errDir; $converter->cookieDir = $this->cookieDir; $converter->source_lang = $this->source_lang; $converter->target_lang = $this->target_lang; $converter->doAction(); $errors = $converter->checkResult(); if (count($errors) > 0) { $this->result['code'] = 2; foreach ($errors as $__err) { $brokenFileName = ZipArchiveExtended::getFileName($__err['debug']); if (!isset($this->result['errors'][$brokenFileName])) { $this->result['errors'][$brokenFileName] = array('code' => $__err['code'], 'message' => $__err['message'], 'debug' => $brokenFileName); } } } }
public function doAction() { $this->result['code'] = 0; // No Good, Default if (empty($this->file_name)) { $this->result['code'] = -1; // No Good, Default $this->result['errors'][] = array("code" => -1, "message" => "Error: missing file name."); return false; } $ext = FilesStorage::pathinfo_fix($this->file_name, PATHINFO_EXTENSION); $conversionHandler = new ConversionHandler(); $conversionHandler->setFileName($this->file_name); $conversionHandler->setSourceLang($this->source_lang); $conversionHandler->setTargetLang($this->target_lang); $conversionHandler->setSegmentationRule($this->segmentation_rule); $conversionHandler->setCookieDir($this->cookieDir); $conversionHandler->setIntDir($this->intDir); $conversionHandler->setErrDir($this->errDir); if ($ext == "zip") { if ($this->convertZipFile) { // this makes the conversionhandler accumulate eventual errors on files and continue $conversionHandler->setStopOnFileException(false); $internalZipFileNames = $conversionHandler->extractZipFile(); //call convertFileWrapper and start conversions for each file if ($conversionHandler->uploadError) { $fileErrors = $conversionHandler->getUploadedFiles(); foreach ($fileErrors as $fileError) { if (count($fileError->error) == 0) { continue; } $brokenFileName = ZipArchiveExtended::getFileName($fileError->name); /* * TODO * return error code is 2 because * <=0 is for errors * 1 is OK * * In this case, we raise warnings, hence the return code must be a new code */ $this->result['code'] = 2; $this->result['errors'][$brokenFileName] = array('code' => $fileError->error['code'], 'message' => $fileError->error['message'], 'debug' => $brokenFileName); } } $realFileNames = array_map(array('ZipArchiveExtended', 'getFileName'), $internalZipFileNames); foreach ($realFileNames as $i => &$fileObject) { $fileObject = array('name' => $fileObject, 'size' => filesize($this->intDir . DIRECTORY_SEPARATOR . $internalZipFileNames[$i])); } $this->result['data']['zipFiles'] = json_encode($realFileNames); $stdFileObjects = array(); if ($internalZipFileNames !== null) { foreach ($internalZipFileNames as $fName) { $newStdFile = new stdClass(); $newStdFile->name = $fName; $stdFileObjects[] = $newStdFile; } } else { $errors = $conversionHandler->getResult(); $errors = array_map(array('Upload', 'formatExceptionMessage'), $errors['errors']); $this->result['errors'] = array_merge($this->result['errors'], $errors); return false; } /* Do conversions here */ $converter = new ConvertFileWrapper($stdFileObjects, false); $converter->intDir = $this->intDir; $converter->errDir = $this->errDir; $converter->cookieDir = $this->cookieDir; $converter->source_lang = $this->source_lang; $converter->target_lang = $this->target_lang; $converter->doAction(); $errors = $converter->checkResult(); if (count($errors) > 0) { $this->result['code'] = 2; foreach ($errors as $__err) { $brokenFileName = ZipArchiveExtended::getFileName($__err['debug']); if (!isset($this->result['errors'][$brokenFileName])) { $this->result['errors'][$brokenFileName] = array('code' => $__err['code'], 'message' => $__err['message'], 'debug' => $brokenFileName); } } } } else { $this->result['errors'][] = array("code" => -2, "message" => "Nested zip files are not allowed"); return false; } } else { $conversionHandler->doAction(); $this->result = $conversionHandler->getResult(); } isset($this->result['errors']) ? null : ($this->result['errors'] = array()); if (count($this->result['errors']) == 0) { $this->result['code'] = 1; } else { $this->result['errors'] = array_values($this->result['errors']); } }