352ab99da68b8194b865e841fa2486a338762e70
[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     protected function getConfigFilePath()
16     {
17         if (!isset($_SERVER['HOME'])) {
18             Log::err('Cannot determine home directory');
19             return false;
20         }
21
22         return $_SERVER['HOME'] . '/.config/shpub.ini';
23     }
24
25     public function load()
26     {
27         $cfgFile = $this->getConfigFilePath();
28         if ($cfgFile == false) {
29             return false;
30         }
31
32         if (!file_exists($cfgFile) || !is_readable($cfgFile)) {
33             return false;
34         }
35
36         $data = parse_ini_file($cfgFile, true);
37         foreach ($data as $key => $val) {
38             if (!is_array($val)) {
39                 continue;
40             }
41             $host = new Config_Host();
42             foreach ($val as $hostProp => $hostVal) {
43                 if (!property_exists($host, $hostProp)) {
44                     Log::err('Invalid config key "' . $hostProp . '"');
45                     exit(1);
46                 }
47                 $host->$hostProp = $hostVal;
48             }
49             $this->hosts[$key] = $host;
50         }
51     }
52
53     public function save()
54     {
55         $str = '';
56         foreach ($this->hosts as $hostName => $host) {
57             if ($str != '') {
58                 $str .= "\n";
59             }
60             $str .= '[' . $hostName . "]\n";
61             foreach ($host as $hostProp => $hostVal) {
62                 if ($hostProp == 'endpoints') {
63                     continue;
64                 }
65                 if ($hostVal == '') {
66                     continue;
67                 }
68                 $str .= $hostProp . '=' . $hostVal . "\n";
69             }
70         }
71         $cfgFilePath = $this->getConfigFilePath();
72         $cfgDir = dirname($cfgFilePath);
73         if (!is_dir($cfgDir)) {
74             mkdir($cfgDir, 0700);
75         }
76         file_put_contents($cfgFilePath, $str);
77         //contains sensitive data; nobody else may read that
78         chmod($cfgFilePath, 0600);
79     }
80
81     public function getDefaultHost()
82     {
83         if (!count($this->hosts)) {
84             return null;
85         }
86         foreach ($this->hosts as $key => $host) {
87             if ($host->default) {
88                 return $host;
89             }
90         }
91         
92         reset($this->hosts);
93         return key($this->hosts);
94     }
95
96     public function getHostByName($keyOrServer)
97     {
98         if (!count($this->hosts)) {
99             return null;
100         }
101         foreach ($this->hosts as $key => $host) {
102             if ($key == $keyOrServer || $host->server == $keyOrServer) {
103                 return $key;
104             }
105         }
106         return null;
107     }
108 }
109 ?>