Project

General

Profile

Download (4.63 KB) Statistics
| Branch: | Tag: | Revision:
1 6b07c15a Matthew Grooms
<?php
2
/*
3 ac24dc24 Renato Botelho
 * meta.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6 c5d81585 Renato Botelho
 * Copyright (c) 2008 Shrew Soft Inc
7 4a762cf0 Steve Beaver
 * Copyright (c) 2008-2019 Rubicon Communications, LLC (Netgate)
8 ac24dc24 Renato Botelho
 * All rights reserved.
9
 *
10 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13 ac24dc24 Renato Botelho
 *
14 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
15 ac24dc24 Renato Botelho
 *
16 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21 ac24dc24 Renato Botelho
 */
22 6b07c15a Matthew Grooms
23
/*
24
 * The meta data format used in pfSense is denoted using markers
25
 * followed by the appropriate value or value pair. All markers
26
 * are prefixed with a ##| sequence. The + suffix is used to
27
 * denote the beginning of a tag block followed by the tag name.
28 b37a2e8c Phil Davis
 * A - suffix is used to denote the end of a tag block. Values
29 6b07c15a Matthew Grooms
 * are denoted using the * suffix and can optionally be expressed
30
 * as a key value pair. An example of a metadata tag block ...
31
 *
32
 * ###|+INFO
33
 * ###|*BLAH
34
 * ###|*TEXT=SOME TEXT
35
 * ###|-INFO
36
 *
37
 * After calling read_file_metadata, the result array would
38
 * contain the following information ...
39
 *
40
 * metadata['<filename>']['INFO']['BLAH'][0] == true
41
 * metadata['<filename>']['INFO']['TEXT'][0] == "SOME TEXT"
42
 *
43 b37a2e8c Phil Davis
 * NOTE: All statements must be at the beginning of a line and
44 6b07c15a Matthew Grooms
 * contiguous for a tag. The example shown above would not be
45
 * processed due to the extra ' * ' comment chars.
46
 *
47
 */
48
49
/*
50
 * locate php files for a given path
51
 */
52
53
function list_phpfiles($path, & $found) {
54
55 b37a2e8c Phil Davis
	if (!is_array($found)) {
56 6b07c15a Matthew Grooms
		$found = array();
57 b37a2e8c Phil Davis
	}
58 6b07c15a Matthew Grooms
59
	$dir = opendir($path);
60
	if (!$dir) {
61 23afee66 Renato Botelho
		printf(gettext("list_phpfiles: unable to examine path %s") . "\n", $path);
62 6b07c15a Matthew Grooms
		return;
63
	}
64
65 b37a2e8c Phil Davis
	while ($fname = readdir($dir)) {
66
		if ($fname == "." || $fname == ".." || $fname[0] == '.') {
67 6b07c15a Matthew Grooms
			continue;
68 b37a2e8c Phil Davis
		}
69
		if (fnmatch('*.php', $fname)) {
70 6b07c15a Matthew Grooms
			$found[] = $fname;
71 b37a2e8c Phil Davis
		}
72 6b07c15a Matthew Grooms
	}
73
}
74
75
/*
76
 * read embedded metadata from a file
77
 */
