Project

General

Profile

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