Project

General

Profile

Download (8.96 KB) Statistics
| Branch: | Tag: | Revision:
1
<?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
 * Copyright (C) 2009 Scott Ullrich (sullrich@gmail.com)
12
 * 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
/*
38
	pfSense_BUILDER_BINARIES:	/bin/echo
39
	pfSense_MODULE:	notifications
40
*/
41

    
42
require_once("globals.inc");
43
require_once("led.inc");
44

    
45
$notice_path = $g['tmp_path'] . '/notices';
46

    
47
/****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
function file_notice($id, $notice, $category = "General", $url = "", $priority = 1) {
56
	/*
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
	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
	if(!$queueout) {
76
		log_error("Could not open {$notice_path} for writing");
77
		return;
78
	}
79
	fwrite($queueout, serialize($queue));
80
	fclose($queueout);
81
	log_error("New alert found: {$notice}");
82
	/* soekris */
83
	if(file_exists("/dev/led/error"))
84
		exec("/bin/echo 1 > /dev/led/error");
85
	/* wrap & alix */
86
	led_normalize();
87
	led_morse(1, 'sos');
88
	notify_via_growl($notice);
89
	notify_via_smtp($notice);
90
	return $queuekey;
91
}
92

    
93
/****f* notices/get_notices
94
 * NAME
95
 *   get_notices
96
 * INPUTS
97
 *	 $category
98
 * RESULT
99
 *   Returns a specific notices text
100
 ******/
101
function get_notices($category = "all") {
102
	if(file_exists('/tmp/notices')) {
103
		$queue = unserialize(file_get_contents('/tmp/notices'));
104
		if(!$queue) return false;
105
		if($category != 'all') {
106
			foreach($queue as $time => $notice) {
107
				if(strtolower($notice['category']) == strtolower($category))
108
					$toreturn[$time] = $notice;
109
			}
110
			return $toreturn;
111
		} else {
112
			return $queue;
113
		}
114
	} else {
115
		return false;
116
	}
117
}
118

    
119
/****f* notices/close_notice
120
 * NAME
121
 *   close_notice
122
 * INPUTS
123
 *	 $id
124
 * RESULT
125
 *   Removes a notice from the list
126
 ******/
127
function close_notice($id) {
128
	global $notice_path;
129
	require_once("util.inc");
130
	/* soekris */
131
	if(file_exists("/dev/led/error"))
132
		exec("/bin/echo 0 > /dev/led/error");
133
	/* wrap & alix */
134
	led_normalize();
135
	$ids = array();
136
	if(!$notices = get_notices()) return;
137
	if($id == "all") {
138
		unlink_if_exists($notice_path);
139
		return;
140
	}
141
	foreach(array_keys($notices) as $time) {
142
		if($id == $time) {
143
			unset($notices[$id]);
144
			break;
145
		}
146
	}
147
	foreach($notices as $key => $notice) {
148
		$ids[$key] = $notice['id'];
149
	}
150
	foreach($ids as $time => $tocheck) {
151
		if($id == $tocheck) {
152
			unset($notices[$time]);
153
			break;
154
		}
155
	}
156
	if(count($notices) != 0) {
157
		$queueout = fopen($notice_path, "w");
158
        	fwrite($queueout, serialize($notices));
159
        	fclose($queueout);
160
	} else {
161
		unlink_if_exists($notice_path);
162
	}
163

    
164
	return;
165
}
166

    
167
/****f* notices/dump_xml_notices
168
 * NAME
169
 *   dump_xml_notices
170
 * INPUTS
171
 *	 NONE
172
 * RESULT
173
 *   Outputs notices in XML formatted text
174
 ******/
175
function dump_xml_notices() {
176
	require_once("xmlparse.inc");
177
	global $notice_path, $listtags;
178
	$listtags[] = 'notice';
179
	if(!$notices = get_notices()) return;
180
	foreach($notices as $time => $notice) {
181
		$notice['time'] = $time;
182
		$toput['notice'][] = $notice;
183
	}
184
	$xml = dump_xml_config($toput, 'notices');
185
	return $xml;
186
}
187

    
188
/****f* notices/print_notices
189
 * NAME
190
 *   print_notices
191
 * INPUTS
192
 *	 $notices, $category
193
 * RESULT
194
 *   prints notices to the GUI
195
 ******/
