Files
HotLunches/ajax.SearchDBFields.php
2026-10-19 15:22:52 -07:00

125 lines
3.4 KiB
PHP

<?php
$ValidAccess = 2010;
require_once("include/globals.php");
if (SessionActive() != 1 || ! UserLoggedIn()) {
logExit("Session is not active\n");
exit();
}
include "include/ajax.csrfToken.php";
// Set default timezone
date_default_timezone_set("Canada/Pacific");
$now = date("Y-m-d H:i:s",time());
$return_data = array();
$moduleName = "ajax.SearchDBFields.php";
// startLog($moduleName);
// Get parameters
if (array_key_exists('table_name', $_GET)) $table_name = db_escape_string($_GET['table_name']);
if (array_key_exists('field_names', $_GET)) $field_names = db_escape_string($_GET['field_names']);
if (array_key_exists('term', $_GET)) $term = db_escape_string($_GET['term']);
if (array_key_exists('term', $_GET)) $term = $_GET['term'];
// Ensure parameters are valid
if (!is_numeric($school_num)) logExit("School num is not numeric\n");
if ($school_num == 0) logExit("school_num is zero\n");
if (!isset($table_name)) logExit("ajax.SearchDBFields.php - table_name is not set");
if (!isset($field_names)) logExit("ajax.SearchDBFields.php - field_names is not set");
writeLog("table_name: $table_name\n");
writeLog("field_names: $field_names\n");
writeLog("term: $term\n");
$fields = explode(',', $field_names);
$terms = array();
// Encode \" to something else temporary
$str = $term;
$str_encoded_quotes = strtr($str,array('\\"'=>'####'));
// Split by strings between double-quotes
$str_arr = preg_split('/("[^"]*")/',$str_encoded_quotes,-1,PREG_SPLIT_DELIM_CAPTURE);
foreach ($str_arr as $substr) {
// If value is a dot or a space, do nothing
if (!preg_match('/^[\s\.]$/',$substr)) {
// If value is between double-quotes, it's a string
// Return as is
if (preg_match('/^"(.*)"$/',$substr)) {
$substr = preg_replace('/^"(.*)"$/','\1',$substr); // Remove double-quotes around
$terms[] = strtr($substr,array('####'=>'"')); // Get escaped double-quotes back inside the string
// Else, it must be splitted
} else {
// Split by dot or space
$substr_arr = preg_split('/[\.\s]/',$substr,-1,PREG_SPLIT_NO_EMPTY);
foreach ($substr_arr as $subsubstr)
$terms[] = strtr($subsubstr,array('####'=>'"')); // Get escaped double-quotes back inside string
}
}
// Else, it's an empty substring
}
$where = "";
for ($i = 0; $i < count($fields); $i++) {
$field = $fields[$i];
$wwhere = "";
for ($w = 0; $w < count($terms); $w++) {
$word = db_escape_string($terms[$w]);
if (strlen($wwhere)) $wwhere .= "AND ";
$wwhere .= "$field LIKE '%$word%' ";
}
if (strlen($where)) $where .= "OR ";
if (strlen($wwhere)) $where .= "($wwhere) ";
}
$query = "SELECT * FROM $table_name WHERE school_num = $school_num AND ($where)";
writeLog($query);
$results = db_query($query);
// Format data for return
$return_data = array();
$num_records = db_num_rows($results);
if ($num_records > 0) {
writeLog("records: $num_records\n");
$return_data[] = array();
while ($row = db_fetch_assoc($results)) {
$text = "";
for ($i = 0; $i < count($fields); $i++) {
$field = $fields[$i];
$text .= $row[$field].' ';
}
$text = trim($text);
$record_num = $row['record_num'];
$return_data[] = array(
"value" => $text,
"record_num" => $record_num
);
}
}
writeLog("completed\n");
echo(json_encode($return_data));
?>