78
79
function read_file_metadata($fpath, & $metadata, $taglist = false) {
80
81 b37a2e8c Phil Davis
	if (!is_array($metadata)) {
82 6b07c15a Matthew Grooms
		$metadata = array();
83 b37a2e8c Phil Davis
	}
84 6b07c15a Matthew Grooms
85 b37a2e8c Phil Davis
	if ($taglist) {
86 6b07c15a Matthew Grooms
		$taglist = explode(",", $taglist);
87 b37a2e8c Phil Davis
	}
88 6b07c15a Matthew Grooms
89
	$fname = $fpath;
90 6c07db48 Phil Davis
	$slash = strrpos($fname, "/");
91 b37a2e8c Phil Davis
	if ($slash) {
92 6c07db48 Phil Davis
		$fname = substr($fname, $slash + 1);
93 b37a2e8c Phil Davis
	}
94 6b07c15a Matthew Grooms
95
	$fdata = @file_get_contents($fpath);
96
	if (!$fdata) {
97 23afee66 Renato Botelho
		printf(gettext("unable to read %s") . "\n", $fpath);
98 39f69cb3 Renato Botelho
		return;
99 6b07c15a Matthew Grooms
	}
100
101
	$offset = 0;
102
103
	$tags = array();
104
105
	while (true) {
106
107
		$tagbeg_off = stripos($fdata, "##|+", $offset);
108 b37a2e8c Phil Davis
		if ($tagbeg_off === false) {
109 6b07c15a Matthew Grooms
			break;
110 b37a2e8c Phil Davis
		}
111 6b07c15a Matthew Grooms
112
		$tagbeg_trm = stripos($fdata, "\n", $tagbeg_off);
113 b37a2e8c Phil Davis
		if ($tagbeg_trm === false) {
114 6b07c15a Matthew Grooms
			break;
115 b37a2e8c Phil Davis
		}
116 6b07c15a Matthew Grooms
117
		$tagend_off = stripos($fdata, "##|-", $tagbeg_trm);
118 b37a2e8c Phil Davis
		if ($tagend_off === false) {
119 6b07c15a Matthew Grooms
			break;
120 b37a2e8c Phil Davis
		}
121 6b07c15a Matthew Grooms
122
		$tagend_trm = stripos($fdata, "\n", $tagend_off);
123 b37a2e8c Phil Davis
		if ($tagend_trm === false) {
124 6b07c15a Matthew Grooms
			break;
125 b37a2e8c Phil Davis
		}
126 6b07c15a Matthew Grooms
127
		$tagbeg_len = $tagbeg_trm - $tagbeg_off;
128
		$tagend_len = $tagend_trm - $tagend_off;
129
130
		$tagbeg = substr($fdata, $tagbeg_off + 4, $tagbeg_len - 4);
131
		$tagend = substr($fdata, $tagend_off + 4, $tagend_len - 4);
132
133
		if ($tagbeg != $tagend) {
134 addc0439 Renato Botelho
			printf(gettext("error: tag mismatch ( %1\$s != %2\$s ) in '%3\$s'%4\$s"), $tagbeg, $tagend, $fpath, "\n");
135 6b07c15a Matthew Grooms
			break;
136
		}
137
138
		$mdata_off = $tagbeg_trm + 1;
139
		$mdata_trm = $tagend_off - 1;
140
		$mdata_len = $mdata_trm - $mdata_off;
141
142
		$mdata = substr($fdata, $mdata_off, $mdata_len);
143
144
		if (!strlen($mdata)) {
145 addc0439 Renato Botelho
			printf(gettext("warning: tag %1\$s has no data in '%2\$s'%3\$s"), $tagbeg, $fpath, "\n");
146 6b07c15a Matthew Grooms
			break;
147
		}
148
149
		$offset = $tagend_trm + 1;
150
151 b37a2e8c Phil Davis
		if (is_array($taglist)) {
152 6c07db48 Phil Davis
			if (!in_array($tagbeg, $taglist)) {
153 6b07c15a Matthew Grooms
				continue;
154 b37a2e8c Phil Davis
			}
155
		}
156 6b07c15a Matthew Grooms
157
		$vals = array();
158
159 6c07db48 Phil Davis
		$lines = explode("\n", $mdata);
160 6b07c15a Matthew Grooms
		foreach ($lines as $line) {
161
162 b37a2e8c Phil Davis
			if (!strlen($line)) {
163 6b07c15a Matthew Grooms
				continue;
164 b37a2e8c Phil Davis
			}
165 6b07c15a Matthew Grooms
166
			$valtag = stripos($line, "##|*");
167
			if ($valtag === false || $valtag) {
168 addc0439 Renato Botelho
				printf(gettext("warning: tag %1\$s has malformed data in '%2\$s'%3\$s"), $tagbeg, $fpath, "\n");
169 6b07c15a Matthew Grooms
				continue;
170
			}
171
172
			$value = substr($line, 4, strlen($line) - 1);
173
			$vlist = explode("=", $value);
174
175
			unset($vname);
176
			unset($vdata);
177
178
			switch (count($vlist)) {
179
				case 1:
180
					$vname = $vlist[0];
181
					$vdata = true;
182
					break;
183
				case 2:
184
					$vname = $vlist[0];
185
					$vdata = $vlist[1];
186
					break;
187
			}
188
189
			if (!isset($vname) || !isset($vdata)) {
190 addc0439 Renato Botelho
				printf(gettext("warning: tag %1\$s has invalid data in '%2\$s'%3\$s"), $tagbeg, $fpath, "\n");
191 6b07c15a Matthew Grooms
				continue;
192
			}
193
194
			$vals[$vname][] = $vdata;
195
		}
196
197 b37a2e8c Phil Davis
		if (count($vals)) {
198 6b07c15a Matthew Grooms
			$tags[$tagbeg] = $vals;
199 b37a2e8c Phil Davis
		}
200 6b07c15a Matthew Grooms
	}
201
202 b37a2e8c Phil Davis
	if (count($tags)) {
203 6b07c15a Matthew Grooms
		$metadata[$fname] = $tags;
204 b37a2e8c Phil Davis
	}
205 6b07c15a Matthew Grooms
}
206
207
?>