Issues (4868)

inc/class.notifications_winpopup.inc.php (4 issues)

1
<?php
2
/**
3
 * eGroupWare - Notifications
4
 *
5
 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
6
 * @package notifications
7
 * @subpackage backends
8
 * @link http://www.egroupware.org
9
 * @author Christian Binder <[email protected]>
10
 * @version $Id$
11
 */
12
13
use EGroupware\Api;
14
15
/**
16
 * User notification via winpopup.
17
 */
18
class notifications_winpopup implements notifications_iface {
19
20
	/**
21
	 * Appname
22
	 */
23
	const _appname = 'notifications';
0 ignored issues
show
This class constant is not uppercase (expected _APPNAME).
Loading history...
24
25
	/**
26
	 * Login table in SQL database
27
	 */
28
	const _login_table = 'egw_access_log';
0 ignored issues
show
This class constant is not uppercase (expected _LOGIN_TABLE).
Loading history...
29
30
	/**
31
	 * holds account object for user who sends the message
32
	 *
33
	 * @var object
34
	 */
35
	private $sender;
36
37
	/**
38
	 * holds account object for user to notify
39
	 *
40
	 * @var object
41
	 */
42
	private $recipient;
43
44
	/**
45
	 * holds config object (sitewide application config)
46
	 *
47
	 * @var object
48
	 */
49
	private $config;
50
51
	/**
52
	 * holds preferences object of user to notify
53
	 *
54
	 * @var object
55
	 */
56
	private $preferences;
57
58
	/**
59
	 * holds the netbios command to be executed on notification
60
	 *
61
	 * @abstract
62
	 * Example: $netbios_command = "/bin/echo [MESSAGE] | /usr/bin/smbclient -M computer-[4] -I [IP] -U [SENDER]";
63
	 *
64
	 * Placeholders are:
65
	 * [MESSAGE] is the notification message itself
66
	 * [1] - [4] are the IP-Octets of the windows machine to notify
67
	 * [IP] is the IP-Adress of the windows machine to notify
68
	 * [SENDER] is the sender of the message
69
	 * Note: the webserver-user needs execute rights for this command
70
	 *
71
	 * @var string
72
	 */
73
	private $netbios_command = false;
74
75
	/**
76
	 * constructor of notifications_winpopup
77
	 *
78
	 * @param object $_sender
79
	 * @param object $_recipient
80
	 * @param object $_config
81
	 * @param object $_preferences
82
	 */
83
	public function __construct($_sender, $_recipient, $_config = null, $_preferences = null) {
84
		if(!is_object($_sender)) { throw new Exception("no sender given."); }
85
		if(!is_object($_recipient)) { throw new Exception("no recipient given."); }
86
		if(!$this->netbios_command) {
87
			throw new Exception(	'Winpopup plugin not configured yet. Skipped sending notification message. '.
88
									'Please check var "netbios_command" in winpopup backend '.
89
									'('.EGW_INCLUDE_ROOT. '/'. self::_appname. '/inc/class.notifications_winpopup.inc.php).');
90
		}
91
		$this->sender = $_sender;
92
		$this->recipient = $_recipient;
93
		$this->config = $_config;
94
		$this->preferences = $_preferences;
95
	}
96
97
	/**
98
	 * sends notification
99
	 *
100
	 * @param array $_messages
101
	 * @param string $_subject
102
	 * @param array $_links
103
	 * @param array $_attachments
104
	 * @param array $_data
105
	 */
106
	public function send(array $_messages, $_subject = false, $_links = false, $_attachments = false, $_data = false)
107
	{
108
		unset($_links, $_attachments, $_data);	// not used
109
110
		$user_sessions = array();
111
		foreach (Api\Session::session_list(0, 'asc', 'session_dla', true) as $session) {
112
			if ($session['session_lid'] == $this->recipient->account_lid. '@'. $GLOBALS['egw_info']['user']['domain']) {
113
				if($this->valid_ip($session['session_ip'])) {
114
					$user_sessions[] = $session['session_ip'];
115
				}
116
			}
117
		}
118
		if ( empty($user_sessions) ) throw new Exception("User #{$this->recipient->account_id} isn't online. Can't send notification via winpopup");
119
120
		$this->send_winpopup( $this->render_infos($_subject).$_messages['plain'], $user_sessions );
121
		return true;
122
	}
123
124
	/**
125
	 * sends the winpopup message via command line string netbios_command specified above
126
	 *
127
	 * @param string $_message
128
	 * @param array $_user_sessions
129
	 */
130
	private function send_winpopup( $_message, array $_user_sessions ) {
131
		foreach($_user_sessions as $user_session) {
132
			$ip_octets=explode(".",$user_session);
133
			// format the ip_octets to 3 digits each
134
			foreach($ip_octets as $id=>$ip_octet) {
135
				if(strlen($ip_octet)==1) { $ip_octets[$id] = '00'.$ip_octet; }
136
				if(strlen($ip_octet)==2) { $ip_octets[$id] = '0'.$ip_octet; }
137
			}
138
			$placeholders = array(	'/\[MESSAGE\]/' => escapeshellarg($_message), // prevent code injection
139
									'/\[1\]/' => $ip_octets[0],
140
									'/\[2\]/' => $ip_octets[1],
141
									'/\[3\]/' => $ip_octets[2],
142
									'/\[4\]/' => $ip_octets[3],
143
									'/\[IP\]/' => $user_session,
144
									'/\[SENDER\]/' => $this->sender->account_fullname ? escapeshellarg($this->sender->account_fullname) : escapeshellarg($this->sender->account_email),
145
									);
146
			$command = preg_replace(array_keys($placeholders), $placeholders, $this->netbios_command);
147
			$output = $returncode = null;
148
			exec($command,$output,$returncode);
149
			if($returncode != 0) {
150
				throw new Exception("Failed sending notification message via winpopup. Error while executing the specified command.");
151
			}
152
		}
153
	}
154
155
	/**
156
	 * checks for a valid IPv4-address without CIDR notation
157
	 *
158
	 * @param string $_ip
159
	 * @return true or false
160
	 */
161
	private function valid_ip($_ip) {
162
		return eregi('^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$',$_ip);
0 ignored issues
show
Deprecated Code introduced by
The function eregi() has been deprecated: 5.3.0 Use preg_match() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

162
		return /** @scrutinizer ignore-deprecated */ eregi('^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$',$_ip);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
163
	}
164
165
	/**
166
	 * renders additional info from subject
167
	 *
168
	 * @param string $_subject
169
	 * @return plain rendered info as complete string
0 ignored issues
show
The type plain was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
170
	 */
171
	private function render_infos($_subject = false) {
172
		$newline = "\n";
173
		if(!empty($_subject)) { return $_subject.$newline; }
174
		return false;
175
	}
176
}