国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

236 rindas
8.3KB

  1. <?php
  2. /**
  3. * 用于后台的api接口
  4. *
  5. * @version $id:api.php 8:26 2022年11月20日 tianya $
  6. * @package DedeBIZ.Administrator
  7. * @copyright Copyright (c) 2022 DedeBIZ.COM
  8. * @license https://www.dedebiz.com/license
  9. * @link https://www.dedebiz.com
  10. */
  11. define('AJAXLOGIN', TRUE);
  12. define('DEDEADMIN', str_replace("\\", '/', dirname(__FILE__)));
  13. require_once(DEDEADMIN.'/../system/common.inc.php');
  14. require_once(DEDEINC.'/userlogin.class.php');
  15. AjaxHead();
  16. helper('cache');
  17. $action = isset($action) && in_array($action, array('is_need_check_code', 'has_new_version', 'get_changed_files', 'update_backup', 'get_update_versions', 'update')) ? $action : '';
  18. $curDir = dirname(GetCurUrl()); //当前目录
  19. /**
  20. * 登录鉴权
  21. *
  22. * @return void
  23. */
  24. function checkLogin()
  25. {
  26. $cuserLogin = new userLogin();
  27. if ($cuserLogin->getUserID() <= 0 || $cuserLogin->getUserType() != 10) {
  28. echo json_encode(array(
  29. "code" => -1,
  30. "msg" => "当前操作需要登录超级管理员账号",
  31. "data" => null,
  32. ));
  33. exit;
  34. }
  35. }
  36. if ($action === 'is_need_check_code') {
  37. $cuserLogin = new userLogin();
  38. $isNeed = $cuserLogin->isNeedCheckCode($userid);
  39. echo json_encode(array(
  40. "code" => 0,
  41. "msg" => "",
  42. "data" => array(
  43. "isNeed" => $isNeed,
  44. ),
  45. ));
  46. exit;
  47. } else if ($action === 'has_new_version') {
  48. require_once(DEDEINC.'/libraries/dedehttpdown.class.php');
  49. checkLogin();
  50. //是否存在更新版本
  51. $offUrl = DEDEBIZURL."/version?version={$cfg_version_detail}&formurl={$nurl}&phpver={$phpv}&os={$sp_os}&mysqlver={$mysql_ver}{$add_query}&json=1";
  52. $dhd = new DedeHttpDown();
  53. $dhd->OpenUrl($offUrl);
  54. $data = $dhd->GetHtml();
  55. echo $data;
  56. } else if ($action === 'get_changed_files') {
  57. require_once(DEDEINC.'/libraries/dedehttpdown.class.php');
  58. checkLogin();
  59. // 获取本地更改过的文件
  60. $hashUrl = DEDEBIZCDN.'/release/'.$cfg_version_detail.'.json';
  61. $dhd = new DedeHttpDown();
  62. $dhd->OpenUrl($hashUrl);
  63. $data = $dhd->GetJSON();
  64. $changedFiles = array();
  65. foreach ($data as $file) {
  66. $realFile = DEDEROOT.str_replace("\\", '/', $file->filename);
  67. if (file_exists($realFile) && md5_file($realFile) !== $file->hash) {
  68. $changedFiles[] = $file;
  69. continue;
  70. }
  71. }
  72. echo json_encode(array(
  73. "code" => 0,
  74. "msg" => "",
  75. "data" => array(
  76. "files" => $changedFiles,
  77. ),
  78. ));
  79. exit;
  80. } else if ($action === 'update_backup') {
  81. require_once(DEDEINC.'/libraries/dedehttpdown.class.php');
  82. checkLogin();
  83. // 获取本地更改过的文件
  84. $hashUrl = DEDEBIZCDN.'/release/'.$cfg_version_detail.'.json';
  85. $dhd = new DedeHttpDown();
  86. $dhd->OpenUrl($hashUrl);
  87. $data = $dhd->GetJSON();
  88. $changedFiles = array();
  89. $enkey = substr(md5(substr($cfg_cookie_encode, 0, 5)), 0, 10);
  90. $backupPath = DEDEDATA."/backupfile_{$enkey}";
  91. RmRecurse($backupPath);
  92. mkdir($backupPath);
  93. foreach ($data as $file) {
  94. $realFile = DEDEROOT.str_replace("\\", '/', $file->filename);
  95. if (file_exists($realFile) && md5_file($realFile) !== $file->hash) {
  96. // 备份文件
  97. $dstFile = $backupPath.'/'.str_replace("\\", '/', $file->filename);
  98. @mkdir(dirname($dstFile), 0777, true);
  99. copy($realFile, $dstFile);
  100. }
  101. }
  102. echo json_encode(array(
  103. "code" => 0,
  104. "msg" => "",
  105. "data" => array(
  106. "backupdir" => "data/backupfile_{$enkey}",
  107. ),
  108. ));
  109. exit;
  110. } else if ($action === 'get_update_versions') {
  111. require_once(DEDEINC.'/libraries/dedehttpdown.class.php');
  112. checkLogin();
  113. //获取本地更改过的文件
  114. $offUrl = DEDEBIZURL."/versions?version={$cfg_version_detail}";
  115. $dhd = new DedeHttpDown();
  116. $dhd->OpenUrl($offUrl);
  117. $data = $dhd->GetHtml();
  118. $arr = json_decode($data);
  119. SetCache('update', 'vers', $arr->result->Versions);
  120. echo $data;
  121. exit;
  122. } else if ($action === 'update') {
  123. require_once(DEDEINC.'/libraries/dedehttpdown.class.php');
  124. $row = GetCache('update', 'vers');
  125. if (count($row) === 0) {
  126. echo json_encode(array(
  127. "code" => -1,
  128. "msg" => "请先获取版本更新记录",
  129. "data" => null,
  130. ));
  131. exit;
  132. }
  133. $enkey = substr(md5(substr($cfg_cookie_encode, 0, 5)), 0, 10);
  134. $backupPath = DEDEDATA."/updatefile_{$enkey}";
  135. @mkdir($backupPath);
  136. foreach ($row as $k => $ver) {
  137. if ($ver->isdownload !== true) {
  138. $filesUrl = DEDEBIZCDN.'/update/'.$ver->ver.'/files.txt';
  139. $dhd = new DedeHttpDown();
  140. $dhd->OpenUrl($filesUrl);
  141. $fileList = $dhd->GetJSON();
  142. $dhd->Close();
  143. $backupVerPath = $backupPath.'/'.$ver->ver;
  144. RmRecurse($backupVerPath);
  145. mkdir($backupVerPath);
  146. foreach ($fileList as $f) {
  147. if (!preg_match("/^\//", $f->filename)) {
  148. //忽略src之外的目录
  149. continue;
  150. }
  151. $fileUrl = DEDEBIZCDN.'/update/'.$ver->ver.'/src'.$f->filename;
  152. $dhd = new DedeHttpDown();
  153. $dhd->OpenUrl($fileUrl);
  154. $fData = $dhd->GetHtml();
  155. $dhd->Close();
  156. $f->filename = preg_replace('/^\/admin/', $curDir, $f->filename);
  157. $realFile = $backupVerPath.$f->filename;
  158. @mkdir(dirname($realFile), 0777, true);
  159. file_put_contents($realFile, $fData);
  160. }
  161. $sqlUrl = DEDEBIZCDN.'/update/'.$ver->ver.'/update.sql';
  162. $dhd = new DedeHttpDown();
  163. $dhd->OpenUrl($sqlUrl);
  164. $fData = $dhd->GetHtml();
  165. $dhd->Close();
  166. $realFile = $backupVerPath.'/update.sql';
  167. file_put_contents($realFile, $fData);
  168. $realFile = $backupVerPath.'/files.txt';
  169. file_put_contents($realFile, json_encode($fileList));
  170. $row[$k]->isdownload = true;
  171. SetCache('update', 'vers', $row);
  172. echo json_encode(array(
  173. "code" => 0,
  174. "msg" => "正在下载{$ver->ver}的版本更新文件",
  175. "data" => array(
  176. "finish" => false,
  177. ),
  178. ));
  179. exit;
  180. }
  181. }
  182. foreach ($row as $k => $ver) {
  183. if ($ver->ispatched !== true) {
  184. $backupVerPath = $backupPath.'/'.$ver->ver;
  185. //执行更新SQL文件
  186. $sql = file_get_contents($backupVerPath.'/update.sql');
  187. if (!empty($sql)) {
  188. $sql = preg_replace('#ENGINE=MyISAM#i', 'TYPE=MyISAM', $sql);
  189. $sql41tmp = 'ENGINE=MyISAM DEFAULT CHARSET='.$cfg_db_language;
  190. $sql = preg_replace('#TYPE=MyISAM#i', $sql41tmp, $sql);
  191. $sqls = explode(";\r\n", $sql);
  192. foreach ($sqls as $sql) {
  193. if (trim($sql) != '') {
  194. $dsql->ExecuteNoneQuery(trim($sql));
  195. }
  196. }
  197. }
  198. //复制文件
  199. $fileList = json_decode(file_get_contents($backupVerPath.'/files.txt'));
  200. foreach ($fileList as $f) {
  201. if (!preg_match("/^\//", $f->filename)) {
  202. //忽略src之外的目录
  203. continue;
  204. }
  205. $f->filename = preg_replace('/^\/admin/', $curDir, $f->filename);
  206. $srcFile = $backupVerPath.$f->filename;
  207. $dstFile = str_replace(array("\\", "//"), '/', DEDEROOT.$f->filename);
  208. var_dump_cli('files','srcFile',$srcFile,'dstFile',$dstFile);
  209. // $rs = @copy($srcFile, $dstFile);
  210. // if($rs) {
  211. // unlink($srcFile);
  212. // }
  213. }
  214. $row[$k]->ispatched = true;
  215. SetCache('update', 'vers', $row);
  216. echo json_encode(array(
  217. "code" => 0,
  218. "msg" => "正在应用{$ver->ver}的版本补丁文件",
  219. "data" => array(
  220. "finish" => false,
  221. ),
  222. ));
  223. exit;
  224. }
  225. }
  226. echo json_encode(array(
  227. "code" => 0,
  228. "msg" => "",
  229. "data" => array(
  230. "finish" => true,
  231. ),
  232. ));
  233. exit;
  234. }