// $SCRIPT_NAME and $SCRIPT_FILENAME are misnomers, they're actually
// the script's http path and full server path, respectively
// This sets $SCRIPT_FILE to just the script's name
$SCRIPT_FILE = basename($SCRIPT_NAME);
function debug_print($name, $index="") {
// $index is for arrays and is otherwise optional
global $settings;
if($settings["debug"]) {
if(isset($GLOBALS[$name])) $var = $GLOBALS[$name];
if(is_string($index) && strlen($index) == 0)
unset($index);
if(isset($var) && is_array($var) && !isset($index))
dump_array($name);
else {
if($settings["debug"] == "0" ||
$settings["debug"] == "quiet") echo "\n";
else echo "
\n";
}
}
}
function dump_array($name) {
$array = $GLOBALS[$name];
for(reset($array); current($array); next($array)) {
$key = key($array); // may be 0, so can't use in loop
debug_print($name, $key);
}
}
function load_list($data_path, $source="") {
$result = array();
if(file_exists($data_path) && is_dir($data_path)) {
if(is_string($source) && strlen($source) == 0)
unset($source);
if(isset($source)) {
if(file_exists($source) && is_dir($source) &&
file_exists("$source/list"))
$list = file("$source/list");
elseif(file_exists($source) && is_file($source))
$list = file($source);
}
if(!isset($list)) {
opendir($data_path);
readdir();
readdir();
while($item = readdir()) $list[] = $item;
closedir();
sort($list);
}
for(reset($list); $item = chop(current($list)); next($list)) {
if(isset($source) &&
file_exists($source) && is_dir($source) &&
file_exists("$source/display_items/$item"))
$element["display_item"] = get_first_line("$source/display_items/$item");
$element["item"] = $item;
if(file_exists("$data_path/$item") && file_exists("$data_path/$item/display_item"))
$element["display_item"] = get_first_line("$data_path/$item/display_item");
$result[] = $element;
unset($element);
}
}
return $result;
}
function get_item_index($list, $curr_item, $data_path="", $pos="") {
// $pos can be "prev", "next" or unspecified
// if $pos is unspecified the current item index is returned
if(is_string($data_path) && strlen($data_path) == 0)
unset($data_path);
for(reset($list); $item = current($list); next($list))
if((is_array($item) && $item["item"] == $curr_item) ||
(chop($item) == $curr_item))
break;
if($pos != "prev" && $pos != "next") return key($list);
else while($pos($list)) {
$item = current($list);
if(is_array($item)) {
if(isset($item["item"])) $item = $item["item"];
else unset($item);
} else $item = chop($item);
if(isset($item)) {
if(isset($data_path)) {
if(file_exists("$data_path/$item"))
return key($list);
} else return key($list);
}
}
}
function display($file) {
if(file_exists($file) && is_file($file)) {
$fp = fopen($file, "r");
echo fgets($fp, 32768);
$line = fgets($fp, 32768);
while($line) {
echo "$line";
# if(!eregi("
", $line)) echo "
";
$line = fgets($fp, 32768);
}
fclose($fp);
}
}
function get_first_line($file) {
if(file_exists($file) && is_file($file)) {
$fp = fopen($file, "r");
$line = chop(fgets($fp, 32768));
fclose($fp);
if($line) return $line;
}
}
function load_data($source, $params=FALSE) {
// loads data from $source, as indicated by items named in the
// $params array, or all items if no $params array is passed.
// Returns data in an associative array indexed on those items.
// This function can be replaced by database access, etc.
$return_val = array();
if(!$params) {
$params = array();
opendir($source);
readdir();
readdir();
$item = readdir();
while($item) {
$params[] = $item;
$item = readdir();
}
closedir();
}
for($i = 0; isset($params[$i]); $i++) {
$data = get_first_line("$source/$params[$i]");
if($data) $return_val[$params[$i]] = $data;
}
return $return_val;
}
function module($source) {
global $TEMPLATE_PATH;
echo "\n";
include("$TEMPLATE_PATH/modules/$source");
echo "\n";
}
$SCRIPT_PATH = dirname($SCRIPT_FILENAME); // our full directory path
$SCRIPT_DIR = dirname($SCRIPT_NAME); // our http dir
if(strcmp($SCRIPT_DIR, "/") == 0) $SCRIPT_DIR = "";
if(!$DATA_PATH) $DATA_PATH = $SCRIPT_PATH; // default data path
$SITE = $SCRIPT_FILE;
$SITE_ROOT = $SCRIPT_DIR;
$MEDIA_DIR = "$SITE_ROOT/media/$SITE";
# $MEDIA_DIR = "http://www.baycon.org/media/$SITE";
$MEDIA_PATH = "$SCRIPT_PATH/media/$SITE";
$DATA_PATH = "$SCRIPT_PATH/data/$SITE";
$TEMPLATE_PATH = "$SCRIPT_PATH/templates/$SITE";
$TEMPLATE_DIR = "$SCRIPT_DIR/templates/$SITE";
$NON_TEMPLATE = "non-template/$SITE";
$NON_TEMPLATE_PATH = "$SCRIPT_PATH/$NON_TEMPLATE";
$NON_TEMPLATE_DIR = "$SCRIPT_DIR/$NON_TEMPLATE";
if(eregi("Mozilla/2.0\(compatible; Elaine/", $HTTP_USER_AGENT))
$PALM_WEBCLIPPING = TRUE;
$curr_dir = $SCRIPT_DIR;
$show_path = "Home";
if(!$title) $title = "Scrapbook";
if($title) $title_suffix = " - "; else $title_suffix = "";
$settings = array();
$PASSTHRU_PATH_INFO = "";
if($PATH_INFO) {
// debug_print() doesn't work, yet
# echo "\$PATH_INFO: $PATH_INFO
\n";
$PATH_INFO_ORIG = $PATH_INFO;
// start of URL parsing code
// This strips parameters from $PATH_INFO, ("/xxx=yyy") puts
// them in $prefix, sets them in the $settings array,
// ($settings["xxx"] = "yyy") and sets $single_item to the
// single item to view (if any)
$path_components = explode("/", $PATH_INFO);
$PATH_INFO = "";
$skip_remainder = FALSE;
for($i = 1; isset($path_components[$i]); $i++) {
if(!$path_components[$i]) {
break;
}
$filename = urldecode($path_components[$i]);
if(!$skip_remainder &&
(is_file("$DATA_PATH/pages$PATH_INFO/$filename") ||
is_file("$NON_TEMPLATE_PATH$PATH_INFO/$filename"))) {
$single_item = $filename;
$skip_remainder = TRUE;
} elseif(!$skip_remainder &&
(is_dir("$DATA_PATH/pages$PATH_INFO/$filename") ||
is_dir("$NON_TEMPLATE_PATH$PATH_INFO/$filename"))) {
if(ereg("item=", $path_components[$i]))
$single_item = $filename;
else $PATH_INFO .= "/" . $path_components[$i];
$last_dir = $filename;
} elseif(ereg("=", $path_components[$i])) {
$settings_str .= "/$path_components[$i]";
$index = strtok($path_components[$i], "=");
$settings[$index] = strtok("=");
// debug_print() doesn't work, yet
# echo "\$settings[$index]: $settings[$index]
\n";
} else {
$skip_remainder = TRUE;
$PASSTHRU_PATH_INFO .= "/" . $path_components[$i];
}
}
// end of URL parsing code
// debug_print() works, now
// set error_reporting level if $settings["debug"] is an integer
if($settings["debug"] == "all") $settings["debug"] = 63;
if(isset($settings["debug"]) && intval($settings["debug"]) > 0)
error_reporting(intval($settings["debug"]));
debug_print("PATH_INFO");
debug_print("PASSTHRU_PATH_INFO");
debug_print("last_dir");
debug_print("single_item");
$dir = urldecode(substr($PATH_INFO, 1, strlen($PATH_INFO)));
}
if(!$dir) $dir = ".";
// if $single_item isn't set, but a directory path was found,
// set $single_item to a non-template index file or a data
// directory, if possible
if(!isset($single_item)) {
// look in the non-template dir
if(file_exists("$NON_TEMPLATE_PATH$PATH_INFO") &&
is_dir("$NON_TEMPLATE_PATH$PATH_INFO") &&
!$PASSTHRU_PATH_INFO) {
// look for an index file in reverse order of
// preference ("index" is most preferred)
$files = array("index.htm", "index.html", "index.php", "index");
for(reset($files); $file = current($files); next($files))
if(file_exists("$NON_TEMPLATE_PATH$PATH_INFO/$file"))
$single_item = $file;
}
// if no index file is found in the non-template
// dir, look for the directory in the data dir
if(!isset($single_item) && isset($last_dir) &&
# !$PASSTHRU_PATH_INFO &&
is_dir("$DATA_PATH/pages$PATH_INFO")) {
# $single_item = $last_dir;
$single_item = basename("$DATA_PATH/pages$PATH_INFO");
$PATH_INFO = dirname($PATH_INFO);
if(strlen($PATH_INFO) == 1) $PATH_INFO = "";
}
if(!isset($single_item) &&
file_exists("$DATA_PATH/pages/index")) {
$single_item = "index";
if(strlen($PATH_INFO) == 1) $PATH_INFO = "";
}
// if no index file or data dir was found,
// $single_item remains unset
}
$PAGE = $single_item;
$SITE = $SITE . $settings_str;
if(isset($settings_str)) $PASSTHRU_PATH_INFO .= $settings_str;
if(!$PASSTHRU_PATH_INFO) unset($PASSTHRU_PATH_INFO);
$SCRIPT_BASE = $SCRIPT_NAME . "/$single_item";
# $prefix = $SCRIPT_NAME . $settings_str;
$prefix = $SCRIPT_BASE . $PASSTHRU_PATH_INFO;
debug_print("SITE_ROOT");
debug_print("PATH_INFO");
debug_print("PASSTHRU_PATH_INFO");
# debug_print("SCRIPT_PATH");
# debug_print("DATA_PATH");
# debug_print("NON_TEMPLATE");
debug_print("SCRIPT_BASE");
debug_print("single_item");
debug_print("settings_str");
debug_print("prefix");
if(is_dir("$NON_TEMPLATE_PATH$PATH_INFO") && isset($single_item) &&
file_exists("$NON_TEMPLATE_PATH$PATH_INFO/$single_item")) {
chdir("$NON_TEMPLATE_PATH$PATH_INFO");
$include_file = "$NON_TEMPLATE_PATH$PATH_INFO/$single_item";
$SCRIPT_FILENAME = $include_file;
$SCRIPT_NAME = "$NON_TEMPLATE_DIR$PATH_INFO/$single_item";
$REQUEST_URI = $SCRIPT_NAME . $PASSTHRU_PATH_INFO;
$PATH_TRANSLATED = $DOCUMENT_ROOT . $PASSTHRU_PATH_INFO;
$PHP_SELF = $REQUEST_URI;
$PATH_INFO = $PASSTHRU_PATH_INFO;
$HTTP_SERVER_VARS["SCRIPT_FILENAME"] = $SCRIPT_FILENAME;
$HTTP_SERVER_VARS["SCRIPT_NAME"] = $SCRIPT_NAME;
$HTTP_SERVER_VARS["REQUEST_URI"] = $REQUEST_URI;
$HTTP_SERVER_VARS["PATH_TRANSLATED"] = $PATH_TRANSLATED;
$HTTP_SERVER_VARS["PHP_SELF"] = $PHP_SELF;
$HTTP_SERVER_VARS["PATH_INFO"] = $PATH_INFO;
if(isset($settings["debug"])) {
echo "
";
debug_print("SCRIPT_FILENAME");
debug_print("SCRIPT_NAME");
debug_print("REQUEST_URI");
debug_print("PATH_TRANSLATED");
debug_print("PHP_SELF");
debug_print("PATH_INFO");
if(isset($settings["debug"]))
echo "Going to load $include_file
";
}
include($include_file);
} elseif(is_dir("$DATA_PATH/pages$PATH_INFO") && isset($single_item)) {
if(isset($settings["debug"]))
echo "
Selected data is $single_item
";
// load template file here
$PAGE_DATA_PATH = "$DATA_PATH/pages$PATH_INFO/$PAGE";
$default_settings = load_data("$DATA_PATH/pages/default");
$section_settings = load_data($PAGE_DATA_PATH);
$PAGE_SETTINGS = array_merge($default_settings, $section_settings);
if(isset($PAGE_SETTINGS["theme"]) && file_exists("$DATA_PATH/themes/" . $PAGE_SETTINGS["theme"]) && is_dir("$DATA_PATH/themes/" . $PAGE_SETTINGS["theme"])) {
$theme_settings = load_data("$DATA_PATH/themes/" . $PAGE_SETTINGS["theme"]);
$PAGE_SETTINGS = array_merge($PAGE_SETTINGS, $theme_settings);
}
$PAGE_SETTINGS = array_merge($PAGE_SETTINGS, $settings);
if(!isset($PAGE_SETTINGS["page_template"]))
$PAGE_SETTINGS["page_template"] = "default.php";
if(isset($settings["debug"])) dump_array("PAGE_SETTINGS");
$PATH_INFO = $PASSTHRU_PATH_INFO;
if(file_exists("$TEMPLATE_PATH/" . $PAGE_SETTINGS["page_template"]))
include("$TEMPLATE_PATH/" . $PAGE_SETTINGS["page_template"]);
} elseif(is_dir("$NON_TEMPLATE_PATH$PATH_INFO")) {
if(isset($PASSTHRU_PATH_INFO)) echo "
File not found.
";
else echo "
Directory listing denied.
";
}
?>