summaryrefslogtreecommitdiff
path: root/util/vndb.pl
blob: 32e1bdb4e9a88b86a24b9af7a2b0cbdac70bdbc9 (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
#!/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.'/yawf/lib';
use lib $ROOT.'/lib';


use YAWF ':html';
use VNDB::L10N;


our(%O, %S);


# load and (if required) regenerate the skins
# NOTE: $S{skins} can be modified in data/config.pl, allowing deletion of skins or forcing only one skin
$S{skins} = readskins();


# automatically regenerate script.js when required and possible
checkjs();


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


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


YAWF::init(
  %O,
  namespace => 'VNDB',
  object_data => \%S,
  pre_request_handler => \&reqinit,
  error_404_handler => \&handle404,
);


sub reqinit {
  my $self = shift;

  # Determine language
  # if the cookie or parameter "l10n" is set, use that.
  #   otherwise, interpret the Accept-Language header or fall back to English
  # if the cookie is set and is the same as either the Accept-Language header or the fallback, remove it
  my $conf = $self->reqParam('l10n') || $self->reqCookie('l10n');
  $conf = '' if !$conf || !grep $_ eq $conf, VNDB::L10N::languages;

  $self->{l10n} = VNDB::L10N->get_handle(); # this uses I18N::LangTags::Detect
  if($self->{l10n}->language_tag() eq $conf && $self->reqCookie('l10n')) {
    $self->resHeader('Set-Cookie', "l10n= ; expires=Sat, 01-Jan-2000 00:00:00 GMT; path=/; domain=$self->{cookie_domain}");
  } elsif($self->reqParam('l10n') && $conf && $conf ne ($self->reqCookie('l10n')||'') && $self->{l10n}->language_tag() ne $conf) {
    $self->resHeader('Set-Cookie', "l10n=$conf; expires=Sat, 01-Jan-2030 00:00:00 GMT; path=/; domain=$self->{cookie_domain}");
  }
  $self->{l10n} = VNDB::L10N->get_handle($conf) if $conf && $self->{l10n}->language_tag() ne $conf;


  # check authentication cookies
  $self->authInit;

  # check for IE6
  if($self->reqHeader('User-Agent') && $self->reqHeader('User-Agent') =~ /MSIE 6/
    && !$self->reqCookie('ie-sucks') && $self->reqPath ne 'we-dont-like-ie6') {
    # act as if we're opening /we-dont-like-ie6 (ugly hack, until YAWF supports preventing URL handlers from firing)
    $ENV{HTTP_REFERER} = $ENV{REQUEST_URI};
    $ENV{REQUEST_URI} = '/we-dont-like-ie6';
  }

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


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 "It seems the page you were looking for does not exist,\n".
      "you may want to try using the menu on your left to find what you are looking for.";
   end;
  end;
  $self->htmlFooter;
}


sub readskins {
  my %skins; # dirname => skin name
  my @regen;
  my $lasttemplate = [stat "$ROOT/data/style.css"]->[9];
  for my $f (glob "$ROOT/static/s/*") {
    next if !-e "$f/conf";
    my $n = $1 if $f =~ m{([^/]+)$};
    open my $F, '<', "$f/conf" or die $!;
    while(<$F>) {
      chomp;
      s/\r//;
      s{[\t\s]*//.*$}{};
      next if !/^name[\t\s]+(.+)$/;
      $skins{$n} = $1;
      last;
    }
    close $F;
    next if !$skins{$n};

    my $css = -f "$f/style.css" && [stat "$f/style.css"]->[9] || 0;
    my $boxbg = -f "$f/boxbg.png" && [stat "$f/boxbg.png"]->[9] || 0;
    my $lastgen = $css < $boxbg ? $css : $boxbg;
    push @regen, $n if (!$lastgen && -x $f && (!$css && !$boxbg || $css && -w "$f/style.css" || $boxbg && -w "$f/boxbg.png"))
      || ([stat "$f/conf"]->[9] > $lastgen || $lasttemplate > $lastgen) && -w "$f/style.css" && -w "$f/boxbg.png";
  }
  system "$ROOT/util/skingen.pl", @regen if @regen;
  return \%skins;
}


sub checkjs {
  my $script = "$ROOT/static/f/script.js";
  my $lastmod = [stat $script]->[9];
  system "$ROOT/util/jsgen.pl" if
       (!-e $script && -x "$ROOT/static/f")
    || (-e $script && -w $script && (
           $lastmod < [stat "$ROOT/data/script.js"]->[9]
        || $lastmod < [stat "$ROOT/data/lang.txt"]->[9]
        || (-e "$ROOT/data/config.pl" && $lastmod < [stat "$ROOT/data/config.pl"]->[9])
        || $lastmod < [stat "$ROOT/data/global.pl"]->[9]
        || $lastmod < [stat "$ROOT/util/jsgen.pl"]->[9]
       ));
}