2011-12-27 13:24:28 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* File including common framework-wide functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to search through the tree to find the necessary file
|
|
|
|
*
|
|
|
|
* @param string $file
|
2011-12-27 16:47:27 -05:00
|
|
|
* @param string $curr_path
|
|
|
|
* @return void
|
2011-12-27 13:24:28 -05:00
|
|
|
*/
|
2011-12-28 19:33:41 -05:00
|
|
|
function load_file($file, $curr_path="")
|
2011-12-27 13:24:28 -05:00
|
|
|
{
|
2011-12-28 19:33:41 -05:00
|
|
|
$path = "";
|
|
|
|
|
2011-12-27 16:47:27 -05:00
|
|
|
if($curr_path === "app")
|
|
|
|
{
|
|
|
|
$path = APP_PATH."{$file}.php";
|
|
|
|
}
|
|
|
|
else if($curr_path === "sys")
|
|
|
|
{
|
|
|
|
$path = SYS_PATH."{$file}.php";
|
|
|
|
}
|
2011-12-28 19:33:41 -05:00
|
|
|
else
|
2011-12-27 16:47:27 -05:00
|
|
|
{
|
2011-12-28 19:33:41 -05:00
|
|
|
$path = MOD_PATH."{$curr_path}/{$file}.php";
|
2011-12-27 16:47:27 -05:00
|
|
|
}
|
|
|
|
|
2011-12-29 10:31:04 -05:00
|
|
|
if(is_file($path))
|
|
|
|
{
|
|
|
|
require_once($path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-27 13:24:28 -05:00
|
|
|
|
2011-12-28 19:33:41 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2011-12-27 13:24:28 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom error handler
|
|
|
|
*/
|
2011-12-27 16:47:27 -05:00
|
|
|
function on_error($severity, $message, $filepath, $line, $context)
|
2011-12-27 13:24:28 -05:00
|
|
|
{
|
2011-12-27 16:47:27 -05:00
|
|
|
$levels = array(
|
|
|
|
E_ERROR => 'Error',
|
|
|
|
E_WARNING => 'Warning',
|
|
|
|
E_PARSE => 'Parsing Error',
|
|
|
|
E_NOTICE => 'Notice',
|
|
|
|
E_CORE_ERROR => 'Core Error',
|
|
|
|
E_CORE_WARNING => 'Core Warning',
|
|
|
|
E_COMPILE_ERROR => 'Compile Error',
|
|
|
|
E_COMPILE_WARNING => 'Compile Warning',
|
|
|
|
E_USER_ERROR => 'User Error',
|
|
|
|
E_USER_WARNING => 'User Warning',
|
|
|
|
E_USER_NOTICE => 'User Notice',
|
2011-12-29 10:31:04 -05:00
|
|
|
E_STRICT => 'Strict Error'
|
2011-12-27 13:24:28 -05:00
|
|
|
);
|
2011-12-27 16:47:27 -05:00
|
|
|
|
|
|
|
$severity = (isset($levels[$severity])) ? $levels[$severity] : $severity;
|
|
|
|
|
|
|
|
// Contain the content for buffering
|
|
|
|
ob_start();
|
|
|
|
|
2011-12-29 16:57:28 -05:00
|
|
|
include(APP_PATH.'/errors/error_php.php');
|
2011-12-27 16:47:27 -05:00
|
|
|
|
|
|
|
$buffer = ob_get_contents();
|
|
|
|
ob_end_clean();
|
|
|
|
echo $buffer;
|
2011-12-27 13:24:28 -05:00
|
|
|
}
|
|
|
|
|
2011-12-28 19:33:41 -05:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2011-12-27 13:24:28 -05:00
|
|
|
/**
|
|
|
|
* Custom exception handler
|
|
|
|
*/
|
|
|
|
function on_exception($exception)
|
|
|
|
{
|
|
|
|
// these are our templates
|
|
|
|
$traceline = "#%s %s(%s): %s(%s)";
|
|
|
|
$msg = "PHP Fatal error: Uncaught exception '%s' with message '%s' in %s:%s<br />Stack trace:<br />%s<br /> thrown in %s on line %s";
|
|
|
|
|
|
|
|
// alter your trace as you please, here
|
|
|
|
$trace = $exception->getTrace();
|
|
|
|
|
|
|
|
// build your tracelines
|
|
|
|
$result = array();
|
|
|
|
foreach ($trace as $key => $stackPoint) {
|
|
|
|
$result[] = sprintf(
|
|
|
|
$traceline,
|
|
|
|
$key,
|
|
|
|
$stackPoint['file'],
|
|
|
|
$stackPoint['line'],
|
|
|
|
$stackPoint['function'],
|
|
|
|
implode(', ', $stackPoint['args'])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// trace always ends with {main}
|
|
|
|
$result[] = '#' . ++$key . ' {main}';
|
|
|
|
|
|
|
|
// write tracelines into main template
|
|
|
|
$msg = sprintf(
|
|
|
|
$msg,
|
|
|
|
get_class($exception),
|
|
|
|
$exception->getMessage(),
|
|
|
|
$exception->getFile(),
|
|
|
|
$exception->getLine(),
|
|
|
|
implode("<br />", $result),
|
|
|
|
$exception->getFile(),
|
|
|
|
$exception->getLine()
|
|
|
|
);
|
|
|
|
|
|
|
|
echo $msg;
|
|
|
|
}
|
|
|
|
|
2011-12-29 16:57:28 -05:00
|
|
|
function show_404()
|
|
|
|
{
|
|
|
|
@header('HTTP/1.1 404 Not Found', TRUE, 404);
|
|
|
|
die('<h1>404 Not Found</h1>');
|
|
|
|
}
|
|
|
|
|
|
|
|
function show_error()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-12-28 19:33:41 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2011-12-27 16:47:27 -05:00
|
|
|
|
2011-12-30 16:41:25 -05:00
|
|
|
/**
|
|
|
|
* Returns routable methods for the specified controller class
|
|
|
|
*
|
|
|
|
* @param string $controller
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function controller_methods($controller)
|
|
|
|
{
|
|
|
|
$methods = get_class_methods($controller);
|
|
|
|
$num = count($methods);
|
|
|
|
|
|
|
|
for($i=0; $i < $num; $i++)
|
|
|
|
{
|
|
|
|
$skip_methods = array(
|
|
|
|
'load_file',
|
|
|
|
'on_error',
|
|
|
|
'on_exception',
|
|
|
|
'show_404',
|
|
|
|
'show_error',
|
|
|
|
'controller_methods',
|
|
|
|
'route',
|
|
|
|
'site_url',
|
|
|
|
'load_view',
|
|
|
|
'get_instance',
|
|
|
|
);
|
|
|
|
|
|
|
|
// If the method starts with an underscore, or
|
|
|
|
// is in the blacklist of methods, it is not callable
|
|
|
|
if($methods[$i]{0} === "_" || in_array($methods[$i], $skip_methods))
|
|
|
|
{
|
|
|
|
unset($methods[$i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $methods;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2011-12-27 16:47:27 -05:00
|
|
|
/**
|
2011-12-28 19:33:41 -05:00
|
|
|
* Calls the appropriate module/controller/function based on the url
|
2011-12-27 16:47:27 -05:00
|
|
|
*/
|
2011-12-28 19:33:41 -05:00
|
|
|
function route()
|
|
|
|
{
|
2011-12-29 16:57:28 -05:00
|
|
|
$pi = $_SERVER['PATH_INFO'];
|
|
|
|
|
|
|
|
// Load the routes config file
|
|
|
|
$routes = require_once(APP_PATH.'config/routes.php');
|
|
|
|
|
|
|
|
// Set the default route
|
|
|
|
$module = $routes['default_module'];
|
|
|
|
$controller = $routes['default_controller'];
|
2011-12-28 19:33:41 -05:00
|
|
|
$func = "index";
|
2011-12-29 16:57:28 -05:00
|
|
|
$route_set = FALSE;
|
2011-12-30 16:41:25 -05:00
|
|
|
|
|
|
|
// If it isn't the index page
|
2011-12-29 16:57:28 -05:00
|
|
|
if( ! empty($pi) && $pi !== "/")
|
2011-12-27 16:47:27 -05:00
|
|
|
{
|
2011-12-29 16:57:28 -05:00
|
|
|
//Remove trailing slash and begining slash
|
|
|
|
$pi = trim($pi, '/');
|
|
|
|
|
|
|
|
// URL matches the route exactly? Cool, that was easy
|
|
|
|
if(isset($routes[$pi]))
|
|
|
|
{
|
|
|
|
list($module, $controller, $func) = explode("/", $routes[$pi]);
|
|
|
|
$route_set = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$custom_routes = $routes;
|
|
|
|
|
|
|
|
// Skip required routes
|
|
|
|
unset($custom_routes['default_module']);
|
|
|
|
unset($custom_routes['default_controller']);
|
|
|
|
unset($custom_routes['404_handler']);
|
|
|
|
|
|
|
|
foreach($custom_routes as $uri => $map)
|
|
|
|
{
|
|
|
|
if(preg_match("`{$uri}`i", $pi))
|
|
|
|
{
|
|
|
|
list($module, $controller, $func) = explode("/", $map);
|
|
|
|
$route_set = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Doesn't match a predefined route?
|
|
|
|
// Match on module/controller/method, controller/method, or method
|
|
|
|
if( ! $route_set)
|
|
|
|
{
|
2011-12-30 16:41:25 -05:00
|
|
|
$num_segments = 0;
|
|
|
|
|
|
|
|
if(strpos($pi, '/') === FALSE && ! empty($pi))
|
2011-12-29 16:57:28 -05:00
|
|
|
{
|
|
|
|
$num_segments = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$segments = explode('/', $pi);
|
|
|
|
$num_segments = count($segments);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($num_segments === 1)
|
|
|
|
{
|
|
|
|
$func = $pi;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($num_segments === 2)
|
|
|
|
{
|
|
|
|
list($controller, $func) = $segments;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($num_segments >= 3)
|
|
|
|
{
|
|
|
|
list($module, $controller, $func) = $segments;
|
|
|
|
}
|
|
|
|
}
|
2011-12-28 19:33:41 -05:00
|
|
|
}
|
2011-12-27 16:47:27 -05:00
|
|
|
|
2011-12-29 16:57:28 -05:00
|
|
|
$path = MOD_PATH."{$module}/controllers/{$controller}.php";
|
|
|
|
|
|
|
|
if(is_file($path))
|
|
|
|
{
|
|
|
|
require_once($path);
|
|
|
|
|
2011-12-30 16:41:25 -05:00
|
|
|
// Get the list of valid methods for that controller
|
|
|
|
$methods = controller_methods($controller);
|
2011-12-29 16:57:28 -05:00
|
|
|
|
|
|
|
if(in_array($func, $methods))
|
|
|
|
{
|
|
|
|
$class = new $controller;
|
2011-12-30 16:41:25 -05:00
|
|
|
|
|
|
|
//echo "Found class";
|
|
|
|
|
|
|
|
call_user_func(array($class, $func));
|
|
|
|
|
|
|
|
return;
|
2011-12-29 16:57:28 -05:00
|
|
|
}
|
|
|
|
|
2011-12-30 16:41:25 -05:00
|
|
|
// Function doesn't exist...404
|
2011-12-29 16:57:28 -05:00
|
|
|
show_404();
|
2011-12-30 16:41:25 -05:00
|
|
|
die();
|
2011-12-29 16:57:28 -05:00
|
|
|
}
|
2011-12-27 16:47:27 -05:00
|
|
|
|
2011-12-29 16:57:28 -05:00
|
|
|
// If it gets here, it's still a 404
|
|
|
|
show_404();
|
2011-12-28 19:33:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2011-12-29 16:57:28 -05:00
|
|
|
/**
|
|
|
|
* Returns a full url from a url segment
|
|
|
|
*
|
|
|
|
* @param string $segment
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function site_url($segment)
|
|
|
|
{
|
|
|
|
return $url = BASEURL . URL_INDEX_FILE . $segment;
|
|
|
|
}
|
2011-12-28 19:33:41 -05:00
|
|
|
|
2011-12-29 16:57:28 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2011-12-28 19:33:41 -05:00
|
|
|
|
|
|
|
//Set error handlers
|
|
|
|
set_error_handler('on_error');
|
2011-12-29 10:31:04 -05:00
|
|
|
set_exception_handler('on_exception');
|
2011-12-28 19:33:41 -05:00
|
|
|
|
|
|
|
// Load Most Common libraries
|
|
|
|
require_once('miniMVC.php');
|
|
|
|
require_once('output.php');
|
|
|
|
require_once('page.php');
|
|
|
|
require_once('db.php');
|
|
|
|
|
|
|
|
//Route to the appropriate function
|
|
|
|
route();
|
|
|
|
|
2011-12-30 16:41:25 -05:00
|
|
|
// End of common.php
|