#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Hobart Tas
    Posts
    5
    Rep Power
    0

    Question Cgi Search feature script


    Look, Im no CGI or Perl guru but Im having trouble with this code and I cannot for the life of me work out what is wrong. All i want is the header and footer to show up on the results page. The search feature works fine (although if anyone has any suggestions on how to format the page so it looks more like a google or other search engine results, you are more than welcome). Ive put html code into the space provided and it just simply does not recognise the command. Can anyone help me????? Ive attahed the txt file.
    Any other suggestions would be greatly appreciated.

    #!/usr/bin/perl

    ###############################################################################
    # PROGRAM VARIABLES TO CONFIGURE BELOW
    ###############################################################################

    #This should be the directory/path that you would like to have this program search.
    #Put the exact path starting with the root directory /.
    #Be sure to add a / at the end or else the program will not display the found
    #results from the search properly.
    $searchDirectory = '';

    #This should point to the same directory as above only as it would be typed in a
    #web browser as a url. Point this url to the same directory that is being searched.
    #This will be used to create a link to the pages being displayed by the search results.
    #Be sure to add the trailing / at the end or else you the program will not display the found
    #results from the search. Be sure to add a / at the end.
    $hyperLink = '';

    #This is the color that you would like the hypertext links to be. These are the links
    #that point to the pages returned from the search results.
    $hypertextLinkColor = 'blue';

    #This is where you specify the types of files that you would like to have searched by this
    #program. Seperate your extensions with a pipe symbol which looks like this | .
    $searchFileTypes = '.html|.htm';

    #This is the width of the search text field. Put the width that you want this text field
    #to be displayed as.
    $formFieldSize = '15';

    #This is the text that you would like to have next to the search field box. The
    #default word that shows up is Search.
    $searchFieldText = '';

    #To exclude the html tags from being searched set this variable to 1. If you want
    #this program to search the html tags as well then set this variable to 0. Keep in
    #mind that html tags contain words and this may throw off your search results. Also
    #keep in mind that if you choose to not search the html and you have missing html tags
    #then your search may not be as accurate either. Recommended that you set this to 0, don't
    #remove the html from the search. Most people have missing html tags and this may rip the
    #html incorrectly.
    #1 turns html remove on. 0 turns remove off.
    $removeHtml = '0';

    #Point this to the header file if you have one that you would like to have displayed.
    #Leave this blank if you do not have a header file to be displayed.
    $header = '';

    #Point this to the footer file if you have one that you would like to have displayed.
    #Leave this blank if you do not have a footer file to be displayed.
    $footer = '';












    ###############################################################################
    # PARSE INCOMING DATA AND FIGURE OUT WHAT TO DO WITH IT
    ###############################################################################
    &PARSE;
    print"Content-type:text/html\n\n";
    if($form{search} || $form{submit}){&SEARCH_FILES;}
    else{&DISPLAY_SEARCH_FIELD;}












    ###############################################################################
    # PROGRAM SUBROUTINES BELOW
    ###############################################################################
    sub DISPLAY_SEARCH_FIELD{
    print qq~
    document.write("<form method='post' action='$ENV{SCRIPT_NAME}' name='searchForm'>");
    document.write("<font face='Times New Roman, Times, serif' size='3'><b><font size='2'>$searchFieldText</font></b></font>");
    document.write("<input type='text' name='search' value='$form{search}' size='$formFieldSize' class='input'><input type='submit' name='submit' value='go'>");
    document.write("</form>");
    ~;
    }

    sub SEARCH_FILES{
    #display the header
    open(R, "$header");
    @R = <R>;
    close R;
    print @R;





    opendir(DIR, "$searchDirectory") || ERROR("ERROR: Unable to open the directory:'$searchDirectory' for searching. Please make sure that the path to the directory is correct.");
    @files = readdir(DIR);
    close DIR;

    foreach $line(@files){
    #get the files extension
    $ext=$line;
    $ext =~ s|.*(\..*)|$1|g;

    #save the files with a valid extension
    if($ext =~ /$searchFileTypes/){
    push(@validFiles, "$line");
    }
    }

    #replace all spaces in the search string with pipes, this is the or symbol
    #this will allow for multiple word searching instead of exact string search
    #also add \b for word boundry control. This is used for a higher accuracy. This
    #will stop partial word matching and make sure that it is only matched by whole words.
    @searchWords = split(/\s+/, $form{search});
    foreach $line(@searchWords){
    $searchString .= "\\b$line\\b\|";
    }
    #remove begining and ending pipes since we don't want to match nothing ""
    $searchString =~ s/^\|+//g;
    $searchString =~ s/\|+$//g;

    #number of files searched
    if(@validFiles){$numSearched = $#validFiles;$numSearched++;}#remember arrays start at 0 so increement one

    #search valid files
    foreach $line(@validFiles){
    open(R, "$searchDirectory$line");
    @R = <R>;
    close R;
    $timesFound=0;
    foreach $nestedLine(@R){
    #remove html if requested
    if($removeHtml eq 1){
    $nestedLine =~ s/<(.|\n)*>?//g;
    }
    if($nestedLine =~ /$searchString/i){
    $timesFound++;
    }
    }

    #save all results that returned positive
    if($timesFound != 0){
    push(@fileMatches, "$timesFound\|$line");
    }
    }

    #sort results, set the most positive results to the top of the list, and
    #the least positive resutls to the bottom of the list.
    @fileMatches = sort{lc($a) cmp lc($b)|$a<=>$b}@fileMatches;
    @fileMatches = reverse(@fileMatches);

    #print search box
    print"<script>";
    &DISPLAY_SEARCH_FIELD;
    print"</script>";

    #if there was no results then print this message
    if(!@fileMatches){
    print qq~
    <font size=3 face='Verdana, Arial, Helvetica, SunSans-Regular'>searched $numSearched page(s)<br><b>No matches were found.</b></font><br><br>\n
    ~;
    }else{
    #if there was positive results found then print the results
    print qq~
    <font size=3 face='Verdana, Arial, Helvetica, SunSans-Regular'>searched $numSearched page(s)<br><b>Your search returned the following pages.</b></font><br><br>\n
    ~;
    foreach $line(@fileMatches){
    ($numberResults,$fileName)=split(/\|/, $line);
    print"<font size=2 face='Verdana, Arial, Helvetica, SunSans-Regular'><b>Words matched:</b>$numberResults <b>File:</b><a href='$hyperLink$fileName'><font color='$hypertextLinkColor'>$fileName</font></a></font><br>\n";
    }
    }

    #display the header
    open(R, "$footer");
    @R = <R>;
    close R;
    print @R;

    }

    #This is the error subroutine
    sub ERROR{
    print"@_";
    exit;
    }

    sub PARSE{
    for($i=0;$i<2;$i++){
    ($i eq 0)?($f=<stdin>)$f=$ENV{QUERY_STRING});
    @f=split(/\&/, $f);
    foreach $l(@f){
    ($n,$v)=split(/\=/, $l);
    $v=~s|\+| |g;
    $v=~s|%([a-fA-F0-9][a-fA-F0-9])|pack("C", hex($1))|ge;
    $v =~ s/<!--(.|\n)*-->//g;
    $form{$n}=$v;
    }
    }
    }
    Last edited by butsy_7; January 21st, 2004 at 11:57 PM.
  2. #2
  3. Y? Y!?
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2003
    Location
    Brisbane, Australia
    Posts
    1,573
    Rep Power
    52
    Code:
    #Point this to the header file if you have one that you would like to have displayed.
    #Leave this blank if you do not have a header file to be displayed.
    $header = '';
    
    #Point this to the footer file if you have one that you would like to have displayed.
    #Leave this blank if you do not have a footer file to be displayed.
    $footer = '';
    You need to fill in these variables with filenames (HTML files) for your header & footer. For example, if you had a header html file called 'header.html', then
    Code:
    $header = '<path_to_header_file>/header.html';
    Where <path_to_header_file> is *your* url path to the file.
    Same goes for the footer.

    [EDIT]
    Alternatively, to hard-code it in, replace
    Code:
    #display the header
    open(R, "$header");
    @R = <R>;
    close R;
    print @R;
    with your own print statements containing html code for the header, and same for the footer.
    Last edited by mlh2003; January 22nd, 2004 at 02:16 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Hobart Tas
    Posts
    5
    Rep Power
    0
    mlh2003
    Thanks for the super quick reply. I already know I should be putting the code in that spot (sorry I should have explained it a bit clearer). The problem is no matter what I put in...it doesnt come out on the page. I've tried everything but it just wont recognise the code.

    I'd be more interested in editing the display header command as you've pointed out. What print commands would I require to do it manually?

    Once again thanks for your help!
  6. #4
  7. Y? Y!?
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2003
    Location
    Brisbane, Australia
    Posts
    1,573
    Rep Power
    52
    OK. Maybe it isn't finding the file that you specify (incorrect path?).

    To hard-code it in, you'll need to replace those lines with print html statements like:
    Code:
    #display the header
    print "<h1>Search Results:</h1>\n";
    print "<hr>\n";
    Then for the footer:
    Code:
    #display the footer
    print "<hr>\n";
    print "<center>Copyright &copy; 2004 MyWebSite.</center>\n";
    Just put your HTML in quotes as above in the print statements. Remember the ;'s at the end of each line. The \n's are optional in the above code, but are just there to put each on a separate line (easier to read when you 'view source' from the browser). The copyright symbol is done with '& copy;' in the HTML code - without the space.

    Let us know how you go.
    Last edited by mlh2003; January 22nd, 2004 at 03:09 PM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Hobart Tas
    Posts
    5
    Rep Power
    0
    mlh2003

    Worked an absolute treat, thank you very much. Have you any suggestions as to change the style of the output of the search results. I want it to come out like a proper web search. At the moment it comes out like this.

    searched 33 page(s)
    Your search returned the following pages.

    Words matched:12 Fileeterhill.htm
    Words matched:8 File:news_2000summer2.htm
    Words matched:8 File:news_2000summer1.htm
    Words matched:3 File:staff.htm
    Words matched:2 File:news_2003winter.htm

    You have done more than enough already so don't worry about it if you dont know. Thanks for all your help again. Cheers
  10. #6
  11. Y? Y!?
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2003
    Location
    Brisbane, Australia
    Posts
    1,573
    Rep Power
    52
    I'll need to take a closer look at the program. You will basically need to add hyperlinks to the result output page, which will link to the file of interest.

    Will get back to you on this one...

IMN logo majestic logo threadwatch logo seochat tools logo