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

api.php 8.9KB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. if (empty($data)) {
  56. echo json_encode(array(
  57. "code"=>-1,
  58. "msg"=>'获取版本信息失败',
  59. ));
  60. } else {
  61. echo $data;
  62. }
  63. } else if ($action === 'get_changed_files') {
  64. require_once(DEDEINC.'/libraries/dedehttpdown.class.php');
  65. checkLogin();
  66. // 获取本地更改过的文件
  67. $hashUrl = DEDEBIZCDN.'/release/'.$cfg_version_detail.'.json';
  68. $dhd = new DedeHttpDown();
  69. $dhd->OpenUrl($hashUrl);
  70. $data = $dhd->GetJSON();
  71. if (empty($data)) {
  72. echo json_encode(array(
  73. "code"=>-1,
  74. "msg"=>'获取版本信息失败',
  75. ));
  76. exit();
  77. }
  78. $changedFiles = array();
  79. foreach ($data as $file) {
  80. $realFile = DEDEROOT.str_replace("\\", '/', $file->filename);
  81. if (file_exists($realFile) && md5_file($realFile) !== $file->hash) {
  82. $changedFiles[] = $file;
  83. continue;
  84. }
  85. }
  86. echo json_encode(array(
  87. "code" => 0,
  88. "msg" => "",
  89. "data" => array(
  90. "files" => $changedFiles,
  91. ),
  92. ));
  93. exit;
  94. } else if ($action === 'update_backup') {
  95. require_once(DEDEINC.'/libraries/dedehttpdown.class.php');
  96. checkLogin();
  97. // 获取本地更改过的文件
  98. $hashUrl = DEDEBIZCDN.'/release/'.$cfg_version_detail.'.json';
  99. $dhd = new DedeHttpDown();
  100. $dhd->OpenUrl($hashUrl);
  101. $data = $dhd->GetJSON();
  102. if (empty($data)) {
  103. echo json_encode(array(
  104. "code"=>-1,
  105. "msg"=>'获取版本信息失败',
  106. ));
  107. exit;
  108. }
  109. $changedFiles = array();
  110. $enkey = substr(md5(substr($cfg_cookie_encode, 0, 5)), 0, 10);
  111. $backupPath = DEDEDATA."/backupfile_{$enkey}";
  112. RmRecurse($backupPath);
  113. mkdir($backupPath);
  114. foreach ($data as $file) {
  115. $realFile = DEDEROOT.str_replace("\\", '/', $file->filename);
  116. if (file_exists($realFile) && md5_file($realFile) !== $file->hash) {
  117. // 备份文件
  118. $dstFile = $backupPath.'/'.str_replace("\\", '/', $file->filename);
  119. @mkdir(dirname($dstFile), 0777, true);
  120. copy($realFile, $dstFile);
  121. }
  122. }
  123. echo json_encode(array(
  124. "code" => 0,
  125. "msg" => "",
  126. "data" => array(
  127. "backupdir" => "data/backupfile_{$enkey}",
  128. ),
  129. ));
  130. exit;
  131. } else if ($action === 'get_update_versions') {
  132. require_once(DEDEINC.'/libraries/dedehttpdown.class.php');
  133. checkLogin();
  134. //获取本地更改过的文件
  135. $offUrl = DEDEBIZURL."/versions?version={$cfg_version_detail}";
  136. $dhd = new DedeHttpDown();
  137. $dhd->OpenUrl($offUrl);
  138. $data = $dhd->GetHtml();
  139. if (empty($data)) {
  140. echo json_encode(array(
  141. "code"=>-1,
  142. "msg"=>'获取版本信息失败',
  143. ));
  144. exit;
  145. }
  146. $arr = json_decode($data);
  147. SetCache('update', 'vers', $arr->result->Versions);
  148. echo $data;
  149. exit;
  150. } else if ($action === 'update') {
  151. require_once(DEDEINC.'/libraries/dedehttpdown.class.php');
  152. $row = GetCache('update', 'vers');
  153. if (count($row) === 0) {
  154. echo json_encode(array(
  155. "code" => -1,
  156. "msg" => "请先获取版本更新记录",
  157. "data" => null,
  158. ));
  159. exit;
  160. }
  161. $enkey = substr(md5(substr($cfg_cookie_encode, 0, 5)), 0, 10);
  162. $backupPath = DEDEDATA."/updatefile_{$enkey}";
  163. @mkdir($backupPath);
  164. foreach ($row as $k => $ver) {
  165. if ($ver->isdownload !== true) {
  166. $filesUrl = DEDEBIZCDN.'/update/'.$ver->ver.'/files.txt';
  167. $dhd = new DedeHttpDown();
  168. $dhd->OpenUrl($filesUrl);
  169. $fileList = $dhd->GetJSON();
  170. $dhd->Close();
  171. $backupVerPath = $backupPath.'/'.$ver->ver;
  172. RmRecurse($backupVerPath);
  173. mkdir($backupVerPath);
  174. foreach ($fileList as $f) {
  175. if (!preg_match("/^\//", $f->filename)) {
  176. //忽略src之外的目录
  177. continue;
  178. }
  179. $fileUrl = DEDEBIZCDN.'/update/'.$ver->ver.'/src'.$f->filename;
  180. $dhd = new DedeHttpDown();
  181. $dhd->OpenUrl($fileUrl);
  182. $fData = $dhd->GetHtml();
  183. $dhd->Close();
  184. $f->filename = preg_replace('/^\/admin/', $curDir, $f->filename);
  185. $realFile = $backupVerPath.$f->filename;
  186. @mkdir(dirname($realFile), 0777, true);
  187. file_put_contents($realFile, $fData);
  188. }
  189. $sqlUrl = DEDEBIZCDN.'/update/'.$ver->ver.'/update.sql';
  190. $dhd = new DedeHttpDown();
  191. $dhd->OpenUrl($sqlUrl);
  192. $fData = $dhd->GetHtml();
  193. $dhd->Close();
  194. $realFile = $backupVerPath.'/update.sql';
  195. file_put_contents($realFile, $fData);
  196. $realFile = $backupVerPath.'/files.txt';
  197. file_put_contents($realFile, json_encode($fileList));
  198. $row[$k]->isdownload = true;
  199. SetCache('update', 'vers', $row);
  200. echo json_encode(array(
  201. "code" => 0,
  202. "msg" => "正在下载{$ver->ver}的版本更新文件",
  203. "data" => array(
  204. "finish" => false,
  205. ),
  206. ));
  207. exit;
  208. }
  209. }
  210. foreach ($row as $k => $ver) {
  211. if ($ver->ispatched !== true) {
  212. $backupVerPath = $backupPath.'/'.$ver->ver;
  213. //执行更新SQL文件
  214. $sql = file_get_contents($backupVerPath.'/update.sql');
  215. if (!empty($sql)) {
  216. $sql = preg_replace('#ENGINE=MyISAM#i', 'TYPE=MyISAM', $sql);
  217. $sql41tmp = 'ENGINE=MyISAM DEFAULT CHARSET='.$cfg_db_language;
  218. $sql = preg_replace('#TYPE=MyISAM#i', $sql41tmp, $sql);
  219. $sqls = explode(";\r\n", $sql);
  220. foreach ($sqls as $sql) {
  221. if (trim($sql) != '') {
  222. $dsql->ExecuteNoneQuery(trim($sql));
  223. }
  224. }
  225. }
  226. //复制文件
  227. $fileList = json_decode(file_get_contents($backupVerPath.'/files.txt'));
  228. foreach ($fileList as $f) {
  229. if (!preg_match("/^\//", $f->filename)) {
  230. //忽略src之外的目录
  231. continue;
  232. }
  233. $f->filename = preg_replace('/^\/admin/', $curDir, $f->filename);
  234. $srcFile = $backupVerPath.$f->filename;
  235. $dstFile = str_replace(array("\\", "//"), '/', DEDEROOT.$f->filename);
  236. var_dump_cli('files','srcFile',$srcFile,'dstFile',$dstFile);
  237. // $rs = @copy($srcFile, $dstFile);
  238. // if($rs) {
  239. // unlink($srcFile);
  240. // }
  241. }
  242. $row[$k]->ispatched = true;
  243. SetCache('update', 'vers', $row);
  244. echo json_encode(array(
  245. "code" => 0,
  246. "msg" => "正在应用{$ver->ver}的版本补丁文件",
  247. "data" => array(
  248. "finish" => false,
  249. ),
  250. ));
  251. exit;
  252. }
  253. }
  254. echo json_encode(array(
  255. "code" => 0,
  256. "msg" => "",
  257. "data" => array(
  258. "finish" => true,
  259. ),
  260. ));
  261. exit;
  262. }