#####################################################
#  LEO SuperCool BBS / LeoBBS X / 雷傲極酷超級論壇  #
#####################################################
# 基於山鷹(糊)、花無缺製作的 LB5000 XP 2.30 免費版  #
#   新版程式製作 & 版權所有: 雷傲科技 (C)(R)2004    #
#####################################################
#      主頁地址： http://www.LeoBBS.com/            #
#      論壇地址： http://bbs.LeoBBS.com/            #
#####################################################

# 返回 -1，沒有會員
# 返回 0 ，會員數目據有問題，寫入失敗
# 返回 1 ，成功\

sub updateuserinfo {  #利用變化量來更新會員信息
    my($nametocheck,$cposts, $creplys, $crating,$cjin1yan,$cmei1li,$cmoney,$cpostdel,$cjhcount,$conlinetime,$cjifen) = @_;
#     會員名稱，發文數變化量，回覆數目變化量，威望變化量，經驗變化量(無用)，魅力變化量(無用)，金錢變化量，文章被刪除數變化量，精華文章變化量，在線時間變化量，積分變化量

    $cposts     = 0 if ($cposts      eq "");
    $creplys    = 0 if ($creplys     eq "");
    $crating    = 0 if ($crating     eq "");
    $cmoney     = 0 if ($cmoney      eq "");
    $cpostdel   = 0 if ($cpostdel    eq "");
    $cjhcount   = 0 if ($cjhcount    eq "");
    $conlinetime= 0 if ($conlinetime eq "");
    $cjifen     = 0 if ($cjifen eq "");

    my $nametochecktemp = $nametocheck;
    $nametocheck =~ s/ /\_/g;
    $nametocheck =~ tr/A-Z/a-z/;
    $nametocheck =~ s/[\a\f\n\e\0\r\t\`\~\!\@\#\$\%\^\&\*\(\)\+\=\\\{\}\;\'\:\"\,\.\/\<\>\?]//isg;
    $userregistered = "";
    my $namenumber = &getnamenumber($nametocheck);
    &checkmemfile($nametocheck,$namenumber);
    my $filetoopen = "${lbdir}$memdir/$namenumber/$nametocheck.cgi";
    if (-e $filetoopen) {
	&winlock($filetoopen) if ($OS_USED eq "Nt");
	open(FILE,"+<$filetoopen");
	flock (FILE, 2) if ($OS_USED eq "Unix");
        my $filedata = <FILE>;
	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  $useradd04,my  $useradd02, my $mymoney, my $postdel, my $sex, my $education, my $marry, my $work, my $born, my $chatlevel,my  $chattime, my $jhmp,my $jhcount,my $ebankdata,my $onlinetime,my $userquestion, my $awards, my $jifen, my $userface, my $soccerdata, my $useradd5) = split(/\t/,$filedata);
	$membercode ||= "me";

	(my $totleposts, my $totlecreplys) = split(/\|/,$numberofposts);

	$totleposts   += $cposts;
	$totlecreplys += $creplys;
	$totleposts   = 0 if($totleposts   < 0);
	$totlecreplys = 0 if($totlecreplys < 0);
	$numberofposts = "$totleposts|$totlecreplys";

	$rating     += $crating;
	$mymoney    += $cmoney;
	$postdel    += $cpostdel;
	$jhcount    += $cjhcount;
	$onlinetime += $conlinetime;
	$jifen      += $cjifen;

	$onlinetime = 0 if ($onlinetime < 0);
	$jhcount    = 0 if ($jhcount < 0);
	$postdel    = 0 if ($postdel < 0 );
	$rating     = -5 if ($rating < -5);
	$rating     = $maxweiwang  if ($rating >  $maxweiwang);

	if (($membername ne "")&&($password ne "")) {
	    seek(FILE,0,0);
	    print FILE "$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$useradd04\t$useradd02\t$mymoney\t$postdel\t$sex\t$education\t$marry\t$work\t$born\t$chatlevel\t$chattime\t$jhmp\t$jhcount\t$ebankdata\t$onlinetime\t$userquestion\t$awards\t$jifen\t$userface\t$soccerdata\t$useradd5\t\n";
	    close(FILE);
	    if (((-M "${lbdir}$memdir/old/$nametocheck.cgi") > 0.5)||(!(-e "${lbdir}$memdir/old/$nametocheck.cgi"))) {
	        open(FILE,">${lbdir}$memdir/old/$nametocheck.cgi");
	        print FILE "$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$useradd04\t$useradd02\t$mymoney\t$postdel\t$sex\t$education\t$marry\t$work\t$born\t$chatlevel\t$chattime\t$jhmp\t$jhcount\t$ebankdata\t$onlinetime\t$userquestion\t$awards\t$jifen\t$userface\t$soccerdata\t$useradd5\t\n";
	        close(FILE);
	    }
	    &winunlock($filetoopen) if ($OS_USED eq "Nt");
	    return 1;
	} else {
	    close(FILE);
	    &winunlock($filetoopen) if ($OS_USED eq "Nt");
	    return 0;
	}
    }
    else {
        return -1;
    }
}

sub upinfodata {  #更新會員檔案庫的任何字段(散列方式,會員名稱不能更新，密碼更新後變成明文)
    my %infos = @_;                      #獲取參數，變成散列
    my $nametocheck = $infos{name};      #會員名稱字
    return -1 if ($nametocheck eq "");   #沒有名字，返回 -1

    $nametocheck =~ s/ /\_/g;
    $nametocheck =~ tr/A-Z/a-z/;
    $nametocheck =~ s/[\a\f\n\e\0\r\t\`\~\!\@\#\$\%\^\&\*\(\)\+\=\\\{\}\;\'\:\"\,\.\/\<\>\?]//isg;
    my $namenumber = &getnamenumber($nametocheck);
    &checkmemfile($nametocheck,$namenumber);
    my $filetoopen = "${lbdir}$memdir/$namenumber/$nametocheck.cgi";
    if ((-e $filetoopen)&&($nametocheck !~ /^訪客/)&&($nametocheck ne "")) {
	&winlock($filetoopen) if ($OS_USED eq "Nt");
	open(FILE,"+<$filetoopen");
	flock (FILE, 2) if ($OS_USED eq "Unix");
        my $filedata = <FILE>;
	chomp($filedata);
	local ($membername, $password, $membertitle, $membercode, $numberofposts, $emailaddress, $showemail, $ipaddress, $homepage, $oicqnumber, $icqnumber ,$location ,$interests, $joineddate, $lastpostdate, $signature, $timedifference, $privateforums, $useravatar, $userflag, $userxz, $usersx, $personalavatar, $personalwidth, $personalheight, $rating, $lastgone, $visitno, $useradd04, $useradd02, $mymoney, $postdel, $sex, $education, $marry, $work, $born, $chatlevel, $chattime, $jhmp,$jhcount,$ebankdata,$onlinetime,$userquestion,$awards,$jifen,$userface, $soccerdata, $useradd5) = split(/\t/,$filedata);
	($numberofposts, local $numberofreplys) = split(/\|/,$numberofposts);
	$numberofposts ||= "0";
	$numberofreplys ||= "0";
	$membercode ||= "me";
	$jhcount = "0" if ($jhcount <= 0);
	$jhcount ||= "0";
	$onlinetime = "3000" if ($onlinetime < 0);
	for ('password','membertitle','membercode','numberofposts','numberofreplys','emailaddress','showemail','ipaddress','homepage','oicqnumber','icqnumber','location','interests','joineddate','lastpostdate','signature','timedifference','privateforums','useravatar','userflag','userxz','usersx','personalavatar','personalwidth','personalheight','rating','lastgone','visitno','useradd04','useradd02','mymoney','postdel','sex','education','marry','work','born','chatlevel','chattime','jhmp','jhcount','ebankdata','onlinetime','userquestion','awards','jifen','userface','soccerdata','useradd5') {
	    ${$_} = $infos{$_} if ($infos{$_} ne ""); #獲取變數：）不在這裡面的變數不於理會：）
	}
	if (($membername ne "")&&($password ne "")) {
	    seek(FILE,0,0);
	    print FILE "$membername\t$password\t$membertitle\t$membercode\t$numberofposts|$numberofreplys\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$useradd04\t$useradd02\t$mymoney\t$postdel\t$sex\t$education\t$marry\t$work\t$born\t$chatlevel\t$chattime\t$jhmp\t$jhcount\t$ebankdata\t$onlinetime\t$userquestion\t$awards\t$jifen\t$userface\t$soccerdata\t$useradd5\t\n";
	    close(FILE);
	    &winunlock($filetoopen) if ($OS_USED eq "Nt");
	    if (((-M "${lbdir}$memdir/old/$nametocheck.cgi") > 0.5)||(!(-e "${lbdir}$memdir/old/$nametocheck.cgi"))) {
	        open(FILE,">${lbdir}$memdir/old/$nametocheck.cgi");
		print FILE "$membername\t$password\t$membertitle\t$membercode\t$numberofposts|$numberofreplys\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$useradd04\t$useradd02\t$mymoney\t$postdel\t$sex\t$education\t$marry\t$work\t$born\t$chatlevel\t$chattime\t$jhmp\t$jhcount\t$ebankdata\t$onlinetime\t$userquestion\t$awards\t$jifen\t$userface\t$soccerdata\t$useradd5\t\n";
	        close(FILE);
		unlink ("${lbdir}cache/meminfo/$nametocheck.pl");
		unlink ("${lbdir}cache/myinfo/$nametocheck.pl");
	    }
	    return 1;
	}
        else { close(FILE); &winunlock($filetoopen) if ($OS_USED eq "Nt"); return 0; }
    }
    else {
        return -1;
    }
}

1;
