#!/usr/bin/perl
######################################################
# news.mp3s.ru - v.2.0 
# [news]
#
# Copyright (c) 2000-2002 WebCreator
# Author: Vokhmianin Vladimir
# 	nightelf@zr.ru
# 	vladimir@webcreator.ru
######################################################

	use lib '/srv/www/vhosts/mp3s.ru/lib';
  use wclib qw($dbh);
	use mp3s;
  use strict;

# ------------------------
# Send them a page 
  wclib::send_header;

########################################################################################################################

# --------------------
# Assign the variables
	my $useragent="$ENV{'HTTP_USER_AGENT'}";
	my $via="$ENV{'HTTP_VIA'}";
	my $httpx="$ENV{'HTTP_X_FORWARDED_FOR'}";
	my $ip="$ENV{'REMOTE_ADDR'}";
	my $ref="$ENV{'HTTP_REFERER'}";

	## ищем символьное соответсвие по ip
	my $dns = $ENV{'REMOTE_HOST'};
	if (!exists $ENV{'REMOTE_HOST'}) {
		my @numbers = split(/\./, $ip);
		my $ip_number = pack("C4", @numbers);
		$dns = (gethostbyaddr($ip_number, 2))[0];
		if (!$dns) { $dns=""; }
	}

	my ($sec,$min,$hour,$day,$mon,$year,$wday,$yday,$isdst) = localtime(time);
	$mon++;
	$year += 1900;
	$mon = ($mon < 10) ? "0".$mon : $mon;
	$day = ($day < 10) ? "0".$day : $day;
	$hour = ($hour < 10) ? "0".$hour : $hour;
	$min = ($min < 10) ? "0".$min : $min;
	$sec = ($sec < 10) ? "0".$sec : $sec;
	my $date="$year-$mon-$day $hour:$min:$sec";

# --------------------
# Assign the variables

	open(LOG,">>export_news.log");
	print LOG "$date\t$ip\t$dns\t$ref\t$useragent\t$via\t$httpx\n";
	close(LOG);

# $type - Тип: 
#		search=$words - вхождение слова в заголовок новости
#		cat=russia - по россии
#		cat=zabugor	- забугорье
#		cat=hitech	- hi-tech
# 	onday	- этот день в истории (???)
#	$vision - варианы вывода
# $count - количество от 1 до 20
# $style_date - стиль для даты
# $style_title - стиль для заголовка новости
# $style_anons - стиль для анонса
# $style_more - стиль для "Подробнее..."
# $style_caption - стиль для "заголовка" (заглавия)
# $target - тиргет
# $bgcolor - бэкграунд таблицы
# $table_width - ширина таблицы
# $java - выводить как javascript'om

# export_news.pl?search=тату;10;;;;;;;ffffff;100;
# http://news.mp3s.ru/cgi-bin/export_news.pl?search=тату;3;10;;;;;;;ffffff;100;
#	http://news.mp3s.ru/cgi-bin/export_news.pl?all          ;10;;;;;;;666699;90

my ($temp,$vision,$count,$style_date,$style_title,$style_anons,$style_more,$style_caption,$target,$bgcolor,$table_width,$java)=split(/;/,$ENV{'QUERY_STRING'});
my ($type,$words)=split(/=/,$temp);

wclib::db_connect();
&news($type,$words,$vision,$count,$style_date,$style_title,$style_anons,$style_more,$style_caption,$target,$bgcolor,$table_width,$java);
wclib::db_disconnect;

