1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* Divine CMS - Open source CMS for widespread use. |
4
|
|
|
Copyright (c) 2019 Mykola Burakov ([email protected]) |
5
|
|
|
|
6
|
|
|
See SOURCE.txt for other and additional information. |
7
|
|
|
|
8
|
|
|
This file is part of Divine CMS. |
9
|
|
|
|
10
|
|
|
This program is free software: you can redistribute it and/or modify |
11
|
|
|
it under the terms of the GNU General Public License as published by |
12
|
|
|
the Free Software Foundation, either version 3 of the License, or |
13
|
|
|
(at your option) any later version. |
14
|
|
|
|
15
|
|
|
This program is distributed in the hope that it will be useful, |
16
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
GNU General Public License for more details. |
19
|
|
|
|
20
|
|
|
You should have received a copy of the GNU General Public License |
21
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
22
|
|
|
|
23
|
|
|
class ModelAccountCustomer extends \Divine\Engine\Core\Model |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
public function addCustomer($data) |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
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'))) { |
28
|
|
|
$customer_group_id = $data['customer_group_id']; |
29
|
|
|
} else { |
30
|
|
|
$customer_group_id = $this->config->get('config_customer_group_id'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$this->load->model('account/customer_group'); |
34
|
|
|
|
35
|
|
|
$customer_group_info = $this->model_account_customer_group->getCustomerGroup($customer_group_id); |
36
|
|
|
|
37
|
|
|
$this->db->query(" |
38
|
|
|
INSERT INTO customer |
39
|
|
|
SET customer_group_id = '" . (int)$customer_group_id . "', |
40
|
|
|
language_id = '" . (int)$this->config->get('config_language_id') . "', |
41
|
|
|
firstname = '" . $this->db->escape($data['firstname']) . "', |
42
|
|
|
lastname = '" . $this->db->escape($data['lastname']) . "', |
43
|
|
|
email = '" . $this->db->escape($data['email']) . "', |
44
|
|
|
telephone = '" . $this->db->escape($data['telephone']) . "', |
45
|
|
|
fax = '" . $this->db->escape($data['fax']) . "', |
46
|
|
|
custom_field = '" . $this->db->escape(isset($data['custom_field']['account']) ? json_encode($data['custom_field']['account']) : '') . "', |
47
|
|
|
salt = '" . $this->db->escape($salt = (new \Tokenly\TokenGenerator\TokenGenerator())->generateToken(9, 'SR')) . "', |
48
|
|
|
password = '" . $this->db->escape(sha1($salt . sha1($salt . sha1($data['password'])))) . "', |
49
|
|
|
newsletter = '" . (isset($data['newsletter']) ? (int)$data['newsletter'] : 0) . "', ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "', |
50
|
|
|
status = '1', |
51
|
|
|
approved = '" . (int)!$customer_group_info['approval'] . "', |
52
|
|
|
date_added = NOW() |
53
|
|
|
"); |
54
|
|
|
|
55
|
|
|
$customer_id = $this->db->getLastId(); |
56
|
|
|
|
57
|
|
|
$this->db->query(" |
58
|
|
|
INSERT INTO address |
59
|
|
|
SET customer_id = '" . (int)$customer_id . "', |
60
|
|
|
firstname = '" . $this->db->escape($data['firstname']) . "', |
61
|
|
|
lastname = '" . $this->db->escape($data['lastname']) . "', |
62
|
|
|
company = '" . $this->db->escape($data['company']) . "', |
63
|
|
|
address_1 = '" . $this->db->escape($data['address_1']) . "', |
64
|
|
|
address_2 = '" . $this->db->escape($data['address_2']) . "', |
65
|
|
|
city = '" . $this->db->escape($data['city']) . "', |
66
|
|
|
postcode = '" . $this->db->escape($data['postcode']) . "', |
67
|
|
|
country_id = '" . (int)$data['country_id'] . "', |
68
|
|
|
zone_id = '" . (int)$data['zone_id'] . "', |
69
|
|
|
custom_field = '" . $this->db->escape(isset($data['custom_field']['address']) ? json_encode($data['custom_field']['address']) : '') . "' |
70
|
|
|
"); |
71
|
|
|
|
72
|
|
|
$address_id = $this->db->getLastId(); |
73
|
|
|
|
74
|
|
|
$this->db->query(" |
75
|
|
|
UPDATE customer |
76
|
|
|
SET address_id = '" . (int)$address_id . "' |
77
|
|
|
WHERE customer_id = '" . (int)$customer_id . "' |
78
|
|
|
"); |
79
|
|
|
|
80
|
|
|
$this->load->language('mail/customer'); |
81
|
|
|
|
82
|
|
|
$subject = sprintf($this->language->get('text_subject'), html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8')); |
83
|
|
|
|
84
|
|
|
$message = sprintf($this->language->get('text_welcome'), html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8')) . "\n\n"; |
85
|
|
|
|
86
|
|
|
if (!$customer_group_info['approval']) { |
87
|
|
|
$message .= $this->language->get('text_login') . "\n"; |
88
|
|
|
} else { |
89
|
|
|
$message .= $this->language->get('text_approval') . "\n"; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$message .= $this->url->link('account/login', '', true) . "\n\n"; |
93
|
|
|
$message .= $this->language->get('text_services') . "\n\n"; |
94
|
|
|
$message .= $this->language->get('text_thanks') . "\n"; |
95
|
|
|
$message .= html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8'); |
96
|
|
|
|
97
|
|
|
$mail = new \Divine\Engine\Library\Mail(); |
98
|
|
|
$mail->protocol = $this->config->get('config_mail_protocol'); |
99
|
|
|
$mail->parameter = $this->config->get('config_mail_parameter'); |
100
|
|
|
$mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname'); |
101
|
|
|
$mail->smtp_username = $this->config->get('config_mail_smtp_username'); |
102
|
|
|
$mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8'); |
103
|
|
|
$mail->smtp_port = $this->config->get('config_mail_smtp_port'); |
104
|
|
|
$mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout'); |
105
|
|
|
|
106
|
|
|
$mail->setTo($data['email']); |
107
|
|
|
$mail->setFrom($this->config->get('config_email')); |
108
|
|
|
$mail->setSender(html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8')); |
109
|
|
|
$mail->setSubject($subject); |
110
|
|
|
$mail->setText($message); |
111
|
|
|
$mail->send(); |
112
|
|
|
|
113
|
|
|
// Send to main admin email if new account email is enabled |
114
|
|
|
if (in_array('account', (array)$this->config->get('config_mail_alert'))) { |
115
|
|
|
$message = $this->language->get('text_signup') . "\n\n"; |
116
|
|
|
$message .= $this->language->get('text_website') . ' ' . html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8') . "\n"; |
117
|
|
|
$message .= $this->language->get('text_firstname') . ' ' . $data['firstname'] . "\n"; |
118
|
|
|
$message .= $this->language->get('text_lastname') . ' ' . $data['lastname'] . "\n"; |
119
|
|
|
$message .= $this->language->get('text_customer_group') . ' ' . $customer_group_info['name'] . "\n"; |
120
|
|
|
$message .= $this->language->get('text_email') . ' ' . $data['email'] . "\n"; |
121
|
|
|
$message .= $this->language->get('text_telephone') . ' ' . $data['telephone'] . "\n"; |
122
|
|
|
|
123
|
|
|
$mail = new \Divine\Engine\Library\Mail(); |
124
|
|
|
$mail->protocol = $this->config->get('config_mail_protocol'); |
125
|
|
|
$mail->parameter = $this->config->get('config_mail_parameter'); |
126
|
|
|
$mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname'); |
127
|
|
|
$mail->smtp_username = $this->config->get('config_mail_smtp_username'); |
128
|
|
|
$mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8'); |
129
|
|
|
$mail->smtp_port = $this->config->get('config_mail_smtp_port'); |
130
|
|
|
$mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout'); |
131
|
|
|
|
132
|
|
|
$mail->setTo($this->config->get('config_email')); |
133
|
|
|
$mail->setFrom($this->config->get('config_email')); |
134
|
|
|
$mail->setSender(html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8')); |
135
|
|
|
$mail->setSubject(html_entity_decode($this->language->get('text_new_customer'), ENT_QUOTES, 'UTF-8')); |
136
|
|
|
$mail->setText($message); |
137
|
|
|
$mail->send(); |
138
|
|
|
|
139
|
|
|
// Send to additional alert emails if new account email is enabled |
140
|
|
|
$emails = explode(',', $this->config->get('config_alert_email')); |
141
|
|
|
|
142
|
|
|
foreach ($emails as $email) { |
143
|
|
|
if (\voku\helper\UTF8::strlen($email) > 0 && filter_var($email, FILTER_VALIDATE_EMAIL)) { |
144
|
|
|
$mail->setTo($email); |
145
|
|
|
$mail->send(); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $customer_id; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function editCustomer($data) |
154
|
|
|
{ |
155
|
|
|
$customer_id = $this->customer->getId(); |
156
|
|
|
|
157
|
|
|
$this->db->query(" |
158
|
|
|
UPDATE customer |
159
|
|
|
SET firstname = '" . $this->db->escape($data['firstname']) . "', |
160
|
|
|
lastname = '" . $this->db->escape($data['lastname']) . "', |
161
|
|
|
email = '" . $this->db->escape($data['email']) . "', |
162
|
|
|
telephone = '" . $this->db->escape($data['telephone']) . "', |
163
|
|
|
fax = '" . $this->db->escape($data['fax']) . "', |
164
|
|
|
custom_field = '" . $this->db->escape(isset($data['custom_field']) ? json_encode($data['custom_field']) : '') . "' |
165
|
|
|
WHERE customer_id = '" . (int)$customer_id . "' |
166
|
|
|
"); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function editPassword($email, $password) |
170
|
|
|
{ |
171
|
|
|
$this->db->query(" |
172
|
|
|
UPDATE customer |
173
|
|
|
SET salt = '" . $this->db->escape($salt = (new \Tokenly\TokenGenerator\TokenGenerator())->generateToken(9, 'SR')) . "', |
174
|
|
|
password = '" . $this->db->escape(sha1($salt . sha1($salt . sha1($password)))) . "', |
175
|
|
|
code = '' WHERE LOWER(email) = '" . $this->db->escape(\voku\helper\UTF8::strtolower($email)) . "' |
176
|
|
|
"); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function editCode($email, $code) |
180
|
|
|
{ |
181
|
|
|
$this->db->query(" |
182
|
|
|
UPDATE `customer` |
183
|
|
|
SET code = '" . $this->db->escape($code) . "' |
184
|
|
|
WHERE LCASE(email) = '" . $this->db->escape(\voku\helper\UTF8::strtolower($email)) . "' |
185
|
|
|
"); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function editNewsletter($newsletter) |
189
|
|
|
{ |
190
|
|
|
$this->db->query(" |
191
|
|
|
UPDATE customer |
192
|
|
|
SET newsletter = '" . (int)$newsletter . "' |
193
|
|
|
WHERE customer_id = '" . (int)$this->customer->getId() . "' |
194
|
|
|
"); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function getCustomer($customer_id) |
198
|
|
|
{ |
199
|
|
|
$query = $this->db->query(" |
200
|
|
|
SELECT * |
201
|
|
|
FROM customer |
202
|
|
|
WHERE customer_id = '" . (int)$customer_id . "' |
203
|
|
|
"); |
204
|
|
|
|
205
|
|
|
return $query->row; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function getCustomerByEmail($email) |
209
|
|
|
{ |
210
|
|
|
$query = $this->db->query(" |
211
|
|
|
SELECT * |
212
|
|
|
FROM customer |
213
|
|
|
WHERE LOWER(email) = '" . $this->db->escape(\voku\helper\UTF8::strtolower($email)) . "' |
214
|
|
|
"); |
215
|
|
|
|
216
|
|
|
return $query->row; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function getCustomerByCode($code) |
220
|
|
|
{ |
221
|
|
|
$query = $this->db->query(" |
222
|
|
|
SELECT customer_id, firstname, lastname, email |
223
|
|
|
FROM `customer` |
224
|
|
|
WHERE code = '" . $this->db->escape($code) . "' AND code != '' |
225
|
|
|
"); |
226
|
|
|
|
227
|
|
|
return $query->row; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function getCustomerByToken($token) |
231
|
|
|
{ |
232
|
|
|
$query = $this->db->query(" |
233
|
|
|
SELECT * |
234
|
|
|
FROM customer |
235
|
|
|
WHERE token = '" . $this->db->escape($token) . "' |
236
|
|
|
AND token != '' |
237
|
|
|
"); |
238
|
|
|
|
239
|
|
|
$this->db->query("UPDATE customer SET token = ''"); |
240
|
|
|
|
241
|
|
|
return $query->row; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function getTotalCustomersByEmail($email) |
245
|
|
|
{ |
246
|
|
|
$query = $this->db->query(" |
247
|
|
|
SELECT COUNT(*) AS total |
248
|
|
|
FROM customer |
249
|
|
|
WHERE LOWER(email) = '" . $this->db->escape(\voku\helper\UTF8::strtolower($email)) . "' |
250
|
|
|
"); |
251
|
|
|
|
252
|
|
|
return $query->row['total']; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
public function getRewardTotal($customer_id) |
256
|
|
|
{ |
257
|
|
|
$query = $this->db->query(" |
258
|
|
|
SELECT SUM(points) AS total |
259
|
|
|
FROM customer_reward |
260
|
|
|
WHERE customer_id = '" . (int)$customer_id . "' |
261
|
|
|
"); |
262
|
|
|
|
263
|
|
|
return $query->row['total']; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
public function getIps($customer_id) |
267
|
|
|
{ |
268
|
|
|
$query = $this->db->query(" |
269
|
|
|
SELECT * |
270
|
|
|
FROM `customer_ip` |
271
|
|
|
WHERE customer_id = '" . (int)$customer_id . "' |
272
|
|
|
"); |
273
|
|
|
|
274
|
|
|
return $query->rows; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
public function addLoginAttempt($email) |
278
|
|
|
{ |
279
|
|
|
$query = $this->db->query(" |
280
|
|
|
SELECT * |
281
|
|
|
FROM customer_login |
282
|
|
|
WHERE email = '" . $this->db->escape(\voku\helper\UTF8::strtolower((string)$email)) . "' |
283
|
|
|
AND ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "' |
284
|
|
|
"); |
285
|
|
|
|
286
|
|
|
if (!$query->num_rows) { |
287
|
|
|
$this->db->query(" |
288
|
|
|
INSERT INTO customer_login |
289
|
|
|
SET email = '" . $this->db->escape(\voku\helper\UTF8::strtolower((string)$email)) . "', |
290
|
|
|
ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "', |
291
|
|
|
total = 1, date_added = '" . $this->db->escape(date('Y-m-d H:i:s')) . "', |
292
|
|
|
date_modified = '" . $this->db->escape(date('Y-m-d H:i:s')) . "' |
293
|
|
|
"); |
294
|
|
|
} else { |
295
|
|
|
$this->db->query(" |
296
|
|
|
UPDATE customer_login |
297
|
|
|
SET total = (total + 1), |
298
|
|
|
date_modified = '" . $this->db->escape(date('Y-m-d H:i:s')) . "' |
299
|
|
|
WHERE customer_login_id = '" . (int)$query->row['customer_login_id'] . "' |
300
|
|
|
"); |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
public function getLoginAttempts($email) |
305
|
|
|
{ |
306
|
|
|
$query = $this->db->query(" |
307
|
|
|
SELECT * |
308
|
|
|
FROM `customer_login` |
309
|
|
|
WHERE email = '" . $this->db->escape(\voku\helper\UTF8::strtolower($email)) . "' |
310
|
|
|
"); |
311
|
|
|
|
312
|
|
|
return $query->row; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
public function deleteLoginAttempts($email) |
316
|
|
|
{ |
317
|
|
|
$this->db->query(" |
318
|
|
|
DELETE |
319
|
|
|
FROM `customer_login` |
320
|
|
|
WHERE email = '" . $this->db->escape(\voku\helper\UTF8::strtolower($email)) . "' |
321
|
|
|
"); |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.