Don't like ads? PRO users don't see any ads ;-)
Guest

kierownik

By: a guest on Feb 19th, 2010  |  syntax: PHP  |  size: 7.01 KB  |  hits: 311  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. /**
  4. * Tell A Friend
  5. *
  6. * @package              Tell A Friend
  7. * @author               daniel rokven (http://kierownik.nl/)
  8. * @copyright    Copyright (c) 2010, daniel rokven
  9. * @license              http://creativecommons.org/licenses/by-sa/3.0/
  10. * @link                 http://kierownik.nl
  11. * @since                Version 1.0
  12. * @filesource
  13. */
  14.  
  15. class tell_a_friend extends Controller {
  16.  
  17.         private $error                          = array();
  18.         private $min_name_length        = 4;    // minimal name length
  19.         private $max_name_length        = 25;   // Maximal name length
  20.         private $message                        = 'A friend recommened that you look at our site.<br /> It is located at http://kierownik.nl. Please visit us.';
  21.         private $subject                        = 'Tell A Friend';
  22.         private $sec_between_sends      = '30'; // Time in seconds
  23.        
  24.         function tell_a_friend()
  25.         {
  26.                 parent::Controller();
  27.                 $this->load->helper(array('form', 'url', 'html', 'file'));
  28.                 $this->load->library(array('table', 'form_validation', 'session'));
  29.                 $this->form_validation->set_error_delimiters('<br /><span class="error_fields_required">', '</span><br />');
  30.         }
  31.        
  32.         function index()
  33.         {
  34.                 // Set the default message that we use in our form
  35.                 $data['message'] = $this->message;
  36.                 /*
  37.                 // Did they hit the Send button
  38.                 if ($_POST)
  39.                 {
  40.                         // Does the log file exist
  41.                         if (read_file('./system/logs/tell_a_friend/' . $this->input->ip_address() .'.php'))
  42.                         {
  43.                                 // Calulate the time difference between sends
  44.                                 if ($this->_time_exceeded())
  45.                                 {
  46.                                         $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.');
  47.                                         $error_time_exeeded = TRUE;
  48.                                 }
  49.                                 else
  50.                                         $this->_create_log_file();
  51.                         }
  52.                         else
  53.                         {
  54.                                 // File does not exist so we create it.
  55.                                 $this->_create_log_file();
  56.                         }
  57.                 }
  58.                 */
  59.                 $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');
  60.                 $this->form_validation->set_rules('your_email',                         'your email',                   'trim|xss_clean|required|valid_email');
  61.                 $this->form_validation->set_rules('first_friend_email',         'First friends email',  'trim|xss_clean|required|valid_email|callback__email_check_first_friend');
  62.                 $this->form_validation->set_rules('second_friend_email',        'Second friends email', 'trim|xss_clean|valid_email|callback__email_check_second_friend');
  63.                 $this->form_validation->set_rules('message',                            'Message',                              'trim|xss_clean|required');
  64.                
  65.                 $data['errors'] = array();
  66.                
  67.                 // Run the validation for the form
  68.                 if ($this->form_validation->run())      // validation went ok
  69.                 {
  70.                         $this->load->library('email');
  71.                        
  72.                         // Set the from adress
  73.                         $this->email->from($this->input->post('your_email'), $this->input->post('your_name'));
  74.                        
  75.                         // Set the reply adress
  76.                         $this->email->reply_to($this->input->post('your_email'), $this->input->post('your_name'));
  77.                        
  78.                         // Set to whom the email should be send
  79.                         $this->email->to($this->input->post('tellafriend_first_friend_email'));
  80.                        
  81.                         // If second friends email is not empty set it as a Blind Carbom Copy so the other friend cannot see the other friends email
  82.                         if ($this->input->post('friend_email2'))
  83.                                 $this->email->bcc($this->input->post('friend_email2'));
  84.                        
  85.                         // Set the subject of the email, you can change this at the top, look for $subject
  86.                         $this->email->subject($this->subject);
  87.                        
  88.                         // Set the message of the email
  89.                         $this->email->message($this->input->post('message'));
  90.                        
  91.                         //
  92.                         // First we controlle the time between the last post of an email and now
  93.                         //
  94.                        
  95.                         // Does the log file exist
  96.                         if (read_file('./system/logs/tell_a_friend/' . $this->input->ip_address() .'.php'))
  97.                         {
  98.                                 // Calulate the time difference between sends
  99.                                 if ($this->_time_exceeded())
  100.                                 {
  101.                                         $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.');
  102.                                 }
  103.                                 else
  104.                                 {
  105.                                         // overwrite the file that exists with a new one so they have to wait longer
  106.                                         $this->_create_log_file();
  107.                                
  108.                                         // Now it is ok to send the email
  109.                                         $this->email->send();
  110.                                 }
  111.                         }
  112.                         else // There is no log file so the did not yet completed the form yet
  113.                         {
  114.                                 // time did not exceed so we send the email and set a new log file
  115.                                 // or if the log file does not excist we set it and this is the first time they send the email
  116.                                 $this->_create_log_file();
  117.                                
  118.                                 // Now it is ok to send the email
  119.                                 $this->email->send();
  120.                         }
  121.                        
  122.                         // debug email, turned off by default, turn on if something is wrong
  123.                         //echo $this->email->print_debugger();
  124.                        
  125.                         // Show the form
  126.                         $this->load->view('tell_a_friend_view', $data);
  127.                 }
  128.                 else    // validation is not ok
  129.                 {
  130.                         // Get all the errors
  131.                         $errors = $this->error;
  132.                        
  133.                         // Set the individuel errors
  134.                         foreach ($errors as $k => $v)
  135.                                 $data['errors'][$k] = $this->lang->line($v);
  136.                        
  137.                         $this->load->view('tell_a_friend_view', $data);
  138.                 }
  139.         }
  140.        
  141.         private function _time_exceeded()
  142.         {
  143.                 $log_file = read_file('./system/logs/tell_a_friend/' . $this->input->ip_address() .'.php');
  144.                 $time = strtotime("now");
  145.                
  146.                 if ($time - $log_file < $this->sec_between_sends)
  147.                         return TRUE;
  148.                 else
  149.                         return FALSE;
  150.         }
  151.        
  152.         private function _create_log_file()
  153.         {
  154.                 // First we check if the directory exists, if so, we can create the file
  155.                 if (file_exists('./system/logs/tell_a_friend'))
  156.                 {
  157.                         if (read_file('./system/logs/tell_a_friend/' . $this->input->ip_address()))
  158.                                 delete_files('./system/logs/tell_a_friend/' . $this->input->ip_address());
  159.                        
  160.                         if (write_file('./system/logs/tell_a_friend/' . $this->input->ip_address() .'.php', time()))
  161.                                 return TRUE;
  162.                         else
  163.                                 return FALSE;
  164.                 }
  165.                 else    // Directory did not exist so we create the directory and after that we create the file
  166.                 {
  167.                         mkdir('./system/logs/tell_a_friend', 0777);
  168.                         if (write_file('./system/logs/tell_a_friend/' . $this->input->ip_address() .'.php', time()))
  169.                                 return TRUE;
  170.                         else
  171.                                 return FALSE;
  172.                 }
  173.         }
  174.        
  175.         function _email_check_first_friend($email)
  176.         {
  177.                 if ($email == $this->input->post('your_email'))
  178.                 {
  179.                         $this->form_validation->set_message('_email_check_first_friend', 'The %s field can not be the same as your own email');
  180.                         return FALSE;
  181.                 }
  182.                 else
  183.                         return TRUE;
  184.         }
  185.        
  186.         function _email_check_second_friend($email)
  187.         {
  188.                 if ($email == $this->input->post('your_email') AND !empty($email))
  189.                 {
  190.                         $this->form_validation->set_message('_email_check_second_friend', 'The %s field can not be the same as your own email');
  191.                         return FALSE;
  192.                 }
  193.                 else if ($email == $this->input->post('friend_email1') AND !empty($email))
  194.                 {
  195.                         $this->form_validation->set_message('_email_check_second_friend', 'The %s field can not be the same as your first friends email');
  196.                         return FALSE;
  197.                 }
  198.                 else
  199.                         return TRUE;
  200.         }
  201. }
  202.  
  203. /* End of file tell_a_friend.php */
  204. /* Location: ./system/application/controllers/tell_a_friend.php */