"", "form" => "", "result" => "", ); //---------------------------- // request parameter //---------------------------- $qProjectID = isset($_SESSION["project_id"]) ? $_SESSION["project_id"] : REQ_NON; $qToken = isset($_REQUEST["q_token"]) ? $_REQUEST["q_token"] : REQ_NON; $qKind = isset($_REQUEST["q_kind"]) ? $_REQUEST["q_kind"] : REQ_NON; $qID = isset($_REQUEST["q_id"]) ? $_REQUEST["q_id"] : REQ_NON; $qType = isset($_REQUEST["q_type"]) ? $_REQUEST["q_type"] : REQ_NON; $qCheckMode = isset($_REQUEST["q_check_mode"]) ? $_REQUEST["q_check_mode"] : REQ_NON; $qFileName1 = isset($_FILES["q_file1"]["name"]) ? $_FILES["q_file1"]["name"] : REQ_NON; $qFileTmpName1 = isset($_FILES["q_file1"]["tmp_name"]) ? $_FILES["q_file1"]["tmp_name"] : REQ_NON; $qFileError1 = isset($_FILES["q_file1"]["error"]) ? $_FILES["q_file1"]["error"] : REQ_NON; $qFileSize1 = isset($_FILES["q_file1"]["size"]) ? $_FILES["q_file1"]["size"] : REQ_NON; $qFileName2 = isset($_FILES["q_file2"]["name"]) ? $_FILES["q_file2"]["name"] : REQ_NON; $qFileTmpName2 = isset($_FILES["q_file2"]["tmp_name"]) ? $_FILES["q_file2"]["tmp_name"] : REQ_NON; $qFileError2 = isset($_FILES["q_file2"]["error"]) ? $_FILES["q_file2"]["error"] : REQ_NON; $qFileSize2 = isset($_FILES["q_file2"]["size"]) ? $_FILES["q_file2"]["size"] : REQ_NON; //---------------------------- // judge post //---------------------------- $isCheckMode = ($qCheckMode !== REQ_NON); $isPost = ( ! $isCheckMode && $qToken !== REQ_NON); //---------------------------- // set types //---------------------------- Util::setType($qProjectID, "int"); Util::setType($qToken, "string"); Util::setType($qKind, "string"); Util::setType($qID, "int"); Util::setType($qType, "int"); Util::setType($qFileName1, "string"); Util::setType($qFileTmpName1, "string"); Util::setType($qFileError1, "int"); Util::setType($qFileSize1, "int"); Util::setType($qFileName2, "string"); Util::setType($qFileTmpName2, "string"); Util::setType($qFileError2, "int"); Util::setType($qFileSize2, "int"); //---------------------------- // Auth-Before validate //---------------------------- if ( $result === RES_OK ){ if ( ! Validate::inKeyArray($qKind, Term::KIND_TABLE()) ){ $result = RES_NG; $gResult->push("{$WORD($wk, 1)}"); } if ( $isPost ){ if ( ! Validate::isToken($qToken, "q_token") ){ $result = RES_NG; $gResult->push("{$WORD($wk, 2)}"); } } if ( ! Validate::isNumeric($qID) ){ $result = RES_NG; $gResult->push("{$WORD($wk, 3)}"); } if ( ! Validate::isNumeric($qProjectID) ){ $result = RES_NG; $gResult->push("{$WORD($wk, 4)}"); } } //---------------------------- // Auth //---------------------------- if ( $result === RES_OK ){ $auth = Auth::instance("term_root", $qKind); if ( ! $auth->processImport($qID, $qProjectID) ){ $result = RES_NG; $gResult->push("{$WORD($wk, 5)}"); } } //---------------------------- // Auth-After validate //---------------------------- if ( $result === RES_OK ){ if ( $isPost || $isCheckMode ){ if ( ! Validate::inArray($qType, $arrTypeKey) ){ $result = RES_NG; $gResult->push("{$WORD($wk, 6)}"); } if ( $qFileSize1 > 0 ){ if ( $result === RES_OK ){ if ( $qFileError1 !== UPLOAD_ERR_OK ){ $result = RES_NG; $gResult->push("{$WORD($wk, 7)}(Code:{$qFileError})"); } } // ファイル確認 if ( $result === RES_OK ){ if ( ! file_exists($qFileTmpName1) ){ $result = RES_NG; $gResult->push("{$WORD($wk, 8)}"); } } } else { $result = RES_NG; $gResult->push("{$WORD($wk, 9)}"); } if ( $qType === TYPE_FILE2 ){ if ( $qFileSize2 > 0 ){ if ( $result === RES_OK ){ if ( $qFileError2 !== UPLOAD_ERR_OK ){ $result = RES_NG; $gResult->push("{$WORD($wk, 10)}(Code:{$qFileError})"); } } // ファイル確認 if ( $result === RES_OK ){ if ( ! file_exists($qFileTmpName2) ){ $result = RES_NG; $gResult->push("{$WORD($wk, 11)}"); } } } else { $result = RES_NG; $gResult->push("{$WORD($wk, 12)}"); } } } } if ( Validate::inKeyArray($qKind, Term::KIND_TABLE()) ){ $kindText = Term::KIND_TABLE($qKind); $kindChildText = Term::KIND_TABLE($qKind, FALSE); } else { $kindText = ""; $kindChildText = ""; } //---------------------------- // function //---------------------------- function getSentenceFromTextFile( $filename ){ global $gResult; $result = RES_OK; $arrSentence = array(); // ファイルオープン -------------------- if ( $result === RES_OK ){ if ( ($fp = fopen($filename, "r")) === FALSE ){ $result = RES_NG; } } // 登録 -------------------- if ( $result === RES_OK ){ $lineNo = 0; while ( ($line = fgets($fp)) !== FALSE ){ $lineNo++; // UTF-8エンコード $line = mb_convert_encoding($line, "UTF-8", "UTF-8,EUC-JP,SJIS,JIS,ASCII"); $line = preg_replace("/(\r|\n)/", "", $line); $arrText = explode("\t", $line); if ( count($arrText) === 0 ){ continue; } else if ( count($arrText) === 1 ){ $textS = $arrText[0]; $textT = ""; } else if ( count($arrText) >= 2 ){ $textS = $arrText[0]; $textT = $arrText[1]; } $arrSentence[] = array( "source_text" => $textS, "target_text" => $textT, ); } fclose($fp); } if ( $result === RES_OK ){ return $arrSentence; } else { return FALSE; } } //---------------------------- // process //---------------------------- if ( $result === RES_OK ){ DAO::begin(); $objDAOTermRoot = DAO::instance("term_root", $qKind); $objDAOTerm = DAO::instance("term", $qKind); $objDAOExterm = DAO::instance("exterm"); $objViewTermRoot = View::instance("term_root"); $objViewTerm = View::instance("term"); // 辞書情報取得 -------------------- if ( $result === RES_OK ){ $objDAOTermRoot->setAuth( $auth ); $resDAOTermRoot = $objDAOTermRoot->get($qID, DAO::T_TERM_ROOT_STATE_NORMAL); $result = $resDAOTermRoot["result"]; if ( $result === RES_OK ){ $dataTermRoot = $resDAOTermRoot["data"][0]; $arrTabRight[] = $objViewTermRoot->title($dataTermRoot); } else { $result = RES_NG; $gResult->push("{$WORD($wk, 13)}"); } } if ( $isPost || $isCheckMode ){ switch ( $qType ){ //--------------------------- // 1ファイル(原訳) //--------------------------- case TYPE_IMDTY: // 拡張子取得 $arr = pathinfo($qFileName1); $ext = $arr["extension"]; if ( $result === RES_OK ){ // TSV if ( strcmp($ext, "tsv") === 0 || strcmp($ext, "txt") === 0 ){ if ( ($arrSentence = getSentenceFromTextFile($qFileTmpName1)) === FALSE ){ $result = RES_NG; $gResult->push("{$WORD($wk, 14)}"); } } // tmx else if ( strcmp($ext, "tmx") === 0 ){ if ( ($tmx = file_get_contents($qFileTmpName1)) === FALSE ){ $result = RES_NG; $gResult->push("{$WORD($wk, 15)}"); } if ( $result === RES_OK ){ $objDocTmx = new DocTmx(); if ( $objDocTmx->load($tmx) ){ $arrSentence = $objDocTmx->getSentence(TRUE); } else { $result = RES_NG; $gResult->push("{$WORD($wk, 16)}"); } } } // tbx else if ( strcmp($ext, "tbx") === 0 ){ if ( ($tmx = file_get_contents($qFileTmpName1)) === FALSE ){ $result = RES_NG; $gResult->push("{$WORD($wk, 17)}"); } if ( $result === RES_OK ){ $langS = $dataTermRoot["t_term_root_lang_s"]; $langT = $dataTermRoot["t_term_root_lang_t"]; if ( ($tmx = ToolDocConverter::convertTMX($qFileTmpName1, "tbx", $langS, $langT, "tmx")) === FALSE ){ $result = RES_NG; $gResult->push("{$WORD($wk, 18)}"); } } if ( $result === RES_OK ){ $objDocTmx = new DocTmx(); if ( $objDocTmx->load($tmx) ){ $arrSentence = $objDocTmx->getSentence(TRUE); } else { $result = RES_NG; $gResult->push("{$WORD($wk, 19)}"); } } } // 対応していないファイル拡張子 else { $result = RES_NG; if ( $isCheckMode ){ $arrMessage = array( "{$WORD($wk, 20)}", "{$WORD($wk, 21)}", "[{$WORD($wk, 22)}]\t*.tsv, *.txt", "[{$WORD($wk, 23)}]\t*.tmx", "[{$WORD($wk, 24)}]\t*.tbx", ); $gResult->push(implode("\r\n", $arrMessage)); } else { $gResult->push("{$WORD($wk, 25)}"); } } } if ( $result === RES_OK ){ if ( $isCheckMode ){ // 原文のみのセンテンスを探す foreach ( $arrSentence as $sentence ){ $textS = $sentence["source_text"]; $textT = $sentence["target_text"]; if ( Validate::isString($textS) && ! Validate::isString($textT) ){ $result = RES_NO_DATA; $gResult->push("{$WORD($wk, 26)}"); break; } } } else { foreach ( $arrSentence as $sentence ){ $textS = $sentence["source_text"]; $textT = $sentence["target_text"]; $textS = mb_convert_encoding($textS, "UTF-8", "UTF-8,EUC-JP,SJIS,JIS,ASCII"); $textS = preg_replace("/(\r|\n)/", "", $textS); $textT = mb_convert_encoding($textT, "UTF-8", "UTF-8,EUC-JP,SJIS,JIS,ASCII"); $textT = preg_replace("/(\r|\n)/", "", $textT); if ( $result === RES_OK ){ $resDAOTerm = $objDAOTerm->register(INIT_ID, $textS, $textT, $qID, User::get("id")); $result = $resDAOTerm["result"]; } if ( $result === RES_NG ){ break; } else { $result = RES_OK; } } } } break; //--------------------------- // 1ファイル(原) //--------------------------- case TYPE_FILE1: /* if ( $result === RES_OK ){ if ( rename($qFileTmpName1, Setting::get("PATH_DATA_EXTERM") . "/{$qID}.1") === FALSE ){ $result = RES_NG; $gResult->push("{$WORD($wk, 27)}"); } chmod(Setting::get("PATH_DATA_EXTERM") . "/{$qID}.1", 0755); } if ( $result === RES_OK ){ $extermState = $objDAOExterm->getInit("t_exterm_state"); $resDAOExterm = $objDAOExterm->register($qID, $qType, $extermState); $result = $resDAOExterm["result"]; } */ break; //--------------------------- // 2ファイル(原訳) //--------------------------- case TYPE_FILE2: /* if ( $result === RES_OK ){ if ( rename($qFileTmpName1, Setting::get("PATH_DATA_EXTERM") . "/{$qID}.1") === FALSE ){ $result = RES_NG; $gResult->push("{$WORD($wk, 28)}"); } chmod(Setting::get("PATH_DATA_EXTERM") . "/{$qID}.1", 0755); } if ( $result === RES_OK ){ if ( rename($qFileTmpName2, Setting::get("PATH_DATA_EXTERM") . "/{$qID}.2") === FALSE ){ $result = RES_NG; $gResult->push("{$WORD($wk, 29)}"); } chmod(Setting::get("PATH_DATA_EXTERM") . "/{$qID}.2", 0755); } if ( $result === RES_OK ){ $extermState = $objDAOExterm->getInit("t_exterm_state"); $resDAOExterm = $objDAOExterm->register($qID, $qType, $extermState); $result = $resDAOExterm["result"]; } */ break; default: $result = RES_NG; $gResult->push("{$WORD($wk, 30)}"); } // 結果 -------------------- if ( $isPost ){ if ( $result === RES_OK ){ $gResult->push("{$WORD($wk, 31)}"); } else { $gResult->push("{$WORD($wk, 32)}"); } } } DAO::end($result); } if ( $isCheckMode ){ $res["result"] = $result; $res["message"] = $gResult->string(FALSE, $result); echo json_encode($res); exit; } //---------------------------- // menu //---------------------------- { // menu ---------------------------------------------------------------- if ( $result !== RES_NG ){ $arrMenu = array(); //----- $arrMenu[0] = $objViewTermRoot->tab($qKind, View_term_root::TAB_LIST, $auth, $qProjectID); if ( $tab = $objViewTermRoot->tab($qKind, View_term_root::TAB_ITEM_EDIT, $auth, $qProjectID, $qID) ){ $arrMenu[1] = $tab; } else if ( $tab = $objViewTermRoot->tab($qKind, View_term_root::TAB_ITEM_LIST, $auth, $qProjectID, $qID) ){ $arrMenu[1] = $tab; } $arrMenu[2] = $objViewTermRoot->tab($qKind, View_term_root::TAB_EDIT, $auth, $qProjectID, $qID); $arrMenu[3] = $objViewTermRoot->tab($qKind, View_term_root::TAB_IMPORT, $auth, $qProjectID, $qID); //----- $arrHtml["menu"] = Printer::menuTab($arrMenu, 3, $arrTabRight); } // title --------------------------------------------------------------- $title = "{$WORD($wk, 33, $kindText)}"; // form ---------------------------------------------------------------- if ( $result !== RES_NG ){ if ( $isPost ){ header("Location: /content/term/?q_kind={$qKind}&q_project_id={$qProjectID}&q_id={$qID}"); } else { $arrControl = array(); // フォーム(タイプ) $arrType = array( TYPE_IMDTY => "{$WORD($wk, 34, $kindChildText)}", // TYPE_FILE1 => "{$WORD($wk, 35)}", // TYPE_FILE2 => "{$WORD($wk, 36)}", ); $arrControl[] = array( "id" => "q_type", "name" => "{$WORD($wk, 37)}", "form" => array( Printer::radio("q_type", "q_type", $arrType, TYPE_IMDTY), $objViewTerm->fileDescription($qKind), ), ); // フォーム(ファイル1) $arrControl[] = array( "id" => "q_file1", "name" => "{$WORD($wk, 38, $kindChildText)}", "form" => Printer::input("file", "q_file1", "q_file1", ""), ); /* // フォーム(ファイル2) $arrControl[] = array( "id" => "q_file2", "name" => "{$WORD($wk, 39)}", "form" => Printer::input("file", "q_file2", "q_file2", ""), ); */ // フォーム(OK) $arrControl[] = array( "id" => NULL, "name" => "", "form" => Printer::input("submit", NULL, NULL, "OK", "btn btn-primary"), ); // フォーム(ID) $arrControl[] = array( "id" => NULL, "name" => NULL, "form" => Printer::input("hidden", "q_id", "q_id", $qID), ); // フォーム(kind) $arrControl[] = array( "id" => NULL, "name" => NULL, "form" => Printer::input("hidden", "q_kind", "q_kind", $qKind), ); // フォーム(token) $arrControl[] = array( "id" => NULL, "name" => NULL, "form" => Printer::input("hidden", "q_token", "q_token", Util::token()), ); $arrHtml["form"] = Printer::form("form_term", "form_term", "./import.html", "post", $arrControl, NULL, array( "enctype" => "multipart/form-data" )); } } else { if ( ! Validate::isNumeric($qID) ){ // アクション(OK) $arrHtml["form"] = Printer::link("OK", "btn btn-default", NULL, NULL, "/content/project/"); } else { // アクション(OK) $arrHtml["form"] = Printer::link("OK", "btn btn-default", NULL, NULL, "./import.html?q_kind={$qKind}&q_id={$qID}"); } } // result -------------------------------------------------------------- $arrHtml["result"] = $gResult->string(TRUE, $result); } ?>