Skip to content
4 changes: 2 additions & 2 deletions application/config/config.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

$config['base_url'] = ''; // Base URL including trailing slash (e.g. http://localhost/)
$config['base_url'] = ROOT_DIR; // Base URL including trailing slash (e.g. http://localhost/)

$config['default_controller'] = 'main'; // Default controller to load
$config['error_controller'] = 'error'; // Controller used for errors (e.g. 404, 500 etc)
Expand All @@ -10,4 +10,4 @@
$config['db_username'] = ''; // Database username
$config['db_password'] = ''; // Database password

?>
?>
31 changes: 23 additions & 8 deletions application/controllers/error.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,30 @@

class Error extends Controller {

function index()
{
$this->error404();
function index($segments){
return $this->error($this->getMessage($segments));
}

function error404()
{
echo '<h1>404 Error</h1>';
echo '<p>Looks like this page doesn\'t exist</p>';

function getMessage($segments){
if(is_array($segments)){
$segments = $segments[0];
}

return $segments;
}

function error($segments="Error!"){
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
return json_encode(array(
'success' => false,
'data' => array(),
'msg'=> utf8_encode($segments)
));
}
else{
return utf8_decode("<h1>{$segments}</h1>");
}

}

}
Expand Down
12 changes: 12 additions & 0 deletions application/controllers/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ function index()
$template = $this->loadView('main_view');
$template->render();
}

function categoryAjax(){
$model = $this->loadModel('example_model');

$response = $model->getProjetos();
if($response[0] === true){
$this->render(array('data'=>$response[1]));
}
else{
$this->render(array('data'=>array(), 'error'=> $response[1]));
}
}

}

Expand Down
6 changes: 2 additions & 4 deletions application/models/example_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

class Example_model extends Model {

public function getSomething($id)
{
$id = $this->escapeString($id);
$result = $this->query('SELECT * FROM something WHERE id="'. $id .'"');
public function getCategory(){
$result = $this->query('SELECT * FROM category');
return $result;
}

Expand Down
20 changes: 11 additions & 9 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
<?php
/*
* PIP v0.5.3
* PIP v0.5.4
*/

//Start the Session
session_start();

// Defines
define('ROOT_DIR', realpath(dirname(__FILE__)) .'/');
define('APP_DIR', ROOT_DIR .'application/');
define('DS', DIRECTORY_SEPARATOR);

define('ROOT_DIR', realpath(dirname(__FILE__)) . DS);
define('APP_DIR', ROOT_DIR .'application' . DS);

// Includes
require(APP_DIR .'config/config.php');
require(ROOT_DIR .'system/model.php');
require(ROOT_DIR .'system/view.php');
require(ROOT_DIR .'system/controller.php');
require(ROOT_DIR .'system/pip.php');
require(APP_DIR .'config'. DS .'config.php');
require(ROOT_DIR .'system'. DS .'model.php');
require(ROOT_DIR .'system'. DS .'view.php');
require(ROOT_DIR .'system'. DS .'controller.php');
require(ROOT_DIR .'system'. DS .'application.php');

// Define base URL
global $config;
define('BASE_URL', $config['base_url']);

pip();
new pip();

?>
36 changes: 22 additions & 14 deletions system/controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,47 @@

class Controller {

public function loadModel($name)
{
require(APP_DIR .'models/'. strtolower($name) .'.php');
public function loadModel($name){
require(APP_DIR .'models'. DS . strtolower($name) .'.php');

$model = new $name;
return $model;
}

public function loadView($name)
{
public function loadView($name){
$view = new View($name);
return $view;
}

public function loadPlugin($name)
{
require(APP_DIR .'plugins/'. strtolower($name) .'.php');
public function loadPlugin($name){
require(APP_DIR .'plugins'. DS . strtolower($name) .'.php');
}

public function loadHelper($name)
{
require(APP_DIR .'helpers/'. strtolower($name) .'.php');
public function loadHelper($name){
require(APP_DIR .'helpers'. DS . strtolower($name) .'.php');
$helper = new $name;
return $helper;
}

public function redirect($loc)
{
public function redirect($loc){
global $config;

header('Location: '. $config['base_url'] . $loc);
}

public function render($response=array(), $tipo='json'){
$tipo = strtolower($tipo);
if($tipo === 'json'){
die(json_encode($response));
}
else if($tipo === 'jsonp'){
die($_REQUEST['callback'] . '(' . json_encode($response) . ');' );
}
else{
echo $response;
}
}

}

?>
?>
66 changes: 38 additions & 28 deletions system/model.php
Original file line number Diff line number Diff line change
@@ -1,62 +1,72 @@
<?php

class Model {
class Model extends PDO{

private $connection;

public function __construct()
{
public function __construct(){
global $config;

$this->connection = mysql_pconnect($config['db_host'], $config['db_username'], $config['db_password']) or die('MySQL Error: '. mysql_error());
mysql_select_db($config['db_name'], $this->connection);

try{
$dsn = "mysql:dbname={$config['db_name']};host={$config['db_host']}";
$this->connection = parent::__construct($dsn, $config['db_username'], $config['db_password']);
parent::setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return array(true, $this->connection);
}
catch(PDOException $e){
return array(false, $e->getMessage());
}
}

public function escapeString($string)
{
//deprecated
public function escapeString($string){
return mysql_real_escape_string($string);
}

public function escapeArray($array)
{
//deprecated
public function escapeArray($array){
array_walk_recursive($array, create_function('&$v', '$v = mysql_real_escape_string($v);'));
return $array;
}

public function to_bool($val)
{
public function to_bool($val){
return !!$val;
}

public function to_date($val)
{
public function to_date($val){
return date('Y-m-d', $val);
}

public function to_time($val)
{
public function to_time($val){
return date('H:i:s', $val);
}

public function to_datetime($val)
{
public function to_datetime($val){
return date('Y-m-d H:i:s', $val);
}

public function query($qry)
{
$result = mysql_query($qry) or die('MySQL Error: '. mysql_error());
$resultObjects = array();
public function query($qry, $params=array()){
try{
$pdo = $this->prepare($qry);
$pdo->execute($params);

while($row = mysql_fetch_object($result)) $resultObjects[] = $row;
return array(true, $pdo->fetchAll(PDO::FETCH_OBJ));
}
catch(PDOException $e){
return array(false, $e->getMessage());
}

return $resultObjects;
}

public function execute($qry)
{
$exec = mysql_query($qry) or die('MySQL Error: '. mysql_error());
return $exec;
public function execute($qry, $params=array()){
try{
$pdo = $this->prepare($qry);
$pdo->execute($params);
return array(true, $pdo->rowCount());
}
catch(PDOException $e){
return array(false, $e->getMessage());
}
}

}
Expand Down
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;
}

}

?>
Loading