Completed
Push — master ( e9cd30...c2303d )
by Henry
05:05
created

includes/Admin/Controller/Setting.php (29 issues)

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);
0 ignored issues
show
It seems like $postArray defined by $this->_normalizePost($this->_sanitizePost()) on line 32 can also be of type null; however, Redaxscript\Admin\Contro...etting::_validatePost() does only seem to accept array, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
34
35
		/* validate post */
36
37 4
		if ($validateArray)
0 ignored issues
show
Bug Best Practice introduced by
The expression $validateArray of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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
				'smtp' => $postArray['smtp'],
62 1
				'subject' => $postArray['subject'],
63 1
				'notification' => $postArray['notification'],
64 1
				'charset' => $postArray['charset'],
65 1
				'locale' => $postArray['locale'],
66 1
				'divider' => $postArray['divider'],
67 1
				'zone' => $postArray['zone'],
68 1
				'time' => $postArray['time'],
69 1
				'date' => $postArray['date'],
70 1
				'homepage' => $postArray['homepage'],
71 1
				'limit' => $postArray['limit'],
72 1
				'order' => $postArray['order'],
73 1
				'pagination' => $postArray['pagination'],
74 1
				'moderation' => $postArray['moderation'],
75 1
				'registration' => $postArray['registration'],
76 1
				'verification' => $postArray['verification'],
77 1
				'recovery' => $postArray['recovery'],
78 1
				'captcha' => $postArray['captcha']
79
			];
80 1
			if ($this->_update($updateArray))
81
			{
82 1
				return $this->_success(
83
				[
84 1
					'route' => 'admin',
85
					'timeout' => 2
86
				]);
87
			}
88
		}
89
90
		/* handle error */
91
92 1
		return $this->_error(
93
		[
94 1
			'route' => $this->_getErrorRoute()
95
		]);
96
	}
97
98
	/**
99
	 * sanitize the post
100
	 *
101
	 * @since 4.0.0
102
	 *
103
	 * @return array
104
	 */
105
106 4
	protected function _sanitizePost() : array
