public function debugSessionAction() { if (Application::inDevelopment()) { Session::start(); return '<pre>' . print_r($_SESSION, true) . '</pre>'; } else { return Redirect::temporary('/'); } }
</p> <?php if (strlen($message) > 0) { ?> <p> <code><?php echo $message; ?> </code> </p> <?php } ?> <?php if (isset($exception) && class_exists('\\Koldy\\Application') && \Koldy\Application::inDevelopment()) { ?> <pre class="text-danger"><?php echo $exception->getTraceAsString(); ?> </pre> <?php } ?> <p> <a class="btn btn-success btn-lg" role="button" href="/"><span class="glyphicon glyphicon-home"></span> Go home</a> <button class="btn btn-success btn-lg" onclick="window.location.reload()"><span class="glyphicon glyphicon-refresh"></span> Retry</button> </p> </div> </div><!-- /row -->
/** * 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'; } }
/** * I'm sure you sometimes want to show nice error to user! Well, if you call * Application::error(404), then it will end up here. This kind of errors are meant * only to show nice error to user. If you also want to log this or alert anyone that * this happened, then do that before calling Application::error() method. * * @param int $code The HTTP error code * @param string $message Optional message that will be visible to user * @param \Exception $e Optional exception, if any. Be careful, you might not * want to show the exceptions to users, but you would like to show it to developers? Then * use Application::inDevelopment() and inProduction() methods. */ public function error($code, $message = null, \Exception $e = null) { if (!headers_sent()) { switch ($code) { case 400: header('HTTP/1.0 400 Bad Request', true, 400); if ($message === null) { $message = 'Bad request'; } break; case 403: header('HTTP/1.0 403 Forbidden', true, 403); if ($message === null) { $message = 'Forbidden'; } break; case 404: header('HTTP/1.0 404 Not Found', true, 404); if ($message === null) { $message = 'Page Not Found'; } break; case 500: header('HTTP/1.0 500 Internal Server Error', true, 500); if ($message === null) { $message = 'Internal Server Error'; } break; case 503: 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 ($message === null) { $message = 'Service Temporarily Unavailable'; } break; } } if ($message === null) { $message = 'Page Not Found'; } if ($this->isAjax()) { $data = array('success' => false, 'type' => 'http', 'code' => $code, 'message' => $message, 'exception' => $e !== null && Application::inDevelopment() ? $e->getMessage() : null); Json::create($data)->flush(); } else { $path = Application::getPublicPath("{$code}.php"); if (file_exists($path)) { $exception = $e; include $path; } else { // i don't know how to handle this message now!? throw new Exception($message === null ? 'Error ' . $code : $message); } } exit(0); }