Lunarpages Web Hosting Forum

Author Topic: Perl script  (Read 810 times)

Offline spottebaum

  • Newbie
  • *
  • Posts: 2
Perl script
« on: March 05, 2004, 02:13:51 PM »
I was wondering if any of you perl experts could tell me if there is any way the following script could cause 50+ threads on LP's server?  According to the logs it only had 1 hit that somehow caused all of these threads.  I've used this script for years without any problems and was unable to replicate the problems that LP was getting on my linux server.  Thanks for looking..

#!/usr/bin/perl -w
use CGI qw(:standard);

my $path = param("path");
my $image = param("image");
my $action = param("action");

$dir_url = "../pictures/$path/big";
# This is the url to the directory where the full size images are.

$tn_dir_url = "../pictures/$path";
# This is the url to the directory where the thumbnail images are.
# The thumbnail images MUST have the same name as the full size images.

$tn_dir = "../pictures/$path";
# This is the path to the directory where the thumbnail images are.

$script_name = "http://www.mydomain.com/cgi-bin/image_page.pl";
# This is the script name, as you have it on your server.

$header_file = "../header.html";
$footer_file = "../footer.html";
$desc_file = "$tn_dir/desc.html";

if ($action =~ "show"){
   &
   exit;
}
else {
   &
   exit;
}
#########################
# Display requested picture

sub display {

   opendir (TN, "$tn_dir");
   rewinddir (TN);
   @tnlist =  grep(!/^\.\.?$/, readdir (TN));
   closedir (TN);
   $temp = pop @tnlist;
   @tntemp = reverse(@tnlist);
   push (@tntemp,$temp);
   @tnlist = reverse(@tntemp);
   @tnlist = sort @tnlist;
   $tnnum = @tnlist;
   


   $dnum = int ($tnnum);
   
   $cnum = "0";
   while ($image ne $tnlist[$cnum]) {
      $cnum = $cnum + 1;
   }
   $content_file = "<center><img border=\"0\" src=\"$dir_url/$image\" ></center><br>";

   print "Content-type: text/html\n\n";
   
   open(FILE,"$header_file");
   while(<FILE>) {
      print $_;
   }
   &print_links;
   open(FILE,"$tn_dir/big/$image.html");
    while(<FILE>) {
    print $_;
   }
   print "$content_file";

   &print_links;
   open(FILE,"$footer_file");
   while(<FILE>) {
      print $_;
   }
}
#######################
# Make table page

sub table {
   opendir (TN, "$tn_dir");
   rewinddir (TN);
   @tnlist =  grep(!/^\.\.?$/, readdir (TN));
   closedir (TN);
   $temp = pop @tnlist;
   @tntemp = reverse(@tnlist);
   push (@tntemp,$temp);
   @tnlist = reverse(@tntemp);
   @tnlist = sort @tnlist;

   $tnnum = @tnlist;
   $dnum = int ($tnnum);
   print "Content-type: text/html\n\n";
   open(FILE,"$header_file");
   while(<FILE>) {
      print $_;
   }
   
   open(FILE,"$desc_file");
   while(<FILE>) {
      print $_;
   }
   
   print "<center><i>(Click on any image to see a full size version and description of the picture.)</i></center><br>\n";
   &main_table;

   print "<center><i>(Click on any image to see a full size version and description of the picture.)</i></center>\n";
   open(FILE,"$footer_file");
   while(<FILE>) {
      print $_;
   }

}

sub main_table {
   $cnum = "0";
   print "<table align=\"center\" border=\"0\" cellpadding=\"1\" cellspacing=\"1\" width=\"70%\">";
   while ($dnum > $cnum) {
      ($dfname, $dext) = split(/\./, $tnlist[$cnum], 2);
      if ($dext eq "jpg") {
         print "<tr>";
         &print_image;
      }
      $cnum++;
      ($dfname, $dext) = split(/\./, $tnlist[$cnum], 2);
      if ($dext eq "jpg") {
         &print_image;
        }
      print "</tr>";
      $cnum = $cnum + 1;
      print "\n";
   }
   print "<tr></table>";
}

sub print_image {
   print "<td align=\"center\" width=\"800\" hieght=\"600\">";
   if ($tnlist[$cnum] ne "") {
      print "<a href=\"$script_name?path=$path&action=show&image=$tnlist[$cnum]\">";
      print "<img border=\"0\" src=\"" . "$tn_dir_url/$tnlist[$cnum]" . "\">";
      print "</a>";
   }
   if  ($tnlist[$cnum] =~ "") {
      print " \;";   
   }
   print "</td>";
}

sub print_links {
   print "<center>";
   ($dfname, $dext) = split(/\./, $tnlist[$cnum-1], 2);
   if ($dext =~ "jpg") {
      print "<a href=\"$script_name?path=$path&action=show&image=$tnlist[$cnum-1]\"><IMG alt=Back src=\"../images/left_arrow.gif\" border=0></a>";
      print "";
   }
   print "<a href=\"$script_name?path=$path\"><IMG alt=\"Back To Index\" src=\"../images/up_arrow.gif\" border=0></a>";
   ($dfname, $dext) = split(/\./, $tnlist[$cnum+1], 2);
   if ($dext =~ "jpg") {
      print "";
      print "<a href=\"$script_name?path=$path&action=show&image=$tnlist[$cnum+1]\"><IMG alt=Forward src=\"../images/right_arrow.gif\" border=0></a>";
   }
   print "</center>";
   print "<br>";
}

Offline Ripta

  • Master Jedi
  • *****
  • Posts: 1271
Re: Perl script
« Reply #1 on: March 07, 2004, 03:37:33 AM »
Quote from: spottebaum
I was wondering if any of you perl experts could tell me if there is any way the following script could cause 50+ threads on LP's server?  According to the logs it only had 1 hit that somehow caused all of these threads.  I've used this script for years without any problems and was unable to replicate the problems that LP was getting on my linux server.  Thanks for looking..
Code: [Select]

-- removed --



Hi!

I can't see why it would cause 50+ threads. It only caused one open process on my local machine which had an error in it. On line 25:

Code: [Select]

if ($action =~ "show"){


You'll need to do a regular expression here, instead of a string. So, you'll need to change that line to something like:

Code: [Select]

if ($action =~ /show/){


which would match if $action contains "show" in any part of the string, case-sensitive. Additionally, on lines 26-27 and 30-31, you have:

Code: [Select]

&
exit;


which doesn't exist. If you're trying to make the script quit, I suggest you use:

Code: [Select]

exit();


(without the prefix &). &exit would run the undefined function exit() in the main body of your script. Since your script does not define this function, it produces a nasty error. :thumb:
GetAFreelancer! (This service is not affiliated with Lunarpages)

Offline Ripta

  • Master Jedi
  • *****
  • Posts: 1271
Perl script
« Reply #2 on: March 07, 2004, 03:41:26 AM »
I forgot to mention that on line 81:

Code: [Select]

sub table {


gets a warning from Perl saying

Quote from: Perl

Subroutine table redefined


where you are redefining an existing subroutine somewhere. If this isn't what you were aiming for, I suggest you change the name "table" to something funky :D
GetAFreelancer! (This service is not affiliated with Lunarpages)