Completed
Push — master ( d10e6f...1f9c45 )
by Henry
08:18
created

Contact::routeHeader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
namespace Redaxscript\Modules\Contact;
3
4
use Redaxscript\Html;
5
use Redaxscript\Filter;
6
use Redaxscript\Mailer;
7
use Redaxscript\Messenger;
8
use Redaxscript\Model;
9
use Redaxscript\Module;
10
use Redaxscript\Validator;
11
12
/**
13
 * simple contact form
14
 *
15
 * @since 2.6.0
16
 *
17
 * @package Redaxscript
18
 * @category Modules
19
 * @author Henry Ruhs
20
 */
21
class Contact extends Module\Module
22
{
23
	/**
24
	 * array of the module
25
	 *
26
	 * @var array
27
	 */
28
29
	protected static $_moduleArray =
30
	[
31
		'name' => 'Contact',
32
		'alias' => 'Contact',
33
		'author' => 'Redaxmedia',
34
		'description' => 'Simple contact form',
35
		'version' => '3.3.0'
36
	];
37
38
	/**
39
	 * routeHeader
40
	 *
41
	 * @since 3.3.0
42
	 */
43
44
	public function routeHeader()
45
	{
46
		if ($this->_request->getPost(get_class()) === 'submit')
47
		{
48
			$this->_request->set('routerBreak', true);
49
		}
50
	}
51
52
	/**
53
	 * routeContent
54
	 *
55
	 * @since 3.3.0
56
	 */
57
58
	public function routeContent()
59
	{
60
		if ($this->_request->getPost(get_class()) === 'submit')
61
		{
62
			echo $this->process();
63
		}
64
	}
65
66
	/**
67
	 * render
68
	 *
69
	 * @since 2.6.0
70
	 *
71
	 * @return string
72
	 */
73
74
	public function render() : string
75
	{
76
		$settingModel = new Model\Setting();
77
		$formElement = new Html\Form($this->_registry, $this->_language);
78
		$formElement->init(
79
		[
80
			'textarea' =>
81
			[
82
				'class' => 'rs-js-auto-resize rs-js-editor-textarea rs-field-textarea'
83
			],
84
			'button' =>
85
			[
86
				'submit' =>
87
				[
88
					'name' => get_class()
89
				]
90
			]
91
		],
92
		[
93
			'captcha' => $settingModel->get('captcha') > 0
94
		]);
95
96
		/* create the form */
97
98
		$formElement
99
			->append('<fieldset>')
100
			->legend()
101
			->append('<ul><li>')
102
			->label('* ' . $this->_language->get('author'),
103
			[
104
				'for' => 'author'
105
			])
106
			->text(
107
			[
108
				'id' => 'author',
109
				'name' => 'author',
110
				'readonly' => $this->_registry->get('myName') ? 'readonly' : null,
111
				'required' => 'required',
112
				'value' => $this->_registry->get('myName')
113
			])
114
			->append('</li><li>')
115
			->label('* ' . $this->_language->get('email'),
116
			[
117
				'for' => 'email'
118
			])
119
			->email(
120
			[
121
				'id' => 'email',
122
				'name' => 'email',
123
				'readonly' => $this->_registry->get('myEmail') ? 'readonly' : null,
124
				'required' => 'required',
125
				'value' => $this->_registry->get('myEmail')
126
			])
127
			->append('</li><li>')
128
			->label($this->_language->get('url'),
129
			[
130
				'for' => 'url'
131
			])
132
			->url(
133
			[
134
				'id' => 'url',
135
				'name' => 'url'
136
			])
137
			->append('</li><li>')
138
			->label('* ' . $this->_language->get('message'),
139
			[
140
				'for' => 'text'
141
			])
142
			->textarea(
143
			[
144
				'id' => 'text',
145
				'name' => 'text',
146
				'required' => 'required'
147
			])
148
			->append('</li>');
149
		if ($settingModel->get('captcha') > 0)
150
		{
151
			$formElement
152
				->append('<li>')
153
				->captcha('task')
154
				->append('</li>');
155
		}
156
		$formElement->append('</ul></fieldset>');
157
		if ($settingModel->get('captcha') > 0)
158
		{
159
			$formElement->captcha('solution');
160
		}
161
		$formElement
162
			->token()
163
			->submit()
164
			->reset();
165
		return $formElement->render();
166
	}
167
168
	/**
169
	 * process
170
	 *
171
	 * @since 3.0.0
172
	 *
173
	 * @return string
174
	 */
175
176
	public function process()
177
	{
178
		$specialFilter = new Filter\Special();
179
		$emailFilter = new Filter\Email();
180
		$urlFilter = new Filter\Url();
181
		$htmlFilter = new Filter\Html();
182
183
		/* process post */
184
185
		$postArray =
186
		[
187
			'author' => $specialFilter->sanitize($this->_request->getPost('author')),
188
			'email' => $emailFilter->sanitize($this->_request->getPost('email')),
189
			'url' => $urlFilter->sanitize($this->_request->getPost('url')),
190
			'text' => nl2br($htmlFilter->sanitize($this->_request->getPost('text'))),
191
			'task' => $this->_request->getPost('task'),
192
			'solution' => $this->_request->getPost('solution')
193
		];
194
195
		/* handle error */
196
197
		$messageArray = $this->_validate($postArray);
198
		if ($messageArray)
0 ignored issues
show
Bug Best Practice introduced by
The expression $messageArray 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...
199
		{
200
			return $this->_error(
201
			[
202
				'message' => $messageArray
203
			]);
204
		}
205
206
		/* handle success */
207
208
		$mailArray =
209
		[
210
			'author' => $postArray['author'],
211
			'email' => $postArray['email'],
212
			'url' => $postArray['url'],
213
			'text' => $postArray['text']
214
		];
215
216
		/* mail */
217
218
		if ($this->_mail($mailArray))
219
		{
220
			return $this->_success();
221
		}
222
		return $this->_error(
223
		[
224
			'message' => $this->_language->get('something_wrong')
225
		]);
226
	}
227
228
	/**
229
	 * success
230
	 *
231
	 * @since 3.0.0
232
	 *
233
	 * @return string
234
	 */
235
236
	protected function _success()
237
	{
238
		$messenger = new Messenger($this->_registry);
239
		return $messenger
240
			->setUrl($this->_language->get('home'), $this->_registry->get('root'))
241
			->doRedirect()
242
			->success($this->_language->get('operation_completed'), $this->_language->get('message_sent', '_contact'));
243
	}
244
245
	/**
246
	 * error
247
	 *
248
	 * @since 3.0.0
249
	 *
250
	 * @param array $errorArray array of the error
251
	 *
252
	 * @return string
253
	 */
254
255
	protected function _error($errorArray = [])
256
	{
257
		$messenger = new Messenger($this->_registry);
258
		return $messenger
259
			->setUrl($this->_language->get('home'), $this->_registry->get('root'))
260
			->error($errorArray['message'], $this->_language->get('error_occurred'));
261
	}
262
263
	/**
264
	 * validate
265
	 *
266
	 * @since 3.0.0
267
	 *
268
	 * @param array $postArray array of the post
269
	 *
270
	 * @return array
271
	 */
272
273
	protected function _validate($postArray = [])
274
	{
275
		$emailValidator = new Validator\Email();
276
		$urlValidator = new Validator\Url();
277
		$captchaValidator = new Validator\Captcha();
278
		$settingModel = new Model\Setting();
279
280
		/* validate post */
281
282
		$messageArray = [];
283
		if (!$postArray['author'])
284
		{
285
			$messageArray[] = $this->_language->get('author_empty');
286
		}
287
		if (!$postArray['email'])
288
		{
289
			$messageArray[] = $this->_language->get('email_empty');
290
		}
291
		else if ($emailValidator->validate($postArray['email']) === Validator\ValidatorInterface::FAILED)
292
		{
293
			$messageArray['email'] = $this->_language->get('email_incorrect');
294
		}
295
		if ($postArray['url'] && $urlValidator->validate($postArray['url']) === Validator\ValidatorInterface::FAILED)
296
		{
297
			$messageArray[] = $this->_language->get('url_incorrect');
298
		}
299
		if (!$postArray['text'])
300
		{
301
			$messageArray[] = $this->_language->get('message_empty');
302
		}
303
		if ($settingModel->get('captcha') > 0 && $captchaValidator->validate($postArray['task'], $postArray['solution']) === Validator\ValidatorInterface::FAILED)
304
		{
305
			$messageArray[] = $this->_language->get('captcha_incorrect');
306
		}
307
		return $messageArray;
308
	}
309
310
	/**
311
	 * mail
312
	 *
313
	 * @since 3.0.0
314
	 *
315
	 * @param array $mailArray
316
	 *
317
	 * @return boolean
318
	 */
319
320
	protected function _mail($mailArray = [])
321
	{
322
		$settingModel = new Model\Setting();
323
324
		/* html elements */
325
326
		$linkElement = new Html\Element();
327
		$linkElement->init('a');
328
		$linkEmail = $linkElement->copy();
329
		$linkEmail
330
			->attr(
331
			[
332
				'href' => 'mailto:' . $mailArray['email']
333
			])
334
			->text($mailArray['email']);
335
		$linkUrl = $linkElement->copy();
336
		$linkUrl
337
			->attr(
338
			[
339
				'href' => $mailArray['url']
340
			])
341
			->text($mailArray['url'] ? $mailArray['url'] : $this->_language->get('none'));
342
343
		/* prepare mail */
344
345
		$toArray =
346
		[
347
			$settingModel->get('author') => $settingModel->get('email')
348
		];
349
		$fromArray =
350
		[
351
			$mailArray['author'] => $mailArray['email']
352
		];
353
		$subject = $this->_language->get('contact');
354
		$bodyArray =
355
		[
356
			$this->_language->get('author') . $this->_language->get('colon') . ' ' . $mailArray['author'],
357
			'<br />',
358
			$this->_language->get('email') . $this->_language->get('colon') . ' ' . $linkEmail,
359
			'<br />',
360
			$this->_language->get('url') . $this->_language->get('colon') . ' ' . $linkUrl,
361
			'<br />',
362
			$this->_language->get('message') . $this->_language->get('colon') . ' ' . $mailArray['text']
363
		];
364
365
		/* send mail */
366
367
		$mailer = new Mailer();
368
		$mailer->init($toArray, $fromArray, $subject, $bodyArray);
369
		return $mailer->send();
370
	}
371
}
372