#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);
use strict;
use vars qw(@Month @WDay $dateformat $flop $datapath $mailserver $admin $page $lpage $npages $module $flock %in %cookie $basepath $cgipath $cgiurl $htmlpath $htmlurl $imageurl $imagepath $username $password $imageurl $sendmail);


#####################################################################
# csDynamic - 1.0 - 072803
#
#####################################################################
#                                                                   #
#    Copyright © 1999-2003 CGISCRIPT.NET - All Rights Reserved      #
#                                                                   #
#####################################################################
#                                                                   #
#          THIS COPYRIGHT INFORMATION MUST REMAIN INTACT            #
#                AND MAY NOT BE MODIFIED IN ANY WAY                 #
#                                                                   #
#####################################################################
#
# When you downloaded this script you agreed to accept the terms 
# of this Agreement. This Agreement is a legal contract, which 
# specifies the terms of the license and warranty limitation between 
# you and CGISCRIPT.NET. You should carefully read the following 
# terms and conditions before installing or using this software.  
# Unless you have a different license agreement obtained from 
# CGISCRIPT.NET, installation or use of this software indicates 
# your acceptance of the license and warranty limitation terms
# contained in this Agreement. If you do not agree to the terms of this
# Agreement, promptly delete and destroy all copies of the Software.
#
# Versions of the Software 
# Only one copy of the registered version of CGISCRIPT.NET 
# may used on one web site.
# 
# License to Redistribute
# Distributing the software and/or documentation with other products
# (commercial or otherwise) or by other than electronic means without
# CGISCRIPT.NET's prior written permission is forbidden.
# All rights to the CGISCRIPT.NET software and documentation not expressly
# granted under this Agreement are reserved to CGISCRIPT.NET.
#
# 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 CGISCRIPT.NET MAY BE USED, NO WARRANTY 
# OF FITNESS FOR A PARTICULAR PURPOSE IS OFFERED.  THE USER MUST ASSUME THE
# ENTIRE RISK OF USING THIS PROGRAM.  ANY LIABILITY OF CGISCRIPT.NET WILL BE
# LIMITED EXCLUSIVELY TO PRODUCT REPLACEMENT OR REFUND OF PURCHASE PRICE.
# IN NO CASE SHALL CGISCRIPT.NET 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
# CGISCRIPT.NET IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. IN NO CASE WILL
# CGISCRIPT.NET' LIABILITY EXCEED THE AMOUNT OF THE LICENSE FEE ACTUALLY PAID
# BY LICENSEE TO CGISCRIPT.NET.
#
# Credits:
# Andy Angrick - Programmer - angrick@cgiscript.net
# Mike Barone - Design - mbarone@cgiscript.net
#
# For information about this script or other scripts see 
# http://www.cgiscript.net
#
# Thank you for trying out our script.
# If you have any suggestions or ideas for a new innovative script
# please direct them to suggest@cgiscript.net.  Thanks.
#
########################################################################



