1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
HCSF - A multilingual CMS and Shopsystem |
5
|
|
|
Copyright (C) 2014 Marcus Haase - [email protected] |
6
|
|
|
|
7
|
|
|
This program is free software: you can redistribute it and/or modify |
8
|
|
|
it under the terms of the GNU General Public License as published by |
9
|
|
|
the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
(at your option) any later version. |
11
|
|
|
|
12
|
|
|
This program is distributed in the hope that it will be useful, |
13
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
GNU General Public License for more details. |
16
|
|
|
|
17
|
|
|
You should have received a copy of the GNU General Public License |
18
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace HaaseIT\HCSF\Customer; |
22
|
|
|
|
23
|
|
|
use HaaseIT\Toolbox\Tools; |
24
|
|
|
use Zend\ServiceManager\ServiceManager; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class Helper |
28
|
|
|
* @package HaaseIT\HCSF\Customer |
29
|
|
|
*/ |
30
|
|
|
class Helper |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var ServiceManager |
34
|
|
|
*/ |
35
|
|
|
protected $serviceManager; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var \HaaseIT\HCSF\HelperConfig |
39
|
|
|
*/ |
40
|
|
|
protected $config; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $core = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $customer = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected $countries = []; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
protected $shop = []; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var \HaaseIT\HCSF\Helper |
64
|
|
|
*/ |
65
|
|
|
protected $helper; |
66
|
|
|
|
67
|
|
|
public function __construct(ServiceManager $serviceManager) |
68
|
|
|
{ |
69
|
|
|
$this->serviceManager = $serviceManager; |
70
|
|
|
$this->config = $serviceManager->get('config'); |
71
|
|
|
$this->core = $this->config->getCore(); |
72
|
|
|
$this->countries = $this->config->getCountries(); |
73
|
|
|
$this->shop = $this->config->getShop(); |
74
|
|
|
$this->helper = $this->serviceManager->get('helper'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $sLang |
79
|
|
|
* @param array $aErr |
80
|
|
|
* @param bool $bEdit |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
public function validateCustomerForm($sLang, $aErr = [], $bEdit = false) |
84
|
|
|
{ |
85
|
|
|
if (empty(filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL))) { |
86
|
|
|
$aErr['email'] = true; |
87
|
|
|
} |
88
|
|
|
$postcorpname = filter_input(INPUT_POST, 'corpname', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); |
89
|
|
View Code Duplication |
if ($this->customer['validate_corpname'] && (empty($postcorpname) || strlen(trim($postcorpname)) < 3)) { |
|
|
|
|
90
|
|
|
$aErr['corpname'] = true; |
91
|
|
|
} |
92
|
|
|
$postname = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); |
93
|
|
View Code Duplication |
if ($this->customer['validate_name'] && (empty($postname) || strlen(trim($postname)) < 3)) { |
|
|
|
|
94
|
|
|
$aErr['name'] = true; |
95
|
|
|
} |
96
|
|
|
$poststreet = filter_input(INPUT_POST, 'street', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); |
97
|
|
View Code Duplication |
if ($this->customer['validate_street'] && (empty($poststreet) || strlen(trim($poststreet)) < 3)) { |
|
|
|
|
98
|
|
|
$aErr['street'] = true; |
99
|
|
|
} |
100
|
|
|
$postzip = filter_input(INPUT_POST, 'zip', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); |
101
|
|
View Code Duplication |
if ($this->customer['validate_zip'] && (empty($postzip) || strlen(trim($postzip)) < 4)) { |
|
|
|
|
102
|
|
|
$aErr['zip'] = true; |
103
|
|
|
} |
104
|
|
|
$posttown = filter_input(INPUT_POST, 'town', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); |
105
|
|
View Code Duplication |
if ($this->customer['validate_town'] && (empty($posttown) || strlen(trim($posttown)) < 3)) { |
|
|
|
|
106
|
|
|
$aErr['town'] = true; |
107
|
|
|
} |
108
|
|
|
$postphone = filter_input(INPUT_POST, 'phone', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); |
109
|
|
View Code Duplication |
if ($this->customer['validate_phone'] && (empty($postphone) || strlen(trim($postphone)) < 6)) { |
|
|
|
|
110
|
|
|
$aErr['phone'] = true; |
111
|
|
|
} |
112
|
|
|
$postcellphone = filter_input(INPUT_POST, 'cellphone', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); |
113
|
|
View Code Duplication |
if ($this->customer['validate_cellphone'] && (empty($postcellphone) || strlen(trim($postcellphone)) < 11)) { |
|
|
|
|
114
|
|
|
$aErr['cellphone'] = true; |
115
|
|
|
} |
116
|
|
|
$postfax = filter_input(INPUT_POST, 'fax', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); |
117
|
|
View Code Duplication |
if ($this->customer['validate_fax'] && (empty($postfax) || strlen(trim($postfax)) < 6)) { |
|
|
|
|
118
|
|
|
$aErr['fax'] = true; |
119
|
|
|
} |
120
|
|
|
$postcountry = filter_input(INPUT_POST, 'country', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); |
121
|
|
|
if ($this->customer['validate_country'] && (empty($postcountry) || !isset($this->countries['countries_' .$sLang][$postcountry]))) { |
122
|
|
|
$aErr['country'] = true; |
123
|
|
|
} |
124
|
|
|
$posttos = filter_input(INPUT_POST, 'tos'); |
125
|
|
|
if (!$bEdit && $posttos !== 'y') { |
126
|
|
|
$aErr['tos'] = true; |
127
|
|
|
} |
128
|
|
|
$postcancellationdisclaimer = filter_input(INPUT_POST, 'cancellationdisclaimer'); |
129
|
|
|
if (!$bEdit && $postcancellationdisclaimer !== 'y') { |
130
|
|
|
$aErr['cancellationdisclaimer'] = true; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$postpwd = filter_input(INPUT_POST, 'pwd'); |
134
|
|
|
$postpwdc = filter_input(INPUT_POST, 'pwdc'); |
135
|
|
|
if (!$bEdit || !empty($postpwd)) { |
136
|
|
|
if (strlen($postpwd) < $this->customer['minimum_length_password']) { |
137
|
|
|
$aErr['passwordlength'] = true; |
138
|
|
|
} |
139
|
|
|
if ($postpwd !== $postpwdc) { |
140
|
|
|
$aErr['passwordmatch'] = true; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $aErr; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param string $sLang |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
public function getDefaultCountryByConfig($sLang) { |
152
|
|
|
if (isset($this->core['defaultcountrybylang'][$sLang])) { |
153
|
|
|
return $this->core['defaultcountrybylang'][$sLang]; |
154
|
|
|
} |
155
|
|
|
return ''; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param string $sKeyConfig |
160
|
|
|
* @param string $sKeyForm |
161
|
|
|
* @param array|bool $aUserData |
162
|
|
|
* @return bool |
163
|
|
|
*/ |
164
|
|
|
public function getCustomerFormDefaultValue($sKeyConfig, $sKeyForm, $aUserData) { |
165
|
|
|
$sDefaultValue = $this->getUserData($sKeyConfig, $aUserData); |
|
|
|
|
166
|
|
|
if (!$sDefaultValue && isset($_SESSION['formsave_addrform'][$sKeyForm])) { |
167
|
|
|
$sDefaultValue = $_SESSION['formsave_addrform'][$sKeyForm]; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $sDefaultValue; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param string $sLang |
175
|
|
|
* @param string $sPurpose |
176
|
|
|
* @param array $aErr |
177
|
|
|
* @param bool $aUserData |
178
|
|
|
* @return array |
179
|
|
|
*/ |
180
|
|
|
public function buildCustomerForm($sLang, $sPurpose = 'none', $aErr = [], $aUserData = false) |
181
|
|
|
{ |
182
|
|
|
$sDefaultCountry = $this->getCustomerFormDefaultValue('cust_country', 'country', $aUserData); |
183
|
|
|
|
184
|
|
|
// Purposes: shoppingcart, userhome, shopadmin, editprofile, register |
185
|
|
|
// fv = field_value, fr = field_required |
186
|
|
|
$aData = [ |
187
|
|
|
'purpose' => $sPurpose, |
188
|
|
|
'errormessage' => $aErr, |
189
|
|
|
'readonlycustno' => $sPurpose === 'shopadmin' ? true : false, |
190
|
|
|
'readonly' => |
191
|
|
|
$sPurpose === 'shopadmin' |
192
|
|
|
|| $sPurpose === 'userhome' |
193
|
|
|
|| ($sPurpose === 'editprofile' && !$this->customer['allow_edituserprofile']) |
194
|
|
|
|| ($sPurpose === 'shoppingcart' && $this->getUserData()) |
195
|
|
|
, |
196
|
|
|
'fv_custno' => Tools::getFormfield( |
197
|
|
|
'custno', |
198
|
|
|
$this->getCustomerFormDefaultValue('cust_no', 'custno', $aUserData), |
|
|
|
|
199
|
|
|
true |
200
|
|
|
), |
201
|
|
|
'fv_email' => Tools::getFormfield( |
202
|
|
|
'email', |
203
|
|
|
$this->getCustomerFormDefaultValue('cust_email', 'email', $aUserData), |
|
|
|
|
204
|
|
|
true |
205
|
|
|
), |
206
|
|
|
'fv_corpname' => Tools::getFormfield( |
207
|
|
|
'corpname', |
208
|
|
|
$this->getCustomerFormDefaultValue('cust_corp', 'corpname', $aUserData), |
|
|
|
|
209
|
|
|
true |
210
|
|
|
), |
211
|
|
|
'fr_corpname' => $this->customer['validate_corpname'], |
212
|
|
|
'fv_name' => Tools::getFormfield( |
213
|
|
|
'name', |
214
|
|
|
$this->getCustomerFormDefaultValue('cust_name', 'name', $aUserData), |
|
|
|
|
215
|
|
|
true |
216
|
|
|
), |
217
|
|
|
'fr_name' => $this->customer['validate_name'], |
218
|
|
|
'fv_street' => Tools::getFormfield( |
219
|
|
|
'street', |
220
|
|
|
$this->getCustomerFormDefaultValue('cust_street', 'street', $aUserData), |
|
|
|
|
221
|
|
|
true |
222
|
|
|
), |
223
|
|
|
'fr_street' => $this->customer['validate_street'], |
224
|
|
|
'fv_zip' => Tools::getFormfield( |
225
|
|
|
'zip', |
226
|
|
|
$this->getCustomerFormDefaultValue('cust_zip', 'zip', $aUserData), |
|
|
|
|
227
|
|
|
true |
228
|
|
|
), |
229
|
|
|
'fr_zip' => $this->customer['validate_zip'], |
230
|
|
|
'fv_town' => Tools::getFormfield( |
231
|
|
|
'town', |
232
|
|
|
$this->getCustomerFormDefaultValue('cust_town', 'town', $aUserData), |
|
|
|
|
233
|
|
|
true |
234
|
|
|
), |
235
|
|
|
'fr_town' => $this->customer['validate_town'], |
236
|
|
|
'fv_phone' => Tools::getFormfield( |
237
|
|
|
'phone', |
238
|
|
|
$this->getCustomerFormDefaultValue('cust_phone', 'phone', $aUserData), |
|
|
|
|
239
|
|
|
true |
240
|
|
|
), |
241
|
|
|
'fr_phone' => $this->customer['validate_phone'], |
242
|
|
|
'fv_cellphone' => Tools::getFormfield( |
243
|
|
|
'cellphone', |
244
|
|
|
$this->getCustomerFormDefaultValue('cust_cellphone', 'cellphone', $aUserData), |
|
|
|
|
245
|
|
|
true |
246
|
|
|
), |
247
|
|
|
'fr_cellphone' => $this->customer['validate_cellphone'], |
248
|
|
|
'fv_fax' => Tools::getFormfield( |
249
|
|
|
'fax', |
250
|
|
|
$this->getCustomerFormDefaultValue('cust_fax', 'fax', $aUserData), |
|
|
|
|
251
|
|
|
true |
252
|
|
|
), |
253
|
|
|
'fr_fax' => $this->customer['validate_fax'], |
254
|
|
|
'fv_country' => Tools::getFormfield( |
255
|
|
|
'country', |
256
|
|
|
($sDefaultCountry ? $sDefaultCountry : $this->getDefaultCountryByConfig($sLang)), |
|
|
|
|
257
|
|
|
true |
258
|
|
|
), |
259
|
|
|
'fr_country' => $this->customer['validate_country'], |
260
|
|
|
]; |
261
|
|
|
|
262
|
|
|
if ($sPurpose === 'admin') { |
263
|
|
|
$aData['fv_custgroups'] = $this->customer['customer_groups']; |
264
|
|
|
$aData['fv_custgroup_selected'] = Tools::getFormfield('custgroup', $this->getUserData('cust_group', $aUserData), true); |
|
|
|
|
265
|
|
|
} elseif ($sPurpose === 'shopadmin') { |
266
|
|
|
$aData['fv_custgroup'] = ''; |
267
|
|
|
if (isset($this->customer['customer_groups'][$this->getUserData('cust_group', $aUserData)])) { |
268
|
|
|
$aData['fv_custgroup'] = $this->customer['customer_groups'][$this->getUserData('cust_group', $aUserData)]; |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
if ($sPurpose === 'admin' || $sPurpose === 'register' || $sPurpose === 'editprofile') { |
273
|
|
|
$aData['fv_pwd'] = (($sPurpose === 'admin' || $sPurpose === 'editprofile') ? '' : Tools::getFormfield('pwd', '')); |
274
|
|
|
$aData['fv_pwdc'] = (($sPurpose === 'admin' || $sPurpose === 'editprofile') ? '' : Tools::getFormfield('pwdc', '')); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
if ($sPurpose === 'shoppingcart') { |
278
|
|
|
$sRememberedRemarks = ''; |
279
|
|
|
if (isset($_SESSION['formsave_addrform']['remarks'])) { |
280
|
|
|
$sRememberedRemarks = $_SESSION['formsave_addrform']['remarks']; |
281
|
|
|
} |
282
|
|
|
$aData['fv_remarks'] = Tools::getFormfield('remarks', $sRememberedRemarks, true); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
if ($sPurpose === 'shoppingcart' || $sPurpose === 'register') { |
286
|
|
|
if (!$this->getUserData()) { |
287
|
|
|
$aData['fv_tos'] = Tools::getCheckbox('tos', 'y'); |
288
|
|
|
$aData['fv_cancellationdisclaimer'] = Tools::getCheckbox('cancellationdisclaimer', 'y'); |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
if ($sPurpose === 'shoppingcart') { |
293
|
|
|
$aData['fv_paymentmethods'] = $this->shop['paymentmethods']; |
294
|
|
|
$aData['fv_paymentmethod'] = Tools::getFormfield('paymentmethod', ''); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
if ($sPurpose === 'admin') { |
298
|
|
|
$aData['fv_active'] = $this->getUserData('cust_active', $aUserData) === 'y'; |
299
|
|
|
$aData['fv_emailverified'] = $this->getUserData('cust_emailverified', $aUserData) === 'y'; |
300
|
|
|
} |
301
|
|
|
return $aData; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @param $sEmailVerificationcode |
306
|
|
|
* @param $sTargetAddress |
307
|
|
|
* @param ServiceManager $serviceManager |
308
|
|
|
* @param bool $bCust |
309
|
|
|
*/ |
310
|
|
|
public function sendVerificationMail($sEmailVerificationcode, $sTargetAddress, ServiceManager $serviceManager, $bCust = false) |
311
|
|
|
{ |
312
|
|
|
if ($bCust) { |
313
|
|
|
$sSubject = $serviceManager->get('textcats')->T('register_mail_emailverification_subject'); |
314
|
|
|
|
315
|
|
|
$serverhttps = filter_input(INPUT_SERVER, 'HTTPS'); |
316
|
|
|
$servername = filter_input(INPUT_SERVER, 'SERVER_NAME', FILTER_SANITIZE_URL); |
317
|
|
|
$aP['link'] = 'http'.($serverhttps === 'on' ? 's' : '').'://'; |
|
|
|
|
318
|
|
|
$aP['link'] .= $servername.'/_misc/verifyemail.html?key='.$sEmailVerificationcode; |
319
|
|
|
|
320
|
|
|
$sMessage = $serviceManager->get('twig')->render('customer/sendverificationmail.twig', $aP); |
321
|
|
|
} else { |
322
|
|
|
$hardcodedtextcats = $serviceManager->get('hardcodedtextcats'); |
323
|
|
|
|
324
|
|
|
$sSubject = $hardcodedtextcats->get('newcustomerregistration_mail_subject'); |
325
|
|
|
$sMessage = $hardcodedtextcats->get('newcustomerregistration_mail_text1').' '; |
326
|
|
|
$sMessage .= $sTargetAddress.$hardcodedtextcats->get( |
327
|
|
|
'newcustomerregistration_mail_text2').' '.date($this->core['locale_format_date_time'] |
328
|
|
|
); |
329
|
|
|
$sTargetAddress = $this->core['email_sender']; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
$this->helper->mailWrapper($sTargetAddress, $sSubject, $sMessage); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* @param string $sField |
337
|
|
|
* @param bool $aUserdata |
338
|
|
|
* @return bool |
339
|
|
|
*/ |
340
|
|
|
public function getUserData($sField = '', $aUserdata = false) |
341
|
|
|
{ |
342
|
|
|
if (!$aUserdata) { |
343
|
|
|
if (!isset($_SESSION['user']) || !is_array($_SESSION['user'])) { |
344
|
|
|
return false; |
345
|
|
|
} elseif ($sField === '') { |
346
|
|
|
return true; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
if ($sField !== '' && isset($_SESSION['user'][$sField]) && $_SESSION['user'][$sField] !== '') { |
350
|
|
|
return $_SESSION['user'][$sField]; |
351
|
|
|
} |
352
|
|
|
} else { |
353
|
|
|
if (isset($aUserdata[$sField])) { |
354
|
|
|
return $aUserdata[$sField]; |
355
|
|
|
} elseif ($sField === '') { |
356
|
|
|
return false; |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
} |
360
|
|
|
} |
361
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.