CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Nobody can install this one!

 



siceman105
Deleted

Jul 18, 2000, 12:57 AM

Post #1 of 8 (1393 views)
Nobody can install this one! Can't Post

Can any of you perl wizards install this program and make it run properly on a Unix server?

If so come forth for your medal of persitance! Smile
http://users.bart.nl/~kamphuys/software/ihp/individualhomepage.htm


Sice


TheGame+
Deleted

Jul 18, 2000, 3:06 AM

Post #2 of 8 (1393 views)
Re: Nobody can install this one! [In reply to] Can't Post

Probably, by reading the Administrator's manual, the FAQ, the Contents Maintainer's manual and the scripts, and then adapting the different scripts as explained in the manuals.

You might start by adding a #! line at the top of each script, replacing all the $path = 'f:\lnvschdw\snips'; lines with your own path, and replacing all the "$path\\..." with "$path/...". You should also try to understand exactly how the script works, to check for security holes etc.
After that, you're on your own. I'm afraid I don't have the persistence to do all this just for a test, so I guess you can keep that medal for now Smile


siceman105
Deleted

Jul 18, 2000, 4:29 AM

Post #3 of 8 (1393 views)
Re: Nobody can install this one! [In reply to] Can't Post

Yeah I did that, but I keep getting an error message saying it cant open the snp file.

Please tell me where I am going so wrong?


The code is

#!/usr/local/bin/perl
#Koen's Perl Individual Home Page v1.1
#Feel free to modify this script but leave my name and url in it
#http://users.bart.nl/~kamphuys/software/individualhomepage.htm


#home.pl - serves a homepage consisting of a default list or a configured
#list, based on the existence of a cookie
#Set snippet path - #######your input is required
$path = 'http://questserver.hypermart.net/secure/cgi-bin/idv/snips';
$naam = "prefs";

@nvcookie = split(/;/, $ENV{'HTTP_COOKIE'});
foreach $nv (@nvcookie) {
($namecookie, $valuecookie) = split(/=/, $nv);
$namecookie =~ s/ //g;
$input{$namecookie} = $valuecookie;
}

