PageRenderTime 55ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/front/AuthController.php

https://bitbucket.org/yhjohn/ayanapure.com
PHP | 676 lines | 481 code | 68 blank | 127 comment | 119 complexity | 1534f389cf71f336afff6eea0e660c48 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0
  1. <?php
  2. /*
  3. * 2007-2012 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2012 PrestaShop SA
  23. * @version Release: $Revision: 7499 $
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. * International Registered Trademark & Property of PrestaShop SA
  26. */
  27. class AuthControllerCore extends FrontController
  28. {
  29. public $ssl = true;
  30. public $php_self = 'authentication';
  31. /**
  32. * @var bool create_account
  33. */
  34. protected $create_account;
  35. /**
  36. * Initialize auth controller
  37. * @see FrontController::init()
  38. */
  39. public function init()
  40. {
  41. parent::init();
  42. if (!Tools::getIsset('step') && $this->context->customer->isLogged() && !$this->ajax)
  43. Tools::redirect('index.php?controller=my-account');
  44. if (Tools::getValue('create_account'))
  45. $this->create_account = true;
  46. }
  47. /**
  48. * Set default medias for this controller
  49. * @see FrontController::setMedia()
  50. */
  51. public function setMedia()
  52. {
  53. parent::setMedia();
  54. $this->addCSS(_THEME_CSS_DIR_.'authentication.css');
  55. $this->addJqueryPlugin('typewatch');
  56. $this->addJS(_THEME_JS_DIR_.'tools/statesManagement.js');
  57. }
  58. /**
  59. * Run ajax process
  60. * @see FrontController::displayAjax()
  61. */
  62. public function displayAjax()
  63. {
  64. $this->display();
  65. }
  66. /**
  67. * Assign template vars related to page content
  68. * @see FrontController::initContent()
  69. */
  70. public function initContent()
  71. {
  72. parent::initContent();
  73. $this->context->smarty->assign('genders', Gender::getGenders());
  74. $this->assignDate();
  75. $this->assignCountries();
  76. $active_module_newsletter = false;
  77. if ($module_newsletter = Module::getInstanceByName('blocknewsletter'))
  78. $active_module_newsletter = $module_newsletter->active;
  79. $this->context->smarty->assign('newsletter', (int)$active_module_newsletter);
  80. $back = Tools::getValue('back');
  81. $key = Tools::safeOutput(Tools::getValue('key'));
  82. if (!empty($key))
  83. $back .= (strpos($back, '?') !== false ? '&' : '?').'key='.$key;
  84. if (!empty($back))
  85. $this->context->smarty->assign('back', Tools::safeOutput($back));
  86. if (Tools::getValue('display_guest_checkout'))
  87. {
  88. if (Configuration::get('PS_RESTRICT_DELIVERED_COUNTRIES'))
  89. $countries = Carrier::getDeliveredCountries($this->context->language->id, true, true);
  90. else
  91. $countries = Country::getCountries($this->context->language->id, true);
  92. $this->context->smarty->assign(array(
  93. 'inOrderProcess' => true,
  94. 'PS_GUEST_CHECKOUT_ENABLED' => Configuration::get('PS_GUEST_CHECKOUT_ENABLED'),
  95. 'PS_REGISTRATION_PROCESS_TYPE' => Configuration::get('PS_REGISTRATION_PROCESS_TYPE'),
  96. 'sl_country' => (int)Tools::getValue('id_country', Configuration::get('PS_COUNTRY_DEFAULT')),
  97. 'countries' => $countries
  98. ));
  99. }
  100. if (Tools::getValue('create_account'))
  101. $this->context->smarty->assign('email_create', 1);
  102. if (Tools::getValue('multi-shipping') == 1)
  103. $this->context->smarty->assign('multi_shipping', true);
  104. else
  105. $this->context->smarty->assign('multi_shipping', false);
  106. $this->assignAddressFormat();
  107. // Call a hook to display more information on form
  108. $this->context->smarty->assign(array(
  109. 'HOOK_CREATE_ACCOUNT_FORM' => Hook::exec('displayCustomerAccountForm'),
  110. 'HOOK_CREATE_ACCOUNT_TOP' => Hook::exec('displayCustomerAccountFormTop')
  111. ));
  112. if ($this->ajax)
  113. {
  114. // Call a hook to display more information on form
  115. $this->context->smarty->assign(array(
  116. 'PS_REGISTRATION_PROCESS_TYPE' => Configuration::get('PS_REGISTRATION_PROCESS_TYPE'),
  117. 'genders' => Gender::getGenders()
  118. ));
  119. $return = array(
  120. 'hasError' => !empty($this->errors),
  121. 'errors' => $this->errors,
  122. 'page' => $this->context->smarty->fetch(_PS_THEME_DIR_.'authentication.tpl'),
  123. 'token' => Tools::getToken(false)
  124. );
  125. die(Tools::jsonEncode($return));
  126. }
  127. $this->setTemplate(_PS_THEME_DIR_.'authentication.tpl');
  128. }
  129. /**
  130. * Assign date var to smarty
  131. */
  132. protected function assignDate()
  133. {
  134. // Generate years, months and days
  135. if (isset($_POST['years']) && is_numeric($_POST['years']))
  136. $selectedYears = (int)($_POST['years']);
  137. $years = Tools::dateYears();
  138. if (isset($_POST['months']) && is_numeric($_POST['months']))
  139. $selectedMonths = (int)($_POST['months']);
  140. $months = Tools::dateMonths();
  141. if (isset($_POST['days']) && is_numeric($_POST['days']))
  142. $selectedDays = (int)($_POST['days']);
  143. $days = Tools::dateDays();
  144. $this->context->smarty->assign(array(
  145. 'onr_phone_at_least' => (int)Configuration::get('PS_ONE_PHONE_AT_LEAST'),
  146. 'years' => $years,
  147. 'sl_year' => (isset($selectedYears) ? $selectedYears : 0),
  148. 'months' => $months,
  149. 'sl_month' => (isset($selectedMonths) ? $selectedMonths : 0),
  150. 'days' => $days,
  151. 'sl_day' => (isset($selectedDays) ? $selectedDays : 0)
  152. ));
  153. }
  154. /**
  155. * Assign countries var to smarty
  156. */
  157. protected function assignCountries()
  158. {
  159. if (isset($this->create_account))
  160. {
  161. // Select the most appropriate country
  162. if (isset($_POST['id_country']) && is_numeric($_POST['id_country']))
  163. $selectedCountry = (int)($_POST['id_country']);
  164. /* FIXME : language iso and country iso are not similar,
  165. * maybe an associative table with country an language can resolve it,
  166. * But for now it's a bug !
  167. * @see : bug #6968
  168. * @link:http://www.prestashop.com/bug_tracker/view/6968/
  169. elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
  170. {
  171. $array = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
  172. if (Validate::isLanguageIsoCode($array[0]))
  173. {
  174. $selectedCountry = Country::getByIso($array[0]);
  175. if (!$selectedCountry)
  176. $selectedCountry = (int)(Configuration::get('PS_COUNTRY_DEFAULT'));
  177. }
  178. }*/
  179. if (!isset($selectedCountry))
  180. $selectedCountry = (int)(Configuration::get('PS_COUNTRY_DEFAULT'));
  181. if (Configuration::get('PS_RESTRICT_DELIVERED_COUNTRIES'))
  182. $countries = Carrier::getDeliveredCountries($this->context->language->id, true, true);
  183. else
  184. $countries = Country::getCountries($this->context->language->id, true);
  185. $this->context->smarty->assign(array(
  186. 'countries' => $countries,
  187. 'PS_REGISTRATION_PROCESS_TYPE' => Configuration::get('PS_REGISTRATION_PROCESS_TYPE'),
  188. 'sl_country' => (isset($selectedCountry) ? $selectedCountry : 0),
  189. 'vat_management' => Configuration::get('VATNUMBER_MANAGEMENT')
  190. ));
  191. }
  192. }
  193. /**
  194. * Assign address var to smarty
  195. */
  196. protected function assignAddressFormat()
  197. {
  198. $addressItems = array();
  199. $addressFormat = AddressFormat::getOrderedAddressFields(Configuration::get('PS_COUNTRY_DEFAULT'), false, true);
  200. $requireFormFieldsList = AddressFormat::$requireFormFieldsList;
  201. foreach ($addressFormat as $addressline)
  202. foreach (explode(' ', $addressline) as $addressItem)
  203. $addressItems[] = trim($addressItem);
  204. // Add missing require fields for a new user susbscription form
  205. foreach ($requireFormFieldsList as $fieldName)
  206. if (!in_array($fieldName, $addressItems))
  207. $addressItems[] = trim($fieldName);
  208. foreach (array('inv', 'dlv') as $addressType)
  209. $this->context->smarty->assign(array($addressType.'_adr_fields' => $addressFormat, $addressType.'_all_fields' => $addressItems));
  210. }
  211. /**
  212. * Start forms process
  213. * @see FrontController::postProcess()
  214. */
  215. public function postProcess()
  216. {
  217. if (Tools::isSubmit('SubmitCreate'))
  218. $this->processSubmitCreate();
  219. if (Tools::isSubmit('submitAccount') || Tools::isSubmit('submitGuestAccount'))
  220. $this->processSubmitAccount();
  221. if (Tools::isSubmit('SubmitLogin'))
  222. $this->processSubmitLogin();
  223. }
  224. /**
  225. * Process login
  226. */
  227. protected function processSubmitLogin()
  228. {
  229. Hook::exec('actionBeforeAuthentication');
  230. $passwd = trim(Tools::getValue('passwd'));
  231. $email = trim(Tools::getValue('email'));
  232. if (empty($email))
  233. $this->errors[] = Tools::displayError('E-mail address required');
  234. elseif (!Validate::isEmail($email))
  235. $this->errors[] = Tools::displayError('Invalid e-mail address');
  236. elseif (empty($passwd))
  237. $this->errors[] = Tools::displayError('Password is required');
  238. elseif (!Validate::isPasswd($passwd))
  239. $this->errors[] = Tools::displayError('Invalid password');
  240. else
  241. {
  242. $customer = new Customer();
  243. $authentication = $customer->getByEmail(trim($email), trim($passwd));
  244. if (!$authentication || !$customer->id)
  245. {
  246. // Handle brute force attacks
  247. sleep(1);
  248. $this->errors[] = Tools::displayError('Authentication failed');
  249. }
  250. else
  251. {
  252. $this->context->cookie->id_compare = isset($this->context->cookie->id_compare) ? $this->context->cookie->id_compare: CompareProduct::getIdCompareByIdCustomer($customer->id);
  253. $this->context->cookie->id_customer = (int)($customer->id);
  254. $this->context->cookie->customer_lastname = $customer->lastname;
  255. $this->context->cookie->customer_firstname = $customer->firstname;
  256. $this->context->cookie->logged = 1;
  257. $customer->logged = 1;
  258. $this->context->cookie->is_guest = $customer->isGuest();
  259. $this->context->cookie->passwd = $customer->passwd;
  260. $this->context->cookie->email = $customer->email;
  261. // Add customer to the context
  262. $this->context->customer = $customer;
  263. if (Configuration::get('PS_CART_FOLLOWING') && (empty($this->context->cookie->id_cart) || Cart::getNbProducts($this->context->cookie->id_cart) == 0))
  264. $this->context->cookie->id_cart = (int)Cart::lastNoneOrderedCart($this->context->customer->id);
  265. // Update cart address
  266. $this->context->cart->id = $this->context->cookie->id_cart;
  267. $this->context->cart->setDeliveryOption(null);
  268. $this->context->cart->id_address_delivery = Address::getFirstCustomerAddressId((int)($customer->id));
  269. $this->context->cart->id_address_invoice = Address::getFirstCustomerAddressId((int)($customer->id));
  270. $this->context->cart->secure_key = $customer->secure_key;
  271. $this->context->cart->update();
  272. $this->context->cart->autosetProductAddress();
  273. Hook::exec('actionAuthentication');
  274. // Login information have changed, so we check if the cart rules still apply
  275. CartRule::autoRemoveFromCart($this->context);
  276. CartRule::autoAddToCart($this->context);
  277. if (!$this->ajax)
  278. {
  279. if ($back = Tools::getValue('back'))
  280. Tools::redirect(html_entity_decode($back));
  281. Tools::redirect('index.php?controller=my-account');
  282. }
  283. }
  284. }
  285. if ($this->ajax)
  286. {
  287. $return = array(
  288. 'hasError' => !empty($this->errors),
  289. 'errors' => $this->errors,
  290. 'token' => Tools::getToken(false)
  291. );
  292. die(Tools::jsonEncode($return));
  293. }
  294. else
  295. $this->context->smarty->assign('authentification_error', $this->errors);
  296. }
  297. /**
  298. * Process the newsletter settings and set the customer infos.
  299. *
  300. * @param Customer $customer Reference on the customer Object.
  301. *
  302. * @note At this point, the email has been validated.
  303. */
  304. protected function processCustomerNewsletter(&$customer)
  305. {
  306. if (Tools::getValue('newsletter'))
  307. {
  308. $customer->ip_registration_newsletter = pSQL(Tools::getRemoteAddr());
  309. $customer->newsletter_date_add = pSQL(date('Y-m-d H:i:s'));
  310. if ($module_newsletter = Module::getInstanceByName('blocknewsletter'))
  311. if ($module_newsletter->active)
  312. $module_newsletter->confirmSubscription(Tools::getValue('email'));
  313. }
  314. }
  315. /**
  316. * Process submit on an account
  317. */
  318. protected function processSubmitAccount()
  319. {
  320. Hook::exec('actionBeforeSubmitAccount');
  321. $this->create_account = true;
  322. if (Tools::isSubmit('submitAccount'))
  323. $this->context->smarty->assign('email_create', 1);
  324. // New Guest customer
  325. if (!Tools::getValue('is_new_customer', 1) && !Configuration::get('PS_GUEST_CHECKOUT_ENABLED'))
  326. $this->errors[] = Tools::displayError('You cannot create a guest account.');
  327. if (!Tools::getValue('is_new_customer', 1))
  328. $_POST['passwd'] = md5(time()._COOKIE_KEY_);
  329. if (isset($_POST['guest_email']) && $_POST['guest_email'])
  330. $_POST['email'] = $_POST['guest_email'];
  331. // Checked the user address in case he changed his email address
  332. if (Validate::isEmail($email = Tools::getValue('email')) && !empty($email))
  333. if (Customer::customerExists($email))
  334. $this->errors[] = Tools::displayError('An account is already registered with this e-mail.', false);
  335. // Preparing customer
  336. $customer = new Customer();
  337. $_POST['lastname'] = Tools::getValue('customer_lastname');
  338. $_POST['firstname'] = Tools::getValue('customer_firstname');
  339. if (Configuration::get('PS_ONE_PHONE_AT_LEAST') && !Tools::getValue('phone') && !Tools::getValue('phone_mobile') &&
  340. (Configuration::get('PS_REGISTRATION_PROCESS_TYPE') || Configuration::get('PS_GUEST_CHECKOUT_ENABLED')))
  341. $this->errors[] = Tools::displayError('You must register at least one phone number');
  342. $this->errors = array_unique(array_merge($this->errors, $customer->validateController()));
  343. // Check the requires fields which are settings in the BO
  344. $this->errors = array_merge($this->errors, $customer->validateFieldsRequiredDatabase());
  345. if (!Configuration::get('PS_REGISTRATION_PROCESS_TYPE') && !$this->ajax && !Tools::isSubmit('submitGuestAccount'))
  346. {
  347. if (!count($this->errors))
  348. {
  349. if (Tools::isSubmit('newsletter'))
  350. $this->processCustomerNewsletter($customer);
  351. $customer->birthday = (empty($_POST['years']) ? '' : (int)$_POST['years'].'-'.(int)$_POST['months'].'-'.(int)$_POST['days']);
  352. $customer->active = 1;
  353. // New Guest customer
  354. if (Tools::isSubmit('is_new_customer'))
  355. $customer->is_guest = !Tools::getValue('is_new_customer', 1);
  356. else
  357. $customer->is_guest = 0;
  358. if (!count($this->errors))
  359. if (!$customer->add())
  360. $this->errors[] = Tools::displayError('An error occurred while creating your account.');
  361. else
  362. {
  363. if (!$customer->is_guest)
  364. if (!$this->sendConfirmationMail($customer))
  365. $this->errors[] = Tools::displayError('Cannot send e-mail');
  366. $this->updateContext($customer);
  367. $this->context->cart->update();
  368. Hook::exec('actionCustomerAccountAdd', array(
  369. '_POST' => $_POST,
  370. 'newCustomer' => $customer
  371. ));
  372. if ($this->ajax)
  373. {
  374. $return = array(
  375. 'hasError' => !empty($this->errors),
  376. 'errors' => $this->errors,
  377. 'isSaved' => true,
  378. 'id_customer' => (int)$this->context->cookie->id_customer,
  379. 'id_address_delivery' => $this->context->cart->id_address_delivery,
  380. 'id_address_invoice' => $this->context->cart->id_address_invoice,
  381. 'token' => Tools::getToken(false)
  382. );
  383. die(Tools::jsonEncode($return));
  384. }
  385. // redirection: if cart is not empty : redirection to the cart
  386. if (count($this->context->cart->getProducts(true)) > 0)
  387. Tools::redirect('index.php?controller=order&multi-shipping='.(int)Tools::getValue('multi-shipping'));
  388. // else : redirection to the account
  389. else
  390. Tools::redirect('index.php?controller=my-account');
  391. }
  392. }
  393. }
  394. else // if registration type is in one step, we save the address
  395. {
  396. $lastnameAddress = $_POST['lastname'];
  397. $firstnameAddress = $_POST['firstname'];
  398. // Preparing address
  399. $address = new Address();
  400. $_POST['lastname'] = $lastnameAddress;
  401. $_POST['firstname'] = $firstnameAddress;
  402. $address->id_customer = 1;
  403. $this->errors = array_unique(array_merge($this->errors, $address->validateController()));
  404. // US customer: normalize the address
  405. if ($address->id_country == Country::getByIso('US'))
  406. {
  407. include_once(_PS_TAASC_PATH_.'AddressStandardizationSolution.php');
  408. $normalize = new AddressStandardizationSolution;
  409. $address->address1 = $normalize->AddressLineStandardization($address->address1);
  410. $address->address2 = $normalize->AddressLineStandardization($address->address2);
  411. }
  412. $country = new Country((int)Tools::getValue('id_country'));
  413. if ($country->need_zip_code)
  414. {
  415. if (($postcode = Tools::getValue('postcode')) && $country->zip_code_format)
  416. {
  417. if (!$country->checkZipCode($postcode))
  418. $this->errors[] = sprintf(
  419. Tools::displayError('Zip/Postal code is invalid. Must be typed as follows: %s'),
  420. str_replace('C', $country->iso_code, str_replace('N', '0', str_replace('L', 'A', $country->zip_code_format)))
  421. );
  422. }
  423. elseif ($country->zip_code_format)
  424. $this->errors[] = Tools::displayError('Zip/Postal code is required.');
  425. elseif ($postcode && !preg_match('/^[0-9a-zA-Z -]{4,9}$/ui', $postcode))
  426. $this->errors[] = Tools::displayError('Zip/Postal code is invalid.');
  427. }
  428. if ($country->need_identification_number && (!Tools::getValue('dni') || !Validate::isDniLite(Tools::getValue('dni'))))
  429. $this->errors[] = Tools::displayError('Identification number is incorrect or has already been used.');
  430. elseif (!$country->need_identification_number)
  431. $address->dni = null;
  432. }
  433. if (!@checkdate(Tools::getValue('months'), Tools::getValue('days'), Tools::getValue('years')) && !(Tools::getValue('months') == '' && Tools::getValue('days') == '' && Tools::getValue('years') == ''))
  434. $this->errors[] = Tools::displayError('Invalid date of birth');
  435. if (!count($this->errors))
  436. {
  437. if (Customer::customerExists(Tools::getValue('email')))
  438. $this->errors[] = Tools::displayError('An account is already registered with this e-mail, please enter your password or request a new one.', false);
  439. if (Tools::isSubmit('newsletter'))
  440. $this->processCustomerNewsletter($customer);
  441. $customer->birthday = (empty($_POST['years']) ? '' : (int)$_POST['years'].'-'.(int)$_POST['months'].'-'.(int)$_POST['days']);
  442. if (!count($this->errors))
  443. {
  444. // if registration type is in one step, we save the address
  445. if (Configuration::get('PS_REGISTRATION_PROCESS_TYPE'))
  446. if (!($country = new Country($address->id_country, Configuration::get('PS_LANG_DEFAULT'))) || !Validate::isLoadedObject($country))
  447. die(Tools::displayError());
  448. $contains_state = isset($country) && is_object($country) ? (int)$country->contains_states: 0;
  449. $id_state = isset($address) && is_object($address) ? (int)$address->id_state: 0;
  450. if (Configuration::get('PS_REGISTRATION_PROCESS_TYPE') && $contains_state && !$id_state)
  451. $this->errors[] = Tools::displayError('This country requires a state selection.');
  452. else
  453. {
  454. $customer->active = 1;
  455. // New Guest customer
  456. if (Tools::isSubmit('is_new_customer'))
  457. $customer->is_guest = !Tools::getValue('is_new_customer', 1);
  458. else
  459. $customer->is_guest = 0;
  460. if (!$customer->add())
  461. $this->errors[] = Tools::displayError('An error occurred while creating your account.');
  462. else
  463. {
  464. $address->id_customer = (int)$customer->id;
  465. $this->errors = array_unique(array_merge($this->errors, $address->validateController()));
  466. if (!count($this->errors) && (Configuration::get('PS_REGISTRATION_PROCESS_TYPE') || $this->ajax || Tools::isSubmit('submitGuestAccount')) && !$address->add())
  467. $this->errors[] = Tools::displayError('An error occurred while creating your address.');
  468. else
  469. {
  470. if (!$customer->is_guest)
  471. {
  472. $this->context->customer = $customer;
  473. $customer->cleanGroups();
  474. // we add the guest customer in the default customer group
  475. $customer->addGroups(array((int)Configuration::get('PS_CUSTOMER_GROUP')));
  476. if (!$this->sendConfirmationMail($customer))
  477. $this->errors[] = Tools::displayError('Cannot send e-mail');
  478. }
  479. else
  480. {
  481. $customer->cleanGroups();
  482. // we add the guest customer in the guest customer group
  483. $customer->addGroups(array((int)Configuration::get('PS_GUEST_GROUP')));
  484. }
  485. $this->updateContext($customer);
  486. $this->context->cart->id_address_delivery = Address::getFirstCustomerAddressId((int)$customer->id);
  487. $this->context->cart->id_address_invoice = Address::getFirstCustomerAddressId((int)$customer->id);
  488. // If a logged guest logs in as a customer, the cart secure key was already set and needs to be updated
  489. $this->context->cart->update();
  490. // Avoid articles without delivery address on the cart
  491. $this->context->cart->autosetProductAddress();
  492. Hook::exec('actionCustomerAccountAdd', array(
  493. '_POST' => $_POST,
  494. 'newCustomer' => $customer
  495. ));
  496. if ($this->ajax)
  497. {
  498. $return = array(
  499. 'hasError' => !empty($this->errors),
  500. 'errors' => $this->errors,
  501. 'isSaved' => true,
  502. 'id_customer' => (int)$this->context->cookie->id_customer,
  503. 'id_address_delivery' => $this->context->cart->id_address_delivery,
  504. 'id_address_invoice' => $this->context->cart->id_address_invoice,
  505. 'token' => Tools::getToken(false)
  506. );
  507. die(Tools::jsonEncode($return));
  508. }
  509. // if registration type is in two steps, we redirect to register address
  510. if (!Configuration::get('PS_REGISTRATION_PROCESS_TYPE') && !$this->ajax && !Tools::isSubmit('submitGuestAccount'))
  511. Tools::redirect('index.php?controller=address');
  512. if ($back = Tools::getValue('back'))
  513. Tools::redirect($back);
  514. Tools::redirect('index.php?controller=my-account');
  515. // redirection: if cart is not empty : redirection to the cart
  516. if (count($this->context->cart->getProducts(true)) > 0)
  517. Tools::redirect('index.php?controller=order&multi-shipping='.(int)Tools::getValue('multi-shipping'));
  518. // else : redirection to the account
  519. else
  520. Tools::redirect('index.php?controller=my-account');
  521. }
  522. }
  523. }
  524. }
  525. }
  526. if (count($this->errors))
  527. {
  528. //for retro compatibility to display guest account creation form on authentication page
  529. if (Tools::getValue('submitGuestAccount'))
  530. $_GET['display_guest_checkout'] = 1;
  531. if (!Tools::getValue('is_new_customer'))
  532. unset($_POST['passwd']);
  533. if ($this->ajax)
  534. {
  535. $return = array(
  536. 'hasError' => !empty($this->errors),
  537. 'errors' => $this->errors,
  538. 'isSaved' => false,
  539. 'id_customer' => 0
  540. );
  541. die(Tools::jsonEncode($return));
  542. }
  543. $this->context->smarty->assign('account_error', $this->errors);
  544. }
  545. }
  546. /**
  547. * Process submit on a creation
  548. */
  549. protected function processSubmitCreate()
  550. {
  551. if (!Validate::isEmail($email = Tools::getValue('email_create')) || empty($email))
  552. $this->errors[] = Tools::displayError('Invalid e-mail address');
  553. elseif (Customer::customerExists($email))
  554. {
  555. $this->errors[] = Tools::displayError('An account is already registered with this e-mail, please enter your password or request a new one.', false);
  556. $_POST['email'] = $_POST['email_create'];
  557. unset($_POST['email_create']);
  558. }
  559. else
  560. {
  561. $this->create_account = true;
  562. $this->context->smarty->assign('email_create', Tools::safeOutput($email));
  563. $_POST['email'] = $email;
  564. }
  565. }
  566. /**
  567. * Update context after customer creation
  568. * @param Customer $customer Created customer
  569. */
  570. protected function updateContext(Customer $customer)
  571. {
  572. $this->context->customer = $customer;
  573. $this->context->smarty->assign('confirmation', 1);
  574. $this->context->cookie->id_customer = (int)$customer->id;
  575. $this->context->cookie->customer_lastname = $customer->lastname;
  576. $this->context->cookie->customer_firstname = $customer->firstname;
  577. $this->context->cookie->passwd = $customer->passwd;
  578. $this->context->cookie->logged = 1;
  579. // if register process is in two steps, we display a message to confirm account creation
  580. if (!Configuration::get('PS_REGISTRATION_PROCESS_TYPE'))
  581. $this->context->cookie->account_created = 1;
  582. $customer->logged = 1;
  583. $this->context->cookie->email = $customer->email;
  584. $this->context->cookie->is_guest = !Tools::getValue('is_new_customer', 1);
  585. // Update cart address
  586. $this->context->cart->secure_key = $customer->secure_key;
  587. }
  588. /**
  589. * sendConfirmationMail
  590. * @param Customer $customer
  591. * @return bool
  592. */
  593. protected function sendConfirmationMail(Customer $customer)
  594. {
  595. return Mail::Send(
  596. $this->context->language->id,
  597. 'account',
  598. Mail::l('Welcome!'),
  599. array(
  600. '{firstname}' => $customer->firstname,
  601. '{lastname}' => $customer->lastname,
  602. '{email}' => $customer->email,
  603. '{passwd}' => Tools::getValue('passwd')),
  604. $customer->email,
  605. $customer->firstname.' '.$customer->lastname
  606. );
  607. }
  608. }