########################
#
#上載處理 By 路楊
#
########################

sub upfileonpost #送出的時候處理臨時檔案到合適的地方：）更新文章
{
  my ($inpost,$inforum,$intopic) = @_;

  if ( $$inpost =~/\[UploadFile.{0,6}=tmp_([^\]]+?)\]/is)
  {
    use File::Copy;
    my $tmppath=&getusrdir(1); #臨時目錄

    $topic =$intopic%100;
    my $topath = "${imagesdir}$usrdir/$inforum/$topic"; #目的目錄
    mkdir ("${imagesdir}$usrdir/$inforum", 0777) if (!(-e "${imagesdir}$usrdir/$inforum"));
    chmod(0777,"${imagesdir}$usrdir/$inforum");
    mkdir ("$topath", 0777) if (!(-e "$topath"));
    chmod(0777,"$topath");

    my $tmpshow=$$inpost;
    while ($tmpshow =~ /\[UploadFile.{0,6}=tmp\_([^\]]+?)\]/i) {
      my $filename = $1;
      if (!(-e "$tmppath/tmp_$filename")) { $$inpost=~ s/\[UploadFile.{0,6}=tmp\_$filename\]//isg; }  #刪除不在的檔案
      else {
    	if ($filename =~ /\.torrent/i) {
	    copy("$tmppath/tmp_$filename", "$topath/$filename"); unlink("$tmppath/tmp_$filename"); copy("$tmppath/tmp_$filename.btfile", "$topath/$filename.btfile"); unlink("$tmppath/tmp_$filename.btfile");
    	} else {
 	    copy("$tmppath/tmp_$filename", "$topath/$filename"); unlink("$tmppath/tmp_$filename");
    	}
	$tmpprint = "$tmpprint$filename\n";  #要寫進cache的內容
      }
      $tmpshow =~ s/\[UploadFile.{0,6}=tmp\_$filename\]//isg;
    }
    
    $$inpost=~ s/(\[UploadFile.{0,6}=)tmp\_(([^\]]+?)\])/$1$2/isg; #更新文章

    mkdir("${lbdir}FileCount/$inforum", 0777) if (!(-e "${lbdir}FileCount/$inforum"));
    chmod(0777,"${lbdir}FileCount/$inforum");
    open (FILE, ">>${lbdir}FileCount/$inforum/$inforum\_$intopic.pl"); #寫進cache
    print FILE "$tmpprint";
    close(FILE);
    my @tmpprint = split(/\n/,$tmpprint);
    $tmpprint = $tmpprint[0];
    my ($up_name,$up_ext) = split(/.*\./,$tmpprint);
    return $up_ext;  ##有附件；給BT區留做判斷
   }
  else {return 0; } ###無附件當然無BT
}

sub delupfiles #刪除目前文章全部附件（遍曆文章方式）---刪除回覆時候用
{
  my ($inpost,$inforum,$intopic) = @_;
  if ( $$inpost =~/\[UploadFile.{0,6}=([^\]]+?)\]/is)
  {
    $topic =$intopic%100;
    my $topath = "${imagesdir}$usrdir/$inforum/$topic"; #目的目錄

    my $usruploadfile = "${lbdir}FileCount/$inforum/${inforum}\_${intopic}.pl";
    if (open(FILEUP,"$usruploadfile")) 
    {
      sysread(FILEUP, $tmpprint,(stat(FILEUP))[7]);
      close(FILEUP);
      $tmpprint =~ s/\r//isg;
    }

    my $tmpshow=$$inpost;
    $tmpshow =~ s/\[UploadFile.{0,6}=tmp\_[^\]]+?\]//isg;  #臨時檔案不處理，因為是這次上載的

    while ($tmpshow =~ /\[UploadFile.{0,6}=([^\]]+?)\]/i) {
    	my $filename = $1;
    	if ($filename =~ /\.torrent/i) {
	    unlink("$topath/$filename"); unlink("$topath/$filename.btfile");  $tmpprint =~ s/$filename//isg;
    	} else {
 	    unlink("$topath/$filename"); $tmpprint =~ s/$filename//isg;
    	}
        $tmpshow =~ s/\[UploadFile.{0,6}=([^\]]+?)\]//i;
    }
