Completed
Pull Request — master (#170)
by Corey
03:03
created

SiteController::actionExport()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 27
c 0
b 0
f 0
rs 9.6333
cc 2
nc 2
nop 0
1
<?php
2
3
namespace site\controllers;
4
5
use Yii;
6
use yii\web\BadRequestHttpException;
7
use yii\web\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
  /**
51
   * @inheritdoc
52
   */
53
  public function actions()
54
  {
55
    return [
56
      'error' => [
57
        'class' => 'yii\web\ErrorAction',
58
      ],
59
      'captcha' => [
60
        'class' => 'yii\captcha\CaptchaAction',
61
      ],
62
    ];
63
  }
64
65
  public function actionIndex()
66
  {
67
    return $this->render('index');
68
  }
69
70
  public function actionBlog()
71
  {
72
    $time = Yii::$container->get(\common\interfaces\TimeInterface::class); 
73
    $key = "index_blog_".$time->getLocalDate('UTC');
74
    $posts = Yii::$app->cache->get($key);
75
    if($posts === false) {
76
      $posts = \Yii::$app->getModule('blog')
77
                            ->fetch()
78
                            ->parse()
79
                            ->results;
80
    }
81
    Yii::$app->cache->set($key, $posts, 60*60*24);
82
83
    return $this->render('blog', ['posts'=>$posts]);
84
  }
85
86
  public function actionLogin()
87
  {
88
    $model = Yii::$container->get(\common\models\LoginForm::class);
89
    if ($model->load(Yii::$app->request->post()) && $model->login()) {
90
      return $this->goBack();
91
    } else {
92
      return $this->render('login', [
93
        'model' => $model,
94
      ]);
95
    }
96
  }
97
98
  public function actionLogout()
99
  {
100
    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

100
    Yii::$app->user->/** @scrutinizer ignore-call */ 
101
                     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...
101
    return $this->goHome();
102
  }
103
104
  public function actionContact()
105
  {
106
    $model = new \site\models\ContactForm();
107
    if ($model->load(Yii::$app->request->post()) && $model->validate()) {
108
      if($model->sendEmail(Yii::$app->params['adminEmail'])) {
109
        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

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