/** * Get value of HTTP cookie from the current HTTP request * * Return the value of a cookie from the current HTTP request, * or return NULL if cookie does not exist. Cookies created during * the current request will not be available until the next request. * * @param string $name * @param bool $deleteIfInvalid doing delete if invalid * @param bool $encrypted use force encryped to set true if encrypted * without following config * set to false if use no encryption * @return string|null */ public static function get($name, $deleteIfInvalid = false, $encrypted = null) { // Get cookie value $cookies = Request::cookies(); $value = $cookies->get($name); $config = Config::singleton(); $prefix = $config->get('cookie_encrypt_prefix', 'enc|'); is_string($prefix) && trim($prefix) || ($prefix = 'enc|'); // Decode if encrypted if (($config->get('cookie_encrypt', true) && $encrypted !== false || $encrypted) && strpos($value, $prefix) === 0) { $value = Security::decrypt($value, sha1($config->security_key . $config->security_salt . $config->session_hash)); if ($value === null && $deleteIfInvalid) { static::deleteCookie($name); } } return $value; }
/** * Parse Request URL * * @return string Request URI parsed */ protected function parseRequestUri() { // static cached static $return = null; if ($return !== null) { return $return; } if (!Server::get('REQUEST_URI') && !Server::get('SCRIPT_NAME')) { $return = ''; return $return; } // add Request URI // $requri = Path::cleanSlashed(Request::getHost().'/'.Server::get('REQUEST_URI')); $requri = Server::get('REQUEST_URI'); $requri = substr($requri, 0, 1) == '/' ? $requri : "/{$requri}"; $requri = rtrim(Request::getHost(), '/') . $requri; $uri = parse_url('http://' . $requri); $query = isset($uri['query']) ? $uri['query'] : ''; $uri = isset($uri['path']) ? $uri['path'] : ''; $script_name = Server::get('SCRIPT_NAME'); if (isset($script_name[0])) { /** * Set New URL Path */ if (strpos($uri, $script_name) === 0) { $uri = substr($uri, strlen($script_name)); } elseif (strpos($uri, dirname($script_name)) === 0) { $uri = substr($uri, strlen(dirname($script_name))); } } // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct // URI is found, and also fixes the QUERY_STRING server var and $_GET array. if (trim($uri, '/') === '' && strncmp($query, '/', 1) === 0) { $query = explode('?', $query, 2); $uri = $query[0]; $_SERVER['QUERY_STRING'] = isset($query[1]) ? $query[1] : ''; } else { $_SERVER['QUERY_STRING'] = $query; } // replace server attributes Server::replace($_SERVER); // parse the string parse_str($_SERVER['QUERY_STRING'], $_GET); // replace get Get::replace($_GET); if ($uri === '/' || $uri === '') { $return = '/'; } else { $return = Path::removeRelativeDirectory($uri); } return $return; }
private final function buildInit() { /** * If one has been called no call anymore! * @var boolean static cached */ static $has_called; // prevent multiple called if ($has_called) { return $this->x_list_templates; } $has_called = true; $documentRoot = Path::documentRoot(); $template_after_dir = preg_replace('/^' . preg_quote($documentRoot, '/') . '/', '', $this->templates_directory); $this->templates_directory_url = Request::baseUrl(rtrim($template_after_dir, '/')); foreach ((array) Internal::readDirList($this->templates_directory, 1) as $key => $value) { if (!is_file("{$this->templates_directory}/{$value}/{$this->x_file_to_read}")) { continue; } $this->buildTemplateDetails("{$this->templates_directory}/{$value}"); } return $this->x_list_templates; }
/** * Agregate Display * * @return void */ private static final function displayRender() { // set 500 fatal error if (static::$x_is_fatal || Route::isFatalError()) { static::$x_is_fatal = true; // set again Route::setFatalError(); // set fatal error Response::setStatus(500); // set 500 } elseif (Route::isNoMatch()) { Response::setStatus(404); // set 404 } /** * check again if not set on boolean */ if (static::$x_is_request_head === null) { static::$x_is_request_head = Request::isHead(); } /** * Get Request * This as cached variable to prevent Being Overide */ $is_head_request = static::$x_is_request_head; /** * If not in Head request * get body content before and prepend it */ if (!$is_head_request) { // start buffer if not exists ob_get_level() || ob_start(); $body = ob_get_clean(); /** * Prepend The Body if there's some output before prepend it */ Response::prepend($body); } else { // if on head request set into empty string Response::setBody(''); } /** * Fetch status, header, and body */ list($status, $headers, $body) = Response::finalize(); /** * Serialize cookies (with optional encryption) * set cookie header into Response */ Response::serializeCookies($headers); /** * no headers hooks for fatal error */ if (!static::$x_is_fatal && !$is_head_request) { /** * Set OLD Header And status * for safe header request */ $old_headers = $headers->all(); $old_status = $headers->all(); /** * Doing Headers Hook * @var string */ $headers = Hook::apply('x_headers', $headers->all()); // if on hooks change headers has not array if (!is_array($headers)) { $headers = $old_headers; } /** * Doing Status Hook * @var string */ $status = (int) Hook::apply('x_header_status', $status); // if on hooks change status and that is invalid if (!Response::getMessageForCode($status)) { $status = $old_status; } // freed unset($old_headers, $old_status); } /** * for safe method, check if headers * has not already sent. * header will be send into client * that if header has been sent , the header set will be * thrown an error */ if (!headers_sent()) { /** * Send status header */ if (strpos(PHP_SAPI, 'cgi') === 0) { header(sprintf('Status: %s', Response::getMessageForCode($status))); } else { header(sprintf('HTTP/%s %s', Config::get('http_version', '1.1'), Response::getMessageForCode($status))); } /** * Send headers, getting all headers and set it */ foreach ($headers as $name => $value) { if (!is_string($value)) { continue; } $hValues = explode("\n", $value); foreach ($hValues as $hVal) { header("{$name}: {$hVal}", false); } } } /** * Hook Body / Output Content * @var string */ $body = Hook::apply('x_before_output', $body); /** * Send body, but only if it isn't a HEAD request */ if (!Request::isHead()) { /** * Hoks only available if no fatal */ if (!static::$x_is_fatal) { /** * Force tag Output */ if (Config::get('force_tag', false)) { // force balance the tags $body = Hook::apply('x_force_tag_output', Filter::forceBalanceTags($body), $body); } /** * Safe Output Check */ if (Config::get('safe_output', false)) { // Filtering multibyte entities and set entities into false $body = Hook::apply('x_safe_output', Filter::multibyteEntities($body, false), $body, false); } /** * Inject Error Info if on debug mode */ if (Config::get('debug', false)) { $error = ErrorHandler::HtmlError(); /** * Insert Into Body content if exists * if exist data-target='x_data_error' -> will be inserted here * or will be inserted into after open <body(.?)> tag */ if ($error && is_string($error)) { $body = Hook::apply('x_error_output', preg_match('/(<div\\s*(?:data\\-target\\=(\'|\\")([\\w:]*\\s+)?x_data_error(\\s+|$2)*)(?:[^>]*)>)/', $body) ? preg_replace('/(<div\\s*(?:data\\-target\\=(\'|\\")([\\w:]*\\s+)?x_data_error(\\s+|$2)*)(?:[^>]*)>(.*))/', "\$1{$error}\$2", $body) : (stripos($body, '<body') !== false && preg_match('/(<body\\s*(?:[^>]*)>)/i', $body) ? preg_replace('/(<body\\s*(?:[^>]*)>)/i', "\$1\n{$error}", $body) : preg_replace("/^\\s\\s(\\s*)/m", "\$1", $error) . "\n{$body}"), $body); } } } /** * set again end of application */ Benchmark::set('app', 'end'); /** * check if contains shortcode here about %[ * if exists will bereturning replace */ if (strpos($body, "%[") !== false) { $body = str_replace(array('%[benchmark]%', '%[memory]%', '%[real_memory]%', '%[\\benchmark\\]%', '%[\\memory\\]%', '%[\\real_memory\\]%'), array(round(Benchmark::get('app'), 6), StringHelper::sizeFormat(Benchmark::getMemory(), 2), StringHelper::sizeFormat(Benchmark::getRealMemory(), 2), '%[benchmark]%', '%[memory]%', '%[real_memory]%'), $body); /** * fix escaped * Above will be replace if only one */ strpos($body, "%[") !== false && ($body = preg_replace('/(\\%\\[)\\\\(\\\\+)(benchmark|memory|real\\_memory)\\\\(\\\\+)(\\]\\%)/', '$1$2$3$4$5', $body)); } /** * Clean Body Output from empty non ascii characters * set second parameters to false because this is not URL */ $body = StringHelper::removeInvisibleCharacters($body, false); /** * set response body */ Response::setBody(Hook::apply('x_before_output', $body)); if (!headers_sent() && in_array('Content-Length', headers_list())) { header('Content-Length: ' . Response::getLength(), true); } /** * freed memory */ unset($body, $headers); /** * starting buffer if buffer has been cleaned before */ ob_get_level() || ob_start(); /** * print output */ echo Response::getBody(); /** * Set Hook after output if not in Fatal */ if (!static::$x_is_fatal) { // do after Hook::doAction('x_after_output', Response::getBody()); /** * start buffer again if bugger is cleared */ ob_get_level() || ob_start(); /** * doing after all end of system */ Hook::doAction('x_after_all'); } /** * set response body to empty freed the memory * Reset Body */ Response::setBody(''); } // restore error handler -> end restore_error_handler(); }