/** * Constructor */ public function __construct($name = '', $cryptkey = '') { parent::__construct($cryptkey); $this->setName($name); $this->setPath('/'); $this->setDomain(Server::getHost()); $this->setSecure(Server::isSecure()); }
/** * Resolves an file path for a row */ public function resolveFile($row = array(), $column = '', $default = '') { if (!empty($column) && array_key_exists($column, $row)) { $value = trim($row[$column]); if (!empty($value) && Validate::isUrl($row[$column]) !== true) { $row[$column] = Server::getBaseUrl($row[$column]); } } return $row; }
/** * Constructor */ public function __construct($name = "", $expire = null) { // factory options $this->setOption("name", "session"); $this->setOption("cookie_lifetime", 0); $this->setOption("cookie_path", "/"); $this->setOption("cookie_domain", Server::getHost()); $this->setOption("cookie_secure", Connection::isSecure()); $this->setOption("cookie_httponly", true); $this->setOption("use_only_cookies", true); $this->setOption("use_trans_sid", false); $this->setOption("entropy_file", "/dev/urandom"); $this->setOption("entropy_length", 256); $this->setOption("hash_function", "sha256"); $this->setOption("hash_bits_per_character", 6); $this->setOption("gc_maxlifetime", 86400); // 24h // set custom values $this->setName($name); $this->setExpire($expire); }
/** * Constructor */ public function __construct($name = '', $expire = null) { // factory options $this->setOption('name', 'session'); $this->setOption('cookie_lifetime', 0); $this->setOption('cookie_path', '/'); $this->setOption('cookie_domain', Server::getHost()); $this->setOption('cookie_secure', Server::isSecure()); $this->setOption('cookie_httponly', true); $this->setOption('use_only_cookies', true); $this->setOption('use_trans_sid', false); $this->setOption('entropy_file', '/dev/urandom'); $this->setOption('entropy_length', 256); $this->setOption('hash_function', 'sha256'); $this->setOption('hash_bits_per_character', 6); $this->setOption('gc_maxlifetime', 86400); // 24h // set custom values $this->setName($name); $this->setExpire($expire); }
/** * Resolve check for SSL request */ public function resolveIsSecure($default = false) { $this->_secure = Server::isSecure(); }
/** * Take a full path and make relative to the application root */ public static function relativePath($path) { if (!empty($path) && is_string($path)) { $path = Sanitize::toPath($path); $root = Server::getScriptPath(); $levels = array($root, dirname($root), dirname(dirname($root)), dirname(dirname(dirname($root)))); foreach ($levels as $level) { if (empty($level) || $levels === "/") { continue; } $path = str_replace($level, "", $path); } } return $path; }
/** * Load and filter list of menu items data from a file */ private function _loadMenuData($file) { $file = Sanitize::toPath($file); $menu = is_file($file) ? include_once $file : []; $output = []; $count = 1; if (is_array($menu)) { foreach ($menu as $idx => $item) { $active = ""; $url = Utils::value(@$item["url"], Server::getBaseUrl()); if (empty($item["url"])) { if (!empty($item["route"])) { if (preg_match("/^(\\/" . $this->_area . ")?(\\/" . $this->_controller . ")/", $item["route"]) === 1) { $active = "active"; // route matched current location } $url = Server::getBaseUrl($item["route"]); } else { if (!empty($item["controller"])) { if ($this->_controller === $item["controller"]) { $active = "active"; // controller matched current controller } $area = $this->_area !== "site" ? $this->_area : ""; $route = Utils::buildPath($area, $item["controller"], @$item["action"]); $url = Server::getBaseUrl($route); } } } $item["active"] = $active; $item["url"] = $url; $output[] = $item; $count++; } } return $output; }
/** * Convert a request path string into routing params */ public function parse() { $this->resetActions(); $this->resetParams(); $this->setArea($this->_default_area); $this->setController($this->_default_controller); $this->addAction('init-action', false); $path = Utils::getValue($this->_path_request, '/'); $path = Sanitize::toPath(@parse_url($path, PHP_URL_PATH)); $path = str_replace(Server::getBasePath(), '', $path); $route = explode('/', trim($path, '/')); if (!empty($route[0]) && $this->areaExists($route[0])) { $this->setArea(array_shift($route)); } if (!empty($route[0])) { $this->setController(array_shift($route)); } if (!empty($route[0])) { $this->addAction(array_shift($route)); } if (!empty($route)) { $this->_params = array_values($route); } if (count($this->_actions) === 1) { $this->addAction($this->_default_action); } }
/** * 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"]]); }
/** * 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 . ').'); }
/** * Get current request URL */ public function getUrl() { return Server::getUrl(); }
/** * 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 public web url for a local file if it exists */ public function fileUrl($file = "") { return Server::getFileUrl($file); }
/** * 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()); }