<?php

session_start();
$currentBaseDir = dirname($_SERVER['PHP_SELF']);
include($_SESSION['basedir'] . "core.php");

$svaTool = new svaTool();

/* Get all interface -> getinterface Details by Id -> Build physical config -> reload config */
$stdSipAccntData = "";
$registrySipAccntData = "";
$errorCode = 0;

$res = $dba->SCCGetServiceChannelById(0);
$total = count($res);

for ($i = 0; $i < $total; $i++) {

    switch ($res[$i]['channel_type_n']) {


        case 1 : /*             * **Voice SIP *** */
            $pres = $dba->SCCGetSipVoiceChannelById($res[$i]['channel_id_n']);

            switch ($pres[0]['sip_type_n']) {

                case 1 :  // Registry SIP Account
                    $registrySipAccntData .= $svaTool->getSIPRegistrationConfig($pres[0]['name'], $pres[0]['username'], $pres[0]['host'], $pres[0]['port'], $pres[0]['secret'], $pres[0]['callerid']);
                    break;
                case 2 : // Trunk SIP Account
                    $stdSipAccntData .= $svaTool->getSIPAgentConfigTrunk($pres[0]['username'], "friend", $pres[0]['host'], $pres[0]['secret'], $pres[0]['call_limit']);
                    break;
                case 3 : // Std user account
                    $stdSipAccntData .= $svaTool->getSIPAgentConfig($pres[0]['username'], "friend", $pres[0]['host'], $pres[0]['secret'], $pres[0]['call_limit']);
                    break;
            }

            break;
        case 2 : /*             * **** Voice TDM *** */
            break;
    }

    /*     * ************** Writing related file ********* */

    $fp = fopen($_SESSION['cfg']['scc']['sip.additional.file'], 'w');
    if ($fp) {

        fputs($fp, $stdSipAccntData);
        fclose($fp);
    }
    else {
        
        $errorCode = 1;
        
    }



    $fp = fopen($_SESSION['cfg']['scc']['sip.register.additional.file'], 'w');
    if ($fp) {

        fputs($fp, $registrySipAccntData);
        fclose($fp);
    }
    else {
        
        $errorCode = 1;
    }



    $manager->reloadSIPConfig();
}






switch ($errorCode) {

    case 0 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"CONFIGURATION SYNCHRONISEE AVEC SUCCES !\"  }";
        break;
    case 1 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! ECHEC DE LA SYNCHRONISATION !\"  }";
        break;
  
}

echo $msg;
?>