Project

General

Profile

Download (11.2 KB) Statistics
| Branch: | Tag: | Revision:
1 9774f564 Colin Smith
<?php
2
/****h* pfSense/notices
3
 * NAME
4
 *   notices.inc - pfSense notice utilities
5
 * DESCRIPTION
6
 *   This include contains the pfSense notice facilities.
7
 * HISTORY
8
 *   $Id$
9
 ******
10
 *
11 91cae49d Scott Ullrich
 * Copyright (C) 2009 Scott Ullrich (sullrich@gmail.com)
12 9774f564 Colin Smith
 * Copyright (C) 2005 Colin Smith (ethethlay@gmail.com)
13
 * All rights reserved.
14
 * Redistribution and use in source and binary forms, with or without
15
 * modification, are permitted provided that the following conditions are met:
16
 *
17
 * 1. Redistributions of source code must retain the above copyright notice,
18
 * this list of conditions and the following disclaimer.
19
 *
20
 * 2. Redistributions in binary form must reproduce the above copyright
21
 * notice, this list of conditions and the following disclaimer in the
22
 * documentation and/or other materials provided with the distribution.
23
 *
24
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
25
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
26
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27
 * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
28
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32
 * RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
 * POSSIBILITY OF SUCH DAMAGE.
34
 *
35
 */
36
37 523855b0 Scott Ullrich
/*
38
	pfSense_BUILDER_BINARIES:	/bin/echo
39
	pfSense_MODULE:	notifications
40
*/
41
42 9774f564 Colin Smith
require_once("globals.inc");
43 0c884086 jim-p
require_once("led.inc");
44 9774f564 Colin Smith
45
$notice_path = $g['tmp_path'] . '/notices';
46
47 91cae49d Scott Ullrich
/****f* notices/file_notice
48
 * NAME
49
 *   file_notice
50
 * INPUTS
51
 *	 $id, $notice, $category, $url, $priority
52
 * RESULT
53
 *   Files a notice and kicks off the various alerts, smtp, growl, system log, LED's, etc.
54
 ******/
55 c6e2b163 Colin Smith
function file_notice($id, $notice, $category = "General", $url = "", $priority = 1) {
56 91cae49d Scott Ullrich
	/*
57
	 * $category - Category that this notice should be displayed under. This can be arbitrary,
58
	 * 	       but a page must be set to receive this messages for it to be displayed.
59
	 *
60
	 * $priority - A notice's priority. Higher numbers indicate greater severity.
61
	 *	       0 = informational, 1 = warning, 2 = error, etc. This may also be arbitrary,
62
	 */
63 9774f564 Colin Smith
	global $notice_path;
64
	if(!$queue = get_notices()) $queue = array();
65
	$queuekey = time();
66
	$toqueue = array(
67
				'id'		=> $id,
68
				'notice'	=> $notice,
69
				'url'		=> $url,
70
				'category'	=> $category,
71
				'priority'	=> $priority,
72
			);
73
	$queue[$queuekey] = $toqueue;
74
	$queueout = fopen($notice_path, "w");
75 34e1ff97 Scott Ullrich
	if(!$queueout) {
76 5025dfe3 Carlos Eduardo Ramos
		log_error(printf(gettext("Could not open %s for writing"), $notice_path));
77 34e1ff97 Scott Ullrich
		return;
78
	}
79 9774f564 Colin Smith
	fwrite($queueout, serialize($queue));
80
	fclose($queueout);
81 a120c194 Vinicius Coque
	log_error("New alert found: $notice");
82 a0a125b9 Scott Ullrich
	/* soekris */
83 385b3413 Scott Ullrich
	if(file_exists("/dev/led/error"))
84 5f9bcd7a Scott Ullrich
		exec("/bin/echo 1 > /dev/led/error");
85 0c884086 jim-p
	/* wrap & alix */
86 1c9512f2 jim-p
	led_normalize();
87 0c884086 jim-p
	led_morse(1, 'sos');
88 3b3a7338 Scott Ullrich
	notify_via_growl($notice);
89 b19369b7 Scott Ullrich
	notify_via_smtp($notice);
90 e2c48999 Colin Smith
	return $queuekey;
91 9774f564 Colin Smith
}
92
93 91cae49d Scott Ullrich
/****f* notices/get_notices
94
 * NAME
95
 *   get_notices
96
 * INPUTS
97
 *	 $category
98
 * RESULT
99
 *   Returns a specific notices text
100
 ******/
