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
|
/* soekris */
|
68
|
if(file_exists("/dev/led/error"))
|
69
|
exec("/bin/echo 1 > /dev/led/error");
|
70
|
/* wrap */
|
71
|
if(file_exists("/dev/led/led2"))
|
72
|
exec("/bin/echo f5 > /dev/led/led2");
|
73
|
|
74
|
return $queuekey;
|
75
|
}
|
76
|
|
77
|
function get_notices($category = "all") {
|
78
|
if(file_exists('/tmp/notices')) {
|
79
|
$queue = unserialize(file_get_contents('/tmp/notices'));
|
80
|
if(!$queue) return false;
|
81
|
if($category != 'all') {
|
82
|
foreach($queue as $time => $notice) {
|
83
|
if(strtolower($notice['category']) == strtolower($category))
|
84
|
$toreturn[$time] = $notice;
|
85
|
}
|
86
|
return $toreturn;
|
87
|
} else {
|
88
|
return $queue;
|
89
|
}
|
90
|
} else {
|
91
|
return false;
|
92
|
}
|
93
|
}
|
94
|
|
95
|
function close_notice($id) {
|
96
|
global $notice_path;
|
97
|
require_once("util.inc");
|
98
|
/* soekris */
|
99
|
if(file_exists("/dev/led/error"))
|
100
|
exec("/bin/echo 0 > /dev/led/error");
|
101
|
/* wrap */
|
102
|
if(file_exists("/dev/led/led2"))
|
103
|
exec("/bin/echo 0 > /dev/led/led2");
|
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
|
?>
|