9 * Currently selected host.
15 protected function getConfigFilePath()
17 if (!isset($_SERVER['HOME'])) {
18 Log::err('Cannot determine home directory');
22 return $_SERVER['HOME'] . '/.config/shpub.ini';
25 public function load()
27 $cfgFile = $this->getConfigFilePath();
28 if ($cfgFile == false) {
32 if (!file_exists($cfgFile) || !is_readable($cfgFile)) {
36 $data = parse_ini_file($cfgFile, true);
37 foreach ($data as $key => $val) {
38 if (!is_array($val)) {
41 $host = new Config_Host();
42 foreach ($val as $hostProp => $hostVal) {
43 if (!property_exists($host, $hostProp)) {
44 Log::err('Invalid config key "' . $hostProp . '"');
47 $host->$hostProp = $hostVal;
49 $this->hosts[$key] = $host;
53 public function save()
56 foreach ($this->hosts as $hostName => $host) {
60 $str .= '[' . $hostName . "]\n";
61 foreach ($host as $hostProp => $hostVal) {
62 if ($hostProp == 'endpoints') {
69 . '=' . static::quoteIniValue($hostVal) . "\n";
72 $cfgFilePath = $this->getConfigFilePath();
73 $cfgDir = dirname($cfgFilePath);
74 if (!is_dir($cfgDir)) {
77 file_put_contents($cfgFilePath, $str);
78 //contains sensitive data; nobody else may read that
79 chmod($cfgFilePath, 0600);
82 public function getDefaultHost()
84 if (!count($this->hosts)) {
87 foreach ($this->hosts as $key => $host) {
94 return key($this->hosts);
97 public function getHostByName($keyOrServer)
99 if (!count($this->hosts)) {
102 foreach ($this->hosts as $key => $host) {
103 if ($key == $keyOrServer || $host->server == $keyOrServer) {
110 public static function quoteIniValue($val)
112 if (strpos($val, '=') === false) {
115 return '"' . $val . '"';