/
pashko
/
siec
Обзор
Документация
Войти
/
pashko
/
siec
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
frontend/controllers/SiteController.php
467 строк
14 KB
Pavel Kokorin
privacy
11 ноя 2025, 06:56
11 ноя 2025, 06:56
bc3a066
Код
Авторство
О чём код?
<?php namespace frontend\controllers; use common\models\Article; use common\models\Blog; use common\models\Feedback; use common\models\Pages; use common\models\Service; use common\models\Vacancy; use frontend\components\Helpers; use frontend\components\SendToBitrix; use Yii; use yii\base\InvalidArgumentException; use yii\helpers\ArrayHelper; use yii\helpers\Url; use yii\web\BadRequestHttpException; use yii\web\Controller; use yii\filters\VerbFilter; use yii\filters\AccessControl; use yii\web\NotFoundHttpException; /** * Site controller */ class SiteController extends Controller { /** * @inheritdoc */ public function beforeAction($action) { $this->enableCsrfValidation = false;//отключаем проверку Csrf return parent::beforeAction($action); } /** * {@inheritdoc} */ public function behaviors() { return [ 'access' => [ 'class' => AccessControl::className(), 'only' => ['logout', 'signup'], 'rules' => [ [ 'actions' => ['signup'], 'allow' => true, 'roles' => ['?'], ], [ 'actions' => ['logout'], 'allow' => true, 'roles' => ['@'], ], ], ], 'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ 'logout' => ['post'], ], ], ]; } /** * {@inheritdoc} */ public function actions() { return [ 'error' => [ 'class' => 'yii\web\ErrorAction', ], 'captcha' => [ 'class' => 'yii\captcha\CaptchaAction', 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, ], ]; } /** * Displays homepage. * * @return mixed */ public function actionIndex() { return $this->render('index', [ 'model' => Pages::findOne(['code' => 'index']) ]); } /** * Displays about page. * * @return mixed */ public function actionAbout() { return $this->render('about'); } /** * Signs user up. * * @return mixed */ public function actionSignup() { $model = new SignupForm(); if ($model->load(Yii::$app->request->post()) && $model->signup()) { Yii::$app->session->setFlash('success', 'Thank you for registration. Please check your inbox for verification email.'); return $this->goHome(); } return $this->render('signup', [ 'model' => $model, ]); } /** * Requests password reset. * * @return mixed */ public function actionRequestPasswordReset() { $model = new PasswordResetRequestForm(); if ($model->load(Yii::$app->request->post()) && $model->validate()) { if ($model->sendEmail()) { Yii::$app->session->setFlash('success', 'Check your email for further instructions.'); return $this->goHome(); } else { Yii::$app->session->setFlash('error', 'Sorry, we are unable to reset password for the provided email address.'); } } return $this->render('requestPasswordResetToken', [ 'model' => $model, ]); } /** * Resets password. * * @param string $token * @return mixed * @throws BadRequestHttpException */ public function actionResetPassword($token) { try { $model = new ResetPasswordForm($token); } catch (InvalidArgumentException $e) { throw new BadRequestHttpException($e->getMessage()); } if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) { Yii::$app->session->setFlash('success', 'New password saved.'); return $this->goHome(); } return $this->render('resetPassword', [ 'model' => $model, ]); } /** * Verify email address * * @param string $token * @throws BadRequestHttpException * @return yii\web\Response */ public function actionVerifyEmail($token) { try { $model = new VerifyEmailForm($token); } catch (InvalidArgumentException $e) { throw new BadRequestHttpException($e->getMessage()); } if ($user = $model->verifyEmail()) { if (Yii::$app->user->login($user)) { Yii::$app->session->setFlash('success', 'Your email has been confirmed!'); return $this->goHome(); } } Yii::$app->session->setFlash('error', 'Sorry, we are unable to verify your account with provided token.'); return $this->goHome(); } /** * Resend verification email * * @return mixed */ public function actionResendVerificationEmail() { $model = new ResendVerificationEmailForm(); if ($model->load(Yii::$app->request->post()) && $model->validate()) { if ($model->sendEmail()) { Yii::$app->session->setFlash('success', 'Check your email for further instructions.'); return $this->goHome(); } Yii::$app->session->setFlash('error', 'Sorry, we are unable to resend verification email for the provided email address.'); } return $this->render('resendVerificationEmail', [ 'model' => $model ]); } public function actionNews() { return $this->render('news'); } public function actionTestData() { Yii::$app->response->format = \yii\web\Response::FORMAT_RAW; Yii::$app->response->headers->add('Content-Type', 'application/json'); $data = [ 'a' => 1, 'b' => 2, 'hello' => 'world' ]; return json_encode($data); } /** * @return \yii\web\Response */ public function actionFeedback() { if (!$data = yii::$app->request->post()) { return $this->redirect('/thanks'); } /*Yii::$app->mailer->compose([ 'html' => 'feedback-html', 'text' => 'feedback-text' ], [ 'data' => $post, ]) ->setTo([ 'test@siec.ru' ]) ->setFrom(['no-reply@siec.ru' => 'Автоматическое уведомление']) ->setSubject('Заявка с сайта siec.ru') ->send();*/ $name = ArrayHelper::getValue($data, 'name'); $phone = ArrayHelper::getValue($data, 'phone'); $contact = ArrayHelper::getValue($data, 'contact'); $contactDop = ArrayHelper::getValue($data, 'contact_dop'); $from = ArrayHelper::getValue($data, 'from'); $to = ArrayHelper::getValue($data, 'to'); $weight = ArrayHelper::getValue($data, 'weight'); $volume = ArrayHelper::getValue($data, 'volume'); $email = ArrayHelper::getValue($data, 'email'); $mess = ArrayHelper::getValue($data, 'message'); $message = "Здравствуйте. На вашем сайте siec.ru была оставлена заявка:\n\n"; $message .= $name ? 'Имя: ' . $name . "\n": ''; $message .= $phone ? 'Телефон: ' . $phone . "\n": ''; $message .= $contact ? 'Контакт: ' . $contact . "\n": ''; $message .= $contactDop ? 'Доп контакт: ' . $contactDop . "\n": ''; $message .= $from ? 'Откуда забрать: ' . $from . "\n": ''; $message .= $to ? 'Куда доставить: ' . $to . "\n": ''; $message .= $weight ? 'Вес: ' . $weight . "\n": ''; $message .= $volume ? 'Объем: ' . $volume . "\n": ''; $message .= $email ? 'Эл. почта: ' . $email . "\n": ''; $message .= $mess ? 'Сообщение: ' . $mess . "\n": ''; $bitrixData = [ 'name' => $name, 'status_id' => 'NEW', 'opened' => 'Y', 'assigned_by_id' => 18, 'source_id' => 5, 'source_description' => 'source_description', 'comments' => '', 'phone' => [ ['VALUE' => $phone, 'VALUE_TYPE' => 'WORK'] ], 'email' => [ ['VALUE' => $email, 'VALUE_TYPE' => 'WORK'], ], 'from' => $from, // Пункт отправления (если есть) 'to' => $to, // Пункт назначения (если есть) 'weight' => $weight, // Вес (строка) (если есть) 'volume' => $volume, // Объем (строка) (если есть) ]; (new SendToBitrix())->send($bitrixData); $to = 'info@siec.ru'; $subject = mb_encode_mimeheader('Заявка с сайта SIEC'); $headers = 'From: no-reply@siec.ru' . "\r\n" . 'Reply-To: no-reply@siec.ru' . "\r\n" . 'Content-Type: text/plain; charset=utf-8' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); $this->saveFeedback($data); if (YII_ENV_PROD && !mail($to, $subject, $message, $headers)) { var_dump('no email send');exit; } return $this->redirect('/thanks'); } /** * @param $data */ public function saveFeedback($data) { $dop = []; $model = new Feedback([ 'ip' => Yii::$app->request->userIP, 'useragent' => Yii::$app->request->userAgent, ]); if (!empty($data['name'])) $model->name = $data['name']; if (!empty($data['phone'])) $model->phone = $data['phone']; if (!empty($data['email'])) $model->email = $data['email']; if (!empty($data['from'])) $model->from = $data['from']; if (!empty($data['to'])) $model->to = $data['to']; if (!empty($data['message'])) $model->message = $data['message']; if (!empty($data['weight'])) $model->weight = $data['weight']; if (!empty($data['volume'])) $model->volume = $data['volume']; if (!empty($data['contact'])) $dop[] = $data['contact']; if (!empty($data['contact_dop'])) $dop[] = $data['contact_dop']; if (!empty($dop)) $model->data = json_encode($dop); if ($model->save()) return true; return false; } public function actionThanks() { return $this->render('thanks', [ 'model' => Pages::findOne(['code' => 'thanks']) ]); } public function actionTest() { $to = 'test@siec.ru'; $subject = 'the subject'; $message = 'hello'; $headers = 'From: test@siec.ru' . "\r\n" . 'Reply-To: test@siec.ru' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); } public function actionSitemap() { if($get = yii::$app->request->get() and $get['hash'] == '9d3bb895f22bf0afa958d68c2a58ded7') { $file = 'sitemap.xml'; // создаем файл если не создан if($fp1 = fopen($file, 'c')) { fclose($fp1); } // Если файл создался больше суток назад то условие выполняется if (time() - filemtime($file) > 1) { $fp = fopen($file, 'w+'); fwrite($fp, $this->siteMapGet()); fclose($fp); exit('success'); } else { exit('not expired'); } } throw new NotFoundHttpException('Страница не найдена'); } protected function siteMapGet() { $blogs = Blog::find()->all(); $services = Service::find()->all(); $vacancy = Vacancy::find()->all(); $return = '<?xml version="1.0" encoding="UTF-8"?>'; $return .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'; $return .= $this->siteMapGetList('blog', $blogs); $return .= $this->siteMapGetList('service', $services); //$return .= $this->siteMapGetList('vacancy', $vacancy); $return .= $this->siteMapGenStatic('contacts'); $return .= $this->siteMapGenStatic('tracking'); $return .= $this->siteMapGenStatic('calculator'); //$return .= $this->siteMapGenStatic('case'); $return .= $this->siteMapGenStatic('service'); $return .= $this->siteMapGenStatic('news'); $return .= $this->siteMapGenStatic('review'); $return .= $this->siteMapGenStatic('about'); $return .= '</urlset>'; return $return; } protected function siteMapGetList($category, $items) { $return = ''; foreach($items as $item): $return .= '<url>'; $return .= '<loc>' . Url::to('/', true) . $category . '/' . $item->slug . '</loc>'; $return .= '<lastmod>' . date('Y-m-d', $item->updated_at) . '</lastmod>'; $return .= '<changefreq>daily</changefreq>'; $return .= '<priority>0.8</priority>'; $return .= '</url>'; endforeach; return $return; } protected function siteMapGenStatic($slug) { $return = '<url>'; $return .= '<loc>' . Url::to('/', true) . $slug . '</loc>'; $return .= '<priority>0.8</priority>'; $return .= '</url>'; return $return; } /** * @return string */ public function actionTurbo() { Yii::$app->response->format = \yii\web\Response::FORMAT_RAW; Yii::$app->response->headers->add('Content-Type', 'text/xml'); $blogs = Blog::find(['active' => 'yes'])->all(); return $this->renderPartial('turbo', [ 'blogs' => $blogs ]); } public function actionPrivacy() { $path = Yii::getAlias('@webroot/doc/politic.pdf'); if (file_exists($path)) { return Yii::$app->response->sendFile($path, 'document.pdf', [ 'inline' => true // открыть в браузере, false - скачать ]); } throw new NotFoundHttpException('Страница не найдена'); } }