3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2009-2014 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2009-2014 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @link http://galette.tuxfamily.org
34 * @since Available since 0.7dev - 2009-03-07
37 namespace Galette\Core
;
40 use Galette\Entity\Adherent
;
49 * @author Johan Cwiklinski <johan@x-tnd.be>
50 * @copyright 2009-2014 The Galette Team
51 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
52 * @link http://galette.tuxfamily.org
53 * @since Available since 0.7dev - 2009-03-07
55 class Mailing
extends GaletteMail
57 public const STEP_START
= 0;
58 public const STEP_PREVIEW
= 1;
59 public const STEP_SEND
= 2;
60 public const STEP_SENT
= 3;
62 public const MIME_HTML
= 'text/html';
63 public const MIME_TEXT
= 'text/plain';
64 public const MIME_DEFAULT
= self
::MIME_TEXT
;
68 private $unreachables = array();
69 private $mrecipients = array();
70 private $current_step;
80 * @param Preferences $preferences Preferences instance
81 * @param array $members An array of members
82 * @param int $id Identifier, defaults to null
84 public function __construct(Preferences
$preferences, array $members = [], int $id = null)
86 parent
::__construct($preferences);
87 $this->id
= $id ??
$this->generateNewId();
89 $this->current_step
= self
::STEP_START
;
90 $this->mime_type
= self
::MIME_DEFAULT
;
91 /** TODO: add a preference that propose default mime-type to use,
93 if (count($members)) {
94 //Check which members have a valid email address and which have not
95 $this->setRecipients($members);
97 $this->loadAttachments();
101 * Generate new mailing id and temporary path
105 private function generateNewId(): int
109 $pass = new Password($zdb);
110 $this->id
= $pass->makeRandomPassword(30);
111 $this->generateTmpPath($this->id
);
116 * Generate temporary path
118 * @param string $id Random id, defautls to null
122 private function generateTmpPath($id = null)
127 $pass = new Password($zdb);
128 $id = $pass->makeRandomPassword(30);
130 $this->tmp_path
= GALETTE_ATTACHMENTS_PATH
. '/' . $id;
134 * Load mailing attachments
138 private function loadAttachments()
142 isset($this->tmp_path
)
143 && trim($this->tmp_path
) !== ''
145 $dir = $this->tmp_path
;
147 $dir = GALETTE_ATTACHMENTS_PATH
. $this->id
. '/';
150 $files = glob($dir . '*.*');
151 foreach ($files as $file) {
153 $f->setFileName(str_replace($dir, '', $file));
154 $this->attachments
[] = $f;
159 * Loads a mailing from history
161 * @param ResultSet $rs Mailing entry
162 * @param boolean $new True if we create a 'new' mailing,
163 * false otherwise (from preview for example)
167 public function loadFromHistory($rs, $new = true)
171 $orig_recipients = unserialize($rs->mailing_recipients
);
173 $_recipients = array();
174 $mdeps = ['parent' => true];
175 foreach ($orig_recipients as $k => $v) {
176 $m = new Adherent($zdb, $k, $mdeps);
179 $this->setRecipients($_recipients);
180 $this->subject
= $rs->mailing_subject
;
181 $this->message
= $rs->mailing_body
;
182 if ($rs->mailing_sender_name
!== null ||
$rs->mailing_sender_address
!== null) {
184 $rs->mailing_sender_name
,
185 $rs->mailing_sender_address
188 //if mailing has already been sent, generate a new id and copy attachments
189 if ($rs->mailing_sent
&& $new) {
190 $this->generateNewId();
191 $this->copyAttachments($rs->mailing_id
);
193 $this->tmp_path
= null;
194 $this->id
= $rs->mailing_id
;
195 if (!$this->attachments
) {
196 $this->loadAttachments();
198 $this->history_id
= $rs->mailing_id
;
203 * Copy attachments from another mailing
205 * @param int $id Original mailing id
209 private function copyAttachments($id)
211 $source_dir = GALETTE_ATTACHMENTS_PATH
. $id . '/';
212 $dest_dir = GALETTE_ATTACHMENTS_PATH
. $this->id
. '/';
214 if (file_exists($source_dir)) {
215 if (file_exists($dest_dir)) {
216 throw new \
RuntimeException(
220 'Attachments directory already exists for mailing %s!'
226 //copy attachments from source mailing and populate attachments
227 $this->attachments
= array();
228 $files = glob($source_dir . '*.*');
229 foreach ($files as $file) {
230 $f = new File($source_dir);
231 $f->setFileName(str_replace($source_dir, '', $file));
232 $f->copyTo($dest_dir);
233 $this->attachments
[] = $f;
238 'No attachments in source directory',
245 * Apply final header to email and send it :-)
247 * @return GaletteMail::MAIL_ERROR|GaletteMail::MAIL_SENT
249 public function send()
252 foreach ($this->mrecipients
as $member) {
253 $email = $member->getEmail();
254 $m[$email] = $member->sname
;
256 parent
::setRecipients($m);
257 return parent
::send();
261 * Set mailing recipients
263 * @param array $members Array of Adherent objects
267 public function setRecipients($members)
270 $this->mrecipients
= array();
271 $this->unreachables
= array();
273 foreach ($members as $member) {
274 $email = $member->getEmail();
276 if (trim($email) != '' && self
::isValidEmail($email)) {
277 if (!in_array($member, $this->mrecipients
)) {
278 $this->mrecipients
[] = $member;
280 $m[$email] = $member->sname
;
282 if (!in_array($member, $this->unreachables
)) {
283 $this->unreachables
[] = $member;
287 parent
::setRecipients($m);
291 * Store maling attachments
293 * @param array $files Array of uploaded files to store
295 * @return true|int error code
297 public function store($files)
299 if ($this->tmp_path
=== null) {
300 $this->generateTmpPath();
303 if (!file_exists($this->tmp_path
)) {
304 //directory does not exists, create it
305 mkdir($this->tmp_path
);
308 if (!is_dir($this->tmp_path
)) {
309 throw new \
RuntimeException(
310 $this->tmp_path
. ' should be a directory!'
315 $attachment = new File($this->tmp_path
);
316 $res = $attachment->store($files);
320 $this->attachments
[] = $attachment;
327 * Move attachments with final id once mailing has been stored
329 * @param int $id Mailing history id
333 public function moveAttachments($id)
336 isset($this->tmp_path
)
337 && trim($this->tmp_path
) !== ''
338 && count($this->attachments
) > 0
340 foreach ($this->attachments
as &$attachment) {
341 $old_path = $attachment->getDestDir() . $attachment->getFileName();
342 $new_path = GALETTE_ATTACHMENTS_PATH
. $this->id
. '/' .
343 $attachment->getFileName();
344 if (!file_exists(GALETTE_ATTACHMENTS_PATH
. $this->id
)) {
345 mkdir(GALETTE_ATTACHMENTS_PATH
. $this->id
);
347 $moved = rename($old_path, $new_path);
349 $attachment->setDestDir(GALETTE_ATTACHMENTS_PATH
);
352 rmdir($this->tmp_path
);
353 $this->tmp_path
= null;
358 * Remove specified attachment
360 * @param string $name Filename
364 public function removeAttachment($name)
368 isset($this->tmp_path
)
369 && trim($this->tmp_path
) !== ''
370 && file_exists($this->tmp_path
)
372 $to_remove = $this->tmp_path
;
373 } elseif (file_exists(GALETTE_ATTACHMENTS_PATH
. $this->id
)) {
374 $to_remove = GALETTE_ATTACHMENTS_PATH
. $this->id
;
377 if ($to_remove !== null) {
378 $to_remove .= '/' . $name;
380 if (!$this->attachments
) {
381 $this->loadAttachments();
384 if (file_exists($to_remove)) {
386 foreach ($this->attachments
as $att) {
387 if ($att->getFileName() == $name) {
388 unset($this->attachments
[$i]);
399 'File %file does not exists and cannot be removed!'
405 throw new \
RuntimeException(
406 'Unable to get attachments path!'
412 * Remove mailing attachments
414 * @param boolean $temp Remove only tmporary attachments,
415 * to avoid history breaking
419 public function removeAttachments($temp = false)
423 isset($this->tmp_path
)
424 && trim($this->tmp_path
) !== ''
425 && file_exists($this->tmp_path
)
427 $to_remove = $this->tmp_path
;
428 } elseif (file_exists(GALETTE_ATTACHMENTS_PATH
. $this->id
)) {
429 if ($temp === true) {
432 $to_remove = GALETTE_ATTACHMENTS_PATH
. $this->id
;
435 if ($to_remove !== null) {
436 $rdi = new \
RecursiveDirectoryIterator(
438 \FilesystemIterator
::SKIP_DOTS
440 $contents = new \
RecursiveIteratorIterator(
442 \RecursiveIteratorIterator
::CHILD_FIRST
444 foreach ($contents as $path) {
445 if ($path->isFile()) {
446 unlink($path->getPathname());
448 rmdir($path->getPathname());
456 * Return textual error message
458 * @param int $code The error code
460 * @return string Localized message
462 public function getAttachmentErrorMessage($code)
464 $f = new File($this->tmp_path
);
465 return $f->getErrorMessage($code);
469 * Does mailing already exists in history?
473 public function existsInHistory()
475 return isset($this->history_id
);
479 * Global getter method
481 * @param string $name name of the property we want to retrive
483 * @return false|object the called property
485 public function __get($name)
487 $forbidden = array('ordered');
488 if (!in_array($name, $forbidden)) {
491 return $this->cleanedHtml();
494 return $this->current_step
;
497 return $this->getSubject();
500 return $this->getMessage();
502 case 'wrapped_message':
503 return $this->getWrappedMessage();
506 return $this->isHTML();
510 return $this->getPhpMailer();
513 return $this->getErrors();
516 return $this->mrecipients
;
519 if (isset($this->tmp_path
) && trim($this->tmp_path
) !== '') {
520 return $this->tmp_path
;
527 return $this->attachments
;
530 return $this->getSenderName();
532 case 'sender_address':
533 return $this->getSenderAddress();
537 '[' . get_class($this) . 'Trying to get ' . $name,
545 '[' . get_class($this) . 'Unable to get ' . $name,
553 * Global setter method
555 * @param string $name name of the property we want to assign a value to
556 * @param object $value a relevant value for the property
560 public function __set($name, $value)
564 $this->setSubject($value);
567 $this->setMessage($value);
570 if (is_bool($value)) {
571 $this->isHTML($value);
574 '[' . get_class($this) . '] Value for field `' . $name .
575 '` should be boolean - (' . gettype($value) . ')' .
584 && ($value == self
::STEP_START
585 ||
$value == self
::STEP_PREVIEW
586 ||
$value == self
::STEP_SEND
587 ||
$value == self
::STEP_SENT
)
589 $this->current_step
= (int)$value;
592 '[' . get_class($this) . '] Value for field `' . $name .
593 '` should be integer and know - (' . gettype($value) . ')' .
604 '[' . get_class($this) . '] Unable to set proprety `' . $name . '`',