Esempio n. 1
0
 public function changePasswordAction()
 {
     $emailer = new \Zf2mail\Controller\EmailersController();
     $id = (int) $this->getCurrentUser()->id;
     $users = $this->getUsersTable()->getUsers($id);
     $form = new ChangePasswordForm();
     $form->setValidationGroup('id');
     $form->bind($users);
     $request = $this->getRequest();
     if ($request->isPost()) {
         $users = new Users();
         $form->setInputFilter($users->getInputFilter());
         $formData = $request->getPost();
         $valid = true;
         /**
          * Check whether userid is exist
          */
         $usersobj = $this->getUsersTable()->getUsers($formData['id']);
         if (empty($usersobj) || $formData['id'] != $id) {
             $valid = false;
             $this->error[0] = array('error' => $this->message->unauthorizedUser);
         }
         /**
          * Check whether password is valid
          */
         if (MD5($formData['cpassword']) != $usersobj->password) {
             $valid = false;
             $this->error[0] = array('error' => $this->message->unauthorizedPassword);
         }
         if ($formData['password'] != $formData['repassword']) {
             $valid = false;
             $this->error[0] = array('error' => $this->message->passwordNotMatch);
         }
         if (strlen($formData['password']) < 6) {
             $valid = false;
             $this->error[0] = array('error' => $this->message->smallPassword);
         }
         if (empty($usersobj->email)) {
             $valid = false;
             $this->error[0] = array('error' => $this->message->emptyUsername);
         }
         $form->setData($formData);
         $form->setValidationGroup('id', 'password');
         if ($form->isValid()) {
             if ($valid) {
                 $body = "Hi " . trim($usersobj->name) . ",<br><br>Your password has been changed. <br /><br>\r\n\r\nGo here: <a href='" . $emailer->siteURL() . "'>" . $emailer->siteURL() . "</a><br />\r\n\r\nIf you have any questions, please do not hesitate to contact us at <a href=\"mailto:support@yourdomain.com\">support@yourdomain.com</a>.";
                 $mailData = array('to' => $usersobj->email, 'subject' => "Password Changed successfully!", 'html_message' => $body);
                 $mail = $emailer->sendMail($mailData);
                 $users->exchangeArray($formData);
                 $this->getUsersTable()->changePassword($users);
                 $this->flashMessenger()->addMessage(array('success' => $this->message->changePassword));
                 if ($mail) {
                 } else {
                     $this->error[0] = array('error' => 'Can not sent email. Try again.');
                 }
                 return $this->redirect()->toRoute('users/change-password');
             }
         }
         // $this->error[0] = array('error' => 'Invalid Information');
     }
     $vm = new ViewModel(array('flashMessages' => $this->flashMessenger()->getMessages(), 'form' => $form, 'error' => $this->error));
     return $vm;
 }