]> git.sesse.net Git - pr0n/blobdiff - perl/Sesse/pr0n/Common.pm
Split infobox generation out into its own function.
[pr0n] / perl / Sesse / pr0n / Common.pm
index 0569ef90fbd3bdb37a32c97a775e3759ac0ffb49..c706bb7f72a720fa5f244b8ed2d2e5b5b044693c 100644 (file)
@@ -13,8 +13,6 @@ use DBD::Pg;
 use Image::Magick;
 use IO::String;
 use POSIX;
-use Digest::SHA;
-use Digest::HMAC_SHA1;
 use MIME::Base64;
 use MIME::Types;
 use LWP::Simple;
@@ -34,7 +32,7 @@ BEGIN {
                require Sesse::pr0n::Config_local;
        };
 
-       $VERSION     = "v3.00-pre";
+       $VERSION     = "v3.01";
        @ISA         = qw(Exporter);
        @EXPORT      = qw(&error &dberror);
        %EXPORT_TAGS = qw();
@@ -130,7 +128,8 @@ sub get_query_string {
        my $first = 1;
        my $str = "";
 
-       while (my ($key, $value) = each %$param) {
+       for my $key (sort keys %$param) {
+               my $value = $param->{$key};
                next unless defined($value);
                next if (defined($defparam->{$key}) && $value == $defparam->{$key});
 
@@ -216,16 +215,14 @@ sub get_cache_location {
        my ($r, $id, $width, $height, $infobox, $dpr) = @_;
         my $dir = POSIX::floor($id / 256);
 
-       if ($infobox eq 'both') {
-               return $Sesse::pr0n::Config::image_base . "cache/$dir/$id-$width-$height.jpg";
-       } elsif ($infobox eq 'nobox') {
-               return $Sesse::pr0n::Config::image_base . "cache/$dir/$id-$width-$height-nobox.jpg";
-       } else {
+       if ($infobox) {
                if ($dpr == 1) {
                        return $Sesse::pr0n::Config::image_base . "cache/$dir/$id-$width-$height-box.png";
                } else {
                        return $Sesse::pr0n::Config::image_base . "cache/$dir/$id-$width-$height-box\@$dpr.png";
                }
+       } else {
+               return $Sesse::pr0n::Config::image_base . "cache/$dir/$id-$width-$height-nobox.jpg";
        }
 }
 
@@ -304,25 +301,6 @@ sub update_image_info {
                         undef, $width, $height, $datetime, $model, $lens, $id)
                        or die "Couldn't update width/height in SQL: $!";
                
-               # Tags
-               my @tags = $exiftool->GetValue('Keywords', 'ValueConv');
-               if (scalar @tags == 0) {
-                       # This is XMP-dc:Subject, an RDF bag of tags.
-                       @tags = $exiftool->GetValue('Subject', 'ValueConv');
-               }
-               $dbh->do('DELETE FROM tags WHERE image=?',
-                       undef, $id)
-                       or die "Couldn't delete old tag information in SQL: $!";
-
-               $q = $dbh->prepare('INSERT INTO tags (image,tag) VALUES (?,?)')
-                       or die "Couldn't prepare inserting tag information: $!";
-
-
-               for my $tag (@tags) {
-                       $q->execute($id, guess_charset($tag))
-                               or die "Couldn't insert tag information in database: $!";
-               }
-
                # update the last_picture cache as well (this should of course be done
                # via a trigger, but this is less complicated :-) )
                $dbh->do('UPDATE last_picture_cache SET last_picture=GREATEST(last_picture, ?),last_update=CURRENT_TIMESTAMP WHERE (vhost,event)=(SELECT vhost,event FROM images WHERE id=?)',
@@ -363,35 +341,16 @@ sub check_basic_auth {
        my ($raw_user, $pass) = split /:/, MIME::Base64::decode_base64($auth);
        my ($user, $takenby) = extract_takenby($raw_user);
 
-       my $ref = $dbh->selectrow_hashref('SELECT sha1password,cryptpassword FROM users WHERE username=? AND vhost=?',
+       my $ref = $dbh->selectrow_hashref('SELECT cryptpassword FROM users WHERE username=? AND vhost=?',
                undef, $user, Sesse::pr0n::Common::get_server_name($r));
-       my ($sha1_matches, $bcrypt_matches) = (0, 0);
-       if (defined($ref) && defined($ref->{'sha1password'})) {
-               $sha1_matches = (Digest::SHA::sha1_base64($pass) eq $ref->{'sha1password'});
-       }
-       if (defined($ref) && defined($ref->{'cryptpassword'})) {
-               $bcrypt_matches = (Crypt::Eksblowfish::Bcrypt::bcrypt($pass, $ref->{'cryptpassword'}) eq $ref->{'cryptpassword'});
-       }
-
-       if (!defined($ref) || (!$sha1_matches && !$bcrypt_matches)) {
+       my $bcrypt_matches = 0;
+       if (!defined($ref) || Crypt::Eksblowfish::Bcrypt::bcrypt($pass, $ref->{'cryptpassword'}) ne $ref->{'cryptpassword'}) {
                $r->content_type('text/plain; charset=utf-8');
                log_warn($r, "Authentication failed for $user/$takenby");
                return undef;
        }
        log_info($r, "Authentication succeeded for $user/$takenby");
 
-       # Make sure we can use bcrypt authentication in the future with this password.
-       # Also remove old-style SHA1 password when we migrate.
-       if (!$bcrypt_matches) {
-               my $salt = get_pseudorandom_bytes(16);  # Doesn't need to be cryptographically secur.
-               my $hash = "\$2a\$07\$" . Crypt::Eksblowfish::Bcrypt::en_base64($salt);
-               my $cryptpassword = Crypt::Eksblowfish::Bcrypt::bcrypt($pass, $hash);
-               $dbh->do('UPDATE users SET sha1password=NULL,cryptpassword=? WHERE username=? AND vhost=?',
-                       undef, $cryptpassword, $user, Sesse::pr0n::Common::get_server_name($r))
-                       or die "Couldn't update: " . $dbh->errstr;
-               log_info($r, "Updated bcrypt hash for $user");
-       }
-
        return ($user, $takenby);
 }
 
@@ -457,14 +416,14 @@ sub stat_image_from_id {
 # the smallest mipmap larger than the largest of them, as well as the original image
 # dimensions.
 sub make_mipmap {
-       my ($r, $filename, $id, $dbwidth, $dbheight, $can_use_qscale, @res) = @_;
+       my ($r, $filename, $id, $dbwidth, $dbheight, @res) = @_;
        my ($img, $mmimg, $width, $height);
        
        my $physical_fname = get_disk_location($r, $id);
 
        # If we don't know the size, we'll need to read it in anyway
        if (!defined($dbwidth) || !defined($dbheight)) {
-               $img = read_original_image($r, $filename, $id, $dbwidth, $dbheight, $can_use_qscale);
+               $img = read_original_image($r, $filename, $id, $dbwidth, $dbheight);
                $width = $img->Get('columns');
                $height = $img->Get('rows');
        } else {
@@ -512,14 +471,10 @@ sub make_mipmap {
                if (! -r $mmlocation or (-M $mmlocation > -M $physical_fname)) {
                        if (!defined($img)) {
                                if (defined($last_good_mmlocation)) {
-                                       if ($can_use_qscale) {
-                                               $img = Sesse::pr0n::QscaleProxy->new;
-                                       } else {
-                                               $img = Image::Magick->new;
-                                       }
+                                       $img = Sesse::pr0n::QscaleProxy->new;
                                        $img->Read($last_good_mmlocation);
                                } else {
-                                       $img = read_original_image($r, $filename, $id, $dbwidth, $dbheight, $can_use_qscale);
+                                       $img = read_original_image($r, $filename, $id, $dbwidth, $dbheight);
                                }
                        }
                        my $cimg;
@@ -542,17 +497,13 @@ sub make_mipmap {
                }
                if ($last && !defined($img)) {
                        # OK, read in the smallest one
-                       if ($can_use_qscale) {
-                               $img = Sesse::pr0n::QscaleProxy->new;
-                       } else {
-                               $img = Image::Magick->new;
-                       }
+                       $img = Sesse::pr0n::QscaleProxy->new;
                        my $err = $img->Read($mmlocation);
                }
        }
 
        if (!defined($img)) {
-               $img = read_original_image($r, $filename, $id, $dbwidth, $dbheight, $can_use_qscale);
+               $img = read_original_image($r, $filename, $id, $dbwidth, $dbheight);
                $width = $img->Get('columns');
                $height = $img->Get('rows');
        }
@@ -560,13 +511,13 @@ sub make_mipmap {
 }
 
 sub read_original_image {
-       my ($r, $filename, $id, $dbwidth, $dbheight, $can_use_qscale) = @_;
+       my ($r, $filename, $id, $dbwidth, $dbheight) = @_;
 
        my $physical_fname = get_disk_location($r, $id);
 
        # Read in the original image
        my $magick;
-       if ($can_use_qscale && ($filename =~ /\.jpeg$/i || $filename =~ /\.jpg$/i)) {
+       if ($filename =~ /\.jpeg$/i || $filename =~ /\.jpg$/i) {
                $magick = Sesse::pr0n::QscaleProxy->new;
        } else {
                $magick = Image::Magick->new;
@@ -618,15 +569,13 @@ sub read_original_image {
 }
 
 sub ensure_cached {
-       my ($r, $filename, $id, $dbwidth, $dbheight, $infobox, $dpr, $xres, $yres, @otherres) = @_;
+       my ($r, $filename, $id, $dbwidth, $dbheight, $dpr, $xres, $yres, @otherres) = @_;
 
        my ($new_dbwidth, $new_dbheight);
 
        my $fname = get_disk_location($r, $id);
-       if ($infobox ne 'box') {
-               unless (defined($xres) && (!defined($dbwidth) || !defined($dbheight) || $xres < $dbwidth || $yres < $dbheight || $xres == -1)) {
-                       return ($fname, undef);
-               }
+       unless (defined($xres) && (!defined($dbwidth) || !defined($dbheight) || $xres < $dbwidth || $yres < $dbheight || $xres == -1)) {
+               return ($fname, undef);
        }
 
        my $cachename = get_cache_location($r, $id, $xres, $yres, $infobox, $dpr);
@@ -639,64 +588,8 @@ sub ensure_cached {
                        error($r, 'System is in overload mode, not doing any scaling');
                }
 
-               # If we're being asked for just the box, make a new image with just the box.
-               # We don't care about @otherres since each of these images are
-               # already pretty cheap to generate, but we need the exact width so we can make
-               # one in the right size.
-               if ($infobox eq 'box') {
-                       my ($img, $width, $height);
-
-                       # This is slow, but should fortunately almost never happen, so don't bother
-                       # special-casing it.
-                       if (!defined($dbwidth) || !defined($dbheight)) {
-                               $img = read_original_image($r, $filename, $id, $dbwidth, $dbheight, 0);
-                               $new_dbwidth = $width = $img->Get('columns');
-                               $new_dbheight = $height = $img->Get('rows');
-                               @$img = ();
-                       } else {
-                               $img = Image::Magick->new;
-                               $width = $dbwidth;
-                               $height = $dbheight;
-                       }
-                       
-                       if (defined($xres) && defined($yres)) {
-                               ($width, $height) = scale_aspect($width, $height, $xres, $yres);
-                       }
-                       $height = 24 * $dpr;
-                       $img->Set(size=>($width . "x" . $height));
-                       $img->Read('xc:white');
-                               
-                       my $info = Image::ExifTool::ImageInfo($fname);
-                       if (make_infobox($img, $info, $r, $dpr)) {
-                               $img->Quantize(colors=>16, dither=>'False');
-
-                               # Since the image is grayscale, ImageMagick overrides us and writes this
-                               # as grayscale anyway, but at least we get rid of the alpha channel this
-                               # way.
-                               $img->Set(type=>'Palette');
-                       } else {
-                               # Not enough room for the text, make a tiny dummy transparent infobox
-                               @$img = ();
-                               $img->Set(size=>"1x1");
-                               $img->Read('null:');
-
-                               $width = 1;
-                               $height = 1;
-                       }
-                               
-                       $err = $img->write(filename => $cachename, quality => 90, depth => 8);
-                       log_info($r, "New infobox cache: $width x $height for $id.jpg");
-                       
-                       return ($cachename, 'image/png');
-               }
-
-               my $can_use_qscale = 0;
-               if ($infobox eq 'nobox') {
-                       $can_use_qscale = 1;
-               }
-
                my $img;
-               ($img, $new_dbwidth, $new_dbheight) = make_mipmap($r, $filename, $id, $dbwidth, $dbheight, $can_use_qscale, $xres, $yres, @otherres);
+               ($img, $new_dbwidth, $new_dbheight) = make_mipmap($r, $filename, $id, $dbwidth, $dbheight, $xres, $yres, @otherres);
 
                while (defined($xres) && defined($yres)) {
                        my ($nxres, $nyres) = (shift @otherres, shift @otherres);
@@ -723,11 +616,6 @@ sub ensure_cached {
                                $cimg->Resize(width=>$nwidth, height=>$nheight, filter=>$filter, 'sampling-factor'=>$sf);
                        }
 
-                       if (($nwidth >= 800 || $nheight >= 600 || $xres == -1) && $infobox ne 'nobox') {
-                               my $info = Image::ExifTool::ImageInfo($fname);
-                               make_infobox($cimg, $info, $r, 1);
-                       }
-
                        # Strip EXIF tags etc.
                        $cimg->Strip();
 
@@ -773,6 +661,70 @@ sub ensure_cached {
        return ($cachename, 'image/jpeg');
 }
 
+sub ensure_infobox_cached {
+       my ($r, $filename, $id, $dbwidth, $dbheight, $infobox, $dpr, $xres, $yres) = @_;
+
+       my ($new_dbwidth, $new_dbheight);
+
+       my $fname = get_disk_location($r, $id);
+       my $cachename = get_cache_location($r, $id, $xres, $yres, 1, $dpr);
+       my $err;
+       if (! -r $cachename or (-M $cachename > -M $fname)) {
+               # If we are in overload mode (aka Slashdot mode), refuse to generate
+               # new thumbnails.
+               if (Sesse::pr0n::Overload::is_in_overload($r)) {
+                       log_warn($r, "In overload mode, not scaling $id to $xres x $yres");
+                       error($r, 'System is in overload mode, not doing any scaling');
+               }
+
+               # We need the exact width so we can make one in the right size.
+               my ($width, $height);
+
+               # This is slow, but should fortunately almost never happen, so don't bother
+               # special-casing it.
+               if (!defined($dbwidth) || !defined($dbheight)) {
+                       my $img = read_original_image($r, $filename, $id, $dbwidth, $dbheight, 0);
+                       $new_dbwidth = $width = $img->Get('columns');
+                       $new_dbheight = $height = $img->Get('rows');
+               } else {
+                       $width = $dbwidth;
+                       $height = $dbheight;
+               }
+               my $img = Image::Magick->new;
+
+               if (defined($xres) && defined($yres)) {
+                       ($width, $height) = scale_aspect($width, $height, $xres, $yres);
+               }
+               $height = 24 * $dpr;
+               $img->Set(size=>($width . "x" . $height));
+               $img->Read('xc:white');
+
+               my $info = Image::ExifTool::ImageInfo($fname);
+               if (make_infobox($img, $info, $r, $dpr)) {
+                       $img->Quantize(colors=>16, dither=>'False');
+
+                       # Since the image is grayscale, ImageMagick overrides us and writes this
+                       # as grayscale anyway, but at least we get rid of the alpha channel this
+                       # way.
+                       $img->Set(type=>'Palette');
+               } else {
+                       # Not enough room for the text, make a tiny dummy transparent infobox
+                       @$img = ();
+                       $img->Set(size=>"1x1");
+                       $img->Read('null:');
+
+                       $width = 1;
+                       $height = 1;
+               }
+
+               $err = $img->write(filename => $cachename, quality => 90, depth => 8);
+               log_info($r, "New infobox cache: $width x $height for $id.jpg");
+
+               return ($cachename, 'image/png');
+       }
+}
+
+
 sub get_mimetype_from_filename {
        my $filename = shift;
        my MIME::Type $type = $mimetypes->mimeTypeOf($filename);
@@ -1065,8 +1017,8 @@ sub get_all_cache_urls {
 sub set_last_modified {
        my ($res, $mtime) = @_;
 
-       my $str = POSIX::strftime("%a, %d %b %Y %H:%M:%S %Z", localtime($mtime));
-       $res->headers({ 'Last-Modified' => $str });
+       my $str = POSIX::strftime("%a, %d %b %Y %H:%M:%S GMT", gmtime($mtime));
+       $res->header('Last-Modified' => $str);
 }
 
 sub get_server_name {