diff --git a/application/controllers/newthread.php b/application/controllers/newthread.php index ab9aa24..29a8627 100644 --- a/application/controllers/newthread.php +++ b/application/controllers/newthread.php @@ -24,13 +24,15 @@ function index() $this->form_validation->set_rules('category[]', 'Category', 'required|exact_length[1]|integer'); $this->form_validation->set_rules('content', 'Content', 'trim|required'); - + + $this->form_validation->set_rules('content', 'Content', 'trim|required'); + if ($this->form_validation->run()) { $subject = $this->form_validation->set_value('subject'); $content = $this->form_validation->set_value('content'); $category = $this->form_validation->set_value('category[]'); - + $comment = array( 'user_id' => $this->session->userdata('user_id'), 'category' => (int)$category[0], @@ -38,7 +40,14 @@ function index() 'content' => _process_post($content), 'original_content' => $content ); - + /* + !$this->thread_dal->are_you_posting_too_fast($this->session->userdata('user_id') ) || + */ + if( $this->thread_dal->has_thread_just_been_posted($subject, $this->session->userdata('user_id')) || $this->thread_dal->are_you_posting_too_fast($this->session->userdata('user_id') == TRUE )) + { + return send_json($this->output, 400, array('error' => true, + 'reason' => "
Your are posting too fast or this thread has just been posted.
")); + } $comment['thread_id'] = $this->thread_dal->new_thread($comment); $this->user_dal->update_thread_count($comment['user_id']); @@ -60,6 +69,7 @@ function index() $this->load->view('newthread'); $this->load->view('shared/footer'); } + } /* End of file newthread.php */ diff --git a/application/controllers/threads.php b/application/controllers/threads.php index a3c8cfd..49a1fba 100755 --- a/application/controllers/threads.php +++ b/application/controllers/threads.php @@ -25,7 +25,8 @@ function index($pagination = 0, $filter = '', $ordering = '', $dir = 'desc', $wh { // uncomment the following line you if broke something but you can't figure out what. // $this->output->enable_profiler(TRUE); - + + $args = (object)array( 'pagination' => (int) $pagination, 'filter' => strtolower($filter), diff --git a/application/models/thread_dal.php b/application/models/thread_dal.php index 23f1725..8bbe2a2 100755 --- a/application/models/thread_dal.php +++ b/application/models/thread_dal.php @@ -23,7 +23,58 @@ function new_thread($data) return $this->db->insert_id(); } - + /** + * Are you posting the same thread twice in a row + * + * @return bool + */ + function has_thread_just_been_posted($subject, $user_id) + { + $sql = "SELECT * FROM threads WHERE subject = ? AND user_id = ? ORDER BY created desc LIMIT 1"; + + $results = $this->db->query($sql, Array($subject,$user_id) ); + + if($results->num_rows() > 0 ) { + return true; + } else { + return false; + } + } + /** + * Are you spamming threads? + * + * @return bool + */ + function are_you_posting_too_fast($user_id) + { + $sql = "SELECT created FROM threads WHERE user_id = ? ORDER BY created DESC LIMIT 1"; // how long ago did you post your last thread, If less then 1 minute ago, return true + + + $results = $this->db->query($sql, $user_id); + + if($results->num_rows() > 0) { + $res_arr = $results->result_array(); + + $last_posted_time = strtotime($res_arr[0]['created']); + + $difference = ((int)utc_time() - (int)$last_posted_time); + if($difference > 30 ) //30 seconds + { + // go ahead and post + return false; + } + else + { + // go away + return true; + } + } + else + { + return false; + } + + } /** * Get some threads from the database *