Initial commit

This commit is contained in:
Ben Ramey
2015-11-12 19:58:50 -06:00
commit 4eb7f46e4c
86 changed files with 4341 additions and 0 deletions

1
php/.htaccess Normal file
View File

@@ -0,0 +1 @@
Options -indexes

344
php/classes/page.php Executable file
View File

@@ -0,0 +1,344 @@
<?php
# start a session
session_start();
# we set the PHP error handler to use this function
set_error_handler("page_errors");
# only care about errors normally
#error_reporting(E_ERROR | E_USER_ERROR);
error_reporting(E_ALL);
# set the include path so that our templates can always include
# relative to the root template folder
$DOCROOT = str_replace($_SERVER['SCRIPT_NAME'],'',$_SERVER['SCRIPT_FILENAME']);
$templates_path = $DOCROOT.'/site';
$php_path = $DOCROOT.'/php';
set_include_path(get_include_path() . PATH_SEPARATOR . $templates_path . PATH_SEPARATOR . $php_path);
# include configuration options
include_once 'includes/config.php';
# require the user class to handle users
require_once 'classes/user.php';
class Page
{
################################################################################
# PRIVATE DATA
private $title,$pageName, $lang;
private $cssFiles = array(),$jsFiles = array();
private $header, $footer, $content, $menu, $layout;
private $display,$data;
private $absPath,$absURL,$site,$templates,$js,$css,$graphics;
private $messages = array(),$errors = array();
private $user;
################################################################################
# PUBLIC FUNCTIONS
# constructor
public function __construct()
{
# set our paths
$this->absPath = str_replace($_SERVER['SCRIPT_NAME'],'',$_SERVER['SCRIPT_FILENAME']).'/';
$this->absURL = preg_replace('/^(.*)\/\w+\.php$/','$1',$_SERVER['SCRIPT_NAME']).'/';
$this->site = 'site/';
$this->templates = $this->absPath.$this->site;
$this->js = '/'.$this->site . 'javascript/';
$this->css = '/'.$this->site . 'css/';
$this->graphics = '/'.$this->site . 'graphics/';
# the page language is automatically pulled from a language
# cookie names 'lang'
if(isset($_GET['lang']))
{
$this->lang = $_GET['lang'];
setcookie('lang',$_GET['lang'],time() + 60*60*24*365);
# if the lang parameter is empty, we want to get rid of the cookie
if(empty($_GET['lang']))
unset($_COOKIE['lang']);
}
else if(isset($_COOKIE['lang']) && !empty($_COOKIE['lang']))
$this->lang = $_COOKIE['lang'];
else
$this->lang = 'en';
$this->templates .= $this->lang .'/';
$this->graphics .= $this->lang .'/';
# default values for title and pageName
$this->title = isset($TITLE_PREFIX) ? $TITLE_PREFIX : 'Welcome!';
$this->pageName = preg_replace('/^.*\/(\w+)\.php$/','$1',$_SERVER['SCRIPT_NAME']);
# elements of our page
$this->header = $this->templates . 'header.php';
$this->footer = $this->templates . 'footer.php';
$this->content = $this->templates . 'home.php';
$this->menu = $this->templates . 'menu.php';
$this->layout = 'default_layout.php';
# by default, the header, footer and content are all displayed
$this->display = array(
'header' => true,
'footer' => true,
'content' => true,
'menu' => true
);
# load default css and js files
array_push($this->cssFiles,$this->css . 'default.css');
array_push($this->jsFiles,$this->js . 'default.js');
}
# automatically load all css files from folder
public function autoloadCSS()
{
# find all CSS files in css folder
$this->cssFiles = preg_grep('/\.css$/',scandir($this->absPath . $this->css));
# make each path relative to absURL
foreach($this->cssFiles as $key => $value)
$this->cssFiles[$key] = $this->css . $value;
}
# automatically load all javascript files from folder
public function autoloadJS()
{
# find all js files in javascript folder
$this->jsFiles = preg_grep('/\.js$/',scandir($this->absPath . $this->js));
# make each path relative to absURL
foreach($this->jsFiles as $key => $value)
$this->jsFiles[$key] = $this->js . $value;
}
# processes all or part of the page
public function process($part = NULL)
{
# it is possible to process the header or footer by
# itself or just the content, or the entire page
if($part == 'header')
$this->_processHeader();
else if($part == 'footer')
$this->_processFooter();
else if($part == 'content')
$this->_processContent();
else
$this->_processPage();
}
# adds a js file to ones already pulled from standard
# folder
public function addJSFile($new_file)
{
# a list of files can be passed to add several js files at once
if(func_num_args() > 1)
{
$files = func_get_args();
foreach($files as $js_file)
{
array_push($this->jsFiles, $this->js . $js_file);
}
}
# add single file with relative path
else
{
array_push($this->jsFiles, $this->js . $new_file);
}
}
# replaces all other js files with this one
public function useJSFile($new_file)
{
$this->jsFiles = array($this->js . $new_file);
}
# adds a css file to ones already pulled from standard folder
public function addCSSFile($new_file)
{
# a list of files can be passed to add several CSS files at once
if(func_num_args() > 1)
{
$files = func_get_args();
foreach($files as $css_file)
{
array_push($this->cssFiles, $this->css . $css_file);
}
}
# add file with relative path
else
{
array_push($this->cssFiles, $this->css . $new_file);
}
}
# replaces all other css files with this one
public function useCSSFile($new_file)
{
$this->cssFiles = array($this->css . $new_file);
}
# adds an error message to the array
public function addError($err)
{
array_push($this->errors,$err);
}
# adds non-error message to the array
public function addMessage($msg)
{
array_push($this->messages,$msg);
}
################################################################################
# SETTERS/GETTERS
# return the user object
public function init_user()
{
$this->user = new User($this->absPath.$this->site);
}
public function user()
{
if(empty($this->user))
$this->init_user();
return $this->user;
}
# you can pass data to the template via this variable
public function data($key,$value = NULL)
{
if(!is_null($value))
$this->data[$key] = $value;
else
return($this->data[$key]);
}
# you can turn off parts of the page by setting it's
# corresponding display element to false
public function display($page_element,$value = NULL)
{
if(!is_null($value))
$this->display[$page_element] = $value;
else
return($this->display[$page_element]);
}
# setter/getter for header file
public function header($new_header = NULL)
{
if(!is_null($new_header))
$this->header = $this->templates . $new_header;
else
return $this->header;
}
# setter/getter for footer file
public function footer($new_footer = NULL)
{
if(!is_null($new_footer))
$this->footer = $this->templates . $new_footer;
else
return $this->footer;
}
# setter/getter for content file
public function content($new_content = NULL)
{
if(!is_null($new_content))
$this->content = $this->templates . $new_content;
else
return $this->content;
}
# setter/getter for menu file
public function menu($new_menu = NULL)
{
if(!is_null($new_menu))
$this->menu = $this->templates . $new_menu;
else
return $this->menu;
}
# setter/getter for layout file
public function layout($new_layout = NULL)
{
if(!is_null($new_layout))
$this->layout = $new_layout;
else
return $this->layout;
}
# accessor/modifier for page attributes
public function attr($attr,$value = NULL)
{
global $CONFIG;
# the case when we are setting an attribute
if(!is_null($value))
{
switch($attr)
{
case 'lang':
$this->lang = $value;
$this->templates = $this->site;
$this->templates .= $this->lang .'/';
$this->graphics .= $this->lang .'/';
$this->header = $this->templates . 'header.php';
$this->footer = $this->templates . 'footer.php';
$this->content = $this->templates . 'home.php';
break;
case 'title':
$this->title = $CONFIG['title_prefix'].': '.$value;
break;
default:
break;
}
}
# the case when we are getting an attribute
else
{
switch($attr)
{
case 'lang':
return $this->lang;
break;
case 'title':
return $this->title;
break;
default:
break;
}
}
}
################################################################################
# PRIVATE FUNCTIONS
private function _processPage()
{
include $this->absPath . $this->site . 'layout/html.php';
}
private function _processHeader()
{
include $this->header;
}
private function _processFooter()
{
include $this->footer;
}
private function _processContent()
{
include $this->content;
}
}
################################################################## page_errors #
function page_errors($errno, $errstr, $errfile, $errline, $errcontext)
{
global $page;
# don't do anything with the message if error_reporting
# is too low for this type of error
if(!(error_reporting() & $errno))
return true;
switch($errno)
{
case E_USER_ERROR:
$page->addError($errstr);
break;
case E_NOTICE:
case E_WARNING:
case E_USER_WARNING:
case E_USER_NOTICE:
$page->addMessage($errstr.$errno.$errfile.$errline);
break;
default:
$page->addError("$errno: $errstr on $errline in $errfile");
break;
}
return true;
}
?>

