-
Notifications
You must be signed in to change notification settings - Fork 23
/
xml.php
279 lines (261 loc) · 7.31 KB
/
xml.php
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
<?php
/**
* PHP操作XML类,读取方法:SimpleXML>正则解析
* @作者 qinggan <[email protected]>
* @主页 https://www.phpok.com
* @版本 6.x
* @授权 MIT License <https://www.phpok.com/mit.html>
* @时间 2015年1月5日
* @更新 2023年7月20日
**/
/**
* 安全限制,防止直接访问
**/
if(!defined("PHPOK_SET")){
exit("<h1>Access Denied</h1>");
}
class xml_lib extends _init_lib
{
private $xml_read_func = 'phpok';
private $xml_save_func = 'phpok';
public function __construct()
{
if(function_exists('simplexml_load_string')){
$this->xml_read_func = 'simplexml';
}
$this->xml_save_func = 'phpok';
}
public function read_setting($type='phpok')
{
$this->xml_read_func = $type;
}
public function save_setting($type="phpok")
{
$this->xml_save_func = $type;
}
public function reset_setting()
{
$this->xml_read_func = function_exists('simplexml_load_string') ? 'simplexml' : 'phpok';
$this->xml_save_func = 'phpok';
}
/**
* 读取XML操作
* @参数 $info XML文件或要解析的XML内容
* @参数 $isfile,是否是文件,默认为是,如果要解析XML内容,请改为false
**/
public function read($info,$isfile=true)
{
$func = "read_".$this->xml_read_func;
if($isfile && !file_exists($info)){
return false;
}
$info = $this->$func($info,$isfile);
/*if($info && is_array($info)){
$list = array();
$this->_stripslashes($list,$info);
return $list;
}*/
return $info;
}
private function _stripslashes(&$list,$info)
{
foreach($info as $key=>$value){
if($value && is_array($value)){
$this->_stripslashes($list[$key],$value);
}else{
if($value != ''){
$list[$key] = stripslashes($value);
}
}
}
}
public function save($data,$file='',$ekey='')
{
if(!$data || !$file || !is_array($data) || !is_string($file)){
return false;
}
$func = "write_".$this->xml_save_func;
return $this->$func($data,$file,$ekey);
}
public function write($data,$file='',$ekey='')
{
return $this->save($data,$file,$ekey);
}
private function write_phpok($data,$file,$ekey='')
{
$dir = pathinfo($file,PATHINFO_DIRNAME);
$tmpfile = $dir.'/'.uniqid('tmp_',true).'.xml';
$handle = fopen($tmpfile,'ab');
fwrite($handle,'<?xml version="1.0" encoding="UTF-8"?>'."\n");
fwrite($handle,'<root>'."\n");
$string = '';
$this->_array_to_string($string,$data,"\t",$ekey);
fwrite($handle,$string);
fwrite($handle,'</root>');
fclose($handle);
if(file_exists($file)){
unlink($file);
}
rename($tmpfile,$file);
return true;
}
public function to_xml($data,$ekey='')
{
$string = '';
$this->_array_to_string($string,$data,"\t",$ekey);
return $string;
}
private function _array_to_string(&$string,$data,$space="",$ekey='')
{
foreach($data as $key=>$value){
$tmpid = (is_numeric($key) && $ekey) ? $ekey : $key;
if($value && (is_array($value) || is_object($value))){
if(count($value)>0){
$tmp = "\n";
$this->_array_to_string($tmp,$value,$space."\t");
$string .= $space."<".$tmpid.">".$tmp.$space."</".$tmpid.">\n";
}
}else{
if(is_array($value) || is_object($value)){
continue;
}
$value = str_replace(array('<![CDATA[',']]>'),array('<![CDATA[',']]>'),$value);
$string .= $space."<".$tmpid."><![CDATA[".$value."]]></".$tmpid.">\n";
}
}
}
//通过SimpleXML读取XML信息
private function read_simplexml($info,$isfile=true)
{
if($isfile){
$info = file_get_contents($info);
}
$info = trim($info);
if(PHP_VERSION_ID && PHP_VERSION_ID < 80000 && function_exists('libxml_disable_entity_loader')){
libxml_disable_entity_loader(true);
}
$xml = simplexml_load_string($info);
$info = $this->simplexml_obj_to_array($xml);
if(!$info){
return false;
}
if(isset($info['root']) && $info['root']){
return $info['root'];
}
return $info;
}
private function simplexml_obj_to_array($xml)
{
$list = array();
if(!is_object($xml) && !is_array($xml)){
return $xml;
}
foreach($xml as $key=>$value){
$attr = false;
if($value->attributes()){
foreach($value->attributes() as $k=>$v){
$attr[$k] = (string) $v;
}
}
//检测子节点
$val = (string) $value;
if($value->children()){
$tmp = $this->simplexml_obj_to_array($value->children());
if($tmp){
$val = $tmp;
}
}
$info = (isset($attr) && $attr) ? array('attr'=>$attr,'val'=>$val) : $val;
if(isset($list[$key])){
if(!is_array($list[$key]) || (isset($list[$key]['attr']) && $list[$key]['attr']) || (isset($list[$key]['val']) && $list[$key]['val'])){
$tmp = $list[$key];
unset($list[$key]);
$list[$key] = array();
array_push($list[$key],$tmp);
}
array_push($list[$key],$info);
}else{
$list[$key] = $info;
}
}
return $list;
}
//通过人工编写自己读取XML,效率较慢
private function read_phpok($info,$isfile=true)
{
if($isfile){
$info = file_get_contents($info);
}
$info = preg_replace('/<\?xml[^\?>]+\?>/isU','',$info);
if(!$info){
return false;
}
$info = str_replace(array("\n","\t","\r"),"",$info);
$info = $this->xml_to_array($info);
if(!$info){
return false;
}
if($info['root']){
return $info['root'];
}
return $info;
}
private function _string_to_array($ext='')
{
if(!$ext || !trim($ext)){
return false;
}
if(function_exists("get_magic_quotes_gpc") && get_magic_quotes_gpc()){
$ext = stripslashes($ext);
}
$ext = trim($ext);
$ext = preg_replace("/(\x20{2,})/"," ",$ext);
$ext = preg_replace("/[\"|'](^[\"|'|\s]+)\s+(^[\"|'|\s]+)[\"|']/isU",'\\1:_:_:-phpok-:_:_:\\2',$ext);
$ext = str_replace(array("'",'"'),'',$ext);
$ext = str_replace(" ","&",$ext);
parse_str($ext,$list);
foreach($list as $key=>$value){
if(substr($value,0,1) == '"' || substr($value,0,1) == "'") $value = substr($value,1);
if(substr($value,-1) == '"' || substr($value,-1) == "'") $value = substr($value,0,-1);
$value = str_replace(':_:_:-phpok-:_:_:',' ',$value);
$list[$key] = $value;
}
return $list;
}
private function xml_to_array($xml)
{
if(!$xml || !trim($xml)){
return false;
}
$xml = trim($xml);
//$reg = "/<(\\w+)([^>]*)>([\\x00-\\xFF]*)<\\/\\1>/isU";
$reg = "/<([a-zA-Z0-9\_\-]+)([^>]*)>(.*)<\/\\1>/isU";
if(!preg_match_all($reg, $xml, $matches)){
return $xml;
}
$count = count($matches[0]);
$array = array();
for($i=0;$i<$count;$i++){
$id = $matches[1][$i];
$attr = $this->_string_to_array($matches[2][$i]);
$val = $this->xml_to_array($matches[3][$i]);
if(is_string($val)){
$val = preg_replace('/<\!\[CDATA\[([^\]\]>]+)\]\]>/isU','\\1',$val);
//将值中的HTML标记更换
$val = preg_replace('/\[html:([^\]]+)\]/isU','<\\1>',$val);
$val = preg_replace('/\[\/([a-zA-Z0-9\_\-]+):html\]/isU','</\\1>',$val);
}
if(isset($array[$id])){
if(!is_array($array[$id]) || $array[$id]['attr'] || $array[$id]['val']){
$tmp = $array[$id];
unset($array[$id]);
$array[$id][] = $tmp;
}
$array[$id][] = $attr ? array('attr'=>$attr,'val'=>$val) : $val;
}else{
$array[$id] = $attr ? array('attr'=>$attr,'val'=>$val) : $val;
}
}
return $array;
}
}