f0550526484d52831500a09754067095b568d415
[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\n");
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("Log created", null, null, null, null);
33                         }
34                         if(!is_writable($this->log_file)) {
35                                 die("{$this->log_file} is not writable.\n");
36                         }
37                 }
38                 if(!is_readable($this->certificate_file))
39                         die("Certificate file {$this->certificate_file} not readable.\n");
40         }
41
42         function parse_request($request) {
43                 $data = parse_url($request);
44                 return $data;
45         }
46
47         function get_valid_hosts() {
48                 $dirs = array_map('basename', glob($this->data_dir.'*', GLOB_ONLYDIR));
49                 return $dirs;
50         }
51
52         function get_host_url($host) {
53                 if(empty($host))
54                         return 'default';
55                 
56         }
57
58         function get_status_code($filepath) {
59                 if(is_file($filepath) and file_exists($filepath))
60                         return "20";
61                 if(!file_exists($filepath))
62                         return "51";
63                 return "50";
64         }
65
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";
73                 return $type;
74         }
75
76         /**
77         * Gets the full file path (assumes directory structure based on host)
78         *
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.
82         *
83         * @param array $url An array returned by the parse_request() method
84         *
85         * @return string
86         */
87         function get_filepath($url) {
88                 $hostname = "";
89                 if(!is_array($url))
90                         return false;
91                 if(!empty($url['host']))
92                         $hostname = $url['host'];
93
94                 $valid_hosts = $this->get_valid_hosts();
95                 if(!in_array($hostname, $valid_hosts))
96                         $hostname = "default";
97
98                 // Kristall Browser is adding "__" to the end of the filenames
99                 // wtf am I missing?
100                 $url['path'] = str_replace("__", "", $url['path']);
101                 // force an index file to be appended if a filename is missing
102                 if(substr($url['path'], -1) == "/") {
103                         $url['path'] .= $this->default_index_file;
104                 } elseif($url['path'] == "/" or $url['path'] == "") {
105                         $url['path'] = "/".$this->default_index_file;
106                 }
107
108                 return $this->data_dir.$hostname.$url['path'];
109         }
110
111         function log_to_file($ip, $status_code, $meta, $filepath, $filesize) {
112                 $ts = date("Y-m-d H:i:s", strtotime('now'));
113                 $this->log_sep;
114                 $str = $ts.$this->log_sep.$ip.$this->log_sep.$status_code.$this->log_sep.
115                 $meta.$this->log_sep.$filepath.$this->log_sep.$filesize."\n";
116                 file_put_contents($this->log_file, $str, FILE_APPEND);
117         }
118 }
119
120 ?>