forked from kuafuRace/phprap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPublicController.php
More file actions
executable file
·155 lines (118 loc) · 3.84 KB
/
Copy pathPublicController.php
File metadata and controls
executable file
·155 lines (118 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace app\controllers\home;
use app\models\Apply;
use app\models\Config;
use app\models\Model;
use app\models\Notify;
use Yii;
use yii\debug\Module;
use yii\helpers\Url;
use yii\web\Controller;
use yii\web\Response;
class PublicController extends Controller
{
/**
* 是否启用布局
* @var bool
*/
public $layout = false;
/**
* 是否检测登录
* @var bool
*/
public $checkLogin = true;
public $debugTags;
public function beforeAction($action)
{
if(!$this->isInstalled()){
return $this->redirect(['home/install/step1'])->send();
}
$config = Config::findOne(['type' => 'safe'])->getField();
$ip_white_list = array_filter(explode("\r\n", trim($config->ip_white_list)));
$ip_black_list = array_filter(explode("\r\n", trim($config->ip_black_list)));
$ip = Yii::$app->request->userIP;
if($ip_white_list && !in_array($ip, $ip_white_list)){
return $this->error('抱歉,该IP不在可访问IP段内');
}
if($ip_black_list && in_array($ip, $ip_black_list)){
return $this->error('抱歉,该IP不允许访问');
}
if($this->checkLogin && Yii::$app->user->isGuest){
return $this->redirect(['home/account/login'])->send();
}
return true;
}
/** 展示模板
* @param $view
* @param array $params
* @return string
*/
public function display($view, $params = [])
{
$account = Yii::$app->user->identity;
if($account->id){
$params['check_status'] = Apply::CHECK_STATUS;
$params['order_by'] = 'id desc';
$notify = Apply::findModel()->search($params);
$params['notify'] = $notify;
$params['account'] = $account;
}
exit($this->render($view . '.html', $params));
}
/**
* 成功消息提示
* @param $message 提示信息
* @param int $jumpSeconds 延迟时间
* @param string $jumpUrl 跳转链接
* @param null $model
* @return string
*/
public function success($message, $jumpSeconds = 1, $jumpUrl = '')
{
$jumpUrl = $jumpUrl ? Url::toRoute($jumpUrl) : \Yii::$app->request->referrer;
return $this->display('/home/public/message', ['flag' => 'success', 'message' => $message, 'time' => $jumpSeconds, 'url' => $jumpUrl]);
}
/**
* 错误消息提示
* @param $message
* @param int $jumpSeconds
* @param string $jumpUrl
* @return string
*/
public function error($message, $jumpSeconds = 3, $jumpUrl = '')
{
$jumpUrl = $jumpUrl ? Url::toRoute($jumpUrl) : \Yii::$app->request->referrer;
return $this->display('/home/public/message', ['flag' => 'error', 'message' => $message, 'time' => $jumpSeconds, 'url' => $jumpUrl]);
}
/**
* 判断是否已经安装过
* @return bool
*/
public function isInstalled()
{
return file_exists(Yii::getAlias("@runtime") . '/install/install.lock');
}
private function getDebugTags($forceReload = false)
{
if ($this->debugTags === null || $forceReload) {
if ($forceReload) {
clearstatcache();
}
$indexFile = Module::getInstance()->dataPath . '/index.data';
$content = '';
$fp = @fopen($indexFile, 'r');
if ($fp !== false) {
@flock($fp, LOCK_SH);
$content = fread($fp, filesize($indexFile));
@flock($fp, LOCK_UN);
fclose($fp);
}
if ($content !== '') {
$this->debugTags = array_reverse(unserialize($content), true);
} else {
$this->debugTags = [];
}
}
return $this->debugTags;
}
}