summaryrefslogtreecommitdiffstats
path: root/etc/inc/notices.inc
blob: e2d5bd9df85fd9f7ab318ec824645df00d47a584 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
/****h* pfSense/notices
 * NAME
 *   notices.inc - pfSense notice utilities
 * DESCRIPTION
 *   This include contains the pfSense notice facilities.
 * HISTORY
 *   $Id$
 ******
 *
 * Copyright (C) 2005 Colin Smith (ethethlay@gmail.com)
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */

require_once("globals.inc");

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

/*
 * $category - Category that this notice should be displayed under. This can be arbitrary,
 * 	       but a page must be set to receive this messages for it to be displayed.
 *
 * $priority - A notice's priority. Higher numbers indicate greater severity.
 *	       0 = informational, 1 = warning, 2 = error, etc. This may also be arbitrary,
 */
function file_notice($id, $notice, $category = "General", $url = "", $priority = 1) {
	global $notice_path;
	if(!$queue = get_notices()) $queue = array();
	$queuekey = time();
	$toqueue = array(
				'id'		=> $id,
				'notice'	=> $notice,
				'url'		=> $url,
				'category'	=> $category,
				'priority'	=> $priority,
			);
	$queue[$queuekey] = $toqueue;
	$queueout = fopen($notice_path, "w");
	if(!$queueout) {
		log_error("Could not open {$notice_path} for writing");
		return;
	}
	fwrite($queueout, serialize($queue));
	fclose($queueout);
	log_error("New alert found: {$notice}");
	/* soekris */
	if(file_exists("/dev/led/error"))
		exec("/bin/echo 1 > /dev/led/error");
	/* wrap */
	if(file_exists("/dev/led/led2"))
		exec("/bin/echo f5 > /dev/led/led2");
	notify_via_growl($notice);
	return $queuekey;
}

function get_notices($category = "all") {
	if(file_exists('/tmp/notices')) {
		$queue = unserialize(file_get_contents('/tmp/notices'));
		if(!$queue) return false;
		if($category != 'all') {
			foreach($queue as $time => $notice) {
				if(strtolower($notice['category']) == strtolower($category))
					$toreturn[$time] = $notice;
			}
			return $toreturn;
		} else {
			return $queue;
		}
	} else {
		return false;
	}
}

function close_notice($id) {
	global $notice_path;
	require_once("util.inc");
	/* soekris */
	if(file_exists("/dev/led/error"))
		exec("/bin/echo 0 > /dev/led/error");
	/* wrap */
	if(file_exists("/dev/led/led2"))
		exec("/bin/echo 0 > /dev/led/led2");
	$ids = array();
	if(!$notices = get_notices()) return;
	if($id == "all") {
		unlink_if_exists($notice_path);
		return;
	}
	foreach(array_keys($notices) as $time) {
		if($id == $time) {
			unset($notices[$id]);
			break;
		}
	}
	foreach($notices as $key => $notice) {
		$ids[$key] = $notice['id'];
	}
	foreach($ids as $time => $tocheck) {
		if($id == $tocheck) {
			unset($notices[$time]);
			break;
		}
	}
	if(count($notices) != 0) {
		$queueout = fopen($notice_path, "w");
        	fwrite($queueout, serialize($notices));
        	fclose($queueout);
	} else {
		unlink_if_exists($notice_path);
	}

	return;
}

function dump_xml_notices() {
	require_once("xmlparse.inc");
	global $notice_path, $listtags;
	$listtags[] = 'notice';
	if(!$notices = get_notices()) return;
	foreach($notices as $time => $notice) {
		$notice['time'] = $time;
		$toput['notice'][] = $notice;
	}
	$xml = dump_xml_config($toput, 'notices');
	return $xml;
}

function print_notices($notices, $category = "all") {
	foreach($notices as $notice) {
		if($category != "all") {
			if(in_array($notice['category'], $category)) $categories[] = $notice['category'];
		} else {
			$categories[] = $notice['category'];
		}
	}
	$categories = array_unique($categories);
	sort($categories);
	foreach($categories as $category) {
		$toreturn .= "<ul><li>{$category}<ul>";
		foreach($notices as $notice) {
			if(strtolower($notice['category']) == strtolower($category)) {
				if($notice['id'] != "") {
					if($notice['url'] != "") {
						$toreturn .= "<li><a href={$notice['url']}>{$notice['id']}</a> - {$notice['notice']}</li>";
					} else {
						$toreturn .= "<li>{$notice['id']} - {$notice['notice']}</li>";
					}
				}
			}
		}
		$toreturn .= "</ul></li></ul>";
	}
	return $toreturn;
}

function print_notice_box($category = "all") {
	$notices = get_notices();
	if(!$notices) return;
	print_info_box_np(print_notices($notices, $category));
	return;
}


function are_notices_pending($category = "all") {
	global $notice_path;
	if(file_exists($notice_path)) {
		return true;
	}
	return false;
}

?>
OpenPOWER on IntegriCloud