Skip to content

Commit

Permalink
Rate Limiting / Spam Limiting
Browse files Browse the repository at this point in the history
30 seconds between threads, no duplicate thread titles in a row.
  • Loading branch information
wowzamade committed Aug 22, 2012
1 parent 5b7cf4b commit 810d974
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
16 changes: 13 additions & 3 deletions application/controllers/newthread.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,30 @@ 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],
'subject' => $subject,
'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' => "<div class=\"error\">Your are posting too fast or this thread has just been posted.</div>"));
}
$comment['thread_id'] = $this->thread_dal->new_thread($comment);
$this->user_dal->update_thread_count($comment['user_id']);

Expand All @@ -60,6 +69,7 @@ function index()
$this->load->view('newthread');
$this->load->view('shared/footer');
}

}

/* End of file newthread.php */
Expand Down
3 changes: 2 additions & 1 deletion application/controllers/threads.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
53 changes: 52 additions & 1 deletion application/models/thread_dal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit 810d974

Please sign in to comment.