From fb5e05b1ee59773ac84cce435ae526cc72a94053 Mon Sep 17 00:00:00 2001 From: Maciel Sousa Date: Tue, 25 Dec 2012 18:18:20 -0200 Subject: [PATCH 01/12] Update system/model.php --- system/model.php | 75 ++++++++++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/system/model.php b/system/model.php index 04503ca..bf996e5 100644 --- a/system/model.php +++ b/system/model.php @@ -1,62 +1,61 @@ 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); - } - - public function escapeString($string) - { - return mysql_real_escape_string($string); - } - public function escapeArray($array) - { - array_walk_recursive($array, create_function('&$v', '$v = mysql_real_escape_string($v);')); - return $array; + 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('success'=>true, 'data'=> $this->connection); + } + catch(PDOException $e){ + return array('success'=>false, 'error'=> $e->getMessage()); + } } - 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(); - - while($row = mysql_fetch_object($result)) $resultObjects[] = $row; - - return $resultObjects; - } - - public function execute($qry) - { - $exec = mysql_query($qry) or die('MySQL Error: '. mysql_error()); - return $exec; + public function query($qry, $params=array()){ + try{ + $pdo = $this->prepare($qry); + $pdo->execute($params); + + return array('success'=>true, 'data'=> $pdo->fetchAll(PDO::FETCH_OBJ)); + } + catch(PDOException $e){ + return array('success'=>false, 'error'=> $e->getMessage()); + } + + } + + public function execute($qry, $params=array()){ + try{ + $pdo = $this->prepare($qry); + $pdo->execute($params); + return array('success'=>true, 'data'=> $pdo->rowCount()); + } + catch(PDOException $e){ + return array('success'=>false, 'error'=> $e->getMessage()); + } } } From 2de86dc0650155aa987afbc17873402aa27b428b Mon Sep 17 00:00:00 2001 From: Maciel Sousa Date: Tue, 25 Dec 2012 18:19:55 -0200 Subject: [PATCH 02/12] Update application/controllers/main.php example return category ajax --- application/controllers/main.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/application/controllers/main.php b/application/controllers/main.php index b1c40ac..f39b256 100644 --- a/application/controllers/main.php +++ b/application/controllers/main.php @@ -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]); + } + } } From 1765980b832884adf05ebf67894f6a49e3afabde Mon Sep 17 00:00:00 2001 From: Maciel Sousa Date: Tue, 25 Dec 2012 18:21:36 -0200 Subject: [PATCH 03/12] Update index.php --- index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.php b/index.php index ed8010b..f86dd20 100644 --- a/index.php +++ b/index.php @@ -21,6 +21,6 @@ global $config; define('BASE_URL', $config['base_url']); -pip(); +new pip(); ?> From bcbc5633a1a973ab1e87e2dc358de68ed2b015de Mon Sep 17 00:00:00 2001 From: Maciel Sousa Date: Tue, 25 Dec 2012 18:23:09 -0200 Subject: [PATCH 04/12] Update index.php --- index.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/index.php b/index.php index f86dd20..356743f 100644 --- a/index.php +++ b/index.php @@ -1,21 +1,23 @@ Date: Tue, 25 Dec 2012 18:24:38 -0200 Subject: [PATCH 05/12] Update system/pip.php --- system/pip.php | 100 +++++++++++++++++++++++++++++-------------------- 1 file changed, 59 insertions(+), 41 deletions(-) diff --git a/system/pip.php b/system/pip.php index c69195c..4332b87 100644 --- a/system/pip.php +++ b/system/pip.php @@ -1,47 +1,65 @@ 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; + } + } ?> From ab56839337ce48d7724dfb1508172ad8f4b087f8 Mon Sep 17 00:00:00 2001 From: Maciel Sousa Date: Tue, 25 Dec 2012 18:25:23 -0200 Subject: [PATCH 06/12] Update system/view.php --- system/view.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/system/view.php b/system/view.php index 1548f6a..5a920f4 100644 --- a/system/view.php +++ b/system/view.php @@ -5,18 +5,15 @@ class View { private $pageVars = array(); private $template; - public function __construct($template) - { - $this->template = APP_DIR .'views/'. $template .'.php'; + public function __construct($template){ + $this->template = APP_DIR . 'views' . DS . $template .'.php'; } - public function set($var, $val) - { + public function set($var, $val){ $this->pageVars[$var] = $val; } - public function render() - { + public function render(){ extract($this->pageVars); ob_start(); @@ -26,4 +23,4 @@ public function render() } -?> \ No newline at end of file +?> From b51a7a7c109111f45630604960215e0762db323d Mon Sep 17 00:00:00 2001 From: Maciel Sousa Date: Tue, 25 Dec 2012 18:26:03 -0200 Subject: [PATCH 07/12] Update system/controller.php --- system/controller.php | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/system/controller.php b/system/controller.php index dc51490..0000807 100644 --- a/system/controller.php +++ b/system/controller.php @@ -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; + } + } } -?> \ No newline at end of file +?> From 7c30661041fdd62f372edfe567beffd8a8a46a1b Mon Sep 17 00:00:00 2001 From: Maciel Sousa Date: Tue, 25 Dec 2012 18:26:49 -0200 Subject: [PATCH 08/12] Update application/config/config.php --- application/config/config.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/config/config.php b/application/config/config.php index d34579b..23c49dd 100644 --- a/application/config/config.php +++ b/application/config/config.php @@ -1,6 +1,6 @@ \ No newline at end of file +?> From fdfb32882345e87526130b8d0e10884c450cf5ca Mon Sep 17 00:00:00 2001 From: Maciel Sousa Date: Tue, 25 Dec 2012 18:27:22 -0200 Subject: [PATCH 09/12] Update application/controllers/error.php --- application/controllers/error.php | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/application/controllers/error.php b/application/controllers/error.php index 99d72f7..1993770 100644 --- a/application/controllers/error.php +++ b/application/controllers/error.php @@ -2,15 +2,30 @@ class Error extends Controller { - function index() - { - $this->error404(); + function index($segments){ + return $this->error($this->getMessage($segments)); } - - function error404() - { - echo '

404 Error

'; - echo '

Looks like this page doesn\'t exist

'; + + 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("

{$segments}

"); + } + } } From 6b4a28f71f2537d132aa04dfdab9208dd376ebff Mon Sep 17 00:00:00 2001 From: Maciel Sousa Date: Tue, 25 Dec 2012 18:28:29 -0200 Subject: [PATCH 10/12] Update application/models/example_model.php --- application/models/example_model.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/application/models/example_model.php b/application/models/example_model.php index 18c960a..5f1b0ad 100644 --- a/application/models/example_model.php +++ b/application/models/example_model.php @@ -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; } From b06cc0be35a08990a16d745cb4e0041666e8f4d7 Mon Sep 17 00:00:00 2001 From: Maciel Sousa Date: Tue, 25 Dec 2012 18:32:19 -0200 Subject: [PATCH 11/12] Update system/model.php --- system/model.php | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/system/model.php b/system/model.php index bf996e5..8ad624e 100644 --- a/system/model.php +++ b/system/model.php @@ -11,12 +11,23 @@ public function __construct(){ $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('success'=>true, 'data'=> $this->connection); + return array(true, $this->connection); } catch(PDOException $e){ - return array('success'=>false, 'error'=> $e->getMessage()); + return array(false, $e->getMessage()); } } + + //deprecated + public function escapeString($string){ + return mysql_real_escape_string($string); + } + + //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){ return !!$val; @@ -39,10 +50,10 @@ public function query($qry, $params=array()){ $pdo = $this->prepare($qry); $pdo->execute($params); - return array('success'=>true, 'data'=> $pdo->fetchAll(PDO::FETCH_OBJ)); + return array(true, $pdo->fetchAll(PDO::FETCH_OBJ)); } catch(PDOException $e){ - return array('success'=>false, 'error'=> $e->getMessage()); + return array(false, $e->getMessage()); } } @@ -51,10 +62,10 @@ public function execute($qry, $params=array()){ try{ $pdo = $this->prepare($qry); $pdo->execute($params); - return array('success'=>true, 'data'=> $pdo->rowCount()); + return array(true, $pdo->rowCount()); } catch(PDOException $e){ - return array('success'=>false, 'error'=> $e->getMessage()); + return array(false, $e->getMessage()); } } From 58e19a6b909a39beb6b91e77c5e50b8fb8e5fbd9 Mon Sep 17 00:00:00 2001 From: Maciel Sousa Date: Tue, 25 Dec 2012 18:33:05 -0200 Subject: [PATCH 12/12] Update application/controllers/main.php --- application/controllers/main.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/controllers/main.php b/application/controllers/main.php index f39b256..ace01bc 100644 --- a/application/controllers/main.php +++ b/application/controllers/main.php @@ -16,7 +16,7 @@ function categoryAjax(){ $this->render(array('data'=>$response[1])); } else{ - $this->render(array('data'=>array(), 'error'=> $response[1]); + $this->render(array('data'=>array(), 'error'=> $response[1])); } }