<?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'];
$smtpUseTls = $_REQUEST['smtpUseTls'];
$useAuthSmtp = $_REQUEST['useAuthSmtp'];


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

    $errorCode = 3;
}


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

    $errorCode = 4;
}




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

    $errorCode = 5;
}

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

    $errorCode = 6;
}





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

    $errorCode = 7;
}


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

    $errorCode = 8;
}


$useTls = $_REQUEST['useTls'];



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

    $errorCode = 10;
}


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

    $errorCode = 9;
}


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







$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->UpdateAddDelMailChannel($opId, $itemId, $accountName, $mailAccount, $password, $smtpServer, $smtpPort, $smtpUseTls, $useAuthSmtp, $incomingServer, $incomingPort, $useTls, $accountType, $itemId,  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 SERVEUR SMTP EST INVALIDE !\"  }";
        break;
    case 4 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LE PORT DU SERVEUR SMTP EST INVALIDE !\"  }";
        break;
    case 5 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! L'INTITULE DU COMPTE EST INVALIDE !\"  }";
        break;
    case 6 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LE COMPTE DE MESSAGERIE EST INVALIDE !\"  }";
        break;
    case 7 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LE SERVEUR ENTRANT EST INVALIDE !\"  }";
        break;
    case 8 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LE PORT DU SERVEUR ENTRANT EST INVALIDE !\"  }";
        break;
    case 9 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LE MOT DE PASSE EST INAVLIDE !\"  }";
        break;
    case 10 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LA CONFIRMATION DU MOT DE PASSE EST INVALIDE !\"  }";
        break;
    
     case 11 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LES MOTS DE PASSE NE CONCORDENT PAS  !\"  }";
        break;
   
}


echo $msg;
?>