#!/usr/bin/perl

###############################################################################
#
#        viewpics.cgi v1.0 by Paul Schnell
#        -----------------------------------------------------------------------
#        The script is designed for reading and displaying an online "Album" of
#         images. Images are categorised into CATEGORIES and GROUPS - these are
#        derived from the location and name of the folders in which the images
#        reside. All paths are defined relative to this script.
#        -----------------------------------------------------------------------
#        viewpix@touchpowder.com
#        www.touchpowder.com/scripts/perl/viewpix
#
################################################################################
#################################################################################
# DISCLAIMER OF WARRANTY                                                                                                                #
# THIS SOFTWARE AND ACCOMPANYING DOCUMENTATION ARE PROVIDED "AS IS" AND                        #
# WITHOUT WARRANTIES AS TO PERFORMANCE OF MERCHANTABILITY OR ANY OTHER                        #
# WARRANTIES WHETHER EXPRESSED OR IMPLIED.   BECAUSE OF THE VARIOUS HARDWARE        #
# AND SOFTWARE ENVIRONMENTS INTO WHICH VIEWPIX MAY BE USED, NO WARRANTY OF                #
# FITNESS FOR A PARTICULAR PURPOSE IS OFFERED.                                                                  #
# THE USER MUST ASSUME THE ENTIRE RISK OF USING THIS PROGRAM AND ITS COMPONENTS.#
# IN NO CASE SHALL THE AUTHOR BE LIABLE FOR ANY INCIDENTAL, SPECIAL OR                        #
# CONSEQUENTIAL DAMAGES OR LOSS, INCLUDING, WITHOUT LIMITATION, LOST PROFITS        #
# OR THE INABILITY TO USE EQUIPMENT OR ACCESS DATA, WHETHER SUCH DAMAGES ARE        #
# BASED UPON A BREACH OF EXPRESS OR IMPLIED WARRANTIES, BREACH OF CONTRACT,                #
# NEGLIGENCE, STRICT TORT, OR ANY OTHER LEGAL THEORY. THIS IS TRUE EVEN IF                #
# THE AUTHOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. IN NO CASE WILL                #
# THE AUTHOR'S LIABILITY EXCEED THE AMOUNT OF THE LICENSE FEE ACTUALLY PAID                #
# BY LICENSEE TO  THE AUTHOR.                                                                                                         #
#################################################################################

use CGI;
$r = new CGI;

#-----------------------------------------------------
# Common Globals
#-----------------------------------------------------
require "common_conf.cmp";        # config vars

#-----------------------------------------------------
# Album Specific Files
#-----------------------------------------------------
$script                 = "viewpix.cgi";        # name of this file
$my_userdb                 = "my_users.dat";        # name of users file

#-----------------------------------------------------
# Album Preferences
#-----------------------------------------------------
$no_of_cols         = 3;                                #  how many columns to display per page
$no_of_rows         = 3;                                # how many rows to display per page
$rand_count                = 3;                                # how many random images to display
$use_myalbum        = 0;                                # allow users to create albums (1=yes|0=no)
$use_email_pic        = 0;                                # allow users to emails pics (1=yes|0=no)
$show_usralbums = 0;                                # display users' albums (1=yes|0=no)
$th_width                = 120;                                # thmbnail image width
$th_height                = 90;                                # thumbnail image height

$cookie_user        = "vpxU";                        # name of Registration cookie
$cookie_temp        = "vpxT";                        # name of session cookie
$site_name                = "Viewpix";                # name of site for HTML: <title>$site_name</title>
#-----------------------------------------------------
# Colours
#-----------------------------------------------------
$bg_light                 = "#EEEEEE";
$bg_x_light         = "#F7FAFB";
$bg_border                 = "#83BCE7";
$bg_warn                 = "#FFD9D9";
$bg_good                = "#C8F7CF";
$bg_nav_off                = "#A5C3D6";
$bg_nav_hot                = "#FFFFFF";
$bg_nav                        = "#197fbb";
$bg_mgroup                = "#1E669B";        # menu 'group' header bg colour
$tableline                 = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><tr><td bgcolor=\"$bg_border\"><img src=\"$img_common/xx.gif\" width=1 height=1></td></tr></table>";
$blank_img                = "$img_common/xx.gif";


#-----------------------------------------------------
# Get user name from cookie, if there is one
#-----------------------------------------------------
$uusr = (defined $r->cookie($cookie_user)) ? $r->cookie($cookie_user) : (defined $r->cookie($cookie_temp)) ? $r->cookie($cookie_temp) : "";

#-----------------------------------------------------
# Module logic handling
#-----------------------------------------------------
if                 ($r->param('mod') eq "view")         { &mod_view; }
elsif         ($r->param('mod') eq "my")                 { &mod_my; }
elsif         ($r->param('mod') eq "users")         { &mod_user_albums; }
elsif         ($r->param('mod') eq "mailpic") { &mod_email_pic; }
else { &mod_intro; }


