#!/usr/bin/perl

#
# This script is copyright 2004, DOMINGO RAMOS CORPAS.
#
#########################################################################
##### USTED NO ESTA AUTORIZADO A COPIAR NI MODIFICAR ESTE SOFTWARE.
#####                                                                   
#####                                                                   
#########################################################################
#

#$scripturl = "http://medicinafetal.info/scgi-bin/testcombinado.pl";

#Recogemos la cadena de datos
%data = &get_data();


#Colocamos los datos en sus variables
$pagina= $data{'direccion'};



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

$archivo = "cabeceranueva";
open(FILE,"$archivo");
while($entry = <FILE>) {
        print $entry;
}
close(FILE);

$archivo = "cabeceranueva2";
open(FILE,"$archivo");
while($entry = <FILE>) {
        print $entry;
}
close(FILE);

$archivo = $pagina;
if (!(-e "$archivo")) {
    print "<br><b><br><p><br><p><br><p><br><p><br><p><br><p><br><p><br><p><br><p><br><p><center><H1>Página en Construcción.</H1><br><p>(Disculpen las molestias)<br><p><br><p><br><p><br><p><br><p><br><p><br><p><br><p><br><p><br><p><br><p><br><p><br><p>";
}
open(FILE,"$archivo");
while($entry = <FILE>) {
        print $entry;
}
close(FILE);

$archivo = "pienuevo";
open(FILE,"$archivo");
while($entry = <FILE>) {
        print $entry;
}
close(FILE);

exit;

#Recabamos los datos entrantes

sub get_data {
    local($string);

    # get data
    if ($ENV{'REQUEST_METHOD'} eq 'GET') {
        $_ = $string = $ENV{'QUERY_STRING'};
	tr/\"~;/_/;
	$string = $_;

    }				
    else { read(STDIN, $string, $ENV{'CONTENT_LENGTH'});
        $_ = $string;
	$OK_CHARS='a-zA-Z0-9=&%\n\/_\-\.@';
	tr/\"~;/_/;
	$string = $_;
	   }

    # split data into name=value pairs
    @data = split(/&/, $string);
   
    # split into name=value pairs in associative array
    foreach (@data) {
	split(/=/, $_);
	$_[0] =~ s/\+/ /g; # plus to space
	$_[0] =~ s/%(..)/pack("c", hex($1))/ge; # hex to alphanumeric
	$data{"$_[0]"} = $_[1];
    }

    # translate special characters
    foreach (keys %data) {
	$data{"$_"} =~ s/\+/ /g; # plus to space
	$data{"$_"} =~ s/%(..)/pack("c", hex($1))/ge; # hex to alphanumeric
    }

    %data;			# return associative array of name=value
}
