163 lines
5.7 KiB
PHP
163 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace frontend\controllers;
|
|
|
|
use common\models\ForumCategory;
|
|
use common\models\ForumComment;
|
|
use common\models\ForumThread;
|
|
use yii\base\Exception;
|
|
use yii\data\ActiveDataProvider;
|
|
use Yii;
|
|
|
|
class ForumController extends \yii\web\Controller
|
|
{
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function actions()
|
|
{
|
|
return [
|
|
|
|
'captcha' => [
|
|
'class' => 'yii\captcha\CaptchaAction',
|
|
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
|
|
],
|
|
];
|
|
}
|
|
public function actionIndex(int $id = null)
|
|
{
|
|
$dataProvider = new ActiveDataProvider([
|
|
'query' => $id ? ForumCategory::find()->where(['status' => 1, 'id' => $id]) : ForumCategory::find()->where(['status' => 1])
|
|
]);
|
|
$category = $id ? ForumCategory::find()->where(['status' => 1, 'id' => $id])->one() : false;
|
|
return $this->render('index', [
|
|
'dataProvider' => $dataProvider,
|
|
'category' => $category,
|
|
]);
|
|
}
|
|
|
|
public function actionView($id)
|
|
{
|
|
$model = $this->findModel($id);
|
|
$comment = new ForumComment([
|
|
'forum_thread_id' => $model->id
|
|
]);
|
|
|
|
if ($comment->load(Yii::$app->request->post()) && $comment->validate()) {
|
|
if ($comment->save()) {
|
|
Yii::$app->session->setFlash('success', 'ตอบกระทู้เรียบร้อยแล้ว รอเจ้าหน้าที่ตรวจสอบ');
|
|
}
|
|
return $this->refresh();
|
|
}
|
|
|
|
// ใส่ตรงนี้เพื่อรับค่า token จาก hCaptcha
|
|
$model->hcaptcha_response = Yii::$app->request->post('h-captcha-response');
|
|
|
|
// ตรวจสอบ hCaptcha ก่อน save
|
|
$hcaptchaValid = $this->verifyHcaptcha($model->hcaptcha_response); // ฟังก์ชันตรวจสอบ hCaptcha
|
|
|
|
if ($hcaptchaValid['success']) {
|
|
try {
|
|
if ($comment->save()) {
|
|
Yii::$app->session->setFlash('success', 'ตอบกระทู้เรียบร้อยแล้ว รอเจ้าหน้าที่ตรวจสอบ');
|
|
}
|
|
return $this->refresh();
|
|
} catch (Exception $exception) {
|
|
throw new Exception($exception->getMessage());
|
|
} catch (\yii\db\Exception $exception) {
|
|
throw new Exception($exception->getMessage());
|
|
}
|
|
} else {
|
|
$model->addError('hcaptcha_response', 'ยืนยัน hCaptcha ไม่สำเร็จ');
|
|
}
|
|
|
|
|
|
$dataProviderComment = new ActiveDataProvider([
|
|
'query' => ForumComment::find()->filterWhere(['forum_thread_id' => $model->id,]),
|
|
'sort' => [
|
|
'defaultOrder' => [
|
|
'id' => SORT_DESC
|
|
]
|
|
]
|
|
]);
|
|
|
|
return $this->render('view', [
|
|
'model' => $model,
|
|
'comment' => $comment,
|
|
'dataProviderComment' => $dataProviderComment,
|
|
]);
|
|
}
|
|
|
|
|
|
public function actionCreate(int $id)
|
|
{
|
|
$forum = ForumCategory::findOne($id);
|
|
if (!$forum) {
|
|
throw new Exception('ไม่พบกระดานข่าวนี้ในระบบ');
|
|
}
|
|
$model = new ForumThread();
|
|
$model->scenario = 'thread_new';
|
|
$model->forum_category_id = $id;
|
|
if ($model->load(Yii::$app->request->post())) {
|
|
|
|
// ใส่ตรงนี้เพื่อรับค่า token จาก hCaptcha
|
|
$model->hcaptcha_response = Yii::$app->request->post('h-captcha-response');
|
|
|
|
// ตรวจสอบ hCaptcha ก่อน save
|
|
$hcaptchaValid = $this->verifyHcaptcha($model->hcaptcha_response); // ฟังก์ชันตรวจสอบ hCaptcha
|
|
|
|
if ($hcaptchaValid['success']) {
|
|
try {
|
|
if ($model->save()) {
|
|
Yii::$app->session->setFlash('success', 'ระบบได้รับข้อมูลเรียบร้อยแล้ว กรุณาอย่าส่งซ้ำ รอการติดต่อกลับจากเจ้าหน้าที่');
|
|
return $this->refresh();
|
|
}
|
|
} catch (Exception $exception) {
|
|
throw new Exception($exception->getMessage());
|
|
} catch (\yii\db\Exception $exception) {
|
|
throw new Exception($exception->getMessage());
|
|
}
|
|
} else {
|
|
$model->addError('hcaptcha_response', 'ยืนยัน hCaptcha ไม่สำเร็จ');
|
|
}
|
|
}
|
|
return $this->render('create', [
|
|
'model' => $model,
|
|
'forum' => $forum
|
|
]);
|
|
}
|
|
|
|
private function verifyHcaptcha($token)
|
|
{
|
|
$secret = Yii::$app->params['hcaptcha_secret'];
|
|
$url = 'https://hcaptcha.com/siteverify';
|
|
|
|
$data = [
|
|
'secret' => $secret,
|
|
'response' => $token,
|
|
];
|
|
|
|
$options = [
|
|
'http' => [
|
|
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
|
|
'method' => 'POST',
|
|
'content' => http_build_query($data),
|
|
],
|
|
];
|
|
|
|
$context = stream_context_create($options);
|
|
$result = file_get_contents($url, false, $context);
|
|
return json_decode($result, true);
|
|
}
|
|
|
|
|
|
protected function findModel($id)
|
|
{
|
|
$model = ForumThread::findOne($id);
|
|
if (!$model) {
|
|
throw new Exception('ไม่พบกระทู้นี้');
|
|
}
|
|
return $model;
|
|
}
|
|
}
|