SiteController.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2017-03-15 21:16
  7. */
  8. namespace backend\controllers;
  9. use Yii;
  10. use Exception;
  11. use common\services\ArticleServiceInterface;
  12. use common\services\UserServiceInterface;
  13. use common\services\CommentServiceInterface;
  14. use common\services\FriendlyLinkServiceInterface;
  15. use common\services\MenuService;
  16. use backend\models\form\LoginForm;
  17. use common\libs\ServerInfo;
  18. use yii\base\UserException;
  19. use yii\filters\AccessControl;
  20. use yii\filters\VerbFilter;
  21. use yii\helpers\Html;
  22. use yii\web\HttpException;
  23. use yii\captcha\CaptchaAction;
  24. /**
  25. * Site default controller
  26. *
  27. * - description:
  28. * - site/index is index page(framework)
  29. * - site/main is the iframe default page
  30. * - site/login backend login page
  31. * - site/logout backend user logout
  32. * - site/language multi language change
  33. * - site/error backend error unify handler
  34. *
  35. * Class SiteController
  36. * @package backend\controllers
  37. */
  38. class SiteController extends \yii\web\Controller
  39. {
  40. public function behaviors()
  41. {
  42. return [
  43. 'access' => [
  44. 'class' => AccessControl::className(),
  45. 'except' =>['login', 'captcha', 'language'],//no need login actions
  46. 'rules' => [
  47. [
  48. 'allow' => true,
  49. 'roles' => ['@'],//"@" represent authenticated user; "?" means guest user (not authenticated yet).
  50. ],
  51. ],
  52. ],
  53. 'verbs' => [
  54. 'class' => VerbFilter::className(),
  55. 'actions' => [
  56. 'logout' => ['post'],//logout action only permit POST request
  57. ],
  58. ],
  59. ];
  60. }
  61. public function actions()
  62. {
  63. $captcha = [
  64. 'class' => CaptchaAction::className(),
  65. 'backColor' => 0x66b3ff,//captcha background color
  66. 'maxLength' => 4,//captcha max count of characters
  67. 'minLength' => 4,//captcha min count of characters
  68. 'padding' => 6,
  69. 'height' => 34,
  70. 'width' => 100,
  71. 'foreColor' => 0xffffff,//captcha character color
  72. 'offset' => 13,//offset between characters
  73. ];
  74. if( YII_ENV_TEST ) $captcha = array_merge($captcha, ['fixedVerifyCode'=>'testme']);
  75. return [
  76. 'captcha' => $captcha,
  77. ];
  78. }
  79. /**
  80. * backend index page(backend default action)
  81. *
  82. * @return string
  83. * @throws yii\base\InvalidConfigException
  84. * @throws \Throwable
  85. */
  86. public function actionIndex()
  87. {
  88. $service = new MenuService();
  89. $menus = $service->getAuthorizedBackendMenusByUserId(Yii::$app->getUser()->getId());
  90. return $this->renderPartial('index', [
  91. "menus" => $menus,
  92. 'identity' => Yii::$app->getUser()->getIdentity(),
  93. ]);
  94. }
  95. /**
  96. * backend main info page(default right iframe page)
  97. *
  98. * @return string
  99. * @throws yii\base\InvalidConfigException
  100. */
  101. public function actionMain()
  102. {
  103. /** @var ArticleServiceInterface $articleService */
  104. $articleService = Yii::$app->get(ArticleServiceInterface::ServiceName);
  105. /** @var CommentServiceInterface $commentService */
  106. $commentService = Yii::$app->get(CommentServiceInterface::ServiceName);
  107. /** @var UserServiceInterface $userService */
  108. $userService = Yii::$app->get(UserServiceInterface::ServiceName);
  109. /** @var FriendlyLinkServiceInterface $friendlyLinkService */
  110. $friendlyLinkService = Yii::$app->get(FriendlyLinkServiceInterface::ServiceName);
  111. $info = [
  112. 'OPERATING_ENVIRONMENT' => PHP_OS . ' ' . $_SERVER['SERVER_SOFTWARE'],
  113. 'PHP_RUN_MODE' => php_sapi_name(),
  114. 'DB_INFO' => Yii::$app->getDb()->getDriverName() . " " . Yii::$app->getDb()->getServerVersion(),
  115. 'UPLOAD_MAX_FILE_SIZE' => ini_get('upload_max_filesize'),
  116. 'MAX_EXECUTION_TIME' => ini_get('max_execution_time') . "s"
  117. ];
  118. $obj = new ServerInfo();
  119. $serverInfo = $obj->getinfo();
  120. $status = [
  121. 'DISK_SPACE' => [
  122. 'NUM' => ceil( $serverInfo['diskTotal'] - $serverInfo['freeSpace'] ) . 'G' . ' / ' . ceil($serverInfo['diskTotal']) . 'G',
  123. 'PERCENTAGE' => (floatval($serverInfo['diskTotal']) != 0) ? round(($serverInfo['diskTotal'] - $serverInfo['freeSpace']) / $serverInfo['diskTotal'] * 100, 2) : 0,
  124. ],
  125. 'MEM' => [
  126. 'NUM' => $serverInfo["UsedMemory"] . ' / ' . $serverInfo['TotalMemory'],
  127. 'PERCENTAGE' => $serverInfo["memPercent"],
  128. ],
  129. 'REAL_MEM' => [
  130. 'NUM' => $serverInfo["memRealUsed"] . "(Cached {$serverInfo['CachedMemory']})" . ' / ' . $serverInfo['TotalMemory'],
  131. 'PERCENTAGE' => $serverInfo['memRealPercent'] . '%',
  132. ],
  133. ];
  134. $temp = [
  135. 'ARTICLE' => $articleService->getArticlesCountByPeriod(),
  136. 'COMMENT' => $commentService->getCommentCountByPeriod(),
  137. 'USER' => $userService->getUserCountByPeriod(),
  138. 'FRIENDLY_LINK' => $friendlyLinkService->getFriendlyLinkCountByPeriod(),
  139. ];
  140. $percent = '0.00';
  141. $statics = [
  142. 'ARTICLE' => [
  143. $temp['ARTICLE'],
  144. $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
  145. ],
  146. 'COMMENT' => [
  147. $temp['COMMENT'],
  148. $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
  149. ],
  150. 'USER' => [
  151. $temp['USER'],
  152. $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
  153. ],
  154. 'FRIENDLY_LINK' => [
  155. $temp['FRIENDLY_LINK'],
  156. $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
  157. ],
  158. ];
  159. /** @var CommentServiceInterface $commentService */
  160. $commentService = Yii::$app->get(CommentServiceInterface::ServiceName);
  161. $comments = $commentService->getRecentComments(6);
  162. return $this->render('main', [
  163. 'info' => $info,
  164. 'status' => $status,
  165. 'statics' => $statics,
  166. 'comments' => $comments,
  167. ]);
  168. }
  169. /**
  170. * admin login
  171. *
  172. * @return string|yii\web\Response
  173. */
  174. public function actionLogin()
  175. {
  176. if (! Yii::$app->getUser()->getIsGuest()) {
  177. return $this->goHome();
  178. }
  179. $model = new LoginForm();
  180. if ($model->load(Yii::$app->getRequest()->post()) && $model->login()) {
  181. return $this->goBack();
  182. } else {
  183. return $this->renderPartial('login', [
  184. 'model' => $model,
  185. ]);
  186. }
  187. }
  188. /**
  189. * admin logout
  190. *
  191. * @return yii\web\Response
  192. */
  193. public function actionLogout()
  194. {
  195. Yii::$app->getUser()->logout(false);
  196. return $this->goHome();
  197. }
  198. /**
  199. *language change
  200. *
  201. */
  202. public function actionLanguage()
  203. {
  204. $language = Yii::$app->getRequest()->get('lang');
  205. if (isset($language)) {
  206. $session = Yii::$app->getSession();
  207. $session->set("language", Html::encode($language));
  208. }
  209. $this->goBack(Yii::$app->getRequest()->headers['referer']);
  210. }
  211. /**
  212. * backend unify exception handler
  213. *
  214. * @return string
  215. */
  216. public function actionError()
  217. {
  218. if (($exception = Yii::$app->getErrorHandler()->exception) === null) {
  219. // action has been invoked not from error handler, but by direct route, so we display '404 Not Found'
  220. $exception = new HttpException(404, Yii::t('yii', 'Page not found.'));
  221. }
  222. if ($exception instanceof HttpException) {
  223. $code = $exception->statusCode;
  224. } else {
  225. $code = $exception->getCode();
  226. }
  227. if ($exception instanceof Exception) {
  228. $name = $exception->getName();
  229. } else {
  230. $name = Yii::t('yii', 'Error');
  231. }
  232. if ($code) {
  233. $name .= " (#$code)";
  234. }
  235. if ($exception instanceof UserException) {
  236. $message = $exception->getMessage();
  237. } else {
  238. $message = Yii::t('yii', 'An internal server error occurred.');
  239. }
  240. $statusCode = $exception->statusCode ? $exception->statusCode : 500;
  241. if (Yii::$app->getRequest()->getIsAjax()) {
  242. return "$name: $message";
  243. } else {
  244. return $this->render('error', [
  245. 'code' => $statusCode,
  246. 'name' => $name,
  247. 'message' => $message
  248. ]);
  249. }
  250. }
  251. }