# Read the photo gallery config.
$root->{photocfg} = &readConfig("photos.conf");
# Get basic query string variables.
my $action = $query->{action} || 'index';
my $album = $query->{album} || '';
my $pic = $query->{pic} || '';
# Get our album privileges.
my $privilege = &getPhotoPermissions();
# Alias the action.
if ($action eq 'index') {
if (length $album > 0 && length $pic > 0) {
$action = 'picture';
}
elsif (length $album > 0) {
$action = 'album';
}
}
# Create the photo directories if they don't exist.
if (!-d "$root->{conf}->{site}->{public}/photos") {
mkdir ("$root->{conf}->{site}->{public}/photos");
}
# Process the action.
if ($action eq 'index') {
# List the photo albums index.
$root->{content} = "Photo Albums
\n\n";
my $meta = &readAllAlbums();
if (scalar keys %{$meta} > 0) {
$root->{content} .= "\n";
foreach my $id (sort { $b cmp $a } keys %{$meta}) {
# Skip albums we can't see.
if ($meta->{$id}->{privacy} eq 'members') {
if (not $root->{auth}) {
# Must be logged in to see member albums.
next;
}
}
elsif ($meta->{$id}->{privacy} eq 'private') {
if ($root->{auth}) {
# Only Owners can see private albums.
if ($privilege !~ /^owners$/i) {
next;
}
}
else {
next;
}
}
# Count the pictures.
my $picmeta = &readPhotos ($id);
my $count = scalar keys %{$picmeta};
$root->{content} .= "\n"
. "\n";
if (length $meta->{$id}->{icon}) {
$root->{content} .= ""
. "{conf}->{site}->{public}/photos/$id/"
. "s-$meta->{$id}->{icon}\" alt=\"$meta->{$id}->{name}\" "
. "style=\"border: 1px solid #000000\">"
. "\n";
}
else {
$root->{content} .= "[no album cover]\n";
}
$root->{content} .= " | \n"
. "\n"
. "\n"
. "$count photo" . ($count == 1 ? '' : 's') . ".\n";
if ($privilege =~ /^owners$/i) {
$root->{content} .= " \n"
. "Privacy: " . ucfirst($meta->{$id}->{privacy})
. "\n";
}
# If 0 photos and we're the Owner, we can delete this.
if ($count == 0) {
if ($privilege =~ /^owners$/i) {
$root->{content} .= " \n"
. "This album is empty. "
. ""
. "Delete it?\n";
}
}
$root->{content} .= " | \n"
. "
\n";
}
$root->{content} .= "
\n";
}
else {
$root->{content} .= "No photo albums have been created.\n";
}
if ($privilege =~ /^(owners|photographers)$/i) {
$root->{content} .= "\n";
if ($privilege =~ /^owners$/i) {
$root->{content} .= "
Owners: "
. "
[Create New Album]"
. "\n";
}
$root->{content} .= "
Photographers: "
. "
[Upload Photos]\n"
. "
\n";
}
}
elsif ($action eq 'album') {
# Load this album's info.
my $meta = &readAlbum($query->{album});
$root->{meta}->{title} = $meta->{name};
# Get the pics in this album.
my $pics = &readPhotos($query->{album});
$root->{content} = "$meta->{name}
\n\n"
. "¤ Back to Index\n\n";
if (scalar keys %{$pics} > 0) {
# Sort the pictures.
my @prefSort = &getPicOrder($query->{album});
my %allPicIDs = %{$pics};
my @sorted = ();
foreach my $id (@prefSort) {
next unless exists $pics->{$id};
push (@sorted,$id);
delete $allPicIDs{$id};
}
push (@sorted, sort { $b cmp $a } keys %allPicIDs);
my $x = 0;
$root->{content} .= "
\n";
}
else {
$root->{content} .= "There are no photos in this album.\n";
}
if ($privilege =~ /^(owners|photographers)$/i) {
$root->{content} .= "\n";
}
}
elsif ($action eq 'picture') {
# Load this album's info.
my $meta = &readAlbum($query->{album});
# Get the pics in this album.
my $pics = &readPhotos($query->{album});
# Sort the pictures.
my @prefSort = &getPicOrder($query->{album});
my %allPicIDs = %{$pics};
my @sorted = ();
foreach my $id (@prefSort) {
next unless exists $pics->{$id};
push (@sorted,$id);
delete $allPicIDs{$id};
}
push (@sorted, sort { $b cmp $a } keys %allPicIDs);
my $nextPic = $sorted[0];
my $i = 0;
foreach my $id (@sorted) {
if ($id eq $pic) {
if (defined $sorted[$i + 1]) {
$nextPic = $sorted[$i + 1];
}
else {
$nextPic = $sorted[0];
}
last;
}
$i++;
}
my $title = $pics->{$pic}->{caption} || $meta->{name};
$root->{meta}->{title} = $title;
$root->{content} = "$meta->{name}
\n\n"
. "¤ Back to Album
\n"
. "¤ Back to Index\n\n";
if ($privilege =~ /^(owners|photographers)$/i) {
$root->{content} .= "
\n";
}
$root->{content} .= "\n"
. "";
}
elsif ($action eq 'newalbum') {
# Only Owners can make a new album.
if ($privilege !~ /^owners$/i) {
$root->{content} = "Permission Denied
\n\n"
. "Only the photo album owners can access this page.";
return 1;
}
$root->{content} = "Create Album
\n\n"
. "\n";
}
elsif ($action eq 'mkalbum') {
# Only Owners can make a new album.
if ($privilege !~ /^owners$/i) {
$root->{content} = "Permission Denied
\n\n"
. "Only the photo album owners can access this page.";
return 1;
}
# Validate the inputs.
my $id = $query->{id} || '';
my $name = $query->{name} || '';
my $perm = $query->{privacy} || 'public';
$id =~ s/[^A-Za-z0-9]//g;
$id = lc($id);
if (not length $id) {
$root->{content} = "Invalid ID
\n\n"
. "Your ID must contain only letters and numbers.";
return 1;
}
if (not length $name) {
$name = ucfirst($id);
}
# Create the album.
&writeAlbum ($id,
name => $name,
privacy => $perm,
icon => '',
);
$root->{content} = "Album Created
\n\n"
. "Your new photo album has been created. You can now "
. "view your album "
. "or upload photos.";
}
elsif ($action eq 'editalbum') {
# Only Owners can make a new album.
if ($privilege !~ /^owners$/i) {
$root->{content} = "Permission Denied
\n\n"
. "Only the photo album owners can access this page.";
return 1;
}
# Load this album's info.
my $meta = &readAlbum($query->{album});
$meta->{name} =~ s/\"/"/ig;
$root->{content} = "Edit Album
\n\n"
. "\n";
}
elsif ($action eq 'updatealbum') {
# Only Owners can make a new album.
if ($privilege !~ /^owners$/i) {
$root->{content} = "Permission Denied
\n\n"
. "Only the photo album owners can access this page.";
return 1;
}
# Load this album's info.
my $meta = &readAlbum($query->{album});
# Validate the inputs.
my $name = $query->{name} || '';
my $perm = $query->{privacy} || 'public';
if (not length $album) {
$root->{content} = "Invalid ID
\n\n"
. "Your ID must contain only letters and numbers.";
return 1;
}
if (not length $name) {
$name = ucfirst($album);
}
# Create the album.
&writeAlbum ($album,
name => $name,
privacy => $perm,
icon => $meta->{icon},
);
$root->{content} = "Album Update
\n\n"
. "Your new photo album has been updated. You can now "
. "view your album "
. "or upload photos.";
}
elsif ($action eq 'delalbum') {
# Only Owners can delete an album.
if ($privilege !~ /^owners$/i) {
$root->{content} = "Permission Denied
\n\n"
. "Only the photo album owners can access this page.";
return 1;
}
# Make sure this album is empty.
my $picmeta = &readPhotos ($album);
if (scalar keys %{$picmeta} > 0) {
$root->{content} = "Can't Delete Album
\n\n"
. "This album still has pictures in it. Delete all of "
. "the pictures before deleting the album.";
return 1;
}
# Delete it.
&deleteAlbum($album);
$root->{content} = "Album Deleted
\n\n"
. "The album has been deleted successfully. "
. "Return to the index.";
}
elsif ($action eq 'setcover') {
# Only Owners can set the album cover picture.
if ($privilege !~ /^owners$/i) {
$root->{content} = "Permission Denied
\n\n"
. "Only the photo album owners can access this page.";
return 1;
}
my $meta = &readAlbum($album);
$meta->{icon} = $pic;
&writeAlbum($album, %{$meta});
$root->{content} = "Cover Set
\n\n"
. "The album cover has been updated. "
. "Return to index or "
. "return to album.";
}
elsif ($action eq 'upload') {
# Only Photographers can upload photos.
if ($privilege !~ /^(owners|photographers)$/i) {
$root->{content} = "Permission Denied
\n\n"
. "Only photographers can access this page.";
return 1;
}
# Make sure we have albums.
my $meta = {};
if (-f "$root->{conf}->{site}->{public}/photos/meta.txt") {
$meta = do "$root->{conf}->{site}->{public}/photos/meta.txt";
}
if (scalar keys %{$meta} == 0) {
$root->{content} = "No Photo Albums
\n\n"
. "No photo albums were found. Please create one first.";
return 1;
}
$root->{content} = "Upload Photos
\n\n"
. "\n";
$root->{content} .= q~
~;
}
elsif ($action eq 'mkphoto') {
# Only Photographers can upload photos.
if ($privilege !~ /^(owners|photographers)$/i) {
$root->{content} = "Permission Denied
\n\n"
. "Only photographers can access this page.";
return 1;
}
# Make sure we have albums.
my $meta = {};
if (-f "$root->{conf}->{site}->{public}/photos/meta.txt") {
$meta = do "$root->{conf}->{site}->{public}/photos/meta.txt";
}
if (scalar keys %{$meta} == 0) {
$root->{content} = "No Photo Albums
\n\n"
. "No photo albums were found. Please create one first.";
return 1;
}
if (!-d "$root->{conf}->{site}->{public}/photos/$album") {
$root->{content} = "Album Not Found
\n\n"
. "The photo album with the ID $album wasn't found.";
return 1;
}
my @msgs = ();
# Upload all the photos.
for (my $i = 1; exists $query->{"pic" . $i}; $i++) {
my $fname = $query->{"pic" . $i} || '';
my $cap = $query->{"cap" . $i} || '';
next if length $fname == 0;
my $file = (split(/(\/|\\)/, $fname))[-1];
# Only accept image files.
if ($fname =~ /\.(jpg|jpe|jpeg|gif|png)$/i) {
# Good.
}
else {
push (@msgs,"Error: File $fname not accepted: must be an image file!");
next;
}
# Read in the image and save it to a file.
my $fh = $cgi->upload ("pic" . $i);
my $bin = '';
while (<$fh>) {
$bin .= $_;
}
# No data?
if (length $bin == 0) {
push (@msgs,"Error: File $file not accepted: zero-length file data!");
next;
}
# Write it to a temp file.
open (WRITE, ">$root->{conf}->{site}->{public}/photos/temp.$file");
binmode WRITE;
print WRITE $bin;
close (WRITE);
# Make up a unique file name.
my $unique = $file;
$unique =~ s/\.(jpg|jpe|jpeg|gif|png)$//i;
my $ui = 0;
while (-f "$root->{conf}->{site}->{public}/photos/$album/$unique\.jpg") {
$ui++;
$unique = $ui . $file;
$unique =~ s/\.(jpg|jpe|jpeg|gif|png)$//i;
}
$unique .= '.jpg';
# Load it into GD.
use GD;
my $orig = undef;
if ($fname =~ /\.(jpg|jpe|jpeg)$/i) {
$orig = GD::Image->newFromJpeg ("$root->{conf}->{site}->{public}/photos/temp.$file");
}
elsif ($fname =~ /\.gif$/i) {
$orig = GD::Image->newFromGif ("$root->{conf}->{site}->{public}/photos/temp.$file");
}
else {
$orig = GD::Image->newFromPng ("$root->{conf}->{site}->{public}/photos/temp.$file");
}
my $oWidth = $orig->width;
my $oHeight = $orig->height;
push (@msgs,"Received image $file ($oWidth x $oHeight)");
# Need to scale it down for the full-size copy?
if ($oWidth > $root->{photocfg}->{photos}->{fullwidth}) {
# Find out the aspect ratio.
my $ratio = $root->{photocfg}->{photos}->{fullwidth} / $oWidth;
my $fHeight = int($oHeight * $ratio);
my $fWidth = $root->{photocfg}->{photos}->{fullwidth};
# Create a new GD image.
my $full = GD::Image->new ($fWidth,$fHeight, 1);
# Copy the resized image over.
$full->copyResized (
$orig,
0, 0,
0, 0,
$fWidth, $fHeight,
$oWidth, $oHeight,
);
push (@msgs,"Resized full size image to $fWidth x $fHeight");
# Write the new image.
push (@msgs,"Wrote image l-$unique");
open (FULL, ">$root->{conf}->{site}->{public}/photos/$album/l-$unique");
binmode FULL;
print FULL $full->jpeg ($root->{photocfg}->{photos}->{quality});
close (FULL);
}
else {
# Doesn't need to be resized. Resave it anyway.
push (@msgs, "Wrote image l-$unique");
open (FULL, ">$root->{conf}->{site}->{public}/photos/$album/l-$unique");
binmode FULL;
print FULL $orig->jpeg ($root->{photocfg}->{photos}->{quality});
close (FULL);
}
# Need to scale it down for the thumbnail-size copy?
if ($oWidth > $root->{photocfg}->{photos}->{thumbwidth}) {
# Find out the aspect ratio.
my $ratio = $root->{photocfg}->{photos}->{thumbwidth} / $oWidth;
my $tHeight = int($oHeight * $ratio);
my $tWidth = $root->{photocfg}->{photos}->{thumbwidth};
# Create a new GD image.
my $thumb = GD::Image->new ($tWidth,$tHeight, 1);
# Copy the resized image over.
$thumb->copyResized (
$orig,
0, 0,
0, 0,
$tWidth, $tHeight,
$oWidth, $oHeight,
);
push (@msgs,"Resized thumbnail image to $tWidth x $tHeight");
# Write the new image.
push (@msgs,"Wrote image s-$unique");
open (THUMB, ">$root->{conf}->{site}->{public}/photos/$album/s-$unique");
binmode THUMB;
print THUMB $thumb->jpeg ($root->{photocfg}->{photos}->{quality});
close (THUMB);
}
else {
# Doesn't need to be resized. Resave it anyway.
push (@msgs, "Wrote image s-$unique");
open (THUMB, ">$root->{conf}->{site}->{public}/photos/$album/s-$unique");
binmode THUMB;
print THUMB $orig->jpeg ($root->{photocfg}->{photos}->{quality});
close (THUMB);
}
# Unlink the temp file.
unlink ("$root->{conf}->{site}->{public}/photos/temp.$file");
# Notify the photo manager about the new photo.
&writePhoto (
album => $album,
photo => $unique,
caption => $cap,
owner => $root->{me},
);
}
$root->{content} = "Photos Uploaded
\n\n"
. "The upload operation has completed successfully. "
. "View this photo album.\n\n"
. "The details of the operation are as follows:\n"
. "
\n"
. "- "
. join("
\n- ",@msgs)
. "
\n"
. "
\n";
}
elsif ($action eq 'editpic') {
# Only Photographers can upload photos.
if ($privilege !~ /^(owners|photographers)$/i) {
$root->{content} = "Permission Denied
\n\n"
. "Only photographers can access this page.";
return 1;
}
my $meta = &readPhotos($album);
my $cap = $meta->{$pic}->{caption} || '';
$album =~ s/\"/"/ig;
$cap =~ s/\"/"/ig;
$root->{content} = "Edit Caption
\n\n"
. "\n";
}
elsif ($action eq 'updatepic') {
# Only Photographers can upload photos.
if ($privilege !~ /^(owners|photographers)$/i) {
$root->{content} = "Permission Denied
\n\n"
. "Only photographers can access this page.";
return 1;
}
my $meta = &readPhotos($album);
my $newcap = $query->{caption} || '';
&writePhoto (
album => $album,
photo => $pic,
caption => $newcap,
owner => $meta->{$pic}->{owner},
);
$root->{content} = "Caption Updated
\n\n"
. "The caption has been updated. "
. "View the picture.";
}
elsif ($action eq 'delpic') {
# Only Photographers can upload photos.
if ($privilege !~ /^(owners|photographers)$/i) {
$root->{content} = "Permission Denied
\n\n"
. "Only photographers can access this page.";
return 1;
}
&deletePhoto ($album,$pic);
$root->{content} = "Photo Deleted
\n\n"
. "The photo has been deleted successfully. "
. "Return to the album.";
}
elsif ($action eq 'sort') {
# Only Photographers can sort pics.
if ($privilege !~ /^(owners|photographers)$/i) {
$root->{content} = "Permission Denied
\n\n"
. "Only photographers can access this page.";
return 1;
}
# Load this album's info.
my $meta = &readAlbum($query->{album});
$root->{meta}->{title} = $meta->{name};
# Get the pics in this album.
my $pics = &readPhotos($query->{album});
$root->{content} = "Sort Photos
\n\n";
# Sort the pictures.
my @prefSort = &getPicOrder($query->{album});
my %allPicIDs = %{$pics};
my @sorted = ();
foreach my $id (@prefSort) {
next unless exists $pics->{$id};
push (@sorted,$id);
delete $allPicIDs{$id};
}
push (@sorted, sort { $b cmp $a } keys %allPicIDs);
my $size = scalar(@sorted);
$root->{content} .= "\n";
$root->{content} .= qq~~;
}
elsif ($action eq "dosort") {
# Only Photographers can sort pics.
if ($privilege !~ /^(owners|photographers)$/i) {
$root->{content} = "Permission Denied
\n\n"
. "Only photographers can access this page.";
return 1;
}
# Load this album's info.
my $meta = &readAlbum($query->{album});
$root->{meta}->{title} = $meta->{name};
# Get the pics in this album.
my $pics = &readPhotos($query->{album});
# Get the sorted pics.
my $sorted = $query->{sorted};
my @order = split(/:/, $sorted);
# Update the ordering with the photo manager.
&setPicOrder($query->{album},@order);
$root->{content} = "Sort Photos
\n\n"
. "The photo album has been organized. "
. "{album}\">"
. "Return to the album.";
}