国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

613 lines
21KB

  1. <?php if (!defined('DEDEINC')) exit("Request Error!");
  2. /**
  3. * 织梦模块类
  4. *
  5. * @version $Id: dedemodule.class.php 1 10:31 2010年7月6日Z tianya $
  6. * @package DedeCMS.Libraries
  7. * @copyright Copyright (c) 2020, DedeBIZ.COM
  8. * @license https://www.dedebiz.com/license
  9. * @link https://www.dedebiz.com
  10. */
  11. require_once(DEDEINC . '/charset.func.php');
  12. require_once(DEDEINC . '/dedeatt.class.php');
  13. require_once(DEDEINC . '/dedehttpdown.class.php');
  14. function base64url_encode($data)
  15. {
  16. return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
  17. }
  18. function base64url_decode($data)
  19. {
  20. return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
  21. }
  22. class DedeModule
  23. {
  24. var $modulesPath;
  25. var $modulesUrl;
  26. var $modules;
  27. var $fileListNames;
  28. var $sysLang;
  29. var $moduleLang;
  30. function __construct($modulespath = '', $modulesUrl = '')
  31. {
  32. global $cfg_soft_lang;
  33. $this->sysLang = $this->moduleLang = $cfg_soft_lang;
  34. $this->fileListNames = array();
  35. $this->modulesPath = $modulespath;
  36. $this->modulesUrl = $modulesUrl;
  37. }
  38. function DedeModule($modulespath = '')
  39. {
  40. $this->__construct($modulespath);
  41. }
  42. /**
  43. * 枚举系统里已经存在的模块(缓存功能实际上只作hash与文件名的解析,在此不特别处理)
  44. *
  45. * @access public
  46. * @param string $moduletype 模块类型
  47. * @return string
  48. */
  49. function GetModuleList($moduletype = '')
  50. {
  51. if (is_array($this->modules)) return $this->modules;
  52. $dh = dir($this->modulesPath) or die("没找到模块目录:({$this->modulesPath})!");
  53. $fp = @fopen($this->modulesPath . '/modulescache.php', 'w') or die('读取文件权限出错,目录文件' . $this->modulesPath . '/modulescache.php不可写!');
  54. fwrite($fp, "<" . "?php\r\n");
  55. fwrite($fp, "global \$allmodules;\r\n");
  56. while ($filename = $dh->read()) {
  57. if (preg_match("/\.xml$/i", $filename)) {
  58. $minfos = $this->GetModuleInfo(str_replace('.xml', '', $filename));
  59. if ($minfos==null) {
  60. continue;
  61. }
  62. if (isset($minfos['moduletype']) && $moduletype != '' && $moduletype != $minfos['moduletype']) {
  63. continue;
  64. }
  65. if ($minfos['hash'] != '') {
  66. $this->modules[$minfos['hash']] = $minfos;
  67. fwrite($fp, '$' . "GLOBALS['allmodules']['{$minfos['hash']}']='{$filename}';\r\n");
  68. }
  69. }
  70. }
  71. fwrite($fp, '?' . '>');
  72. fclose($fp);
  73. $dh->Close();
  74. return $this->modules;
  75. }
  76. /**
  77. * 从远程获取模块信息
  78. *
  79. * @access public
  80. * @param string $moduletype 模块类型
  81. * @return string
  82. */
  83. function GetModuleUrlList($moduletype = '', $url = '')
  84. {
  85. return false;
  86. }
  87. /**
  88. * 转换编码
  89. *
  90. * @access public
  91. * @param string $str 字符串
  92. * @return string
  93. */
  94. function AppCode(&$str)
  95. {
  96. if ($this->moduleLang == $this->sysLang) {
  97. return $str;
  98. } else {
  99. if ($this->sysLang == 'utf-8') {
  100. if ($this->moduleLang == 'gbk') return gb2utf8($str);
  101. if ($this->moduleLang == 'big5') return gb2utf8(big52gb($str));
  102. } else if ($this->sysLang == 'gbk') {
  103. if ($this->moduleLang == 'utf-8') return utf82gb($str);
  104. if ($this->moduleLang == 'big5') return big52gb($str);
  105. } else if ($this->sysLang == 'big5') {
  106. if ($this->moduleLang == 'utf-8') return gb2big5(utf82gb($str));
  107. if ($this->moduleLang == 'gbk') return gb2big5($str);
  108. } else {
  109. return $str;
  110. }
  111. }
  112. }
  113. /**
  114. * 获得指定hash的模块文件
  115. *
  116. * @access public
  117. * @param string $hash hash文件
  118. * @return string
  119. */
  120. function GetHashFile($hash)
  121. {
  122. include_once($this->modulesPath . '/modulescache.php');
  123. if (isset($GLOBALS['allmodules'][$hash])) return $GLOBALS['allmodules'][$hash];
  124. else return $hash . '.xml';
  125. }
  126. /**
  127. * 获得某模块的基本信息
  128. *
  129. * @access public
  130. * @param string $hash hash
  131. * @param string $ftype 文件类型
  132. * @return string
  133. */
  134. function GetModuleInfo($hash, $ftype = 'hash')
  135. {
  136. if ($ftype == 'file') $filename = $hash;
  137. else if (!empty($this->modulesUrl)) {
  138. $filename = $this->modulesUrl . $hash . '.xml';
  139. } else $filename = $this->modulesPath . '/' . $this->GetHashFile($hash);
  140. $start = 0;
  141. $minfos = array();
  142. $minfos['name'] = $minfos['info'] = $minfos['time'] = '';
  143. $minfos['hash'] = $minfos['indexname'] = $minfos['indexurl'] = '';
  144. $minfos['ismember'] = $minfos['autosetup'] = $minfos['autodel'] = 0;
  145. //$minfos['filename'] = $filename;
  146. if (empty($this->modulesUrl)) {
  147. $minfos['filesize'] = filesize($filename) / 1024;
  148. $minfos['filesize'] = number_format($minfos['filesize'], 2, '.', '') . ' Kb';
  149. }
  150. $fp = fopen($filename, 'r') or die("文件 {$filename} 不存在或不可读!");
  151. $n = 0;
  152. while (!feof($fp)) {
  153. $n++;
  154. if ($n > 30) break;
  155. $line = fgets($fp, 1024);
  156. if ($start == 0) {
  157. if (preg_match("/<baseinfo/is", $line)) $start = 1;
  158. } else {
  159. if (preg_match("/<\/baseinfo/is", $line)) break;
  160. $line = trim($line);
  161. list($skey, $svalue) = explode('=', $line);
  162. $skey = trim($skey);
  163. $minfos[$skey] = $svalue;
  164. }
  165. }
  166. fclose($fp);
  167. if (empty($minfos['lang'])) {
  168. $minfos['lang'] = "utf-8";
  169. }
  170. if (isset($minfos['lang'])) $this->moduleLang = trim($minfos['lang']);
  171. else $this->moduleLang = 'gbk';
  172. if ($this->sysLang == 'gb2312') $this->sysLang = 'gbk';
  173. if ($this->moduleLang == 'gb2312') $this->moduleLang = 'gbk';
  174. if ($this->sysLang != $this->moduleLang) {
  175. foreach ($minfos as $k => $v) $minfos[$k] = $this->AppCode($v);
  176. }
  177. // 验证模块信息
  178. $pubKey = @base64url_decode($minfos['pubkey']);
  179. @openssl_public_decrypt(base64url_decode($minfos['info']), $decontent, $pubKey);
  180. $enInfo = (array)json_decode($decontent);
  181. if (count($enInfo)==0) {
  182. return null;
  183. }
  184. if ($enInfo['module_name'] != $minfos['name'] || $enInfo['dev_id'] != $minfos['dev_id']) {
  185. return null;
  186. }
  187. return $minfos;
  188. }
  189. /**
  190. * 获得某模块的基本信息
  191. *
  192. * @access public
  193. * @param string $hash hash
  194. * @param string $ftype 文件类型
  195. * @return string
  196. */
  197. function GetFileXml($hash, $ftype = 'hash')
  198. {
  199. if ($ftype == 'file') $filename = $hash;
  200. else $filename = $this->modulesPath . '/' . $this->GetHashFile($hash);
  201. $filexml = '';
  202. $fp = fopen($filename, 'r') or die("文件 {$filename} 不存在或不可读!");
  203. $start = 0;
  204. while (!feof($fp)) {
  205. $line = fgets($fp, 1024);
  206. if ($start == 0) {
  207. if (preg_match("/<modulefiles/is", $line)) {
  208. $filexml .= $line;
  209. $start = 1;
  210. }
  211. continue;
  212. } else {
  213. $filexml .= $line;
  214. }
  215. }
  216. fclose($fp);
  217. return $filexml;
  218. }
  219. /**
  220. * 获得系统文件的内容
  221. * 指安装、删除、协议文件
  222. *
  223. * @access public
  224. * @param string $hashcode hash码
  225. * @param string $ntype 文件类型
  226. * @param string $enCode 是否加密
  227. * @return string
  228. */
  229. function GetSystemFile($hashcode, $ntype, $enCode = TRUE)
  230. {
  231. $this->GetModuleInfo($hashcode, $ntype);
  232. $start = FALSE;
  233. $filename = $this->modulesPath . '/' . $this->GetHashFile($hashcode);
  234. $fp = fopen($filename, 'r') or die("文件 {$filename} 不存在或不可读!");
  235. $okdata = '';
  236. while (!feof($fp)) {
  237. $line = fgets($fp, 1024);
  238. if (!$start) {
  239. // 2011-6-7 修复模块打包程序中上传安装程序生成为空白文件(by:华强)
  240. if (preg_match("#<{$ntype}>#i", $line)) $start = TRUE;
  241. } else {
  242. if (preg_match("#<\/{$ntype}#i", $line)) break;
  243. $okdata .= $line;
  244. unset($line);
  245. }
  246. }
  247. fclose($fp);
  248. $okdata = trim($okdata);
  249. if (!empty($okdata) && $enCode) $okdata = base64_decode($okdata);
  250. $okdata = $this->AppCode($okdata);
  251. return $okdata;
  252. }
  253. /**
  254. * 把某系统文件转换为文件
  255. *
  256. * @access public
  257. * @param string $hashcode hash码
  258. * @param string $ntype 文件类型
  259. * @return string 返回文件名
  260. */
  261. function WriteSystemFile($hashcode, $ntype)
  262. {
  263. $filename = $hashcode . "-{$ntype}.php";
  264. $fname = $this->modulesPath . '/' . $filename;
  265. $filect = $this->GetSystemFile($hashcode, $ntype);
  266. $fp = fopen($fname, 'w') or die('生成 {$ntype} 文件失败!');
  267. fwrite($fp, $filect);
  268. fclose($fp);
  269. return $filename;
  270. }
  271. /**
  272. * 删除系统文件
  273. *
  274. * @access public
  275. * @param string $hashcode hash码
  276. * @param string $ntype 文件类型
  277. * @return void
  278. */
  279. function DelSystemFile($hashcode, $ntype)
  280. {
  281. $filename = $this->modulesPath . '/' . $hashcode . "-{$ntype}.php";
  282. unlink($filename);
  283. }
  284. /**
  285. * 检查是否已经存在指定的模块
  286. *
  287. * @access public
  288. * @param string $hashcode hash码
  289. * @return bool 如果存在则返回True,否则为False
  290. */
  291. function HasModule($hashcode)
  292. {
  293. $modulefile = $this->modulesPath . '/' . $this->GetHashFile($hashcode);
  294. if (file_exists($modulefile) && !is_dir($modulefile)) return TRUE;
  295. else return FALSE;
  296. }
  297. /**
  298. * 读取文件,返回编码后的文件内容
  299. *
  300. * @access public
  301. * @param string $filename 文件名
  302. * @param string $isremove 是否删除
  303. * @return string
  304. */
  305. function GetEncodeFile($filename, $isremove = FALSE)
  306. {
  307. $fp = fopen($filename, 'r') or die("文件 {$filename} 不存在或不可读!");
  308. $str = @fread($fp, filesize($filename));
  309. fclose($fp);
  310. if ($isremove) @unlink($filename);
  311. if (!empty($str)) return base64_encode($str);
  312. else return '';
  313. }
  314. /**
  315. * 获取模块包里的文件名列表
  316. *
  317. * @access public
  318. * @param string $hashcode hash码
  319. * @return string 返回文件列表
  320. */
  321. function GetFileLists($hashcode)
  322. {
  323. $dap = new DedeAttParse();
  324. $filelists = array();
  325. $modulefile = $this->modulesPath . '/' . $this->GetHashFile($hashcode);
  326. $fp = fopen($modulefile, 'r') or die("文件 {$modulefile} 不存在或不可读!");
  327. $i = 0;
  328. while (!feof($fp)) {
  329. $line = fgets($fp, 1024);
  330. if (preg_match("/^[\s]{0,}<file/i", $line)) {
  331. $i++;
  332. $line = trim(preg_replace("/[><]/", "", $line));
  333. $dap->SetSource($line);
  334. $filelists[$i]['type'] = $dap->CAtt->GetAtt('type');
  335. $filelists[$i]['name'] = $dap->CAtt->GetAtt('name');
  336. }
  337. }
  338. fclose($fp);
  339. return $filelists;
  340. }
  341. /**
  342. * 删除已安装模块附带的文件
  343. *
  344. * @access public
  345. * @param string $hashcode hash码
  346. * @param string $isreplace 是否替换
  347. * @return string
  348. */
  349. function DeleteFiles($hashcode, $isreplace = 0)
  350. {
  351. if ($isreplace == 0) return TRUE;
  352. else {
  353. $dap = new DedeAttParse();
  354. $modulefile = $this->modulesPath . '/' . $this->GetHashFile($hashcode);
  355. $fp = fopen($modulefile, 'r') or die("文件 {$modulefile} 不存在或不可读!");
  356. $i = 0;
  357. $dirs = '';
  358. while (!feof($fp)) {
  359. $line = fgets($fp, 1024);
  360. if (preg_match("/^[\s]{0,}<file/i", $line)) {
  361. $i++;
  362. $line = trim(preg_replace("/[><]/", "", $line));
  363. $dap->SetSource($line);
  364. $filetype = $dap->CAtt->GetAtt('type');
  365. $filename = $dap->CAtt->GetAtt('name');
  366. $filename = str_replace("\\", "/", $filename);
  367. if ($filetype == 'dir') {
  368. $dirs[] = $filename;
  369. } else {
  370. @unlink($filename);
  371. }
  372. }
  373. }
  374. $okdirs = array();
  375. if (is_array($dirs)) {
  376. $st = count($dirs) - 1;
  377. for ($i = $st; $i >= 0; $i--) {
  378. @rmdir($dirs[$i]);
  379. }
  380. }
  381. fclose($fp);
  382. }
  383. return TRUE;
  384. }
  385. /**
  386. * 把模块包里的文件写入服务器
  387. *
  388. * @access public
  389. * @param string $hashcode hash码
  390. * @param string $isreplace 是否替换
  391. * @return string
  392. */
  393. function WriteFiles($hashcode, $isreplace = 3)
  394. {
  395. global $AdminBaseDir;
  396. $dap = new DedeAttParse();
  397. $modulefile = $this->modulesPath . '/' . $this->GetHashFile($hashcode);
  398. $fp = fopen($modulefile, 'r') or die("文件 {$modulefile} 不存在或不可读!");
  399. $i = 0;
  400. while (!feof($fp)) {
  401. $line = fgets($fp, 1024);
  402. if (preg_match("/^[\s]{0,}<file/i", $line)) {
  403. $i++;
  404. $line = trim(preg_replace("/[><]/", "", $line));
  405. $dap->SetSource($line);
  406. $filetype = $dap->CAtt->GetAtt('type');
  407. $filename = $dap->CAtt->GetAtt('name');
  408. $filename = str_replace("\\", "/", $filename);
  409. if (!empty($AdminBaseDir)) $filename = $AdminBaseDir . $filename;
  410. if ($filetype == 'dir') {
  411. if (!is_dir($filename)) {
  412. @mkdir($filename, $GLOBALS['cfg_dir_purview']);
  413. }
  414. @chmod($filename, $GLOBALS['cfg_dir_purview']);
  415. } else {
  416. $this->TestDir($filename);
  417. if ($isreplace == 0) continue;
  418. if ($isreplace == 3) {
  419. if (is_file($filename)) {
  420. $copyname = @preg_replace("/([^\/]{1,}$)/", "bak-$1", $filename);
  421. @copy($filename, $copyname);
  422. }
  423. }
  424. if (!empty($filename)) {
  425. $fw = fopen($filename, 'w') or die("写入文件 {$filename} 失败,请检查相关目录的权限!");
  426. $ct = '';
  427. while (!feof($fp)) {
  428. $l = fgets($fp, 1024);
  429. if (preg_match("/^[\s]{0,}<\/file/i", trim($l))) {
  430. break;
  431. }
  432. $ct .= $l;
  433. }
  434. $ct = base64_decode($ct);
  435. if ($this->sysLang != $this->moduleLang) {
  436. //转换内码
  437. if (preg_match('/\.(xml|php|inc|txt|htm|html|shtml|tpl|css)$/', $filename)) {
  438. $ct = $this->AppCode($ct);
  439. }
  440. //转换HTML编码标识
  441. if (preg_match('/\.(php|htm|html|shtml|inc|tpl)$/i', $filename)) {
  442. if ($this->sysLang == 'big5') $charset = 'charset=big5';
  443. else if ($this->sysLang == 'utf-8') $charset = 'charset=gb2312';
  444. else $charset = 'charset=gb2312';
  445. $ct = preg_match("/charset=([a-z0-9-]*)/i", $charset, $ct);
  446. }
  447. }
  448. fwrite($fw, $ct);
  449. fclose($fw);
  450. }
  451. }
  452. }
  453. }
  454. fclose($fp);
  455. return TRUE;
  456. }
  457. /**
  458. * 测试某文件的文件夹是否创建
  459. *
  460. * @access public
  461. * @param string $filename 文件名称
  462. * @return string
  463. */
  464. function TestDir($filename)
  465. {
  466. $fs = explode('/', $filename);
  467. $fn = count($fs) - 1;
  468. $ndir = '';
  469. for ($i = 0; $i < $fn; $i++) {
  470. if ($ndir != '') $ndir = $ndir . '/' . $fs[$i];
  471. else $ndir = $fs[$i];
  472. $rs = @is_dir($ndir);
  473. if (!$rs) {
  474. @mkdir($ndir, $GLOBALS['cfg_dir_purview']);
  475. @chmod($ndir, $GLOBALS['cfg_dir_purview']);
  476. }
  477. }
  478. return TRUE;
  479. }
  480. /**
  481. * 获取某个目录或文件的打包数据
  482. *
  483. * @access public
  484. * @param string $basedir 基本目录
  485. * @param string $f
  486. * @param string $fp 文件指针
  487. * @return bool
  488. */
  489. function MakeEncodeFile($basedir, $f, $fp)
  490. {
  491. $this->fileListNames = array();
  492. $this->MakeEncodeFileRun($basedir, $f, $fp);
  493. return TRUE;
  494. }
  495. /**
  496. * 测试目标文件
  497. *
  498. * @access public
  499. * @param string $basedir 基本目录
  500. * @param string $f
  501. * @return bool
  502. */
  503. function MakeEncodeFileTest($basedir, $f)
  504. {
  505. $this->fileListNames = array();
  506. $this->MakeEncodeFileRunTest($basedir, $f);
  507. return TRUE;
  508. }
  509. /**
  510. * 检测某个目录或文件的打包数据,递归
  511. *
  512. * @access public
  513. * @param string $basedir 基本目录
  514. * @param string $f
  515. * @return void
  516. */
  517. function MakeEncodeFileRunTest($basedir, $f)
  518. {
  519. $filename = $basedir . '/' . $f;
  520. if (isset($this->fileListNames[$f])) return;
  521. else if (preg_match("/Thumbs\.db/i", $f)) return;
  522. else $this->fileListNames[$f] = 1;
  523. $fileList = '';
  524. if (!file_exists($filename)) {
  525. ShowMsg("文件或文件夹: {$filename} 不存在,无法进行编译!", "-1");
  526. exit();
  527. }
  528. if (is_dir($filename)) {
  529. $dh = dir($filename);
  530. while ($filename = $dh->read()) {
  531. if ($filename[0] == '.' || strtolower($filename) == 'cvs') continue;
  532. $nfilename = $f . '/' . $filename;
  533. $this->MakeEncodeFileRunTest($basedir, $nfilename);
  534. }
  535. }
  536. }
  537. /**
  538. * 获取个目录或文件的打包数据,递归
  539. *
  540. * @access public
  541. * @param string $basedir 基本目录
  542. * @param string $f
  543. * @param string $fp 文件指针
  544. * @return string
  545. */
  546. function MakeEncodeFileRun($basedir, $f, $fp)
  547. {
  548. $filename = $basedir . '/' . $f;
  549. if (isset($this->fileListNames[$f])) return;
  550. else if (preg_match("#Thumbs\.db#i", $f)) return;
  551. else $this->fileListNames[$f] = 1;
  552. $fileList = '';
  553. if (is_dir($filename)) {
  554. $fileList .= "<file type='dir' name='$f'>\r\n";
  555. $fileList .= "</file>\r\n";
  556. fwrite($fp, $fileList);
  557. $dh = dir($filename);
  558. while ($filename = $dh->read()) {
  559. if ($filename[0] == '.' || strtolower($filename) == 'cvs') continue;
  560. $nfilename = $f . '/' . $filename;
  561. $this->MakeEncodeFileRun($basedir, $nfilename, $fp);
  562. }
  563. } else {
  564. $fileList .= "<file type='file' name='$f'>\r\n";
  565. $fileList .= $this->GetEncodeFile($filename);
  566. $fileList .= "\r\n</file>\r\n";
  567. fwrite($fp, $fileList);
  568. }
  569. }
  570. /**
  571. * 清理
  572. *
  573. * @access public
  574. * @return void
  575. */
  576. function Clear()
  577. {
  578. unset($this->modules);
  579. unset($this->fileList);
  580. unset($this->fileListNames);
  581. }
  582. }//End Class