PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/catalog/model/account/customer.php

https://bitbucket.org/Hibiki08/goodlack
PHP | 178 lines | 131 code | 45 blank | 2 comment | 13 complexity | 6f25781adc77e9b6985d3582c5c27c4f MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  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. $this->load->model('account/customer_group');
  10. $customer_group_info = $this->model_account_customer_group->getCustomerGroup($customer_group_id);
  11. $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') . "', language_id = '" . (int)$this->config->get('config_language_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']) ? json_encode($data['custom_field']['account']) : '') . "', salt = '" . $this->db->escape($salt = token(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()");
  12. $customer_id = $this->db->getLastId();
  13. $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']) ? json_encode($data['custom_field']['address']) : '') . "'");
  14. $address_id = $this->db->getLastId();
  15. $this->db->query("UPDATE " . DB_PREFIX . "customer SET address_id = '" . (int)$address_id . "' WHERE customer_id = '" . (int)$customer_id . "'");
  16. $this->load->language('mail/customer');
  17. $subject = sprintf($this->language->get('text_subject'), html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8'));
  18. $message = sprintf($this->language->get('text_welcome'), html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8')) . "\n\n";
  19. if (!$customer_group_info['approval']) {
  20. $message .= $this->language->get('text_login') . "\n";
  21. } else {
  22. $message .= $this->language->get('text_approval') . "\n";
  23. }
  24. $message .= $this->url->link('account/login', '', true) . "\n\n";
  25. $message .= $this->language->get('text_services') . "\n\n";
  26. $message .= $this->language->get('text_thanks') . "\n";
  27. $message .= html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8');
  28. $mail = new Mail();
  29. $mail->protocol = $this->config->get('config_mail_protocol');
  30. $mail->parameter = $this->config->get('config_mail_parameter');
  31. $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
  32. $mail->smtp_username = $this->config->get('config_mail_smtp_username');
  33. $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
  34. $mail->smtp_port = $this->config->get('config_mail_smtp_port');
  35. $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');
  36. $mail->setTo($data['email']);
  37. $mail->setFrom($this->config->get('config_email'));
  38. $mail->setSender(html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8'));
  39. $mail->setSubject($subject);
  40. $mail->setText($message);
  41. $mail->send();
  42. // Send to main admin email if new account email is enabled
  43. if (in_array('account', (array)$this->config->get('config_mail_alert'))) {
  44. $message = $this->language->get('text_signup') . "\n\n";
  45. $message .= $this->language->get('text_website') . ' ' . html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8') . "\n";
  46. $message .= $this->language->get('text_firstname') . ' ' . $data['firstname'] . "\n";
  47. $message .= $this->language->get('text_lastname') . ' ' . $data['lastname'] . "\n";
  48. $message .= $this->language->get('text_customer_group') . ' ' . $customer_group_info['name'] . "\n";
  49. $message .= $this->language->get('text_email') . ' ' . $data['email'] . "\n";
  50. $message .= $this->language->get('text_telephone') . ' ' . $data['telephone'] . "\n";
  51. $mail = new Mail();
  52. $mail->protocol = $this->config->get('config_mail_protocol');
  53. $mail->parameter = $this->config->get('config_mail_parameter');
  54. $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
  55. $mail->smtp_username = $this->config->get('config_mail_smtp_username');
  56. $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
  57. $mail->smtp_port = $this->config->get('config_mail_smtp_port');
  58. $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');
  59. $mail->setTo($this->config->get('config_email'));
  60. $mail->setFrom($this->config->get('config_email'));
  61. $mail->setSender(html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8'));
  62. $mail->setSubject(html_entity_decode($this->language->get('text_new_customer'), ENT_QUOTES, 'UTF-8'));
  63. $mail->setText($message);
  64. $mail->send();
  65. // Send to additional alert emails if new account email is enabled
  66. $emails = explode(',', $this->config->get('config_alert_email'));
  67. foreach ($emails as $email) {
  68. if (utf8_strlen($email) > 0 && preg_match($this->config->get('config_mail_regexp'), $email)) {
  69. $mail->setTo($email);
  70. $mail->send();
  71. }
  72. }
  73. }
  74. return $customer_id;
  75. }
  76. public function editCustomer($data) {
  77. $customer_id = $this->customer->getId();
  78. $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']) ? json_encode($data['custom_field']) : '') . "' WHERE customer_id = '" . (int)$customer_id . "'");
  79. }
  80. public function editPassword($email, $password) {
  81. $this->db->query("UPDATE " . DB_PREFIX . "customer SET salt = '" . $this->db->escape($salt = token(9)) . "', password = '" . $this->db->escape(sha1($salt . sha1($salt . sha1($password)))) . "', code = '' WHERE LOWER(email) = '" . $this->db->escape(utf8_strtolower($email)) . "'");
  82. }
  83. public function editCode($email, $code) {
  84. $this->db->query("UPDATE `" . DB_PREFIX . "customer` SET code = '" . $this->db->escape($code) . "' WHERE LCASE(email) = '" . $this->db->escape(utf8_strtolower($email)) . "'");
  85. }
  86. public function editNewsletter($newsletter) {
  87. $this->db->query("UPDATE " . DB_PREFIX . "customer SET newsletter = '" . (int)$newsletter . "' WHERE customer_id = '" . (int)$this->customer->getId() . "'");
  88. }
  89. public function getCustomer($customer_id) {
  90. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE customer_id = '" . (int)$customer_id . "'");
  91. return $query->row;
  92. }
  93. public function getCustomerByEmail($email) {
  94. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE LOWER(email) = '" . $this->db->escape(utf8_strtolower($email)) . "'");
  95. return $query->row;
  96. }
  97. public function getCustomerByCode($code) {
  98. $query = $this->db->query("SELECT customer_id, firstname, lastname, email FROM `" . DB_PREFIX . "customer` WHERE code = '" . $this->db->escape($code) . "' AND code != ''");
  99. return $query->row;
  100. }
  101. public function getCustomerByToken($token) {
  102. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE token = '" . $this->db->escape($token) . "' AND token != ''");
  103. $this->db->query("UPDATE " . DB_PREFIX . "customer SET token = ''");
  104. return $query->row;
  105. }
  106. public function getTotalCustomersByEmail($email) {
  107. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "customer WHERE LOWER(email) = '" . $this->db->escape(utf8_strtolower($email)) . "'");
  108. return $query->row['total'];
  109. }
  110. public function getRewardTotal($customer_id) {
  111. $query = $this->db->query("SELECT SUM(points) AS total FROM " . DB_PREFIX . "customer_reward WHERE customer_id = '" . (int)$customer_id . "'");
  112. return $query->row['total'];
  113. }
  114. public function getIps($customer_id) {
  115. $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "customer_ip` WHERE customer_id = '" . (int)$customer_id . "'");
  116. return $query->rows;
  117. }
  118. public function addLoginAttempt($email) {
  119. $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']) . "'");
  120. if (!$query->num_rows) {
  121. $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')) . "'");
  122. } else {
  123. $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'] . "'");
  124. }
  125. }
  126. public function getLoginAttempts($email) {
  127. $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "customer_login` WHERE email = '" . $this->db->escape(utf8_strtolower($email)) . "'");
  128. return $query->row;
  129. }
  130. public function deleteLoginAttempts($email) {
  131. $this->db->query("DELETE FROM `" . DB_PREFIX . "customer_login` WHERE email = '" . $this->db->escape(utf8_strtolower($email)) . "'");
  132. }
  133. }