Files
2026-10-19 15:22:52 -07:00

662 lines
19 KiB
Plaintext

<?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[] = array($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";
$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('');
}
}
?>