Detect PHP flexibly, assert wland configuration
[infodrom/musiikki-web.git] / class / config.class.php
1 <?php
2
3 class Config {
4   const PATH = '/etc/musiikki.conf';
5   const PATH_DLNA = '/etc/minidlna.conf';
6   const SHARE_READ = 'musicread';
7   const SHARE_WRITE = 'musicwrite';
8
9   private static $main = false;
10
11   private $values = array();
12
13   public static function main()
14   {
15     if (self::$main === false)
16       self::$main = new Config(Config::PATH);
17
18     return self::$main;
19   }
20
21   private function __construct($path)
22   {
23     $this->loadValues($path);
24     $this->loadMiniDLNA(static::PATH_DLNA);
25   }
26
27   private function loadValues($path)
28   {
29     if (!is_readable($path))
30       throw new Exception('Cannot open config file ' . $path);
31
32     $f = fopen($path, 'r');
33     if ($f === false)
34       throw new Exception('Cannot open config file ' . $path);
35
36     while (($line = fgets($f)) !== false) {
37       $line = trim($line);
38       if (substr($line,0,1) == '#') continue;
39       if (!strlen($line)) continue;
40
41       $parts = explode('=', $line, 2);
42       $this->values[$parts[0]] = $parts[1];
43     }
44
45     fclose($f);
46   }
47
48   private function loadMiniDLNA($path)
49   {
50     if (!is_readable($path))
51       throw new Exception('Cannot open config file ' . $path);
52
53     $f = fopen($path, 'r');
54     if ($f === false)
55       throw new Exception('Cannot open config file ' . $path);
56
57     while (($line = fgets($f)) !== false) {
58       $line = trim($line);
59       if (substr($line,0,1) == '#') continue;
60       if (!strlen($line)) continue;
61       if (substr($line,0,10) != 'media_dir=') continue;
62
63       $parts = explode('=', $line, 2);
64       $details = explode(',', $parts[1], 2);
65       $this->values[$parts[0].'_'.$details[0]] = $details[1];
66     }
67
68     fclose($f);
69   }
70
71   public function get($name)
72   {
73     if (!array_key_exists($name, $this->values))
74       return NULL;
75
76     return $this->values[$name];
77   }
78 }