Quote values in .ini file
[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
69                     . '=' . static::quoteIniValue($hostVal) . "\n";
70             }
71         }
72         $cfgFilePath = $this->getConfigFilePath();
73         $cfgDir = dirname($cfgFilePath);
74         if (!is_dir($cfgDir)) {
75             mkdir($cfgDir, 0700);
76         }
77         file_put_contents($cfgFilePath, $str);
78         //contains sensitive data; nobody else may read that
79         chmod($cfgFilePath, 0600);
80     }
81
82     public function getDefaultHost()
83     {
84         if (!count($this->hosts)) {
85             return null;
86         }
87         foreach ($this->hosts as $key => $host) {
88             if ($host->default) {
89                 return $host;
90             }
91         }
92         
93         reset($this->hosts);
94         return key($this->hosts);
95     }
96
97     public function getHostByName($keyOrServer)
98     {
99         if (!count($this->hosts)) {
100             return null;
101         }
102         foreach ($this->hosts as $key => $host) {
103             if ($key == $keyOrServer || $host->server == $keyOrServer) {
104                 return $key;
105             }
106         }
107         return null;
108     }
109
110     public static function quoteIniValue($val)
111     {
112         if (strpos($val, '=') === false) {
113             return $val;
114         }
115         return '"' . $val . '"';
116     }
117 }
118 ?>