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

384 lines
13KB

  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('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. 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', 'upload_image')) ? $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. $row = $dsql->GetOne("SELECT COUNT(*) as dd FROM `#@__sysconfig` WHERE varname = 'cfg_bizcore_api'");
  76. if ($row['dd'] == 0) {
  77. $unQueryVer[] = "6.2.3";
  78. }
  79. if (!$dsql->IsTable("#@__sys_payment")) {
  80. $unQueryVer[] = "6.2.5";
  81. }
  82. if (!TableHasField("#@__arctype", "apienabled")) {
  83. $unQueryVer[] = "6.2.7";
  84. }
  85. if (count($unQueryVer) > 0) {
  86. $upsqls = GetUpdateSQL();
  87. foreach ($unQueryVer as $vv) {
  88. $ss = $upsqls[$vv];
  89. foreach ($ss as $s) {
  90. if (trim($s) != '') {
  91. $dsql->safeCheck = false;
  92. $dsql->ExecuteNoneQuery(trim($s));
  93. $dsql->safeCheck = true;
  94. }
  95. }
  96. }
  97. }
  98. require_once(DEDEINC.'/libraries/dedehttpdown.class.php');
  99. checkLogin();
  100. //是否存在更新版本
  101. $phpv = phpversion();
  102. $sp_os = PHP_OS;
  103. $mysql_ver = $dsql->GetVersion();
  104. $nurl = $_SERVER['HTTP_HOST'];
  105. if (preg_match("#[a-z\-]{1,}\.[a-z]{2,}#i", $nurl)) {
  106. $nurl = urlencode($nurl);
  107. } else {
  108. $nurl = "test";
  109. }
  110. $add_query = '';
  111. $query = "SELECT COUNT(*) AS dd FROM `#@__member` ";
  112. $row1 = $dsql->GetOne($query);
  113. if ($row1) $add_query .= "&mcount={$row1['dd']}";
  114. $query = "SELECT COUNT(*) AS dd FROM `#@__arctiny` ";
  115. $row2 = $dsql->GetOne($query);
  116. if ($row2) $add_query .= "&acount={$row2['dd']}";
  117. $offUrl = DEDEBIZURL."/version?version={$cfg_version_detail}&formurl={$nurl}&phpver={$phpv}&os={$sp_os}&mysqlver={$mysql_ver}{$add_query}&json=1";
  118. $dhd = new DedeHttpDown();
  119. $dhd->OpenUrl($offUrl);
  120. $data = $dhd->GetHtml();
  121. if (empty($data)) {
  122. echo json_encode(array(
  123. "code"=>-1,
  124. "msg"=>'获取版本信息失败',
  125. ));
  126. } else {
  127. echo $data;
  128. }
  129. } else if ($action === 'get_changed_files') {
  130. require_once(DEDEINC.'/libraries/dedehttpdown.class.php');
  131. checkLogin();
  132. //获取本地更改过的文件
  133. $hashUrl = DEDEBIZCDN.'/release/'.$cfg_version_detail.'.json';
  134. $dhd = new DedeHttpDown();
  135. $dhd->OpenUrl($hashUrl);
  136. $data = $dhd->GetJSON();
  137. if (empty($data)) {
  138. echo json_encode(array(
  139. "code"=>-1,
  140. "msg"=>'获取版本信息失败',
  141. ));
  142. exit();
  143. }
  144. $changedFiles = array();
  145. foreach ($data as $file) {
  146. $realFile = DEDEROOT.str_replace("\\", '/', $file->filename);
  147. if (file_exists($realFile) && md5_file($realFile) !== $file->hash) {
  148. $changedFiles[] = $file;
  149. continue;
  150. }
  151. }
  152. echo json_encode(array(
  153. "code" => 0,
  154. "msg" => "",
  155. "data" => array(
  156. "files" => $changedFiles,
  157. ),
  158. ));
  159. exit;
  160. } else if ($action === 'update_backup') {
  161. require_once(DEDEINC.'/libraries/dedehttpdown.class.php');
  162. checkLogin();
  163. //获取本地更改过的文件
  164. $hashUrl = DEDEBIZCDN.'/release/'.$cfg_version_detail.'.json';
  165. $dhd = new DedeHttpDown();
  166. $dhd->OpenUrl($hashUrl);
  167. $data = $dhd->GetJSON();
  168. if (empty($data)) {
  169. echo json_encode(array(
  170. "code"=>-1,
  171. "msg"=>'获取版本信息失败',
  172. ));
  173. exit;
  174. }
  175. $changedFiles = array();
  176. $enkey = substr(md5(substr($cfg_cookie_encode, 0, 5)), 0, 10);
  177. $backupPath = DEDEDATA."/backupfile_{$enkey}";
  178. RmRecurse($backupPath);
  179. mkdir($backupPath);
  180. foreach ($data as $file) {
  181. $realFile = DEDEROOT.str_replace("\\", '/', $file->filename);
  182. if (file_exists($realFile) && md5_file($realFile) !== $file->hash) {
  183. //备份文件
  184. $dstFile = $backupPath.'/'.str_replace("\\", '/', $file->filename);
  185. @mkdir(dirname($dstFile), 0777, true);
  186. copy($realFile, $dstFile);
  187. }
  188. }
  189. echo json_encode(array(
  190. "code" => 0,
  191. "msg" => "",
  192. "data" => array(
  193. "backupdir" => "data/backupfile_{$enkey}",
  194. ),
  195. ));
  196. exit;
  197. } else if ($action === 'get_update_versions') {
  198. require_once(DEDEINC.'/libraries/dedehttpdown.class.php');
  199. checkLogin();
  200. //获取本地更改过的文件
  201. $offUrl = DEDEBIZURL."/versions?version={$cfg_version_detail}";
  202. $dhd = new DedeHttpDown();
  203. $dhd->OpenUrl($offUrl);
  204. $data = $dhd->GetHtml();
  205. if (empty($data)) {
  206. echo json_encode(array(
  207. "code"=>-1,
  208. "msg"=>'获取版本信息失败',
  209. ));
  210. exit;
  211. }
  212. $arr = json_decode($data);
  213. SetCache('update', 'vers', $arr->result->Versions);
  214. echo $data;
  215. exit;
  216. } else if ($action === 'update') {
  217. require_once(DEDEINC.'/libraries/dedehttpdown.class.php');
  218. $row = GetCache('update', 'vers');
  219. if (count($row) === 0) {
  220. echo json_encode(array(
  221. "code" => -1,
  222. "msg" => "请先获取版本更新记录",
  223. "data" => null,
  224. ));
  225. exit;
  226. }
  227. $enkey = substr(md5(substr($cfg_cookie_encode, 0, 5)), 0, 10);
  228. $backupPath = DEDEDATA."/updatefile_{$enkey}";
  229. @mkdir($backupPath);
  230. foreach ($row as $k => $ver) {
  231. if ($ver->isdownload !== true) {
  232. $filesUrl = DEDEBIZCDN.'/update/'.$ver->ver.'/files.txt';
  233. $dhd = new DedeHttpDown();
  234. $dhd->OpenUrl($filesUrl);
  235. $fileList = $dhd->GetJSON();
  236. $dhd->Close();
  237. $backupVerPath = $backupPath.'/'.$ver->ver;
  238. RmRecurse($backupVerPath);
  239. mkdir($backupVerPath);
  240. foreach ($fileList as $f) {
  241. if (!preg_match("/^\//", $f->filename)) {
  242. //忽略src之外的目录
  243. continue;
  244. }
  245. $fileUrl = DEDEBIZCDN.'/update/'.$ver->ver.'/src'.$f->filename;
  246. $dhd = new DedeHttpDown();
  247. $dhd->OpenUrl($fileUrl);
  248. $fData = $dhd->GetHtml();
  249. $dhd->Close();
  250. $f->filename = preg_replace('/^\/admin/', $curDir, $f->filename);
  251. $realFile = $backupVerPath.$f->filename;
  252. @mkdir(dirname($realFile), 0777, true);
  253. file_put_contents($realFile, $fData);
  254. }
  255. $sqlUrl = DEDEBIZCDN.'/update/'.$ver->ver.'/update.sql';
  256. $dhd = new DedeHttpDown();
  257. $dhd->OpenUrl($sqlUrl);
  258. $fData = $dhd->GetHtml();
  259. $dhd->Close();
  260. $realFile = $backupVerPath.'/update.sql';
  261. file_put_contents($realFile, $fData);
  262. $realFile = $backupVerPath.'/files.txt';
  263. file_put_contents($realFile, json_encode($fileList));
  264. $row[$k]->isdownload = true;
  265. SetCache('update', 'vers', $row);
  266. echo json_encode(array(
  267. "code" => 0,
  268. "msg" => "正在下载{$ver->ver}的版本更新文件",
  269. "data" => array(
  270. "finish" => false,
  271. ),
  272. ));
  273. exit;
  274. }
  275. }
  276. foreach ($row as $k => $ver) {
  277. if ($ver->ispatched !== true) {
  278. $backupVerPath = $backupPath.'/'.$ver->ver;
  279. //执行更新SQL文件
  280. $sql = file_get_contents($backupVerPath.'/update.sql');
  281. if (!empty($sql)) {
  282. $sql = preg_replace('#ENGINE=MyISAM#i', 'TYPE=MyISAM', $sql);
  283. $sql41tmp = 'ENGINE=MyISAM DEFAULT CHARSET='.$cfg_db_language;
  284. $sql = preg_replace('#TYPE=MyISAM#i', $sql41tmp, $sql);
  285. $sqls = explode(";\r\n", $sql);
  286. foreach ($sqls as $sql) {
  287. if (trim($sql) != '') {
  288. $dsql->safeCheck = false;
  289. $dsql->ExecuteNoneQuery(trim($sql));
  290. $dsql->safeCheck = true;
  291. }
  292. }
  293. }
  294. //复制文件
  295. $fileList = json_decode(file_get_contents($backupVerPath.'/files.txt'));
  296. foreach ($fileList as $f) {
  297. if (!preg_match("/^\//", $f->filename)) {
  298. //忽略src之外的目录
  299. continue;
  300. }
  301. $f->filename = preg_replace('/^\/admin/', $curDir, $f->filename);
  302. $srcFile = $backupVerPath.$f->filename;
  303. $dstFile = str_replace(array("\\", "//"), '/', DEDEROOT.$f->filename);
  304. @mkdir(dirname($dstFile), 0777, true);
  305. $rs = @copy($srcFile, $dstFile);
  306. if ($rs) {
  307. unlink($srcFile);
  308. }
  309. }
  310. $row[$k]->ispatched = true;
  311. SetCache('update', 'vers', $row);
  312. RmRecurse($backupVerPath);
  313. echo json_encode(array(
  314. "code" => 0,
  315. "msg" => "正在应用{$ver->ver}的版本补丁文件",
  316. "data" => array(
  317. "finish" => false,
  318. ),
  319. ));
  320. exit;
  321. }
  322. }
  323. echo json_encode(array(
  324. "code" => 0,
  325. "msg" => "",
  326. "data" => array(
  327. "finish" => true,
  328. ),
  329. ));
  330. exit;
  331. } else if($action === 'upload_image'){
  332. checkLogin();
  333. $imgfile_name = $_FILES["file"]['name'];
  334. $activepath = $cfg_image_dir;
  335. $allowedTypes = array("image/pjpeg", "image/jpeg", "image/gif", "image/png", "image/xpng", "image/wbmp", "image/webp");
  336. $uploadedFile = $_FILES['file']['tmp_name'];
  337. $fileType = mime_content_type($uploadedFile);
  338. $imgSize = getimagesize($uploadedFile);
  339. if (!in_array($fileType, $allowedTypes) || !$imgSize) {
  340. echo json_encode(array(
  341. "code" => -1,
  342. "msg" => "仅支持图片格式文件",
  343. "data" => null,
  344. ));
  345. exit;
  346. }
  347. $nowtme = time();
  348. $mdir = MyDate($cfg_addon_savetype, $nowtme);
  349. if (!is_dir($cfg_basedir.$activepath."/$mdir")) {
  350. MkdirAll($cfg_basedir.$activepath."/$mdir", $cfg_dir_purview);
  351. CloseFtp();
  352. }
  353. $cuserLogin = new userLogin();
  354. $iseditor = isset($iseditor)? intval($iseditor) : 0;
  355. $filename_name = $cuserLogin->getUserID().'-'.dd2char(MyDate("ymdHis", $nowtme).mt_rand(100, 999));
  356. $filename = $mdir.'/'.$filename_name;
  357. $fs = explode('.', $imgfile_name);
  358. $filename = $filename.'.'.$fs[count($fs) - 1];
  359. $filename_name = $filename_name.'.'.$fs[count($fs) - 1];
  360. $fullfilename = $cfg_basedir.$activepath."/".$filename;
  361. move_uploaded_file($_FILES["file"]["tmp_name"], $fullfilename) or die(json_encode(array(
  362. "code" => -1,
  363. "msg" => "上传失败",
  364. "data" => null,
  365. )));
  366. $info = '';
  367. $sizes[0] = 0;
  368. $sizes[1] = 0;
  369. $sizes = getimagesize($fullfilename, $info);
  370. $imgwidthValue = $sizes[0];
  371. $imgheightValue = $sizes[1];
  372. $imgsize = filesize($fullfilename);
  373. $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()."'); ";
  374. $dsql->ExecuteNoneQuery($inquery);
  375. $fid = $dsql->GetLastID();
  376. AddMyAddon($fid, $activepath.'/'.$filename);
  377. echo json_encode(array(
  378. "code" => 0,
  379. "msg" => "上传成功",
  380. "data" => $activepath."/".$filename,
  381. ));
  382. }
  383. ?>