#!/usr/bin/perl
#CCB======================China City BBS  SYSTEM=========================CCB#
#       精彩奇訊系統源碼安裝程序(China City BBS)                            #
#       程序編著：麻辣  2011年5月 Version 201105                            #
#\\\---------------------------------------------------------------------\\\#
#       歡迎愛好者免費使用和修改,商業用途請聯繫原作者授權                 #
#\\\---------------------------------------------------------------------\\\#
#       歡迎這裡交流和提問 http://www.sun126.com 多提寶貴意見               #
#       版權所有：《精彩奇訊》工作室                                        #
#          sunluck_office@sina.com                                          #
#CCB======================China City BBS  SYSTEM=========================CCB#

#\\\-----------------------後台系統級管理主引導 s_index.pl----------------\\\#
BEGIN {
	$SET{root_path} = ($0 =~ /^(.+)[\/\\]/)[0] || '.';
	$SET{root_path} =~ s/\\/\//g;
	
	unshift(@INC, "$SET{root_path}");

	$SET{charset} = '<meta http-equiv=Content-Type content=text/html; charset=utf-8>';

	if ($SET{charset}=~ /gbk/){print "Content-type: text/html; charset=gbk\n\n";}
	elsif ($SET{charset}=~ /utf-8/){print "Content-type: text/html; charset=utf\-8\n\n\xef\xbb\xbf";}
	elsif ($SET{charset}=~ /big5/){print "Content-type: text/html; charset=big5\n\n";}
	else {print "Content-type: text/html\n\n";}

}

# -------------------------------------------------------加載模塊和基本配置文件
	use CGI::Carp qw(fatalsToBrowser);

# ------------------------------------------------------ 獲取用戶表單和Cookie信息
   &GET_REQUEST(); 
   &GET_COOKIES();	

&Print_control_canel_header;


$Split_TAG = '<--CCB-'.'BLABK-BOX-->';

if ($_REQUEST{finish} == 1){print '網站目錄恢復已完成!';}

if ($_REQUEST{deltar}) {unlink "$SET{root_path}/$_REQUEST{deltar}";}

unless($_REQUEST{Toopen}){
	
	@files = &Readdir_to_array("$SET{root_path}");
	 
	 
	  foreach $FileName (@files){
    	next if ($FileName eq '.');
    	next if ($FileName eq '..');

my $fileNameHeader = substr($FileName,0,2);

next if($fileNameHeader ne '!!');

$i++;
	
	if ($i==1) {$check="checked";}else{$check="";}

		$n="";
		$view="文件包";

	my($left,$data)=split(/-/,$FileName);

if($left eq '!!CCB'){	$view = 'CCB 論壇程序';	}
	else{$view = '文件包';}

my($left,$data)=split(/-/,$FileName);

$y=substr($data,0,4);

$m=substr($data,4,2);

$d=substr($data,6,2);

$h=substr($data,8,2);

$mi=substr($data,10,2);	

	my  $len =(-s "$SET{root_path}/$FileName")/1024/1024;
		$len = sprintf("%.2f",$len);#取小數點2位數
		
		&Readfile_sys_limit("$SET{root_path}/$FileName",100,\$head);
    $VerCharset = substr($head,0,index($head,$Split_TAG));
		my ($Ver,$Charset,$lang,$FileBytes) = split(/\;/,$VerCharset);
		(undef,$Charset)=split(/=/,$Charset);
		$Charset =~ s/\\//g;
		(undef,$lang)=split(/=/,$lang);
		$FileList .= "<tr><td>$Ver $Charset $lang
		<br>發佈時間:$y-$m-$d $h:$mi</td>
		<td>$view $FileName $n($len 兆)</td>
		<td><INPUT type=radio name=Toopen value='$FileName'> 選擇</td>
		<TD><a href=$SET{script_name}?rq=s_index&menu=s_update_index&class=&deltar=$FileName>刪除</a></TD></tr>";

	
}
	


print qq(<table width=700 cellSpacing=0 cellPadding=5 style="BORDER-COLLAPSE: collapse" border=1 borderColor=#215DC6 >
<FORM  METHOD=get>

$FileList

<tr><td colspan=3 align=right><INPUT type=submit name=submit value=' 一 鍵 快 速 安 裝 '></td></tr>
</table>
</form>
);
 
 
  $ccb_html=&Html_end;print"$ccb_html";
 exit;
}   