95
php/classes/user.php Normal file
View File

@@ -0,0 +1,95 @@
<?php
class User
{
##############################################################################
# PRIVATE DATA
private $password, $username, $loggedin, $role;
private $user_data;
##############################################################################
# PUBLIC FUNCTIONS
public function __construct($site_path)
{
$this->user_data = simplexml_load_file($site_path.'data/users/users.xml');
$this->username = $_SESSION['username'];
$this->loggedin = $_SESSION['_loggedin'];
$this->role = $_SESSION['userrole'];
}
public function __destruct()
{
# save the session data when the object goes out of context
$_SESSION['username'] = $this->username;
$_SESSION['_loggedin'] = $this->loggedin;
$_SESSION['userrole'] = $this->role;
}
# see if a user is logged in
public function user_exists()
{
return $this->loggedin;
}
# check if the user has a role equal to what is passed
public function check_role($role_to_check)
{
$role_to_check = strtolower($role_to_check);
if(($role_to_check == $this->role) && $this->loggedin)
return true;
else
return false;
}
public function login($username,$password)
{
foreach($this->user_data->user as $user)
{
if($username == (string)$user->username)
{
$hash = sha1($password);
#print $hash;
if($hash == (string)$user->password)
{
$this->username = $username;
$this->password = $hash;
$this->loggedin = true;
$this->role = (string)$user->role;
return true;
}
return false;
}
}
return false;
}
# log the user out
public function logout()
{
# delete cookie
setcookie('PHPSESSID','',time()-3600);
# delete the session entirely
session_unset();
# reset object vars
$this->loggedin = false;
$this->username = NULL;
$this->password = NULL;
$this->role = NULL;
return true;
}
##############################################################################
# SETTERS/GETTERS
public function username($new_username = NULL)
{
if(!is_null($new_username))
$this->username = $new_username;
else
return $this->username;
}
public function role()
{
return $this->role;
}
##############################################################################
# PRIVATE FUNCTIONS
}
?>