107
	{
108 4
		$emailFilter = new Filter\Email();
109 4
		$numberFilter = new Filter\Number();
110 4
		$specialFilter = new Filter\Special();
111 4
		$textFilter = new Filter\Text();
112 4
		$toggleFilter = new Filter\Toggle();
113
114
		/* sanitize post */
115
116
		return
117
		[
118 4
			'language' => $specialFilter->sanitize($this->_request->getPost('language')),
0 ignored issues
show
It seems like $this->_request->getPost('language') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Special::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
119 4
			'template' => $specialFilter->sanitize($this->_request->getPost('template')),
0 ignored issues
show
It seems like $this->_request->getPost('template') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Special::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
120 4
			'title' => $textFilter->sanitize($this->_request->getPost('title')),
0 ignored issues
show
It seems like $this->_request->getPost('title') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Text::sanitize() does only seem to accept integer|string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
121 4
			'author' => $textFilter->sanitize($this->_request->getPost('author')),
0 ignored issues
show
It seems like $this->_request->getPost('author') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Text::sanitize() does only seem to accept integer|string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
122 4
			'copyright' => $textFilter->sanitize($this->_request->getPost('copyright')),
0 ignored issues
show
It seems like $this->_request->getPost('copyright') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Text::sanitize() does only seem to accept integer|string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
123 4
			'description' => $textFilter->sanitize($this->_request->getPost('description')),
0 ignored issues
show
It seems like $this->_request->getPost('description') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Text::sanitize() does only seem to accept integer|string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
124 4
			'keywords' => $textFilter->sanitize($this->_request->getPost('keywords')),
0 ignored issues
show
It seems like $this->_request->getPost('keywords') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Text::sanitize() does only seem to accept integer|string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
125 4
			'robots' => $numberFilter->sanitize($this->_request->getPost('robots')),
0 ignored issues
show
It seems like $this->_request->getPost('robots') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Number::sanitize() does only seem to accept integer|string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
126 4
			'email' => $emailFilter->sanitize($this->_request->getPost('email')),
0 ignored issues
show
It seems like $this->_request->getPost('email') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Email::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
127 4
			'smtp' => $emailFilter->sanitize($this->_request->getPost('smtp')),
0 ignored issues
show
It seems like $this->_request->getPost('smtp') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Email::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
128 4
			'subject' => $textFilter->sanitize($this->_request->getPost('subject')),
0 ignored issues
show
It seems like $this->_request->getPost('subject') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Text::sanitize() does only seem to accept integer|string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
129 4
			'notification' => $toggleFilter->sanitize($this->_request->getPost('notification')),
0 ignored issues
show
It seems like $this->_request->getPost('notification') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Toggle::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
130 4
			'charset' => $textFilter->sanitize($this->_request->getPost('charset')),
0 ignored issues
show
It seems like $this->_request->getPost('charset') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Text::sanitize() does only seem to accept integer|string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
131 4
			'locale' => $textFilter->sanitize($this->_request->getPost('locale')),
0 ignored issues
show
It seems like $this->_request->getPost('locale') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Text::sanitize() does only seem to accept integer|string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
132 4
			'divider' => $textFilter->sanitize($this->_request->getPost('divider')),
0 ignored issues
show
It seems like $this->_request->getPost('divider') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Text::sanitize() does only seem to accept integer|string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
133 4
			'zone' => $textFilter->sanitize($this->_request->getPost('zone')),
0 ignored issues
show
It seems like $this->_request->getPost('zone') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Text::sanitize() does only seem to accept integer|string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
134 4
			'time' => $textFilter->sanitize($this->_request->getPost('time')),
0 ignored issues
show
It seems like $this->_request->getPost('time') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Text::sanitize() does only seem to accept integer|string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
135 4
			'date' => $textFilter->sanitize($this->_request->getPost('date')),
0 ignored issues
show
It seems like $this->_request->getPost('date') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Text::sanitize() does only seem to accept integer|string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
136 4
			'homepage' => $numberFilter->sanitize($this->_request->getPost('homepage')),
0 ignored issues
show
It seems like $this->_request->getPost('homepage') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Number::sanitize() does only seem to accept integer|string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
137 4
			'limit' => $numberFilter->sanitize($this->_request->getPost('limit')),
0 ignored issues
show
It seems like $this->_request->getPost('limit') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Number::sanitize() does only seem to accept integer|string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
138 4
			'order' => $specialFilter->sanitize($this->_request->getPost('order')),
0 ignored issues
show
It seems like $this->_request->getPost('order') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Special::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
139 4
			'pagination' => $toggleFilter->sanitize($this->_request->getPost('pagination')),
0 ignored issues
show
It seems like $this->_request->getPost('pagination') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Toggle::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
140 4
			'moderation' => $toggleFilter->sanitize($this->_request->getPost('moderation')),
0 ignored issues
show
It seems like $this->_request->getPost('moderation') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Toggle::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
141 4
			'registration' => $toggleFilter->sanitize($this->_request->getPost('registration')),
0 ignored issues
show
It seems like $this->_request->getPost('registration') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Toggle::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
142 4
			'verification' => $toggleFilter->sanitize($this->_request->getPost('verification')),
0 ignored issues
show
It seems like $this->_request->getPost('verification') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Toggle::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
143 4
			'recovery' => $toggleFilter->sanitize($this->_request->getPost('recovery')),
0 ignored issues
show
It seems like $this->_request->getPost('recovery') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Toggle::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
144 4
			'captcha' => $numberFilter->sanitize($this->_request->getPost('captcha'))
0 ignored issues
show
It seems like $this->_request->getPost('captcha') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Number::sanitize() does only seem to accept integer|string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

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