Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 59 additions & 41 deletions system/pip.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,65 @@
<?php

function pip()
{
global $config;

// Set our defaults
$controller = $config['default_controller'];
$action = 'index';
$url = '';

// Get request url and script url
$request_url = (isset($_SERVER['REQUEST_URI'])) ? $_SERVER['REQUEST_URI'] : '';
$script_url = (isset($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : '';

// Get our url path and trim the / of the left and the right
if($request_url != $script_url) $url = trim(preg_replace('/'. str_replace('/', '\/', str_replace('index.php', '', $script_url)) .'/', '', $request_url, 1), '/');

// Split the url into segments
$segments = explode('/', $url);

// Do our default checks
if(isset($segments[0]) && $segments[0] != '') $controller = $segments[0];
if(isset($segments[1]) && $segments[1] != '') $action = $segments[1];

// Get our controller file
$path = APP_DIR . 'controllers/' . $controller . '.php';
if(file_exists($path)){
require_once($path);
} else {
$controller = $config['error_controller'];
require_once(APP_DIR . 'controllers/' . $controller . '.php');

class Pip {
private $controller;
private $action;
private $url;

function __construct(){
$segments = $this->startParams();
$segments = $this->startActions($segments);

$obj = new $this->controller;
die(call_user_func_array(array($obj, $this->action), array($segments)));
}

function startParams(){
global $config;

$this->controller = $config['default_controller'];
$this->action = 'index';
$this->url = APP_DIR;

$request_url = (isset($_SERVER['REQUEST_URI'])) ? $_SERVER['REQUEST_URI'] : '';
$script_url = (isset($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : '';

if($request_url != $script_url){
$this->url = trim(preg_replace('/'. str_replace('/', '\/', str_replace('index.php', '', $script_url)) .'/', '', $request_url, 1), '/');
}
$segments = explode('/', $this->url);

if(isset($segments[0]) && $segments[0] != '') $this->controller = $segments[0];
if(isset($segments[1]) && $segments[1] != '') $this->action = $segments[1];

return $segments;
}

// Check the action exists
if(!method_exists($controller, $action)){
$controller = $config['error_controller'];
require_once(APP_DIR . 'controllers/' . $controller . '.php');
$action = 'index';
}

// Create object and call method
$obj = new $controller;
die(call_user_func_array(array($obj, $action), array_slice($segments, 2)));

function startActions($segments){
global $config;

$path = APP_DIR . 'controllers' . DS . $this->controller . '.php';
$controller = $this->controller;

if(file_exists($path)){
require_once($path);
} else {
$segments[0] = "Controller '{$controller}' Not Found! ";
$this->controller = $config['error_controller'];
require_once(APP_DIR . 'controllers' . DS . $this->controller . '.php');
}

if(!method_exists($this->controller, $this->action)){
$this->action = 'index';
$action = $this->action;
$segments[0] = "Method '{$action}' in Controller '{$controller}' Not Found! ";
$this->controller = $config['error_controller'];
require_once(APP_DIR . 'controllers' . DS . $this->controller . '.php');
}

return $segments;
}

}

?>