#!/usr/bin/perl
# Julio-Diciembre 2004 Daniel Clemente (.com)
#
#  Haz lo que quieras con este script.
#  Aviso: no sé mucho Perl.
#  

$me = "comenta.pl"; # el nombre de este archivo
$base = "comments.htm"; # el archivo (inicialmente vacío) con cada registro

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

# filtrar entrada
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
        ($name, $value) = split(/=/, $pair);
        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        $value =~ s/<([^>]|\n)*>//g; 
        $value =~ s/<//g;
        $value =~ s/>//g;
        $FORM{$name} = $value;
}

$nom=$FORM{'nom'};
$com=$FORM{'com'};


$nom=substr($nom,0,70); # limita el nombre

# corta las palabras largas
my (@longwords) = split(/\s+/, $com);
$com = "";
foreach (@longwords) {
  $com .= " ".substr($_,0,142);
}

# limita la longitud del comentario
$com=substr($com,1,2500); # de paso, quita el espacio inicial
 

# llama a lo que le toca. No añadir comentarios en blanco.
if ($ENV{'QUERY_STRING'} =~ /add/ && !($nom eq "" && $com eq "") ) {
  &Add;
} else {
  &Muestra;
}


sub Add {


  # valores por defecto
  if($nom eq "") { $nom="Anónimo"; }
  if($com eq "") { $com="(sin texto)"; }

         
 # fecha
 ($day, $month, $year) = (localtime)[3,4,5];
 $year+=1900; $month++;
 $fecha = $day."-".$month."-".$year; 
 
  open FILE, ">>$base";

  print FILE
  "<h3><em>$fecha</em> &nbsp;&nbsp; Nombre: <em>$nom</em></h3>\n<p>$com</p>\n\n";

  close(FILE);
  
&Muestra;
}


sub Muestra{

# Cabecera
print "<html>
  <head>
  <title>Deja tu opinión</title>
  <style type='text/css'>
h1 {text-align:center; text-decoration:underline; margin-bottom: 40px;}
h3 { background: #cecedd; margin-bottom: 0;}
p {background: #f0f0ee; margin-top:0; padding: 5px;}
p,h3 {text-indent: 20px;}
body {margin: 50px 20% 40px;}

  </style>
  </head>
  <body>
  <h1>Comentarios cortos</h1>
  ";

# Mensajes
  open FILE, "$base"; while (<FILE>) { print "$_"; } close(FILE);

# Formulario
print "<br><div><form action='$me?add' method=post>
  Nombre: <input type=text name=nom size=30 maxlength=70><br>
  Comentario: <br><textarea name=com rows=5 cols=50 wrap=physical></textarea><br>
  <input type=submit value='Añade'><br>
  </form></div>\n";
 
# Cierre
  print "<br></body></html>"; 
}


syntax highlighted by Code2HTML, v. 0.9.1