Project

General

Profile

Download (6.21 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) 2005 Colin Smith (ethethlay@gmail.com)
12
 * All rights reserved.
13
 * Redistribution and use in source and binary forms, with or without
14
 * modification, are permitted provided that the following conditions are met:
15
 *
16
 * 1. Redistributions of source code must retain the above copyright notice,
17
 * this list of conditions and the following disclaimer.
18
 *
19
 * 2. Redistributions in binary form must reproduce the above copyright
20
 * notice, this list of conditions and the following disclaimer in the
21
 * documentation and/or other materials provided with the distribution.
22
 *
23
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26
 * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
27
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
 * RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
 * POSSIBILITY OF SUCH DAMAGE.
33
 *
34
 */
35

    
36
require_once("globals.inc");
37
require_once("led.inc");
38

    
39
$notice_path = $g['tmp_path'] . '/notices';
40

    
41
/*
42
 * $category - Category that this notice should be displayed under. This can be arbitrary,
43
 * 	       but a page must be set to receive this messages for it to be displayed.
44
 *
45
 * $priority - A notice's priority. Higher numbers indicate greater severity.
46
 *	       0 = informational, 1 = warning, 2 = error, etc. This may also be arbitrary,
47
 */
48
function file_notice($id, $notice, $category = "General", $url = "", $priority = 1) {
49
	global $notice_path;
50
	if(!$queue = get_notices()) $queue = array();
51
	$queuekey = time();
52
	$toqueue = array(
53
				'id'		=> $id,
54
				'notice'	=> $notice,
55
				'url'		=> $url,
56
				'category'	=> $category,
57
				'priority'	=> $priority,
58
			);
59
	$queue[$queuekey] = $toqueue;
60
	$queueout = fopen($notice_path, "w");
61
	if(!$queueout) {
62
		log_error("Could not open {$notice_path} for writing");
63
		return;
64
	}
65
	fwrite($queueout, serialize($queue));
66
	fclose($queueout);
67
	log_error("New alert found: {$notice}");
68
	/* soekris */
69
	if(file_exists("/dev/led/error"))
70
		exec("/bin/echo 1 > /dev/led/error");
71
	/* wrap & alix */
72
	led_normalize();
73
	led_morse(1, 'sos');
74
	notify_via_growl($notice);
75
	return $queuekey;
76
}
77

    
78
function get_notices($category = "all") {
79
	if(file_exists('/tmp/notices')) {
80
		$queue = unserialize(file_get_contents('/tmp/notices'));
81
		if(!$queue) return false;
82
		if($category != 'all') {
83
			foreach($queue as $time => $notice) {
84
				if(strtolower($notice['category']) == strtolower($category))
85
					$toreturn[$time] = $notice;
86
			}
87
			return $toreturn;
88
		} else {
89
			return $queue;
90
		}
91
	} else {
92
		return false;
93
	}
94
}
95

    
96
function close_notice($id) {
97
	global $notice_path;
98
	require_once("util.inc");
99
	/* soekris */
100
	if(file_exists("/dev/led/error"))
101
		exec("/bin/echo 0 > /dev/led/error");
102
	/* wrap & alix */
103
	led_normalize();
104
	$ids = array();
105
	if(!$notices = get_notices()) return;
106
	if($id == "all") {
107
		unlink_if_exists($notice_path);
108
		return;
109
	}
110
	foreach(array_keys($notices) as $time) {
111
		if($id == $time) {
112
			unset($notices[$id]);
113
			break;
114
		}
115
	}
116
	foreach($notices as $key => $notice) {
117
		$ids[$key] = $notice['id'];
118
	}
119
	foreach($ids as $time => $tocheck) {
120
		if($id == $tocheck) {
121
			unset($notices[$time]);
122
			break;
123
		}
124
	}
125
	if(count($notices) != 0) {
126
		$queueout = fopen($notice_path, "w");
127
        	fwrite($queueout, serialize($notices));
128
        	fclose($queueout);
129
	} else {
130
		unlink_if_exists($notice_path);
131
	}
132

    
133
	return;
134
}
135

    
136
function dump_xml_notices() {
137
	require_once("xmlparse.inc");
138
	global $notice_path, $listtags;
139
	$listtags[] = 'notice';
140
	if(!$notices = get_notices()) return;
141
	foreach($notices as $time => $notice) {
142
		$notice['time'] = $time;
143
		$toput['notice'][] = $notice;
144
	}
145
	$xml = dump_xml_config($toput, 'notices');
146
	return $xml;
147
}
148

    
149
function print_notices($notices, $category = "all") {
150
	foreach($notices as $notice) {
151
		if($category != "all") {
152
			if(in_array($notice['category'], $category)) $categories[] = $notice['category'];
153
		} else {
154
			$categories[] = $notice['category'];
155
		}
156
	}
157
	$categories = array_unique($categories);
158
	sort($categories);
159
	foreach($categories as $category) {
160
		$toreturn .= "<ul><li>{$category}<ul>";
161
		foreach($notices as $notice) {
162
			if(strtolower($notice['category']) == strtolower($category)) {
163
				if($notice['id'] != "") {
164
					if($notice['url'] != "") {
165
						$toreturn .= "<li><a href={$notice['url']}>{$notice['id']}</a> - {$notice['notice']}</li>";
166
					} else {
167
						$toreturn .= "<li>{$notice['id']} - {$notice['notice']}</li>";
168
					}
169
				}
170
			}
171
		}
172
		$toreturn .= "</ul></li></ul>";
173
	}
174
	return $toreturn;
175
}
176

    
177
function print_notice_box($category = "all") {
178
	$notices = get_notices();
179
	if(!$notices) return;
180
	print_info_box_np(print_notices($notices, $category));
181
	return;
182
}
183

    
184

    
185
function are_notices_pending($category = "all") {
186
	global $notice_path;
187
	if(file_exists($notice_path)) {
188
		return true;
189
	}
190
	return false;
191
}
192

    
193
/****f* pfsense-utils/notify_via_growl
194
 * NAME
195
 *   notify_via_growl
196
 * INPUTS
197
 *	 notification string to send
198
 * RESULT
199
 *   returns true if message was sent
200
 ******/
201
function notify_via_growl($message) {
202
	require_once("growl.class");
203
	global $config;
204
	$growl_ip = $config['notifications']['growl']['ipaddress'];
205
	$growl_password = $config['notifications']['growl']['password'];
206
	if($growl_ip) {
207
		$growl = new Growl($growl_ip, $growl_password);
208
		$growl->notify("pfSense growl alert", "pfSense", "{$message}");
209
	}
210
}
211

    
212
/****f* pfsense-utils/register_via_growl
213
 * NAME
214
 *   register_via_growl
215
 * INPUTS
216
 *	 none
217
 * RESULT
218
 *   none
219
 ******/
220
function register_via_growl() {
221
	require_once("growl.class");
222
	global $config;
223
	$growl_ip = $config['notifications']['growl']['ipaddress'];
224
	$growl_password = $config['notifications']['growl']['password'];
225
	if($growl_ip) {
226
		$growl = new Growl($growl_ip, $growl_password);
227
		$growl->register();
228
	}
229
}
230

    
231
?>
(21-21/43)