From b3e723c43ef10d215dc52115aaf026addbb51a3d Mon Sep 17 00:00:00 2001
From: Woraprat <73052317+aeworaprat@users.noreply.github.com>
Date: Tue, 14 Mar 2023 22:51:45 +0700
Subject: [PATCH] update captcha

---
 html/exhi/application/controllers/General.php | 40 ++++++++++++++-----
 .../application/models/company/M_company.php  |  2 +-
 .../application/models/general/M_general.php  |  2 +-
 .../models/instructor/M_instructor.php        |  2 +-
 .../application/models/project/M_project.php  |  5 ++-
 html/exhi/application/models/tag/M_tag.php    |  2 +-
 html/exhi/application/models/user/M_user.php  |  2 +-
 .../views/general/v_general_select.php        | 15 ++++---
 8 files changed, 45 insertions(+), 25 deletions(-)

diff --git a/html/exhi/application/controllers/General.php b/html/exhi/application/controllers/General.php
index afb3286..3474eb1 100644
--- a/html/exhi/application/controllers/General.php
+++ b/html/exhi/application/controllers/General.php
@@ -279,14 +279,34 @@ class general extends Exhibition_Controller
         echo json_encode($data);
     }
 
-    public function insert_comment()
-    {
-        $sender = $this->input->post('sender');
-        $detail = $this->input->post('detail');
-        $id = $this->input->post('id');
-        $this->load->model('general/M_general', 'general');
-        $this->general->insert_comment($sender, $detail, date("Y-m-d"), $id);
-        $data['message'] = true;
-        echo json_encode($data);
-    }
+    public function insert_comment(){
+		$data['message'] = true;
+		$sender = $this->input->post('sender');
+		$detail = $this->input->post('detail');
+		$id = $this->input->post('id');
+		$google_url = "https://www.google.com/recaptcha/api/siteverify";
+		$secret_key = '6Lec8uckAAAAALcbXBS1XQ36NNrY8ztqPtwuPM8W';
+		$response = $this->input->post('captcha');
+		if(!empty($response))
+		{
+			$url = $google_url."?secret=".$secret_key."&response=".$response;
+			$ch = curl_init(); 
+			curl_setopt($ch, CURLOPT_URL, $url); 
+			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
+			$output = curl_exec($ch); 
+			curl_close($ch);      
+			$res= json_decode($output, true);
+			if($res['success'] == 'true') 
+				$data['message'] = true;
+			else
+			$data['message'] = false;
+		}else{
+			$data['message'] = false;
+		}
+		$this->load->model('general/M_general', 'general');
+		if($data['message'] == true){
+			$this->general->insert_comment($sender, $detail, date("Y-m-d"), $id);
+		}
+		echo json_encode($data);
+	}
 }
diff --git a/html/exhi/application/models/company/M_company.php b/html/exhi/application/models/company/M_company.php
index 524573e..1837cbb 100644
--- a/html/exhi/application/models/company/M_company.php
+++ b/html/exhi/application/models/company/M_company.php
@@ -15,7 +15,7 @@ class M_company extends Da_company
 	public function get_all()
 	{
 		$sql = "SELECT * FROM {$this->db_name}.company
-		Where is_delete = 0";
+		Where is_delete = 0 order by company_id DESC";
         $query = $this->db->query($sql);
         return $query;
 	}
diff --git a/html/exhi/application/models/general/M_general.php b/html/exhi/application/models/general/M_general.php
index 9aa210b..ea0a24b 100644
--- a/html/exhi/application/models/general/M_general.php
+++ b/html/exhi/application/models/general/M_general.php
@@ -192,7 +192,7 @@ class M_general extends Da_general
     public function get_comment_all($id)
     {
         $sql = "SELECT * FROM {$this->db_name}.comment
-		Where project_id = $id";
+		Where project_id = $id order by comment_id DESC";
         $query = $this->db->query($sql);
         return $query;
     }
diff --git a/html/exhi/application/models/instructor/M_instructor.php b/html/exhi/application/models/instructor/M_instructor.php
index b72398d..26566cc 100644
--- a/html/exhi/application/models/instructor/M_instructor.php
+++ b/html/exhi/application/models/instructor/M_instructor.php
@@ -15,7 +15,7 @@ class M_instructor extends Da_instructor
 	public function get_all()
 	{
 		$sql = "SELECT * FROM {$this->db_name}.instructor
-		Where is_delete = 0 and instructor_id != 0";
+		Where is_delete = 0 and instructor_id != 0 order by instructor_id DESC";
         $query = $this->db->query($sql);
         return $query;
 	}
