| 63 | | ); |
|---|
| 64 | | |
|---|
| 65 | | // TODO: this becomes part of the class |
|---|
| 66 | | function parse_amportal_conf($filename) { |
|---|
| 67 | | global $amp_conf_defaults, $db; |
|---|
| 68 | | |
|---|
| 69 | | /* defaults |
|---|
| 70 | | * This defines defaults and formating to assure consistency across the system so that |
|---|
| 71 | | * components don't have to keep being 'gun shy' about these variables. |
|---|
| 72 | | * |
|---|
| 73 | | * we will read these settings out of the db, but only when $filename is writeable |
|---|
| 74 | | * otherwise, we read the $filename |
|---|
| 75 | | */ |
|---|
| 76 | | if (!is_writable($filename)) { |
|---|
| 77 | | $file = file($filename); |
|---|
| 78 | | if (is_array($file)) { |
|---|
| 79 | | foreach ($file as $line) { |
|---|
| 80 | | if (preg_match("/^\s*([a-zA-Z0-9_]+)=([a-zA-Z0-9 .&-@=_!<>\"\']+)\s*$/",$line,$matches)) { |
|---|
| 81 | | $conf[ $matches[1] ] = $matches[2]; |
|---|
| 82 | | } |
|---|
| 83 | | } |
|---|
| 84 | | $conf['amportal_canwrite'] = false; |
|---|
| 85 | | } else { |
|---|
| 86 | | die_freepbx("<h1>".sprintf(_("Missing or unreadable config file (%s)...cannot continue"), $filename)."</h1>"); |
|---|
| 87 | | } |
|---|
| 88 | | } else { |
|---|
| 89 | | $sql = 'SELECT `keyword`, value FROM freepbx_settings'; |
|---|
| 90 | | $conf = $db->getAssoc($sql); |
|---|
| 91 | | if(DB::IsError($conf)) { |
|---|
| 92 | | die_freepbx($conf->getMessage()); |
|---|
| 93 | | } else { |
|---|
| 94 | | $conf['amportal_canwrite'] = true; |
|---|
| 95 | | } |
|---|
| 96 | | |
|---|
| 97 | | } |
|---|
| 98 | | |
|---|
| 99 | | |
|---|
| 100 | | // set defaults |
|---|
| 101 | | foreach ($amp_conf_defaults as $key=>$arr) { |
|---|
| 102 | | |
|---|
| 103 | | switch ($arr[0]) { |
|---|
| 104 | | // for type dir, make sure there is no trailing '/' to keep consistent everwhere |
|---|
| 105 | | // |
|---|
| 106 | | case 'dir': |
|---|
| 107 | | if (!isset($conf[$key]) || trim($conf[$key]) == '') { |
|---|
| 108 | | $conf[$key] = $arr[1]; |
|---|
| 109 | | } else { |
|---|
| 110 | | $conf[$key] = rtrim($conf[$key],'/'); |
|---|
| 111 | | } |
|---|
| 112 | | break; |
|---|
| 113 | | // booleans: |
|---|
| 114 | | // "yes", "true", "on", true, 1 (case-insensitive) will be treated as true, everything else is false |
|---|
| 115 | | // |
|---|
| 116 | | case 'bool': |
|---|
| 117 | | if (!isset($conf[$key])) { |
|---|
| 118 | | $conf[$key] = $arr[1]; |
|---|
| 119 | | } else { |
|---|
| 120 | | $conf[$key] = ($conf[$key] === true || strtolower($conf[$key]) == 'true' || $conf[$key] === 1 || $conf[$key] == '1' |
|---|
| 121 | | || strtolower($conf[$key]) == 'yes' || strtolower($conf[$key]) == 'on'); |
|---|
| 122 | | } |
|---|
| 123 | | break; |
|---|
| 124 | | default: |
|---|
| 125 | | if (!isset($conf[$key])) { |
|---|
| 126 | | $conf[$key] = $arr[1]; |
|---|
| 127 | | } else { |
|---|
| 128 | | $conf[$key] = trim($conf[$key]); |
|---|
| 129 | | } |
|---|
| 130 | | } |
|---|
| 131 | | } |
|---|
| 132 | | return $conf; |
|---|
| | 64 | ); |
|---|
| | 65 | |
|---|
| | 66 | var $db_store; |
|---|
| | 67 | |
|---|
| | 68 | var $conf = array(); |
|---|
| | 69 | |
|---|
| | 70 | var $asterisk_conf = array(); |
|---|
| | 71 | |
|---|
| | 72 | var $parsed_from_db = false; |
|---|
| | 73 | var $amportal_canwrite; |
|---|
| | 74 | |
|---|
| | 75 | function &create() { |
|---|
| | 76 | static $obj; |
|---|
| | 77 | global $db; |
|---|
| | 78 | if (!isset($obj)) { |
|---|
| | 79 | $obj = new freepbx_conf(); |
|---|
| | 80 | } |
|---|
| | 81 | return $obj; |
|---|
| | 82 | } |
|---|
| | 83 | |
|---|
| | 84 | function freepbx_conf($var) { |
|---|
| | 85 | $this->__construct(); |
|---|
| | 86 | } |
|---|
| | 87 | function __construct() { |
|---|
| | 88 | $db_raw = sql('SELECT * from freepbx_settings', 'getAll', DB_FETCHMODE_ASSOC); |
|---|
| | 89 | foreach($db_raw as $setting) { |
|---|
| | 90 | $this->db_store[$setting['keyword']] = $setting; |
|---|
| | 91 | $this->db_store[$setting['keyword']]['modified'] = false; |
|---|
| | 92 | // setup the conf array also |
|---|
| | 93 | $this->conf[$setting['keyword']] = $setting['value']; |
|---|
| | 94 | } |
|---|
| | 95 | unset($db_raw); |
|---|
| | 96 | } |
|---|
| | 97 | |
|---|
| | 98 | function parse_amportal_conf($filename, $bootstrap_conf = array()) { |
|---|
| | 99 | global $db; |
|---|
| | 100 | |
|---|
| | 101 | // if we have loaded for the db, then just return what we already have |
|---|
| | 102 | if ($this->parsed_from_db) { |
|---|
| | 103 | return $this->conf; |
|---|
| | 104 | } |
|---|
| | 105 | |
|---|
| | 106 | /* defaults |
|---|
| | 107 | * This defines defaults and formating to assure consistency across the system so that |
|---|
| | 108 | * components don't have to keep being 'gun shy' about these variables. |
|---|
| | 109 | * |
|---|
| | 110 | * we will read these settings out of the db, but only when $filename is writeable |
|---|
| | 111 | * otherwise, we read the $filename |
|---|
| | 112 | */ |
|---|
| | 113 | // If conf file is not writable, then we use it as the master so parse it. |
|---|
| | 114 | if (!is_writable($filename)) { |
|---|
| | 115 | $file = file($filename); |
|---|
| | 116 | if (is_array($file)) { |
|---|
| | 117 | $write_back = false; |
|---|
| | 118 | foreach ($file as $line) { |
|---|
| | 119 | if (preg_match("/^\s*([a-zA-Z0-9_]+)=([a-zA-Z0-9 .&-@=_!<>\"\']+)\s*$/",$line,$matches)) { |
|---|
| | 120 | // overrite anything that was initialized from the db with the conf file authoritative source |
|---|
| | 121 | // if different from the db value then let's write it back to the db |
|---|
| | 122 | if (!isset($this->conf[$matches[1]]) || $this->conf[$matches[1]] != $matches[2]) { |
|---|
| | 123 | $this->conf[ $matches[1] ] = $matches[2]; |
|---|
| | 124 | if (isset($this->db_store[$matches[1]])) { |
|---|
| | 125 | $this->db_store[$matches[1]]['value'] = $matches[2]; |
|---|
| | 126 | $this->db_store[$matches[1]]['modified'] = true; |
|---|
| | 127 | $write_back = true; |
|---|
| | 128 | } |
|---|
| | 129 | } |
|---|
| | 130 | } |
|---|
| | 131 | } |
|---|
| | 132 | $this->conf['amportal_canwrite'] = false; |
|---|
| | 133 | $this->amportal_canwrite = false; |
|---|
| | 134 | if ($write_back) { |
|---|
| | 135 | $this->commit_settings(); |
|---|
| | 136 | } |
|---|
| | 137 | } else { |
|---|
| | 138 | die_freepbx("<h1>".sprintf(_("Missing or unreadable config file (%s)...cannot continue"), $filename)."</h1>"); |
|---|
| | 139 | } |
|---|
| | 140 | // Need to handle transitionary period where modules are adding new settings. So once we parsed the file |
|---|
| | 141 | // we still go read from the database and add anything that isn't there from the conf file. |
|---|
| | 142 | // |
|---|
| | 143 | } else { |
|---|
| | 144 | $this->conf['amportal_canwrite'] = true; |
|---|
| | 145 | $this->amportal_canwrite = true; |
|---|
| | 146 | $this->parsed_from_db = true; |
|---|
| | 147 | } |
|---|
| | 148 | // TODO: am I correct to NOT write these back to the db_store? |
|---|
| | 149 | foreach ($bootstrap_conf as $key => $value) { |
|---|
| | 150 | $this->conf['key'] = $value; |
|---|
| | 151 | } |
|---|
| | 152 | |
|---|
| | 153 | // TODO: have a closer look at these in light of default values in the db. |
|---|
| | 154 | // this does set some minimum settings that should be there but the db |
|---|
| | 155 | // could have different defaults right? |
|---|
| | 156 | // |
|---|
| | 157 | // set defaults |
|---|
| | 158 | foreach ($this->conf_defaults as $key=>$arr) { |
|---|
| | 159 | |
|---|
| | 160 | switch ($arr[0]) { |
|---|
| | 161 | // for type dir, make sure there is no trailing '/' to keep consistent everwhere |
|---|
| | 162 | // |
|---|
| | 163 | case 'dir': |
|---|
| | 164 | if (!isset($this->conf[$key]) || trim($this->conf[$key]) == '') { |
|---|
| | 165 | $this->conf[$key] = $arr[1]; |
|---|
| | 166 | } else { |
|---|
| | 167 | $this->conf[$key] = rtrim($this->conf[$key],'/'); |
|---|
| | 168 | } |
|---|
| | 169 | break; |
|---|
| | 170 | // booleans: |
|---|
| | 171 | // "yes", "true", "on", true, 1 (case-insensitive) will be treated as true, everything else is false |
|---|
| | 172 | // |
|---|
| | 173 | case 'bool': |
|---|
| | 174 | if (!isset($this->conf[$key])) { |
|---|
| | 175 | $this->conf[$key] = $arr[1]; |
|---|
| | 176 | } else { |
|---|
| | 177 | $this->conf[$key] = ($this->conf[$key] === true || strtolower($this->conf[$key]) == 'true' || $this->conf[$key] === 1 || $this->conf[$key] == '1' |
|---|
| | 178 | || strtolower($this->conf[$key]) == 'yes' || strtolower($this->conf[$key]) == 'on'); |
|---|
| | 179 | } |
|---|
| | 180 | break; |
|---|
| | 181 | default: |
|---|
| | 182 | if (!isset($this->conf[$key])) { |
|---|
| | 183 | $this->conf[$key] = $arr[1]; |
|---|
| | 184 | } else { |
|---|
| | 185 | $this->conf[$key] = trim($this->conf[$key]); |
|---|
| | 186 | } |
|---|
| | 187 | } |
|---|
| | 188 | } |
|---|
| | 189 | |
|---|
| | 190 | $convert = array( |
|---|
| | 191 | 'astetcdir' => 'ASTETCDIR', |
|---|
| | 192 | 'astmoddir' => 'ASTMODDIR', |
|---|
| | 193 | 'astvarlibdir' => 'ASTVARLIBDIR', |
|---|
| | 194 | 'astagidir' => 'ASTAGIDIR', |
|---|
| | 195 | 'astspooldir' => 'ASTSPOOLDIR', |
|---|
| | 196 | 'astrundir' => 'ASTRUNDIR', |
|---|
| | 197 | 'astlogdir' => 'ASTLOGDIR' |
|---|
| | 198 | ); |
|---|
| | 199 | |
|---|
| | 200 | $file = file($this->conf['ASTETCDIR'].'/asterisk.conf'); |
|---|
| | 201 | foreach ($file as $line) { |
|---|
| | 202 | if (preg_match("/^\s*([a-zA-Z0-9]+)\s* => \s*(.*)\s*([;#].*)?/",$line,$matches)) { |
|---|
| | 203 | $this->asterisk_conf[ $matches[1] ] = rtrim($matches[2],"/ \t"); |
|---|
| | 204 | } |
|---|
| | 205 | } |
|---|
| | 206 | |
|---|
| | 207 | // Now that we parsed asterisk.conf, we need to make sure $amp_conf is consistent |
|---|
| | 208 | // so just set it to what we found, since this is what asterisk will use anyhow. |
|---|
| | 209 | // |
|---|
| | 210 | foreach ($convert as $ast_conf_key => $amp_conf_key) { |
|---|
| | 211 | if (isset($this->conf[$ast_conf_key])) { |
|---|
| | 212 | $this->conf[$amp_conf_key] = $this->asterisk_conf[$ast_conf_key]; |
|---|
| | 213 | } |
|---|
| | 214 | } |
|---|
| | 215 | |
|---|
| | 216 | return $this->conf; |
|---|
| | 217 | } |
|---|
| | 218 | |
|---|
| | 219 | function get_asterisk_conf() { |
|---|
| | 220 | return $this->asterisk_conf; |
|---|
| | 221 | } |
|---|
| | 222 | |
|---|
| | 223 | // Commit back any dirty settings to the database, if they are not modified then |
|---|
| | 224 | // don't bother. |
|---|
| | 225 | function commit_settings() { |
|---|
| | 226 | // TODO: foreach db_store that modified === true, package up to do an update |
|---|
| | 227 | // then mark clean |
|---|
| | 228 | } |
|---|
| | 229 | |
|---|
| | 230 | function get_setting($keyword) { |
|---|
| | 231 | if (isset($this->db_store[$keyword])) { |
|---|
| | 232 | return $this->db_store[$keyword]; |
|---|
| | 233 | } else { |
|---|
| | 234 | return false; |
|---|
| | 235 | } |
|---|
| | 236 | } |
|---|
| | 237 | |
|---|
| | 238 | // TODO: used to insert or update an existing setting such as in an install |
|---|
| | 239 | // script. $vars will include some required fields and we will be strict |
|---|
| | 240 | // with a die_freebpx() if they are missing. Some fields are optional |
|---|
| | 241 | // and we will set default values also if appropriate. |
|---|
| | 242 | // |
|---|
| | 243 | function update_setting($keyword,$vars,$commit=false) { |
|---|
| | 244 | } |
|---|
| | 245 | |
|---|
| | 246 | // Used to remove settings from the database that are no longer needed like with an |
|---|
| | 247 | // uninstall script. |
|---|
| | 248 | // TODO: remove from db_store, from conf, delete from table directly |
|---|
| | 249 | function del_setting($keyword) { |
|---|
| | 250 | } |
|---|
| | 251 | |
|---|