SiteController.php 3.76 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
	public function behaviors()
	{
17 18
		return [
			'access' => [
19
				'class' => \yii\web\AccessControl::className(),
20 21 22 23
				'only' => ['login', 'logout', 'signup'],
				'rules' => [
					[
						'actions' => ['login', 'signup'],
24
						'allow' => true,
25 26 27 28
						'roles' => ['?'],
					],
					[
						'actions' => ['logout'],
29
						'allow' => true,
30 31 32 33 34
						'roles' => ['@'],
					],
				],
			],
		];
35 36
	}

37 38
	public function actions()
	{
39 40
		return [
			'error' => [
41
				'class' => 'yii\web\ErrorAction',
42 43
			],
			'captcha' => [
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', [
62
				'model' => $model,
63
			]);
64 65 66 67 68
		}
	}

	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', [
81
				'model' => $model,
82
			]);
83 84 85 86 87
		}
	}

	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
		return $this->render('signup', [
102
			'model' => $model,
103
		]);
104
	}
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', [
119
			'model' => $model,
120
		]);
121
	}
122

123 124
	public function actionResetPassword($token)
	{
125
		$model = User::find([
126 127
			'password_reset_token' => $token,
			'status' => User::STATUS_ACTIVE,
128
		]);
129 130 131

		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', [
141
			'model' => $model,
142
		]);
143 144 145 146
	}

	private function sendPasswordResetEmail($email)
	{
147
		$user = User::find([
148 149
			'status' => User::STATUS_ACTIVE,
			'email' => $email,
150
		]);
151 152 153 154 155 156 157 158 159 160

		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) . '?=';
161
			$body = $this->renderPartial('/emails/passwordResetToken', [
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
}