From 538dfdaf580f2be31a4be2cd29a1bb543087a816 Mon Sep 17 00:00:00 2001 From: seven Date: Mon, 28 Sep 2020 00:45:55 +0100 Subject: [PATCH] Initial commit --- gemini.class.php | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 gemini.class.php diff --git a/gemini.class.php b/gemini.class.php new file mode 100644 index 0000000..8b4fdf3 --- /dev/null +++ b/gemini.class.php @@ -0,0 +1,74 @@ +data_dir = "hosts/"; + $this->default_host_dir = "default/"; + } + + function parse_request($request) { + $data = parse_url($request); + return $data; + } + + function get_valid_hosts() { + $dirs = array_map('basename', glob($this->data_dir.'*', GLOB_ONLYDIR)); + return $dirs; + } + + function get_dir($parsed_url) { + } + + function get_host_url($host) { + if(empty($host)) + return 'default'; + + } + + function get_status_code($filepath) { + if(is_file($filepath) and file_exists($filepath)) + return "20"; + if(!file_exists($filepath)) + return "51"; + return "50"; + } + + function get_mime_type($filepath) { + return "text/gemini"; + } + + /** + * Gets the full file path (assumes directory structure based on host) + * + * This function determines where the requested file is stored + * based on the hostname supplied in the request from the client. + * If no host is supplied, the default directory is assumed. + * + * @param array $url An array returned by the parse_request() method + * + * @return string + */ + function get_filepath($url) { + $hostname = ""; + if(!is_array($url)) + return false; + if(!empty($parsed_url['host'])) + $hostname = $parsed_url['host']; + + $valid_hosts = $this->get_valid_hosts(); + if(!in_array($hostname, $valid_hosts)) + $hostname = "default"; + + // Kristall Browser is adding "__" to the end of the filenames + // wtf am I missing? + $url['path'] = str_replace("__", "", $url['path']); + + return $this->data_dir.$hostname.$url['path']; + } +} + +?>