| | 99 | } |
|---|
| | 100 | |
|---|
| | 101 | function initializeMidgard($init_file) |
|---|
| | 102 | { |
|---|
| | 103 | mgd_config_init($init_file); |
|---|
| | 104 | if (!isset($_MIDGARD)) |
|---|
| | 105 | { |
|---|
| | 106 | PEAR::raiseError("Failed to initialize Midgard using configuration file \"{$init_file}\".", mgd_errno(), PEAR_ERROR_DIE); |
|---|
| | 107 | return false; |
|---|
| | 108 | } |
|---|
| | 109 | |
|---|
| | 110 | if ($this->config->get('midgard_sitegroup') != 'root') |
|---|
| | 111 | { |
|---|
| | 112 | // We have to dig up the init file to read the MidgardUsername and MidgardPassword properties |
|---|
| | 113 | switch ($_MIDGARD['config']['prefix']) |
|---|
| | 114 | { |
|---|
| | 115 | case '/usr': |
|---|
| | 116 | case '/usr/local': |
|---|
| | 117 | $midgard_path = '/etc/midgard'; |
|---|
| | 118 | break; |
|---|
| | 119 | |
|---|
| | 120 | default: |
|---|
| | 121 | $midgard_path = "{$_MIDGARD['config']['prefix']}/etc/midgard"; |
|---|
| | 122 | break; |
|---|
| | 123 | } |
|---|
| | 124 | |
|---|
| | 125 | $init_file_path = "{$midgard_path}/conf.d/{$init_file}"; |
|---|
| | 126 | if (!file_exists($init_file_path)) |
|---|
| | 127 | { |
|---|
| | 128 | PEAR::raiseError("Midgard configuration file \"{$init_file_path}\" not found.", mgd_errno(), PEAR_ERROR_DIE); |
|---|
| | 129 | return false; |
|---|
| | 130 | } |
|---|
| | 131 | $midgard_username = null; |
|---|
| | 132 | $midgard_password = null; |
|---|
| | 133 | |
|---|
| | 134 | $lines = explode("\n", file_get_contents($init_file_path)); |
|---|
| | 135 | foreach ($lines as $line) |
|---|
| | 136 | { |
|---|
| | 137 | if (substr($line, 0, 1) == '#') |
|---|
| | 138 | { |
|---|
| | 139 | // This is a comment line, skip |
|---|
| | 140 | continue; |
|---|
| | 141 | } |
|---|
| | 142 | |
|---|
| | 143 | $parts = explode('=', $line); |
|---|
| | 144 | if (count($parts) != 2) |
|---|
| | 145 | { |
|---|
| | 146 | // This is not valid config field, skip |
|---|
| | 147 | continue; |
|---|
| | 148 | } |
|---|
| | 149 | |
|---|
| | 150 | switch ($parts[0]) |
|---|
| | 151 | { |
|---|
| | 152 | case 'MidgardUsername': |
|---|
| | 153 | $midgard_username = $parts[1]; |
|---|
| | 154 | break; |
|---|
| | 155 | |
|---|
| | 156 | case 'MidgardPassword': |
|---|
| | 157 | $midgard_password = $parts[1]; |
|---|
| | 158 | break; |
|---|
| | 159 | } |
|---|
| | 160 | } |
|---|
| | 161 | |
|---|
| | 162 | if ( is_null($midgard_username) |
|---|
| | 163 | || is_null($midgard_password)) |
|---|
| | 164 | { |
|---|
| | 165 | PEAR::raiseError("Midgard configuration file \"{$init_file}\" did not contain MidgardUsername and MidgardPassword directives.", mgd_errno(), PEAR_ERROR_DIE); |
|---|
| | 166 | return false; |
|---|
| | 167 | } |
|---|
| | 168 | |
|---|
| | 169 | if (!mgd_auth_midgard($midgard_username, $midgard_password)) |
|---|
| | 170 | { |
|---|
| | 171 | PEAR::raiseError("Failed to authenticate as Midgard user \"{$midgard_username}\" using credentials from configuration file \"{$init_file}\".", mgd_errno(), PEAR_ERROR_DIE); |
|---|
| | 172 | return false; |
|---|
| | 173 | } |
|---|
| | 174 | |
|---|
| | 175 | return true; |
|---|
| | 176 | } |
|---|