Solved

Easy modification to script....I HOPE!

Posted on 2001-06-20
14
198 Views
Last Modified: 2013-12-25
Hi all,
I have the following script (included at the botton of this message)

What it basically does is when its called it looks for a variable called "id" and opens an associated text file. For example page.cgi?id=hello
would open hello.txt
It prints this out between a header ($header) and footer ($footer)
What I would like to do is is modify the script to accept the details of which header and footer to display.
For example, if the script was called as page.cgi?id=hello&template=red

Then the header red1.txt and the footer red2.txt would be opened.

Can someone please help me modify the script, and maybe tidy it up a little.

Thanks

Siceman

---------
#!/usr/local/bin/perl
#

use LWP::Simple;
use LWP::UserAgent;

&cgi_header;
&cgi_receive;

# Here are your variables, change them to your liking

$header = "/data1/hypermart.net/x/header.html";                    # The location of your header file
$footer = "/data1/hypermart.net/x/footer.html";                    # The location of your footer file
$script_url = "/page.cgi";                    # URL of this program
$horoscopes_dir = "/data1/hypermart.net/x/weekly.txt";     # The locations of your weekly.txt file

# Shouldn't have to change anything below this
#------------------------------------------------------------------------------------------------

open (header, "$header");
while (<header>) { print; }
$name = $FORM{id};
if ($name eq "") { $name="error"; }

#open (HEADER, "$header");
#     while (<HEADER>) { print; }

if (!(-f  "$name.txt") ){ ## That file DOES NOT exist!!

## Open a custom error page
$name="error";
open(E,"$name.txt") || die $!;
@E=<E>;
close(E);

print join("",@E);


 ## Print the footer and exit
 open (FOOTER, "$footer");
 while (<FOOTER>) { print; }
 close(FOOTER);
 exit;
}

open (file, "$name.txt");
 while (<file>) { print; }

open (FOOTER, "$footer");
     while (<FOOTER>) { print; }

#<-------===========Subroutines===========------->
#
# cgi_header      { Outputs HTML Header }
# cgi_receive     { Decodes POST-type Forms }
# parse_form     { Decodes GET-type Forms }
#

sub parse_form {

   # Get the input
   read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

   # Split the name-value pairs
   @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;

      $FORM{$name} = $value;
   }
}


sub cgi_header {
    print "Content-type: text/html\n";
    print "\n" unless ($FORM{'next-url'});
}        

sub cgi_receive {
    if ($ENV{'REQUEST_METHOD'} eq "POST") {
        read(STDIN, $incoming, $ENV{'CONTENT_LENGTH'});
    }
    else {
        $incoming = $ENV{'QUERY_STRING'};
    }
    @pairs = split(/&/, $incoming);

    foreach (@pairs) {
        ($name, $value) = split(/=/, $_);

        $name  =~ tr/+/ /;
        $value =~ tr/+/ /;
        $name  =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;
        $value =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;

        #### Strip out semicolons unless for special character
        $value =~ s/;/$$/g;
        $value =~ s/&(\S{1,6})$$/&\1;/g;
        $value =~ s/$$/ /g;

        $value =~ s/\|/ /g;
        $value =~ s/^!/ /g; ## Allow exclamation points in sentences

        #### Skip blank text entry fields
        next if ($value eq "");

        #### Check for "assign-dynamic" field names
        #### Mainly for on-the-fly input names, especially checkboxes
        if ($name =~ /^assign-dynamic/) {
            $name = $value;
            $value = "on";
        }

     #### Allow for multiple values of a single name
        $FORM{$name} .= ", " if ($FORM{$name});

        $FORM{$name} .= $value;
    }
}
0
Comment
Question by:siceman105
  • 9
  • 5
14 Comments
 
LVL 8

Expert Comment

by:bebonham
Comment Utility
why are you in the cgi area and not using cgi.pm :)