print '<pre>';


my $file;
my $Open_Path = $SET{root_path};

unless(-e "$SET{root_path}/$_REQUEST{Toopen}"){stop_view("壓縮包不存在!");}

&Readfile_all_contents("$SET{root_path}/$_REQUEST{Toopen}",\$file,1);

@file = split(/$Split_TAG/,$file);
shift @file;

#-----------------------------------------------------論壇文本數據
my $path;

foreach (@file) {
	
	@line = split(/\n/,$_);
	
	$path =shift @line;
	#print $path;
	
	#-------------------------------------	
	@dir=split(/\//,$path);#識別目錄
	
	#print_array(@dir);
	
	my $old_path = $Open_Path;
			
	pop @dir;#---------------刪除最後一個元素,文件名或為.空表示空目錄
		
	foreach(@dir) {
		
		$old_path .= "/$_";
	unless(-d"$old_path") {
		mkdir( "$old_path", 0777);
   chmod( 0777, "$old_path");
		
		print " 正在創建 $old_path ..\n";
		}
		#else{print " <font color=red>跳過 $old_path ..</font>\n";};
	
	}
	
#---------------------------------------
	
		my $unfile;
	
	if ($#line > 0) {
	
	 $unfile=join("\n",@line); 
}
else {$unfile=$line[0];}

my $END =chop $unfile;
   $r =ord $END;
	
	unless ($r ==13) {$unfile .= $END; }#---------------判斷是否為\r


next if ($unfile eq '');

my $bytes_cmp = 0;#大小比較
my $len_new = length($unfile);
my $len_old = (-s"$Open_Path/$path");
if ($len_new == $len_old ){$bytes_cmp = 1};#判斷文件大小是否相同

#-------------------------------------------
if ($_REQUEST{overite} == 0){
	if($len_old > 0){print "存在同名文件$Open_Path/$path,跳過..\n"; next;}
}
#-------------------------------------------
if ($_REQUEST{overite} == 1){
	if ($len_new == $len_old ){print "存在同名同大小文件$Open_Path/$path,\n跳過.."; next;}
}
#-------------------------------------------
if ($_REQUEST{overite} == 2){
	if ($len_new == $len_old ){print "存在同名同大小文件強制覆蓋";}
}

		open FILE,">$Open_Path/$path";
 
 		binmode FILE;
 		
 		print FILE $unfile;
 		
		close(FILE);
	
	 chmod( "$Open_Path/$path", 0777) || chmod( "$Open_Path/$path", 0755) || chmod( "$Open_Path/$path", 0666);;
	 
	 if ($path =~ /ccb\/index\.pl/){chmod( "$Open_Path/$path", 0755);}
   if ($path =~ /ccb\/index\.cgi/){chmod( "$Open_Path/$path", 0755);}
	
		
}


print "\n<pre>";

my($left,$data)=split(/-/,$_REQUEST{Toopen});

if($left eq '!!CCB'){
	
($path)=split(/\/index\.htm/,$path);	

	
	$finish_view = qq(恭喜!安裝已完成!請不要關閉窗口,稍後系統自動啟動<meta HTTP-EQUIV=REFRESH CONTENT=1;URL="$path/ccb/index.pl">);
	}
	else{
	
		$finish_view = qq(恭喜!網站目目恢復已完成!請不要關閉窗口,系統自動返回<meta HTTP-EQUIV=REFRESH CONTENT=1;URL="install.pl?finish=1">);
}


print $finish_view;
	
print &Html_end;

exit;
 
#################################程序結束,下面是相關函數################################################

    
#-----------------------------------調試相關函數----------------------------#
#\\\---------------------------------------------------------------------\\\#
# 函數 Print_hash,用途 :   打印哈希數組(調試用)                             #
# 示例:Print_hash(%_REQUEST);                                               #
#\\\---------------------------------------------------------------------\\\#
sub print_hash {
	my %HASH = @_;
	
		print "<p align=left><table><tr><td><pre>HASH:(\n";
	
	foreach(sort keys %HASH) {
		print '      ['.$_.'] => '.$HASH{$_}." \n";	
	}

print ")\n</pre></td></tr></table></p>";

}          
#\\\---------------------------------------------------------------------\\\#
# 函數 Print_array,用途 :   打印數組結構(調試用)                            #
# 示例:Print_ARRAY(@array);                                                 #
#\\\---------------------------------------------------------------------\\\#    
sub print_array {
	
			print "<p align=left><table><tr><td><pre>Array:(\n";
			
			my $i = 0;
	foreach(@_) {$i++;
		
		print ' ['.$i.'] => '.$_." \n";	
	
	}

print ")\n</pre></td></tr></table></p>";
	
}     

#\\\---------------------------------------------------------------------\\\#
# 函數 Print_var,用途 :   打印變量內容(調試用)                              #
# 示例:Print_avr($var);                                                     #
#\\\---------------------------------------------------------------------\\\#
sub print_var {

	print "<p align=left><table><tr><td><pre>Var:(\n";

	print 'var = [' . $_[0] .']';

	print " \n)</pre></td></tr></table></p>";

}
    
#\\\---------------------------------------------------------------------\\\#  
# 函數用途      :   讀取整個文本文件                                        #  
# 所需元素      :   $_[0]$_[1]文件路徑                                      #  
# 返回結果      :   變量形式文件內容                                        #
#\\\---------------------------------------------------------------------\\\# 
sub Readfile_all_contents {
   undef $/;
   my $CCBREAD=$_[1];
   open FILE, $_[0];
   
   binmode FILE;
   
   $$CCBREAD = <FILE>;
   close FILE;
   $/="\n";
}
#\\\---------------------------------------------------------------------\\\#
# 函數用途      :   讀取目錄下所有的文件名                                  #
# 所需元素      :   $[0]文件路徑                                            #
# 返回結果      :   數組                                                    #
#\\\---------------------------------------------------------------------\\\#
sub Readdir_to_array {
    opendir( FILESS, "$_[0]" )or return 0;
   my @lines = readdir(FILESS);
    closedir(FILESS);
  
return (@lines);
}

#\\\---------------------------------------------------------------------\\\#
# 函數用途      :   讀取文件系統級                                          #
# 所需元素      :   $[0]文件路徑,限制讀取的字節數                               #
# 返回結果      :   變量                                                    #
#\\\---------------------------------------------------------------------\\\#
sub Readfile_sys_limit {
	$RD++;
        my $CCBREAD=$_[2];
        sysopen FILES, "$_[0]", O_RDONLY;
        sysread( FILES, $$CCBREAD, $_[1] );
        close(FILES);

}
#\\\---------------------------------------------------------------------\\\#
# 函數用途      :   獲取所有Cookies值,同時為漢字解碼                        #
#\\\---------------------------------------------------------------------\\\#
sub GET_COOKIES {
	foreach ( split( /; /, $ENV{'HTTP_COOKIE'} ) ) {
		( $cookie, $value ) = split(/=/);
		$_COOKIE{$cookie} = $value;
	}
	
	if($ENV{'QUERY_STRING'}){my $get_url = "?$ENV{'QUERY_STRING'}";};
	#print_var($ENV{HTTP_REFERER});
	unless($_COOKIE{admname}){
	if ("$SET{url_path}$SET{script_name}$get_url" eq $ENV{HTTP_REFERER}) {
		if(($SET{now_time_id}-$_COOKIE{lasttime}) < $S_cfg{Reload_time}){
			&stop_view("系統提示，連續刷新同一頁面不能小於$S_cfg{Reload_time}秒");
			exit;
		}
	}
	
}
}
#\\\---------------------------------------------------------------------\\\#
# 函數用途      :   獲取表單信息                                            #
#\\\---------------------------------------------------------------------\\\#
sub GET_REQUEST {
	
		
	
		$_FORM_BUFFER = $ENV{'QUERY_STRING'};
	        $_FORM_BUFFER =~ s/\.\.\///g;#--------防黑客,過濾相對路徑請求
	
	@pairs = split( /&/, $_FORM_BUFFER );
	
	foreach $pair (@pairs) {
		my ( $name, $value ) = split( /=/, $pair );
		$value =~ tr/+/ /;
		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
			
		if ( $_REQUEST{$name} ) { 
			$_REQUEST{$name} = $_REQUEST{$name} . "," . $value; 
			
			}
		else { $_REQUEST{$name} = $value;$_GET{$name} = $value;  }
	
	}
}
########### css控制管理中心html頭
sub Print_control_canel_header {
		
    print &Html_Meta_css;

    $bgcolor_admin = "#d7def8";
    
    print <<HTML;
<center>
	<body topmargin=0  text=#215DC6  bgcolor=$bgcolor_admin>
    <table border=0 width=100% cellSpacing=0 cellPadding=0>
  <tr >
   <td bgcolor="#1352C0" valign=middle align=left height=20>&nbsp;&nbsp;&nbsp;<font color=ffffff><b>中國 精彩奇訊 系 統 -- CCB System Version $ver_1 </b></font>
    </td>
    <td bgcolor=1856c7 valign=middle align=right>&nbsp;&nbsp;&nbsp;
   </td>
  </tr>
 </table>
 
<table border=0 width=100% cellSpacing=0 cellPadding=6 >
<tr><td width=100%  align=center><br>&nbsp;<b><font color=004080>
	<font color="#215DC6">$S_cfg{CCB_Name}&nbsp;CCB 論 壇 一 鍵 安 裝 程 序 &nbsp;&nbsp;</font>
</td></tr></table>
HTML

}
sub Html_Meta_css {
	
return qq(

<HTML>
	<HEAD>
	$SET{charset}
	<meta name=keywords content="$S_cfg{CCB_Name},$S_cfg{Website_Name},$C_cfg{ClassName},bbs,CCB,CGI論壇,社區,文本,數據庫,源碼,源代碼,服務器,perl,cgi,麻辣">
	<meta name="description" content="$S_cfg{CCB_Name},$S_cfg{Website_Name},$C_cfg{ClassName},bbs,CCB,論壇,社區,文章管理,下載中心,系統,文本,數據庫,動態網頁,原碼,原代碼,服務器,perl,cgi,麻辣">
	<meta name="Robots" contect= "all">
	<STYLE type=text/css>
	A:link {
		FONT-SIZE: 12px; COLOR: blue; TEXT-DECORATION: none
	}
	A:visited {
		COLOR: #800080; TEXT-DECORATION: none
	}
	A:active {
		COLOR: #004080; TEXT-DECORATION: none
	}
	A:hover {
		COLOR: #ff0000; TEXT-DECORATION: underline
	}
	BODY {
		FONT-SIZE: 12px; FONT-FAMILY: Tahoma,Verdana
	}
	TABLE {
		FONT-SIZE: 12px; FONT-FAMILY: Tahoma,Verdana
	}
	TR {
		FONT-SIZE: 12px; FONT-FAMILY: Tahoma,Verdana
	}
	TD {
		FONT-SIZE: 12px; FONT-FAMILY: Tahoma,Verdana
	}
	OPTION {
		FONT-SIZE: 12px; FONT-FAMILY: Tahoma,Verdana
	}
	P {
		FONT-SIZE: 12px; FONT-FAMILY: Tahoma,Verdana
	}
	TEXTAREA {
		FONT-SIZE: 12px; FONT-FAMILY: Tahoma,Verdana
	}
	INPUT {
		FONT-SIZE: 12px; FONT-FAMILY: Tahoma,Verdana
	}
	SELECT {
		FONT-SIZE: 12px; FONT-FAMILY: Tahoma,Verdana
	}
	category {
		FONT-SIZE: 12px; FONT-FAMILY: Tahoma,Verdana
	}
	BODY {
		SCROLLBAR-HIGHLIGHT-COLOR: gainsboro; SCROLLBAR-SHADOW-COLOR: gainsboro; SCROLLBAR-3DLIGHT-COLOR: gainsboro; SCROLLBAR-TRACK-COLOR: beige; SCROLLBAR-DARKSHADOW-COLOR: tan
	}
	INPUT {
		BORDER-TOP-WIDTH: 1px; PADDING-RIGHT: 1px; PADDING-LEFT: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-LEFT-COLOR: #cccccc; BORDER-BOTTOM-WIDTH: 1px; BORDER-BOTTOM-COLOR: #cccccc; PADDING-BOTTOM: 1px; BORDER-TOP-COLOR: #cccccc; PADDING-TOP: 1px; HEIGHT: 20px; BORDER-RIGHT-WIDTH: 1px; BORDER-RIGHT-COLOR: #cccccc
	}
	TEXTAREA {
		BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-LEFT-COLOR: #000000; BORDER-BOTTOM-WIDTH: 1px; BORDER-BOTTOM-COLOR: #000000; BORDER-TOP-COLOR: #000000; BORDER-RIGHT-WIDTH: 1px; BORDER-RIGHT-COLOR: #000000
	}
	SELECT {
		BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-LEFT-COLOR: #000000; BORDER-BOTTOM-WIDTH: 1px; BORDER-BOTTOM-COLOR: #000000; BORDER-TOP-COLOR: #000000; BORDER-RIGHT-WIDTH: 1px; BORDER-RIGHT-COLOR: #000000
	}
	input[type=checkbox]
	{
		vertical-align: middle;
		padding: 2px;
	}
	</STYLE>
	<script>
	function CheckAll(form){for (var i=0;i<form.elements.length;i++){var e = form.elements[i];e.checked = true;}}
	function FanAll(form){for (var i=0;i<form.elements.length;i++){var e = form.elements[i];if (e.checked == true){ e.checked = false; }else { e.checked = true;}}}
	function Sunluck(para_URL){var URL=new String(para_URL);window.open(URL,'','top=0,left=5,toolbar=yes,location=no,scrollbars=yes,resizable=yes')}
	function Sun(para_URL){var URL=new String(para_URL);window.open(URL,'','top=0,left=5,menubar=yes,toolbar=yes,location=yes,scrollbars=yes,resizable=yes')}
	function New(para_URL){var URL=new String(para_URL);window.open(URL,'','resizable,width=800,scrollbars=yes')}
	function hao(para_URL){var URL=new String(para_URL);window.open(URL,'friend','top=50,left=5,width=450,height=270,scrollbars=no')}
	document.cookie="lasttime="+"$SET{now_time_id}";
	document.cookie="Last_url="+"$Last_url";
	</script>
	<link REL="SHORTCUT ICON" href=$S_cfg{noncgi_url}/ccb.ico>
	</HEAD>

);	
}	
######## 輸出中斷信息.
sub stop_view {
   
		print <<HTML;

<table border=0 width=100% cellSpacing=0 cellPadding=6>
<tr><td width=100%  align=center><br><b><font color=004080>
	$_[0]
</td></tr></table>

HTML

}

##################################
sub Html_end {
   
#####################################################33
  
    my $Html_end=qq(
<center>
<table width=100% border=0>
<tr><td><p algin=left>$_QUERY_STRING</p></td></tr>
<tr align=center><td  align=center>

<br><hr size=1><br>$ver </td></tr></table>$timejs
<table width=100% border=0><tr align=center><td  align=center>Powered by CCB Version 201105 c 2007-2015 All Rights Reserved </td></tr></table>
$view_times 
$av<br><br>
</BODY>
</html><style>
);
return ($Html_end);
}
	
#stop_view($_FORM_BUFFER);
1;