2 error_reporting(E_ALL);
8 function __construct($config) {
9 if(empty($config['certificate_file']))
10 die("Missing certificate file. Edit config.php\n");
13 $this->data_dir = "hosts/";
14 $this->default_host_dir = "default/";
15 $this->default_index_file = "index.gemini";
17 $this->log_file = "logs/gemini-php.log";
18 $this->log_sep = "\t";
19 $settings = array('ip', 'port', 'data_dir', 'default_host_dir', 'default_index_file',
20 'certificate_file', 'certificate_passphrase');
21 foreach($settings as $setting_key) {
22 if(!empty($config[$setting_key]))
23 $this->$setting_key = $config[$setting_key];
25 // append the required filepath slashes if they're missing
26 if(substr($this->data_dir, -1) != "/")
27 $this->data_dir .= "/";
28 if(substr($this->default_host_dir, -1) != "/")
29 $this->default_host_dir .= "/";
31 if(!file_exists($this->log_file)) {
32 $this->log_to_file("Log created", null, null, null, null);
34 if(!is_writable($this->log_file)) {
35 die("{$this->log_file} is not writable.\n");
38 if(!is_readable($this->certificate_file))
39 die("Certificate file {$this->certificate_file} not readable.\n");
42 function parse_request($request) {
43 $data = parse_url($request);
47 function get_valid_hosts() {
48 $dirs = array_map('basename', glob($this->data_dir.'*', GLOB_ONLYDIR));
52 function get_host_url($host) {
58 function get_status_code($filepath) {
59 if(is_file($filepath) and file_exists($filepath))
61 if(!file_exists($filepath))
66 function get_mime_type($filepath) {
67 $type = mime_content_type($filepath);
68 // we need a way to detect gemini file types, which PHP doesn't
69 // so.. if it ends with gemini (or if it has no extension), assume
70 $path_parts = pathinfo($filepath);
71 if(empty($path_parts['extension']) or $path_parts['extension'] == "gemini")
72 $type = "text/gemini";
77 * Gets the full file path (assumes directory structure based on host)
79 * This function determines where the requested file is stored
80 * based on the hostname supplied in the request from the client.
81 * If no host is supplied, the default directory is assumed.
83 * @param array $url An array returned by the parse_request() method
87 function get_filepath($url) {
91 if(!empty($url['host']))
92 $hostname = $url['host'];
94 $valid_hosts = $this->get_valid_hosts();
95 if(!in_array($hostname, $valid_hosts))
96 $hostname = "default";
98 // Kristall Browser is adding "__" to the end of the filenames
100 $url['path'] = str_replace("__", "", $url['path']);
101 if(substr($url['path'], -1) == "/" or $url['path'] == "/" or $url['path'] == "")
102 $url['path'] = "/".$this->default_index_file;
104 return $this->data_dir.$hostname.$url['path'];
107 function log_to_file($ip, $status_code, $meta, $filepath, $filesize) {
108 $ts = date("Y-m-d H:i:s", strtotime('now'));
110 $str = $ts.$this->log_sep.$ip.$this->log_sep.$status_code.$this->log_sep.
111 $meta.$this->log_sep.$filepath.$this->log_sep.$filesize."\n";
112 file_put_contents($this->log_file, $str, FILE_APPEND);