2007-12-05 09:55:10 +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-12-05 09:55:10 +01:00
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Christian Binder <christian@jaytraxx.de>
|
2008-07-30 08:49:55 +02:00
|
|
|
* @version $Id$
|
2007-12-05 09:55:10 +01:00
|
|
|
*/
|
|
|
|
|
2016-05-06 11:19:36 +02:00
|
|
|
use EGroupware\Api;
|
|
|
|
|
2007-12-05 09:55:10 +01:00
|
|
|
/**
|
|
|
|
* User notification via winpopup.
|
|
|
|
*/
|
2008-01-30 19:58:00 +01:00
|
|
|
class notifications_winpopup implements notifications_iface {
|
2007-12-05 09:55:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Appname
|
|
|
|
*/
|
|
|
|
const _appname = 'notifications';
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-12-05 09:55:10 +01:00
|
|
|
/**
|
|
|
|
* Login table in SQL database
|
|
|
|
*/
|
|
|
|
const _login_table = 'egw_access_log';
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-12-05 09:55:10 +01:00
|
|
|
/**
|
|
|
|
* holds account object for user who sends the message
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
private $sender;
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-12-05 09:55:10 +01:00
|
|
|
/**
|
|
|
|
* holds account object for user to notify
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
private $recipient;
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-12-05 09:55:10 +01:00
|
|
|
/**
|
|
|
|
* holds config object (sitewide application config)
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
private $config;
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-12-05 09:55:10 +01:00
|
|
|
/**
|
|
|
|
* holds preferences object of user to notify
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
private $preferences;
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-12-05 09:55:10 +01:00
|
|
|
/**
|
|
|
|
* holds the netbios command to be executed on notification
|
|
|
|
*
|
2008-07-30 08:49:55 +02:00
|
|
|
* @abstract
|
2007-12-18 16:53:38 +01:00
|
|
|
* Example: $netbios_command = "/bin/echo [MESSAGE] | /usr/bin/smbclient -M computer-[4] -I [IP] -U [SENDER]";
|
2007-12-05 09:55:10 +01:00
|
|
|
*
|
|
|
|
* Placeholders are:
|
|
|
|
* [MESSAGE] is the notification message itself
|
|
|
|
* [1] - [4] are the IP-Octets of the windows machine to notify
|
|
|
|
* [IP] is the IP-Adress of the windows machine to notify
|
|
|
|
* [SENDER] is the sender of the message
|
|
|
|
* Note: the webserver-user needs execute rights for this command
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2009-01-25 19:05:13 +01:00
|
|
|
private $netbios_command = false;
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-12-05 09:55:10 +01:00
|
|
|
/**
|
2008-01-30 19:58:00 +01:00
|
|
|
* constructor of notifications_winpopup
|
2007-12-05 09:55:10 +01:00
|
|
|
*
|
|
|
|
* @param object $_sender
|
|
|
|
* @param object $_recipient
|
|
|
|
* @param object $_config
|
|
|
|
* @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."); }
|
|
|
|
if(!$this->netbios_command) {
|
|
|
|
throw new Exception( 'Winpopup plugin not configured yet. Skipped sending notification message. '.
|
|
|
|
'Please check var "netbios_command" in winpopup backend '.
|
2016-07-11 10:08:11 +02:00
|
|
|
'('.EGW_INCLUDE_ROOT. '/'. self::_appname. '/inc/class.notifications_winpopup.inc.php).');
|
2007-12-05 09:55:10 +01:00
|
|
|
}
|
2007-12-18 16:53:38 +01:00
|
|
|
$this->sender = $_sender;
|
|
|
|
$this->recipient = $_recipient;
|
|
|
|
$this->config = $_config;
|
|
|
|
$this->preferences = $_preferences;
|
2007-12-05 09:55:10 +01:00
|
|
|
}
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-12-05 09:55:10 +01:00
|
|
|
/**
|
|
|
|
* sends notification
|
|
|
|
*
|
|
|
|
* @param array $_messages
|
2007-12-18 16:53:38 +01:00
|
|
|
* @param string $_subject
|
|
|
|
* @param array $_links
|
2007-12-05 09:55:10 +01:00
|
|
|
* @param array $_attachments
|
|
|
|
*/
|
2016-05-06 11:19:36 +02:00
|
|
|
public function send(array $_messages, $_subject = false, $_links = false, $_attachments = false)
|
|
|
|
{
|
|
|
|
unset($_links, $_attachments); // not used
|
|
|
|
|
2007-12-05 09:55:10 +01:00
|
|
|
$user_sessions = array();
|
2016-05-06 11:19:36 +02:00
|
|
|
foreach (Api\Session::session_list(0, 'asc', 'session_dla', true) as $session) {
|
2007-12-10 09:26:29 +01:00
|
|
|
if ($session['session_lid'] == $this->recipient->account_lid. '@'. $GLOBALS['egw_info']['user']['domain']) {
|
2007-12-05 09:55:10 +01:00
|
|
|
if($this->valid_ip($session['session_ip'])) {
|
|
|
|
$user_sessions[] = $session['session_ip'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-12-10 09:26:29 +01:00
|
|
|
if ( empty($user_sessions) ) throw new Exception("User #{$this->recipient->account_id} isn't online. Can't send notification via winpopup");
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-12-18 16:53:38 +01:00
|
|
|
$this->send_winpopup( $this->render_infos($_subject).$_messages['plain'], $user_sessions );
|
2007-12-05 09:55:10 +01:00
|
|
|
return true;
|
|
|
|
}
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-12-05 09:55:10 +01:00
|
|
|
/**
|
|
|
|
* sends the winpopup message via command line string netbios_command specified above
|
|
|
|
*
|
|
|
|
* @param string $_message
|
|
|
|
* @param array $_user_sessions
|
|
|
|
*/
|
|
|
|
private function send_winpopup( $_message, array $_user_sessions ) {
|
|
|
|
foreach($_user_sessions as $user_session) {
|
|
|
|
$ip_octets=explode(".",$user_session);
|
|
|
|
// format the ip_octets to 3 digits each
|
|
|
|
foreach($ip_octets as $id=>$ip_octet) {
|
|
|
|
if(strlen($ip_octet)==1) { $ip_octets[$id] = '00'.$ip_octet; }
|
|
|
|
if(strlen($ip_octet)==2) { $ip_octets[$id] = '0'.$ip_octet; }
|
|
|
|
}
|
2007-12-18 16:53:38 +01:00
|
|
|
$placeholders = array( '/\[MESSAGE\]/' => escapeshellarg($_message), // prevent code injection
|
2007-12-05 09:55:10 +01:00
|
|
|
'/\[1\]/' => $ip_octets[0],
|
|
|
|
'/\[2\]/' => $ip_octets[1],
|
|
|
|
'/\[3\]/' => $ip_octets[2],
|
|
|
|
'/\[4\]/' => $ip_octets[3],
|
|
|
|
'/\[IP\]/' => $user_session,
|
2007-12-14 14:52:28 +01:00
|
|
|
'/\[SENDER\]/' => $this->sender->account_fullname ? escapeshellarg($this->sender->account_fullname) : escapeshellarg($this->sender->account_email),
|
2007-12-05 09:55:10 +01:00
|
|
|
);
|
|
|
|
$command = preg_replace(array_keys($placeholders), $placeholders, $this->netbios_command);
|
2016-05-06 11:19:36 +02:00
|
|
|
$output = $returncode = null;
|
2007-12-05 09:55:10 +01:00
|
|
|
exec($command,$output,$returncode);
|
|
|
|
if($returncode != 0) {
|
|
|
|
throw new Exception("Failed sending notification message via winpopup. Error while executing the specified command.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-12-05 09:55:10 +01:00
|
|
|
/**
|
|
|
|
* checks for a valid IPv4-address without CIDR notation
|
|
|
|
*
|
|
|
|
* @param string $_ip
|
|
|
|
* @return true or false
|
|
|
|
*/
|
|
|
|
private function valid_ip($_ip) {
|
|
|
|
return eregi('^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$',$_ip);
|
|
|
|
}
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-12-18 16:53:38 +01:00
|
|
|
/**
|
|
|
|
* renders additional info from subject
|
|
|
|
*
|
|
|
|
* @param string $_subject
|
|
|
|
* @return plain rendered info as complete string
|
|
|
|
*/
|
|
|
|
private function render_infos($_subject = false) {
|
2008-07-30 08:49:55 +02:00
|
|
|
$newline = "\n";
|
2007-12-18 16:53:38 +01:00
|
|
|
if(!empty($_subject)) { return $_subject.$newline; }
|
|
|
|
return false;
|
|
|
|
}
|
2007-12-05 09:55:10 +01:00
|
|
|
}
|