-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathregister.php
More file actions
151 lines (140 loc) · 6.32 KB
/
Copy pathregister.php
File metadata and controls
151 lines (140 loc) · 6.32 KB
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
<?php
/**
* BINSHOPS
*
* @author BINSHOPS
* @copyright BINSHOPS
*
*/
require_once dirname(__FILE__) . '/../AbstractRESTController.php';
use PrestaShop\PrestaShop\Core\Security\PasswordPolicyConfiguration;
use ZxcvbnPhp\Zxcvbn;
class BinshopsrestRegisterModuleFrontController extends AbstractRESTController
{
protected function processPostRequest()
{
$_POST = json_decode(Tools::file_get_contents('php://input'), true);
$psdata = "";
$messageCode = 0;
$hasError = false;
$success = true;
$firstName = Tools::getValue('firstName');
$lastName = Tools::getValue('lastName');
$email = Tools::getValue('email');
$password = Tools::getValue('password');
$gender = Tools::getValue('gender');
$newsletter = Tools::getValue('newsletter');
if (empty($email)) {
$psdata = $this->trans("An email address required", [], 'Modules.Binshopsrest.Auth');
$messageCode = 301;
$hasError = true;
} elseif (!Validate::isEmail($email)) {
$psdata = $this->trans("Invalid email address", [], 'Modules.Binshopsrest.Auth');
$messageCode = 302;
} elseif (!empty($password) && !Validate::isAcceptablePasswordLength($password)) {
$psdata = $this->trans("Invalid Password", [], 'Modules.Binshopsrest.Auth');
$messageCode = 304;
} elseif (empty($firstName)) {
$psdata = $this->trans("First name required", [], 'Modules.Binshopsrest.Auth');
$messageCode = 305;
$hasError = true;
} elseif (empty($lastName)) {
$psdata = $this->trans("Last name required", [], 'Modules.Binshopsrest.Auth');
$messageCode = 306;
$hasError = true;
} elseif (Customer::customerExists($email, false, true)) {
$psdata = $this->trans("User already exists - checked by email", [], 'Modules.Binshopsrest.Auth');
$messageCode = 308;
$hasError = true;
}else{
if (version_compare(_PS_VERSION_, '8.0', '<=')) {
if (!Validate::isPasswd($password)) {
$psdata = $this->trans("Invalid Password", [], 'Modules.Binshopsrest.Auth');
$messageCode = 309;
$hasError = true;
}
}else{
if (Validate::isAcceptablePasswordLength($password) === false) {
$psdata = $this->trans('Password must be between %d and %d characters long',
[
Configuration::get(PasswordPolicyConfiguration::CONFIGURATION_MINIMUM_LENGTH),
Configuration::get(PasswordPolicyConfiguration::CONFIGURATION_MAXIMUM_LENGTH),
],
'Modules.Binshopsrest.Auth');
$messageCode = 303;
$hasError = true;
}
if (Validate::isAcceptablePasswordScore($password) === false) {
$wordingsForScore = [
$this->translator->trans('Very weak', [], 'Shop.Theme.Global'),
$this->translator->trans('Weak', [], 'Shop.Theme.Global'),
$this->translator->trans('Average', [], 'Shop.Theme.Global'),
$this->translator->trans('Strong', [], 'Shop.Theme.Global'),
$this->translator->trans('Very strong', [], 'Shop.Theme.Global'),
];
$globalErrorMessage = $this->translator->trans(
'The minimum score must be: %s',
[
$wordingsForScore[(int) Configuration::get(PasswordPolicyConfiguration::CONFIGURATION_MINIMUM_SCORE)],
],
'Shop.Notifications.Error'
);
if ($this->context->shop->theme->get('global_settings.new_password_policy_feature') !== true) {
$zxcvbn = new Zxcvbn();
$result = $zxcvbn->passwordStrength($password);
if (!empty($result['feedback']['warning'])) {
$psdata = $this->translator->trans(
$result['feedback']['warning'], [], 'Shop.Theme.Global'
);
} else {
$psdata = $globalErrorMessage;
}
foreach ($result['feedback']['suggestions'] as $suggestion) {
$psdata = $this->translator->trans($suggestion, [], 'Shop.Theme.Global');
}
} else {
$psdata = $globalErrorMessage;
}
$hasError = true;
$messageCode = 304;
}
}
}
if (!$hasError){
$guestAllowedCheckout = Configuration::get('PS_GUEST_CHECKOUT_ENABLED');
$cp = new CustomerPersister(
$this->context,
$this->get('hashing'),
$this->getTranslator(),
$guestAllowedCheckout
);
try {
$customer = new Customer();
$customer->firstname = $firstName;
$customer->lastname = $lastName;
$customer->email = $email;
$customer->id_gender = $gender;
$customer->id_shop = (int)$this->context->shop->id;
$customer->newsletter = $newsletter;
$status = $cp->save($customer, $password);
$messageCode = 200;
$psdata = array(
'registered' => $status,
'message' => $this->trans('User registered successfully', [], 'Modules.Binshopsrest.Auth'),
'customer_id' => $customer->id,
'session_data' => (int)$this->context->cart->id
);
} catch (Exception $exception) {
$messageCode = 300;
$psdata = $exception->getMessage();
$success = false;
}
}
$this->ajaxRender(json_encode([
'success' => $success,
'code' => $messageCode,
'psdata' => $psdata
]));
die;
}
}