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
|
2019-09-13 14:30:55 +02:00
|
|
|
* @param array $_data value for key "reply_to" to use a custom ReplyTo address
|
2007-11-22 09:29:16 +01:00
|
|
|
*/
|
2018-01-24 19:02:35 +01:00
|
|
|
public function send(array $_messages, $_subject = false, $_links = false, $_attachments = false, $_data = false)
|
2014-11-25 15:22:16 +01:00
|
|
|
{
|
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
|
|
|
|
2019-09-13 14:30:55 +02:00
|
|
|
if ( $_data && !empty( $_data['reply_to'] ) )
|
|
|
|
{
|
|
|
|
$this->mail->addReplyTo($_data['reply_to']);
|
|
|
|
}
|
|
|
|
// do NOT set sender as From, as this might not be allowed, set it instead as ReplyTo, if that one it not explicitly set already
|
|
|
|
elseif ($this->mail->getHeader('From') != Api\Mailer::add_personal($this->sender->account_email, $this->sender->account_fullname))
|
|
|
|
{
|
|
|
|
$this->mail->addReplyTo($this->sender->account_email, $this->sender->account_fullname);
|
2019-03-12 22:26:35 +01:00
|
|
|
}
|
|
|
|
|
2015-02-26 15:56:23 +01:00
|
|
|
$this->mail->addHeader('Subject', trim($_subject)); // trim the subject to avoid strange wrong encoding problem
|
2017-10-27 19:03:54 +02:00
|
|
|
if ($_messages['html'])
|
|
|
|
{
|
|
|
|
// embed images as inline
|
|
|
|
\EGroupware\Api\Mail::processURL2InlineImages($this->mail, $body_html, null);
|
|
|
|
}
|
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
|
2018-03-21 12:40:59 +01:00
|
|
|
if ($url[0] == '/' && $_render_external) $url = Api\Framework::getUrl($url);
|
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
|
|
|
|
|
|
|
}
|