1
|
|
|
<?php |
2
|
|
|
namespace Redaxscript\Admin\Controller; |
3
|
|
|
|
4
|
|
|
use Redaxscript\Admin; |
5
|
|
|
use Redaxscript\Filter; |
6
|
|
|
use Redaxscript\Validator; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* children class to process the admin setting request |
10
|
|
|
* |
11
|
|
|
* @since 4.0.0 |
12
|
|
|
* |
13
|
|
|
* @package Redaxscript |
14
|
|
|
* @category Controller |
15
|
|
|
* @author Henry Ruhs |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
class Setting extends ControllerAbstract |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* process the class |
22
|
|
|
* |
23
|
|
|
* @since 4.0.0 |
24
|
|
|
* |
25
|
|
|
* @param string $action action to process |
26
|
|
|
* |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
4 |
|
|
30
|
|
|
public function process(string $action = null) : string |
31
|
4 |
|
{ |
32
|
4 |
|
$postArray = $this->_normalizePost($this->_sanitizePost()); |
33
|
|
|
$validateArray = $this->_validatePost($postArray); |
|
|
|
|
34
|
|
|
|
35
|
|
|
/* validate post */ |
36
|
4 |
|
|
37
|
|
|
if ($validateArray) |
|
|
|
|
38
|
2 |
|
{ |
39
|
|
|
return $this->_error( |
40
|
2 |
|
[ |
41
|
2 |
|
'route' => $this->_getErrorRoute(), |
42
|
|
|
'message' => $validateArray |
43
|
|
|
]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/* handle update */ |
47
|
2 |
|
|
48
|
|
|
if ($action === 'update') |
49
|
|
|
{ |
50
|
|
|
$updateArray = |
51
|
1 |
|
[ |
52
|
1 |
|
'language' => $postArray['language'], |
53
|
1 |
|
'template' => $postArray['template'], |
54
|
1 |
|
'title' => $postArray['title'], |
55
|
1 |
|
'author' => $postArray['author'], |
56
|
1 |
|
'copyright' => $postArray['copyright'], |
57
|
1 |
|
'description' => $postArray['description'], |
58
|
1 |
|
'keywords' => $postArray['keywords'], |
59
|
1 |
|
'robots' => $postArray['robots'], |
60
|
1 |
|
'email' => $postArray['email'], |
61
|
1 |
|
'subject' => $postArray['subject'], |
62
|
1 |
|
'notification' => $postArray['notification'], |
63
|
1 |
|
'charset' => $postArray['charset'], |
64
|
1 |
|
'divider' => $postArray['divider'], |
65
|
1 |
|
'zone' => $postArray['zone'], |
66
|
1 |
|
'time' => $postArray['time'], |
67
|
1 |
|
'date' => $postArray['date'], |
68
|
1 |
|
'homepage' => $postArray['homepage'], |
69
|
1 |
|
'limit' => $postArray['limit'], |
70
|
1 |
|
'order' => $postArray['order'], |
71
|
1 |
|
'pagination' => $postArray['pagination'], |
72
|
1 |
|
'moderation' => $postArray['moderation'], |
73
|
1 |
|
'registration' => $postArray['registration'], |
74
|
1 |
|
'verification' => $postArray['verification'], |
75
|
1 |
|
'recovery' => $postArray['recovery'], |
76
|
|
|
'captcha' => $postArray['captcha'] |
77
|
1 |
|
]; |
78
|
|
|
if ($this->_update($updateArray)) |
79
|
1 |
|
{ |
80
|
|
|
return $this->_success( |
81
|
1 |
|
[ |
82
|
|
|
'route' => 'admin', |
83
|
|
|
'timeout' => 2 |
84
|
|
|
]); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/* handle error */ |
89
|
1 |
|
|
90
|
|
|
return $this->_error( |
91
|
1 |
|
[ |
92
|
|
|
'route' => $this->_getErrorRoute() |
93
|
|
|
]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* sanitize the post |
98
|
|
|
* |
99
|
|
|
* @since 4.0.0 |
100
|
|
|
* |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
4 |
|
|
104
|
|
|
protected function _sanitizePost() : array |
105
|
4 |
|
{ |
106
|
4 |
|
$emailFilter = new Filter\Email(); |
107
|
4 |
|
$nameFilter = new Filter\Name(); |
108
|
4 |
|
$numberFilter = new Filter\Number(); |
109
|
|
|
$specialFilter = new Filter\Special(); |
110
|
|
|
$toggleFilter = new Filter\Toggle(); |
111
|
|
|
|
112
|
|
|
/* sanitize post */ |
113
|
|
|
|
114
|
4 |
|
return |
115
|
4 |
|
[ |
116
|
4 |
|
'language' => $specialFilter->sanitize($this->_request->getPost('language')), |
|
|
|
|
117
|
4 |
|
'template' => $specialFilter->sanitize($this->_request->getPost('template')), |
|
|
|
|
118
|
4 |
|
'title' => $nameFilter->sanitize($this->_request->getPost('title')), |
|
|
|
|
119
|
4 |
|
'author' => $nameFilter->sanitize($this->_request->getPost('author')), |
|
|
|
|
120
|
4 |
|
'copyright' => $this->_request->getPost('copyright'), |
121
|
4 |
|
'description' => $this->_request->getPost('description'), |
122
|
4 |
|
'keywords' => $this->_request->getPost('keywords'), |
123
|
4 |
|
'robots' => $this->_request->getPost('robots'), |
124
|
4 |
|
'email' => $emailFilter->sanitize($this->_request->getPost('email')), |
|
|
|
|
125
|
4 |
|
'subject' => $this->_request->getPost('subject'), |
126
|
4 |
|
'notification' => $toggleFilter->sanitize($this->_request->getPost('notification')), |
|
|
|
|
127
|
4 |
|
'charset' => $this->_request->getPost('charset'), |
128
|
4 |
|
'divider' => $this->_request->getPost('divider'), |
129
|
4 |
|
'zone' => $this->_request->getPost('zone'), |
130
|
4 |
|
'time' => $this->_request->getPost('time'), |
131
|
4 |
|
'date' => $this->_request->getPost('date'), |
132
|
4 |
|
'homepage' => $this->_request->getPost('homepage'), |
133
|
4 |
|
'limit' => $numberFilter->sanitize($this->_request->getPost('limit')), |
|
|
|
|
134
|
4 |
|
'order' => $specialFilter->sanitize($this->_request->getPost('order')), |
|
|
|
|
135
|
4 |
|
'pagination' => $toggleFilter->sanitize($this->_request->getPost('pagination')), |
|
|
|
|
136
|
4 |
|
'moderation' => $toggleFilter->sanitize($this->_request->getPost('moderation')), |
|
|
|
|
137
|
4 |
|
'registration' => $toggleFilter->sanitize($this->_request->getPost('registration')), |
|
|
|
|
138
|
4 |
|
'verification' => $toggleFilter->sanitize($this->_request->getPost('verification')), |
|
|
|
|
139
|
|
|
'recovery' => $toggleFilter->sanitize($this->_request->getPost('recovery')), |
|
|
|
|
140
|
|
|
'captcha' => $numberFilter->sanitize($this->_request->getPost('captcha')) |
|
|
|
|
141
|
|
|
]; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* validate the post |
146
|
|
|
* |
147
|
|
|
* @since 4.0.0 |
148
|
|
|
* |
149
|
|
|
* @param array $postArray array of the post |
150
|
|
|
* |
151
|
|
|
* @return array |
152
|
4 |
|
*/ |
153
|
|
|
|
154
|
4 |
|
protected function _validatePost(array $postArray = []) : array |
155
|
|
|
{ |
156
|
|
|
$nameValidator = new Validator\Name(); |
157
|
|
|
$userValidator = new Validator\User(); |
158
|
4 |
|
$validateArray = []; |
159
|
|
|
|
160
|
2 |
|
/* validate post */ |
161
|
|
|
|
162
|
4 |
|
if (!$postArray['title']) |
163
|
|
|
{ |
164
|
|
|
$validateArray[] = $this->_language->get('title_empty'); |
165
|
|
|
} |
166
|
|
|
else if (!$nameValidator->validate($postArray['title'])) |
167
|
|
|
{ |
168
|
|
|
$validateArray[] = $this->_language->get('title_incorrect'); |
169
|
|
|
} |
170
|
|
|
if (!$postArray['author']) |
171
|
|
|
{ |
172
|
|
|
$validateArray[] = $this->_language->get('author_empty'); |
173
|
|
|
} |
174
|
|
|
else if (!$userValidator->validate($postArray['author'])) |
175
|
1 |
|
{ |
176
|
|
|
$validateArray[] = $this->_language->get('author_incorrect'); |
177
|
1 |
|
} |
178
|
1 |
|
if (!$postArray['charset'] || !$postArray['limit']) |
179
|
|
|
{ |
180
|
|
|
$validateArray[] = $this->_language->get('input_empty'); |
181
|
|
|
} |
182
|
|
|
return $validateArray; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* update the setting |
187
|
|
|
* |
188
|
|
|
* @since 4.0.0 |
189
|
3 |
|
* |
190
|
|
|
* @param array $updateArray array of the update |
191
|
3 |
|
* |
192
|
|
|
* @return bool |
193
|
2 |
|
*/ |
194
|
|
|
|
195
|
1 |
|
protected function _update(array $updateArray = []) : bool |
196
|
|
|
{ |
197
|
|
|
$settingModel = new Admin\Model\Setting(); |
198
|
|
|
return $settingModel->updateByArray($updateArray); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* get error route |
203
|
|
|
* |
204
|
|
|
* @since 4.1.0 |
205
|
|
|
* |
206
|
|
|
* @return string |
207
|
|
|
*/ |
208
|
|
|
|
209
|
|
|
protected function _getErrorRoute() : string |
210
|
|
|
{ |
211
|
|
|
if ($this->_registry->get('settingsEdit')) |
212
|
|
|
{ |
213
|
|
|
return 'admin/edit/settings'; |
214
|
|
|
} |
215
|
|
|
return 'admin'; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.