mirror of
https://github.com/lrsjng/h5ai.git
synced 2025-05-31 07:18:21 -04:00
More PHP refactorings. Fixes text preview.
This commit is contained in:
parent
838a346c29
commit
029872a212
8 changed files with 141 additions and 134 deletions
|
@ -1,371 +0,0 @@
|
|||
<?php
|
||||
|
||||
|
||||
define("H5AI_ABS_PATH", H5ai::normalize_path(dirname(dirname(dirname(dirname(__FILE__))))));
|
||||
|
||||
|
||||
H5ai::req_once("/conf/config.php");
|
||||
H5ai::req_once("/server/php/inc/Entry.php");
|
||||
|
||||
|
||||
class H5ai {
|
||||
|
||||
|
||||
public static final function normalize_path($path, $endWithSlash = false) {
|
||||
|
||||
$path = str_replace("\\", "/", $path);
|
||||
return preg_match("#^(\w:)?/$#", $path) ? $path : (preg_replace('#/$#', '', $path) . ($endWithSlash ? "/" : ""));
|
||||
}
|
||||
|
||||
|
||||
public static final function req_once($lib) {
|
||||
|
||||
require_once(H5AI_ABS_PATH . $lib);
|
||||
}
|
||||
|
||||
|
||||
private static final function load_config($file) {
|
||||
|
||||
if (!file_exists($file)) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$str = file_get_contents($file);
|
||||
|
||||
// remove comments to get pure json
|
||||
$str = preg_replace("/\/\*.*?\*\/|\/\/.*?(\n|$)/s", "", $str);
|
||||
|
||||
return json_decode($str, true);
|
||||
}
|
||||
|
||||
|
||||
private static final function merge_config($a, $b) {
|
||||
|
||||
$result = array_merge(array(), $a);
|
||||
|
||||
foreach ($b as $key => $value) {
|
||||
$result[$key] = array_merge($result[$key], $b[$key]);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
private static $H5AI_CONTENT_TYPE = "Content-Type: text/html;h5ai=";
|
||||
|
||||
|
||||
|
||||
|
||||
private $requested_from,
|
||||
$h5aiAbsPath,
|
||||
$rootAbsPath, $ignore_names, $ignore_patterns, $index_files,
|
||||
$config, $options,
|
||||
$rootAbsHref, $h5aiAbsHref,
|
||||
$absHref, $absPath;
|
||||
|
||||
|
||||
public function __construct($requested_from) {
|
||||
|
||||
$this->requested_from = H5ai::normalize_path($requested_from);
|
||||
|
||||
$this->h5aiAbsPath = H5ai::normalize_path(H5AI_ABS_PATH);
|
||||
$this->rootAbsPath = H5ai::normalize_path(dirname(H5AI_ABS_PATH));
|
||||
|
||||
global $H5AI_CONFIG;
|
||||
$this->ignore_names = $H5AI_CONFIG["IGNORE"];
|
||||
$this->ignore_patterns = $H5AI_CONFIG["IGNORE_PATTERNS"];
|
||||
$this->index_files = $H5AI_CONFIG["INDEX_FILES"];
|
||||
|
||||
$this->config = array("options" => array(), "types" => array(), "langs" => array());
|
||||
$this->config = H5ai::merge_config($this->config, H5ai::load_config($this->h5aiAbsPath . "/conf/config.json"));
|
||||
$this->options = $this->config["options"];
|
||||
|
||||
$this->h5aiAbsHref = H5ai::normalize_path($this->options["h5aiAbsHref"], true);
|
||||
$this->rootAbsHref = H5ai::normalize_path(dirname($this->options["h5aiAbsHref"]), true);
|
||||
|
||||
$this->absHref = H5ai::normalize_path(preg_replace('/[^\\/]*$/', '', getenv("REQUEST_URI")), true);
|
||||
$this->absPath = $this->getAbsPath($this->absHref);
|
||||
|
||||
$this->config = H5ai::merge_config($this->config, H5ai::load_config($this->absPath . "/_h5ai.config.json"));
|
||||
$this->options = $this->config["options"];
|
||||
}
|
||||
|
||||
|
||||
public function getRootAbsPath() {
|
||||
|
||||
return $this->rootAbsPath;
|
||||
}
|
||||
|
||||
|
||||
public function getH5aiAbsPath() {
|
||||
|
||||
return $this->h5aiAbsPath;
|
||||
}
|
||||
|
||||
|
||||
public function getRootAbsHref() {
|
||||
|
||||
return $this->rootAbsHref;
|
||||
}
|
||||
|
||||
|
||||
public function getH5aiAbsHref() {
|
||||
|
||||
return $this->h5aiAbsHref;
|
||||
}
|
||||
|
||||
|
||||
public function getCacheAbsPath() {
|
||||
|
||||
return $this->h5aiAbsPath . '/cache';
|
||||
}
|
||||
|
||||
|
||||
public function getCacheAbsHref() {
|
||||
|
||||
return $this->h5aiAbsHref . 'cache/';
|
||||
}
|
||||
|
||||
|
||||
public function getOptions() {
|
||||
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
|
||||
public function getAbsHref($absPath = null, $endWithSlash = true) {
|
||||
|
||||
if ($absPath === null) {
|
||||
return $this->absHref;
|
||||
}
|
||||
|
||||
$absPath = substr($absPath, strlen($this->rootAbsPath));
|
||||
|
||||
$parts = explode("/", $absPath);
|
||||
$encodedParts = array();
|
||||
foreach ($parts as $part) {
|
||||
$encodedParts[] = rawurlencode($part);
|
||||
}
|
||||
|
||||
return H5ai::normalize_path($this->rootAbsHref . implode("/", $encodedParts), $endWithSlash);
|
||||
}
|
||||
|
||||
|
||||
public function getAbsPath($absHref = null) {
|
||||
|
||||
if ($absHref === null) {
|
||||
return $this->absPath;
|
||||
}
|
||||
|
||||
$absHref = substr($absHref, strlen($this->rootAbsHref));
|
||||
|
||||
return H5ai::normalize_path($this->rootAbsPath . "/" . rawurldecode($absHref));
|
||||
}
|
||||
|
||||
|
||||
public function is_ignored($name) {
|
||||
|
||||
// always ignore
|
||||
if ($name === "." || $name === "..") {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (in_array($name, $this->ignore_names)) {
|
||||
return true;
|
||||
}
|
||||
foreach ($this->ignore_patterns as $re) {
|
||||
if (preg_match($re, $name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public function read_dir($path) {
|
||||
|
||||
$content = array();
|
||||
if (is_dir($path)) {
|
||||
if ($dir = opendir($path)) {
|
||||
while (($file = readdir($dir)) !== false) {
|
||||
if (!$this->is_ignored($file) && !$this->is_ignored($this->getAbsHref($path) . $file)) {
|
||||
$content[] = $file;
|
||||
}
|
||||
}
|
||||
closedir($dir);
|
||||
}
|
||||
}
|
||||
return $content;
|
||||
}
|
||||
|
||||
|
||||
public function getHttpCode($absHref) {
|
||||
|
||||
if (!is_dir($this->getAbsPath($absHref))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->options["folderstatus"]["enabled"]) {
|
||||
$folders = $this->options["folderstatus"]["folders"];
|
||||
if (array_key_exists($absHref, $folders)) {
|
||||
return $folders[$absHref];
|
||||
}
|
||||
}
|
||||
|
||||
// return $this->fetchHttpCode($absHref);
|
||||
return $this->guessHttpCode($absHref);
|
||||
}
|
||||
|
||||
|
||||
public function guessHttpCode($absHref) {
|
||||
|
||||
$absPath = $this->getAbsPath($absHref);
|
||||
|
||||
foreach ($this->index_files as $if) {
|
||||
if (file_exists($absPath . "/" . $if)) {
|
||||
if ($if === "index.php") {
|
||||
$fileheader = file_get_contents($absPath . "/" . $if, false, null, -1, 50);
|
||||
return stripos($fileheader, H5ai::$H5AI_CONTENT_TYPE) === false ? 200 : "h5ai";
|
||||
}
|
||||
return 200;
|
||||
}
|
||||
}
|
||||
return "h5ai";
|
||||
}
|
||||
|
||||
|
||||
public function fetchHttpCode($absHref) {
|
||||
|
||||
$host = getenv("HTTP_HOST");
|
||||
$port = getenv("SERVER_PORT");
|
||||
$msg = "HEAD $absHref HTTP/1.1\r\nHost: $host\r\nConnection: Close\r\n";
|
||||
if (isset($_SERVER['PHP_AUTH_USER'])) {
|
||||
$msg .= "Authorization: Basic " . base64_encode($_SERVER['PHP_AUTH_USER'] . ":" . $_SERVER['PHP_AUTH_PW']) . "\r\n";
|
||||
}
|
||||
$msg .= "\r\n";
|
||||
|
||||
$errno = "";
|
||||
$errstr = "";
|
||||
$socket = fsockopen($host, $port, $errno, $errstr, 30);
|
||||
if($socket === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
fwrite($socket, $msg);
|
||||
$content = fgets($socket);
|
||||
$code = intval(trim(substr($content, 9, 4)));
|
||||
if ($code === 200) {
|
||||
while ($content !== false && stripos($content, "Content-Type") === false) {
|
||||
$content = fgets($socket);
|
||||
}
|
||||
if (stripos($content, H5ai::$H5AI_CONTENT_TYPE) !== false) {
|
||||
$code = "h5ai";
|
||||
}
|
||||
}
|
||||
fclose($socket);
|
||||
|
||||
return $code;
|
||||
}
|
||||
|
||||
|
||||
private function fileExists($file) {
|
||||
|
||||
return is_string($file) && file_exists($file);
|
||||
}
|
||||
|
||||
|
||||
public function getGenericJson() {
|
||||
|
||||
$entries = $this->getEntries($this->absHref, 1);
|
||||
|
||||
$header = $this->options["custom"]["header"];
|
||||
$footer = $this->options["custom"]["footer"];
|
||||
$header = $this->fileExists($header ? $this->absPath . "/" . $header : null) ? $header : null;
|
||||
$footer = $this->fileExists($footer ? $this->absPath . "/" . $footer : null) ? $footer : null;
|
||||
|
||||
$json = array(
|
||||
"id" => $this->requested_from === $this->h5aiAbsPath . "/server/php/index.php" ? "php" : "idx.php",
|
||||
"serverName" => strtolower(preg_replace("/\\/.*$/", "", getenv("SERVER_SOFTWARE"))),
|
||||
"serverVersion" => strtolower(preg_replace("/^.*\\//", "", preg_replace("/\\s.*$/", "", getenv("SERVER_SOFTWARE")))),
|
||||
"customHeader" => $header,
|
||||
"customFooter" => $footer,
|
||||
"entries" => $entries
|
||||
);
|
||||
|
||||
return json_encode($json) . "\n";
|
||||
}
|
||||
|
||||
|
||||
public function getCustomConfig() {
|
||||
|
||||
$config = "_h5ai.config.json";
|
||||
$config = $this->fileExists($config ? $this->absPath . "/" . $config : "ignore") ? $config : "ignore";
|
||||
return $config;
|
||||
}
|
||||
|
||||
|
||||
public function getEntries($absHref, $content) {
|
||||
|
||||
$folder = Entry::get($this, $this->getAbsPath($absHref), $absHref);
|
||||
if ($content > 1 && $folder !== null) {
|
||||
foreach ($folder->getContent() as $entry) {
|
||||
$entry->getContent();
|
||||
}
|
||||
$folder = $folder->getParent();
|
||||
}
|
||||
while ($content > 0 && $folder !== null) {
|
||||
$folder->getContent();
|
||||
$folder = $folder->getParent();
|
||||
}
|
||||
Entry::sort();
|
||||
|
||||
$entries = array();
|
||||
foreach (Entry::get_cache() as $entry) {
|
||||
$entries[] = $entry->toJsonObject(true);
|
||||
}
|
||||
|
||||
return $entries;
|
||||
}
|
||||
|
||||
|
||||
public function getNoJsFallback() {
|
||||
|
||||
date_default_timezone_set ("UTC");
|
||||
|
||||
function _cmp($entry1, $entry2) {
|
||||
|
||||
if ($entry1->isFolder && !$entry2->isFolder) {
|
||||
return -1;
|
||||
}
|
||||
if (!$entry1->isFolder && $entry2->isFolder) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return strcasecmp($entry1->absHref, $entry2->absHref);
|
||||
}
|
||||
|
||||
$folder = Entry::get($this, $this->absPath, $this->absHref);
|
||||
$entries = $folder->getContent();
|
||||
uasort($entries, "_cmp");
|
||||
|
||||
$html = "<table>";
|
||||
$html .= "<tr><th></th><th><span>Name</span></th><th><span>Last modified</span></th><th><span>Size</span></th></tr>";
|
||||
if ($folder->parent) {
|
||||
$html .= "<tr><td></td><td><a href=\"..\">Parent Directory</a></td><td></td><td></td></tr>";
|
||||
}
|
||||
foreach ($entries as $entry) {
|
||||
$html .= "<tr>";
|
||||
$html .= "<td></td>";
|
||||
$html .= "<td><a href=\"" . $entry->absHref . "\">" . basename($entry->absPath) . ($entry->isFolder ? "/" : "") . "</a></td>";
|
||||
$html .= "<td>" . date("Y-m-d H:i", $entry->date) . "</td>";
|
||||
$html .= "<td>" . ($entry->size !== null ? intval($entry->size / 1000) . " KB" : "" ) . "</td>";
|
||||
$html .= "</tr>";
|
||||
}
|
||||
$html .= "</table>";
|
||||
|
||||
return $html;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Add table
Add a link
Reference in a new issue