#!/usr/bin/perl
use DBI;
require "/usr/local/apache/cgi-bin/games2000/location.set";
###### getting form data
%formdata=();
%filenames=();
if( $ENV{'CONTENT_TYPE'}=~/multipart/ )
{
binmode STDIN;
read(STDIN, $query, $ENV{'CONTENT_LENGTH'});
$query=~/\A(.+?)(\015|\012)/;
$boundary = $1;
(@allkeys) = split/$boundary/,$query;
foreach $mykey (@allkeys) {
if($mykey=~/name="(.+?)"/) {
$keyname=$1;
$mykey=~s/\A(.+?)\015{0,1}\012\015{0,1}\012//s;
$keyinfo=$1;
if( $keyinfo=~/filename="(.+?)"/ ) {
$filename = $1;
} else { $filename=""; }
$mykey=~s/\015{0,1}\012\Z//;
$formdata{$keyname}=$mykey;
$filenames{$keyname}=$filename;
}
}
} else {
if( $ENV{'REQUEST_METHOD'} eq 'POST' )
{
read(STDIN, $query, $ENV{'CONTENT_LENGTH'});
@params=split(/&/, $query);
foreach $keyvalueparam (@params) {
($keyparam, $valueparam) = split(/=/, $keyvalueparam);
$valueparam=~tr/+/ /;
$valueparam=~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
$formdata{$keyparam}=$valueparam;
}
} else
{
$query = $ENV{'QUERY_STRING'};
@params=split(/&/, $query);
foreach $keyvalueparam (@params) {
($keyparam, $valueparam) = split(/=/, $keyvalueparam);
$valueparam=~tr/+/ /;
$valueparam=~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
$formdata{$keyparam}=$valueparam;
}
}
}
###### analyzing parameters
$action = $formdata{'action'};
$recordid = $formdata{selector};
if( $recordid eq "") { $recordid = $formdata{recordid}; }
$jsstart = "";
if( ($action eq "") or ($action eq "main") ) { show_main(); exit;}
if( $action eq "update" ) { update_me(); exit;}
if( $action eq "updatetoday" ) { updatetoday_me(); exit;}
if( $action eq "publish" ) { publish_me(); exit;}
if( $action eq "gettoday" ) { gettoday(); exit;}
if( $action eq "insert" ) { insert_me(); exit;}
if( $action eq "delete" ) { delete_me(); exit;}
print $standardheader;
print "Query error. Tell jager.";
exit;
sub parse_client_cookies
{
local (*COOKIE_DATA) = @_;
local (@key_value_pairs, $key_value, $key, $value);
@key_value_pairs = split (/;\s/, $ENV{'HTTP_COOKIE'});
foreach $key_value (@key_value_pairs) {
($key, $value) = split (/=/, $key_value);
$key =~ tr/+/ /;
$value =~ tr/+/ /;
$key =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
$value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
if (defined($FORM_DATA{$key})) {
$FORM_DATA{$key} = join ("\0", $FORM_DATA{$key}, $value);
} else {
$FORM_DATA{$key} = $value;
}
}
}
sub show_main() {
print $standardheader;
local $/=undef;
open(T,"$tplpath/schedule_menu.html") || die "Cannot open template: $tplpath/schedule_menu.html";
$template=; close(T);
open(T,"$tplpath/navigation") || die "Cannot open template: $tplpath/navigation";
$navigation=; close(T);
($navigationselected) = $navigation=~/::selected(.+?)selected::/s; $navigation=~s/::schedule::/$navigationselected/; $navigation=~s/::(.+?):://g;
$template=~s/::navigation::/$navigation/;
$dbh = DBI->connect("DBI:mysql:$database:$hostname", $username, $password);
if( !$dbh ) {
print "Unable to open database\n";
exit;
}
($jsblock) = $template=~/::jsblock(.+?)jsblock::/s;
$jsfiller="";
$sth = $dbh->prepare("select * from dates order by date_date");
$sth->execute;
if($recordid ne "") { $selected=$recordid; } else {$selected=-1;};
$newsblock="";
for( $i=0; $i<$sth->rows; $i++) {
$row = $sth->fetchrow_hashref;
if( ($selected==$row->{date_id}) or (($selected==-1) and ($i==0))) { $isselected=" selected "; } else { $isselected="" };
$newsblock.="\n";
$row->{date_text}=~s/\015//g;
$row->{date_text}=~s/\\/\\\\/g;
$row->{date_text}=~s/"/\\"/g;
$row->{date_text}=~s/\012/\\n/g;
$jsblockcopy=$jsblock;
$jsblockcopy=~s/::jstext::/$row->{date_text}/;
$jsblockcopy=~s/::jsid::/$row->{date_id}/;
$jsfiller.=$jsblockcopy;
}
$sth->finish;
$template=~s/::newsblock::/$newsblock/;
$sth = $dbh->prepare("select * from sports order by sport_name");
$sth->execute;
if($recordid ne "") { $selected=$recordid; } else {$selected=-1;};
$newsblock="";
for( $i=0; $i<$sth->rows; $i++) {
$row = $sth->fetchrow_hashref;
if( ($selected==$row->{sport_id}) or (($selected==-1) and ($i==0))) { $isselected=" selected "; } else { $isselected="" };
$newsblock.="\n";
}
$sth->finish;
$template=~s/::sportsblock::/$newsblock/;
$template=~s/::scriptname::/$cgipath\/edit_schedule.pl/g;
$template=~s/::jsblock(.+?)jsblock::/$jsfiller/s;
print $template;
$dbh->disconnect;
}
sub updatetoday_me() {
print $standardheader;
$dbh = DBI->connect("DBI:mysql:$database:$hostname", $username, $password);
if( !$dbh ) {
print "Unable to open database\n";
exit;
}
$recordid = $formdata{eventos};
$sth = $dbh->prepare("update sportday set sportday_name=?, sportday_gender=?, sportday_final=? where sportday_id=?");
$sth->execute($formdata{mysubject}, $formdata{mygender}, $formdata{myfinal}, $recordid);
$sth->finish;
$magicteleport =~s/::whereto::/edit_schedule.pl?action=gettoday&mysport=$formdata{mysport}&mydate=$formdata{mydate}&recordid=$recordid/;
print $magicteleport;
$dbh->disconnect;
}
sub publish_me() {
print $standardheader;
local $/=undef;
open(T,"$tplpath/games2000_sched_inner.shtml") || die "Cannot 1open template: $tplpath/games2000_sched_inner.shtml";
$template=; close(T);
$templatefiller="";
$dbh = DBI->connect("DBI:mysql:$database:$hostname", $username, $password);
if( !$dbh ) {
print "Unable to open database\n";
exit;
}
$sth = $dbh->prepare("select * from sports order by sport_name");
$sth2 = $dbh->prepare("select sportday_date from sportday where sportday_sport=?");
$sth->execute();
for($i=0; $i < $sth->rows; $i++) {
$row = $sth->fetchrow_hashref;
$templatecopy = $template;
$templatecopy=~s/::sportname::/$row->{sport_name}/;
$templatecopy=~s/::sporturl::/\/games2000\/schedule\/s$row->{sport_id}xx.shtml/;
$sth2->execute( $row->{sport_id} );
while( $row2 = $sth2->fetchrow_hashref ) {
$templatecopy=~s/::day$row2->{sportday_date}(\D)/$1/g;
$templatecopy=~s/day$row2->{sportday_date}:://g;
}
$templatecopy=~s/::day.+day\d+::/ /g;
$templatefiller.=$templatecopy;
}
$sth->finish;
open(MYCLOWN, ">$htmldir/includes/schedule.ssi");
print MYCLOWN $templatefiller;
close(MYCLOWN);
chmod 0666, "$htmldir/includes/schedule.ssi";
$sth2->finish;
$sth->finish;
### ------ fucking difficult part ergh.
open(T,"$tplpath/games2000_sched_sport_fn.shtml") || die "Cannot 1open template: $tplpath/games2000_sched_sport_fn.shtml";
$template=; close(T);
$templatefiller="";
($sportblock) = $template=~/::sportblock(.+?)sportblock::/s;
$sth = $dbh->prepare("select * from sports");
$sth->execute;
$sth2 = $dbh->prepare("
select * from sportday, dates where sportday_date=date_id and sportday_sport=? order by sportday_date, sportday_name");
while( $row = $sth->fetchrow_hashref ) {
$templatecopy = $template;
$sth2->execute( $row->{sport_id} );
$sportfiller="";
$lastdate="";
while( $row2 = $sth2->fetchrow_hashref ) {
$sportblockcopy = $sportblock;
if( $lastdate eq $row2->{date_name} ) {
$row2->{date_name}="";
} else {
$lastdate = $row2->{date_name};
}
$sportblockcopy=~s/::sportdate::/$row2->{date_name}/;
$sportblockcopy=~s/::sportthing::/$row2->{sportday_name}/;
$sportfiller.=$sportblockcopy;
}
$templatecopy=~s/::sportname::/$row->{sport_name}/;
$templatecopy=~s/::sporticon::/$row->{sport_icon}/;
$templatecopy=~s/::sportblock(.+?)sportblock::/$sportfiller/s;
open(MYCLOWN, ">$htmldir/schedule/s$row->{sport_id}xx.shtml");
print MYCLOWN $templatecopy;
close(MYCLOWN);
chmod 0666, "$htmldir/schedule/s$row->{sport_id}xx.shtml";
}
$sth2->finish;
$sth->finish;
$magicteleport =~s/::whereto::/edit_schedule.pl?action=main&recordid=$recordid/;
print $magicteleport;
$dbh->disconnect;
}
sub gettoday() {
print $standardheader;
local $/=undef;
open(T,"$tplpath/schedule_sports.html") || die "Cannot open template: $tplpath/schedule_sports.html";
$template=; close(T);
$dbh = DBI->connect("DBI:mysql:$database:$hostname", $username, $password);
if( !$dbh ) {
print "Unable to open database\n";
exit;
}
($jsblock) = $template=~/::jsblock(.+?)jsblock::/s;
$jsfiller="";
$sth = $dbh->prepare("select * from sportday where sportday_sport=? and sportday_date=? order by sportday_name");
$sth->execute($formdata{mysport}, $formdata{mydate});
if($recordid ne "") { $selected=$formdata{recordid}; } else {$selected=-1;};
$newsblock="";
for( $i=0; $i<$sth->rows; $i++) {
$row = $sth->fetchrow_hashref;
if( ($selected==$row->{sportday_id}) or (($selected==-1) and ($i==0))) { $isselected=" selected "; } else { $isselected="" };
$newsblock.="\n";
$row->{date_text}=~s/\015//g;
$row->{date_text}=~s/\\/\\\\/g;
$row->{date_text}=~s/"/\\"/g;
$row->{date_text}=~s/\012/\\n/g;
$row->{sportday_name}=~s/\015//g;
$row->{sportday_name}=~s/\\/\\\\/g;
$row->{sportday_name}=~s/"/\\"/g;
$row->{sportday_name}=~s/\012/\\n/g;
$jsblockcopy=$jsblock;
# $jsblockcopy=~s/::jstext::/$row->{}/;
$jsblockcopy=~s/::jssubject::/$row->{sportday_name}/;
$jsblockcopy=~s/::jsid::/$row->{sportday_id}/;
$jsblockcopy=~s/::jsgender::/$row->{sportday_gender}/;
$jsblockcopy=~s/::jsfinal::/$row->{sportday_final}/;
$jsfiller.=$jsblockcopy;
}
$sth->finish;
$template=~s/::newsblock::/$newsblock/;
$sth = $dbh->prepare("select * from sports order by sport_name");
$sth->execute;
if($recordid ne "") { $selected=$recordid; } else {$selected=-1;};
$newsblock="";
for( $i=0; $i<$sth->rows; $i++) {
$row = $sth->fetchrow_hashref;
if( ($selected==$row->{sport_id}) or (($selected==-1) and ($i==0))) { $isselected=" selected "; } else { $isselected="" };
$newsblock.="\n";
}
$sth->finish;
$template=~s/::sportsblock::/$newsblock/;
$template=~s/::mysport::/$formdata{mysport}/g;
$template=~s/::mydate::/$formdata{mydate}/g;
$template=~s/::scriptname::/$cgipath\/edit_schedule.pl/g;
$template=~s/::jsblock(.+?)jsblock::/$jsfiller/s;
print $template;
$dbh->disconnect;
}
sub insert_me() { ### inserts new record
print $standardheader;
$dbh = DBI->connect("DBI:mysql:$database:$hostname", $username, $password);
if( !$dbh ) {
print "Unable to open database\n";
exit;
}
($myinserthour, $myinsertminute) = ($formdata{myinsert}=~/ *(\d+):(\d+)\D/);
if( $myinserthour eq "" ) { $myinserthour="12" };
if( $myinsertminute eq "" ) { $myinsertminute="00"; }
$sth = $dbh->prepare("insert into sportday (sportday_name, sportday_time, sportday_sport, sportday_date) values (?,?,?,?)");
$sth->execute($formdata{myinsert}, $myinserthour.":".$myinsertminute.":00", $formdata{mysport}, $formdata{mydate} );
$sth->finish;
$sth = $dbh->prepare("select last_insert_id()");
$sth->execute;
$lastinsertid = $sth->fetchrow_arrayref->[0];
$sth->finish;
$magicteleport =~s/::whereto::/edit_schedule.pl?action=gettoday&mysport=$formdata{mysport}&mydate=$formdata{mydate}&recordid=$lastinsertid/;
print $magicteleport;
$dbh->disconnect;
}
sub delete_me() { ### inserts new record
print $standardheader;
$dbh = DBI->connect("DBI:mysql:$database:$hostname", $username, $password);
if( !$dbh ) {
print "Unable to open database\n";
exit;
}
$sth = $dbh->prepare("delete from sportday where sportday_id=?");
$sth->execute($formdata{selector} );
$sth->finish;
$magicteleport =~s/::whereto::/edit_schedule.pl?action=gettoday&mysport=$formdata{mysport}&mydate=$formdata{mydate}/;
print $magicteleport;
$dbh->disconnect;
}