really, in all seriousness...it is a standard part of perl, and I would be glad to decomplicate your script by using cgi.pm if that is okay, and I can also add that dynamic headers...

since you already are using lwp, why not use cgi?

Bob
0
 

Author Comment

by:siceman105
Comment Utility
Im not sure how to use cgi.pm

But Im open to it if youd show me what to do etc!

Cheers

Siceman
0
 
LVL 8

Expert Comment

by:bebonham
Comment Utility
how can this work ??!?!

$name = $FORM{id}; ??

doesn't it have to be $FORM{'id'} or $FORM{$id} !?!?

I would like an explanation on that one...

anyways, try these changes.

it should work...I can't belive that id thing works...

just make sure you make a field in your html form called template!!





#!/usr/local/bin/perl
#

use LWP::Simple;
use LWP::UserAgent;

&cgi_header;
&cgi_receive;

# Here are your variables, change them to your liking

$header = "/data1/hypermart.net/x/header.html";                    # The location of your header file
$footer = "/data1/hypermart.net/x/footer.html";                    # The location of your footer file
$script_url = "/page.cgi";                    # URL of this program
$horoscopes_dir = "/data1/hypermart.net/x/weekly.txt";     # The locations of your weekly.txt file

# Shouldn't have to change anything below this
#------------------------------------------------------------------------------------------------


$header=$FORM{'template'}
$footer=$header + "2";
$header=$header + "1";
open (header, "$header");
while (<header>) { print; }
$name = $FORM{id};
if ($name eq "") { $name="error"; }

#open (HEADER, "$header");
#     while (<HEADER>) { print; }

if (!(-f  "$name.txt") ){ ## That file DOES NOT exist!!

## Open a custom error page
$name="error";
open(E,"$name.txt") || die $!;
@E=<E>;
close(E);

print join("",@E);


## Print the footer and exit
open (FOOTER, "$footer");
while (<FOOTER>) { print; }
close(FOOTER);
exit;
}

open (file, "$name.txt");
while (<file>) { print; }

open (FOOTER, "$footer");
    while (<FOOTER>) { print; }

#<-------===========Subroutines===========------->
#
# cgi_header      { Outputs HTML Header }
# cgi_receive     { Decodes POST-type Forms }
# parse_form     { Decodes GET-type Forms }
#

sub parse_form {

  # Get the input
  read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

  # Split the name-value pairs
  @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;

     $FORM{$name} = $value;
  }
}


sub cgi_header {
   print "Content-type: text/html\n";
   print "\n" unless ($FORM{'next-url'});
}        

sub cgi_receive {
   if ($ENV{'REQUEST_METHOD'} eq "POST") {
       read(STDIN, $incoming, $ENV{'CONTENT_LENGTH'});
   }
   else {
       $incoming = $ENV{'QUERY_STRING'};
   }
   @pairs = split(/&/, $incoming);

   foreach (@pairs) {
       ($name, $value) = split(/=/, $_);

       $name  =~ tr/+/ /;
       $value =~ tr/+/ /;
       $name  =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;
       $value =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;

       #### Strip out semicolons unless for special character
       $value =~ s/;/$$/g;
       $value =~ s/&(\S{1,6})$$/&\1;/g;
       $value =~ s/$$/ /g;

       $value =~ s/\|/ /g;
       $value =~ s/^!/ /g; ## Allow exclamation points in sentences

       #### Skip blank text entry fields
       next if ($value eq "");

       #### Check for "assign-dynamic" field names
       #### Mainly for on-the-fly input names, especially checkboxes
       if ($name =~ /^assign-dynamic/) {
           $name = $value;
           $value = "on";
       }

    #### Allow for multiple values of a single name
       $FORM{$name} .= ", " if ($FORM{$name});

       $FORM{$name} .= $value;
   }
}



good luck,

Bob
0
 
LVL 8

Expert Comment

by:bebonham
Comment Utility
cool...this is in cgi...

