Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Script to search cgi-bin

    Looking for a perl script which will do word or string search in my cgi-bin,
    which has over a dozen folders plus 3 dozen files not in folders.
    Simple script, simple html, webmaster use only.
    Have one that does not function, worn out attempting to edit.
    thx
    Mike

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,871
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Post your script and specific question on the part that's giving you trouble. Include all warnings and errors that the script generates.

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Since the script I have does not work, I thought that I might find a kind soul that knew of a cgi-bin search script that does work, rather than continue to struggle with the unsatisfactory one.
    BTW, I searched the CF archives, not cgi specific scripts.
    The script I've installed returns a "no results" message, appearing to operate normally, i.e. no error msg's etc.
    Don't know what which part is not written properly...
    Will dig it out tho for inspection,
    thx

  • #4
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,871
    Thanks
    2
    Thanked 164 Times in 159 Posts
    I don't know of a prewritten script that does what you want. You'll probably need to fix the script you currently have, or start over with a new one. I willing help either way, but not write it for you.

  • Users who have thanked FishMonger for this post:

    mwgordon (10-06-2011)

  • #5
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thx for the response -
    I have this set up on my site,
    the url
    http://dcdancenet.com/search/search1cgi.html
    calls/runs (action=)
    cgi-bin/searchfiles/search1cgi.pl

    It does actually run, it will return text results from html files, but will not search .cgi or .pl files.

    Code:
    #!/usr/bin/perl
    # search.pl - creates a list off files to approve for searching
    
    if ($ENV{'REQUEST_METHOD'} eq 'POST') {
       read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
       @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;
          $contents{$name} = $value;
        }
    }
    
    if ($contents{'thing'} eq "searchem"){
    print "Content-type: text/html\n\n";
    $maxfinds = 25;
    $allwordsfound = 0;
    $urlpath = "http://dcdancenet.com/cgi-bin/";
    $toplevel = "/usr/local/etc/httpd/cgi-bin/";
    print <<"HTML";
    <HTML><HEAD><TITLE>Search Results for cgi-bin</TITLE></HEAD>
    <BODY>
    <H3>Search results for "$contents{'keyword'}": </H3><P>
    <b>(matches = number of lines containing keyword)</b><P>
    HTML
    if ($contents{'dirs'} eq "some"){
    foreach $dir (@do_dirs) {
      $dir =~ s/^\s+//;
      $dir =~ s/\s+$//;
      $lastchar = substr($dir, -1, 1);
         if ($lastchar ne "/") {
           $dir = "$dir/";
         }
      @files = ();
      opendir (ADIR, "$dir") || do {&open_not;};
      @files = readdir(ADIR);
      close(ADIR);
     #see if $dirfile has an appropriate extension
      foreach $dirfile (sort @files) {
         $tl = length($toplevel);
         $dl = length($dir);
         $dif = $dl - $tl;
         if ($dif != 0) {
           $add_to_url = substr($dir, -$dif,$dif);
           $fullpath = $urlpath . $add_to_url . $dirfile;
         }
         else {
           $fullpath = $urlpath . $dirfile;
         }
        &check_exts;
       }\n } # foreach dir
     &finish;
     exit;
    } #end if some
    
    sub finish{
    print <<"HTML";
    <P>Total number of matches: $allwordsfound</P>
    </BODY>
    </HTML>
    HTML
    } #end sub finish
    
    if ($contents{'dirs'} eq "all"){
    opendir (TOPLEV, $toplevel);
      @files_dirs = readdir(TOPLEV);
      closedir (TOPLEV);
      $dircount = 0;
      foreach $dirfile (sort @files_dirs){
       # test for dot in file extensions and print files in toplevel 1st
       if ($dirfile=~ m/\./){
         if ($dirfile ne '.' && $dirfile ne '..'){
           $fullpath = $urlpath . $dirfile;
              &check_exts;
         }
       }
       else{
         #assign dirs to another array
         $justdirs[$dircount] = $toplevel."$dirfile/";
         $dircount++;
       }
      }
       $dirfile=""; #empty it
      foreach $jud (@justdirs){
        opendir (DIR, $jud);
        @files = readdir(DIR);
        closedir (DIR);
        $filecount = 0;
        foreach $file (sort @files){
          if ($file=~ m/\./){ #get just the files this time
             $justfiles[$filecount] = $file;
             $filecount++;
          } # test for dot in file extensions and do nothing if exists
          else{
          #add one more to @justdirs to continue through all directories
            $justdirs[$dircount] = $jud."$file/";
            $dircount++;
          }
        }
        #now print files in the directory $jud
        foreach $dirfile (sort @justfiles){
          if ($dirfile ne '.' && $dirfile ne '..'){
            $tl = length($toplevel);
            $jl = length($jud);
            $dif = $jl - $tl;
            $addto_url = substr($jud, -$dif,$dif);
            $fullpath = "";  #re-initialize
            $fullpath = $urlpath . $addto_url . $dirfile;
              &check_exts;
          }
        }
        #empty @justfiles and @files
        $#justfiles = -1;
        $#files = -1;
      }
     &finish;
     exit;
    }
    sub check_exts{
    $extensions[0] = "htm";
    $extensions[1] = "html";
    $extensions[2] = "shtml";
    #$extensions[3] = "htaccess";
     foreach $extension (@extensions) {
        $extension =~ s/^\s+//;
        $extension =~ s/\s+$//;
        while ($dirfile=~ /\./g){
          $dotpos = pos($dirfile);
          $dirfile_len = length($dirfile);
          $after_dot = $dirfile_len - $dotpos;
        }
        if (substr($dirfile, -2,2) eq $extension && $after_dot==2){
          &searchfile($dirfile);
        }
        elsif (substr($dirfile, -3,3) eq $extension && $after_dot==3) {
          &searchfile($dirfile);
        }
        elsif (substr($dirfile, -4,4) eq $extension && $after_dot==4) {
          &searchfile($dirfile);
        }
        elsif (substr($dirfile, -5,5) eq $extension && $after_dot==5) {
          &searchfile($dirfile);
        }
        elsif (substr($dirfile, -6,6) eq $extension && $after_dot==6) {
          &searchfile($dirfile);
        }
     }
    }
    
    sub searchfile{
      my ($onefile) = @_;
      $kw = $contents{'keyword'};
      $kw =~ s/^\s+//;
      $kw =~ s/\s+$//;
      $onefile =~ s/^\s+//;
      $onefile =~ s/\s+$//;
     if ($contents{'dirs'} eq "some"){
       open (SEARCHFILE, "$dir$onefile") || print "$onefile could not be opened.<br>";
     }
     else{
       if ($jud eq ""){
          open (SEARCHFILE, "$toplevel$onefile") || print "$onefile could not be opened.<br>";
       }
       else{
          open (SEARCHFILE, "$jud$onefile") || print "$onefile could not be opened.<br>";
       }
     }
        $count = 0;
         while ($line2 = <SEARCHFILE>) {
          chop($line2);
          $line2 =~ s/<(([^ >]|\n)*)>//g;
          if ($contents{'srchtype'} eq "exact") {
            @sepwords = split (/ /, $line2);
            $wordplace = 0;
            while ($wordplace <= @sepwords) {
              foreach $drp (@drop){
                if (substr($sepwords[$wordplace],-1,1) eq $drp){
                  chop($sepwords[$wordplace]);
                }
              }
              if ($sepwords[$wordplace] eq $kw) {
                $count++;
              } # if sepwords
              $wordplace++;
            } # while wordplace
           } # if exact
           elsif ($contents{'srchtype'} eq "within") {
             if ($line2 =~ /($kw)/gi) {
               $count++; 
    #		print "string<BR>";
    #		print "$1";
    		print "$1",'<BR>';
             }
           }
          if ($maxfinds > 0){ # zero means maxfinds option not used
             if ($count == $maxfinds){ last;}
          }
        } # while line2
        close(SEARCHFILE);
        if ($count > 0) {
    print <<"HTML";
    <A HREF="$fullpath">$onefile</a> - $count matches<BR>
    HTML
        print '( ',"$fullpath",' )','<BR>';
        } #if count
        $allwordsfound += $count;
    } #end searchfile
    } #end of if thing = searchem

  • #6
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hello Fishmonger!
    I'm wondering whether to keep the coding forum site open --
    Should I go ahead and close the window and look for an eMail when/if you find it convenient to fiddled with my inquiry?
    thx, Mike

  • #7
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,871
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Sorry for the delay in responding, I've been tied up on a major project at work which won't be completed for another week or so.

    Your script has numerous problems, most of which I don't have time to go into now.

    To start with, ALL Perl scripts should begin by loading the strict and warnings pragmas. Those 2 pragmas will point out lots of coding mistakes which could be difficult to track down without their help.

    You should not manually parse the form submission. Instead, use the CGI module and let it do the parsing to make sure it's done correctly.

    Since you're wanting to traverse a directory tree, you should use the File::Find or File::Find::Rule module to find and process the files.

    Here is a base skeleton of the script.
    Code:
    #!/usr/bin/perl
    # search.pl - creates a list off files to approve for searching
    
    use strict;
    use warnings;
    use CGI;
    use File::Find;
    
    my $cgi = CGI->new;
    
    if ( $cgi->param('keyword') ) {
    
        my %form        = $cgi->Vars;
        my $max_finds   = 25;
        my $words_found = 0;
        my $url_path    = 'http://dcdancenet.com/cgi-bin/';
        my $top_level   = '/usr/local/etc/httpd/cgi-bin/';
        
        print $cgi->header,
              $cgi-start_html('Search Results for cgi-bin'),
              $cgi->h3("Search results for '$form{keyword}':"),
              $cgi->p,
              $cgi->b('(matches = number of lines containing keyword)'),
              $cgi->p;
        
        if ( $form{dirs} eq 'all' ) {
            find(\&all_dirs, $top_level);
        }
        else {
            my @dirs = qw( list of dirs );
            find(\&some_dirs, @dirs); 
        }
    }
    else {
        error_page();
    }
    
    
    #### subroutine definitions ####
    
    sub all_dirs {
        
    }
    
    sub some_dirs {
        $File::Find::prune = 1;
    }
    
    sub error_page {
        
    }

  • Users who have thanked FishMonger for this post:

    mwgordon (10-06-2011)

  • #8
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi FM!
    Dident mean to complain, appreciate your kind attention!
    Thx for the base skeleton of the script.
    I've waited to reply as UR busy.
    Don't have desire to keep/use the script I found somewhere,
    and will be grateful for your help on this new one as it looks like I'm out of my (shallow) depth here.
    I will flail away at the html page and flounder around the script, not sure about the sub dir defs.....
    thx again for your effort and hope you may find a convenient moment to flesh out this skeleton
    Mike

  • #9
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,871
    Thanks
    2
    Thanked 164 Times in 159 Posts
    I'll try to help as much as I can, but I'm still tied up working on the IT portion of the PGA Tournament that our company is hosting.

  • #10
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi,
    just wondering whether you'd have additional comments?

  • #11
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,871
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Some of your requirements are not real clear and I'll not inclined to write the entire script for you, but I will post an updated version later today or tomorrow.

    Have you tried to fill in any of the blanks?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •