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') {
68 $str .= $hostProp . '=' . $hostVal . "\n";
71 $cfgFilePath = $this->getConfigFilePath();
72 $cfgDir = dirname($cfgFilePath);
73 if (!is_dir($cfgDir)) {
76 file_put_contents($cfgFilePath, $str);
77 //contains sensitive data; nobody else may read that
78 chmod($cfgFilePath, 0600);
81 public function getDefaultHost()
83 if (!count($this->hosts)) {
86 foreach ($this->hosts as $key => $host) {
93 return key($this->hosts);
96 public function getHostByName($keyOrServer)
98 if (!count($this->hosts)) {
101 foreach ($this->hosts as $key => $host) {
102 if ($key == $keyOrServer || $host->server == $keyOrServer) {