PHP File Manager
Editing File: Contact.php
<?php /* * Copyright (C) Wayne Purton-Smith - All Rights Reserved * Unauthorized copying of this file or removing this paragraph, via any medium is strictly prohibited * Proprietary and confidential * Written by Wayne Purton-Smith <waynepurtonsmith@hotmail.co.uk> February 2014 */ class Contact extends CI_Model { public function get($enquiry_id = 0, $from_email = NULL, $skip = 0, $limit = 30) { $sql = "SELECT `ce`.*, `u`.`first_name` AS `replied_by_name` FROM `contact_enquiries` `ce` LEFT OUTER JOIN `members` `u` ON `u`.`user_id` = `ce`.`replied_by` WHERE `ce`.`is_deleted` = 0"; if(($enquiry_id = (int) $enquiry_id) > 0) { $sql .= " AND `ce`.`enquiry_id` = $enquiry_id "; $limit = 1; } if(($from_email = remove_whitespace($from_email)) != '') { $sql .= " AND CASE WHEN `ce`.`replied_on` IS NOT NULL THEN 0 ELSE 1 END, `ce`.`email` = " . $this->db->escape($from_email) . " "; } $sql .= " GROUP BY `ce`.`enquiry_id` ORDER BY `ce`.`date` DESC"; $sql .= $this->common->returnOffset($skip, $limit); $result = $this->db->query($sql); //preprint($this->db->last_query()); if($result->num_rows()) { if($enquiry_id > 0) return $result->row(); $rows = $result->result(); $result->free_result(); return $rows; } return array(); } public function send($data = array()) { // Input name - true/false for required. // Comment to disable $contact_fields = array ( 'full_name' => true, 'email' => true, //'phone' => false 'message' => true ); foreach($contact_fields as $input_key => $is_required) { $$input_key = (isset($data[$input_key])) ? nullify(format_whitespace($data[$input_key])) : NULL; if($is_required && $$input_key == '') { return $this->common->setResponseMessage('Please enter the missing fields'); } } if(isset($contact_fields['email']) && $contact_fields['email'] === true && !filter_var($email, FILTER_VALIDATE_EMAIL)) { return $this->common->setResponseMessage('A valid email address is required'); } if($this->config->item('contact_captcha')) { if(!isset($data['g-recaptcha-response']) || ($recaptcha_response = $data['g-recaptcha-response']) == '') { return $this->common->setResponseMessage('Please respond to the security captcha'); } $recaptcha_check = @json_decode($this->common->curlURL('https://www.google.com/recaptcha/api/siteverify', array ( 'secret' => $this->config->item('recaptcha_secret_key'), 'response' => $recaptcha_response, 'remoteip' => $this->input->ip_address() ))); if(!isset($recaptcha_check->success) || $recaptcha_check->success === false) { return $this->common->setResponseMessage('Please respond to the security captcha'); } } $full_name = format_name($full_name); $email = strtolower($email); $enquiry_date = time(); $sql_data = array ( 'full_name' => (isset($full_name)) ? $full_name : NULL, 'email' => (isset($email)) ? $email : NULL, 'phone' => (isset($phone)) ? $phone : NULL, 'message' => $message, 'date' => $enquiry_date ); $this->db->query($this->db->insert_string('contact_enquiries', $sql_data)); if($this->db->affected_rows() === 1) { $send_to_email = $this->config->item('company_email'); if($this->config->item('send_email_enquiries') && $send_to_email) { $email_subject = 'Enquiry'; $person_name = (isset($full_name) && $full_name != '') ? input_value($full_name) : NULL; $person_email = (isset($email)) ? $email : NULL; if($person_name) { $email_subject .= ' from ' . $person_name; } $email_subject .= ' via the Kentish Football website'; $this->common->sendEmail(array ( 'email' => $send_to_email, 'subject' => $email_subject, 'reply' => array($person_email, $person_name), 'variables' => array ( 'name' => $person_name, 'email' => $person_email, 'date' => date('l, jS F Y \a\t H:i', $enquiry_date), 'message' => nl2br(input_value($message)) ) ), 'enquiry'); } return $this->common->setResponseMessage('The enquiry was successfully sent', true); } return $this->common->setResponseMessage('The enquiry could not be sent at this time - please try again'); } public function sendReply($enquiry_id = 0, $message = NULL) { if(($enquiry_id = (int) $enquiry_id) > 0 && ($enquiry_info = $this->get($enquiry_id))) { if(!filter_var($enquiry_info->email, FILTER_VALIDATE_EMAIL)) { return $this->common->setResponseMessage('The enquiry does not have an email address to be replied to'); } $message = trim($message); if($message == '') { return $this->common->setResponseMessage('The reply message is required'); } $update_reply = $this->db->query($this->db->update_string('contact_enquiries', array ( 'replied_message' => $message, 'replied_on' => time(), 'replied_by' => $this->users->user_id ), "`enquiry_id` = $enquiry_id")); if($this->db->affected_rows()) { $send_to_email = ($this->config->item('contact_enquiry_email')) ? $this->config->item('contact_enquiry_email') : $this->config->item('company_email'); $this->common->sendEmail(array ( 'email' => $enquiry_info->email, 'subject' => 'Reply to enquiry - ' . input_value($this->config->item('company_name')), 'reply' => array($send_to_email, input_value($this->config->item('company_name'))), 'variables' => array ( 'reply_message' => nl2br(input_value($message)), 'original_message' => nl2br(input_value($enquiry_info->message)) ) ), 'reply-enquiry'); return $this->common->setResponseMessage('The enquiry was successfully replied to', true); } return $this->common->setResponseMessage('The enquiry could not be replied at this time - please try again'); } return $this->common->setResponseMessage('The enquiry does not exist'); } }
Cancel