View File

@@ -0,0 +1,58 @@
<?php
################################################ FUNCTION: file_process_upload #
function file_process_upload($input_name,$save_path,$overwrite = false,$replace_regex = "/[^A-Za-z0-9]+/")
{
$no_errs = true;
$clean_names = array();
if(!is_dir($save_path))
{
trigger_error('Path is not a directory.',E_USER_ERROR);
return false;
}
if(!is_array($_FILES[$input_name]['tmp_name']))
{
foreach($_FILES[$input_name] as $key => $value)
$_FILES[$input_name][$key] = array($_FILES[$input_name][$key]);
}
foreach($_FILES[$input_name]['tmp_name'] as $key => $value)
{
if(!is_uploaded_file($_FILES[$input_name]['tmp_name'][$key]))
{
$no_errs = false;
trigger_error('File "'.$_FILES[$input_name]['name'][$key].'" is not an uploaded file.',E_USER_ERROR);
}
$clean_name = $_FILES[$input_name]['name'][$key];
# find the extension
preg_match("/(\.\S+)$/",$clean_name,$matches);
$ext = $matches[1];
# take the extension off of the file name
$clean_name = str_replace($ext,'',$clean_name);
$clean_name = preg_replace($replace_regex,'_',$clean_name);
# move the uploaded file to the new location, unless a file with the same name
# is already there
if(file_exists($save_path.'/'.$clean_name.$ext) and !$overwrite)
{
$no_errs = false;
trigger_error('File "'.$_FILES[$input_name]['name'][$key].'" already exists.', E_USER_ERROR);
}
if(move_uploaded_file($_FILES[$input_name]['tmp_name'][$key],$save_path.'/'.$clean_name.$ext))
array_push($clean_names,$clean_name.$ext);
else
{
$no_errs = false;
trigger_error('File "'.$_FILES[$input_name]['name'][$key].'" could not be moved to permanent location.', E_USER_ERROR);
}
}
if(count($clean_names) == 1)
return($clean_names[0]);
else
return($clean_names);
}
?>