because it looks simpler :)


#!/usr/local/bin/perl
#

use LWP::Simple;
use LWP::UserAgent;
use CGI ':standard';

&cgi_header;
&cgi_receive;

# Here are your variables, change them to your liking

$header = "/data1/hypermart.net/x/header.html";                    # The location of your header file
$footer = "/data1/hypermart.net/x/footer.html";                    # The location of your footer file
$script_url = "/page.cgi";                    # URL of this program
$horoscopes_dir = "/data1/hypermart.net/x/weekly.txt";     # The locations of your weekly.txt file

# Shouldn't have to change anything below this
#------------------------------------------------------------------------------------------------


$header=$FORM{'template'}
$footer=$header + "2";
$header=$header + "1";
open (header, "$header");
while (<header>) { print; }
$name = $FORM{id};
if ($name eq "") { $name="error"; }

#open (HEADER, "$header");
#     while (<HEADER>) { print; }

if (!(-f  "$name.txt") ){ ## That file DOES NOT exist!!

## Open a custom error page
$name="error";
open(E,"$name.txt") || die $!;
@E=<E>;
close(E);

print join("",@E);


## Print the footer and exit
open (FOOTER, "$footer");
while (<FOOTER>) { print; }
close(FOOTER);
exit;
}

open (file, "$name.txt");
while (<file>) { print; }

open (FOOTER, "$footer");
    while (<FOOTER>) { print; }

#<-------===========Subroutines===========------->
#
# cgi_header      { Outputs HTML Header }
# cgi_receive     { Decodes POST-type Forms }
# parse_form     { Decodes GET-type Forms }
#

sub parse_form {
&cgi_receive;
}



sub cgi_header {
 print header unless ($FORM{'next-url'});
}        

sub cgi_receive {

foreach(param())
{
$FORM{$_}=param($_);
}


otherwise, I pretty much like the script...

I like how you have an error routine that include the printing of the header...

I would have done it like this
..this isn't real code...this is just the logic I would have used...

if(error)
{
print (errmsg);
}

if(!error)
{
print (correcttextFile);
}

print footer;
exit;

but you did...

if(error)
{
print (errmsg);
print header;
exit;
}

print (correcttextFile);
print footer;
exit;


I think I like your logic better :)


Bob
0
 
LVL 8

Expert Comment

by:bebonham
Comment Utility
hold on here...I will repost...

forgot to add the .txt to the new files...


first, the CGI version:


#!/usr/local/bin/perl
#

use LWP::Simple;
use LWP::UserAgent;
use CGI ':standard';

&cgi_header;
&cgi_receive;

# Here are your variables, change them to your liking

$header = "/data1/hypermart.net/x/header.html";                    # The location of your header file
$footer = "/data1/hypermart.net/x/footer.html";                    # The location of your footer file
$script_url = "/page.cgi";                    # URL of this program
$horoscopes_dir = "/data1/hypermart.net/x/weekly.txt";     # The locations of your weekly.txt file

# Shouldn't have to change anything below this
#------------------------------------------------------------------------------------------------


$header=$FORM{'template'}
$footer=$header + "2.txt";
$header=$header + "1.txt";
open (header, "$header");
while (<header>) { print; }
$name = $FORM{id};
if ($name eq "") { $name="error"; }

#open (HEADER, "$header");
#     while (<HEADER>) { print; }

if (!(-f  "$name.txt") ){ ## That file DOES NOT exist!!

## Open a custom error page
$name="error";
open(E,"$name.txt") || die $!;
@E=<E>;
close(E);

print join("",@E);


## Print the footer and exit
open (FOOTER, "$footer");
while (<FOOTER>) { print; }
close(FOOTER);
exit;
}

open (file, "$name.txt");
while (<file>) { print; }

open (FOOTER, "$footer");
   while (<FOOTER>) { print; }