101 9774f564 Colin Smith
function get_notices($category = "all") {
102 6955830f Ermal Lu?i
	global $g;
103
104
	if(file_exists("{$g['tmp_path']}/notices")) {
105
		$queue = unserialize(file_get_contents("{$g['tmp_path']}/notices"));
106 20d4d05e Colin Smith
		if(!$queue) return false;
107 9774f564 Colin Smith
		if($category != 'all') {
108
			foreach($queue as $time => $notice) {
109
				if(strtolower($notice['category']) == strtolower($category))
110
					$toreturn[$time] = $notice;
111
			}
112
			return $toreturn;
113
		} else {
114
			return $queue;
115
		}
116
	} else {
117
		return false;
118
	}
119
}
120
121 91cae49d Scott Ullrich
/****f* notices/close_notice
122
 * NAME
123
 *   close_notice
124
 * INPUTS
125
 *	 $id
126
 * RESULT
127
 *   Removes a notice from the list
128
 ******/
129 9774f564 Colin Smith
function close_notice($id) {
130
	global $notice_path;
131
	require_once("util.inc");
132 5f9bcd7a Scott Ullrich
	/* soekris */
133
	if(file_exists("/dev/led/error"))
134
		exec("/bin/echo 0 > /dev/led/error");
135 0c884086 jim-p
	/* wrap & alix */
136
	led_normalize();
137 fab14377 Colin Smith
	$ids = array();
138 9774f564 Colin Smith
	if(!$notices = get_notices()) return;
139
	if($id == "all") {
140
		unlink_if_exists($notice_path);
141
		return;
142
	}
143
	foreach(array_keys($notices) as $time) {
144
		if($id == $time) {
145
			unset($notices[$id]);
146
			break;
147
		}
148
	}
149
	foreach($notices as $key => $notice) {
150
		$ids[$key] = $notice['id'];
151
	}
152
	foreach($ids as $time => $tocheck) {
153
		if($id == $tocheck) {
154
			unset($notices[$time]);
155
			break;
156
		}
157
	}
158 fab14377 Colin Smith
	if(count($notices) != 0) {
159 cebc42e6 Colin Smith
		$queueout = fopen($notice_path, "w");
160 fab14377 Colin Smith
        	fwrite($queueout, serialize($notices));
161 cebc42e6 Colin Smith
        	fclose($queueout);
162
	} else {
163
		unlink_if_exists($notice_path);
164
	}
165 a0a125b9 Scott Ullrich
166 669e1adb Bill Marquette
	return;
167 9774f564 Colin Smith
}
168
169 91cae49d Scott Ullrich
/****f* notices/dump_xml_notices
170
 * NAME
171
 *   dump_xml_notices
172
 * INPUTS
173
 *	 NONE
174
 * RESULT
175
 *   Outputs notices in XML formatted text
176
 ******/
177 9774f564 Colin Smith
function dump_xml_notices() {
178 093bcebc Scott Ullrich
	if(file_exists("/cf/conf/use_xmlreader"))
179
		require_once("xmlreader.inc");
180
	else
181
		require_once("xmlparse.inc");
182 9774f564 Colin Smith
	global $notice_path, $listtags;
183
	$listtags[] = 'notice';
184
	if(!$notices = get_notices()) return;
185
	foreach($notices as $time => $notice) {
186
		$notice['time'] = $time;
187
		$toput['notice'][] = $notice;
188
	}
189
	$xml = dump_xml_config($toput, 'notices');
190
	return $xml;
191
}
192
193 91cae49d Scott Ullrich
/****f* notices/print_notices
194
 * NAME
195
 *   print_notices
196
 * INPUTS
197
 *	 $notices, $category
198
 * RESULT
199
 *   prints notices to the GUI
200
 ******/
201 9774f564 Colin Smith
function print_notices($notices, $category = "all") {
202
	foreach($notices as $notice) {
203
		if($category != "all") {
204
			if(in_array($notice['category'], $category)) $categories[] = $notice['category'];
205
		} else {
206
			$categories[] = $notice['category'];
207
		}
208
	}
209
	$categories = array_unique($categories);
210
	sort($categories);
211
	foreach($categories as $category) {
212
		$toreturn .= "<ul><li>{$category}<ul>";
213
		foreach($notices as $notice) {
214 08421c0d Colin Smith
			if(strtolower($notice['category']) == strtolower($category)) {
215 9774f564 Colin Smith
				if($notice['id'] != "") {
216
					if($notice['url'] != "") {
217
						$toreturn .= "<li><a href={$notice['url']}>{$notice['id']}</a> - {$notice['notice']}</li>";
218
					} else {
219
						$toreturn .= "<li>{$notice['id']} - {$notice['notice']}</li>";
220
					}
221
				}
222
			}
223
		}
224
		$toreturn .= "</ul></li></ul>";
225
	}
226
	return $toreturn;
227
}
228
229 91cae49d Scott Ullrich
/****f* notices/print_notice_box
230
 * NAME
231
 *   print_notice_box
232
 * INPUTS
233
 *	 $category
234
 * RESULT
235
 *   prints an info box to the GUI
236
 ******/
