summaryrefslogtreecommitdiff
path: root/lib/VNDB.pm
blob: 5ac3bc87b9d7b1a4e794157327436710d24588a2 (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package VNDB;

use strict;
use warnings;

BEGIN { require 'global.pl'; }
our $DEBUG;
our %VNDBopts = (
  CookieDomain  => '.vndb.org',
  root_url      => $DEBUG ? 'http://beta.vndb.org' : 'http://vndb.org',
  static_url    => $DEBUG ? 'http://static.beta.vndb.org' : 'http://static.vndb.org',
  debug         => $DEBUG,
  tplopts       => {
    filename      => 'main',
    searchdir     => '/www/vndb/data/tpl',
    compiled      => '/www/vndb/data/tplcompiled.pm',
    namespace     => 'VNDB::Util::Template::tpl',
    pre_chomp     => 1,
    post_chomp    => 1,
    rm_newlines   => 0,
    deep_reload   => $DEBUG,
  },
  ranks => [
    [ [ qw| visitor loser user mod admin | ], [] ],
    {map{$_,1}qw| hist                                     |}, # 0 - visitor (not logged in)
    {map{$_,1}qw| hist                                     |}, # 1 - loser
    {map{$_,1}qw| hist edit                                |}, # 2 - user
    {map{$_,1}qw| hist edit mod lock                       |}, # 3 - mod
    {map{$_,1}qw| hist edit mod lock del userlist useredit |}, # 4 - admin
  ],
  imgpath => '/www/vndb/static/cv',
  mappath => '/www/vndb/data/rg',
  docpath => '/www/vndb/data/docs',
);
$VNDBopts{ranks}[0][1] = { (map{$_,1} map { keys %{$VNDBopts{ranks}[$_]} } 1..5) };

require Time::HiRes if $DEBUG;
require Data::Dumper if $DEBUG;
use VNDB::Util::Template;
use VNDB::Util::Request;
use VNDB::Util::Response;
use VNDB::Util::DB;
use VNDB::Util::Tools;
use VNDB::Util::Auth;
use VNDB::HomePages;
use VNDB::Producers;
use VNDB::Releases;
use VNDB::VNLists;
use VNDB::Users;
use VNDB::Votes;
use VNDB::VN;


my %VNDBuris = ( # wildcards: * -> (.+), + -> ([0-9]+)
  '/'           => sub { shift->HomePage },
  'd+'          => sub { shift->DocPage(shift) },
  nospam        => sub { shift->ResAddTpl(error => { err => 'formerr' }) },
  hist =>   {'*'=> sub { shift->History(undef, undef, $_[1]) } },
 # users
  u => {
    login       => sub { shift->UsrLogin },
    logout      => sub { shift->UsrLogout },
    register    => sub { shift->UsrReg },
    newpass     => sub { shift->UsrPass },
    list => {
      '/'       => sub { shift->UsrList },
      '*'       => sub { $_[3] =~ /^([a-z0]|all)$/ ? shift->UsrList($_[2]) : shift->ResNotFound },
    },
  },
  'u+' => {
    '/'         => sub { shift->UsrPage(shift) },
    votes       => sub { shift->VNVotes(shift) },
    edit        => sub { shift->UsrEdit(shift) },
    pending     => sub { shift->UsrPending(shift) },
    list        => sub { shift->VNMyList(shift) },
    hist => {'*'=> sub { shift->History('u', shift, $_[1]) } },
  },
 # visual novels
  v => {
    '/'         => sub { shift->VNBrowse },
    new         => sub { shift->VNEdit(0); },
    '*'         => sub { $_[2] =~ /^([a-z0]|all|search)$/ ? shift->VNBrowse($_[1]) : shift->ResNotFound; },
  },
  'v+' => {
    '/'         => sub { shift->VNPage(shift) },
    stats       => sub { shift->VNPage(shift, shift) },
    rg          => sub { shift->VNPage(shift, shift) },
    edit        => sub { shift->VNEdit(shift) },
    del         => sub { shift->VNDel(shift)  },
    vote        => sub { shift->VNVote(shift) },
    list        => sub { shift->VNListMod(shift) },
    add         => sub { shift->REdit('v', shift) },
    lock        => sub { shift->VNLock(shift) },     
    hide        => sub { shift->VNHide(shift) },
    hist => {'*'=> sub { shift->History('v', shift, $_[1]) } },
  },
 # releases
  'r+' => {
    '/'         => sub { shift->RPage(shift) },
    edit        => sub { shift->REdit('r', shift) },
    lock        => sub { shift->RLock(shift) },
    del         => sub { shift->RDel(shift)  },
    hide        => sub { shift->RHide(shift) },
    hist => {'*'=> sub { shift->History('r', shift, $_[1]) } },
  },
 # producers
  p => {
    '/'         => sub { shift->PBrowse },
    add         => sub { shift->PEdit(0) },
    '*'         => sub { $_[2] =~ /^([a-z0]|all)$/ ? shift->PBrowse($_[1]) : shift->ResNotFound; }
  },
  'p+' => {
    '/'         => sub { shift->PPage(shift) },
    edit        => sub { shift->PEdit(shift) },
    del         => sub { shift->PDel(shift) },
    lock        => sub { shift->PLock(shift) },
    hide        => sub { shift->PHide(shift) },
    hist => {'*'=> sub { shift->History('p', shift, $_[1]) } },
  },
 # stuff (.xml extension to make sure they aren't counted as pageviews)
  xml => {
    'producers.xml'   => sub { shift->PXML },
    'vn.xml'          => sub { shift->VNXML },
  },
);


# provide redirects for old URIs
my %OLDuris = (
  faq           => sub { shift->ResRedirect('/d6', 'perm') },
  notes         => sub { shift->ResRedirect('/d8', 'perm') },
  vn => {
    rss         => sub { shift->ResRedirect('/hist/rss?t=v&e=1', 'perm') },
    '*'         => sub { shift->ResRedirect('/v/'.$_[1], 'perm') },
  },
  v => {
    cat         => sub {
      my $f = $_[0]->FormCheck({name=>'i',required=>0},{name=>'e',required=>0},{name=>'l',required=>0},
                               {name=>'p',required=>0},{name=>'o',required=>0},{name=>'s',required=>0});
      my %f;
      $f{$_} = $f->{$_} for (qw|p o s|);
      $f{q} = join ' ', (map $VNDB::CAT->{substr($_,0,1)}[1]{substr($_,1,2)}, split /,/, $f->{i}),
                        (map '-'.$VNDB::CAT->{substr($_,0,1)}[1]{substr($_,1,2)}, split /,/, $f->{e}),
                        (map $VNDB::LANG->{$_}, split /,/, $f->{l});
      !$f{$_}&&delete $f{$_} for keys %f;
      $_[0]->ResRedirect('/v/search'.(!(keys %f)?'':'?'.join(';', map $_.'='.$f{$_}, keys %f) ), 'perm');
    },
  },
  'v+' => {
    votes       => sub { shift->ResRedirect('/v'.(shift).'/stats', 'perm') },
    hist=>{rss  => sub { shift->ResRedirect('/v'.(shift).'/hist/rss.xml', 'perm') } },
  },
  u => {
    '*' => {
      '*'       => sub {
        if($_[2] =~ /^_(login|logout|register|newpass|list)$/) {
          $_[3] eq '/' ? $_[0]->ResRedirect('/u/'.$1, 'perm') : $_[0]->ResRedirect('/u/'.$1.'/'.$_[3], 'perm');
        } else {
          my $id = $_[0]->DBGetUser(username => $_[2])->[0]{id};
          $id ? $_[0]->ResRedirect('/u'.$id.'/'.$_[3], 'perm') : $_[0]->ResNotFound;
        }
      },
    }
  },
  'u+' => {
    hist=>{rss  => sub { shift->ResRedirect('/u'.(shift).'/hist/rss.xml', 'perm') } },
  },
  'p+' => {
    hist=>{rss  => sub { shift->ResRedirect('/p'.(shift).'/hist/rss.xml', 'perm') } },
  },
  hist=>{rss    => sub { shift->ResRedirect('/hist/rss.xml', 'perm') } },
);



sub new {
  my $self = shift;
  my $type = ref($self) || $self;
  my %args = @_;

  my $me = bless {
    %args,
    _DB => VNDB::Util::DB->new(@VNDB::DBLOGIN),
    _TPL => VNDB::Util::Template->new(%{$args{tplopts}}),
  }, $type;
  
  return $me;
}


sub get_page {
  my $self = shift;
  my $r = shift;

  $self->{_Req} = VNDB::Util::Request->new($r);
  $self->{_Res} = VNDB::Util::Response->new($self->{_TPL});

  $self->AuthCheckCookie();
  $self->checkuri();

  my $res = $self->ResSetModPerl($r);
  $self->DBCommit();

  return($self, $res);
}


sub checkuri {
  my $self = shift;
  (my $uri = lc($self->ReqUri)) =~ s/^\/+//;
  $uri =~ s/\?.*$//;
  return $self->ResRedirect("/$uri", 'perm') if $uri =~ s/\/+$//;
  $uri =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; # ugly hack, but we only accept ASCII anyway
  return $self->ResNotFound() if $uri !~ /^[a-z0-9\-\._~\/]*$/; # rfc3986 section 2.3, "Unreserved Characters"
  my @uri;
  defined $_ and push(@uri, $_) for (split(/\/+/, $uri));
  my @ouri = @uri; # items in @uri can be modified by uri2page
  $self->uri2page(\%VNDBuris, \@uri, 0);
  $self->uri2page(\%OLDuris, \@ouri, 0) # provide redirects for old uris
    if $self->{_Res}->{code} == 404;
}


sub uri2page {
  my($s, $o, $u, $i) = @_;
  $u->[$i] = '/' if !defined $u->[$i];
  my $n = $o->{$u->[$i]} ? $u->[$i] : ((map { 
    if(/[\*\+]/) {
      my $t = "^$_\$";
      /\*/ ? ($t =~ s/\*/(.+)/) : ($t =~ s/\+/([1-9][0-9]*)/);
      $u->[$i] =~ /$t/ ? ($u->[$i] = $1) && $_ : ();
    } else { () } }
    sort { length($b) <=> length($a) } keys %$o)[0] || '*');
  ref($o->{$n}) eq 'HASH' && $n ne '/' ?
    $s->uri2page($o->{$n}, $u, ++$i) :
    ref($o->{$n}) eq 'CODE' && $i == $#$u ?
    &{$o->{$n}}($s, @$u) :
    $s->ResNotFound();
}


1;


__END__

#   O L D   C O D E   -   N O T   U S E D   A N Y M O R E


# Apache 2 handler
sub handler ($$) {
  my $r = shift;

 # we don't handle internal redirects! (fixes ErrorDocument directives)
  return Apache2::Const::DECLINED
    if $r->prev || $r->next;

  my $start = [Time::HiRes::gettimeofday()] if $DEBUG;
  @WARN = ();
  my($code, $res, $err);
  $SIG{__WARN__} = sub { push(@VNDB::WARN, @_); warn @_; };
  
  $err = eval {

    @Time::CTime::DoW = qw|Sun Mon Tue Wed Thu Fri Sat|;
    @Time::CTime::DayOfWeek = qw|Sunday Monday Tuesday Wednesday Thursday Friday Saturday|;
    @Time::CTime::MoY = qw|Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec|;
    @Time::CTime::MonthOfYear = qw|January February March April May June July August September October November December|;

    $VNDB = VNDB->new(%VNDBopts) if !$VNDB;
    $VNDB->{r} = $r;

   # let apache handle static files
    (my $uri = lc($r->uri())) =~ s/\/+//;
    if(index($uri, '..') == -1 && -f '/www/vndb/www/' . $uri) {
      $code = Apache2::Const::DECLINED;
      return $code;
    }

    $VNDB->DBCheck();
    ($res, $code) = $VNDB->get_page($r);
    if($DEBUG) {
      my($sqlt, $sqlc) = (0, 0);
      foreach (@{$res->{_DB}->{Queries}}) {
        if($_->[0]) {
          $sqlc++;
          $sqlt += $_->[1];
        }
      }
      my $time = Time::HiRes::tv_interval($start);
      my $tpl = $res->{_Res}->{_tpltime} ? $res->{_Res}->{_tpltime}/$time*100 : 0;
      my $gzip = 0;
      $gzip = 100 - $res->{_Res}->{_gzip}->[1]/$res->{_Res}->{_gzip}->[0]*100
        if($res->{_Res}->{_gzip} && ref($res->{_Res}->{_gzip}) eq 'ARRAY' && $res->{_Res}->{_gzip}->[0] > 0);
      printf STDERR "Took %3dms (SQL/TPL/perl: %4.1f%% %4.1f%% %4.1f%%) (GZIP: %4.1f%%) to parse %s\n",
        $time*1000, $sqlt/$time*100, $tpl, 100-($sqlt/$time*100)-$tpl, $gzip, $r->uri();
    }

  };

 # error occured, create a dump file
  if(!defined $err && $@ && $DEBUG) {
    undef $res->{_Res};
    undef $res->{_Req};
    die $@;
  } elsif(!defined $err && $@) {
    if(open(my $E, sprintf '>/www/vndb/data/errors/%04d-%02d-%02d-%d',
        (localtime)[5]+1900, (localtime)[4]+1, (localtime)[3], time)) {
      print $E 'Error @ ' . scalar localtime;

      print $E "\n\nRequest:\n" . $r->the_request . "\n";
      print $E "$_: " . $r->headers_in->{$_} . "\n"
        for (keys %{$r->headers_in});

      print $E "\nParams:\n";
      my $re = Apache2::Request->new($r);
      print $E "$_: " . $re->param($_) . "\n"
        for ($re->param());

      print $E "\nError:\n$@\n\n";
      print $E "Warnings:\n".join('', @WARN)."\n";
      close($E);
    }
    $VNDB->DBRollBack();
    undef $res->{_Res};
    undef $res->{_Req};
    die "Error, check dumpfile!\n";
  }

  undef $res->{_Res};
  undef $res->{_Req};
 # let apache handle 404's
  $code = Apache2::Const::DECLINED if $code == 404;
  return $code;
}


sub mod_perl_init {
  require Apache2::RequestRec;
  require Apache2::RequestIO;
  $VNDB = __PACKAGE__->new(%VNDBopts);
  return 0;
}


sub mod_perl_exit {
  $VNDB->DBExit() if defined $VNDB && ref $VNDB eq __PACKAGE__;
  return 0;
}