Completed
Pull Request — master (#167)
by Corey
06:12 queued 03:00
created

SiteController::actionPrivacy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 1
nc 1
nop 0
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();
0 ignored issues
show
Bug introduced by
The method logout() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
    Yii::$app->user->/** @scrutinizer ignore-call */ 
107
                     logout();

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.

Loading history...
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.');
0 ignored issues
show
Bug introduced by
The method setFlash() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

115
        Yii::$app->session->/** @scrutinizer ignore-call */ 
116
                            setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');

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.

Loading history...
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");
0 ignored issues
show
Bug introduced by
Accessing email on the interface yii\web\IdentityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
231
232
    $reader = Yii::$app->user->identity->getExportData();
0 ignored issues
show
Bug introduced by
The method getExportData() does not exist on yii\web\IdentityInterface. It seems like you code against a sub-type of said class. However, the method does not exist in site\tests\_support\MockUser. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

232
    /** @scrutinizer ignore-call */ 
233
    $reader = Yii::$app->user->identity->getExportData();
Loading history...
Bug introduced by
The method getExportData() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

232
    /** @scrutinizer ignore-call */ 
233
    $reader = Yii::$app->user->identity->getExportData();

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.

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fputcsv() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

244
    fputcsv(/** @scrutinizer ignore-type */ $fp, $header);
Loading history...
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);
0 ignored issues
show
Bug introduced by
The method cleanExportData() does not exist on yii\web\IdentityInterface. It seems like you code against a sub-type of said class. However, the method does not exist in site\tests\_support\MockUser. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

248
      /** @scrutinizer ignore-call */ 
249
      $row = Yii::$app->user->identity->cleanExportData($row);
Loading history...
249
      fputcsv($fp, $row[0]);
250
    }
251
    fclose($fp);
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

251
    fclose(/** @scrutinizer ignore-type */ $fp);
Loading history...
252
253
    die;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
254
  }
255
}
256