SiteController.php 3.86 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

class SiteController extends Controller
{
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
	public function behaviors()
	{
		return array(
			'access' => array(
				'class' => \yii\web\AccessControl::className(),
				'only' => array('login', 'logout', 'signup'),
				'rules' => array(
					array(
						'actions' => array('login', 'signup'),
						'allow' => true,
						'roles' => array('?'),
					),
					array(
						'actions' => array('logout'),
						'allow' => true,
						'roles' => array('@'),
					),
				),
			),
		);
	}

37 38 39
	public function actions()
	{
		return array(
40 41 42
			'error' => array(
				'class' => 'yii\web\ErrorAction',
			),
43
			'captcha' => array(
Qiang Xue committed
44
				'class' => 'yii\captcha\CaptchaAction',
45
				'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
46 47 48 49 50 51
			),
		);
	}

	public function actionIndex()
	{
52
		return $this->render('index');
53 54 55 56 57
	}

	public function actionLogin()
	{
		$model = new LoginForm();
58
		if ($model->load($_POST) && $model->login()) {
Qiang Xue committed
59
			return $this->goHome();
60
		} else {
61
			return $this->render('login', array(
62 63 64 65 66 67 68
				'model' => $model,
			));
		}
	}

	public function actionLogout()
	{
69
		Yii::$app->user->logout();
Qiang Xue committed
70
		return $this->goHome();
71 72 73 74 75
	}

	public function actionContact()
	{
		$model = new ContactForm;
76
		if ($model->load($_POST) && $model->contact(Yii::$app->params['adminEmail'])) {
77
			Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
78
			return $this->refresh();
79
		} else {
80
			return $this->render('contact', array(
81 82 83 84 85 86 87
				'model' => $model,
			));
		}
	}

	public function actionAbout()
	{
88
		return $this->render('about');
89
	}
90 91 92 93 94 95 96

	public function actionSignup()
	{
		$model = new User();
		$model->setScenario('signup');
		if ($model->load($_POST) && $model->save()) {
			if (Yii::$app->getUser()->login($model)) {
Qiang Xue committed
97
				return $this->goHome();
98 99 100 101 102 103 104
			}
		}

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

106
	public function actionRequestPasswordReset()
107
	{
108 109 110
		$model = new User();
		$model->scenario = 'requestPasswordResetToken';
		if ($model->load($_POST) && $model->validate()) {
111
			if ($this->sendPasswordResetEmail($model->email)) {
112
				Yii::$app->getSession()->setFlash('success', 'Check your email for further instructions.');
Qiang Xue committed
113
				return $this->goHome();
114 115
			} else {
				Yii::$app->getSession()->setFlash('error', 'There was an error sending email.');
116
			}
117
		}
118
		return $this->render('requestPasswordResetToken', array(
119 120 121
			'model' => $model,
		));
	}
122

123 124 125 126 127 128 129 130 131
	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.');
132
		}
133 134 135 136

		$model->scenario = 'resetPassword';
		if ($model->load($_POST) && $model->save()) {
			Yii::$app->getSession()->setFlash('success', 'New password was saved.');
Qiang Xue committed
137
			return $this->goHome();
138 139
		}

140
		return $this->render('resetPassword', array(
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
			'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(
162
				'user' => $user,
163
			));
164 165 166
			$headers = "From: $name <{$fromEmail}>\r\n" .
				"MIME-Version: 1.0\r\n" .
				"Content-type: text/plain; charset=UTF-8";
gsd committed
167
			return mail($email, $subject, $body, $headers);
168
		}
169 170

		return false;
171
	}
172
}