237 20d4d05e Colin Smith
function print_notice_box($category = "all") {
238
	$notices = get_notices();
239
	if(!$notices) return;
240
	print_info_box_np(print_notices($notices, $category));
241
	return;
242
}
243 6e2ec568 Scott Ullrich
244 91cae49d Scott Ullrich
/****f* notices/are_notices_pending
245
 * NAME
246
 *   are_notices_pending
247
 * INPUTS
248
 *	 $category to check
249
 * RESULT
250
 *   returns true if notices are pending, false if they are not
251
 ******/
252 34e1ff97 Scott Ullrich
function are_notices_pending($category = "all") {
253
	global $notice_path;
254
	if(file_exists($notice_path)) {
255
		return true;
256
	}
257
	return false;
258 6e2ec568 Scott Ullrich
}
259
260 91cae49d Scott Ullrich
/****f* notices/notify_via_smtp
261 b19369b7 Scott Ullrich
 * NAME
262
 *   notify_via_smtp
263
 * INPUTS
264
 *	 notification string to send as an email
265
 * RESULT
266
 *   returns true if message was sent
267
 ******/
268 48b86f62 jim-p
function notify_via_smtp($message, $force = false) {
269 8273ea35 Scott Ullrich
	global $config, $g;
270
	if($g['booting'])
271
		return;
272 b19369b7 Scott Ullrich
273 48b86f62 jim-p
	if(isset($config['notifications']['smtp']['disable']) && !$force)
274
		return;
275
276 743c30df Scott Ullrich
	/* Do NOT send the same message twice */
277 2c7ac8dc Scott Ullrich
	if(file_exists("/var/db/notices_lastmsg.txt")) {
278
		$lastmsg = trim(file_get_contents("/var/db/notices_lastmsg.txt"));
279
		if($lastmsg == $message)
280
			return;
281
	}
282 743c30df Scott Ullrich
283 7c845149 jim-p
	/* Store last message sent to avoid spamming */
284
	$fd = fopen("/var/db/notices_lastmsg.txt", "w");
285
	fwrite($fd, $message);
286
	fclose($fd);
287
288
	send_smtp_message($message, "{$config['system']['hostname']}.{$config['system']['domain']} - Notification");
289
	return;
290
}
291
292
function send_smtp_message($message, $subject = "(no subject)") {
293
	global $config, $g;
294 2cd8d942 Pierre POMES
	require_once("sasl.inc");
295 4bbb3155 Scott Ullrich
	require_once("smtp.inc");
296
297 7c845149 jim-p
	if(!$config['notifications']['smtp']['ipaddress'])
298
		return;
299
300
	if(!$config['notifications']['smtp']['notifyemailaddress'])
301
		return;
302
303 b19369b7 Scott Ullrich
	$smtp = new smtp_class;
304
305
	$from = "pfsense@{$config['system']['hostname']}.{$config['system']['domain']}";
306
	$to = $config['notifications']['smtp']['notifyemailaddress'];
307
308
	$smtp->host_name = $config['notifications']['smtp']['ipaddress'];
309 9277b7ef jim-p
	$smtp->host_port = empty($config['notifications']['smtp']['port']) ? 25 : $config['notifications']['smtp']['port'];
310 b19369b7 Scott Ullrich
311 96c52992 Scott Ullrich
	$smtp->direct_delivery = 0;
312 2d74f1cf Albert S. Causing
	$smtp->ssl = ($config['notifications']['smtp']['ssl'] == "checked") ? 1 : 0;
313 f602d493 sullrich
	$smtp->debug = 0;
314 b19369b7 Scott Ullrich
	$smtp->html_debug = 0;
315 1467b79f pierrepomes
	$smtp->localhost=$config['system']['hostname'].".".$config['system']['domain'];
316 72306d5a Scott Ullrich
	
317
	if($config['notifications']['smtp']['fromaddress'])
318
		$from = $config['notifications']['smtp']['fromaddress'];
319
	
320
	// Use SMTP Auth if fields are filled out
321
	if($config['notifications']['smtp']['username'] && 
322
	   $config['notifications']['smtp']['password']) {
323
		$smtp->authentication_mechanism = "PLAIN";
324 1edfb2de Pierre POMES
		$smtp->user = $config['notifications']['smtp']['username'];
325 72306d5a Scott Ullrich
		$smtp->password = $config['notifications']['smtp']['password'];
326
	}
327 b19369b7 Scott Ullrich
328
	$headers = array(
329
		"From: {$from}",
330
		"To: {$to}",
331 7c845149 jim-p
		"Subject: {$subject}",
332 4582ef41 jim-p
		"Date: ".date("r")
333 b19369b7 Scott Ullrich
	);
334
335 90dd6423 Darren Embry
	if($smtp->SendMessage($from, preg_split('/\s*,\s*/', trim($to)), $headers, $message)) {
336 5025dfe3 Carlos Eduardo Ramos
		log_error(sprintf(gettext("Message sent to %s OK"), $to));
337 f9fb2569 Scott Ullrich
		return;
338
	} else {
339 addc0439 Renato Botelho
		log_error(sprintf(gettext('Could not send the message to %1$s -- Error: %2$s'), $to, $smtp->error));
340
		return(sprintf(gettext('Could not send the message to %1$s -- Error: %2$s'), $to, $smtp->error));
341 f9fb2569 Scott Ullrich
	}
342 b19369b7 Scott Ullrich
}
343
344 91cae49d Scott Ullrich
/****f* notices/notify_via_growl
345 b21fc797 Scott Ullrich
 * NAME
346
 *   notify_via_growl
347
 * INPUTS
348
 *	 notification string to send
349
 * RESULT
350
 *   returns true if message was sent
351
 ******/
