0) {
//do explicit file list ordering
foreach($LEFT_NAV_LIST as $orderedFile) {
// check to see if the file in the LEFT_NAV_LIST
// actually exists in the files array (which is a list
// of files in the web directory
// this step is important so you don't create
// a link in the left nav that does not exist
if(array_key_exists($orderedFile, $files)) {
//array_push($tempFiles, array$files[$orderedFile]);
$tempFiles[$orderedFile] = $files[$orderedFile];
}
}
$files = $tempFiles;
} else {
//if no file list ordering given, use alphabetical
ksort($files);
}
}
/**
* add_files_to_menu
*
* This function builds the left navigation bar displayed
* to the user. If the user added entries to the IGNORED_FILES_LIST
* array, the file is not included in the left navigation menu. If the
* user added the file to the IGNORED_FILES_TEMPLATE array, the
* the link to the file is not to the templated version, but instead is
* just to the file.
*
* @param $this_file the file that is currently executing
* @param $files a reference to an array that contains a list of files in the current directory
*
* @return $left_nav_menu the html for the left navigation menu
*/
function add_files_to_menu($this_file, $regex_ext, &$files)
{
global $IGNORED_FILES_LIST;
global $IGNORED_FILES_TEMPLATE;
global $EXTRA_LINKS;
global $EXTRA_LINKS_BOTTOM;
// does something with the EXTRA_LINKS & EXTRA_LINKS_BOTTOM
// This used to be called from the template_processor.php file, but
set_extra_links($regex_ext, $this_file);
// files is passed by reference to order_files
order_files($files);
$left_nav_menu = '
- Home
';
//loop through files and add menu items
foreach($files as $file => $filename_parts){
// the link name should be based on the filename not the extension
$link_name = capitalize(str_replace('_', ' ', $filename_parts[0]));
//if not an ignored menu item
if(!is_array($IGNORED_FILES_LIST) or (is_array($IGNORED_FILES_LIST) and !in_array($file, $IGNORED_FILES_LIST))) {
//if not ignored by templates
if(!is_array($IGNORED_FILES_TEMPLATE) or (is_array($IGNORED_FILES_TEMPLATE) and !in_array($file, $IGNORED_FILES_TEMPLATE))) {
$file_link = str_replace(' ', '%20', $filename_parts[0]);
$left_nav_menu .= '- '.$link_name.'
';
} else {
$left_nav_menu .= '- '.$link_name.'
';
}
}
}
/*
* if the user would like to add some extra links to the left nav, we need
* to add it to the end of the list. The following preg looks for links. For
* each link found, it adds li tags and adds it to the left nav list. I am
* using the preg because in the past we used
tags to separate one
* navigation element from another, so users would include the br tags in
* the EXTRA_LINKS definition, which are no longer needed.
*/
if (isset($EXTRA_LINKS) and preg_match_all('//iU', $EXTRA_LINKS, $matches)) {
foreach ($matches[0] as $match) {
$left_nav_menu .= '- ' . $match . '
';
}
}
if (isset($EXTRA_LINKS_BOTTOM) and preg_match_all('//iU', $EXTRA_LINKS_BOTTOM, $matches)) {
foreach ($matches[0] as $match) {
$left_nav_menu .= '- ' . $match . '
';
}
}
$left_nav_menu .= '
';
return $left_nav_menu;
}
/**
* get_accessed_page
*
* find the name of the page the user is trying to
* access. Be sure that the user is not trying to
* access a file that is not actually in the directory
* for the index.php file is located.
*
* @param $path path of the current working directory
*
* @return $page the name of the page the user is trying to access
*/
function get_accessed_page($path)
{
global $TEMP_EXT;
if(!isset($TEMP_EXT) or $TEMP_EXT === '' or count($TEMP_EXT) === 0 or $TEMP_EXT[0] === '') {
$TEMP_EXT = array('html', 'htm');
}
// If the user did not pass in a page, default to home.html.
if (isset($_GET['page'])){
$page = $_GET['page'];
$real_ext = '';
if ($page == 'rss' && file_exists($path.'announcements.php')) {
header("Content-Type: application/rss+xml; charset=ISO-8859-1");
echo generate_rss($path);
exit;
}
//Get the extension of the file
if (!is_dir($path . $page)) {
foreach ($TEMP_EXT as $ext) {
if (is_readable($path.$page . '.' . $ext)) {
$real_ext = '.' . $ext;
}
}
}
//if ($path is not in $page then bork)
// it is important that we use realpath at this point.
// realpath resolves all symbolic links - resolves references
// to / ./ and /../ it also removes extra / characters
// if the path is not in the real path to the file, send the user
// to the home page, otherwise we will try to access the file
// the user chose
if (!preg_match('|' . realpath($path) . '|', realpath($path . $page . $real_ext)))
{
$page = 'home';
}
} else {
// the user did not try to access a particular
// file in the site, so send them to the home page
$page = 'home';
}
return $page;
}
/**
* get _current_dir
*
* Gets the directory in which the file is located
* relative to where the index.php file exists
*
* @parameter $page the name of the page the user is trying to access
*
* @return $current_dir the directory the requested file is located
*/
function get_current_dir($page)
{
// find the current directory of the file by reversing the
// string stored in the page variable so we can work backwards
// and find the last two strings seperated by a '/'
$current_dir = explode('/', strrev($page), 2);
// if current_dir is an array of 2, then current_dir[0]
// has the name of the file we are trying to access
// and current_dir[1] has the path to that file
if (count($current_dir) == 2){
$current_dir = strrev($current_dir[1]) . '/';
}
// there was no path included in the requested
// file. We are in the sites root directory
else{
$current_dir = "./";
}
return $current_dir;
}
/**
* capitalize is a simple function that capitalizes
* the first letter of a string
*
* @param $string the string to capatilize
* @return $string the capatilized string
*/
function capitalize($string){
$string = strtolower($string);
$length = strlen($string);
for($i = 0; $i < $length - 1; $i++){
if($string[$i] == ' '){
$string[$i + 1] = strtoupper($string[$i + 1]);
}
}
return ucfirst($string);
}
/**
* DownloadSize determines the size of a file
* and puts in a human readable format
*
* @param $file path to file
*/
function DownloadSize($file) {
$size = filesize($file);
$sizes = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB');
$ext = $sizes[0];
for ($i=1; (($i < count($sizes)) && ($size >= 1024)); $i++) {
$size = $size / 1024;
$ext = $sizes[$i];
}
return round($size, 2) . ' ' . $ext;
}
/**
* get_regex_ext_and_filename determines the name of the
* file the user is trying to access (along with the path to the
* file), and it determines the regexp value for the
* file extension (not sure why we are doing this).
*
* @param $path path to the file the user is trying to access
* @param $page name of the page the user is trying to access
*
* @return array the array contains the filename (including path)
* along with the regexp for the file extension
*/
function get_regex_ext_and_filename($path, $page) {
global $TEMP_EXT;
if(!isset($TEMP_EXT)) {
$TEMP_EXT = array('html', 'htm');
}
$filename = '';
// Find the file that has been called.
$regex_ext = '(';
foreach ($TEMP_EXT as $ext){
$regex_ext .= $ext . '|';
if (is_file($path . $page . '.' . $ext)){
$filename = $path . $page . '.' . $ext;
break;
}
}
//echo "regex_ext = $regex_ext
";
$regex_ext = substr($regex_ext, 0, -1) . ')';
return array($regex_ext, $filename);
//echo "now regex_ext = $regex_ext
";
}
/**
* set_extra_links updates the links entered by the user
* in the EXTRA_LINKS and EXTRA_LINKS_BOTTOM arrays.
* I do not have the time to decifer the regexp used
* so I don't officially know what this code does.
*
* @param $regex_ext the regexp for the filename extension
* @param $this_file the name of the file that is currently executing
*/
function set_extra_links($regex_ext, $this_file) {
global $EXTRA_LINKS;
global $EXTRA_LINKS_BOTTOM;
if(!isset($EXTRA_LINKS)){
$EXTRA_LINKS = '';
}
if(!isset($EXTRA_LINKS_BOTTOM)) {
$EXTRA_LINKS_BOTTOM = '';
}
// once again, comments for regexp are very useful. I am not sure what the following
// is trying to do, much less what it actually does.
$EXTRA_LINKS = preg_replace("/]*[^\/\"'>]*[^\/\"'>]*)\." . $regex_ext . "(#[^\"']*)?([\"'])/Uis", "]*[^\/\"'>]*[^\/\"'>]*)\.".$regex_ext."(#[^\"']*)?([\"'])/Uis","]*[^\/\"'>]*[^\/\"'>]*\/)([\"'])/Uis","]*[^\/\"'>]*[^\/\"'>]*\/)([\"'])/Uis","Directory Listing of ' . $page . '';
$page_data .= '' . "\n";
$page_data .= 'File Name | Size | Modification Date |
' . "\n";
foreach ($files as $num => $filename) {
$file = $path . $page . '/' . $filename;
// if the file is readable and is not a hidden file (starts with a .),
// include it in the list of files in the directory.
if (is_readable($file) and !preg_match('/^\..*/', $filename)) {
$page_data .= '';
if (is_dir($file)) {
$page_data .= '' . $filename . '';
$file_size = 'directory';
} else {
$page_data .= '' . $filename . '';
$file_size = DownloadSize($file);
}
$page_data .= ' | ';
$page_data .= '' . $file_size . ' | ';
$page_data .= '' . date('Y-n-j H:i:s', filectime($file)) . ' | ';
$page_data .= '
' . "\n";
}
}
$page_data .= '
';
return $page_data;
}
/**
* Extracts the contents found in the tags
* of the file to be templated
*
* @param $page_data The contents of the file to be templated
*
* @return returns the contents of the tag if found, or returns empty string
*/
function get_head_of_file($page_data) {
$head_match = array();
if (preg_match('/(.*)<\/head>/Uis', $page_data, $head_match) == 1){
return $head_match[0];
} else {
return '';
}
}
/**
* extracts the contents of the tags of the
* file to be templated
*
* @param $head_data The contents found in the tags of the file
*
* @return returns the contents of the title to be used
* for the page
*/
function get_page_title($head_data) {
global $CLASS_NAME;
global $CLASS_NAME_SHORT;
$title_match = array();
if (preg_match('/(.*)<\/title>/Uis', $head_data, $title_match) == 1){
$title = $CLASS_NAME_SHORT . ': ' . $title_match[1];
} else {
$title = $CLASS_NAME;
}
return $title;
}
/**
* extracts the style information in the