| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- <?php
- /**
- * Author: lf
- * Blog: https://blog.feehi.com
- * Email: job@feehi.com
- * Created at: 2017-03-15 21:16
- */
- namespace backend\controllers;
- use Yii;
- use Exception;
- use common\services\ArticleServiceInterface;
- use common\services\UserServiceInterface;
- use common\services\CommentServiceInterface;
- use common\services\FriendlyLinkServiceInterface;
- use common\services\MenuService;
- use backend\models\form\LoginForm;
- use common\libs\ServerInfo;
- use yii\base\UserException;
- use yii\filters\AccessControl;
- use yii\filters\VerbFilter;
- use yii\helpers\Html;
- use yii\web\HttpException;
- use yii\captcha\CaptchaAction;
- /**
- * Site default controller
- *
- * - description:
- * - site/index is index page(framework)
- * - site/main is the iframe default page
- * - site/login backend login page
- * - site/logout backend user logout
- * - site/language multi language change
- * - site/error backend error unify handler
- *
- * Class SiteController
- * @package backend\controllers
- */
- class SiteController extends \yii\web\Controller
- {
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::className(),
- 'except' =>['login', 'captcha', 'language'],//no need login actions
- 'rules' => [
- [
- 'allow' => true,
- 'roles' => ['@'],//"@" represent authenticated user; "?" means guest user (not authenticated yet).
- ],
- ],
- ],
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'logout' => ['post'],//logout action only permit POST request
- ],
- ],
- ];
- }
- public function actions()
- {
- $captcha = [
- 'class' => CaptchaAction::className(),
- 'backColor' => 0x66b3ff,//captcha background color
- 'maxLength' => 4,//captcha max count of characters
- 'minLength' => 4,//captcha min count of characters
- 'padding' => 6,
- 'height' => 34,
- 'width' => 100,
- 'foreColor' => 0xffffff,//captcha character color
- 'offset' => 13,//offset between characters
- ];
- if( YII_ENV_TEST ) $captcha = array_merge($captcha, ['fixedVerifyCode'=>'testme']);
- return [
- 'captcha' => $captcha,
- ];
- }
- /**
- * backend index page(backend default action)
- *
- * @return string
- * @throws yii\base\InvalidConfigException
- * @throws \Throwable
- */
- public function actionIndex()
- {
- $service = new MenuService();
- $menus = $service->getAuthorizedBackendMenusByUserId(Yii::$app->getUser()->getId());
- return $this->renderPartial('index', [
- "menus" => $menus,
- 'identity' => Yii::$app->getUser()->getIdentity(),
- ]);
- }
- /**
- * backend main info page(default right iframe page)
- *
- * @return string
- * @throws yii\base\InvalidConfigException
- */
- public function actionMain()
- {
- /** @var ArticleServiceInterface $articleService */
- $articleService = Yii::$app->get(ArticleServiceInterface::ServiceName);
- /** @var CommentServiceInterface $commentService */
- $commentService = Yii::$app->get(CommentServiceInterface::ServiceName);
- /** @var UserServiceInterface $userService */
- $userService = Yii::$app->get(UserServiceInterface::ServiceName);
- /** @var FriendlyLinkServiceInterface $friendlyLinkService */
- $friendlyLinkService = Yii::$app->get(FriendlyLinkServiceInterface::ServiceName);
- $info = [
- 'OPERATING_ENVIRONMENT' => PHP_OS . ' ' . $_SERVER['SERVER_SOFTWARE'],
- 'PHP_RUN_MODE' => php_sapi_name(),
- 'DB_INFO' => Yii::$app->getDb()->getDriverName() . " " . Yii::$app->getDb()->getServerVersion(),
- 'UPLOAD_MAX_FILE_SIZE' => ini_get('upload_max_filesize'),
- 'MAX_EXECUTION_TIME' => ini_get('max_execution_time') . "s"
- ];
- $obj = new ServerInfo();
- $serverInfo = $obj->getinfo();
- $status = [
- 'DISK_SPACE' => [
- 'NUM' => ceil( $serverInfo['diskTotal'] - $serverInfo['freeSpace'] ) . 'G' . ' / ' . ceil($serverInfo['diskTotal']) . 'G',
- 'PERCENTAGE' => (floatval($serverInfo['diskTotal']) != 0) ? round(($serverInfo['diskTotal'] - $serverInfo['freeSpace']) / $serverInfo['diskTotal'] * 100, 2) : 0,
- ],
- 'MEM' => [
- 'NUM' => $serverInfo["UsedMemory"] . ' / ' . $serverInfo['TotalMemory'],
- 'PERCENTAGE' => $serverInfo["memPercent"],
- ],
- 'REAL_MEM' => [
- 'NUM' => $serverInfo["memRealUsed"] . "(Cached {$serverInfo['CachedMemory']})" . ' / ' . $serverInfo['TotalMemory'],
- 'PERCENTAGE' => $serverInfo['memRealPercent'] . '%',
- ],
- ];
- $temp = [
- 'ARTICLE' => $articleService->getArticlesCountByPeriod(),
- 'COMMENT' => $commentService->getCommentCountByPeriod(),
- 'USER' => $userService->getUserCountByPeriod(),
- 'FRIENDLY_LINK' => $friendlyLinkService->getFriendlyLinkCountByPeriod(),
- ];
- $percent = '0.00';
- $statics = [
- 'ARTICLE' => [
- $temp['ARTICLE'],
- $temp['ARTICLE'] ? sprintf("%01.2f",($articleService->getArticlesCountByPeriod(strtotime(date('Y-m-01 00:00:00')), strtotime(date('Y-m-01 23:59:59') . " +1 month -1 day")) / $temp['ARTICLE']) * 100) : $percent
- ],
- 'COMMENT' => [
- $temp['COMMENT'],
- $temp['COMMENT'] ? sprintf("%01.2f",($commentService->getCommentCountByPeriod(strtotime(date('Y-m-01 00:00:00')), strtotime(date('Y-m-01 23:59:59'))) / $temp['COMMENT']) * 100) : $percent
- ],
- 'USER' => [
- $temp['USER'],
- $temp['USER'] ? sprintf("%01.2f",($userService->getUserCountByPeriod(strtotime(date('Y-m-01 00:00:00')), strtotime(date('Y-m-01 23:59:59') . " +1 month -1 day")) / $temp['USER']) * 100) : $percent
- ],
- 'FRIENDLY_LINK' => [
- $temp['FRIENDLY_LINK'],
- $temp['FRIENDLY_LINK'] ? sprintf("%01.2f",($friendlyLinkService->getFriendlyLinkCountByPeriod(strtotime(date('Y-m-01 00:00:00')), strtotime(date('Y-m-01 23:59:59') . " +1 month -1 day")) / $temp['FRIENDLY_LINK']) * 100) : $percent
- ],
- ];
- /** @var CommentServiceInterface $commentService */
- $commentService = Yii::$app->get(CommentServiceInterface::ServiceName);
- $comments = $commentService->getRecentComments(6);
- return $this->render('main', [
- 'info' => $info,
- 'status' => $status,
- 'statics' => $statics,
- 'comments' => $comments,
- ]);
- }
- /**
- * admin login
- *
- * @return string|yii\web\Response
- */
- public function actionLogin()
- {
- if (! Yii::$app->getUser()->getIsGuest()) {
- return $this->goHome();
- }
- $model = new LoginForm();
- if ($model->load(Yii::$app->getRequest()->post()) && $model->login()) {
- return $this->goBack();
- } else {
- return $this->renderPartial('login', [
- 'model' => $model,
- ]);
- }
- }
- /**
- * admin logout
- *
- * @return yii\web\Response
- */
- public function actionLogout()
- {
- Yii::$app->getUser()->logout(false);
- return $this->goHome();
- }
- /**
- *language change
- *
- */
- public function actionLanguage()
- {
- $language = Yii::$app->getRequest()->get('lang');
- if (isset($language)) {
- $session = Yii::$app->getSession();
- $session->set("language", Html::encode($language));
- }
- $this->goBack(Yii::$app->getRequest()->headers['referer']);
- }
- /**
- * backend unify exception handler
- *
- * @return string
- */
- public function actionError()
- {
- if (($exception = Yii::$app->getErrorHandler()->exception) === null) {
- // action has been invoked not from error handler, but by direct route, so we display '404 Not Found'
- $exception = new HttpException(404, Yii::t('yii', 'Page not found.'));
- }
- if ($exception instanceof HttpException) {
- $code = $exception->statusCode;
- } else {
- $code = $exception->getCode();
- }
- if ($exception instanceof Exception) {
- $name = $exception->getName();
- } else {
- $name = Yii::t('yii', 'Error');
- }
- if ($code) {
- $name .= " (#$code)";
- }
- if ($exception instanceof UserException) {
- $message = $exception->getMessage();
- } else {
- $message = Yii::t('yii', 'An internal server error occurred.');
- }
- $statusCode = $exception->statusCode ? $exception->statusCode : 500;
- if (Yii::$app->getRequest()->getIsAjax()) {
- return "$name: $message";
- } else {
- return $this->render('error', [
- 'code' => $statusCode,
- 'name' => $name,
- 'message' => $message
- ]);
- }
- }
- }
|