1 package Sesse::pr0n::Common;
5 use Sesse::pr0n::Templates;
6 use Sesse::pr0n::Overload;
8 use Apache2::RequestRec (); # for $r->content_type
9 use Apache2::RequestIO (); # for $r->print
10 use Apache2::Const -compile => ':common';
27 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
31 @EXPORT = qw(&error &dberror);
33 @EXPORT_OK = qw(&error &dberror);
35 our $dbh = DBI->connect("dbi:Pg:dbname=pr0n;host=127.0.0.1", "pr0n", "EsVdwImY")
36 or die "Couldn't connect to PostgreSQL database: " . DBI->errstr;
37 our $mimetypes = new MIME::Types;
39 Apache2::ServerUtil->server->log_error("Initializing pr0n $VERSION");
46 our ($dbh, $mimetypes);
49 my ($r,$err,$status,$title) = @_;
51 if (!defined($status) || !defined($title)) {
53 $title = "Internal server error";
56 $r->content_type('text/html; charset=utf-8');
60 $r->print(" <p>Error: $err</p>\n");
65 ModPerl::Util::exit();
70 error($r, "$err (DB error: " . $dbh->errstr . ")");
76 $r->content_type("text/html; charset=utf-8");
78 # Fetch quote if we're itk-bilder.samfundet.no
80 if ($r->get_server_name eq 'itk-bilder.samfundet.no') {
81 $quote = LWP::Simple::get("http://itk.samfundet.no/include/quotes.cli.php");
82 $quote = "Error: Could not fetch quotes." if (!defined($quote));
84 Sesse::pr0n::Templates::print_template($r, "header", { title => $title, quotes => $quote });
89 Sesse::pr0n::Templates::print_template($r, "footer",
90 { version => $Sesse::pr0n::Common::VERSION });
94 my ($width, $height, $thumbxres, $thumbyres) = @_;
96 unless ($thumbxres >= $width &&
97 $thumbyres >= $height) {
98 my $sfh = $width / $thumbxres;
99 my $sfv = $height / $thumbyres;
107 $width = POSIX::floor($width);
108 $height = POSIX::floor($height);
111 return ($width, $height);
115 my ($r, $title, $baseurl, $param, $defparam) = @_;
116 my $str = "<a href=\"$baseurl";
119 while (my ($key, $value) = each %$param) {
120 next unless defined($value);
121 next if (defined($defparam->{$key}) && $value == $defparam->{$key});
123 $str .= ($first) ? "?" : '&';
124 $str .= "$key=$value";
128 $str .= "\">$title</a>";
133 # Check that we are alive
134 if (!(defined($dbh) && $dbh->ping)) {
136 Apache2::ServerUtil->server->log_error("Lost contact with PostgreSQL server, trying to reconnect...");
137 unless ($dbh = DBI->connect("dbi:Pg:dbname=pr0n;host=127.0.0.1", "pr0n", "EsVdwImY")) {
139 die "Couldn't connect to PostgreSQL database";
148 return $r->dir_config('ImageBase');
151 sub get_disk_location {
153 my $dir = POSIX::floor($id / 256);
154 return get_base($r) . "images/$dir/$id.jpg";
157 sub get_cache_location {
158 my ($r, $id, $width, $height, $infobox) = @_;
159 my $dir = POSIX::floor($id / 256);
162 return get_base($r) . "cache/$dir/$id-$width-$height.jpg";
164 return get_base($r) . "cache/$dir/$id-$width-$height-nobox.jpg";
168 sub update_width_height {
169 my ($r, $id, $width, $height) = @_;
171 # Also find the date taken if appropriate (from the EXIF tag etc.)
172 my $info = Image::ExifTool::ImageInfo(get_disk_location($r, $id));
173 my $datetime = undef;
175 if (defined($info->{'DateTimeOriginal'})) {
176 # Parse the date and time over to ISO format
177 if ($info->{'DateTimeOriginal'} =~ /^(\d{4}):(\d\d):(\d\d) (\d\d):(\d\d):(\d\d)$/ && $1 > 1990) {
178 $datetime = "$1-$2-$3 $4:$5:$6";
182 $dbh->do('UPDATE images SET width=?, height=?, date=? WHERE id=?',
183 undef, $width, $height, $datetime, $id)
184 or die "Couldn't update width/height in SQL: $!";
186 # update the last_picture cache as well (this should of course be done
187 # via a trigger, but this is less complicated :-) )
188 $dbh->do('UPDATE events SET last_picture=(SELECT COALESCE(MAX(date),\'1970-01-01 00:00:00\') FROM images WHERE event=events.id) WHERE id=(SELECT event FROM images WHERE id=?)',
190 or die "Couldn't update last_picture in SQL: $!";
196 my $auth = $r->headers_in->{'authorization'};
197 if (!defined($auth) || $auth !~ m#^Basic ([a-zA-Z0-9+/]+=*)$#) {
198 $r->content_type('text/plain; charset=utf-8');
200 $r->headers_out->{'www-authenticate'} = 'Basic realm="pr0n.sesse.net"';
201 $r->print("Need authorization\n");
205 #return qw(sesse Sesse);
207 my ($user, $pass) = split /:/, MIME::Base64::decode_base64($1);
208 # WinXP is stupid :-)
209 if ($user =~ /^.*\\(.*)$/) {
214 if ($user =~ /^([a-zA-Z0-9^_-]+)\@([a-zA-Z0-9^_-]+)$/) {
218 ($takenby = $user) =~ s/^([a-zA-Z])/uc($1)/e;
222 $pass = Digest::SHA1::sha1_base64($pass);
223 my $ref = $dbh->selectrow_hashref('SELECT count(*) AS auth FROM users WHERE username=? AND sha1password=? AND vhost=?',
224 undef, $user, $pass, $r->get_server_name);
225 if ($ref->{'auth'} != 1) {
226 $r->content_type('text/plain; charset=utf-8');
227 warn "No user exists, only $auth";
229 $r->headers_out->{'www-authenticate'} = 'Basic realm="pr0n.sesse.net"';
230 $r->print("Authorization failed");
231 $r->log->warn("Authentication failed for $user/$takenby");
235 $r->log->info("Authentication succeeded for $user/$takenby");
237 return ($user, $takenby);
241 my ($r, $event, $filename) = (@_);
242 my $ref = $dbh->selectrow_hashref(
243 'SELECT id FROM images WHERE event=? AND filename=?',
244 undef, $event, $filename);
245 if (!defined($ref)) {
246 return (undef, undef, undef);
248 return stat_image_from_id($r, $ref->{'id'});
251 sub stat_image_from_id {
254 my $fname = get_disk_location($r, $id);
255 my (undef, undef, undef, undef, undef, undef, undef, $size, undef, $mtime) = stat($fname)
256 or return (undef, undef, undef);
258 return ($fname, $size, $mtime);
262 my ($r, $filename, $id, $dbwidth, $dbheight, $infobox, $xres, $yres, @otherres) = @_;
264 my $fname = get_disk_location($r, $id);
265 unless (defined($xres) && ($xres < $dbheight || $yres < $dbwidth || $dbwidth == -1 || $dbheight == -1)) {
269 my $cachename = get_cache_location($r, $id, $xres, $yres, $infobox);
270 if (! -r $cachename or (-M $cachename > -M $fname)) {
271 # If we are in overload mode (aka Slashdot mode), refuse to generate
273 if (Sesse::pr0n::Overload::is_in_overload($r)) {
274 $r->log->warn("In overload mode, not scaling $id to $xres x $yres");
275 error($r, 'System is in overload mode, not doing any scaling');
278 # Need to generate the cache; read in the image
279 my $magick = new Image::Magick;
280 my $info = Image::ExifTool::ImageInfo($fname);
282 # NEF files aren't autodetected
283 $fname = "NEF:$fname" if ($filename =~ /\.nef$/i);
284 $r->log->warn("Generating $fname for $filename");
286 my $err = $magick->Read($fname);
288 $r->log->warn("$fname: $err");
292 error($r, "$fname: $err");
296 # If we use ->[0] unconditionally, text rendering (!) seems to crash
297 my $img = (scalar @$magick > 1) ? $magick->[0] : $magick;
299 my $width = $img->Get('columns');
300 my $height = $img->Get('rows');
302 # Update the SQL database if it doesn't contain the required info
303 if ($dbwidth == -1 || $dbheight == -1) {
304 $r->log->info("Updating width/height for $id: $width x $height");
305 update_width_height($r, $id, $width, $height);
308 # We always want RGB JPEGs
309 if ($img->Get('Colorspace') eq "CMYK") {
310 $img->Set(colorspace=>'RGB');
313 while (defined($xres) && defined($yres)) {
314 my ($nxres, $nyres) = (shift @otherres, shift @otherres);
315 my $cachename = get_cache_location($r, $id, $xres, $yres, $infobox);
318 if (defined($nxres) && defined($nyres)) {
319 # we have more resolutions to scale, so don't throw
321 $cimg = $img->Clone();
326 my ($nwidth, $nheight) = scale_aspect($width, $height, $xres, $yres);
328 # Use lanczos (sharper) for heavy scaling, mitchell (faster) otherwise
329 my $filter = 'Mitchell';
332 if ($width / $nwidth > 8.0 || $height / $nheight > 8.0) {
337 $cimg->Resize(width=>$nwidth, height=>$nheight, filter=>$filter);
339 if (($nwidth >= 800 || $nheight >= 600) && $infobox == 1) {
340 make_infobox($cimg, $info, $r);
343 # Strip EXIF tags etc.
346 $err = $cimg->write(filename=>$cachename, quality=>$quality);
350 ($xres, $yres) = ($nxres, $nyres);
352 $r->log->info("New cache: $nwidth x $nheight for $id.jpg");
358 $r->log->warn("$fname: $err");
362 error($r, "$fname: $err");
366 return ($cachename, 1);
369 sub get_mimetype_from_filename {
370 my $filename = shift;
371 my MIME::Type $type = $mimetypes->mimeTypeOf($filename);
372 $type = "image/jpeg" if (!defined($type));
377 my ($img, $info, $r) = @_;
380 my @classic_fields = ();
382 if (defined($info->{'DateTimeOriginal'}) &&
383 $info->{'DateTimeOriginal'} =~ /^(\d{4}):(\d\d):(\d\d) (\d\d):(\d\d):(\d\d)$/
385 push @lines, "$1-$2-$3 $4:$5";
388 push @lines, $info->{'Model'} if (defined($info->{'Model'}));
391 if (defined($info->{'FocalLength'}) && $info->{'FocalLength'} =~ /^(\d+)(?:\.\d+)?(?:mm)?$/) {
392 push @classic_fields, ($1 . "mm");
393 } elsif (defined($info->{'FocalLength'}) && $info->{'FocalLength'} =~ /^(\d+)\/(\d+)$/) {
394 push @classic_fields, (sprintf "%.1fmm", ($1/$2));
396 if (defined($info->{'ExposureTime'}) && $info->{'ExposureTime'} =~ /^(\d+)\/(\d+)$/) {
397 my ($a, $b) = ($1, $2);
398 my $gcd = gcd($a, $b);
399 push @classic_fields, ($a/$gcd . "/" . $b/$gcd . "s");
401 if (defined($info->{'FNumber'}) && $info->{'FNumber'} =~ /^(\d+)\/(\d+)$/) {
404 push @classic_fields, (sprintf "f/%.0f", $f);
406 push @classic_fields, (sprintf "f/%.1f", $f);
408 } elsif (defined($info->{'FNumber'}) && $info->{'FNumber'} =~ /^(\d+)\.(\d+)$/) {
409 my $f = $info->{'FNumber'};
411 push @classic_fields, (sprintf "f/%.0f", $f);
413 push @classic_fields, (sprintf "f/%.1f", $f);
417 # Apache2::ServerUtil->server->log_error(join(':', keys %$info));
419 if (defined($info->{'NikonD1-ISOSetting'})) {
420 push @classic_fields, $info->{'NikonD1-ISOSetting'}->[1] . " ISO";
421 } elsif (defined($info->{'ISOSetting'})) {
422 push @classic_fields, $info->{'ISOSetting'} . " ISO";
425 push @classic_fields, $info->{'ExposureBiasValue'} . " EV" if (defined($info->{'ExposureBiasValue'}) && $info->{'ExposureBiasValue'} != 0);
427 if (scalar @classic_fields > 0) {
428 push @lines, join(', ', @classic_fields);
431 if (defined($info->{'Flash'})) {
432 if ($info->{'Flash'} =~ /did not fire/ || $info->{'Flash'} =~ /No Flash/) {
433 push @lines, "No flash";
434 } elsif ($info->{'Flash'} =~ /fired/) {
435 push @lines, "Flash";
437 push @lines, $info->{'Flash'};
441 return if (scalar @lines == 0);
443 # OK, this sucks. Let's make something better :-)
444 @lines = ( join(" - ", @lines) );
446 # Find the required width
447 my $th = 14 * (scalar @lines) + 6;
450 for my $line (@lines) {
451 my $this_w = ($img->QueryFontMetrics(text=>$line, font=>'/usr/share/fonts/truetype/msttcorefonts/Arial.ttf', pointsize=>12))[4];
452 $tw = $this_w if ($this_w >= $tw);
457 # Round up so we hit exact DCT blocks
458 $tw += 8 - ($tw % 8) unless ($tw % 8 == 0);
459 $th += 8 - ($th % 8) unless ($th % 8 == 0);
461 return if ($tw > $img->Get('columns'));
463 # my $x = $img->Get('columns') - 8 - $tw;
464 # my $y = $img->Get('rows') - 8 - $th;
466 my $y = $img->Get('rows') - $th;
467 $tw = $img->Get('columns');
472 my $points = sprintf "%u,%u %u,%u", $x, $y, ($x+$tw-1), ($img->Get('rows') - 1);
473 my $lpoints = sprintf "%u,%u %u,%u", $x, $y, ($x+$tw-1), $y;
474 # $img->Draw(primitive=>'rectangle', stroke=>'black', fill=>'white', points=>$points);
475 $img->Draw(primitive=>'rectangle', stroke=>'white', fill=>'white', points=>$points);
476 $img->Draw(primitive=>'line', stroke=>'black', points=>$lpoints);
478 my $i = -(scalar @lines - 1)/2.0;
479 my $xc = $x + $tw / 2 - $img->Get('columns')/2;
480 my $yc = ($y + $img->Get('rows'))/2 - $img->Get('rows')/2;
481 #my $yc = ($y + $img->Get('rows'))/4;
482 my $yi = $th / (scalar @lines);
484 $lpoints = sprintf "%u,%u %u,%u", $x, $yc + $img->Get('rows')/2, ($x+$tw-1), $yc+$img->Get('rows')/2;
486 for my $line (@lines) {
487 $img->Annotate(text=>$line, font=>'/usr/share/fonts/truetype/msttcorefonts/Arial.ttf', pointsize=>12, gravity=>'Center',
488 # $img->Annotate(text=>$line, font=>'Helvetica', pointsize=>12, gravity=>'Center',
489 x=>int($xc), y=>int($yc + $i * $yi));
497 return $a if ($b == 0);
498 return gcd($b, $a % $b);