*/ protected function _getHelper($path) { return Mage::helper($path); } /** * Get App * * @return Mage_Core_Model_App */ protected function _getApp() { return Mage::app(); } /** * Dispatch Event * * @param Mage_Customer_Model_Customer $customer */ protected function _dispatchRegisterSuccess($customer) { Mage::dispatchEvent('customer_register_success', array('account_controller' => $this, 'customer' => $customer) ); } /** * Gets customer address * * @param $customer * @return array $errors */ protected function _getErrorsOnCustomerAddress($customer) { $errors = array(); /* @var $address Mage_Customer_Model_Address */ $address = $this->_getModel('customer/address'); /* @var $addressForm Mage_Customer_Model_Form */ $addressForm = $this->_getModel('customer/form'); $addressForm->setFormCode('customer_register_address') ->setEntity($address); $addressData = $addressForm->extractData($this->getRequest(), 'address', false); $addressErrors = $addressForm->validateData($addressData); if (is_array($addressErrors)) { $errors = array_merge($errors, $addressErrors); } $address->setId(null) ->setIsDefaultBilling($this->getRequest()->getParam('default_billing', false)) ->setIsDefaultShipping($this->getRequest()->getParam('default_shipping', false)); $addressForm->compactData($addressData); $customer->addAddress($address); $addressErrors = $address->validate(); if (is_array($addressErrors)) { $errors = array_merge($errors, $addressErrors); } return $errors; } /** * Get model by path * * @param string $path * @param array|null $arguments * @return false|Mage_Core_Model_Abstract */ public function _getModel($path, $arguments = array()) { return Mage::getModel($path, $arguments); } /** * Get model from registry by path * * @param string $path * @return mixed */ protected function _getFromRegistry($path) { return Mage::registry($path); } /** * Add welcome message and send new account email. * Returns success URL * * @param Mage_Customer_Model_Customer $customer * @param bool $isJustConfirmed * @return string */ protected function _welcomeCustomer(Mage_Customer_Model_Customer $customer, $isJustConfirmed = false) { $this->_getSession()->addSuccess( $this->__('Thank you for registering with %s.', Mage::app()->getStore()->getFrontendName()) ); if ($this->_isVatValidationEnabled()) { // Show corresponding VAT message to customer $configAddressType = $this->_getHelper('customer/address')->getTaxCalculationAddressType(); $userPrompt = ''; switch ($configAddressType) { case Mage_Customer_Model_Address_Abstract::TYPE_SHIPPING: $userPrompt = $this->__('If you are a registered VAT customer, please click here to enter you shipping address for proper VAT calculation', $this->_getUrl('customer/address/edit')); break; default: $userPrompt = $this->__('If you are a registered VAT customer, please click here to enter you billing address for proper VAT calculation', $this->_getUrl('customer/address/edit')); } $this->_getSession()->addSuccess($userPrompt); } $customer->sendNewAccountEmail( $isJustConfirmed ? 'confirmed' : 'registered', '', Mage::app()->getStore()->getId() ); $successUrl = $this->_getUrl('*/*/index', array('_secure' => true)); if ($this->_getSession()->getBeforeAuthUrl()) { $successUrl = $this->_getSession()->getBeforeAuthUrl(true); } return $successUrl; } /** * Confirm customer account by id and confirmation key */ public function confirmAction() { $session = $this->_getSession(); if ($session->isLoggedIn()) { $this->_getSession()->logout()->regenerateSessionId(); } try { $id = $this->getRequest()->getParam('id', false); $key = $this->getRequest()->getParam('key', false); $backUrl = $this->getRequest()->getParam('back_url', false); if (empty($id) || empty($key)) { throw new Exception($this->__('Bad request.')); } // load customer by id (try/catch in case if it throws exceptions) try { $customer = $this->_getModel('customer/customer')->load($id); if ((!$customer) || (!$customer->getId())) { throw new Exception('Failed to load customer by id.'); } } catch (Exception $e) { throw new Exception($this->__('Wrong customer account specified.')); } // check if it is inactive if ($customer->getConfirmation()) { if ($customer->getConfirmation() !== $key) { throw new Exception($this->__('Wrong confirmation key.')); } // activate customer try { $customer->setConfirmation(null); $customer->save(); } catch (Exception $e) { throw new Exception($this->__('Failed to confirm customer account.')); } // log in and send greeting email, then die happy $session->setCustomerAsLoggedIn($customer); $successUrl = $this->_welcomeCustomer($customer, true); $this->_redirectSuccess($backUrl ? $backUrl : $successUrl); return; } // die happy $this->_redirectSuccess($this->_getUrl('*/*/index', array('_secure' => true))); return; } catch (Exception $e) { // die unhappy $this->_getSession()->addError($e->getMessage()); $this->_redirectError($this->_getUrl('*/*/index', array('_secure' => true))); return; } } /** * Send confirmation link to specified email */ public function confirmationAction() { $customer = $this->_getModel('customer/customer'); if ($this->_getSession()->isLoggedIn()) { $this->_redirect('*/*/'); return; } // try to confirm by email $email = $this->getRequest()->getPost('email'); if ($email) { try { $customer->setWebsiteId(Mage::app()->getStore()->getWebsiteId())->loadByEmail($email); if (!$customer->getId()) { throw new Exception(''); } if ($customer->getConfirmation()) { $customer->sendNewAccountEmail('confirmation', '', Mage::app()->getStore()->getId()); $this->_getSession()->addSuccess($this->__('Please, check your email for confirmation key.')); } else { $this->_getSession()->addSuccess($this->__('This email does not require confirmation.')); } $this->_getSession()->setUsername($email); $this->_redirectSuccess($this->_getUrl('*/*/index', array('_secure' => true))); } catch (Exception $e) { $this->_getSession()->addException($e, $this->__('Wrong email.')); $this->_redirectError($this->_getUrl('*/*/*', array('email' => $email, '_secure' => true))); } return; } // output form $this->loadLayout(); $this->getLayout()->getBlock('accountConfirmation') ->setEmail($this->getRequest()->getParam('email', $email)); $this->_initLayoutMessages('customer/session'); $this->renderLayout(); } /** * Get Url method * * @param string $url * @param array $params * @return string */ protected function _getUrl($url, $params = array()) { return Mage::getUrl($url, $params); } /** * Forgot customer password page */ public function forgotPasswordAction() { $this->loadLayout(); $this->getLayout()->getBlock('forgotPassword')->setEmailValue( $this->_getSession()->getForgottenEmail() ); $this->_getSession()->unsForgottenEmail(); $this->_initLayoutMessages('customer/session'); $this->renderLayout(); } /** * Forgot customer password action */ public function forgotPasswordPostAction() { $email = (string) $this->getRequest()->getPost('email'); if ($email) { if (!Zend_Validate::is($email, 'EmailAddress')) { $this->_getSession()->setForgottenEmail($email); $this->_getSession()->addError($this->__('Invalid email address.')); $this->_redirect('*/*/forgotpassword'); return; } /** @var $customer Mage_Customer_Model_Customer */ $customer = $this->_getModel('customer/customer') ->setWebsiteId(Mage::app()->getStore()->getWebsiteId()) ->loadByEmail($email); if ($customer->getId()) { try { $newResetPasswordLinkToken = $this->_getHelper('customer')->generateResetPasswordLinkToken(); $customer->changeResetPasswordLinkToken($newResetPasswordLinkToken); $customer->sendPasswordResetConfirmationEmail(); } catch (Exception $exception) { $this->_getSession()->addError($exception->getMessage()); $this->_redirect('*/*/forgotpassword'); return; } } $this->_getSession() ->addSuccess( $this->_getHelper('customer') ->__('If there is an account associated with %s you will receive an email with a link to reset your password.', $this->_getHelper('customer')->escapeHtml($email))); $this->_redirect('*/*/'); return; } else { $this->_getSession()->addError($this->__('Please enter your email.')); $this->_redirect('*/*/forgotpassword'); return; } } /** * Display reset forgotten password form * * User is redirected on this action when he clicks on the corresponding link in password reset confirmation email * */ public function resetPasswordAction() { $resetPasswordLinkToken = (string) $this->getRequest()->getQuery('token'); $customerId = (int) $this->getRequest()->getQuery('id'); try { $this->_validateResetPasswordLinkToken($customerId, $resetPasswordLinkToken); $this->loadLayout(); // Pass received parameters to the reset forgotten password form $this->getLayout()->getBlock('resetPassword') ->setCustomerId($customerId) ->setResetPasswordLinkToken($resetPasswordLinkToken); $this->renderLayout(); } catch (Exception $exception) { $this->_getSession()->addError( $this->_getHelper('customer')->__('Your password reset link has expired.')); $this->_redirect('*/*/forgotpassword'); } } /** * Reset forgotten password * Used to handle data recieved from reset forgotten password form */ public function resetPasswordPostAction() { $resetPasswordLinkToken = (string) $this->getRequest()->getQuery('token'); $customerId = (int) $this->getRequest()->getQuery('id'); $password = (string) $this->getRequest()->getPost('password'); $passwordConfirmation = (string) $this->getRequest()->getPost('confirmation'); try { $this->_validateResetPasswordLinkToken($customerId, $resetPasswordLinkToken); } catch (Exception $exception) { $this->_getSession()->addError( $this->_getHelper('customer')->__('Your password reset link has expired.')); $this->_redirect('*/*/'); return; } $errorMessages = array(); if (iconv_strlen($password) <= 0) { array_push($errorMessages, $this->_getHelper('customer')->__('New password field cannot be empty.')); } /** @var $customer Mage_Customer_Model_Customer */ $customer = $this->_getModel('customer/customer')->load($customerId); $customer->setPassword($password); $customer->setPasswordConfirmation($passwordConfirmation); $validationErrorMessages = $customer->validate(); if (is_array($validationErrorMessages)) { $errorMessages = array_merge($errorMessages, $validationErrorMessages); } if (!empty($errorMessages)) { $this->_getSession()->setCustomerFormData($this->getRequest()->getPost()); foreach ($errorMessages as $errorMessage) { $this->_getSession()->addError($errorMessage); } $this->_redirect('*/*/resetpassword', array( 'id' => $customerId, 'token' => $resetPasswordLinkToken )); return; } try { // Empty current reset password token i.e. invalidate it $customer->setRpToken(null); $customer->setRpTokenCreatedAt(null); $customer->cleanPasswordsValidationData(); $customer->save(); $this->_getSession()->addSuccess( $this->_getHelper('customer')->__('Your password has been updated.')); $this->_redirect('*/*/login'); } catch (Exception $exception) { $this->_getSession()->addException($exception, $this->__('Cannot save a new password.')); $this->_redirect('*/*/resetpassword', array( 'id' => $customerId, 'token' => $resetPasswordLinkToken )); return; } } /** * Check if password reset token is valid * * @param int $customerId * @param string $resetPasswordLinkToken * @throws Mage_Core_Exception */ protected function _validateResetPasswordLinkToken($customerId, $resetPasswordLinkToken) { if (!is_int($customerId) || !is_string($resetPasswordLinkToken) || empty($resetPasswordLinkToken) || empty($customerId) || $customerId < 0 ) { throw Mage::exception('Mage_Core', $this->_getHelper('customer')->__('Invalid password reset token.')); } /** @var $customer Mage_Customer_Model_Customer */ $customer = $this->_getModel('customer/customer')->load($customerId); if (!$customer || !$customer->getId()) { throw Mage::exception('Mage_Core', $this->_getHelper('customer')->__('Wrong customer account specified.')); } $customerToken = $customer->getRpToken(); if (strcmp($customerToken, $resetPasswordLinkToken) != 0 || $customer->isResetPasswordLinkTokenExpired()) { throw Mage::exception('Mage_Core', $this->_getHelper('customer')->__('Your password reset link has expired.')); } } /** * Forgot customer account information page */ public function editAction() { $this->loadLayout(); $this->_initLayoutMessages('customer/session'); $this->_initLayoutMessages('catalog/session'); $block = $this->getLayout()->getBlock('customer_edit'); if ($block) { $block->setRefererUrl($this->_getRefererUrl()); } $data = $this->_getSession()->getCustomerFormData(true); $customer = $this->_getSession()->getCustomer(); if (!empty($data)) { $customer->addData($data); } if ($this->getRequest()->getParam('changepass') == 1) { $customer->setChangePassword(1); } $this->getLayout()->getBlock('head')->setTitle($this->__('Account Information')); $this->getLayout()->getBlock('messages')->setEscapeMessageFlag(true); $this->renderLayout(); } /** * Change customer password action */ public function editPostAction() { if (!$this->_validateFormKey()) { return $this->_redirect('*/*/edit'); } if ($this->getRequest()->isPost()) { /** @var $customer Mage_Customer_Model_Customer */ $customer = $this->_getSession()->getCustomer(); /** @var $customerForm Mage_Customer_Model_Form */ $customerForm = $this->_getModel('customer/form'); $customerForm->setFormCode('customer_account_edit') ->setEntity($customer); $customerData = $customerForm->extractData($this->getRequest()); $errors = array(); $customerErrors = $customerForm->validateData($customerData); if ($customerErrors !== true) { $errors = array_merge($customerErrors, $errors); } else { $customerForm->compactData($customerData); $errors = array(); // If password change was requested then add it to common validation scheme if ($this->getRequest()->getParam('change_password')) { $currPass = $this->getRequest()->getPost('current_password'); $newPass = $this->getRequest()->getPost('password'); $confPass = $this->getRequest()->getPost('confirmation'); $oldPass = $this->_getSession()->getCustomer()->getPasswordHash(); if ( $this->_getHelper('core/string')->strpos($oldPass, ':')) { list($_salt, $salt) = explode(':', $oldPass); } else { $salt = false; } if ($customer->hashPassword($currPass, $salt) == $oldPass) { if (strlen($newPass)) { /** * Set entered password and its confirmation - they * will be validated later to match each other and be of right length */ $customer->setPassword($newPass); $customer->setPasswordConfirmation($confPass); } else { $errors[] = $this->__('New password field cannot be empty.'); } } else { $errors[] = $this->__('Invalid current password'); } } // Validate account and compose list of errors if any $customerErrors = $customer->validate(); if (is_array($customerErrors)) { $errors = array_merge($errors, $customerErrors); } } if (!empty($errors)) { $this->_getSession()->setCustomerFormData($this->getRequest()->getPost()); foreach ($errors as $message) { $this->_getSession()->addError($message); } $this->_redirect('*/*/edit'); return $this; } try { $customer->cleanPasswordsValidationData(); $customer->save(); $this->_getSession()->setCustomer($customer) ->addSuccess($this->__('The account information has been saved.')); $this->_redirect('customer/account'); return; } catch (Mage_Core_Exception $e) { $this->_getSession()->setCustomerFormData($this->getRequest()->getPost()) ->addError($e->getMessage()); } catch (Exception $e) { $this->_getSession()->setCustomerFormData($this->getRequest()->getPost()) ->addException($e, $this->__('Cannot save the customer.')); } } $this->_redirect('*/*/edit'); } /** * Filtering posted data. Converting localized data if needed * * @param array * @return array */ protected function _filterPostData($data) { $data = $this->_filterDates($data, array('dob')); return $data; } /** * Check whether VAT ID validation is enabled * * @param Mage_Core_Model_Store|string|int $store * @return bool */ protected function _isVatValidationEnabled($store = null) { return $this->_getHelper('customer/address')->isVatValidationEnabled($store); } }