196
function print_notices($notices, $category = "all") {
197
	foreach($notices as $notice) {
198
		if($category != "all") {
199
			if(in_array($notice['category'], $category)) $categories[] = $notice['category'];
200
		} else {
201
			$categories[] = $notice['category'];
202
		}
203
	}
204
	$categories = array_unique($categories);
205
	sort($categories);
206
	foreach($categories as $category) {
207
		$toreturn .= "<ul><li>{$category}<ul>";
208
		foreach($notices as $notice) {
209
			if(strtolower($notice['category']) == strtolower($category)) {
210
				if($notice['id'] != "") {
211
					if($notice['url'] != "") {
212
						$toreturn .= "<li><a href={$notice['url']}>{$notice['id']}</a> - {$notice['notice']}</li>";
213
					} else {
214
						$toreturn .= "<li>{$notice['id']} - {$notice['notice']}</li>";
215
					}
216
				}
217
			}
218
		}
219
		$toreturn .= "</ul></li></ul>";
220
	}
221
	return $toreturn;
222
}
223

    
224
/****f* notices/print_notice_box
225
 * NAME
226
 *   print_notice_box
227
 * INPUTS
228
 *	 $category
229
 * RESULT
230
 *   prints an info box to the GUI
231
 ******/
232
function print_notice_box($category = "all") {
233
	$notices = get_notices();
234
	if(!$notices) return;
235
	print_info_box_np(print_notices($notices, $category));
236
	return;
237
}
238

    
239
/****f* notices/are_notices_pending
240
 * NAME
241
 *   are_notices_pending
242
 * INPUTS
243
 *	 $category to check
244
 * RESULT
245
 *   returns true if notices are pending, false if they are not
246
 ******/
247
function are_notices_pending($category = "all") {
248
	global $notice_path;
249
	if(file_exists($notice_path)) {
250
		return true;
251
	}
252
	return false;
253
}
254

    
255
/****f* notices/notify_via_smtp
256
 * NAME
257
 *   notify_via_smtp
258
 * INPUTS
259
 *	 notification string to send as an email
260
 * RESULT
261
 *   returns true if message was sent
262
 ******/
263
function notify_via_smtp($message) {
264
	global $config;
265

    
266
	if(!$config['notifications']['smtp']['ipaddress'])
267
		return;
268

    
269
	if(!$config['notifications']['smtp']['notifyemailaddress']) 
270
		return;
271

    
272
	/* Do NOT send the same message twice */
273
	if(file_exists("/var/db/notices_lastmsg.txt")) {
274
		$lastmsg = trim(file_get_contents("/var/db/notices_lastmsg.txt"));
275
		if($lastmsg == $message)
276
			return;
277
	}
278

    
279
	require_once("smtp.inc");
280

    
281
	$smtp = new smtp_class;
282

    
283
	$from = "pfsense@{$config['system']['hostname']}.{$config['system']['domain']}";
284
	$to = $config['notifications']['smtp']['notifyemailaddress'];
285

    
286
	$smtp->host_name = $config['notifications']['smtp']['ipaddress'];
287
	$smtp->host_port = 25;
288

    
289
	$smtp->direct_delivery = 0;
290
	$smtp->ssl = 0;
291
	$smtp->debug = 1;
292
	$smtp->html_debug = 0;
293

    
294
	$headers = array(
295
		"From: {$from}",
296
		"To: {$to}",
297
		"Subject: {$config['system']['hostname']}.{$config['system']['domain']} - Notification",
298
		"Date: ".strftime("%a, %d %b %Y %H:%M:%S %Z")
299
	);
300

    
301
	/* Store last message sent to avoid spamming */
302
	$fd = fopen("/var/db/notices_lastmsg.txt", "w");
303
	fwrite($fd, $message);
304
	fclose($fd);
305

    
306
	if($smtp->SendMessage($from, array($to), $headers, $message)) {
307
		log_error("Message sent to {$to} OK");
308
		return;
309
	} else {
310
		log_error("Could not send the message to {$to} -- Error: {$smtp->error}");
311
		return("Could not send the message to {$to} -- Error: {$smtp->error}");
312
	}
313

    
314
}
315

    
316

    
317
/****f* notices/notify_via_growl
318
 * NAME
319
 *   notify_via_growl
320
 * INPUTS
321
 *	 notification string to send
322
 * RESULT
323
 *   returns true if message was sent
324
 ******/
325
function notify_via_growl($message) {
326
	require_once("growl.class");
327
	global $config;
328
	$growl_ip = $config['notifications']['growl']['ipaddress'];
329
	$growl_password = $config['notifications']['growl']['password'];
330
	if($growl_ip) {
331
		$growl = new Growl($growl_ip, $growl_password);
332
		$growl->notify("pfSense growl alert", "pfSense", "{$message}");
333
	}
334
}
335

    
336
/****f* notices/register_via_growl
337
 * NAME
338
 *   register_via_growl
339
 * INPUTS
340
 *	 none
341
 * RESULT
342
 *   none
343
 ******/
344
function register_via_growl() {
345
	require_once("growl.class");
346
	global $config;
347
	$growl_ip = $config['notifications']['growl']['ipaddress'];
348
	$growl_password = $config['notifications']['growl']['password'];
349
	if($growl_ip) {
350
		$growl = new Growl($growl_ip, $growl_password);
351
		$growl->register();
352
	}
353
}
354

    
355
?>
(22-22/44)