#!/usr/bin/perl

#############################################################
#  LeoBoard ver.5000 / LB5000 / 雷傲超級論壇 ver.5000
#
#  版權所有: 雷傲工$twTzuo室(原藍寶石軟件工$twTzuo室)
#
#  制$twTzuo人  : 山鷹糊 (Shining Hu)
#      花無缺 (Ifairy Han)
#       
#  首頁位址: http://www.CGIer.com/    CGI 編程者之家
#	   http://www.LeoBoard.com/   雷傲論壇支援首頁
#	   http://www.leoBBS.com/   本論壇直通車
#      
#############################################################
BEGIN {
  $LBPATH = '.';
  my $pgm = $0;
  $pgm =~s/\\/\//g;
  $pgm =~s/^.*\/([^\/]+)$/$1/g;
  unless (-e $LBPATH.'/'.$pgm) {
    foreach ($0, $ENV{'SCRIPT_FILENAME'}, $ENV{'PATH_TRANSLATED'}) {
      s!\\!/!g; s/^(.*)\/[^\/]+$/$1/g;
      if (-e $_ . '/' .$pgm) { $LBPATH = $_; last; }
    }
  }
  unshift (@INC, "$LBPATH");
}
use LBCGI;
use Digest::MD5 qw(md5_hex);
$LBCGI::POST_MAX=2000;
$LBCGI::DISABLE_UPLOADS = 1;
$LBCGI::HEADERS_ONCE = 1;
require "data/styles.cgi";
require "data/boardinfo.cgi";
require "lb.lib.pl";

$|++;
$thisprog = "upgrade.cgi";

