| 1 | package OurNet::WebBuilder; | ||
| 2 | require 5.005; | ||
| 3 | |||
| 4 | $OurNet::WebBuilder::VERSION = '1.2'; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use lib qw/./; | ||
| 8 | use vars qw/$Backend/; | ||
| 9 | |||
| 10 | use CGI; | ||
| 11 | use CGI::Cookie; | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | OurNet::WebBuilder - Web rendering for BBS-based services | ||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | use OurNet::WebBuilder; | ||
| 20 | |||
| 21 | my $tmpl = { | ||
| 22 | dir => \%dir, | ||
| 23 | article => \%article, | ||
| 24 | cgi_url => CGI->url | ||
| 25 | }; | ||
| 26 | |||
| 27 | my $opref = { | ||
| 28 | '' => sub { | ||
| 29 | return '!view'; | ||
| 30 | }, | ||
| 31 | |||
| 32 | 'view' => sub { | ||
| 33 | $tmpl->{'menu'} ||= param('curdir') || $dir{'oin'}[0]{id}; | ||
| 34 | $tmpl->{'curdir'} ||= param('curdir') || $dir{'oin'}[0]{id}; | ||
| 35 | loadboard($tmpl->{'curdir'}); | ||
| 36 | |||
| 37 | loadarticle($_->{'id'}, | ||
| 38 | ($tmpl->{'curdir'} eq 'bbs' ? $bbs : ($oin, $tmpl->{'curdir'}))) | ||
| 39 | foreach @{$dir{$tmpl->{'curdir'}}}; | ||
| 40 | |||
| 41 | return 'view'; | ||
| 42 | } | ||
| 43 | }; | ||
| 44 | |||
| 45 | OurNet::WebBuilder::Display($tmpl, $opref); | ||
| 46 | |||
| 47 | |||
| 48 | =head1 DESCRIPTION | ||
| 49 | |||
| 50 | the method C<Display> takes $tmpl, which is a variable pool, and $opref, | ||
| 51 | which is hash of mapping from 'op' to the subroutine for that 'op'. | ||
| 52 | |||
| 53 | The op routine fill in the $tmpl with the varialbes it would like the | ||
| 54 | variable pool to have, and returns the name of the template file that | ||
| 55 | would be used. or if the return value begins with '!', the variable pool | ||
| 56 | will be transit to the specified op again. | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | # --------------- | ||
| 61 | # Variable Fields | ||
| 62 | # --------------- | ||
| 63 | use fields qw/header template errormsg cache params extension filename/; | ||
| 64 | |||
| 65 | # ----------------- | ||
| 66 | # Package Constants | ||
| 67 | # ----------------- | ||
| 68 | use constant LANGS => [{'zh-tw', 1, 'en-us', 2}, 'big5', 'iso-8859-1']; | ||
| 69 | |||
| 70 | # ---------------------- | ||
| 71 | # Subroutine new(%param) | ||
| 72 | # ---------------------- | ||
| 73 | sub new { | ||
| 74 | my $class = shift; | ||
| 75 | my $self = ($] > 5.00562) ? fields::new($class) | ||
| 76 | : do { no strict 'refs'; | ||
| 77 | bless [\%{"$class\::FIELDS"}], $class }; | ||
| 78 | |||
| 79 | $self->{'cache'} = (defined $ENV{'GATEWAY_INTERFACE'} and | ||
| 80 | $ENV{'GATEWAY_INTERFACE'} =~ m/PerlEx|CGI-Perl/); | ||
| 81 | |||
| 82 | %{$self->{'params'}} = @_; | ||
| 83 | |||
| 84 | return $self; | ||
| 85 | } | ||
| 86 | |||
| 87 | # ----------------------- | ||
| 88 | # Subroutine show($self) | ||
| 89 | # ----------------------- | ||
| 90 | # Outputs the whole page. | ||
| 91 | # ----------------------- | ||
| 92 | sub show { | ||
| 93 | my $self = shift; | ||
| 94 | |||
| 95 | if (my $lang = $self->dotemplate and $Backend eq 'HTML::Template') { | ||
| 96 | foreach my $key (%{$self->{'params'}}) { | ||
| 97 | if (UNIVERSAL::isa($key, "HASH") and exists($key->{$lang})) { | ||
| 98 | $key = $key->{$lang}; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | $self->{'template'}->param(%{$self->{'params'}}); | ||
| 102 | } | ||
| 103 | |||
| 104 | if (UNIVERSAL::isa($self, __PACKAGE__) and $self->{'template'}) { | ||
| 105 | print CGI->header($self->{'header'}); | ||
| 106 | if ($Backend eq 'HTML::Template') { | ||
| 107 | print $self->{'template'}->output; | ||
| 108 | } | ||
| 109 | else { | ||
| 110 | $self->{'template'}->process($self->{'filename'}, $self->{'params'})|| die $self->{'template'}->error(); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | # --------------------------------------------------------- | ||
| 116 | # Subroutine dotemplate($self) | ||
| 117 | # --------------------------------------------------------- | ||
| 118 | # Determines and populates the template file/string/object. | ||
| 119 | # --------------------------------------------------------- | ||
| 120 | sub dotemplate { | ||
| 121 | my $self = shift; | ||
| 122 | my $CGIOBJ = CGI->new(); | ||
| 123 | my ($path, $info, $dir, $filename, $extension); | ||
| 124 | |||
| 125 | $extension = $self->{'extension'}; | ||
| 126 | |||
| 127 | if (defined $self->{'template'}) { | ||
| 128 | if (UNIVERSAL::isa($self->{'template'}, 'Template')) { | ||
| 129 | # Template object already specified: populate CGI if must | ||
| 130 | push(@{$self->{'template'}->{'options'}{'associate'}}, $CGIOBJ); | ||
| 131 | } | ||
| 132 | elsif (UNIVERSAL::isa($self->{'template'}, 'HTML::Template')) { | ||
| 133 | # Template object already specified: populate CGI if must | ||
| 134 | push(@{$self->{'template'}->{'options'}{'associate'}}, $CGIOBJ); | ||
| 135 | } | ||
| 136 | elsif (!ref($self->{'template'})) { | ||
| 137 | # It's a file name, so put into $info | ||
| 138 | $info = $self->{'template'}; | ||
| 139 | $self->{'template'} = undef; | ||
| 140 | $path = $CGIOBJ->path_translated() || $0; | ||
| 141 | } | ||
| 142 | else { | ||
| 143 | # Something else... | ||
| 144 | die "Unknown type of 'template' field (". | ||
| 145 | ref($self->{'template'}). | ||
| 146 | ")"; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | else { | ||
| 150 | $path = $CGIOBJ->path_translated() || $0; | ||
| 151 | } | ||
| 152 | |||
| 153 | if (($dir, $filename) = $path =~ m|(.*[\\/])?(.*)\.|) { | ||
| 154 | $dir ||= ''; | ||
| 155 | $dir =~ tr|\\|/|; | ||
| 156 | $filename = $info if $info; | ||
| 157 | $filename =~ tr|\\|/|; | ||
| 158 | |||
| 159 | foreach my $lang (map( { ".$_" } $CGIOBJ->param('lang')), '') { | ||
| 160 | if (-e ($dir.$filename.$extension.$lang)) { | ||
| 161 | $self->{'header'} = "text/html"; | ||
| 162 | $self->{'header'}.= "; charset=" . LANGS->{substr($lang, 1)} | ||
| 163 | if exists LANGS->{substr($lang, 1)}; | ||
| 164 | |||
| 165 | if ($Backend eq 'HTML::Template') { | ||
| 166 | require HTML::Template; | ||
| 167 | $self->{'template'} = HTML::Template->new( | ||
| 168 | filename => $dir.$filename.$extension.$lang, | ||
| 169 | associate => $CGIOBJ, | ||
| 170 | cache => $self->{'cache'} | ||
| 171 | ); | ||
| 172 | } | ||
| 173 | else { | ||
| 174 | require Template; | ||
| 175 | $self->{'template'} = Template->new( | ||
| 176 | INCLUDE_PATH => $dir, | ||
| 177 | INTERPOLATE => 1, | ||
| 178 | POST_CHOMP => 1, | ||
| 179 | ); | ||
| 180 | $self->{'filename'} = $filename.$extension.$lang; | ||
| 181 | } | ||
| 182 | |||
| 183 | return $lang ? substr($lang, 1) : 1; | ||
| 184 | } | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | $info = $CGIOBJ->path_info() || $filename.'.pl'; | ||
| 189 | |||
| 190 | if (my @list = <$dir$filename$extension.??-??> and (-e $dir.'error_lang')) { | ||
| 191 | $self->{'header'} = "Content-Type: text/html; charset=iso-8859-1"; | ||
| 192 | |||
| 193 | if ($Backend eq 'HTML::Template') { | ||
| 194 | require HTML::Template; | ||
| 195 | $self->{'template'} = HTML::Template->new( | ||
| 196 | filename => $dir.'error_lang', | ||
| 197 | associate => $CGIOBJ, | ||
| 198 | cache => $self->{'cache'} | ||
| 199 | ); | ||
| 200 | } | ||
| 201 | else { | ||
| 202 | require Template; | ||
| 203 | $self->{'template'} = Template->new( | ||
| 204 | INCLUDE_PATH => $dir, | ||
| 205 | INTERPOLATE => 1, | ||
| 206 | POST_CHOMP => 1, | ||
| 207 | ); | ||
| 208 | |||
| 209 | $self->{'filename'} = 'error_lang'; | ||
| 210 | } | ||
| 211 | |||
| 212 | |||
| 213 | @{$self->{'params'}}{qw/title url langs/} = ( | ||
| 214 | 'Available Languages for: '.$CGIOBJ->param('lang'), | ||
| 215 | $info, | ||
| 216 | [map {s|^.*\.||; {code => $_, url => $info}} @list], | ||
| 217 | ); | ||
| 218 | |||
| 219 | return 0; | ||
| 220 | } | ||
| 221 | elsif (-e $dir.'error_url') { | ||
| 222 | if ($Backend eq 'HTML::Template') { | ||
| 223 | require HTML::Template; | ||
| 224 | $self->{'template'} = HTML::Template->new( | ||
| 225 | filename => $dir.'error_url', | ||
| 226 | associate => $CGIOBJ, | ||
| 227 | cache => $self->{'cache'} | ||
| 228 | ); | ||
| 229 | } | ||
| 230 | else { | ||
| 231 | require Template; | ||
| 232 | $self->{'template'} = Template->new( | ||
| 233 | INCLUDE_PATH => $dir, | ||
| 234 | INTERPOLATE => 1, | ||
| 235 | POST_CHOMP => 1, | ||
| 236 | ); | ||
| 237 | |||
| 238 | $self->{'filename'} = 'error_url'; | ||
| 239 | } | ||
| 240 | |||
| 241 | @{$self->{'params'}}{qw/title url/} = ( | ||
| 242 | 'Address error', $path, | ||
| 243 | ); | ||
| 244 | |||
| 245 | return 0; | ||
| 246 | } | ||
| 247 | else { | ||
| 248 | $self->{'header'} = "text/plain"; | ||
| 249 | print qq{ | ||
| 250 | A Terrible error happened when parsing $info. | ||
| 251 | |||
| 252 | Worse yet, nobody is there to help you. | ||
| 253 | |||
| 254 | Please mail your complaints to <autrijus\@autrijus.org>. | ||
| 255 | |||
| 256 | With sincere apologies, | ||
| 257 | |||
| 258 | /Autrijus/ | ||
| 259 | }; | ||
| 260 | exit; | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 | # Display($tmpl_param, $ophashref, [$fail_url], [$session_db, $flavor]) | ||
| 265 | sub Display { | ||
| 266 | my $tmpl_param = shift; | ||
| 267 | my $ophashref = shift; | ||
| 268 | my $fail_url = shift; | ||
| 269 | my $session_db = shift; | ||
| 270 | my $flavor = shift || 'WEBBUILDERID'; | ||
| 271 | my $op = CGI::param('op'); | ||
| 272 | my $user; | ||
| 273 | |||
| 274 | if ($op and $session_db) { | ||
| 275 | my $cookie; | ||
| 276 | |||
| 277 | if (CGI->path_translated()) { | ||
| 278 | my %cookies = CGI::Cookie->fetch; | ||
| 279 | $cookie = $cookies{$flavor} if exists $cookies{$flavor}; | ||
| 280 | } | ||
| 281 | else { | ||
| 282 | $cookie = CGI::Cookie->new(-value => 'localhost'); | ||
| 283 | } | ||
| 284 | |||
| 285 | if (exists $session_db->{$cookie->value}) { | ||
| 286 | my $sescook = CGI::Cookie->new(-name => $flavor, | ||
| 287 | -value => $cookie->value, | ||
| 288 | -expires => '+1h', | ||
| 289 | -domain => $cookie->domain); | ||
| 290 | |||
| 291 | print "Set-Cookie: $sescook\n"; | ||
| 292 | |||
| 293 | $user = $session_db->{$cookie->value}; | ||
| 294 | } | ||
| 295 | else { | ||
| 296 | print CGI->header(-location => ($fail_url || '/')); | ||
| 297 | return; | ||
| 298 | } | ||
| 299 | } | ||
| 300 | |||
| 301 | unless (exists $ophashref->{$op}) { | ||
| 302 | # No such page; panic! | ||
| 303 | print CGI->header(-location => ($fail_url || '/')); | ||
| 304 | return; | ||
| 305 | } | ||
| 306 | |||
| 307 | # Found a page | ||
| 308 | my $ext = do{&{$ophashref->{$op}}($tmpl_param, $user)}; | ||
| 309 | |||
| 310 | while (substr($ext, 0, 1) eq '!') { | ||
| 311 | $op = substr($ext, 1); | ||
| 312 | $ext = eval{&{$ophashref->{$op}}($tmpl_param, $user)}; | ||
| 313 | } | ||
| 314 | |||
| 315 | if ($@) { | ||
| 316 | print CGI->header; | ||
| 317 | print "Error occured! Op=$op Ext=$ext Errors=$@ $! $^E $? "; | ||
| 318 | return; | ||
| 319 | } | ||
| 320 | |||
| 321 | my $page = OurNet::WebBuilder->new(%{$tmpl_param}); | ||
| 322 | |||
| 323 | $page->{'extension'} = $ext ? ".$ext.w" : '.w'; | ||
| 324 | $page->show; | ||
| 325 | } | ||
| 326 | |||
| 327 | sub SetCookie { | ||
| 328 | my $flavor = shift || 'WEBBUILDERID'; | ||
| 329 | my $sescook = CGI::Cookie->new(-name => $flavor, | ||
| 330 | -value => crypt(time, substr(CGI::remote_host(), -2)), | ||
| 331 | -expires => '+1h'); | ||
| 332 | |||
| 333 | print "Set-Cookie: $sescook\n"; | ||
| 334 | return (CGI::path_translated()) ? $sescook->value : 'localhost'; | ||
| 335 | } | ||
| 336 | |||
| 337 | |||
| 338 | 1; | ||
| 339 | |||
| 340 | =head1 AUTHORS | ||
| 341 | |||
| 342 | Autrijus Tang E<lt>autrijus@autrijus.org> | ||
| 343 | |||
| 344 | =head1 COPYRIGHT | ||
| 345 | |||
| 346 | Copyright 2000 by Autrijus Tang E<lt>autrijus@autrijus.org>. | ||
| 347 | |||
| 348 | All rights reserved. You can redistribute and/or modify | ||
| 349 | this module under the same terms as Perl itself. | ||
| 350 | |||
| 351 | =cut |