/** * Renders an error template for PHP errors and Exceptions */ public function _onException($e) { $response = new Response(); $error = $this->_filterError($e); $data = ["status" => $this->_status, "title" => $this->_status . ": " . $error["type"], "description" => "There has been a problem of type (" . $error["type"] . ").", "address" => Server::getUrl(), "method" => Connection::getMethod(), "date" => date("F jS Y h:i:s A T"), "headers" => getallheaders(), "error" => $error]; // send error data to log file $this->log($error["type"] . ": " . $error["message"] . " in " . $error["file"] . " on line " . $error["line"] . "."); // send http reposnse if (Connection::isMethod("GET")) { // template has been set... if (!empty($this->_template) && is_file($this->_template)) { // send all data to template to be rendered as needed $response->sendTemplate($this->_status, $this->_template, $data); } // no template, send just error description for security reasons $response->sendText($this->_status, $data["description"]); } // non-GET, send error description data as JSON $response->sendJson($this->_status, ["status" => $this->_status, "error" => $data["description"]]); }
/** * Get current request URL */ public function getUrl() { return Server::getUrl(); }
/** * Send redirect response */ public function redirect($location = '', $code = 302, $delay = 1) { $current = Server::getUrl(); $location = Sanitize::toUrl($location); $path1 = Sanitize::toPath(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)); $path2 = Sanitize::toPath(parse_url($location, PHP_URL_PATH)); $code = is_numeric($code) ? intval($code) : 302; if (Validate::isExternal($location) || $path1 !== $path2) { $this->flushHeaders(); $this->flushContents(); $this->setText($code, ''); $this->setHeader('Location', $location, true); $this->setHeader('Connection', 'close', true); $this->send($delay); } throw new Exception('Redirect aborted, from (' . $current . ') to (' . $location . ').'); }
/** * Send a rendered template file HTML message */ public function sendTemplate($file = '', $data = array()) { if (!empty($file) && is_file($file)) { $tpl_base = dirname($file); $tpl_file = '/' . basename($file); $view = new View(); $view->setPlublicPath($tpl_base); $view->addRenderPath($tpl_base); $view->setTemplate($tpl_file); $view->set('url', Server::getUrl()); $view->set('ip', Client::getIp()); $view->set('browser', Client::getAgent()); $view->set('date', date('l jS \\of F Y h:i A T')); return $this->sendHtml($view->render()); } return false; }
/** * Helper: Get full page url with param */ public function currentUrl() { return Server::getUrl(); }
/** * Builds and returns error output data for views */ private function _getOutputData($full = true) { $output = array('status' => $this->_status_code, 'info' => $this->_error_type, 'error' => $this->_error_message, 'file' => $this->_relativePath($this->_error_file), 'line' => $this->_error_line, 'date' => date('r'), 'url' => Server::getUrl(), 'host' => Server::getHost(), 'domain' => Server::getDomain(), 'memory' => Numeric::toSize(memory_get_peak_usage(true)), 'speed' => $this->_getRuntimeSpeed()); if ($full === true) { $output['headers'] = getallheaders(); if (!empty($this->_error_backtrace)) { $output['trace'] = $this->_getBacktrace(); } if (!empty($this->_error_file) && !empty($this->_error_line)) { $output['source'] = $this->_getSourceCode($this->_error_file, $this->_error_line); } } return $output; }
/** * Redirect to another URL or path safely */ public function redirect($location) { // build possible versions of current route $cur_file = Server::getScriptFile(); $cur_address = Server::getUrl(); $cur_clean = str_replace("/" . basename($cur_file), "", $cur_address); // convert route path to full URL address if (preg_match("/^\\/{1}.*\$/ui", $location)) { $location = Server::getScriptUrl($location); } // new location matches current url/route if ($location === $cur_address || $location === $cur_clean) { $this->sendDefault(500, "Possible redirect loop detected for new location (" . $location . ")."); } // go for it $this->setStatus(302); $this->setHeader("Location", $location); $this->setHeader("Connection", "close"); $this->send(); }
/** * Send a rendered template file HTML message */ public function sendTemplate($file, $data = []) { $view = new View(); $view->setTemplate($file); $view->setKey("url", Server::getUrl()); $view->setKey("baseurl", Server::getBaseUrl()); $view->setKey("browser", Connection::getAgent()); $view->setKey("ip", Connection::getIp()); $view->setKey("date", date("r T")); $view->mergeData($data); return $this->sendHtml($view->render()); }