diff --git a/html/exhi/application/models/project/M_project.php b/html/exhi/application/models/project/M_project.php
index 7ca185b..0970cf8 100644
--- a/html/exhi/application/models/project/M_project.php
+++ b/html/exhi/application/models/project/M_project.php
@@ -14,7 +14,8 @@ class M_project extends Da_project
 
 	public function get_all()
 	{
-		$sql = "SELECT * FROM {$this->db_name}.project";
+		$sql = "SELECT * FROM {$this->db_name}.project
+		order by project_id DESC";
         $query = $this->db->query($sql);
         return $query;
 	}
@@ -30,7 +31,7 @@ class M_project extends Da_project
 	public function get_all_user($id)
 	{
 		$sql = "SELECT * FROM {$this->db_name}.project
-		Where user_id = $id";
+		Where user_id = $id order by project_id DESC";
         $query = $this->db->query($sql);
         return $query;
 	}
diff --git a/html/exhi/application/models/tag/M_tag.php b/html/exhi/application/models/tag/M_tag.php
index 978fd20..5a3ff3e 100644
--- a/html/exhi/application/models/tag/M_tag.php
+++ b/html/exhi/application/models/tag/M_tag.php
@@ -15,7 +15,7 @@ class M_tag extends Da_tag
 	public function get_all()
 	{
 		$sql = "SELECT * FROM {$this->db_name}.tag
-		Where is_delete = 0";
+		Where is_delete = 0 order by tag_id DESC";
         $query = $this->db->query($sql);
         return $query;
 	}
diff --git a/html/exhi/application/models/user/M_user.php b/html/exhi/application/models/user/M_user.php
index 5474616..88f3788 100644
--- a/html/exhi/application/models/user/M_user.php
+++ b/html/exhi/application/models/user/M_user.php
@@ -24,7 +24,7 @@ class M_user extends Da_user
 	public function get_all()
 	{
 		$sql = "SELECT * FROM {$this->db_name}.user
-		Where is_delete = 0";
+		Where is_delete = 0 order by user_id DESC";
         $query = $this->db->query($sql);
         return $query;
 	}
diff --git a/html/exhi/application/views/general/v_general_select.php b/html/exhi/application/views/general/v_general_select.php
index a658009..2a1ad2c 100644
--- a/html/exhi/application/views/general/v_general_select.php
+++ b/html/exhi/application/views/general/v_general_select.php
@@ -1,8 +1,3 @@
-<link rel="stylesheet" href="https://cdn.datatables.net/1.11.4/css/jquery.dataTables.min.css" />
-<!-- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.css" /> -->
-<!-- <link rel="stylesheet" href="https://cdn.datatables.net/1.11.4/css/dataTables.bootstrap4.min.css" /> -->
-
-<script src="https://cdn.datatables.net/1.11.4/js/jquery.dataTables.min.js"></script>
 <link href="<?php echo base_url().'assets/plugins/page.css'?>" rel="stylesheet" />
 
 
@@ -326,7 +321,8 @@ span{
 						<input type="text" id="sender" value="" placeholder="ชื่อ-นามสกุล" class="form-control">
 					</div>
 					<textarea class="form-control ml-1 shadow-none textarea" placeholder="เขียนความคิดเห็น..." id="detail"></textarea></div>
-                    	<div class="mt-2 text-right"><button class="btn btn-info btn-sm shadow-none" type="button" onclick="insert_comment()">เพิ่มความคิดเห็น</button></div>
+                    <div class="g-recaptcha" data-sitekey="6Lec8uckAAAAABGZ4_CaawpSb9hXgbDlwnhXaJco"></div>	
+					<div class="mt-2 text-right"><button class="btn btn-info btn-sm shadow-none" type="button" onclick="insert_comment()">เพิ่มความคิดเห็น</button></div>
                 	</div>
 					<div class="data-container"></div>
 					<div id="demo"></div>
@@ -361,6 +357,7 @@ span{
     </div>
 </footer>
 <script src="<?php echo base_url()?>assets/plugins/page.js"></script>
+<script src="https://www.google.com/recaptcha/api.js" async defer></script>
 <script>
 	
 	$(document).ready(function() {
@@ -464,7 +461,8 @@ span{
 					data: {
 						'sender': sender,
 						'detail': detail,
-						'id': '<?php echo $project->project_id ?>'
+						'id': '<?php echo $project->project_id ?>',
+						'captcha': grecaptcha.getResponse()
 					},
 					dataType: 'json',
 					success: function(data) {
@@ -476,8 +474,9 @@ span{
 								get_comment()
 								$('#sender').val('')
 								$('#detail').val('')
+								grecaptcha.reset();
 							})
-						}else {
+						}else{
 							Toast.fire({
 								icon: 'error',
 								title: "เพิ่มข้อมูลไม่สำเร็จ",
-- 
GitLab