Subversion Repositories CoffeeCatalog

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
#!/usr/bin/perl -w
2
 
3
use strict;
4
use CGI qw(:standard escapeHTML);
5
 
6
if (defined (param ("name"))) {
7
	display_image (param ("name"), param ("thumbnail"));
8
} elsif (defined (param ("gallery"))) {
9
	display_gallery ()
10
} else {
11
	error ("Unknown request type");
12
}
13
 
14
 
15
sub display_image {
16
	my ($name, $show_thumbnail) = @_;
17
	my $col_name = (defined ($show_thumbnail) ? "thumbnail" : "image");
18
	my ($dbh, $mime_type, $data);
19
 
20
	$dbh = WebDB::connect ();
21
	($mime_type, $data) = $dbh->selectrow_array (
22
                     "SELECT mime_type, $col_name FROM image WHERE name = ?",
23
                      undef, $name);
24
	$dbh->disconnect ();
25
 
26
	# did we find a record?
27
	error ("Cannot find image named $name") unless defined ($mime_type);
28
 
29
	print header (-type => $mime_type, -Content_Length => length ($data)), $data;
30
}
31
 
32
sub display_gallery {
33
	my ($dbh, $sth);
34
 
35
	print header (), start_html ("Image Gallery");
36
 
37
	$dbh = WebDB::connect ();
38
	$sth = $dbh->prepare ("SELECT name FROM image ORDER BY name");
39
	$sth->execute ();
40
 
41
	# we're fetching a single value (name), so we can call fetchrow_array()
42
	# in a scalar context to get the value
43
 
44
	while (my $name = $sth->fetchrow_array ()) {
45
		# encode the name with escape() for the URL, with escapeHTML() otherwise
46
		my $url = url () . sprintf ("?name=%s", escape ($name));
47
		$name = escapeHTML ($name);
48
		print p ($name),
49
					a ({-href => $url},     # link for full size image
50
                  # embed thumbnail as the link content to make it clickable
51
					img ({-src => "$url;thumbnail=1", -alt => $name})
52
					),
53
					"\n";
54
	}
55
	$sth->finish ();
56
	$dbh->disconnect ();
57
	print end_html ();
58
}
59
 
60
sub error {
61
	my $msg = shift;
62
	print header (),
63
		start_html ("Error"),
64
		p (escapeHTML ($msg)),
65
		end_html ();
66
	exit (0);
67
}
68
 
69
 
70
 
71
 
72
 
73
 
74
 
75