/** * Set file to save error_log messages to */ public function setLogPath($path) { $path = Sanitize::toPath($path); if (is_dir($path) || mkdir($path, 0775, true)) { $this->_logdir = $path; } }
/** * Load array data from files in a path, or a single file */ public function loadData($path) { if (!empty($path) && is_string($path)) { $path = Sanitize::toPath($path); if (is_dir($path)) { foreach (glob($path . "/*.php", GLOB_NOSORT) as $file) { $data = $this->_fileData($file); $this->mergeData($data); } } else { if (is_file($path)) { $data = $this->_fileData($path); $this->mergeData($data); } } } }
/** * Get public web url for a local file if it exists */ public static function getFileUrl($file = "") { $root = self::getScriptPath(); $file = Sanitize::toPath(trim($file, ". ")); $path = str_replace($root, "", $file); return self::getBaseUrl($path); }
/** * Set the base path where session files will be saved */ public function setSavePath($path = "") { $path = Sanitize::toPath($path); if (!empty($path)) { if (is_dir($path) || mkdir($path, 0777, true)) { $this->setOption("save_path", $path); } } }
/** * Resolve the current HTTP request path */ public static function getPath($default = "/") { $path = Utils::value(@$_SERVER["PATH_INFO"], @$_SERVER["ORIG_PATH_INFO"], ""); $path = Sanitize::toPath($path); return !empty($path) ? $path : $default; }
/** * Get the item mime/content-type string */ public function getMimeType($default = "") { $path = $this->getPath(); $ext = $this->getExtension("none"); $output = ""; if (is_dir($path)) { $output = "inode/directory"; } else { if (is_file($path) && !empty($ext)) { // try from cache if (empty($output) && !empty($this->_cache["mimetype"][$ext])) { $output = $this->_cache["mimetype"][$ext]; } // try using finfo_open if (empty($output) && function_exists("finfo_open")) { $finfo = @finfo_open(@FILEINFO_MIME_TYPE); $output = Sanitize::toPath(@finfo_file($finfo, $path)); @finfo_close($finfo); } // try using system file command if (empty($output) && function_exists("system")) { @ob_start(); @system("file -bi " . escapeshellarg($path)); $output = Sanitize::toPath(preg_replace("/\\;.*\$/ui", "", @ob_get_clean())); } // try using exif_imagetype if (empty($output) && function_exists("exif_imagetype") && ($imgtype = @exif_imagetype($path))) { $output = Sanitize::toPath(@image_type_to_mime_type($imgtype)); } } } if (!empty($output)) { $this->_cache["mimetype"][$ext] = $output; return $output; } return $default; }
/** * Set file to be read */ public function setFile($file) { if (is_string($file)) { $this->_file = Sanitize::toPath($file); } }
/** * 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; }
/** * 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; }
/** * Copy existing file to another location */ public function copy($newpath = "") { $path = $this->getPath(); $newpath = Sanitize::toPath($newpath); $parent = dirname($newpath); $output = false; if (is_file($path) && !empty($newpath)) { if (is_dir($parent) || mkdir($parent, 0777, true)) { $strin = fopen($path, "rb"); $strout = fopen($newpath, "wb"); $output = stream_copy_to_stream($strin, $strout); fclose($strin); fclose($strout); } } return $output; }
/** * Get folder items list (recursive) */ public function getRecursiveList() { $path = $this->getPath(); $output = array(); if (is_dir($path)) { $dir = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS); $items = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::CHILD_FIRST); foreach ($items as $item) { $output[] = Sanitize::toPath($item->getRealPath()); } } return $output; }
/** * Save final image */ public function save($file = "", $quality = 80) { $file = Sanitize::toPath($file); $folder = dirname($file); $saved = false; if (!empty($file) && $this->img_source !== null) { if (is_dir($folder) || mkdir($folder, 0777, true)) { @imagealphablending($this->img_source, false); @imagesavealpha($this->img_source, true); if ($this->img_type === IMAGETYPE_JPEG) { $saved = @imagejpeg($this->img_source, $file, $quality); } if ($this->img_type === IMAGETYPE_GIF) { $saved = @imagegif($this->img_source, $file); } if ($this->img_type === IMAGETYPE_PNG) { $saved = @imagepng($this->img_source, $file); } } } @imagedestroy($this->img_source); $this->img_source = null; return $saved; }
/** * Try to load a file and return it"s rendered output */ private function _load($file = "", $data = []) { $file = Sanitize::toPath($file); $output = null; if (!empty($file) && is_file($file)) { ob_start(); include $file; $output = ob_get_contents(); ob_end_clean(); } return $output; }