Finish web interface for admin
[infodrom/musiikki-web.git] / class / config.class.php
1 <?php
2
3 class Config {
4   const PATH = '/etc/musiikki.conf';
5   const SHARE_READ = 'musicread';
6   const SHARE_WRITE = 'musicwrite';
7
8   private static $main = false;
9
10   private $values = array();
11
12   public static function main()
13   {
14     if (self::$main === false)
15       self::$main = new Config(Config::PATH);
16
17     return self::$main;
18   }
19
20   private function __construct($path)
21   {
22     $this->loadValues($path);
23   }
24
25   private function loadValues($path)
26   {
27     if (!is_readable($path))
28       throw new Exception('Cannot open config file ' . $path);
29
30     $f = fopen($path, 'r');
31     if ($f === false)
32       throw new Exception('Cannot open config file ' . $path);
33
34     while (($line = fgets($f)) !== false) {
35       $line = trim($line);
36       if (substr($line,0,1) == '#') continue;
37       if (!strlen($line)) continue;
38
39       $parts = explode('=', $line, 2);
40       $this->values[$parts[0]] = $parts[1];
41     }
42
43     fclose($f);
44   }
45
46   public function get($name)
47   {
48     if (!array_key_exists($name, $this->values))
49       return NULL;
50
51     return $this->values[$name];
52   }
53 }