Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/fluent-smtp/app/Services/Mailer/BaseHandler.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 +
3 + namespace FluentMail\App\Services\Mailer;
4 +
5 + use Exception;
6 + use InvalidArgumentException;
7 + use FluentMail\App\Models\Logger;
8 + use FluentMail\Includes\Support\Arr;
9 + use FluentMail\Includes\Core\Application;
10 + use FluentMail\App\Services\Mailer\Manager;
11 + use FluentMail\App\Services\Mailer\ValidatorTrait;
12 +
13 + class BaseHandler
14 + {
15 + use ValidatorTrait;
16 +
17 + protected $app = null;
18 +
19 + protected $params = [];
20 +
21 + protected $manager = null;
22 +
23 + protected $phpMailer = null;
24 +
25 + protected $settings = [];
26 +
27 + protected $attributes = [];
28 +
29 + protected $response = null;
30 +
31 + protected $existing_row_id = null;
32 +
33 + public function __construct(?Application $app = null, ?Manager $manager = null)
34 + {
35 + $this->app = $app ?: fluentMail();
36 + $this->manager = $manager ?: fluentMail(Manager::class);
37 + }
38 +
39 + public function setPhpMailer($phpMailer)
40 + {
41 + $this->phpMailer = $phpMailer;
42 +
43 + if(!$this->phpMailer->CharSet) {
44 + $this->phpMailer->CharSet = 'UTF-8';
45 + }
46 +
47 + return $this;
48 + }
49 +
50 + public function setSettings($settings)
51 + {
52 +
53 + $this->settings = $settings;
54 +
55 + return $this;
56 + }
57 +
58 + protected function preSend()
59 + {
60 + $this->attributes = [];
61 +
62 + if ($this->isForced('from_name')) {
63 + $this->phpMailer->FromName = $this->getSetting('sender_name');
64 + }
65 +
66 + if ($this->getSetting('return_path') == 'yes') {
67 + $this->phpMailer->Sender = $this->phpMailer->From;
68 + }
69 +
70 + $this->attributes = $this->setAttributes();
71 +
72 + return true;
73 + }
74 +
75 + protected function isForced($key)
76 + {
77 + return $this->getSetting("force_{$key}") == 'yes';
78 + }
79 +
80 + public function isForcedEmail()
81 + {
82 + return $this->getSetting("force_from_email") != 'no';
83 + }
84 +
85 + public function isActive()
86 + {
87 + return $this->getSetting('is_active') == 'yes';
88 + }
89 +
90 + protected function getDefaultParams()
91 + {
92 + $timeout = (int)ini_get('max_execution_time');
93 +
94 + return [
95 + 'timeout' => $timeout ?: 30,
96 + 'httpversion' => '1.1',
97 + 'blocking' => true,
98 + ];
99 + }
100 +
101 + protected function setAttributes()
102 + {
103 + $from = $this->setFrom();
104 +
105 + $replyTos = $this->setRecipientsArray(array_values(
106 + $this->phpMailer->getReplyToAddresses()
107 + ));
108 +
109 + $contentType = $this->phpMailer->ContentType;
110 +
111 + $customHeaders = $this->setFormattedCustomHeaders();
112 +
113 + $recipients = [
114 + 'to' => $this->setRecipientsArray($this->phpMailer->getToAddresses()),
115 + 'cc' => $this->setRecipientsArray($this->phpMailer->getCcAddresses()),
116 + 'bcc' => $this->setRecipientsArray($this->phpMailer->getBccAddresses())
117 + ];
118 +
119 + return array_merge($this->attributes, [
120 + 'from' => $from,
121 + 'to' => $recipients['to'],
122 + 'subject' => $this->phpMailer->Subject,
123 + 'message' => $this->phpMailer->Body,
124 + 'alt_body' => $this->phpMailer->AltBody,
125 + 'attachments' => $this->phpMailer->getAttachments(),
126 + 'custom_headers' => $customHeaders,
127 + 'headers' => [
128 + 'reply-to' => $replyTos,
129 + 'cc' => $recipients['cc'],
130 + 'bcc' => $recipients['bcc'],
131 + 'content-type' => $contentType
132 + ]
133 + ]);
134 + }
135 +
136 + protected function setFrom()
137 + {
138 + $name = $this->getSetting('sender_name');
139 + $email = $this->getSetting('sender_email');
140 + $overrideName = $this->getSetting('force_from_name');
141 +
142 + if ($name && ($overrideName == 'yes' || $this->phpMailer->FromName == 'WordPress')) {
143 + $this->attributes['sender_name'] = $name;
144 + $this->attributes['sender_email'] = $email;
145 + $from = $name . ' <' . $email . '>';
146 + } elseif ($this->phpMailer->FromName) {
147 + $this->attributes['sender_email'] = $email;
148 + $this->attributes['sender_name'] = $this->phpMailer->FromName;
149 + $from = $this->phpMailer->FromName . ' <' . $email . '>';
150 + } else {
151 + $from = $this->attributes['sender_email'] = $email;
152 + }
153 +
154 + return $from;
155 + }
156 +
157 + protected function setRecipientsArray($array)
158 + {
159 + $recipients = [];
160 +
161 + foreach ($array as $key => $recipient) {
162 + $recipient = array_filter($recipient);
163 +
164 + if (!$recipient) continue;
165 +
166 + $recipients[$key] = [
167 + 'email' => array_shift($recipient)
168 + ];
169 +
170 + if ($recipient) {
171 + $recipients[$key]['name'] = array_shift($recipient);
172 + }
173 + }
174 +
175 + return $recipients;
176 + }
177 +
178 + protected function setFormattedCustomHeaders()
179 + {
180 + $headers = [];
181 +
182 + $customHeaders = $this->phpMailer->getCustomHeaders();
183 +
184 + foreach ($customHeaders as $key => $header) {
185 + if ($header[0] == 'Return-Path') {
186 + if ($this->getSetting('options.return_path') == 'no') {
187 + if (!empty($header[1])) {
188 + $this->phpMailer->Sender = $header[1];
189 + }
190 + }
191 + unset($customHeaders[$key]);
192 + } else {
193 + $headers[] = [
194 + 'key' => $header[0],
195 + 'value' => $header[1]
196 + ];
197 + }
198 + }
199 +
200 + $this->phpMailer->clearCustomHeaders();
201 +
202 + foreach ($customHeaders as $customHeader) {
203 + $this->phpMailer->addCustomHeader($customHeader[0], $customHeader[1]);
204 + }
205 +
206 + return $headers;
207 + }
208 +
209 + public function getSetting($key = null, $default = null)
210 + {
211 + try {
212 + return $key ? Arr::get($this->settings, $key, $default) : $this->settings;
213 + } catch (Exception $e) {
214 + return $default;
215 + }
216 + }
217 +
218 + protected function getParam($key = null, $default = null)
219 + {
220 + try {
221 + return $key ? Arr::get($this->attributes, $key, $default) : $this->attributes;
222 + } catch (Exception $e) {
223 + return $default;
224 + }
225 + }
226 +
227 + protected function getHeader($key, $default = null)
228 + {
229 + try {
230 + return Arr::get(
231 + $this->attributes['headers'], $key, $default
232 + );
233 + } catch (Exception $e) {
234 + return $default;
235 + }
236 + }
237 +
238 + public function getSubject()
239 + {
240 + $subject = '';
241 +
242 + if (isset($this->attributes['subject'])) {
243 + $subject = $this->attributes['subject'];
244 + }
245 +
246 + return $subject;
247 + }
248 +
249 + protected function getExtraParams()
250 + {
251 + $this->attributes['extra']['provider'] = $this->getSetting('provider');
252 +
253 + return $this->attributes['extra'];
254 + }
255 +
256 + public function handleResponse($response)
257 + {
258 + if ( is_wp_error($response) ) {
259 + $code = $response->get_error_code();
260 +
261 + if (!is_numeric($code)) {
262 + $code = 400;
263 + }
264 +
265 + $message = $response->get_error_message();
266 +
267 + $errorResponse = [
268 + 'code' => $code,
269 + 'message' => $message,
270 + 'errors' => $response->get_error_data()
271 + ];
272 +
273 + $status = $this->processResponse($errorResponse, false);
274 +
275 + if ( !$status ) {
276 + throw new \PHPMailer\PHPMailer\Exception($message, $code); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
277 + } else {
278 + return $status;
279 + }
280 +
281 + } else {
282 + return $this->processResponse($response, true);
283 + }
284 + }
285 +
286 + public function processResponse($response, $status)
287 + {
288 + if ($this->shouldBeLogged($status)) {
289 + $data = [
290 + 'to' => $this->serialize($this->attributes['to']),
291 + 'from' => $this->attributes['from'],
292 + 'subject' => sanitize_text_field($this->attributes['subject']),
293 + 'body' => $this->attributes['message'],
294 + 'attachments' => $this->serialize($this->attributes['attachments']),
295 + 'status' => $status ? 'sent' : 'failed',
296 + 'response' => $this->serialize($response),
297 + 'headers' => $this->serialize($this->getParam('headers')),
298 + 'extra' => $this->serialize($this->getExtraParams())
299 + ];
300 +
301 + if($this->existing_row_id) {
302 + $row = (new Logger())->find($this->existing_row_id);
303 + if($row) {
304 + $row['response'] = (array) $row['response'];
305 + if($status) {
306 + $row['response']['fallback'] = __('Sent using fallback connection ', 'fluent-smtp') . $this->attributes['from'];
307 + $row['response']['fallback_response'] = $response;
308 + } else {
309 + $row['response']['fallback'] = __('Tried to send using fallback but failed. ', 'fluent-smtp') . $this->attributes['from'];
310 + $row['response']['fallback_response'] = $response;
311 + }
312 +
313 + $data['response'] = $this->serialize( $row['response']);
314 + $data['retries'] = $row['retries'] + 1;
315 + (new Logger())->updateLog($data, ['id' => $row['id']]);
316 +
317 + if(!$status) {
318 + do_action('fluentmail_email_sending_failed_no_fallback', $row['id'], $this, $data);
319 + }
320 + }
321 + } else {
322 + $logId = (new Logger)->add($data);
323 + if(!$status) {
324 + // We have to fire an action for this failed job
325 + $status = apply_filters('fluentmail_email_sending_failed', $status, $logId, $this, $data);
326 + }
327 + }
328 + }
329 +
330 + return $status;
331 + }
332 +
333 + protected function shouldBeLogged($status)
334 + {
335 + if($this->existing_row_id) {
336 + return true;
337 + }
338 + if (defined('FLUENTMAIL_LOG_OFF') && FLUENTMAIL_LOG_OFF) {
339 + return false;
340 + }
341 +
342 + if (!$status) {
343 + return true;
344 + }
345 +
346 + $miscSettings = $this->manager->getConfig('misc');
347 + $isLogOn = $miscSettings['log_emails'] == 'yes';
348 +
349 + return apply_filters('fluentmail_will_log_email', $isLogOn, $miscSettings, $this);
350 + }
351 +
352 + protected function serialize(array $data)
353 + {
354 + foreach ($data as $key => $item) {
355 +
356 + if (is_array($item)) {
357 + $this->serialize($item);
358 + }
359 +
360 + if (is_object($item) || is_resource($item)) {
361 + throw new InvalidArgumentException(
362 + "Invalid Data: Array cannot contain an object or resource."
363 + );
364 + }
365 +
366 + if (is_string($item)) {
367 + if (is_serialized($item)) {
368 + throw new InvalidArgumentException(
369 + "Invalid Data: Array cannot contain serialized data."
370 + );
371 + }
372 +
373 + if (filter_var($item, FILTER_VALIDATE_EMAIL)) {
374 + $data[$key] = sanitize_email($item);
375 + } elseif (filter_var($item, FILTER_VALIDATE_URL)) {
376 + $data[$key] = esc_url_raw($item);
377 + } else {
378 + $data[$key] = sanitize_text_field($item);
379 + }
380 + }
381 + }
382 +
383 + return serialize($data);
384 + }
385 +
386 + protected function fireWPMailFailedAction($data)
387 + {
388 + $code = is_numeric($data['code']) ? $data['code'] : 400;
389 + $code = strlen($code) < 3 ? 400 : $code;
390 +
391 + $mail_error_data['phpmailer_exception_code'] = $code;
392 + $mail_error_data['errors'] = $data['errors'];
393 +
394 + $error = new \WP_Error(
395 + $code, $data['message'], $mail_error_data
396 + );
397 +
398 + $this->app->doAction('wp_mail_failed', $error);
399 + }
400 +
401 + protected function updatedLog($id, $data)
402 + {
403 + try {
404 + $data['updated_at'] = current_time('mysql');
405 + (new Logger)->updateLog($data, ['id' => $id]);
406 + } catch (Exception $e) {
407 + error_log($e->getMessage());
408 + }
409 + }
410 +
411 + public function getValidSenders($connection)
412 + {
413 + return [$connection['sender_email']];
414 + }
415 +
416 + public function checkConnection($connection)
417 + {
418 + return true;
419 + }
420 +
421 + public function getConnectionInfo($connection)
422 + {
423 + return [
424 + 'info' => (string) fluentMail('view')->make('admin.general_connection_info', [
425 + 'connection' => $connection
426 + ])
427 + ];
428 + }
429 +
430 + public function getPhpMailer()
431 + {
432 + return $this->phpMailer;
433 + }
434 +
435 + public function setRowId($id)
436 + {
437 + $this->existing_row_id = $id;
438 + }
439 +
440 + public function addNewSenderEmail($connection, $email)
441 + {
442 + return new \WP_Error('not_implemented', __('Not implemented', 'fluent-smtp'));
443 + }
444 +
445 + public function removeSenderEmail($connection, $email)
446 + {
447 + return new \WP_Error('not_implemented', __('Not implemented', 'fluent-smtp'));
448 + }
449 +
450 + }
451 +