diff --git a/bin/test_cli.php b/bin/test_cli.php new file mode 100644 index 0000000..84e70da --- /dev/null +++ b/bin/test_cli.php @@ -0,0 +1,11 @@ + + + Options +MultiViews + php_value auto_prepend_file /var/www/ember/system/include/common.inc.php php_value register_globals off php_value magic_quotes_gpc off diff --git a/system/class/Debug.class.php b/system/class/Debug.class.php new file mode 100644 index 0000000..033919e --- /dev/null +++ b/system/class/Debug.class.php @@ -0,0 +1,248 @@ + TRUE, + 'mode' => TRUE, + 'options' => TRUE, + ); + + const OBJECT_VERSION = 1; + + //Log states + const LOG_DEFAULT = 'LOG_DEFAULT'; + const LOG_SESSION = 'LOG_SESSION'; + const LOG_SITE = 'LOG_SITE'; + const LOG_DEBUG = 'LOG_DEBUG'; + const LOG_DB = 'LOG_DB'; + const LOG_CACHE = 'LOG_CACHE'; + const LOG_USER = 'LOG_USER'; + const LOG_PERMISSION = 'LOG_PERMISSION'; + const LOG_SMARTY = 'LOG_SMARTY'; + + const LOG_LOADED_FILES = 'LOG_LOADED_FILES'; + const LOG_GLOBALS = 'LOG_GLOBALS'; + const LOG_PAGELOAD_TIME = 'LOG_PAGE_LOAD_TIME'; + const LOG_CODEBASE = 'LOG_CODEBASE'; + + static $avalable_options = array( + self::LOG_DEFAULT, + self::LOG_SESSION, + self::LOG_SITE, + self::LOG_DEBUG, + self::LOG_LOADED_FILES, + self::LOG_GLOBALS, + self::LOG_PAGELOAD_TIME, + self::LOG_CODEBASE, + self::LOG_DB, + self::LOG_CACHE, + self::LOG_USER, + self::LOG_PERMISSION, + self::LOG_SMARTY, + ); + + //Display modes + const MODE_CLI = 'MODE_CLI'; + const MODE_SIMPLE = 'MODE_SIMPLE'; + const MODE_STANDARD = 'MODE_STANDARD'; + const MODE_ADVANCED = 'MODE_ADVANCED'; + + /** + * Sets up the Debug object regenerating it from the session + * or building it from scratch + * @return boolean True of the init came from the session, false if it is defaults + */ + public static function init() + { + $data = Session::getObject(get_class()); + + $new = TRUE; + + //Attempt to recover Object + if(is_array($data)) + { + $required = self::$required_vars; + + foreach($data as $key => $value) + { + if(isset($required[$key])) + unset($required[$key]); + + self::$$key = $value; + } + + //Detect if Object recovered + $new = array_search(TRUE, $required) === FALSE ? FALSE : TRUE; + + if(self::$enabled) + foreach($required as $k => $r) + { + if($r) + Debug::log('Debug: var not recovered: '.$k, self::LOG_SESSION); + } + } + + if($new) + { + self::$enabled = FALSE; + switch(SESSION_TYPE) + { + case 'web': + self::$mode = self::MODE_SIMPLE; + break; + case 'cli': + self::$mode = self::MODE_CLI; + break; + } + self::$options = array(); + + Session::register(get_class()); + } + + self::$data = array(); + + return !$new; + + } + + /** + * Serializes the object to be stored in the session + * @return array data to be stored in the session + */ + public static function serialize() + { + $result = array(); + foreach(self::$required_vars as $var => $required) + $result[$var] = self::$$var; + + return $result; + } + + public static function isEnabled() + { + return self::$enabled; + } + + private static function validateOption($option) + { + if(in_array($option, self::$avalable_options) === FALSE) + return FALSE; + else + return TRUE; + } + + public static function getAvalableOptions() + { + return self::$avalable_options; + } + + public static function setOption($option, $enable = TRUE) + { + if(!self::$enabled) return; + if(!self::validateOption($option)) + throw new Exception ('Unknown option '.$option); + + if(is_bool($enable)) + self::$options[$option] = $enable; + else + throw new Exception ('Invalid enable value, must be boolean'); + } + + public static function isOptionEnabled($option) + { + if(!self::$enabled) return FALSE; + + if(isset(self::$options[$option])) return self::$options[$option]; + else return FALSE; + } + + public static function enable() + { + self::$enabled = TRUE; + } + + public static function disable() + { + self::$enabled = FALSE; + } + + public static function print_r($var, $label = 'Display', $mode = NULL) + { + if(!self::$enabled) return; + + if(!isset($mode)) + $mode = self::$mode; + elseif(!self::validateMode($mode)) + throw new exception('Unknown mode '.$mode); + + + switch($mode) + { + case self::MODE_CLI: + echo PHP_EOL.'###################### Start '.$label.' ########################'.PHP_EOL; + print_r($var); + echo PHP_EOL.'######################## End '.$label.' ########################'.PHP_EOL; + break; + case self::MODE_SIMPLE: + case self::MODE_STANDARD: + case self::MODE_ADVANCED: + echo PHP_EOL.'
'.$label.''.PHP_EOL;
+				print_r($var);
+				echo PHP_EOL.'
'.PHP_EOL; + break; + } + } + + public static function log($value, $log_option = self::LOG_DEFAULT, $label = NULL) + { + if(!self::$enabled) return; + if(!is_array(self::$data)) self::$data = array(); + if(!isset(self::$data[$log_option]) || !is_array(self::$data[$log_option])) self::$data[$log_option] = array(); + + if(isset($label)) + self::$data[$log_option][$label] = $value; + else + self::$data[$log_option][] = $value; + } + + + public static function getCallingFunction() + { + return 'Obj:method'; + } + + public static function printDebuggingInfo($overide = FALSE) + { + if(!self::$enabled || (self::$debug_printed && !$overide)) + return; + + if(self::isOptionEnabled(self::LOG_PAGELOAD_TIME)) + Debug::print_r('Page Load Time: ' . getPageLoadTime(), 'Page Load Time'); + + if(self::isOptionEnabled(self::LOG_GLOBALS)) + Debug::print_r($GLOBALS, 'Globals'); + + + Debug::print_r(self::$data, 'RAW Debug'); + + if(self::isOptionEnabled(self::LOG_CODEBASE)) + Debug::print_r(array('DOCUMENT_ROOT: '.DOCUMENT_ROOT, + 'CODE_BASE: '.CODE_BASE, + 'CODE_BASE_ROOT:'.CODE_BASE_ROOT, + )); + + if(self::isOptionEnabled(self::LOG_LOADED_FILES)) + Debug::print_r(get_included_files(), 'Included Files'); + + self::$debug_printed = TRUE; + } +} \ No newline at end of file diff --git a/system/class/Session.class.php b/system/class/Session.class.php new file mode 100644 index 0000000..fd4e564 --- /dev/null +++ b/system/class/Session.class.php @@ -0,0 +1,230 @@ + TRUE, + 'user_id' => TRUE, + 'session_start_time' => TRUE, + 'tz_offset' => TRUE, + ); + + // Saved directly in session with finalize + private static $data = array(); + private static $objects = array(); + + /* + * Not stored in session + */ + /** + * Indecates if the current page load has been counted + * @var boolean + */ + private static $page_counted = FALSE; + + const SESSION_OBJECT_KEY = 'EMBER_SESSION'; + const OBJECT_VERSION = 1; + + /** + * Sets up the Session object regenerating it from the session + * or building it from scratch + * @return boolean True of the init came from the session, false if it is defaults + */ + public static function init() + { + //Set up the session for the session object, done outside the usual session object regeneration + // Because it is used to make the session object recoverable by it's self + if(!isset($_SESSION[self::SESSION_OBJECT_KEY]) || + !is_array($_SESSION[self::SESSION_OBJECT_KEY]) || + !isset($_SESSION[self::SESSION_OBJECT_KEY]['data']) || + !is_array($_SESSION[self::SESSION_OBJECT_KEY]['data'])) + $_SESSION[self::SESSION_OBJECT_KEY] = array('data' => array(), 'objects' => array()); + else + { + self::$data = $_SESSION[self::SESSION_OBJECT_KEY]['data']; + self::$objects = $_SESSION[self::SESSION_OBJECT_KEY]['objects']; + } + + $data = self::getObject(get_class()); + + $new = TRUE; + + //Attempt to recover Object + if(is_array($data)) + { + $required = self::$required_vars; + + foreach($data as $key => $value) + { + if(isset($required[$key])) + unset($required[$key]); + + self::$$key = $value; + } + + //Detect if Object recovered + $new = array_search(TRUE, $required) === FALSE ? FALSE : TRUE; + + if(Debug::isEnabled()) + foreach($required as $k => $r) + { + if($r) + Debug::log('Session: var not recovered: '.$k, Debug::LOG_SESSION); + } + } + + if($new) + { + self::$id = 0; + self::$user_id = 0; + self::$session_page_number = 0; + self::$session_start_time = time(); + self::$tz_offset = NULL; + + Session::register(get_class()); + } + + if (!isset($_SESSION[CSRF_FIELD])) + $_SESSION[CSRF_FIELD] = session_id(); + + return !$new; + + } + + /** + * Serializes the object to be stored in the session + * @return array data to be stored in the session + */ + public static function serialize() + { + $result = array(); + foreach(self::$required_vars as $var => $required) + $result[$var] = self::$$var; + + return $result; + } + + /** + * Clears the current session, starts a new session + * must be called before a new user can login after a user + * has already logged in + */ + public static function newSession() + { + //TODO + } + + /** + * Registers the object to be saved in the session + * The class must implement iSession + * @param string $class name of the class to be saved to the session + */ + public static function register($class) + { + $implements = class_implements($class); + if(array_search('iSession', $implements) === FALSE) + throw new Exception("Class: {$class} not compatable, does not implement iSession"); + + Debug::log('Registering: '.$class, Debug::LOG_SESSION); + self::$objects[$class] = $class::OBJECT_VERSION; + } + + /** + * Recovers the data for the specified class + * @param string $class class to be recovered from the session + * @return array + */ + public static function getObject($class) + { + //Rebuilding object, version violation + if(!isset(self::$objects[$class])) + { + Debug::log('Object not in session: '.$class, Debug::LOG_SESSION); + return array(); + } + + if($class::OBJECT_VERSION != self::$objects[$class]) + { + Debug::log('Version changed: '.$class, Debug::LOG_SESSION); + unset(self::$objects[$class]); + return array(); + } + else + { + Debug::log('Object in session: '.$class, Debug::LOG_SESSION); + return self::$data[$class]; + } + } + + /** + * Saves the session to the dabase + * populates the id value, if the session was lost recovers what it + * can from the DB + */ + public static function initDBSession() + { +//TODO + } + + /** + * Saves the user_id to the session and the database + * @param uInt $user_id id to save to the db and in the session + */ + public static function setUserId($user_id) + { + self::$user_id = $user_id; + + //TODO: sync to the database + } + + /** + * Saves the named class to the data array. + * @param string $class name of the class to save + */ + public static function saveObject($class) + { + if(!isset(self::$objects[$class])) + throw new Exception( "Class not registered" ); + + Debug::log('Object saved to session: '.$class, Debug::LOG_SESSION); + + self::$data[$class] = $class::serialize(); + self::$objects[$class] = $class::OBJECT_VERSION; + } + + /** + * Saves all the registered objects to the session + */ + public static function finalize() + { + foreach(self::$objects as $class => $version) + self::saveObject ($class); + + $_SESSION[self::SESSION_OBJECT_KEY]['data'] = self::$data; + $_SESSION[self::SESSION_OBJECT_KEY]['objects'] = self::$objects; + } + + /** + * Increments the page counter. + * Will only ever allow a page load to be counted once + */ + public static function incrementPageCount() + { + if(self::$page_counted) + return; + + self::$session_page_number++; + } +} \ No newline at end of file diff --git a/system/class/iSession.class.php b/system/class/iSession.class.php new file mode 100644 index 0000000..042153e --- /dev/null +++ b/system/class/iSession.class.php @@ -0,0 +1,28 @@ + $required) + $result[$var] = self::$$var; + + return $result; + } + * + */ +} diff --git a/system/include/common.inc.php b/system/include/common.inc.php index 2b0bb5e..d1f432f 100644 --- a/system/include/common.inc.php +++ b/system/include/common.inc.php @@ -95,8 +95,15 @@ function __autoload($class) //spl_autoload_register('emberAutoload', true, true); - //INIT Session + Session::init(); + + Debug::init(); + + //INIT DB + //INIT Cache //INIT Site //INIT Smarty //INIT Permissions - //INIT User \ No newline at end of file + //INIT User + + Session::finalize(); \ No newline at end of file diff --git a/webroot/debug.php b/webroot/debug.php new file mode 100644 index 0000000..18486e0 --- /dev/null +++ b/webroot/debug.php @@ -0,0 +1,22 @@ +'; + Debug::disable(); + echo 'Enable Debugging
Home'; +} +else +{ + echo 'Debugging Enabled
'; + Debug::enable (); + + foreach(Debug::getAvalableOptions() as $o) + Debug::setOption($o); + + echo 'Disable Debugging
Home'; +} + + +Session::finalize(); \ No newline at end of file diff --git a/webroot/index.php b/webroot/index.php index ec98913..d3acb1a 100644 --- a/webroot/index.php +++ b/webroot/index.php @@ -1,19 +1,6 @@ '; - print_r($GLOBALS); -echo ''; + Session::finalize(); -echo '
';
-	print_r(get_included_files());
-echo '
'; - -echo '
';
-	echo 'DOCUMENT_ROOT: '.DOCUMENT_ROOT . PHP_EOL;
-	echo 'CODE_BASE: '.CODE_BASE . PHP_EOL;
-	echo 'CODE_BASE_ROOT:'.CODE_BASE_ROOT . PHP_EOL;
-echo '
'; - -echo '
';
-echo 'Page Load Time: ' . getPageLoadTime();
-echo '
'; \ No newline at end of file + Debug::printDebuggingInfo(); \ No newline at end of file diff --git a/webroot/resetsession.php b/webroot/resetsession.php new file mode 100644 index 0000000..acc9e41 --- /dev/null +++ b/webroot/resetsession.php @@ -0,0 +1,3 @@ +