Projet

Général

Profil

Télécharger (5,05 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / etc / inc / meta.inc @ c650b2f7

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 blaock. 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 begining 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
	$dir = opendir($path);
67
	if (!$dir) {
68
		printf(gettext("list_phpfiles: unable to examine path %s\n"), $path);
69
		return;
70
	}
71

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

    
80
/*
81
 * read embedded metadata from a file
82
 */
83

    
84
function read_file_metadata($fpath, & $metadata, $taglist = false) {
85

    
86
	if (!is_array($metadata))
87
		$metadata = array();
88

    
89
	if ($taglist)
90
		$taglist = explode(",", $taglist);
91

    
92
	$fname = $fpath;
93
	$slash = strrpos($fname,"/");
94
	if ($slash)
95
		$fname = substr($fname,$slash + 1);
96

    
97
	$fdata = @file_get_contents($fpath);
98
	if (!$fdata) {
99
		printf(gettext("unable to read %s\n"), $fpath);
100
		continue;
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
		$tagbeg_trm = stripos($fdata, "\n", $tagbeg_off);
114
		if ($tagbeg_trm === false)
115
			break;
116

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

    
121
		$tagend_trm = stripos($fdata, "\n", $tagend_off);
122
		if ($tagend_trm === false)
123
			break;
124

    
125
		$tagbeg_len = $tagbeg_trm - $tagbeg_off;
126
		$tagend_len = $tagend_trm - $tagend_off;
127

    
128
		$tagbeg = substr($fdata, $tagbeg_off + 4, $tagbeg_len - 4);
129
		$tagend = substr($fdata, $tagend_off + 4, $tagend_len - 4);
130

    
131
		if ($tagbeg != $tagend) {
132
			printf(gettext("error: tag mismatch ( %1\$s != %2\$s ) in '%3\$s'%4\$s"), $tagbeg, $tagend, $fpath, "\n");
133
			break;
134
		}
135

    
136
		$mdata_off = $tagbeg_trm + 1;
137
		$mdata_trm = $tagend_off - 1;
138
		$mdata_len = $mdata_trm - $mdata_off;
139

    
140
		$mdata = substr($fdata, $mdata_off, $mdata_len);
141

    
142
		if (!strlen($mdata)) {
143
			printf(gettext("warning: tag %1\$s has no data in '%2\$s'%3\$s"), $tagbeg, $fpath, "\n");
144
			break;
145
		}
146

    
147
		$offset = $tagend_trm + 1;
148

    
149
		if (is_array($taglist))
150
			if (!in_array($tagbeg,$taglist))
151
				continue;
152

    
153
		$vals = array();
154

    
155
		$lines = explode("\n",$mdata);
156
		foreach ($lines as $line) {
157

    
158
			if (!strlen($line)) 
159
				continue;
160

    
161
			$valtag = stripos($line, "##|*");
162
			if ($valtag === false || $valtag) {
163
				printf(gettext("warning: tag %1\$s has malformed data in '%2\$s'%3\$s"), $tagbeg, $fpath, "\n");
164
				continue;
165
			}
166

    
167
			$value = substr($line, 4, strlen($line) - 1);
168
			$vlist = explode("=", $value);
169

    
170
			unset($vname);
171
			unset($vdata);
172

    
173
			switch (count($vlist)) {
174
				case 1:
175
					$vname = $vlist[0];
176
					$vdata = true;
177
					break;
178
				case 2:
179
					$vname = $vlist[0];
180
					$vdata = $vlist[1];
181
					break;
182
			}
183

    
184
			if (!isset($vname) || !isset($vdata)) {
185
				printf(gettext("warning: tag %1\$s has invalid data in '%2\$s'%3\$s"), $tagbeg, $fpath, "\n");
186
				continue;
187
			}
188

    
189
			$vals[$vname][] = $vdata;
190
		}
191

    
192
		if (count($vals))
193
			$tags[$tagbeg] = $vals;
194
	}
195

    
196
	if (count($tags))
197
		$metadata[$fname] = $tags;
198
}
199

    
200
?>
(33-33/68)