9 * Currently selected host.
15 public $debug = false;
17 protected function getConfigFilePath()
19 if (!isset($_SERVER['HOME'])) {
20 Log::err('Cannot determine home directory');
24 return $_SERVER['HOME'] . '/.config/shpub.ini';
27 public function load()
29 $cfgFile = $this->getConfigFilePath();
30 if ($cfgFile == false) {
34 if (!file_exists($cfgFile) || !is_readable($cfgFile)) {
38 $data = parse_ini_file($cfgFile, true);
39 foreach ($data as $key => $val) {
40 if (!is_array($val)) {
43 $host = new Config_Host();
44 foreach ($val as $hostProp => $hostVal) {
45 if (!property_exists($host, $hostProp)) {
46 Log::err('Invalid config key "' . $hostProp . '"');
49 $host->$hostProp = $hostVal;
51 $this->hosts[$key] = $host;
55 public function save()
58 foreach ($this->hosts as $hostName => $host) {
62 $str .= '[' . $hostName . "]\n";
63 foreach ($host as $hostProp => $hostVal) {
64 if ($hostProp == 'endpoints') {
71 . '=' . static::quoteIniValue($hostVal) . "\n";
74 $cfgFilePath = $this->getConfigFilePath();
75 $cfgDir = dirname($cfgFilePath);
76 if (!is_dir($cfgDir)) {
79 file_put_contents($cfgFilePath, $str);
80 //contains sensitive data; nobody else may read that
81 chmod($cfgFilePath, 0600);
84 public function getDefaultHost()
86 if (!count($this->hosts)) {
89 foreach ($this->hosts as $key => $host) {
96 return key($this->hosts);
99 public function getHostByName($keyOrServer)
101 if (!count($this->hosts)) {
104 foreach ($this->hosts as $key => $host) {
105 if ($key == $keyOrServer || $host->server == $keyOrServer) {
112 public static function quoteIniValue($val)
114 if (strpos($val, '=') === false) {
117 return '"' . $val . '"';
120 public function setDebug($debug)
122 $this->debug = $debug;