Various improvements

This commit is contained in:
Timothy Warren 2012-01-03 16:36:46 -05:00
parent f12070b262
commit e19814fdfa
4 changed files with 143 additions and 10 deletions

View File

@ -0,0 +1,28 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Error</title>
<style type="text/css">
.message{
position:relative;
margin:0.5em auto;
padding:0.5em;
width:95%;
}
.error{
border:1px solid #924949;
background: #f3e6e6;
}
</style>
</head>
<body>
<div class="message error">
<?php if(isset($title)) : ?>
<h1><?php echo $title ?></h1>
<?php endif ?>
<?php echo $message; ?>
</div>
</body>
</html>

View File

@ -23,5 +23,4 @@ class Welcome extends MM_Controller {
$this->output->set_output($output); $this->output->set_output($output);
} }
} }

View File

@ -113,15 +113,40 @@ function on_exception($exception)
echo $msg; echo $msg;
} }
// --------------------------------------------------------------------------
/**
* General 404 function
*/
function show_404() function show_404()
{ {
@header('HTTP/1.1 404 Not Found', TRUE, 404); @header('HTTP/1.1 404 Not Found', TRUE, 404);
die('<h1>404 Not Found</h1>'); die('<h1>404 Not Found</h1>');
} }
function show_error() // --------------------------------------------------------------------------
{
/**
* Fatal Error page function
*
* @param string $message
* @param int $status_code
*/
function show_error($message, $status_code=null)
{
if( ! is_null($status_code))
{
@header("HTTP/1.1 {$status_code}", TRUE, (int)$status_code);
}
// Contain the content for buffering
ob_start();
include(APP_PATH.'/errors/error_general.php');
$buffer = ob_get_contents();
ob_end_clean();
die($buffer);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -170,7 +195,7 @@ function controller_methods($controller)
*/ */
function route() function route()
{ {
$pi = $_SERVER['PATH_INFO']; $pi = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : null;
// Load the routes config file // Load the routes config file
$routes = require_once(APP_PATH.'config/routes.php'); $routes = require_once(APP_PATH.'config/routes.php');
@ -259,11 +284,7 @@ function route()
{ {
$class = new $controller; $class = new $controller;
//echo "Found class"; return call_user_func(array($class, $func));
call_user_func(array($class, $func));
return;
} }
// Function doesn't exist...404 // Function doesn't exist...404
@ -290,6 +311,44 @@ function site_url($segment)
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/**
* Wrapper function to simplify using curl
*
* @param string $url
* @param array $options
* @return mixed
*/
function do_curl($url, $options=array())
{
$cookie = tempnam ("/tmp", "CURLCOOKIE");
$ch = curl_init($url);
//Use the user's User Agent and Cookies
$opts= array(
CURLOPT_USERAGENT => $_SERVER['HTTP_USER_AGENT'],
CURLOPT_COOKIEJAR => $cookie,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_ENCODING => "",
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_AUTOREFERER => TRUE,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 10,
CURLOPT_MAXREDIRS => 10,
CURLOPT_PROTOCOLS => array(CURLPROTO_HTTP,CURLPROTO_HTTPS)
);
//Set default options
curl_setopt_array($ch, $opts);
//Set additional options
curl_setopt_array($ch, $options);
return curl_exec($ch);
}
// --------------------------------------------------------------------------
//Set error handlers //Set error handlers
set_error_handler('on_error'); set_error_handler('on_error');
set_exception_handler('on_exception'); set_exception_handler('on_exception');
@ -303,4 +362,4 @@ require_once('db.php');
//Route to the appropriate function //Route to the appropriate function
route(); route();
// End of common.php // End of common.php

View File

@ -142,6 +142,53 @@ class miniMVC{
{ {
return self::get_instance(); return self::get_instance();
} }
/**
* Method to load classes into the singleton
*
* @param string $name
*/
function load_class($name)
{
$path = APP_PATH . "classes/{$name}.php";
$class = "{$name}";
if(class_exists($class, FALSE))
{
if ( ! isset($this->$name))
{
$this->$name = new $class;
return;
}
}
if(is_file($path))
{
require_once($path);
if(class_exists($class, FALSE))
{
if ( ! isset($this->$name))
{
$this->$name = new $class;
return;
}
}
}
}
/**
* Convenience function to remove an object from the singleton
*
* @param string $name
*/
function unload($name)
{
if(isset($this->$name))
{
unset($this->$name);
}
}
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------