Guest User

Untitled

a guest
Oct 26th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.00 KB | None | 0 0
  1. <?php
  2. class ModelAccountCustomer extends Model {
  3.     public function addCustomer($data) {
  4.         if (isset($data['customer_group_id']) && is_array($this->config->get('config_customer_group_display')) && in_array($data['customer_group_id'], $this->config->get('config_customer_group_display'))) {
  5.             $customer_group_id = $data['customer_group_id'];
  6.         } else {
  7.             $customer_group_id = $this->config->get('config_customer_group_id');
  8.         }
  9.  
  10.         $this->load->model('account/customer_group');
  11.  
  12.         $customer_group_info = $this->model_account_customer_group->getCustomerGroup($customer_group_id);
  13.  
  14.         $this->db->query("INSERT INTO " . DB_PREFIX . "customer SET store_id = '" . (int)$this->config->get('config_store_id') . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', fax = '" . $this->db->escape($data['fax']) . "', salt = '" . $this->db->escape($salt = substr(md5(uniqid(rand(), true)), 0, 9)) . "', password = '" . $this->db->escape(sha1($salt . sha1($salt . sha1($data['password'])))) . "', newsletter = '" . (isset($data['newsletter']) ? (int)$data['newsletter'] : 0) . "', customer_group_id = '" . (int)$customer_group_id . "', ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "', status = '1', approved = '" . (int)!$customer_group_info['approval'] . "', date_added = NOW()");
  15.  
  16.         $customer_id = $this->db->getLastId();
  17.  
  18.         $this->db->query("INSERT INTO " . DB_PREFIX . "address SET customer_id = '" . (int)$customer_id . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', company = '" . $this->db->escape($data['company']) . "', company_id = '" . $this->db->escape($data['company_id']) . "', tax_id = '" . $this->db->escape($data['tax_id']) . "', address_1 = '" . $this->db->escape($data['address_1']) . "', address_2 = '" . $this->db->escape($data['address_2']) . "', city = '" . $this->db->escape($data['city']) . "', postcode = '" . $this->db->escape($data['postcode']) . "', country_id = '" . (int)$data['country_id'] . "', zone_id = '" . (int)$data['zone_id'] . "'");
  19.  
  20.         $address_id = $this->db->getLastId();
  21.  
  22.         $this->db->query("UPDATE " . DB_PREFIX . "customer SET address_id = '" . (int)$address_id . "' WHERE customer_id = '" . (int)$customer_id . "'");
  23.  
  24.         $this->language->load('mail/customer');
  25.  
  26.         $subject = sprintf($this->language->get('text_subject'), $this->config->get('config_name'));
  27.  
  28.         $message = sprintf($this->language->get('text_welcome'), $this->config->get('config_name')) . "\n\n";
  29.  
  30.         if (!$customer_group_info['approval']) {
  31.             $message .= $this->language->get('text_login') . "\n";
  32.         } else {
  33.             $message .= $this->language->get('text_approval') . "\n";
  34.         }
  35.  
  36.         $message .= $this->url->link('account/login', '', 'SSL') . "\n\n";
  37.         $message .= $this->language->get('text_services') . "\n\n";
  38.         $message .= $this->language->get('text_thanks') . "\n";
  39.         $message .= $this->config->get('config_name');
  40.  
  41.         $mail = new Mail();
  42.         $mail->protocol = $this->config->get('config_mail_protocol');
  43.         $mail->parameter = $this->config->get('config_mail_parameter');
  44.         $mail->hostname = $this->config->get('config_smtp_host');
  45.         $mail->username = $this->config->get('config_smtp_username');
  46.         $mail->password = $this->config->get('config_smtp_password');
  47.         $mail->port = $this->config->get('config_smtp_port');
  48.         $mail->timeout = $this->config->get('config_smtp_timeout');            
  49.         $mail->setTo($data['email']);
  50.         $mail->setFrom($this->config->get('config_email'));
  51.         $mail->setSender($this->config->get('config_name'));
  52.         $mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
  53.         $mail->setText(html_entity_decode($message, ENT_QUOTES, 'UTF-8'));
  54.         $mail->send();
  55.  
  56.         // Send to main admin email if new account email is enabled
  57.         if ($this->config->get('config_account_mail')) {
  58.             $message  = $this->language->get('text_signup') . "\n\n";
  59.             $message .= $this->language->get('text_website') . ' ' . $this->config->get('config_name') . "\n";
  60.             $message .= $this->language->get('text_firstname') . ' ' . $data['firstname'] . "\n";
  61.             $message .= $this->language->get('text_lastname') . ' ' . $data['lastname'] . "\n";
  62.             $message .= $this->language->get('text_customer_group') . ' ' . $customer_group_info['name'] . "\n";
  63.  
  64.             if ($data['company']) {
  65.                 $message .= $this->language->get('text_company') . ' '  . $data['company'] . "\n";
  66.             }
  67.  
  68.             $message .= $this->language->get('text_email') . ' '  .  $data['email'] . "\n";
  69.             $message .= $this->language->get('text_telephone') . ' ' . $data['telephone'] . "\n";
  70.  
  71.             $mail->setTo($this->config->get('config_email'));
  72.             $mail->setSubject(html_entity_decode($this->language->get('text_new_customer'), ENT_QUOTES, 'UTF-8'));
  73.             $mail->setText(html_entity_decode($message, ENT_QUOTES, 'UTF-8'));
  74.             $mail->send();
  75.  
  76.             // Send to additional alert emails if new account email is enabled
  77.             $emails = explode(',', $this->config->get('config_alert_emails'));
  78.  
  79.             foreach ($emails as $email) {
  80.                 if (strlen($email) > 0 && preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $email)) {
  81.                     $mail->setTo($email);
  82.                     $mail->send();
  83.                 }
  84.             }
  85.         }
  86.     }
  87.  
  88.     public function editCustomer($data) {
  89.         $this->db->query("UPDATE " . DB_PREFIX . "customer SET firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', fax = '" . $this->db->escape($data['fax']) . "' WHERE customer_id = '" . (int)$this->customer->getId() . "'");
  90.     }
  91.  
  92.     public function editPassword($email, $password) {
  93.         $this->db->query("UPDATE " . DB_PREFIX . "customer SET salt = '" . $this->db->escape($salt = substr(md5(uniqid(rand(), true)), 0, 9)) . "', password = '" . $this->db->escape(sha1($salt . sha1($salt . sha1($password)))) . "' WHERE LOWER(email) = '" . $this->db->escape(utf8_strtolower($email)) . "'");
  94.     }
  95.  
  96.     public function editNewsletter($newsletter) {
  97.         $this->db->query("UPDATE " . DB_PREFIX . "customer SET newsletter = '" . (int)$newsletter . "' WHERE customer_id = '" . (int)$this->customer->getId() . "'");
  98.     }
  99.  
  100.     public function getCustomer($customer_id) {
  101.         $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE customer_id = '" . (int)$customer_id . "'");
  102.  
  103.         return $query->row;
  104.     }
  105.  
  106.     public function getCustomerByEmail($email) {
  107.         $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE LOWER(email) = '" . $this->db->escape(utf8_strtolower($email)) . "'");
  108.  
  109.         return $query->row;
  110.     }
  111.  
  112.     public function getCustomerByToken($token) {
  113.         $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE token = '" . $this->db->escape($token) . "' AND token != ''");
  114.  
  115.         $this->db->query("UPDATE " . DB_PREFIX . "customer SET token = ''");
  116.  
  117.         return $query->row;
  118.     }
  119.  
  120.     public function getCustomers($data = array()) {
  121.         $sql = "SELECT *, CONCAT(c.firstname, ' ', c.lastname) AS name, cg.name AS customer_group FROM " . DB_PREFIX . "customer c LEFT JOIN " . DB_PREFIX . "customer_group cg ON (c.customer_group_id = cg.customer_group_id) ";
  122.  
  123.         $implode = array();
  124.  
  125.         if (isset($data['filter_name']) && !is_null($data['filter_name'])) {
  126.             $implode[] = "LCASE(CONCAT(c.firstname, ' ', c.lastname)) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";
  127.         }
  128.  
  129.         if (isset($data['filter_email']) && !is_null($data['filter_email'])) {
  130.             $implode[] = "LCASE(c.email) = '" . $this->db->escape(utf8_strtolower($data['filter_email'])) . "'";
  131.         }
  132.  
  133.         if (isset($data['filter_customer_group_id']) && !is_null($data['filter_customer_group_id'])) {
  134.             $implode[] = "cg.customer_group_id = '" . $this->db->escape($data['filter_customer_group_id']) . "'";
  135.         }  
  136.  
  137.         if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
  138.             $implode[] = "c.status = '" . (int)$data['filter_status'] . "'";
  139.         }  
  140.  
  141.         if (isset($data['filter_approved']) && !is_null($data['filter_approved'])) {
  142.             $implode[] = "c.approved = '" . (int)$data['filter_approved'] . "'";
  143.         }  
  144.  
  145.         if (isset($data['filter_ip']) && !is_null($data['filter_ip'])) {
  146.             $implode[] = "c.customer_id IN (SELECT customer_id FROM " . DB_PREFIX . "customer_ip WHERE ip = '" . $this->db->escape($data['filter_ip']) . "')";
  147.         }  
  148.  
  149.         if (isset($data['filter_date_added']) && !is_null($data['filter_date_added'])) {
  150.             $implode[] = "DATE(c.date_added) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
  151.         }
  152.  
  153.         if ($implode) {
  154.             $sql .= " WHERE " . implode(" AND ", $implode);
  155.         }
  156.  
  157.         $sort_data = array(
  158.             'name',
  159.             'c.email',
  160.             'customer_group',
  161.             'c.status',
  162.             'c.ip',
  163.             'c.date_added'
  164.         ); 
  165.  
  166.         if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
  167.             $sql .= " ORDER BY " . $data['sort'];  
  168.         } else {
  169.             $sql .= " ORDER BY name";  
  170.         }
  171.  
  172.         if (isset($data['order']) && ($data['order'] == 'DESC')) {
  173.             $sql .= " DESC";
  174.         } else {
  175.             $sql .= " ASC";
  176.         }
  177.  
  178.         if (isset($data['start']) || isset($data['limit'])) {
  179.             if ($data['start'] < 0) {
  180.                 $data['start'] = 0;
  181.             }          
  182.  
  183.             if ($data['limit'] < 1) {
  184.                 $data['limit'] = 20;
  185.             }  
  186.  
  187.             $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
  188.         }      
  189.  
  190.         $query = $this->db->query($sql);
  191.  
  192.         return $query->rows;   
  193.     }
  194.  
  195.     public function getTotalCustomersByEmail($email) {
  196.         $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "customer WHERE LOWER(email) = '" . $this->db->escape(utf8_strtolower($email)) . "'");
  197.  
  198.         return $query->row['total'];
  199.     }
  200.  
  201.     public function getIps($customer_id) {
  202.         $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "customer_ip` WHERE customer_id = '" . (int)$customer_id . "'");
  203.  
  204.         return $query->rows;
  205.     }  
  206.  
  207.     public function isBanIp($ip) {
  208.         $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "customer_ban_ip` WHERE ip = '" . $this->db->escape($ip) . "'");
  209.  
  210.         return $query->num_rows;
  211.     }  
  212. }
  213. ?>
Add Comment
Please, Sign In to add comment