-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.php
449 lines (434 loc) · 14.5 KB
/
db.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<?php
/*
# -- BEGIN LICENSE BLOCK ----------------------------------
#
# This file is part of MAGIX CMS.
# MAGIX CMS, The content management system optimized for users
# Copyright (C) 2008 - 2018 magix-cms.com support[at]magix-cms[point]com
#
# OFFICIAL TEAM :
#
# * Gerits Aurelien (Author - Developer) <[email protected]> <[email protected]>
#
# Redistributions of files must retain the above copyright notice.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# -- END LICENSE BLOCK -----------------------------------
#
# DISCLAIMER
#
# Do not edit or add to this file if you wish to upgrade MAGIX CMS to newer
# versions in the future. If you wish to customize MAGIX CMS for your
# needs please refer to http://www.magix-cms.com for more information.
*/
class plugins_account_db {
/**
* @param array $config
* @param array $params
* @return array|bool
*/
public function fetchData(array $config, array $params = []) {
if ($config['context'] === 'all') {
switch ($config['type']) {
case 'accounts':
$limit = '';
if ($config['offset']) {
$limit = ' LIMIT 0, ' . $config['offset'];
if (isset($config['page']) && $config['page'] > 1) {
$limit = ' LIMIT ' . (($config['page'] - 1) * $config['offset']) . ', ' . $config['offset'];
}
}
$cond = '';
if(isset($config['search'])) {
if(is_array($config['search'])) {
$nbc = 0;
foreach ($config['search'] as $key => $q) {
if($q !== '') {
$cond .= !$nbc ? ' WHERE ' : 'AND ';
switch ($key) {
case 'id_account':
case 'active_ac':
$cond .= 'ma.'.$key.' = '.$q.' ';
break;
case 'iso_lang':
$cond .= 'ml.id_lang = '.$q.' ';
break;
case 'email_ac':
case 'firstname_ac':
case 'lastname_ac':
$cond .= "ma.".$key." LIKE '%".$q."%' ";
break;
case 'date_register':
$dateFormat = new component_format_date();
$q = $dateFormat->date_to_db_format($q);
$cond .= "ma.".$key." LIKE '%".$q."%' ";
break;
}
$nbc++;
}
}
}
}
if(isset($params['where'])) {
unset($params['where']);
}
$select = ['ma.id_account', 'ml.iso_lang', 'ma.email_ac', 'ma.firstname_ac', 'ma.lastname_ac', 'ma.active_ac', 'ma.date_create'];
if(isset($params['select'])) {
foreach ($params['select'] as $extendSelect) {
$select = array_merge($select, $extendSelect);
}
unset($params['select']);
}
$joins = '';
if(isset($params['join']) && is_array($params['join'])) {
foreach ($params['join'] as $join) {
$joins .= ' '.$join['type'].' '.$join['table'].' '.$join['as'].' ON ('.$join['on']['table'].'.'.$join['on']['key'].' = '.$join['as'].'.'.$join['on']['key'].') ';
}
unset($params['join']);
}
$query = 'SELECT '.implode(',', $select).'
FROM `mc_account` ma
JOIN `mc_lang` ml USING(`id_lang`)'.$joins.$cond. ' GROUP By id_account ORDER BY id_account DESC'. $limit;
break;
case 'search_account':
$cond = '';
$query = "SELECT
ma.id_account
FROM mc_account ma
WHERE $cond AND
ma.pseudo_ac LIKE :resume_needle
ORDER BY ma.pseudo_ac DESC";
unset($params['needle']);
break;
case 'search_ft_account':
$cond = '';
$query = "SELECT
ma.id_account
FROM mc_account ma
WHERE $cond AND
AND (
ma.pseudo_ac LIKE :name_needle
) ORDER BY ma.pseudo_ac DESC";
break;
default:
return false;
}
try {
return component_routing_db::layer()->fetchAll($query, $params);
}
catch (Exception $e) {
if(!isset($this->logger)) $this->logger = new debug_logger(MP_LOG_DIR);
$this->logger->log('statement','db',$e->getMessage(),$this->logger::LOG_MONTH);
}
}
elseif ($config['context'] === 'one') {
switch ($config['type']) {
case 'account':
$query = 'SELECT
a.*,
asos.*
FROM `mc_account` as a
JOIN `mc_account_social` as asos USING(`id_account`)
JOIN `mc_lang` as l USING(`id_lang`)
WHERE `id_account` = :id';
break;
case 'billing_address':
$query = "SELECT
*
FROM `mc_account_address`
WHERE `id_account` = :id AND `type_address` = 'billing'";
break;
case 'delivery_address':
$query = "SELECT
*
FROM `mc_account_address`
WHERE `id_account` = :id AND `type_address` = 'delivery'";
break;
case 'config':
$query = 'SELECT * FROM `mc_account_config` ORDER BY id_config DESC LIMIt 0,1';
break;
case 'accountEmail':
$query = 'SELECT *
FROM `mc_account` as a
JOIN `mc_account_address` as aa USING(`id_account`)
JOIN `mc_account_social` as asos USING(`id_account`)
JOIN `mc_lang` as l USING(`id_lang`)
WHERE `email_ac` = :email_ac';
break;
case 'accountFromKey':
$query = 'SELECT id_account, email_ac, active_ac FROM `mc_account` WHERE `keyuniqid_ac` = :keyuniqid';
break;
case 'accountHashKey':
$query = 'SELECT *
FROM `mc_account` as a
JOIN `mc_account_address` as aa USING(`id_account`)
JOIN `mc_account_social` as asos USING(`id_account`)
JOIN `mc_lang` as l USING(`id_lang`)
WHERE `keyuniqid_ac` = :keyuniqid
AND `change_pwd` = :token';
break;
case 'accountSession':
$query = 'SELECT *
FROM mc_account_session
WHERE `keyuniqid_ac` = :keyuniqid_ac';
break;
case 'auth':
$query = 'SELECT *
FROM `mc_account` as a
WHERE `email_ac` = :email_ac
AND `passcrypt_ac` = :passwd_ac AND `active_ac` = 1';
break;
case 'idFromIso':
$query = 'SELECT `id_lang` FROM `mc_lang` WHERE `iso_lang` = :iso';
break;
case 'pwdcryptEmail':
$query = 'SELECT passcrypt_ac
FROM `mc_account` as a
WHERE `email_ac` = :email_ac';
break;
case 'searchmail':
$query = 'SELECT email_ac FROM `mc_account` WHERE `email_ac` = :email_ac';
break;
default:
return false;
}
try {
return component_routing_db::layer()->fetch($query, $params);
}
catch (Exception $e) {
if(!isset($this->logger)) $this->logger = new debug_logger(MP_LOG_DIR);
$this->logger->log('statement','db',$e->getMessage(),$this->logger::LOG_MONTH);
}
}
return false;
}
/**
* @param array $config
* @param array $params
* @return bool|string
*/
public function insert(array $config, array $params = []) {
switch ($config['type']) {
case 'account':
$datetime = new component_format_date();
$params['birthdate_ac'] = empty($params['birthdate_ac']) ? NULL : $datetime->date_to_db_format($params['birthdate_ac']);
$queries = [
['request' => 'INSERT INTO `mc_account` (
`id_lang`,
`email_ac`,
`passcrypt_ac`,
`keyuniqid_ac`,
`firstname_ac`,
`lastname_ac`,
`birthdate_ac`,
`phone_ac`,
`company_ac`,
`vat_ac`,
`active_ac`,
`date_create`
)
VALUES (
:id_lang,
:email_ac,
:passcrypt_ac,
:keyuniqid_ac,
:firstname_ac,
:lastname_ac,
:birthdate_ac,
:phone_ac,
:company_ac,
:vat_ac,
:active_ac,
NOW()
)', 'params' => $params],
['request' => 'SELECT @account_id := LAST_INSERT_ID() as id_account', 'params' => [], 'fetch' => true],
['request' => "INSERT INTO `mc_account_address` (`id_account`,`type_address`) VALUE (@account_id,'billing')", 'params' => []],
['request' => "INSERT INTO `mc_account_address` (`id_account`,`type_address`) VALUE (@account_id,'delivery')", 'params' => []],
['request' => 'INSERT INTO `mc_account_social` (`id_account`) VALUE (@account_id)', 'params' => []]
];
try {
$results = component_routing_db::layer()->transaction($queries);
return $results[1];
}
catch (Exception $e) {
return 'Exception reçue : '.$e->getMessage();
}
default:
return false;
}
try {
component_routing_db::layer()->insert($query,$params);
return true;
}
catch (Exception $e) {
return 'Exception reçue : '.$e->getMessage();
}
}
/**
* @param array $config
* @param array $params
* @return bool|string
*/
public function update(array $config, array $params = []) {
switch ($config['type']) {
case 'account':
$datetime = new component_format_date();
$params['pseudo_ac'] = $params['pseudo_ac']?: NULL;
$params['birthdate_ac'] = empty($params['birthdate_ac']) ? NULL : $datetime->date_to_db_format($params['birthdate_ac']);
$query = 'UPDATE `mc_account`
SET
`pseudo_ac` = :pseudo_ac,
`lastname_ac` = :lastname_ac,
`firstname_ac` = :firstname_ac,
`birthdate_ac` = :birthdate_ac,
`phone_ac` = :phone_ac,
`company_ac` = :company_ac,
`vat_ac` = :vat_ac
WHERE `id_account` = :id';
break;
case 'sameAddress':
$query = 'UPDATE `mc_account`
SET `same_address` = :same_address
WHERE `id_account` = :id';
break;
case 'accountActive':
$query = 'UPDATE `mc_account` SET `active_ac` = :active_ac WHERE `id_account` IN ('.$params['id'].')';
try {
component_routing_db::layer()->update($query,array('active_ac' => $params['active_ac']));
return true;
}
catch (Exception $e) {
return 'Exception reçue : '.$e->getMessage();
}
case 'accountAdminConfig':
$query = 'UPDATE `mc_account`
SET
id_lang = :id_lang,
referral_ac = :referral_ac,
active_ac = :active_ac,
email_ac = :email_ac
WHERE id_account = :id';
break;
case 'accountConfig':
$query = 'UPDATE `mc_account`
SET
`id_lang` = :id_lang,
`active_ac` = :active_ac,
`email_ac` = :email_ac
WHERE `id_account` = :id';
break;
case 'activate':
$query = 'UPDATE `mc_account` SET `active_ac` = 1 WHERE `id_account` = :id';
break;
case 'address':
$query = 'UPDATE `mc_account_address`
SET
`street_address` = :street_address,
`postcode_address` = :postcode_address,
`city_address` = :city_address,
`country_address` = :country_address
WHERE `id_account` = :id AND `type_address` = :type_address';
break;
case 'alert':
$query = 'UPDATE `mc_account`
SET `weekly_alert` = :weekly_alert,
`monthly_alert` = :monthly_alert,
`overbid_alert` = :overbid_alert,
`endofsale_alert` = :endofsale_alert
WHERE `id_account` = :id';
break;
case 'socials':
$query = 'UPDATE `mc_account_social`
SET
`website` = :website,
`facebook` = :facebook,
`twitter` = :twitter,
`instagram` = :instagram,
`tiktok` = :tiktok,
`youtube` = :youtube,
`pinterest` = :pinterest,
`tumblr` = :tumblr,
`linkedin` = :linkedin,
`viadeo` = :viadeo,
`github` = :github,
`soundcloud` = :soundcloud
WHERE `id_account` = :id_account';
break;
case 'config':
$query = 'UPDATE `mc_account_config`
SET
`pseudo` = :pseudo,
`birthdate` = :birthdate,
`links` = :links,
`picture` = :picture,
`public` = :public
WHERE id_config = :id';
break;
case 'img':
$query = 'UPDATE mc_account
SET img_ac = :img_ac
WHERE id_account = :id_account';
break;
case 'newPwd':
$query = 'UPDATE `mc_account`
SET `change_pwd` = NULL,
`passcrypt_ac` = :newpwd
WHERE `id_account` = :id';
break;
case 'pwd':
$query = 'UPDATE `mc_account`
SET
`passcrypt_ac` = :passcrypt_ac
WHERE `id_account` = :id';
break;
case 'pwdTicket':
$query = 'UPDATE `mc_account`
SET `change_pwd` = :token
WHERE `id_account` = :id';
break;
default:
return false;
}
try {
component_routing_db::layer()->update($query,$params);
return true;
}
catch (Exception $e) {
return 'Exception reçue : '.$e->getMessage();
}
}
/**
* @param array $config
* @param array $params
* @return bool|string
*/
public function delete(array $config, array $params = []) {
switch ($config['type']) {
case 'account':
$query = 'DELETE FROM `mc_account`
WHERE `id_account` IN ('.$params['id'].')';
$params = [];
break;
default:
return false;
}
try {
component_routing_db::layer()->delete($query,$params);
return true;
}
catch (Exception $e) {
return 'Exception reçue : '.$e->getMessage();
}
}
}