Project

General

Profile

Download (4.97 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
	return $queuekey;
68
}
69

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

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

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

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

    
163
function print_notice_box($category = "all") {
164
	$notices = get_notices();
165
	if(!$notices) return;
166
	print_info_box_np(print_notices($notices, $category));
167
	return;
168
}
169

    
170

    
171
function are_notices_pending($category = "all") {
172
	global $notice_path;
173
	if(file_exists($notice_path)) {
174
		return true;
175
	}
176
	return false;
177
}
178

    
179
?>
(12-12/27)