#<-------===========Subroutines===========------->
#
# cgi_header      { Outputs HTML Header }
# cgi_receive     { Decodes POST-type Forms }
# parse_form     { Decodes GET-type Forms }
#

sub parse_form {
&cgi_receive;
}



sub cgi_header {
print header unless ($FORM{'next-url'});
}        

sub cgi_receive {

foreach(param())
{
$FORM{$_}=param($_);
}





#######################
okay, now for the non-cgi version:







#!/usr/local/bin/perl
#

use LWP::Simple;
use LWP::UserAgent;

&cgi_header;
&cgi_receive;

# Here are your variables, change them to your liking

$header = "/data1/hypermart.net/x/header.html";                    # The location of your header file
$footer = "/data1/hypermart.net/x/footer.html";                    # The location of your footer file
$script_url = "/page.cgi";                    # URL of this program
$horoscopes_dir = "/data1/hypermart.net/x/weekly.txt";     # The locations of your weekly.txt file

# Shouldn't have to change anything below this
#------------------------------------------------------------------------------------------------


$header=$FORM{'template'}
$footer=$header + "2.txt";
$header=$header + "1.txt";
open (header, "$header");
while (<header>) { print; }
$name = $FORM{id};
if ($name eq "") { $name="error"; }

#open (HEADER, "$header");
#     while (<HEADER>) { print; }

if (!(-f  "$name.txt") ){ ## That file DOES NOT exist!!

## Open a custom error page
$name="error";
open(E,"$name.txt") || die $!;
@E=<E>;
close(E);

print join("",@E);


## Print the footer and exit
open (FOOTER, "$footer");
while (<FOOTER>) { print; }
close(FOOTER);
exit;
}

open (file, "$name.txt");
while (<file>) { print; }

open (FOOTER, "$footer");
   while (<FOOTER>) { print; }

#<-------===========Subroutines===========------->
#
# cgi_header      { Outputs HTML Header }
# cgi_receive     { Decodes POST-type Forms }
# parse_form     { Decodes GET-type Forms }
#

sub parse_form {

 # Get the input
 read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

 # Split the name-value pairs
 @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;

    $FORM{$name} = $value;
 }
}


sub cgi_header {
  print "Content-type: text/html\n";
  print "\n" unless ($FORM{'next-url'});
}        

sub cgi_receive {
  if ($ENV{'REQUEST_METHOD'} eq "POST") {
      read(STDIN, $incoming, $ENV{'CONTENT_LENGTH'});
  }
  else {
      $incoming = $ENV{'QUERY_STRING'};
  }
  @pairs = split(/&/, $incoming);

  foreach (@pairs) {
      ($name, $value) = split(/=/, $_);

      $name  =~ tr/+/ /;
      $value =~ tr/+/ /;
      $name  =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;
      $value =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;

      #### Strip out semicolons unless for special character
      $value =~ s/;/$$/g;
      $value =~ s/&(\S{1,6})$$/&\1;/g;
      $value =~ s/$$/ /g;

      $value =~ s/\|/ /g;
      $value =~ s/^!/ /g; ## Allow exclamation points in sentences

      #### Skip blank text entry fields
      next if ($value eq "");

      #### Check for "assign-dynamic" field names
      #### Mainly for on-the-fly input names, especially checkboxes
      if ($name =~ /^assign-dynamic/) {
          $name = $value;
          $value = "on";
      }

   #### Allow for multiple values of a single name
      $FORM{$name} .= ", " if ($FORM{$name});

      $FORM{$name} .= $value;
  }
}




okay,


there it is, all fixed up.

Bob
0
 

Author Comment

by:siceman105
Comment Utility
So the first one uses CGI.pm? Does that mean it will run faster or something?

Thanks

Siceman
0
 
LVL 8

Expert Comment

by:bebonham
Comment Utility
no it actually means it will run slower :)
really!

cgi.pm is one of the biggest (besides lwp and gd) best modules available for perl...(it also takes a few seconds...maybe milliseconds) to load

