Issues (441)

site/controllers/SiteController.php (2 issues)

1
<?php
2
3
namespace site\controllers;
4
5
use Yii;
6
use yii\web\BadRequestHttpException;
7
use common\components\Controller;
8
use yii\filters\VerbFilter;
9
use common\components\AccessControl;
10
11
/**
12
 * Site controller
13
 */
14
class SiteController extends Controller
15
{
16
  /**
17
   * @inheritdoc
18
   */
19
  public function behaviors()
20
  {
21
    return [
22
      'access' => [
23
        'class' => AccessControl::class,
24
        'rules' => [
25
          [
26
            'actions' => ['index', 'blog', 'error', 'privacy', 'terms', 'about', 'captcha', 'contact', 'faq'],
27
            'allow' => true,
28
          ],
29
          [
30
            'actions' => ['login', 'signup', 'reset-password', 'request-password-reset', 'verify-email'],
31
            'allow' => true,
32
            'roles' => ['?'],
33
          ],
34
          [
35
            'actions' => ['logout', 'welcome'],
36
            'allow' => true,
37
            'roles' => ['@'],
38
          ],
39
        ],
40
      ],
41
      'verbs' => [
42
        'class' => VerbFilter::class,
43
        'actions' => [
44
          'logout' => ['post'],
45
        ],
46
      ],
47
    ];
48
  }
49
50
  public function actionIndex()
51
  {
52
    return $this->render('index');
53
  }
54
55
  public function actionBlog()
56
  {
57
    $time = Yii::$container->get(\common\interfaces\TimeInterface::class); 
58
    $key = "index_blog_".$time->getLocalDate('UTC');
59
    $posts = Yii::$app->cache->get($key);
60
    if($posts === false) {
61
      $posts = \Yii::$app->getModule('blog')
62
                            ->fetch()
63
                            ->parse()
64
                            ->results;
65
      Yii::$app->cache->set($key, $posts, 60*60*24);
66
    }
67
68
    return $this->render('blog', ['posts'=>$posts]);
69
  }
70
71
  public function actionLogin()
72
  {
73
    $model = Yii::$container->get(\common\models\LoginForm::class);
74
    if ($model->load(Yii::$app->request->post()) && $model->login()) {
75
      return $this->goBack();
76
    } else {
77
      return $this->render('login', [
78
        'model' => $model,
79
      ]);
80
    }
81
  }
82
83
  public function actionLogout()
84
  {
85
    Yii::$app->user->logout();
0 ignored issues
show
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

85
    Yii::$app->user->/** @scrutinizer ignore-call */ 
86
                     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...
86
    return $this->goHome();
87
  }
88
89
  public function actionContact()
90
  {
91
    $model = new \site\models\ContactForm();
92
    if ($model->load(Yii::$app->request->post()) && $model->validate()) {
93
      if($model->sendEmail(Yii::$app->params['adminEmail'])) {
94
        Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
0 ignored issues
show
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

94
        Yii::$app->session->/** @scrutinizer ignore-call */ 
95
                            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...
95
      } else {
96
        Yii::$app->session->setFlash('error', 'There was an error sending email.');
97
      }
98
      return $this->refresh();
99
    } else {
100
      return $this->render('contact', [
101
        'model' => $model,
102
      ]);
103
    }
104
  }
105
106
  public function actionAbout()
107
  {
108
    return $this->render('about');
109
  }
110
111
  public function actionFaq()
112
  {
113
    return $this->render('faq');
114
  }
115
116
  public function actionWelcome()
117
  {
118
    return $this->render('welcome');
119
  }
120
121
  public function actionSignup()
122
  {
123
    $model = Yii::$container->get(\site\models\SignupForm::class);
124
    if($model->load(Yii::$app->request->post()) && $model->validate()) {
125
      $model->signup();
126
      Yii::$app->session->setFlash('success', "We have sent a verification email to the email address you provided.<br /><br />Please check your inbox and follow the instructions to verify your account. If the email does not arrive please check your spam folder.");
127
      return $this->redirect('/',302);
128
    }
129
130
    return $this->render('signup', [
131
      'model' => $model,
132
    ]);
133
  }
134
135
  public function actionRequestPasswordReset()
136
  {
137
    $model = Yii::$container->get(\site\models\PasswordResetRequestForm::class);
138
    if($model->load(Yii::$app->request->post()) && $model->validate()) {
139
      if(!$model->sendEmail()) {
140
        $ip = Yii::$app->getRequest()->getUserIP() ?: "UNKNOWN";
141
        Yii::warning("$ip has tried to reset the password for ".$model->email);
142
      }
143
144
      Yii::$app->session->setFlash('success', 'If there is an account with the submitted email address you will receive further instructions in your email inbox.<br /><br />If the email does not arrive please check your spam folder.');
145
      return $this->goHome();
146
    }
147
148
    return $this->render('requestPasswordResetToken', [
149
      'model' => $model,
150
    ]);
151
  }
152
153
  public function actionResetPassword($token)
154
  {
155
    try {
156
      $model = Yii::$container->get(\site\models\ResetPasswordForm::class, [$token]);
157
    } catch (\yii\base\InvalidParamException $e) {
158
      throw new BadRequestHttpException($e->getMessage());
159
    }
160
161
    if ($model->load(Yii::$app->request->post())
162
        && $model->validate()
163
        && $model->resetPassword()) {
164
      Yii::$app->session->setFlash('success', 'New password was saved.');
165
      return $this->goHome();
166
    }
167
168
    return $this->render('resetPassword', [
169
      'model' => $model,
170
    ]);
171
  }
172
173
  public function actionVerifyEmail($token)
174
  {
175
    if (empty($token) || !is_string($token)) {
176
      throw new BadRequestHttpException('Email verification token cannot be blank.');
177
    }
178
179
    $user = Yii::$container->get(\common\interfaces\UserInterface::class)
180
              ->findByVerifyEmailToken($token);
181
    if (!$user) {
182
      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.");
183
    }
184
185
    if($user->isTokenConfirmed($user->verify_email_token)) {
186
      Yii::$app->session->setFlash('success', 'Your account has already been verified. Please log in.');
187
      return $this->redirect('/login',302);
188
    } else if (Yii::$app->getUser()->login($user)) {
189
      $user->confirmVerifyEmailToken();
190
      $user->save();
191
      Yii::$app->session->setFlash('success', 'Your account has been verified. Please continue with your check-in.');
192
      return $this->redirect('/welcome',302);
193
    }
194
  }
195
196
  public function actionPrivacy()
197
  {
198
    return $this->render('privacy');
199
  }
200
201
  public function actionTerms()
202
  {
203
    return $this->render('terms');
204
  }
205
}
206