/** * Add single check test to local checklist */ public function addCheck($info, $test, $pass = "", $fail = "") { $info = Sanitize::toString($info); $test = Sanitize::toBool($test); $pass = Utils::value($pass, "Ready"); $fail = Utils::value($fail, "Missing"); $this->_checklist[] = array("info" => $info, "test" => $test, "pass" => $pass, "fail" => $fail); }
/** * Get the server script handling the request */ public static function getScriptFile() { $value = Utils::value(@$_SERVER["SCRIPT_FILENAME"], ""); $value = Sanitize::toPath($value); return $value; }
/** * Extends the session cookie lifetime period */ public function extendCookie($expire = null) { $time = time(); $default = $time + $this->getOption("cookie_lifetime", 0); $expire = Utils::value(Sanitize::toTimestamp($expire), $default); if ($expire > $time) { return setcookie(session_name(), session_id(), $expire, $this->getOption("cookie_path"), $this->getOption("cookie_domain"), $this->getOption("cookie_secure"), $this->getOption("cookie_httponly")); } return false; }
/** * Returns the HTTP authentication password (if any) */ public static function getPassword($default = "") { return Utils::value(@$_SERVER["PHP_AUTH_PW"], $default); }
/** * Get array of item timestamps */ public function getTimestamps() { $path = $this->getPath(); $now = time(); $created = Utils::value(@filectime($path), $now); $modified = Utils::value(@filemtime($path), $now); $accessed = Utils::value(@fileatime($path), $now); return array("created" => min($created, $modified, $accessed), "modified" => max($created, $modified), "accessed" => max($modified, $accessed)); }
/** * Parse file data as PHP var_dump() data dump */ private function _parseDump($data = "") { $output = []; $data = preg_replace("/(array|object|string)\\(\\w*\\)(\\#\\d*\\s*)?(\\(\\d*\\))?\\s*{?/ui", "", $data); $data = preg_replace("/(int|bool)\\((\\w+)\\)/ui", "\$2", $data); $data = preg_replace("/\\s*=>[\r\n]+/ui", " => ", $data); $data = preg_replace("/[ ]{2,}/ui", " ", $data); $data = str_replace(['"', "{", "}"], "", $data); foreach ($this->_keymap as $key => $search) { @preg_match("/(\\[" . $search . "\\])([\\s\\=\\>]+)(.*)/u", $data, $matches); $output[$key] = trim(Utils::value(@$matches[3], "")); } return $output; }
/** * Adds a list of columns to group by */ public function group() { foreach (Utils::split(func_get_args()) as $column) { $this->_group[] = Sanitize::toSqlName($column); } return $this; }
/** * Parse request body as multipart FormData */ private function _parseForm() { $boundary = trim(strtok($this->_body, "\n")); $chunks = preg_split("/" . $boundary . "(\\-\\-)?/", $this->_body, -1, PREG_SPLIT_NO_EMPTY); $params = []; $files = []; $counter = []; if (is_array($chunks)) { foreach ($chunks as $index => $chunk) { // skip empty chunks $chunk = ltrim($chunk, "-\r\n\t\\s "); if (empty($chunk)) { continue; } // split chunk into headers and value @(list($head, $value) = explode("\r\n\r\n", $chunk, 2)); $headers = $this->_parseBodyHeaders($head); $name = Utils::value(@$headers["name"], "undefined_" . ($index + 1)); $type = Utils::value(@$headers["content-type"], "application/octet-stream"); $key = Sanitize::toKey($name); $value = trim($value); // counter to increment array-like param names if (isset($counter[$key]) !== true) { $counter[$key] = 0; } // process uploaded file if (isset($headers["filename"])) { $file = $headers["filename"]; $name = str_replace("[]", "", $name); $path = ""; $copy = false; if (!empty($headers["filename"]) && !empty($value)) { $path = Sanitize::toPath(tempnam($this->_tmpdir, "upload")); $copy = file_put_contents($path, $value); } if (preg_match("/\\[\\d+\\]\$/", $name) !== 1) { $name .= "[" . $counter[$key] . "]"; } $files[$name . "[name]"] = $file; $files[$name . "[type]"] = $type; $files[$name . "[tmp_name]"] = $path; $files[$name . "[error]"] = !empty($copy) ? 0 : UPLOAD_ERR_NO_FILE; $files[$name . "[size]"] = !empty($copy) ? filesize($path) : 0; } else { if (preg_match("/\\[\\]\$/", $name) === 1) { $name = str_replace("[]", "[" . $counter[$key] . "]", $name); } $params[$name] = $value; } $counter[$key] += 1; } // finalize arrays parse_str(urldecode(http_build_query($params, "", "&")), $_POST); parse_str(urldecode(http_build_query($files, "", "&")), $_FILES); } }
/** * Set and error and return false */ public function setError($error = "") { $this->_error = Utils::value($error, "There has been an unspecified database error."); return false; }
/** * 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; }
/** * Filter data from given exception object into an array */ protected function _filterError($error) { $message = $error->getMessage(); $file = $error->getFile(); $code = $error->getCode(); $line = $error->getLine(); $type = $this->_getType($code, $message); return ["type" => $type, "message" => Utils::relativePath($message), "file" => Utils::relativePath($file), "line" => $line, "code" => $code]; }
/** * IP v6 address */ public static function toIpv6($value = null) { $value = Utils::replace($value, "/[^a-fA-F0-9\\:]+/i"); $value = Utils::replace($value, "/\\:\\:{3,}/", "::"); $value = trim($value, ":"); return $value; }
/** * Adds authenticated user data to the session */ public function login($data = []) { $this->reset(); $this->session->set($this->key . ".info.last_active", time()); $this->session->set($this->key . ".info.login_time", time()); $this->session->set($this->key . ".info.login_hash", $this->unique); if (!empty($data) && is_array($data)) { if (empty($data["image"]) && !empty($data["email"])) { $data["image"] = Utils::gravatar($data["email"]); } if (!empty($data["options"]) && is_string($data["options"])) { $data["options"] = @json_decode($data["options"], true); } $this->session->set($this->key . ".user", $data); } return true; }
/** * Send rendered view object html */ public function sendView($status, $view, $replace = []) { if ($view instanceof View) { $html = $view->render(); $body = Utils::render($html, Utils::merge($replace, ["load_time" => Server::getLoadTime(@APP_START_TIME), "mem_usage" => Server::getMemUsage()])); $this->sendHtml($status, $body); } $this->sendHtml($status, "Empty response."); }