Add logs and config.php to gitignore
[gemini-php.git/.git] / gemini.class.php
1 <?php
2 error_reporting(E_ALL);
3 set_time_limit(0);
4 ob_implicit_flush();
5
6 class Gemini {
7
8         function __construct($config) {
9                 if(empty($config['certificate_file']))
10                         die("Missing certificate file.  Edit config.php");
11                 $this->ip = "0";
12                 $this->port = "1965";
13                 $this->data_dir = "hosts/";
14                 $this->default_host_dir = "default/";
15                 $this->default_index_file = "index.gemini";
16                 $this->logging = 1;
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];
24                 }
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 .= "/";
30                 if($this->logging) {
31                         if(!file_exists($this->log_file)) {
32                                 $this->log_to_file("New log file created");
33                         }
34                         if(!is_writable($this->log_file)) {
35                                 die("{$this->log_file} is not writable.");
36                         }
37                 }
38         }
39
40         function parse_request($request) {
41                 $data = parse_url($request);
42                 return $data;
43         }
44
45         function get_valid_hosts() {
46                 $dirs = array_map('basename', glob($this->data_dir.'*', GLOB_ONLYDIR));
47                 return $dirs;
48         }
49
50         function get_host_url($host) {
51                 if(empty($host))
52                         return 'default';
53                 
54         }
55
56         function get_status_code($filepath) {
57                 if(is_file($filepath) and file_exists($filepath))
58                         return "20";
59                 if(!file_exists($filepath))
60                         return "51";
61                 return "50";
62         }
63
64         function get_mime_type($filepath) {
65                 $type = mime_content_type($filepath);
66                 // we need a way to detect gemini file types, which PHP doesn't
67                 // so.. if it ends with gemini (or if it has no extension), assume
68                 $path_parts = pathinfo($filepath);
69                 if(empty($path_parts['extension']) or $path_parts['extension'] == "gemini")
70                         $type = "text/gemini";
71                 return $type;
72         }
73
74         /**
75         * Gets the full file path (assumes directory structure based on host)
76         *
77         * This function determines where the requested file is stored
78         * based on the hostname supplied in the request from the client.
79         * If no host is supplied, the default directory is assumed.
80         *
81         * @param array $url An array returned by the parse_request() method
82         *
83         * @return string
84         */
85         function get_filepath($url) {
86                 $hostname = "";
87                 if(!is_array($url))
88                         return false;
89                 if(!empty($parsed_url['host']))
90                         $hostname = $parsed_url['host'];
91
92                 $valid_hosts = $this->get_valid_hosts();
93                 if(!in_array($hostname, $valid_hosts))
94                         $hostname = "default";
95
96                 // Kristall Browser is adding "__" to the end of the filenames
97                 // wtf am I missing?
98                 $url['path'] = str_replace("__", "", $url['path']);
99                 if($url['path'] == "" or $url['path'] == "/")
100                         $url['path'] = "/".$this->default_index_file;
101
102                 return $this->data_dir.$hostname.$url['path'];
103         }
104
105         function log_to_file($ip, $status_code, $meta, $filepath, $filesize) {
106                 $ts = date("Y-m-d H:i:s", strtotime('now'));
107                 $this->log_sep;
108                 $str = $ts.$this->log_sep.$ip.$this->log_sep.$status_code.$this->log_sep.
109                 $meta.$this->log_sep.$filepath.$this->log_sep.$filesize."\n";
110                 file_put_contents($this->log_file, $str, FILE_APPEND);
111         }
112 }
113
114 ?>