summaryrefslogtreecommitdiffstats
path: root/staticweb/index.pl
blob: e6d647ba4c15477f75c235d0abd9d87562522962 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/perl
# $Id$
use lib qw/./;
use LocalVars;
use CGI qw/:standard/;
use strict;
use Template;
#use boardlist;
use b2g;
use DB_File;
use Data::Serializer;

sub deserialize  
{   
    my($what) = @_;
    my $obj = Data::Serializer->new(serializer => 'Storable',
                                    digester   => 'MD5',
                                    compress   => 0,
                                    );
    return $obj->deserialize($what);
}

sub main
{
    my($tmpl, %rh, $bid, %brd);

    if( param('gb') ){
    $rh{gb} = 1;
    $rh{encoding} = 'gb2312';
    $rh{lang} = 'zh-CN';
    $rh{charset} = 'gb2312';    }
    else{
    print redirect("/man.pl/$1/")
        if( $ENV{REDIRECT_REQUEST_URI} =~ m|/\?(.*)| );
    $rh{encoding} = 'Big5';
    $rh{lang} = 'zh-TW';
    $rh{charset} = 'big5';
    }

    return redirect('/index.pl/'.($rh{gb}?'?gb=1':''))
    if( $ENV{REQUEST_URI} eq '/' );

    charset('');
    print header();
    tie %brd, 'DB_File', 'boardlist.db', O_RDONLY, 0666, $DB_HASH;

    ($bid) = $ENV{PATH_INFO} =~ m|.*/(\d+)/$|;
    $bid ||= 0;
    $rh{isroot} = ($bid == 0);

    if( !exists $brd{"class.$bid"} ){
    print "sorry, this bid $bid not found :(";
    return ;
    }

    foreach( @{deserialize($brd{"class.$bid"})} ){
    next if( $brd{"$_.isboard"} &&
         !-e "$MANDATA/".$brd{"tobrdname.$_"}.'.db' );

    push @{$rh{dat}}, [$brd{"$_.isboard"} ? -1 : $_,
               $brd{"$_.brdname"},
               $brd{"$_.title"},
               ];
    }

    my $path = '';
    foreach( $ENV{PATH_INFO} =~ m|(\w+)|g ){
    push @{$rh{class}}, {path => "$path/$_/",
                 title => $brd{"$_.title"}};
    $path .= "/$_";
    }
    $rh{exttitle} = ($rh{class} ? 
             $rh{class}[ $#{@{$rh{class}}} ]{title} : '首頁');

    $tmpl = Template->new({INCLUDE_PATH => '.',
               ABSOLUTE => 0,
               RELATIVE => 0,
               RECURSION => 0,
               EVAL_PERL => 0,
               COMPILE_EXT => '.tmpl',
               COMPILE_DIR => $MANCACHE,
               });
    if( !$rh{gb} ){
    $tmpl->process('index.html', \%rh);
    }
    else{
    my $output;
    $tmpl->process('index.html', \%rh, \$output);
    b2g::big5togb($output);
    print $output;
    }
    untie %brd;
}

main();
1;