Projet

Général

Profil

Télécharger (11,2 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / etc / inc / notices.inc @ 34bb5eb0

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(printf(gettext("Could not open %s for writing"), $notice_path));
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
	global $g;
103

    
104
	if(file_exists("{$g['tmp_path']}/notices")) {
105
		$queue = unserialize(file_get_contents("{$g['tmp_path']}/notices"));
106
		if(!$queue) return false;
107
		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
/****f* notices/close_notice
122
 * NAME
123
 *   close_notice
124
 * INPUTS
125
 *	 $id
126
 * RESULT
127
 *   Removes a notice from the list
128
 ******/
129
function close_notice($id) {
130
	global $notice_path;
131
	require_once("util.inc");
132
	/* soekris */
133
	if(file_exists("/dev/led/error"))
134
		exec("/bin/echo 0 > /dev/led/error");
135
	/* wrap & alix */
136
	led_normalize();
137
	$ids = array();
138
	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
	if(count($notices) != 0) {
159
		$queueout = fopen($notice_path, "w");
160
        	fwrite($queueout, serialize($notices));
161
        	fclose($queueout);
162
	} else {
163
		unlink_if_exists($notice_path);
164
	}
165

    
166
	return;
167
}
168

    
169
/****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
function dump_xml_notices() {
178
	if(file_exists("/cf/conf/use_xmlreader"))
179
		require_once("xmlreader.inc");
180
	else
181
		require_once("xmlparse.inc");
182
	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
/****f* notices/print_notices
194
 * NAME
195
 *   print_notices
196
 * INPUTS
197
 *	 $notices, $category
198
 * RESULT
199
 *   prints notices to the GUI
200
 ******/
201
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
			if(strtolower($notice['category']) == strtolower($category)) {
215
				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
/****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
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

    
244
/****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
function are_notices_pending($category = "all") {
253
	global $notice_path;
254
	if(file_exists($notice_path)) {
255
		return true;
256
	}
257
	return false;
258
}
259

    
260
/****f* notices/notify_via_smtp
261
 * 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
function notify_via_smtp($message, $force = false) {
269
	global $config, $g;
270
	if($g['booting'])
271
		return;
272

    
273
	if(isset($config['notifications']['smtp']['disable']) && !$force)
274
		return;
275

    
276
	/* Do NOT send the same message twice */
277
	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

    
283
	/* 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
	require_once("sasl.inc");
295
	require_once("smtp.inc");
296

    
297
	if(!$config['notifications']['smtp']['ipaddress'])
298
		return;
299

    
300
	if(!$config['notifications']['smtp']['notifyemailaddress'])
301
		return;
302

    
303
	$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
	$smtp->host_port = empty($config['notifications']['smtp']['port']) ? 25 : $config['notifications']['smtp']['port'];
310

    
311
	$smtp->direct_delivery = 0;
312
	$smtp->ssl = (isset($config['notifications']['smtp']['ssl'])) ? 1 : 0;
313
	$smtp->tls = (isset($config['notifications']['smtp']['tls'])) ? 1 : 0;
314
	$smtp->debug = 0;
315
	$smtp->html_debug = 0;
316
	$smtp->localhost=$config['system']['hostname'].".".$config['system']['domain'];
317
	
318
	if($config['notifications']['smtp']['fromaddress'])
319
		$from = $config['notifications']['smtp']['fromaddress'];
320
	
321
	// Use SMTP Auth if fields are filled out
322
	if($config['notifications']['smtp']['username'] && 
323
	   $config['notifications']['smtp']['password']) {
324
		$smtp->authentication_mechanism = "PLAIN";
325
		$smtp->user = $config['notifications']['smtp']['username'];
326
		$smtp->password = $config['notifications']['smtp']['password'];
327
	}
328

    
329
	$headers = array(
330
		"From: {$from}",
331
		"To: {$to}",
332
		"Subject: {$subject}",
333
		"Date: ".date("r")
334
	);
335

    
336
	if($smtp->SendMessage($from, preg_split('/\s*,\s*/', trim($to)), $headers, $message)) {
337
		log_error(sprintf(gettext("Message sent to %s OK"), $to));
338
		return;
339
	} else {
340
		log_error(sprintf(gettext('Could not send the message to %1$s -- Error: %2$s'), $to, $smtp->error));
341
		return(sprintf(gettext('Could not send the message to %1$s -- Error: %2$s'), $to, $smtp->error));
342
	}
343
}
344

    
345
/****f* notices/notify_via_growl
346
 * NAME
347
 *   notify_via_growl
348
 * INPUTS
349
 *	 notification string to send
350
 * RESULT
351
 *   returns true if message was sent
352
 ******/
353
function notify_via_growl($message, $force=false) {
354
	require_once("growl.class");
355
	global $config,$g;
356

    
357
	if (isset($config['notifications']['growl']['disable']) && !$force)
358
		return;
359

    
360
	/* Do NOT send the same message twice */
361
	if(file_exists("/var/db/growlnotices_lastmsg.txt")) {
362
		$lastmsg = trim(file_get_contents("/var/db/growlnotices_lastmsg.txt"));
363
		if($lastmsg == $message)
364
			return;
365
	}
366

    
367
	$hostname = $config['system']['hostname'] . "." . $config['system']['domain'];
368
	$growl_ip = $config['notifications']['growl']['ipaddress'];
369
	$growl_password = $config['notifications']['growl']['password'];
370
	$growl_name = $config['notifications']['growl']['name'];
371
	$growl_notification = $config['notifications']['growl']['notification_name'];
372
	
373
	if(!empty($growl_ip)) {
374
		$growl = new Growl($growl_ip, $growl_password, $growl_name);
375
		$growl->notify("{$growl_notification}", gettext(sprintf("%s (%s) - Notification", $g['product_name'], $hostname)), "{$message}");
376
	}
377

    
378
	/* Store last message sent to avoid spamming */
379
	$fd = fopen("/var/db/growlnotices_lastmsg.txt", "w");
380
	fwrite($fd, $message);
381
	fclose($fd);
382
}
383

    
384
/****f* notices/register_via_growl
385
 * NAME
386
 *   register_via_growl
387
 * INPUTS
388
 *	 none
389
 * RESULT
390
 *   none
391
 ******/
392
function register_via_growl() {
393
	require_once("growl.class");
394
	global $config;
395
	$growl_ip = $config['notifications']['growl']['ipaddress'];
396
	$growl_password = $config['notifications']['growl']['password'];
397
	$growl_name = $config['notifications']['growl']['name'];
398
	$growl_notification = $config['notifications']['growl']['notification_name'];
399
	
400
	if($growl_ip) {
401
	  $growl = new Growl($growl_ip, $growl_password, $growl_name);
402
		$growl->addNotification($growl_notification);
403
		$growl->register();
404
	}
405
}
406

    
407
/* Notify via remote methods only - not via GUI. */
408
function notify_all_remote($msg) {
409
	notify_via_smtp($msg);
410
	notify_via_growl($msg);
411
}
412

    
413
?>
(33-33/67)