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-2013 BSD Perimeter
|
8
|
* Copyright (c) 2013-2016 Electric Sheep Fencing
|
9
|
* Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
|
10
|
* All rights reserved.
|
11
|
*
|
12
|
* 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
|
*
|
16
|
* http://www.apache.org/licenses/LICENSE-2.0
|
17
|
*
|
18
|
* 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
|
*/
|
24
|
|
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
|
* A - suffix is used to denote the end of a tag block. Values
|
31
|
* 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
|
* NOTE: All statements must be at the beginning of a line and
|
46
|
* 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
|
if (!is_array($found)) {
|
58
|
$found = array();
|
59
|
}
|
60
|
|
61
|
$dir = opendir($path);
|
62
|
if (!$dir) {
|
63
|
printf(gettext("list_phpfiles: unable to examine path %s") . "\n", $path);
|
64
|
return;
|
65
|
}
|
66
|
|
67
|
while ($fname = readdir($dir)) {
|
68
|
if ($fname == "." || $fname == ".." || $fname[0] == '.') {
|
69
|
continue;
|
70
|
}
|
71
|
if (fnmatch('*.php', $fname)) {
|
72
|
$found[] = $fname;
|
73
|
}
|
74
|
}
|
75
|
}
|
76
|
|
77
|
/*
|
78
|
* read embedded metadata from a file
|
79
|
*/
|
80
|
|
81
|
function read_file_metadata($fpath, & $metadata, $taglist = false) {
|
82
|
|
83
|
if (!is_array($metadata)) {
|
84
|
$metadata = array();
|
85
|
}
|
86
|
|
87
|
if ($taglist) {
|
88
|
$taglist = explode(",", $taglist);
|
89
|
}
|
90
|
|
91
|
$fname = $fpath;
|
92
|
$slash = strrpos($fname, "/");
|
93
|
if ($slash) {
|
94
|
$fname = substr($fname, $slash + 1);
|
95
|
}
|
96
|
|
97
|
$fdata = @file_get_contents($fpath);
|
98
|
if (!$fdata) {
|
99
|
printf(gettext("unable to read %s") . "\n", $fpath);
|
100
|
return;
|
101
|
}
|
102
|
|
103
|
$offset = 0;
|
104
|
|
105
|
$tags = array();
|
106
|
|
107
|
while (true) {
|
108
|
|
109
|
$tagbeg_off = stripos($fdata, "##|+", $offset);
|
110
|
if ($tagbeg_off === false) {
|
111
|
break;
|
112
|
}
|
113
|
|
114
|
$tagbeg_trm = stripos($fdata, "\n", $tagbeg_off);
|
115
|
if ($tagbeg_trm === false) {
|
116
|
break;
|
117
|
}
|
118
|
|
119
|
$tagend_off = stripos($fdata, "##|-", $tagbeg_trm);
|
120
|
if ($tagend_off === false) {
|
121
|
break;
|
122
|
}
|
123
|
|
124
|
$tagend_trm = stripos($fdata, "\n", $tagend_off);
|
125
|
if ($tagend_trm === false) {
|
126
|
break;
|
127
|
}
|
128
|
|
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
|
printf(gettext("error: tag mismatch ( %1\$s != %2\$s ) in '%3\$s'%4\$s"), $tagbeg, $tagend, $fpath, "\n");
|
137
|
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
|
printf(gettext("warning: tag %1\$s has no data in '%2\$s'%3\$s"), $tagbeg, $fpath, "\n");
|
148
|
break;
|
149
|
}
|
150
|
|
151
|
$offset = $tagend_trm + 1;
|
152
|
|
153
|
if (is_array($taglist)) {
|
154
|
if (!in_array($tagbeg, $taglist)) {
|
155
|
continue;
|
156
|
}
|
157
|
}
|
158
|
|
159
|
$vals = array();
|
160
|
|
161
|
$lines = explode("\n", $mdata);
|
162
|
foreach ($lines as $line) {
|
163
|
|
164
|
if (!strlen($line)) {
|
165
|
continue;
|
166
|
}
|
167
|
|
168
|
$valtag = stripos($line, "##|*");
|
169
|
if ($valtag === false || $valtag) {
|
170
|
printf(gettext("warning: tag %1\$s has malformed data in '%2\$s'%3\$s"), $tagbeg, $fpath, "\n");
|
171
|
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
|
printf(gettext("warning: tag %1\$s has invalid data in '%2\$s'%3\$s"), $tagbeg, $fpath, "\n");
|
193
|
continue;
|
194
|
}
|
195
|
|
196
|
$vals[$vname][] = $vdata;
|
197
|
}
|
198
|
|
199
|
if (count($vals)) {
|
200
|
$tags[$tagbeg] = $vals;
|
201
|
}
|
202
|
}
|
203
|
|
204
|
if (count($tags)) {
|
205
|
$metadata[$fname] = $tags;
|
206
|
}
|
207
|
}
|
208
|
|
209
|
?>
|