the code without the cgi.pm will run faster, and be more portable...

of course...cgi.pm is great, and it's nice to have it available to your scripts...I personally always use cgi.pm if I am going to be reading fields from an http post...

but that is a matter of preference...

totally up to you...

maneshr, #1 in both this area and the perl area, would probably suggest you use the NON cgi version.

Bob
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:siceman105
Comment Utility
Hi,
yeah Manshr helped me develop the first one.

However, the non cgi version you just gave me doesnt seem to work, I get an Error 500 with the following details:

Scalar found where operator expected at /home//temp.cgi line 22, at end of line
     (Missing semicolon on previous line?)
syntax error at /home//temp.cgi line 22, near "$footer"
Execution of /home//temp.cgi aborted due to compilation errors.



Any ideas?

Thanks

Steve
0
 
LVL 8

Expert Comment

by:bebonham
Comment Utility
man...
sometimes I wonder about myself :)

I can't even type 2 lines of code without a typo!

this is the problem in BOTH scripts...

$header=$FORM{'template'}


needs to have a ; after it so this is the fixed non-cgi version.



#!/usr/local/bin/perl
#

use LWP::Simple;
use LWP::UserAgent;

&cgi_header;
&cgi_receive;

# Here are your variables, change them to your liking

$header = "/data1/hypermart.net/x/header.html";                    # The location of your header file
$footer = "/data1/hypermart.net/x/footer.html";                    # The location of your footer file
$script_url = "/page.cgi";                    # URL of this program
$horoscopes_dir = "/data1/hypermart.net/x/weekly.txt";     # The locations of your weekly.txt file

# Shouldn't have to change anything below this
#------------------------------------------------------------------------------------------------


$header=$FORM{'template'};
$footer=$header + "2.txt";
$header=$header + "1.txt";
open (header, "$header");
while (<header>) { print; }
$name = $FORM{id};
if ($name eq "") { $name="error"; }

#open (HEADER, "$header");
#     while (<HEADER>) { print; }

if (!(-f  "$name.txt") ){ ## That file DOES NOT exist!!

## Open a custom error page
$name="error";
open(E,"$name.txt") || die $!;
@E=<E>;
close(E);

print join("",@E);


## Print the footer and exit
open (FOOTER, "$footer");
while (<FOOTER>) { print; }
close(FOOTER);
exit;
}

open (file, "$name.txt");
while (<file>) { print; }

open (FOOTER, "$footer");
  while (<FOOTER>) { print; }

#<-------===========Subroutines===========------->
#
# cgi_header      { Outputs HTML Header }
# cgi_receive     { Decodes POST-type Forms }
# parse_form     { Decodes GET-type Forms }
#

sub parse_form {

# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@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;

   $FORM{$name} = $value;
}
}


sub cgi_header {
 print "Content-type: text/html\n";
 print "\n" unless ($FORM{'next-url'});
}        

sub cgi_receive {
 if ($ENV{'REQUEST_METHOD'} eq "POST") {
     read(STDIN, $incoming, $ENV{'CONTENT_LENGTH'});
 }
 else {
     $incoming = $ENV{'QUERY_STRING'};
 }
 @pairs = split(/&/, $incoming);

 foreach (@pairs) {
     ($name, $value) = split(/=/, $_);

     $name  =~ tr/+/ /;
     $value =~ tr/+/ /;
     $name  =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;
     $value =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;

     #### Strip out semicolons unless for special character
     $value =~ s/;/$$/g;
     $value =~ s/&(\S{1,6})$$/&\1;/g;
     $value =~ s/$$/ /g;

     $value =~ s/\|/ /g;
     $value =~ s/^!/ /g; ## Allow exclamation points in sentences

     #### Skip blank text entry fields
     next if ($value eq "");

     #### Check for "assign-dynamic" field names
     #### Mainly for on-the-fly input names, especially checkboxes
     if ($name =~ /^assign-dynamic/) {
         $name = $value;
         $value = "on";
     }

  #### Allow for multiple values of a single name
     $FORM{$name} .= ", " if ($FORM{$name});

     $FORM{$name} .= $value;
 }
}



