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

331 lines
12KB

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