###########################################################################
# news - Последние новости
###########################################################################
sub news {
	my ($type,$words,$vision,$count,$style_date,$style_title,$style_anons,$style_more,$style_caption,$target,$bgcolor,$table_width,$java)=@_;

	my $q="";
	my $blank="";
	if ($type eq "search") {
		$q=qq~WHERE title like '%$words%'~;
		my $words_de=wclib::url_encode($words);
	} elsif ($type eq "cat" && $words=~/^[1-3]$/) {
		$q=qq~WHERE CAT_ID=$words~;
	}

	$count=5 if ($count>20 || $count<1);
	$target="_blank" if ($target eq "");
	$table_width=($table_width>1000 || $table_width<99 || ($table_width!=/^[0-9]{2,3}\%$/ && $table_width!=/\%$/))?"100%":$table_width;

	if ($java) { print qq~document.writeln ('~; }

#	print qq~<style>.md	{ font-family: arial; font-size: 12px; color: #ffffff; }.mt	{ font-family: arial; font-size: 15px; color: #000000; font-weight: bold; text-decoration : none; }.ma	{ font-family: arial; font-size: 14px; color: #333333; }.mm	{ font-family: arial; font-size: 12px; color: #291BA8; font-weight: bold; text-decoration : none; }.mc	{ font-family: arial; font-size: 17px; color: #291BA8; font-weight: bold; text-decoration : none; }</style>~;
	print qq~<style>.md	{ font-family: arial; font-size: 9pt; color: #000000; }.mt	{ font-family: arial; font-size: 9pt; color: #000f5b; font-weight: bold; text-decoration : none; }.ma	{ font-family: arial; font-size: 9pt; color: #000000; }.mm	{ font-family: arial; font-size: 9pt; color: #cc0000; font-weight: bold; text-decoration : none; }.mc	{ font-family: arial; font-size: 12pt; color: #291BA8; font-weight: bold; text-decoration : none; }</style>~;

	$style_date=($style_date eq "")?"md":$style_date;
	$style_title=($style_title eq "")?"mt":$style_title;
	$style_anons=($style_anons eq "")?"ma":$style_anons;
	$style_more=($style_more eq "")?"mm":$style_more;
	$style_caption=($style_caption eq "")?"mc":$style_caption;

	if ($bgcolor ne "") { 
		print qq~<table border=0 width=$table_width cellpadding=0 cellspacing=0 bgcolor="\#$bgcolor">~ 
	} else {
		print qq~<table border=0 width=$table_width cellpadding=0 cellspacing=0>~;
	}
	if ($style_caption ne "-") {
		print qq~<tr><td align=center><a href=http://news.mp3s.ru/$blank target=$target class=$style_caption>&nbsp;Новости от mp3s.ru&nbsp;</a></td></tr>~;
	}
	print qq~<tr><td><dl>~;

	my $query = qq!
		SELECT id, title, info, DATE_FORMAT(date_up,'%d-%m-%Y %H:%i')
		FROM News $q
		ORDER BY date_up DESC
		LIMIT $count
 	!;
 	my $sth = $dbh -> prepare ($query) || wclib::panic("Соединение с базой невозможно", $dbh->errstr.":".$query);
 	$sth -> execute() || wclib::panic("Соединение с базой невозможно", $dbh->errstr.":".$query);

	my $i=0;
	while (my ($id,$title,$anons,$date_up) = $sth -> fetchrow_array) {
		mp3s::news_review(\$anons);
		if ($java) {
			$title=~s/[\n\t\r]//ig;
			$anons=~s/[\n\t\r]//ig;
			$title=~s/\'/\\'/ig;
			$anons=~s/\'/\\'/ig;
		}
		my $link=$date_up;
		$link=~s/(\d\d)-(\d\d)-(\d\d\d\d) .*/$3\/$2\/$1/;
#		print qq~<tr><td valign=top nowrap><font size=1>$date_up</font></td><td valign=top><a href="/news.html/$link/$id/" class=tdlink><b>$title</b></a><br></td></tr>~;

		my $br="<br>";
		my $dd1="";
		my $dd2="<dd>";
		my $dd3="</dd>";
		if ($vision==1) {
			$br="<dd>";
			$dd1="";
			$dd2="<br>";
			$dd3="</dd>";
		} elsif ($vision==2) {
			$br="&nbsp;";
			$dd1="";
			$dd2="<br>";
			$dd3="";
		} elsif ($vision==3) {
			$br="&nbsp;";
			$dd1="";
			$dd2="<dd>";
			$dd3="</dd>";
		}
		
    print qq~<dt>~;
    if ($style_date ne "-") { 
			print qq~$dd1<font class=$style_date>$date_up</font>$br<a class=$style_title target=$target href=http://news.mp3s.ru/view/news/$link/$id.html>$title</a>~;
		}
		my $more="";
    if ($style_more ne "-") {
			$more=qq~<a class=$style_more target=$target href=http://news.mp3s.ru/view/news/$link/$id.html>Подробнее...</a>~;
		}
    if ($style_anons ne "-") {
			print qq~$dd2<font class=$style_anons>$anons$more<br>$dd3<br>~;
		}

	}
 	$sth -> finish;

	print qq~</td></tr></table>~;

	if ($java) { print qq~');~; }

}

#######################################################
# EOF Last Update: 31.03.02 3:30:54, Vokhmianin Vladimir