##############################################################
# 'MY' album module
#-------------------------------------------------------------
sub mod_my {
        require "$component_loc/album_my.cmp";

        if($use_myalbum){
                if ($r->param('action') eq "viewMyitem") {&viewMyitem;}
                elsif ($r->param('action') eq "myalbum")         {&myalbum;}
                elsif ($r->param('action') eq "user_reg")         {&user_reg;}
                elsif ($r->param('action') eq "user_login") {&user_login;}
                elsif ($r->param('action') eq "my_reg")         {&my_reg;}
                elsif ($r->param('action') eq "my_info")         {&my_info;}
                elsif ($r->param('action') eq "update_info"){&update_info;}
                elsif ($r->param('action') eq "addpic")         {&myalbum_addpic;}
                elsif ($r->param('action') eq "deletepic")         {&myalbum_deletepic;}
        }
        else { &mod_intro; }

}

##############################################################
# User Albums module
#-------------------------------------------------------------
sub mod_user_albums {
        require "$component_loc/album_user_albums.cmp";
        require "$component_loc/album_my.cmp";

        if ($r->param('action') eq "useralbums") {&useralbums;}
        elsif ($r->param('action') eq "view_usr_alb"){&view_usr_alb;}
        elsif ($r->param('action') eq "viewMyitem") {&viewMyitem;}
        else { &mod_intro; }

}

##############################################################
# View Album module
#-------------------------------------------------------------
sub mod_view {
        require "$component_loc/album_view.cmp";
        require "$component_loc/album_randpic.cmp";

        if    ($r->param('action') eq "viewgroup")         {&viewgroup;}
        elsif ($r->param('action') eq "viewcat")         {&viewcat;}
        elsif ($r->param('action') eq "viewitem")         {&viewitem;}
        else { &mod_intro; }
}

##############################################################
# Email a pic module
#-------------------------------------------------------------
sub mod_email_pic {
        require "$component_loc/album_email_pic.cmp";

        if ($r->param('action') eq "emailpic")         {&emailpic;}
        elsif ($r->param('action') eq "confirmmail"){&confirmmail;}
        elsif ($r->param('action') eq "sendemail")         {&sendemail;}
        else { &mod_intro; }

}

##############################################################
# Album Intro module
#-------------------------------------------------------------
sub mod_intro {
        require "$component_loc/album_intro.cmp";
        require "$component_loc/album_randpic.cmp";

        &intro;
}


