| 59 | | ?> |
|---|
| | 59 | // This next set of functions and code are used to migrate from the old |
|---|
| | 60 | // global variable storage of trunk data to the new trunk table and trunk |
|---|
| | 61 | // pattern table for localprefixes.conf |
|---|
| | 62 | // this is taken straight out of the core install.php script, as new installs |
|---|
| | 63 | // with install_amp break and haven't taken the time to figure out why. |
|---|
| | 64 | // |
|---|
| | 65 | |
|---|
| | 66 | //Sort trunks for sqlite |
|---|
| | 67 | function __sort_trunks($a,$b) { |
|---|
| | 68 | global $unique_trunks; |
|---|
| | 69 | ereg("OUT_([0-9]+)",$unique_trunks[$a][0],$trunk_num1); |
|---|
| | 70 | ereg("OUT_([0-9]+)",$unique_trunks[$b][0],$trunk_num2); |
|---|
| | 71 | return ($trunk_num1[1] >= $trunk_num2[1]? 1:-1); |
|---|
| | 72 | } |
|---|
| | 73 | |
|---|
| | 74 | // Get values from localprefix configuration file where values are stored |
|---|
| | 75 | // for fixlocalprefix macro |
|---|
| | 76 | // |
|---|
| | 77 | function __parse_DialRulesFile($filename, &$conf, &$section) { |
|---|
| | 78 | if (is_null($conf)) { |
|---|
| | 79 | $conf = array(); |
|---|
| | 80 | } |
|---|
| | 81 | if (is_null($section)) { |
|---|
| | 82 | $section = "general"; |
|---|
| | 83 | } |
|---|
| | 84 | |
|---|
| | 85 | if (file_exists($filename)) { |
|---|
| | 86 | $fd = fopen($filename, "r"); |
|---|
| | 87 | while ($line = fgets($fd, 1024)) { |
|---|
| | 88 | if (preg_match("/^\s*([a-zA-Z0-9-_]+)\s*=\s*(.*?)\s*([;#].*)?$/",$line,$matches)) { |
|---|
| | 89 | // name = value |
|---|
| | 90 | // option line |
|---|
| | 91 | $conf[$section][ $matches[1] ] = $matches[2]; |
|---|
| | 92 | } else if (preg_match("/^\s*\[(.+)\]/",$line,$matches)) { |
|---|
| | 93 | // section name |
|---|
| | 94 | $section = strtolower($matches[1]); |
|---|
| | 95 | } else if (preg_match("/^\s*#include\s+(.*)\s*([;#].*)?/",$line,$matches)) { |
|---|
| | 96 | // include another file |
|---|
| | 97 | |
|---|
| | 98 | if ($matches[1][0] == "/") { |
|---|
| | 99 | // absolute path |
|---|
| | 100 | $filename = $matches[1]; |
|---|
| | 101 | } else { |
|---|
| | 102 | // relative path |
|---|
| | 103 | $filename = dirname($filename)."/".$matches[1]; |
|---|
| | 104 | } |
|---|
| | 105 | __parse_DialRulesFile($filename, $conf, $section); |
|---|
| | 106 | } |
|---|
| | 107 | } |
|---|
| | 108 | } |
|---|
| | 109 | } |
|---|
| | 110 | |
|---|
| | 111 | function __migrate_trunks_to_table() { |
|---|
| | 112 | |
|---|
| | 113 | global $db; |
|---|
| | 114 | global $amp_conf; |
|---|
| | 115 | |
|---|
| | 116 | $sql = " |
|---|
| | 117 | CREATE TABLE `trunks` |
|---|
| | 118 | ( |
|---|
| | 119 | `trunkid` INTEGER, |
|---|
| | 120 | `name` VARCHAR( 50 ) NOT NULL DEFAULT '', |
|---|
| | 121 | `tech` VARCHAR( 20 ) NOT NULL , |
|---|
| | 122 | `outcid` VARCHAR( 40 ) NOT NULL DEFAULT '', |
|---|
| | 123 | `keepcid` VARCHAR( 4 ) DEFAULT 'off', |
|---|
| | 124 | `maxchans` VARCHAR( 6 ) DEFAULT '', |
|---|
| | 125 | `failscript` VARCHAR( 255 ) NOT NULL DEFAULT '', |
|---|
| | 126 | `dialoutprefix` VARCHAR( 255 ) NOT NULL DEFAULT '', |
|---|
| | 127 | `channelid` VARCHAR( 255 ) NOT NULL DEFAULT '', |
|---|
| | 128 | `usercontext` VARCHAR( 255 ) NULL, |
|---|
| | 129 | `provider` VARCHAR( 40 ) NULL, |
|---|
| | 130 | `disabled` VARCHAR( 4 ) DEFAULT 'off', |
|---|
| | 131 | |
|---|
| | 132 | PRIMARY KEY (`trunkid`, `tech`, `channelid`) |
|---|
| | 133 | ) |
|---|
| | 134 | "; |
|---|
| | 135 | $check = $db->query($sql); |
|---|
| | 136 | if(DB::IsError($check)) { |
|---|
| | 137 | if($check->getCode() == DB_ERROR_ALREADY_EXISTS) { |
|---|
| | 138 | //echo ("already exists\n"); |
|---|
| | 139 | return false; |
|---|
| | 140 | } else { |
|---|
| | 141 | die_freepbx($check->getDebugInfo()); |
|---|
| | 142 | } |
|---|
| | 143 | } |
|---|
| | 144 | |
|---|
| | 145 | // sqlite doesn't support the syntax required for the SQL so we have to do it the hard way |
|---|
| | 146 | if ($amp_conf["AMPDBENGINE"] == "sqlite3") { |
|---|
| | 147 | $sqlstr = "SELECT variable, value FROM globals WHERE variable LIKE 'OUT\_%' ESCAPE '\'"; |
|---|
| | 148 | $my_unique_trunks = sql($sqlstr,"getAll",DB_FETCHMODE_ASSOC); |
|---|
| | 149 | |
|---|
| | 150 | $sqlstr = "SELECT variable, value FROM globals WHERE variable LIKE 'OUTDISABLE\_%' ESCAPE '\'"; |
|---|
| | 151 | $disable_states = sql($sqlstr,"getAll",DB_FETCHMODE_ASSOC); |
|---|
| | 152 | |
|---|
| | 153 | foreach($disable_states as $arr) { |
|---|
| | 154 | $disable_states_assoc[$arr['variable']] = $arr['value']; |
|---|
| | 155 | } |
|---|
| | 156 | global $unique_trunks; |
|---|
| | 157 | $unique_trunks = array(); |
|---|
| | 158 | |
|---|
| | 159 | foreach ($my_unique_trunks as $this_trunk) { |
|---|
| | 160 | |
|---|
| | 161 | $trunk_num = substr($this_trunk['variable'],4); |
|---|
| | 162 | $this_state = (isset($disable_states_assoc['OUTDISABLE_'.$trunk_num]) ? $disable_states_assoc['OUTDISABLE_'.$trunk_num] : 'off'); |
|---|
| | 163 | $unique_trunks[] = array($this_trunk['variable'], $this_trunk['value'], $this_state); |
|---|
| | 164 | } |
|---|
| | 165 | // sort this array using a custom function __sort_trunks(), defined above |
|---|
| | 166 | uksort($unique_trunks,"__sort_trunks"); |
|---|
| | 167 | // re-index the newly sorted array |
|---|
| | 168 | foreach($unique_trunks as $arr) { |
|---|
| | 169 | $unique_trunks_t[] = array($arr[0],$arr[1],$arr[2]); |
|---|
| | 170 | } |
|---|
| | 171 | $unique_trunks = $unique_trunks_t; |
|---|
| | 172 | |
|---|
| | 173 | } else { |
|---|
| | 174 | $sqlstr = "SELECT t.variable, t.value, d.value state FROM `globals` t "; |
|---|
| | 175 | $sqlstr .= "JOIN (SELECT x.variable, x.value FROM globals x WHERE x.variable LIKE 'OUTDISABLE\_%') d "; |
|---|
| | 176 | $sqlstr .= "ON substring(t.variable,5) = substring(d.variable,12) WHERE t.variable LIKE 'OUT\_%' "; |
|---|
| | 177 | $sqlstr .= "UNION ALL "; |
|---|
| | 178 | $sqlstr .= "SELECT v.variable, v.value, concat(substring(v.value,1,0),'off') state FROM `globals` v "; |
|---|
| | 179 | $sqlstr .= "WHERE v.variable LIKE 'OUT\_%' AND concat('OUTDISABLE_',substring(v.variable,5)) NOT IN "; |
|---|
| | 180 | $sqlstr .= " ( SELECT variable from globals WHERE variable LIKE 'OUTDISABLE\_%' ) "; |
|---|
| | 181 | $sqlstr .= "ORDER BY variable"; |
|---|
| | 182 | $unique_trunks = sql($sqlstr,"getAll"); |
|---|
| | 183 | } |
|---|
| | 184 | |
|---|
| | 185 | $trunkinfo = array(); |
|---|
| | 186 | foreach ($unique_trunks as $trunk) { |
|---|
| | 187 | list($tech,$name) = explode('/',$trunk[1]); |
|---|
| | 188 | $trunkid = ltrim($trunk[0],'OUT_'); |
|---|
| | 189 | |
|---|
| | 190 | $sqlstr = " |
|---|
| | 191 | SELECT `variable`, `value` FROM `globals` WHERE `variable` IN ( |
|---|
| | 192 | 'OUTCID_$trunkid', 'OUTFAIL_$trunkid', 'OUTKEEPCID_$trunkid', |
|---|
| | 193 | 'OUTMAXCHANS_$trunkid', 'OUTPREFIX_$trunkid') |
|---|
| | 194 | "; |
|---|
| | 195 | $trunk_attribs = sql($sqlstr,'getAll',DB_FETCHMODE_ASSOC); |
|---|
| | 196 | $trunk_attrib_hash = array(); |
|---|
| | 197 | foreach ($trunk_attribs as $attribs) { |
|---|
| | 198 | $trunk_attrib_hash[$attribs['variable']] = $attribs['value']; |
|---|
| | 199 | } |
|---|
| | 200 | |
|---|
| | 201 | switch ($tech) { |
|---|
| | 202 | case 'SIP': |
|---|
| | 203 | $tech = 'sip'; |
|---|
| | 204 | $user = sql("SELECT `data` FROM `sip` WHERE `id` = '99999$trunkid' AND `keyword` = 'account'",'getOne'); |
|---|
| | 205 | break; |
|---|
| | 206 | case 'IAX': |
|---|
| | 207 | case 'IAX2': |
|---|
| | 208 | $tech = 'iax'; |
|---|
| | 209 | $user = sql("SELECT `data` FROM `iax` WHERE `id` = '99999$trunkid' AND `keyword` = 'account'",'getOne'); |
|---|
| | 210 | break; |
|---|
| | 211 | case 'ZAP': |
|---|
| | 212 | case 'DUNDI': |
|---|
| | 213 | case 'ENUM': |
|---|
| | 214 | $tech = strtolower($tech); |
|---|
| | 215 | $user = ''; |
|---|
| | 216 | break; |
|---|
| | 217 | default: |
|---|
| | 218 | if (substr($tech,0,4) == 'AMP:') { |
|---|
| | 219 | $tech='custom'; |
|---|
| | 220 | $name = substr($trunk[1],4); |
|---|
| | 221 | } else { |
|---|
| | 222 | $tech = strtolower($tech); |
|---|
| | 223 | } |
|---|
| | 224 | $user = ''; |
|---|
| | 225 | } |
|---|
| | 226 | |
|---|
| | 227 | $trunkinfo[] = array( |
|---|
| | 228 | 'trunkid' => $trunkid, |
|---|
| | 229 | 'tech' => $tech, |
|---|
| | 230 | 'outcid' => $trunk_attrib_hash['OUTCID_'.$trunkid], |
|---|
| | 231 | 'keepcid' => $trunk_attrib_hash['OUTKEEPCID_'.$trunkid], |
|---|
| | 232 | 'maxchans' => $trunk_attrib_hash['OUTMAXCHANS_'.$trunkid], |
|---|
| | 233 | 'failscript' => $trunk_attrib_hash['OUTFAIL_'.$trunkid], |
|---|
| | 234 | 'dialoutprefix' => $trunk_attrib_hash['OUTPREFIX_'.$trunkid], |
|---|
| | 235 | 'channelid' => $name, |
|---|
| | 236 | 'usercontext' => $user, |
|---|
| | 237 | 'disabled' => $trunk[2], // disable state |
|---|
| | 238 | ); |
|---|
| | 239 | |
|---|
| | 240 | $sqlstr = "INSERT INTO `trunks` |
|---|
| | 241 | ( trunkid, tech, outcid, keepcid, maxchans, failscript, dialoutprefix, channelid, usercontext, disabled) |
|---|
| | 242 | VALUES ( |
|---|
| | 243 | '".$db->escapeSimple($trunkid)."', |
|---|
| | 244 | '".$db->escapeSimple($tech)."', |
|---|
| | 245 | '".$db->escapeSimple($trunk_attrib_hash['OUTCID_'.$trunkid])."', |
|---|
| | 246 | '".$db->escapeSimple($trunk_attrib_hash['OUTKEEPCID_'.$trunkid])."', |
|---|
| | 247 | '".$db->escapeSimple($trunk_attrib_hash['OUTMAXCHANS_'.$trunkid])."', |
|---|
| | 248 | '".$db->escapeSimple($trunk_attrib_hash['OUTFAIL_'.$trunkid])."', |
|---|
| | 249 | '".$db->escapeSimple($trunk_attrib_hash['OUTPREFIX_'.$trunkid])."', |
|---|
| | 250 | '".$db->escapeSimple($name)."', |
|---|
| | 251 | '".$db->escapeSimple($user)."', |
|---|
| | 252 | '".$db->escapeSimple($trunk[2])."' |
|---|
| | 253 | ) |
|---|
| | 254 | "; |
|---|
| | 255 | sql($sqlstr); |
|---|
| | 256 | } |
|---|
| | 257 | |
|---|
| | 258 | return $trunkinfo; |
|---|
| | 259 | } |
|---|
| | 260 | |
|---|
| | 261 | // __migrate_trunks_to_table will return false if the trunks table already exists and |
|---|
| | 262 | // no migration is needed |
|---|
| | 263 | // |
|---|
| | 264 | outn(_("Checking if trunk table migration required..")); |
|---|
| | 265 | $trunks = __migrate_trunks_to_table(); |
|---|
| | 266 | if ($trunks !== false) { |
|---|
| | 267 | outn(_("migrating..")); |
|---|
| | 268 | foreach ($trunks as $trunk) { |
|---|
| | 269 | $tech = $trunk['tech']; |
|---|
| | 270 | $trunkid = $trunk['trunkid']; |
|---|
| | 271 | switch ($tech) { |
|---|
| | 272 | case 'sip': |
|---|
| | 273 | case 'iax': |
|---|
| | 274 | $sql = "UPDATE `$tech` SET `id` = 'tr-peer-$trunkid' WHERE `id` = '9999$trunkid'"; |
|---|
| | 275 | sql($sql); |
|---|
| | 276 | $sql = "UPDATE `$tech` SET `id` = 'tr-user-$trunkid' WHERE `id` = '99999$trunkid'"; |
|---|
| | 277 | sql($sql); |
|---|
| | 278 | $sql = "UPDATE `$tech` SET `id` = 'tr-reg-$trunkid' WHERE `id` = '9999999$trunkid' AND `keyword` = 'register'"; |
|---|
| | 279 | sql($sql); |
|---|
| | 280 | break; |
|---|
| | 281 | default: |
|---|
| | 282 | break; |
|---|
| | 283 | } |
|---|
| | 284 | } |
|---|
| | 285 | outn(_("removing globals..")); |
|---|
| | 286 | // Don't do this above, in case something goes wrong |
|---|
| | 287 | // |
|---|
| | 288 | // At this point we have created our trunks table and update the sip and iax files |
|---|
| | 289 | // time to get rid of the old globals which will not be auto-generated |
|---|
| | 290 | // |
|---|
| | 291 | foreach ($trunks as $trunk) { |
|---|
| | 292 | $trunkid = $trunk['trunkid']; |
|---|
| | 293 | |
|---|
| | 294 | $sqlstr = " |
|---|
| | 295 | DELETE FROM `globals` WHERE `variable` IN ( |
|---|
| | 296 | 'OUTCID_$trunkid', 'OUTFAIL_$trunkid', 'OUTKEEPCID_$trunkid', |
|---|
| | 297 | 'OUTMAXCHANS_$trunkid', 'OUTPREFIX_$trunkid', 'OUT_$trunkid', |
|---|
| | 298 | 'OUTDISABLE_$trunkid' |
|---|
| | 299 | ) |
|---|
| | 300 | "; |
|---|
| | 301 | sql($sqlstr); |
|---|
| | 302 | } |
|---|
| | 303 | out(_("done")); |
|---|
| | 304 | } else { |
|---|
| | 305 | out(_("not needed")); |
|---|
| | 306 | } |
|---|
| | 307 | |
|---|
| | 308 | $sql = " |
|---|
| | 309 | CREATE TABLE `trunks_dialpatterns` |
|---|
| | 310 | ( |
|---|
| | 311 | `trunkid` INTEGER, |
|---|
| | 312 | `rule` VARCHAR( 255 ) NOT NULL, |
|---|
| | 313 | `seq` INTEGER, |
|---|
| | 314 | PRIMARY KEY (`trunkid`, `rule`, `seq`) |
|---|
| | 315 | ) |
|---|
| | 316 | "; |
|---|
| | 317 | outn(_("Checking if trunks_dialpatterns table exists..")); |
|---|
| | 318 | $check = $db->query($sql); |
|---|
| | 319 | if(DB::IsError($check) && $check->getCode() != DB_ERROR_ALREADY_EXISTS) { |
|---|
| | 320 | die_freepbx("Can not create trunks_dialpatterns table"); |
|---|
| | 321 | } else if(DB::IsError($check) && $check->getCode() == DB_ERROR_ALREADY_EXISTS) { |
|---|
| | 322 | out(_("already exists")); |
|---|
| | 323 | } else { |
|---|
| | 324 | out(_("created")); |
|---|
| | 325 | outn(_("loading table from localprefixes.conf..")); |
|---|
| | 326 | $localPrefixFile = $amp_conf['ASTETCDIR']."/localprefixes.conf"; |
|---|
| | 327 | $conf == array(); |
|---|
| | 328 | __parse_DialRulesFile($localPrefixFile, $conf, $section); |
|---|
| | 329 | |
|---|
| | 330 | $rules_arr = array(); |
|---|
| | 331 | foreach ($conf as $tname => $rules) { |
|---|
| | 332 | $tid = ltrim($tname,'trunk-'); |
|---|
| | 333 | ksort($rules); //make sure they are in order |
|---|
| | 334 | $seq = 1; |
|---|
| | 335 | foreach ($rules as $rule) { |
|---|
| | 336 | $rules_arr[] = array($tid,$rule,$seq); |
|---|
| | 337 | $seq++; |
|---|
| | 338 | } |
|---|
| | 339 | } |
|---|
| | 340 | $compiled = $db->prepare("INSERT INTO `trunks_dialpatterns` (trunkid, rule, seq) VALUES (?,?,?)"); |
|---|
| | 341 | $result = $db->executeMultiple($compiled,$rules_arr); |
|---|
| | 342 | if(DB::IsError($result)) { |
|---|
| | 343 | die_freepbx($result->getDebugInfo().'error populating trunks_dialpatterns table'); |
|---|
| | 344 | } |
|---|
| | 345 | out(_("loaded")); |
|---|
| | 346 | } |
|---|
| | 347 | |
|---|
| | 348 | // END of trunks migration code |
|---|