Ejemplo n.º 1
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;
}
Ejemplo n.º 2
0
*/
/*
include_once("common/json.inc.php");
*/
/* We can use the cached config data as we were just told that
   trunk params have changed and chose to update
 */
$sip_username = $_POST['sip_username'];
$gateways['gw1'] = $_POST['gw1'];
$gateways['gw2'] = $_POST['gw2'];
$json_array['status'] = 'success';
// Get the Asterisk Registration Status
$trunk_status = sipstation_get_registration_status($sip_username);
foreach ($gateways as $gw => $trunk) {
    if (isset($trunk_status[$trunk])) {
        $json_array['asterisk_registry'][$gw] = $trunk_status[$trunk];
    } else {
        $json_array['asterisk_registry'][$gw] = _("Not Registered");
    }
}
for ($i = 1; $i < 3; $i++) {
    $channelid = "fpbx-{$i}-{$sip_username}";
    $trunk_status = sipstation_get_peer_status($channelid);
    if ($trunk_status['sipstation_status'] == 'ok') {
        $json_array['trunk_qualify']["gw{$i}"] = $trunk_status['Status'];
    }
}
$json = new Services_JSON();
$value = $json->decode($_POST);
header("Content-type: application/json");
echo $json->encode($json_array);