国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

487 行
17KB

  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 GNU GPL v2 (https://www.dedebiz.com/license)
  9. * @link https://www.dedebiz.com
  10. */
  11. define('AJAXLOGIN', TRUE);
  12. define('IS_DEDEAPI', TRUE);
  13. define('DEDEADMIN', str_replace("\\", '/', dirname(__FILE__)));
  14. require_once(DEDEADMIN.'/../system/common.inc.php');
  15. require_once(DEDEINC.'/userlogin.class.php');
  16. @set_time_limit(0);
  17. AjaxHead();
  18. helper('cache');
  19. $action = isset($action) && in_array($action, array('is_need_check_code', 'has_new_version', 'get_changed_files', 'update_backup', 'get_update_versions', 'update', 'upload_image', 'get_ai_server', 'get_setbody_url')) ? $action : '';
  20. $curDir = dirname(GetCurUrl());//当前目录
  21. /**
  22. * 登录鉴权
  23. *
  24. * @return void
  25. */
  26. function checkLogin()
  27. {
  28. $cuserLogin = new userLogin();
  29. if ($cuserLogin->getUserID() <= 0 || $cuserLogin->getUserType() != 10) {
  30. echo json_encode(array(
  31. "code" => -1,
  32. "msg" => "此操作需要登录超级管理员权限",
  33. "data" => null,
  34. ));
  35. exit;
  36. }
  37. }
  38. if ($action === 'is_need_check_code') {
  39. $cuserLogin = new userLogin();
  40. $isNeed = $cuserLogin->isNeedCheckCode($userid);
  41. echo json_encode(array(
  42. "code" => 0,
  43. "msg" => "",
  44. "data" => array(
  45. "isNeed" => $isNeed,
  46. ),
  47. ));
  48. exit;
  49. } else if ($action === 'has_new_version') {
  50. //判断版本SQL之间差异
  51. $unQueryVer = array();
  52. if (!TableHasField("#@__tagindex", "keywords")) {
  53. $unQueryVer[] = "6.0.2";
  54. }
  55. if (!TableHasField("#@__feedback", "replycount")) {
  56. $unQueryVer[] = "6.0.3";
  57. }
  58. if (!TableHasField("#@__arctype", "litimg")) {
  59. $unQueryVer[] = "6.1.0";
  60. }
  61. if (!$dsql->IsTable("#@__statistics")) {
  62. $unQueryVer[] = "6.1.7";
  63. }
  64. if (TableHasField("#@__tagindex", "tag_pinyin")) {
  65. $unQueryVer[] = "6.1.8";
  66. }
  67. if (!TableHasField("#@__admin", "pwd_new")) {
  68. $unQueryVer[] = "6.1.9";
  69. }
  70. if (!TableHasField("#@__arctype", "cnoverview")) {
  71. $unQueryVer[] = "6.1.10";
  72. }
  73. if (!TableHasField("#@__admin", "loginerr") || !TableHasField("#@__member", "loginerr")) {
  74. $unQueryVer[] = "6.2.0";
  75. }
  76. $row = $dsql->GetOne("SELECT COUNT(*) as dd FROM `#@__sysconfig` WHERE varname = 'cfg_bizcore_api'");
  77. if (isset($row['dd']) && $row['dd'] == 0) {
  78. $unQueryVer[] = "6.2.3";
  79. }
  80. if (!$dsql->IsTable("#@__sys_payment")) {
  81. $unQueryVer[] = "6.2.5";
  82. }
  83. if (!TableHasField("#@__arctype", "apienabled")) {
  84. $unQueryVer[] = "6.2.7";
  85. }
  86. if (!$dsql->IsTable("#@__ai")) {
  87. $unQueryVer[] = "6.5.0";
  88. }
  89. $row = $dsql->GetOne("SELECT COUNT(*) as dd FROM `#@__ai_prompt` WHERE pname = 'body_edit'");
  90. if (isset($row['dd']) && $row['dd'] == 0) {
  91. $unQueryVer[] = "6.5.1";
  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. $phpv = phpversion();
  110. $sp_os = PHP_OS;
  111. $mysql_ver = $dsql->GetVersion();
  112. $nurl = $_SERVER['HTTP_HOST'];
  113. if (preg_match("#[a-z\-]{1,}\.[a-z]{2,}#i", $nurl)) {
  114. $nurl = urlencode($nurl);
  115. } else {
  116. $nurl = "test";
  117. }
  118. $add_query = '';
  119. $query = "SELECT COUNT(*) AS dd FROM `#@__member` ";
  120. $row1 = $dsql->GetOne($query);
  121. if ($row1) $add_query .= "&mcount={$row1['dd']}";
  122. $query = "SELECT COUNT(*) AS dd FROM `#@__arctiny` ";
  123. $row2 = $dsql->GetOne($query);
  124. if ($row2) $add_query .= "&acount={$row2['dd']}";
  125. $offUrl = DEDEBIZURL."/version?version={$cfg_version_detail}&formurl={$nurl}&phpver={$phpv}&os={$sp_os}&mysqlver={$mysql_ver}{$add_query}&json=1";
  126. if (strpos($_SERVER['SERVER_SOFTWARE'], 'Development Server') !== false && version_compare(phpversion(), '7.2', '<')) {
  127. echo json_encode(array(
  128. "code"=>-1,
  129. "msg"=>'获取版本信息失败',
  130. ));
  131. exit;
  132. }
  133. $dhd = new DedeHttpDown();
  134. $dhd->OpenUrl($offUrl);
  135. $data = $dhd->GetHtml();
  136. if (empty($data)) {
  137. echo json_encode(array(
  138. "code" => -1,
  139. "msg" => '获取版本信息失败',
  140. ));
  141. } else {
  142. echo $data;
  143. }
  144. } else if ($action === 'get_changed_files') {
  145. require_once(DEDEINC.'/libraries/dedehttpdown.class.php');
  146. checkLogin();
  147. //获取本地更改过的文件
  148. $hashUrl = DEDEBIZCDN.'/release/'.$cfg_version_detail.'.json';
  149. $dhd = new DedeHttpDown();
  150. $dhd->OpenUrl($hashUrl);
  151. $data = $dhd->GetJSON();
  152. if (empty($data)) {
  153. echo json_encode(array(
  154. "code" => -1,
  155. "msg" => '获取版本信息失败',
  156. ));
  157. exit();
  158. }
  159. $changedFiles = array();
  160. foreach ($data as $file) {
  161. $realFile = DEDEROOT.str_replace("\\", '/', $file->filename);
  162. if (file_exists($realFile) && md5_file($realFile) !== $file->hash) {
  163. $changedFiles[] = $file;
  164. continue;
  165. }
  166. }
  167. echo json_encode(array(
  168. "code" => 0,
  169. "msg" => "",
  170. "data" => array(
  171. "files" => $changedFiles,
  172. ),
  173. ));
  174. exit;
  175. } else if ($action === 'update_backup') {
  176. require_once(DEDEINC.'/libraries/dedehttpdown.class.php');
  177. checkLogin();
  178. //获取本地更改过的文件
  179. $hashUrl = DEDEBIZCDN.'/release/'.$cfg_version_detail.'.json';
  180. $dhd = new DedeHttpDown();
  181. $dhd->OpenUrl($hashUrl);
  182. $data = $dhd->GetJSON();
  183. if (empty($data)) {
  184. echo json_encode(array(
  185. "code" => -1,
  186. "msg" => '获取版本信息失败',
  187. ));
  188. exit;
  189. }
  190. $changedFiles = array();
  191. $enkey = substr(md5(substr($cfg_cookie_encode, 0, 5)), 0, 10);
  192. $backupPath = DEDEDATA."/backupfile_{$enkey}";
  193. RmRecurse($backupPath);
  194. mkdir($backupPath);
  195. foreach ($data as $file) {
  196. $realFile = DEDEROOT.str_replace("\\", '/', $file->filename);
  197. //备份文件
  198. if (file_exists($realFile) && md5_file($realFile) !== $file->hash) {
  199. $dstFile = $backupPath.'/'.str_replace("\\", '/', $file->filename);
  200. @mkdir(dirname($dstFile), 0777, true);
  201. copy($realFile, $dstFile);
  202. }
  203. }
  204. echo json_encode(array(
  205. "code" => 0,
  206. "msg" => "",
  207. "data" => array(
  208. "backupdir" => "data/backupfile_{$enkey}",
  209. ),
  210. ));
  211. exit;
  212. } else if ($action === 'get_update_versions') {
  213. require_once(DEDEINC.'/libraries/dedehttpdown.class.php');
  214. checkLogin();
  215. //获取本地更改过的文件
  216. $offUrl = DEDEBIZURL."/versions?version={$cfg_version_detail}";
  217. $dhd = new DedeHttpDown();
  218. $dhd->OpenUrl($offUrl);
  219. $data = $dhd->GetHtml();
  220. if (empty($data)) {
  221. echo json_encode(array(
  222. "code" => -1,
  223. "msg" => '获取版本信息失败',
  224. ));
  225. exit;
  226. }
  227. $arr = json_decode($data);
  228. SetCache('update', 'vers', $arr->result->Versions);
  229. echo $data;
  230. exit;
  231. } else if ($action === 'update') {
  232. require_once(DEDEINC.'/libraries/dedehttpdown.class.php');
  233. $row = GetCache('update', 'vers');
  234. if (count($row) === 0) {
  235. echo json_encode(array(
  236. "code" => -1,
  237. "msg" => "请获取版本更新记录",
  238. "data" => null,
  239. ));
  240. exit;
  241. }
  242. $enkey = substr(md5(substr($cfg_cookie_encode, 0, 5)), 0, 10);
  243. $backupPath = DEDEDATA."/updatefile_{$enkey}";
  244. @mkdir($backupPath);
  245. foreach ($row as $k => $ver) {
  246. if ($ver->isdownload !== true) {
  247. $filesUrl = DEDEBIZCDN.'/update/'.$ver->ver.'/files.txt';
  248. $dhd = new DedeHttpDown();
  249. $dhd->OpenUrl($filesUrl);
  250. $fileList = $dhd->GetJSON();
  251. $dhd->Close();
  252. $backupVerPath = $backupPath.'/'.$ver->ver;
  253. if (!is_dir($backupVerPath)) {
  254. @mkdir($backupVerPath);
  255. }
  256. $i = 0;
  257. foreach ($fileList as $f) {
  258. $realFile = $backupVerPath.$f->filename;
  259. //忽略src之外的目录
  260. if (!preg_match("/^\//", $f->filename)) {
  261. continue;
  262. }
  263. if (file_exists($realFile)) {
  264. continue;
  265. }
  266. $fileUrl = DEDEBIZCDN.'/update/'.$ver->ver.'/src'.$f->filename;
  267. $dhd = new DedeHttpDown();
  268. $dhd->OpenUrl($fileUrl);
  269. $fData = $dhd->GetHtml();
  270. $dhd->Close();
  271. $f->filename = preg_replace('/^\/admin/', $curDir, $f->filename);
  272. @mkdir(dirname($realFile), 0777, true);
  273. file_put_contents($realFile, $fData);
  274. $i++;
  275. if ($i === 10) {
  276. echo json_encode(array(
  277. "code" => 0,
  278. "msg" => "正在下载{$ver->ver}版本的{$f->filename}文件",
  279. "data" => array(
  280. "finish" => false,
  281. ),
  282. ));
  283. exit;
  284. }
  285. }
  286. $sqlUrl = DEDEBIZCDN.'/update/'.$ver->ver.'/update.sql';
  287. $dhd = new DedeHttpDown();
  288. $dhd->OpenUrl($sqlUrl);
  289. $fData = $dhd->GetHtml();
  290. $dhd->Close();
  291. $realFile = $backupVerPath.'/update.sql';
  292. file_put_contents($realFile, $fData);
  293. $realFile = $backupVerPath.'/files.txt';
  294. file_put_contents($realFile, json_encode($fileList));
  295. $row[$k]->isdownload = true;
  296. SetCache('update', 'vers', $row);
  297. echo json_encode(array(
  298. "code" => 0,
  299. "msg" => "正在下载{$ver->ver}版本更新文件",
  300. "data" => array(
  301. "finish" => false,
  302. ),
  303. ));
  304. exit;
  305. }
  306. }
  307. foreach ($row as $k => $ver) {
  308. if ($ver->ispatched !== true) {
  309. $backupVerPath = $backupPath.'/'.$ver->ver;
  310. //执行更新SQL文件
  311. $sql = file_get_contents($backupVerPath.'/update.sql');
  312. if (!empty($sql)) {
  313. $sql = preg_replace('#ENGINE=MyISAM#i', 'TYPE=MyISAM', $sql);
  314. $sql41tmp = 'ENGINE=MyISAM DEFAULT CHARSET='.$cfg_db_language;
  315. $sql = preg_replace('#TYPE=MyISAM#i', $sql41tmp, $sql);
  316. $sqls = explode(";\r\n", $sql);
  317. foreach ($sqls as $sql) {
  318. if (trim($sql) != '') {
  319. $dsql->safeCheck = false;
  320. $dsql->ExecuteNoneQuery(trim($sql));
  321. $dsql->safeCheck = true;
  322. }
  323. }
  324. }
  325. //复制文件
  326. $fileList = json_decode(file_get_contents($backupVerPath.'/files.txt'));
  327. foreach ($fileList as $f) {
  328. //忽略src之外的目录
  329. if (!preg_match("/^\//", $f->filename)) {
  330. continue;
  331. }
  332. $f->filename = preg_replace('/^\/admin/', $curDir, $f->filename);
  333. $srcFile = $backupVerPath.$f->filename;
  334. $dstFile = str_replace(array("\\", "//"), '/', DEDEROOT.$f->filename);
  335. @mkdir(dirname($dstFile), 0777, true);
  336. $rs = @copy($srcFile, $dstFile);
  337. if ($rs) {
  338. unlink($srcFile);
  339. }
  340. }
  341. $row[$k]->ispatched = true;
  342. SetCache('update', 'vers', $row);
  343. RmRecurse($backupVerPath);
  344. echo json_encode(array(
  345. "code" => 0,
  346. "msg" => "正在更新{$ver->ver}版本补丁文件",
  347. "data" => array(
  348. "finish" => false,
  349. ),
  350. ));
  351. exit;
  352. }
  353. }
  354. echo json_encode(array(
  355. "code" => 0,
  356. "msg" => "",
  357. "data" => array(
  358. "finish" => true,
  359. ),
  360. ));
  361. exit;
  362. } else if($action === 'upload_image') {
  363. $cuserLogin = new userLogin();
  364. if ($cuserLogin->getUserID() <= 0) {
  365. echo json_encode(array(
  366. "code" => -1,
  367. "msg" => "登录系统后才能上传图片",
  368. "data" => null,
  369. ));
  370. exit;
  371. }
  372. $imgfile_name = $_FILES["file"]['name'];
  373. $activepath = $cfg_image_dir;
  374. $allowedTypes = array("image/pjpeg", "image/jpeg", "image/gif", "image/png", "image/xpng", "image/wbmp", "image/webp");
  375. $uploadedFile = $_FILES['file']['tmp_name'];
  376. if (!function_exists('mime_content_type')) {
  377. echo json_encode(array(
  378. "code" => -1,
  379. "uploaded" => 0,
  380. "error" => array(
  381. "message" => "系统不支持fileinfo组件,建议php.ini中开启",
  382. ),
  383. ));
  384. exit;
  385. }
  386. if (empty($uploadedFile)) {
  387. echo json_encode(array(
  388. "code" => -1,
  389. "msg" => "文件为空",
  390. "data" => null,
  391. ));
  392. exit;
  393. }
  394. $fileType = mime_content_type($uploadedFile);
  395. $imgSize = getimagesize($uploadedFile);
  396. if (!in_array($fileType, $allowedTypes) || !$imgSize) {
  397. echo json_encode(array(
  398. "code" => -1,
  399. "uploaded" => 0,
  400. "error" => array(
  401. "message" => "仅支持图片格式文件",
  402. ),
  403. ));
  404. exit;
  405. }
  406. $nowtme = time();
  407. $mdir = MyDate($cfg_addon_savetype, $nowtme);
  408. if (!is_dir($cfg_basedir.$activepath."/$mdir")) {
  409. MkdirAll($cfg_basedir.$activepath."/$mdir", $cfg_dir_purview);
  410. }
  411. $cuserLogin = new userLogin();
  412. $iseditor = isset($iseditor)? intval($iseditor) : 0;
  413. $filename_name = $cuserLogin->getUserID().'-'.dd2char(MyDate("ymdHis", $nowtme).mt_rand(100, 999));
  414. $filename = $mdir.'/'.$filename_name;
  415. $fs = explode('.', $imgfile_name);
  416. $filename = $filename.'.'.$fs[count($fs) - 1];
  417. $filename_name = $filename_name.'.'.$fs[count($fs) - 1];
  418. $fullfilename = $cfg_basedir.$activepath."/".$filename;
  419. if (preg_match('#\.(php|pl|cgi|asp|aspx|jsp|php5|php4|php3|shtm|shtml|htm)$#i', trim($fullfilename))) {
  420. echo json_encode(array(
  421. "code" => -1,
  422. "uploaded" => 0,
  423. "error" => array(
  424. "message" => "文件扩展名已被系统禁止",
  425. ),
  426. ));
  427. exit;
  428. }
  429. move_uploaded_file($_FILES["file"]["tmp_name"], $fullfilename) or die(json_encode(array(
  430. "code" => -1,
  431. "uploaded" => 0,
  432. "error" => array(
  433. "message" => "上传失败",
  434. ),
  435. )));
  436. $info = '';
  437. $sizes[0] = 0;
  438. $sizes[1] = 0;
  439. $sizes = getimagesize($fullfilename, $info);
  440. $imgwidthValue = $sizes[0];
  441. $imgheightValue = $sizes[1];
  442. $imgsize = filesize($fullfilename);
  443. $inquery = "INSERT INTO `#@__uploads` (arcid,title,url,mediatype,width,height,playtime,filesize,uptime,mid) VALUES ('0','$filename','".$activepath."/".$filename."','1','$imgwidthValue','$imgheightValue','0','{$imgsize}','{$nowtme}','".$cuserLogin->getUserID()."'); ";
  444. $dsql->ExecuteNoneQuery($inquery);
  445. $fid = $dsql->GetLastID();
  446. AddMyAddon($fid, $activepath.'/'.$filename);
  447. echo json_encode(array(
  448. "code" => 0,
  449. "msg" => "上传成功",
  450. "data" => $activepath."/".$filename,
  451. ));
  452. } else if($action === 'get_ai_server') {
  453. $params = $_GET;
  454. unset($params['action']);
  455. checkLogin();
  456. $params['timestamp'] = time(); // 加入时间戳
  457. $cuserLogin = new userLogin();
  458. $params['adminid'] = $cuserLogin->getUserID(); // 加入时间戳
  459. $params['ip'] = $_SERVER['REMOTE_ADDR'] ?? '127.0.0.1'; // 获取客户端IP
  460. ksort($params); // 按字典序排序
  461. $queryString = http_build_query($params); // 生成查询字符串
  462. $params['sign'] = md5($queryString . $cfg_ai_apikey); // 计算MD5签名
  463. $url = $cfg_ai_server . '/ai?' . http_build_query($params);
  464. echo json_encode(array(
  465. "code" => 0,
  466. "data" => $url,
  467. ));
  468. } else if($action === 'get_setbody_url') {
  469. $params = $_GET;
  470. unset($params['action']);
  471. checkLogin();
  472. $params['timestamp'] = time(); // 加入时间戳
  473. $cuserLogin = new userLogin();
  474. $params['adminid'] = $cuserLogin->getUserID(); // 加入时间戳
  475. $params['ip'] = $_SERVER['REMOTE_ADDR'] ?? '127.0.0.1'; // 获取客户端IP
  476. ksort($params); // 按字典序排序
  477. $queryString = http_build_query($params); // 生成查询字符串
  478. $params['sign'] = md5($queryString . $cfg_ai_apikey); // 计算MD5签名
  479. $url = $cfg_ai_server . '/api/setbody?' . http_build_query($params);
  480. echo json_encode(array(
  481. "code" => 0,
  482. "data" => $url,
  483. ));
  484. }
  485. ?>