sub afdrukken {
if ($regel1 =~ /^\#LITERAL_CATYES/) {
if ($input{$naam} =~ /cat/) {
$regel1 =~ s/\#LITERAL_CATYES //;
print $regel1;
}
}
elsif ($regel1 =~ /^\#LITERAL_CATNO/) {
if ($input{$naam} !~ /cat/) {
$regel1 =~ s/\#LITERAL_CATNO //;
print $regel1;
}
}
else {
print $regel1 if ($regel1 !~ /^\#/);
}
}
#end sub afdrukken


print "Content-Type: text/html\nPragma: no-cache\n\n";

unless (open (INVOER, "$path/first.snp")) { #aangepast t.o.v. standaardversie
print "unable to open $path/first.snp";
exit;
}
@eerste = <INVOER>;
close INVOER;
foreach $regel1 (@eerste) {
afdrukken();
}


if ($input{$naam} !~ /cat/) {
#er zijn geen categorieen gevraagd - default-instellingen worden opgehaald
unless (open (INVOER, "$path/default.cfg")) {
print "unable to open $path/default.cfg";
exit;
}
@default = <INVOER>;
close INVOER;
foreach $regel (@default) {
if ($regel !~ /^\#/) {
$regel =~ s/\n//;
$opensnip = "$path/$regel";
open (INVOER, $opensnip);
@snip = <INVOER>;
close INVOER;
foreach $regel1 (@snip) {
afdrukken();
}
}
}
}
else {


unless (open (INVOER, "$path/configurable.cfg")) {
print "Unable to open $path/configurable.cfg";
}
@instelbaar = <INVOER>;
close INVOER;
foreach $regel (@instelbaar) {
if ($regel !~ /^\#/) {
if ($regel =~ /^cat/i) {
($catnummer, $catwaarde, $catomschrijving) = split (/ --> /, $regel);
if ($input{$naam} =~ /$catnummer/) {
$opensnip = "$path/$catwaarde";
open (INVOER1, $opensnip);
@snip = <INVOER1>;
close INVOER1;
foreach $regel1 (@snip) {
afdrukken();
}
}
}
}
}
}

open (INVOER, "$path/last.snp");
@laatste = <INVOER>;
close INVOER;
foreach $regel1 (@laatste) {
afdrukken();
}



rGeoffrey
User

Jul 18, 2000, 7:50 AM

Post #4 of 8 (1393 views)
Re: Nobody can install this one! [In reply to] Can't Post

The first place to start is the lines with open. In some cases the code uses an unless block to print an error message on failure, and in other cases it does absolutely nothing (which is bad).

Unless you have a compelling reason to be different, open should always be followed by 'or die'. If things work as they should, the open will do it's job, the or will short circuit, and the die will never happen. When things go badly, this will give some hints as to why.

I also noticed that the four times the subroutine &afdrukken was called, it was always after the open, slurp file, close, and foreach lines. So I have provided a new subroutine &afdrukken_Wrapper to take the file name and do those lines.

One potential problem with my code is I hold the files open until after the foreach loops. If the files could be in use by others this puts them at greater risk of strange things happening. But in most cases this is not an issue.

I am too lazy to actually test it (but I did to a perl -c and the syntax check is good) so I don't know if I deserve the medal either.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


#!/usr/local/bin/perl
#Koen's Perl Individual Home Page v1.1
#Feel free to modify this script but leave my name and url in it
#http://users.bart.nl/~kamphuys/software/individualhomepage.htm


#home.pl - serves a homepage consisting of a default list or a configured
#list, based on the existence of a cookie
#Set snippet path - #######your input is required

$path = 'http://questserver.hypermart.net/secure/cgi-bin/idv/snips';
$naam = "prefs";

@nvcookie = split(/;/, $ENV{'HTTP_COOKIE'});
foreach $nv (@nvcookie) {
($namecookie, $valuecookie) = split(/=/, $nv);
$namecookie =~ s/ //g;
$input{$namecookie} = $valuecookie;
}

sub afdrukken_Wrapper {
my ($file) = @_;

open (DATA, $file) or die "unable to open $file; $!\n";
foreach (<DATA> ) {
afdrukken($_);
}
close DATA;
}

sub afdrukken {
my ($regel1) = @_;

if ($regel1 =~ /^\#LITERAL_CATYES/) {
if ($input{$naam} =~ /cat/) {
$regel1 =~ s/\#LITERAL_CATYES //;
print $regel1;
}
} elsif ($regel1 =~ /^\#LITERAL_CATNO/) {
if ($input{$naam} !~ /cat/) {
$regel1 =~ s/\#LITERAL_CATNO //;
print $regel1;
}
} else {
print $regel1 if ($regel1 !~ /^\#/);
}
}
#end sub afdrukken


print "Content-Type: text/html\nPragma: no-cache\n\n";

&afdrukken_Wrapper ("$path/first.snp");

if ($input{$naam} !~ /cat/) {
#er zijn geen categorieen gevraagd - default-instellingen worden opgehaald
open (INVOER, "$path/default.cfg") or die "unable to open $path/default.cfg; $!\n";
foreach $regel (<INVOER> ) {
if ($regel !~ /^\#/) {
$regel =~ s/\n//;
&afdrukken_Wrapper ("$path/$regel");
}
}
close INVOER;
} else {
open (INVOER, "$path/configurable.cfg") or die "unable to open $path/configurable.cfg; $!\n";
foreach $regel (<INVOER> ) {
if (($regel !~ /^\#/) && ($regel =~ /^cat/i)) {
($catnummer, $catwaarde, $catomschrijving) = split (/ --> /, $regel);
if ($input{$naam} =~ /$catnummer/) {
&afdrukken_Wrapper ("$path/$catwaarde");
}
}
}
close INVOER;
}

&afdrukken_Wrapper ("$path/last.snp");
</pre><HR></BLOCKQUOTE>


TheGame+
Deleted

Jul 18, 2000, 9:02 AM

Post #5 of 8 (1393 views)
Re: Nobody can install this one! [In reply to] Can't Post

It probably told you something like "unable to open http://questserver.hypermart.net/secure/cgi-bin/idv/snips/first.snp". If you add a $! at the end of the print "unable..." line to show the actual error message, it will tell you : 'no such file or directory' or something similar.

Basically, the problem is that $path should contain the path to your local directory on the server, like /usr/home/whatever/this_is_the_place/where_I_uploaded_my_files, and NOT some URL.

Some Per[l] syst[em-s]ense is needed


siceman105
Deleted

Jul 18, 2000, 3:04 PM

Post #6 of 8 (1393 views)
Re: Nobody can install this one! [In reply to] Can't Post

Well I copies the code and changed the path to the correct one. And I didnt get the error message! But the page was blank! Whats that about?
http://questserver.hypermart.net/secure/cgi-bin/idv/home.pl

For your help the two of you have been awarded the medal of persitance. Now who wants the medal of perl honour? Smile

Sice


rGeoffrey
User

Jul 19, 2000, 6:46 AM

Post #7 of 8 (1394 views)
Re: Nobody can install this one! [In reply to] Can't Post

You might want to change...

print "Content-Type: text/html\nPragma: no-cache\n\n";
to...
print "Content-Type: text/html\nPragma: no-cache\n\n<html><head></head><body>";

and add a line to the very end...
print "</body></html>\n";

So the document you are printing will look like the html the Content-Type claims it to be.

Other than that I don't see anything else that jumps out at me. So I suggest BFI (Brute Force and Ignorance). Add 'print STDERR' lines frequently to see where it is getting stuck and exactly what files it is trying to open. To help with that I have included two functions to help flush the buffers and redirect the errors to a log of your choosing.

So try adding these two functions to the beginning of the script and call them with something like...

&Flush_Buffers_Quickly ("logfile.log");

If the web server runs as a user other than yourself, you might need to change the permissions so the server can write to that file. One good way is to create the file yourself and then change the permissions on just the file...

echo something > logfile.log
chmod 0666 logfile.log

I keep these two functions in my configure.pm that is called by all the scripts in my application...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>



sub Flush_Buffers_Quickly
{
my ($file) = @_;

if ($file) {
close (STDERR);
open (STDERR, ">$file") or &Log_File_Error ($file);
}
select (STDERR); $| = 1;
select (STDOUT); $| = 1;

} #Flush_Buffers_Quickly

sub Log_File_Error
{
my ($file) = @_;

select (STDOUT); $| = 1;

#///////////////////////////message
print STDOUT <<EOF;
Content-type: text/html

<html>
<head>
<title>Problem with log file</title>
</head>
<body>
<center>
<h1>ERROR!!!</h1>
This program wants to remap STDERR to a log file '$file'. But it died trying
to open the file '$file'.
<p>
The most likely cause is a lack of write permission on the log file.
<p>
Please check the permissions and then try again.
</center>
</body>
</html>
EOF
#///////////////////////////message
exit (0);

} #Log_File_Error

</pre><HR></BLOCKQUOTE>

In the normal course of events, when something goes wrong, the error message ends up in the main error log file that is shared by most everything on the server (or at least your part of the server).

To get the errors in a place where they can do me some more good, I close STDERR and reopen it to a file of my choosing. Thus things like 'die' which like to write to STDERR can be captured along with any custom 'print STDERR "..."' messages I add.

One problem with this approach is that if something goes wrong with opening the log file, there is no way to report the warning because STDERR is closed. So I include the second function to print that warning directly to the browser, along with the most likely reason for failure.

The other thing that happens here is I play with $| to turn off buffering. So each print will send the data right away, which can be very important if bad things could happen before the buffer is flushed.

Also notice that if you send in a file name like 'logfile.log', the open will have one greater than character and the file will be emptied and started fresh each time. If you send in '>logfile.log', then the open will have two greater than characters, and the log will append to previous results. Just remember if you use the append option that you are responsible for reclaiming the disk space when you are done with the log.

[This message has been edited by rGeoffrey (edited 07-19-2000).]


kamphuys
Deleted

Jul 19, 2000, 11:16 AM

Post #8 of 8 (1393 views)
Re: Nobody can install this one! [In reply to] Can't Post

O, what a bare nonsense about my script!

rGeoffrey wrote: The first place to start is the lines with open. In some cases the code uses an unless block to print an error message on failure, and in other cases it does absolutely nothing (which is bad).

Nope. Ever used a die on IIS/NT? It sure dies, yes, but it won't echo an error to the user. As that's where the script was developed, I workarounded for 'die' using a print message and an exit.

rGeoffrey again:

print "Content-Type: text/html\nPragma: no-cache\n\n<html><head></head><body>";
and add a line to the very end...
print "</body></html>\n";
So the document you are printing will look like the html the Content-Type claims it to be.

NO!!! It always opens first.snp first. This one should have the opening HTML tags. It always opens last.snp at last having the closing html tags. And so you have to edit the script if the content responsible person says: I want a green background in my body.

And rGeoffey again:
One potential problem with my code is I hold the files open until after the foreach loops.

Yep. You hit it right where it should. That's why I first read the snp into an array, close the file handle, and then print...

Koen Kamphuys

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives