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

305 lines
10KB

  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. $cfg_NotPrintHead = 'Y';
  14. require_once(DEDEADMIN.'/../system/common.inc.php');
  15. require_once(DEDEINC.'/userlogin.class.php');
  16. AjaxHead();
  17. helper('cache');
  18. $action = isset($action) && in_array($action, array('is_need_check_code', 'has_new_version', 'get_changed_files', 'update_backup', 'get_update_versions', 'update')) ? $action : '';
  19. $curDir = dirname(GetCurUrl()); //当前目录
  20. /**
  21. * 登录鉴权
  22. *
  23. * @return void
  24. */
  25. function checkLogin()
  26. {
  27. $cuserLogin = new userLogin();
  28. if ($cuserLogin->getUserID() <= 0 || $cuserLogin->getUserType() != 10) {
  29. echo json_encode(array(
  30. "code" => -1,
  31. "msg" => "当前操作需要登录超级管理员账号",
  32. "data" => null,
  33. ));
  34. exit;
  35. }
  36. }
  37. if ($action === 'is_need_check_code') {
  38. $cuserLogin = new userLogin();
  39. $isNeed = $cuserLogin->isNeedCheckCode($userid);
  40. echo json_encode(array(
  41. "code" => 0,
  42. "msg" => "",
  43. "data" => array(
  44. "isNeed" => $isNeed,
  45. ),
  46. ));
  47. exit;
  48. } else if ($action === 'has_new_version') {
  49. //判断版本更新差异sql
  50. $unQueryVer = array();
  51. if (!TableHasField("#@__tagindex", "keywords")) {
  52. $unQueryVer[] = "6.0.2";
  53. }
  54. if (!TableHasField("#@__feedback", "replycount")) {
  55. $unQueryVer[] = "6.0.3";
  56. }
  57. if (!TableHasField("#@__arctype", "litimg")) {
  58. $unQueryVer[] = "6.1.0";
  59. }
  60. if (!$dsql->IsTable("#@__statistics")) {
  61. $unQueryVer[] = "6.1.7";
  62. }
  63. if (TableHasField("#@__tagindex", "tag_pinyin")) {
  64. $unQueryVer[] = "6.1.8";
  65. }
  66. if (!TableHasField("#@__admin", "pwd_new")) {
  67. $unQueryVer[] = "6.1.9";
  68. }
  69. if (!TableHasField("#@__arctype", "cnoverview")) {
  70. $unQueryVer[] = "6.1.10";
  71. }
  72. if (!TableHasField("#@__admin", "loginerr") || !TableHasField("#@__member", "loginerr")) {
  73. $unQueryVer[] = "6.2.0";
  74. }
  75. if (count($unQueryVer) > 0) {
  76. $upsqls = GetUpdateSQL();
  77. foreach ($unQueryVer as $vv) {
  78. $ss = $upsqls[$vv];
  79. foreach ($ss as $s) {
  80. if (trim($s) != '') {
  81. $dsql->safeCheck = false;
  82. $dsql->ExecuteNoneQuery(trim($s));
  83. $dsql->safeCheck = true;
  84. }
  85. }
  86. }
  87. }
  88. require_once(DEDEINC.'/libraries/dedehttpdown.class.php');
  89. checkLogin();
  90. //是否存在更新版本
  91. $offUrl = DEDEBIZURL."/version?version={$cfg_version_detail}&formurl={$nurl}&phpver={$phpv}&os={$sp_os}&mysqlver={$mysql_ver}{$add_query}&json=1";
  92. $dhd = new DedeHttpDown();
  93. $dhd->OpenUrl($offUrl);
  94. $data = $dhd->GetHtml();
  95. if (empty($data)) {
  96. echo json_encode(array(
  97. "code"=>-1,
  98. "msg"=>'获取版本信息失败',
  99. ));
  100. } else {
  101. echo $data;
  102. }
  103. } else if ($action === 'get_changed_files') {
  104. require_once(DEDEINC.'/libraries/dedehttpdown.class.php');
  105. checkLogin();
  106. //获取本地更改过的文件
  107. $hashUrl = DEDEBIZCDN.'/release/'.$cfg_version_detail.'.json';
  108. $dhd = new DedeHttpDown();
  109. $dhd->OpenUrl($hashUrl);
  110. $data = $dhd->GetJSON();
  111. if (empty($data)) {
  112. echo json_encode(array(
  113. "code"=>-1,
  114. "msg"=>'获取版本信息失败',
  115. ));
  116. exit();
  117. }
  118. $changedFiles = array();
  119. foreach ($data as $file) {
  120. $realFile = DEDEROOT.str_replace("\\", '/', $file->filename);
  121. if (file_exists($realFile) && md5_file($realFile) !== $file->hash) {
  122. $changedFiles[] = $file;
  123. continue;
  124. }
  125. }
  126. echo json_encode(array(
  127. "code" => 0,
  128. "msg" => "",
  129. "data" => array(
  130. "files" => $changedFiles,
  131. ),
  132. ));
  133. exit;
  134. } else if ($action === 'update_backup') {
  135. require_once(DEDEINC.'/libraries/dedehttpdown.class.php');
  136. checkLogin();
  137. //获取本地更改过的文件
  138. $hashUrl = DEDEBIZCDN.'/release/'.$cfg_version_detail.'.json';
  139. $dhd = new DedeHttpDown();
  140. $dhd->OpenUrl($hashUrl);
  141. $data = $dhd->GetJSON();
  142. if (empty($data)) {
  143. echo json_encode(array(
  144. "code"=>-1,
  145. "msg"=>'获取版本信息失败',
  146. ));
  147. exit;
  148. }
  149. $changedFiles = array();
  150. $enkey = substr(md5(substr($cfg_cookie_encode, 0, 5)), 0, 10);
  151. $backupPath = DEDEDATA."/backupfile_{$enkey}";
  152. RmRecurse($backupPath);
  153. mkdir($backupPath);
  154. foreach ($data as $file) {
  155. $realFile = DEDEROOT.str_replace("\\", '/', $file->filename);
  156. if (file_exists($realFile) && md5_file($realFile) !== $file->hash) {
  157. //备份文件
  158. $dstFile = $backupPath.'/'.str_replace("\\", '/', $file->filename);
  159. @mkdir(dirname($dstFile), 0777, true);
  160. copy($realFile, $dstFile);
  161. }
  162. }
  163. echo json_encode(array(
  164. "code" => 0,
  165. "msg" => "",
  166. "data" => array(
  167. "backupdir" => "data/backupfile_{$enkey}",
  168. ),
  169. ));
  170. exit;
  171. } else if ($action === 'get_update_versions') {
  172. require_once(DEDEINC.'/libraries/dedehttpdown.class.php');
  173. checkLogin();
  174. //获取本地更改过的文件
  175. $offUrl = DEDEBIZURL."/versions?version={$cfg_version_detail}";
  176. $dhd = new DedeHttpDown();
  177. $dhd->OpenUrl($offUrl);
  178. $data = $dhd->GetHtml();
  179. if (empty($data)) {
  180. echo json_encode(array(
  181. "code"=>-1,
  182. "msg"=>'获取版本信息失败',
  183. ));
  184. exit;
  185. }
  186. $arr = json_decode($data);
  187. SetCache('update', 'vers', $arr->result->Versions);
  188. echo $data;
  189. exit;
  190. } else if ($action === 'update') {
  191. require_once(DEDEINC.'/libraries/dedehttpdown.class.php');
  192. $row = GetCache('update', 'vers');
  193. if (count($row) === 0) {
  194. echo json_encode(array(
  195. "code" => -1,
  196. "msg" => "请先获取版本更新记录",
  197. "data" => null,
  198. ));
  199. exit;
  200. }
  201. $enkey = substr(md5(substr($cfg_cookie_encode, 0, 5)), 0, 10);
  202. $backupPath = DEDEDATA."/updatefile_{$enkey}";
  203. @mkdir($backupPath);
  204. foreach ($row as $k => $ver) {
  205. if ($ver->isdownload !== true) {
  206. $filesUrl = DEDEBIZCDN.'/update/'.$ver->ver.'/files.txt';
  207. $dhd = new DedeHttpDown();
  208. $dhd->OpenUrl($filesUrl);
  209. $fileList = $dhd->GetJSON();
  210. $dhd->Close();
  211. $backupVerPath = $backupPath.'/'.$ver->ver;
  212. RmRecurse($backupVerPath);
  213. mkdir($backupVerPath);
  214. foreach ($fileList as $f) {
  215. if (!preg_match("/^\//", $f->filename)) {
  216. //忽略src之外的目录
  217. continue;
  218. }
  219. $fileUrl = DEDEBIZCDN.'/update/'.$ver->ver.'/src'.$f->filename;
  220. $dhd = new DedeHttpDown();
  221. $dhd->OpenUrl($fileUrl);
  222. $fData = $dhd->GetHtml();
  223. $dhd->Close();
  224. $f->filename = preg_replace('/^\/admin/', $curDir, $f->filename);
  225. $realFile = $backupVerPath.$f->filename;
  226. @mkdir(dirname($realFile), 0777, true);
  227. file_put_contents($realFile, $fData);
  228. }
  229. $sqlUrl = DEDEBIZCDN.'/update/'.$ver->ver.'/update.sql';
  230. $dhd = new DedeHttpDown();
  231. $dhd->OpenUrl($sqlUrl);
  232. $fData = $dhd->GetHtml();
  233. $dhd->Close();
  234. $realFile = $backupVerPath.'/update.sql';
  235. file_put_contents($realFile, $fData);
  236. $realFile = $backupVerPath.'/files.txt';
  237. file_put_contents($realFile, json_encode($fileList));
  238. $row[$k]->isdownload = true;
  239. SetCache('update', 'vers', $row);
  240. echo json_encode(array(
  241. "code" => 0,
  242. "msg" => "正在下载{$ver->ver}的版本更新文件",
  243. "data" => array(
  244. "finish" => false,
  245. ),
  246. ));
  247. exit;
  248. }
  249. }
  250. foreach ($row as $k => $ver) {
  251. if ($ver->ispatched !== true) {
  252. $backupVerPath = $backupPath.'/'.$ver->ver;
  253. //执行更新SQL文件
  254. $sql = file_get_contents($backupVerPath.'/update.sql');
  255. if (!empty($sql)) {
  256. $sql = preg_replace('#ENGINE=MyISAM#i', 'TYPE=MyISAM', $sql);
  257. $sql41tmp = 'ENGINE=MyISAM DEFAULT CHARSET='.$cfg_db_language;
  258. $sql = preg_replace('#TYPE=MyISAM#i', $sql41tmp, $sql);
  259. $sqls = explode(";\r\n", $sql);
  260. foreach ($sqls as $sql) {
  261. if (trim($sql) != '') {
  262. $dsql->safeCheck = false;
  263. $dsql->ExecuteNoneQuery(trim($sql));
  264. $dsql->safeCheck = true;
  265. }
  266. }
  267. }
  268. //复制文件
  269. $fileList = json_decode(file_get_contents($backupVerPath.'/files.txt'));
  270. foreach ($fileList as $f) {
  271. if (!preg_match("/^\//", $f->filename)) {
  272. //忽略src之外的目录
  273. continue;
  274. }
  275. $f->filename = preg_replace('/^\/admin/', $curDir, $f->filename);
  276. $srcFile = $backupVerPath.$f->filename;
  277. $dstFile = str_replace(array("\\", "//"), '/', DEDEROOT.$f->filename);
  278. var_dump_cli('files','srcFile',$srcFile,'dstFile',$dstFile);
  279. // $rs = @copy($srcFile, $dstFile);
  280. // if($rs) {
  281. // unlink($srcFile);
  282. // }
  283. }
  284. $row[$k]->ispatched = true;
  285. SetCache('update', 'vers', $row);
  286. echo json_encode(array(
  287. "code" => 0,
  288. "msg" => "正在应用{$ver->ver}的版本补丁文件",
  289. "data" => array(
  290. "finish" => false,
  291. ),
  292. ));
  293. exit;
  294. }
  295. }
  296. echo json_encode(array(
  297. "code" => 0,
  298. "msg" => "",
  299. "data" => array(
  300. "finish" => true,
  301. ),
  302. ));
  303. exit;
  304. }
  305. ?>