summaryrefslogtreecommitdiff
path: root/util/vndb.pl
blob: 88281da9e8d6b4914222e68ca4461a8e52054a8c (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
#!/usr/bin/perl


package VNDB;

use strict;
use warnings;


use Cwd 'abs_path';
our $ROOT;
BEGIN { ($ROOT = abs_path $0) =~ s{/util/vndb\.pl$}{}; }


use lib $ROOT.'/lib';


use TUWF ':html';
use VNDB::L10N;
use SkinFile;


our(%O, %S);


# load the skins
# NOTE: $S{skins} can be modified in data/config.pl, allowing deletion of skins or forcing only one skin
my $skin = SkinFile->new("$ROOT/static/s");
$S{skins} = { map +($_ => [ $skin->get($_, 'name'), $skin->get($_, 'userid') ]), $skin->list };


# load lang.dat
VNDB::L10N::loadfile();


# load settings from global.pl
require $ROOT.'/data/global.pl';


# automatically regenerate the skins and script.js and whatever else should be done
system "make -sC $ROOT" if $S{regen_static};


$TUWF::OBJ->{$_} = $S{$_} for (keys %S);
TUWF::set(
  %O,
  pre_request_handler => \&reqinit,
  error_404_handler => \&handle404,
  log_format => \&logformat,
  validate_templates => {
    id    => { template => 'uint', min => 1, max => 1<<40 },
    page  => { template => 'uint', max => 1000 },
    uname => { regex => qr/^[a-z0-9-]*$/, minlength => 2, maxlength => 15 },
  },
);
TUWF::load_recursive('VNDB::Util', 'VNDB::DB', 'VNDB::Handler');
TUWF::run();


sub reqinit {
  my $self = shift;

  # check authentication cookies
  $self->authInit;

  # Determine language
  my $cookie = $self->reqCookie('l10n');
  $cookie = '' if !$cookie || !grep $_ eq $cookie, VNDB::L10N::languages;
  my $handle = VNDB::L10N->get_handle(); # falls back to English
  my $browser = $handle->language_tag();
  my $rmcookie = 0;

  # when logged in, the setting is kept in the DB even if it's the same as what
  # the browser requests. This is to ensure a user gets the same language even
  # when switching PCs
  if($self->authInfo->{id}) {
    my $db = $self->authPref('l10n');
    if($db && !grep $_ eq $db, VNDB::L10N::languages) {
      $self->authPref(l10n => undef);
      $db = '';
    }
    $rmcookie = 1 if $cookie;
    if(!$db && $cookie && $cookie ne $browser) {
      $self->authPref(l10n => $cookie);
      $db = $cookie;
    }
    $handle = VNDB::L10N->get_handle($db) if $db && $db ne $browser;
  }

  else {
    $rmcookie = 1 if $cookie && $cookie eq $browser;
    $handle = VNDB::L10N->get_handle($cookie) if $cookie && $browser ne $cookie;
  }
  $self->resCookie(l10n => undef) if $rmcookie;
  $self->{l10n} = $handle;

  # load some stats (used for about all pageviews, anyway)
  $self->{stats} = $self->dbStats;

  return 1;
}


sub handle404 {
  my $self = shift;
  $self->resStatus(404);
  $self->htmlHeader(title => 'Page Not Found');
  div class => 'mainbox';
   h1 'Page not found';
   div class => 'warning';
    h2 'Oops!';
    p;
     txt 'It seems the page you were looking for does not exist,';
     br;
     txt 'you may want to try using the menu on your left to find what you are looking for.';
    end;
   end;
  end;
  $self->htmlFooter;
}


# log user IDs (necessary for determining performance issues, user preferences
# have a lot of influence in this)
sub logformat {
  my($self, $uri, $msg) = @_;
  sprintf "[%s] %s %s: %s\n", scalar localtime(), $uri,
    $self->authInfo->{id} ? 'u'.$self->authInfo->{id} : '-', $msg;
}