#!/usr/local/bin/perl
#---------------------------------------------------------------------
# YMCA ECKE NEWS POSTING DATABASE INTERFACE
# July 11, 2000 BG Modify
#
# Events On Line
# http://www.eventsonline.com/ info@eventsonline.com
# Copyright (c) 1997,2000 Events On Line. All Rights Reserved.
#---------------------------------------------------------------------
$TDB::lock = 2;
$TDB::unlock = 8;
$TDB::delimiter = "\t";
$template = 'template.html';
$template_tag = '%DATA%';
$db_file = 'admin/classes.db';
$db_header = "TITLE\tCDATE\tCTIME\tBYLINE\tSP_EVENT\tCLASS_MO\tADDRESS\tBY_COMPANY\tCOMMENTS";
# change the header information inside the " " marks
#
$data .= "
What's New
";
MAIN: {
open (FILE, "< admin/date.txt");
$date = ;
close (FILE);
chop ($date);
&getvar;
if ($Z::id ne '') {
&class_detail;
} else {
&class_list
}
&PrintHTML;
}
sub PrintHTML {
print "Content-type: text/html\n\n";
open (HTML, "< $template") || die "Can't open $template!\n";
while ($oneline = ) {
$oneline =~ s/$template_tag/$data/gi;
print "$oneline";
}
close (HTML);
}
sub class_detail {
local (%value) = ();
%value = &TDB::find ($db_file, $db_header, $Z::id);
if (%value) {
$data .= "
| $value{TITLE}
$value{CDATE} - $value{CTIME}
$value{BYLINE}
$value{BY_COMPANY}
$value{ADDRESS}
|
$value{COMMENTS} |
Last Updated: $date
Return to the News Page
";
} else {
&class_list;
}
}
sub class_list {
local (@match) = &TDB::search ($db_file, $db_header);
local (@sorted) = &TDB::rdatesort (@match);
$data .= "
(Click on the News title for more details)
| Date |
News |
Summary |
";
if ($sorted[0] == 0) {
$data .= "| No Records Were Found. |
";
} else {
foreach (@sorted) {
($ID, $time, $title, $cdate, $ctime, $byline, $sp_event, $class_mo, $address, $by_company) = split (/$TDB::delimiter/);
$data .= "| $cdate | $title | $byline |
";
}
}
$data .= "
Last Updated: $date
";
}
sub getvar {
local ($buf, $end, @in, $boundary, $key, $val);
if ($ENV{'REQUEST_METHOD'} eq 'POST') {
read (STDIN, $buf, $ENV{'CONTENT_LENGTH'});
@in = split (/&/, $buf);
foreach $i (0 .. $#in) {
$in[$i] =~ s/\+/ /g;
$in[$i] =~ s/%(..)/pack("c",hex($1))/ge;
($key, $val) = split (/=/, $in[$i], 2);
${'Z::'.$key} = $val;
}
} else {
@in = split (/&/, $ENV{'QUERY_STRING'});
foreach $i (0 .. $#in) {
$in[$i] =~ s/\+/ /g;
$in[$i] =~ s/%(..)/pack("c",hex($1))/ge;
($key, $val) = split (/=/, $in[$i], 2);
${'Z::'.$key} = $val;
}
}
}
sub TDB::search {
local ($db_file, $db_header, %patterns) = @_;
if (!-e $db_file || !-r $db_file) {
return 0;
}
if ($db_header !~ /^ID\tTIMESTAMP\t/) {
$db_header = "ID\tTIMESTAMP\t" . $db_header;
}
local (@names) = split (/$TDB::delimiter/, $db_header);
local ($pattern) = '';
if ($patterns{'ID'} ne '') {
$pattern = $patterns{'ID'};
} else {
$pattern = "([^$TDB::delimiter]*)";
}
shift (@names);
foreach $name (@names) {
$pattern .= "$TDB::delimiter";
if ($patterns{$name} ne '') {
$pattern .= "([^$TDB::delimiter]*" .
$patterns{$name} .
"[^$TDB::delimiter]*)";
} else {
$pattern .= "([^$TDB::delimiter]*)";
}
}
local (@match) = ();
open (TDBFILE, "< $db_file");
while () {
chop;
local ($record) = &TDB::decode ($_);
if ($record =~ /^$pattern$/i) {
push (@match, $record);
}
}
close (TDBFILE);
if ($#match == -1) {
return 0;
} else {
return @match;
}
}
sub TDB::find {
local ($db_file, $db_header, $record_ID) = @_;
if (!-e $db_file || !-r $db_file) {
return 0;
}
if ($db_header !~ /^ID\tTIMESTAMP\t/) {
$db_header = "ID\tTIMESTAMP\t" . $db_header;
}
local (@names) = split (/$TDB::delimiter/, $db_header);
local (%data) = ();
local ($found_flag) = 0;
open (TDBFILE, "< $db_file");
while () {
chop;
if (/^$record_ID$TDB::delimiter/i) {
local (@values) = split (/$TDB::delimiter/);
foreach $i (0 .. $#names) {
$data{$names[$i]} = &TDB::decode ($values[$i]);
}
$found_flag = 1;
}
}
close (TDBFILE);
if ($found_flag) {
return %data;
} else {
return 0;
}
}
sub TDB::sort {
local ($index, $order, $alpha, $case, @to_sort) = @_;
return sort {
local (@data_a) = split (/$TDB::delimiter/, $a);
local (@data_b) = split (/$TDB::delimiter/, $b);
if ($order > 0) {
if ($alpha > 0) {
if ($case > 0) {
$data_a[$index] cmp $data_b[$index];
} else {
uc($data_a[$index]) cmp uc($data_b[$index]);
}
} else {
$data_a[$index] <=> $data_b[$index];
}
} else {
if ($alpha) {
if ($case > 0) {
$data_b[$index] cmp $data_a[$index];
} else {
uc($data_b[$index]) cmp uc($data_a[$index]);
}
} else {
$data_b[$index] <=> $data_a[$index];
}
}
} @to_sort;
}
sub TDB::decode {
local ($to_decode) = @_;
$to_decode =~ tr/+/ /;
$to_decode =~ s/%([0-9a-fA-F]{2})/pack("c",hex($1))/ge;
return $to_decode;
}
sub TDB::datesort {
local (@to_sort) = @_;
local (@names) = split (/$TDB::delimiter/, $db_header);
local ($index);
for ($i=0; $i<$#names; $i++) {
if ($names[$i] eq "CDATE") {
$index = $i + 2;
}
}
return sort {
local (@data_a) = split (/$TDB::delimiter/, $a);
local (@data_b) = split (/$TDB::delimiter/, $b);
&dateToNum($data_a[$index]) <=> &dateToNum($data_b[$index]);
} @to_sort;
}
sub TDB::rdatesort {
local (@to_sort) = @_;
local (@names) = split (/$TDB::delimiter/, $db_header);
local ($index);
for ($i=0; $i<$#names; $i++) {
if ($names[$i] eq "CDATE") {
$index = $i + 2;
}
}
return sort {
local (@data_a) = split (/$TDB::delimiter/, $a);
local (@data_b) = split (/$TDB::delimiter/, $b);
&dateToNum($data_b[$index]) <=> &dateToNum($data_a[$index]);
} @to_sort;
}
sub dateToNum {
local($datetext) = @_;
$datetext =~ /([a-zA-Z]{3,})\.? ([0-9]+), ([0-9]{2,4})/;
local ($month) = $1;
local ($day) = $2;
local ($year) = $3;
SWITCH: {
if ($month =~ /^Jan/i) { $month = 0; last SWITCH; }
if ($month =~ /^Feb/i) { $month = 1; last SWITCH; }
if ($month =~ /^Mar/i) { $month = 2; last SWITCH; }
if ($month =~ /^Apr/i) { $month = 3; last SWITCH; }
if ($month =~ /^May/i) { $month = 4; last SWITCH; }
if ($month =~ /^Jun/i) { $month = 5; last SWITCH; }
if ($month =~ /^Jul/i) { $month = 6; last SWITCH; }
if ($month =~ /^Aug/i) { $month = 7; last SWITCH; }
if ($month =~ /^Sep/i) { $month = 8; last SWITCH; }
if ($month =~ /^Oct/i) { $month = 9; last SWITCH; }
if ($month =~ /^Nov/i) { $month = 10; last SWITCH; }
if ($month =~ /^Dec/i) { $month = 11; last SWITCH; }
}
if ($year < 100) { # in year 2090 people will have
if ($year > 90) { # a potential "year 2000" problem...
$year += 1900; # (but most systems will break in 2038)
}
else {
$year += 2000;
}
}
return ($year*10000 + $month*100 + $day);
}