#    $tmpshow =~ s/(.*?)(\[UploadFile.{0,6}=[^\]]+?\])(.*?)/$2/isg;
#    $tmpshow=~ s/\[UploadFile.{0,6}=([^\]]+?(\.torrent))\]/unlink\("$topath\/$1"\)\; unlink\("$topath\/$1\.btfile"\)\;  $tmpprint\=\~ s\/$1\/\/isg\;/isg;  ##刪除BT檔案
#    $tmpshow=~ s/\[UploadFile.{0,6}=([^\]]+?)\]/ unlink\("$topath\/$1"\)\;/isg; ###unlink\("$topath\/$1"\)\;$tmpprint\=\~ s\/$1\/\/isg\;/isg; 

#    $err=0;
 #   eval $tmpshow; #unlink
#    if ($@) {$err=1;} 

    ### $tmpprint=~ s/$1//isg;  #要寫進cache的內容

    $$inpost =~ s/\[UploadFile(.{0,6})=(tmp\_[^\]]+?)\]/\[Uploadhtc$1=$2\]/isg;
    $$inpost =~ s/\[UploadFile.{0,6}=(.+?)\]//isg;
    $$inpost =~ s/\[Uploadhtc(.{0,6})=(tmp\_[^\]]+?)\]/\[UploadFile$1=$2\]/isg;

    $tmpprint =~ s/\n\n/\n/isg;
    
    open (FILE, ">${lbdir}FileCount/$inforum/$inforum\_$intopic.pl"); #寫進cache
    print FILE "$tmpprint";
    close(FILE);
    return $err; 
   }
  else {return 0; }
}

sub delallupfiles #刪除目前主貼全部附件,全部刪除的時候使用（cache方式）
{
  my ($inforum,$intopic) = @_;

    $topic =$intopic%100;
    my $topath = "${imagesdir}$usrdir/$inforum/$topic"; #目的目錄

    my $usruploadfile = "${lbdir}FileCount/$inforum/${inforum}\_${intopic}.pl";
    if (open(FILEUP,"$usruploadfile")) 
    {
      sysread(FILEUP, $files,(stat(FILEUP))[7]);
      close(FILEUP);
      $files =~ s/\r//isg;
      my @files=split(/\n/,$files);
      foreach my $line (@files)  {  chomp $line; unlink("$topath/$line");  }
    }
    unlink ("$usruploadfile");
    return ; 

}

sub moveallupfiles #移動/copy目前主貼全部附件,移動/copy的時候使用（cache方式）
{
  my ($oldforum,$oldtopic,$newforum,$newtopic,$copy) = @_;

    my $topic =$oldtopic%100;
    my $nowpath = "${imagesdir}$usrdir/$oldforum/$topic"; #原來目錄

    $topic =$newtopic%100;
    my $topath = "${imagesdir}$usrdir/$newforum/$topic"; #目的目錄
    mkdir ("${imagesdir}$usrdir/$newforum", 0777) if (!(-e "${imagesdir}$usrdir/$newforum"));
    chmod(0777,"${imagesdir}$usrdir/$newforum");
    mkdir ("$topath", 0777) if (!(-e "$topath"));
    chmod(0777,"$topath");


    my $usruploadfile = "${lbdir}FileCount/$oldforum/${oldforum}\_${oldtopic}.pl";
    mkdir("${lbdir}FileCount/$newforum", 0777) if (!(-e "${lbdir}FileCount/$newforum"));
    chmod(0777,"${lbdir}FileCount/$newforum");
    my $newusruploadfile = "${lbdir}FileCount/$newforum/${newforum}\_${newtopic}.pl";
    if (open(FILEUP,"$usruploadfile")) 
    {
      sysread(FILEUP, $files,(stat(FILEUP))[7]);
      close(FILEUP);
      $files =~ s/\r//isg;
      my @files=split(/\n/,$files);
      foreach my $line (@files)  
      {  
        chomp $line; 
        copy("$nowpath/$line","$topath/$line");
        unlink("$nowpath/$line") if ($copy eq "no");
       }
    }

    copy("$usruploadfile","$newusruploadfile");
    open(FILEUP,"$newusruploadfile");
    sysread(FILEUP, $files,(stat(FILEUP))[7]);
    close(FILEUP);
    $files =~ s/\r//isg;
    $files =~ s/$oldforum\_$oldtopic\_/$newforum\_$newtopic\_/isg;
    $files =~ s/$oldforum\_$oldtopic\./$newforum\_$newtopic\./isg;
    open(FILEUP,">$newusruploadfile");
    print FILEUP "$files";
    close(FILEUP);
    unlink ("$usruploadfile") if ($copy eq "no");
    return ; 

}

