/** * Encodes an email message before sending * @param AEmail $email the email being sent * @return string the encoded message */ protected function encodeMessage(AEmail $email) { $encoded = array(); $encoded[] = "--" . $email->uniqueId; $encoded[] = "Content-Type: " . ($email->isHtml ? "text/html" : "text/plain") . "; charset=" . Yii::app()->charset; $encoded[] = "Content-Transfer-Encoding: base64"; $encoded = implode("\r\n", $encoded) . "\r\n\r\n"; $encoded .= chunk_split(base64_encode($email->render())); return $encoded; }
/** * Allows a user to reset their password if they've forgotten it. * The user enters their email address and we send them a link with * a unique key. When they click this link they're presented with * a form to reset their password. After reseting their password successfully * we log them in and redirect them to their account page. * @param integer $id The id of this user * @param string $key The unique key for this user */ public function actionResetPassword($id = null, $key = null) { $usersModule = Yii::app()->getModule("users"); $modelClass = $usersModule->userModelClass; if ($id !== null && $key !== null) { // check if the id + key match for this user $user = $modelClass::model()->findByPk($id); if (!is_object($user)) { Yii::log("Invalid password reset attempt (no such user)", "warning", "user.activity.resetPassword"); throw new CHttpException(500, "Your request is invalid"); } elseif ($user->passwordResetCode != $key) { Yii::log("[{$user->id}] Invalid password reset attempt (invalid code)", "warning", "user.activity.resetPassword"); throw new CHttpException(500, "Your request is invalid"); } // now the user needs to change their password $user->scenario = "newPassword"; if (isset($_POST[$modelClass])) { $user->attributes = $_POST[$modelClass]; if ($user->save()) { Yii::log("[{$user->id}] Password reset via email", "info", "user.activity.resetPassword"); $identityClass = $usersModule->identityClass; $identity = new $identityClass($user->email); $identity->id = $user->id; $identity->name = $user->name; if ($usersModule->autoLoginByDefault) { Yii::app()->user->login($identity, $usersModule->autoLoginDuration); } else { Yii::app()->user->login($identity, 0); } Yii::app()->user->setFlash("success", "<h2>Your password was changed</h2>"); $this->redirect(array("/users/user/account")); } } $user->password = ""; $this->render("newPassword", array("model" => $user)); return; } $model = new $modelClass("resetPassword"); if (isset($_POST[$modelClass])) { $user = $modelClass::model()->findByAttributes(array("email" => $_POST[$modelClass]['email'])); if (is_object($user)) { // send the user a password reset email $email = new AEmail(); $email->recipient = $user->email; $email->viewData = array("user" => $user); $email->view = "/user/emails/resetPassword"; $email->isHtml = true; if ($email->send() || true) { Yii::app()->user->setFlash("info", $this->renderPartial("flashMessages/resetEmailSent", array("user" => $user), true)); $this->redirect(array("/site/index")); } else { $model->addError("email", "There was a problem sending email to this address"); } } else { $model->addError("email", "We couldn't find a user with that email address"); } } if (Yii::app()->request->isAjaxRequest) { $this->renderPartial("resetPassword", array("model" => $model), false, true); } else { $this->render("resetPassword", array("model" => $model)); } }