diff --git a/gemini.class.php b/gemini.class.php index 8b4fdf3..a6c5fdb 100644 --- a/gemini.class.php +++ b/gemini.class.php @@ -5,9 +5,36 @@ ob_implicit_flush(); class Gemini { - function __construct() { + function __construct($config) { + if(empty($config['certificate_file'])) + die("Missing certificate file. Edit config.php"); + $this->ip = "0"; + $this->port = "1965"; $this->data_dir = "hosts/"; $this->default_host_dir = "default/"; + $this->default_index_file = "index.gemini"; + $this->logging = 1; + $this->log_file = "logs/gemini-php.log"; + $this->log_sep = "\t"; + $settings = array('ip', 'port', 'data_dir', 'default_host_dir', 'default_index_file', + 'certificate_file', 'certificate_passphrase'); + foreach($settings as $setting_key) { + if(!empty($config[$setting_key])) + $this->$setting_key = $config[$setting_key]; + } + // append the required filepath slashes if they're missing + if(substr($this->data_dir, -1) != "/") + $this->data_dir .= "/"; + if(substr($this->default_host_dir, -1) != "/") + $this->default_host_dir .= "/"; + if($this->logging) { + if(!file_exists($this->log_file)) { + $this->log_to_file("New log file created"); + } + if(!is_writable($this->log_file)) { + die("{$this->log_file} is not writable."); + } + } } function parse_request($request) { @@ -20,9 +47,6 @@ class Gemini { return $dirs; } - function get_dir($parsed_url) { - } - function get_host_url($host) { if(empty($host)) return 'default'; @@ -38,7 +62,13 @@ class Gemini { } function get_mime_type($filepath) { - return "text/gemini"; + $type = mime_content_type($filepath); + // we need a way to detect gemini file types, which PHP doesn't + // so.. if it ends with gemini (or if it has no extension), assume + $path_parts = pathinfo($filepath); + if(empty($path_parts['extension']) or $path_parts['extension'] == "gemini") + $type = "text/gemini"; + return $type; } /** @@ -66,9 +96,19 @@ class Gemini { // Kristall Browser is adding "__" to the end of the filenames // wtf am I missing? $url['path'] = str_replace("__", "", $url['path']); + if($url['path'] == "" or $url['path'] == "/") + $url['path'] = "/".$this->default_index_file; return $this->data_dir.$hostname.$url['path']; } + + function log_to_file($ip, $status_code, $meta, $filepath, $filesize) { + $ts = date("Y-m-d H:i:s", strtotime('now')); + $this->log_sep; + $str = $ts.$this->log_sep.$ip.$this->log_sep.$status_code.$this->log_sep. + $meta.$this->log_sep.$filepath.$this->log_sep.$filesize."\n"; + file_put_contents($this->log_file, $str, FILE_APPEND); + } } ?>