#!/usr/bin/perl

require"./read.pl";
require"./kopf.lib";

&ReadParse;
&SpaceKill;

$counter_art=$in{'button'}||"blank";
$farbe=$in{'farbe'}||"0,0,0";

$home=$ENV{DOCUMENT_ROOT};
$url_system=$ENV{SERVER_NAME};

$url_kunde=$in{'url'}||$url_system;
$url_kunde=~ s/,/\//g;
$name=$url_kunde;


$text = $in{'text'}||$url_kunde;
$wunschzahl = $in{'counter'};

@keyIN=keys(%in);

#print "Content-type: text/html\n\n";


if($in{ok} ne "eins"){

open(HTML0,"counter.txt");
@counter=<HTML0>;
close(HTML0);
# hash fuer die counter zusammenstellen
for(@counter)
{
chop($_);
@teile=split(/=/,$_);
$COUNT{$teile[0]}=$teile[1];
}
# zahl benoetigt

if($COUNT{$name} ne ""){&weiter($name,$COUNT{$name});}

if($wunschzahl/1 >= 0){$COUNT{$name}=$wunschzahl;}else{$COUNT{$name}=1;}

# neuen Counter Hinzufuegen

open(HTML0,">>counter.txt")|| goto neuAnlegen;
print HTML0 "$name=".$COUNT{$name}."\n";
close(HTML0);

open(STAT,"count_statistik.txt");
@STAT=<STAT>;
close(STAT);
$count_statistik=@STAT;
$count_statistik++;

open(HTML0,">>count_statistik.txt");
print HTML0 "$name=$count_statistik\n";
close(HTML0);


goto hierher;
neuAnlegen:

open(HTML0,">counter.txt");
print HTML0 "$name=".$COUNT{$name}."\n";
close(HTML0);

open(HTML0,">count_statistik.txt");
print HTML0 "$name=1\n";
close(HTML0);


hierher:

&net_mail($name);

}# ende neu anlegen
#print "Content-type: text/html\n\n";

$eingang="\?link=$name\&form=$counter_art\&text_color=$farbe\&text=$text";

$gesamt="<center><a href=\"http://$url_system/cgi-bin/Link_counter/LC_counter.pl?link=$url_kunde\"><IMG SRC=\"http://$url_system/cgi-bin/Link_counter/LC.pl$eingang\" border=0 alt=\"kostenlos von www.netViper.de\"></a></center>";
$gesamt=&COD($gesamt);

$url="http://".$ENV{SERVER_NAME}."/cgi-bin/Link_counter/link_button_4.pl?$gesamt";

print "Location:$url\n\n";

exit(0);
sub weiter{
local($wert,$zahl)=@_;

print "Content-type: text/html\n\n";
print "<HTML><BODY BGCOLOR=ffffff><BR><BR><center><H3>";
print "<Form action=\"/cgi-bin/Link_counter/link_button_3.pl\" METHOD=\"POST\">";
print "Nur zur Information !<BR><BR>";
print "Der Name <Font color=ff0000>$wert</Font> ist bereits mit $zahl belegt !</H3><BR><BR>";
@keyIN=keys(%in);
for(@keyIN){
print "<input type=\"hidden\" name=\"".$_."\" value=\"".$in{$_}."\">\n";
}
print "<input type=\"hidden\" name=\"ok\" value=\"eins\">\n";
print "<INPUT TYPE=\"submit\" VALUE=\"weiter\"></form><BR></center></BODY></HTML>";
exit(0);}

sub ende{
local($wert)=@_;

print "Content-type: text/html\n\n";
print "<HTML><BODY BGCOLOR=ffffff><BR><BR><center><H3>";
print "<Form action=\"/cgi-bin/Link_counter/link_button_3.pl\" METHOD=\"POST\">";
print "Der Name <Font color=ff0000>$wert</Font> ist nicht korrekt !</H3><BR><BR>";
print "Bitte eingeben !<BR><BR>";
print "neuer $wert :<INPUT type=text name=$wert><BR><BR>";
delete($in{$wert});
@keyIN=keys(%in);
for(@keyIN){
print "<input type=\"hidden\" name=\"".$_."\" value=\"".$in{$_}."\">\n";
}
print "<INPUT TYPE=\"submit\" VALUE=\"neuer Versuch\"></form><BR></center></BODY></HTML>";
exit(0);
}




sub net_mail{
local($wert)=@_;
$mailprog = '/usr/sbin/sendmail';
$recipient = 'linkButton@netviper.de';
$absender='admin@netviper.de';

# Mail Option
   open (MAIL, "|$mailprog $recipient") || die "Can't open $mailprog!\n";

   print MAIL "Reply-to: $recipient\n";
   print MAIL "From: $absender\n";
   print MAIL "Sie haben den neuen Link_counter\n";# 1.Eintrag in Homepageliste, 2.Eintrag in Counterliste, 3.Eintrag in Statistik 
   print MAIL "------------------------------------------------------\n";
   print MAIL " - $wert\n";
   print MAIL "------------------------------------------------------\n";

   close (MAIL);

#exit;
}
