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

322 lines
11KB

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