First Commit

This commit is contained in:
mark committed 2026-10-19 15:22:52 -07:00
1 parent 5eb9dd037d
commit 2e62c0e60b
11877 files changed
+2035312

No files matched your search

+142
View File
@@ -0,0 +1,142 @@
<?php
//
// Global functions of useful code
//
//
// year_calc
// - added 2007-02-08 (MWF)
//
// Calculates a person's age or anniversary number given
// a start date and an optional anniversary date. If no
// anniversary date is given it will use today's date.
//
// If called with a person's birth date it will return their
// current age.
//
if (!function_exists('year_calc')) {
function year_calc($start_date,$anniversary_date="") {
list($year,$month,$day) = explode("-",$start_date);
if ($anniversary_date) list($year1,$month1,$day1) = explode("-",$anniversary_date);
else list($year1,$month1,$day1) = explode("-",date("Y-m-d",time()));
$year_diff = $year1 - $year;
$month_diff = $month1 - $month;
$day_diff = $day1 - $day;
if ($month_diff < 0) $year_diff--;
elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
return $year_diff;
}
}
//
// date_difference
// - added 2007-05-09
//
// Same as above but this function returns an array of values
// -> years
// -> months
// -> days
// -> total_months
// -> total_days
//
//// if (!function_exists('date_difference')) {
//// function date_difference($start_date,$anniversary_date="") {
//// list($year,$month,$day) = explode("-",$start_date);
////
//// if ($anniversary_date) list($year1,$month1,$day1) = explode("-",$anniversary_date);
//// else list($year1,$month1,$day1) = explode("-",date("Y-m-d",time()));
////
//// $year_diff = $year1 - $year;
//// $month_diff = $month1 - $month;
//// $day_diff = $day1 - $day;
////
//// if ($month_diff < 0) {
//// $year_diff--;
//// $month_diff = 12 + $month_diff;
//// }
//// elseif (($month_diff==0) && ($day_diff < 0)) {
//// $year_diff--;
//// }
////
//// return $year_diff;
//// }
////}
//
// html
// - added 2007-05-09
//
// Convert a string of text so that all applicable characters are HTML entities
//
if (!function_exists('html')) {
function html($text) {
return htmlentities(stripslashes($text));
}
}
//
// parseEmailText
// - added 2007-05-09
//
// Perform value replacement on all variables in a block of text. The set of
// values to replace is passed as an array and the array indices are the variables..
//
if (!function_exists('parseEmailText')) {
function parseEmailText($message, $vals) {
$msg = $message;
foreach ($vals as $name => $value) {
$msg = eregi_replace("\[{$name}\]", $value, $msg);
$msg = eregi_replace("\{{$name}\}", $value, $msg);
}
return nl2br(trim($msg));
}
}
//
// timeToEnglish
// - added 2007-05-09
//
// Convert a time value (seconds since midnight) to an English value of hours and minutes
//
if (!function_exists('timeToEnglish')) {
function timeToEnglish($time, $hr24=0, $ampm=0) {
$hr = floor($time / 60);
if (!$hr24) {
$hr = $hr % 12;
if ($hr == 0) $hr = 12;
}
else {
if ($hr < 10) $hr = "0" . $hr;
}
$min = $time % 60;
if ($min < 10) $min = "0" . $min;
$t = "{$hr}:{$min}";
if ($ampm) {
if ($time < 12*60) {
$t .= " am";
}
else {
$t .= " pm";
}
}
return $t;
}
}
?>
+25
View File
@@ -0,0 +1,25 @@
<?php
$post_fields = array_keys($_POST);
// Print each form field as a hidden field
for ($i = 0; $i < count($post_fields); $i++) {
$field_name = $post_fields[$i];
$display_field = 1;
// If there is a set of fields to skip, check if this is one of them
if (is_array($skip_fields)) {
for ($j = 0; $j < count($skip_fields); $j++) {
if ($field_name == $skip_fields[$j]) $display_field = 0;
}
}
if ($display_field) {
$field_value = stripslashes($$field_name);
$field_value = str_replace('"',"&quot;",$field_value);
$field_value = str_replace("'","&#39;",$field_value);
echo($indent_spaces . "<input type=\"hidden\" name=\"$field_name\" value=\"$field_value\">\n");
}
}
?>
+662
View File
@@ -0,0 +1,662 @@
<?php
if (!isset($OPTIONLIST_INCLUDED) || ! $OPTIONLIST_INCLUDED) {
$OPTIONLIST_INCLUDED = 1;
//
// Get a list of matching files from a directory and display them
//
function BuildDirList($directory,$matches="",$current="",$excludes="",$include_path=0) {
// Get the actual directory listing
$result = GetDirList($directory,$matches,$current,$excludes,0);
// If necessary add the path into the optionlist
if ($include_path) {
$result = str_replace("value=\"","value=\"$directory/",$result);
}
// Display the result string
printf("$result");
} // End of BuildDirList() function
//
// Get a list of matching files from a directory and display the file titles
//
function BuildDirTitleList($directory,$matches="",$current="",$excludes="") {
// Get the actual directory listing
$result = GetDirList($directory,$matches,$current,$excludes,1);
// Display the result string
printf("$result");
} // End of BuildDirTitleList() function
//
// Get list of files from a directory and return them
//
function GetDirList($directory,$matches="",$current="",$excludes="",
$ftitle=0) {
$result = "";
if (is_dir($directory)) {
$dir = opendir($directory);
$count = 0;
while ($file = readdir($dir)) {
if ($file == ".") continue;
if ($file == "..") continue;
// If a set of pattern matches is passed validate the patterns
if ($matches) {
// Check for valid patterns in file name
for ($i = 0; $i < count($matches); $i++) {
if (stristr($matches[$i], $file)) {
// Validate exclusion patterns
if ($excludes) {
// Check for excluded patterns
for ($j = 0; $j < count($excludes); $j++) {
if (stristr($excludes[$j], $file)) {
break 2;
}
}
$file_names[] = $file;
$count++;
break;
}
else {
$file_names[] = $file;
$count++;
break;
}
}
}
}
else {
$file_names[] = $file;
$count++;
}
}
// If no files display a message string
if (!$count) {
$result .= "<option value=\"\">Error: No Files Found</option>\n";
return $result;
}
if ($ftitle) {
rsort($file_names);
reset($file_names);
}
else {
sort($file_names);
}
while (list($key, $file) = each($file_names)) {
$selected = "";
$title = $file;
// If desired, check the title tag of the files
if ($ftitle) {
$text = "";
// Check if the first line is a title tag
$fp = fopen("$directory/$file", "r");
if (!$fp) {
echo("File Open Error: $directory/$file");
return;
}
while (! feof($fp)) {
$record = fgets($fp, 1000);
$text .= $record;
if (stristr("</title",$record)) {
break;
}
}
fclose($fp);
$text = str_ireplace("^.*<title>","",$text);
$text = str_ireplace("</title>.*$","",$text);
$text = str_ireplace("amp;","",$text);
$title = trim($text);
}
// Build option list
$file = htmlentities($file);
$title = htmlentities($title);
if ($file == $current) $selected = " selected=\"selected\"";
$result .= "<option value=\"$file\"$selected>$title</option>\n";
}
}
else {
$result .= "<option value=\"\">Error: Directory Not Found: $directory" .
"</option>\n";
}
// Return the result string
return $result;
} // End of GetDirList() function
//
// Function to get a file list and display the results
//
function BuildFileList($file, $current="", $separator="", $date_format = 'N') {
// Get the actual file list
$result = GetFileList($file, $current, $separator, $date_format);
// Display the result string
printf("$result");
} // End of BuildFileList() function
//
// Function to get the file list and return the option statements
//
function GetFileList($file, $current="", $separator="", $date_format = 'N') {
// Define the character to split on
$splitChar = ":";
if (isset($GLOBALS['splitChar']) && strlen($GLOBALS['splitChar']) > 0) $splitChar = $GLOBALS['splitChar'];
if (isset($separator) && strlen($separator) > 0) $splitChar = $separator;
$result = "";
if (is_file($file)) {
$source = fopen($file, "r");
if (!$source) {
echo("File Open Error: $file");
return;
}
while (! feof($source)) {
// Read a record
$record = trim(fgets($source, 1000));
$fields = explode("$splitChar", $record, 2);
// Check how many fields are in record
if (count($fields) == 2) {
$code = $fields[0];
$value = $fields[1];
}
else {
$code = $fields[0];
$value = $fields[0];
}
if (strlen($value) > 0) {
// Handle language lookups
$lvalue = "";
if (strpos($value,"TEXT-") !== FALSE) $lvalue = getLanguageText($value);
if (strlen($lvalue) == 0) {
$lcode = "CODE-" . strtoupper(str_replace(' ','-',$value));
$lvalue = getLanguageText($lcode);
}
if (strlen($lvalue) == 0) {
$lcode = "TEXT-" . strtoupper(str_replace(' ','-',$value));
$lvalue = getLanguageText($lcode);
}
if (strlen($lvalue)) $value = $lvalue;
// Add additional info if required
if ($date_format != "") {
if ($date_format == 'Y') $value = date($code,time());
if ($date_format == 'A') $value .= " - " . date($code,time());
}
$selected = "";
if ($code == $current) $selected = " selected=\"selected\"";
$result .= "<option value=\"$code\"$selected>$value</option>\n";
}
}
}
else {
$result .= "<option value=\"\">Error: File Not Found: $file" .
"</option>\n";
}
// Return the result string
return $result;
} // End of GetFileList() function
//
// Function to get the file list and return the options in an array
//
function GetFileListArray($file, $current="", $separator="", $date_format = 'N') {
// Define the character to split on
$splitChar = ":";
if (isset($GLOBALS['splitChar']) && strlen($GLOBALS['splitChar']) > 0) $splitChar = $GLOBALS['splitChar'];
if (isset($separator) && strlen($separator) > 0) $splitChar = $separator;
$result = array();
if (is_file($file)) {
$source = fopen($file, "r");
if (!$source) {
echo("File Open Error: $file");
return;
}
while (! feof($source)) {
// Read a record
$record = trim(fgets($source, 1000));
$fields = explode("$splitChar", $record, 2);
// Check how many fields are in record
if (count($fields) == 2) {
$code = $fields[0];
$value = $fields[1];
}
else {
$code = $fields[0];
$value = $fields[0];
}
if (strlen($value) > 0) {
// Handle language lookups
$lvalue = "";
if (strpos($value,"TEXT-") !== FALSE) $lvalue = getLanguageText($value);
if (strlen($lvalue) == 0) {
$lcode = "CODE-" . strtoupper(str_replace(' ','-',$value));
$lvalue = getLanguageText($lcode);
}
if (strlen($lvalue) == 0) {
$lcode = "TEXT-" . strtoupper(str_replace(' ','-',$value));
$lvalue = getLanguageText($lcode);
}
if (strlen($lvalue)) $value = $lvalue;
// Add additional info if required
if ($date_format != "") {
if ($date_format == 'Y') $value = date($code,time());
if ($date_format == 'A') $value .= " - " . date($code,time());
}
$result[$code] = $value;
}
}
}
else {
$result[] = "Error: File Not Found: $file";
}
// Return the result string
return $result;
} // End of GetFileList() function
//
// Build a simple checkbox display field and display it
//
function BuildCheckBox($name, $value, $current="") {
printf("<input type=\"checkbox\" name=\"$name\" value=\"$value\"");
if ($current == $value) {
printf(" checked");
}
printf(">\n");
} // End of BuildCheckBox() function
//
// Build a checkbox field based on a text file of options & display it
//
function BuildFileCheckbox($file, $name, $current="", $separator="", $spanid="") {
$result = "";
if (is_file($file)) {
$source = fopen($file, "r");
if (!$source) {
echo("File Open Error: $file");
return;
}
while (! feof($source)) {
// Read a record
$record = trim(fgets($source, 1000));
$fields = explode(":", $record);
// Check how many fields are in record
if (count($fields) == 2) {
$code = $fields[0];
$value = $fields[1];
}
else {
$code = $fields[0];
$value = $fields[0];
}
if ($code) {
// Handle language lookups
$lvalue = "";
if (strpos($value,"TEXT-") !== FALSE) $lvalue = getLanguageText($value);
if (strlen($lvalue) == 0) {
$lcode = "CODE-" . strtoupper(str_replace(' ','-',$value));
$lvalue = getLanguageText($lcode);
}
if (strlen($lvalue) == 0) {
$lcode = "TEXT-" . strtoupper(str_replace(' ','-',$value));
$lvalue = getLanguageText($lcode);
}
if (strlen($lvalue)) $value = $lvalue;
$sspan = "";
$espan = "";
if (strlen($spanid)) {
$sspan = "<span class='$spanid'>";
$espan = "</span>";
}
$checked = "";
if (!is_array($current) && $code == $current) $checked = " checked=\"checked\"";
if (is_array($current) && in_array($code,$current)) $checked = " checked=\"checked\"";
$result .= "$sspan<input type=\"checkbox\" name=\"$name" . "[]\" value=\"$code\" class=\"$name\"" .
"$checked>$value$espan $separator ";
}
}
}
else {
$result .= "<b>Error: File Not Found: $file</b>";
}
// Display the result string
printf("$result");;
} // End of BuildFileCheckbox() function
//
// Build a radio selection field based on a text file of options & display it
//
function BuildFileRadio($file, $name, $current="", $separator="", $class="") {
$result = GetFileRadio($file, $name, $current, $separator, $class);
// Display the result string
printf("$result");;
} // End of BuildFileRadio() function
function GetFileRadio($file, $name, $current="", $separator="", $class="") {
global $currLanguage;
$result = "";
if (is_file($file)) {
$source = fopen($file, "r");
if (!$source) {
echo("File Open Error: $file");
return;
}
while (! feof($source)) {
// Read a record
$record = trim(fgets($source, 1000));
$fields = explode(":", $record);
// Check how many fields are in record
if (count($fields) == 2) {
$code = $fields[0];
$value = $fields[1];
}
else {
$code = $fields[0];
$value = $fields[0];
}
if (strlen($value) > 0) {
// Handle language lookups
$lvalue = "";
if (strpos($value,"TEXT-") !== FALSE) $lvalue = getLanguageText($value);
if (strlen($lvalue) == 0) {
$lcode = "CODE-" . strtoupper(str_replace(' ','-',$value));
$lvalue = getLanguageText($lcode);
}
if (strlen($lvalue) == 0) {
$lcode = "TEXT-" . strtoupper(str_replace(' ','-',$value));
$lvalue = getLanguageText($lcode);
}
if (strlen($lvalue)) $value = $lvalue;
$id = $name . "_" . str_replace(' ','_',$code);
$checked = "";
if ($code == $current) $checked = " checked='checked'";
$rclass = "";
if (strlen($class)) $rclass = " class='$class'";
$result .= "<span class='no-break'><input type='radio' name='$name' id='$id'$rclass value='$code' $checked>$value </span> $separator";
// $result .= "<input type='radio' name='$name' id='$id'$rclass value='$code' $checked>$value </span> ";
}
}
}
else {
$result .= "<b>Error: File Not Found: $file</b>";
}
return($result);
} // End of GetFileRadio() function
//
// Build a selection list from a code table in the database and display it
//
function BuildDBList($table,$code_field,$display_field,$sort_field,
$current="",$sort="ASC",$where="",$errmsg=""){
// Get the actual list from the database
$result = GetDBList($table,$code_field,$display_field,$sort_field,
$current,$sort,$where,$errmsg);
// Print the result string
printf("%s",$result);
} // End of BuildDBList() function
//
// Build a selection list from a code table in the database and return it
//
function GetDBList($table,$code_field,$display_field,$sort_field,
$current="",$sort="ASC",$where="",$errmsg=""){
// Define the separator for the display fields
$separator = " - ";
if (array_key_exists('field_separator',$GLOBALS)) $separator = $GLOBALS["field_separator"];
// Split display_fields as there may be more than one field to display
$display_fields = explode(",", $display_field);
$query = "select distinct $code_field, $display_field, $sort_field " .
"from $table " .
"where $code_field is not null ";
// Process the display fields to ensure they are not null
for ($j = 0; $j < count($display_fields); $j++) {
$d_field = $display_fields[$j];
$query .= "and $d_field is not null ";
}
// Add special where clauses if there is one
if (strlen($where) > 0) {
if (stripos($where, "and ") !== 0 && stripos($where, " and ") !== 0) $where = "and $where";
}
$query .= $where . " order by $sort_field $sort";
writeLog($query);
$results = array();
$results = db_query($query); // or error_log("/usr/lib/php/optionlist.php - cannot connect to db from {$_SERVER['HTTP_HOST']} {$_SERVER['PHP_SELF']}");
if (!$results) {
$now = date("Y-m-d H:i:s",time());
$fp = fopen("/usr/lib/php/optionlist.log","a");
fputs($fp,"$now - GetDBList: $GLOBALS[SCRIPT_FILENAME]\n");
$result = "ERROR: Unable to access MySQL Database";
return $result;
}
$num_rows = db_num_rows($results);
$result = "";
if (!$num_rows) {
if ($errmsg == "N") {
$result = "<option value=\"\">No Records Found</option>";
}
else {
$result .= "<option value=\"\">Error: No Records Found: $table" . "</option>\n";
}
}
else {
$current = str_replace("&quot;","\"",$current);
while ($row = db_fetch_assoc($results)) {
$code = $row[$code_field];
$value = "";
for ($j = 0; $j < count($display_fields); $j++) {
$d_field = $display_fields[$j];
$c_value = $row[$d_field];
if (strlen($value)) $value .= $separator;
$value .= "$c_value";
}
$selected = "";
if ($code == $current) $selected = " selected=\"selected\"";
$result .= "<option value=\"" . str_replace("\"","&quot;",$code) .
"\"$selected>" . $value . "</option>\n";
}
}
// Return the result string
return $result;
} // End of GetDBList() function
//
// Build and display a simple selection menu from a code table
// - Built for justladiesfitness.com
//
function BuildDBMenu($table,$code_field,$display_field,$sort_field,
$base="",$where="") {
// Get the actual list from the database
$result = GetDBMenu($table,$code_field,$display_field,$sort_field,
$base,$where);
// Print the result string
printf("$result");
}
//
// Function to get the DB Menu and return a string
//
function GetDBMenu($table,$code_field,$display_field,$sort_field,
$base="",$where="") {
$query = "select $code_field, $display_field, $sort_field " .
"from $table " .
"where $code_field is not null " .
"and $display_field is not null $where " .
"order by $sort_field";
$results = array();
$results = db_query($query);
if (!$results) {
$now = date("Y-m-d H:i:s",time());
$fp = fopen("/usr/lib/php/optionlist.log","a");
fputs($fp,"$now - GetDBMenu: $GLOBALS[SCRIPT_FILENAME]\n");
$result = "ERROR: Unable to access MySQL Database";
return $result;
}
$num_rows = db_num_rows($results);
$result = "";
if (!$num_rows) {
$result .= "Error: No Records Found: $table";
}
else {
while ($row = db_fetch_assoc($results)) {
$code = $row[$code_field];
$value = $row[$display_field];
if ($i) {
$result .= "| ";
}
// Convert spaces to '+'
$code = str_replace(" ","+",$code);
$result .= "<a href=\"$base" . $code . "\">" . $value . "</a> ";
}
}
// Return the result string
return $result;
} // End of BuildDBMenu() function
}
if (!function_exists('getLanguageText')) {
function getLanguageText($code) {
return('');
}
}
?>
+80
View File
@@ -0,0 +1,80 @@
<?php
date_default_timezone_set("America/Vancouver");
//
// If already set just return
//
if (isset($phpdir) && is_dir($phpdir)) {
if (!isset($webdir)) {
if (isset($_SERVER) && isset($_SERVER['DOCUMENT_ROOT'])) {
$webdir = $_SERVER['DOCUMENT_ROOT'];
}
else {
$webdir = str_replace("/admin/php","",$phpdir);
}
}
return;
}
//
// Check if script being called from web browser
//
$current_domain_name = '';
if (isset($_SERVER) && array_key_exists('DOCUMENT_ROOT', $_SERVER)) {
// User web browser _SERVER variable to set webdir
$webdir = $_SERVER['DOCUMENT_ROOT'];
if (array_key_exists('HTTP_HOST',$_SERVER)) {
$current_domain_name = str_replace("www.","",strtolower($_SERVER['HTTP_HOST']));
}
}
if (!isset($webdir)) {
// Base webdir on current working directory
$cwd = getcwd();
$cwd = str_replace("/disk1/","/",$cwd);
unset($webdir);
if (preg_match("#^/m/data[0-9]/#",$cwd)) {
$webdir = preg_replace("#(^/m/data[0-9]/[^/]*).*#","\1",$cwd);
}
if (!isset($webdir)) {
return;
}
}
if (!isset($current_domain_name)) {
$current_domain_name = preg_replace("#^/m/data[0-9]/#","",$webdir);
}
//
// Define phpdir based on webdir
//
$phpdir = "$webdir/admin/php";
// Check for a different phpdir location
if (!is_dir($phpdir)) {
$phpdir = "$webdir/php";
}
// Check for a different phpdir location
if (!is_dir($phpdir)) {
$phpdir = "$webdir/include";
}
// Check for a different phpdir location
if (!is_dir($phpdir)) {
$phpdir = "$webdir/includes";
}
// Give up and set phpdir = webdir
if (!is_dir($phpdir)) {
$phpdir = $webdir;
}
?>
+581
View File
@@ -0,0 +1,581 @@
<?php
if (!isset($TABLE_INCLUDED) || $TABLE_INCLUDED != 1) {
$TABLE_INCLUDED = 1;
// Define the Table class
// - This assumes we have already connected to the database
class Table
{
public $table_name;
public $column_names = array();
public $column_types = array();
public $column_nulls = array();
public $column_defaults = array();
public $column_values = array();
public $column_values_array = array();
public $column_default_values = array();
public $key_columns = array();
public $num_columns;
public $columns = array();
public $school_num_flag = 'N';
public $userdata;
public $err;
public $err_no;
//
// Initialize the table data object with column and key information
//
public function __construct($table_name) {
// Setup main parameters
$this->table_name = $table_name;
// Get table information
$query = "show columns from $table_name";
$result = db_query($query);
if (! $result) {
$this->err = db_error();
$this->err_no = 103;
return;
}
// Get column information
$this->num_columns = db_num_rows($result);
$data = array();
while ($row = db_fetch_array($result)) {
$data[] = $row;
}
$this->columns = $data;
for ($i = 0; $i < count($data); $i++) {
$row = $data[$i];
$name = $row['Field'];
if ($row['Field'] == 'school_num') $this->school_num_flag = 'Y';
$type = preg_replace("#\(.*\)#", "", $row['Type']);
$type = str_replace(" unsigned", "", $type);
$values = "";
$values_array = array();
if ($type == 'enum') {
$values = preg_replace("#\)#", "", preg_replace("#.*\(#", "", $row['Type']));
if (strlen($values)) {
$values_array = explode(',', str_replace("'","",$values));
}
}
$null = $row['Null'];
$key = $row['Key'];
$default = $row['Default'];
$this->column_names[$i] = $name;
// echo("$i $name ".$this->column_names[$i]."\n");
$this->column_types[$i] = $type;
$this->column_nulls[$i] = $null;
$this->column_defaults[$i] = $default;
$this->column_values[$i] = $values;
$this->column_values_array[$i] = $values_array;
$this->key_columns[$i] = ($key == "PRI");
$this->column_types[$name] = $type;
$this->column_nulls[$name] = $null;
$this->column_defaults[$name] = $default;
$this->key_columns[$name] = ($key == "PRI");
switch ($name) {
case 'school_num':
if (array_key_exists('school_num',$GLOBALS)) $this->column_default_values[$name] = $GLOBALS['school_num'];
else $this->column_default_values[$name] = 0;
break;
default:
$this->column_default_values[$name] = $default;
break;
}
}
$this->err_no = 0;
} // End of Table() function
//
// Display the table structure in report format
//
function ShowTable() {
printf(" Table: $this->table_name\n\n");
printf(" %-30s %-15s %-15s% -15s\n",
"Column", "Type", "Null", "Pri");
for ($i = 0; $i < count($this->column_names); $i++) {
$value = $this->column_values[$i];
if (strlen($this->column_defaults[$i])) $value .= " (".$this->column_defaults[$i].")";
printf("%4d %-30s %-15s %-15s %-15s %s\n",
$i + 1,
$this->column_names[$i],
$this->column_types[$i],
$this->column_nulls[$i],
$this->key_columns[$i],
$value);
}
return;
} // End of ShowTable() function
//
// Display table structure in SQL format
//
function ShowStructure() {
print("create table $this->table_name (\n");
$primary = "";
$unique = "";
$index = "";
for ($i = 0; $i < count($this->columns); $i++) {
$row = $this->columns[$i];
$name = $row[0];
$type = $row[1];
$null = ($row[2] == "YES" ? "" : " not null");
switch ($row[3]) {
case "PRI":
if (strlen($primary)) $primary .= ", ";
$primary .= $name;
break;
case "UNI":
$unique .= " unique ($name),\n";
break;
case "MUL":
$index .= " index ($name),\n";
break;
default:
if ($row[3]) printf("%s - ", $row[3]);
}
printf(" %-30s%s%s,\n", $name, $type, $null);
}
printf("\n");
if (strlen($unique)) {
printf("%s\n", $unique);
}
if (strlen($index)) {
printf("%s\n", $index);
}
if (strlen($primary)) {
printf(" primary key (%s)\n", $primary);
}
print(");\n\n");
return;
} // End of ShowStructure() function
//
// Build an SQL query statement based on the current values
// of GLOBALS variables
//
// - This assumes that all values that are set in GLOBALS are to be used.
// It may be a problem if lingering data is in the GLOBALS array
//
function BuildQuery($orderby_fields="",$special_where="",$prefix="") {
// Initialize variables
$query = "";
$where = "$special_where";
$orderby = "";
// Process each field in the table
for ($i = 0; $i < count($this->column_names); $i++) {
$name = $this->column_names[$i];
$data = $GLOBALS[$name];
if (! empty($data) && $data != "NULL") {
// If there is a column already in the where clause add 'and'
if ($where) {
$where .= " and ";
}
// LIKE WILDCARD
if (preg_match("/[\*\?%]/", $data)) {
$data = preg_replace("/[\*]/", "%", $data);
$where .= "$prefix$name like " . $this->FieldData($name, $data);
}
// NOT EQUAL TO
elseif (preg_match("/^!= /",$data)) {
$data = preg_replace("/^!= /","",$data);
$where .= "$prefix$name != " . $this->FieldData($name, $data);
}
// LESS THAN OR EQUAL TO
elseif (preg_match("/^<= /",$data)) {
$data = preg_replace("/^<= /","",$data);
$where .= "$prefix$name <= " . $this->FieldData($name, $data);
}
// LESS THAN
elseif (preg_match("/^< /",$data)) {
$data = preg_replace("/^< /","",$data);
$where .= "$prefix$name < " . $this->FieldData($name, $data);
}
// GREATER THAN OR EQUAL TO
elseif (preg_match("/^>= /",$data)) {
$data = preg_replace("/^>= /","",$data);
$where .= "$prefix$name >= " . $this->FieldData($name, $data);
}
// GREATER THAN
elseif (preg_match("/^> /",$data)) {
$data = preg_replace("/^> /","",$data);
$where .= "$prefix$name > " . $this->FieldData($name, $data);
}
// EQUAL TO
else {
$where .= "$prefix$name = " . $this->FieldData($name, $data);
}
}
// Build order by part based on field sequence
if ($name != "record_num" && $this->key_columns[$i]) {
if ($ord) {
$orderby .= ",";
}
$orderby .= "$prefix$name";
$ord = 1;
}
}
// Build up the statement
$query = "SELECT * from $this->table_name";
// If where clause then append it
if ($where) {
$query .= " where $where";
}
// If there is an orderby clause append it
if ($orderby_fields) {
$query .= " order by " . $orderby_fields;
}
else {
if ($ord) {
$query .= " order by $orderby";
}
}
return $query;
} // End of BuildQuery() function
//
// Build the set of hidden fields for this table
// - This function has probably been replaced with the script:
// "hidden-form-fields.php"
//
function BuildHidden() {
// Initialize variables
$hidden = "";
// Process each field in the table
for ($i = 0; $i < count($this->column_names); $i++) {
$name = $this->column_names[$i];
$data = db_escape_string($GLOBALS[$name]);
$hidden .= "<INPUT TYPE=\"HIDDEN\" NAME=\"$name\" VALUE=\"$data\">\n";
}
return $hidden;
} // End of BuildHidden() function
//
// Build an SQL insert statement based on the values in the GLOBALS array
//
function BuildInsert($data_array=0,$include_record_num='No') {
// Set processing array
if (!is_array($data_array) || count($data_array) == 0) $data_array = $GLOBALS;
// Initialize variables
$query = "";
$q1 = "INSERT into $this->table_name (";
$q2 = "values (";
// Process each field in the table
for ($i = 0; $i < count($this->column_names); $i++) {
$name = $this->column_names[$i];
$ftype = $this->column_types[$i];
$data = '';
if (array_key_exists($name, $data_array)) $data = $data_array[$name];
else {
if ($ftype == 'enum') $data = $this->column_defaults[$i];
}
if ($name != "record_num" || $include_record_num == 'Yes') {
// If there is a column already in the string add a comma
if ($i) {
$q1 .= ", ";
$q2 .= ", ";
}
$q1 .= $name;
$q2 .= $this->FieldData($name, $data);
}
}
$query = $q1 . ") " . $q2 . ")";
return $query;
} // End of BuildInsert() function
//
// Build an SQL update statement based on the values in the GLOBALS array
//
function BuildUpdate($data_array=0,$mode="single") {
// Initialize variables
$query = "UPDATE $this->table_name set ";
$update = "";
// Process each field in the table
for ($i = 0; $i < count($this->column_names); $i++) {
$name = $this->column_names[$i];
if (($name != "record_num")) {
unset($data);
// See if a data value is available for use in query
if (is_array($data_array) && count($data_array) > 0) {
if (isset($data_array[$name])) $data = $data_array[$name];
}
else {
if (isset($GLOBALS[$name])) $data = $GLOBALS[$name];
}
if (isset($data)) {
// If there is a column already in the string add a comma
$value = $this->FieldData($name, $data);
// Single record update statement
if ($mode == "single") {
if ($update) $update .= ", ";
$update .= $name . " = " . $value;
}
// Multi record update statement
if ($mode == "multi") {
if ($value == "''") $value = NULL;
if ($value == "NULL") $value = NULL;
if ($value == "0") $value = NULL;
if ($value) {
if ($update) $update .= ", ";
$update .= $name . " = " . $value;
}
}
}
}
}
// Define the where clause
$where_clause = "";
if ($mode == "single") {
$record_num = 0;
if (is_array($data_array) && count($data_array) > 0) {
if (array_key_exists('record_num',$data_array)) $record_num = intval($data_array["record_num"]);
}
else {
if (array_key_exists('record_num',$GLOBALS)) $record_num = intval($GLOBALS["record_num"]);
}
$where_clause = "where record_num = $record_num";
}
$query = "$query $update $where_clause";
return $query;
} // End of BuildUpdate() function
//
// Format the data correctly based on the field type in
// the table structure
//
function FieldData($field_name, $data = "") {
$value = "";
$type = $this->column_types[$field_name];
$null = $this->column_nulls[$field_name];
$data = trim($data);
// format data based on field type
switch ($type) {
case "varchar":
case "text":
case "longtext":
case "char":
case "enum":
$data = htmlspecialchars($data,ENT_QUOTES);
$data = str_replace("&amp;quot;",'"',$data);
$data = str_replace("&amp;","&",$data);
$data = str_replace("&amp;","&",$data);
$data = str_replace("&quot;",'"',$data);
$data = str_replace("&#039;","'",$data);
$data = str_replace("\\","",$data);
$data = str_replace("\\","",$data);
$data = db_escape_string($data);
if (strlen($data) == 0) {
if ($null == "NO" || $type == "enum") {
$data = $this->column_defaults[$field_name];
}
}
$value = "'$data'";
break;
case "int":
case "bigint":
case "smallint":
case "decimal":
if (strlen($data) == 0) {
if ($null == "YES") {
$value = "NULL";
}
else {
$value = "0";
}
}
else {
if ($type == 'decimal') $value = floatval($data);
else $value = intval($data);
}
break;
case "date":
if (empty($data) || strlen($data) == 0) {
if ($null == "YES") {
$value = "NULL";
}
else {
$dt = date("Y-m-d", time());
$value = "'$dt'";
}
}
else {
$data = db_escape_string($data);
$value = "'$data'";
}
break;
case "time":
if (empty($data) || strlen($data) == 0) {
if ($null == "YES") {
$value = "NULL";
}
else {
$dt = date("H:i:s", time());
$value = "'$dt'";
}
}
else {
$data = db_escape_string($data);
$value = "'$data'";
}
break;
case "datetime":
if (! $data) {
$dt = date("Y-m-d H:i:s", time());
$value = "'$dt'";
}
else {
$data = db_escape_string($data);
$value = "'$data'";
}
break;
case "timestamp":
if (! $data) {
$dt = date("YmdHis", time());
$value = "'$dt'";
}
else {
$data = db_escape_string($data);
$value = "'$data'";
}
break;
default:
$data = db_escape_string($data);
$value = "'$data'";
}
// return the correctly formatted data
return $value;
} // End of FieldData() function
//
// Function to convert a data record into GLOBALS data
//
function RecordData($record) {
// Process each field in the table
for ($i = 0; $i < count($this->column_names); $i++) {
$name = $this->column_names[$i];
$GLOBALS[$name] = $record[$name];
}
} // End of RecordData() function
} // End of class definition
} // End of if TABLE_INCLUDED statement
?>