#!/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 = "<script language='javascript'><!--\n";
    $jsend = "\n//--></script>";

    if( ($action eq "") or ($action eq "main") ) { show_main(); exit;}
    if( $action eq "update" ) { update_me(); exit;}
    if( $action eq "publish" ) { publish_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/tv_menu.html") || die "Cannot open template: $tplpath/tv_menu.html";
    $template=<T>; close(T);

    open(T,"$tplpath/navigation") || die "Cannot open template: $tplpath/navigation";
    $navigation=<T>; close(T);
    ($navigationselected) = $navigation=~/::selected(.+?)selected::/s; $navigation=~s/::tv::/$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.="<option value=$row->{date_id}$isselected>$row->{date_name}</option>\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/::scriptname::/$cgipath\/edit_tv.pl/g;
    $template=~s/::newsblock::/$newsblock/;
    $template=~s/::jsblock(.+?)jsblock::/$jsfiller/s;
    
    print $template;

    $dbh->disconnect;
}





sub update_me() {
    print $standardheader;

    $dbh = DBI->connect("DBI:mysql:$database:$hostname", $username, $password);
    if( !$dbh ) {
        print "Unable to open database\n";
        exit;
    }

    $sth = $dbh->prepare("update dates set date_text=? where date_id=?");
    $sth->execute($formdata{mytext}, $recordid);
    $sth->finish;
    
    $magicteleport =~s/::whereto::/edit_tv.pl?action=main&recordid=$recordid/;
    print $magicteleport;

    $dbh->disconnect;
}



sub publish_me() {
    print $standardheader;

    local $/=undef;
    open(T,"$tplpath/newshour_lenta.html") || die "Cannot 1open template: $tplpath/newshour_lenta.html";
    $template=<T>; close(T);

    $dbh = DBI->connect("DBI:mysql:$database:$hostname", $username, $password);
    if( !$dbh ) {
        print "Unable to open database\n";
        exit;
    }

    ($lentablock) = $template=~/::lentablock(.+?)lentablock::/s;
    $lentafiller="";

    $sth = $dbh->prepare("select * from newshour order by newshour_sorter desc");
    $sth->execute();
    
    for($i=0; $i < $sth->rows; $i++) {
    
	$row = $sth->fetchrow_hashref;
	
	$lentablockcopy = $lentablock;
	
	$row->{newshour_name}=~s/</&lt;/g;
	$row->{newshour_text}=~s/</&lt;/g;
	$row->{newshour_text}=~s/\015//g;
	$row->{newshour_text}=~s/\012/<br>/g;

	$lentablockcopy =~s/::lentatext::/$row->{newshour_text}/;
	$lentablockcopy =~s/::lentaheader::/$row->{newshour_name}/;    
	($myyear, $mymonth, $myday, $myhour, $myminute) = split/\D/, $row->{newshour_time};
	$lentablockcopy =~s/::lentatime::/$myhour:$myminute/;
	$lentablockcopy =~s/::lentasource::/$row->{newshour_source}/;

	if( $row->{newshour_picture} eq "" ) {
	    $lentablockcopy=~s/::lentapictureblock(.+?)lentapictureblock:://s;
	} else {
	    $lentablockcopy=~s/(::)*lentapictureblock(::)*//g;
	    $lentablockcopy=~s/::lentapicture::/$row->{newshour_picture}/;
	}
    	
	$lentafiller.=$lentablockcopy;
    }
    $sth->finish;
    
    $template=~s/::lentablock(.+?)lentablock::/$lentafiller/s;
    
    open(MYCLOWN, ">$htmldir/newshour_lenta.shtml");
    print MYCLOWN $template;
    close(MYCLOWN);
        
    chmod 0666, "$htmldir/newshour_lenta.shtml";


# Отжимаем новость часа

    local $/=undef;
    open(T,"$tplpath/newshour_current.html") || die "Cannot open template: $tplpath/newshour_current.html";
    $template=<T>; close(T);


    $sth = $dbh->prepare("select * from newshour where newshour_current=1");
    $sth->execute();
    
    $row = $sth->fetchrow_hashref;
    $row->{newshour_name}=~s/</&lt;/g;
    $row->{newshour_text}=~s/</&lt;/g;
    $row->{newshour_text}=~s/\015//g;
    $row->{newshour_text}=~s/\012/<br>/g;

    $template =~s/::lentatext::/$row->{newshour_text}/;
    $template =~s/::lentaheader::/$row->{newshour_name}/;    
    ($myyear, $mymonth, $myday, $myhour, $myminute) = split/\D/, $row->{newshour_time};
    $template =~s/::lentatime::/$myhour:$myminute/;
    $template =~s/::lentasource::/$row->{newshour_source}/;

    if( $row->{newshour_picture} eq "" ) {
        $template=~s/::lentapictureblock(.+?)lentapictureblock:://s;
    } else {
        $template=~s/(::)*lentapictureblock(::)*//g;
        $template=~s/::lentapicture::/$row->{newshour_picture}/;
    }
    $sth->finish;
    
    open(MYCLOWN, ">$htmldir/newshour_current.shtml");
    print MYCLOWN $template;
    close(MYCLOWN);
        
    chmod 0666, "$htmldir/newshour_current.shtml";


    $magicteleport =~s/::whereto::/edit_newshour.pl?action=main&recordid=$recordid/;
    print $magicteleport;

    $dbh->disconnect;
}


