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

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