$basepath = "./";
$module=0;
$admin=0;
@Month = ('','January','February','March','April','May','June','July','August','September','October','November','December');
@WDay = ('Every Day','Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$in{'scriptname'} = 'csDynamic.cgi';

require("$basepath/libs.cgi");

if(-f "$basepath/setup.cgi"){
	require("$basepath/setup.cgi");
	$in{'cgiurl'} = $cgiurl.'/'.$in{'scriptname'};
	$in{'imageurl'} = $imageurl;
	}
	
if($cgiurl =~ /\/mod\//){
	$module=1;
	}
else{
	$module=0;
	}
	
&main;

sub main{
&getdata;
print "Content-type: text/html\n\n";

if((! -e "$basepath/setup.cgi")&&($in{'command'} eq "")){
	&DoSetup;
	}
	
if($in{'command'} eq "savesetup"){
	if(-e "$basepath/setup.cgi"){
		&PError("Error. Permission denied.");
		}
else{
	&SaveSetup;
	}
}

($in{'command'} eq "")&&(&DoLogin);
($in{'command'} eq "login")&&(&DoLogin);
($in{'command'} eq "view")&&(&View);
($module)?(&GetLoginMod):(&GetLogin);
($in{'command'} eq "manage")&&(&Manage);
($in{'command'} eq "showadd")&&(&ShowAdd);
($in{'command'} eq "showedit")&&(&ShowEdit);
($in{'command'} eq "add")&&(&Add);
($in{'command'} eq "savechanges")&&(&SaveChanges);
($in{'command'} eq "delete")&&(&Delete);
($in{'command'} eq "showaddc")&&(&ShowAddC);
($in{'command'} eq "showeditc")&&(&ShowEditC);
($in{'command'} eq "addc")&&(&AddC);
($in{'command'} eq "savechangesc")&&(&SaveChangesC);
($in{'command'} eq "deletec")&&(&DeleteC);
($in{'command'} eq "slw")&&(&ShowLinksWizard);
($in{'command'} eq "slwm")&&(&ShowLinksWizard);
($in{'command'} eq "scp")&&(&ShowCP);
($in{'command'} eq "cp")&&(&ChangePass);
($in{'command'} eq "preview")&&(&Preview);
($in{'command'} eq "mu")&&(&UPMD(-3));
($in{'command'} eq "md")&&(&UPMD(3));
($in{'command'} eq "rt")&&(&RT);
}


sub RT{
my($id) = $in{'id'};
(!$id)&&(PError("No record selected to edit"));
($id =~ /[^\d]/)&&(&PError("Error. Invalid id"));
my($cid) = $in{'cid'};
(!$cid)&&(PError("No group selected to edit"));
($cid =~ /[^\d]/)&&(&PError("Error. Invalid group id"));

my(@l,$line);
open(DB,"+<$datapath/dynamics.cgi");
($flock)&&(flock(DB,2));
while(<DB>){
chomp;
my(@f) = split("\t",$_);
if($f[0] == $cid){
	$f[7] = $id;
	$line = join("\t",@f);
	}
else{
	$line = $_;
	}
push(@l,$line);
}


if(@l){
	seek(DB,0,0);
	foreach my $i (@l){
		print DB "$i\n";
		}
	truncate(DB, tell(DB));
	}

($flock)&&(flock(DB,2));
close DB;

&Manage;
exit;
}

sub UPMD{
my($x) = @_;
my($aid,$fid,@l,$count,%r,$mc);
my($id) = $in{'id'};
(!$id)&&(PError("No record selected to edit"));
($id =~ /[^\d]/)&&(&PError("Error. Invalid id"));
my($cid) = $in{'cid'};
(!$cid)&&(PError("No group selected to edit"));
($cid =~ /[^\d]/)&&(&PError("Error. Invalid group id"));

open(DB,"+<$datapath/$cid.cgi");
($flock)&&(flock(DB,2));
while(<DB>){
my(@f) = split("\t",$_);
$count += 2;

if($f[0] == $id){
	$mc = $count+$x;
	}
else{
	$mc = $count;
	}

$mc = sprintf("%.4d",$mc);

#print "MC: $mc - $_<br>";
$r{$mc} = $_;
}

seek(DB,0,0);

foreach my $i (sort keys %r){
#	print "R: $i - $r{$i}<br>";
	print DB $r{$i};
	}

truncate(DB, tell(DB));
($flock)&&(flock(DB,8));
close DB;
	
&Manage;
exit;
}

sub ShowLinksWizard{
my($command,$copt,$cid,$sel,$rurl,$selected);
$cid = $in{'cid'};
$command = $in{'command'};

my $line = "";
my $result = "";

	open(DB,"<$datapath/dynamics.cgi");
	while(<DB>){
		chomp;
		my($id,$catname,$uid,@rest) = split("\t",$_);
		($module)&&(!$admin)&&($uid ne $in{'UserName'})&&(next);
		(!$cid)&&($cid = $id);
			if ($id == $cid) {
				$selected = 'selected';
				$in{"CatName"} = $catname; 
				}
			else{
				$selected = '';
				}
		$in{'copt'} .= qq|<option value=\"$id\" $selected>$catname</option>\n|;
		}
	close DB;

if(!$in{'copt'}){
	if($command eq "slwm"){
		print "<script language=javascript>\n";
		print "alert(\"No content defined\");\n";
		print "window.close();\n";
		print "</script>\n";
		dbexit();
	}
	else{
		$copt = "<option value=\"\">No content configured</option>\n";
		}
	}
else{
	($cid eq 'all')?($sel='selected'):($sel='');
	}
$cgiurl = $in{'cgiurl'};
$in{'cid'} = $cid;

$rurl = $cgiurl;
$rurl =~ s/http:\/\/.*?\//\//i;
$in{'jcode'} = "<script language=javascript src=\"$cgiurl?command=view&cid=$cid&j=1\"></script>";
$in{'ssicode'} = "<!--#include virtual=\"$rurl?command=view&cid=$cid\" -->";
$in{'phpcode'} = "<? include \"$cgiurl?command=view&cid=$cid\" ; ?>";

if($command eq "slwm"){
	&PageOut("$cgipath/t_linkswizardmod.htm");
	}
else{
	&PageOut("$cgipath/t_linkswizard.htm");
	}
exit;
}

sub View{
my($cid) = $in{'cid'};
my($isset,$id,$rcid,$sortid,%l,$fn,$fo,$fid);
(!$cid)&&(PError("Error. No group specified"));
($cid =~ /[^\d]/)&&(&PError("Error. Invalid group"));
my $j = $in{'j'};
my $content;

&GetConfig($cid);

my(@s) = localtime(time);
my $myday = sprintf("%.2d",$s[3]);
my $mymon = sprintf("%.2d",$s[4]+1);
my $myyear = sprintf("%.4d",$s[5]+1900);
my $myweekday = $s[6]+1;
my $myhour = sprintf("%.2d",$s[2]);

my $ts = time;
my $tdiff = ($ts - $in{'ts'})/86400;

if($in{'displaytype'} == 1){
srand(time);

if(($in{'rdays'} > 0)&&($tdiff >= $in{'rdays'})){
		$isset = '';
		}
	elsif(($in{'rdays'} > 0)&&($tdiff < $in{'rdays'})){
		$isset = $in{'did'};
		}
	else{
		$isset = '';
		}
}

if($in{'displaytype'} == 2){
if(($in{'sdays'} > 0)&&($tdiff >= $in{'sdays'})){
		$isset = '';
		}
	elsif(($in{'sdays'} > 0)&&($tdiff < $in{'sdays'})){
		$isset = $in{'did'};
		}
	else{
		$isset = '';
		}
}

open(DB,"<$datapath/$cid.cgi");
while(<DB>){
chomp;
($id,$rcid,$in{'title'},$in{'content'},$in{'ddate'},$in{'dmonth'},$in{'dweekday'},$in{'dday'},$ts,$in{'status'},$in{'dyear'},$in{'dhour'}) = split("\t",$_);

if($in{'displaytype'} == 1){
	$sortid = int(rand(1000))."-$id";
	}
if($in{'displaytype'} == 2){
	$sortid++;
	}
if($in{'displaytype'} == 3){
	$sortid = "$in{'dmonth'}-$id";
	if(($in{'dmonth'} <= $mymon)&&($in{'dmonth'} > $isset)){
		$isset = $in{'dmonth'};
		}
	}
if($in{'displaytype'} == 4){
	$sortid = "$in{'dweekday'}-$id";	
	if(($in{'dweekday'} <= $myweekday)&&($in{'dweekday'} > $isset)){
		$isset = $in{'dweekday'};
		}
	}
if($in{'displaytype'} == 5){
	$sortid = "$in{'dday'}-$id";
	if(($in{'dday'} <= $myday)&&($in{'dday'} > $isset)){
			$isset = $in{'dday'};
		}
	}
if($in{'displaytype'} == 6){
	$sortid = "$in{'dyear'}$in{'dmonth'}$in{'dday'}-$id";
	if(("$in{'dyear'}$in{'dmonth'}$in{'dday'}" <= "$myyear$mymon$myday")&&("$in{'dyear'}$in{'dmonth'}$in{'dday'}" > $isset)){
			$isset = "$in{'dyear'}$in{'dmonth'}$in{'dday'}";
		}
	}
if($in{'displaytype'} == 7){
	$sortid = "$in{'dmonth'}-$in{'dday'}-$id";
	if(("$in{'dmonth'}$in{'dday'}" <= "$mymon$myday")&&("$in{'dmonth'}$in{'dday'}" > $isset)){
		$isset = "$in{'dmonth'}$in{'dday'}";
		}
	}
	
if($in{'displaytype'} == 8){
	(!$in{'dweekday'})&&($in{'dweekday'} = $myweekday);
	$sortid = "$in{'dweekday'}$in{'dhour'}-$id";
	if(("$in{'dweekday'}$in{'dhour'}" <= "$myweekday$myhour")&&("$in{'dweekday'}$in{'dhour'}" > $isset)){
		$isset = "$in{'dweekday'}$in{'dhour'}";
		}
	}
	
$l{$sortid} = $_;
}

foreach my $i (sort keys %l){
($id,$rcid,$in{'title'},$in{'content'},$in{'ddate'},$in{'dmonth'},$in{'dweekday'},$in{'dday'},$ts,$in{'status'},$in{'dyear'},$in{'dhour'}) = split("\t",$l{$i});

if($in{'displaytype'}== 1){

		if(!$isset){
			$isset = $id;
			&SetNewId($isset);
			}
			
		if($id == $isset){
			$content .= $in{'content'};
			}
		}
	elsif($in{'displaytype'} == 2){
	
		if(!$isset){
			if($fn){
				$isset = $id;
				&SetNewId($isset);
				}
			if($id == $in{'did'}){
				$fn=1;
				}
			}
		
		if($id == $isset){
			$content .= $in{'content'};
			}
			
		#firstone
		(!$fid)&&($fid = $id)&&($fo = $in{'content'});
		
		}
	elsif($in{'displaytype'} == 3){
		if($isset == $in{'dmonth'}){
			$content .= $in{'content'};
			}
		}
	elsif($in{'displaytype'} == 4){
		if($isset == $in{'dweekday'}){
			$content .= $in{'content'};
			}
		}
	elsif($in{'displaytype'} == 5){
		if($isset == $in{'dday'}){
			$content .= $in{'content'};
			}
		}
	elsif($in{'displaytype'} == 7){
		if($isset eq "$in{'dmonth'}$in{'dday'}"){
			$content .= $in{'content'};
			}
		}
	elsif($in{'displaytype'} == 8){
		(!$in{'dweekday'})&&($in{'dweekday'} = $myweekday);
			if($isset eq "$in{'dweekday'}$in{'dhour'}"){
				$content .= $in{'content'};
				}
		#lastone
		($fid = $id)&&($fo = $in{'content'});
		}
	else{
		if($isset eq "$in{'dyear'}$in{'dmonth'}$in{'dday'}"){
			$content .= $in{'content'};
			}
		}
	
}

if($in{'displaytype'} == 2){
	if(!$content){
		$content = $fo;
		&SetNewId($fid);
		}
}

if($in{'displaytype'} == 8){
	if(!$content){
		$content = $fo;
		}
}

$content = &reverseHTML($content);
$content =~ s/\\t/\t/g;
$content =~ s/\\n/\n/g;
&ViewOut($content,$j);
exit;
}

sub SetNewId{
my($id) = @_;
(!$id)&&(PError("No record selected to edit"));
($id =~ /[^\d]/)&&(&PError("Error. Invalid id"));
my($cid) = $in{'cid'};
(!$cid)&&(PError("No group selected to edit"));
($cid =~ /[^\d]/)&&(&PError("Error. Invalid group id"));

my(@l,$line);
open(DB,"+<$datapath/dynamics.cgi");
($flock)&&(flock(DB,2));
while(<DB>){
chomp;
my(@f) = split("\t",$_);
if($f[0] == $cid){
	$f[6] = time;
	$f[7] = $id;
	$line = join("\t",@f);
	}
else{
	$line = $_;
	}
push(@l,$line);
}


if(@l){
	seek(DB,0,0);
	foreach my $i (@l){
		print DB "$i\n";
		}
	truncate(DB, tell(DB));
	}

($flock)&&(flock(DB,2));
close DB;
}

sub ViewOut{
my($content,$j) = @_;
if($content !~ /<.*>/){
	#assume text content
	$content =~ s/\n/<br>\n/g;
	}
if($j != 1){
	print $content;
	}
else{
	$content =~ s/\r//g;
	my(@lines) = split("\n",$content);
	foreach my $i (@lines){
		$i =~ s/\r//g;
		$i =~ s/\"/\\"/g;
		$i =~ s/\\n/\\\\n/g;
		$i =~ s/(scr)(ipt)/$1\"\+\"$2/gsi;
		print "document.write(\"$i\\n\");\n";
		}
}

exit;
}

sub SaveChangesC{
(!$in{'name'})&&(PError("Error. Please enter a name for this group."));
(length($in{'name'}) > 80)&&(PError("Error. Group Name too Long."));
($in{'name'} =~ /[^A-Za-z0-9_\-()\&\.\,\'\;\:\? ]/)&&(PError("Error. Invalid Character in group name."));
($in{'rdays'})&&($in{'rdays'} =~ /[^\d]/)&&(&PError("Error. Invalid random days"));
($in{'sdays'})&&($in{'sdays'} =~ /[^\d]/)&&(&PError("Error. Invalid sequential days"));
($in{'display'} =~ /[^\d]/)&&(&PError("Error. Invalid display type"));
my @l;

open(DB,"+<$datapath/dynamics.cgi");
($flock)&&(flock(DB,2));
while(<DB>){
	my(@f) = split("\t",$_);
	if($f[0] == $in{'id'}){
		(!$admin)&&($in{'Username'} ne $f[2])&&(&PError("Error. Permission denied"));
		$f[1] = $in{'name'};
		$f[3] = $in{'displaytype'};
		$f[4] = $in{'rdays'};
		$f[5] = $in{'sdays'};
		}
	my $newentry = join("\t",@f);
	push(@l,$newentry);
	}

if(@l){
seek(DB,0,0);
foreach my $i (@l){
	print DB $i;
	}
truncate(DB, tell(DB));
}
($flock)&&(flock(DB,8));
close DB;

&Redirect($in{'cgiurl'}."?command=manage&cid=$in{'id'}","Changes Saved");
exit;
}

sub SaveChanges(){
(!$in{'cid'})&&(&PError("Error. No group specified"));
($in{'cid'} =~ /[^\d]/)&&(&PError("Error. Invalid Group"));
(!$in{'id'})&&(&PError("Error. No content specified"));
($in{'id'} =~ /[^\d]/)&&(&PError("Error. Content ID"));

&GetConfig($in{'cid'});
(!$admin)&&($in{'uname'} ne $in{'UserName'})&&(&PError("Permission denied"));

(length($in{'title'}) > 80)&&(PError("Error. Title Name too Long."));
($in{'title'} =~ /[^A-Za-z0-9_\-() \&\.\,\'\;\:\?]/)&&(PError("Error. Invalid Character in title."));


	if($in{'displaytype'} == 3){
		($in{'dmonth'} =~ /[^\d]/)&&(&PError("Error. Invalid Month"));
		(!$in{'dmonth'})&&(&PError("Please select a month"));
		}
	if($in{'displaytype'} == 4){
		($in{'dweekday'} =~ /[^\d]/)&&(&PError("Error. Invalid Weekday"));
		(!$in{'dweekday'})&&(&PError("Please select a weekday"));
		}
	if($in{'displaytype'} == 5){
		($in{'dday'} =~ /[^\d]/)&&(&PError("Error. Invalid Day"));
		(!$in{'dday'})&&(&PError("Please select a day"));
		}
	if($in{'displaytype'} == 6){
		($in{'dmonth'} =~ /[^\d]/)&&(&PError("Error. Invalid Month"));
		($in{'dday'} =~ /[^\d]/)&&(&PError("Error. Invalid Day"));
		($in{'dyear'} =~ /[^\d]/)&&(&PError("Error. Invalid Year"));
		(!$in{'dmonth'})&&(&PError("Please select a month"));
		(!$in{'dday'})&&(&PError("Please select a day"));
		(!$in{'dyear'})&&(&PError("Please select a year"));
		}
	if($in{'displaytype'} == 7){
		($in{'dmonth'} =~ /[^\d]/)&&(&PError("Error. Invalid Month"));
		($in{'dday'} =~ /[^\d]/)&&(&PError("Error. Invalid Day"));
		(!$in{'dmonth'})&&(&PError("Please select a month"));
		(!$in{'dday'})&&(&PError("Please select a day"));
		}
		
	if($in{'displaytype'} == 8){
			($in{'dhour'} ne '0')&&(!$in{'dhour'})&&(&PError("Error. Please select an hour"));
			($in{'dhour'} =~ /[^\d]/)&&(&PError("Error. Invalid Hour"));
		}
$in{'content'} = &reverseHTML($in{'content'});
$in{'title'} = &reverseHTML($in{'title'});
$in{'content'} =~ s/\t/\\t/g;
$in{'content'} =~ s/\r*\n/\\n/g;



my @l;

#0        1             2             3                4             5              6             7         8          9              10    11        
#$id\t$in{'cid'}\t$in{'title'}\t$in{'content'}\t$in{'ddate'}\t$in{'dmonth'}\t$in{'dweekday'}\t$in{'dday'}\t$ts\t$in{'status'}\t$in{'dyear'}\t$in{'dhour'}

open(DB,"+<$datapath/$in{'cid'}.cgi");
($flock)&&(flock(DB,2));
while(<DB>){
	chomp;
	my(@f) = split("\t",$_);
	if($f[0] == $in{'id'}){
		(!$admin)&&($in{'Username'} ne $f[2])&&(&PError("Error. Permission denied"));
		$f[2] = $in{'title'};
		$f[3] = $in{'content'};
		$f[4] = $in{'ddate'};
		$f[5] = $in{'dmonth'};
		$f[6] = $in{'dweekday'};
		$f[7] = $in{'dday'};
		$f[10] = $in{'dyear'};
		$f[11] = $in{'dhour'};
		}
	my $newentry = join("\t",@f);
	push(@l,"$newentry\n");
	}

if(@l){
seek(DB,0,0);
foreach my $i (@l){
	print DB $i;
	}
truncate(DB, tell(DB));
}
($flock)&&(flock(DB,8));
close DB;

&Redirect($in{'cgiurl'}."?command=manage&cid=$in{'cid'}","Changes Saved");
exit;
}

sub Preview{
(!$in{'cid'})&&(&PError("Error. No group specified"));
($in{'cid'} =~ /[^\d]/)&&(&PError("Error. Invalid Group"));
(!$in{'id'})&&(&PError("Error. No content specified"));
($in{'id'} =~ /[^\d]/)&&(&PError("Error. Content ID"));
my @f;
my $found=0;

open(DB,"<$datapath/$in{'cid'}.cgi");
while(<DB>){
chomp;
@f = split("\t",$_);
if($f[0] == $in{'id'}){
	$found=1;
	last;
	}
}
close DB;
(!$found)&&(&PError("Error. No record found"));

my $content = $f[3];
$content = &reverseHTML($content);
$content =~ s/\\t/\t/g;
$content =~ s/\\n/\n/g;

if($content !~ /<.*>/){
	#assume text content
	$content =~ s/\n/<br>\n/g;
	}
	
print $content;
exit;
}

sub ShowEdit{
	$in{'command'} = 'savechanges';
	my($id) = $in{'id'};
	(!$id)&&(PError("No record selected to edit"));
	($id =~ /[^\d]/)&&(&PError("Error. Invalid id"));
	my($cid) = $in{'cid'};
	(!$cid)&&(PError("No group selected to edit"));
	($cid =~ /[^\d]/)&&(&PError("Error. Invalid group id"));
	my $found = 0;
	
	&GetConfig($cid);
	(!$admin)&&($in{'uname'} ne $in{'UserName'})&&(&PError("Permission denied"));
	
	open(DB,"$datapath/$cid.cgi");
	while(<DB>){
	my($rid,$rcid,$ts);
	($rid,$rcid,$in{'title'},$in{'content'},$in{'ddate'},$in{'dmonth'},$in{'dweekday'},$in{'dday'},$ts,$in{'status'},$in{'dyear'},$in{'dhour'}) = split("\t",$_);
	if($rid == $id){
		$found=1;
		last;
		}
	}
	close DB;
	
	(!$found)&&(&PError("Error. No record found"));
	
	if($in{'displaytype'} == 1){
			$in{'cfields'} = '';
			}
	elsif($in{'displaytype'} == 2){
			$in{'cfields'} = '';
			}
	elsif($in{'displaytype'} == 3){
		my $dmonth = $in{'dmonth'};
		my $tmonth = $Month[$dmonth];
				$in{'cfields'} = "
				<tr>
				                  <td align=right nowrap>
				                  <font size=\"2\">Month to Display<br>This Content:</font></td>
				                  <td nowrap>
				                    <table border=0 cellpadding=3 cellspacing=0>
				                      <tr>
				                          <td><font face=\"Tahoma\">
				                          <select size=\"1\" name=\"dmonth\" style=\"font-size: 8pt\">
				                          <option value=$dmonth selected>$tmonth</option>
				                          <option value=1>$Month[1]</option>
			                          <option value=2>$Month[2]</option>
			                          <option value=3>$Month[3]</option>
			                          <option value=4>$Month[4]</option>
			                          <option value=5>$Month[5]</option>
			                          <option value=6>$Month[6]</option>
			                          <option value=7>$Month[7]</option>
			                          <option value=8>$Month[8]</option>
			                          <option value=9>$Month[9]</option>
			                          <option value=10>$Month[10]</option>
			                          <option value=11>$Month[11]</option>
			                          <option value=12>$Month[12]</option>
			                          </select></font></td>
				                      </tr>
				                    </table>
				                  </td>
	                	</tr>
				";
				}
	elsif($in{'displaytype'} == 7){
		my $dmonth = $in{'dmonth'};
		my $tmonth = $Month[$dmonth];
		my $dday = $in{'dday'};
						$in{'cfields'} = "
						<tr>
						                  <td align=right nowrap>
						                  <font size=2>Date To Display<br>This Content:<br>(Month-Day)</font></td>
						                  <td nowrap>
						                  <div align=left>
						                    <table border=0 cellpadding=3 cellspacing=0>
						                      <tr>
						                          <td><font face=\"Tahoma\">
						                          <select size=\"1\" name=\"dmonth\" style=\"font-size: 8pt\">
						                        <option value=$dmonth selected>$tmonth</option>
						                        <option value=1>$Month[1]</option>
						                        <option value=2>$Month[2]</option>
						                        <option value=3>$Month[3]</option>
						                        <option value=4>$Month[4]</option>
						                        <option value=5>$Month[5]</option>
						                        <option value=6>$Month[6]</option>
						                        <option value=7>$Month[7]</option>
						                        <option value=8>$Month[8]</option>
						                        <option value=9>$Month[9]</option>
						                        <option value=10>$Month[10]</option>
						                        <option value=11>$Month[11]</option>
						                        <option value=12>$Month[12]</option>
						                        </select></font></td>
						                          <td><font face=\"Tahoma\">
						                          <select size=\"1\" name=\"dday\" style=\"font-size: 8pt\">
						                          
						                          <option>$dday</option>
					                        	  <option>01</option>
						                          <option>02</option>
						                          <option>03</option>
						                          <option>04</option>
						                          <option>05</option>
						                          <option>06</option>
						                          <option>07</option>
						                          <option>08</option>
						                          <option>09</option>
						                          <option>10</option>
						                          <option>11</option>
						                          <option>12</option>
						                          <option>13</option>
						                          <option>14</option>
						                          <option>15</option>
						                          <option>16</option>
						                          <option>17</option>
						                          <option>18</option>
						                          <option>19</option>
						                          <option>20</option>
						                          <option>21</option>
						                          <option>22</option>
						                          <option>23</option>
						                          <option>24</option>
						                          <option>25</option>
						                          <option>26</option>
						                          <option>27</option>
						                          <option>28</option>
						                          <option>29</option>
						                          <option>30</option>
						                          <option>31</option>
						                          </select></font></td>
						                         
						                      </tr>
						                    </table>
						                  </div>
						                  </td>
			
						</tr>
						";
		}
			elsif($in{'displaytype'} == 4){
				my $dweekday = $in{'dweekday'};
				my $tweekday = $WDay[$dweekday];
				
				$in{'cfields'} = "
				<tr>
				                  <td align=right nowrap>
				                  <font size=2>Weekday to Display<br>This Content:</font></td>
				                  <td nowrap>
				                  <div align=left>
				                    <table border=0 cellpadding=3 cellspacing=0>
				                      <tr>
				                          <td><font face=\"Tahoma\">
				                          <select size=\"1\" name=\"dweekday\" style=\"font-size: 8pt\">
				                          <option value=$dweekday selected>$tweekday</option>
				                          <option value=1>Sunday</option>
				                          <option value=2>Monday</option>
				                          <option value=3>Tuesday</option>
				                          <option value=4>Wednesday</option>
				                          <option value=5>Thursday</option>
				                          <option value=6>Friday</option>
				                          <option value=7>Saturday</option>
				                          </select></font></td>
				                      </tr>
				                    </table>
				                  </div>
				                  </td>
				                </tr>
	
				";
				}
			elsif($in{'displaytype'} == 5){
			my $dday = $in{'dday'};
				$in{'cfields'} = "
				<tr>
				                  <td  align=right nowrap>
				                  <font size=2>Day of the Month<br>to Display<br>This Content:</font></td>
				                  <td nowrap>
				                  <div align=left>
				                    <table border=0 cellpadding=3 cellspacing=0>
				                      <tr>
				                          <td><font face=\"Tahoma\">
				                          <select size=\"1\" name=\"dday\" style=\"font-size: 8pt\">
				                          <option value=\"$dday\">$dday</option>
				                          <option>01</option>
				                          <option>02</option>
				                          <option>03</option>
				                          <option>04</option>
				                          <option>05</option>
				                          <option>06</option>
				                          <option>07</option>
				                          <option>08</option>
				                          <option>09</option>
				                          <option>10</option>
				                          <option>11</option>
				                          <option>12</option>
				                          <option>13</option>
				                          <option>14</option>
				                          <option>15</option>
				                          <option>16</option>
				                          <option>17</option>
				                          <option>18</option>
				                          <option>19</option>
				                          <option>20</option>
				                          <option>21</option>
				                          <option>22</option>
				                          <option>23</option>
				                          <option>24</option>
				                          <option>25</option>
				                          <option>26</option>
				                          <option>27</option>
				                          <option>28</option>
				                          <option>29</option>
				                          <option>30</option>
				                          <option>31</option>
				                          </select></font></td>
				                      </tr>
				                    </table>
				                  </div>
				                  </td>
	                	</tr>
				";
				}
			elsif($in{'displaytype'} == 8){
			my($dhour) = $in{'dhour'};
			my $dweekday = $in{'dweekday'};
			
			my $tweekday = $WDay[$dweekday];
			
			
			my $dhourt;
			if($dhour >= 12){
				$dhourt = $dhour-12;
				if($dhourt == 0){
					$dhourt = "12 PM";
					}
				else{
					$dhourt = ($dhourt+0) . " PM";
					}
				}
			else{
				if($dhour == 0){
					$dhourt = "12 AM";
				}
				else{
					$dhourt = ($dhour+0) . " AM";
					}
				}
			$dhour = sprintf("%.2d",$dhour);
			$in{'cfields'} = "
							<tr>
							                  <td  align=right nowrap>
							                  <font size=2>Hour of the Day<br>to Display<br>This Content:</font></td>
							                  <td nowrap>
							                  <div align=left>
							                    <table border=0 cellpadding=3 cellspacing=0>
							                      <tr>
							                          <td><font face=\"Tahoma\">
							                          <select size=\"1\" name=\"dhour\" style=\"font-size: 8pt\">
							                          <option value=\"$dhour\">$dhourt</option>
							                          <option value='09'>9 AM</option>
							                          <option value='10'>10 AM</option>
							                          <option value='11'>11 AM</option>
							                          <option value='12'>12 PM</option>
							                          <option value='13'>1 PM</option>
							                          <option value='14'>2 PM</option>
							                          <option value='15'>3 PM</option>
							                          <option value='16'>4 PM</option>
							                          <option value='17'>5 PM</option>
							                          <option value='18'>6 PM</option>
							                          <option value='19'>7 PM</option>
							                          <option value='20'>8 PM</option>
							                          <option value='21'>9 PM</option>
							                          <option value='22'>10 PM</option>
							                          <option value='23'>11 PM</option>
							                          <option value='00'>12 AM</option>
							                          <option value='01'>1 AM</option>
							                          <option value='02'>2 AM</option>
							                          <option value='03'>3 AM</option>
							                          <option value='04'>4 AM</option>
							                          <option value='05'>5 AM</option>
							                          <option value='06'>6 AM</option>
							                          <option value='07'>7 AM</option>
							                          <option value='08'>8 AM</option>
							                          </select></font></td>
							                          <td nowrap>
										  <div align=left>
										  <table border=0 cellpadding=3 cellspacing=0>
										  <tr>
										  <td><font face=\"Tahoma\">
										 <select size=\"1\" name=\"dweekday\" style=\"font-size: 8pt\">
										  <option value=$dweekday selected>$tweekday</option>
										  <option value=0>Every Day</option>
										  <option value=1>Sunday</option>
										 <option value=2>Monday</option>
										 <option value=3>Tuesday</option>
										  <option value=4>Wednesday</option>
										 <option value=5>Thursday</option>
										 <option value=6>Friday</option>
										 <option value=7>Saturday</option>
										 </select></font></td>
										 </tr>
										 </table>
										 </div>
				                  				</td>
				                  
							                      </tr>
							                    </table>
							                  </div>
							                  </td>
				                	</tr>
				";
			}
			else{
				my $dday = $in{'dday'};
				my $dmonth = $in{'dmonth'};
				my $dyear = $in{'dyear'};
				
				$in{'cfields'} = "
				<tr>
				                  <td align=right nowrap>
				                  <font size=2>Date to Display<br>This Content:</font></td>
				                  <td nowrap>
				                  <div align=left>
				                    <table border=0 cellpadding=3 cellspacing=0>
				                      <tr>
				                          <td><font face=\"Tahoma\">
				                          <select size=\"1\" name=\"dmonth\" style=\"font-size: 8pt\">
				                          <option value=$dmonth>$dmonth</option>
				                          <option>01</option>
				                          <option>02</option>
				                          <option>03</option>
				                          <option>04</option>
				                          <option>05</option>
				                          <option>06</option>
				                          <option>07</option>
				                          <option>08</option>
				                          <option>09</option>
				                          <option>10</option>
				                          <option>11</option>
				                          <option>12</option>
				                          </select></font></td>
				                          <td><font face=\"Tahoma\">
				                          <select size=\"1\" name=\"dday\" style=\"font-size: 8pt\">
				                          <option value=$dday>$dday</option>
				                          <option>01</option>
				                          <option>02</option>
				                          <option>03</option>
				                          <option>04</option>
				                          <option>05</option>
				                          <option>06</option>
				                          <option>07</option>
				                          <option>08</option>
				                          <option>09</option>
				                          <option>10</option>
				                          <option>11</option>
				                          <option>12</option>
				                          <option>13</option>
				                          <option>14</option>
				                          <option>15</option>
				                          <option>16</option>
				                          <option>17</option>
				                          <option>18</option>
				                          <option>19</option>
				                          <option>20</option>
				                          <option>21</option>
				                          <option>22</option>
				                          <option>23</option>
				                          <option>24</option>
				                          <option>25</option>
				                          <option>26</option>
				                          <option>27</option>
				                          <option>28</option>
				                          <option>29</option>
				                          <option>30</option>
				                          <option>31</option>
				                          </select></font></td>
				                          <td><font face=\"Tahoma\">
				                          <select size=\"1\" name=\"dyear\" style=\"font-size: 8pt\">
				                          <option value=$dyear>$dyear</option>
				                          <option>2003</option>
				                          <option>2004</option>
				                          <option>2005</option>
				                          <option>2006</option>
				                          <option>2007</option>
				                          <option>2008</option>
				                          <option>2009</option>
				                          <option>2010</option>
				                          <option>2011</option>
				                          <option>2012</option>
				                          <option>2013</option>
				                          <option>2014</option>
				                          <option>2015</option>
				                          <option>2016</option>
				                          <option>2017</option>
				                          <option>2018</option>
				                          <option>2019</option>
				                          <option>2020</option>
				                          </select></font></td>
				                      </tr>
				                    </table>
				                  </div>
				                  </td>
	
				</tr>
				";
		}
	$in{'content'} =~ s/\\t/\t/g;
	$in{'content'} =~ s/\\n/\n/g;
	$in{'title'}= &htmlspecialchars($in{'title'});
	$in{'content'}= &htmlspecialchars($in{'content'});
	&PageOut("$cgipath/t_add.htm");
	exit;
}

sub GetConfig{
my($gid) = @_;
my $found=0;
open(DB,"<$datapath/dynamics.cgi");
while(<DB>){
chomp;
($in{'cid'},$in{'name'},$in{'UserName'},$in{'displaytype'},$in{'rdays'},$in{'sdays'},$in{'ts'},$in{'did'}) = split("\t",$_); 
if($gid == $in{'cid'}){
	$found=1;
	last;
	}
}
close DB;
($gid)&&(!$found)&&(&PError("Error. No group found"));
}

sub ShowEditC{
	$in{'command'} = 'savechangesc';
	my $id = $in{'id'};
	&GetConfig($in{'id'});	
	$in{'name'} = &htmlspecialchars($in{'name'});
	$in{'rdays'} = &htmlspecialchars($in{'rdays'});
	$in{'sdays'} = &htmlspecialchars($in{'sdays'});
	($in{'displaytype'}==1)&&($in{'display1'}='checked');
	($in{'displaytype'}==2)&&($in{'display2'}='checked');
	($in{'displaytype'}==3)&&($in{'display3'}='checked');
	($in{'displaytype'}==4)&&($in{'display4'}='checked');
	($in{'displaytype'}==5)&&($in{'display5'}='checked');
	($in{'displaytype'}==6)&&($in{'display6'}='checked');
	($in{'displaytype'}==7)&&($in{'display7'}='checked');
	($in{'displaytype'}==8)&&($in{'display8'}='checked');
	&PageOut("$basepath/t_configure.htm");
	exit;
}


sub Delete{

my($id) = $in{'id'};
(!$id)&&(PError("No record selected to edit"));
($id =~ /[^\d]/)&&(&PError("Error. Invalid id"));
my($cid) = $in{'cid'};

(!$cid)&&(PError("No group selected to edit"));
($cid =~ /[^\d]/)&&(&PError("Error. Invalid group id"));
my(@l);

&GetConfig($cid);
(!$admin)&&($in{'uname'} ne $in{'UserName'})&&(&PError("Permission denied"));

open(DB,"+<$datapath/$cid.cgi");
($flock)&&(flock(DB,2));
while(<DB>){
	my(@f) = split("\t",$_);
	if($f[0] != $id){
		push(@l,$_);
		}
	}


seek(DB,0,0);
foreach my $i (@l){
	print DB $i;
	}
truncate(DB, tell(DB));

($flock)&&(flock(DB,8));
close DB;

&Redirect($in{'cgiurl'}."?command=manage&cid=$cid","Content Deleted");
exit;
}

sub DeleteC(){
my($cid) = $in{'cid'};
my(@l);
(!$cid)&&(PError("No group selected to edit"));
($cid =~ /[^\d]/)&&(&PError("Error. Invalid group id"));
open(DB,"+<$datapath/dynamics.cgi");
($flock)&&(flock(DB,2));
while(<DB>){
	my(@f) = split("\t",$_);
	if($f[0] == $cid){
		(!$admin)&&($in{'Username'} ne $f[2])&&(&PError("Error. Permission denied"));
		}
	else{
		push(@l,$_);
		}
	}


seek(DB,0,0);
foreach my $i (@l){
	print DB $i;
	}
truncate(DB, tell(DB));

($flock)&&(flock(DB,8));
close DB;

undef @l;

open(DB,"+<$datapath/$cid.cgi");
($flock)&&(flock(DB,2));
while(<DB>){
	my(@f) = split("\t",$_);
	if($f[1] != $cid){
		push(@l,$_);
		}
	}


seek(DB,0,0);
foreach my $i (@l){
	print DB $i;
	}
truncate(DB, tell(DB));

($flock)&&(flock(DB,8));
close DB;


&Redirect($in{'cgiurl'}."?command=manage","Group Deleted");
exit;
}


sub Add{
(!$in{'cid'})&&(&PError("Error. No group specified"));
($in{'cid'} =~ /[^\d]/)&&(&PError("Error. Invalid Group"));
&GetConfig($in{'cid'});
(!$admin)&&($in{'uname'} ne $in{'UserName'})&&(&PError("Permission denied"));

(length($in{'title'}) > 80)&&(PError("Error. Title Name too Long."));
($in{'title'} =~ /[^A-Za-z0-9_\-() \&\.\,\'\;\:\?]/)&&(PError("Error. Invalid Character in title."));


	if($in{'displaytype'} == 3){
		($in{'dmonth'} =~ /[^\d]/)&&(&PError("Error. Invalid Month"));
		(!$in{'dmonth'})&&(&PError("Please select a month"));
		}
	if($in{'displaytype'} == 4){
		($in{'dweekday'} =~ /[^\d]/)&&(&PError("Error. Invalid Weekday"));
		(!$in{'dweekday'})&&(&PError("Please select a weekday"));
		}
	if($in{'displaytype'} == 5){
		($in{'dday'} =~ /[^\d]/)&&(&PError("Error. Invalid Day"));
		(!$in{'dday'})&&(&PError("Please select a day"));
		}
	if($in{'displaytype'} == 6){
		($in{'dmonth'} =~ /[^\d]/)&&(&PError("Error. Invalid Month"));
		($in{'dday'} =~ /[^\d]/)&&(&PError("Error. Invalid Day"));
		($in{'dyear'} =~ /[^\d]/)&&(&PError("Error. Invalid Year"));
		(!$in{'dmonth'})&&(&PError("Please select a month"));
		(!$in{'dday'})&&(&PError("Please select a day"));
		(!$in{'dyear'})&&(&PError("Please select a year"));
		}
	if($in{'displaytype'} == 7){
		($in{'dmonth'} =~ /[^\d]/)&&(&PError("Error. Invalid Month"));
		($in{'dday'} =~ /[^\d]/)&&(&PError("Error. Invalid Day"));
		(!$in{'dmonth'})&&(&PError("Please select a month"));
		(!$in{'dday'})&&(&PError("Please select a day"));
		}
		
	if($in{'displaytype'} == 8){
		($in{'dhour'} ne '0')&&(!$in{'dhour'})&&(&PError("Error. Please select an hour"));
		($in{'dhour'} =~ /[^\d]/)&&(&PError("Error. Invalid Hour"));
		}

$in{'content'} =~ s/\t/\\t/g;
$in{'content'} =~ s/\r*\n/\\n/g;

my $ts = time;
$in{'status'} = 1;
my $id = &GetID;

my($newrec) = "$id\t$in{'cid'}\t$in{'title'}\t$in{'content'}\t$in{'ddate'}\t$in{'dmonth'}\t$in{'dweekday'}\t$in{'dday'}\t$ts\t$in{'status'}\t$in{'dyear'}\t$in{'dhour'}";

open(DB,">>$datapath/$in{'cid'}.cgi");
($flock)&&(flock(DB,2));
print DB "$newrec\n";
($flock)&&(flock(DB,8));
close DB;

&Redirect($in{'cgiurl'}."?command=manage&cid=$in{'cid'}","Content Added");
exit;
}

sub AddC{

(!$in{'name'})&&(PError("Error. Please enter a name for this group."));
(length($in{'name'}) > 80)&&(PError("Error. Group Name too Long."));
($in{'name'} =~ /[^A-Za-z0-9_\-() \&\.\,\'\;\:\? ]/)&&(PError("Error. Invalid Character in group name."));
($in{'rdays'})&&($in{'rdays'} =~ /[^\d]/)&&(&PError("Error. Invalid random days"));
($in{'sdays'})&&($in{'sdays'} =~ /[^\d]/)&&(&PError("Error. Invalid sequential days"));
($in{'displaytype'} =~ /[^\d]/)&&(&PError("Error. Invalid display type"));

my $ts = time;
my $id = &GetID;
my $did='';
my $newentry  = "$id\t$in{'name'}\t$in{'UserName'}\t$in{'displaytype'}\t$in{'rdays'}\t$in{'sdays'}\t$ts\t$did"; 

open(DB,">>$datapath/dynamics.cgi");
($flock)&&(flock(DB,2));
print DB "$newentry\n";
($flock)&&(flock(DB,8));
close DB;

&Redirect($in{'cgiurl'}."?command=manage&cid=$id","Group Added");
exit;
}

sub ShowAdd{
	my $cid = $in{'cid'};
	(!$cid)&&(PError("Error. Please select a category"));
	if($in{'command'} eq 'spa'){
		$in{'command'} = 'padd';
		}
	else{
		$in{'command'} = 'add';
		}
	
	&GetConfig($cid);
	
	if($in{'displaytype'} == 1){
			$in{'cfields'} = '';
			}
		elsif($in{'displaytype'} == 2){
			$in{'cfields'} = '';
			}
		elsif($in{'displaytype'} == 3){
			$in{'cfields'} = "
			<tr>
			                  <td align=right nowrap>
			                  <font size=\"2\">Month To Display<br>This Content:</font></td>
			                  <td nowrap>
			                    <table border=0 cellpadding=3 cellspacing=0>
			                      <tr>
			                          <td><font face=\"Tahoma\">
			                          <select size=\"1\" name=\"dmonth\" style=\"font-size: 8pt\">
			                          <option value=0> -=Select=-</option>
			                          <option value=1>$Month[1]</option>
			                          <option value=2>$Month[2]</option>
			                          <option value=3>$Month[3]</option>
			                          <option value=4>$Month[4]</option>
			                          <option value=5>$Month[5]</option>
			                          <option value=6>$Month[6]</option>
			                          <option value=7>$Month[7]</option>
			                          <option value=8>$Month[8]</option>
			                          <option value=9>$Month[9]</option>
			                          <option value=10>$Month[10]</option>
			                          <option value=11>$Month[11]</option>
			                          <option value=12>$Month[12]</option>
			                          </select></font></td>
			                      </tr>
			                    </table>
			                  </td>
                	</tr>
			";
			}
		elsif($in{'displaytype'} == 4){
			$in{'cfields'} = "
			<tr>
			                  <td align=right nowrap>
			                  <font size=2>Weekday To Display<br>This Content:</font></td>
			                  <td nowrap>
			                  <div align=left>
			                    <table border=0 cellpadding=3 cellspacing=0>
			                      <tr>
			                          <td><font face=\"Tahoma\">
			                          <select size=\"1\" name=\"dweekday\" style=\"font-size: 8pt\">
			                          <option value=1>Sunday</option>
			                          <option value=2>Monday</option>
			                          <option value=3>Tuesday</option>
			                          <option value=4>Wednesday</option>
			                          <option value=5>Thursday</option>
			                          <option value=6>Friday</option>
			                          <option value=7>Saturday</option>
			                          </select></font></td>
			                      </tr>
			                    </table>
			                  </div>
			                  </td>
			                </tr>

			";
			}
		elsif($in{'displaytype'} == 5){
			$in{'cfields'} = "
			<tr>
			                  <td  align=right nowrap>
			                  <font size=2>Day of the Month<br>To Display<br>This Content:</font></td>
			                  <td nowrap>
			                  <div align=left>
			                    <table border=0 cellpadding=3 cellspacing=0>
			                      <tr>
			                          <td><font face=\"Tahoma\">
			                          <select size=\"1\" name=\"dday\" style=\"font-size: 8pt\">
			                          <option value=\"0\">-=Select=-</option>
			                          <option>01</option>
			                          <option>02</option>
			                          <option>03</option>
			                          <option>04</option>
			                          <option>05</option>
			                          <option>06</option>
			                          <option>07</option>
			                          <option>08</option>
			                          <option>09</option>
			                          <option>10</option>
			                          <option>11</option>
			                          <option>12</option>
			                          <option>13</option>
			                          <option>14</option>
			                          <option>15</option>
			                          <option>16</option>
			                          <option>17</option>
			                          <option>18</option>
			                          <option>19</option>
			                          <option>20</option>
			                          <option>21</option>
			                          <option>22</option>
			                          <option>23</option>
			                          <option>24</option>
			                          <option>25</option>
			                          <option>26</option>
			                          <option>27</option>
			                          <option>28</option>
			                          <option>29</option>
			                          <option>30</option>
			                          <option>31</option>
			                          </select></font></td>
			                      </tr>
			                    </table>
			                  </div>
			                  </td>
                	</tr>
			";
			}
		elsif($in{'displaytype'} == 7){
					$in{'cfields'} = "
					<tr>
					                  <td align=right nowrap>
					                  <font size=2>Date To Display<br>This Content:<br>(Month-Day)</font></td>
					                  <td nowrap>
					                  <div align=left>
					                    <table border=0 cellpadding=3 cellspacing=0>
					                      <tr>
					                          <td><font face=\"Tahoma\">
					                          <select size=\"1\" name=\"dmonth\" style=\"font-size: 8pt\">
			                          <option value=0> -=Select=-</option>
			                          <option value=1>$Month[1]</option>
			                          <option value=2>$Month[2]</option>
			                          <option value=3>$Month[3]</option>
			                          <option value=4>$Month[4]</option>
			                          <option value=5>$Month[5]</option>
			                          <option value=6>$Month[6]</option>
			                          <option value=7>$Month[7]</option>
			                          <option value=8>$Month[8]</option>
			                          <option value=9>$Month[9]</option>
			                          <option value=10>$Month[10]</option>
			                          <option value=11>$Month[11]</option>
			                          <option value=12>$Month[12]</option>
			                          </select></font></td>
					                          <td><font face=\"Tahoma\">
					                          <select size=\"1\" name=\"dday\" style=\"font-size: 8pt\">
					                          <option value=0>-=Select=-</option>
					                          <option>01</option>
					                          <option>02</option>
					                          <option>03</option>
					                          <option>04</option>
					                          <option>05</option>
					                          <option>06</option>
					                          <option>07</option>
					                          <option>08</option>
					                          <option>09</option>
					                          <option>10</option>
					                          <option>11</option>
					                          <option>12</option>
					                          <option>13</option>
					                          <option>14</option>
					                          <option>15</option>
					                          <option>16</option>
					                          <option>17</option>
					                          <option>18</option>
					                          <option>19</option>
					                          <option>20</option>
					                          <option>21</option>
					                          <option>22</option>
					                          <option>23</option>
					                          <option>24</option>
					                          <option>25</option>
					                          <option>26</option>
					                          <option>27</option>
					                          <option>28</option>
					                          <option>29</option>
					                          <option>30</option>
					                          <option>31</option>
					                          </select></font></td>
					                         
					                      </tr>
					                    </table>
					                  </div>
					                  </td>
		
					</tr>
					";
		}
		elsif($in{'displaytype'} == 8){
		$in{'cfields'} = "
									<tr>
									                  <td  align=right nowrap>
									                  <font size=2>Hour of the Day<br>to Display<br>This Content:</font></td>
									                  <td nowrap>
									                  <div align=left>
									                    <table border=0 cellpadding=3 cellspacing=0>
									                      <tr>
									                          <td><font face=\"Tahoma\">
									                          <select size=\"1\" name=\"dhour\" style=\"font-size: 8pt\">
									                          <option value='09'>9 AM</option>
									                          <option value='10'>10 AM</option>
									                          <option value='11'>11 AM</option>
									                          <option value='12'>12 PM</option>
									                          <option value='13'>1 PM</option>
									                          <option value='14'>2 PM</option>
									                          <option value='15'>3 PM</option>
									                          <option value='16'>4 PM</option>
									                          <option value='17'>5 PM</option>
									                          <option value='18'>6 PM</option>
									                          <option value='19'>7 PM</option>
									                          <option value='20'>8 PM</option>
									                          <option value='21'>9 PM</option>
									                          <option value='22'>10 PM</option>
									                          <option value='23'>11 PM</option>
									                          <option value='00'>12 AM</option>
									                          <option value='01'>1 AM</option>
									                          <option value='02'>2 AM</option>
									                          <option value='03'>3 AM</option>
									                          <option value='04'>4 AM</option>
									                          <option value='05'>5 AM</option>
									                          <option value='06'>6 AM</option>
									                          <option value='07'>7 AM</option>
									                          <option value='08'>8 AM</option>
									                          </select></font></td>
									                           <td nowrap>
												  <div align=left>
												  <table border=0 cellpadding=3 cellspacing=0>
												  <tr>
												  <td><font face=\"Tahoma\">
												  <select size=\"1\" name=\"dweekday\" style=\"font-size: 8pt\">
												  <option value=0>Every Day</option>
												  <option value=1>Sunday</option>
												  <option value=2>Monday</option>
												  <option value=3>Tuesday</option>
												 <option value=4>Wednesday</option>
												 <option value=5>Thursday</option>
												 <option value=6>Friday</option>
												  <option value=7>Saturday</option>
												  </select></font></td>
												 </tr>
												</table>
												</div>
												</td>
				                  
									                      </tr>
									                    </table>
									                  </div>
									                  </td>
						                	</tr>
				";
		}
		else{
			$in{'cfields'} = "
			<tr>
			                  <td align=right nowrap>
			                  <font size=2>Date To Display<br>This Content:</font></td>
			                  <td nowrap>
			                  <div align=left>
			                    <table border=0 cellpadding=3 cellspacing=0>
			                      <tr>
			                          <td><font face=\"Tahoma\">
			                          <select size=\"1\" name=\"dmonth\" style=\"font-size: 8pt\">
			                          <option value=0>-=Select=-</option>
			                          <option>01</option>
			                          <option>02</option>
			                          <option>03</option>
			                          <option>04</option>
			                          <option>05</option>
			                          <option>06</option>
			                          <option>07</option>
			                          <option>08</option>
			                          <option>09</option>
			                          <option>10</option>
			                          <option>11</option>
			                          <option>12</option>
			                          </select></font></td>
			                          <td><font face=\"Tahoma\">
			                          <select size=\"1\" name=\"dday\" style=\"font-size: 8pt\">
			                          <option value=0>-=Select=-</option>
			                          <option>01</option>
			                          <option>02</option>
			                          <option>03</option>
			                          <option>04</option>
			                          <option>05</option>
			                          <option>06</option>
			                          <option>07</option>
			                          <option>08</option>
			                          <option>09</option>
			                          <option>10</option>
			                          <option>11</option>
			                          <option>12</option>
			                          <option>13</option>
			                          <option>14</option>
			                          <option>15</option>
			                          <option>16</option>
			                          <option>17</option>
			                          <option>18</option>
			                          <option>19</option>
			                          <option>20</option>
			                          <option>21</option>
			                          <option>22</option>
			                          <option>23</option>
			                          <option>24</option>
			                          <option>25</option>
			                          <option>26</option>
			                          <option>27</option>
			                          <option>28</option>
			                          <option>29</option>
			                          <option>30</option>
			                          <option>31</option>
			                          </select></font></td>
			                          <td><font face=\"Tahoma\">
			                          <select size=\"1\" name=\"dyear\" style=\"font-size: 8pt\">
			                          <option value=0>-=Select=-</option>
			                          <option>2003</option>
			                          <option>2004</option>
			                          <option>2005</option>
			                          <option>2006</option>
			                          <option>2007</option>
			                          <option>2008</option>
			                          <option>2009</option>
			                          <option>2010</option>
			                          <option>2011</option>
			                          <option>2012</option>
			                          <option>2013</option>
			                          <option>2014</option>
			                          <option>2015</option>
			                          <option>2016</option>
			                          <option>2017</option>
			                          <option>2018</option>
			                          <option>2019</option>
			                          <option>2020</option>
			                          </select></font></td>
			                      </tr>
			                    </table>
			                  </div>
			                  </td>

			</tr>
			";
		}
		
	&PageOut("$basepath/t_add.htm");
	exit;
	}
	
sub ShowAddC{
	$in{'command'} = 'addc';
	$in{'id'}='';
	$in{'name'}='';
	$in{'rdays'}='';
	$in{'sdays'}='';
	$in{'display1'}='checked';
	$in{'display2'}='';
	$in{'display3'}='';
	$in{'display4'}='';
	$in{'display5'}='';
	$in{'display6'}='';
	$in{'display7'}='';
	&PageOut("$basepath/t_configure.htm");
	exit;
	}

sub Manage{

my $cid = $in{'cid'};
($cid)&&($cid =~ /[^\d]/)&&(&PError("Error invalid group"));	
my ($selected,$hcolor,$hid,$when,$mcode,$id,$title,$rcid,$ts,%l,$sortid,$isset,$isset2);
my $where='';

	open(DB,"<$datapath/dynamics.cgi");
	while(<DB>){
		chomp;
		my($id,$catname,$uid,@rest) = split("\t",$_);
		($module)&&(!$admin)&&($uid ne $in{'UserName'})&&(next);
		(!$cid)&&($cid = $id)&&($in{'cid'} = $id);
			if ($id == $cid) {
				$selected = 'selected';
				$in{"CatName"} = $catname; 
				}
			else{
				$selected = '';
				}
		$in{'copt'} .= qq|<option value=\"$id\" $selected>$catname</option>\n|;
		}
	close DB;
	
(!$in{'copt'})&&($in{'copt'} = "<option value=\"\">No groups configured</option>\n");
$in{'CatName'}  = &htmlspecialchars($in{'CatName'});
&GetConfig($cid);

my(@s) = localtime(time);
my $myday = sprintf("%.2d",$s[3]);
my $mymon = sprintf("%.2d",$s[4]+1);
my $myyear = sprintf("%.4d",$s[5]+1900);
my $myweekday = $s[6]+1;
my $myhour = sprintf("%.2d",$s[2]);
open(DB,"<$datapath/$in{'cid'}.cgi");
while(<DB>){
chomp;
($id,$rcid,$in{'title'},$in{'content'},$in{'ddate'},$in{'dmonth'},$in{'dweekday'},$in{'dday'},$ts,$in{'status'},$in{'dyear'},$in{'dhour'}) = split("\t",$_);

if($in{'displaytype'} == 1){
	$sortid++;
	$sortid = sprintf("%.6d",$sortid);
	}
if($in{'displaytype'} == 2){
	$sortid++;

	if((!$isset)&&(!$in{'did'})){
		$isset = $id;
		}
	if((!$isset)&&($in{'did'})){
		$isset = $in{'did'};
		}
	$sortid = sprintf("%.6d",$sortid);
	}
if($in{'displaytype'} == 3){
	$sortid = "$in{'dmonth'}-$id";
	if(($in{'dmonth'} <= $mymon)&&($in{'dmonth'} > $isset)){
		$isset = $in{'dmonth'};
		}
	}
if($in{'displaytype'} == 4){
	$sortid = "$in{'dweekday'}-$id";	
	if(($in{'dweekday'} <= $myweekday)&&($in{'dweekday'} > $isset)){
		$isset = $in{'dweekday'};
		}
	}
if($in{'displaytype'} == 5){
	$sortid = "$in{'dday'}-$id";
	if(($in{'dday'} <= $myday)&&($in{'dday'} > $isset)){
			$isset = $in{'dday'};
		}
	}
if($in{'displaytype'} == 6){
	$sortid = "$in{'dyear'}$in{'dmonth'}$in{'dday'}-$id";
	if(("$in{'dyear'}$in{'dmonth'}$in{'dday'}" <= "$myyear$mymon$myday")&&("$in{'dyear'}$in{'dmonth'}$in{'dday'}" > $isset)){
			$isset = "$in{'dyear'}$in{'dmonth'}$in{'dday'}";
		}
	}
if($in{'displaytype'} == 7){
	$sortid = "$in{'dmonth'}-$in{'dday'}-$id";
	if(("$in{'dmonth'}$in{'dday'}" <= "$mymon$myday")&&("$in{'dmonth'}$in{'dday'}" > $isset)){
		$isset = "$in{'dmonth'}$in{'dday'}";
		}
	}
	
if($in{'displaytype'} == 8){
	(!$in{'dweekday'})&&($in{'dweekday'} = $myweekday);
	$sortid = "$in{'dweekday'}$in{'dhour'}-$id";
	
	if(("$in{'dweekday'}$in{'dhour'}" <= "$myweekday$myhour")&&("$in{'dweekday'}$in{'dhour'}" > $isset)){
		$isset = "$in{'dweekday'}$in{'dhour'}";
		($in{'dweekday'})&&($isset2=1);
		}
	}

$l{$sortid} = $_;
}

foreach my $i (sort keys %l){
($id,$rcid,$in{'title'},$in{'content'},$in{'ddate'},$in{'dmonth'},$in{'dweekday'},$in{'dday'},$ts,$in{'status'},$in{'dyear'},$in{'dhour'}) = split("\t",$l{$i});

if($in{'displaytype'}== 1){
		$when = 'Random';
		}
	elsif($in{'displaytype'} == 2){
		if($id == $isset){
			$hid = $id;
			}
		$when = "Sequential [ <a href=\"javascript:Reset('$id');\"> Set Current </a>]";
		$mcode = " | <a href=\"javascript:MoveUp('$id');\">Move Up</a> | <a href=\"javascript:MoveDown('$id');\">Move Down</a>";
		}
	elsif($in{'displaytype'} == 3){
		$when = $Month[$in{'dmonth'}];
		if($isset == $in{'dmonth'}){
			$hid = $id;
			}
		}
	elsif($in{'displaytype'} == 4){
		$when = $WDay[$in{'dweekday'}];
		if($isset == $in{'dweekday'}){
			 $hid = $id;
			}
		}
	elsif($in{'displaytype'} == 5){
		$when = "Day ".$in{'dday'}." of the Month";
		if($isset == $in{'dday'}){
				$hid = $id;
			}
		}
	elsif($in{'displaytype'} == 7){
		$when = $Month[$in{'dmonth'}]."-".$in{'dday'};
		if($isset eq "$in{'dmonth'}$in{'dday'}"){
			$hid = $id;
			}
		}
	elsif($in{'displaytype'} == 8){
			my $dweekday = $in{'dweekday'};
			my $tweekday;
			if($dweekday){
				$tweekday = $WDay[$dweekday];
				}
			else{
			if($isset2){
				$in{'dweekday'} = $myweekday;
				}
			else{
				$in{'dweekday'} = '0';
				}
			$tweekday = 'Every Day';
			}
			
			if($isset == "$in{'dweekday'}$in{'dhour'}"){
					$hid = $id;
				}
			if($in{'dhour'} >= 12){
				$in{'dhour'} = $in{'dhour'} - 12;
				if($in{'dhour'} == 0){
					$when = '12 PM '.$tweekday;
					}
				else{
					$when = $in{'dhour'} . ' PM '.$tweekday;
					}
				}
			else{
				$in{'dhour'} = $in{'dhour'}+0;
				if($in{'dhour'} == 0){
					$in{'dhour'} = '12';
					}
				$when = $in{'dhour'} . ' AM '.$tweekday;
				}
			
			
		}
	else{
		if($isset eq "$in{'dyear'}$in{'dmonth'}$in{'dday'}"){
			$hid = $id;
			}
		$when = "$in{'dmonth'}-$in{'dday'}-$in{'dyear'}";
		}
	
	if($hid == $id){
		$hcolor="bgcolor='#00FF00'";
		}
	else{
		$hcolor="";
		}
		
	$in{'line'} .= "<tr><td valign=top align=center><a href=\"javascript:Edit('$id');\">Edit</a> | <a href=\"javascript:Delete('$id');\">Delete</a> | <a href=\"javascript:View('$id');\">View</a>$mcode</td><td valign=top align=left>$in{'title'}</td><td valign=top align=center $hcolor><b>$when</b></td></tr>";
}


(!$in{'CatName'})&&($in{'CatName'} = 'none');
(!$in{'line'})&&($in{'line'} = '<tr><td colspan=3>No content defined</td></tr>');


if($admin){
	$in{'cp'} = "| [ <a href=\"javascript: ShowCP();\">Change Password</a> ]";
	}

&PageOut("$cgipath/t_manage.htm");
exit;
}

sub GetLogin{
    &GetCookies;
    $in{'UserName'} = $cookie{'UserName'};
    $in{'PassWord'} = $cookie{'PassWord'};
    
    # if no password, then output the login screen    
    if(!$in{'UserName'}){
        &PageOut("$cgipath/t_login.htm");
        exit;
    }
    
    if(($^O =~ /win/i)||($password !~ /^CS/)){
        	if(($in{'UserName'} eq $username)&&($in{'PassWord'} eq $password)){
        			#good to go.
        			$admin = 1;
        			return 1;
        	}
        }
        else{
        	if(($in{'UserName'} eq $username)&&(crypt($in{'PassWord'},'CS') eq $password)){
    			#good to go.
    			$admin=1;
    			return 1;
        	}               
    }
    # password failed
    &PError("Error. Invalid username or password");
}

sub DoLogin{
	&PageOut("$cgipath/t_login.htm");
	exit;
	}

sub PageOut{
my ($file) = @_;
if($in{'js'} == 1){
	&PageOutJS($file);
	}
open(OUT,"$file")||print "$!: $file<br>";
while(<OUT>){
$_ =~ s/in\((\w+)\)/$in{$1}/g;
print;
}
close OUT;
}



sub reverseHTML{
	my($text) = @_;
	$text =~ s/\&gt;/>/g;
	$text =~ s/\&lt;/</g;
	$text =~ s/\&quot;/\"/g;
	$text =~ s/\&amp;/\&/g;
	return $text;
	}
	
sub htmlspecialchars{
	my($buffer) =@_;
	$buffer =~ s/\&/\&amp;/g;
	$buffer =~ s/\</\&lt;/g;
	$buffer =~ s/\>/\&gt;/g;
	$buffer =~ s/\"/\&quot;/g;	
	return $buffer;
	}

sub GetID{
	my($id);
	open(DB,"<$datapath/_gcount.cgi");
	($flock)&&(flock(DB,2));
	$id = <DB>;
	($flock)&&(flock(DB,8));
	close DB;
	$id++;
	open(DB,">$datapath/_gcount.cgi");
	($flock)&&(flock(DB,2));
	print DB $id;
	($flock)&&(flock(DB,8));
	close DB;
	return $id;
	}
	
sub DoSetup{
	(-f "$basepath/setup.cgi")&&(&PError("Error. Access Denied"));
	(-f "../setup.cgi")&&(&PError("Error. Access Denied"));
	use Cwd;
	$in{'mcgipath'} = Cwd::cwd();
	$in{'mcgiurl'} = "$ENV{'HTTP_HOST'}/$ENV{'SCRIPT_NAME'}";
	$in{'mcgiurl'} =~ s/\/\//\//g;
	$in{'mcgiurl'} = "http://".$in{'mcgiurl'};
	$in{'mcgiurl'} =~ s/\/$in{'scriptname'}//i;
	$in{'mimageurl'} = $in{'mcgiurl'}.'/images';
	$in{'mdatapath'} = $in{'mcgipath'} .'/data';
	&PageOut("$basepath/t_setup.htm");
	exit;
	}

sub SaveSetup{
	(-f "$basepath/setup.cgi")&&(&PError("Error. Access Denied"));
	(-f "../setup.cgi")&&(&PError("Error. Access Denied"));
	$in{'setup'} =~ s/\r*\n/\n/g;
	my $sfile = "$basepath/setup.cgi";
	
	if($^O !~ /win/i){
	    	$in{'mpassword'} = crypt($in{'mpassword'},'CS');
	    	}
	$in{'mcgiurl'} =~ s/[\'\%\$]//g;
	$in{'mcgipath'} =~ s/[\'\%\$]//g;
	$in{'mimageurl'} =~ s/[\'\%\$]//g;
	$in{'mdatapath'} =~ s/[\'\%\$]//g;
	$in{'musername'} =~ s/[\'\%\$]//g;
	$in{'mpassword'} =~ s/[\'\%\$]//g;
	
	open(SETUP,">$basepath/setup.cgi");
	print SETUP "\$cgiurl='$in{'mcgiurl'}';\n";
	print SETUP "\$cgipath='$in{'mcgipath'}';\n";
	print SETUP "\$imageurl='$in{'mimageurl'}';\n";
	print SETUP "\$datapath='$in{'mdatapath'}';\n";
	print SETUP "\$username='$in{'musername'}';\n";
	print SETUP "\$password='$in{'mpassword'}';\n";
	print SETUP "1;\n";
	&Redirect("$ENV{'SCRIPT_NAME'}?command=login","Setup.cgi reconfigured");
	exit;
}


sub ChangePass{
my($buff,$encpass);
(!$in{'musername'})&&(&PError("Error. Please enter a username"));
(!$in{'mpassword'})&&(&PError("Error. Please enter a password"));
($in{'mpassword'} ne $in{'mpassword2'})&&(&PError("Error. Please retype passwords"));
($in{'musername'} =~ /[\%\s\|\$\*\.\']/)&&(&PError("Error. Invalid character in username"));
($in{'mpassword'} =~ /[\%\s\|\$\*\.\']/)&&(&PError("Error. Invalid character in password"));
open(DB,"<$basepath/setup.cgi");
while(<DB>){
	$buff .= $_;
	}
close DB;

if($^O !~ /win/i){
    	$encpass = crypt($in{'mpassword'},'CS');
    	}
else{
	$encpass = $in{'mpassword'};
	}
    	
$buff =~ s/\$username='.*'/\$username='$in{'musername'}'/;
$buff =~ s/\$password='.*'/\$password='$encpass'/;

open(DB,">$basepath/setup.cgi");
print DB $buff;
close DB;

print <<"EOF";
<script language=javascript>
var rndURL = (1000*Math.random());
document.cookie="UserName=$in{'musername'}";
document.cookie="PassWord=$in{'mpassword'}";
alert("Username/Password changed");
window.location="$in{'cgiurl'}?command=manage&rnd="+rndURL;
</script>
EOF
exit;
}

sub ShowCP{
if(!$admin){
	&PError("Permission denied");
	}
$in{'musername'} = $in{'username'};
&PageOut("$cgipath/t_cp.htm");
exit;
}


