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

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