#!/usr/bin/perl
###############################################################################
# imageFolio.cgi (lightbox ready)                                             #
###############################################################################
# BizDesign ImageFolio Professional Edition
# BizDesign Software
# written by Greg Raaum, webmaster@imageFolio.com
# Available from http://www.ImageFolio.com
# ---------------------------------------------------------------------------
# PROGRAM NAME : ImageFolio Pro Edition
# VERSION : 2.26
# LAST MODIFIED : 04/12/2001
# ===========================================================================
# COPYRIGHT NOTICE :
#
# Copyright (c) 1999-2001 BizDesign, Inc. All Rights Reserved.
# Selling the code for this program without prior written consent is
# expressly forbidden.
#
# Obtain written permission before redistributing this software over the
# Internet or in any other medium.  In all cases copyright and header must
# remain intact.
#
# Feel free to modify the code of this program to suit your likings.
#
# Although this program has been thoroughly tested on BizDesign's servers, we
# do not warrant that it works on all servers and will not be held liable
# for anything, including but not limited to, misusage, error, or loss of data.
#
# Use at your own risk!
###############################################################################
# Do not modify below this line unless you know what you are doing.
###############################################################################

use vars qw/$libpath $referrer_check $domain $image_directory/;

eval {
   ($0 =~ m,(.*)/[^/]+,)   && unshift (@INC, "$1");   # Get the script location: UNIX
   ($0 =~ m,(.*)\\[^\\]+,) && unshift (@INC, "$1");   # Get the script location: Windows

   require "config.pl";           # Change this to the absolute path if you have problems.
   if ($display_lightbox==1){ require "$lightbox_scripts_path/lightbox_config.pl"; }
   require "$libpath/shared.pl";    # Change this to the absolute path if you have problems.
   &top;
};

if ($@) { print "Content-type: text/html\n\n<pre>Script Error: $@</pre>\n"; }

exit;

###############################################################################
# TOP
# Determines what to do
###############################################################################
sub top {

   $| = 1;   # flush output

   local(%FORM) = &parse_form;

   &load_values;
   if (!$FORM{'lightbox'}){ if ($referrer_check) { &check_referrer; }}
   if ($FORM{'action'} eq "view") { require "$libpath/images.pl";                 &view_image;      }
   elsif ($FORM{'direct'})        { require "$libpath/thumbnails.pl";             &gen_image_table; }
   elsif ($FORM{'search'})        { require "$libpath/search.pl";                 &search;          }
   elsif ($FORM{'lightbox'})      { require "$lightbox_scripts_path/lightbox.pl"; &gen_image_table; }
   else                           { require "$libpath/home.pl";                   &gen_home_page;   }
}

###############################################################################
# CHECK REFERRER
# Make sure we are pulling this page from the site and not a bookmark or link
###############################################################################
sub check_referrer {
   my $ok;
   for(@ref) { if ($ENV{'HTTP_REFERER'} =~ /$_/) { $ok = 1; } }
   if ($ok != 1) { print "Content-type: text/html\nLocation:$domain\n\n"; exit; }
}

###############################################################################
# LOAD VALUES
# Establishes default values for commonly used variables
###############################################################################
sub load_values {
   my ($found, $i, $pos, $count, $cnt, $nav, $subcatlinks, $thumbimages, $template);

   if ($FORM{'direct'}) {
     if ( (!(-e "$image_directory/$FORM{'direct'}")) ||
       (!(-d "$image_directory/$FORM{'direct'}")) ||
       ($FORM{'direct'} =~ /\.\./) ) {
       print qq|Content-type: text/html\n\n<br><br><center><font face="Verdana,Arial,Helvtica" size=2>
       <font color="red"><b>Error!</font>  $FORM{'direct'}</b> is not a valid category!<br><br>
       <a href="javascript:history.go(-1)"><b>Return to Previous Page</b></a></font></center>|;
       exit;
     }
     $thisdirectory = get_directory($FORM{'direct'});
     $newthisdirectory = get_clean_name($thisdirectory);
   }
   elsif ($FORM{'link'}) {
     $thisdirectory = get_directory($FORM{'link'});
     $newthisdirectory = get_clean_name($thisdirectory);
   }
}

###############################################################################
# PARSE THE FORM
# Parses the form input and returns a hash with all the name
# value pairs.
###############################################################################
sub parse_form {
   my (@pairs, %FORM);
   my ($buffer, $pair, $name, $value);

   if ($ENV{'REQUEST_METHOD'} eq 'GET') {
     @pairs = split(/&/, $ENV{'QUERY_STRING'});
   }
   elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
     read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
     @pairs = split(/&/, $buffer);
   }

   PAIR: foreach $pair (@pairs) {
     ($name, $value) = split(/=/, $pair);
     $name =~ tr/+/ /;
     $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
     $value =~ tr/+/ /;
     $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
     $value =~ s/\.\.//g;
     $value =~ s/<!--(.|\n)*-->//g;
     if ($FORM{$name} && ($value)) { $FORM{$name} = "$FORM{$name},$value"; }
     elsif ($value) { $FORM{$name} = $value; }
   }
   return %FORM;
}

