Skip to content

Instantly share code, notes, and snippets.

@creaktive
Created May 3, 2011 15:22
Show Gist options
  • Save creaktive/953522 to your computer and use it in GitHub Desktop.
Save creaktive/953522 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
@split_file[2];# Duas metades do nome do arquivo a ser aberto.
$file; # Nome do arquivo a ser aberto.
%form; # Contem as variaveis retornadas do Ambiente.
$path_file=('/zaz/htdocs/esoterico/astrologia/comb/'); # Caminho onde encontrar os arquivos *.htm.
$file_erro=('/zaz/htdocs/esoterico/astrologia/');
# Retorna do ambiente a varialvel %form contendo
# a escolha do usuario.
&getArgs();
#print "Content-type: text/html\n\n";
#print %form;
#exit(1);
foreach $name(keys %form){
if($name eq 'meu'){
$split_file[0]=$form{$name};
}
if($name eq 'par'){
$split_file[1]=$form{$name};
}
}
if($split_file[0] eq ""){
&MontaHTML("$file_erro"."erro.htm","ERRO=\"MEU SIGNO\" tambem deve ser selecionado");
}
if($split_file[1] eq ""){
&MontaHTML("$file_erro"."erro.htm","ERRO=\"MEU PARCEIRO\" tambem deve ser selecionado");
}
#monta nome do arquivo que sera aberto.
$file="$split_file[0]"."_"."$split_file[1]".".htm";
open(FILE,"$path_file"."$file") or &erro("Erro na abertura do arquivo $file") ;
print "Content-type: text/html\n\n";
#Imprime em stdout
while($line = <FILE>){
print STDOUT $line;
}
close(FILE);
###########################################################################
#
# Function: getArgs()
#
# Descrip.:
# Returns :
#
# Criacao:
# Revisao:
# Modific:
#
sub getArgs {
if ($ENV{'REQUEST_METHOD'} eq 'POST') {
# POST method dictates that we get the form
# input from standard input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
# Split the name-value pairs on &
@pairs = split(/&/, $buffer);
# Go through the pairs and determine the name
# and value for each form variable
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-f0-9])/pack("C", hex($1))/eg;
$form{$name} = $value;
}
}
}
###########################################################################
#
# Function: erro()
#
# Descrip.:
# Returns :
#
# Criacao:
# Revisao:
# Modific:
sub erro{
local($mensagem)=@_;
print "Content-type: text/html\n\n";
print STDOUT $mensagem;
exit(1);
}
###############################################################################
#
#
#
#
#
#
#
#
#
sub MontaHTML {
local ($szFileName) = $_[0];
local ($szString) = $_[1];
local ($szContent) = $_[2];
#Abre o arquivo da pagina
if (!open( hHTML, $szFileName)) {
print "Content-type: text/html\n\n\n";
print "<P>ERRO: pagina $szFileName nao encontrada";
return 1;
}
#Contador de variaveis
$imax=0;
#Separa as variaveis
@buffer = split(/%/, $szString);
foreach $buffer (@buffer) {
# Procura pelo primeiro igual
$pos = index($buffer,"=");
$nome = substr($buffer,0,$pos);
$valor = substr($buffer,$pos+1);
$valor =~ tr/+/ /;
$NOME[$imax]=$nome;
$VALOR[$imax]=$valor;
$imax++;
}
# Envia para o browser o tipo de pagina
if ($szContent eq "") {
print "Content-type: text/html\n\n\n";
}
while( <hHTML>) {
$Linha = $_;
# Inicio de bloco
if (index($Linha, "<!--INICIO-->") != -1) {
$ini_bloco = 1;
next;
}
# Fim de bloco
if (index($Linha, "<!--FIM-->") != -1) {
&MontaBloco($bloco);
$ini_bloco = 0;
next;
}
# Esta dentro do bloco
if ($ini_bloco == 1) {
$bloco = "$bloco$Linha";
next;
}
# Substitui as variaveis do HTML
for ($i = 0; $i < $imax; $i++) {
# Esta em texto normal
$Linha =~ s/%%$NOME[$i]%%/$VALOR[$i]/g;
}
$Linha =~ s/%%(\w+)%%//g;
print $Linha;
}
close( hHTML);
exit(0);
}
###############################################################################
#
#
#
#
#
#
#
#
#
sub MontaBloco() {
local($bloco) = @_;
for ($j = 1; $j <= scalar(@NOME); $j++) {
my $tmp = $bloco;
$tmp =~ s/(%%)(\w+)(%%)/$1$2$j$3/g;
if (&esta_no_array("$2$j") == 0) {
next;
}
for ($i = 0; $i < scalar(@NOME); $i++) {
$tmp =~ s/%%$NOME[$i]%%/$VALOR[$i]/g;
}
print $tmp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment