1: <?php
2: namespace Opencart\Catalog\Controller\Account;
3: /**
4: * Class Register
5: *
6: * @package Opencart\Catalog\Controller\Account
7: */
8: class Register extends \Opencart\System\Engine\Controller {
9: /**
10: * @return void
11: */
12: public function index(): void {
13: if ($this->customer->isLogged()) {
14: $this->response->redirect($this->url->link('account/account', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token'], true));
15: }
16:
17: $this->load->language('account/register');
18:
19: $this->document->setTitle($this->language->get('heading_title'));
20:
21: $data['breadcrumbs'] = [];
22:
23: $data['breadcrumbs'][] = [
24: 'text' => $this->language->get('text_home'),
25: 'href' => $this->url->link('common/home', 'language=' . $this->config->get('config_language'))
26: ];
27:
28: $data['breadcrumbs'][] = [
29: 'text' => $this->language->get('text_account'),
30: 'href' => $this->url->link('account/account', 'language=' . $this->config->get('config_language'))
31: ];
32:
33: $data['breadcrumbs'][] = [
34: 'text' => $this->language->get('text_register'),
35: 'href' => $this->url->link('account/register', 'language=' . $this->config->get('config_language'))
36: ];
37:
38: $data['text_account_already'] = sprintf($this->language->get('text_account_already'), $this->url->link('account/login', 'language=' . $this->config->get('config_language')));
39:
40: $data['error_upload_size'] = sprintf($this->language->get('error_upload_size'), $this->config->get('config_file_max_size'));
41:
42: $data['config_file_max_size'] = ((int)$this->config->get('config_file_max_size') * 1024 * 1024);
43: $data['config_telephone_display'] = $this->config->get('config_telephone_display');
44: $data['config_telephone_required'] = $this->config->get('config_telephone_required');
45:
46: $this->session->data['register_token'] = oc_token(26);
47:
48: $data['register'] = $this->url->link('account/register.register', 'language=' . $this->config->get('config_language') . '&register_token=' . $this->session->data['register_token']);
49:
50: $this->session->data['upload_token'] = oc_token(32);
51:
52: $data['upload'] = $this->url->link('tool/upload', 'language=' . $this->config->get('config_language') . '&upload_token=' . $this->session->data['upload_token']);
53:
54: $data['customer_groups'] = [];
55:
56: if (is_array($this->config->get('config_customer_group_display'))) {
57: $this->load->model('account/customer_group');
58:
59: $customer_groups = $this->model_account_customer_group->getCustomerGroups();
60:
61: foreach ($customer_groups as $customer_group) {
62: if (in_array($customer_group['customer_group_id'], $this->config->get('config_customer_group_display'))) {
63: $data['customer_groups'][] = $customer_group;
64: }
65: }
66: }
67:
68: $data['customer_group_id'] = $this->config->get('config_customer_group_id');
69:
70: // Custom Fields
71: $data['custom_fields'] = [];
72:
73: $this->load->model('account/custom_field');
74:
75: $custom_fields = $this->model_account_custom_field->getCustomFields();
76:
77: foreach ($custom_fields as $custom_field) {
78: if ($custom_field['location'] == 'account') {
79: $data['custom_fields'][] = $custom_field;
80: }
81: }
82:
83: // Captcha
84: $this->load->model('setting/extension');
85:
86: $extension_info = $this->model_setting_extension->getExtensionByCode('captcha', $this->config->get('config_captcha'));
87:
88: if ($extension_info && $this->config->get('captcha_' . $this->config->get('config_captcha') . '_status') && in_array('register', (array)$this->config->get('config_captcha_page'))) {
89: $data['captcha'] = $this->load->controller('extension/' . $extension_info['extension'] . '/captcha/' . $extension_info['code']);
90: } else {
91: $data['captcha'] = '';
92: }
93:
94: $this->load->model('catalog/information');
95:
96: $information_info = $this->model_catalog_information->getInformation($this->config->get('config_account_id'));
97:
98: if ($information_info) {
99: $data['text_agree'] = sprintf($this->language->get('text_agree'), $this->url->link('information/information.info', 'language=' . $this->config->get('config_language') . '&information_id=' . $this->config->get('config_account_id')), $information_info['title']);
100: } else {
101: $data['text_agree'] = '';
102: }
103:
104: $data['language'] = $this->config->get('config_language');
105:
106: $data['column_left'] = $this->load->controller('common/column_left');
107: $data['column_right'] = $this->load->controller('common/column_right');
108: $data['content_top'] = $this->load->controller('common/content_top');
109: $data['content_bottom'] = $this->load->controller('common/content_bottom');
110: $data['footer'] = $this->load->controller('common/footer');
111: $data['header'] = $this->load->controller('common/header');
112:
113: $this->response->setOutput($this->load->view('account/register', $data));
114: }
115:
116: /**
117: * Register
118: *
119: * @return void
120: */
121: public function register(): void {
122: $this->load->language('account/register');
123:
124: $json = [];
125:
126: $keys = [
127: 'customer_group_id',
128: 'firstname',
129: 'lastname',
130: 'email',
131: 'telephone',
132: 'custom_field',
133: 'password',
134: 'confirm',
135: 'agree'
136: ];
137:
138: foreach ($keys as $key) {
139: if (!isset($this->request->post[$key])) {
140: $this->request->post[$key] = '';
141: }
142: }
143:
144: if (!isset($this->request->get['register_token']) || !isset($this->session->data['register_token']) || ($this->session->data['register_token'] != $this->request->get['register_token'])) {
145: $json['redirect'] = $this->url->link('account/register', 'language=' . $this->config->get('config_language'), true);
146: }
147:
148: if (!$json) {
149: // Customer Group
150: if ($this->request->post['customer_group_id']) {
151: $customer_group_id = (int)$this->request->post['customer_group_id'];
152: } else {
153: $customer_group_id = (int)$this->config->get('config_customer_group_id');
154: }
155:
156: $this->load->model('account/customer_group');
157:
158: $customer_group_info = $this->model_account_customer_group->getCustomerGroup($customer_group_id);
159:
160: if (!$customer_group_info || !in_array($customer_group_id, (array)$this->config->get('config_customer_group_display'))) {
161: $json['error']['warning'] = $this->language->get('error_customer_group');
162: }
163:
164: if ((oc_strlen($this->request->post['firstname']) < 1) || (oc_strlen($this->request->post['firstname']) > 32)) {
165: $json['error']['firstname'] = $this->language->get('error_firstname');
166: }
167:
168: if ((oc_strlen($this->request->post['lastname']) < 1) || (oc_strlen($this->request->post['lastname']) > 32)) {
169: $json['error']['lastname'] = $this->language->get('error_lastname');
170: }
171:
172: if ((oc_strlen($this->request->post['email']) > 96) || !filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL)) {
173: $json['error']['email'] = $this->language->get('error_email');
174: }
175:
176: $this->load->model('account/customer');
177:
178: if ($this->model_account_customer->getTotalCustomersByEmail($this->request->post['email'])) {
179: $json['error']['warning'] = $this->language->get('error_exists');
180: }
181:
182: if ($this->config->get('config_telephone_required') && (oc_strlen($this->request->post['telephone']) < 3) || (oc_strlen($this->request->post['telephone']) > 32)) {
183: $json['error']['telephone'] = $this->language->get('error_telephone');
184: }
185:
186: // Custom field validation
187: $this->load->model('account/custom_field');
188:
189: $custom_fields = $this->model_account_custom_field->getCustomFields($customer_group_id);
190:
191: foreach ($custom_fields as $custom_field) {
192: if ($custom_field['location'] == 'account') {
193: if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
194: $json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
195: } elseif (($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !preg_match(html_entity_decode($custom_field['validation'], ENT_QUOTES, 'UTF-8'), $this->request->post['custom_field'][$custom_field['custom_field_id']])) {
196: $json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_regex'), $custom_field['name']);
197: }
198: }
199: }
200:
201: if ((oc_strlen(html_entity_decode($this->request->post['password'], ENT_QUOTES, 'UTF-8')) < 6) || (oc_strlen(html_entity_decode($this->request->post['password'], ENT_QUOTES, 'UTF-8')) > 40)) {
202: $json['error']['password'] = $this->language->get('error_password');
203: }
204:
205: // Captcha
206: $this->load->model('setting/extension');
207:
208: $extension_info = $this->model_setting_extension->getExtensionByCode('captcha', $this->config->get('config_captcha'));
209:
210: if ($extension_info && $this->config->get('captcha_' . $this->config->get('config_captcha') . '_status') && in_array('register', (array)$this->config->get('config_captcha_page'))) {
211: $captcha = $this->load->controller('extension/' . $extension_info['extension'] . '/captcha/' . $extension_info['code'] . '.validate');
212:
213: if ($captcha) {
214: $json['error']['captcha'] = $captcha;
215: }
216: }
217:
218: // Agree to terms
219: $this->load->model('catalog/information');
220:
221: $information_info = $this->model_catalog_information->getInformation($this->config->get('config_account_id'));
222:
223: if ($information_info && !$this->request->post['agree']) {
224: $json['error']['warning'] = sprintf($this->language->get('error_agree'), $information_info['title']);
225: }
226: }
227:
228: if (!$json) {
229: $customer_id = $this->model_account_customer->addCustomer($this->request->post);
230:
231: // Login if requires approval
232: if (!$customer_group_info['approval']) {
233: $this->customer->login($this->request->post['email'], html_entity_decode($this->request->post['password'], ENT_QUOTES, 'UTF-8'));
234:
235: // Add customer details into session
236: $this->session->data['customer'] = [
237: 'customer_id' => $customer_id,
238: 'customer_group_id' => $customer_group_id,
239: 'firstname' => $this->request->post['firstname'],
240: 'lastname' => $this->request->post['lastname'],
241: 'email' => $this->request->post['email'],
242: 'telephone' => $this->request->post['telephone'],
243: 'custom_field' => $this->request->post['custom_field']
244: ];
245:
246: // Log the IP info
247: $this->model_account_customer->addLogin($this->customer->getId(), $this->request->server['REMOTE_ADDR']);
248:
249: // Create customer token
250: $this->session->data['customer_token'] = oc_token(26);
251: }
252:
253: // Clear any previous login attempts for unregistered accounts.
254: $this->model_account_customer->deleteLoginAttempts($this->request->post['email']);
255:
256: unset($this->session->data['guest']);
257: unset($this->session->data['register_token']);
258: unset($this->session->data['shipping_method']);
259: unset($this->session->data['shipping_methods']);
260: unset($this->session->data['payment_method']);
261: unset($this->session->data['payment_methods']);
262:
263: $json['redirect'] = $this->url->link('account/success', 'language=' . $this->config->get('config_language') . (isset($this->session->data['customer_token']) ? '&customer_token=' . $this->session->data['customer_token'] : ''), true);
264: }
265:
266: $this->response->addHeader('Content-Type: application/json');
267: $this->response->setOutput(json_encode($json));
268: }
269: }
270: