Project

General

Profile

Download (5.14 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
	pfSense_MODULE:	utils
28

    
29
*/
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
 * A - suffix is used to denote the end of a tag block. Values
37
 * 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
 * NOTE: All statements must be at the beginning of a line and
52
 * 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
	if (!is_array($found)) {
64
		$found = array();
65
	}
66

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

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

    
83
/*
84
 * read embedded metadata from a file
85
 */
86

    
87
function read_file_metadata($fpath, & $metadata, $taglist = false) {
88

    
89
	if (!is_array($metadata)) {
90
		$metadata = array();
91
	}
92

    
93
	if ($taglist) {
94
		$taglist = explode(",", $taglist);
95
	}
96

    
97
	$fname = $fpath;
98
	$slash = strrpos($fname, "/");
99
	if ($slash) {
100
		$fname = substr($fname, $slash + 1);
101
	}
102

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

    
109
	$offset = 0;
110

    
111
	$tags = array();
112

    
113
	while (true) {
114

    
115
		$tagbeg_off = stripos($fdata, "##|+", $offset);
116
		if ($tagbeg_off === false) {
117
			break;
118
		}
119

    
120
		$tagbeg_trm = stripos($fdata, "\n", $tagbeg_off);
121
		if ($tagbeg_trm === false) {
122
			break;
123
		}
124

    
125
		$tagend_off = stripos($fdata, "##|-", $tagbeg_trm);
126
		if ($tagend_off === false) {
127
			break;
128
		}
129

    
130
		$tagend_trm = stripos($fdata, "\n", $tagend_off);
131
		if ($tagend_trm === false) {
132
			break;
133
		}
134

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

    
157
		$offset = $tagend_trm + 1;
158

    
159
		if (is_array($taglist)) {
160
			if (!in_array($tagbeg, $taglist)) {
161
				continue;
162
			}
163
		}
164

    
165
		$vals = array();
166

    
167
		$lines = explode("\n", $mdata);
168
		foreach ($lines as $line) {
169

    
170
			if (!strlen($line)) {
171
				continue;
172
			}
173

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

    
202
			$vals[$vname][] = $vdata;
203
		}
204

    
205
		if (count($vals)) {
206
			$tags[$tagbeg] = $vals;
207
		}
208
	}
209

    
210
	if (count($tags)) {
211
		$metadata[$fname] = $tags;
212
	}
213
}
214

    
215
?>
(33-33/67)