PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/authentication.php

https://github.com/pal/prestashop
PHP | 233 lines | 205 code | 21 blank | 7 comment | 35 complexity | 98202d36012723e4c605a0eb020e015d MD5 | raw file
  1. <?php
  2. /* SSL Management */
  3. $useSSL = true;
  4. include(dirname(__FILE__).'/config/config.inc.php');
  5. include(dirname(__FILE__).'/init.php');
  6. if ($cookie->isLogged())
  7. Tools::redirect('my-account.php');
  8. //CSS ans JS file calls
  9. $js_files = array(
  10. _THEME_JS_DIR_.'tools/statesManagement.js',
  11. __PS_BASE_URI__.'js/jquery/jquery-typewatch.pack.js'
  12. );
  13. $errors = array();
  14. $back = Tools::getValue('back');
  15. $key = Tools::safeOutput(Tools::getValue('key'));
  16. if (!empty($key))
  17. $back .= (strpos($back, '?') !== false ? '&' : '?').'key='.$key;
  18. if (!empty($back))
  19. $smarty->assign('back', Tools::safeOutput($back));
  20. if (Tools::getValue('create_account'))
  21. {
  22. $create_account = 1;
  23. $smarty->assign('email_create', 1);
  24. }
  25. if (Tools::isSubmit('SubmitCreate'))
  26. {
  27. if (!Validate::isEmail($email = Tools::getValue('email_create')))
  28. $errors[] = Tools::displayError('invalid e-mail address');
  29. elseif (Customer::customerExists($email))
  30. $errors[] = Tools::displayError('someone has already registered with this e-mail address');
  31. else
  32. {
  33. $create_account = 1;
  34. $smarty->assign('email_create', Tools::safeOutput($email));
  35. $_POST['email'] = $email;
  36. }
  37. }
  38. if (Tools::isSubmit('submitAccount'))
  39. {
  40. $create_account = 1;
  41. $smarty->assign('email_create', 1);
  42. $validateDni = Validate::isDni(Tools::getValue('dni'));
  43. if (!Validate::isEmail($email = Tools::getValue('email')))
  44. $errors[] = Tools::displayError('e-mail not valid');
  45. elseif (!Validate::isPasswd(Tools::getValue('passwd')))
  46. $errors[] = Tools::displayError('invalid password');
  47. elseif (Customer::customerExists($email))
  48. $errors[] = Tools::displayError('someone has already registered with this e-mail address');
  49. elseif (Tools::getValue('dni') != NULL AND $validateDni != 1)
  50. {
  51. $error = array(
  52. 0 => Tools::displayError('DNI isn\'t valid'),
  53. -1 => Tools::displayError('this DNI has been already used'),
  54. -2 => Tools::displayError('NIF isn\'t valid'),
  55. -3 => Tools::displayError('CIF isn\'t valid'),
  56. -4 => Tools::displayError('NIE isn\'t valid')
  57. );
  58. $errors[] = $error[$validateDni];
  59. }
  60. elseif (!@checkdate(Tools::getValue('months'), Tools::getValue('days'), Tools::getValue('years')) AND !(Tools::getValue('months') == '' AND Tools::getValue('days') == '' AND Tools::getValue('years') == ''))
  61. $errors[] = Tools::displayError('invalid birthday');
  62. else
  63. {
  64. $customer = new Customer();
  65. if (Tools::isSubmit('newsletter'))
  66. {
  67. $customer->ip_registration_newsletter = pSQL($_SERVER['REMOTE_ADDR']);
  68. $customer->newsletter_date_add = pSQL(date('Y-m-d H:i:s'));
  69. }
  70. $customer->birthday = (empty($_POST['years']) ? '' : intval($_POST['years']).'-'.intval($_POST['months']).'-'.intval($_POST['days']));
  71. /* Customer and address, same fields, caching data */
  72. $addrLastname = isset($_POST['lastname']) ? $_POST['lastname'] : $_POST['customer_lastname'];
  73. $addrFirstname = isset( $_POST['firstname']) ? $_POST['firstname'] : $_POST['customer_firstname'];
  74. $_POST['lastname'] = $_POST['customer_lastname'];
  75. $_POST['firstname'] = $_POST['customer_firstname'];
  76. $errors = $customer->validateControler();
  77. $_POST['lastname'] = $addrLastname;
  78. $_POST['firstname'] = $addrFirstname;
  79. $address = new Address();
  80. $address->id_customer = 1;
  81. $errors = array_unique(array_merge($errors, $address->validateControler()));
  82. if (!sizeof($errors))
  83. {
  84. if (!$country = new Country($address->id_country) OR !Validate::isLoadedObject($country))
  85. die(Tools::displayError());
  86. if (intval($country->contains_states) AND !intval($address->id_state))
  87. $errors[] = Tools::displayError('this country require a state selection');
  88. else
  89. {
  90. $customer->active = 1;
  91. if (!$customer->add())
  92. $errors[] = Tools::displayError('an error occurred while creating your account');
  93. else
  94. {
  95. $address->id_customer = intval($customer->id);
  96. if (!$address->add())
  97. $errors[] = Tools::displayError('an error occurred while creating your address');
  98. else
  99. {
  100. if (!Mail::Send(intval($cookie->id_lang), 'account', 'Welcome!',
  101. array('{firstname}' => $customer->firstname, '{lastname}' => $customer->lastname, '{email}' => $customer->email, '{passwd}' => Tools::getValue('passwd')), $customer->email, $customer->firstname.' '.$customer->lastname))
  102. $errors[] = Tools::displayError('cannot send email');
  103. $smarty->assign('confirmation', 1);
  104. $cookie->id_customer = intval($customer->id);
  105. $cookie->customer_lastname = $customer->lastname;
  106. $cookie->customer_firstname = $customer->firstname;
  107. $cookie->passwd = $customer->passwd;
  108. $cookie->logged = 1;
  109. $cookie->email = $customer->email;
  110. Module::hookExec('createAccount', array(
  111. '_POST' => $_POST,
  112. 'newCustomer' => $customer
  113. ));
  114. if ($back)
  115. Tools::redirect($back);
  116. }
  117. }
  118. }
  119. }
  120. }
  121. }
  122. if (Tools::isSubmit('SubmitLogin'))
  123. {
  124. $passwd = trim(Tools::getValue('passwd'));
  125. $email = trim(Tools::getValue('email'));
  126. if (empty($email))
  127. $errors[] = Tools::displayError('e-mail address is required');
  128. elseif (!Validate::isEmail($email))
  129. $errors[] = Tools::displayError('invalid e-mail address');
  130. elseif (empty($passwd))
  131. $errors[] = Tools::displayError('password is required');
  132. elseif (Tools::strlen($passwd) > 32)
  133. $errors[] = Tools::displayError('password is too long');
  134. elseif (!Validate::isPasswd($passwd))
  135. $errors[] = Tools::displayError('invalid password');
  136. else
  137. {
  138. $customer = new Customer();
  139. $authentication = $customer->getByemail(trim($email), trim($passwd));
  140. /* Handle brute force attacks */
  141. sleep(1);
  142. if (!$authentication OR !$customer->id)
  143. $errors[] = Tools::displayError('authentication failed');
  144. else
  145. {
  146. $cookie->id_customer = intval($customer->id);
  147. $cookie->customer_lastname = $customer->lastname;
  148. $cookie->customer_firstname = $customer->firstname;
  149. $cookie->logged = 1;
  150. $cookie->passwd = $customer->passwd;
  151. $cookie->email = $customer->email;
  152. if (Configuration::get('PS_CART_FOLLOWING') AND (empty($cookie->id_cart) OR Cart::getNbProducts($cookie->id_cart) == 0))
  153. $cookie->id_cart = intval(Cart::lastNoneOrderedCart(intval($customer->id)));
  154. $id_address = intval(Address::getFirstCustomerAddressId(intval($customer->id)));
  155. $cookie->id_address_delivery = $id_address;
  156. $cookie->id_address_invoice = $id_address;
  157. Module::hookExec('authentication');
  158. if ($back = Tools::getValue('back'))
  159. Tools::redirect($back);
  160. Tools::redirect('my-account.php');
  161. }
  162. }
  163. }
  164. if (isset($create_account))
  165. {
  166. /* Generate years, months and days */
  167. if (isset($_POST['years']) AND is_numeric($_POST['years']))
  168. $selectedYears = intval($_POST['years']);
  169. $years = Tools::dateYears();
  170. if (isset($_POST['months']) AND is_numeric($_POST['months']))
  171. $selectedMonths = intval($_POST['months']);
  172. $months = Tools::dateMonths();
  173. if (isset($_POST['days']) AND is_numeric($_POST['days']))
  174. $selectedDays = intval($_POST['days']);
  175. $days = Tools::dateDays();
  176. /* Select the most appropriate country */
  177. if (isset($_POST['id_country']) AND is_numeric($_POST['id_country']))
  178. $selectedCountry = intval($_POST['id_country']);
  179. elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
  180. {
  181. $array = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
  182. if (Validate::isLanguageIsoCode($array[0]))
  183. {
  184. $selectedCountry = Country::getByIso($array[0]);
  185. if (!$selectedCountry)
  186. $selectedCountry = intval(Configuration::get('PS_COUNTRY_DEFAULT'));
  187. }
  188. }
  189. if (!isset($selectedCountry))
  190. $selectedCountry = intval(Configuration::get('PS_COUNTRY_DEFAULT'));
  191. $countries = Country::getCountries(intval($cookie->id_lang), true);
  192. $smarty->assign(array(
  193. 'years' => $years,
  194. 'sl_year' => (isset($selectedYears) ? $selectedYears : 0),
  195. 'months' => $months,
  196. 'sl_month' => (isset($selectedMonths) ? $selectedMonths : 0),
  197. 'days' => $days,
  198. 'sl_day' => (isset($selectedDays) ? $selectedDays : 0),
  199. 'countries' => $countries,
  200. 'sl_country' => (isset($selectedCountry) ? $selectedCountry : 0)
  201. ));
  202. /* Call a hook to display more information on form */
  203. $smarty->assign(array('HOOK_CREATE_ACCOUNT_FORM' => Module::hookExec('createAccountForm'),
  204. 'HOOK_CREATE_ACCOUNT_TOP' => Module::hookExec('createAccountTop')
  205. ));
  206. }
  207. include(dirname(__FILE__).'/header.php');
  208. $smarty->assign('errors', $errors);
  209. Tools::safePostVars();
  210. $smarty->display(_PS_THEME_DIR_.'authentication.tpl');
  211. include(dirname(__FILE__).'/footer.php');
  212. ?>