Project

General

Profile

Download (4.63 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-2018 Rubicon Communications, LLC (Netgate)
8
 * All rights reserved.
9
 *
10
 * 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
 *
14
 * http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * 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
 */
22

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

    
59
	$dir = opendir($path);
60
	if (!$dir) {
61
		printf(gettext("list_phpfiles: unable to examine path %s") . "\n", $path);
62
		return;
63
	}
64

    
65
	while ($fname = readdir($dir)) {
66
		if ($fname == "." || $fname == ".." || $fname[0] == '.') {
67
			continue;
68
		}
69
		if (fnmatch('*.php', $fname)) {
70
			$found[] = $fname;
71
		}
72
	}
73
}
74

    
75
/*
76
 * read embedded metadata from a file
77
 */
78

    
79
function read_file_metadata($fpath, & $metadata, $taglist = false) {
80

    
81
	if (!is_array($metadata)) {
82
		$metadata = array();
83
	}
84

    
85
	if ($taglist) {
86
		$taglist = explode(",", $taglist);
87
	}
88

    
89
	$fname = $fpath;
90
	$slash = strrpos($fname, "/");
91
	if ($slash) {
92
		$fname = substr($fname, $slash + 1);
93
	}
94

    
95
	$fdata = @file_get_contents($fpath);
96
	if (!$fdata) {
97
		printf(gettext("unable to read %s") . "\n", $fpath);
98
		return;
99
	}
100

    
101
	$offset = 0;
102

    
103
	$tags = array();
104

    
105
	while (true) {
106

    
107
		$tagbeg_off = stripos($fdata, "##|+", $offset);
108
		if ($tagbeg_off === false) {
109
			break;
110
		}
111

    
112
		$tagbeg_trm = stripos($fdata, "\n", $tagbeg_off);
113
		if ($tagbeg_trm === false) {
114
			break;
115
		}
116

    
117
		$tagend_off = stripos($fdata, "##|-", $tagbeg_trm);
118
		if ($tagend_off === false) {
119
			break;
120
		}
121

    
122
		$tagend_trm = stripos($fdata, "\n", $tagend_off);
123
		if ($tagend_trm === false) {
124
			break;
125
		}
126

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

    
149
		$offset = $tagend_trm + 1;
150

    
151
		if (is_array($taglist)) {
152
			if (!in_array($tagbeg, $taglist)) {
153
				continue;
154
			}
155
		}
156

    
157
		$vals = array();
158

    
159
		$lines = explode("\n", $mdata);
160
		foreach ($lines as $line) {
161

    
162
			if (!strlen($line)) {
163
				continue;
164
			}
165

    
166
			$valtag = stripos($line, "##|*");
167
			if ($valtag === false || $valtag) {
168
				printf(gettext("warning: tag %1\$s has malformed data in '%2\$s'%3\$s"), $tagbeg, $fpath, "\n");
169
				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
				printf(gettext("warning: tag %1\$s has invalid data in '%2\$s'%3\$s"), $tagbeg, $fpath, "\n");
191
				continue;
192
			}
193

    
194
			$vals[$vname][] = $vdata;
195
		}
196

    
197
		if (count($vals)) {
198
			$tags[$tagbeg] = $vals;
199
		}
200
	}
201

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

    
207
?>
(30-30/60)