]> mj.ucw.cz Git - gallery.git/blob - gal/Gallery/Web.pm
Fix bugs caused by recent changes
[gallery.git] / gal / Gallery / Web.pm
1 # Simple Photo Gallery: Web Interface
2 # (c) 2003--2005 Martin Mares <mj@ucw.cz>
3
4 package Gallery::Web;
5 import Gallery qw(%CF);
6
7 use UCW::CGI;
8
9 my $show_img;
10 my $send_archive;
11
12 my %args = (
13         'i'     => { 'var' => \$show_img, 'check' => '\d+' },
14         'a'     => { 'var' => \$send_archive }
15 );
16
17 sub new() {
18         UCW::CGI::parse_args(\%args);
19         if ($send_archive && $CF{'AllowArchives'}) {
20                 require Gallery::Archive;
21                 return new Gallery::Archive($send_archive);
22         } else {
23                 return bless {};
24         }
25 }
26
27 sub error($) {
28         print "<p style='color:red'>Bad luck, the script is broken. Sorry.\n<p>$_[0]\n";
29         print "</body></html>\n";
30 }
31
32 our (@images, %widths, %heights, %widths_orig, %heights_orig, %imgno);
33
34 sub load_notes() {
35         @images = ("");
36         %widths = ();
37         %heights = ();
38         %imgno = ();
39         my $notes = $CF{'MetaDataDir'} . "/notes";
40         open NOTES, $notes or die "Unable to read notes ($notes): $!";
41         while (<NOTES>) {
42                 chomp;
43                 my ($n, $w, $h, $wo, $ho) = split /\s+/;
44                 push @images, $n;
45                 $widths{$n} = $w;
46                 $heights{$n} = $h;
47                 $widths_orig{$n} = $wo;
48                 $heights_orig{$n} = $ho;
49                 $imgno{$n} = $#images;
50         }
51         close NOTES;
52 }
53
54 sub show_links($$$) {
55         my ($prev, $up, $next) = @_;
56         print "<p class=parent>";
57         print "<span class=back style='width: $CF{'BackW'}px; height: $CF{'BackH'}px'>";
58         print "<a href='$prev'><img src='$CF{'BackImg'}' width=$CF{'BackW'} height=$CF{'BackH'} alt='Back'></a>" if $prev ne "";
59         print "</span>\n";
60         print "<span class=fwd style='width: $CF{'FwdW'}px; height: $CF{'FwdH'}px'>";
61         print "<a href='$next'><img src='$CF{'FwdImg'}' width=$CF{'FwdW'} height=$CF{'FwdH'} alt='Forward'></a>" if $next ne "";
62         print "</span>\n";
63         print "<a href='$up'><img src='$CF{'ParentImg'}' width=$CF{'ParentW'} height=$CF{'ParentH'} alt='Up'></a>\n" if $up ne "";
64 }
65
66 sub Start($) {
67         my $title = UCW::CGI::html_escape($CF{"Title"});
68         print <<EOF ;
69 Content-Type: text/html
70
71 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
72 <html><head>
73 $CF{"HeadExtras"}
74 <link rel=stylesheet href="$CF{"StyleSheet"}" type="text/css" media=all>
75 <title>$title</title>
76 </head><body>
77 $CF{"TopExtras"}
78 EOF
79         $UCW::CGI::error_hook = \&error;
80         load_notes();
81         if ($show_img ne "" && $show_img > 0 && $show_img <= $#images) {
82                 show_links(($show_img > 1 ? ("?i=".($show_img-1)) : ""),
83                            "?",
84                            ($show_img < $#images ? ("?i=".($show_img+1)) : ""));
85         } else {
86                 show_links($CF{'BackURL'}, $CF{'ParentURL'}, $CF{'FwdURL'});
87                 print "<h1>$CF{'Title'}</h1>\n";
88                 print "<h2>$CF{'SubTitle'}</h2>\n" if defined $CF{'SubTitle'};
89         }
90 }
91
92 sub Finish($) {
93         print "$CF{'BotExtras'}\n</body></html>\n";
94 }
95
96 sub RawHTML($$) {
97         my ($obj, $h) = @_;
98         print $h if $show_img eq "";
99 }
100
101 sub img($$$) {
102         my ($this, $orig, $annot) = @_;
103         my ($base, $ext) = ($orig =~ /^(.*)\.([^.]+)$/) or die "Unable to dissect name $orig";
104         $annot = UCW::CGI::html_escape($annot);
105         my $id = $imgno{$base} || 0;
106
107         if ($show_img ne "") {
108                 if ($id == $show_img) {
109                         print "<h1>$annot</h1>\n" if $annot ne "";
110                         my $img = $CF{'PhotoUrlPrefix'} . $orig;
111                         print "<p class=large><img src='$img' width=$widths_orig{$base} height=$heights_orig{$base} alt='$orig'>\n";
112                 }
113                 return;
114         }
115
116         my $thumb = $CF{'ThumbUrlPrefix'} . "$base-thumb.jpg";
117         my $w = $widths{$base} || 10;
118         my $h = $heights{$base} || 10;
119         my $side_w = $CF{"ThumbW"} + 2*$CF{"InteriorMargin"};
120         my $side_h = $CF{"ThumbH"} + 2*$CF{"InteriorMargin"};
121         my $box_w = $CF{"LeftW"} + $side_w + $CF{"RightW"};
122         my $box_h = $CF{"TopH"} + $side_h + $CF{"BotH"};
123         print "<div class=thf><div class=thumb>\n";
124         print "<img src='$CF{'TopImg'}' width=$box_w height=$CF{'TopH'} alt='' class=tt>\n";
125         print "<img src='$CF{'LeftImg'}' width=$CF{'LeftW'} height=$side_h alt='' class=tl>\n";
126         my $ol = $CF{'LeftW'} + $CF{'InteriorMargin'} + int(($CF{'ThumbW'} - $w)/2);
127         my $ot = $CF{'TopH'} + $CF{'InteriorMargin'} + int(($CF{'ThumbH'} - $h)/2);
128         my $tit = ($annot ne "") ? " title=\"$annot\"" : "";
129         my $url = ($CF{"ImageSubpages"} ? "?i=$id" : $orig);
130         print "<a href='$url'><img src='$thumb' width=$w height=$h alt='$orig'$tit class=ti style='left: ${ol}px; top: ${ot}px'></a>\n";
131         print "<img src='$CF{'RightImg'}' width=$CF{'RightW'} height=$side_h alt='' class=tr>\n";
132         print "<img src='$CF{'BotImg'}' width=$box_w height=$CF{'BotH'} alt='' class=tb>\n";
133         print "</div>\n";
134 #       if ($annot ne "") {
135 #               print "<p class=annot>$annot\n";
136 #       }
137         print "</div>\n\n";
138 }
139
140 1;