152 lines
4.0 KiB
Perl
Executable File
152 lines
4.0 KiB
Perl
Executable File
#!/usr/bin/perl -w
|
|
|
|
use strict;
|
|
use warnings;
|
|
use JSON;
|
|
use Image::Magick;
|
|
|
|
my $images_path = "/home/bluesaxman/imagemasterImages/";
|
|
my $libraries_path = "/home/bluesaxman/labs/p/imagemaster/libs//";
|
|
my $json = JSON->new;
|
|
|
|
|
|
sub get_info {
|
|
# foreach (%ENV) { print $_."<br />"; }
|
|
my $clientIP = ($ENV{"REMOTE_ADDR"} or "0.0.0.0");
|
|
my $proto = ($ENV{"REQUEST_SCHEME"} or "http");
|
|
my $query = (split(/\?/,($ENV{"QUERY_STRING"} or "")))[0];
|
|
my $referrer = ($ENV{"HTTP_REFERER"} or "");
|
|
return ($proto,$clientIP,$query,$referrer);
|
|
}
|
|
|
|
sub http_status {
|
|
my ($status,$content,$target) = @_;
|
|
my $header = "";
|
|
$header .= "status: ".$status."\r\n";
|
|
$header .= "Location: ".$target."\r\n" if $target;
|
|
$header .= "Content-Type: ".$content."\r\n" if $content;
|
|
$header .= "\r\n";
|
|
return $header;
|
|
}
|
|
|
|
sub html_element {
|
|
return "<".($_[0] or "div").($_[2] or "").">".($_[1] or "")."</".($_[0] or "div").">\n";
|
|
}
|
|
|
|
sub style_links {
|
|
return html_link("icon","image/gif","img/".$_[0].".png").html_link("stylesheet","text/css","css/css_".$_[0]);
|
|
}
|
|
|
|
sub page_content {
|
|
return html_element("html", html_element("head", html_element("title",shift).shift ).html_element("body",shift) );
|
|
}
|
|
|
|
sub soft_die {
|
|
print http_status(500,"text/html; charset=utf-8");
|
|
print page_content("500",style_links("no"),shift);
|
|
exit;
|
|
}
|
|
|
|
sub normalize_string {
|
|
return join(" ",(map { ucfirst($_) } split("_",shift)));
|
|
}
|
|
|
|
sub list_galleries {
|
|
opendir(GALLERY,$images_path);
|
|
my @galleries = readdir(GALLERY);
|
|
close GALLERY;
|
|
my @filtered_galleries = ();
|
|
foreach (@galleries) {
|
|
next if ($_ =~ m/^\./);
|
|
next unless (-d $images_path.$_ );
|
|
push(@filtered_galleries,$_);
|
|
}
|
|
return $json->encode(\@filtered_galleries);
|
|
}
|
|
|
|
sub list_images {
|
|
my $path = $images_path."/".$_[0];
|
|
opendir(GALLERY,$path);
|
|
my @images = readdir(GALLERY);
|
|
close GALLERY;
|
|
my @filtered_images = ();
|
|
foreach (@images) {
|
|
next if ($_ =~ m/^\./);
|
|
next if (-d $path."/".$_ );
|
|
push(@filtered_images, $_);
|
|
}
|
|
return $json->encode(\@filtered_images);
|
|
}
|
|
|
|
sub make_imagepath {
|
|
my ($name, $gallery, $version) = @_;
|
|
return $gallery."/".$version."/".$name;
|
|
}
|
|
|
|
sub thumb_gen {
|
|
my ($image,$thumb)=@_;
|
|
my $file = Image::Magick->new;
|
|
$file->Read("$image");
|
|
$file->Resize('200x200');
|
|
print $file->Write("$thumb");
|
|
}
|
|
|
|
sub get_image {
|
|
print http_status(200,"image/png;");
|
|
my ($name,$gallery,$version) = @_;
|
|
unless ($name) { $name = "test.jpg"; }
|
|
unless ($gallery) { $gallery = "/"; }
|
|
unless ($version) { $version = "/"; }
|
|
my $full_path = $images_path.make_imagepath($name,$gallery,"");
|
|
my $thumb_path = $images_path.make_imagepath($name,$gallery,$version);
|
|
if ( "thumb" eq $version ) {
|
|
unless (-e $thumb_path ) {
|
|
thumb_gen($full_path,$thumb_path); #If the thumb doesn't exsist we make it.
|
|
}
|
|
open(FILE,"<",$thumb_path) or return print "0";
|
|
} else {
|
|
if ( "" eq $version ) {
|
|
open(FILE,"<",$full_path) or return print "0";
|
|
} else {
|
|
open(FILE,"<",$thumb_path) or return print "0";
|
|
}
|
|
}
|
|
while(<FILE>) { print; }
|
|
close FILE;
|
|
}
|
|
|
|
sub giveBook {
|
|
my ($library, $book) = @_;
|
|
open(THEBOOK, $libraries_path.$library."/".$book) or return print "0";
|
|
while(<THEBOOK>) { print; }
|
|
close THEBOOK;
|
|
}
|
|
|
|
sub clean_input {
|
|
my $input = shift;
|
|
if ($input =~ m!%2F!) { print "Location: /hax\r\n\r\n"; exit; }
|
|
$input =~ s!%(..)!chr hex $1!ge;
|
|
$input =~ s!\+! !g;
|
|
return $input;
|
|
}
|
|
|
|
# And now for our verbs
|
|
my @request = get_info();
|
|
my @get_params = split("!",clean_input($request[2]));
|
|
my $directive = shift(@get_params);
|
|
if ( "i" eq $directive ) {
|
|
get_image(@get_params);
|
|
} elsif ( "lg" eq $directive ) {
|
|
print http_status(200,"text/json");
|
|
print '{"galeries":'.list_galleries().'}';
|
|
} elsif ( "gg" eq $directive ) {
|
|
print http_status(200,"text/json");
|
|
print '{"images":'.list_images($get_params[0]).'}';
|
|
} elsif ( "l" eq $directive ) {
|
|
print http_status(200,"text/plain");
|
|
giveBook(@get_params);
|
|
} else {
|
|
print http_status(200,"text/json");
|
|
print '{"error":"No input or incorrect input"}';
|
|
}
|