150
php/functions/func_filesystem.php Executable file
View File

@@ -0,0 +1,150 @@
<?php
#################################################################################
# #
# written by benjamin ramey #
# bsr@rameydesign.net #
# #
# This file contains: #
# Functions that perform various filesystem tasks such as removing #
# unwanted types of files/folders from an array of files and folders #
# in a folder #
# #
#################################################################################
############################################# FUNCTION: filesys_get_folder_tree #
# returns: html of folder tree
# parameters: path to root folder
function filesys_get_folder_tree($path)
{
global $_DOC_ROOT,$_SCRIPT_FOLDER;
# variable that will hold all the HTML for the folder
$folderhtml = '';
# get folder contents in alphabetical order
$folder_contents = scandir($path);
# the url is used so that people can download/look at these files through
# the browser, a doc_root path wouldn't work, of course
$url = str_replace($_DOC_ROOT,"$_SCRIPT_FOLDER",$path);
# one by one, set $element to a file or folder inside the passed folder
foreach($folder_contents as $element)
{
# ignore current and parent folders
if( ($element != '.') and ($element != '..') )
{
# make sure each is_dir call is not using cached information
# from last is_dir check
clearstatcache();
# if $element is a folder, call this function recursively and
# create HTML for this subfolder
if( is_dir("$path/$element") )
{
$folderhtml .= "
<div class='folder_name' id='${element}_name'
onclick='ExpandFolder(this)'
onmouseover='ChangeFolderNameBG(this,0)'
onmouseout='ChangeFolderNameBG(this,1)'>
+ $element
</div>
<div class='sub_folder' id='${element}_folder'>";
$folderhtml .= filesys_get_folder_tree("$path/$element");
$folderhtml .= "
</div>\n\n";
}
else
{
# get the size of this file in kilobytes
$file_size_kb = intval(filesize($path.'/'.$element) / 1024);
$folderhtml .= "
+ <a href='/$url/$element'>$element</a>
($file_size_kb Kb)<br />\n";
}
}
}
return $folderhtml;
}
################################################ FUNCTION: filesys_dirs_present #
# returns: true or false
# parameters: path to directory to inspect, pattern to match directory name
# against
function filesys_dirs_present($path,$pattern = "/.*/")
{
# make sure the path passed here is really a directory we can't
# check anything but a directory, since only directory will contain
# other directories, duh
$handle = is_dir($path) ? opendir($path) : NULL ;
if(empty($handle))
{ return(NULL); }
# set through each child in this directory and see if the child
# is directory but not the parent or current directory
# readdir returns FALSE when no children are left to inspect
while($child = readdir($handle) and $child !== false)
{
# make sure each is_dir call is not using cached information
# from last is_dir check
clearstatcache();
# check if this child is directory and whether it
# matches the user given pattern or not
if(is_dir($path.'/'.$child)
and ($child !== '..')
and ($child !== '.')
and (preg_match($pattern,$child)) )
{
# we can return true with the first matching folder we find
# this is an OR operation
return(true);
}
}
# if we get here, no matching folders were found, so none exist
# so we return false
return(false);
}
######################################### FUNCTION: filesys_get_wanted_children #
# returns: array of wanted dir children
# parameters: 1) path to directory; 2) command to remove [files,folders]
# 3) a pattern to match folder/file names against
function filesys_get_wanted_children($path,$exclude,$pattern = "/.*/")
{
# the cmd can be passed as either all cap or not, so conform it
# here to lower case
$exclude = strtolower($exclude);
# fill an array full of the directory children, but only, of course if
# the passed path is really to a directory
$children = is_dir($path) ? scandir($path) : NULL ;
if(empty($children))
{ return(NULL); }
# remove the current and parent directories
$children = preg_grep("/^\.{1,2}$/",$children,PREG_GREP_INVERT);
# get only the children that match the pattern
$children = preg_grep($pattern,$children);
# if files are to be removed, remove them
if($exclude == 'files')
{
foreach($children as $key => $value)
{
if(!is_dir($path.'/'.$value))
unset($children[$key]);
}
}
# if folders are to be removed, remove them
else if($exclude == 'folders')
{
foreach($children as $key => $value)
{
if(is_dir($path.'/'.$value))
unset($children[$key]);
}
}
# return the array of directory children, free of the bad little ones
return($children);
}
?>

