Project

General

Profile

Download (5.11 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

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

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

    
72
function get_notices($category = "all") {
73
	if(file_exists('/tmp/notices')) {
74
		$queue = unserialize(file_get_contents('/tmp/notices'));
75
		if(!$queue) return false;
76
		if($category != 'all') {
77
			foreach($queue as $time => $notice) {
78
				if(strtolower($notice['category']) == strtolower($category))
79
					$toreturn[$time] = $notice;
80
			}
81
			return $toreturn;
82
		} else {
83
			return $queue;
84
		}
85
	} else {
86
		return false;
87
	}
88
}
89

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

    
126
function dump_xml_notices() {
127
	require_once("xmlparse.inc");
128
	global $notice_path, $listtags;
129
	$listtags[] = 'notice';
130
	if(!$notices = get_notices()) return;
131
	foreach($notices as $time => $notice) {
132
		$notice['time'] = $time;
133
		$toput['notice'][] = $notice;
134
	}
135
	$xml = dump_xml_config($toput, 'notices');
136
	return $xml;
137
}
138

    
139
function print_notices($notices, $category = "all") {
140
	foreach($notices as $notice) {
141
		if($category != "all") {
142
			if(in_array($notice['category'], $category)) $categories[] = $notice['category'];
143
		} else {
144
			$categories[] = $notice['category'];
145
		}
146
	}
147
	$categories = array_unique($categories);
148
	sort($categories);
149
	foreach($categories as $category) {
150
		$toreturn .= "<ul><li>{$category}<ul>";
151
		foreach($notices as $notice) {
152
			if(strtolower($notice['category']) == strtolower($category)) {
153
				if($notice['id'] != "") {
154
					if($notice['url'] != "") {
155
						$toreturn .= "<li><a href={$notice['url']}>{$notice['id']}</a> - {$notice['notice']}</li>";
156
					} else {
157
						$toreturn .= "<li>{$notice['id']} - {$notice['notice']}</li>";
158
					}
159
				}
160
			}
161
		}
162
		$toreturn .= "</ul></li></ul>";
163
	}
164
	return $toreturn;
165
}
166

    
167
function print_notice_box($category = "all") {
168
	$notices = get_notices();
169
	if(!$notices) return;
170
	print_info_box_np(print_notices($notices, $category));
171
	return;
172
}
173

    
174

    
175
function are_notices_pending($category = "all") {
176
	global $notice_path;
177
	if(file_exists($notice_path)) {
178
		return true;
179
	}
180
	return false;
181
}
182

    
183
?>
(12-12/27)