/** * Loads data from a text/plain file as INI data and put it into the _ENV array. */ public function loadEnv($file) { if (is_file($file)) { $data = @parse_ini_file($file, false); if (is_array($data)) { foreach ($data as $key => $value) { $key = Sanitize::toKey($key); $value = Utils::unserialize($value); if (empty($key) || is_numeric($key) || is_string($key) !== true) { continue; } if (array_key_exists($key, $_ENV)) { continue; } putenv($key . "=" . $value); $_ENV[$key] = $value; } } } }
/** * Get the hostname */ public static function getHost() { $value = Utils::value(@$_SERVER["SERVER_NAME"], ""); $value = Sanitize::toKey($value); return $value; }
/** * Delete an entry for a dot-notated key string */ public function delete($key = "") { $key = Sanitize::toKey($key); if (!empty($key) && $this->started === true) { $path = trim($this->container . "." . $key, "."); $list = explode(".", $path); $last = array_pop($list); $data =& $_SESSION; foreach ($list as $step) { if (!isset($data[$step])) { return; // gone } $data =& $data[$step]; } if (isset($data[$last])) { // need to reference the last key for unset() to work $data[$last] = null; unset($data[$last]); } } }
/** * Delete an existing key */ public function deleteKey($key) { $key = Sanitize::toKey($key); if (!empty($key)) { $list = explode(".", $key); $last = array_pop($list); $data =& $this->_data; foreach ($list as $step) { if (!isset($data[$step])) { return; } $data =& $data[$step]; } if (isset($data[$last])) { $data[$last] = null; unset($data[$last]); } } }
/** * Set file format to me processed */ public function setFormat($format) { if (is_string($format)) { $this->_format = Sanitize::toKey($format); } }
/** * 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); } }
/** * Load list of table queries by scanning files from a folder */ public static function loadQueries($path) { $path = Sanitize::toPath($path); $output = []; if (!empty($path) && is_dir($path)) { foreach (glob($path . "/*.php") as $file) { $table = Sanitize::toKey(basename($file, ".php")); $queries = []; include_once $file; $output[$table] = $queries; } } return $output; }
/** * Resolve route details for a given request path */ public function setRoute($route = "/") { $this->_route = $route; $route = trim($route, "/"); $route = preg_replace("/[^\\w\\-\\:\\/]+/", "", $route); $route = preg_replace("/\\/\\/+/", "/", $route); $this->_area = "site"; $this->_controller = "home"; $this->_action = "index"; $this->_params = explode("/", $route); if (!empty($this->_params[0]) && $this->areaExists($this->_params[0])) { $this->_area = array_shift($this->_params); } if (!empty($this->_params[0])) { $this->_controller = Sanitize::toKey(array_shift($this->_params)); } if (!empty($this->_params[0])) { $this->_action = Sanitize::toKey(array_shift($this->_params)); } }