Add rsvp command
[shpub.git] / src / shpub / Config.php
1 <?php
2 namespace shpub;
3
4 class Config
5 {
6     public $hosts = [];
7
8     /**
9      * Currently selected host.
10      *
11      * @var Host
12      */
13     public $host;
14
15     public $debug = false;
16
17     protected function getConfigFilePath()
18     {
19         if (!isset($_SERVER['HOME'])) {
20             Log::err('Cannot determine home directory');
21             return false;
22         }
23
24         return $_SERVER['HOME'] . '/.config/shpub.ini';
25     }
26
27     public function load()
28     {
29         $cfgFile = $this->getConfigFilePath();
30         if ($cfgFile == false) {
31             return false;
32         }
33
34         if (!file_exists($cfgFile) || !is_readable($cfgFile)) {
35             return false;
36         }
37
38         $data = parse_ini_file($cfgFile, true);
39         foreach ($data as $key => $val) {
40             if (!is_array($val)) {
41                 continue;
42             }
43             $host = new Config_Host();
44             foreach ($val as $hostProp => $hostVal) {
45                 if (!property_exists($host, $hostProp)) {
46                     Log::err('Invalid config key "' . $hostProp . '"');
47                     exit(1);
48                 }
49                 $host->$hostProp = $hostVal;
50             }
51             $this->hosts[$key] = $host;
52         }
53     }
54
55     public function save()
56     {
57         $str = '';
58         foreach ($this->hosts as $hostName => $host) {
59             if ($str != '') {
60                 $str .= "\n";
61             }
62             $str .= '[' . $hostName . "]\n";
63             foreach ($host as $hostProp => $hostVal) {
64                 if ($hostProp == 'endpoints') {
65                     continue;
66                 }
67                 if ($hostVal == '') {
68                     continue;
69                 }
70                 $str .= $hostProp
71                     . '=' . static::quoteIniValue($hostVal) . "\n";
72             }
73         }
74         $cfgFilePath = $this->getConfigFilePath();
75         $cfgDir = dirname($cfgFilePath);
76         if (!is_dir($cfgDir)) {
77             mkdir($cfgDir, 0700);
78         }
79         file_put_contents($cfgFilePath, $str);
80         //contains sensitive data; nobody else may read that
81         chmod($cfgFilePath, 0600);
82     }
83
84     public function getDefaultHost()
85     {
86         if (!count($this->hosts)) {
87             return null;
88         }
89         foreach ($this->hosts as $key => $host) {
90             if ($host->default) {
91                 return $key;
92             }
93         }
94         
95         reset($this->hosts);
96         return key($this->hosts);
97     }
98
99     public function getHostByName($keyOrServer)
100     {
101         if (!count($this->hosts)) {
102             return null;
103         }
104         foreach ($this->hosts as $key => $host) {
105             if ($key == $keyOrServer || $host->server == $keyOrServer) {
106                 return $key;
107             }
108         }
109         return null;
110     }
111
112     public static function quoteIniValue($val)
113     {
114         if (strpos($val, '=') === false) {
115             return $val;
116         }
117         return '"' . $val . '"';
118     }
119
120     public function setDebug($debug)
121     {
122         $this->debug = $debug;
123     }
124 }
125 ?>