/** * 投稿実行コントローラ * @return */ public function action_post() { $result = false; //掲示板を得る $board = Model_Board::find(Input::post('bbsId_')); if ($board == null) { $log = new Logging(); $log->writeLog_Warning('Invalid parameters at post(Board is missing) BBSID=' . $bbsId, __FILE__, __LINE__); return Response::forge('パラメータ異常'); } //2ch型掲示板の場合は、バリデーションルールを変える $val = $this->doValidate($board); //コメントの場合、スレッド番号を求める if (isset($_POST['commentOf_'])) { $threadNum = $_POST['commentOf_']; } else { $threadNum = 0; } $out = ''; //CSRFチェック if (!CSRFCheck::chkCSRFToken(__FILE__, __LINE__)) { // CSRF 攻撃または CSRF トークンの期限切れ $out = 'ページロードから時間が経過している為、投稿失敗しました。<BR>リロードして再投稿して下さい。'; goto INVALIDPOST; } //添付ファイルを検証 // エラーを処理する $out = ''; $aReasult = true; try { foreach (Upload::get_errors() as $file) { if ($file['size'] != 0) { foreach ($file['errors'] as $error) { $out .= $error['message'] . '<br>'; } $aReasult = false; } } } catch (Exception $ex) { $aReasult = false; } if ($val->run()) { $bbsId = $val->validated('bbsId_'); $article = Model_Article::forge(); $article->bbsId = $bbsId; $article->authorName = $val->validated('authorName'); $article->authorAge = $val->validated('authorAge'); $article->authorPrefecture = $val->validated('authorPrefecture'); $article->authorIsMale = $val->validated('authorIsMale'); $article->authorProfile = $val->validated('authorProfile'); $article->authorEmail = $val->validated('authorEmail'); $article->commentOf = $threadNum; $article->title = $val->validated('title'); $article->body = $val->validated('body'); $article->password = $val->validated('password'); $article->numberOfLike = 0; $article->numberOfView = 0; $article->authorAgent = $_SERVER['HTTP_USER_AGENT']; $article->authorIP = Input::ip(); $article->reserve1 = -1; $article->isDeleted = 0; if ($board->allowXvideos == true) { $article->xvideosURL = $val->validated('xvideosURL'); } $article->save(); //新規投稿ではUpdate_atが付かないため、一度修正して再度保存 $article->reserve1 = 0; $article->save(); if ($aReasult == true) { if (Upload::is_valid()) { $image = Image::forge(); $files = Upload::get_Files(); $tmpDir = DOCROOT . 'assets/img/tmp'; foreach ($files as $file) { $fileName = $file['file']; $img_file = file_get_contents($fileName); if ($img_file) { //一時ファイルを拡張子付きにリネーム $fileWithExt = $fileName . '.' . $file['extension']; rename($fileName, $fileWithExt); //PC用としても大きすぎる場合はリサイズ $imgInfo = getimagesize($fileWithExt); if ($imgInfo[0] > FULL_SIZE_X) { $image->load($fileWithExt); $image->config('bgcolor', '#FFF')->resize(FULL_SIZE_X, FULL_SIZE_X, true, false); $image->save($fileWithExt); $img_file = file_get_contents($fileWithExt); } //サムネイル作成 $thumbName = $tmpDir . DS . date('_ymdhis') . $this->random() . $file['name']; $image->load($fileWithExt); $image->config('bgcolor', '#FFF')->resize(THUMBNAIL_SIZE_X, THUMBNAIL_SIZE_Y, true, false); $image->save($thumbName); $attach = Model_Attach::forge(); $attach->bbsId = $bbsId; $attach->mime = $file['mimetype']; $attach->attachOf = $article->id; $attach->rawData = $img_file; if ($threadNum != 0) { $attach->threadId = $threadNum; } else { $attach->threadId = $article->id; } $thumb_file = file_get_contents($thumbName); if ($thumb_file) { $attach->thumbData = $thumb_file; $attach->save(); unlink($thumbName); unlink($fileWithExt); } else { $attach->save(); unlink($fileWithExt); } } } } } //掲示板のupdate_at更新 $bd = Model_Board::find($bbsId); $bd->postCount = $bd->postCount + 1; $bd->save(); //スレッド元のupdated_at更新 $query = Model_Article::query(); $query->where('id', '=', $threadNum); $query->and_where_open(); $query->where('bbsId', $bbsId); $query->and_where_close(); $th = $query->get_one(); if ($th != null) { $th->commentCount = $th->commentCount + 1; $th->save(); } //新しいスレッドをTweet if ($bd->type != 3 && $bd->twitter) { $tw = new Twitter(); $tw->tweet_newArticleBuild($board, $article); } $out = '投稿完了しました。'; $result = true; } else { foreach ($val->error() as $error) { $out .= $error . '<br>'; } $result = false; } //正常系 if ($result == true) { $this->showPostSucessPage($bbsId, $threadNum); return; } //異常系 INVALIDPOST: //Boardを取得 $board = Model_Board::find($_POST['bbsId_']); if ($threadNum == 0) { //新規スレッド投稿 $redirectURL = 'bbs/index/' . $board->shortName; } else { //コメント投稿 $redirectURL = 'bbs/thread/' . $board->shortName . DS . $threadNum; } //エラーメッセージと、POSTをsessionで渡す Session::set('errorMsg', $out); Session::set('oldPost', $_POST); Response::redirect($redirectURL); }