Project

General

Profile

Download (4.87 KB) Statistics
| Branch: | Tag: | Revision:
1 6b07c15a Matthew Grooms
<?php
2
/*
3
	Copyright (C) 2008 Shrew Soft Inc
4
	All rights reserved.
5
6
	Redistribution and use in source and binary forms, with or without
7
	modification, are permitted provided that the following conditions are met:
8
9
	1. Redistributions of source code must retain the above copyright notice,
10
	   this list of conditions and the following disclaimer.
11
12
	2. Redistributions in binary form must reproduce the above copyright
13
	   notice, this list of conditions and the following disclaimer in the
14
	   documentation and/or other materials provided with the distribution.
15
16
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
20
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25
	POSSIBILITY OF SUCH DAMAGE.
26
*/
27
28
/*
29
 * The meta data format used in pfSense is denoted using markers
30
 * followed by the appropriate value or value pair. All markers
31
 * are prefixed with a ##| sequence. The + suffix is used to
32
 * denote the beginning of a tag block followed by the tag name.
33
 * A - suffix is used to denote the end of a tag blaock. Values
34
 * are denoted using the * suffix and can optionally be expressed
35
 * as a key value pair. An example of a metadata tag block ...
36
 *
37
 * ###|+INFO
38
 * ###|*BLAH
39
 * ###|*TEXT=SOME TEXT
40
 * ###|-INFO
41
 *
42
 * After calling read_file_metadata, the result array would
43
 * contain the following information ...
44
 *
45
 * metadata['<filename>']['INFO']['BLAH'][0] == true
46
 * metadata['<filename>']['INFO']['TEXT'][0] == "SOME TEXT"
47
 *
48
 * NOTE: All statements must be at the begining of a line and
49
 * contiguous for a tag. The example shown above would not be
50
 * processed due to the extra ' * ' comment chars.
51
 *
52
 */
53
54
/*
55
 * locate php files for a given path
56
 */
57
58
function list_phpfiles($path, & $found) {
59
60
	if (!is_array($found))
61
		$found = array();
62
63
	$dir = opendir($path);
64
	if (!$dir) {
65
		echo "list_phpfiles: unable to examine path {$path}\n";
66
		return;
67
	}
68
69
	while($fname = readdir($dir)) {
70
		if($fname == "." || $fname == ".." || $fname[0] == '.')
71
			continue;
72
		if (fnmatch('*.php', $fname))
73
			$found[] = $fname;
74
	}
75
}
76
77
/*
78
 * read embedded metadata from a file
79
 */
80
81
function read_file_metadata($fpath, & $metadata, $taglist = false) {
82
83
	if (!is_array($metadata))
84
		$metadata = array();
85
86
	if ($taglist)
87
		$taglist = explode(",", $taglist);
88
89
	$fname = $fpath;
90
	$slash = strrpos($fname,"/");
91
	if ($slash)
92
		$fname = substr($fname,$slash + 1);
93
94
	$fdata = @file_get_contents($fpath);
95
	if (!$fdata) {
96
		echo "unable to read {$fpath}\n";
97
		continue;
98
	}
99
100
	$offset = 0;
101
102
	$tags = array();
103
104
	while (true) {
105
106
		$tagbeg_off = stripos($fdata, "##|+", $offset);
107
		if ($tagbeg_off === false)
108
			break;
109
110
		$tagbeg_trm = stripos($fdata, "\n", $tagbeg_off);
111
		if ($tagbeg_trm === false)
112
			break;
113
114
		$tagend_off = stripos($fdata, "##|-", $tagbeg_trm);
115
		if ($tagend_off === false)
116
			break;
117
118
		$tagend_trm = stripos($fdata, "\n", $tagend_off);
119
		if ($tagend_trm === false)
120
			break;
121
122
		$tagbeg_len = $tagbeg_trm - $tagbeg_off;
123
		$tagend_len = $tagend_trm - $tagend_off;
124
125
		$tagbeg = substr($fdata, $tagbeg_off + 4, $tagbeg_len - 4);
126
		$tagend = substr($fdata, $tagend_off + 4, $tagend_len - 4);
127
128
		if ($tagbeg != $tagend) {
129
			echo "error: tag mismatch ( {$tagbeg} != {$tagend} ) in '$fpath'\n";
130
			break;
131
		}
132
133
		$mdata_off = $tagbeg_trm + 1;
134
		$mdata_trm = $tagend_off - 1;
135
		$mdata_len = $mdata_trm - $mdata_off;
136
137
		$mdata = substr($fdata, $mdata_off, $mdata_len);
138
139
		if (!strlen($mdata)) {
140
			echo "warning: tag {$tagbeg} has no data in '$fpath'\n";
141
			break;
142
		}
143
144
		$offset = $tagend_trm + 1;
145
146
		if (is_array($taglist))
147
			if (!in_array($tagbeg,$taglist))
148
				continue;
149
150
		$vals = array();
151
152
		$lines = explode("\n",$mdata);
153
		foreach ($lines as $line) {
154
155
			if (!strlen($line)) 
156
				continue;
157
158
			$valtag = stripos($line, "##|*");
159
			if ($valtag === false || $valtag) {
160
				echo "warning: tag {$tagbeg} has malformed data in '$fpath'\n";
161
				continue;
162
			}
163
164
			$value = substr($line, 4, strlen($line) - 1);
165
			$vlist = explode("=", $value);
166
167
			unset($vname);
168
			unset($vdata);
169
170
			switch (count($vlist)) {
171
				case 1:
172
					$vname = $vlist[0];
173
					$vdata = true;
174
					break;
175
				case 2:
176
					$vname = $vlist[0];
177
					$vdata = $vlist[1];
178
					break;
179
			}
180
181
			if (!isset($vname) || !isset($vdata)) {
182
				echo "warning: tag {$tagbeg} has invalid data in '$fpath'\n";
183
				continue;
184
			}
185
186
			$vals[$vname][] = $vdata;
187
		}
188
189
		if (count($vals))
190
			$tags[$tagbeg] = $vals;
191
	}
192
193
	if (count($tags))
194
		$metadata[$fname] = $tags;
195
}
196
197
?>