forked from kuafuRace/phprap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingController.php
More file actions
130 lines (98 loc) · 3.7 KB
/
Copy pathSettingController.php
File metadata and controls
130 lines (98 loc) · 3.7 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
<?php
namespace app\controllers\admin;
use Yii;
use yii\helpers\Html;
use yii\web\Response;
use app\models\Config;
class SettingController extends PublicController
{
/**
* 基础设置
*
* @return string
*/
public function actionApp()
{
$request = Yii::$app->request;
$response = Yii::$app->response;
$config = Config::findOne(['type' => 'app']);
if($request->isPost){
$response->format = Response::FORMAT_JSON;
$config->content = $this->form2json($request->post('Config'));
if ($config->save()) {
return ['status' => 'success', 'message' => '保存成功'];
}
return ['status' => 'error', 'message' => $config->getErrorMessage(), 'label' => $config->getErrorLabel()];
}
return $this->display('app', ['config' => $config]);
}
/**
* 邮箱设置
* @return array|string
*/
public function actionEmail()
{
$request = Yii::$app->request;
$response = Yii::$app->response;
$config = Config::findOne(['type' => 'email']);
if($request->isPost){
$response->format = Response::FORMAT_JSON;
$config->content = $this->form2json($request->post('Config'));
if ($config->save()) {
return ['status' => 'success', 'message' => '保存成功'];
}
return ['status' => 'error', 'message' => $config->getErrorMessage(), 'label' => $config->getErrorLabel()];
}
return $this->display('email', ['config' => $config]);
}
/**
* 安全设置
* @return array|string
*/
public function actionSafe()
{
$request = Yii::$app->request;
$response = Yii::$app->response;
$config = Config::findOne(['type' => 'safe']);
if($request->isPost){
$response->format = Response::FORMAT_JSON;
$data = $request->post('Config');
// 判断输入IP是否同时存在于白名单和黑名单
$ip_white_list = explode("\r\n", trim($data['ip_white_list']));
$ip_black_list = explode("\r\n", trim($data['ip_black_list']));
$conflict_list = array_intersect($ip_white_list, $ip_black_list);
if(array_filter($conflict_list)){
return ['status' => 'error', 'message' => '黑名单和白名单里不能出现相同的IP'];
}
// 判断邮箱后缀是否同时存在于白名单和黑名单
$email_white_list = explode('\r\n', trim($data['email_white_list']));
$email_black_list = explode('\r\n', trim($data['email_black_list']));
$conflict_list = array_intersect($email_white_list, $email_black_list);
if(array_filter($conflict_list)){
return ['status' => 'error', 'message' => '黑名单和白名单里不能出现相同的邮箱后缀'];
}
$config->content = json_encode($data, JSON_UNESCAPED_UNICODE);
if ($config->save()) {
return ['status' => 'success', 'message' => '保存成功'];
}
return ['status' => 'error', 'message' => $config->getErrorMessage(), 'label' => $config->getErrorLabel()];
}
return $this->display('safe', ['config' => $config]);
}
/**
* 表单过滤后转json
* @param $table
* @return false|string
*/
private function form2json($form)
{
if(!is_array($form) || !$form){
return;
}
$array = [];
foreach ($form as $k => $v) {
$array[$k] = trim(Html::encode($v));
}
return json_encode($array, JSON_UNESCAPED_UNICODE);
}
}