Project

General

Profile

Download (6.18 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	meta.inc
4

    
5
	part of pfSense (https://www.pfsense.org)
6
	Copyright (C) 2008 Shrew Soft Inc
7
	Copyright (c) 2008-2016 Electric Sheep Fencing, LLC.
8
	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

    
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
 * A - suffix is used to denote the end of a tag block. Values
61
 * 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
 * NOTE: All statements must be at the beginning of a line and
76
 * 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
	if (!is_array($found)) {
88
		$found = array();
89
	}
90

    
91
	$dir = opendir($path);
92
	if (!$dir) {
93
		printf(gettext("list_phpfiles: unable to examine path %s\n"), $path);
94
		return;
95
	}
96

    
97
	while ($fname = readdir($dir)) {
98
		if ($fname == "." || $fname == ".." || $fname[0] == '.') {
99
			continue;
100
		}
101
		if (fnmatch('*.php', $fname)) {
102
			$found[] = $fname;
103
		}
104
	}
105
}
106

    
107
/*
108
 * read embedded metadata from a file
109
 */
110

    
111
function read_file_metadata($fpath, & $metadata, $taglist = false) {
112

    
113
	if (!is_array($metadata)) {
114
		$metadata = array();
115
	}
116

    
117
	if ($taglist) {
118
		$taglist = explode(",", $taglist);
119
	}
120

    
121
	$fname = $fpath;
122
	$slash = strrpos($fname, "/");
123
	if ($slash) {
124
		$fname = substr($fname, $slash + 1);
125
	}
126

    
127
	$fdata = @file_get_contents($fpath);
128
	if (!$fdata) {
129
		printf(gettext("unable to read %s\n"), $fpath);
130
		continue;
131
	}
132

    
133
	$offset = 0;
134

    
135
	$tags = array();
136

    
137
	while (true) {
138

    
139
		$tagbeg_off = stripos($fdata, "##|+", $offset);
140
		if ($tagbeg_off === false) {
141
			break;
142
		}
143

    
144
		$tagbeg_trm = stripos($fdata, "\n", $tagbeg_off);
145
		if ($tagbeg_trm === false) {
146
			break;
147
		}
148

    
149
		$tagend_off = stripos($fdata, "##|-", $tagbeg_trm);
150
		if ($tagend_off === false) {
151
			break;
152
		}
153

    
154
		$tagend_trm = stripos($fdata, "\n", $tagend_off);
155
		if ($tagend_trm === false) {
156
			break;
157
		}
158

    
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
			printf(gettext("error: tag mismatch ( %1\$s != %2\$s ) in '%3\$s'%4\$s"), $tagbeg, $tagend, $fpath, "\n");
167
			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
			printf(gettext("warning: tag %1\$s has no data in '%2\$s'%3\$s"), $tagbeg, $fpath, "\n");
178
			break;
179
		}
180

    
181
		$offset = $tagend_trm + 1;
182

    
183
		if (is_array($taglist)) {
184
			if (!in_array($tagbeg, $taglist)) {
185
				continue;
186
			}
187
		}
188

    
189
		$vals = array();
190

    
191
		$lines = explode("\n", $mdata);
192
		foreach ($lines as $line) {
193

    
194
			if (!strlen($line)) {
195
				continue;
196
			}
197

    
198
			$valtag = stripos($line, "##|*");
199
			if ($valtag === false || $valtag) {
200
				printf(gettext("warning: tag %1\$s has malformed data in '%2\$s'%3\$s"), $tagbeg, $fpath, "\n");
201
				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
				printf(gettext("warning: tag %1\$s has invalid data in '%2\$s'%3\$s"), $tagbeg, $fpath, "\n");
223
				continue;
224
			}
225

    
226
			$vals[$vname][] = $vdata;
227
		}
228

    
229
		if (count($vals)) {
230
			$tags[$tagbeg] = $vals;
231
		}
232
	}
233

    
234
	if (count($tags)) {
235
		$metadata[$fname] = $tags;
236
	}
237
}
238

    
239
?>
(32-32/65)