352 48b86f62 jim-p
function notify_via_growl($message, $force=false) {
353 b21fc797 Scott Ullrich
	require_once("growl.class");
354 fea89a63 Warren Baker
	global $config,$g;
355 a93020d5 Scott Ullrich
356 48b86f62 jim-p
	if (isset($config['notifications']['growl']['disable']) && !$force)
357
		return;
358
359 a93020d5 Scott Ullrich
	/* Do NOT send the same message twice */
360
	if(file_exists("/var/db/growlnotices_lastmsg.txt")) {
361
		$lastmsg = trim(file_get_contents("/var/db/growlnotices_lastmsg.txt"));
362
		if($lastmsg == $message)
363
			return;
364
	}
365
366 9a132756 Scott Ullrich
	$hostname = $config['system']['hostname'] . "." . $config['system']['domain'];
367 b21fc797 Scott Ullrich
	$growl_ip = $config['notifications']['growl']['ipaddress'];
368
	$growl_password = $config['notifications']['growl']['password'];
369 addbcae7 Scott Ullrich
	$growl_name = $config['notifications']['growl']['name'];
370
	$growl_notification = $config['notifications']['growl']['notification_name'];
371
	
372 575a42f5 Ermal Lu?i
	if(!empty($growl_ip)) {
373 addbcae7 Scott Ullrich
		$growl = new Growl($growl_ip, $growl_password, $growl_name);
374 fea89a63 Warren Baker
		$growl->notify("{$growl_notification}", gettext(sprintf("%s (%s) - Notification", $g['product_name'], $hostname)), "{$message}");
375 b21fc797 Scott Ullrich
	}
376 a93020d5 Scott Ullrich
377
	/* Store last message sent to avoid spamming */
378
	$fd = fopen("/var/db/growlnotices_lastmsg.txt", "w");
379
	fwrite($fd, $message);
380
	fclose($fd);
381 b21fc797 Scott Ullrich
}
382
383 91cae49d Scott Ullrich
/****f* notices/register_via_growl
384 b21fc797 Scott Ullrich
 * NAME
385
 *   register_via_growl
386
 * INPUTS
387
 *	 none
388
 * RESULT
389
 *   none
390
 ******/
391
function register_via_growl() {
392
	require_once("growl.class");
393
	global $config;
394
	$growl_ip = $config['notifications']['growl']['ipaddress'];
395
	$growl_password = $config['notifications']['growl']['password'];
396 addbcae7 Scott Ullrich
	$growl_name = $config['notifications']['growl']['name'];
397
	$growl_notification = $config['notifications']['growl']['notification_name'];
398
	
399 b21fc797 Scott Ullrich
	if($growl_ip) {
400 addbcae7 Scott Ullrich
	  $growl = new Growl($growl_ip, $growl_password, $growl_name);
401
		$growl->addNotification($growl_notification);
402 b21fc797 Scott Ullrich
		$growl->register();
403
	}
404
}
405
406 53842a6c jim-p
/* Notify via remote methods only - not via GUI. */
407
function notify_all_remote($msg) {
408
	notify_via_smtp($msg);
409
	notify_via_growl($msg);
410
}
411
412 6955830f Ermal Lu?i
?>