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 address 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 $mdeps = ['parent' => true];
169 foreach ( $orig_recipients as $k=>$v ) {
170 $m = new Adherent($k, $mdeps);
173 $this->setRecipients($_recipients);
174 $this->subject
= $rs->mailing_subject
;
175 $this->message
= $rs->mailing_body
;
176 //if mailing has already been sent, generate a new id and copy attachments
177 if ( $rs->mailing_sent
&& $new ) {
178 $this->_generateNewId();
179 $this->_copyAttachments($rs->mailing_id
);
181 $this->_tmp_path
= null;
182 $this->_id
= $rs->mailing_id
;
183 if ( !$this->attachments
) {
184 $this->_loadAttachments();
186 $this->_history_id
= $rs->mailing_id
;
191 * Copy attachments from another mailing
193 * @param int $id Original mailing id
197 private function _copyAttachments($id)
199 $source_dir = GALETTE_ATTACHMENTS_PATH
. $id . '/';
200 $dest_dir = GALETTE_ATTACHMENTS_PATH
. $this->_id
. '/';
202 if ( file_exists($source_dir) ) {
203 if ( file_exists($dest_dir) ) {
204 throw new \
RuntimeException(
208 'Attachments directory already exists for mailing %s!'
214 //copy attachments from source mailing and populate attachments
215 $this->attachments
= array();
216 $files = glob($source_dir . '*.*');
217 foreach ( $files as $file ) {
218 $f = new File($source_dir);
219 $f->setFileName(str_replace($source_dir, '', $file));
220 $f->copyTo($dest_dir);
221 $this->attachments
[] = $f;
226 'No attachments in source directory',
233 * Apply final header to mail and send it :-)
235 * @return GaletteMail::MAIL_ERROR|GaletteMail::MAIL_SENT
237 public function send()
240 foreach ($this->_mrecipients
as $member) {
241 $email = $member->getEmail();
242 $m[$email] = $member->sname
;
244 parent
::setRecipients($m);
245 return parent
::send();
249 * Set mailing recipients
251 * @param array $members Array of Adherent objects
255 public function setRecipients($members)
258 $this->_mrecipients
= array();
259 $this->_unreachables
= array();
261 foreach ($members as $member) {
262 $email = $member->getEmail();
264 if ( trim($email) != '' && self
::isValidEmail($email) ) {
265 if ( !in_array($member, $this->_mrecipients
) ) {
266 $this->_mrecipients
[] = $member;
268 $m[$email] = $member->sname
;
270 if ( !in_array($member, $this->_unreachables
) ) {
271 $this->_unreachables
[] = $member;
275 parent
::setRecipients($m);
279 * Store maling attachments
281 * @param array $files Array of uploaded files to store
283 * @return true|int error code
285 public function store($files)
287 if ( $this->_tmp_path
=== null ) {
288 $this->_generateTmpPath();
291 if ( !file_exists($this->_tmp_path
) ) {
292 //directory does not exists, create it
293 mkdir($this->_tmp_path
);
296 if ( !is_dir($this->_tmp_path
) ) {
297 throw new \
RuntimeException(
298 $this->_tmp_path
. ' should be a directory!'
303 $attachment = new File($this->_tmp_path
);
304 $res = $attachment->store($files);
308 $this->attachments
[] = $attachment;
315 * Move attachments with final id once mailing has been stored
317 * @param int $id Mailing history id
321 public function moveAttachments($id)
323 if ( isset($this->_tmp_path
)
324 && trim($this->_tmp_path
) !== ''
325 && count($this->attachments
) > 0
327 foreach ( $this->attachments
as &$attachment ) {
328 $old_path = $attachment->getDestDir() . $attachment->getFileName();
329 $new_path = GALETTE_ATTACHMENTS_PATH
. $this->_id
.'/' .
330 $attachment->getFileName();
331 if ( !file_exists(GALETTE_ATTACHMENTS_PATH
. $this->_id
) ) {
332 mkdir(GALETTE_ATTACHMENTS_PATH
. $this->_id
);
334 $moved = rename($old_path, $new_path);
336 $attachment->setDestDir(GALETTE_ATTACHMENTS_PATH
);
339 rmdir($this->_tmp_path
);
340 $this->_tmp_path
= null;
345 * Remove specified attachment
347 * @param string $name Filename
351 public function removeAttachment($name)
354 if ( isset($this->_tmp_path
)
355 && trim($this->_tmp_path
) !== ''
356 && file_exists($this->_tmp_path
)
358 $to_remove = $this->_tmp_path
;
359 } else if ( file_exists(GALETTE_ATTACHMENTS_PATH
. $this->_id
) ) {
360 $to_remove = GALETTE_ATTACHMENTS_PATH
. $this->_id
;
363 if ( $to_remove !== null ) {
364 $to_remove .= '/' . $name;
366 if ( !$this->attachments
) {
367 $this->_loadAttachments();
370 if ( file_exists($to_remove) ) {
372 foreach ( $this->attachments
as $att ) {
373 if ( $att->getFileName() == $name ) {
374 unset($this->attachments
[$i]);
385 'File %file does not exists and cannot be removed!'
391 throw new \
RuntimeException(
392 'Unable to get attachments path!'
398 * Remove mailing attachments
400 * @param boolean $temp Remove only tmporary attachments,
401 * to avoid history breaking
405 public function removeAttachments($temp = false)
408 if ( isset($this->_tmp_path
)
409 && trim($this->_tmp_path
) !== ''
410 && file_exists($this->_tmp_path
)
412 $to_remove = $this->_tmp_path
;
413 } else if ( file_exists(GALETTE_ATTACHMENTS_PATH
. $this->_id
) ) {
414 if ( $temp === true ) {
417 $to_remove = GALETTE_ATTACHMENTS_PATH
. $this->_id
;
420 if ( $to_remove !== null ) {
421 $rdi = new \
RecursiveDirectoryIterator(
423 \FilesystemIterator
::SKIP_DOTS
425 $contents = new \
RecursiveIteratorIterator(
427 \RecursiveIteratorIterator
::CHILD_FIRST
429 foreach ( $contents as $path) {
430 if ( $path->isFile() ) {
431 unlink($path->getPathname());
433 rmdir($path->getPathname());
441 * Return textual error message
443 * @param int $code The error code
445 * @return string Localized message
447 public function getAttachmentErrorMessage($code)
449 $f = new File($this->_tmp_path
);
450 return $f->getErrorMessage($code);
454 * Does mailing already exists in history?
458 public function existsInHistory()
460 return isset($this->_history_id
);
464 * Global getter method
466 * @param string $name name of the property we want to retrive
468 * @return false|object the called property
470 public function __get($name)
472 $forbidden = array('ordered');
473 if ( !in_array($name, $forbidden) ) {
476 return $this->cleanedHtml();
479 return $this->current_step
;
482 return $this->getSubject();
485 return $this->getMessage();
487 case 'wrapped_message':
488 return $this->getWrappedMessage();
491 return $this->isHTML();
495 return $this->getPhpMailer();
498 return $this->getErrors();
501 return $this->_mrecipients
;
504 if ( isset($this->_tmp_path
) && trim($this->_tmp_path
) !== '') {
505 return $this->_tmp_path
;
512 return $this->attachments
;
515 $rname = '_' . $name;
517 '[' . get_class($this) . 'Trying to get ' . $name .
518 ' renamed: ' . $rname,
521 return $this->$rname;
526 '[' . get_class($this) . 'Unable to get ' . $name .
527 ' renamed: ' . $rname,
535 * Global setter method
537 * @param string $name name of the property we want to assign a value to
538 * @param object $value a relevant value for the property
542 public function __set($name, $value)
544 $rname = '_' . $name;
548 $this->setSubject($value);
551 $this->setMessage($value);
554 if ( is_bool($value) ) {
555 $this->isHTML($value);
558 '[' . get_class($this) . '] Value for field `' . $name .
559 '` should be boolean - (' . gettype($value) . ')' .
567 && ( $value == self
::STEP_START
568 ||
$value == self
::STEP_PREVIEW
569 ||
$value == self
::STEP_SEND
570 ||
$value == self
::STEP_SENT
)
572 $this->_current_step
= (int)$value;
575 '[' . get_class($this) . '] Value for field `' . $name .
576 '` should be integer and know - (' . gettype($value) . ')' .
587 '[' . get_class($this) . '] Unable to set proprety `' . $name . '`',