HRDIODB
[ class tree: HRDIODB ] [ index: HRDIODB ] [ all elements ]

Source for file search.php

Documentation is available at search.php

  1. <?php
  2. /**
  3.  * Search.
  4.  * This file contains the logic that composes and routes search requests.
  5.  * It makes use of search_forms.php for display elements and search_class.php for search execution.
  6.  * @package HRDIODB
  7.  */
  8.  
  9. /**
  10.  * This file is part of HRDIODB.
  11.  *
  12.  * HRDIODB is free software; you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation; either version 2 of the License, or
  15.  * (at your option) any later version.
  16.  *
  17.  * HRDIODB is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with HRDIODB; if not, write to the Free Software
  24.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25.  */
  26.  
  27. /** Configuration file */
  28. require_once("conf.php");
  29. /** Debugging functions */
  30. require_once("debugging.php");
  31. /** Search class */
  32. require_once("search_class.php");
  33. /** Search forms */
  34. require_once("search_forms.php");
  35. /** PHP session class */
  36. require_once("session_class.php")
  37. /** HRDI database class */
  38. require_once("hrdi_db_class.php");
  39. /** User class */
  40. require_once("user_class.php");
  41.  
  42. /**
  43.  * Composes an indexed array of associative arrays for free form searching from the $_GET
  44.  * formatted input.  See search_class::free_form for the required conditions format.
  45.  * @param mixed $got The sanitized get variable
  46.  * @param int $num_forms The number of arrays to be built
  47.  */
  48. function compose_conditions ($got$num_forms{
  49.   $conditions array();
  50.  
  51.   for($i =1$i <=$num_forms$i++){
  52.     if(isset($got["value$i"]&& $got["value$i"!= "" && $got["field$i"!= "" && $got["operator$i"!= ""){
  53.     $conditions[array("Table"=>$got["table"],
  54.                           "Field"=>$got["field$i"],
  55.                           "Operator"=>$got["operator$i"],
  56.                           "Value"=>$got["value$i"]);
  57.                           }
  58.  
  59.   }
  60.  
  61.   return $conditions;
  62. }
  63.  
  64. /**
  65.  * Check string for sql injection attacks.  Every value that originates in GET and is put into a sql statement
  66.  * must be passed through this function for security.
  67.  * @param string $string A string to be sanitized.
  68.  */
  69. function clean_sql($string){
  70.   $clean str_replace(";"," ",addslashes(stripslashes($string)));
  71.   return $clean;
  72. }
  73.  
  74. /**
  75.  * Validates $_GET values.
  76.  * This function validates and cleans the values passed in the $_GET array.
  77.  * It ensures that malicious actions cannot be performed.
  78.  * @return array Clean values
  79.  */
  80. function clean_get({
  81.   $clean=array();
  82.   
  83.   // General search action
  84.   foreach($_GET as $k=>$v){
  85.     if(is_array($v)){
  86.       foreach($v as $key=>$val){
  87.         $clean[$k][$key]=clean_sql($val);
  88.       }
  89.     
  90.     else 
  91.     {
  92.       $clean[$k]=clean_sql($v);
  93.     }
  94.   
  95.  
  96.   // Determine which page of a multi-page result set is shown
  97.   if(isset($_GET['page']&& !is_numeric($_GET['page'])){
  98.     error("Bad page value");
  99.     exit();
  100.   }
  101.  
  102.   // Default to page 1 if a page is not set in the get variable, else set $page to the get value.
  103.   if(isset($_GET['page'])){
  104.     $clean['page']=$_GET['page'];
  105.   else {
  106.     $clean['page'1;
  107.   }
  108.   
  109.   // Determine how many free form search input forms are/were displayed
  110.   if(isset($_GET['num_forms']&& !is_numeric($_GET['num_forms'])){
  111.     error("Bad num_forms value");
  112.     exit();
  113.   }
  114.  
  115.   if(isset($_GET['num_forms']&& ($_GET['num_forms'1)){
  116.     $clean['num_forms'$_GET['num_forms'];
  117.     if($clean['num_forms'100){
  118.       error("Please limit yourself to 100 search forms");
  119.       $clean['num_forms'100;
  120.     }
  121.   else {
  122.     $clean['num_forms'1;
  123.   }
  124.  
  125.   // Free form search
  126.   if(isset($_GET['free_form']|| isset($_GET['free_form'])){
  127.     $clean['free_form'"yes";
  128.   }
  129.  
  130.   return $clean;
  131. }
  132. // Prepare the conditions from the get value
  133. $got clean_get();
  134.  
  135. // Determine which cookie the search conditions may be stored/read from
  136. $cookie_name 'hrdiodb_search';
  137. if(isset($got['free_form'])){
  138.     $cookie_name 'hrdiodb_free_form_search';
  139. }
  140.  
  141. // If a search was performed, store it in a cookie
  142. if(isset($got['submitted']&& $got['submitted']!="no"){
  143.     $got_copy  $got;
  144.     unset($got_copy['submitted']);
  145.     setcookie($cookie_name,serialize($got_copy)strtotime("+30 days"));
  146. }
  147.  
  148. // Make sure the search doesn't time out
  149.  
  150. // Create class instances
  151. $session new session();
  152. $hdb new hrdi_db($dsn);
  153. $user new user($hdb$session);
  154.  
  155.  
  156. // Page title
  157. if (isset($got['free_form'])) {
  158.   $page_title "Free Form Search";
  159. else {
  160.   $page_title "Pre-defined Search";
  161. }
  162.  
  163. // Include the page header
  164. require_once("header.php");
  165.  
  166. $cookie_loaded false;
  167.  
  168. // Create the search instance and perform the search
  169. if(isset($got['submitted']&& $got['submitted']!="no"){
  170.   $search new search($hdb);
  171.   $status TRUE;
  172.  
  173.   // Process free form search entries
  174.     if(isset($got['free_form'])){
  175.     $conditions compose_conditions($got$got['num_forms']);
  176.     if(!$conditions){
  177.       $status FALSE;
  178.       inline_error("No search conditions entered");
  179.     else {
  180.       $status $search->free_form($conditions);
  181.       if(!$status and !is_array($status)){
  182.         error("Free form search failed");
  183.       }
  184.     }
  185.   
  186.   else {
  187.   // Process a predefined search entry
  188.         if($got['submitted']=="pid_day_coverage"){
  189.       // The got variable is not passed itself because searches may be (in other interfaces) called in other manners.
  190.             $status =  $search->pid_day_coverage($got['pid']@$got['date_type'],$got['date_value_low'],$got['date_value_high']$got['lat_day_min']$got['lat_day_max']$got['lat_night_min']$got['lat_night_max']);
  191.     }
  192.     if($got['submitted']=="pid"){
  193.       $conditions[0]["Table"]="HRDI_Modes";
  194.       $conditions[0]["Field"]="Process_ID";
  195.       $conditions[0]["Operator"]="=";
  196.       $conditions[0]["Value"]=@$got['pid'];
  197.       $status =  $search->free_form($conditions);
  198.     }
  199.   }
  200.  
  201.   // Apply file type filter to search results
  202.     if(isset($status&& isset($got['filetypes'])){
  203.     $search->filetype_filter($got['filetypes']);
  204.   }
  205.     
  206.   // Store search results for download logic to access 
  207.     if (isset($got['filetypes'])) {
  208.     $session->set_var("types"$got['filetypes'])
  209.     $session->set_var("days"$search->results)
  210.   else {
  211.     $session->unset_var("types");
  212.     $session->unset_var("days");
  213.   }
  214.  
  215.   // Re-prepare the conditions from the get value to account for changes made by search class methods
  216.     $got clean_get();
  217. }
  218. elseif(isset($_COOKIE[$cookie_name]&& (!isset($got['submitted']|| $got['submitted'!='no')){
  219.     $got unserialize($_COOKIE[$cookie_name]);
  220.     $cookie_loaded true;
  221. }
  222.  
  223.  
  224. if (isset($got['free_form'])) {
  225.   if(isset($search&& $status){
  226.     free_search_forms($hdb$got$search)
  227.   }
  228.   else{
  229.     free_search_forms($hdb$got)
  230.   }
  231. else {
  232.   if(isset($search&& $status){
  233.     predefined_search_forms($hdb$got$search)
  234.   }
  235.   else{
  236.     predefined_search_forms($hdb$got)
  237.   }
  238. }
  239.  
  240. if(isset($got['submitted']&& $got['submitted']!="no" && !$cookie_loaded){
  241.   // Display the search results
  242.     if($status){
  243.     display_search_results($search$hdb$got['page']30);
  244.   }
  245. }
  246.  
  247. // Include the page footer
  248. require_once("footer.php");
  249. ?>

Documentation generated on Tue, 23 Jan 2007 22:57:51 -0500 by phpDocumentor 1.3.0RC6