Example #1
0
 /**
  * Construct the object
  * 
  * @param string $uri
  */
 public function __construct($uri, array $config = null)
 {
     parent::__construct($uri, $config);
     // first, check the URI for duplicate slashes - they are not allowed
     // if you must pass duplicate slashes in URL, then urlencode them
     if (strpos($uri, '//') !== false) {
         header('Location: ' . str_replace('//', '/', $uri));
         exit(0);
     }
     $slash = DS;
     // There are two possible scenarios:
     // 1. The first part of URL leads to the module controller
     // 2. The first part of URL leads to the default controller
     if ($this->uri[1] == '') {
         $this->controllerUrl = 'index';
         $this->controllerClass = 'IndexController';
     } else {
         $this->controllerUrl = strtolower($this->uri[1]);
         $this->controllerClass = str_replace(' ', '', ucwords(str_replace(array('-', '.'), ' ', $this->controllerUrl))) . 'Controller';
     }
     // Now we have the controller class name detected, but, should it be
     // taken from module or from default controllers?
     $moduleDir = Application::getApplicationPath("modules{$slash}{$this->controllerUrl}");
     if (is_dir($moduleDir)) {
         // ok, it is a module with module/controller/action path
         $moduleUrl = $this->controllerUrl;
         $this->moduleUrl = $moduleUrl;
         if (isset($this->uri[2]) && $this->uri[2] != '') {
             $this->controllerUrl = strtolower($this->uri[2]);
             $this->controllerClass = str_replace(' ', '', ucwords(str_replace(array('-', '.'), ' ', $this->controllerUrl))) . 'Controller';
         } else {
             $this->controllerUrl = 'index';
             $this->controllerClass = 'IndexController';
         }
         $this->controllerPath = "{$moduleDir}{$slash}controllers{$slash}{$this->controllerClass}.php";
         $mainControllerExists = true;
         if (!is_file($this->controllerPath)) {
             // lets try with default controller when requested one is not here
             $this->controllerPath = Application::getApplicationPath("modules{$slash}{$moduleUrl}{$slash}controllers{$slash}IndexController.php");
             if (!is_file($this->controllerPath)) {
                 // Even IndexController is missing. Can not resolve that.
                 Log::notice("Can not find {$this->controllerClass} nor IndexController in {$moduleDir}{$slash}controllers");
                 Application::error(404, 'Page not found');
             }
             $mainControllerExists = false;
             $this->controllerClass = 'IndexController';
         }
         if ($mainControllerExists) {
             if (!isset($this->uri[3]) || $this->uri[3] == '') {
                 $this->actionUrl = 'index';
                 $this->actionMethod = 'index';
             } else {
                 $this->actionUrl = strtolower($this->uri[3]);
                 $this->actionMethod = ucwords(str_replace(array('-', '.'), ' ', $this->actionUrl));
                 $this->actionMethod = str_replace(' ', '', $this->actionMethod);
                 $this->actionMethod = strtolower(substr($this->actionMethod, 0, 1)) . substr($this->actionMethod, 1);
             }
         } else {
             if (isset($this->uri[2]) && $this->uri[2] != '') {
                 $this->actionUrl = strtolower($this->uri[2]);
                 $this->actionMethod = ucwords(str_replace(array('-', '.'), ' ', $this->actionUrl));
                 $this->actionMethod = str_replace(' ', '', $this->actionMethod);
                 $this->actionMethod = strtolower(substr($this->actionMethod, 0, 1)) . substr($this->actionMethod, 1);
             } else {
                 $this->actionUrl = 'index';
                 $this->actionMethod = 'index';
             }
         }
         // and now, configure the include paths according to the case
         $basePath = Application::getApplicationPath();
         $includePath = array("{$moduleDir}{$slash}controllers", "{$moduleDir}{$slash}modles", "{$moduleDir}{$slash}library", get_include_path());
         set_include_path(implode(PATH_SEPARATOR, $includePath));
     } else {
         // ok, it is the default controller/action
         $this->controllerPath = Application::getApplicationPath("controllers{$slash}{$this->controllerClass}.php");
         $mainControllerExists = true;
         if (!is_file($this->controllerPath)) {
             $this->controllerPath = Application::getApplicationPath("controllers{$slash}IndexController.php");
             if (!is_file($this->controllerPath)) {
                 // Even IndexController is missing. Can not resolve that.
                 Log::notice("Can not find {$this->controllerClass} nor IndexController in " . Application::getApplicationPath('controllers'));
                 Application::error(404, 'Page not found');
             }
             $mainControllerExists = false;
             $this->controllerClass = 'IndexController';
         }
         if ($mainControllerExists) {
             if (!isset($this->uri[2]) || $this->uri[2] == '') {
                 $this->actionUrl = 'index';
                 $this->actionMethod = 'index';
             } else {
                 $this->actionUrl = strtolower($this->uri[2]);
                 $this->actionMethod = ucwords(str_replace(array('-', '.'), ' ', $this->actionUrl));
                 $this->actionMethod = str_replace(' ', '', $this->actionMethod);
                 $this->actionMethod = strtolower(substr($this->actionMethod, 0, 1)) . substr($this->actionMethod, 1);
             }
         } else {
             $this->actionUrl = strtolower($this->uri[1]);
             $this->actionMethod = ucwords(str_replace(array('-', '.'), ' ', $this->actionUrl));
             $this->actionMethod = str_replace(' ', '', $this->actionMethod);
             $this->actionMethod = strtolower(substr($this->actionMethod, 0, 1)) . substr($this->actionMethod, 1);
         }
         // and now, configure the include paths according to the case
         $basePath = Application::getApplicationPath();
         Application::addIncludePath(array($basePath . 'controllers', $basePath . 'models', $basePath . 'library'));
     }
     $this->isAjax = isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST' && isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' || isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false;
     $controllerClassName = $this->getControllerClass();
     $controllerInstance = new $controllerClassName();
     $alwaysRestful = isset($this->config['always_restful']) && $this->config['always_restful'] === true;
     if ($alwaysRestful || property_exists($controllerInstance, 'restful') && $controllerInstance::$restful === true) {
         // it is restful
         $this->actionMethod = strtolower($_SERVER['REQUEST_METHOD']) . ucfirst($this->actionMethod);
     } else {
         if ($this->isAjax) {
             $this->actionMethod .= 'Ajax';
         } else {
             $this->actionMethod .= 'Action';
         }
     }
     $this->controllerInstance = $controllerInstance;
 }
