SiteController.php 3.36 KB
Newer Older
1 2
<?php

3
namespace frontend\controllers;
4 5 6

use Yii;
use yii\web\Controller;
7 8
use common\models\LoginForm;
use frontend\models\ContactForm;
9
use common\models\User;
10
use yii\web\HttpException;
11
use yii\helpers\Security;
12 13 14 15 16 17 18

class SiteController extends Controller
{
	public function actions()
	{
		return array(
			'captcha' => array(
Qiang Xue committed
19
				'class' => 'yii\captcha\CaptchaAction',
20 21 22 23 24 25
			),
		);
	}

	public function actionIndex()
	{
26
		return $this->render('index');
27 28 29 30 31
	}

	public function actionLogin()
	{
		$model = new LoginForm();
32 33
		if ($model->load($_POST) && $model->login()) {
			return $this->redirect(array('site/index'));
34
		} else {
35
			return $this->render('login', array(
36 37 38 39 40 41 42
				'model' => $model,
			));
		}
	}

	public function actionLogout()
	{
43
		Yii::$app->user->logout();
44
		return $this->redirect(array('site/index'));
45 46 47 48 49
	}

	public function actionContact()
	{
		$model = new ContactForm;
50
		if ($model->load($_POST) && $model->contact(Yii::$app->params['adminEmail'])) {
51
			Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
52
			return $this->refresh();
53
		} else {
54
			return $this->render('contact', array(
55 56 57 58 59 60 61
				'model' => $model,
			));
		}
	}

	public function actionAbout()
	{
62
		return $this->render('about');
63
	}
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

	public function actionSignup()
	{
		$model = new User();
		$model->setScenario('signup');
		if ($model->load($_POST) && $model->save()) {
			if (Yii::$app->getUser()->login($model)) {
				$this->redirect('index');
			}
		}

		return $this->render('signup', array(
			'model' => $model,
		));
	}
79

80
	public function actionRequestPasswordReset()
81
	{
82 83 84
		$model = new User();
		$model->scenario = 'requestPasswordResetToken';
		if ($model->load($_POST) && $model->validate()) {
85
			if ($this->sendPasswordResetEmail($model->email)) {
86
				Yii::$app->getSession()->setFlash('success', 'Check your email for further instructions.');
87
				$this->redirect('index');
88 89
			} else {
				Yii::$app->getSession()->setFlash('error', 'There was an error sending email.');
90
			}
91
		}
92
		return $this->render('requestPasswordResetToken', array(
93 94 95
			'model' => $model,
		));
	}
96

97 98 99 100 101 102 103 104 105
	public function actionResetPassword($token)
	{
		$model = User::find(array(
			'password_reset_token' => $token,
			'status' => User::STATUS_ACTIVE,
		));

		if (!$model) {
			throw new HttpException(400, 'Wrong password reset token.');
106
		}
107 108 109 110 111 112 113

		$model->scenario = 'resetPassword';
		if ($model->load($_POST) && $model->save()) {
			Yii::$app->getSession()->setFlash('success', 'New password was saved.');
			$this->redirect('index');
		}

114
		return $this->render('resetPassword', array(
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
			'model' => $model,
		));
	}

	private function sendPasswordResetEmail($email)
	{
		$user = User::find(array(
			'status' => User::STATUS_ACTIVE,
			'email' => $email,
		));

		if (!$user) {
			return false;
		}

		$user->password_reset_token = Security::generateRandomKey();
		if ($user->save(false)) {
			$fromEmail = \Yii::$app->params['supportEmail'];
			$name = '=?UTF-8?B?' . base64_encode(\Yii::$app->name . ' robot') . '?=';
			$subject = '=?UTF-8?B?' . base64_encode('Password reset for ' . \Yii::$app->name) . '?=';
			$body = $this->renderPartial('/emails/passwordResetToken', array(
136
				'user' => $user,
137
			));
138 139 140 141
			$headers = "From: $name <{$fromEmail}>\r\n" .
				"MIME-Version: 1.0\r\n" .
				"Content-type: text/plain; charset=UTF-8";
			return mail($fromEmail, $subject, $body, $headers);
142
		}
143 144

		return false;
145
	}
146
}