#!/usr/bin/perl -w

#############################################################################
#                                                                           #
#           Guestwho? The Ultimate Guestbook  V 2.1 Beta                    #
#                                                                           #
#                   Copyright Skeleton Man 2003                             #
#                     http://www.guestwho.com                               #
#                                                                           #
#   This program is free software; you can redistribute it and/or modify    #
#   it under the terms of the GNU General Public License as published by    #
#   the Free Software Foundation; either version 2 of the License, or       #
#   (at your option) any later version.                                     #
#                                                                           #
#   This program is distributed in the hope that it will be useful,         #
#   but WITHOUT ANY WARRANTY; without even the implied warranty of          #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           #
#   GNU General Public License (docs/GPL) for more details.                 #
#                                                                           #
#                                                                           #
#############################################################################

# Set the path to the config file and load it:

$requirefile = "./gwconfig.pl"; 
require $requirefile;

#########################################################################
#                                                                       #
#               DO NOT modify anything past this line!                  #
#                                                                       #
#########################################################################

use strict;                 # Enable strict mode for everything.

# Error handling routine:

# 1. Fatal errors to the screen.
# 2. Other errors and warnings to log.

BEGIN 
{
 use CGI::Carp qw(fatalsToBrowser carpout);
 my $error_log = "guest-errors.txt";
 open(LOG, ">$error_log") or die("Unable to open $error_log: $!\n");
 carpout(\*LOG);
}

use CGI qw(:standard);      # Processes raw form data for us.
use CGI::Cookie;            # Processes raw cookie data for us.
use File::Copy;             # Provides a simple method for copying/moving files.
use Cwd;                    # Used to get the current working directory.


    # Declare all our global variables (ones that are available to the entire program.
    # For legacy perl support (pre 5.6.1) we are using "use vars" as opposed to "our()":

use vars qw(@required_fields @params %html_fields %action_subs $action $datenow $timenow 
            $scripturl $host $entrylog $perpage $adminpass $adminfile $welcomefile $submitfile 
            $entrytemplate $entriesfile $uid $chpassfile $emailhide  $editfile $smutfile 
            $sub $maxlength $pagetitle $ubbenable $timedif $iconpath $requirefile $pagesig 
            $delete_confirm $edit_confirm $chpass_confirm $countriesfile $blankpass_error 
            $pass_nomatch_error $thankyou_confirm $incorrect_old_pass_error $navfile
            $already_posted_error $sort_template $results_template $results_layout 
            $search_template $actionsdir $commondir $list $header_done $previewfile
            $exp_template $exp_res_template %delims $pwd);


    # First we need to gather a few bits of information about the script:
    
    # 1. An array of all form fields (parameters) we received.
    # 2. The hostname of the site on which we are running.
    # 3. The requested action to perform (e.g view, search, etc).
    # 4. The relative URL to this script (e.g. /cgi-bin/guestwho.cgi). 
    
@params     = param();
$host       = $ENV{'HTTP_HOST'};
$action     = param("action") || '';
$scripturl  = $ENV{'SCRIPT_NAME'};

if (&Cwd::cwd() =~ /(.+)/){ $pwd = $1; }


    # Now we define which function should be associated with which action:
    # These should be in the format: 'action_name' => 'function_name'.

%action_subs = ('admin'=>'admin','admin_delete'=>'delete_entry','admin_edit'=>'edit_entry',
                'admin_do_edit'=>'do_edit','submit'=>'parse_form','showform'=>'show_form',
                'view'=>'show_entries','admin_chpass'=>'change_pass','search'=>'search_entries',
                'admin_export' => 'export_entries','parse'=>'parse','showtime'=>'datetime');


# Load GuestWho common libraries:
load_subs($commondir,'',0);
 
# Now get the date and time:
$datenow = datetime("date");
$timenow = datetime("time");
 
 
    # Load the required action libraries as follows:

    # 1. If we have a valid action to call:
    #   a. Call the load_subs() function to load the requested action.

    # 2. If we don't have a valid action:
    #   a. Load the welcome function into the script.
    #   b. Call the function to print the welcome page.


if ($action_subs{$action} && $action =~ /^admin_/){
 my ($act,$func) = split (/_/,$action,2);
 load_subs("$actionsdir/admin",'',1);
 load_subs("$actionsdir/admin",$func,1);
}elsif ($action eq "admin"){
 load_subs($actionsdir,"admin",1);
}elsif ($action_subs{$action}){
 load_subs($actionsdir,$action,0);
}else{
 require "$actionsdir/welcome.pl";
 welcome();
}
 
    # Here we define the load_subs() funtion to load our libraries.
    # This function loads all library files in the given location:

sub load_subs
{
 my ($dir,$function,$no_recurse) = @_;
 my ($loadir,$loaddir,$sub);
 
 $loadir = ($function) ? "$dir/$function" : $dir;
 if ($loadir =~ /^(.+)$/){ $loaddir = $1; }
 
 opendir(DIR, $loaddir) || die ("Can't open $loaddir: $!");
 foreach my $file (readdir(DIR)){
  next if ($file eq "." || $file eq "..");
  if ($file =~ /^(\w+\.pl)$/){ 
   require "$loaddir/$1"; 
   $list .= "$file ";
  }elsif(-d "$loaddir/$file" && !$no_recurse){
   load_subs("$loaddir/$file",'');
  }else{
   ;
  }
 }
 closedir(DIR);
 
 if ($function && $action =~ /^admin_/){ $function = $action; }
 
 if ($function){
  $sub = \&{$action_subs{$function}};
  &$sub();
 }
}
