/**
  * Activation link lands here to activate user account
  */
 public function activateAction()
 {
     $this->_helper->_layout->setLayout('layout_wide');
     // flush if already logged in
     Zend_Auth::getInstance()->clearIdentity();
     $activateaccount_form = new Application_Form_ActivateAccount();
     $this->view->activateaccount_form = $activateaccount_form;
     $key = $this->getRequest()->getParam('key', false);
     $resend_username = $this->getRequest()->getParam('resend', false);
     $Profiles = new Application_Model_Profiles();
     $ProfilesMeta = new Application_Model_ProfilesMeta();
     $userData = $Profiles->getProfileByField('activationkey', $key);
     if (!$userData || $key == 'activated') {
         // try if this is a resend
         $userData = $Profiles->getProfile($resend_username);
         if (!$userData || $userData->activationkey == 'activated') {
             $this->redirect('');
         } else {
             $resend_lock = $ProfilesMeta->getMetaValue('resend_activation_lock', $userData->id);
             $hour_lock = date('H');
             // prevent too many attempts
             if ($resend_lock && $resend_lock == $hour_lock) {
                 Application_Plugin_Alerts::info(Zend_Registry::get('Zend_Translate')->translate('Please Check your Inbox and come back after you activate your account.'), 'off');
                 $this->redirect('');
             }
             $ret = Application_Plugin_Common::sendActivationEmail($userData->email, $userData->name, $userData->activationkey);
             // email has been sent, show success message
             if ($ret) {
                 Application_Plugin_Alerts::info(Zend_Registry::get('Zend_Translate')->translate('Please Check your Inbox and come back after you activate your account.'), 'off');
                 // once per day
                 $ProfilesMeta->metaUpdate('resend_activation_lock', $hour_lock, $userData->id);
             } else {
                 // show error message
                 Application_Plugin_Alerts::error(Zend_Registry::get('Zend_Translate')->translate('Something went wrong, email was not sent.'), 'off');
             }
             $this->redirect('');
         }
     }
     $request = $this->getRequest();
     if ($request->isPost() && isset($_POST['identifier']) && $_POST['identifier'] == 'ActivateAccount') {
         if ($activateaccount_form->isValid($_POST)) {
             if ($Profiles->activateAccount($key)) {
                 // auto-login user and store identity
                 $authAdapter = Application_Plugin_Common::getAuthAdapter();
                 $authAdapter->setIdentity($userData->email)->setCredential('whatever')->setCredentialTreatment('autologin');
                 $auth = Zend_Auth::getInstance();
                 $auth->authenticate($authAdapter);
                 $identity = $authAdapter->getResultRowObject();
                 $authStorage = $auth->getStorage();
                 $authStorage->write($identity);
                 // update last login date
                 $ProfilesMeta = new Application_Model_ProfilesMeta();
                 $ProfilesMeta->metaUpdate('last_login', Application_Plugin_Common::now(), $identity->id);
                 // show welcome message
                 Application_Plugin_Alerts::success($this->view->translate('Welcome to the network.'), 'on');
                 $this->redirect('');
             }
         }
     }
 }