#!/usr/local/bin/perl -w # # xhibition admin.cgi (c) Marc Majcher 2002 # # Administrative interface for editing XML gallery configuration # files. Probably not the prettiest way to do it, but hey. # # Warning: Ass-ugliness ahead. # # I know, there's a ton of HTML in here, and that's # just plain wrong, but it's just an admin script, # so deal with it. use strict; use CGI qw(:standard); use XML::Twig; my $password = "ph0t0s"; my $cookie_name = "xhibition-admin"; my $cookie = cookie($cookie_name); my $crypted = crypt($password,"xh"); my $action = param('action'); my $conf_dir = "config"; my $image_dir = "images"; my $template_dir = "$conf_dir/admin"; my $xhconf_file = "$conf_dir/xhconf.xml"; my $table_width = 4; my %actions = ( index => \&index, show_gallery => \&show_gallery, edit_gallery => \&edit_gallery, show_image => \&show_image, edit_image => \&edit_image, render_all => \&render_all, render_gallery => \&render_gallery, import_directory => \&import_directory, generate_thumbs => \&generate_thumbs, ); $action = 'index' unless ((defined $action) && (defined $actions{$action})); # read gallery list from XML file my %config = read_config($xhconf_file); my $xhibition_url = $config{'base_url'}; my @gallery_files = @{$config{'galleries'}}; if ($crypted eq $cookie) { # Main section - process arguments blah blah blah my ($bodytext, $location) = &{$actions{$action}}; if (defined $location) { print <xhibition administration $bodytext HTML } } elsif (param('password') eq $password) { print <xhibition administration

Enter Password:

HTML } ##### action subs sub index { my $content = "

Xhibition Administration

"; my @new_dirs = &get_new_dirs; $content .= <Select a gallery file to edit:

