PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/magento/app/code/core/Mage/Customer/controllers/AccountController.php

https://bitbucket.org/jit_bec/shopifine
PHP | 847 lines | 598 code | 84 blank | 165 comment | 98 complexity | affcd5ed2c512236cf0b5ac009cf007e MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * Magento
  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@magentocommerce.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 Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Customer
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Customer account controller
  28. *
  29. * @category Mage
  30. * @package Mage_Customer
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Customer_AccountController extends Mage_Core_Controller_Front_Action
  34. {
  35. /**
  36. * Action list where need check enabled cookie
  37. *
  38. * @var array
  39. */
  40. protected $_cookieCheckActions = array('loginPost', 'createpost');
  41. /**
  42. * Retrieve customer session model object
  43. *
  44. * @return Mage_Customer_Model_Session
  45. */
  46. protected function _getSession()
  47. {
  48. return Mage::getSingleton('customer/session');
  49. }
  50. /**
  51. * Action predispatch
  52. *
  53. * Check customer authentication for some actions
  54. */
  55. public function preDispatch()
  56. {
  57. // a brute-force protection here would be nice
  58. parent::preDispatch();
  59. if (!$this->getRequest()->isDispatched()) {
  60. return;
  61. }
  62. $action = $this->getRequest()->getActionName();
  63. $openActions = array(
  64. 'create',
  65. 'login',
  66. 'logoutsuccess',
  67. 'forgotpassword',
  68. 'forgotpasswordpost',
  69. 'resetpassword',
  70. 'resetpasswordpost',
  71. 'confirm',
  72. 'confirmation'
  73. );
  74. $pattern = '/^(' . implode('|', $openActions) . ')/i';
  75. if (!preg_match($pattern, $action)) {
  76. if (!$this->_getSession()->authenticate($this)) {
  77. $this->setFlag('', 'no-dispatch', true);
  78. }
  79. } else {
  80. $this->_getSession()->setNoReferer(true);
  81. }
  82. }
  83. /**
  84. * Action postdispatch
  85. *
  86. * Remove No-referer flag from customer session after each action
  87. */
  88. public function postDispatch()
  89. {
  90. parent::postDispatch();
  91. $this->_getSession()->unsNoReferer(false);
  92. }
  93. /**
  94. * Default customer account page
  95. */
  96. public function indexAction()
  97. {
  98. $this->loadLayout();
  99. $this->_initLayoutMessages('customer/session');
  100. $this->_initLayoutMessages('catalog/session');
  101. $this->getLayout()->getBlock('content')->append(
  102. $this->getLayout()->createBlock('customer/account_dashboard')
  103. );
  104. $this->getLayout()->getBlock('head')->setTitle($this->__('My Account'));
  105. $this->renderLayout();
  106. }
  107. /**
  108. * Customer login form page
  109. */
  110. public function loginAction()
  111. {
  112. if ($this->_getSession()->isLoggedIn()) {
  113. $this->_redirect('*/*/');
  114. return;
  115. }
  116. $this->getResponse()->setHeader('Login-Required', 'true');
  117. $this->loadLayout();
  118. $this->_initLayoutMessages('customer/session');
  119. $this->_initLayoutMessages('catalog/session');
  120. $this->renderLayout();
  121. }
  122. /**
  123. * Login post action
  124. */
  125. public function loginPostAction()
  126. {
  127. if ($this->_getSession()->isLoggedIn()) {
  128. $this->_redirect('*/*/');
  129. return;
  130. }
  131. $session = $this->_getSession();
  132. if ($this->getRequest()->isPost()) {
  133. $login = $this->getRequest()->getPost('login');
  134. if (!empty($login['username']) && !empty($login['password'])) {
  135. try {
  136. $session->login($login['username'], $login['password']);
  137. if ($session->getCustomer()->getIsJustConfirmed()) {
  138. $this->_welcomeCustomer($session->getCustomer(), true);
  139. }
  140. } catch (Mage_Core_Exception $e) {
  141. switch ($e->getCode()) {
  142. case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
  143. $value = Mage::helper('customer')->getEmailConfirmationUrl($login['username']);
  144. $message = Mage::helper('customer')->__('This account is not confirmed. <a href="%s">Click here</a> to resend confirmation email.', $value);
  145. break;
  146. case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD:
  147. $message = $e->getMessage();
  148. break;
  149. default:
  150. $message = $e->getMessage();
  151. }
  152. $session->addError($message);
  153. $session->setUsername($login['username']);
  154. } catch (Exception $e) {
  155. // Mage::logException($e); // PA DSS violation: this exception log can disclose customer password
  156. }
  157. } else {
  158. $session->addError($this->__('Login and password are required.'));
  159. }
  160. }
  161. $this->_loginPostRedirect();
  162. }
  163. /**
  164. * Define target URL and redirect customer after logging in
  165. */
  166. protected function _loginPostRedirect()
  167. {
  168. $session = $this->_getSession();
  169. if (!$session->getBeforeAuthUrl() || $session->getBeforeAuthUrl() == Mage::getBaseUrl()) {
  170. // Set default URL to redirect customer to
  171. $session->setBeforeAuthUrl(Mage::helper('customer')->getAccountUrl());
  172. // Redirect customer to the last page visited after logging in
  173. if ($session->isLoggedIn()) {
  174. if (!Mage::getStoreConfigFlag(
  175. Mage_Customer_Helper_Data::XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD
  176. )) {
  177. $referer = $this->getRequest()->getParam(Mage_Customer_Helper_Data::REFERER_QUERY_PARAM_NAME);
  178. if ($referer) {
  179. // Rebuild referer URL to handle the case when SID was changed
  180. $referer = Mage::getModel('core/url')
  181. ->getRebuiltUrl(Mage::helper('core')->urlDecode($referer));
  182. if ($this->_isUrlInternal($referer)) {
  183. $session->setBeforeAuthUrl($referer);
  184. }
  185. }
  186. } else if ($session->getAfterAuthUrl()) {
  187. $session->setBeforeAuthUrl($session->getAfterAuthUrl(true));
  188. }
  189. } else {
  190. $session->setBeforeAuthUrl(Mage::helper('customer')->getLoginUrl());
  191. }
  192. } else if ($session->getBeforeAuthUrl() == Mage::helper('customer')->getLogoutUrl()) {
  193. $session->setBeforeAuthUrl(Mage::helper('customer')->getDashboardUrl());
  194. } else {
  195. if (!$session->getAfterAuthUrl()) {
  196. $session->setAfterAuthUrl($session->getBeforeAuthUrl());
  197. }
  198. if ($session->isLoggedIn()) {
  199. $session->setBeforeAuthUrl($session->getAfterAuthUrl(true));
  200. }
  201. }
  202. $this->_redirectUrl($session->getBeforeAuthUrl(true));
  203. }
  204. /**
  205. * Customer logout action
  206. */
  207. public function logoutAction()
  208. {
  209. $this->_getSession()->logout()
  210. ->setBeforeAuthUrl(Mage::getUrl());
  211. $this->_redirect('*/*/logoutSuccess');
  212. }
  213. /**
  214. * Logout success page
  215. */
  216. public function logoutSuccessAction()
  217. {
  218. $this->loadLayout();
  219. $this->renderLayout();
  220. }
  221. /**
  222. * Customer register form page
  223. */
  224. public function createAction()
  225. {
  226. if ($this->_getSession()->isLoggedIn()) {
  227. $this->_redirect('*/*');
  228. return;
  229. }
  230. $this->loadLayout();
  231. $this->_initLayoutMessages('customer/session');
  232. $this->renderLayout();
  233. }
  234. /**
  235. * Create customer account action
  236. */
  237. public function createPostAction()
  238. {
  239. $session = $this->_getSession();
  240. if ($session->isLoggedIn()) {
  241. $this->_redirect('*/*/');
  242. return;
  243. }
  244. $session->setEscapeMessages(true); // prevent XSS injection in user input
  245. if ($this->getRequest()->isPost()) {
  246. $errors = array();
  247. if (!$customer = Mage::registry('current_customer')) {
  248. $customer = Mage::getModel('customer/customer')->setId(null);
  249. }
  250. /* @var $customerForm Mage_Customer_Model_Form */
  251. $customerForm = Mage::getModel('customer/form');
  252. $customerForm->setFormCode('customer_account_create')
  253. ->setEntity($customer);
  254. $customerData = $customerForm->extractData($this->getRequest());
  255. if ($this->getRequest()->getParam('is_subscribed', false)) {
  256. $customer->setIsSubscribed(1);
  257. }
  258. /**
  259. * Initialize customer group id
  260. */
  261. $customer->getGroupId();
  262. if ($this->getRequest()->getPost('create_address')) {
  263. /* @var $address Mage_Customer_Model_Address */
  264. $address = Mage::getModel('customer/address');
  265. /* @var $addressForm Mage_Customer_Model_Form */
  266. $addressForm = Mage::getModel('customer/form');
  267. $addressForm->setFormCode('customer_register_address')
  268. ->setEntity($address);
  269. $addressData = $addressForm->extractData($this->getRequest(), 'address', false);
  270. $addressErrors = $addressForm->validateData($addressData);
  271. if ($addressErrors === true) {
  272. $address->setId(null)
  273. ->setIsDefaultBilling($this->getRequest()->getParam('default_billing', false))
  274. ->setIsDefaultShipping($this->getRequest()->getParam('default_shipping', false));
  275. $addressForm->compactData($addressData);
  276. $customer->addAddress($address);
  277. $addressErrors = $address->validate();
  278. if (is_array($addressErrors)) {
  279. $errors = array_merge($errors, $addressErrors);
  280. }
  281. } else {
  282. $errors = array_merge($errors, $addressErrors);
  283. }
  284. }
  285. try {
  286. $customerErrors = $customerForm->validateData($customerData);
  287. if ($customerErrors !== true) {
  288. $errors = array_merge($customerErrors, $errors);
  289. } else {
  290. $customerForm->compactData($customerData);
  291. $customer->setPassword($this->getRequest()->getPost('password'));
  292. $customer->setConfirmation($this->getRequest()->getPost('confirmation'));
  293. $customerErrors = $customer->validate();
  294. if (is_array($customerErrors)) {
  295. $errors = array_merge($customerErrors, $errors);
  296. }
  297. }
  298. $validationResult = count($errors) == 0;
  299. if (true === $validationResult) {
  300. $customer->save();
  301. Mage::dispatchEvent('customer_register_success',
  302. array('account_controller' => $this, 'customer' => $customer)
  303. );
  304. if ($customer->isConfirmationRequired()) {
  305. $customer->sendNewAccountEmail(
  306. 'confirmation',
  307. $session->getBeforeAuthUrl(),
  308. Mage::app()->getStore()->getId()
  309. );
  310. $session->addSuccess($this->__('Account confirmation is required. Please, check your email for the confirmation link. To resend the confirmation email please <a href="%s">click here</a>.', Mage::helper('customer')->getEmailConfirmationUrl($customer->getEmail())));
  311. $this->_redirectSuccess(Mage::getUrl('*/*/index', array('_secure'=>true)));
  312. return;
  313. } else {
  314. $session->setCustomerAsLoggedIn($customer);
  315. $url = $this->_welcomeCustomer($customer);
  316. $this->_redirectSuccess($url);
  317. return;
  318. }
  319. } else {
  320. $session->setCustomerFormData($this->getRequest()->getPost());
  321. if (is_array($errors)) {
  322. foreach ($errors as $errorMessage) {
  323. $session->addError($errorMessage);
  324. }
  325. } else {
  326. $session->addError($this->__('Invalid customer data'));
  327. }
  328. }
  329. } catch (Mage_Core_Exception $e) {
  330. $session->setCustomerFormData($this->getRequest()->getPost());
  331. if ($e->getCode() === Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS) {
  332. $url = Mage::getUrl('customer/account/forgotpassword');
  333. $message = $this->__('There is already an account with this email address. If you are sure that it is your email address, <a href="%s">click here</a> to get your password and access your account.', $url);
  334. $session->setEscapeMessages(false);
  335. } else {
  336. $message = $e->getMessage();
  337. }
  338. $session->addError($message);
  339. } catch (Exception $e) {
  340. $session->setCustomerFormData($this->getRequest()->getPost())
  341. ->addException($e, $this->__('Cannot save the customer.'));
  342. }
  343. }
  344. $this->_redirectError(Mage::getUrl('*/*/create', array('_secure' => true)));
  345. }
  346. /**
  347. * Add welcome message and send new account email.
  348. * Returns success URL
  349. *
  350. * @param Mage_Customer_Model_Customer $customer
  351. * @param bool $isJustConfirmed
  352. * @return string
  353. */
  354. protected function _welcomeCustomer(Mage_Customer_Model_Customer $customer, $isJustConfirmed = false)
  355. {
  356. $this->_getSession()->addSuccess(
  357. $this->__('Thank you for registering with %s.', Mage::app()->getStore()->getFrontendName())
  358. );
  359. if ($this->_isVatValidationEnabled()) {
  360. // Show corresponding VAT message to customer
  361. $configAddressType = Mage::helper('customer/address')->getTaxCalculationAddressType();
  362. $userPrompt = '';
  363. switch ($configAddressType) {
  364. case Mage_Customer_Model_Address_Abstract::TYPE_SHIPPING:
  365. $userPrompt = $this->__('If you are a registered VAT customer, please click <a href="%s">here</a> to enter you shipping address for proper VAT calculation', Mage::getUrl('customer/address/edit'));
  366. break;
  367. default:
  368. $userPrompt = $this->__('If you are a registered VAT customer, please click <a href="%s">here</a> to enter you billing address for proper VAT calculation', Mage::getUrl('customer/address/edit'));
  369. }
  370. $this->_getSession()->addSuccess($userPrompt);
  371. }
  372. $customer->sendNewAccountEmail(
  373. $isJustConfirmed ? 'confirmed' : 'registered',
  374. '',
  375. Mage::app()->getStore()->getId()
  376. );
  377. $successUrl = Mage::getUrl('*/*/index', array('_secure'=>true));
  378. if ($this->_getSession()->getBeforeAuthUrl()) {
  379. $successUrl = $this->_getSession()->getBeforeAuthUrl(true);
  380. }
  381. return $successUrl;
  382. }
  383. /**
  384. * Confirm customer account by id and confirmation key
  385. */
  386. public function confirmAction()
  387. {
  388. if ($this->_getSession()->isLoggedIn()) {
  389. $this->_redirect('*/*/');
  390. return;
  391. }
  392. try {
  393. $id = $this->getRequest()->getParam('id', false);
  394. $key = $this->getRequest()->getParam('key', false);
  395. $backUrl = $this->getRequest()->getParam('back_url', false);
  396. if (empty($id) || empty($key)) {
  397. throw new Exception($this->__('Bad request.'));
  398. }
  399. // load customer by id (try/catch in case if it throws exceptions)
  400. try {
  401. $customer = Mage::getModel('customer/customer')->load($id);
  402. if ((!$customer) || (!$customer->getId())) {
  403. throw new Exception('Failed to load customer by id.');
  404. }
  405. }
  406. catch (Exception $e) {
  407. throw new Exception($this->__('Wrong customer account specified.'));
  408. }
  409. // check if it is inactive
  410. if ($customer->getConfirmation()) {
  411. if ($customer->getConfirmation() !== $key) {
  412. throw new Exception($this->__('Wrong confirmation key.'));
  413. }
  414. // activate customer
  415. try {
  416. $customer->setConfirmation(null);
  417. $customer->save();
  418. }
  419. catch (Exception $e) {
  420. throw new Exception($this->__('Failed to confirm customer account.'));
  421. }
  422. // log in and send greeting email, then die happy
  423. $this->_getSession()->setCustomerAsLoggedIn($customer);
  424. $successUrl = $this->_welcomeCustomer($customer, true);
  425. $this->_redirectSuccess($backUrl ? $backUrl : $successUrl);
  426. return;
  427. }
  428. // die happy
  429. $this->_redirectSuccess(Mage::getUrl('*/*/index', array('_secure'=>true)));
  430. return;
  431. }
  432. catch (Exception $e) {
  433. // die unhappy
  434. $this->_getSession()->addError($e->getMessage());
  435. $this->_redirectError(Mage::getUrl('*/*/index', array('_secure'=>true)));
  436. return;
  437. }
  438. }
  439. /**
  440. * Send confirmation link to specified email
  441. */
  442. public function confirmationAction()
  443. {
  444. $customer = Mage::getModel('customer/customer');
  445. if ($this->_getSession()->isLoggedIn()) {
  446. $this->_redirect('*/*/');
  447. return;
  448. }
  449. // try to confirm by email
  450. $email = $this->getRequest()->getPost('email');
  451. if ($email) {
  452. try {
  453. $customer->setWebsiteId(Mage::app()->getStore()->getWebsiteId())->loadByEmail($email);
  454. if (!$customer->getId()) {
  455. throw new Exception('');
  456. }
  457. if ($customer->getConfirmation()) {
  458. $customer->sendNewAccountEmail('confirmation', '', Mage::app()->getStore()->getId());
  459. $this->_getSession()->addSuccess($this->__('Please, check your email for confirmation key.'));
  460. } else {
  461. $this->_getSession()->addSuccess($this->__('This email does not require confirmation.'));
  462. }
  463. $this->_getSession()->setUsername($email);
  464. $this->_redirectSuccess(Mage::getUrl('*/*/index', array('_secure' => true)));
  465. } catch (Exception $e) {
  466. $this->_getSession()->addException($e, $this->__('Wrong email.'));
  467. $this->_redirectError(Mage::getUrl('*/*/*', array('email' => $email, '_secure' => true)));
  468. }
  469. return;
  470. }
  471. // output form
  472. $this->loadLayout();
  473. $this->getLayout()->getBlock('accountConfirmation')
  474. ->setEmail($this->getRequest()->getParam('email', $email));
  475. $this->_initLayoutMessages('customer/session');
  476. $this->renderLayout();
  477. }
  478. /**
  479. * Forgot customer password page
  480. */
  481. public function forgotPasswordAction()
  482. {
  483. $this->loadLayout();
  484. $this->getLayout()->getBlock('forgotPassword')->setEmailValue(
  485. $this->_getSession()->getForgottenEmail()
  486. );
  487. $this->_getSession()->unsForgottenEmail();
  488. $this->_initLayoutMessages('customer/session');
  489. $this->renderLayout();
  490. }
  491. /**
  492. * Forgot customer password action
  493. */
  494. public function forgotPasswordPostAction()
  495. {
  496. $email = (string) $this->getRequest()->getPost('email');
  497. if ($email) {
  498. if (!Zend_Validate::is($email, 'EmailAddress')) {
  499. $this->_getSession()->setForgottenEmail($email);
  500. $this->_getSession()->addError($this->__('Invalid email address.'));
  501. $this->_redirect('*/*/forgotpassword');
  502. return;
  503. }
  504. /** @var $customer Mage_Customer_Model_Customer */
  505. $customer = Mage::getModel('customer/customer')
  506. ->setWebsiteId(Mage::app()->getStore()->getWebsiteId())
  507. ->loadByEmail($email);
  508. if ($customer->getId()) {
  509. try {
  510. $newResetPasswordLinkToken = Mage::helper('customer')->generateResetPasswordLinkToken();
  511. $customer->changeResetPasswordLinkToken($newResetPasswordLinkToken);
  512. $customer->sendPasswordResetConfirmationEmail();
  513. } catch (Exception $exception) {
  514. $this->_getSession()->addError($exception->getMessage());
  515. $this->_redirect('*/*/forgotpassword');
  516. return;
  517. }
  518. }
  519. $this->_getSession()
  520. ->addSuccess(Mage::helper('customer')->__('If there is an account associated with %s you will receive an email with a link to reset your password.', Mage::helper('customer')->htmlEscape($email)));
  521. $this->_redirect('*/*/');
  522. return;
  523. } else {
  524. $this->_getSession()->addError($this->__('Please enter your email.'));
  525. $this->_redirect('*/*/forgotpassword');
  526. return;
  527. }
  528. }
  529. /**
  530. * Display reset forgotten password form
  531. *
  532. * User is redirected on this action when he clicks on the corresponding link in password reset confirmation email
  533. *
  534. */
  535. public function resetPasswordAction()
  536. {
  537. $resetPasswordLinkToken = (string) $this->getRequest()->getQuery('token');
  538. $customerId = (int) $this->getRequest()->getQuery('id');
  539. try {
  540. $this->_validateResetPasswordLinkToken($customerId, $resetPasswordLinkToken);
  541. $this->loadLayout();
  542. // Pass received parameters to the reset forgotten password form
  543. $this->getLayout()->getBlock('resetPassword')
  544. ->setCustomerId($customerId)
  545. ->setResetPasswordLinkToken($resetPasswordLinkToken);
  546. $this->renderLayout();
  547. } catch (Exception $exception) {
  548. $this->_getSession()->addError(Mage::helper('customer')->__('Your password reset link has expired.'));
  549. $this->_redirect('*/*/forgotpassword');
  550. }
  551. }
  552. /**
  553. * Reset forgotten password
  554. *
  555. * Used to handle data recieved from reset forgotten password form
  556. *
  557. */
  558. public function resetPasswordPostAction()
  559. {
  560. $resetPasswordLinkToken = (string) $this->getRequest()->getQuery('token');
  561. $customerId = (int) $this->getRequest()->getQuery('id');
  562. $password = (string) $this->getRequest()->getPost('password');
  563. $passwordConfirmation = (string) $this->getRequest()->getPost('confirmation');
  564. try {
  565. $this->_validateResetPasswordLinkToken($customerId, $resetPasswordLinkToken);
  566. } catch (Exception $exception) {
  567. $this->_getSession()->addError(Mage::helper('customer')->__('Your password reset link has expired.'));
  568. $this->_redirect('*/*/');
  569. return;
  570. }
  571. $errorMessages = array();
  572. if (iconv_strlen($password) <= 0) {
  573. array_push($errorMessages, Mage::helper('customer')->__('New password field cannot be empty.'));
  574. }
  575. /** @var $customer Mage_Customer_Model_Customer */
  576. $customer = Mage::getModel('customer/customer')->load($customerId);
  577. $customer->setPassword($password);
  578. $customer->setConfirmation($passwordConfirmation);
  579. $validationErrorMessages = $customer->validate();
  580. if (is_array($validationErrorMessages)) {
  581. $errorMessages = array_merge($errorMessages, $validationErrorMessages);
  582. }
  583. if (!empty($errorMessages)) {
  584. $this->_getSession()->setCustomerFormData($this->getRequest()->getPost());
  585. foreach ($errorMessages as $errorMessage) {
  586. $this->_getSession()->addError($errorMessage);
  587. }
  588. $this->_redirect('*/*/resetpassword', array(
  589. 'id' => $customerId,
  590. 'token' => $resetPasswordLinkToken
  591. ));
  592. return;
  593. }
  594. try {
  595. // Empty current reset password token i.e. invalidate it
  596. $customer->setRpToken(null);
  597. $customer->setRpTokenCreatedAt(null);
  598. $customer->setConfirmation(null);
  599. $customer->save();
  600. $this->_getSession()->addSuccess(Mage::helper('customer')->__('Your password has been updated.'));
  601. $this->_redirect('*/*/login');
  602. } catch (Exception $exception) {
  603. $this->_getSession()->addException($exception, $this->__('Cannot save a new password.'));
  604. $this->_redirect('*/*/resetpassword', array(
  605. 'id' => $customerId,
  606. 'token' => $resetPasswordLinkToken
  607. ));
  608. return;
  609. }
  610. }
  611. /**
  612. * Check if password reset token is valid
  613. *
  614. * @param int $customerId
  615. * @param string $resetPasswordLinkToken
  616. * @throws Mage_Core_Exception
  617. */
  618. protected function _validateResetPasswordLinkToken($customerId, $resetPasswordLinkToken)
  619. {
  620. if (!is_int($customerId)
  621. || !is_string($resetPasswordLinkToken)
  622. || empty($resetPasswordLinkToken)
  623. || empty($customerId)
  624. || $customerId < 0
  625. ) {
  626. throw Mage::exception('Mage_Core', Mage::helper('customer')->__('Invalid password reset token.'));
  627. }
  628. /** @var $customer Mage_Customer_Model_Customer */
  629. $customer = Mage::getModel('customer/customer')->load($customerId);
  630. if (!$customer || !$customer->getId()) {
  631. throw Mage::exception('Mage_Core', Mage::helper('customer')->__('Wrong customer account specified.'));
  632. }
  633. $customerToken = $customer->getRpToken();
  634. if (strcmp($customerToken, $resetPasswordLinkToken) != 0 || $customer->isResetPasswordLinkTokenExpired()) {
  635. throw Mage::exception('Mage_Core', Mage::helper('customer')->__('Your password reset link has expired.'));
  636. }
  637. }
  638. /**
  639. * Forgot customer account information page
  640. */
  641. public function editAction()
  642. {
  643. $this->loadLayout();
  644. $this->_initLayoutMessages('customer/session');
  645. $this->_initLayoutMessages('catalog/session');
  646. $block = $this->getLayout()->getBlock('customer_edit');
  647. if ($block) {
  648. $block->setRefererUrl($this->_getRefererUrl());
  649. }
  650. $data = $this->_getSession()->getCustomerFormData(true);
  651. $customer = $this->_getSession()->getCustomer();
  652. if (!empty($data)) {
  653. $customer->addData($data);
  654. }
  655. if ($this->getRequest()->getParam('changepass')==1){
  656. $customer->setChangePassword(1);
  657. }
  658. $this->getLayout()->getBlock('head')->setTitle($this->__('Account Information'));
  659. $this->getLayout()->getBlock('messages')->setEscapeMessageFlag(true);
  660. $this->renderLayout();
  661. }
  662. /**
  663. * Change customer password action
  664. */
  665. public function editPostAction()
  666. {
  667. if (!$this->_validateFormKey()) {
  668. return $this->_redirect('*/*/edit');
  669. }
  670. if ($this->getRequest()->isPost()) {
  671. /** @var $customer Mage_Customer_Model_Customer */
  672. $customer = $this->_getSession()->getCustomer();
  673. /** @var $customerForm Mage_Customer_Model_Form */
  674. $customerForm = Mage::getModel('customer/form');
  675. $customerForm->setFormCode('customer_account_edit')
  676. ->setEntity($customer);
  677. $customerData = $customerForm->extractData($this->getRequest());
  678. $errors = array();
  679. $customerErrors = $customerForm->validateData($customerData);
  680. if ($customerErrors !== true) {
  681. $errors = array_merge($customerErrors, $errors);
  682. } else {
  683. $customerForm->compactData($customerData);
  684. $errors = array();
  685. // If password change was requested then add it to common validation scheme
  686. if ($this->getRequest()->getParam('change_password')) {
  687. $currPass = $this->getRequest()->getPost('current_password');
  688. $newPass = $this->getRequest()->getPost('password');
  689. $confPass = $this->getRequest()->getPost('confirmation');
  690. $oldPass = $this->_getSession()->getCustomer()->getPasswordHash();
  691. if (Mage::helper('core/string')->strpos($oldPass, ':')) {
  692. list($_salt, $salt) = explode(':', $oldPass);
  693. } else {
  694. $salt = false;
  695. }
  696. if ($customer->hashPassword($currPass, $salt) == $oldPass) {
  697. if (strlen($newPass)) {
  698. /**
  699. * Set entered password and its confirmation - they
  700. * will be validated later to match each other and be of right length
  701. */
  702. $customer->setPassword($newPass);
  703. $customer->setConfirmation($confPass);
  704. } else {
  705. $errors[] = $this->__('New password field cannot be empty.');
  706. }
  707. } else {
  708. $errors[] = $this->__('Invalid current password');
  709. }
  710. }
  711. // Validate account and compose list of errors if any
  712. $customerErrors = $customer->validate();
  713. if (is_array($customerErrors)) {
  714. $errors = array_merge($errors, $customerErrors);
  715. }
  716. }
  717. if (!empty($errors)) {
  718. $this->_getSession()->setCustomerFormData($this->getRequest()->getPost());
  719. foreach ($errors as $message) {
  720. $this->_getSession()->addError($message);
  721. }
  722. $this->_redirect('*/*/edit');
  723. return $this;
  724. }
  725. try {
  726. $customer->setConfirmation(null);
  727. $customer->save();
  728. $this->_getSession()->setCustomer($customer)
  729. ->addSuccess($this->__('The account information has been saved.'));
  730. $this->_redirect('customer/account');
  731. return;
  732. } catch (Mage_Core_Exception $e) {
  733. $this->_getSession()->setCustomerFormData($this->getRequest()->getPost())
  734. ->addError($e->getMessage());
  735. } catch (Exception $e) {
  736. $this->_getSession()->setCustomerFormData($this->getRequest()->getPost())
  737. ->addException($e, $this->__('Cannot save the customer.'));
  738. }
  739. }
  740. $this->_redirect('*/*/edit');
  741. }
  742. /**
  743. * Filtering posted data. Converting localized data if needed
  744. *
  745. * @param array
  746. * @return array
  747. */
  748. protected function _filterPostData($data)
  749. {
  750. $data = $this->_filterDates($data, array('dob'));
  751. return $data;
  752. }
  753. /**
  754. * Check whether VAT ID validation is enabled
  755. *
  756. * @param Mage_Core_Model_Store|string|int $store
  757. * @return bool
  758. */
  759. protected function _isVatValidationEnabled($store = null)
  760. {
  761. return Mage::helper('customer/address')->isVatValidationEnabled($store);
  762. }
  763. }