9 * Currently selected host.
15 public $debug = false;
17 public $dryRun = false;
19 protected function getConfigFilePath()
21 if (!isset($_SERVER['HOME'])) {
22 Log::err('Cannot determine home directory');
26 return $_SERVER['HOME'] . '/.config/shpub.ini';
29 public function load()
31 $cfgFile = $this->getConfigFilePath();
32 if ($cfgFile == false) {
36 if (!file_exists($cfgFile) || !is_readable($cfgFile)) {
40 $data = parse_ini_file($cfgFile, true);
41 foreach ($data as $key => $val) {
42 if (!is_array($val)) {
45 $host = new Config_Host();
46 foreach ($val as $hostProp => $hostVal) {
47 if (!property_exists($host, $hostProp)) {
48 Log::err('Invalid config key "' . $hostProp . '"');
51 $host->$hostProp = $hostVal;
53 $this->hosts[$key] = $host;
57 public function save()
60 foreach ($this->hosts as $hostName => $host) {
64 $str .= '[' . $hostName . "]\n";
65 foreach ($host as $hostProp => $hostVal) {
66 if ($hostProp == 'endpoints') {
73 . '=' . static::quoteIniValue($hostVal) . "\n";
76 $cfgFilePath = $this->getConfigFilePath();
77 $cfgDir = dirname($cfgFilePath);
78 if (!is_dir($cfgDir)) {
81 file_put_contents($cfgFilePath, $str);
82 //contains sensitive data; nobody else may read that
83 chmod($cfgFilePath, 0600);
86 public function getDefaultHost()
88 if (!count($this->hosts)) {
91 foreach ($this->hosts as $key => $host) {
98 return key($this->hosts);
101 public function getHostByName($keyOrServer)
103 if (!count($this->hosts)) {
106 foreach ($this->hosts as $key => $host) {
107 if ($key == $keyOrServer || $host->server == $keyOrServer) {
114 public static function quoteIniValue($val)
116 if (strpos($val, '=') === false) {
119 return '"' . $val . '"';
122 public function setDebug($debug)
124 $this->debug = $debug;
127 public function setDryRun($dryRun)
129 $this->dryRun = $dryRun;