PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/catalog/model/account/customer.php

https://gitlab.com/hazelnuts23/unitedfoodstuff
PHP | 184 lines | 132 code | 50 blank | 2 comment | 12 complexity | 87b9f07f369f6e618faec54cfbc82934 MD5 | raw file
  1. <?php
  2. class ModelAccountCustomer extends Model {
  3. public function addCustomer($data) {
  4. $this->event->trigger('pre.customer.add', $data);
  5. 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'))) {
  6. $customer_group_id = $data['customer_group_id'];
  7. } else {
  8. $customer_group_id = $this->config->get('config_customer_group_id');
  9. }
  10. $this->load->model('account/customer_group');
  11. $customer_group_info = $this->model_account_customer_group->getCustomerGroup($customer_group_id);
  12. $this->db->query("INSERT INTO " . DB_PREFIX . "customer SET customer_group_id = '" . (int)$customer_group_id . "', 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']) . "', custom_field = '" . $this->db->escape(isset($data['custom_field']['account']) ? serialize($data['custom_field']['account']) : '') . "', 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) . "', ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "', status = '1', approved = '" . (int)!$customer_group_info['approval'] . "', date_added = NOW()");
  13. $customer_id = $this->db->getLastId();
  14. $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']) . "', 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'] . "', custom_field = '" . $this->db->escape(isset($data['custom_field']['address']) ? serialize($data['custom_field']['address']) : '') . "'");
  15. $address_id = $this->db->getLastId();
  16. $this->db->query("UPDATE " . DB_PREFIX . "customer SET address_id = '" . (int)$address_id . "' WHERE customer_id = '" . (int)$customer_id . "'");
  17. $this->load->language('mail/customer');
  18. $subject = sprintf($this->language->get('text_subject'), html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8'));
  19. $message = sprintf($this->language->get('text_welcome'), html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8')) . "\n\n";
  20. if (!$customer_group_info['approval']) {
  21. $message .= $this->language->get('text_login') . "\n";
  22. } else {
  23. $message .= $this->language->get('text_approval') . "\n";
  24. }
  25. $message .= $this->url->link('account/login', '', 'SSL') . "\n\n";
  26. $message .= $this->language->get('text_services') . "\n\n";
  27. $message .= $this->language->get('text_thanks') . "\n";
  28. $message .= html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8');
  29. $mail = new Mail();
  30. $mail->protocol = $this->config->get('config_mail_protocol');
  31. $mail->parameter = $this->config->get('config_mail_parameter');
  32. $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
  33. $mail->smtp_username = $this->config->get('config_mail_smtp_username');
  34. $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
  35. $mail->smtp_port = $this->config->get('config_mail_smtp_port');
  36. $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');
  37. $mail->setTo($data['email']);
  38. $mail->setFrom($this->config->get('config_email'));
  39. $mail->setSender(html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8'));
  40. $mail->setSubject($subject);
  41. $mail->setText($message);
  42. $mail->send();
  43. // Send to main admin email if new account email is enabled
  44. if ($this->config->get('config_account_mail')) {
  45. $message = $this->language->get('text_signup') . "\n\n";
  46. $message .= $this->language->get('text_website') . ' ' . html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8') . "\n";
  47. $message .= $this->language->get('text_firstname') . ' ' . $data['firstname'] . "\n";
  48. $message .= $this->language->get('text_lastname') . ' ' . $data['lastname'] . "\n";
  49. $message .= $this->language->get('text_customer_group') . ' ' . $customer_group_info['name'] . "\n";
  50. $message .= $this->language->get('text_email') . ' ' . $data['email'] . "\n";
  51. $message .= $this->language->get('text_telephone') . ' ' . $data['telephone'] . "\n";
  52. $mail = new Mail();
  53. $mail->protocol = $this->config->get('config_mail_protocol');
  54. $mail->parameter = $this->config->get('config_mail_parameter');
  55. $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
  56. $mail->smtp_username = $this->config->get('config_mail_smtp_username');
  57. $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
  58. $mail->smtp_port = $this->config->get('config_mail_smtp_port');
  59. $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');
  60. $mail->setTo($this->config->get('config_email'));
  61. $mail->setFrom($this->config->get('config_email'));
  62. $mail->setSender(html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8'));
  63. $mail->setSubject(html_entity_decode($this->language->get('text_new_customer'), ENT_QUOTES, 'UTF-8'));
  64. $mail->setText($message);
  65. $mail->send();
  66. // Send to additional alert emails if new account email is enabled
  67. $emails = explode(',', $this->config->get('config_mail_alert'));
  68. foreach ($emails as $email) {
  69. if (utf8_strlen($email) > 0 && preg_match('/^[^\@]+@.*.[a-z]{2,15}$/i', $email)) {
  70. $mail->setTo($email);
  71. $mail->send();
  72. }
  73. }
  74. }
  75. $this->event->trigger('post.customer.add', $customer_id);
  76. return $customer_id;
  77. }
  78. public function editCustomer($data) {
  79. $this->event->trigger('pre.customer.edit', $data);
  80. $customer_id = $this->customer->getId();
  81. $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']) . "', custom_field = '" . $this->db->escape(isset($data['custom_field']) ? serialize($data['custom_field']) : '') . "' WHERE customer_id = '" . (int)$customer_id . "'");
  82. $this->event->trigger('post.customer.edit', $customer_id);
  83. }
  84. public function editPassword($email, $password) {
  85. $this->event->trigger('pre.customer.edit.password');
  86. $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)) . "'");
  87. $this->event->trigger('post.customer.edit.password');
  88. }
  89. public function editNewsletter($newsletter) {
  90. $this->event->trigger('pre.customer.edit.newsletter');
  91. $this->db->query("UPDATE " . DB_PREFIX . "customer SET newsletter = '" . (int)$newsletter . "' WHERE customer_id = '" . (int)$this->customer->getId() . "'");
  92. $this->event->trigger('post.customer.edit.newsletter');
  93. }
  94. public function getCustomer($customer_id) {
  95. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE customer_id = '" . (int)$customer_id . "'");
  96. return $query->row;
  97. }
  98. public function getCustomerByEmail($email) {
  99. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE LOWER(email) = '" . $this->db->escape(utf8_strtolower($email)) . "'");
  100. return $query->row;
  101. }
  102. public function getCustomerByToken($token) {
  103. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE token = '" . $this->db->escape($token) . "' AND token != ''");
  104. $this->db->query("UPDATE " . DB_PREFIX . "customer SET token = ''");
  105. return $query->row;
  106. }
  107. public function getTotalCustomersByEmail($email) {
  108. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "customer WHERE LOWER(email) = '" . $this->db->escape(utf8_strtolower($email)) . "'");
  109. return $query->row['total'];
  110. }
  111. public function getIps($customer_id) {
  112. $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "customer_ip` WHERE customer_id = '" . (int)$customer_id . "'");
  113. return $query->rows;
  114. }
  115. public function isBanIp($ip) {
  116. $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "customer_ban_ip` WHERE ip = '" . $this->db->escape($ip) . "'");
  117. return $query->num_rows;
  118. }
  119. public function addLoginAttempt($email) {
  120. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer_login WHERE email = '" . $this->db->escape(utf8_strtolower((string)$email)) . "' AND ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "'");
  121. if (!$query->num_rows) {
  122. $this->db->query("INSERT INTO " . DB_PREFIX . "customer_login SET email = '" . $this->db->escape(utf8_strtolower((string)$email)) . "', ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "', total = 1, date_added = '" . $this->db->escape(date('Y-m-d H:i:s')) . "', date_modified = '" . $this->db->escape(date('Y-m-d H:i:s')) . "'");
  123. } else {
  124. $this->db->query("UPDATE " . DB_PREFIX . "customer_login SET total = (total + 1), date_modified = '" . $this->db->escape(date('Y-m-d H:i:s')) . "' WHERE customer_login_id = '" . (int)$query->row['customer_login_id'] . "'");
  125. }
  126. }
  127. public function getLoginAttempts($email) {
  128. $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "customer_login` WHERE email = '" . $this->db->escape(utf8_strtolower($email)) . "'");
  129. return $query->row;
  130. }
  131. public function deleteLoginAttempts($email) {
  132. $this->db->query("DELETE FROM `" . DB_PREFIX . "customer_login` WHERE email = '" . $this->db->escape(utf8_strtolower($email)) . "'");
  133. }
  134. }