Tetra-WebBBS Debugger 6.10

Contents of /home/zoetecnocampoco/public_html/cgi-bin/bbs/lib/webbbs_rebuild.pl

    1- #<<<
    2- #######################################
    3- #    ---  Tetra-WebBBS  --- 
    4- # By:             tetrabb.com
    5- # Version:        6.10
    6- # Last modified:  2009-11-01
    7- # Copyright (c)   2006-2009
    8- # Website:        http://tetrabb.com
    9- # For more details see license.txt
   10- #
   11- # Core script:    webbbs_rebuild.pl
   12- # Authors:        Rainer Libowski
   13- #                 Tarty
   14- # Derived from code by Darryl Burgdorf
   15- #######################################
   16- #>>>
   17- 
   18- sub Rebuild_Database {
   19- &LockOpen( NEWDBLOCK, "$dir/newmessagelist.lock" );
   20- opendir( MESSAGES, $dir );
   21- @messagedir = readdir(MESSAGES);
   22- closedir(MESSAGES);
   23- foreach $message (@messagedir) {
   24- if ( $message =~ /^newmessagelist/ ) {
   25- unlink "$dir/$message";
   26- }
   27- }
   28- if ( $DBMType == 1 ) {
   29- tie( %NewMessageList, 'AnyDBM_File', "$dir/newmessagelist", O_RDWR | O_CREAT, 0666, $DB_HASH )
   30-     || &Error( "9150", "9151" );
   31- }
   32- elsif ( $DBMType == 2 ) {
   33- dbmopen( %NewMessageList, "$dir/newmessagelist", 0666 )
   34-     || &Error( "9150", "9151" );
   35- }
   36- else {
   37- tie( %NewMessageList, 'AnyDBM_File', "$dir/newmessagelist", O_RDWR | O_CREAT, 0666 )
   38-     || &Error( "9150", "9151" );
   39- }
   40- %MonthToNumber = ( 'Jan', 1, 'Feb', 2,  'Mar', 3,  'Apr', 4,  'May', 5,   'Jun', 6,   'Jul', 7,   'Aug', 8,   'Sep', 9,   'Oct', 10,  'Nov', 11,  'Dec', 12 );
   41- %day_counts    = ( 1,     0, 2,     31, 3,     59, 4,     90, 5,     120, 6,     151, 7,     181, 8,     212, 9,     243, 10,    273, 11,    304, 12,    334 );
   42- opendir( MESSAGES, $dir );
   43- @messagedir = readdir(MESSAGES);
   44- closedir(MESSAGES);
   45- @messagecount     = ();
   46- @physicalmessages = ();
   47- %is_real          = ();
   48- 
   49- foreach $message (@messagedir) {
   50- next if ( $message =~ /^\./ );
   51- if ( ( -d "$dir/$message" ) && ( $message =~ /^bbs\d+/ ) ) {
   52- opendir( COUNT, "$dir/$message" );
   53- push( @messagecount, readdir(COUNT) );
   54- closedir(COUNT);
   55- }
   56- else {
   57- push( @messagecount, "$message" );
   58- }
   59- }
   60- foreach $message (@messagecount) {
   61- unless ( ( $message =~ /\.tmp$/ ) || ( $message == 0 ) ) {
   62- push( @physicalmessages, $message );
   63- $is_real{$message} = 1;
   64- $subdir = "bbs" . int( $message / 1000 );
   65- unless ( -d "$dir/$subdir" ) {
   66- mkdir( "$dir/$subdir", 0777 );
   67- chmod 0777, "$dir/$subdir";
   68- }
   69- unless ( -e "$dir/$subdir/$message" ) {
   70- rename( "$dir/$message", "$dir/$subdir/$message" );
   71- }
   72- }
   73- }
   74- if ( -e "$dir/messages.idx" ) {
   75- open( INDEX, "$dir/messages.idx" );
   76- while (<INDEX>) {
   77- chomp;
   78- if ( ( $message, $timestamp, $sub, $poster, $date, $prev, $next, $count, $admin, $ip ) = split(/\|/) ) {
   79- $count = "";
   80- unless (    ( $date =~ /^\d+$/ )
   81-          && ( $date > 500000000 )
   82-          && ( $date < 1500000000 ) )
   83- {
   84- &ConvertOldDate;
   85- }
   86- unless ( $admin eq "AdminPost" ) { $admin = ""; }
   87- foreach $key ( $date, $sub, $poster, $prev, $next, $count, $admin, $ip ) {
   88- $NewMessageList{$message} .= "$key|";
   89- if ( $message > $lastmessage ) {
   90- $lastmessage = $message;
   91- }
   92- }
   93- }
   94- }
   95- close(INDEX);
   96- unlink "$dir/messages.idx";
   97- while ( ( $key, $value ) = each(%NewMessageList) ) {
   98- unless ( $is_real{$key} > 0 ) {
   99- delete( $NewMessageList{$key} );
  100- }
  101- }
  102- }
  103- if ( !$main::tbb->{'conf'}->{'search'}->{'enable'} ) {
  104- open( SEARCH, "$dir/searchterms.idx" );
  105- while (<SEARCH>) {
  106- if (/^(\d+) /) { $searched{$1} = 1; }
  107- }
  108- close(SEARCH);
  109- &LockOpen( SEARCH, "$dir/searchterms.idx", "a" );
  110- }
  111- foreach $message (@physicalmessages) {
  112- next if ( $NewMessageList{$message} && $searched{$message} );
  113- if ( !$main::tbb->{'conf'}->{'search'}->{'enable'} ) {
  114- unless ( $searched{$message} ) { print SEARCH "$message "; }
  115- }
  116- $subdir = "bbs" . int( $message / 1000 );
  117- open( FILE, "$dir/$subdir/$message" );
  118- @message = <FILE>;
  119- close(FILE);
  120- %wordlist  = ();
  121- $firstline = 0;
  122- $date      = $sub = $poster = $prev = $next = $count = $admin = $ip = "";
  123- 
  124- foreach $line (@message) {
  125- $line =~ s/\r|\n//g;
  126- @words = ();
  127- if    ( $line =~ /^SUBJECT>(.*)/i ) { $sub    = $1; }
  128- elsif ( $line =~ /^ADMIN>(.*)/i )   { $admin  = $1; }
  129- elsif ( $line =~ /^POSTER>(.*)/i )  { $poster = $1; }
  130- elsif ( $line =~ /^EMAIL>/i )       { next; }
  131- elsif ( $line =~ /^DATE>(.*)/i )    { $date   = $1; }
  132- elsif ( $line =~ /^EMAILNOTICES>/i )   { next; }
  133- elsif ( $line =~ /^IP_ADDRESS>(.*)/i ) { $ip = $1; }
  134- elsif ( $line =~ /^<!--/i )            { next; }
  135- elsif ( $line =~ /^PASSWORD>/i )       { next; }
  136- elsif ( $line =~ /^PREVIOUS>(.*)/i )   { $prev = $1; }
  137- elsif ( $line =~ /^NEXT>(.*)/i )       { $next = $1; }
  138- elsif ( $line =~ /^IMAGE>/i )          { next; }
  139- elsif ( $line =~ /^LINKNAME>/i )       { next; }
  140- elsif ( $line =~ /^LINKURL>/i )        { next; }
  141- else {
  142- last if ( $main::tbb->{'conf'}->{'search'}->{'enable'} );
  143- last if ( $searched{$message} );
  144- unless ($firstline) {
  145- $line      = "$sub $poster $line";
  146- $firstline = 1;
  147- }
  148- $line =~ s/<([^>]|\n)*(>|$)/ /g;
  149- 
  150- $line =~ tr/A-Z/a-z/;
  151- 
  152- $line =~ s/&[^;\s]*;/ /g;
  153- $line = &prepare_searchindex($line);
  154- $line =~ s/(\s)+/ /g;
  155- @words = split( /\s/, $line );
  156- foreach $word (@words) {
  157- next if ( $wordlist{$word} );
  158- $wordlist{$word} = 1;
  159- print SEARCH "$word ";
  160- }
  161- }
  162- }
  163- if ( !$main::tbb->{'conf'}->{'search'}->{'enable'} ) {
  164- unless ( $searched{$message} ) { print SEARCH "\n"; }
  165- }
  166- next if ( $NewMessageList{$message} );
  167- unless (    ( $date =~ /^\d+$/ )
  168-          && ( $date > 500000000 )
  169-          && ( $date < 1500000000 ) )
  170- {
  171- &ConvertOldDate;
  172- }
  173- unless ( $admin eq "AdminPost" ) { $admin = ""; }
  174- unless ($sub) {
  175- unlink "$dir/$subdir/$message";
  176- $is_real{$message} = 0;
  177- next;
  178- }
  179- $sub    =~ s/\|/&pipe;/g;
  180- $poster =~ s/\|/&pipe;/g;
  181- foreach $key ( $date, $sub, $poster, $prev, $next, $count, $admin, $ip ) {
  182- $NewMessageList{$message} .= "$key|";
  183- }
  184- if ( $message > $lastmessage ) { $lastmessage = $message; }
  185- }
  186- if (%CountList) {
  187- foreach $key ( keys %CountList ) {
  188- unless ( $is_real{$key} ) {
  189- delete( $CountList{$key} );
  190- }
  191- }
  192- }
  193- if ( !$main::tbb->{'conf'}->{'search'}->{'enable'} ) {
  194- &LockClose( SEARCH, "$dir/searchterms.idx" );
  195- open( SEARCH, "$dir/searchterms.idx" );
  196- &LockOpen( NEWSEARCH, "$dir/newsearchterms.idx" );
  197- while (<SEARCH>) {
  198- if (/^(\d+) /) {
  199- $message = $1;
  200- if ( $is_real{$message} ) {
  201- print NEWSEARCH "$_";
  202- $is_real{$message} = 0;
  203- }
  204- }
  205- }
  206- close(SEARCH);
  207- if ( $ENV{'OS'} =~ /win/i ) {
  208- &LockClose( NEWSEARCH, "$dir/newsearchterms.idx" );
  209- }
  210- else {
  211- &LockOpen( SEARCH, "$dir/searchterms.idx" );
  212- }
  213- rename( "$dir/newsearchterms.idx", "$dir/searchterms.idx" );
  214- unless ( $ENV{'OS'} =~ /win/i ) {
  215- &LockClose( NEWSEARCH, "$dir/newsearchterms.idx" );
  216- &LockClose( SEARCH,    "$dir/searchterms.idx" );
  217- }
  218- }
  219- $number = 0;
  220- if ( -e "$dir/data.txt" ) {
  221- open( NUMBER, "$dir/data.txt" );
  222- $number = <NUMBER>;
  223- close(NUMBER);
  224- }
  225- unless ( $number > $lastmessage ) {
  226- &LockOpen( NUMBER, "$dir/data.txt" );
  227- $lastmessage++;
  228- seek( NUMBER, 0, 0 );
  229- print NUMBER "$lastmessage";
  230- truncate( NUMBER, tell(NUMBER) );
  231- &LockClose( NUMBER, "$dir/data.txt" );
  232- }
  233- if   ( $DBMType == 2 ) { dbmclose(%NewMessageList); }
  234- else                   { untie %NewMessageList; }
  235- &LockClose( NEWDBLOCK, "$dir/newmessagelist.lock" );
  236- 
  237- opendir( MESSAGES, $dir );
  238- @messagedir = readdir(MESSAGES);
  239- closedir(MESSAGES);
  240- foreach $message (@messagedir) {
  241- 
  242- if ( $message =~ /^messagelist/ ) {
  243- unlink "$dir/$message";
  244- }
  245- }
  246- foreach $message (@messagedir) {
  247- if ( $message =~ /^newmessagelist.(.*)/ ) {
  248- rename( "$dir/$message", "$dir/messagelist.$1" );
  249- }
  250- elsif ( $message eq "newmessagelist" ) {
  251- rename( "$dir/newmessagelist", "$dir/messagelist" );
  252- }
  253- }
  254- 
  255- my $cfg  = $tbb->{'conf'}->{'main'};
  256- my %data = ();
  257- $data{'output'} = "<div class='ac'>\n" . "<h3>" . $tbb->{'conf'}->{'text'}->{'0525.1'} . "</h3>\n" . "<p>" . $tbb->{'conf'}->{'text'}->{'0525.2'} . " <a href='" . $cfg->{'cgiurl'} . $cfg->{'BBSquery'} . 'md' . $cfg->{'QS2'} . "checkdb" . "'>" . $text{'8002'} . "</a></p>\n" . "</div>\n";
  258- $tbb->send_output_nontemplate( 'template'   => 'master',
  259-                                'data'       => \%data,
  260-                                'sub_navbar' => 'maintenance', 'subnav_args' => undef,
  261- );
  262- exit;
  263- } ## end sub Rebuild_Database
  264- 
  265- sub ConvertOldDate {
  266- if ( $date =~ /\w+, (\d+) (\w+) (\d+), at (\d+):(\d+) (\w+)/ ) {
  267- $mday = int($1);
  268- $mon  = $2;
  269- $year = int($3);
  270- $hour = int($4);
  271- $min  = int($5);
  272- $ampm = $6;
  273- if ( $ampm =~ /p/ ) { $hour += 12; }
  274- if ( $year > 19000 ) { $year -= 17100; }
  275- $year -= 1900;
  276- $mon   = substr( $mon, 0, 3 );
  277- $mon   = int( $MonthToNumber{$mon} );
  278- $mdays = ( ( $year - 69 ) * 365 ) + ( int( ( $year - 69 ) / 4 ) );
  279- $mdays += $day_counts{$mon};
  280- 
  281- if (    ( int( ( $year - 68 ) / 4 ) eq ( ( $year - 68 ) / 4 ) )
  282-      && ( $mon > 2 ) )
  283- {
  284- $mdays++;
  285- }
  286- $mdays += $mday;
  287- $mdays -= 366;
  288- $date    = ( $mdays * 86400 ) + 18000;
  289- $dsthour = ( localtime($date) )[2];
  290- if ( $dsthour > 0 ) { $date -= 3600; }
  291- $date += ( $hour * 3600 );
  292- $date += ( $min * 60 );
  293- if ($HourOffset) { $date -= ( $HourOffset * 3600 ); }
  294- }
  295- unless (    ( $date =~ /^\d+$/ )
  296-          && ( $date > 500000000 )
  297-          && ( $date < 1500000000 ) )
  298- {
  299- $date = ( stat("$dir/$subdir/$message") )[9];
  300- }
  301- open( FILE, "$dir/$subdir/$message" );
  302- @message = <FILE>;
  303- close(FILE);
  304- open( FILE, ">$dir/$subdir/$message" );
  305- foreach $line (@message) {
  306- if   ( $line =~ /^DATE>/i ) { print FILE "DATE>$date\n"; }
  307- else                        { print FILE $line; }
  308- }
  309- close(FILE);
  310- } ## end sub ConvertOldDate
  311- 
  312- 1;