okay, hope that works for you,

Bob

0
 

Author Comment

by:siceman105
Comment Utility
Hi,
thanks. Im not getting the error any more, but the header and footer arent appearing as expected. Im wondering if it has something to do with this bit of code

$header=$FORM{'template'};
$footer=$header + "2.txt";
$header=$header + "1.txt";

isnt $header being defined twice?

Thanks

Siceman
0
 
LVL 8

Expert Comment

by:bebonham
Comment Utility
doesn't matter, I am reassigning the value...

but... in this case, the value that is being assigned is not existing.

and in this case, it is again an error of my poor debugging...


I am doing this $string + "string";

which is okay in javascript, but in perl, it doesn't work...you have to use .

so...here is the once again corrected code...sorry about the trouble :)


#!/usr/local/bin/perl
#

use LWP::Simple;
use LWP::UserAgent;

&cgi_header;
&cgi_receive;

# Here are your variables, change them to your liking

$header = "/data1/hypermart.net/x/header.html";                    # The location of your header file
$footer = "/data1/hypermart.net/x/footer.html";                    # The location of your footer file
$script_url = "/page.cgi";                    # URL of this program
$horoscopes_dir = "/data1/hypermart.net/x/weekly.txt";     # The locations of your weekly.txt file

# Shouldn't have to change anything below this
#------------------------------------------------------------------------------------------------


$header=$FORM{'template'};
$footer=$header . "2.txt";
$header=$header . "1.txt";
open (header, "$header");
while (<header>) { print; }
$name = $FORM{id};
if ($name eq "") { $name="error"; }

#open (HEADER, "$header");
#     while (<HEADER>) { print; }

if (!(-f  "$name.txt") ){ ## That file DOES NOT exist!!

## Open a custom error page
$name="error";
open(E,"$name.txt") || die $!;
@E=<E>;
close(E);

print join("",@E);


## Print the footer and exit
open (FOOTER, "$footer");
while (<FOOTER>) { print; }
close(FOOTER);
exit;
}

open (file, "$name.txt");
while (<file>) { print; }

open (FOOTER, "$footer");
 while (<FOOTER>) { print; }

#<-------===========Subroutines===========------->
#
# cgi_header      { Outputs HTML Header }
# cgi_receive     { Decodes POST-type Forms }
# parse_form     { Decodes GET-type Forms }
#

sub parse_form {

# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@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;

  $FORM{$name} = $value;
}
}


sub cgi_header {
print "Content-type: text/html\n";
print "\n" unless ($FORM{'next-url'});
}        

sub cgi_receive {
if ($ENV{'REQUEST_METHOD'} eq "POST") {
    read(STDIN, $incoming, $ENV{'CONTENT_LENGTH'});
}
else {
    $incoming = $ENV{'QUERY_STRING'};
}
@pairs = split(/&/, $incoming);

foreach (@pairs) {
    ($name, $value) = split(/=/, $_);

    $name  =~ tr/+/ /;
    $value =~ tr/+/ /;
    $name  =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;
    $value =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;

    #### Strip out semicolons unless for special character
    $value =~ s/;/$$/g;
    $value =~ s/&(\S{1,6})$$/&\1;/g;
    $value =~ s/$$/ /g;

    $value =~ s/\|/ /g;
    $value =~ s/^!/ /g; ## Allow exclamation points in sentences

    #### Skip blank text entry fields
    next if ($value eq "");

    #### Check for "assign-dynamic" field names
    #### Mainly for on-the-fly input names, especially checkboxes
    if ($name =~ /^assign-dynamic/) {
        $name = $value;
        $value = "on";
    }

 #### Allow for multiple values of a single name
    $FORM{$name} .= ", " if ($FORM{$name});

    $FORM{$name} .= $value;
}
}


