1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace site\controllers; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use common\models\LoginForm; |
7
|
|
|
use common\models\Question; |
8
|
|
|
use site\models\PasswordResetRequestForm; |
9
|
|
|
use site\models\ResetPasswordForm; |
10
|
|
|
use site\models\ContactForm; |
11
|
|
|
use yii\base\InvalidParamException; |
12
|
|
|
use yii\web\BadRequestHttpException; |
13
|
|
|
use yii\web\Controller; |
14
|
|
|
use yii\filters\VerbFilter; |
15
|
|
|
use common\components\AccessControl; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Site controller |
19
|
|
|
*/ |
20
|
|
|
class SiteController extends Controller |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @inheritdoc |
24
|
|
|
*/ |
25
|
|
|
public function behaviors() |
26
|
|
|
{ |
27
|
|
|
return [ |
28
|
|
|
'access' => [ |
29
|
|
|
'class' => AccessControl::class, |
30
|
|
|
'rules' => [ |
31
|
|
|
[ |
32
|
|
|
'actions' => ['index', 'blog', 'error', 'privacy', 'terms', 'about', 'captcha', 'contact', 'faq'], |
33
|
|
|
'allow' => true, |
34
|
|
|
], |
35
|
|
|
[ |
36
|
|
|
'actions' => ['login', 'signup', 'reset-password', 'request-password-reset', 'verify-email'], |
37
|
|
|
'allow' => true, |
38
|
|
|
'roles' => ['?'], |
39
|
|
|
], |
40
|
|
|
[ |
41
|
|
|
'actions' => ['logout', 'welcome'], |
42
|
|
|
'allow' => true, |
43
|
|
|
'roles' => ['@'], |
44
|
|
|
], |
45
|
|
|
], |
46
|
|
|
], |
47
|
|
|
'verbs' => [ |
48
|
|
|
'class' => VerbFilter::class, |
49
|
|
|
'actions' => [ |
50
|
|
|
'logout' => ['post'], |
51
|
|
|
], |
52
|
|
|
], |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritdoc |
58
|
|
|
*/ |
59
|
|
|
public function actions() |
60
|
|
|
{ |
61
|
|
|
return [ |
62
|
|
|
'error' => [ |
63
|
|
|
'class' => 'yii\web\ErrorAction', |
64
|
|
|
], |
65
|
|
|
'captcha' => [ |
66
|
|
|
'class' => 'yii\captcha\CaptchaAction', |
67
|
|
|
], |
68
|
|
|
]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function actionIndex() |
72
|
|
|
{ |
73
|
|
|
return $this->render('index'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function actionBlog() |
77
|
|
|
{ |
78
|
|
|
$time = Yii::$container->get(\common\interfaces\TimeInterface::class); |
79
|
|
|
$key = "index_blog_".$time->getLocalDate('UTC'); |
80
|
|
|
$posts = Yii::$app->cache->get($key); |
81
|
|
|
if($posts === false) { |
82
|
|
|
$posts = \Yii::$app->getModule('blog') |
83
|
|
|
->fetch() |
84
|
|
|
->parse() |
85
|
|
|
->results; |
86
|
|
|
} |
87
|
|
|
Yii::$app->cache->set($key, $posts, 60*60*24); |
88
|
|
|
|
89
|
|
|
return $this->render('blog', ['posts'=>$posts]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function actionLogin() |
93
|
|
|
{ |
94
|
|
|
$model = Yii::$container->get(\common\models\LoginForm::class); |
95
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->login()) { |
96
|
|
|
return $this->goBack(); |
97
|
|
|
} else { |
98
|
|
|
return $this->render('login', [ |
99
|
|
|
'model' => $model, |
100
|
|
|
]); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function actionLogout() |
105
|
|
|
{ |
106
|
|
|
Yii::$app->user->logout(); |
|
|
|
|
107
|
|
|
return $this->goHome(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function actionContact() |
111
|
|
|
{ |
112
|
|
|
$model = new ContactForm(); |
113
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->validate()) { |
114
|
|
|
if($model->sendEmail(Yii::$app->params['adminEmail'])) { |
115
|
|
|
Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.'); |
|
|
|
|
116
|
|
|
} else { |
117
|
|
|
Yii::$app->session->setFlash('error', 'There was an error sending email.'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this->refresh(); |
121
|
|
|
} else { |
122
|
|
|
return $this->render('contact', [ |
123
|
|
|
'model' => $model, |
124
|
|
|
]); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function actionAbout() |
129
|
|
|
{ |
130
|
|
|
return $this->render('about'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function actionFaq() |
134
|
|
|
{ |
135
|
|
|
return $this->render('faq'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function actionWelcome() |
139
|
|
|
{ |
140
|
|
|
return $this->render('welcome'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function actionSignup() |
144
|
|
|
{ |
145
|
|
|
$model = Yii::$container->get(\site\models\SignupForm::class); |
146
|
|
|
if($model->load(Yii::$app->request->post()) && $model->validate()) { |
147
|
|
|
$model->signup(); |
148
|
|
|
Yii::$app->getSession()->setFlash('success', 'We have sent a verification email to the email address you provided. Please check your inbox and follow the instructions to verify your account.'); |
149
|
|
|
return $this->redirect('/',302); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $this->render('signup', [ |
153
|
|
|
'model' => $model, |
154
|
|
|
]); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function actionRequestPasswordReset() |
158
|
|
|
{ |
159
|
|
|
$model = Yii::$container->get(\site\models\PasswordResetRequestForm::class); |
160
|
|
|
if($model->load(Yii::$app->request->post()) && $model->validate()) { |
161
|
|
|
if(!$model->sendEmail()) { |
162
|
|
|
$ip = Yii::$app->getRequest()->getUserIP() ?: "UNKNOWN"; |
163
|
|
|
Yii::warning("$ip has tried to reset the password for ".$model->email); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
Yii::$app->getSession()->setFlash('success', 'If there is an account with the submitted email address you will receive further instructions in your email inbox.'); |
167
|
|
|
return $this->goHome(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $this->render('requestPasswordResetToken', [ |
171
|
|
|
'model' => $model, |
172
|
|
|
]); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function actionResetPassword($token) |
176
|
|
|
{ |
177
|
|
|
try { |
178
|
|
|
$model = Yii::$container->get(\site\models\ResetPasswordForm::class, [$token]); |
179
|
|
|
} catch (InvalidParamException $e) { |
180
|
|
|
throw new BadRequestHttpException($e->getMessage()); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
if ($model->load(Yii::$app->request->post()) |
184
|
|
|
&& $model->validate() |
185
|
|
|
&& $model->resetPassword()) { |
186
|
|
|
Yii::$app->getSession()->setFlash('success', 'New password was saved.'); |
187
|
|
|
return $this->goHome(); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $this->render('resetPassword', [ |
191
|
|
|
'model' => $model, |
192
|
|
|
]); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function actionVerifyEmail($token) |
196
|
|
|
{ |
197
|
|
|
if (empty($token) || !is_string($token)) { |
198
|
|
|
throw new BadRequestHttpException('Email verification token cannot be blank.'); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$user = Yii::$container->get(\common\interfaces\UserInterface::class)->findByVerifyEmailToken($token); |
202
|
|
|
if (!$user) { |
203
|
|
|
throw new BadRequestHttpException("Wrong or expired email verification token. If you aren't sure why this error occurs perhaps you've already verified your account. Please try logging in."); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
if($user->isTokenConfirmed($user->verify_email_token)) { |
207
|
|
|
Yii::$app->getSession()->setFlash('success', 'Your account has already been verified. Please log in.'); |
208
|
|
|
return $this->redirect('/login',302); |
209
|
|
|
} else if (Yii::$app->getUser()->login($user)) { |
210
|
|
|
$user->confirmVerifyEmailToken(); |
211
|
|
|
$user->save(); |
212
|
|
|
Yii::$app->getSession()->setFlash('success', 'Your account has been verified. Please continue with your check-in.'); |
213
|
|
|
return $this->redirect('/welcome',302); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function actionPrivacy() |
218
|
|
|
{ |
219
|
|
|
return $this->render('privacy'); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function actionTerms() |
223
|
|
|
{ |
224
|
|
|
return $this->render('terms'); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function actionExport() |
228
|
|
|
{ |
229
|
|
|
header("Content-Type: text/csv"); |
230
|
|
|
header("Content-Disposition: attachment; filename=fsa-data-export-".Yii::$app->user->identity->email."-".date('Ymd').".csv"); |
|
|
|
|
231
|
|
|
|
232
|
|
|
$reader = Yii::$app->user->identity->getExportData(); |
|
|
|
|
233
|
|
|
$fp = fopen('php://output', 'w'); |
234
|
|
|
|
235
|
|
|
$header = [ |
236
|
|
|
'Date', |
237
|
|
|
'Behavior', |
238
|
|
|
'Category', |
239
|
|
|
Question::$QUESTIONS[1], |
240
|
|
|
Question::$QUESTIONS[2], |
241
|
|
|
Question::$QUESTIONS[3], |
242
|
|
|
]; |
243
|
|
|
|
244
|
|
|
fputcsv($fp, $header); |
|
|
|
|
245
|
|
|
$user_behavior = Yii::$container->get(\common\interfaces\UserBehaviorInterface::class); |
246
|
|
|
while($row = $reader->read()) { |
247
|
|
|
$row = $user_behavior::decorateWithCategory([$row]); |
248
|
|
|
$row = Yii::$app->user->identity->cleanExportData($row); |
|
|
|
|
249
|
|
|
fputcsv($fp, $row[0]); |
250
|
|
|
} |
251
|
|
|
fclose($fp); |
|
|
|
|
252
|
|
|
|
253
|
|
|
die; |
|
|
|
|
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.