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

301 lines
12KB

  1. <?php
  2. /**
  3. * 远程发布
  4. *
  5. * @version $Id: makeremote_all.php 1 11:17 2010年7月19日Z tianya $
  6. * @package DedeBIZ.Administrator
  7. * @copyright Copyright (c) 2020, DedeBIZ.COM
  8. * @license https://www.dedebiz.com/license
  9. * @link https://www.dedebiz.com
  10. */
  11. require_once(dirname(__FILE__) . "/config.php");
  12. CheckPurview('sys_MakeHtml');
  13. require_once(DEDEINC . "/arc.partview.class.php");
  14. if ($cfg_remote_site == 'N') exit('Error:$cfg_remote_site is OFF,Check it!');
  15. //生成必须同步服务器的列表
  16. if (file_exists(DEDEDATA . '/config.file.inc.php')) {
  17. require_once(DEDEDATA . '/config.file.inc.php');
  18. }
  19. if (empty($dopost)) $dopost = '';
  20. $step = !isset($step) ? 1 : $step;
  21. $sta = !isset($sta) ? 0 : $sta;
  22. $totalnum = !isset($totalnum) ? 0 : $totalnum;
  23. $maketype = empty($maketype) ? '' : $maketype;
  24. //获取同步状态
  25. function GetState($val)
  26. {
  27. $color = ($val == 0) ? 'red' : 'green';
  28. $signer = ($val == 0) ? '未同步' : '已同步';
  29. return '<font color="' . $color . '">' . $signer . '</font>';
  30. }
  31. //生成远程目录数组
  32. function addDir($filedir = '', $description = '', $dfserv = 0, $state = 0, $issystem = 0)
  33. {
  34. return array(
  35. 'filedir' => $filedir,
  36. 'description' => $description,
  37. 'dfserv' => $dfserv,
  38. 'state' => $state,
  39. 'issystem' => $issystem
  40. );
  41. }
  42. //将配置数组生成配置内容
  43. function makeConfig($dirarray = array())
  44. {
  45. $config_str = '';
  46. foreach ($dirarray as $k => $val) {
  47. $config_str .= '$remotefile[' . $k . '] = array(' . "\n";
  48. $config_str .= ' \'filedir\'=>\'' . $val['filedir'] . "',\n";
  49. $config_str .= ' \'description\'=>\'' . $val['description'] . "',\n";
  50. $config_str .= ' \'dfserv\'=>' . $val['dfserv'] . ",\n";
  51. $config_str .= ' \'state\'=>' . $val['state'] . ",\n";
  52. $config_str .= ' \'issystem\'=>' . $val['issystem'] . "\n";
  53. $config_str .= ");\n";
  54. }
  55. return ($config_str == '') ? '' : $config_str;
  56. }
  57. //递归获取目录 by:tianya
  58. function getDirs($directory, $exempt = array('.', '..', '.ds_store', '.svn'), &$files = array())
  59. {
  60. //没有则创建
  61. if (is_dir($directory) && !opendir($directory)) mkdir($directory, 0777, TRUE);
  62. $handle = opendir($directory);
  63. while (false !== ($resource = readdir($handle))) {
  64. if (!in_array(strtolower($resource), $exempt)) {
  65. //排除目录
  66. if (is_dir($directory . $resource . '/')) {
  67. array_merge(
  68. $files,
  69. getDirs($directory . $resource . '/', $exempt, $files)
  70. );
  71. } else {
  72. //if(!is_file($directory.'/'.$resource))
  73. //{
  74. $files[] = $directory . '/' . $resource;
  75. //}
  76. }
  77. }
  78. }
  79. closedir($handle);
  80. return $files;
  81. }
  82. function updateConfig($dirarray = array())
  83. {
  84. //将其转换为配置写入文件
  85. $configfile = DEDEDATA . '/config.file.inc.php';
  86. $old_config = @file_get_contents($configfile);
  87. $config_str = makeConfig($dirarray);
  88. //替换配置项
  89. $new_config = preg_replace("/#<s_config>(.*)#<e_config>/s", "#<s_config>\n\n{$config_str}#<e_config>", $old_config);
  90. file_put_contents($configfile, $new_config);
  91. }
  92. if ($dopost == '') {
  93. }
  94. /*
  95. function Updateremote()
  96. */ else if ($dopost == 'updateremote') {
  97. $dirbox = array(); // 定义一个目录容器,确保目录的唯一性
  98. //获取所有HTML生成文件夹列表
  99. $query = "SELECT id,typedir,ispart FROM #@__arctype WHERE ispart <> '3'";
  100. $dsql->SetQuery($query);
  101. $dsql->Execute('al');
  102. $dirarray = array();
  103. $i = 0;
  104. while ($row = $dsql->GetArray("al")) {
  105. $darray = explode('/', preg_replace('/{cmspath}/', '', $row['typedir']));
  106. //仅获取顶级目录作为文件同步更新目录
  107. if (!in_array($darray[1], $dirbox)) {
  108. $dirarray[$i] = addDir('/' . $darray[1], '文档HTML默认保存路', 0, 0, 1);
  109. $dirbox[] = $darray[1];
  110. $i++;
  111. }
  112. }
  113. //系统附件存放目录
  114. $dirarray[$i++] = addDir($cfg_medias_dir, '图片/上传文件默认路径', 0, 0, 1);
  115. //专题目录
  116. $dirarray[$i++] = addDir('/special', '专题目录', 0, 0, 1);
  117. //data/js目录
  118. $dirarray[$i++] = addDir('/data/js', '生成js目录', 0, 0, 1);
  119. //$remotefile = array();
  120. //把非系统目录的内容提取出来
  121. foreach ($remotefile as $key => $value) {
  122. //处理用户自定义配置
  123. if ($value['issystem'] == 0) {
  124. $dirarray[$i++] = addDir(
  125. $value['filedir'],
  126. $value['description'],
  127. $value['dfserv'],
  128. $value['state'],
  129. $value['issystem']
  130. );
  131. }
  132. }
  133. updateConfig($dirarray);
  134. ShowMsg("成功更新同步目录,请重新对目录进行同步操作!", "makeremote_all.php");
  135. exit;
  136. }
  137. /*
  138. function Make()&MakeAll()
  139. */ else if ($dopost == 'make') {
  140. if ($step == 1) {
  141. if ($maketype == 'makeall') {
  142. //如果更新所有,则需要重新组合$Iterm
  143. foreach ($remotefile as $key => $val) {
  144. $Iterm[] = $val['filedir'];
  145. }
  146. } else {
  147. //初始化配置
  148. $Iterm = !isset($Iterm) ? array() : $Iterm;
  149. }
  150. $serviterm = !isset($serviterm) ? array() : $serviterm;
  151. $cacheMakeFile = DEDEDATA . '/cache/filelist.inc.php';
  152. $dirlist = $alllist = $updir = array();
  153. $dirindex = 0; //目录统一索引
  154. //采用比较人性化的更新方式进行提示更新
  155. //初始化本地文件夹底层的子集目录
  156. if (count($Iterm) > 0) {
  157. //获取远程文件(夹)列表
  158. foreach ($Iterm as $key => $val) {
  159. $config = $serviterm[$key];
  160. if (is_array($dirlist = getDirs(DEDEROOT . $val))) {
  161. foreach ($dirlist as $k => $v) {
  162. $alllist[] = $v . '|' . $config;
  163. if (!in_array($val, array_values($updir))) $updir[] = $val;
  164. }
  165. }
  166. }
  167. //遍历文件夹列表,如果存在子集文件夹大于3的则需要进行细分
  168. //将列表写入缓存
  169. $cachestr = "<?php \n global \$dirlist,\$upremote;\n \$dirlist=array();\n";
  170. foreach ($alllist as $key => $val) {
  171. list($filename, $fileconfig) = explode('|', $val);
  172. if (is_dir($filename)) {
  173. $deepDir = getDirs($filename);
  174. $dd = 0;
  175. //先遍历一遍确定子集文件夹数目
  176. foreach ($deepDir as $k => $v) {
  177. if (is_dir($v)) $dd++;
  178. }
  179. if ($dd > 3) {
  180. //如果自己文件夹数目大于3则
  181. foreach ($deepDir as $k => $v) {
  182. $v .= '|' . $fileconfig;
  183. $cachestr .= " \$dirlist['$dirindex']='$v';\n";
  184. $dirindex++;
  185. }
  186. } else {
  187. $cachestr .= " \$dirlist['$dirindex']='$val';\n";
  188. $dirindex++;
  189. }
  190. }
  191. }
  192. foreach ($updir as $key => $val) {
  193. $cachestr .= " \$upremote['$key']='$val';\n";
  194. }
  195. $cachestr .= "?>";
  196. file_put_contents($cacheMakeFile, $cachestr);
  197. $tnum = count($alllist);
  198. ShowMsg("成功获取远程列表,下面进行文件远程发布!", "makeremote_all.php?dopost=make&step=2&sta=1&totalnum=$tnum");
  199. exit;
  200. } else {
  201. echo '您没有选择,请先选择再点击更新!';
  202. }
  203. exit;
  204. } elseif ($step == 2) {
  205. if (file_exists(DEDEDATA . '/cache/filelist.inc.php')) {
  206. require_once(DEDEDATA . '/cache/filelist.inc.php');
  207. }
  208. if (is_array($dirlist)) {
  209. if ($sta > 0 && $sta < $totalnum) {
  210. list($dirname, $ftpconfig) = explode('|', $dirlist[$sta - 1]);
  211. list($servurl, $servuser, $servpwd) = explode(',', $ftpconfig);
  212. $config = array(
  213. 'hostname' => $servurl, 'username' => $servuser,
  214. 'password' => $servpwd, 'debug' => 'TRUE'
  215. );
  216. if ($ftp->connect($config)) {
  217. //var_dump(is_dir($dirname));exit;
  218. if (is_dir($dirname)) {
  219. //如果是文件目录
  220. $remotedir = str_replace(DEDEROOT, '', $dirname) . '/';
  221. $localdir = '..' . $remotedir . '/';
  222. $ftp->rmkdir($remotedir);
  223. if ($ftp->mirror($localdir, $remotedir)) {
  224. $sta++;
  225. ShowMsg("成功同步文件夹$remotedir,进入下一个任务", "makeremote_all.php?dopost=make&step=2&sta={$sta}&totalnum=$totalnum");
  226. exit;
  227. }
  228. } else {
  229. $remotefile = str_replace(DEDEROOT, '', $dirname);
  230. $localfile = '..' . $remotefile;
  231. //创建远程文件夹
  232. $remotedir = preg_replace('/[^\/]*\.(\w){0,}/', '', $remotefile);
  233. //如果是文件则需要智能处理
  234. $remotebox = array();
  235. $ftp->rmkdir($remotedir);
  236. foreach ($dirlist as $key => $val) {
  237. list($filename, $fileconfig) = explode('|', $val);
  238. if (preg_replace('/[^\/]*\.(\w){0,}/', '', str_replace(DEDEROOT, '', $filename)) == $remotedir) {
  239. //如果这些文件都在同一目录,则统计这些记录的id项目
  240. $remotebox[] = $key;
  241. }
  242. }
  243. //print_r($remotebox);
  244. //if(count($remotebox) > 1 && count($remotebox) < 20)
  245. if (count($remotebox) > 1) {
  246. //如果大于1,则说明有多条记录在同一文件夹内
  247. $localdir = '..' . $remotedir;
  248. if ($ftp->mirror($localdir, $remotedir)) {
  249. $sta = end($remotebox) + 1;
  250. ShowMsg("成功同步文件夹$remotedir,进入下一个任务", "makeremote_all.php?dopost=make&step=2&sta={$sta}&totalnum=$totalnum");
  251. exit;
  252. }
  253. } else {
  254. if ($ftp->upload($localfile, $remotefile)) {
  255. $sta++;
  256. ShowMsg("成功同步文件$remotefile,进入下一个任务", "makeremote_all.php?dopost=make&step=2&sta={$sta}&totalnum=$totalnum");
  257. exit;
  258. }
  259. }
  260. }
  261. }
  262. } else {
  263. //否则成功更新完毕
  264. foreach ($remotefile as $key => $val) {
  265. if (in_array($val['filedir'], array_values($upremote))) {
  266. $remotefile[$key]['state'] = 1;
  267. }
  268. }
  269. updateConfig($remotefile);
  270. @unlink(DEDEDATA . '/cache/filelist.inc.php');
  271. echo '全部同步完毕!';
  272. exit;
  273. }
  274. } else {
  275. exit('Error:None remote cache file exist!');
  276. }
  277. exit;
  278. }
  279. }
  280. include DedeInclude('templets/makeremote_all.htm');