/
customer.php
139 lines (112 loc) · 4.84 KB
/
customer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace Opencart\System\Library\Cart;
class Customer {
private $customer_id = 0;
private $firstname = '';
private $lastname = '';
private $customer_group_id = 0;
private $email = '';
private $telephone = '';
private $newsletter = false;
private $address_id = 0;
public function __construct(\Opencart\System\Engine\Registry $registry) {
$this->config = $registry->get('config');
$this->db = $registry->get('db');
$this->request = $registry->get('request');
$this->session = $registry->get('session');
if (isset($this->session->data['customer_id'])) {
$customer_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "customer` WHERE `customer_id` = '" . (int)$this->session->data['customer_id'] . "' AND `status` = '1'");
if ($customer_query->num_rows) {
$this->customer_id = $customer_query->row['customer_id'];
$this->firstname = $customer_query->row['firstname'];
$this->lastname = $customer_query->row['lastname'];
$this->customer_group_id = $customer_query->row['customer_group_id'];
$this->email = $customer_query->row['email'];
$this->telephone = $customer_query->row['telephone'];
$this->newsletter = $customer_query->row['newsletter'];
$this->address_id = $customer_query->row['address_id'];
$this->db->query("UPDATE `" . DB_PREFIX . "customer` SET `language_id` = '" . (int)$this->config->get('config_language_id') . "', `ip` = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "' WHERE `customer_id` = '" . (int)$this->customer_id . "'");
} else {
$this->logout();
}
}
}
public function login(string $email, string $password, bool $override = false): bool {
$customer_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "customer` WHERE LOWER(`email`) = '" . $this->db->escape(utf8_strtolower($email)) . "' AND `status` = '1'");
if ($customer_query->row) {
if (!$override) {
if (password_verify($password, $customer_query->row['password'])) {
$rehash = password_needs_rehash($customer_query->row['password'], PASSWORD_DEFAULT);
} elseif (isset($customer_query->row['salt']) && $customer_query->row['password'] == sha1($customer_query->row['salt'] . sha1($customer_query->row['salt'] . sha1($password)))) {
$rehash = true;
} elseif ($customer_query->row['password'] == md5($password)) {
$rehash = true;
} else {
return false;
}
if ($rehash) {
$this->db->query("UPDATE `" . DB_PREFIX . "customer` SET `password` = '" . $this->db->escape(password_hash($password, PASSWORD_DEFAULT)) . "' WHERE `customer_id` = '" . (int)$customer_query->row['customer_id'] . "'");
}
}
$this->session->data['customer_id'] = $customer_query->row['customer_id'];
$this->customer_id = $customer_query->row['customer_id'];
$this->firstname = $customer_query->row['firstname'];
$this->lastname = $customer_query->row['lastname'];
$this->customer_group_id = $customer_query->row['customer_group_id'];
$this->email = $customer_query->row['email'];
$this->telephone = $customer_query->row['telephone'];
$this->newsletter = $customer_query->row['newsletter'];
$this->address_id = $customer_query->row['address_id'];
$this->db->query("UPDATE `" . DB_PREFIX . "customer` SET `language_id` = '" . (int)$this->config->get('config_language_id') . "', `ip` = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "' WHERE `customer_id` = '" . (int)$this->customer_id . "'");
return true;
} else {
return false;
}
}
public function logout(): void {
unset($this->session->data['customer_id']);
$this->customer_id = 0;
$this->firstname = '';
$this->lastname = '';
$this->customer_group_id = 0;
$this->email = '';
$this->telephone = '';
$this->newsletter = false;
$this->address_id = 0;
}
public function isLogged(): bool {
return $this->customer_id ? true : false;
}
public function getId(): int {
return $this->customer_id;
}
public function getFirstName(): string {
return $this->firstname;
}
public function getLastName(): string {
return $this->lastname;
}
public function getGroupId(): int {
return $this->customer_group_id;
}
public function getEmail(): string {
return $this->email;
}
public function getTelephone(): string {
return $this->telephone;
}
public function getNewsletter(): bool {
return $this->newsletter;
}
public function getAddressId(): int {
return $this->address_id;
}
public function getBalance(): float {
$query = $this->db->query("SELECT SUM(`amount`) AS `total` FROM `" . DB_PREFIX . "customer_transaction` WHERE `customer_id` = '" . (int)$this->customer_id . "'");
return (float)$query->row['total'];
}
public function getRewardPoints(): float {
$query = $this->db->query("SELECT SUM(`points`) AS `total` FROM `" . DB_PREFIX . "customer_reward` WHERE `customer_id` = '" . (int)$this->customer_id . "'");
return (float)$query->row['total'];
}
}