######################################################
#### header ##########################################
######################################################
sub header {

        my $caption = shift;
        my $mcat         = $r->param('cat');
        my $mgrp         = $r->param('group');
        my $mact         = $r->param('action');
        my $mmod          = $r->param('mod');
        my $mode        = $r->param('mode');
        $caption = ($caption eq '') ? "The Album" : $caption;

        my $passref = $ENV{'QUERY_STRING'};
        $passref =~ s/=/-/g;
        $passref =~ s/\&/|/g;

        print qq~
                <html>
    <head>
<title>Andrew and Louise Townsin</title>
<script language="JavaScript" src="/js/common.js"></script>
<link rel="STYLESHEET" type="text/css" href="/css/default.css">
</head>
<body class="bodyStyle" bgcolor="White" text="#303030" alink="Red" vlink="Red" link="Red">
        ~;

        # get the album menu data
        open (MENU, "$data_loc/$menufile") or &err("[main] cant get menu file",1);
        my @menudat = <MENU>;
        close (MENU);

        # total images is the first line of the file
        my $imagetotal = shift @menudat;
        chomp $imagetotal;
        # open the menu
        print qq~
                <div id="flatNav">
                <table border="0" cellpadding="1" cellspacing="1" width="137">
        <tr><td bgcolor="#A5C3D6"><img src="/images/xx.gif" width="1" height="1" alt="" border="0"></td></tr>
        <tr><td bgcolor="#98B5C9"><a class="navLink" href="/">Home</a></td></tr>
        <tr><td bgcolor="#A5C3D6"><img src="/images/xx.gif" width="1" height="1" alt="" border="0"></td></tr>
        <tr><td bgcolor="#98B5C9"><a class="navLink" href="/cgi-bin/viewpix.cgi">Photo Album</a></td></tr> ~;

        # if the 'my album' option is on
        if($use_myalbum){
                my $reg_user = (defined $r->cookie($cookie_temp)) ? $r->cookie($cookie_temp) : (defined $r->cookie($cookie_user))? $r->cookie($cookie_user) : 0;
                my $ubgcol         = ($mmod eq "my") ? $bg_nav : $bg_mgroup;
                my $ulclass = ($mmod eq "my") ? "navLink" : "navHead";
                print qq~
                        <tr><td>$tableline</td></tr>
                        <tr><td bgcolor="$ubgcol"><a class="$ulclass" href="$script?mod=my&action=myalbum">My Album</a></td></tr>        ~;
                # if the current user is registered & we are viewing the 'my' module
                if($reg_user && $mmod eq "my"){
                        # set bg and link classes according to mode=edit
                        my $ebgcol         = ($mode eq "edit") ? $bg_nav_hot : $bg_nav_off;
                        my $elclass = ($mode eq "edit") ? "navLinkHot" : "navLink";
                        my $ibgcol         = ($mact eq "my_info") ? $bg_nav_hot : $bg_nav_off;
                        my $ilclass = ($mact eq "my_info") ? "navLinkHot" : "navLink";
                        #~~~~~~~~~~~~~~~~~
                        print qq~
                                <tr><td bgcolor="$ebgcol"><a class="$elclass" href="$script?mod=my&action=myalbum&mode=edit">Edit my album</a></td></tr>
                                <tr><td bgcolor="$ibgcol"><a class="$ilclass" href="$script?mod=my&action=my_info">Edit my info</a></td></tr>        ~;
                }
                if($show_usralbums){
                        my $obgcol         = ($mmod eq "users") ? $bg_nav_hot : $bg_mgroup;
                        my $olclass = ($mmod eq "users") ? "navLinkHot" : "navHead";
                        print qq~
                                <tr><td bgcolor="$obgcol"><a class="$olclass" href="$script?mod=users&action=useralbums">View user albums</a></td></tr> ~;
                }
        }

        # start the core menu
        foreach(@menudat){
                chomp;
                my @catgrp = split(/\|/, $_);
                my $cat = shift @catgrp;
                my $cplain = $cat;
                $cplain =~ tr/_/ /;
                # set bg and link classes according to cat=xx_catname
                my $cbgcol         = ($cat eq $mcat) ? $bg_nav : $bg_mgroup;
                my $clclass = ($cat eq $mcat) ? "navLink" : "navHead";
                #~~~~~~~~~~~~~~~~~
                print qq~
                        <tr><td>$tableline</td></tr>
                        <tr><td bgcolor="#CCCCCC" class="navHead">$cplain</td></tr>        ~;
                        #<tr><td bgcolor="$cbgcol"><a class="$clclass" href="$script?mod=view&action=viewcat&cat=$cat">$cplain</a></td></tr>
                #if($cat eq $mcat){
                        foreach(@catgrp){
                                my $grp = $_;
                                $grp =~ tr/_/ /;
                                # set bg and link classes according to group=xx_groupname
                                my $gbgcol         = ($_ eq $mgrp) ? $bg_nav_hot : $bg_nav_off;
                                my $glclass = ($_ eq $mgrp) ? "navLinkHot" : "navLink";
                                #~~~~~~~~~~~~~~~~~
                                print qq~
                                        <tr><td bgcolor="$gbgcol"><a class="$glclass" href="$script?mod=view&action=viewgroup&cat=$cat&group=$_">$grp</a></td></tr>        ~;
                        }
                #}
        }
        print qq~
                </table>
                </div>
        ~;
        print qq~
        <div id="logo">
        <table border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td valign="top"><img src="/images/logo.gif" width="350" height="68" alt="" border="0"></td>
            </tr>
        </table>
        </div>
                <div id="headline"><nobr>$caption</nobr></div>

        <div id="count">$imagetotal images</div>
                <div id="albumCont">
                <!--- open body --->

        ~;

}
######################################################
## Footer ############################################
######################################################
sub footer {
        print qq~
                <!--- close body --->
                <br><br>
                <table border="0" width="615" cellpadding="0" cellspacing="0">
                <tr><td>$tableline</td></tr>
                <tr><td align="right"><img src="$img_common/vp_powered.gif" width="70" height="16" alt="" border="0"></td></tr>
                </table>
                </div>
        </body>
        </html>
        ~;
}

######################################################
#  Error ############################################
######################################################
sub err {
        my $error = shift;
        my $dohead = shift;
        #----------------------
        if(!$dohead){&header;}
        #----------------------
        print qq~
        <table border="0" width="570" cellspacing="1" cellpadding="3">
                <tr><td class="tdBold">The script encountered the following error:</td></tr>
                <tr><td>$tableline</td></tr>
                <tr><td class="tdNormal" bgcolor="$bg_warn">$error</td></tr>
                <tr><td>$tableline</td></tr>
        </table>
        ~;
        #----------------------
        &footer;
        #----------------------
        exit;

}

###################################################
### DATE ##########################################
###################################################
sub date {

# Define arrays for the day of the week and month of the year.           #
    my @months = ('01','02','03','04','05','06','07','08','09','10','11','12');

    my ($sec,$min,$hour,$mday,$mon,$ryear,$wday,$yday) = (localtime(time))[0,1,2,3,4,5,6,7];
    $ryear += 1900;

        my $rmonth = $months[$mon];
        $rmonth = "0" . $rmonth  if (length($rmonth) < 2);
        $mday = "0" . $mday  if (length($mday) < 2);

        return my $rdate = "$mday/$rmonth/$ryear";

}
