2007-11-22 09:29:16 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* eGroupWare - Notifications
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package notifications
|
2007-12-18 16:53:38 +01:00
|
|
|
* @subpackage backends
|
2007-11-22 09:29:16 +01:00
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Christian Binder <christian@jaytraxx.de>
|
2008-07-30 08:49:55 +02:00
|
|
|
* @version $Id$
|
2007-11-22 09:29:16 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* User notification via email.
|
|
|
|
*/
|
2008-01-30 19:58:00 +01:00
|
|
|
class notifications_email implements notifications_iface {
|
2007-11-22 09:29:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Appname
|
|
|
|
*/
|
|
|
|
const _appname = 'notifications';
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-11-22 09:29:16 +01:00
|
|
|
/**
|
|
|
|
* holds account object for user who sends the message
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
private $sender;
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-11-22 09:29:16 +01:00
|
|
|
/**
|
|
|
|
* holds account object for user to notify
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
private $recipient;
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-11-22 09:29:16 +01:00
|
|
|
/**
|
|
|
|
* holds config object (sitewide application config)
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
private $config;
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-11-22 09:29:16 +01:00
|
|
|
/**
|
|
|
|
* holds preferences object of user to notify
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
private $preferences;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* holds mail object
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
private $mail;
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-11-22 09:29:16 +01:00
|
|
|
/**
|
2008-01-30 19:58:00 +01:00
|
|
|
* constructor of notifications_email
|
2007-11-22 09:29:16 +01:00
|
|
|
*
|
2007-12-10 09:26:29 +01:00
|
|
|
* @param object $_sender
|
2007-11-22 09:29:16 +01:00
|
|
|
* @param object $_recipient
|
2007-12-10 09:26:29 +01:00
|
|
|
* @param object $_config
|
2007-11-22 09:29:16 +01:00
|
|
|
* @param object $_preferences
|
|
|
|
*/
|
2007-12-18 16:53:38 +01:00
|
|
|
public function __construct($_sender, $_recipient, $_config = null, $_preferences = null) {
|
|
|
|
if(!is_object($_sender)) { throw new Exception("no sender given."); }
|
|
|
|
if(!is_object($_recipient)) { throw new Exception("no recipient given."); }
|
|
|
|
$this->sender = $_sender;
|
|
|
|
$this->recipient = $_recipient;
|
|
|
|
$this->config = $_config;
|
|
|
|
$this->preferences = $_preferences;
|
2007-11-22 09:29:16 +01:00
|
|
|
if(!is_object($this->mail))
|
|
|
|
{
|
|
|
|
$this->mail = new send();
|
|
|
|
}
|
|
|
|
}
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-11-22 09:29:16 +01:00
|
|
|
/**
|
|
|
|
* sends notification
|
|
|
|
*
|
|
|
|
* @param array $_messages
|
2007-12-18 16:53:38 +01:00
|
|
|
* @param string $_subject
|
|
|
|
* @param array $_links
|
2007-11-22 09:29:16 +01:00
|
|
|
* @param array $_attachments
|
|
|
|
*/
|
2007-12-18 16:53:38 +01:00
|
|
|
public function send(array $_messages, $_subject = false, $_links = false, $_attachments = false) {
|
|
|
|
$body_plain = $_messages['plain'].$this->render_links($_links, false, $this->preferences->external_mailclient);
|
2008-01-30 19:58:00 +01:00
|
|
|
$body_html = "<html><body>\n".$_messages['html'].$this->render_links($_links, true, $this->preferences->external_mailclient)."</body>\n</html>\n";
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-11-22 09:29:16 +01:00
|
|
|
$this->mail->ClearAddresses();
|
|
|
|
$this->mail->ClearAttachments();
|
|
|
|
$this->mail->IsHTML(true);
|
2007-12-10 09:26:29 +01:00
|
|
|
$this->mail->AddAddress($this->recipient->account_email, $this->recipient->account_fullname);
|
2007-11-22 09:29:16 +01:00
|
|
|
$this->mail->AddCustomHeader('X-eGroupWare-type: notification-mail');
|
2007-12-10 09:26:29 +01:00
|
|
|
$this->mail->From = $this->sender->account_email;
|
|
|
|
$this->mail->FromName = $this->sender->account_fullname;
|
2007-11-22 09:29:16 +01:00
|
|
|
$this->mail->Subject = $this->mail->encode_subject($_subject);
|
|
|
|
$this->mail->Body = $body_html;
|
|
|
|
$this->mail->AltBody = $body_plain;
|
2007-12-18 16:53:38 +01:00
|
|
|
if(is_array($_attachments) && count($_attachments) > 0) {
|
2007-11-22 09:29:16 +01:00
|
|
|
foreach($_attachments as $attachment) {
|
|
|
|
$this->mail->AddStringAttachment($attachment->string, $attachment->filename, $attachment->encoding, $attachment->type);
|
2008-12-09 15:02:15 +01:00
|
|
|
}
|
2007-11-22 09:29:16 +01:00
|
|
|
}
|
|
|
|
if(!$error=$this->mail->Send()) {
|
2008-12-09 15:02:15 +01:00
|
|
|
throw new Exception("Failed sending notification message via email.$error".print_r($this->mail->ErrorInfo,true));
|
2007-11-22 09:29:16 +01:00
|
|
|
}
|
|
|
|
}
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-12-18 16:53:38 +01:00
|
|
|
/**
|
|
|
|
* renders plaintext/html links from given link array
|
|
|
|
*
|
|
|
|
* @param array $_links
|
|
|
|
* @param boolean $_render_html
|
|
|
|
* @param boolean $_render_external
|
|
|
|
* @return plain or html rendered link(s) as complete string
|
|
|
|
*/
|
|
|
|
private function render_links($_links = false, $_render_html = false, $_render_external = true) {
|
|
|
|
if(!is_array($_links) || count($_links) == 0) { return false; }
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-12-18 16:53:38 +01:00
|
|
|
// provide defaults if given arguments are null
|
|
|
|
// php distinguishes between missing and present(null) arguments
|
|
|
|
if(is_null($_render_html)) { $_render_html = false; }
|
|
|
|
if(is_null($_render_external)) { $_render_external = true; }
|
2008-01-30 19:58:00 +01:00
|
|
|
$newline = $_render_html ? "<br />" : "\n";
|
2008-03-09 15:35:48 +01:00
|
|
|
$hruler = $_render_html ? html::hr() : '';
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2008-01-30 19:58:00 +01:00
|
|
|
$rendered_links = array();
|
2007-12-18 16:53:38 +01:00
|
|
|
foreach($_links as $link) {
|
2008-01-30 19:58:00 +01:00
|
|
|
if($_render_external || ! $link->popup) { $link->view['no_popup'] = 1; }
|
2008-03-09 15:35:48 +01:00
|
|
|
$url = html::link('/index.php', $link->view);
|
2008-01-30 19:58:00 +01:00
|
|
|
// do not expose sensitive data
|
|
|
|
$url = preg_replace('/(sessionid|kp3|domain)=[^&]+&?/','',$url);
|
|
|
|
// complete missing protocol and domain part if needed
|
|
|
|
if ($url{0} == '/' && $_render_external) {
|
|
|
|
$url = ($_SERVER['HTTPS'] || $GLOBALS['egw_info']['server']['enforce_ssl'] ? 'https://' : 'http://').
|
|
|
|
($GLOBALS['egw_info']['server']['hostname'] ? $GLOBALS['egw_info']['server']['hostname'] : $_SERVER['HTTP_HOST']).$url;
|
2007-12-18 16:53:38 +01:00
|
|
|
}
|
2008-03-09 15:35:48 +01:00
|
|
|
$rendered_links[] = $_render_html ? html::a_href($link->text, $url, false, 'target="_blank"') : $url;
|
2007-12-18 16:53:38 +01:00
|
|
|
}
|
|
|
|
|
2008-01-30 19:58:00 +01:00
|
|
|
return $hruler.$newline.lang('Linked entries:').$newline.implode($newline,$rendered_links);
|
2007-12-18 16:53:38 +01:00
|
|
|
}
|
2007-11-22 09:29:16 +01:00
|
|
|
|
|
|
|
}
|