PageRenderTime 55ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/controllers/AuthController.php

http://marocmall.googlecode.com/
PHP | 408 lines | 326 code | 30 blank | 52 comment | 54 complexity | 7d033060314232e9c7b3e3affa75768c MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. * 2007-2011 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-2011 PrestaShop SA
  23. * @version Release: $Revision: 6878 $
  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 function __construct()
  30. {
  31. $this->ssl = true;
  32. $this->php_self = 'authentication.php';
  33. parent::__construct();
  34. }
  35. public function preProcess()
  36. {
  37. parent::preProcess();
  38. if (self::$cookie->isLogged() AND !Tools::isSubmit('ajax'))
  39. Tools::redirect('my-account.php');
  40. if (Tools::getValue('create_account'))
  41. {
  42. $create_account = 1;
  43. self::$smarty->assign('email_create', 1);
  44. }
  45. if (Tools::isSubmit('SubmitCreate'))
  46. {
  47. if (!Validate::isEmail($email = Tools::getValue('email_create')) OR empty($email))
  48. $this->errors[] = Tools::displayError('Invalid e-mail address');
  49. elseif (Customer::customerExists($email))
  50. {
  51. $this->errors[] = Tools::displayError('An account is already registered with this e-mail, please fill in the password or request a new one.');
  52. $_POST['email'] = $_POST['email_create'];
  53. unset($_POST['email_create']);
  54. }
  55. else
  56. {
  57. $create_account = 1;
  58. self::$smarty->assign('email_create', Tools::safeOutput($email));
  59. $_POST['email'] = $email;
  60. }
  61. }
  62. if (Tools::isSubmit('submitAccount') OR Tools::isSubmit('submitGuestAccount'))
  63. {
  64. $create_account = 1;
  65. if (Tools::isSubmit('submitAccount'))
  66. self::$smarty->assign('email_create', 1);
  67. /* New Guest customer */
  68. if (!Tools::getValue('is_new_customer', 1) AND !Configuration::get('PS_GUEST_CHECKOUT_ENABLED'))
  69. $this->errors[] = Tools::displayError('You cannot create a guest account.');
  70. if (!Tools::getValue('is_new_customer', 1))
  71. $_POST['passwd'] = md5(time()._COOKIE_KEY_);
  72. if (isset($_POST['guest_email']) AND $_POST['guest_email'])
  73. $_POST['email'] = $_POST['guest_email'];
  74. /* Preparing customer */
  75. $customer = new Customer();
  76. $lastnameAddress = $_POST['lastname'];
  77. $firstnameAddress = $_POST['firstname'];
  78. $_POST['lastname'] = $_POST['customer_lastname'];
  79. $_POST['firstname'] = $_POST['customer_firstname'];
  80. if (!Tools::getValue('phone') AND !Tools::getValue('phone_mobile'))
  81. $this->errors[] = Tools::displayError('You must register at least one phone number');
  82. $this->errors = array_unique(array_merge($this->errors, $customer->validateControler()));
  83. /* Preparing address */
  84. $address = new Address();
  85. $_POST['lastname'] = $lastnameAddress;
  86. $_POST['firstname'] = $firstnameAddress;
  87. $address->id_customer = 1;
  88. $this->errors = array_unique(array_merge($this->errors, $address->validateControler()));
  89. /* US customer: normalize the address */
  90. if($address->id_country == Country::getByIso('US'))
  91. {
  92. include_once(_PS_TAASC_PATH_.'AddressStandardizationSolution.php');
  93. $normalize = new AddressStandardizationSolution;
  94. $address->address1 = $normalize->AddressLineStandardization($address->address1);
  95. $address->address2 = $normalize->AddressLineStandardization($address->address2);
  96. }
  97. $zip_code_format = Country::getZipCodeFormat((int)(Tools::getValue('id_country')));
  98. if (Country::getNeedZipCode((int)(Tools::getValue('id_country'))))
  99. {
  100. if (($postcode = Tools::getValue('postcode')) AND $zip_code_format)
  101. {
  102. $zip_regexp = '/^'.$zip_code_format.'$/ui';
  103. $zip_regexp = str_replace(' ', '( |)', $zip_regexp);
  104. $zip_regexp = str_replace('-', '(-|)', $zip_regexp);
  105. $zip_regexp = str_replace('N', '[0-9]', $zip_regexp);
  106. $zip_regexp = str_replace('L', '[a-zA-Z]', $zip_regexp);
  107. $zip_regexp = str_replace('C', Country::getIsoById((int)(Tools::getValue('id_country'))), $zip_regexp);
  108. if (!preg_match($zip_regexp, $postcode))
  109. $this->errors[] = '<strong>'.Tools::displayError('Zip/ Postal code').'</strong> '.Tools::displayError('is invalid.').'<br />'.Tools::displayError('Must be typed as follows:').' '.str_replace('C', Country::getIsoById((int)(Tools::getValue('id_country'))), str_replace('N', '0', str_replace('L', 'A', $zip_code_format)));
  110. }
  111. elseif ($zip_code_format)
  112. $this->errors[] = '<strong>'.Tools::displayError('Zip/ Postal code').'</strong> '.Tools::displayError('is required.');
  113. elseif ($postcode AND !preg_match('/^[0-9a-zA-Z -]{4,9}$/ui', $postcode))
  114. $this->errors[] = '<strong>'.Tools::displayError('Zip/ Postal code').'</strong> '.Tools::displayError('is invalid.');
  115. }
  116. if (Country::isNeedDniByCountryId($address->id_country) AND (!Tools::getValue('dni') OR !Validate::isDniLite(Tools::getValue('dni'))))
  117. $this->errors[] = Tools::displayError('Identification number is incorrect or has already been used.');
  118. elseif (!Country::isNeedDniByCountryId($address->id_country))
  119. $address->dni = NULL;
  120. if (!@checkdate(Tools::getValue('months'), Tools::getValue('days'), Tools::getValue('years')) AND !(Tools::getValue('months') == '' AND Tools::getValue('days') == '' AND Tools::getValue('years') == ''))
  121. $this->errors[] = Tools::displayError('Invalid date of birth');
  122. if (!sizeof($this->errors))
  123. {
  124. if (Customer::customerExists(Tools::getValue('email')))
  125. $this->errors[] = Tools::displayError('An account is already registered with this e-mail, please fill in the password or request a new one.');
  126. if (Tools::isSubmit('newsletter'))
  127. {
  128. $customer->ip_registration_newsletter = pSQL(Tools::getRemoteAddr());
  129. $customer->newsletter_date_add = pSQL(date('Y-m-d H:i:s'));
  130. }
  131. $customer->birthday = (empty($_POST['years']) ? '' : (int)($_POST['years']).'-'.(int)($_POST['months']).'-'.(int)($_POST['days']));
  132. if (!sizeof($this->errors))
  133. {
  134. if (!$country = new Country($address->id_country, Configuration::get('PS_LANG_DEFAULT')) OR !Validate::isLoadedObject($country))
  135. die(Tools::displayError());
  136. if ((int)($country->contains_states) AND !(int)($address->id_state))
  137. $this->errors[] = Tools::displayError('This country requires a state selection.');
  138. else
  139. {
  140. $customer->active = 1;
  141. /* New Guest customer */
  142. if (Tools::isSubmit('is_new_customer'))
  143. $customer->is_guest = !Tools::getValue('is_new_customer', 1);
  144. else
  145. $customer->is_guest = 0;
  146. if (!$customer->add())
  147. $this->errors[] = Tools::displayError('An error occurred while creating your account.');
  148. else
  149. {
  150. $address->id_customer = (int)($customer->id);
  151. if (!$address->add())
  152. $this->errors[] = Tools::displayError('An error occurred while creating your address.');
  153. else
  154. {
  155. if (!$customer->is_guest)
  156. {
  157. if (!Mail::Send((int)(self::$cookie->id_lang), 'account', Mail::l('Welcome!'),
  158. array('{firstname}' => $customer->firstname, '{lastname}' => $customer->lastname, '{email}' => $customer->email, '{passwd}' => Tools::getValue('passwd')), $customer->email, $customer->firstname.' '.$customer->lastname))
  159. $this->errors[] = Tools::displayError('Cannot send email');
  160. }
  161. self::$smarty->assign('confirmation', 1);
  162. self::$cookie->id_customer = (int)($customer->id);
  163. self::$cookie->customer_lastname = $customer->lastname;
  164. self::$cookie->customer_firstname = $customer->firstname;
  165. self::$cookie->passwd = $customer->passwd;
  166. self::$cookie->logged = 1;
  167. self::$cookie->email = $customer->email;
  168. self::$cookie->is_guest = !Tools::getValue('is_new_customer', 1);
  169. /* Update cart address */
  170. self::$cart->secure_key = $customer->secure_key;
  171. self::$cart->id_address_delivery = Address::getFirstCustomerAddressId((int)($customer->id));
  172. self::$cart->id_address_invoice = Address::getFirstCustomerAddressId((int)($customer->id));
  173. self::$cart->update();
  174. Module::hookExec('createAccount', array(
  175. '_POST' => $_POST,
  176. 'newCustomer' => $customer
  177. ));
  178. if (Tools::isSubmit('ajax'))
  179. {
  180. $return = array(
  181. 'hasError' => !empty($this->errors),
  182. 'errors' => $this->errors,
  183. 'isSaved' => true,
  184. 'id_customer' => (int)self::$cookie->id_customer,
  185. 'id_address_delivery' => self::$cart->id_address_delivery,
  186. 'id_address_invoice' => self::$cart->id_address_invoice,
  187. 'token' => Tools::getToken(false)
  188. );
  189. die(Tools::jsonEncode($return));
  190. }
  191. if ($back = Tools::getValue('back'))
  192. Tools::redirect($back);
  193. Tools::redirect('my-account.php');
  194. }
  195. }
  196. }
  197. }
  198. }
  199. if (sizeof($this->errors))
  200. {
  201. if (!Tools::getValue('is_new_customer'))
  202. unset($_POST['passwd']);
  203. if (Tools::isSubmit('ajax'))
  204. {
  205. $return = array(
  206. 'hasError' => !empty($this->errors),
  207. 'errors' => $this->errors,
  208. 'isSaved' => false,
  209. 'id_customer' => 0
  210. );
  211. die(Tools::jsonEncode($return));
  212. }
  213. }
  214. }
  215. if (Tools::isSubmit('SubmitLogin'))
  216. {
  217. Module::hookExec('beforeAuthentication');
  218. $passwd = trim(Tools::getValue('passwd'));
  219. $email = trim(Tools::getValue('email'));
  220. if (empty($email))
  221. $this->errors[] = Tools::displayError('E-mail address required');
  222. elseif (!Validate::isEmail($email))
  223. $this->errors[] = Tools::displayError('Invalid e-mail address');
  224. elseif (empty($passwd))
  225. $this->errors[] = Tools::displayError('Password is required');
  226. elseif (Tools::strlen($passwd) > 32)
  227. $this->errors[] = Tools::displayError('Password is too long');
  228. elseif (!Validate::isPasswd($passwd))
  229. $this->errors[] = Tools::displayError('Invalid password');
  230. else
  231. {
  232. $customer = new Customer();
  233. $authentication = $customer->getByEmail(trim($email), trim($passwd));
  234. if (!$authentication OR !$customer->id)
  235. {
  236. /* Handle brute force attacks */
  237. sleep(1);
  238. $this->errors[] = Tools::displayError('Authentication failed');
  239. }
  240. else
  241. {
  242. self::$cookie->id_customer = (int)($customer->id);
  243. self::$cookie->customer_lastname = $customer->lastname;
  244. self::$cookie->customer_firstname = $customer->firstname;
  245. self::$cookie->logged = 1;
  246. self::$cookie->is_guest = $customer->isGuest();
  247. self::$cookie->passwd = $customer->passwd;
  248. self::$cookie->email = $customer->email;
  249. if (Configuration::get('PS_CART_FOLLOWING') AND (empty(self::$cookie->id_cart) OR Cart::getNbProducts(self::$cookie->id_cart) == 0))
  250. self::$cookie->id_cart = (int)(Cart::lastNoneOrderedCart((int)($customer->id)));
  251. /* Update cart address */
  252. self::$cart->id_carrier = 0;
  253. self::$cart->id_address_delivery = Address::getFirstCustomerAddressId((int)($customer->id));
  254. self::$cart->id_address_invoice = Address::getFirstCustomerAddressId((int)($customer->id));
  255. self::$cart->update();
  256. Module::hookExec('authentication');
  257. if (!Tools::isSubmit('ajax'))
  258. {
  259. if ($back = Tools::getValue('back'))
  260. Tools::redirect($back);
  261. Tools::redirect('my-account.php');
  262. }
  263. }
  264. }
  265. if (Tools::isSubmit('ajax'))
  266. {
  267. $return = array(
  268. 'hasError' => !empty($this->errors),
  269. 'errors' => $this->errors,
  270. 'token' => Tools::getToken(false)
  271. );
  272. die(Tools::jsonEncode($return));
  273. }
  274. }
  275. if (isset($create_account))
  276. {
  277. /* Select the most appropriate country */
  278. if (isset($_POST['id_country']) AND is_numeric($_POST['id_country']))
  279. $selectedCountry = (int)($_POST['id_country']);
  280. /* FIXME : language iso and country iso are not similar,
  281. * maybe an associative table with country an language can resolve it,
  282. * But for now it's a bug !
  283. * @see : bug #6968
  284. * @link:http://www.prestashop.com/bug_tracker/view/6968/
  285. elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
  286. {
  287. $array = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
  288. if (Validate::isLanguageIsoCode($array[0]))
  289. {
  290. $selectedCountry = Country::getByIso($array[0]);
  291. if (!$selectedCountry)
  292. $selectedCountry = (int)(Configuration::get('PS_COUNTRY_DEFAULT'));
  293. }
  294. }*/
  295. if (!isset($selectedCountry))
  296. $selectedCountry = (int)(Configuration::get('PS_COUNTRY_DEFAULT'));
  297. $countries = Country::getCountries((int)(self::$cookie->id_lang), true);
  298. self::$smarty->assign(array(
  299. 'countries' => $countries,
  300. 'sl_country' => (isset($selectedCountry) ? $selectedCountry : 0),
  301. 'vat_management' => Configuration::get('VATNUMBER_MANAGEMENT')
  302. ));
  303. /* Call a hook to display more information on form */
  304. self::$smarty->assign(array(
  305. 'HOOK_CREATE_ACCOUNT_FORM' => Module::hookExec('createAccountForm'),
  306. 'HOOK_CREATE_ACCOUNT_TOP' => Module::hookExec('createAccountTop')
  307. ));
  308. }
  309. /* Generate years, months and days */
  310. if (isset($_POST['years']) AND is_numeric($_POST['years']))
  311. $selectedYears = (int)($_POST['years']);
  312. $years = Tools::dateYears();
  313. if (isset($_POST['months']) AND is_numeric($_POST['months']))
  314. $selectedMonths = (int)($_POST['months']);
  315. $months = Tools::dateMonths();
  316. if (isset($_POST['days']) AND is_numeric($_POST['days']))
  317. $selectedDays = (int)($_POST['days']);
  318. $days = Tools::dateDays();
  319. self::$smarty->assign(array(
  320. 'years' => $years,
  321. 'sl_year' => (isset($selectedYears) ? $selectedYears : 0),
  322. 'months' => $months,
  323. 'sl_month' => (isset($selectedMonths) ? $selectedMonths : 0),
  324. 'days' => $days,
  325. 'sl_day' => (isset($selectedDays) ? $selectedDays : 0)
  326. ));
  327. self::$smarty->assign('newsletter', (int)Module::getInstanceByName('blocknewsletter')->active);
  328. }
  329. public function setMedia()
  330. {
  331. parent::setMedia();
  332. Tools::addCSS(_THEME_CSS_DIR_.'authentication.css');
  333. Tools::addJS(array(_THEME_JS_DIR_.'tools/statesManagement.js', _PS_JS_DIR_.'jquery/jquery-typewatch.pack.js'));
  334. }
  335. public function process()
  336. {
  337. parent::process();
  338. $back = Tools::getValue('back');
  339. $key = Tools::safeOutput(Tools::getValue('key'));
  340. if (!empty($key))
  341. $back .= (strpos($back, '?') !== false ? '&' : '?').'key='.$key;
  342. if (!empty($back))
  343. {
  344. self::$smarty->assign('back', Tools::safeOutput($back));
  345. if (strpos($back, 'order.php') !== false)
  346. {
  347. $countries = Country::getCountries((int)(self::$cookie->id_lang), true);
  348. self::$smarty->assign(array(
  349. 'inOrderProcess' => true,
  350. 'PS_GUEST_CHECKOUT_ENABLED' => Configuration::get('PS_GUEST_CHECKOUT_ENABLED'),
  351. 'sl_country' => (int)Tools::getValue('id_country', Configuration::get('PS_COUNTRY_DEFAULT')),
  352. 'countries' => $countries
  353. ));
  354. }
  355. }
  356. }
  357. public function displayContent()
  358. {
  359. $this->processAddressFormat();
  360. parent::displayContent();
  361. self::$smarty->display(_PS_THEME_DIR_.'authentication.tpl');
  362. }
  363. protected function processAddressFormat()
  364. {
  365. $addressItems = array();
  366. $addressFormat = AddressFormat::getOrderedAddressFields(Configuration::get('PS_COUNTRY_DEFAULT'));
  367. $requireFormFieldsList = AddressFormat::$requireFormFieldsList;
  368. foreach ($addressFormat as $addressline)
  369. foreach (explode(' ', $addressline) as $addressItem)
  370. $addressItems[] = trim($addressItem);
  371. // Add missing require fields for a new user susbscription form
  372. foreach($requireFormFieldsList as $fieldName)
  373. if (!in_array($fieldName, $addressItems))
  374. $addressItems[] = trim($fieldName);
  375. foreach (array('inv', 'dlv') as $addressType)
  376. self::$smarty->assign(array($addressType.'_adr_fields' => $addressFormat, $addressType.'_all_fields' => $addressItems));
  377. }
  378. }