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