#!/usr/local/bin/perl # Name: db_search.cgi # # Version: 5.02 # # Last Modified: 01-23-97 # # Copyright Information: This application was written by Selena Sol # (selena@eff.org, http://www.eff.org/~erict) and Gunther Birznieks # (birzniek@hlsun.redcross.org) having been inspired by # countless other Perl authors. Feel free to copy, cite, reference, # sample, borrow, resell or plagiarize the contents. However, if you # don't mind, please let me know where it goes so that I can at least # watch and take part in the development of the memes. Information wants # to be free, support public domain freware. Donations are appreciated # and will be spent on further upgrades and other public domain scripts. ####################################################################### # Flush the Perl Buffer. # ####################################################################### # The script begins by telling the Perl interpreter that # it should continuously flush its buffer so that text # from this script is sent directly to the Web Browser. # We do this to streamline debugging and make sure that # the script operates with the flow we want it to. $| = 1; # Also, send out the http header early for easy debugging # and so that the server will not time us out if we take a # while to process. print "Content-type: text/html\n\n"; ####################################################################### # Read and Parse Form Data # ####################################################################### # Next, the ReadParse subroutine in cgi-lib.pl is used to # read the incoming form data. However, the subroutine is # sent "form_data" as a parameter so that the associative # array of form keys/values comes back with a descriptinve # name rather than just %in. &require_supporting_libraries (__FILE__, __LINE__, "./Library/cgi-lib.pl", "./Library/db-lib.pl", "./Library/auth-lib.pl"); &ReadParse(*form_data); ####################################################################### # Load Supporting Files # ####################################################################### # Once it has read the incoming form data, the script # will be able to determine which setup file it should # use to process the incoming form data. # # Perhaps a bit of explanation is in order. # # Whenever you run this application, you must pass to it # the name of the setup file which it will use to process # the search request. # # This variable will provide the name of the file which # this script will use to define all of the customizable # aspects of its operation. For example, the setup file # defines what is contained in the database and which # fields should be displayed to the user. # # The reason for this is that this one script can handle # an infinite amount of databases. # # Each database has a corresponding setup file which defines # how the script performs. The logic (and programming) # remains the same for all db's. All that changes are # the variables and subroutines in the setup files. This # makes it very easy for you to quickly generate diverse # databases with the one backend. # # The script first takes the value of "setup_file" # coming in from the form (which cgi-lib.pl has already # parsed into the %form_data associative array) and # assigns it to the variable $setup_file. # # So how do you get this information to the script? # # There are two ways to do that. Firstly, you can encode # the information into the URL if you are executing this # script directly from a hyperlink. # # For example, you might use the following hyperlink to # direct the script to access address_book.setup: # # http://www.you.com/cgi/db_search.cgi?setup_file=address_book.setup # # You can also send this information as a hidden field in # an HTML form using something like the following. # # # # For example, the following code would define a setup # file called address_book.setup: # # # # You might also create a select box so that the user can # choose from a number of databases dynamically: # # # # The script uses the subroutine require_supporting_libraries # documented later in this script to actually load the # setup file and all of its configuration options. # # Once the setup file has been loaded, the script also # uses the require_supporting_libraries subroutine to # load the mail library which we will use to send email to # the form administrator. $setup_file = $form_data{'setup_file'}; &require_supporting_libraries (__FILE__, __LINE__, "./Setup_files/$setup_file"); #print "$form_data"; ####################################################################### # Perform Authentication # ####################################################################### if ($should_i_authenticate eq "yes") { $confirm_access = "./admin/confirm_access.pl"; # $cgi_utils = "./cgi-utils.pl"; # Access level required to view this page ### G -- guest access $access_code = "U"; # U -- user access # C -- content author # S -- system administrator # require $cgi_utils; # Load up libraries ### require $confirm_access; # # %query = &get_query; # get session key and check it ### $session_key = $form_data{'session_key'}; # #chdir '..'; #change to base dir if necessary(GIMME/ISAP) # # print " -> $form_data{'session_key'} - session key "; $access_ok = &confirm_access($session_key, $access_code); # if (!$access_ok) { exit; } } ####################################################################### # Printout the Database # ####################################################################### if ($form_data{'print_data'} ne "") { ($total_row_count) = &submit_query(*database_rows); &print_db; exit; } ####################################################################### # Printout the Menu # ####################################################################### if ($form_data{'print_menu'} ne "") { ($total_row_count) = &submit_query(*database_rows); &print_menu($form_data{'frame'}); exit; } ####################################################################### # Display Search Screen # ####################################################################### # There are really only two things required of this # script. Firstly, it will have to give the user a form # in which she can submit her search criteria and # secondly, it must process her search request. # # The way the script knows which task it is being asked to # perform depends on a special HTML form submit button. # When the user clicks on the submit button "Submit the # Search Request" the script will know that it is supposed # to search. # # Of course you can have the button say anything you want, # the important thing is that you have the submit button # on the bottom of your frontend form which looks like # this: # # # # The other thing this script does, as we said, is create # the search form on which the above button appears. The # script does this by accessing the output_html_query_form # subroutine in the setup file which basically prints out # an HTML form for you which you can create or customize # as you desire. # # Thus, the following routine asks, "if thre is no value # for the submit button, that means I am not be asked to # do a search...therefore, I must be being asked to # display the search form!" It does so and then quits. # # By the way, you can bypass this form generated script # and use your own so long as you have the correct submit # button on your form. You can also hardcode searches by # including the submit_search parameter as URl encoded # data. Maybe something like the following: # # http://www.you.com/cgi/db_manager.cgi?setup_file=customer_list.setup&submit_search=yes&lname=B # # This would theoretically access a setup file called # customer_list.setup and would search the database set in # that setup file for the lname field for all names # starting with B. if ($form_data{'submit_search'} eq "") { &output_html_query_form; exit; } ####################################################################### # Search the Database # ####################################################################### # As we said, the other thing that you can do with this # script is to actually search the database which is # defined in the setup file. # # The script begins by sending out the beginning of the # HTML response to the client defined in # search_results_header which is located in the setup # file. # # Then we are ready to begin returning search results. # # Most of the work for search is done by db-lib.pl. # The script access the submit_query subroutine passing it # an array database_rows by reference (which means that # the subroutine is going to fill that array with # database rows which were matches to the users search # criteria directly and not pass it back when it is done), # # It will also expect to be returned a total row count of # successful hits. # # The working of the search are covered in depth in # db-lib.pl so go there if you are still confused. else { # $form_data{'db_id'} = $form_data{'item_id'}; # Before we go in and search however, we format any # incoming sort_by information. We'll discuss the sorting # algorithm in just a minute. However, I want to note # here that there are two ways to define a field by which # this script will sort the returned database rows. You # can set a default row in the setup file by setting # $index_of_field_to_be_sorted_by equal to the index of # the field that you want sorted by. Thus, you may just # want to sort automatically by last name and not even # give the user the option to sort by another row. # # On the other hand, you might want to allow the user to # choose which field the returned rows are sorted by. If # this is the case, you need to add another form variable # to your HTML interface. This variable MUST be called # "sort_by" and will usually be in the form of a # select box such as the following: # #