HTML if (@new_dirs) { $content .= <Select a new directory to import:

HTML } else { $content .= "

(Put new image directories in \$XHROOT/$image_dir to add galleries.)" } $content .= <

Return to Xhibition

HTML return $content; } sub show_gallery { my $gallery = param('gallery'); my $t = parse_gallery($gallery); my $r = $t->root; my $gallery_name = $r->att('name'); $gallery_name =~ s/\s/_/g; my $gallery_title = $r->first_child('gallery_title')->text; my $gallery_text = $r->first_child('gallery_text')->text; my $gallery_teaser = $r->first_child('gallery_teaser')->text; my $gallery_date = $r->first_child('gallery_date')->text; my $display_gallery = $r->att('display'); my $display = "checked" unless (lc($display_gallery) eq "false"); my $html = <
Gallery: $gallery_name Gallery Title: Gallery Date: Gallery Text: Display Gallery:

HTML my $count = 0; my @images = $r->children('image'); for my $i (0..$#images) { my $image = $images[$i]; my $image_src = $image->att('src'); my $image_title = $image->first_child('title')->text; my $image_hidden = ""; if ($image->first_child('hide')) { $image_hidden = "
(hidden)"; } my $selected = ""; $selected = "checked" if ($image_src eq $gallery_teaser); my $image_thumb = $image_src; if ($image_thumb =~ /\//) { $image_thumb =~ s/^(.*\/)([^\/]+)$/$1$config{'thumbnail_prefix'}$2/; } else { $image_thumb = "$config{'thumbnail_prefix'}$image_thumb"; } $html .= <$image_title
$image_hidden
TD $count++; $html .= "
" unless ($count % $table_width); } $html .= <

Return to Xhibition Admin Index

HTML return $html; } sub show_image { my $image_src = param('image_src'); my $gallery = param('gallery'); my $t = parse_gallery($gallery); my $r = $t->root; my $image; for my $i ($r->children('image')) { next unless ($i->att('src') eq $image_src); $image = $i; last; } my $image_title = $image->first_child('title')->text; my $image_date = $image->first_child('date')->text; my $image_caption = $image->first_child('caption')->text; my $image_rotation; if ($image->first_child('rotation')) { $image_rotation = $image->first_child('rotation')->text; } my ($rot_0,$rot_90,$rot_180,$rot_270); if ($image_rotation eq "90") { $rot_90 = "checked"; } elsif ($image_rotation eq "180") { $rot_180 = "checked"; } elsif ($image_rotation eq "270") { $rot_270 = "checked"; } else { $rot_0 = "checked"; } my $hidden; if ($image->first_child('hide')) { $hidden = "checked"; } my $html = <
Showing image: $image_src Image Title: Image Date: Image Caption: Rotate Image: 0 90 180 270 Image Hidden:

HTML return $html; } sub edit_gallery { my $gallery = param('gallery'); my $t = parse_gallery($gallery); my $r = $t->root; for (qw(gallery_title gallery_text gallery_teaser gallery_date)) { my $element = $r->first_child($_); $element->set_text(param($_)); } if (param('display_gallery') eq "on") { $r->set_att('display' => "true"); } else { $r->set_att('display' => "false"); } write_gallery($gallery, $t); return ("

editing gallery $gallery

", "admin.cgi?action=show_gallery&gallery=$gallery"); } sub edit_image { my $image_src = param('image_src'); my $gallery = param('gallery'); my $t = parse_gallery($gallery); my $r = $t->root; my $image; for my $i ($r->children('image')) { next unless ($i->att('src') eq $image_src); $image = $i; last; } for (qw(date title caption)) { my $element = $image->first_child($_); $element->set_text(param($_)); } if (param('rotation') eq "0") { while ($image->first_child('rotation')) { $image->first_child('rotation')->delete; } } else { unless ($image->first_child('rotation')) { $image->insert_new_elt('last_child','rotation'); } $image->first_child('rotation')->set_text(param('rotation')); } if (param('hidden') ne "on") { while ($image->first_child('hide')) { $image->first_child('hide')->delete; } } else { $image->insert_new_elt('last_child','hide'); } # my $content = $t->sprint;; # $content =~ s//>/g; # return"
\n$content\n
"; write_gallery($gallery, $t); return ("

editing image $image_src

", "admin.cgi?action=show_gallery&gallery=$gallery"); } sub render_all { system("./bin/xhibition_render.pl"); system("./bin/index_gallery_text.pl"); return("Xhibition render successful","admin.cgi"); } sub render_gallery { my $gallery = param('gallery'); (my $gallery_dir = $gallery) =~ s/\.xml$//; system("./bin/xhibition_render.pl -g $conf_dir/$gallery"); system("./bin/index_gallery_text.pl"); return <Gallery rendered: $gallery

Return | View

HTML } sub generate_thumbs { my $gallery = param('gallery'); system("./bin/xhibition_render.pl -tT -g $conf_dir/$gallery"); return "thumbs generated for $gallery", "admin.cgi?action=show_gallery&gallery=$gallery"; } sub import_directory { my $dir = param('dir'); system("./bin/import_directory.pl -a $image_dir/$dir"); return "importing dir: $dir","admin.cgi"; } ##### sub get_new_dirs { my @new_dirs; opendir(IMG,$image_dir) || die "can't read $image_dir: $!"; for my $img (readdir IMG) { next if ($img =~ /^[._]/); next if ($img eq "CVS"); next unless (-d "$image_dir/$img"); next if (-e "$conf_dir/$img.xml"); push @new_dirs, $img; } closedir(IMG); return @new_dirs; } sub parse_gallery { # takes an xml config file, reads it, returns the twig my $gallery = shift || return; my $gallery_file = "$conf_dir/$gallery"; # Read and parse gallery XML file my $content; open(GXML, $gallery_file) || die "can't open $gallery_file: $!"; { local $/ = undef; $content = ; } close(GXML); my $twig = XML::Twig->new(PrettyPrint => 'indented'); $twig->parse($content); return $twig; } sub write_gallery { # takes an xml config file and a twig, writes twig to file my $gallery = shift || return; my $twig = shift || return; my $gallery_file = "$conf_dir/$gallery"; open(OUT,">$gallery_file") || die "can't open $gallery_file for writing:$!"; print OUT $twig->sprint; close(OUT); } ## Ohhh, look at the False Laziness! Refactor, lazy! sub read_config { # Reads an XML configuration file, and returns a hash of values my $file = shift || return; my %config; my $content; open(CONF,$file) || die "can't open config file $file: $!"; { local $/ = undef; $content = ; } close(CONF); my $t = XML::Twig->new(PrettyPrint => 'indented'); $t->parse($content); my $r = $t->root; $config{'base_url'} = $r->first_child('base_url')->text; $config{'root_dir'} = $r->first_child('root_directory')->text; $config{'thumbnail_size'} = $r->first_child('thumbnail_size')->text; $config{'gallery_width'} = $r->first_child('gallery_width')->text; $config{'index_template'} = $r->first_child('index_template')->text; $config{'gallery_template'} = $r->first_child('gallery_template')->text; $config{'photo_template'} = $r->first_child('photo_template')->text; $config{'page_extension'} = $r->first_child('page_extension')->text; $config{'max_image_width'} = $r->first_child('max_image_width')->text; $config{'thumbnail_prefix'} = $r->first_child('thumbnail_prefix')->text; my @galleries; for my $gtwig ($r->first_child('galleries')->children('gallery_file')) { push @galleries, $gtwig->text; } $config{'galleries'} = \@galleries; return %config; }