sub getusrdir #獲取臨時檔案夾
{
# my $tmpname = $inmembername;
 my $tmpname = $query->cookie("amembernamecookie");
 $tmpname =~ s/ /\_/g;
 $tmpname =~ tr/A-Z/a-z/;
 $tmpname =~ s/[\a\f\n\e\0\r\t\`\~\!\@\#\$\%\^\&\*\(\)\+\=\\\{\}\;\'\:\"\,\.\/\<\>\?]//isg;
 mkdir ("${imagesdir}$usrdir/tmp", 0777) if (!(-e "${imagesdir}$usrdir/tmp"));
 chmod(0777,"${imagesdir}$usrdir/tmp");
 my $pathtocheck ="${imagesdir}$usrdir/tmp/$tmpname";

 mkdir ("$pathtocheck", 0777) if (!(-e "$pathtocheck"));
 chmod(0777,"$pathtocheck");

 opendir (DIR, "$pathtocheck"); #第1次
 my @files = readdir(DIR);
 closedir (DIR);

 foreach (@files) {unlink("$pathtocheck/$_") if ((-M "$pathtocheck/$_") *86400 > 60*60);} #臨時上載保留60min


 opendir (DIR, "$pathtocheck"); #第2次
 @files = readdir(DIR);
 closedir (DIR);
 $filesno = @files;
 $filesno = $filesno - 2;
 $maxaddnum = 10 if ($maxaddnum eq "" || $maxaddnum < 1);
 if ($_[0] ne 1) {
     if ($filesno > $maxaddnum && $membercode ne "ad" && $membercode ne "smo") {$pathtocheck = "ERR";} ##同時上載到了最大限度##{$thisout="您今天上載的東西太多了或者請不要同時在幾個區同時上載，謝謝合$div_edit";&thisout("$thisoutput");exit;}
 }
 return "$pathtocheck" ;
}

sub gettmpname #獲取臨時檔案名字
{
 my $thisname =shift;
 $thisname=uri_escape($thisname); #建議上載的檔案不要含中文(路楊)
 $thisname =~ s/%//isg;#建議上載的檔案不要含中文(路楊)
 $thisname =~ s/\.//isg;
 $thisname =~ s/\\//isg;
 $thisname =~ s/\///isg;
 $thisname =~ s/\=//isg;
 $thisname =~ s/\-//isg;
 $thisname =~ s/\)//isg;
 $thisname =~ s/\(//isg;
 $thisname =~ s/\+//isg;
 $thisname =~ s/\|//isg;
 $thisname =~ s/\$//isg;
 $thisname =~ s/\,//isg;
 $thisname =~ s/\{//isg;
 $thisname =~ s/\}//isg;
 $thisname =~ s/\[//isg;
 $thisname =~ s/\]//isg;
 $thisname = substr($thisname,0,16) if (length($thisname)>16);
 my $prefix = "tmp";
 if ($thisname=~/^tmp/) {$prefix .= "_up";} #tmp開頭$div_edit為識別，所以如果開頭是tmp則加上up前綴
 my $thistime =time;
 $thisname = "${prefix}_${thisname}_$thistime";
 return "$thisname";
}
1;