Project

General

Profile

Download (5.14 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 523855b0 Scott Ullrich
27 b6aed0e1 Scott Ullrich
	pfSense_MODULE:	utils
28 523855b0 Scott Ullrich
29 6b07c15a Matthew Grooms
*/
30
31
/*
32
 * The meta data format used in pfSense is denoted using markers
33
 * followed by the appropriate value or value pair. All markers
34
 * are prefixed with a ##| sequence. The + suffix is used to
35
 * denote the beginning of a tag block followed by the tag name.
36 b37a2e8c Phil Davis
 * A - suffix is used to denote the end of a tag block. Values
37 6b07c15a Matthew Grooms
 * are denoted using the * suffix and can optionally be expressed
38
 * as a key value pair. An example of a metadata tag block ...
39
 *
40
 * ###|+INFO
41
 * ###|*BLAH
42
 * ###|*TEXT=SOME TEXT
43
 * ###|-INFO
44
 *
45
 * After calling read_file_metadata, the result array would
46
 * contain the following information ...
47
 *
48
 * metadata['<filename>']['INFO']['BLAH'][0] == true
49
 * metadata['<filename>']['INFO']['TEXT'][0] == "SOME TEXT"
50
 *
51 b37a2e8c Phil Davis
 * NOTE: All statements must be at the beginning of a line and
52 6b07c15a Matthew Grooms
 * contiguous for a tag. The example shown above would not be
53
 * processed due to the extra ' * ' comment chars.
54
 *
55
 */
56
57
/*
58
 * locate php files for a given path
59
 */
60
61
function list_phpfiles($path, & $found) {
62
63 b37a2e8c Phil Davis
	if (!is_array($found)) {
64 6b07c15a Matthew Grooms
		$found = array();
65 b37a2e8c Phil Davis
	}
66 6b07c15a Matthew Grooms
67
	$dir = opendir($path);
68
	if (!$dir) {
69 8e1fd4fe Renato Botelho
		printf(gettext("list_phpfiles: unable to examine path %s\n"), $path);
70 6b07c15a Matthew Grooms
		return;
71
	}
72
73 b37a2e8c Phil Davis
	while ($fname = readdir($dir)) {
74
		if ($fname == "." || $fname == ".." || $fname[0] == '.') {
75 6b07c15a Matthew Grooms
			continue;
76 b37a2e8c Phil Davis
		}
77
		if (fnmatch('*.php', $fname)) {
78 6b07c15a Matthew Grooms
			$found[] = $fname;
79 b37a2e8c Phil Davis
		}
80 6b07c15a Matthew Grooms
	}
81
}
82
83
/*
84
 * read embedded metadata from a file
85
 */
86
87
function read_file_metadata($fpath, & $metadata, $taglist = false) {
88
89 b37a2e8c Phil Davis
	if (!is_array($metadata)) {
90 6b07c15a Matthew Grooms
		$metadata = array();
91 b37a2e8c Phil Davis
	}
92 6b07c15a Matthew Grooms
93 b37a2e8c Phil Davis
	if ($taglist) {
94 6b07c15a Matthew Grooms
		$taglist = explode(",", $taglist);
95 b37a2e8c Phil Davis
	}
96 6b07c15a Matthew Grooms
97
	$fname = $fpath;
98 6c07db48 Phil Davis
	$slash = strrpos($fname, "/");
99 b37a2e8c Phil Davis
	if ($slash) {
100 6c07db48 Phil Davis
		$fname = substr($fname, $slash + 1);
101 b37a2e8c Phil Davis
	}
102 6b07c15a Matthew Grooms
103
	$fdata = @file_get_contents($fpath);
104
	if (!$fdata) {
105 8e1fd4fe Renato Botelho
		printf(gettext("unable to read %s\n"), $fpath);
106 6b07c15a Matthew Grooms
		continue;
107
	}
108
109
	$offset = 0;
110
111
	$tags = array();
112
113
	while (true) {
114
115
		$tagbeg_off = stripos($fdata, "##|+", $offset);
116 b37a2e8c Phil Davis
		if ($tagbeg_off === false) {
117 6b07c15a Matthew Grooms
			break;
118 b37a2e8c Phil Davis
		}
119 6b07c15a Matthew Grooms
120
		$tagbeg_trm = stripos($fdata, "\n", $tagbeg_off);
121 b37a2e8c Phil Davis
		if ($tagbeg_trm === false) {
122 6b07c15a Matthew Grooms
			break;
123 b37a2e8c Phil Davis
		}
124 6b07c15a Matthew Grooms
125
		$tagend_off = stripos($fdata, "##|-", $tagbeg_trm);
126 b37a2e8c Phil Davis
		if ($tagend_off === false) {
127 6b07c15a Matthew Grooms
			break;
128 b37a2e8c Phil Davis
		}
129 6b07c15a Matthew Grooms
130
		$tagend_trm = stripos($fdata, "\n", $tagend_off);
131 b37a2e8c Phil Davis
		if ($tagend_trm === false) {
132 6b07c15a Matthew Grooms
			break;
133 b37a2e8c Phil Davis
		}
134 6b07c15a Matthew Grooms
135
		$tagbeg_len = $tagbeg_trm - $tagbeg_off;
136
		$tagend_len = $tagend_trm - $tagend_off;
137
138
		$tagbeg = substr($fdata, $tagbeg_off + 4, $tagbeg_len - 4);
139
		$tagend = substr($fdata, $tagend_off + 4, $tagend_len - 4);
140
141
		if ($tagbeg != $tagend) {
142 addc0439 Renato Botelho
			printf(gettext("error: tag mismatch ( %1\$s != %2\$s ) in '%3\$s'%4\$s"), $tagbeg, $tagend, $fpath, "\n");
143 6b07c15a Matthew Grooms
			break;
144
		}
145
146
		$mdata_off = $tagbeg_trm + 1;
147
		$mdata_trm = $tagend_off - 1;
148
		$mdata_len = $mdata_trm - $mdata_off;
149
150
		$mdata = substr($fdata, $mdata_off, $mdata_len);
151
152
		if (!strlen($mdata)) {
153 addc0439 Renato Botelho
			printf(gettext("warning: tag %1\$s has no data in '%2\$s'%3\$s"), $tagbeg, $fpath, "\n");
154 6b07c15a Matthew Grooms
			break;
155
		}
156
157
		$offset = $tagend_trm + 1;
158
159 b37a2e8c Phil Davis
		if (is_array($taglist)) {
160 6c07db48 Phil Davis
			if (!in_array($tagbeg, $taglist)) {
161 6b07c15a Matthew Grooms
				continue;
162 b37a2e8c Phil Davis
			}
163
		}
164 6b07c15a Matthew Grooms
165
		$vals = array();
166
167 6c07db48 Phil Davis
		$lines = explode("\n", $mdata);
168 6b07c15a Matthew Grooms
		foreach ($lines as $line) {
169
170 b37a2e8c Phil Davis
			if (!strlen($line)) {
171 6b07c15a Matthew Grooms
				continue;
172 b37a2e8c Phil Davis
			}
173 6b07c15a Matthew Grooms
174
			$valtag = stripos($line, "##|*");
175
			if ($valtag === false || $valtag) {
176 addc0439 Renato Botelho
				printf(gettext("warning: tag %1\$s has malformed data in '%2\$s'%3\$s"), $tagbeg, $fpath, "\n");
177 6b07c15a Matthew Grooms
				continue;
178
			}
179
180
			$value = substr($line, 4, strlen($line) - 1);
181
			$vlist = explode("=", $value);
182
183
			unset($vname);
184
			unset($vdata);
185
186
			switch (count($vlist)) {
187
				case 1:
188
					$vname = $vlist[0];
189
					$vdata = true;
190
					break;
191
				case 2:
192
					$vname = $vlist[0];
193
					$vdata = $vlist[1];
194
					break;
195
			}
196
197
			if (!isset($vname) || !isset($vdata)) {
198 addc0439 Renato Botelho
				printf(gettext("warning: tag %1\$s has invalid data in '%2\$s'%3\$s"), $tagbeg, $fpath, "\n");
199 6b07c15a Matthew Grooms
				continue;
200
			}
201
202
			$vals[$vname][] = $vdata;
203
		}
204
205 b37a2e8c Phil Davis
		if (count($vals)) {
206 6b07c15a Matthew Grooms
			$tags[$tagbeg] = $vals;
207 b37a2e8c Phil Davis
		}
208 6b07c15a Matthew Grooms
	}
209
210 b37a2e8c Phil Davis
	if (count($tags)) {
211 6b07c15a Matthew Grooms
		$metadata[$fname] = $tags;
212 b37a2e8c Phil Davis
	}
213 6b07c15a Matthew Grooms
}
214
215
?>