okay, good luck!

Bob
0
 

Author Comment

by:siceman105
Comment Utility
Cheers Bob! It works!

Just one last thing, is there any way that a small bit of code could be added setting a default for the template incase no variable or an invalid one is entered

Siceman
0
 
LVL 8

Accepted Solution

by:
bebonham earned 150 total points
Comment Utility
certainly,

I'm glad it is working...



#!/usr/local/bin/perl
#

use LWP::Simple;
use LWP::UserAgent;

&cgi_header;
&cgi_receive;

# Here are your variables, change them to your liking

$header = "/data1/hypermart.net/x/header.html";                    # The location of your header file
$footer = "/data1/hypermart.net/x/footer.html";                    # The location of your footer file
$script_url = "/page.cgi";                    # URL of this program
$horoscopes_dir = "/data1/hypermart.net/x/weekly.txt";     # The locations of your weekly.txt file

# Shouldn't have to change anything below this
#------------------------------------------------------------------------------------------------


$template=$FORM{'template'};
$footer= -e $template . "2.txt" ? $template . "2.txt" : $footer;
$header= -e $template . "1.txt" ? $template . "1.txt" : $header;
open (header, "$header");
while (<header>) { print; }
$name = $FORM{id};
if ($name eq "") { $name="error"; }

#open (HEADER, "$header");
#     while (<HEADER>) { print; }

if (!(-f  "$name.txt") ){ ## That file DOES NOT exist!!

## Open a custom error page
$name="error";
open(E,"$name.txt") || die $!;
@E=<E>;
close(E);

print join("",@E);


## Print the footer and exit
open (FOOTER, "$footer");
while (<FOOTER>) { print; }
close(FOOTER);
exit;
}

open (file, "$name.txt");
while (<file>) { print; }

open (FOOTER, "$footer");
while (<FOOTER>) { print; }

#<-------===========Subroutines===========------->
#
# cgi_header      { Outputs HTML Header }
# cgi_receive     { Decodes POST-type Forms }
# parse_form     { Decodes GET-type Forms }
#

sub parse_form {

# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@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;

 $FORM{$name} = $value;
}
}


sub cgi_header {
print "Content-type: text/html\n";
print "\n" unless ($FORM{'next-url'});
}        

sub cgi_receive {
if ($ENV{'REQUEST_METHOD'} eq "POST") {
   read(STDIN, $incoming, $ENV{'CONTENT_LENGTH'});
}
else {
   $incoming = $ENV{'QUERY_STRING'};
}
@pairs = split(/&/, $incoming);

foreach (@pairs) {
   ($name, $value) = split(/=/, $_);

   $name  =~ tr/+/ /;
   $value =~ tr/+/ /;
   $name  =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;
   $value =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;

   #### Strip out semicolons unless for special character
   $value =~ s/;/$$/g;
   $value =~ s/&(\S{1,6})$$/&\1;/g;
   $value =~ s/$$/ /g;

   $value =~ s/\|/ /g;
   $value =~ s/^!/ /g; ## Allow exclamation points in sentences

   #### Skip blank text entry fields
   next if ($value eq "");

   #### Check for "assign-dynamic" field names
   #### Mainly for on-the-fly input names, especially checkboxes
   if ($name =~ /^assign-dynamic/) {
       $name = $value;
       $value = "on";
   }

#### Allow for multiple values of a single name
   $FORM{$name} .= ", " if ($FORM{$name});

   $FORM{$name} .= $value;
}
}

the default value for $header and $footer as set in the begenning of the script will be used if the entered header/footer don't exist.

and good news, I tested it and it works!


hope that helps,

Bob
0
 
LVL 8

Expert Comment

by:bebonham
Comment Utility
did you have a chance to try the above modified script?
take your time, though, if you need to.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now