2007-11-22 09:29:16 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2016-05-06 11:19:36 +02:00
|
|
|
* EGroupware - Notifications
|
2007-11-22 09:29:16 +01:00
|
|
|
*
|
|
|
|
* @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
|
|
|
*/
|
|
|
|
|
2016-05-06 11:19:36 +02:00
|
|
|
use EGroupware\Api;
|
|
|
|
|
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
|
|
|
|
*
|
2016-05-06 11:19:36 +02:00
|
|
|
* @var Api\Preferences
|
2007-11-22 09:29:16 +01:00
|
|
|
*/
|
|
|
|
private $preferences;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* holds mail object
|
|
|
|
*
|
2011-06-23 11:05:25 +02:00
|
|
|
* @var send
|
2007-11-22 09:29:16 +01:00
|
|
|
*/
|
|
|
|
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;
|
2011-02-21 17:30:27 +01:00
|
|
|
if(is_object($this->mail))
|
2011-02-21 13:12:29 +01:00
|
|
|
{
|
|
|
|
unset($this->mail);
|
|
|
|
}
|
2016-05-06 11:19:36 +02:00
|
|
|
$this->mail = new Api\Mailer();
|
2007-11-22 09:29:16 +01:00
|
|
|
}
|
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
|
|
|
|
*/
|
2014-11-25 15:22:16 +01:00
|
|
|
public function send(array $_messages, $_subject = false, $_links = false, $_attachments = false)
|
|
|
|
{
|
2007-12-18 16:53:38 +01:00
|
|
|
$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();
|
2014-11-25 15:22:16 +01:00
|
|
|
$this->mail->addAddress($this->recipient->account_email, $this->recipient->account_fullname);
|
|
|
|
$this->mail->addHeader('X-EGroupware-Type', 'notification-mail');
|
|
|
|
$this->mail->addHeader('X-EGroupware-Install', $GLOBALS['egw_info']['server']['install_id'].'@'.$GLOBALS['egw_info']['server']['default_domain']);
|
|
|
|
//$this->mail->AddHeader('X-EGroupware-URL', 'notification-mail');
|
|
|
|
//$this->mail->AddHeader('X-EGroupware-Tracker', 'notification-mail');
|
2014-11-18 11:50:35 +01:00
|
|
|
//error_log(__METHOD__.__LINE__."preparing notification message via email.".array2string($this->mail));
|
2014-11-25 15:22:16 +01:00
|
|
|
|
|
|
|
$this->mail->setFrom($this->sender->account_email, $this->sender->account_fullname);
|
|
|
|
|
2015-02-26 15:56:23 +01:00
|
|
|
$this->mail->addHeader('Subject', trim($_subject)); // trim the subject to avoid strange wrong encoding problem
|
2014-11-25 15:22:16 +01:00
|
|
|
$this->mail->setHtmlBody($body_html, null, false); // no automatic alternativ
|
|
|
|
$this->mail->setBody($body_plain);
|
|
|
|
|
2011-06-23 11:05:25 +02:00
|
|
|
if(is_array($_attachments) && count($_attachments) > 0)
|
|
|
|
{
|
|
|
|
foreach($_attachments as $attachment)
|
|
|
|
{
|
|
|
|
if ($attachment->string)
|
|
|
|
{
|
|
|
|
$this->mail->AddStringAttachment($attachment->string, $attachment->filename, $attachment->encoding, $attachment->type);
|
|
|
|
}
|
|
|
|
elseif($attachment->path)
|
|
|
|
{
|
|
|
|
$this->mail->AddAttachment($attachment->path, $attachment->filename, $attachment->encoding, $attachment->type);
|
|
|
|
}
|
2011-05-26 15:26:09 +02:00
|
|
|
}
|
2007-11-22 09:29:16 +01:00
|
|
|
}
|
2014-11-18 11:50:35 +01:00
|
|
|
//error_log(__METHOD__.__LINE__."about sending notification message via email.".array2string($this->mail));
|
2014-11-25 15:22:16 +01:00
|
|
|
$this->mail->send();
|
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";
|
2016-05-06 11:19:36 +02:00
|
|
|
$hruler = $_render_html ? Api\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; }
|
|
|
|
// do not expose sensitive data
|
2016-05-06 11:19:36 +02:00
|
|
|
$url = preg_replace('/(sessionid|kp3|domain)=[^&]+&?/','',Api\Html::link('/index.php', $link->view));
|
2008-01-30 19:58:00 +01:00
|
|
|
// 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
|
|
|
}
|
2009-10-09 21:51:25 +02:00
|
|
|
$a_href = '<a href="'.$url.'" target="_blank">'.$link->text.'</a>';
|
|
|
|
$rendered_links[] = $_render_html ? $a_href : $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
|
|
|
|
|
|
|
}
|