/** * PHP5 Constructor */ public function __construct() { parent::__construct(); $this->x_uri_sufix = Config::get('uri_sufix', ''); /** * Set BenchMark Start */ Benchmark::set('url', 'start'); /** * initiate */ $this->reInitiate(); /** * Set BenchMark End */ Benchmark::set('url', 'end'); }
/** * HTML Error If exists */ public static function htmlError() { /* ======================== * Getting & Set config * ======================== */ $error_to_show_ = Config::get('show_error_count', 3); if (!is_numeric($error_to_show_) && !empty($error_to_show_)) { $error_to_show = 3; Config::replace('show_error_count', $error_to_show); } else { $error_to_show = abs($error_to_show_) < 0 || abs($error_to_show_) >= 30 ? 30 : abs(intval($error_to_show_)); $error_to_show_ !== $error_to_show && Config::replace('show_error_count', $error_to_show); } /* ======================== * Getting & Set Language * ======================== */ if (!is_string(static::$x_html_error_type) || !trim(static::$x_html_error_type)) { static::$x_html_error_type = 'Error Type'; } if (!is_string(static::$x_html_error_message) || !trim(static::$x_html_error_message)) { static::$x_html_error_message = 'Error Message'; } if (!is_string(static::$x_html_error_file) || !trim(static::$x_html_error_file)) { static::$x_html_error_file = 'Error File'; } if (!is_string(static::$x_html_error_line) || !trim(static::$x_html_error_line)) { static::$x_html_error_line = 'Error Line'; } if (!is_string(static::$x_html_error_more) || is_numeric(static::$x_html_error_more) || !trim(static::$x_html_error_more)) { static::$x_html_error_more = null; } else { static::$x_html_error_more = 'And %[more_error]% more.'; } if (!is_string(static::$x_html_error_document_root) || is_numeric(static::$x_html_error_document_root)) { static::$x_html_error_document_root = null; } else { static::$x_html_error_document_root = !trim(static::$x_html_error_document_root) ? '' : '{DOCUMENT ROOT}'; } $err_type = static::$x_html_error_type; $err_msg = static::$x_html_error_message; $err_file = static::$x_html_error_file; $err_line = static::$x_html_error_line; $another_error = static::$x_html_error_more; $doc_root = static::$x_html_error_document_root; // default returns $html = false; // get Error $error = static::getError(); if ($error_to_show && !empty($error)) { $html = " <div class=\"x_error_info\">\n"; $c = 0; // length of document root $strlen_doc_root = strlen(Path::documentRoot()); // split error to shown on html foreach ($error as $key => $value) { /** * If static::$x_html_error_document_root is not null * will be set alternative */ if (static::$x_html_error_document_root !== null) { // safe output show replaced document root to static::$x_html_error_document_root // default set {DOCUMENT ROOT} $value['file'] = substr_replace($value['file'], static::$x_html_error_document_root !== '' ? '<span class="x_error_doc_root">' . static::$x_html_error_document_root . '</span>' : '', 0, $strlen_doc_root); } $html .= " <div class=\"x_error_section\">\n"; $html .= " <table class=\"x_error_table\">\n"; $html .= " <tr class=\"x_error_type\">\n"; $html .= " <td class=\"x_error_label\"><span>{$err_type}</span></td>\n" . " <td class=\"x_error_value\"><span><span class=\"x_error_type_code\">{$value['type']}</span>" . "<span class=\"x_error_type_string\">{$value['type_string']}</span>" . "</span></td>\n"; $html .= " </tr>\n"; $html .= " <tr class=\"x_error_message\">\n"; $html .= " <td class=\"x_error_label\"><span>{$err_msg}</span></td>\n" . " <td class=\"x_error_value\"><span>{$value['message']}</span></td>\n"; $html .= " </tr>\n"; $html .= " <tr class=\"x_error_file\">\n"; $html .= " <td class=\"x_error_label\"><span>{$err_file}</span></td>\n" . " <td class=\"x_error_value\"><span>{$value['file']}</span></td>\n"; $html .= " </tr>\n"; $html .= " <tr class=\"x_error_line\">\n"; $html .= " <td class=\"x_error_label\"><span>{$err_line}</span></td>\n" . " <td class=\"x_error_value\"><span>{$value['line']}</span></td>\n"; $html .= " </tr>\n"; $html .= " </table>\n"; $html .= " </div>\n"; $c++; /** * check if has limit */ if ($c >= $error_to_show && ($error_count = count($error) - $c) > 0) { if (static::$x_html_error_more) { $html .= " <div class=\"x_error_more\">\n"; $html .= " <div class=\"x_error_more_info\">" . str_replace('%[more_error]%', "<span class=\"x_error_more_count\">{$error_count}</span>", static::$x_html_error_more) . "</div>\n"; $html .= " </div>\n"; } // stop break; } } $html .= " </div>"; unset($error); } return $html; }
/** * Set HTTP cookie to be sent with the HTTP response * * @param string $name The cookie name * @param string $value The cookie value * @param int|string $expires The duration of the cookie; * If integer, should be UNIX timestamp; * If string, converted to UNIX timestamp with `strtotime`; * @param string $path The path on the server in which the cookie will be available on * @param string $domain The domain that the cookie is available to * @param bool $secure Indicates that the cookie should only be transmitted over a secure * HTTPS connection to/from the client * @param bool $httponly When TRUE the cookie will be made accessible only through the HTTP protocol * @param bool $encrypted When TRUE the cookie will be made as encrypted */ public static function set($name, $value, $expires = null, $path = null, $domain = null, $secure = null, $httponly = null, $encrypted = null) { $settings = array('value' => $value, 'expires' => is_null($expires) ? Config::get('cookie_lifetime') : $expires, 'path' => is_null($path) ? Config::get('cookie_path', '/') : $path, 'domain' => is_null($domain) ? Config::get('cookie_domain', null) : $domain, 'secure' => is_null($secure) ? Config::get('cookie_secure', false) : $secure, 'httponly' => is_null($httponly) ? Config::get('cookie_httponly', false) : $httponly, 'encrypted' => $encrypted); $cookies = Response::cookies(); $cookies->set($name, $settings); }
/** * Init , template * becarefull, if template has been called once it will be no affected to this init * @return object current class */ public function standardInit() { /** * If one has been called no call anymore! * @var boolean static cached */ static $has_called; // prevent multiple called if ($has_called) { return $this; } $has_called = true; $has_set = false; if (!$this->templates_directory || !is_string($this->templates_directory)) { $template = Config::Get('template_directory', null); if (!$template && ($template = realpath('templates'))) { $template = 'templates'; } elseif ($template && is_string($template)) { $has_set = true; $template = $template ? realpath(trim($template)) : null; } $this->templates_directory = !$template ? null : $template; unset($template); } /** * Checking */ if (!$this->templates_directory || is_string($this->templates_directory) && trim($this->templates_directory) !== '' && !Internal::isDir($this->templates_directory)) { if ($has_set || is_string($this->templates_directory)) { trigger_error('Templates directory does not exists!', E_USER_ERROR); } return $this; } /** * Templates Directory Replace if it can * That we just try */ Config::replace('templates_directory', $this->templates_directory); if (!$this->x_default_template || !is_string($this->x_default_template) || !trim($this->x_default_template)) { $this->x_default_template = 'Default'; } if (!is_string($this->x_file_to_read) || !trim($this->x_file_to_read)) { $this->x_file_to_read = 'templates.php'; } /** * Trimming empty right and left * @var string */ $this->x_default_template = trim($this->x_default_template); $this->x_file_to_read = trim($this->x_file_to_read); if (!is_array($this->x_mustbe_exist)) { trigger_error('Invalid Templates Definition!', E_USER_ERROR); return $this; } if (!is_array($this->x_headers)) { trigger_error('Invalid Templates Headers Definition!', E_USER_ERROR); return $this; } // add file to read to be must be exists in_array($this->x_file_to_read, $this->x_mustbe_exist) || ($this->x_mustbe_exist[] = $this->x_file_to_read); $this->templates_directory = Path::cleanPath(realpath($this->templates_directory)); $this->buildInit(); // set if (!empty($this->x_list_templates)) { if (!array_key_exists($this->x_default_template, $this->x_list_templates)) { $activeTemplate = $this->getAllTemplate(); $this->x_default_template = key($activeTemplate); } // set active templates $this->setActiveTemplate($this->x_default_template); } return $this; }
/** * Alternative decryption using Pure PHP Libraries * @http://px.sklar.com/code.html/id=1287 * Fix and added More Secure Method * * @param string $str string to be decode * @param string $pass the hash key * @return mixed decryption value output */ public static function altDecrypt($enc, $pass = '') { // if has $enc or invalid no value or not as string stop here if (!is_string($enc) || strlen(trim($enc)) < 4 || (strlen($enc) > 10 ? strpos($enc, 'aCb') !== 10 : strpos($enc, 'aCb') !== 2)) { // check if mcrypt loaded and crypt using mcrypt if (is_string($enc) && strlen(trim($enc)) > 3 && extension_loaded('mcrypt') && (strlen($enc) > 10 ? strpos($enc, 'mCb') === 10 : strpos($enc, 'mCb') === 2)) { return static::decrypt($enc, $pass); } return null; } /** * Replace Injection 3 characters sign */ $enc = strlen($enc) > 10 ? substr_replace($enc, '', 10, 3) : substr_replace($enc, '', 2, 3); // this is base64 safe encoded? if (preg_match('/[^a-z0-9\\+\\/\\=\\-\\_]/i', $enc)) { return null; } /** * ------------------------------------ * Safe Sanitized * ------------------------------------ */ $pass = !$pass ? Config::get('security_salt', '') : $pass; (is_null($pass) || $pass === false) && ($pass = ''); // safe is use array orobject as hash $pass = StringHelper::maybeSerialize($pass); if (!$pass) { $pass = Sha1::hash($pass); } /** * Doing decode of input encryption */ $enc = Internal::safeBase64Decode($enc); /** * ------------------------------------ * Doing convert encrypted string * ------------------------------------ */ $enc_arr = str_split($enc); $pass_arr = str_split($pass); $add = 0; $div = strlen($enc) / strlen($pass); $newpass = ''; while ($add <= $div) { $newpass .= $pass; $add++; } $pass_arr = str_split($newpass); $ascii = ''; foreach ($enc_arr as $key => $asc) { $pass_int = ord($pass_arr[$key]); $enc_int = ord($asc); $str_int = $enc_int - $pass_int; $ascii .= chr($str_int - strlen($enc)); } /* -------------------------------- * reversing * ------------------------------ */ // unpack $unpack = unpack('a*', trim($ascii)); /** * if empty return here */ if (!$unpack) { return null; } // implode the unpacking array $unpack = implode('', (array) $unpack); /** * Doing decode of input encryption from unpacked */ $unpack = Internal::safeBase64Decode($unpack); /** * Reverse Rotate */ $retval = Internal::rotate($unpack, 13); /** * For some case packing returning invisible characters * remove it */ $retval = StringHelper::removeInvisibleCharacters($retval, false); // check if string less than 40 && match end of hash if (strlen($retval) < 40 || substr($retval, -40) !== Sha1::hash(Sha256::hash($pass))) { return; } // remove last 40 characters $retval = substr($retval, 0, strlen($retval) - 40); // check if result is not string it will be need to be unserialize $retval = StringHelper::maybeUnserialize($retval); /** * Check if value is array */ if (is_array($retval) && array_key_exists('acb', $retval)) { return $retval['acb']; } // freed the memory unset($retval); return null; }
/** * Default error 500 output Handler */ public static function error500() { $args_ = func_get_args(); $template = Template::singleton(); $template_dir = $template->getActiveTemplateDirectory(); static::$x_is_fatal = true; if ($template_dir && $template->x_500_file && is_string($template->x_500_file)) { if (is_file("{$template_dir}/{$template->x_500_file}")) { $message = (array) reset($args_); // using callback to prevent direct access return call_user_func(function ($a) use($message) { ob_start(); require $a; $content = ob_get_clean(); Response::setBody($content); static::displayRender(); exit(1); // and then exit here }, "{$template_dir}/{$template->x_500_file}"); } } /** * Body container * @var string */ $body = "<h1 class=\"big\">500</h1>\n"; if (Config::get('debug', true)) { $args_ = current($args_); $strlen_doc_root = strlen(Path::documentRoot()); // safe output show replaced document root to {DOCUMENT ROOT} $args_['file'] = substr_replace($args_['file'], '<span class="x_error_doc_root">{DOCUMENT ROOT}</span>', 0, $strlen_doc_root); $body .= " <div class=\"x_error_section\">\n" . " <table class=\"x_error_table\">\n" . " <tr class=\"x_error_type\">\n" . " <td class=\"x_error_label\"><span>Error Type</span></td>\n" . " <td class=\"x_error_value\"><span><span class=\"x_error_type_code\">{$args_['type']}</span>" . "<span class=\"x_error_type_string\">{$args_['type_string']}</span></span></td>\n" . " </tr>\n" . " <tr class=\"x_error_message\">\n" . " <td class=\"x_error_label\"><span>Error Message</span></td>\n" . " <td class=\"x_error_value\"><span>{$args_['message']}</span></td>\n" . " </tr>\n" . " <tr class=\"x_error_file\">\n" . " <td class=\"x_error_label\"><span>Error File</span></td>\n" . " <td class=\"x_error_value\"><span>{$args_['file']}</span></td>\n" . " </tr>\n" . " <tr class=\"x_error_line\">\n" . " <td class=\"x_error_label\"><span>Error Line</span></td>\n" . " <td class=\"x_error_value\"><span>{$args_['line']}</span></td>\n" . " </tr>\n" . " </table>\n" . " </div>\n"; } else { $body .= "<h2 class=\"desc\">Internal Server Error</h2>\n" . "<p>We are sorry for inconvenience</p>"; } /** * Set Body */ Response::setBody(Html::create('Internal Server Error', $body, array('style' => "body{font-size: 14px;font-family: helvetica, arial, sans-serif;color: #555;line-height: normal;background: #f1f1f1;}\n" . ".wrap{margin: 0 auto;max-width: 700px;text-align: center;}\n" . (Config::get('debug', false) ? ".x_error_section{display:block;padding: 10px;background: #fff;border: 1px solid #ddd;}\n" . ".x_error_table{border-collapse: collapse;border:0;border-spacing:0;}\n" . ".x_error_label{padding: 5px 10px;text-align: left;border-right: 2px solid #bbb;}\n" . ".x_error_value{padding: 5px 10px;text-align: left;border-right: 0px solid #ddd;}\n" . ".x_error_type .x_error_type_string{background: #f18181;padding: 3px 5px;color:#fff;font-weight: bold;margin-left:0px;}\n" . ".x_error_type .x_error_type_code{background: #4359fe;margin-right: 0px;padding: 3px 6px;color:#fff;font-weight: bold;}\n" : '') . ".big{font-size: 180px;margin: .7em 0 20px;}\n.desc{font-size: 28px;margin: .3em 0 0;}"))); // doing display static::displayRender(); exit(1); // and then exit here }
/** * Serialize Response cookies into raw HTTP header * * @param \Enproject\ErSysDucation\Response\Header $header The Response header */ public static function serializeCookies(Headers &$header) { $instance = static::singleton(); $config = Config::singleton(); $cookies = $instance->cookies(); $prefix = $config->get('cookie_encrypt_prefix', 'enc|'); is_string($prefix) && trim($prefix) || ($prefix = 'enc|'); $config->cookie_encrypt = $config->get('cookie_encrypt', true); foreach ($cookies as $name => $settings) { if (is_string($settings['expires'])) { $expires = strtotime($settings['expires']); } else { $expires = (int) $settings['expires']; } /** * Check if is has encrypted value * if config cookie encrypt has true * and * (__ settings['encrypted'] = has null or not exists) * or not empty $settings['encrypted'] * @var boolean */ if (!empty($settings['encrypted']) || $config->cookie_encrypt && !isset($settings['encrypted'])) { // add prefix enc to make sure if cookie has encrypt $settings['value'] = $prefix . Security::encrypt($settings['value'], Sha1::hash($config->security_key . $config->security_salt . $config->session_hash)); } /** * Cookie only accept 4KB */ if (strlen($settings['value']) > 4096) { ErrorHandler::set(E_USER_WARNING, sprintf('Cookie %s has been generate more than 4KB failed to save! if there was cookie before, it will be not replaced!', $name), __FILE__, __LINE__); } else { // set header cookies static::setCookieHeader($header, $name, $settings); } } }