[go: up one dir, main page]

Menu

[r19]: / tags / 0.58 / lib / xml.php  Maximize  Restore  History

Download this file

347 lines (228 with data), 8.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?
/****************************************************************************
xml.php
Houses all our xml parsing functions which use simplexml when
available. This file can only be used with PHP 5
****************************************************************************/
/*****************************************************************************
loadSiteStructure
Finds all module.xml files, merges, and parses them. If a
cache file exists, it uses that first. The parsed xml
is then turned into an associative array and returned
*****************************************************************************/
function loadSiteStructure($dir) {
$arr = findModConfig($dir);
$cacheFile = $dir."/module-cache.xml";
//if a cached version of the structure exists, get it instead
//of search through all of the directories
if (file_exists($cacheFile)) {
$str = file_get_contents($cacheFile);
return return_modlist($str);
}
$str = outputXmlHeader();
for ($row=0;$row<count($arr);$row++) {
$path = $arr[$row];
//dynamically add the path of the module. This should be faster overall
$tmp = file_get_contents($arr[$row]);
$path = str_replace("module.xml","",$path);
$search = "<module>\n";
$replace = "<module>\n\t<module_path>".$path."</module_path>\n";
$replace .= "\t<owner_path>".getOwnerPath($path)."</owner_path>\n";
$str .= str_replace($search,$replace,$tmp);
}
$str .= outputXmlFooter();
return return_modlist($str);
}
/******************************************************************
setPermDefines
Loads and parses the permissions.xml file. It then
sets defines based on their bit position
******************************************************************/
function setPermDefines() {
if (!$_SESSION["definePermArray"] || defined("DEV_MODE")) {
if (defined("ALT_FILE_PATH")) $xmlFile = ALT_FILE_PATH."/config/permissions.xml";
else $xmlFile = "config/permissions.xml";
$str = file_get_contents("$xmlFile");
$_SESSION["definePermArray"] = parseGenericXml("perm",$str);
}
$permArray = $_SESSION["definePermArray"];
if ($permArray) {
for ($row=0;$row<count($permArray["name"]);$row++) {
$dn = $permArray["define_name"][$row];
$bitVal = bitCal($permArray["bitpos"][$row]);
define("$dn","$bitVal");
}
} else return false;
}
/******************************************************************
setCustomPermDefines
Loads and parses the customperm.xml file. It then
sets defines based on their bit position
******************************************************************/
function setCustomPermDefines() {
if (!$_SESSION["defineCustomPermArray"] || defined("DEV_MODE")) {
if (defined("ALT_FILE_PATH")) $xmlFile = ALT_FILE_PATH."/config/customperm.xml";
else $xmlFile = "config/customperm.xml";
if (file_exists("$xmlFile")) {
$str = file_get_contents("$xmlFile");
$_SESSION["defineCustomPermArray"] = parseGenericXml("perm",$str);
} else $_SESSION["defineCustomPermArray"] = "ignore";
}
//skip if there are no perms to worry about
if ($_SESSION["defineCustomPermArray"]=="ignore") return false;
$permArray = $_SESSION["defineCustomPermArray"];
if ($permArray) {
for ($row=0;$row<count($permArray["name"]);$row++) {
$dn = $permArray["define_name"][$row];
$bitVal = bitCal($permArray["bitpos"][$row]);
define("$dn","$bitVal");
}
} else return false;
}
/*********************************************************
outputXmlHeader
adds the appropriate header to our xml string
*********************************************************/
function outputXmlHeader() {
$str .= "<data>\n";
return $str;
}
/*********************************************************
outputXmlFooter
adds the appropriate footer to our xml string
*********************************************************/
function outputXmlFooter() {
$str = "</data>\n\n";
return $str;
}
/*********************************************************
findModConfig
returns an array of the relative paths to
all our module.xml files
*********************************************************/
function findModConfig($directory,$resultArray=null) {
if (!is_dir($directory)) return false;
$arr = scandir($directory);
foreach ($arr AS $file) {
//skip directory markers
if ($file=="." || $file==".." || $file==".svn") continue;
//recurse into subdirectories
if (is_dir($directory.$file)) $resultArray = findModConfig($directory.$file."/",$resultArray);
elseif ($file=="module.xml") $resultArray[] = $directory.$file;
}
sort($resultArray);
return $resultArray;
}
/*********************************************************
return_modlist
creates an associative array from our combined
module.xml string
*********************************************************/
function return_modlist($str) {
$info = array();
$list = array();
$xml = simplexml_load_string($str);
$i = 0;
foreach ($xml -> module AS $modArr) {
$fields = null;
$key = (string)$modArr -> link_name;
//if this value has already been set, then there are duplicate modules
//I.E., two modules with the same link name
if (is_array($info[$key]))
die("Module Error! Two modules have the link name <b>\"".$key."\"</b>.");
foreach ($modArr -> children() AS $field => $val) {
if ($field == "count") continue;
//check to make sure we do not have show_module and hide_module in this entry
if (is_array($list["show_module"][$i]) && is_array($list["hide_module"][$i]))
die("Cannot set \"show_module\" and \"hide_module\" simultaneously in \"".$key."\" module.xml");
//these have multiple values, handle that
if ( $field=="permissions" ||
$field=="custom_perm" ||
$field=="show_module" ||
$field=="hide_module") {
$info["$key"][$field][] = (string)$val;
$list[$field][$i][] = (string)$val;
}
else {
$info["$key"][$field] = (string)$val;
$list[$field][$i] = (string)$val;
}
}
$i++;
}
$arr["info"] = $info;
$arr["list"] = arrayMultiSort($list,"sort_order");
return $arr;
}
/*********************************************************
parseGenericXML
parses a generic xml string into an associative
array. It does not handle multiple entries
of a tag within an element.
*********************************************************/
function parseGenericXml($obj,$data,$multi = null) {
$list = array();
$xml = simplexml_load_string($data);
$i = 0;
foreach ($xml -> $obj AS $arr) {
$fields = null;
foreach ($arr -> children() AS $field => $val) {
if ($field == "count") continue;
if (@in_array($field,$multi)) $list[$field][$i][] = (string)$val;
else $list[$field][$i] = (string)$val;
}
$i++;
}
return $list;
}
function xml2array($str) {
$xml = simplexml_load_string($str);
return simplexml2array($xml);
}
//taken directly from the php website
function simplexml2array($xml) {
if (get_class($xml) == 'SimpleXMLElement') {
$attributes = $xml->attributes();
foreach($attributes as $k=>$v) {
if ($v) $a[$k] = (string) $v;
}
$x = $xml;
$xml = get_object_vars($xml);
}
if (is_array($xml)) {
if (count($xml) == 0) return (string) $x; // for CDATA
foreach($xml as $key=>$value) {
$r[$key] = simplexml2array($value);
}
if (isset($a)) $r['@'] = $a;// Attributes
return $r;
}
return (string) $xml;
}
//this function creates an xml header with typeid so we can process and associate
//the proper response handler with the returned data
function createXmlHeader($type) {
header("Content-Type: text/xml");
//use a default dbencoding
if (!defined("VIEW_CHARSET")) define("VIEW_CHARSET","ISO-8859-1");
$str = "<?xml version=\"1.0\" encoding=\"".VIEW_CHARSET."\" standalone=\"yes\"?>\n";
$str .= "<data>\n";
$str .= "\t<typeid>".$type."</typeid>\n";
return $str;
}
//puts a footer on the end of our xml data
function createXmlFooter() {
return "</data>\n";
}
//encases the data in its xml tags and CDATA declaration
function xmlEntry($key,$data,$ignore = null) {
$str = "<".$key.">";
if ($data!=NULL) {
//convert our db data to the proper encoding if able
if (defined("DB_CHARSET") && defined("VIEW_CHARSET")) $data = charConv($data,DB_CHARSET,VIEW_CHARSET);
if ($ignore) $str .= $data;
else $str .= "<![CDATA[".$data."]]>";
}
$str .= "</".$key.">\n";
return $str;
}