Example #2
0
 /**
  * Run the application with given URI. If URI is not set, then application
  * will try to detect it automatically.
  * 
  * @param string $uri [optional]
  */
 public static function run($uri = null)
 {
     static::$requestStartTime = isset($_SERVER['REQUEST_TIME_FLOAT']) ? $_SERVER['REQUEST_TIME_FLOAT'] : microtime(true);
     $config = static::getConfig();
     $isCLI = defined('KOLDY_CLI') && KOLDY_CLI === true;
     if (!$isCLI) {
         // this is normal HTTP request that came from Web Server, so we'll handle it
         if ($uri === null && isset($_SERVER['REQUEST_URI'])) {
             static::$uri = $uri = $_SERVER['REQUEST_URI'];
             $questionPos = strpos($uri, '?');
             if ($questionPos !== false) {
                 $uri = substr($uri, 0, $questionPos);
             }
         } else {
             if ($uri === null) {
                 // if your script goes here, then something is really f****d up on your server
                 throw new Exception('URI doesn\'t exists');
             }
         }
         try {
             static::init();
             $routingClassName = $config['routing_class'];
             $routeOptions = isset($config['routing_options']) ? $config['routing_options'] : null;
             static::$routing = new $routingClassName($uri, $routeOptions);
             $response = static::$routing->exec();
             if ($response instanceof Response) {
                 print $response->flush();
             } else {
                 if (is_array($response)) {
                     $className = static::$routing->getControllerClass();
                     $actionName = static::$routing->getActionMethod();
                     throw new Exception("Invalid return value from {$className}->{$actionName}; got array instead of something else");
                 } else {
                     print $response;
                 }
             }
         } catch (\Exception $e) {
             // something threw up
             $route = static::route();
             if ($route === null) {
                 // damn, even route class wasn't initialized
                 header('HTTP/1.1 503 Service Temporarily Unavailable', true, 503);
                 header('Status: 503 Service Temporarily Unavailable');
                 header('Retry-After: 300');
                 // 300 seconds / 5 minutes
                 if (static::inDevelopment()) {
                     print "<p>{$e->getMessage()}</p>";
                     print "<pre>{$e->getTraceAsString()}</pre>";
                 } else {
                     print '<p>Something went really wrong. Please try again later.</p>';
                 }
             } else {
                 // otherwise, route should handle exception
                 // because of this, you can customize almost everything
                 static::route()->handleException($e);
             }
         }
     } else {
         // and this is case when you're dealing with CLI request
         // scripts are stored in /application/scripts, but before that, we need to determin which script is called
         global $argv;
         // $argv[0] - this should be "cli.php", but we don't need this at all
         static::init();
         try {
             // so, if you run your script as "php cli.php backup", you'll have only two elements
             // in the future, we might handle different number of parameters, but until that, we won't
             // you can also call script in module using standard colon as separator
             // example: php cli.php user:backup   -> where "user" is module and "backup" is script name
             $script = $argv[1];
             // this should be the second parameter
             static::$cliName = $script;
             if (preg_match('/^([a-zA-Z0-9\\_\\-\\:]+)$/', $script)) {
                 if (strpos($script, ':') !== false) {
                     // script name has colon - it means that the script needs to be looked for in modules
                     $tmp = explode(':', $script);
                     static::$cliScript = static::getApplicationPath("modules/{$tmp[0]}/scripts/{$tmp[1]}.php");
                     if (is_dir(static::getApplicationPath("modules/{$tmp[0]}"))) {
                         static::registerModule($tmp[0]);
                     }
                 } else {
                     static::$cliScript = static::getApplicationPath("scripts/{$script}.php");
                 }
                 if (!is_file(static::$cliScript)) {
                     throw new Exception('CLI script doesn\'t exist on ' . static::$cliScript);
                 } else {
                     include static::$cliScript;
                 }
             } else {
                 throw new Exception("Invalid script name: {$script}");
             }
         } catch (\Exception $e) {
             echo "{$e->getMessage()} in {$e->getFile()}:{$e->getLine()}\n\n{$e->getTraceAsString()}";
             Log::exception($e);
         }
     }
 }
