2dd77470bc95eec121632e68e7df4aca3c1db4c4
[MyWeb.git] / Web.pm
1 # $Id$
2 # Common functions for HTML/XHTML output generation
3 # Copyright (C) 2003 Jan Kratochvil <project-www.jankratochvil.net@jankratochvil.net>
4
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; exactly version 2 of June 1991 is required
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18
19 package My::Web;
20 require 5.6.0;  # at least 'use warnings;' but we need some 5.6.0+ modules anyway
21 our $VERSION=do { my @r=(q$Revision$=~/\d+/g); sprintf "%d.".("%03d"x$#r),@r; };
22 our $CVS_ID=q$Id$;
23 use strict;
24 use warnings;
25
26 use Exporter;
27 sub Wrequire ($);
28 sub Wuse ($@);
29 our $W;
30 our @EXPORT=qw(&Wrequire &Wuse &a_href &a_href_cz &vskip &img &centerimg &rightimg $W &top_dir &top_dir_disk &Wprint);
31 our @ISA=qw(Exporter);
32
33 BEGIN
34 {
35         $W->{"__My::Web_init"}=1;
36
37         sub Wrequire ($)
38         {
39         my($file)=@_;
40
41 #               print STDERR "Wrequire $file\n";
42                 $file=~s#/#::#g;
43                 $file=~s/[.]pm$//;
44                 my $class=$file;
45                 $file=~s#::#/#g;
46                 $file.=".pm";
47                 my $who=$W->{"__PACKAGE__"};
48                 $who||="__My::Web" if $W->{"__My::Web_init"};
49                 my $aref=($W->{"packages_used"}{$who}||=[]);
50                 push @$aref,$class
51                                 if !{ map(($_=>1),@$aref) }->{$class};  # Prevent duplicated entries.
52                 CORE::require $file;
53                 1;      # Otherwise 'require' would already file above.
54         }
55
56         sub Wuse ($@)
57         {
58         my($file,@list)=@_;
59
60 #               print STDERR "Wuse $file\n";
61                 Wrequire $file;
62                 local $Exporter::ExportLevel=$Exporter::ExportLevel+1;
63                 $file->import(@list);
64                 1;
65         }
66 }
67
68 BEGIN { Wuse 'WebConfig'; }     # for %WebConfig
69 require CGI;    # for &escapeHTML
70 require Image::Size;    # for &imgsize
71 use File::Basename;     # &basename
72 use Carp qw(cluck confess);
73 use URI::Escape;
74 require HTTP::BrowserDetect;
75 require HTTP::Negotiate;
76 my $have_Geo_IP; BEGIN { $have_Geo_IP=eval { require Geo::IP; 1; }; }
77 use ModPerl::Util qw(exit);
78 use POSIX qw(strftime);
79
80
81 #our $W;
82                 # $W->{"title"}
83                 # $W->{"head"}
84                 # $W->{"head_css"}
85                 # $W->{"force_charset"}
86                 # $W->{"heading_done"}
87                 # $W->{"footer_passed"}
88                 # %{$W->{"headers"}}
89                 # %{$W->{"headers_lc"}} # maps lc($headers_key)=>$headers_key
90                 # @{$W->{"packages_used"}{$W->{"__PACKAGE__"}}}
91                 # @{$W->{"packages_used"}{"__My::Web"}}
92                 # %{$W->{"args"}}
93
94 sub init ($%)
95 {
96 my($class,%args)=@_;
97
98         print STDERR "$class->init ".Apache->request()->unparsed_uri()."\n";
99
100         my $packages_used_save=$W->{"packages_used"};
101         $W={ %WebConfig,%args };        # override %WebConfig settings
102         $W->{"packages_used"}=$packages_used_save;
103
104         # {"__PACKAGE__"} is mandatory for mod_perl-2.0;
105         # $Apache::Registry::curstash is no longer supported.
106         do { cluck "No $_" if !$W->{$_}; } for "__PACKAGE__";
107
108         $W->{"top_dir"}||=eval '$'.$W->{"__PACKAGE__"}.'::top_dir';
109
110         do { $W->{$_}=0  if !defined $W->{$_}; } for ("detect_ent");
111         do { $W->{$_}=0  if !defined $W->{$_}; } for ("detect_js");
112         do { $W->{$_}=1  if !defined $W->{$_}; } for ("have_css");      # AFAIK it does not hurt anyone.
113         do { $W->{$_}=1  if !defined $W->{$_}; } for ("heading");
114         do { $W->{$_}=1  if !defined $W->{$_}; } for ("footer");
115         do { $W->{$_}=1  if !defined $W->{$_}; } for ("footer_delimit");
116         do { $W->{$_}=1  if !defined $W->{$_}; } for ("footer_mailme");
117         do { $W->{$_}=1  if !defined $W->{$_}; } for ("footer_ids");
118         do { $W->{$_}=1  if !defined $W->{$_}; } for ("indexme");
119         do { $W->{$_}="" if !defined $W->{$_}; } for ("head");
120         do { $W->{$_}="" if !defined $W->{$_}; } for ("head_css");
121
122         my $footer_any=0;
123         for (qw(footer_mailme footer_ids)) {
124                 $W->{$_}=0 if !$W->{"footer"};
125                 $footer_any=1 if $W->{$_};
126                 }
127         $W->{"footer"}=0 if !$footer_any;
128         $W->{"footer_delimit"}=0 if !$W->{"footer"};
129
130         $W->{"r"}=Apache->request();
131
132         $W->{"QUERY_STRING"}=$W->{"r"}->args() || "";
133            if ($W->{"QUERY_STRING"}=~/[&]amp;have_ent/)
134                 { $W->{"have_ent"}=0; }
135         elsif ($W->{"QUERY_STRING"}=~    /[&]have_ent/)
136                 { $W->{"have_ent"}=1; }
137         else
138                 { delete $W->{"have_ent"}; }
139         if ($W->{"detect_ent"} && !defined $W->{"have_ent"} && $W->{"r"}->method() eq "GET") {
140                 $W->{"head"}.='<meta http-equiv="Refresh" content="0; URL='
141                                 .CGI::escapeHTML("http://".&{$W->{"web_hostname_sub"}}()."/".($W->{"r"}->uri()=~m#^/*(.*)$#)[0]
142                                                 ."?".($W->{"QUERY_STRING"} || "detect_ent_glue=1").'&have_ent=detect')
143                                 .'" />'."\n";
144                 }
145         $W->{"QUERY_STRING"}=~s/([&])amp;/$1/g;
146         $W->{"r"}->args($W->{"QUERY_STRING"});
147         # Do not: $W->{"r"}->args()
148         # as it parses only QUERY_STRING (not POST data).
149         $W->{"args"}={ CGI->new($W->{"r"})->Vars() };
150         for (keys(%{$W->{"args"}})) {
151                 my @vals=split /\x00/,$W->{"args"}{$_};
152                 next if @vals<=1;
153                 $W->{"args"}{$_}=[@vals];
154                 }
155
156         do { $W->{$_}=$W->{"r"}->headers_in()->{"Accept"}     if !defined $W->{$_}; } for ("accept");
157         do { $W->{$_}=$W->{"r"}->headers_in()->{"User-Agent"} if !defined $W->{$_}; } for ("user_agent");
158
159         $W->{"browser"}=HTTP::BrowserDetect->new($W->{"user_agent"});
160
161         if (!defined $W->{"have_style"}) {
162                 $W->{"have_style"}=(!$W->{"browser"}->netscape() || ($W->{"browser"}->major() && $W->{"browser"}->major()>4) ? 1 : 0);
163                 }
164
165         $W->{"have_js"}=($W->{"args"}{"have_js"} ? 1 : 0);
166         if ($W->{"detect_js"} && !$W->{"have_js"}) {
167                 $W->{"head"}.='<script type="text/javascript" src="'.top_dir('/have_js.js.pl').'"></script>'."\n";
168                 }
169
170         do { args_check(%$_) if $_; } for ($W->{"args_check"});
171
172         $ENV{"HOSTNAME"}||=&{$W->{"web_hostname_sub"}}();
173
174         return $W;
175 }
176
177 sub Wprint($)
178 {
179 my($text)=@_;
180
181         $W->{"r"}->print($text);
182 }
183
184 sub top_dir_disk ()
185 {
186         do { return $_ if $_; } for ($W->{"top_dir"});
187         return $INC[0]; # fallback
188 }
189
190 sub top_dir (;$)
191 {
192 my($in)=@_;
193
194         if (my $uri=$W->{"r"}->unparsed_uri()) {
195                 if ($W->{"args"}{"Wabs"}) {
196                         # FIXME: $in may not be defined here!
197                         # to prevent: Use of uninitialized value in ...
198                         if ($in=~m#^/#) {
199                                 $in=~s#^/*##;
200                                 }
201                         else {
202                                 $in=$uri."/".$in;
203                                 $in=~tr#/#/#s;
204                                 1 while $in=~s#/(?:[^/]+)/\Q..\E/#/#g
205                                 }
206                         return "http://".&{$W->{"web_hostname_sub"}}()."/".(defined $in ? $in : "");
207                         }
208                 $uri.="Index" if $uri=~m#/$#;
209                 if (defined $in) {
210                         my($inpath,$inquery)=split /[?]/,$in,2;
211                         $inpath=~tr///cs;
212                         $uri=~tr///cs;
213                         for (;;) {
214                                 my($in1 ,$in2 )=($in =~m#^(/[^/]+)(/.*)$#);
215                                 my($uri1,$uri2)=($uri=~m#^(/[^/]+)(/.*)$#);
216                                 last if !defined $in1 || !defined $uri1 || $in1 ne $uri1;
217                                 $in=$in2;
218                                 $uri=$uri2;
219                                 }
220                         }
221                 $uri=~s#^/*##;
222                 $uri=~s#[^/]+#..#g;
223                 $uri=File::Basename::dirname($uri);
224                 my $r=$uri.(defined $in ? $in : "");
225 #               1 while $r=~s#^[.]/##;
226 #               $r="./$r" if $r=~m#^(?:?.*)$#;  # empty pathname?
227                 return $r;
228                 }
229         return top_dir_disk().$in;
230 }
231
232 sub fatal (;$);
233
234 sub args_check (%)
235 {
236 my(%tmpl)=@_;
237
238         while (my($name,$regex)=each(%tmpl)) {
239                 my $name_html="Parameter <span class=\"quote\">".CGI::escapeHTML($name)."</span>";
240                 $W->{"args"}{$name}="" if !defined $W->{"args"}{$name};
241                 $W->{"args"}{$name}=[ $W->{"args"}{$name} ] if !ref $W->{"args"}{$name} && ref $regex;
242                 fatal "$name_html passed as multivar although singlevar expected"
243                                 if ref $W->{"args"}{$name} && !ref $regex;
244                 $regex=$regex->[0] if ref $regex;
245                 for my $val (!ref $W->{"args"}{$name} ? $W->{"args"}{$name} : @{$W->{"args"}{$name}}) {
246                         $val="" if !defined $val;
247                         fatal "$name_html <span class=\"quote\">".CGI::escapeHTML($val)."</span>"
248                                                         ." does not match the required regex <span class=\"quote\">".CGI::escapeHTML($regex)."</span> "
249                                         if $regex ne "" && $val!~/$regex/;
250                         }
251                 }
252 }
253
254 sub vskip (;$)
255 {
256 my($height)=@_;
257
258         return '<p'.(!defined $height ? "" : ' style="height: '.$height.';"').'>&nbsp;</p>'."\n";
259 }
260
261 sub fatal (;$)
262 {
263 my($msg)=@_;
264
265         $msg="UNKNOWN" if !$msg;
266
267         $W->{"indexme"}=0;      # For the case no heading was sent yet.
268         My::Web->heading();
269         Wprint "\n".vskip("3ex")."<hr /><h1 class=\"error\">FATAL ERROR: $msg!</h1>\n"
270                         ."<p>You can report this problem's details to"
271                         ." ".a_href("mailto:".$W->{"admin_mail"},"admin of this website").".</p>\n";
272         footer();
273 }
274
275 sub footer (;$)
276 {
277         exit 1 if $W->{"footer_passed"}++;      # deadlock prevention:
278
279         Wprint vskip if $W->{"footer_delimit"};
280
281         if ($W->{"heading"}) {
282                 do { &{$_}() if $_; } for ($W->{"footing_delimit"});
283                 }
284
285         Wprint "<hr />\n" if $W->{"footer"};
286
287         if ($W->{"footer_mailme"}) {
288                 Wprint '<form action="'.top_dir('/SendMsg.pl').'" method="post" onsubmit="'
289                                 ."this.elements['msgscript'].value=this.elements['msghtml'].value;"
290                                 ."this.elements['msghtml'].value='';"
291                                 ."this.submit();"
292                                 .'">'."\n";
293                         Wprint '<p align="right">'."\n";
294                                 Wprint '<input name="msgscript" type="hidden" />'."\n";
295                                 Wprint '<input name="msghtml" type="text" size="32" alt="Message" />'."\n";
296                                 Wprint '<input name="submit" type="submit" value="Quick Note" />'."\n";
297                         Wprint '</p>'."\n";
298                 Wprint '</form>'."\n";
299                 }
300
301         my @packages_used=(
302                         $W->{"__PACKAGE__"},
303                         __PACKAGE__,
304                         @{$W->{"packages_used"}{"__My::Web"}},
305                         map((!$_ ? () : @$_),$W->{"packages_used"}{$W->{"__PACKAGE__"}}),
306                         );
307         my %packages_used;
308         @packages_used=grep((!$packages_used{$_}++),@packages_used);
309         if ($W->{"footer_ids"}) {
310                 Wprint '<p class="cvs-id">';
311                 Wprint join("<br />\n",map({ my $package=$_;
312                         my $cvs_id=(eval('$'.$package."::CVS_ID")
313 #                                       || $package     # debug
314                                         );
315                         if (!$cvs_id) {
316                                 ();
317                                 }
318                         else {
319                                 $cvs_id='$'.$cvs_id.'$';        # Eaten by 'q' operator.
320                                 my @cvs_id_split=split / +/,$cvs_id;
321                                 if (@cvs_id_split==8) {
322                                         my $file=$package;
323                                         $file=~s#::#/#g;
324                                         my $ext;
325                                         my @tried;
326                                         for (qw(.html.pl .pl .pm),"") {
327                                                 $ext=$_;
328                                                 my $pathname=top_dir_disk()."/$file$ext";
329                                                 push @tried,$pathname;
330                                                 last if -r $pathname;
331                                                 cluck "Class file $file not found; tried: ".join(" ",@tried) if !$ext;
332                                                 }
333                                         $file.=$ext;
334                                         $cvs_id_split[2]=""
335                                                         .a_href((map({ my $s=$_; $s=~s#/viewcvs/#$&~checkout~/#; $s; } $W->{"viewcvs"}))[0]."$file?rev=".$cvs_id_split[2],
336                                                                         $cvs_id_split[2]);
337                                         $cvs_id_split[1]=a_href($W->{"viewcvs"}.$file,
338                                                         ($package!~/^Apache::/ ? $package : $cvs_id_split[1]));
339                                         $cvs_id_split[5]=&{$W->{"cvs_id_author"}}($cvs_id_split[5]);
340                                         }
341                                 join " ",@cvs_id_split;
342                                 }
343                         } @packages_used));
344                 Wprint "</p>\n";
345                 }
346
347         for my $package (@packages_used) {
348                 my $cvs_id=(eval('$'.$package."::CVS_ID")
349 #                               || $package     # debug
350                                 );
351                 Wprint '<!-- '.$package.' - $'.$cvs_id.'$ -->'."\n" if $cvs_id;
352                 }
353
354         if ($W->{"heading"}) {
355                 do { &{$_}() if $_; } for ($W->{"footing"});
356                 }
357
358         Wprint "</body></html>\n";
359         exit(0);
360 }
361
362 sub header (%)
363 {
364 my(%pairs)=@_;
365
366         while (my($key,$val)=each(%pairs)) {
367                 do { cluck "Headers already sent"; next; } if $W->{"heading_done"};
368                 for ($W->{"headers_lc"}{lc $key} || ()) {
369                         delete $W->{"headers"}{$_};
370                         }
371                 $W->{"headers_lc"}{lc $key}=$key;
372                 $W->{"headers"}{$key}=$val;
373                 }
374 }
375
376 sub size_display ($)
377 {
378 my($size)=@_;
379
380            if ($size<4096)
381                 {}
382         elsif ($size<1024*1024)
383                 { $size=sprintf "%.1fK",$size/1024; }
384         else
385                 { $size=sprintf "%.1fM",$size/1024/1024; }
386         $size.="B";
387         return $size;
388 }
389
390 sub url_is_local ($)
391 {
392 my($url)=@_;
393
394         return $url!~m#^[a-z]+://#;
395 }
396
397 sub a_href ($;$%)
398 {
399 my($url,$contents,%args)=@_;
400
401         do { $$_=1 if !defined $$_; } for (\$args{"size"});
402         if (!defined $contents) {
403                 $contents=$url;
404                 $contents=File::Basename::basename($contents) if $args{"basename"};
405                 $contents=CGI::escapeHTML($contents);
406                 }
407         $contents=~s#<a\b[^>]*>##gi;
408         $contents=~s#</a>##gi;
409
410         $url=top_dir($url) if url_is_local $url && $url=~m#^/#;
411
412         my $r='<a href="';
413         my $urlent=CGI::escapeHTML($url);
414            if ($url eq $urlent)
415                 { $r.=$url; }
416         elsif (url_is_local $url)
417                 { $r.=$urlent; }
418         elsif (defined $W->{"have_ent"} && !$W->{"have_ent"})   # non-ent client
419                 { $r.=$url; }
420         elsif ($W->{"have_ent"})        # ent client
421                 { $r.=$urlent; }
422         else    # unknown client, &CGI::escapeHTML should not be needed here
423                 { $r.=CGI::escapeHTML(top_dir('/Redirect.pl?location='.uri_escape($url))); }
424         $r.='"';
425         do { $r.=" $_" if $_; } for ($args{"attr"});
426         $r.='>'.$contents.'</a>';
427         if ($args{"size"} && url_is_local($url) && ($args{"size"}>=2 || $url=~/[.](?:gz|Z|rpm|zip|deb|lha)/)) { # Downloadable?
428                 $url=top_dir_disk().$url if $url=~m#^/#;
429                 if (!-r $url)
430                         { cluck "File not readable: $url"; }
431                 else {
432                         $r.='&nbsp;('.size_display((stat($url))[7]).')';
433                         }
434                 }
435         return $r;
436 }
437
438 sub remote_ip ()
439 {
440         # Do not: PerlModule                 Apache::ForwardedFor
441         #         PerlPostReadRequestHandler Apache::ForwardedFor
442         # As 'Apache::ForwardedFor' takes the first of $ENV{"HTTP_X_FORWARDED_FOR"}
443         # while the contents is '127.0.0.1, 213.220.195.171' if client has its own proxy.
444         # We must take the last item ourselves.
445         my $r=$W->{"r"}->headers_in()->{"X-Forwarded-For"} || $W->{"r"}->get_remote_host();
446         $r=~s/^.*,\s*//;
447         return $r;
448 }
449
450 sub is_cz ()
451 {
452         return 0 if !$have_Geo_IP;
453         return "CZ" eq Geo::IP->new()->country_code_by_addr(remote_ip());
454 }
455
456 sub a_href_cz ($$;%)
457 {
458 my($url,$contents,%args)=@_;
459
460         return a_href $url,$contents,%args if is_cz();
461         return $contents;
462 }
463
464 sub make ($)
465 {
466 my($cmd)=@_;
467
468         system {'flock'} 'flock','-x',top_dir_disk(),$cmd.' >&2';
469 }
470
471 sub img_size ($$)
472 {
473 my($width,$height)=@_;
474
475         return ($W->{"have_style"} ? "style=\"border:0;width:${width}px;height:${height}px\"" : "border=\"0\"")
476                         ." width=\"$width\" height=\"$height\"";
477 }
478
479 sub negotiate_variant (%)
480 {
481 my(%args)=@_;
482
483         my @fields=("id","qs","content-type","encoding","charset","lang","size");
484         return [ map(($args{$_}),@fields) ];
485 }
486
487 my @img_variants=(
488                 { "id"=>"png","qs"=>1.0,"content-type"=>"image/png" },
489                 { "id"=>"gif","qs"=>0.9,"content-type"=>"image/gif" },
490                 );
491 my $img_variants_re='[.](?:'.join('|',"jpeg",map(($_->{"id"}),@img_variants)).')$';
492
493 sub img_src ($)
494 {
495 my($file_base)=@_;
496
497         if (!url_is_local($file_base)) {
498                 return $file_base if !wantarray();
499                 return ($file_base,$file_base);
500                 }
501         # Known image extension?
502         if ($file_base=~m#$img_variants_re#o) {
503                 return $file_base if !wantarray();
504                 return ($file_base,$file_base) if $file_base!~m#^/#;
505                 return (top_dir($file_base),top_dir_disk().$file_base);
506                 }
507
508         my $file_base_disk;
509         my $file_base_uri;
510         if ($file_base!~m#^/#) {
511                 $file_base_disk=$file_base_uri=$file_base;
512                 }
513         else {
514                 $file_base_disk=top_dir_disk().$file_base;
515                 $file_base_uri=top_dir($file_base);
516                 }
517
518         my @nego_variants;
519         for my $var (@img_variants) {
520                 my $file=$file_base_disk.".".$var->{"id"};
521                 # TODO: Somehow quickly check dependencies?
522                 make('make -s --no-print-directory'
523                                                 .' -C '."'".File::Basename::dirname($file)."' '".File::Basename::basename($file)."'")
524                                 if !-f $file;
525                 push @nego_variants,negotiate_variant(
526                                 %$var,
527                                 "size"=>(stat $file)[7],
528                                 );
529                 }
530         # Do not: ,$W->{"r"});
531         # but should we provide somehow either 'HTTP::Headers' or 'HTTP::Request' ?
532         my $ext=HTTP::Negotiate::choose(\@nego_variants);
533         $ext||=$img_variants[0]->{"id"};        # &HTTP::Negotiate::choose failed?
534
535         return $file_base_uri.".".$ext if !wantarray();
536         return ($file_base_uri.".".$ext,$file_base_disk.".".$ext);
537 }
538
539 sub img ($$;%)
540 {
541 my($file_base,$alt,%attr)=@_;
542
543         my($file_uri,$file_disk)=img_src $file_base;
544         my($width,$height)=Image::Size::imgsize($file_disk);
545         $alt=~s/<[^>]*>//g;
546         $alt=CGI::escapeHTML($alt);
547         my $content="<img src=\"$file_uri\" alt=\"$alt\" title=\"$alt\" ".img_size($width,$height)
548                         .(!$attr{"attr"} ? "" : " ".$attr{"attr"})." />";
549         return a_href img_src($attr{"a_href_img"}),$content if $attr{"a_href_img"};
550         return a_href $attr{"a_href"},$content if $attr{"a_href"};
551         return $content;
552 }
553
554 sub centerimg
555 {
556         my $r.="";
557         $r.='<table border="0" width="100%"><tr>'."\n";
558         @_=( [@_] ) if !ref $_[0];
559         for (@_) {
560                 $r.="\t".'<td align="center">'.&{\&img}(@$_).'</td>'."\n";
561                 }
562         $r.='</tr></table>'."\n";
563         return $r;
564 }
565
566 sub rightimg
567 {
568 my($text,@args_img)=@_;
569
570         # Workaround bug of 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)':
571         return <<"HERE";
572 <table border="0" width="100%">
573         <col width="@{[ (!$W->{"browser"}->ie() ? "1*" : "90%" ) ]}" />
574         <col width="@{[ (!$W->{"browser"}->ie() ? "0*" : "10%" ) ]}" />
575         <tr>
576                 <td align="left">
577                         @{[ $text ]}
578                 </td>
579                 <td align="right">
580                         @{[ &{\&img}(@args_img) ]}
581                 </td>
582         </tr>
583 </table>
584 HERE
585 }
586
587 sub readfile ($$)
588 {
589 my($class,$filename)=@_;
590
591         local *F;
592         open F,$filename or die "Cannot open \"$filename\": $!";
593         local $/=undef();
594         my $data=<F>;
595         close F;
596         return $data;
597 }
598
599 sub arr_keys (@)
600 {
601 my(@arr)=@_;
602
603         my @r=();
604         while (@arr) {
605                 push @r,shift @arr;     # key
606                 shift @arr;     # val
607                 }
608         return @r;
609 }
610
611 sub no_cache($)
612 {
613 my($self)=@_;
614
615         header("Expires"=>"Mon, 26 Jul 1997 05:00:00 GMT");     # date in the past
616         header("Last-Modified"=>strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime()));        # always modified
617         header("Cache-Control"=>"no-cache, must-revalidate");   # HTTP/1.1
618         header("Pragma"=>"no-cache");   # HTTP/1.0
619 }
620
621 sub heading ()
622 {
623 my($class)=@_;
624
625         return if $W->{"heading_passed"}++;
626
627         # $ENV{"CLIENT_CHARSET"} ignored (mod_czech support dropped!)
628         my $client_charset=$W->{"force_charset"} || "us-ascii";
629         header("Content-Style-Type"=>"text/css");
630         header("Content-Script-Type"=>"text/javascript");
631         $class->no_cache() if $W->{"no_cache"};
632
633         while (my($key,$val)=each(%{$W->{"headers"}})) {
634                 $W->{"r"}->header_out($key,$val);
635                 }
636         $W->{"r"}->send_http_header("text/html; charset=$client_charset");      # "Content-type"; do not use header()
637
638         return if $W->{"heading_done"}++;
639         exit if $W->{"r"}->header_only();
640
641         if (1) { # || !$msie_major || $msie_major>=4) # TODO:dyn
642                 Wprint '<?xml version="1.0" encoding="'.$client_charset.'"?>'."\n";
643                 }
644         Wprint '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'."\n";
645         Wprint '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">'."\n";
646         my $title=$W->{"title_prefix"}.join("",map({ ': '.$_; } ($W->{"title"} || ())));
647         $title=~s#<[^>]*>##g;
648         Wprint "<head>";
649         Wprint "<title>$title</title>\n";
650
651         if ($W->{"have_css"}) {
652                 Wprint <<'HERE';
653 <style type="text/css"><!--
654 .cvs-id   { font-family: monospace; }
655 .error    { color: red;   background-color: transparent; }
656 .quote    { font-family: monospace; }
657 .nowrap   { white-space: nowrap; }
658 .centered { text-align: center; }
659 .tab-bold { font-weight: bold; }
660 .tab-head { font-weight: bold; }
661 /*
662 .tab-head { font-weight: bold; color: yellow; background-color: transparent; }
663 body {
664                 background-color: black;
665                 color: white;
666                 }
667 :link    { color: aqua;   background-color: transparent; }
668 :visited { color: teal;   background-color: transparent; }
669 h1,h2    { color: yellow; background-color: transparent; }
670 */
671 td       { padding: 2px; }
672 caption  { caption-side: bottom; }
673 .footer img { vertical-align: middle; }
674 HERE
675                 Wprint $W->{"head_css"}."\n";
676                 Wprint "--></style>\n";
677                 }
678
679         Wprint '<meta name="robots" content="'.($W->{"indexme"} ? "" : "no" ).'index,follow" />'."\n";
680         Wprint $W->{"head"};
681         for my $type (qw(prev next index contents start up)) {
682                 do { Wprint '<link rel="'.$type.'" href="'.$_.'" />'."\n" if $_; } for ($W->{"rel_$type"});
683                 }
684         Wprint "</head><body";
685 #       Wprint ' bgcolor="black" text="white" link="aqua" vlink="teal"'
686 #                       if $W->{"browser"}->netscape() && (!$W->{"browser"}->major() || $W->{"browser"}->major()<=4);
687         do { &{$_}($W) if $_; } for $W->{"body_attr_sub"};
688         Wprint ">\n";
689
690         if ($W->{"heading"}) {
691                 do { &{$_}() if $_; } for ($W->{"heading"});
692                 }
693 }
694
695 BEGIN {
696         delete $W->{"__My::Web_init"};
697         }
698
699 1;