dedemodule.class.php
20.5 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
<?php if(!defined('DEDEINC')) exit("Request Error!");
/**
* 织梦模块类
*
* @version $Id: dedemodule.class.php 1 10:31 2010年7月6日Z tianya $
* @package DedeCMS.Libraries
* @copyright Copyright (c) 2007 - 2010, DesDev, Inc.
* @license http://help.dedecms.com/usersguide/license.html
* @link http://www.dedecms.com
*/
require_once(DEDEINC.'/charset.func.php');
require_once(DEDEINC.'/dedeatt.class.php');
require_once(DEDEINC.'/dedehttpdown.class.php');
class DedeModule
{
var $modulesPath;
var $modulesUrl;
var $modules;
var $fileListNames;
var $sysLang;
var $moduleLang;
function __construct($modulespath='',$modulesUrl='')
{
global $cfg_soft_lang;
$this->sysLang = $this->moduleLang = $cfg_soft_lang;
$this->fileListNames = array();
$this->modulesPath = $modulespath;
$this->modulesUrl = $modulesUrl;
}
function DedeModule($modulespath='')
{
$this->__construct($modulespath);
}
/**
* 枚举系统里已经存在的模块(缓存功能实际上只作hash与文件名的解析,在此不特别处理)
*
* @access public
* @param string $moduletype 模块类型
* @return string
*/
function GetModuleList($moduletype='')
{
if(is_array($this->modules)) return $this->modules;
$dh = dir($this->modulesPath) or die("没找到模块目录:({$this->modulesPath})!");
$fp = @fopen($this->modulesPath.'/modulescache.php','w') or die('读取文件权限出错,目录文件'.$this->modulesPath.'/modulescache.php不可写!');
fwrite($fp, "<"."?php\r\n");
fwrite($fp, "global \$allmodules;\r\n");
while($filename = $dh->read())
{
if(preg_match("/\.xml$/i", $filename))
{
$minfos = $this->GetModuleInfo(str_replace('.xml','',$filename));
if(isset($minfos['moduletype']) && $moduletype!='' && $moduletype!=$minfos['moduletype'])
{
continue;
}
if($minfos['hash']!='')
{
$this->modules[$minfos['hash']] = $minfos;
fwrite($fp, '$'."GLOBALS['allmodules']['{$minfos['hash']}']='{$filename}';\r\n");
}
}
}
fwrite($fp,'?'.'>');
fclose($fp);
$dh->Close();
return $this->modules;
}
/**
* 从远程获取模块信息
*
* @access public
* @param string $moduletype 模块类型
* @return string
*/
function GetModuleUrlList($moduletype='',$url='')
{
$dh = dir($this->modulesPath) or die("没找到模块目录:({$this->modulesPath})!");
$fp = @fopen($this->modulesPath.'/modulescache.php','w') or die('读取文件权限出错,目录文件'.$this->modulesPath.'/modulescache.php不可写!');
$cachefile = DEDEDATA.'/module/moduleurllist.txt';
$remotelist = '';
if(file_exists($cachefile) && (filemtime($cachefile) + 60 * 30) > time())
{
// 30分钟本地缓存一次
$remotelist = file_get_contents($cachefile);
} else {
$del = new DedeHttpDown();
$del->OpenUrl($url);
$remotelist = $del->GetHtml();
PutFile($cachefile, $remotelist);
}
if(empty($remotelist)) return false;
$modules = unserialize($remotelist);
if(empty($moduletype)){
return $modules;
}
$return = array();
foreach($modules as $arrow=>$data) {
if($data['moduletype']==$moduletype)
$return[] = $data;
}
return $return;
}
/**
* 转换编码
*
* @access public
* @param string $str 字符串
* @return string
*/
function AppCode(&$str)
{
if($this->moduleLang==$this->sysLang)
{
return $str;
}
else
{
if($this->sysLang=='utf-8')
{
if($this->moduleLang=='gbk') return gb2utf8($str);
if($this->moduleLang=='big5') return gb2utf8(big52gb($str));
}
else if($this->sysLang=='gbk')
{
if($this->moduleLang=='utf-8') return utf82gb($str);
if($this->moduleLang=='big5') return big52gb($str);
}
else if($this->sysLang=='big5')
{
if($this->moduleLang=='utf-8') return gb2big5(utf82gb($str));
if($this->moduleLang=='gbk') return gb2big5($str);
}
else
{
return $str;
}
}
}
/**
* 获得指定hash的模块文件
*
* @access public
* @param string $hash hash文件
* @return string
*/
function GetHashFile($hash)
{
include_once($this->modulesPath.'/modulescache.php');
if(isset($GLOBALS['allmodules'][$hash])) return $GLOBALS['allmodules'][$hash];
else return $hash.'.xml';
}
/**
* 获得某模块的基本信息
*
* @access public
* @param string $hash hash
* @param string $ftype 文件类型
* @return string
*/
function GetModuleInfo($hash, $ftype='hash')
{
if($ftype=='file') $filename = $hash;
else if(!empty($this->modulesUrl)) {
$filename = $this->modulesUrl.$hash.'.xml';
}else $filename = $this->modulesPath.'/'.$this->GetHashFile($hash);
$start = 0;
$minfos = array();
$minfos['name']=$minfos['team']=$minfos['time']=$minfos['email']=$minfos['url']='';
$minfos['hash']=$minfos['indexname']=$minfos['indexurl']='';
$minfos['ismember']=$minfos['autosetup']=$minfos['autodel']=0;
//$minfos['filename'] = $filename;
if(empty($this->modulesUrl)){
$minfos['filesize'] = filesize($filename)/1024;
$minfos['filesize'] = number_format($minfos['filesize'],2,'.','').' Kb';
}
$fp = fopen($filename,'r') or die("文件 {$filename} 不存在或不可读!");
$n = 0;
while(!feof($fp))
{
$n++;
if($n > 30) break;
$line = fgets($fp,256);
if($start==0)
{ if(preg_match("/<baseinfo/is",$line)) $start = 1; }
else
{
if(preg_match("/<\/baseinfo/is",$line)) break;
$line = trim($line);
list($skey,$svalue) = explode('=',$line);
$skey = trim($skey);
$minfos[$skey] = $svalue;
}
}
fclose($fp);
if(isset($minfos['lang'])) $this->moduleLang = trim($minfos['lang']);
else $this->moduleLang = 'gbk';
if($this->sysLang=='gb2312') $this->sysLang = 'gbk';
if($this->moduleLang=='gb2312') $this->moduleLang = 'gbk';
if($this->sysLang != $this->moduleLang)
{
foreach($minfos as $k=>$v) $minfos[$k] = $this->AppCode($v);
}
return $minfos;
}
/**
* 获得某模块的基本信息
*
* @access public
* @param string $hash hash
* @param string $ftype 文件类型
* @return string
*/
function GetFileXml($hash, $ftype='hash')
{
if($ftype=='file') $filename = $hash;
else $filename = $this->modulesPath.'/'.$this->GetHashFile($hash);
$filexml = '';
$fp = fopen($filename,'r') or die("文件 {$filename} 不存在或不可读!");
$start = 0;
while(!feof($fp))
{
$line = fgets($fp,1024);
if($start==0)
{
if(preg_match("/<modulefiles/is",$line))
{
$filexml .= $line;
$start = 1;
}
continue;
}
else
{
$filexml .= $line;
}
}
fclose($fp);
return $filexml;
}
/**
* 获得系统文件的内容
* 指安装、删除、协议文件
*
* @access public
* @param string $hashcode hash码
* @param string $ntype 文件类型
* @param string $enCode 是否加密
* @return string
*/
function GetSystemFile($hashcode, $ntype, $enCode=TRUE)
{
$this->GetModuleInfo($hashcode,$ntype);
$start = FALSE;
$filename = $this->modulesPath.'/'.$this->GetHashFile($hashcode);
$fp = fopen($filename,'r') or die("文件 {$filename} 不存在或不可读!");
$okdata = '';
while(!feof($fp))
{
$line = fgets($fp,1024);
if(!$start)
{
// 2011-6-7 修复模块打包程序中上传安装程序生成为空白文件(by:华强)
if(preg_match("#<{$ntype}>#i", $line)) $start = TRUE;
}
else
{
if(preg_match("#<\/{$ntype}#i", $line)) break;
$okdata .= $line;
unset($line);
}
}
fclose($fp);
$okdata = trim($okdata);
if(!empty($okdata) && $enCode) $okdata = base64_decode($okdata);
$okdata = $this->AppCode($okdata);
return $okdata;
}
/**
* 把某系统文件转换为文件
*
* @access public
* @param string $hashcode hash码
* @param string $ntype 文件类型
* @return string 返回文件名
*/
function WriteSystemFile($hashcode, $ntype)
{
$filename = $hashcode."-{$ntype}.php";
$fname = $this->modulesPath.'/'.$filename;
$filect = $this->GetSystemFile($hashcode,$ntype);
$fp = fopen($fname,'w') or die('生成 {$ntype} 文件失败!');
fwrite($fp,$filect);
fclose($fp);
return $filename;
}
/**
* 删除系统文件
*
* @access public
* @param string $hashcode hash码
* @param string $ntype 文件类型
* @return void
*/
function DelSystemFile($hashcode,$ntype)
{
$filename = $this->modulesPath.'/'.$hashcode."-{$ntype}.php";
unlink($filename);
}
/**
* 检查是否已经存在指定的模块
*
* @access public
* @param string $hashcode hash码
* @return bool 如果存在则返回True,否则为False
*/
function HasModule($hashcode)
{
$modulefile = $this->modulesPath.'/'.$this->GetHashFile($hashcode);
if(file_exists($modulefile) && !is_dir($modulefile)) return TRUE;
else return FALSE;
}
/**
* 读取文件,返回编码后的文件内容
*
* @access public
* @param string $filename 文件名
* @param string $isremove 是否删除
* @return string
*/
function GetEncodeFile($filename,$isremove=FALSE)
{
$fp = fopen($filename,'r') or die("文件 {$filename} 不存在或不可读!");
$str = @fread($fp,filesize($filename));
fclose($fp);
if($isremove) @unlink($filename);
if(!empty($str)) return base64_encode($str);
else return '';
}
/**
* 获取模块包里的文件名列表
*
* @access public
* @param string $hashcode hash码
* @return string 返回文件列表
*/
function GetFileLists($hashcode)
{
$dap = new DedeAttParse();
$filelists = array();
$modulefile = $this->modulesPath.'/'.$this->GetHashFile($hashcode);
$fp = fopen($modulefile,'r') or die("文件 {$modulefile} 不存在或不可读!");
$i = 0;
while(!feof($fp))
{
$line = fgets($fp,1024);
if(preg_match("/^[\s]{0,}<file/i",$line))
{
$i++;
$line = trim(preg_replace("/[><]/","",$line));
$dap->SetSource($line);
$filelists[$i]['type'] = $dap->CAtt->GetAtt('type');
$filelists[$i]['name'] = $dap->CAtt->GetAtt('name');
}
}
fclose($fp);
return $filelists;
}
/**
* 删除已安装模块附带的文件
*
* @access public
* @param string $hashcode hash码
* @param string $isreplace 是否替换
* @return string
*/
function DeleteFiles($hashcode,$isreplace=0)
{
if($isreplace==0) return TRUE;
else
{
$dap = new DedeAttParse();
$modulefile = $this->modulesPath.'/'.$this->GetHashFile($hashcode);
$fp = fopen($modulefile,'r') or die("文件 {$modulefile} 不存在或不可读!");
$i = 0;
$dirs = '';
while(!feof($fp))
{
$line = fgets($fp,1024);
if(preg_match("/^[\s]{0,}<file/i",$line))
{
$i++;
$line = trim(preg_replace("/[><]/","",$line));
$dap->SetSource($line);
$filetype = $dap->CAtt->GetAtt('type');
$filename = $dap->CAtt->GetAtt('name');
$filename = str_replace("\\","/",$filename);
if($filetype=='dir'){ $dirs[] = $filename; }
else{ @unlink($filename); }
}
}
$okdirs = array();
if(is_array($dirs)){
$st = count($dirs) -1;
for($i=$st;$i>=0;$i--){ @rmdir($dirs[$i]); }
}
fclose($fp);
}
return TRUE;
}
/**
* 把模块包里的文件写入服务器
*
* @access public
* @param string $hashcode hash码
* @param string $isreplace 是否替换
* @return string
*/
function WriteFiles($hashcode, $isreplace=3)
{
global $AdminBaseDir;
$dap = new DedeAttParse();
$modulefile = $this->modulesPath.'/'.$this->GetHashFile($hashcode);
$fp = fopen($modulefile,'r') or die("文件 {$modulefile} 不存在或不可读!");
$i = 0;
while(!feof($fp))
{
$line = fgets($fp,1024);
if( preg_match("/^[\s]{0,}<file/i",$line) )
{
$i++;
$line = trim(preg_replace("/[><]/","",$line));
$dap->SetSource($line);
$filetype = $dap->CAtt->GetAtt('type');
$filename = $dap->CAtt->GetAtt('name');
$filename = str_replace("\\","/",$filename);
if(!empty($AdminBaseDir)) $filename = $AdminBaseDir.$filename;
if($filetype=='dir')
{
if(!is_dir($filename))
{
@mkdir($filename,$GLOBALS['cfg_dir_purview']);
}
@chmod($filename,$GLOBALS['cfg_dir_purview']);
}
else
{
$this->TestDir($filename);
if($isreplace==0) continue;
if($isreplace==3)
{
if(is_file($filename))
{
$copyname = @preg_replace("/([^\/]{1,}$)/","bak-$1",$filename);
@copy($filename,$copyname);
}
}
if(!empty($filename))
{
$fw = fopen($filename,'w') or die("写入文件 {$filename} 失败,请检查相关目录的权限!");
$ct = '';
while(!feof($fp))
{
$l = fgets($fp,1024);
if(preg_match("/^[\s]{0,}<\/file/i",trim($l))){ break; }
$ct .= $l;
}
$ct = base64_decode($ct);
if($this->sysLang!=$this->moduleLang)
{
//转换内码
if(preg_match('/\.(xml|php|inc|txt|htm|html|shtml|tpl|css)$/', $filename))
{
$ct = $this->AppCode($ct);
}
//转换HTML编码标识
if(preg_match('/\.(php|htm|html|shtml|inc|tpl)$/i', $filename))
{
if($this->sysLang=='big5') $charset = 'charset=big5';
else if($this->sysLang=='utf-8') $charset = 'charset=gb2312';
else $charset = 'charset=gb2312';
$ct = preg_match("/charset=([a-z0-9-]*)/i", $charset, $ct);
}
}
fwrite($fw,$ct);
fclose($fw);
}
}
}
}
fclose($fp);
return TRUE;
}
/**
* 测试某文件的文件夹是否创建
*
* @access public
* @param string $filename 文件名称
* @return string
*/
function TestDir($filename)
{
$fs = explode('/',$filename);
$fn = count($fs) - 1 ;
$ndir = '';
for($i=0;$i < $fn;$i++)
{
if($ndir!='') $ndir = $ndir.'/'.$fs[$i];
else $ndir = $fs[$i];
$rs = @is_dir($ndir);
if( !$rs ) {
@mkdir($ndir,$GLOBALS['cfg_dir_purview']);
@chmod($ndir,$GLOBALS['cfg_dir_purview']);
}
}
return TRUE;
}
/**
* 获取某个目录或文件的打包数据
*
* @access public
* @param string $basedir 基本目录
* @param string $f
* @param string $fp 文件指针
* @return bool
*/
function MakeEncodeFile($basedir,$f,$fp)
{
$this->fileListNames = array();
$this->MakeEncodeFileRun($basedir,$f,$fp);
return TRUE;
}
/**
* 测试目标文件
*
* @access public
* @param string $basedir 基本目录
* @param string $f
* @return bool
*/
function MakeEncodeFileTest($basedir,$f)
{
$this->fileListNames = array();
$this->MakeEncodeFileRunTest($basedir,$f);
return TRUE;
}
/**
* 检测某个目录或文件的打包数据,递归
*
* @access public
* @param string $basedir 基本目录
* @param string $f
* @return void
*/
function MakeEncodeFileRunTest($basedir,$f)
{
$filename = $basedir.'/'.$f;
if(isset($this->fileListNames[$f])) return;
else if(preg_match("/Thumbs\.db/i",$f)) return;
else $this->fileListNames[$f] = 1;
$fileList = '';
if(!file_exists($filename))
{
ShowMsg("文件或文件夹: {$filename} 不存在,无法进行编译!","-1");
exit();
}
if(is_dir($filename))
{
$dh = dir($filename);
while($filename = $dh->read())
{
if($filename[0]=='.' || strtolower($filename)=='cvs') continue;
$nfilename = $f.'/'.$filename;
$this->MakeEncodeFileRunTest($basedir,$nfilename);
}
}
}
/**
* 获取个目录或文件的打包数据,递归
*
* @access public
* @param string $basedir 基本目录
* @param string $f
* @param string $fp 文件指针
* @return string
*/
function MakeEncodeFileRun($basedir,$f,$fp)
{
$filename = $basedir.'/'.$f;
if(isset($this->fileListNames[$f])) return;
else if(preg_match("#Thumbs\.db#i", $f)) return;
else $this->fileListNames[$f] = 1;
$fileList = '';
if(is_dir($filename))
{
$fileList .= "<file type='dir' name='$f'>\r\n";
$fileList .= "</file>\r\n";
fwrite($fp,$fileList);
$dh = dir($filename);
while($filename = $dh->read())
{
if($filename[0]=='.' || strtolower($filename)=='cvs') continue;
$nfilename = $f.'/'.$filename;
$this->MakeEncodeFileRun($basedir,$nfilename,$fp);
}
}
else
{
$fileList .= "<file type='file' name='$f'>\r\n";
$fileList .= $this->GetEncodeFile($filename);
$fileList .= "\r\n</file>\r\n";
fwrite($fp,$fileList);
}
}
/**
* 清理
*
* @access public
* @return void
*/
function Clear()
{
unset($this->modules);
unset($this->fileList);
unset($this->fileListNames);
}
}//End Class