$query = new LBCGI;
$inmembername = $query->cookie("amembernamecookie");
$inmembername = &cleaninput($inmembername);
$inpassword = $query->cookie("apasswordcookie");
$inmembername =~ s/[\a\f\n\e\0\r\t\`\~\!\@\#\$\%\^\&\*\(\)\+\=\\\{\}\;\'\:\"\,\.\/\<\>\?]//isg;
$inpassword =~ s/[\a\f\n\e\0\r\t\|\@\;\#\{\}\$]//isg;

for ('errorCode', 'errorMsg', 'challengeWord', 'mobile', 'tokenWord') {
  next unless defined $_;
  next if $_ eq 'SEND_MAIL';
  $tp = $query->param($_);
  $tp = &unHTML("$tp");
  ${$_} = $tp;
}

print header(-charset=>big5);

if ($errorCode ne "") {
  if ($errorCode eq "100") {
  	($tzy, $bbsid, $userid) = split(/\t/, &getkey);
  	$tzc = "$challengeWord:$tzy";
    $rzh = md5_hex($tzc);
    if ($mobilephone !~ /^[0-9]+$/) { $mobilephone = "110"; }
    if ((!(-e "${lbdir}rankey/$challengeWord"))||($challengeWord eq "")||(!(-e "${lbdir}mobile/$mobile.cgi"))) {
    	&testerror("認證失敗，請重試");
    } else {
	  if ($rzh eq $tokenWord) {
  	  unlink ("${lbdir}rankey/$challengeWord");

  	  open(FILE,"${lbdir}mobile/$mobile.cgi");
  	  my $name = <FILE>;
	  close(FILE);
	  chomp $name;
	  $name =~ s/ /\_/g;
	  $name =~ tr/A-Z/a-z/;
      $name =~ s/[\a\f\n\e\0\r\t\`\~\!\@\#\$\%\^\&\*\(\)\+\=\\\{\}\;\'\:\"\,\.\/\<\>\?]//isg;
	  
    my $filetoopen = "${lbdir}$memdir/$name.cgi";
    $filetoopen  = "${lbdir}$memdir/old/$name.cgi" if (!(-e $filetoopen));
	if (-e $filetoopen) {
	  &winlock($filetoopen) if ($OS_USED eq "Nt");
	  open(FILE6,"$filetoopen");
	  flock (FILE6, 1) if ($OS_USED eq "Unix");
      my $filedata = <FILE6>;
      close(FILE6);
	  chomp($filedata);
	  (my $membername, my $password, my $membertitle, my $membercode, my $numberofposts, my $emailaddress, my $showemail, my $ipaddress, my $homepage, my $oicqnumber, my $icqnumber ,my $location ,my $interests, my $joineddate, my $lastpostdate, my $signature, my $timedifference, my $privateforums, my $useravatar, my $userflag,my  $userxz, my $usersx, my $personalavatar, my $personalwidth, my $personalheight, my $rating, my $lastgone, my $visitno,my  $addjy,my  $meili, my $mymoney, my $postdel, my $sex, my $education, my $marry, my $work, my $born, my $chatlevel,my  $chattime, my $jhmp,my $useradd3,my $ebankdata,my $onlinetime,my $userquestion,my $useradd6,my $useradd7,my $useradd8, my $mobilephone, my $advuser) = split(/\t/,$filedata);
	  if (($membername ne "")&&($password ne "")) {
	    if (open(FILE6,">$filetoopen")) {
	    flock (FILE6, 2) if ($OS_USED eq "Unix");
	    $lastgone=time;
	    print FILE6 "$membername\t$password\t$membertitle\t$membercode\t$numberofposts\t$emailaddress\t$showemail\t$ipaddress\t$homepage\t$oicqnumber\t$icqnumber\t$location\t$interests\t$joineddate\t$lastpostdate\t$signature\t$timedifference\t$privateforums\t$useravatar\t$userflag\t$userxz\t$usersx\t$personalavatar\t$personalwidth\t$personalheight\t$rating\t$lastgone\t$visitno\t$addjy\t$meili\t$mymoney\t$postdel\t$sex\t$education\t$marry\t$work\t$born\t$chatlevel\t$chattime\t$jhmp\t$useradd3\t$ebankdata\t$onlinetime\t$userquestion\t$useradd6\t$useradd7\t$useradd8\t$mobile\t1\t\n";
	    close(FILE6);
	    }
	  }
	  &winunlock($filetoopen) if ($OS_USED eq "Nt");

	}
    else {	&testerror("對不起，沒有發現此會員名稱！"); }

	  mkdir("${lbdir}mobile/ok", 0777) if (!(-e "${lbdir}mobile/ok"));
	  rename ("${lbdir}mobile/$mobile.cgi", "${lbdir}mobile/ok/$mobile.cgi");
  	   &testerror("恭喜您，您已升級為陽光會員。<BR><BR><a href=javascript:top.window.close()>按此關閉此認證視窗</a>！");
  	  }
  	  else {
  	    &testerror("認證失敗，請重試。");
  	  }
  	}
  	
  }
  elsif ($errorMsg ne "") {
  	&testerror("$errormsg");
  }
  elsif ($errorCode eq "101"){
  	&testerror("註冊失敗，請重試");
  }
  elsif ($errorCode eq "102"){
  	&testerror("此手機在本論壇已經存在，請不要重複註冊!<BR>您可以直接用手機號碼登入論壇！<a href=loginout.cgi>按此登入</a>。");
  }
  else {
  	&testerror("錯$twWu代號未知($errorCode)，請重試");
  }
exit;
}


if ($inmembername eq "") { &testerror("會員名稱不能留空"); }

$tempinmembername =$inmembername;
$tempinmembername =~ s/ //g;
$tempinmembername =~ s/  //g;
if ($inmembername =~ /^訪客/) { &testerror("會員名稱有點問題喲，請不要在會員名稱的開頭中使用訪客字樣。"); }
if ($inmembername =~ /_/)   { &testerror("會員名稱有點問題喲，請不要在會員名稱中使用底線！"); }
if ($inmembername =~ /\t/)  { &testerror("會員名稱有點問題喲，請不要在會員名稱中使用特殊符號！"); }
if (length($inmembername)>12) { &testerror("會員名稱太長了，請不要超過 12 個字（6 個中文字）。"); }
if (length($inmembername)<2)  { &testerror("會員名稱太短了，請不要少於 2 個字（1 個中文字）。"); }
&testerror("對不起，您輸入的會員名稱有問題，請不要在會員名稱刈荺含\@\#\$\%\^\&\*\(\)\+\=\\\{\}\;\'\:\"\,\.\/\<\>\?\[\]這類符號！") if ($inmembername =~ /[\a\f\n\e\0\r\t\`\~\!\@\#\$\%\^\&\*\(\)\+\=\\\{\}\;\'\:\"\,\.\/\<\>\?\[\]]/);

#&getmember("$inmembername");
&getmembernolock("$inmembername");
if ($userregistered eq "no") { &testerror("該會員不存在"); }
&testerror("密碼與會員名稱不符，請重新登入！") if ($inpassword ne $password);

$phone     = $query -> param('phone');
$phone     = &cleaninput("$phone");
$mobilephone   = $phone;

if ($advuser eq 1) { &testerror("您已經是陽光會員了，無需再次申請！請更新頁面！"); }
if ($mobilephone eq "") { &testerror("手機號碼不能留空，請先填入手機號碼後再來升級陽光會員！！"); }
if (length($mobilephone) != 11 ) { &testerror("手機號碼有問題，長度非 11 位！！"); }
if ($mobilephone !~ m/^13/) { &testerror("手機號碼有問題，非 13 開頭！！"); }
if (-e "${lbdir}mobile/ok/$mobilephone.cgi") {

  	  open(FILE,"${lbdir}mobile/ok/$mobilephone.cgi");
  	  my $name = <FILE>;
	  close(FILE);
	  chomp $name;
	  $name =~ s/ /\_/g;
	  $name =~ tr/A-Z/a-z/;
      $name =~ s/[\a\f\n\e\0\r\t\`\~\!\@\#\$\%\^\&\*\(\)\+\=\\\{\}\;\'\:\"\,\.\/\<\>\?]//isg;
	  
    my $filetoopen = "${lbdir}$memdir/$name.cgi";
    $filetoopen  = "${lbdir}$memdir/old/$name.cgi" if (!(-e $filetoopen));
	if (-e $filetoopen) {
	  &winlock($filetoopen) if ($OS_USED eq "Nt");
	  open(FILE6,"$filetoopen");
	  flock (FILE6, 1) if ($OS_USED eq "Unix");
      my $filedata = <FILE6>;
      close(FILE6);
	  chomp($filedata);
	  (my $membername, my $password, my $membertitle, my $membercode, my $numberofposts, my $emailaddress, my $showemail, my $ipaddress, my $homepage, my $oicqnumber, my $icqnumber ,my $location ,my $interests, my $joineddate, my $lastpostdate, my $signature, my $timedifference, my $privateforums, my $useravatar, my $userflag,my  $userxz, my $usersx, my $personalavatar, my $personalwidth, my $personalheight, my $rating, my $lastgone, my $visitno,my  $addjy,my  $meili, my $mymoney, my $postdel, my $sex, my $education, my $marry, my $work, my $born, my $chatlevel,my  $chattime, my $jhmp,my $useradd3,my $ebankdata,my $onlinetime,my $userquestion,my $useradd6,my $useradd7,my $useradd8, my $mobilephone, my $advuser) = split(/\t/,$filedata);
	  if (($membername ne "")&&($password ne "")) {
	    if (open(FILE6,">$filetoopen")) {
	    flock (FILE6, 2) if ($OS_USED eq "Unix");
	    $lastgone=time;
	    print FILE6 "$membername\t$password\t$membertitle\t$membercode\t$numberofposts\t$emailaddress\t$showemail\t$ipaddress\t$homepage\t$oicqnumber\t$icqnumber\t$location\t$interests\t$joineddate\t$lastpostdate\t$signature\t$timedifference\t$privateforums\t$useravatar\t$userflag\t$userxz\t$usersx\t$personalavatar\t$personalwidth\t$personalheight\t$rating\t$lastgone\t$visitno\t$addjy\t$meili\t$mymoney\t$postdel\t$sex\t$education\t$marry\t$work\t$born\t$chatlevel\t$chattime\t$jhmp\t$useradd3\t$ebankdata\t$onlinetime\t$userquestion\t$useradd6\t$useradd7\t$useradd8\t$mobilephone\t1\t\n";
	    close(FILE6);
	    }
	  }
	  &winunlock($filetoopen) if ($OS_USED eq "Nt");
	}
    &testerror("此手機號碼已經在本站註冊過了，已經恢復！！");
}

  	open (FILE, ">${lbdir}mobile/$mobilephone.cgi");
  	print FILE "$inmembername\n";
  	close(FILE);

  	$tzc = sprintf ("%08d",&myrand(100000000));
  	$tzc = time . "$tzc";
  	mkdir ("${lbdir}rankey", 0777) if (!(-e "${lbdir}rankey"));
  	open (FILE, ">${lbdir}rankey/$tzc");
  	close(FILE);
  ($tzy, $bbsid, $userid) = split(/\t/, &getkey);
  $myurl = "$boardurl/";
  $sex = "n" if ($sex eq "no");
  $sex=~ tr/a-z/A-Z/;

print qq~
<html> 
<head> 
<script> 
function abc_js() { 
  document.test_form.submit(); 
} 
</script> 
</head> 
<body onLoad="abc_js();"> 
個人訊息已經通過驗証，現在正在進行升級步驟，不要關閉瀏覽器，請耐心等待 ....
<form action="http://bbs.ray5198.com/user_upgrade.jsp" method="post" name="test_form"> 
  <input type="hidden" name="challengeWord" value="$tzc">
  <input type="hidden" name="dirPage" value="$thisprog">

  <input type="hidden" name="forumId" value="$bbsid">
  <input type="hidden" name="forumUrl" value="$myurl">

  <input type="hidden" name="username" value="$inmembername">
  <input type="hidden" name="forumPwd" value="$password">
  <input type="hidden" name="mobile" value="$mobilephone">
  <input type="hidden" name="sex" value="$sex">
  <input type="hidden" name="qq" value="$oicqnumber">
  <input type="hidden" name="email" value="$emailaddress">
</form> 

</body> 
</html> 
~;

exit;



sub testerror {
&rayhtml(1);
my $errormsg = shift;
print qq~
<html>
<head> 
<title>$boardname</title>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<!--end Java-->
<!--css info(editable)-->
<style>
A:visited{TEXT-DECORATION: none}
A:active{TEXT-DECORATION: none}
A:hover{TEXT-DECORATION: underline overline}
A:link{text-decoration: none;}
.t{LINE-HEIGHT: 1.4}
BODY{FONT-FAMILY: 新細明體; FONT-SIZE: 9pt;
SCROLLBAR-HIGHLIGHT-COLOR: buttonface;
SCROLLBAR-SHADOW-COLOR: buttonface;
SCROLLBAR-3DLIGHT-COLOR: buttonhighlight;
SCROLLBAR-TRACK-COLOR: #eeeeee;
SCROLLBAR-DARKSHADOW-COLOR: buttonshadow}
TD,DIV,form ,OPTION,P,TD,BR{FONT-FAMILY: 新細明體; FONT-SIZE: 9pt} 
INPUT{BORDER-TOP-WIDTH: 1px; PADDING-RIGHT: 1px; PADDING-LEFT: 1px; BORDER-LEFT-WIDTH: 1px; FONT-SIZE: 9pt; BORDER-LEFT-COLOR: #cccccc; BORDER-BOTTOM-WIDTH: 1px; BORDER-BOTTOM-COLOR: #cccccc; PADDING-BOTTOM: 1px; BORDER-TOP-COLOR: #cccccc; PADDING-TOP: 1px; HEIGHT: 18px; BORDER-RIGHT-WIDTH: 1px; BORDER-RIGHT-COLOR: #cccccc}
textarea, select {border-width: 1; border-color: #000000; background-color: #efefef; font-family: 新細明體; font-size: 9pt; font-style: bold;}
--> 
</style> 
<!--end css info-->
</head>
<body $lbbody>
<table width=100% align=center cellspacing=0 cellpadding=1  border=0 bgcolor=$titleborder height="100%"><tr><td>
<table width=100% cellspacing=0 cellpadding=4 border=0 height="100%">
<tr><td align="center" bgcolor=$menubackground><font color=$menufontcolor><B>$errormsg</B></font><BR><BR>$output</td></tr></table></td></tr></table>
</body>
</html>
~;
exit;
}
