Completed
Push — master ( 96a033...7625bb )
by Henry
07:07
created

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

Labels
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
7
/**
8
 * children class to process the admin setting request
9
 *
10
 * @since 4.0.0
11
 *
12
 * @package Redaxscript
13
 * @category Controller
14
 * @author Henry Ruhs
15
 */
16
17
class Setting extends ControllerAbstract
18
{
19
	/**
20
	 * process the class
21
	 *
22
	 * @since 4.0.0
23
	 *
24
	 * @param string $action action to process
25
	 *
26
	 * @return string
27
	 */
28
29
	public function process(string $action = null) : string
30
	{
31
		$postArray = $this->_normalizePost($this->_sanitizePost());
32
		$validateArray = $this->_validatePost($postArray);
33
34
		/* validate post */
35
36
		if ($validateArray)
37
		{
38
			return $this->_error(
39
			[
40
				'route' => $this->_getErrorRoute(),
41
				'message' => $validateArray
42
			]);
43
		}
44
45
		/* handle update */
46
47
		if ($action === 'update')
48
		{
49
			$updateArray =
50
			[
51
				'language' => $postArray['language'],
52
				'template' => $postArray['template'],
53
				'title' => $postArray['title'],
54
				'author' => $postArray['author'],
55
				'copyright' => $postArray['copyright'],
56
				'description' => $postArray['description'],
57
				'keywords' => $postArray['keywords'],
58
				'robots' => $postArray['robots'],
59
				'email' => $postArray['email'],
60
				'subject' => $postArray['subject'],
61
				'notification' => $postArray['notification'],
62
				'charset' => $postArray['charset'],
63
				'divider' => $postArray['divider'],
64
				'zone' => $postArray['zone'],
65
				'time' => $postArray['time'],
66
				'date' => $postArray['date'],
67
				'homepage' => $postArray['homepage'],
68
				'limit' => $postArray['limit'],
69
				'order' => $postArray['order'],
70
				'pagination' => $postArray['pagination'],
71
				'moderation' => $postArray['moderation'],
72
				'registration' => $postArray['registration'],
73
				'verification' => $postArray['verification'],
74
				'recovery' => $postArray['recovery'],
75
				'captcha' => $postArray['captcha']
76
			];
77
			if ($this->_update($updateArray))
78
			{
79
				return $this->_success(
80
				[
81
					'route' => 'admin',
82
					'timeout' => 2
83
				]);
84
			}
85
		}
86
87
		/* handle error */
88
89
		return $this->_error(
90
		[
91
			'route' => $this->_getErrorRoute()
92
		]);
93
	}
94
95
	/**
96
	 * sanitize the post
97
	 *
98
	 * @since 4.0.0
99
	 *
100
	 * @return array
101
	 */
102
103
	protected function _sanitizePost() : array
104
	{
105
		$emailFilter = new Filter\Email();
106
		$numberFilter = new Filter\Number();
107
		$specialFilter = new Filter\Special();
108
		$toggleFilter = new Filter\Toggle();
109
110
		/* sanitize post */
111
112
		return
113
		[
114
			'language' => $specialFilter->sanitize($this->_request->getPost('language')),
115
			'template' => $specialFilter->sanitize($this->_request->getPost('template')),
116
			'title' => $this->_request->getPost('title'),
117
			'author' => $this->_request->getPost('author'),
118
			'copyright' => $this->_request->getPost('copyright'),
119
			'description' => $this->_request->getPost('description'),
120
			'keywords' => $this->_request->getPost('keywords'),
121
			'robots' => $this->_request->getPost('robots'),
122
			'email' => $emailFilter->sanitize($this->_request->getPost('email')),
123
			'subject' => $this->_request->getPost('subject'),
124
			'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...
125
			'charset' => $this->_request->getPost('charset'),
126
			'divider' => $this->_request->getPost('divider'),
127
			'zone' => $this->_request->getPost('zone'),
128
			'time' => $this->_request->getPost('time'),
129
			'date' => $this->_request->getPost('date'),
130
			'homepage' => $this->_request->getPost('homepage'),
131
			'limit' => $numberFilter->sanitize($this->_request->getPost('limit')),
132
			'order' => $specialFilter->sanitize($this->_request->getPost('order')),
133
			'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...
134
			'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...
135
			'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...
136
			'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...
137
			'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...
138
			'captcha' => $numberFilter->sanitize($this->_request->getPost('captcha'))
139
		];
140
	}
141
142
	/**
143
	 * validate the post
144
	 *
145
	 * @since 4.0.0
146
	 *
147
	 * @param array $postArray array of the post
148
	 *
149
	 * @return array
150
	 */
151
152
	protected function _validatePost(array $postArray = []) : array
153
	{
154
		$validateArray = [];
155
156
		/* validate post */
157
158
		if (!$postArray['charset'] || !$postArray['limit'])
159
		{
160
			$validateArray[] = $this->_language->get('input_empty');
161
		}
162
		return $validateArray;
163
	}
164
165
	/**
166
	 * update the setting
167
	 *
168
	 * @since 4.0.0
169
	 *
170
	 * @param array $updateArray array of the update
171
	 *
172
	 * @return bool
173
	 */
174
175
	protected function _update(array $updateArray = []) : bool
176
	{
177
		$settingModel = new Admin\Model\Setting();
178
		return $settingModel->updateByArray($updateArray);
179
	}
180
181
	/**
182
	 * get error route
183
	 *
184
	 * @since 4.1.0
185
	 *
186
	 * @return string
187
	 */
188
189
	protected function _getErrorRoute() : string
190
	{
191
		if ($this->_registry->get('settingsEdit'))
192
		{
193
			return 'admin/edit/settings';
194
		}
195
		return 'admin';
196
	}
197
}
198