#!/usr/bin/perl


use CGI::Carp(fatalsToBrowser);
use DBD::mysql;
use CGI;
use Image::Size;
require GT::Template; 

use Date::Calc qw(Time_to_Date Today Date_to_Days Add_Delta_Days check_date Days_in_Month);

require 'lib/sub.pl';
require 'lib/meteo.pl';
require '/home/mapsochi/www/editor/config.Global.pl';
require '/home/mapsochi/www/editor/config.Custom.pl';

#===================================
# Работа с командной строкой
#===================================

if ($ENV{'REQUEST_METHOD'} eq "GET") {
$qstring= $ENV{'QUERY_STRING'};
} 
elsif ($ENV{'REQUEST_METHOD'} eq "POST") 
{
read(STDIN, $qstring, $ENV{'CONTENT_LENGTH'});
}
@split = split (/&/, $qstring);
foreach $split (@split) {
    ($query_key, $query_value) = split (/=/, $split);
    $query_value =~ tr/+/ /;
    $query_value =~ s/%([\dA-Fa-f][\dA-Fa-f])/ pack ("C", hex ($1))/eg;
    $query_value =~ s/'/"/g;
    $in{$query_key} = $query_value;
}

if (!$ENV{'QUERY_STRING_UNESCAPED'}){
$ENV{'QUERY_STRING_UNESCAPED'}=$ENV{'REDIRECT_QUERY_STRING_UNESCAPED'};
}

@split = split (/\\&/, $ENV{'QUERY_STRING_UNESCAPED'});
foreach $split (@split) {
    ($query_key, $query_value) = split (/=/, $split);
    $query_value =~ tr/+/ /;
    $query_value =~ s/%([\dA-Fa-f][\dA-Fa-f])/ pack ("C", hex ($1))/eg;
    $query_value =~ s/'/"/g;
    $in{$query_key} = $query_value;
}


$dbh=DBI->connect("DBI:mysql:$config{database}:$config{host}", $config{user}, $config{password})
 or die "connecting error: $DBI::errstr"; 


$temp_worker->{root}='templ/';
$temp_worker->{compress}='0';
$temp_worker->{strict}='0';
$temp_worker->{begin}='[$';
$temp_worker->{end}=']';
$template = new GT::Template ($temp_worker);



  

temp_worker->{root}='templ/';
$temp_worker->{compress}='0';
$temp_worker->{strict}='0';
$temp_worker->{begin}='[$';
$temp_worker->{end}=']';
$template = new GT::Template ($temp_worker);


	$card_name=win2utf("Mapsochi\.su\: Реальная погода в Сочи");


	$contents=meteo();

	$ip=qq[$ENV{REMOTE_ADDR}];
	$agent=qq[$ENV{HTTP_USER_AGENT}];
	$from=qq[$ENV{HTTP_REFERER}];


	$sql = $dbh->prepare("insert INTO map_stat SET ip='$ip', date=CURDATE(), f_url='$from', agent='$agent', time=CURTIME()")or die "Inserting error: ", $dbh->errstr;
	$sql->execute or die "Inserting error: ", $dbh->errstr;



my $replace = {

	contents=>$contents,
	card_name=>$card_name,
};


# вывод содержимого
TextHeader();
print $template->parse('index.htm', $replace);