###############################################################################
# Attachments.pl #
###############################################################################
# YaBB: Yet another Bulletin Board #
# Open-Source Community Software for Webmasters #
# Version: YaBB 2.4 #
# Packaged: April 12, 2009 #
# Distributed by: http://www.yabbforum.com #
# =========================================================================== #
# Copyright (c) 2000-2009 YaBB (www.yabbforum.com) - All Rights Reserved. #
# Software by: The YaBB Development Team #
# with assistance from the YaBB community. #
# Sponsored by: Xnull Internet Media, Inc. - http://www.ximinc.com #
# Your source for web hosting, web design, and domains. #
###############################################################################
$attachmentsplver = 'YaBB 2.4 $Revision$';
if ($GLOBAL::ACTION eq 'detailedversion') { return 1; }
sub Attachments {
&is_admin_or_gmod;
my @attachments = &read_DBorFILE(1,'',$vardir,'attachments','txt');
my $attachment_space = 0;
foreach (@attachments) {
$attachment_space += (split /\|/, $_, 7)[5];
}
my $remaining_space;
if (!$dirlimit) {
$remaining_space = "$fatxt{'23'}";
} else {
$remaining_space = ($dirlimit - $attachment_space) . " KB";
}
$maxdaysattach = &read_DBorFILE(1,'',$vardir,'oldestattach','txt');
$maxsizeattach = &read_DBorFILE(1,'',$vardir,'maxattachsize','txt');
my $totalattachnum = @attachments;
$yymain .= qq~
$fatxt{'24'} |
$fatxt{'25'}
|
| $fatxt{'26'} |
$fatxt{'27'}
|
| $fatxt{'28'} |
$totalattachnum |
| $fatxt{'29'} |
$attachment_space KB
|
| $fatxt{'30'} |
$remaining_space |
|
$fatxt{'31'}
|
|
|
~;
$yytitle = "$fatxt{'36'}";
$action_area = "manageattachments";
&AdminTemplate;
}
sub RemoveOldAttachments {
&is_admin_or_gmod;
my $maxdaysattach = $FORM{'maxdaysattach'} || $INFO{'maxdaysattach'};
if ($maxdaysattach !~ /^[0-9]+$/) { &fatal_error("only_numbers_allowed"); }
# Set up the multi-step action
$time_to_jump = time() + $max_process_time;
&automaintenance('on');
opendir(ATT, $uploaddir) || &fatal_error('cannot_open', "$uploaddir", 1);
my @attachments = sort( grep(/\w+$/, readdir(ATT)) );
closedir(ATT);
my @attachmentstxt = &read_DBorFILE(1,'',$vardir,'attachments','txt');
my (%att,@line);
foreach (@attachmentstxt) {
@line = split(/\|/, $_);
$att{$line[7]} = $line[0];
}
my $info;
if (!@attachments) {
&write_DBorFILE(0,'',$vardir,'attachments','txt',(''));
$info = qq~
$fatxt{'48'}.~;
} else {
&delete_DBorFILE("$vardir/rem_old_attach.tmp") unless exists $INFO{'next'};
my %rem_attachments;
for (my $a = ($INFO{'next'} || 0); $a < @attachments; $a++) {
# -M => Script start time minus file modification time, in days.
my $age = sprintf("%.2f", -M "$uploaddir/$attachments[$a]");
if ($age <= $maxdaysattach) {
# If the attachment is not too old
$info .= qq~
$attachments[$a] = $age $admin_txt{'122'}.~;
} elsif (exists $att{$attachments[$a]}) {
$rem_attachments{$att{$attachments[$a]}} .= $rem_attachments{$att{$attachments[$a]}} ? "|$attachments[$a]" : $attachments[$a];
$info .= qq~
$attachments[$a] $fatxt{'1'} = $age $admin_txt{'122'}.~;
}
if ($time_to_jump < time() && ($a + 1) < @attachments) {
# save the $info of this run until the end of 'RemoveOldAttachments'
&write_DBorFILE(0,FILE,$vardir,'rem_old_attach','tmp',(&read_DBorFILE(1,FILE,$vardir,'rem_old_attach','tmp'),$info));
$yySetLocation = qq~$adminurl?action=removeoldattachments;maxdaysattach=$maxdaysattach;next=~ . ($a + 1 - &RemoveAttachments(\%rem_attachments));
&redirectexit;
}
}
&RemoveAttachments(\%rem_attachments);
}
&automaintenance('off');
$yymain .= qq~$fatxt{'32'} $maxdaysattach $fatxt{'58'}.
~;
$yymain .= join('', &read_DBorFILE(1,'',$vardir,'rem_old_attach','tmp')) . $info;
&delete_DBorFILE("$vardir/rem_old_attach.tmp");
&write_DBorFILE(0,'',$vardir,'oldestattach','txt',($maxdaysattach));
$yytitle = "$fatxt{'34'} $maxdaysattach";
$action_area = "removeoldattachments";
&AdminTemplate;
}
sub RemoveBigAttachments {
&is_admin_or_gmod;
my $maxsizeattach = $FORM{'maxsizeattach'} || $INFO{'maxsizeattach'};
if ($maxsizeattach !~ /^[0-9]+$/) { &fatal_error("only_numbers_allowed"); }
# Set up the multi-step action
$time_to_jump = time() + $max_process_time;
&automaintenance('on');
opendir(ATT, $uploaddir) || &fatal_error('cannot_open', "$uploaddir", 1);
my @attachments = sort( grep(/\w+$/, readdir(ATT)) );
closedir(ATT);
@attachmentstxt = &read_DBorFILE(1,'',$vardir,'attachments','txt');
my (%att,@line);
foreach (@attachmentstxt) {
@line = split(/\|/, $_);
$att{$line[7]} = $line[0];
}
my $info;
if (!@attachments) {
&write_DBorFILE(0,'',$vardir,'attachments','txt',(''));
$info = qq~
$fatxt{'48'}.~;
} else {
&delete_DBorFILE("$vardir/rem_big_attach.tmp") unless exists $INFO{'next'};
my (%rem_attachments,@line);
for ($a = ($INFO{'next'} || 0); $a < @attachments; $a++) {
my $size = sprintf("%.2f", ((-s "$uploaddir/$attachments[$a]") / 1024));
if ($size <= $maxsizeattach) {
# If the attachment is not too big
$info .= qq~
$attachments[$a] = $size KB~;
} elsif (exists $att{$attachments[$a]}) {
$rem_attachments{$att{$attachments[$a]}} .= $rem_attachments{$att{$attachments[$a]}} ? "|$attachments[$a]" : $attachments[$a];
$info .= qq~
$attachments[$a] $fatxt{'1'} = $size KB~;
}
if ($time_to_jump < time() && ($a + 1) < @attachments) {
# save the $info of this run until the end of 'RemoveBigAttachments'
&write_DBorFILE(0,FILE,$vardir,'rem_big_attach','tmp',(&read_DBorFILE(1,FILE,$vardir,'rem_big_attach','tmp'),$info));
$yySetLocation = qq~$adminurl?action=removebigattachments;maxsizeattach=$maxsizeattach;next=~ . ($a + 1 - &RemoveAttachments(\%rem_attachments));
&redirectexit;
}
}
&RemoveAttachments(\%rem_attachments);
}
$yymain .= qq~$fatxt{'33'} $maxsizeattach KB.
~;
$yymain .= join('', &read_DBorFILE(1,'',$vardir,'rem_big_attach','tmp')) . $info;
&delete_DBorFILE("$vardir/rem_big_attach.tmp");
&write_DBorFILE(0,'',$vardir,'maxattachsize','txt',($maxsizeattach));
&automaintenance('off');
$yytitle = "$fatxt{'35'} $maxsizeattach KB";
$action_area = "removebigattachments";
&AdminTemplate;
}
sub Attachments2 {
&is_admin_or_gmod;
my @attachinput = &read_DBorFILE(1,'',$vardir,'attachments','txt');
my $max = @attachinput;
my $action = $INFO{'action'};
my $sort = $INFO{'sort'} || 6;
my $newstart = $INFO{'newstart'} || 0;
if (!$max) {
$viewattachments .= qq~| $fatxt{'48'} |
~;
} else {
$yymain .= qq~
' if $max;
$yytitle = "$fatxt{'37'}";
$action_area = "manageattachments";
&AdminTemplate;
}
sub DeleteAttachments {
&is_admin_or_gmod;
&automaintenance('on') if !$FORM{'formsession'};
my %rem_att;
foreach (keys(%FORM)) {
next unless $_ =~ /^del_(\d+)$/;
my $thread = $1;
$rem_att{$thread} = $FORM{$_};
$rem_att{$thread} =~ s/, /|/g;
}
&RemoveAttachments(\%rem_att);
&automaintenance('off') if !$FORM{'formsession'};
$yySetLocation = $FORM{'formsession'} ? qq~$scripturl?action=viewdownloads;thread=~ . (keys(%rem_att))[0] . qq~;newstart=$FORM{'newstart'}~ : qq~$adminurl?action=manageattachments2;newstart=$FORM{'newstart'}~;
&redirectexit;
}
sub FullRebuildAttachents {
&is_admin_or_gmod();
unless (defined $INFO{'boardnum'}) {
&automaintenance('on');
&delete_DBorFILE("$vardir/newattachments.tmp");
$yySetLocation = qq~$adminurl?action=rebuildattach;topicnum=0;boardnum=0~;
&redirectexit();
}
# Set up the multi-step action
$time_to_jump = time() + $max_process_time;
@boardlist = sort( keys(%board) );
# Find the current board:
my $curboard = $boardlist[$INFO{'boardnum'}];
# store all downloadcounts in variable
my %attachments;
if (-s "$vardir/attachments.txt" > 5) {
my ($atfile,$atcount);
foreach (&read_DBorFILE(0,'',$vardir,'attachments','txt')) {
(undef, undef, undef, undef, undef, undef, undef, $atfile, $atcount) =split(/\|/, $_);
chomp $atcount;
$attachments{$atfile} = $atcount;
}
}
# Get the topic list.
my @topiclist = &read_DBorFILE(0,'',$boardsdir,$curboard,"txt");
my ($topicnum,@newattachments,$mreplies,$msub, $mname, $mdate, $mfn, $nexttopic);
for (my $i = $INFO{'topicnum'}; $i < @topiclist; $i++) {
($topicnum, undef) = split(/\|/, $topiclist[$i], 2);
my @topic = &read_DBorFILE(0,'',$datadir,$topicnum,'txt');
chomp(@topic);
$mreplies = 0;
foreach (@topic) {
($msub, $mname, undef, $mdate, undef, undef, undef, undef, undef, undef, undef, undef, $mfn) = split(/\|/, $_);
foreach (split(/,/, $mfn)) {
if (-e "$uploaddir/$_") {
my $asize = int((-s "$uploaddir/$_") / 1024) || 1;
push (@newattachments, qq~$topicnum|$mreplies|$msub|$mname|$curboard|$asize|$mdate|$_|~ . ($attachments{$_} || 0) . qq~\n~);
}
}
$mreplies++;
}
if (time() > $time_to_jump && ($i + 1) < @topiclist) {
$nexttopic = $i + 1;
last;
}
}
if (@newattachments) {
&write_DBorFILE(0,NEWATM,$vardir,'newattachments','tmp',(&read_DBorFILE(1,NEWATM,$vardir,'newattachments','tmp'),@newattachments));
}
# Prepare to continue...
if ($nexttopic) { $INFO{'topicnum'} = $nexttopic; }
else { $INFO{'boardnum'}++; $INFO{'topicnum'} = 0; }
my $numleft = @boardlist - $INFO{'boardnum'};
if ($numleft == 0) {
my @newattachments = &read_DBorFILE(1,'',$vardir,'newattachments','tmp');
&write_DBorFILE(0,'',$vardir,"attachments","txt",sort( { (split /\|/,$a)[6] <=> (split /\|/,$b)[6] } @newattachments));
&delete_DBorFILE("$vardir/newattachments.tmp");
&automaintenance("off");
$yySetLocation = qq~$adminurl?action=remghostattach~;
&redirectexit;
}
# Continue
$action_area = 'manageattachments';
$yytitle = "$fatxt{'37'}";
$yymain .= qq~
$rebuild_txt{'1'}
$rebuild_txt{'5'} $max_process_time $rebuild_txt{'6'}
$rebuild_txt{'9'} ~ . (@boardlist - $INFO{'boardnum'}) . "/" . @boardlist . qq~
~;
&AdminTemplate();
}
sub RemoveGhostAttach {
&is_admin_or_gmod;
$yymain .= qq~$fatxt{'62'}
~;
my @attachmentstxt = &read_DBorFILE(0,'',$vardir,'attachments','txt');
my %att;
foreach (@attachmentstxt) {
$att{(split(/\|/, $_))[7]} = 1;
}
opendir(DIR, $uploaddir);
my @filesDIR = grep(/\w+$/, readdir(DIR));
close(DIR);
$yymain .= qq~$fatxt{'61'}:
~;
foreach my $fileinDIR (@filesDIR) {
if (!$att{$fileinDIR}) {
&delete_DBorFILE("$uploaddir/$fileinDIR");
$yymain .= qq~
$fatxt{'61b'}: $fileinDIR~;
}
}
$yymain .= qq~
$fatxt{'61a'}~;
$yytitle = $fatxt{'61'};
$action_area = 'manageattachments';
&AdminTemplate;
}
sub RemoveAttachments { # remove single or multiple attachments stored in a hash-reference
my $count = 0;
my $ThreadHashref = shift; # usage: ${$ThreadHashref}{'threadnum'} = 'filename1|filename2|...'
# all attachments of thread are included if filname is undefined (undef)
return $count if !%$ThreadHashref;
my @attachments = &read_DBorFILE(0,ATM,$vardir,'attachments','txt');
my ($athreadnum, $afilename, %del_filename, @new_attach);
foreach (@attachments) {
(undef, undef, undef, undef, undef, undef, undef, $afilename, undef) = split(/\|/, $_);
$del_filename{$afilename}++;
}
for ($i = 0; $i < @attachments; $i++) {
($athreadnum, undef, undef, undef, undef, undef, undef, $afilename, undef) = split(/\|/, $attachments[$i]);
my $del = 0;
if (exists ${$ThreadHashref}{$athreadnum}) {
if (defined ${$ThreadHashref}{$athreadnum}) {
foreach (split(/\|/, ${$ThreadHashref}{$athreadnum})) {
if ($_ eq $afilename) { $del = 1; last; }
}
} else {
$del = 1;
}
}
if ($del) {
# deletes the file only if NO other entry for the same filename is in the attachments.txt
&delete_DBorFILE("$uploaddir/$afilename") if $del_filename{$afilename} == 1;
$del_filename{$afilename}--;
$count++;
} else {
push(@new_attach, $attachments[$i]);
}
}
&write_DBorFILE(0,ATM,$vardir,'attachments','txt',@new_attach);
$count;
}
1;