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;
70 private $_mrecipients;
71 private $_current_step;
81 * @param array $members An array of members
82 * @param int $id Identifier, defaults to null
84 public function __construct($members, $id = null)
89 $this->_generateNewId();
91 $this->_current_step
= self
::STEP_START
;
92 $this->_mime_type
= self
::MIME_DEFAULT
;
93 /** TODO: add a preference that propose default mime-type to use,
95 if ( $members !== null) {
96 //Check which members have a valid email adress and which have not
97 $this->setRecipients($members);
99 $this->_loadAttachments();
103 * Generate new mailing id and temporary path
107 private function _generateNewId()
109 $pass = new Password();
110 $this->_id
= $pass->makeRandomPassword(30);
111 $this->_generateTmpPath($this->_id
);
115 * Generate temporary path
117 * @param string $id Random id, defautls to null
121 private function _generateTmpPath($id = null)
123 if ( $id === null ) {
124 $pass = new Password();
125 $id = $pass->makeRandomPassword(30);
127 $this->_tmp_path
= GALETTE_ATTACHMENTS_PATH
. '/' . $id;
131 * Load mailing attachments
135 private function _loadAttachments()
138 if ( isset($this->_tmp_path
)
139 && trim($this->_tmp_path
) !== ''
141 $dir = $this->_tmp_path
;
143 $dir = GALETTE_ATTACHMENTS_PATH
. $this->_id
. '/';
146 $files = glob($dir . '*.*');
147 foreach ( $files as $file ) {
149 $f->setFileName(str_replace($dir, '', $file));
150 $this->attachments
[] = $f;
155 * Loads a mailing from history
157 * @param ResultSet $rs Mailing entry
158 * @param boolean $new True if we create a 'new' mailing,
159 * false otherwise (from preview for example)
163 public function loadFromHistory($rs, $new = true)
165 $orig_recipients = unserialize($rs->mailing_recipients
);
167 $_recipients = array();
168 foreach ( $orig_recipients as $k=>$v ) {
169 $m = new Adherent($k);
172 $this->setRecipients($_recipients);
173 $this->subject
= $rs->mailing_subject
;
174 $this->message
= $rs->mailing_body
;
175 //if mailing has already been sent, generate a new id and copy attachments
176 if ( $rs->mailing_sent
&& $new ) {
177 $this->_generateNewId();
178 $this->_copyAttachments($rs->mailing_id
);
180 $this->_tmp_path
= null;
181 $this->_id
= $rs->mailing_id
;
182 if ( !$this->attachments
) {
183 $this->_loadAttachments();
185 $this->_history_id
= $rs->mailing_id
;
190 * Copy attachments from another mailing
192 * @param int $id Original mailing id
196 private function _copyAttachments($id)
198 $source_dir = GALETTE_ATTACHMENTS_PATH
. $id . '/';
199 $dest_dir = GALETTE_ATTACHMENTS_PATH
. $this->_id
. '/';
201 if ( file_exists($source_dir) ) {
202 if ( file_exists($dest_dir) ) {
203 throw new \
RuntimeException(
207 'Attachments directory already exists for mailing %s!'
213 //copy attachments from source mailing and populate attachments
214 $this->attachments
= array();
215 $files = glob($source_dir . '*.*');
216 foreach ( $files as $file ) {
217 $f = new File($source_dir);
218 $f->setFileName(str_replace($source_dir, '', $file));
219 $f->copyTo($dest_dir);
220 $this->attachments
[] = $f;
225 'No attachments in source directory',
232 * Apply final header to mail and send it :-)
234 * @return GaletteMail::MAIL_ERROR|GaletteMail::MAIL_SENT
236 public function send()
239 foreach ( $this->_mrecipients
as $member ) {
240 $m[$member->email
] = $member->sname
;
242 parent
::setRecipients($m);
243 return parent
::send();
247 * Set mailing recipients
249 * @param array $members Array of Adherent objects
253 public function setRecipients($members)
256 $this->_mrecipients
= array();
257 $this->_unreachables
= array();
259 foreach ($members as $member) {
260 $email = $member->email
;
261 if ( trim($email) != '' && self
::isValidEmail($email) ) {
262 if ( !in_array($member, $this->_mrecipients
) ) {
263 $this->_mrecipients
[] = $member;
265 $m[$email] = $member->sname
;
267 if ( !in_array($member, $this->_unreachables
) ) {
268 $this->_unreachables
[] = $member;
272 parent
::setRecipients($m);
276 * Store maling attachments
278 * @param array $files Array of uploaded files to store
280 * @return true|int error code
282 public function store($files)
284 if ( $this->_tmp_path
=== null ) {
285 $this->_generateTmpPath();
288 if ( !file_exists($this->_tmp_path
) ) {
289 //directory does not exists, create it
290 mkdir($this->_tmp_path
);
293 if ( !is_dir($this->_tmp_path
) ) {
294 throw new \
RuntimeException(
295 $this->_tmp_path
. ' should be a directory!'
300 $attachment = new File($this->_tmp_path
);
301 $res = $attachment->store($files);
305 $this->attachments
[] = $attachment;
312 * Move attachments with final id once mailing has been stored
314 * @param int $id Mailing history id
318 public function moveAttachments($id)
320 if ( isset($this->_tmp_path
)
321 && trim($this->_tmp_path
) !== ''
322 && count($this->attachments
) > 0
324 foreach ( $this->attachments
as &$attachment ) {
325 $old_path = $attachment->getDestDir() . $attachment->getFileName();
326 $new_path = GALETTE_ATTACHMENTS_PATH
. $this->_id
.'/' .
327 $attachment->getFileName();
328 if ( !file_exists(GALETTE_ATTACHMENTS_PATH
. $this->_id
) ) {
329 mkdir(GALETTE_ATTACHMENTS_PATH
. $this->_id
);
331 $moved = rename($old_path, $new_path);
333 $attachment->setDestDir(GALETTE_ATTACHMENTS_PATH
);
336 rmdir($this->_tmp_path
);
337 $this->_tmp_path
= null;
342 * Remove specified attachment
344 * @param string $name Filename
348 public function removeAttachment($name)
351 if ( isset($this->_tmp_path
)
352 && trim($this->_tmp_path
) !== ''
353 && file_exists($this->_tmp_path
)
355 $to_remove = $this->_tmp_path
;
356 } else if ( file_exists(GALETTE_ATTACHMENTS_PATH
. $this->_id
) ) {
357 $to_remove = GALETTE_ATTACHMENTS_PATH
. $this->_id
;
360 if ( $to_remove !== null ) {
361 $to_remove .= '/' . $name;
363 if ( !$this->attachments
) {
364 $this->_loadAttachments();
367 if ( file_exists($to_remove) ) {
369 foreach ( $this->attachments
as $att ) {
370 if ( $att->getFileName() == $name ) {
371 unset($this->attachments
[$i]);
382 'File %file does not exists and cannot be removed!'
388 throw new \
RuntimeException(
389 'Unable to get attachments path!'
395 * Remove mailing attachments
397 * @param boolean $temp Remove only tmporary attachments,
398 * to avoid history breaking
402 public function removeAttachments($temp = false)
405 if ( isset($this->_tmp_path
)
406 && trim($this->_tmp_path
) !== ''
407 && file_exists($this->_tmp_path
)
409 $to_remove = $this->_tmp_path
;
410 } else if ( file_exists(GALETTE_ATTACHMENTS_PATH
. $this->_id
) ) {
411 if ( $temp === true ) {
414 $to_remove = GALETTE_ATTACHMENTS_PATH
. $this->_id
;
417 if ( $to_remove !== null ) {
418 $rdi = new \
RecursiveDirectoryIterator(
420 \FilesystemIterator
::SKIP_DOTS
422 $contents = new \
RecursiveIteratorIterator(
424 \RecursiveIteratorIterator
::CHILD_FIRST
426 foreach ( $contents as $path) {
427 if ( $path->isFile() ) {
428 unlink($path->getPathname());
430 rmdir($path->getPathname());
438 * Return textual error message
440 * @param int $code The error code
442 * @return string Localized message
444 public function getAttachmentErrorMessage($code)
446 $f = new File($this->_tmp_path
);
447 return $f->getErrorMessage($code);
451 * Does mailing already exists in history?
455 public function existsInHistory()
457 return isset($this->_history_id
);
461 * Global getter method
463 * @param string $name name of the property we want to retrive
465 * @return false|object the called property
467 public function __get($name)
469 $forbidden = array('ordered');
470 if ( !in_array($name, $forbidden) ) {
473 return $this->cleanedHtml();
476 return $this->current_step
;
479 return $this->getSubject();
482 return $this->getMessage();
484 case 'wrapped_message':
485 return $this->getWrappedMessage();
488 return $this->isHTML();
492 return $this->getPhpMailer();
495 return $this->getErrors();
498 return $this->_mrecipients
;
501 if ( isset($this->_tmp_path
) && trim($this->_tmp_path
) !== '') {
502 return $this->_tmp_path
;
509 return $this->attachments
;
512 $rname = '_' . $name;
514 '[' . get_class($this) . 'Trying to get ' . $name .
515 ' renamed: ' . $rname,
518 return $this->$rname;
523 '[' . get_class($this) . 'Unable to get ' . $name .
524 ' renamed: ' . $rname,
532 * Global setter method
534 * @param string $name name of the property we want to assign a value to
535 * @param object $value a relevant value for the property
539 public function __set($name, $value)
541 $rname = '_' . $name;
545 $this->setSubject($value);
548 $this->setMessage($value);
551 if ( is_bool($value) ) {
552 $this->isHTML($value);
555 '[' . get_class($this) . '] Value for field `' . $name .
556 '` should be boolean - (' . gettype($value) . ')' .
564 && ( $value == self
::STEP_START
565 ||
$value == self
::STEP_PREVIEW
566 ||
$value == self
::STEP_SEND
567 ||
$value == self
::STEP_SENT
)
569 $this->_current_step
= (int)$value;
572 '[' . get_class($this) . '] Value for field `' . $name .
573 '` should be integer and know - (' . gettype($value) . ')' .
584 '[' . get_class($this) . '] Unable to set proprety `' . $name . '`',