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
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.7dev - 2009-03-07
38 namespace Galette\Core
;
41 use Galette\Entity\Adherent
;
50 * @author Johan Cwiklinski <johan@x-tnd.be>
51 * @copyright 2009-2014 The Galette Team
52 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
53 * @link http://galette.tuxfamily.org
54 * @since Available since 0.7dev - 2009-03-07
56 class Mailing
extends GaletteMail
59 const STEP_PREVIEW
= 1;
63 const MIME_HTML
= 'text/html';
64 const MIME_TEXT
= 'text/plain';
65 const MIME_DEFAULT
= self
::MIME_TEXT
;
69 private $unreachables = array();
70 private $mrecipients = array();
71 private $current_step;
81 * @param Preferences $preferences Preferences instance
82 * @param array $members An array of members
83 * @param int $id Identifier, defaults to null
85 public function __construct(Preferences
$preferences, $members, $id = null)
87 parent
::__construct($preferences);
91 $this->generateNewId();
93 $this->current_step
= self
::STEP_START
;
94 $this->mime_type
= self
::MIME_DEFAULT
;
95 /** TODO: add a preference that propose default mime-type to use,
97 if ($members !== null) {
98 //Check which members have a valid email address and which have not
99 $this->setRecipients($members);
101 $this->loadAttachments();
105 * Generate new mailing id and temporary path
109 private function generateNewId()
113 $pass = new Password($zdb);
114 $this->id
= $pass->makeRandomPassword(30);
115 $this->generateTmpPath($this->id
);
119 * Generate temporary path
121 * @param string $id Random id, defautls to null
125 private function generateTmpPath($id = null)
130 $pass = new Password($zdb);
131 $id = $pass->makeRandomPassword(30);
133 $this->tmp_path
= GALETTE_ATTACHMENTS_PATH
. '/' . $id;
137 * Load mailing attachments
141 private function loadAttachments()
144 if (isset($this->tmp_path
)
145 && trim($this->tmp_path
) !== ''
147 $dir = $this->tmp_path
;
149 $dir = GALETTE_ATTACHMENTS_PATH
. $this->id
. '/';
152 $files = glob($dir . '*.*');
153 foreach ($files as $file) {
155 $f->setFileName(str_replace($dir, '', $file));
156 $this->attachments
[] = $f;
161 * Loads a mailing from history
163 * @param ResultSet $rs Mailing entry
164 * @param boolean $new True if we create a 'new' mailing,
165 * false otherwise (from preview for example)
169 public function loadFromHistory($rs, $new = true)
173 $orig_recipients = unserialize($rs->mailing_recipients
);
175 $_recipients = array();
176 $mdeps = ['parent' => true];
177 foreach ($orig_recipients as $k => $v) {
178 $m = new Adherent($zdb, $k, $mdeps);
181 $this->setRecipients($_recipients);
182 $this->subject
= $rs->mailing_subject
;
183 $this->message
= $rs->mailing_body
;
184 if ($rs->mailing_sender_name
!== null ||
$rs->mailing_sender_address
!== null) {
186 $rs->mailing_sender_name
,
187 $rs->mailing_sender_address
190 //if mailing has already been sent, generate a new id and copy attachments
191 if ($rs->mailing_sent
&& $new) {
192 $this->generateNewId();
193 $this->copyAttachments($rs->mailing_id
);
195 $this->tmp_path
= null;
196 $this->id
= $rs->mailing_id
;
197 if (!$this->attachments
) {
198 $this->loadAttachments();
200 $this->history_id
= $rs->mailing_id
;
205 * Copy attachments from another mailing
207 * @param int $id Original mailing id
211 private function copyAttachments($id)
213 $source_dir = GALETTE_ATTACHMENTS_PATH
. $id . '/';
214 $dest_dir = GALETTE_ATTACHMENTS_PATH
. $this->id
. '/';
216 if (file_exists($source_dir)) {
217 if (file_exists($dest_dir)) {
218 throw new \
RuntimeException(
222 'Attachments directory already exists for mailing %s!'
228 //copy attachments from source mailing and populate attachments
229 $this->attachments
= array();
230 $files = glob($source_dir . '*.*');
231 foreach ($files as $file) {
232 $f = new File($source_dir);
233 $f->setFileName(str_replace($source_dir, '', $file));
234 $f->copyTo($dest_dir);
235 $this->attachments
[] = $f;
240 'No attachments in source directory',
247 * Apply final header to email and send it :-)
249 * @return GaletteMail::MAIL_ERROR|GaletteMail::MAIL_SENT
251 public function send()
254 foreach ($this->mrecipients
as $member) {
255 $email = $member->getEmail();
256 $m[$email] = $member->sname
;
258 parent
::setRecipients($m);
259 return parent
::send();
263 * Set mailing recipients
265 * @param array $members Array of Adherent objects
269 public function setRecipients($members)
272 $this->mrecipients
= array();
273 $this->unreachables
= array();
275 foreach ($members as $member) {
276 $email = $member->getEmail();
278 if (trim($email) != '' && self
::isValidEmail($email)) {
279 if (!in_array($member, $this->mrecipients
)) {
280 $this->mrecipients
[] = $member;
282 $m[$email] = $member->sname
;
284 if (!in_array($member, $this->unreachables
)) {
285 $this->unreachables
[] = $member;
289 parent
::setRecipients($m);
293 * Store maling attachments
295 * @param array $files Array of uploaded files to store
297 * @return true|int error code
299 public function store($files)
301 if ($this->tmp_path
=== null) {
302 $this->generateTmpPath();
305 if (!file_exists($this->tmp_path
)) {
306 //directory does not exists, create it
307 mkdir($this->tmp_path
);
310 if (!is_dir($this->tmp_path
)) {
311 throw new \
RuntimeException(
312 $this->tmp_path
. ' should be a directory!'
317 $attachment = new File($this->tmp_path
);
318 $res = $attachment->store($files);
322 $this->attachments
[] = $attachment;
329 * Move attachments with final id once mailing has been stored
331 * @param int $id Mailing history id
335 public function moveAttachments($id)
337 if (isset($this->tmp_path
)
338 && trim($this->tmp_path
) !== ''
339 && count($this->attachments
) > 0
341 foreach ($this->attachments
as &$attachment) {
342 $old_path = $attachment->getDestDir() . $attachment->getFileName();
343 $new_path = GALETTE_ATTACHMENTS_PATH
. $this->id
. '/' .
344 $attachment->getFileName();
345 if (!file_exists(GALETTE_ATTACHMENTS_PATH
. $this->id
)) {
346 mkdir(GALETTE_ATTACHMENTS_PATH
. $this->id
);
348 $moved = rename($old_path, $new_path);
350 $attachment->setDestDir(GALETTE_ATTACHMENTS_PATH
);
353 rmdir($this->tmp_path
);
354 $this->tmp_path
= null;
359 * Remove specified attachment
361 * @param string $name Filename
365 public function removeAttachment($name)
368 if (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)
422 if (isset($this->tmp_path
)
423 && trim($this->tmp_path
) !== ''
424 && file_exists($this->tmp_path
)
426 $to_remove = $this->tmp_path
;
427 } elseif (file_exists(GALETTE_ATTACHMENTS_PATH
. $this->id
)) {
428 if ($temp === true) {
431 $to_remove = GALETTE_ATTACHMENTS_PATH
. $this->id
;
434 if ($to_remove !== null) {
435 $rdi = new \
RecursiveDirectoryIterator(
437 \FilesystemIterator
::SKIP_DOTS
439 $contents = new \
RecursiveIteratorIterator(
441 \RecursiveIteratorIterator
::CHILD_FIRST
443 foreach ($contents as $path) {
444 if ($path->isFile()) {
445 unlink($path->getPathname());
447 rmdir($path->getPathname());
455 * Return textual error message
457 * @param int $code The error code
459 * @return string Localized message
461 public function getAttachmentErrorMessage($code)
463 $f = new File($this->tmp_path
);
464 return $f->getErrorMessage($code);
468 * Does mailing already exists in history?
472 public function existsInHistory()
474 return isset($this->history_id
);
478 * Global getter method
480 * @param string $name name of the property we want to retrive
482 * @return false|object the called property
484 public function __get($name)
486 $forbidden = array('ordered');
487 if (!in_array($name, $forbidden)) {
490 return $this->cleanedHtml();
493 return $this->current_step
;
496 return $this->getSubject();
499 return $this->getMessage();
501 case 'wrapped_message':
502 return $this->getWrappedMessage();
505 return $this->isHTML();
509 return $this->getPhpMailer();
512 return $this->getErrors();
515 return $this->mrecipients
;
518 if (isset($this->tmp_path
) && trim($this->tmp_path
) !== '') {
519 return $this->tmp_path
;
526 return $this->attachments
;
529 return $this->getSenderName();
531 case 'sender_address':
532 return $this->getSenderAddress();
536 '[' . get_class($this) . 'Trying to get ' . $name,
544 '[' . get_class($this) . 'Unable to get ' . $name,
552 * Global setter method
554 * @param string $name name of the property we want to assign a value to
555 * @param object $value a relevant value for the property
559 public function __set($name, $value)
563 $this->setSubject($value);
566 $this->setMessage($value);
569 if (is_bool($value)) {
570 $this->isHTML($value);
573 '[' . get_class($this) . '] Value for field `' . $name .
574 '` should be boolean - (' . gettype($value) . ')' .
582 && ($value == self
::STEP_START
583 ||
$value == self
::STEP_PREVIEW
584 ||
$value == self
::STEP_SEND
585 ||
$value == self
::STEP_SENT
)
587 $this->current_step
= (int)$value;
590 '[' . get_class($this) . '] Value for field `' . $name .
591 '` should be integer and know - (' . gettype($value) . ')' .
602 '[' . get_class($this) . '] Unable to set proprety `' . $name . '`',