Project

General

Profile

Download (5.12 KB) Statistics
| Branch: | Tag: | Revision:
1
<?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 block. 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 beginning 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

    
64
	$dir = opendir($path);
65
	if (!$dir) {
66
		printf(gettext("list_phpfiles: unable to examine path %s\n"), $path);
67
		return;
68
	}
69

    
70
	while ($fname = readdir($dir)) {
71
		if ($fname == "." || $fname == ".." || $fname[0] == '.') {
72
			continue;
73
		}
74
		if (fnmatch('*.php', $fname)) {
75
			$found[] = $fname;
76
		}
77
	}
78
}
79

    
80
/*
81
 * read embedded metadata from a file
82
 */
83

    
84
function read_file_metadata($fpath, & $metadata, $taglist = false) {
85

    
86
	if (!is_array($metadata)) {
87
		$metadata = array();
88
	}
89

    
90
	if ($taglist) {
91
		$taglist = explode(",", $taglist);
92
	}
93

    
94
	$fname = $fpath;
95
	$slash = strrpos($fname, "/");
96
	if ($slash) {
97
		$fname = substr($fname, $slash + 1);
98
	}
99

    
100
	$fdata = @file_get_contents($fpath);
101
	if (!$fdata) {
102
		printf(gettext("unable to read %s\n"), $fpath);
103
		continue;
104
	}
105

    
106
	$offset = 0;
107

    
108
	$tags = array();
109

    
110
	while (true) {
111

    
112
		$tagbeg_off = stripos($fdata, "##|+", $offset);
113
		if ($tagbeg_off === false) {
114
			break;
115
		}
116

    
117
		$tagbeg_trm = stripos($fdata, "\n", $tagbeg_off);
118
		if ($tagbeg_trm === false) {
119
			break;
120
		}
121

    
122
		$tagend_off = stripos($fdata, "##|-", $tagbeg_trm);
123
		if ($tagend_off === false) {
124
			break;
125
		}
126

    
127
		$tagend_trm = stripos($fdata, "\n", $tagend_off);
128
		if ($tagend_trm === false) {
129
			break;
130
		}
131

    
132
		$tagbeg_len = $tagbeg_trm - $tagbeg_off;
133
		$tagend_len = $tagend_trm - $tagend_off;
134

    
135
		$tagbeg = substr($fdata, $tagbeg_off + 4, $tagbeg_len - 4);
136
		$tagend = substr($fdata, $tagend_off + 4, $tagend_len - 4);
137

    
138
		if ($tagbeg != $tagend) {
139
			printf(gettext("error: tag mismatch ( %1\$s != %2\$s ) in '%3\$s'%4\$s"), $tagbeg, $tagend, $fpath, "\n");
140
			break;
141
		}
142

    
143
		$mdata_off = $tagbeg_trm + 1;
144
		$mdata_trm = $tagend_off - 1;
145
		$mdata_len = $mdata_trm - $mdata_off;
146

    
147
		$mdata = substr($fdata, $mdata_off, $mdata_len);
148

    
149
		if (!strlen($mdata)) {
150
			printf(gettext("warning: tag %1\$s has no data in '%2\$s'%3\$s"), $tagbeg, $fpath, "\n");
151
			break;
152
		}
153

    
154
		$offset = $tagend_trm + 1;
155

    
156
		if (is_array($taglist)) {
157
			if (!in_array($tagbeg, $taglist)) {
158
				continue;
159
			}
160
		}
161

    
162
		$vals = array();
163

    
164
		$lines = explode("\n", $mdata);
165
		foreach ($lines as $line) {
166

    
167
			if (!strlen($line)) {
168
				continue;
169
			}
170

    
171
			$valtag = stripos($line, "##|*");
172
			if ($valtag === false || $valtag) {
173
				printf(gettext("warning: tag %1\$s has malformed data in '%2\$s'%3\$s"), $tagbeg, $fpath, "\n");
174
				continue;
175
			}
176

    
177
			$value = substr($line, 4, strlen($line) - 1);
178
			$vlist = explode("=", $value);
179

    
180
			unset($vname);
181
			unset($vdata);
182

    
183
			switch (count($vlist)) {
184
				case 1:
185
					$vname = $vlist[0];
186
					$vdata = true;
187
					break;
188
				case 2:
189
					$vname = $vlist[0];
190
					$vdata = $vlist[1];
191
					break;
192
			}
193

    
194
			if (!isset($vname) || !isset($vdata)) {
195
				printf(gettext("warning: tag %1\$s has invalid data in '%2\$s'%3\$s"), $tagbeg, $fpath, "\n");
196
				continue;
197
			}
198

    
199
			$vals[$vname][] = $vdata;
200
		}
201

    
202
		if (count($vals)) {
203
			$tags[$tagbeg] = $vals;
204
		}
205
	}
206

    
207
	if (count($tags)) {
208
		$metadata[$fname] = $tags;
209
	}
210
}
211

    
212
?>
(32-32/65)