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

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