Example #3
0
 /**
  * Construct the object
  * @param string $uri
  */
 public function __construct($uri, array $config = null)
 {
     if ($config === null || count($config) == 0 || !isset($config['module_param']) || !isset($config['controller_param']) || !isset($config['action_param'])) {
         throw new Exception('Route config options are missing');
     }
     parent::__construct($uri, $config);
     if (isset($_GET[$config['controller_param']])) {
         $c = trim($_GET[$config['controller_param']]);
         if ($c == '') {
             $this->controllerUrl = 'index';
             $this->controllerClass = 'IndexController';
         } else {
             $this->controllerUrl = strtolower($c);
             $this->controllerClass = str_replace(' ', '', ucwords(str_replace(array('-', '.'), ' ', $this->controllerUrl))) . 'Controller';
         }
     } else {
         $this->controllerUrl = 'index';
         $this->controllerClass = 'IndexController';
     }
     // Now we have the controller class name detected, but, should it be
     // taken from module or from default controllers?
     // TODO: Zavrsiti ovo!
     $moduleDir = Application::getApplicationPath() . 'modules' . DS . $this->controllerUrl;
     if (is_dir($moduleDir)) {
         // ok, it is a module with module/controller/action path
         $moduleUrl = $this->controllerUrl;
         $this->moduleUrl = $moduleUrl;
         $a = isset($_GET[$config['action_param']]) ? trim($_GET[$config['action_param']]) : '';
         if ($a != '') {
             $this->controllerUrl = strtolower($a);
             $this->controllerClass = str_replace(' ', '', ucwords(str_replace(array('-', '.'), ' ', $this->controllerUrl))) . 'Controller';
         } else {
             $this->controllerUrl = 'index';
             $this->controllerClass = 'IndexController';
         }
         $this->controllerPath = $moduleDir . DS . 'controllers' . DS . $this->controllerClass . '.php';
         $mainControllerExists = true;
         if (!is_file($this->controllerPath)) {
             $this->controllerPath = Application::getApplicationPath() . 'modules' . DS . $moduleUrl . DS . 'controllers' . DS . 'IndexController.php';
             if (!is_file($this->controllerPath)) {
                 // Even IndexController is missing. Can not resolve that.
                 if (Application::inDevelopment()) {
                     $controllersPath = $moduleDir . DS . 'controllers';
                     \Koldy\Log::debug("Can not find {$this->controllerClass} nor IndexController in {$controllersPath}");
                 }
                 Application::error(404, 'Page not found');
             }
             $mainControllerExists = false;
             $this->controllerClass = 'IndexController';
         }
         if ($mainControllerExists) {
             if (!isset($this->uri[3]) || $this->uri[3] == '') {
                 $this->actionUrl = 'index';
                 $this->actionMethod = 'index';
             } else {
                 $this->actionUrl = strtolower($this->uri[3]);
                 $this->actionMethod = ucwords(str_replace(array('-', '.'), ' ', $this->actionUrl));
                 $this->actionMethod = str_replace(' ', '', $this->actionMethod);
                 $this->actionMethod = strtolower(substr($this->actionMethod, 0, 1)) . substr($this->actionMethod, 1);
             }
         } else {
             if (isset($this->uri[2]) && $this->uri[2] != '') {
                 $this->actionUrl = strtolower($this->uri[2]);
                 $this->actionMethod = ucwords(str_replace(array('-', '.'), ' ', $this->actionUrl));
                 $this->actionMethod = str_replace(' ', '', $this->actionMethod);
                 $this->actionMethod = strtolower(substr($this->actionMethod, 0, 1)) . substr($this->actionMethod, 1);
             } else {
                 $this->actionUrl = 'index';
                 $this->actionMethod = 'index';
             }
         }
         // and now, configure the include paths according to the case
         Application::addIncludePath(array($moduleDir . DS . 'controllers' . DS, $moduleDir . DS . 'models' . DS, $moduleDir . DS . 'library' . DS, Application::getApplicationPath() . 'controllers' . DS, Application::getApplicationPath() . 'models' . DS, Application::getApplicationPath() . 'library' . DS));
     } else {
         // ok, it is the default controller/action
         $this->controllerPath = Application::getApplicationPath() . 'controllers' . DS . $this->controllerClass . '.php';
         $mainControllerExists = true;
         if (!is_file($this->controllerPath)) {
             $this->controllerPath = Application::getApplicationPath() . 'controllers' . DS . 'IndexController.php';
             if (!is_file($this->controllerPath)) {
                 // Even IndexController is missing. Can not resolve that.
                 if (Application::inDevelopment()) {
                     $controllersPath = Application::getApplicationPath() . 'controllers';
                     \Koldy\Log::debug("Can not find {$this->controllerClass} nor IndexController in {$controllersPath}");
                 }
                 Application::error(404, 'Page not found');
             }
             $mainControllerExists = false;
             $this->controllerClass = 'IndexController';
         }
         if ($mainControllerExists) {
             if (!isset($this->uri[2]) || $this->uri[2] == '') {
                 $this->actionUrl = 'index';
                 $this->actionMethod = 'index';
             } else {
                 $this->actionUrl = strtolower($this->uri[2]);
                 $this->actionMethod = ucwords(str_replace(array('-', '.'), ' ', $this->actionUrl));
                 $this->actionMethod = str_replace(' ', '', $this->actionMethod);
                 $this->actionMethod = strtolower(substr($this->actionMethod, 0, 1)) . substr($this->actionMethod, 1);
             }
         } else {
             $this->actionUrl = strtolower($this->uri[1]);
             $this->actionMethod = ucwords(str_replace(array('-', '.'), ' ', $this->actionUrl));
             $this->actionMethod = str_replace(' ', '', $this->actionMethod);
             $this->actionMethod = strtolower(substr($this->actionMethod, 0, 1)) . substr($this->actionMethod, 1);
         }
         // and now, configure the include paths according to the case
         Application::addIncludePath(array(Application::getApplicationPath() . 'controllers' . DS, Application::getApplicationPath() . 'models' . DS, Application::getApplicationPath() . 'library' . DS));
     }
     $this->isAjax = isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST' && isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' || isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false;
     if ($this->isAjax) {
         $this->actionMethod .= 'Ajax';
     } else {
         $this->actionMethod .= 'Action';
     }
 }