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';
31 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
33 use Sesse::pr0n::Config;
35 require Sesse::pr0n::Config_local;
40 @EXPORT = qw(&error &dberror);
42 @EXPORT_OK = qw(&error &dberror);
44 our $dbh = DBI->connect("dbi:Pg:dbname=pr0n;host=" . $Sesse::pr0n::Config::db_host,
45 $Sesse::pr0n::Config::db_username, $Sesse::pr0n::Config::db_password)
46 or die "Couldn't connect to PostgreSQL database: " . DBI->errstr;
47 our $mimetypes = new MIME::Types;
49 Apache2::ServerUtil->server->log_error("Initializing pr0n $VERSION");
56 our ($dbh, $mimetypes);
59 my ($r,$err,$status,$title) = @_;
61 if (!defined($status) || !defined($title)) {
63 $title = "Internal server error";
66 $r->content_type('text/html; charset=utf-8');
70 $r->print(" <p>Error: $err</p>\n");
74 $r->log->error("Stack trace follows: " . Carp::longmess());
76 ModPerl::Util::exit();
81 error($r, "$err (DB error: " . $dbh->errstr . ")");
87 $r->content_type("text/html; charset=utf-8");
89 # Fetch quote if we're itk-bilder.samfundet.no
91 if ($r->get_server_name eq 'itk-bilder.samfundet.no') {
92 $quote = LWP::Simple::get("http://itk.samfundet.no/include/quotes.cli.php");
93 $quote = "Error: Could not fetch quotes." if (!defined($quote));
95 Sesse::pr0n::Templates::print_template($r, "header", { title => $title, quotes => Encode::decode_utf8($quote) });
100 Sesse::pr0n::Templates::print_template($r, "footer",
101 { version => $Sesse::pr0n::Common::VERSION });
105 my ($width, $height, $thumbxres, $thumbyres) = @_;
107 unless ($thumbxres >= $width &&
108 $thumbyres >= $height) {
109 my $sfh = $width / $thumbxres;
110 my $sfv = $height / $thumbyres;
118 $width = POSIX::floor($width);
119 $height = POSIX::floor($height);
122 return ($width, $height);
125 sub get_query_string {
126 my ($param, $defparam) = @_;
130 while (my ($key, $value) = each %$param) {
131 next unless defined($value);
132 next if (defined($defparam->{$key}) && $value == $defparam->{$key});
134 $value = pretty_escape($value);
136 $str .= ($first) ? "?" : ';';
137 $str .= "$key=$value";
143 # This is not perfect (it can't handle "_ " right, for one), but it will do for now
144 sub weird_space_encode {
147 return "_" x (length($val) * 2);
149 return "_" x (length($val) * 2 - 1);
153 sub weird_space_unencode {
155 if (length($val) % 2 == 0) {
156 return "_" x (length($val) / 2);
158 return " " x ((length($val) + 1) / 2);
165 $value =~ s/(([_ ])\2*)/weird_space_encode($1)/ge;
166 $value = URI::Escape::uri_escape($value);
167 $value =~ s/%2F/\//g;
172 sub pretty_unescape {
175 # URI unescaping is already done for us
176 $value =~ s/(_+)/weird_space_unencode($1)/ge;
182 my ($r, $title, $baseurl, $param, $defparam, $accesskey) = @_;
183 my $str = "<a href=\"$baseurl" . get_query_string($param, $defparam) . "\"";
184 if (defined($accesskey) && length($accesskey) == 1) {
185 $str .= " accesskey=\"$accesskey\"";
187 $str .= ">$title</a>";
192 # Check that we are alive
193 if (!(defined($dbh) && $dbh->ping)) {
195 Apache2::ServerUtil->server->log_error("Lost contact with PostgreSQL server, trying to reconnect...");
196 unless ($dbh = DBI->connect("dbi:Pg:dbname=pr0n;host=" . $Sesse::pr0n::Config::db_host,
197 $Sesse::pr0n::Config::db_username, $Sesse::pr0n::Config::db_password)) {
199 die "Couldn't connect to PostgreSQL database";
208 return $r->dir_config('ImageBase');
211 sub get_disk_location {
213 my $dir = POSIX::floor($id / 256);
214 return get_base($r) . "images/$dir/$id.jpg";
217 sub get_cache_location {
218 my ($r, $id, $width, $height, $infobox) = @_;
219 my $dir = POSIX::floor($id / 256);
222 return get_base($r) . "cache/$dir/$id-$width-$height.jpg";
224 return get_base($r) . "cache/$dir/$id-$width-$height-nobox.jpg";
228 sub get_mipmap_location {
229 my ($r, $id, $width, $height) = @_;
230 my $dir = POSIX::floor($id / 256);
232 return get_base($r) . "cache/$dir/$id-mipmap-$width-$height.jpg";
235 sub update_image_info {
236 my ($r, $id, $width, $height) = @_;
238 # Also find the date taken if appropriate (from the EXIF tag etc.)
239 my $exiftool = Image::ExifTool->new;
240 $exiftool->ExtractInfo(get_disk_location($r, $id));
241 my $info = $exiftool->GetInfo();
242 my $datetime = undef;
244 if (defined($info->{'DateTimeOriginal'})) {
245 # Parse the date and time over to ISO format
246 if ($info->{'DateTimeOriginal'} =~ /^(\d{4}):(\d\d):(\d\d) (\d\d):(\d\d):(\d\d)(?:\+\d\d:\d\d)?$/ && $1 > 1990) {
247 $datetime = "$1-$2-$3 $4:$5:$6";
252 local $dbh->{AutoCommit} = 0;
255 $dbh->do('DELETE FROM exif_info WHERE image=?',
257 or die "Couldn't delete old EXIF information in SQL: $!";
259 my $q = $dbh->prepare('INSERT INTO exif_info (image,key,value) VALUES (?,?,?)')
260 or die "Couldn't prepare inserting EXIF information: $!";
262 for my $key (keys %$info) {
263 next if ref $info->{$key};
264 $q->execute($id, $key, guess_charset($info->{$key}))
265 or die "Couldn't insert EXIF information in database: $!";
269 my $model = $exiftool->GetValue('Model', 'PrintConv');
270 my $lens = $exiftool->GetValue('Lens', 'PrintConv');
271 $lens = $exiftool->GetValue('LensSpec', 'PrintConv') if (!defined($lens));
275 $model = undef if (length($model) == 0);
279 $lens = undef if (length($lens) == 0);
281 # Now update the main table with the information we've got
282 $dbh->do('UPDATE images SET width=?, height=?, date=?, model=?, lens=? WHERE id=?',
283 undef, $width, $height, $datetime, $model, $lens, $id)
284 or die "Couldn't update width/height in SQL: $!";
287 my @tags = $exiftool->GetValue('Keywords', 'ValueConv');
288 $dbh->do('DELETE FROM tags WHERE image=?',
290 or die "Couldn't delete old tag information in SQL: $!";
292 $q = $dbh->prepare('INSERT INTO tags (image,tag) VALUES (?,?)')
293 or die "Couldn't prepare inserting tag information: $!";
296 for my $tag (@tags) {
297 $q->execute($id, guess_charset($tag))
298 or die "Couldn't insert tag information in database: $!";
301 # update the last_picture cache as well (this should of course be done
302 # via a trigger, but this is less complicated :-) )
303 $dbh->do('UPDATE last_picture_cache SET last_picture=GREATEST(last_picture, ?) WHERE (vhost,event)=(SELECT vhost,event FROM images WHERE id=?)',
304 undef, $datetime, $id)
305 or die "Couldn't update last_picture in SQL: $!";
312 my $auth = $r->headers_in->{'authorization'};
313 if (!defined($auth) || $auth !~ m#^Basic ([a-zA-Z0-9+/]+=*)$#) {
314 $r->content_type('text/plain; charset=utf-8');
316 $r->headers_out->{'www-authenticate'} = 'Basic realm="pr0n.sesse.net"';
317 $r->print("Need authorization\n");
321 #return qw(sesse Sesse);
323 my ($user, $pass) = split /:/, MIME::Base64::decode_base64($1);
324 # WinXP is stupid :-)
325 if ($user =~ /^.*\\(.*)$/) {
330 if ($user =~ /^([a-zA-Z0-9^_-]+)\@([a-zA-Z0-9^_-]+)$/) {
334 ($takenby = $user) =~ s/^([a-zA-Z])/uc($1)/e;
338 $pass = Digest::SHA1::sha1_base64($pass);
339 my $ref = $dbh->selectrow_hashref('SELECT count(*) AS auth FROM users WHERE username=? AND sha1password=? AND vhost=?',
340 undef, $user, $pass, $r->get_server_name);
341 if ($ref->{'auth'} != 1) {
342 $r->content_type('text/plain; charset=utf-8');
343 warn "No user exists, only $auth";
345 $r->headers_out->{'www-authenticate'} = 'Basic realm="pr0n.sesse.net"';
346 $r->print("Authorization failed");
347 $r->log->warn("Authentication failed for $user/$takenby");
351 $r->log->info("Authentication succeeded for $user/$takenby");
353 return ($user, $takenby);
357 my ($r, $event, $filename) = (@_);
358 my $ref = $dbh->selectrow_hashref(
359 'SELECT id FROM images WHERE event=? AND filename=?',
360 undef, $event, $filename);
361 if (!defined($ref)) {
362 return (undef, undef, undef);
364 return stat_image_from_id($r, $ref->{'id'});
367 sub stat_image_from_id {
370 my $fname = get_disk_location($r, $id);
371 my (undef, undef, undef, undef, undef, undef, undef, $size, undef, $mtime) = stat($fname)
372 or return (undef, undef, undef);
374 return ($fname, $size, $mtime);
377 # Takes in an image ID and a set of resolutions, and returns (generates if needed)
378 # the smallest mipmap larger than the largest of them.
380 my ($r, $filename, $id, $dbwidth, $dbheight, @res) = @_;
381 my ($img, $mmimg, $width, $height);
383 # If we don't know the size, we'll need to read it in anyway
384 if (!defined($dbwidth) || !defined($dbheight)) {
385 $img = read_original_image($r, $id, $dbwidth, $dbheight);
386 $width = $img->Get('columns');
387 $height = $img->Get('rows');
393 # Generate the list of mipmaps
396 my $mmwidth = $width;
397 my $mmheight = $height;
399 while ($mmwidth > 1 || $mmheight > 1) {
400 my $new_mmwidth = POSIX::floor($mmwidth / 2);
401 my $new_mmheight = POSIX::floor($mmheight / 2);
403 $new_mmwidth = 1 if ($new_mmwidth < 1);
404 $new_mmheight = 1 if ($new_mmheight < 1);
406 my $large_enough = 1;
407 for my $i (0..($#res/2)) {
408 my ($xres, $yres) = ($res[$i*2], $res[$i*2+1]);
409 if ($xres > $new_mmwidth || $yres > $new_mmheight) {
415 last if (!$large_enough);
417 $mmwidth = $new_mmwidth;
418 $mmheight = $new_mmheight;
420 push @mmlist, [ $mmwidth, $mmheight ];
423 # Ensure that all of them are OK
424 my $last_good_mmlocation;
425 for my $i (0..$#mmlist) {
426 my $last = ($i == $#mmlist);
427 my $mmres = $mmlist[$i];
429 my $mmlocation = get_mipmap_location($r, $id, $mmres->[0], $mmres->[1]);
430 if (! -r $mmlocation or (-M $mmlocation > -M $filename)) {
431 if (!defined($img)) {
432 if (defined($last_good_mmlocation)) {
433 $img = Image::Magick->new;
434 $img->Read($last_good_mmlocation);
436 $img = read_original_image($r, $id, $dbwidth, $dbheight);
443 $cimg = $img->Clone();
445 $r->log->info("Making mipmap for $id: " . $mmres->[0] . " x " . $mmres->[1]);
446 $cimg->Resize(width=>$mmres->[0], height=>$mmres->[1], filter=>'Lanczos');
448 my $err = $cimg->write(
449 filename => $mmlocation,
451 'sampling-factor' => '1x1'
455 $last_good_mmlocation = $mmlocation;
457 if ($last && !defined($img)) {
458 # OK, read in the smallest one
459 $img = Image::Magick->new;
460 my $err = $img->Read($mmlocation);
467 sub read_original_image {
468 my ($r, $id, $dbwidth, $dbheight) = @_;
470 my $fname = get_disk_location($r, $id);
472 # Read in the original image
473 my $magick = new Image::Magick;
476 # ImageMagick can handle NEF files, but it does it by calling dcraw as a delegate.
477 # The delegate support is rather broken and causes very odd stuff to happen when
478 # more than one thread does this at the same time. Thus, we simply do it ourselves.
479 if ($fname =~ /\.nef$/i) {
480 # this would suffice if ImageMagick gets to fix their handling
481 # $fname = "NEF:$fname";
483 open DCRAW, "-|", "dcraw", "-w", "-c", $fname
484 or error("dcraw: $!");
485 $err = $magick->Read(file => \*DCRAW);
488 # We always want YCbCr JPEGs. Setting this explicitly here instead of using
489 # RGB is slightly faster (no colorspace conversion needed) and works equally
490 # well for our uses, as long as we don't need to draw an information box,
491 # which trickles several ImageMagick bugs related to colorspace handling.
492 # (Ideally we'd be able to keep the image subsampled and
493 # planar, but that would probably be difficult for ImageMagick to expose.)
495 # $magick->Set(colorspace=>'YCbCr');
497 $err = $magick->Read($fname);
501 $r->log->warn("$fname: $err");
505 error($r, "$fname: $err");
509 # If we use ->[0] unconditionally, text rendering (!) seems to crash
510 my $img = (scalar @$magick > 1) ? $magick->[0] : $magick;
512 my $width = $img->Get('columns');
513 my $height = $img->Get('rows');
515 # Update the SQL database if it doesn't contain the required info
516 if (!defined($dbwidth) || !defined($dbheight)) {
517 $r->log->info("Updating width/height for $id: $width x $height");
518 update_image_info($r, $id, $width, $height);
525 my ($r, $filename, $id, $dbwidth, $dbheight, $infobox, $xres, $yres, @otherres) = @_;
527 my $fname = get_disk_location($r, $id);
528 unless (defined($xres) && (!defined($dbwidth) || !defined($dbheight) || $xres < $dbheight || $yres < $dbwidth || $xres == -1)) {
532 my $cachename = get_cache_location($r, $id, $xres, $yres, $infobox);
534 if (! -r $cachename or (-M $cachename > -M $fname)) {
535 # If we are in overload mode (aka Slashdot mode), refuse to generate
537 if (Sesse::pr0n::Overload::is_in_overload($r)) {
538 $r->log->warn("In overload mode, not scaling $id to $xres x $yres");
539 error($r, 'System is in overload mode, not doing any scaling');
542 my $img = make_mipmap($r, $fname, $id, $dbwidth, $dbheight, $xres, $yres, @otherres);
544 while (defined($xres) && defined($yres)) {
545 my ($nxres, $nyres) = (shift @otherres, shift @otherres);
546 my $cachename = get_cache_location($r, $id, $xres, $yres, $infobox);
549 if (defined($nxres) && defined($nyres)) {
550 # we have more resolutions to scale, so don't throw
552 $cimg = $img->Clone();
557 my $width = $img->Get('columns');
558 my $height = $img->Get('rows');
559 my ($nwidth, $nheight) = scale_aspect($width, $height, $xres, $yres);
561 # Use lanczos (sharper) for heavy scaling, mitchell (faster) otherwise
562 my $filter = 'Mitchell';
566 if ($width / $nwidth > 8.0 || $height / $nheight > 8.0) {
573 $cimg->Resize(width=>$nwidth, height=>$nheight, filter=>$filter);
576 if (($nwidth >= 800 || $nheight >= 600 || $xres == -1) && $infobox == 1) {
577 my $info = Image::ExifTool::ImageInfo($fname);
578 make_infobox($cimg, $info, $r);
581 # Strip EXIF tags etc.
586 filename => $cachename,
589 if (($nwidth >= 640 && $nheight >= 480) ||
590 ($nwidth >= 480 && $nheight >= 640)) {
591 $parms{'interlace'} = 'Plane';
594 $parms{'sampling-factor'} = $sf;
596 $err = $cimg->write(%parms);
601 ($xres, $yres) = ($nxres, $nyres);
603 $r->log->info("New cache: $nwidth x $nheight for $id.jpg");
608 $r->log->warn("$fname: $err");
612 error($r, "$fname: $err");
616 return ($cachename, 1);
619 sub get_mimetype_from_filename {
620 my $filename = shift;
621 my MIME::Type $type = $mimetypes->mimeTypeOf($filename);
622 $type = "image/jpeg" if (!defined($type));
627 my ($img, $info, $r) = @_;
629 # The infobox is of the form
630 # "Time - date - focal length, shutter time, aperture, sensitivity, exposure bias - flash",
631 # possibly with some parts omitted -- the middle part is known as the "classic
632 # fields"; note the comma separation. Every field has an associated "bold flag"
633 # in the second part.
635 my $shutter_priority = (defined($info->{'ExposureProgram'}) &&
636 $info->{'ExposureProgram'} =~ /shutter\b.*\bpriority/i);
637 my $aperture_priority = (defined($info->{'ExposureProgram'}) &&
638 $info->{'ExposureProgram'} =~ /aperture\b.*\bpriority/i);
640 my @classic_fields = ();
641 if (defined($info->{'FocalLength'}) && $info->{'FocalLength'} =~ /^(\d+)(?:\.\d+)?(?:mm)?$/) {
642 push @classic_fields, [ $1 . "mm", 0 ];
643 } elsif (defined($info->{'FocalLength'}) && $info->{'FocalLength'} =~ /^(\d+)\/(\d+)$/) {
644 push @classic_fields, [ (sprintf "%.1fmm", ($1/$2)), 0 ];
647 if (defined($info->{'ExposureTime'}) && $info->{'ExposureTime'} =~ /^(\d+)\/(\d+)$/) {
648 my ($a, $b) = ($1, $2);
649 my $gcd = gcd($a, $b);
650 push @classic_fields, [ $a/$gcd . "/" . $b/$gcd . "s", $shutter_priority ];
651 } elsif (defined($info->{'ExposureTime'}) && $info->{'ExposureTime'} =~ /^(\d+)$/) {
652 push @classic_fields, [ $1 . "s", $shutter_priority ];
655 if (defined($info->{'FNumber'}) && $info->{'FNumber'} =~ /^(\d+)\/(\d+)$/) {
658 push @classic_fields, [ (sprintf "f/%.0f", $f), $aperture_priority ];
660 push @classic_fields, [ (sprintf "f/%.1f", $f), $aperture_priority ];
662 } elsif (defined($info->{'FNumber'}) && $info->{'FNumber'} =~ /^(\d+)\.(\d+)$/) {
663 my $f = $info->{'FNumber'};
665 push @classic_fields, [ (sprintf "f/%.0f", $f), $aperture_priority ];
667 push @classic_fields, [ (sprintf "f/%.1f", $f), $aperture_priority ];
671 # Apache2::ServerUtil->server->log_error(join(':', keys %$info));
674 if (defined($info->{'NikonD1-ISOSetting'})) {
675 $iso = $info->{'NikonD1-ISOSetting'};
676 } elsif (defined($info->{'ISO'})) {
677 $iso = $info->{'ISO'};
678 } elsif (defined($info->{'ISOSetting'})) {
679 $iso = $info->{'ISOSetting'};
681 if (defined($iso) && $iso =~ /(\d+)/) {
682 push @classic_fields, [ $1 . " ISO", 0 ];
685 if (defined($info->{'ExposureBiasValue'}) && $info->{'ExposureBiasValue'} ne "0") {
686 push @classic_fields, [ $info->{'ExposureBiasValue'} . " EV", 0 ];
687 } elsif (defined($info->{'ExposureCompensation'}) && $info->{'ExposureCompensation'} != 0) {
688 push @classic_fields, [ $info->{'ExposureCompensation'} . " EV", 0 ];
691 # Now piece together the rest
694 if (defined($info->{'DateTimeOriginal'}) &&
695 $info->{'DateTimeOriginal'} =~ /^(\d{4}):(\d\d):(\d\d) (\d\d):(\d\d):(\d\d)$/
697 push @parts, [ "$1-$2-$3 $4:$5", 0 ];
700 if (defined($info->{'Model'})) {
701 my $model = $info->{'Model'};
705 push @parts, [ ' - ', 0 ] if (scalar @parts > 0);
706 push @parts, [ $model, 0 ];
710 if (scalar @classic_fields > 0) {
711 push @parts, [ ' - ', 0 ] if (scalar @parts > 0);
714 for my $field (@classic_fields) {
715 push @parts, [ ', ', 0 ] if (!$first_elem);
721 if (defined($info->{'Flash'})) {
722 if ($info->{'Flash'} =~ /did not fire/i ||
723 $info->{'Flash'} =~ /no flash/i ||
724 $info->{'Flash'} =~ /not fired/i ||
725 $info->{'Flash'} =~ /Off/) {
726 push @parts, [ ' - ', 0 ] if (scalar @parts > 0);
727 push @parts, [ "No flash", 0 ];
728 } elsif ($info->{'Flash'} =~ /fired/i ||
729 $info->{'Flash'} =~ /On/) {
730 push @parts, [ ' - ', 0 ] if (scalar @parts > 0);
731 push @parts, [ "Flash", 0 ];
733 push @parts, [ ' - ', 0 ] if (scalar @parts > 0);
734 push @parts, [ $info->{'Flash'}, 0 ];
738 return if (scalar @parts == 0);
740 # Find the required width
744 for my $part (@parts) {
747 $font = '/usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf';
749 $font = '/usr/share/fonts/truetype/msttcorefonts/Arial.ttf';
752 my (undef, undef, $h, undef, $w) = ($img->QueryFontMetrics(text=>$part->[0], font=>$font, pointsize=>12));
755 $th = $h if ($h > $th);
758 return if ($tw > $img->Get('columns'));
761 my $y = $img->Get('rows') - 24;
763 # Hit exact DCT blocks
766 my $points = sprintf "%u,%u %u,%u", $x, $y, ($img->Get('columns') - 1), ($img->Get('rows') - 1);
767 my $lpoints = sprintf "%u,%u %u,%u", $x, $y, ($img->Get('columns') - 1), $y;
768 $img->Draw(primitive=>'rectangle', stroke=>'white', fill=>'white', points=>$points);
769 $img->Draw(primitive=>'line', stroke=>'black', points=>$lpoints);
771 # Start writing out the text
772 $x = ($img->Get('columns') - $tw) / 2;
774 my $room = ($img->Get('rows') - 1 - $y - $th);
775 $y = ($img->Get('rows') - 1) - $room/2;
777 for my $part (@parts) {
780 $font = '/usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf';
782 $font = '/usr/share/fonts/truetype/msttcorefonts/Arial.ttf';
784 $img->Annotate(text=>$part->[0], font=>$font, pointsize=>12, x=>int($x), y=>int($y));
785 $x += ($img->QueryFontMetrics(text=>$part->[0], font=>$font, pointsize=>12))[4];
791 return $a if ($b == 0);
792 return gcd($b, $a % $b);
796 my ($dbh, $id, $date, $desc, $vhost) = @_;
799 if (!defined($id) || $id =~ /^\s*$/ || $id !~ /^([a-zA-Z0-9-]+)$/) {
800 push @errors, "Manglende eller ugyldig ID.";
802 if (!defined($date) || $date =~ /^\s*$/ || $date =~ /[<>&]/ || length($date) > 100) {
803 push @errors, "Manglende eller ugyldig dato.";
805 if (!defined($desc) || $desc =~ /^\s*$/ || $desc =~ /[<>&]/ || length($desc) > 100) {
806 push @errors, "Manglende eller ugyldig beskrivelse.";
809 if (scalar @errors > 0) {
813 $dbh->do("INSERT INTO events (event,date,name,vhost) VALUES (?,?,?,?)",
814 undef, $id, $date, $desc, $vhost)
815 or return ("Kunne ikke sette inn ny hendelse" . $dbh->errstr);
816 $dbh->do("INSERT INTO last_picture_cache (vhost,event,last_picture) VALUES (?,?,NULL)",
818 or return ("Kunne ikke sette inn ny cache-rad" . $dbh->errstr);
828 $decoded = Encode::decode("utf-8", $text, Encode::FB_CROAK);
831 $decoded = Encode::decode("iso8859-1", $text);