Project

General

Profile

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