PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.8.16
VikBooking Hotel Booking Engine & PMS v1.8.16
1.8.16 1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 All 37 releases
← All changes | admin/helpers/src/mail/wrapper.php +453 -0 1.6.3 → 1.8.16 View file →
@@ -1,0 +1,453 @@
1 +<?php
2 +/**
3 + * @package VikBooking
4 + * @subpackage core
5 + * @author E4J s.r.l.
6 + * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 + * @link https://vikwp.com
9 + */
10 +
11 +// No direct access
12 +defined('ABSPATH') or die('No script kiddies please!');
13 +
14 +/**
15 + * Encapsulation of the e-mail data.
16 + *
17 + * - Bind from array/object:
18 + * new VBOMailWrapper([
19 + * 'sender' => '[email protected]',
20 + * 'recipient' => '[email protected]',
21 + * 'subject' => 'E-mail subject',
22 + * 'content' => '<p>This is the e-mail message.</p>',
23 + * ]);
24 + *
25 + * - or:
26 + * VBOMailWrapper::getInstance()->bind([...]);
27 + *
28 + * - Construct through setters:
29 + * VBOMailWrapper::getInstance()
30 + * ->setSender('[email protected]', 'Sender Name')
31 + * ->addRecipient('[email protected]')
32 + * ->setSubject('E-mail subject')
33 + * ->setContent('<p>This is the e-mail message.</p>');
34 + *
35 + * @since 1.5
36 + */
37 +final class VBOMailWrapper
38 +{
39 + /**
40 + * Holds the sender name and e-mail address.
41 + *
42 + * @var array|null
43 + */
44 + private $sender;
45 +
46 + /**
47 + * A list of recipients e-mail addresses.
48 + *
49 + * @var array
50 + */
51 + private $recipient = [];
52 +
53 + /**
54 + * The reply-to e-mail address.
55 + *
56 + * @var string|null
57 + */
58 + private $reply;
59 +
60 + /**
61 + * The BCC e-mail addresses.
62 + *
63 + * @var array
64 + */
65 + private $bcc = [];
66 +
67 + /**
68 + * The e-mail subject.
69 + *
70 + * @var string
71 + */
72 + private $subject;
73 +
74 + /**
75 + * The e-mail content.
76 + *
77 + * @var string
78 + */
79 + private $content;
80 +
81 + /**
82 + * A list of attachments.
83 + *
84 + * @var array
85 + */
86 + private $attachments = [];
87 +
88 + /**
89 + * Flag used to check whether the e-mail is HTML or plain text.
90 + *
91 + * @var bool|null
92 + */
93 + private $isHtml;
94 +
95 + /**
96 + * Proxy used to immediately support chaining.
97 + *
98 + * @param array|object $data The e-mail data to bind.
99 + *
100 + * @return self
101 + */
102 + public static function getInstance($data = [])
103 + {
104 + return new static($data);
105 + }
106 +
107 + /**
108 + * Class constructor.
109 + *
110 + * @param array|object $data The e-mail data to bind.
111 + */
112 + public function __construct($data = [])
113 + {
114 + // create the e-mail wrapper
115 + $this->bind($data);
116 + }
117 +
118 + /**
119 + * Binds the e-mail data.
120 + *
121 + * @param array|object $data The e-mail data to bind.
122 + *
123 + * @return self This object to support chaining.
124 + */
125 + public function bind($data)
126 + {
127 + if (!is_array($data) && !is_object($data))
128 + {
129 + // cannot accept the received argument
130 + throw new InvalidArgumentException(sprintf('Mail wrapper only accepts arrays or objects, %s given', gettype($data)), 406);
131 + }
132 +
133 + foreach ($data as $k => $v)
134 + {
135 + $k = strtolower($k);
136 +
137 + switch ($k)
138 + {
139 + case 'ishtml':
140 + case 'is_html':
141 + $method = 'setHtml';
142 + break;
143 +
144 + default:
145 + $method = 'set' . ucfirst($k);
146 + }
147 +
148 + if (method_exists($this, $method))
149 + {
150 + // invoke default setter
151 + call_user_func_array([$this, $method], [$v]);
152 + }
153 + }
154 +
155 + return $this;
156 + }
157 +
158 + /**
159 + * Sets the e-mail sender.
160 + *
161 + * @param string $address The sender e-mail address.
162 + * @param string $name The sender name.
163 + *
164 + * @return self This object to support chaining.
165 + */
166 + public function setSender($address, $name = null)
167 + {
168 + if (is_array($address))
169 + {
170 + if (count($address) > 1)
171 + {
172 + // extract name from array
173 + $name = array_pop($address);
174 + }
175 +
176 + // extract address from array
177 + $address = array_shift($address);
178 + }
179 +
180 + $this->sender = [
181 + 'address' => $address,
182 + 'name' => $address != $name ? $name : null,
183 + ];
184 +
185 + return $this;
186 + }
187 +
188 + /**
189 + * Returns the sender e-mail address.
190 + *
191 + * @return string|null
192 + */
193 + public function getSenderMail()
194 + {
195 + return !empty($this->sender['address']) ? $this->sender['address'] : VBOFactory::getConfig()->get('senderemail');
196 + }
197 +
198 + /**
199 + * Returns the sender name.
200 + *
201 + * @return string|null
202 + */
203 + public function getSenderName()
204 + {
205 + return !empty($this->sender['name']) ? $this->sender['name'] : VikBooking::getFrontTitle();
206 + }
207 +
208 + /**
209 + * Sets the e-mail recipient(s).
210 + *
211 + * @param string|array $address Either an array or a string.
212 + *
213 + * @return self This object to support chaining.
214 + */
215 + public function setRecipient($address)
216 + {
217 + // reset recipient
218 + $this->recipient = [];
219 +
220 + // add recipient(s)
221 + return $this->addRecipient($address);
222 + }
223 +
224 + /**
225 + * Adds a new recipient.
226 + *
227 + * @param string|array $address Either an array or a string.
228 + *
229 + * @return self This object to support chaining.
230 + */
231 + public function addRecipient($address)
232 + {
233 + foreach ((array) $address as $addr)
234 + {
235 + if ($addr)
236 + {
237 + $this->recipient[] = $addr;
238 + }
239 + }
240 +
241 + return $this;
242 + }
243 +
244 + /**
245 + * Returns the list of recipients.
246 + *
247 + * @return array
248 + */
249 + public function getRecipient()
250 + {
251 + return $this->recipient;
252 + }
253 +
254 + /**
255 + * Sets the reply-to e-mail address.
256 + *
257 + * @param string $address The reply-to e-mail address.
258 + *
259 + * @return self This object to support chaining.
260 + */
261 + public function setReply($address)
262 + {
263 + $this->reply = $address;
264 +
265 + return $this;
266 + }
267 +
268 + /**
269 + * Returns the reply-to e-mail address.
270 + *
271 + * @return string|null
272 + */
273 + public function getReply()
274 + {
275 + return $this->reply;
276 + }
277 +
278 + /**
279 + * Sets the e-mail BCC(s).
280 + *
281 + * @param string|array $address Either an array or a string.
282 + *
283 + * @return self This object to support chaining.
284 + */
285 + public function setBcc($address)
286 + {
287 + // reset BCC
288 + $this->bcc = [];
289 +
290 + // add BCC(s)
291 + return $this->addBcc($address);
292 + }
293 +
294 + /**
295 + * Adds a new BCC e-mail address.
296 + *
297 + * @param string|array $address Either an array or a string.
298 + *
299 + * @return self This object to support chaining.
300 + */
301 + public function addBcc($address)
302 + {
303 + foreach ((array) $address as $addr)
304 + {
305 + if ($addr)
306 + {
307 + $this->bcc[] = $addr;
308 + }
309 + }
310 +
311 + return $this;
312 + }
313 +
314 + /**
315 + * Returns the list of BCC(s).
316 + *
317 + * @return array
318 + */
319 + public function getBcc()
320 + {
321 + return $this->bcc;
322 + }
323 +
324 + /**
325 + * Sets the e-mail subject.
326 + *
327 + * @param string $subject The e-mail subject.
328 + *
329 + * @return self This object to support chaining.
330 + */
331 + public function setSubject($subject)
332 + {
333 + $this->subject = $subject;
334 +
335 + return $this;
336 + }
337 +
338 + /**
339 + * Returns the e-mail subject.
340 + *
341 + * @return string|null
342 + */
343 + public function getSubject()
344 + {
345 + return $this->subject;
346 + }
347 +
348 + /**
349 + * Sets the e-mail content.
350 + *
351 + * @param string $content The e-mail content.
352 + *
353 + * @return self This object to support chaining.
354 + */
355 + public function setContent($content)
356 + {
357 + $this->content = $content;
358 +
359 + return $this;
360 + }
361 +
362 + /**
363 + * Returns the e-mail content.
364 + *
365 + * @return string|null
366 + */
367 + public function getContent()
368 + {
369 + return $this->content;
370 + }
371 +
372 + /**
373 + * Sets the e-mail attachments.
374 + *
375 + * @param array $attachments An array of files.
376 + *
377 + * @return self This object to support chaining.
378 + */
379 + public function setAttachments($attachments = null)
380 + {
381 + // reset attachments
382 + $this->attachments = [];
383 +
384 + if (empty($attachments))
385 + {
386 + return $this;
387 + }
388 +
389 + // add attachments
390 + return $this->addAttachment($attachments);
391 + }
392 +
393 + /**
394 + * Registers a new attachment file.
395 + *
396 + * @param string|array $attachment Either an array or a string.
397 + *
398 + * @return self This object to support chaining.
399 + */
400 + public function addAttachment($attachment)
401 + {
402 + foreach ((array) $attachment as $file)
403 + {
404 + if ($file && is_file($file) && !in_array($file, $this->attachments))
405 + {
406 + $this->attachments[] = $file;
407 + }
408 + }
409 +
410 + return $this;
411 + }
412 +
413 + /**
414 + * Returns the list of attachments.
415 + *
416 + * @return array
417 + */
418 + public function getAttachments()
419 + {
420 + return $this->attachments;
421 + }
422 +
423 + /**
424 + * Sets the document type of the e-mail.
425 + *
426 + * @param bool|null $is True to use text/html, false to use
427 + * text/plain, null for auto-detection.
428 + *
429 + * @return self This object to support chaining.
430 + */
431 + public function setHtml($is = true)
432 + {
433 + $this->isHtml = is_null($is) ? null : (bool) $is;
434 +
435 + return $this;
436 + }
437 +
438 + /**
439 + * Checks whether the document type is text/html or not.
440 + *
441 + * @return bool True for text/html, false for text/plain.
442 + */
443 + public function isHtml()
444 + {
445 + if (is_null($this->isHtml))
446 + {
447 + // auto-detect whether the content supports HTML tags
448 + return preg_match("/<\/?[a-z0-9_\-]+\s*\/?>/i", (string) $this->content);
449 + }
450 +
451 + return $this->isHtml;
452 + }
453 +}