-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
customer.php
218 lines (191 loc) · 5.88 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
namespace Opencart\System\Library\Cart;
class Customer {
private int $customer_id = 0;
private string $firstname = '';
private string $lastname = '';
private int $customer_group_id = 0;
private string $email = '';
private string $telephone = '';
private bool $newsletter = false;
private \Opencart\System\Engine\Config $config;
private \Opencart\System\Library\DB $db;
private \Opencart\System\Library\Request $request;
private \Opencart\System\Library\Session $session;
/**
* Constructor
*
* @param object $registry
*/
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->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();
}
}
}
/**
* Login
*
* @param string $email
* @param string $password
* @param bool $override
*
* @return bool
*/
public function login(string $email, string $password, bool $override = false): bool {
$customer_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "customer` WHERE LCASE(`email`) = '" . $this->db->escape(oc_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->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;
}
}
/**
* Logout
*
* @return void
*/
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;
}
/**
* isLogged
*
* @return bool
*/
public function isLogged(): bool {
return $this->customer_id ? true : false;
}
/**
* getId
*
* @return int
*/
public function getId(): int {
return $this->customer_id;
}
/**
* getFirstName
*
* @return string
*/
public function getFirstName(): string {
return $this->firstname;
}
/**
* getLastName
*
* @return string
*/
public function getLastName(): string {
return $this->lastname;
}
/**
* getGroupId
*
* @return int
*/
public function getGroupId(): int {
return $this->customer_group_id;
}
/**
* getEmail
*
* @return string
*/
public function getEmail(): string {
return $this->email;
}
/**
* getTelephone
*
* @return string
*/
public function getTelephone(): string {
return $this->telephone;
}
/**
* getNewsletter
*
* @return bool
*/
public function getNewsletter(): bool {
return $this->newsletter;
}
/**
* getAddressId
*
* @return int
*/
public function getAddressId(): int {
$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "address` WHERE `customer_id` = '" . (int)$this->customer_id . "' AND `default` = '1'");
if ($query->num_rows) {
return (int)$query->row['address_id'];
} else {
return 0;
}
}
/**
* getBalance
*
* @return float
*/
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'];
}
/**
* getRewardPoints
*
* @return float
*/
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'];
}
}