Completed
Push — master ( 4e7f70...16eef9 )
by Henry
09:26
created

includes/Admin/Controller/Setting.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
30 4
	public function process(string $action = null) : string
31
	{
32 4
		$postArray = $this->_normalizePost($this->_sanitizePost());
33 4
		$validateArray = $this->_validatePost($postArray);
34
35
		/* validate post */
36
37 4
		if ($validateArray)
38
		{
39 2
			return $this->_error(
40
			[
41 2
				'route' => $this->_getErrorRoute(),
42 2
				'message' => $validateArray
43
			]);
44
		}
45
46
		/* handle update */
47
48 2
		if ($action === 'update')
49
		{
50
			$updateArray =
51
			[
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 1
				'captcha' => $postArray['captcha']
77
			];
78 1
			if ($this->_update($updateArray))
79
			{
80 1
				return $this->_success(
81
				[
82 1
					'route' => 'admin',
83
					'timeout' => 2
84
				]);
85
			}
86
		}
87
88
		/* handle error */
89
90 1
		return $this->_error(
91
		[
92 1
			'route' => $this->_getErrorRoute()
93
		]);
94
	}
95
96
	/**
97
	 * sanitize the post
98
	 *
99
	 * @since 4.0.0
100
	 *
101
	 * @return array
102
	 */
103
104 4
	protected function _sanitizePost() : array
105
	{
106 4
		$emailFilter = new Filter\Email();
107 4
		$numberFilter = new Filter\Number();
108 4
		$specialFilter = new Filter\Special();
109 4
		$textFilter = new Filter\Text();
110 4
		$toggleFilter = new Filter\Toggle();
111
112
		/* sanitize post */
113
114
		return
115
		[
116 4
			'language' => $specialFilter->sanitize($this->_request->getPost('language')),
117 4
			'template' => $specialFilter->sanitize($this->_request->getPost('template')),
118 4
			'title' => $textFilter->sanitize($this->_request->getPost('title')),
119 4
			'author' => $textFilter->sanitize($this->_request->getPost('author')),
120 4
			'copyright' => $textFilter->sanitize($this->_request->getPost('copyright')),
121 4
			'description' => $textFilter->sanitize($this->_request->getPost('description')),
122 4
			'keywords' => $textFilter->sanitize($this->_request->getPost('keywords')),
123 4
			'robots' => $numberFilter->sanitize($this->_request->getPost('robots')),
124 4
			'email' => $emailFilter->sanitize($this->_request->getPost('email')),
125 4
			'subject' => $textFilter->sanitize($this->_request->getPost('subject')),
126 4
			'notification' => $toggleFilter->sanitize($this->_request->getPost('notification')),
127 4
			'charset' => $textFilter->sanitize($this->_request->getPost('charset')),
128 4
			'divider' => $textFilter->sanitize($this->_request->getPost('divider')),
129 4
			'zone' => $textFilter->sanitize($this->_request->getPost('zone')),
130 4
			'time' => $textFilter->sanitize($this->_request->getPost('time')),
131 4
			'date' => $textFilter->sanitize($this->_request->getPost('date')),
132 4
			'homepage' => $numberFilter->sanitize($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 4
			'recovery' => $toggleFilter->sanitize($this->_request->getPost('recovery')),
140 4
			'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
	 */
153
154 4
	protected function _validatePost(array $postArray = []) : array
155
	{
156 4
		$nameValidator = new Validator\Name();
157 4
		$userValidator = new Validator\User();
0 ignored issues
show
$userValidator is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
158 4
		$validateArray = [];
159
160
		/* validate post */
161
162 4
		if (!$postArray['title'])
163
		{
164 2
			$validateArray[] = $this->_language->get('title_empty');
165
		}
166 2
		else if (!$nameValidator->validate($postArray['title']))
167
		{
168
			$validateArray[] = $this->_language->get('title_incorrect');
169
		}
170 4
		if (!$postArray['author'])
171
		{
172 2
			$validateArray[] = $this->_language->get('author_empty');
173
		}
174 2
		else if (!$nameValidator->validate($postArray['author']))
175
		{
176
			$validateArray[] = $this->_language->get('author_incorrect');
177
		}
178 4
		if (!$postArray['charset'] || !$postArray['limit'])
179
		{
180 2
			$validateArray[] = $this->_language->get('input_empty');
181
		}
182 4
		return $validateArray;
183
	}
184
185
	/**
186
	 * update the setting
187
	 *
188
	 * @since 4.0.0
189
	 *
190
	 * @param array $updateArray array of the update
191
	 *
192
	 * @return bool
193
	 */
194
195 1
	protected function _update(array $updateArray = []) : bool
196
	{
197 1
		$settingModel = new Admin\Model\Setting();
198 1
		return $settingModel->updateByArray($updateArray);
199
	}
200
201
	/**
202
	 * get error route
203
	 *
204
	 * @since 4.1.0
205
	 *
206
	 * @return string
207
	 */
208
209 3
	protected function _getErrorRoute() : string
210
	{
211 3
		if ($this->_registry->get('settingsEdit'))
212
		{
213 2
			return 'admin/edit/settings';
214
		}
215 1
		return 'admin';
216
	}
217
}
218