#!/usr/bin/perl
##############
#Измените пароль и логин
$login="login";
$pass="pass";
##############

@months = ('01','02','03','04','05','06','07','08','09','10','11','12');
@days = ('Вс','Пн','Вт','Ср','Чт','Пт','Сб');
($sec,$min,$hour,$mday,$mon,$year,$wday) = localtime(time);
$year+=1900;
$data1="$mday/$months[$mon]/$year";
$file="news.blt";

#Get data from memory --------
if ($ENV{'REQUEST_METHOD'} eq "POST")
    {
      read(STDIN, $bufer, $ENV{'CONTENT_LENGTH'});
    }
else
    {
      $bufer=$ENV{'QUERY_STRING'};
    }    
#End -------------------------

# Decode data ----------------
@pairs = split(/&/, $bufer);
foreach $pair (@pairs)
      {
        ($name, $value) = split(/=/, $pair);
        $name =~ tr/+/ /;
        $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        $name =~ s/<!--(.|\n)*-->//g;
        $name =~ s/<([^>]|\n)*>//g;
        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        $value =~ s/<!--(.|\n)*-->//g;
        
        $FORM{$name} = $value;
      }      
# End ------------------------   
$step=$FORM{'step'};




print "Content-type: text/html\n\n";
print <<STYLE;
	<html><body><head>
	<style type="text/css">
	<!--
	.in, INPUT, TEXTAREA{border-style: solid; border-width:1 ; font-family: Tahoma; color:black; font-size=13px;}
	.bt{ border:0;  background=#FCCCAB; color:black; font-size=14px;}
	body {font-family:tahoma;background: white;font-size=14px; align=center;}
	table,{background=#C4B473;}
	a:link {text-decoration: none; color: "black"}
	a:visited {text-decoration: none;color: "black"}
	a:active {text-decoration: none; color: "black"}
	a:hover {text-decoration: underline; color: "black"}
	-->
	</style>
	</head><center><font face=tahoma>
STYLE

if ($step eq "1")
  {
	if($FORM{'login'} eq $login and $FORM{'pass'} eq $pass)
	  {
	  	&add;
	  	&showdel;
	  }
	else
	  {
	  	print "ПАРОЛЬ НЕВЕРЕН<br></center></body></html>";
	  	exit;
	  }		
  }
elsif($step eq "2")
  {
  	$data=$FORM{'data'};
  	$FORM{'news'} =~ s/\n/<br>/g;
	$news=$FORM{'news'};
  	open (ADD, ">>$file") || open (ADD, ">$file");
	print ADD "$data\:\:$news\n";
	close (ADD);
	&add;
	&showdel;
  }
elsif($step eq "3")
  {
  	$delet=$FORM{'del'};
  	&del;
  	&add;
  	&showdel;
  }	  	  
else
  {
  
  	print "<form action=addnews.pl method=post><table border=0 ><tr><td align=center><b>Введите login и пароль</b></td></tr>";
  	print "<tr><td>login: <input type=text size=15 name=login></td></tr><br>";
  	print "<tr><td>password: <input type=password size=15 name=pass><input type=hidden name=step value=1></td></tr><br>";
  	print "<tr><td align=center><input class=bt type=\"submit\" value=\"Отправить\"> <input class=bt type=\"reset\" value=\"Очистить\"><td></tr></table></form>";
  }	  
  
  
print "</font></center></body></html>";  


 
sub add
  {
  	print "<table border=0><tr><td align=center>Добавление новости</td></tr>";
  	print "<form action=addnews.pl method=post>";
  	print "<tr><td>Дата: <input type=text name=data value=$data1 size=10>";
  	print "<br>Новость:</td></tr><tr><td><textarea name=news cols=40 rows=2></textarea></td></tr><tr><td align=center><input class=bt type=submit value=Добавить></td></tr>";
  	print "<input type=hidden name=step value=2></form></table><br>";
  }	
  
sub showdel
  {
  	open (NEWS, "$file");
	@line=<NEWS>;
	close (NEWS);
	@line=reverse(@line);
  	print "<br><table border=0 width=400><tr><td align=center><small>Что-бы удалить новость, отметьте её и нажмите кнопку \"удалить\"</small>";
  	print "<form action=addnews.pl method=post><tr><td>";
  	for($i=0;$i<@line;$i++)
  	  {
  	  	($d,$n)=split("::",$line[$i]);
	  	print "<input type=radio name=del value=$i >[$d] - $n<br>";
	  	
	  }	
  	print "</td></tr><tr><td align=center><input class=bt type=submit value=Удалить></td></tr>";
  	print "<input type=hidden name=step value=3></form></table>";
  }	
  
sub del
  {
  	open (NEWS, "$file");
	@line=<NEWS>;
	close (NEWS);
	@line=reverse(@line);
	@newline="";
	for($i=0;$i<@line;$i++)
	  {
	  	if($i ne $delet)
	  	  {
	  	  	@newline=(@line[$i],@newline);
	  	  }	
	  }
	open (NEWS, ">$file");
	seek (NEWS,0,0);
	print NEWS @newline;
	close (NEWS); 	
  }	      	