Esempio n. 1
0
    $existing_trunks = array();
    $existing_trunk_hash = array();
    foreach ($results2 as $tr) {
        if (preg_match('/^9999([\\d]{1,2})$|^99999([\\d]{1,2})$|^9999999([\\d]{1,2})$|^tr-peer-([\\d]+)$|^tr-user-([\\d]+)$|^tr-reg-([\\d]+)$/', $tr, $match)) {
            $existing_trunks[$match[count($match) - 1]][] = $tr;
            $existing_trunk_hash[$tr] = $match[count($match) - 1];
        }
    }
    $sql = "SELECT `data` name, `id` FROM `sip` WHERE `keyword` = 'account' AND `id` IN ('" . implode("','", array_keys($existing_trunk_hash)) . "')";
    $results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
    if (DB::IsError($results)) {
        $results = array();
    }
    foreach ($results as $trunk_name) {
        if ($trunk_name['name'] != "fpbx-1-{$sip_user}" && $trunk_name['name'] != "fpbx-2-{$sip_user}") {
            $tname = core_trunks_getTrunkTrunkName($existing_trunk_hash[$trunk_name['id']]);
            if ($tname != $trunk_name['name']) {
                $tname .= " ({$trunk_name['name']})";
            }
            $trunk_conflict[] = array('name' => $tname, 'href' => 'OUT_' . $existing_trunk_hash[$trunk_name['id']]);
        }
    }
}
/* If we detected trunk conflicts then we don't bother with the Trunk and Routing
   section, just report and be done.
 */
if (!empty($trunk_conflict)) {
    unset($json_array['query_status_message']);
    unset($json_array['query_status']);
    $json_array['trunk_conflict'] = $trunk_conflict;
} elseif ($json_array['query_status'] == 'SUCCESS' && $json_array['xml_version'] == $sipstation_xml_version) {
Esempio n. 2
0
function sipstation_get_or_create_trunks(&$json_array, &$globalvar1, &$trunknum1, &$globalvar2, &$trunknum2)
{
    // now check on trunk config
    // fpbx-1-$sip_user / fpbx-2-$sip_user
    // TODO: 2.6 has trunkname, provider options that should be used
    //
    global $ast_codec_hash;
    $sip_user = $json_array['sip_username'];
    $sip_pass = $json_array['sip_password'];
    $default_did = $json_array['e911_address']['default_did'];
    $need_reload = false;
    $tlist = core_trunks_list(true);
    $tech = 'sip';
    $keepcid = 'off';
    $disabletrunk = 'off';
    $peerdetails = "disallow=all\nallow=" . implode('&', array_keys($ast_codec_hash)) . "\n";
    $peer_array = array();
    foreach ($json_array['asterisk_settings']['peer'] as $param) {
        $peerdetails .= trim($param) . "\n";
        $parts = explode('=', $param, 2);
        $peer_array[$parts[0]] = $parts[1];
    }
    $peerdetails .= "username={$sip_user}\nsecret={$sip_pass}\nhost=";
    $register = "{$sip_user}:{$sip_pass}@";
    for ($i = 1; $i < 3; $i++) {
        $gidx = "gw{$i}";
        $channelid = "fpbx-{$i}-{$sip_user}";
        $gw = $json_array['gateways'][$gidx];
        if (isset($tlist["SIP/{$channelid}"])) {
            $globalvar = $tlist["SIP/{$channelid}"]['globalvar'];
            $trunknum = ltrim($globalvar, 'OUT_');
            // Now get some trunk status info
            $trunk_status = sipstation_get_peer_status($channelid);
            if ($trunk_status['sipstation_status'] == 'ok') {
                $json_array['trunk_qualify'][$gidx] = $trunk_status['Status'];
                $json_array['trunk_codecs'][$gidx] = implode(' | ', sipstation_get_configured_codecs($channelid, $trunk_status));
            } else {
                //TODO: probably nothing
            }
            $json_array['trunk_name'][$gidx] = core_trunks_getTrunkTrunkName($trunknum);
        } else {
            $trunknum = core_trunks_add($tech, $channelid, '', '', $default_did, $peerdetails . $gw, '', '', $register . $gw, $keepcid, '', $disabletrunk);
            $globalvar = "OUT_" . $trunknum;
            $need_reload = true;
            $json_array['created_trunks'][$gidx] = $channelid;
            // TODO: 2.6 should be name
            $json_array['trunk_name'][$gidx] = "SIP/{$channelid}";
        }
        // We need these next and need them past back up
        $gv = "globalvar{$i}";
        $tn = "trunknum{$i}";
        ${$gv} = $globalvar;
        ${$tn} = $trunknum;
    }
    $peer_array['username'] = $sip_user;
    $peer_array['secret'] = $sip_pass;
    $trunk_check = array($trunknum1, $trunknum2);
    $cnt = 1;
    foreach ($trunk_check as $tr) {
        $json_array['trunk_id']["gw{$cnt}"] = $tr;
        // need to get this set for both anyhow
        $gw = $json_array['gateways']["gw{$cnt}"];
        $peer_array['host'] = $gw;
        $peer_stuff = array();
        $tr_reg = core_trunks_getTrunkRegister($tr);
        foreach (explode("\n", core_trunks_getTrunkPeerDetails($tr)) as $elem) {
            $temp = explode("=", $elem, 2);
            if ($temp[0] == 'allow') {
                $peer_stuff[$temp[0]] = explode('&', $temp[1]);
            } elseif ($temp[0] != '') {
                $peer_stuff[$temp[0]] = $temp[1];
            }
        }
        // Unset some settings that do not hurt to change and might help
        //
        if (isset($peer_stuff['allow'])) {
            unset($peer_stuff['allow']);
        }
        if (isset($peer_stuff['disallow'])) {
            unset($peer_stuff['disallow']);
        }
        unset($peer_array['qualify']);
        if (isset($peer_stuff['qualify'])) {
            unset($peer_stuff['qualify']);
        }
        unset($peer_array['qualify']);
        if (isset($peer_stuff['context'])) {
            unset($peer_stuff['context']);
        }
        unset($peer_array['context']);
        if (isset($peer_stuff['qualifyfreq'])) {
            unset($peer_stuff['qualifyfreq']);
        }
        if (isset($peer_stuff['dtmfmode'])) {
            switch ($peer_stuff['dtmfmode']) {
                case 'inband':
                case 'rfc2833':
                case 'auto':
                    unset($peer_stuff['dtmfmode']);
                    unset($peer_array['dtmfmode']);
                    break;
            }
        }
        if ($peer_array != $peer_stuff || $tr_reg != $register . $gw) {
            $json_array['changed_trunks']["gw{$cnt}"] = $tr;
        }
        $cnt++;
    }
    return $need_reload;
}