Permalink
Browse files

Adding JSON to the url automatically creates the output in JSON

  • Loading branch information...
1 parent 684bcff commit 5002c41ecec3cbcc74ed48a3171ec06c6b48fb96 Sudhanshu Raheja committed Oct 27, 2011
@@ -30,47 +30,30 @@ public function getSubDomain() {
return false;
}
- public function getP1() {
+ private function getP($i) {
$url = $this->getURL();
- $p1 = isset($url[0]) ? mysql_real_escape_string($url[0], $this->getDb()->getConnection()) : false;
- return $p1;
- }
- public function getP2() {
- $url = $this->getURL();
- $p2 = isset($url[1]) ? mysql_real_escape_string($url[1], $this->getDb()->getConnection()) : false;
- return $p2;
- }
-
- public function getP3() {
- $url = $this->getURL();
- $p3 = isset($url[2]) ? mysql_real_escape_string($url[2], $this->getDb()->getConnection()) : false;
- return $p3;
- }
-
- public function getP4() {
- $url = $this->getURL();
- $p4 = isset($url[3]) ? mysql_real_escape_string($url[3], $this->getDb()->getConnection()) : false;
- return $p4;
- }
+ $output = '';
+ if( $this->getDb()->getConnection() !== false ) {
+ $output = isset($url[$i]) ? mysql_real_escape_string($url[$i], $this->getDb()->getConnection()) : false;
+ } else {
+ $output = isset($url[$i]) ? $url[$i] : false;
+ }
- public function getP5() {
- $url = $this->getURL();
- $p4 = isset($url[4]) ? mysql_real_escape_string($url[4], $this->getDb()->getConnection()) : false;
- return $p4;
- }
+ $output = $this->getGeneratrix()->removeRequestType($output);
- public function getP6() {
- $url = $this->getURL();
- $p4 = isset($url[5]) ? mysql_real_escape_string($url[5], $this->getDb()->getConnection()) : false;
- return $p4;
+ return $output;
}
- public function getP7() {
- $url = $this->getURL();
- $p4 = isset($url[6]) ? mysql_real_escape_string($url[6], $this->getDb()->getConnection()) : false;
- return $p4;
- }
+ public function getP1() { return $this->getP(0); }
+ public function getP2() { return $this->getP(1); }
+ public function getP3() { return $this->getP(2); }
+ public function getP4() { return $this->getP(3); }
+ public function getP5() { return $this->getP(4); }
+ public function getP6() { return $this->getP(5); }
+ public function getP7() { return $this->getP(6); }
+ public function getP8() { return $this->getP(7); }
+ public function getP9() { return $this->getP(8); }
public function getUploadedFile($tag, $where = '/app/cache/uploads/', $allowed = '') {
$return = array('error' => false, 'path' => false, 'name' => false);
@@ -198,9 +181,9 @@ public function getSessionValue($tag_name) {
return checkArray($_SESSION, $tag_name) ? mysql_real_escape_string($_SESSION[$tag_name], $this->getDb()->getConnection()) : false;
}
- public function getGetValue($tag_name) {
- return checkArray($_GET, $tag_name) ? mysql_real_escape_string($_GET[$tag_name], $this->getDb()->getConnection()) : false;
- }
+ public function getGetValue($tag_name) {
+ return checkArray($_GET, $tag_name) ? mysql_real_escape_string($_GET[$tag_name], $this->getDb()->getConnection()) : false;
+ }
public function getURL() {
return $this->getGeneratrix()->getRequestArray();
@@ -80,11 +80,15 @@ private function connect() {
// TODO : Connect to multiple databases
if(!$this->connection) {
// Connect to the database
- if(!$this->connection = mysql_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PASS))
+ if(!$this->connection = mysql_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PASS)) {
$this->setError();
+ $this->connection = false;
+ }
// Select the database
- if(!mysql_select_db(DATABASE_NAME))
+ if(!mysql_select_db(DATABASE_NAME)) {
$this->setError();
+ $this->connection = false;
+ }
}
}
@@ -18,6 +18,7 @@ class Generatrix {
private $controller;
private $method;
+ private $request_type;
private $mail;
private $file;
@@ -120,10 +121,10 @@ private function bootstrap($argv) {
private function handleRequest() {
// We have got the url value from .htaccess, use it to find which page is to be displayed
- $details = $this->getControllerAndMethod();
- $controller_class = $details['controller'] . 'Controller';
- $view_class = $details['controller'] . 'View';
- $controller_method = $details['method'];
+ $this->getControllerAndMethod();
+ $controller_class = $this->controller . 'Controller';
+ $view_class = $this->controller . 'View';
+ $controller_method = $this->method;
// Check if the page is available in the cache
$found_cached_page = $this->checkCache();
@@ -155,70 +156,117 @@ private function handleRequest() {
// Create the header etc
$view->startPage($controller->isControllerHtml());
// Get the final page to be displayed
- if(version_compare(PHP_VERSION, '5.2.0') >= 0) {
- $view->$controller_method();
- if($controller->isControllerHtml()) {
- $final_page .= $view->endPage();
+
+ if( in_array($this->request_type, array('json') ) ) {
+
+ $view_variables = $view->getAllVariables();
+ if($view_variables == '') {
+ $view_variables = array();
}
+
+ showJSON($view_variables);
+
} else {
- $view->$controller_method();
- $html_object = $view->endPage();
- if ( is_object($html_object) && $controller->isControllerHtml() ) {
- $final_page .= $html_object->_toString();
+
+ if(version_compare(PHP_VERSION, '5.2.0') >= 0) {
+ $view->$controller_method();
+ if($controller->isControllerHtml()) {
+ $final_page .= $view->endPage();
+ }
+ } else {
+ $view->$controller_method();
+ $html_object = $view->endPage();
+ if ( is_object($html_object) && $controller->isControllerHtml() ) {
+ $final_page .= $html_object->_toString();
+ }
+ }
+
+ if(!$this->cli->isEnabled()) {
+ echo $final_page;
}
- }
- if(!$this->cli->isEnabled())
- echo $final_page;
+ }
} else {
- display_404('The method <strong>"'. $controller_method . '"</strong> in class <strong>"'. $view_class .'"</strong> does not exist');
+ if( $this->request_type == 'json' ) {
+ display_404_json('The method <strong>"'. $controller_method . '"</strong> in class <strong>"'. $view_class .'"</strong> does not exist');
+ } else {
+ display_404('The method <strong>"'. $controller_method . '"</strong> in class <strong>"'. $view_class .'"</strong> does not exist');
+ }
}
} else {
- display_404('The class <strong>"'. $view_class . '"</strong> does not exist');
+ if( $this->request_type == 'json' ) {
+ display_404_json('The class <strong>"'. $view_class . '"</strong> does not exist');
+ } else {
+ display_404('The class <strong>"'. $view_class . '"</strong> does not exist');
+ }
}
} else {
- display_404('The method <strong>"'. $controller_method . '"</strong> in class <strong>"'. $controller_class .'"</strong> does not exist');
+ if( $this->request_type == 'json' ) {
+ display_404_json('The method <strong>"'. $controller_method . '"</strong> in class <strong>"'. $controller_class .'"</strong> does not exist');
+ } else {
+ display_404('The method <strong>"'. $controller_method . '"</strong> in class <strong>"'. $controller_class .'"</strong> does not exist');
+ }
}
} else {
- //if(!$this->handleCatchAllRequest())
+ if( $this->request_type == 'json' ) {
+ display_404_json('The class <strong>"' . $controller_class .'"</strong> does not exist');
+ } else {
display_404('The class <strong>"' . $controller_class .'"</strong> does not exist');
+ }
}
}
private function getControllerAndMethod() {
// Parse the values obtained from the url (obtained from .htaccess) to get the controller and view
- $details = array();
-
if(USE_CATCH_ALL) {
require_once(path('/app/settings/mapping.php'));
$request = array();
- if($this->cli->isEnabled())
+ if($this->cli->isEnabled()) {
$request = $this->getCliArray();
- else
+ } else {
$request = $this->getRequestArray();
+ }
+
+ $last_element = '';
+ for($i = 9; $i >= 0; $i--) {
+ if( ($last_element == '') && isset($request[$i]) && ($request[$i] != '') ) {
+ $last_element = $request[$i];
+ }
+ }
+
+ $dots = explode('.', $last_element);
+
+ $type = '';
+ if( count($dots) > 1)
+ $type = $dots[count($dots) - 1];
$details = mapping($request);
+ if( _g($details, 'controller') ) $this->controller = _g($details, 'controller');
+ if( _g($details, 'method') ) $this->method = _g($details, 'method');
- if(!checkArray($details, 'controller')) {
- $details['controller'] = (isset($request[0]) && ($request[0] != '')) ? $request[0] : DEFAULT_CONTROLLER;
+ $this->request_type = $type;
+
+ if( $this->controller == '' ) {
+ $this->controller = (isset($request[0]) && ($request[0] != '')) ? $request[0] : DEFAULT_CONTROLLER;
}
- if(!checkArray($details, 'method')) {
- $details['method'] = (isset($request[1]) && ($request[1] != '')) ? $request[1] : 'base';
+ if( $this->method == '' ) {
+ $this->method = (isset($request[1]) && ($request[1] != '')) ? $request[1] : 'base';
}
// Do not destroy the generatrix controller
$c_id = ($this->cli->isEnabled()) ? 1 : 0;
if(isset($request[$c_id]) && ($request[$c_id] == 'generatrix')) {
- $details['controller'] = $request[$c_id];
+ $this->controller = $request[$c_id];
$c_id++;
if(isset($request[$c_id]) && ($request[$c_id] != '')) {
- $details['method'] = $request[$c_id];
+ $this->method = $request[$c_id];
} else {
- $details['method'] = 'base';
+ $this->method = 'base';
}
}
+
} else {
// If no controller or method is defined, we need to use the DEFAULT_CONTROLLER (defined in app/settings/config.php)
// If cli is enabled, we use the format site.com/index.php controller function
@@ -228,35 +276,52 @@ private function getControllerAndMethod() {
header('HTTP/1.1 301 Moved Permanently');
location('/' . DEFAULT_CONTROLLER);
}
- $details['controller'] = $this->cli->getValue('controller') == "" ? DEFAULT_CONTROLLER : $this->cli->getValue('controller');
- $details['method'] = $this->cli->getValue('method') == "" ? 'base' : $this->cli->getValue('method');
+ $this->controller = $this->cli->getValue('controller') == "" ? DEFAULT_CONTROLLER : $this->cli->getValue('controller');
+ $this->method = $this->cli->getValue('method') == "" ? 'base' : $this->cli->getValue('method');
+
+ $type = '';
+ $ARGV = _g($_SERVER, 'argv');
+ if(isset($ARGV[0])) {
+ $last_element = $ARGV[ count($ARGV) - 1];
+ $dots = explode('.', $last_element);
+
+ if( count($dots) > 1 )
+ $type = $dots[count($dots) - 1];
+ $type = $type;
+ }
+ $this->request_type = $type;
} else {
// If this request is coming from the browser, we need to get the value from url (obtained from .htaccess)
if($this->request->getValue('controller') == "") {
header('HTTP/1.1 301 Moved Permanently');
location('/' . DEFAULT_CONTROLLER);
}
- $details['controller'] = $this->request->getValue('controller') == "" ? DEFAULT_CONTROLLER : $this->request->getValue('controller');
- $details['method'] = $this->request->getValue('method') == "" ? 'base' : $this->request->getValue('method');
+ $this->controller = $this->request->getValue('controller') == "" ? DEFAULT_CONTROLLER : $this->request->getValue('controller');
+ $this->method = $this->request->getValue('method') == "" ? 'base' : $this->request->getValue('method');
+
+ $URL = _g($_SERVER, 'REQUEST_URI');
+ $slashes = explode('/', $URL);
+ // Take the last slash and explode on .
+ $dots = explode('.', $slashes[count($slashes) - 1]);
+
+ $type = '';
+ if( count($dots) > 1 )
+ $type = $dots[count($dots) - 1];
+ $this->request_type = $type;
}
- // TODO : Add customHandlers
- // We need to set the $controller and $method for the generatrix class
- $this->controller = (function_exists('customHandlers')) ? customHandlers($details, 'controller') : $details['controller'];
- $this->method = (function_exists('customHandlers')) ? customHandlers($details, 'method') : $details['method'];
- // set the controller and method again (depending on the customHandlers)
- $details['controller'] = $this->controller;
- $details['method'] = $this->method;
}
// check for cache.manifest
- if($details['controller'] == 'cache.manifest') {
+ /* if($details['controller'] == 'cache.manifest') {
$details['controller'] = 'cacheManifest';
$details['method'] = 'base';
- }
+ } */
- return $details;
+ $this->controller = $this->removeRequestType($this->controller);
+ $this->method = $this->removeRequestType($this->method);
+ //return $details;
}
private function requireFiles() {
@@ -300,6 +365,13 @@ public function getRequest() {
public function getMemoryFootprint($message) {
display($message . ' Usage: ' . memory_get_usage(true) . ' Peak: ' . memory_get_peak_usage(true));
}
+
+ public function removeRequestType($string) {
+ if( strpos($string, '.' . $this->request_type) !== false ) {
+ $string = str_replace('.' . $this->request_type, '', $string);
+ }
+ return $string;
+ }
}
?>
@@ -33,12 +33,42 @@ function display_404 ($message, $file = '', $line = '') {
echo "</div>";
}
}
+ function display_404_json ($message, $file = '', $line = '') {
+ if(isset($_SERVER['argc']) && ($_SERVER['argc'] > 1)) {
+ showJSON("This page does not exist", 'File not found');
+ } else {
+ header("HTTP/1.1 404 Not Found");
+ showJSON("This page does not exist", 'File not found');
+ }
+ }
function add_file_and_line($file, $line) {
$return = (($file != '') || ($line != '')) ? '<br />In file <strong>'. str_replace(DISK_ROOT, '', $file) . '</strong> on line <strong>' . $line . '</strong>' : '';
return $return;
}
+ // Show JSON
+ function showJSON($data, $error = '') {
+ $url = _g($_SERVER, 'REQUEST_URI');
+
+ $has_error = false;
+ if($error != '') {
+ $has_error = true;
+ }
+
+ $variables = array(
+ 'url' => $url,
+ 'error' => $has_error,
+ 'error_description' => $error,
+ 'data' => $data,
+ 'timestamp' => time()
+ );
+
+ header('Content-Type: application/json');
+ echo json_encode( $variables );
+ die();
+ }
+
// This displays an error in php, shows up in red
function display_message($message, $file, $line, $level, $dump = false) {
$using_cli = (isset($_SERVER['argc']) && ($_SERVER['argc'] > 1)) ? true : false;
Oops, something went wrong.

0 comments on commit 5002c41

Please sign in to comment.