国内流行的内容管理系统(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.

235 lines
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. $row = GetCache('update', 'vers');
  124. if (count($row) === 0) {
  125. echo json_encode(array(
  126. "code" => -1,
  127. "msg" => "请先获取版本更新记录",
  128. "data" => null,
  129. ));
  130. exit;
  131. }
  132. $enkey = substr(md5(substr($cfg_cookie_encode, 0, 5)), 0, 10);
  133. $backupPath = DEDEDATA."/updatefile_{$enkey}";
  134. @mkdir($backupPath);
  135. foreach ($row as $k => $ver) {
  136. if ($ver->isdownload !== true) {
  137. $filesUrl = DEDEBIZCDN.'/update/'.$ver->ver.'/files.txt';
  138. $dhd = new DedeHttpDown();
  139. $dhd->OpenUrl($filesUrl);
  140. $fileList = $dhd->GetJSON();
  141. $dhd->Close();
  142. $backupVerPath = $backupPath.'/'.$ver->ver;
  143. RmRecurse($backupVerPath);
  144. mkdir($backupVerPath);
  145. foreach ($fileList as $f) {
  146. if (!preg_match("/^\//", $f->filename)) {
  147. //忽略src之外的目录
  148. continue;
  149. }
  150. $fileUrl = DEDEBIZCDN.'/update/'.$ver->ver.'/src'.$f->filename;
  151. $dhd = new DedeHttpDown();
  152. $dhd->OpenUrl($fileUrl);
  153. $fData = $dhd->GetHtml();
  154. $dhd->Close();
  155. $f->filename = preg_replace('/^\/admin/', $curDir, $f->filename);
  156. $realFile = $backupVerPath.$f->filename;
  157. @mkdir(dirname($realFile), 0777, true);
  158. file_put_contents($realFile, $fData);
  159. }
  160. $sqlUrl = DEDEBIZCDN.'/update/'.$ver->ver.'/update.sql';
  161. $dhd = new DedeHttpDown();
  162. $dhd->OpenUrl($sqlUrl);
  163. $fData = $dhd->GetHtml();
  164. $dhd->Close();
  165. $realFile = $backupVerPath.'/update.sql';
  166. file_put_contents($realFile, $fData);
  167. $realFile = $backupVerPath.'/files.txt';
  168. file_put_contents($realFile, json_encode($fileList));
  169. $row[$k]->isdownload = true;
  170. SetCache('update', 'vers', $row);
  171. echo json_encode(array(
  172. "code" => 0,
  173. "msg" => "正在下载{$ver->ver}的版本更新文件",
  174. "data" => array(
  175. "finish" => false,
  176. ),
  177. ));
  178. exit;
  179. }
  180. }
  181. foreach ($row as $k => $ver) {
  182. if ($ver->ispatched !== true) {
  183. $backupVerPath = $backupPath.'/'.$ver->ver;
  184. //执行更新SQL文件
  185. $sql = file_get_contents($backupVerPath.'/update.sql');
  186. if (!empty($sql)) {
  187. $sql = preg_replace('#ENGINE=MyISAM#i', 'TYPE=MyISAM', $sql);
  188. $sql41tmp = 'ENGINE=MyISAM DEFAULT CHARSET='.$cfg_db_language;
  189. $sql = preg_replace('#TYPE=MyISAM#i', $sql41tmp, $sql);
  190. $sqls = explode(";\r\n", $sql);
  191. foreach ($sqls as $sql) {
  192. if (trim($sql) != '') {
  193. $dsql->ExecuteNoneQuery(trim($sql));
  194. }
  195. }
  196. }
  197. //复制文件
  198. $fileList = json_decode(file_get_contents($backupVerPath.'/files.txt'));
  199. foreach ($fileList as $f) {
  200. if (!preg_match("/^\//", $f->filename)) {
  201. //忽略src之外的目录
  202. continue;
  203. }
  204. $f->filename = preg_replace('/^\/admin/', $curDir, $f->filename);
  205. $srcFile = $backupVerPath.$f->filename;
  206. $dstFile = str_replace(array("\\", "//"), '/', DEDEROOT.$f->filename);
  207. var_dump_cli('files','srcFile',$srcFile,'dstFile',$dstFile);
  208. // $rs = @copy($srcFile, $dstFile);
  209. // if($rs) {
  210. // unlink($srcFile);
  211. // }
  212. }
  213. $row[$k]->ispatched = true;
  214. SetCache('update', 'vers', $row);
  215. echo json_encode(array(
  216. "code" => 0,
  217. "msg" => "正在应用{$ver->ver}的版本补丁文件",
  218. "data" => array(
  219. "finish" => false,
  220. ),
  221. ));
  222. exit;
  223. }
  224. }
  225. echo json_encode(array(
  226. "code" => 0,
  227. "msg" => "",
  228. "data" => array(
  229. "finish" => true,
  230. ),
  231. ));
  232. exit;
  233. }