<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Tell A Friend
*
* @package Tell A Friend
* @author daniel rokven (http://kierownik.nl/)
* @copyright Copyright (c) 2010, daniel rokven
* @license http://creativecommons.org/licenses/by-sa/3.0/
* @link http://kierownik.nl
* @since Version 1.0
* @filesource
*/
class tell_a_friend extends Controller {
private $error = array();
private $min_name_length = 4; // minimal name length
private $max_name_length = 25; // Maximal name length
private $message = 'A friend recommened that you look at our site.<br /> It is located at http://kierownik.nl. Please visit us.';
private $subject = 'Tell A Friend';
private $sec_between_sends = '30'; // Time in seconds
function tell_a_friend()
{
parent::Controller();
$this->load->helper(array('form', 'url', 'html', 'file'));
$this->load->library(array('table', 'form_validation', 'session'));
$this->form_validation->set_error_delimiters('<br /><span class="error_fields_required">', '</span><br />');
}
function index()
{
// Set the default message that we use in our form
$data['message'] = $this->message;
/*
// Did they hit the Send button
if ($_POST)
{
// Does the log file exist
if (read_file('./system/logs/tell_a_friend/' . $this->input->ip_address() .'.php'))
{
// Calulate the time difference between sends
if ($this->_time_exceeded())
{
$this->session->set_flashdata('tell_a_friend_time', 'Hey there is less then ' . $this->sec_between_sends . ' seconds between this send and your last send.<br />You will have to wait.');
$error_time_exeeded = TRUE;
}
else
$this->_create_log_file();
}
else
{
// File does not exist so we create it.
$this->_create_log_file();
}
}
*/
$this->form_validation->set_rules('your_name', 'Your name', 'trim|xss_clean|min_length[' . $this->min_name_length . ']|max_length[' . $this->max_name_length . ']|required');
$this->form_validation->set_rules('your_email', 'your email', 'trim|xss_clean|required|valid_email');
$this->form_validation->set_rules('first_friend_email', 'First friends email', 'trim|xss_clean|required|valid_email|callback__email_check_first_friend');
$this->form_validation->set_rules('second_friend_email', 'Second friends email', 'trim|xss_clean|valid_email|callback__email_check_second_friend');
$this->form_validation->set_rules('message', 'Message', 'trim|xss_clean|required');
$data['errors'] = array();
// Run the validation for the form
if ($this->form_validation->run()) // validation went ok
{
$this->load->library('email');
// Set the from adress
$this->email->from($this->input->post('your_email'), $this->input->post('your_name'));
// Set the reply adress
$this->email->reply_to($this->input->post('your_email'), $this->input->post('your_name'));
// Set to whom the email should be send
$this->email->to($this->input->post('tellafriend_first_friend_email'));
// If second friends email is not empty set it as a Blind Carbom Copy so the other friend cannot see the other friends email
if ($this->input->post('friend_email2'))
$this->email->bcc($this->input->post('friend_email2'));
// Set the subject of the email, you can change this at the top, look for $subject
$this->email->subject($this->subject);
// Set the message of the email
$this->email->message($this->input->post('message'));
//
// First we controlle the time between the last post of an email and now
//
// Does the log file exist
if (read_file('./system/logs/tell_a_friend/' . $this->input->ip_address() .'.php'))
{
// Calulate the time difference between sends
if ($this->_time_exceeded())
{
$this->session->set_flashdata('tell_a_friend_time', 'Hey there is less then ' . $this->sec_between_sends . ' seconds between this send and your last send.<br />You will have to wait.');
}
else
{
// overwrite the file that exists with a new one so they have to wait longer
$this->_create_log_file();
// Now it is ok to send the email
$this->email->send();
}
}
else // There is no log file so the did not yet completed the form yet
{
// time did not exceed so we send the email and set a new log file
// or if the log file does not excist we set it and this is the first time they send the email
$this->_create_log_file();
// Now it is ok to send the email
$this->email->send();
}
// debug email, turned off by default, turn on if something is wrong
//echo $this->email->print_debugger();
// Show the form
$this->load->view('tell_a_friend_view', $data);
}
else // validation is not ok
{
// Get all the errors
$errors = $this->error;
// Set the individuel errors
foreach ($errors as $k => $v)
$data['errors'][$k] = $this->lang->line($v);
$this->load->view('tell_a_friend_view', $data);
}
}
private function _time_exceeded()
{
$log_file = read_file('./system/logs/tell_a_friend/' . $this->input->ip_address() .'.php');
if ($time - $log_file < $this->sec_between_sends)
return TRUE;
else
return FALSE;
}
private function _create_log_file()
{
// First we check if the directory exists, if so, we can create the file
{
if (read_file('./system/logs/tell_a_friend/' . $this->input->ip_address()))
delete_files('./system/logs/tell_a_friend/' . $this->input->ip_address());
if (write_file
('./system/logs/tell_a_friend/' . $this->input->ip_address() .'.php', time()))
return TRUE;
else
return FALSE;
}
else // Directory did not exist so we create the directory and after that we create the file
{
mkdir('./system/logs/tell_a_friend', 0777
);
if (write_file
('./system/logs/tell_a_friend/' . $this->input->ip_address() .'.php', time()))
return TRUE;
else
return FALSE;
}
}
function _email_check_first_friend($email)
{
if ($email == $this->input->post('your_email'))
{
$this->form_validation->set_message('_email_check_first_friend', 'The %s field can not be the same as your own email');
return FALSE;
}
else
return TRUE;
}
function _email_check_second_friend($email)
{
if ($email == $this->input->post('your_email') AND
!empty($email))
{
$this->form_validation->set_message('_email_check_second_friend', 'The %s field can not be the same as your own email');
return FALSE;
}
else if ($email == $this->input->post('friend_email1') AND
!empty($email))
{
$this->form_validation->set_message('_email_check_second_friend', 'The %s field can not be the same as your first friends email');
return FALSE;
}
else
return TRUE;
}
}
/* End of file tell_a_friend.php */
/* Location: ./system/application/controllers/tell_a_friend.php */