Completed
Push — master ( 4eb4a8...a52438 )
by Henry
07:48
created

modules/Contact/Controller.php (1 issue)

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\Modules\Contact;
3
4
use Redaxscript\Controller\ControllerAbstract;
5
use Redaxscript\Filter;
6
use Redaxscript\Html;
7
use Redaxscript\Mailer;
8
use Redaxscript\Model;
9
use Redaxscript\Validator;
10
11
/**
12
 * children class to process the contact request
13
 *
14
 * @since 4.0.0
15
 *
16
 * @package Redaxscript
17
 * @category Modules
18
 * @author Henry Ruhs
19
 */
20
21
class Controller extends ControllerAbstract
22
{
23
	/**
24
	 * process
25
	 *
26
	 * @since 4.0.0
27
	 *
28
	 * @return string
29
	 */
30
31
	public function process() : string
32
	{
33
		$postArray = $this->_normalizePost($this->_sanitizePost());
34
		$validateArray = $this->_validatePost($postArray);
35
36
		/* handle validate */
37
38
		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...
39
		{
40
			return $this->_error(
41
			[
42
				'message' => $validateArray
43
			]);
44
		}
45
46
		/* handle mail */
47
48
		$mailArray =
49
		[
50
			'author' => $postArray['author'],
51
			'email' => $postArray['email'],
52
			'url' => $postArray['url'],
53
			'text' => $postArray['text']
54
		];
55
		if ($this->_mail($mailArray))
56
		{
57
			return $this->_success(
58
			[
59
				'route' => $this->_registry->get('liteRoute'),
60
				'timeout' => 2,
61
				'message' => $this->_language->get('_contact')['message_sent']
62
			]);
63
		}
64
65
		/* handle error */
66
67
		return $this->_error(
68
		[
69
			'message' => $this->_language->get('email_failed')
70
		]);
71
	}
72
73
	/**
74
	 * sanitize the post
75
	 *
76
	 * @since 4.0.0
77
	 *
78
	 * @return array
79
	 */
80
81
	protected function _sanitizePost() : array
82
	{
83
		$htmlFilter = new Filter\Html();
84
		$emailFilter = new Filter\Email();
85
		$textFilter = new Filter\Text();
86
		$numberFilter = new Filter\Number();
87
		$urlFilter = new Filter\Url();
88
89
		/* sanitize post */
90
91
		return
92
		[
93
			'author' => $textFilter->sanitize($this->_request->getPost('author')),
94
			'email' => $emailFilter->sanitize($this->_request->getPost('email')),
95
			'url' => $urlFilter->sanitize($this->_request->getPost('url')),
96
			'text' => $htmlFilter->sanitize($this->_request->getPost('text')),
97
			'task' => $numberFilter->sanitize($this->_request->getPost('task')),
98
			'solution' => $numberFilter->sanitize($this->_request->getPost('solution'))
99
		];
100
	}
101
102
	/**
103
	 * validate
104
	 *
105
	 * @since 4.0.0
106
	 *
107
	 * @param array $postArray array of the post
108
	 *
109
	 * @return array
110
	 */
111
112
	protected function _validatePost(array $postArray = []) : array
113
	{
114
		$emailValidator = new Validator\Email();
115
		$nameValidator = new Validator\Name();
116
		$urlValidator = new Validator\Url();
117
		$captchaValidator = new Validator\Captcha();
118
		$settingModel = new Model\Setting();
119
		$validateArray = [];
120
121
		/* validate post */
122
123
		if (!$postArray['author'])
124
		{
125
			$validateArray[] = $this->_language->get('author_empty');
126
		}
127
		else if (!$nameValidator->validate($postArray['author']))
128
		{
129
			$validateArray[] = $this->_language->get('author_incorrect');
130
		}
131
		if (!$postArray['email'])
132
		{
133
			$validateArray[] = $this->_language->get('email_empty');
134
		}
135
		else if (!$emailValidator->validate($postArray['email']))
136
		{
137
			$validateArray['email'] = $this->_language->get('email_incorrect');
138
		}
139
		if ($postArray['url'] && !$urlValidator->validate($postArray['url']))
140
		{
141
			$validateArray[] = $this->_language->get('url_incorrect');
142
		}
143
		if (!$postArray['text'])
144
		{
145
			$validateArray[] = $this->_language->get('message_empty');
146
		}
147
		if ($settingModel->get('captcha') > 0 && !$captchaValidator->validate($postArray['task'], $postArray['solution']))
148
		{
149
			$validateArray[] = $this->_language->get('captcha_incorrect');
150
		}
151
		return $validateArray;
152
	}
153
154
	/**
155
	 * mail
156
	 *
157
	 * @since 4.0.0
158
	 *
159
	 * @param array $mailArray
160
	 *
161
	 * @return bool
162
	 */
163
164
	protected function _mail(array $mailArray = []) : bool
165
	{
166
		$settingModel = new Model\Setting();
167
168
		/* html element */
169
170
		$element = new Html\Element();
171
		$linkEmail = $element
172
			->copy()
173
			->init('a',
174
			[
175
				'href' => 'mailto:' . $mailArray['email']
176
			])
177
			->text($mailArray['email']);
178
		$linkUrl = $element
179
			->copy()
180
			->init('a',
181
			[
182
				'href' => $mailArray['url']
183
			])
184
			->text($mailArray['url'] ? : $this->_language->get('none'));
185
186
		/* prepare mail */
187
188
		$toArray =
189
		[
190
			$settingModel->get('author') => $settingModel->get('email')
191
		];
192
		$fromArray =
193
		[
194
			$mailArray['author'] => $mailArray['email']
195
		];
196
		$subject = $this->_language->get('contact');
197
		$bodyArray =
198
		[
199
			$this->_language->get('author') . $this->_language->get('colon') . ' ' . $mailArray['author'],
200
			'<br />',
201
			$this->_language->get('email') . $this->_language->get('colon') . ' ' . $linkEmail,
202
			'<br />',
203
			$this->_language->get('url') . $this->_language->get('colon') . ' ' . $linkUrl,
204
			'<br />',
205
			$this->_language->get('message') . $this->_language->get('colon') . ' ' . $mailArray['text']
206
		];
207
208
		/* send mail */
209
210
		$mailer = new Mailer();
211
		$mailer->init($toArray, $fromArray, $subject, $bodyArray);
212
		return $mailer->send();
213
	}
214
}
215