9 * Currently selected host.
15 protected function getConfigFilePath()
17 if (!isset($_SERVER['HOME'])) {
21 return $_SERVER['HOME'] . '/.config/shpub.ini';
24 public function load()
26 $cfgFile = $this->getConfigFilePath();
27 if ($cfgFile == false) {
31 if (!file_exists($cfgFile) || !is_readable($cfgFile)) {
35 $data = parse_ini_file($cfgFile, true);
36 foreach ($data as $key => $val) {
37 if (!is_array($val)) {
40 $host = new Config_Host();
41 foreach ($val as $hostProp => $hostVal) {
42 if (!property_exists($host, $hostProp)) {
43 Log::err('Invalid config key "' . $hostProp . '"');
46 $host->$hostProp = $hostVal;
48 $this->hosts[$key] = $host;
52 public function save()
55 foreach ($this->hosts as $hostName => $host) {
59 $str .= '[' . $hostName . "]\n";
60 foreach ($host as $hostProp => $hostVal) {
61 if ($hostProp == 'endpoints') {
67 $str .= $hostProp . '=' . $hostVal . "\n";
70 $cfgFilePath = $this->getConfigFilePath();
71 $cfgDir = dirname($cfgFilePath);
72 if (!is_dir($cfgDir)) {
75 file_put_contents($cfgFilePath, $str);
76 //contains sensitive data; nobody else may read that
77 chmod($cfgFilePath, 0600);
80 public function getDefaultHost()
82 if (!count($this->hosts)) {
85 foreach ($this->hosts as $key => $host) {
92 return key($this->hosts);
95 public function getHostByName($keyOrServer)
97 if (!count($this->hosts)) {
100 foreach ($this->hosts as $key => $host) {
101 if ($key == $keyOrServer || $host->server == $keyOrServer) {