Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Started wrapping index.php functionality into Caffeine class. Still n…

…eeds work. Added MIT license.
  • Loading branch information...
commit bdade42e31d6e73bd5d851c81b82aa3a45c0345e 1 parent c9e3b24
@geekforbrains authored
Showing with 135 additions and 62 deletions.
  1. +7 −0 LICENSE
  2. +128 −62 index.php
View
7 LICENSE
@@ -0,0 +1,7 @@
+Copyright (C) 2012 Caffeine
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
190 index.php
@@ -1,38 +1,110 @@
<?php
-session_start();
-
-date_default_timezone_set('UTC');
-define('ROOT', __DIR__ . '/');
-define('EXT', '.php');
-define('VERSION', '1.0');
-
-define('ERROR_NOTFOUND', 404);
-define('ERROR_ACCESSDENIED', 401);
-define('ERROR_MAINTENANCE', 'maintenance');
+/**
+ * Caffeine
+ *
+ * Caffeine is a simple PHP layer that combines modules through the use of events
+ * to form an application.
+ *
+ * @version 1.0
+ * @author Gavin Vickery <gavin@geekforbrains.com>
+ * @license http://www.opensource.org/licenses/mit-license.php
+ */
+class Caffeine {
+
+ /**
+ * Allows Caffeine to only be initialized once.
+ */
+ private static $_inited = false;
+
+ /**
+ * Required core modules to load in order for autoloading magic to work.
+ */
+ private static $_requiredCore = array(
+ 'module',
+ 'config',
+ 'load',
+ 'site'
+ );
+
+ /**
+ * Starting point for every page request. Loads required core modules, gets data from url and calls
+ * necessary modules to make things happen.
+ */
+ public static function init()
+ {
+ if(!self::$_inited)
+ {
+ self::$_inited = true;
-$core = array(
- 'module', // Module must be loaded first; everything extends it
- 'config',
- 'load',
- 'site'
-);
+ foreach(self::$_requiredCore as $module)
+ require_once(ROOT . 'core/' . $module . '/' . $module . EXT);
+ // Set the Load::auto method to handle all class loading from now on
+ spl_autoload_register('Load::auto');
-foreach($core as $module)
- require_once(ROOT . 'core/' . $module . '/' . $module . EXT);
+ Load::loadSetupFiles();
+ Event::trigger('caffeine.started');
-spl_autoload_register('Load::auto');
-Load::loadSetupFiles();
-//Db::install();
+ // If maintenance mode was enabled in the admin, load maintenace view and stop everything
+ if(Variable::get('system.maintenance_mode', false))
+ View::error(ERROR_MAINTENANCE);
+ else
+ {
+ list($route, $data) = Router::getRouteData();
-Event::trigger('caffeine.started');
+ if($data)
+ {
+ if(self::_hasPermission($route, $data))
+ {
+ list($module, $controller, $method) = $data['callback'];
+ $params = Router::getParams();
+
+ // Make sure controller words are upper-case
+ $conBits = explode('_', $controller);
+ foreach($conBits as &$bit)
+ $bit = ucfirst($bit);
+ $controller = implode('_', $conBits);
+
+ $controller = sprintf('%s_%sController', ucfirst($module), ucwords($controller));
+
+ // Call the routes controller and method
+ $response = call_user_func_array(array($controller, $method), $params);
+
+ // If the response is an int, assume its a pre-defined error code
+ if(!is_int($response))
+ {
+ Event::trigger('module.response', array($response));
+ View::load($module, $controller, $method);
+ }
+
+ // All other method responses are assumed to be errors. Load their views.
+ else
+ View::error($response);
+ }
+ else
+ View::error(ERROR_ACCESSDENIED);
+ }
+ else
+ {
+ if($route !== '[index]' || !View::directLoad('index'))
+ View::error(ERROR_NOTFOUND);
+ }
+ }
-if(!Variable::get('system.maintenance_mode', false))
-{
- list($route, $data) = Router::getRouteData();
+ View::output();
+ Event::trigger('caffeine.finished');
+ }
+ else
+ die('Why are you trying to re-initialize Caffeine?');
+ }
- if($data)
+ /**
+ * Checks if the current user has access to the current route.
+ *
+ * @return boolean
+ */
+ private static function _hasPermission($route, $data)
{
$hasPermission = false;
@@ -41,7 +113,7 @@
$hasPermission = true;
Dev::debug('user', 'User has permission');
- // Only do callbacks if user is admin, otherwise always allow
+ // Only do user permission callbacks if not admin, otherwise its pointless
if(User::current()->is_admin <= 0)
{
foreach($data['permissions'] as $k)
@@ -61,44 +133,38 @@
}
}
else
- Dev::debug('user', 'User does NOT have permissions');
+ Dev::debug('user', 'User does NOT have permission');
- if($hasPermission)
- {
- list($module, $controller, $method) = $data['callback'];
- $params = Router::getParams();
+ return $hasPermission;
+ }
- // Make sure controller words are upper
- $conBits = explode('_', $controller);
- foreach($conBits as &$bit)
- $bit = ucfirst($bit);
- $controller = implode('_', $conBits);
+}
- $controller = sprintf('%s_%sController', ucfirst($module), ucwords($controller));
- $response = call_user_func_array(array($controller, $method), $params);
+/**
+ * These constants allow Caffeine to determine where files are and how to load them.
+ * Dont mess with these unless you know what you're doing.
+ */
+define('ROOT', __DIR__ . '/');
+define('EXT', '.php');
+define('VERSION', '1.0');
- // If the response is an int, assume its a pre-defined error code
- if(!is_int($response))
- {
- Event::trigger('module.response', array($response));
- View::load($module, $controller, $method);
- }
+/**
+ * These constants are used to load specific error view pages such as 404,
+ * access denied and maintenance.
+ */
+define('ERROR_NOTFOUND', 404);
+define('ERROR_ACCESSDENIED', 401);
+define('ERROR_MAINTENANCE', 'maintenance');
- // All other method responses are assumed to be errors. Load their views.
- else
- View::error($response);
- }
- else
- View::error(ERROR_ACCESSDENIED);
- }
- else
- {
- if($route != '[index]' || !View::directLoad('index'))
- View::error(ERROR_NOTFOUND);
- }
-}
-else // In maintenance mode!!!
- View::error(ERROR_MAINTENANCE);
+/**
+ * Sometimes PHP complains if a timezone isn't set, so set UTC initially but we'll set it again
+ * using the Config module once its loaded.
+ *
+ * DONT CHANGE LANGUAGE HERE, DO IT IN CONFIG WITH "system.timezone".
+ */
+date_default_timezone_set('UTC');
-View::output();
-Event::trigger('caffeine.finished');
+// Vroom, vroom!
+session_start();
+session_regenerate_id();
+Caffeine::init();
Please sign in to comment.
Something went wrong with that request. Please try again.