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

$svaTool = new svaTool();


$errorCode = 0;

$password = NULL;
$confPasswd = NULL;
// Ajout de nouvel utilisateur
if (isset($_REQUEST['lastName']) && strlen($_REQUEST['lastName']) > 0)
    $lastName = addslashes($_REQUEST['lastName']);
else
    $errorCode = 1;
if (isset($_REQUEST['firstName']) && strlen($_REQUEST['firstName']) > 0)
    $firstName = addslashes($_REQUEST['firstName']);
else
    $errorCode = 2;

if ((isset($_REQUEST['msisdn']) && is_numeric($_REQUEST['msisdn'])) || $_REQUEST['msisdn'] == "")
    $mobilePhone = addslashes($_REQUEST['msisdn']);
else
    $errorCode = 3;

if (isset($_REQUEST['profileId']) && $_REQUEST['profileId'] != "-1")
    $profileId = addslashes($_REQUEST['profileId']);
else
    $errorCode = 4;

if (isset($_REQUEST['login']) && $svaTool->regularStringCheck($_REQUEST['login']))
    $login = addslashes($_REQUEST['login']);
else
    $errorCode = 5;

if (isset($_REQUEST['email']) && strlen($_REQUEST['email']) > 0 && $svaTool->regularEmailCheck($_REQUEST['email']))
    $email = addslashes($_REQUEST['email']);
else
    $errorCode = 6;
	

$id = $_REQUEST['sccUserId'];
$username = $_REQUEST['accountName'];

if (isset($_REQUEST['agencyId']) && $_REQUEST['agencyId'] != "-1")
    $agencyId = addslashes($_REQUEST['agencyId']);
else
    $errorCode = 15;
	
	/*
	if (isset($_REQUEST['password']) && strlen($_REQUEST['password']) > 0)
    $password = md5($_REQUEST['password']);
else
    $errorCode = 7;

if (isset($_REQUEST['confPasswd']) && strlen($_REQUEST['confPasswd']) > 0)
    $confPasswd = md5($_REQUEST['confPasswd']);
else
    $errorCode = 8;

if (strcmp($password, $confPasswd) != 0) {

    $errorCode = 9;
}
*/
//if (isset($_REQUEST['tariffId']) &&  $_REQUEST['tariffId'] !=-1) $tariffId = $_REQUEST['tariffId'] ;else $errorCode = 14;
$tariffId=13;


if ($errorCode == 0) {
$password = 0;

$res = $dba->UpdateAddSCCUserExtend(1,$id,$profileId, $login,$firstName, $lastName, $password, $mobilePhone, $_SESSION['ownerId'],  $email,$agencyId) ;

if( $profileId== 2 || $profileId == 3 ){
	
	$id = $dba->getSIPAccountIdbyUsername($username);
	
	$dba->UpdateAddSIPTrunk(1, $id, $mobilePhone,"dynamic", 5060, "friend", $mobilePhone, $mobilePhone, 0, 0);
	
}

/** Reconstruction des agents ***/
$res = $dba->getSIPDetailConfiguration(-1, 0);
$sipuserlist = "";
for($i=0;$i<count($res);$i++){
	
		$sipuserlist .= $svaTool->getSIPAgentConfig($res[$i]['username'], $res[$i]['type'], $res[$i]['host'], $res[$i]['secret'] );
	
	
}
$fp = fopen($_SESSION['cfg']['scc']['sip.additional.file'], 'w') or die ('Impossible d\'accéder au fichier.');
fputs($fp,$sipuserlist);
fclose($fp);

$manager->reloadSIPConfig();




   //$errorCode = 10;
}




switch ($errorCode) {

    case 0 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"COMPTE MODIFIE AVEC SUCCES !\"  }"; 
        break;
    case 1 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LE NOM ENTRE N'EST PAS VALIDE !\"  }";
        break;
    case 2 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LE PRENOM ENTRE N'EST PAS VALIDE !\"  }"; 
        break;
    case 3 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LE NUMERO DE TELEPHONE ENTRE N'EST PAS VALIDE.\"  }"; 
        break;
    case 4 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! VEUILLEZ SELECTIONNER LE PROFIL DE L'UTILISATEUR.\"  }";
        break;
    case 5 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LE LOGIN EST INVALIDE.\"  }"; 
        break;
    case 6 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! L'EMAIL EST INVALIDE.\"  }";
        break;
    /*case 7 : $msg =  "ERREUR ! LE MOT DE PASSE EST INVALIDE.";
        break;*/
    case 8 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LE MOT DE PASSE DE CONFIRMATION EST INVALIDE .!\"  }"; 
        break;
    case 9 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LES MOTS DE PASSE NE CONCORDENT PAS.\"  }";
        break;
    case 13 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! IMPOSSIBLE DE CREER LE COMPTE. VEUILLEZ RE-ESSAYER ULTERIEUREMENT!\"  }";  break;
		case 14 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! VEUILLEZ SELECTIONNER LE PLAN TARIFAIRE ASSOCIE AU COMPTE !\"  }";   break;
		case 15 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! VEUILLEZ SELECTIONNER L'AGENCE !\"  }";   break;
   
	 
}


echo $msg ;


?>