<?php

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

$svaTool = new svaTool();

//print_r($_REQUEST);


$errorCode = 0;
$opId = $userId = $username = $pwd = $userType = $profileId = $onwerId = $lang = $citizen = $firstName = $lastName = $primaryPhone = $secondaryPhone = $homePhone = $mobilePhone = $email = $primAddress = $secondAddress = $profession = $char1 = $char2 = $char3 = $char4 = $char5 = $char6 = $char7 = $char8 = $char9 = $char10 = $status = "";

$password = NULL;
$confPasswd = NULL;
// Ajout de nouvel utilisateur


if (isset($_REQUEST['channelName']) && strlen($_REQUEST['channelName']) > 0) {
    $channelName = $_REQUEST['channelName'];
} else {

    $errorCode = 1;
}

if (isset($_REQUEST['accountType']) && $_REQUEST['accountType'] != -1) {
    $accountType = $_REQUEST['accountType'];
} else {

    $errorCode = 2;
}

$description = $_REQUEST['description'];
$channelId = $_REQUEST['channelId'];
$opId = $_REQUEST['opId'];
$itemId = $_REQUEST['itemId'];
if (isset($_REQUEST['username']) && strlen($_REQUEST['username']) > 0) {
    $username = $_REQUEST['username'];
} else {

    $errorCode = 3;
}

if (isset($_REQUEST['authUsername']) && strlen($_REQUEST['authUsername']) > 0) {
    $authUsername = $_REQUEST['authUsername'];
} else {

    $errorCode = 4;
}



if (isset($_REQUEST['callerId']) && strlen($_REQUEST['callerId']) > 0 && is_numeric($_REQUEST['callerId'])) {
    $callerId = $_REQUEST['callerId'];
} else {

    $errorCode = 5;
}


if (isset($_REQUEST['domainName']) && strlen($_REQUEST['domainName']) > 0) {
    $domainName = $_REQUEST['domainName'];
} else {

    $errorCode = 6;
}


if (isset($_REQUEST['password']) && strlen($_REQUEST['password']) > 0) {
    $password = $_REQUEST['password'];
} else {

    $errorCode = 7;
}


if (isset($_REQUEST['confPasswd']) && strlen($_REQUEST['confPasswd']) > 0) {
    $confPasswd = $_REQUEST['confPasswd'];
} else {

    $errorCode = 8;
}


if (strcmp($password, $confPasswd) != 0) {
		
			$errorCode = 9;
		}

if (isset($_REQUEST['voiceCodec']) && strlen($_REQUEST['voiceCodec']) > 0) {
    $voiceCodec = $_REQUEST['voiceCodec'];
} else {

    $errorCode = 10;
}


if (isset($_REQUEST['callLimit']) && strlen($_REQUEST['callLimit']) > 0 && is_numeric($_REQUEST['callLimit'])) {
    $callLimit = $_REQUEST['callLimit'];
} else {

    $errorCode = 11;
}


if (isset($_REQUEST['domainPort']) && strlen($_REQUEST['domainPort']) > 0 && is_numeric($_REQUEST['domainPort'])) {
    $port = $_REQUEST['domainPort'];
} else {

    $errorCode = 12;
}







$userId = $_SESSION['ownerId'];



if ($opId == 0) {
    $label = "CREE";
} else {
    $label = "MODIFIE";
}


if ($errorCode == 0) {
    
    
    $res = $dba->UpdateAddSCCChannel($opId, $itemId, $channelId, $userId, $channelName, $description, 0);
    
    if($opId == 0){
        $itemId = $res ;
    }
     $res = $dba->UpdateAddSIPChannel($opId, $itemId, $username, $domainName, $port, "friend", $authUsername, $password, $accountType, $itemId, $voiceCodec,$callLimit, $callerId,  0);


}


switch ($errorCode) {

    case 0 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"CANAL DE SERVICE $label AVEC SUCCES !\"  }";
        break;
    case 1 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! L'IDENTIFIANT DE SERVICE EST INVALIDE !\"  }";
        break;
    case 2 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! VEUILLEZ SELECTIONNER LE TYPE DE COMPTE !\"  }";
        break;
    case 3 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LE NOM D'UTILISATEUR EST INVALIDE !\"  }";
        break;
    case 4 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LE NOM D'UTILISATEUR AUTORISE EST INVALIDE !\"  }";
        break;
    case 5 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LE NUMERO D'APPEL EST INVALIDE !\"  }";
        break;
    case 6 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! L'ADRESSE IP / NOM DE DOMAINE EST INVALIDE !\"  }";
        break;
    case 7 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LE MOT DE PASSE EST INVALIDE !\"  }";
        break;
    case 8 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LA CONFRMATION DU MOT DE PASSE EST INVALIDE !\"  }";
        break;
    case 9 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LES MOTS DE PASSE NE CONCORDENT PAS !\"  }";
        break;
    case 10 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! VEUILLEZ SELECTIONNER UN CODEC !\"  }";
        break;
    
     case 11 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! VEUILLEZ DEFINIR LE NOM D'APPELS SIMULTANES !\"  }";
        break;
     case 12 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LE NUMERO DE PORT EST INVALIDE !\"  }";
        break;
}


echo $msg;
?>