View File

@@ -0,0 +1,63 @@
<?php
################################################### FUNCTION: xml_random_child #
function xml_random_child($xml_file)
{
$xml = xml_get_from_file($xml_file);
$children = $xml->children();
$count = count($children);
return($children[rand(0,$count-1)]);
}
################################################## FUNCTION: xml_get_from_file #
function xml_get_from_file($file_path)
{
if(!file_exists($file_path))
{
trigger_error('XML file "'.$file_path.'" does not exist!');
return NULL;
}
return simplexml_load_file($file_path);
}
################################################ FUNCTION: xml_nl_to_paragraph #
function xml_nl_to_paragraph($string)
{
$string = '<p>'.preg_replace('#([\r\n]\s*?[\r\n]){2,}#','</p>$0<p>',$string).'</p>';
$string = str_replace('<p></p>','',$string);
return $string;
}
################################################### FUNCTION: xml_parse_bbcode #
function xml_parse_bbcode($string)
{
$codes = array('[b]','[/b]','[i]','[/i]','[/link]');
$translation = array('<strong>','</strong>','<em>','</em>','</a>');
// replace bbcode items from above
$string = str_replace($codes,$translation,$string);
// deal with [link]s
$pattern = "#\[link href=([^\]]+)\]#";
$replacement = "<a href='$1' title='$1'>";
$string = preg_replace($pattern,$replacement,$string);
return $string;
}
###################################################### FUNCTION: xml_fix_cdata #
function xml_fix_cdata($string)
{
$find[] = '&lt;![CDATA[';
$replace[] = '<![CDATA[';
$find[] = ']]&gt;';
$replace[] = ']]>';
return $string = str_replace($find, $replace, $string);
}
?>

15
php/includes/config.php Normal file
View File

@@ -0,0 +1,15 @@
<?php
$CONFIG = array(
'title_prefix' => 'Friendship Tours International',
'email' => 'Friendship Tours Intl <travel@friendshiptours.org>',
);
$FILES = array(
'news' => $DOCROOT.'/site/data/news.xml',
'passportinfo' => $DOCROOT.'/site/data/passportinfo.xml',
'ccinfo' => $DOCROOT.'/site/data/creditcards.xml',
'linksandnumbers' => $DOCROOT.'/site/data/linksandnumbers.xml',
);
?>