<?php

session_start();
include './core.php';

$tool = new svaTool();
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$login = addslashes($_REQUEST['username']);
$password = addslashes($_REQUEST['password']);
$extensions = trim($_REQUEST['extensions']);

//print_r($cfgWeb);
//echo "Password : ".$password ;

$managerDtls = $dba->getManagerDetails($login);

//print_r($managerDtls);

if (strcmp(md5($password), $managerDtls['password_v']) == 0) {
    $_SESSION['login'] = $login;
    $_SESSION['ownerId'] = $managerDtls['user_id_n'];
    $_SESSION['identity'] = $managerDtls['last_name_v'] . "  " . $managerDtls['first_name_v'];
    $_SESSION['right'] = $managerDtls['profile_id_n'];
    $_SESSION['sequence'] = 1;
    $_SESSION['leftActiveItem'] = 1;
    $_SESSION['basedir'] = __DIR__ . DIRECTORY_SEPARATOR;

    if ($managerDtls['profile_id_n'] == "1")
        $_SESSION['accntType'] = "Administrateur";
    else
        $_SESSION['accntType'] = "Utilisateur";

    $_SESSION['firstName'] = $managerDtls['first_name_v'];
    $_SESSION['lastName'] = $managerDtls['last_name_v'];
    $_SESSION['phone'] = $managerDtls['mobile_phone_v'];
    $_SESSION['email'] = $managerDtls['email_v'];
    $_SESSION['pid'] = $managerDtls['password_v'];
    $_SESSION['cfg'] = $cfg;

    //$managerDtls = $dba->addPresence($_SESSION['ownerId']);
    // header("Location:main.php",false); 
    
     //################ Inserting logging Information #############
        // Method = 0 -- Login/Loggout     , Event =0 -- Login /1 Loggout
        // Method = 1 -- Pause/Unpaus     , Event =0 -- Pause /1 Unpause
        
         $dba->UploadEventInformation($managerDtls['user_id_n'], 0, 0);
        
        //############################################################
 
    
    if(is_numeric($extensions)) {
        
         $resdba = $dba->checkSccOnlineUser($userId, $extension);

    if (count($resdba) == 0) {
        
        // Insert user data
        $lastUpdate = "";
        $_SESSION['extension'] = $extensions;
        $dba->UpdateAddDelSCCOnlineUser(0, 0, $_SESSION['ownerId'], $_SESSION['extension'] , $lastUpdate, 0);
        $dba->UpdateAddDelSCCUserNoProfilCheck(1, $_SESSION['ownerId'], "", $extensions, 0) ;
        $tool->SCCRefreshQueue($dba, $manager);

        echo '{"errorCode":0, "errorMessage" : "Success"}';
    } elseif($resdba[0]['user_id_n'] == $userId) {
         
         $lastUpdate = "";
        $_SESSION['extension'] = $extensions;
        $dba->UpdateAddDelSCCOnlineUser(1, $resdba[0]['item_id_n'], $_SESSION['ownerId'], $_SESSION['extension'] , $lastUpdate, 0);
         $dba->UpdateAddDelSCCUserNoProfilCheck(1, $_SESSION['ownerId'], "", $extensions, 0) ;
        $tool->SCCRefreshQueue($dba, $manager);

        echo '{"errorCode":0, "errorMessage" : "Success"}';

      
    }
 else {
        echo '{"errorCode":3, "errorMessage" : "<b style=\"color:red\" >L\'extension est en cours d\'utilisation !</b>"}';
    }
        
    }
    else {
        
        echo '{"errorCode":4, "errorMessage" : "<b style=\"color:red\" >L\'extension est invalide !</b>"}';
    }

   
} elseif ($managerDtls == -1) {

    echo '{"errorCode":2, "errorMessage" : "<b style=\"color:red\" >Impossible de se connecter à la base de données !</b>"}';
} else {

    echo '{"errorCode":1, "errorMessage" : "<b style=\"color:red\" >Login ou Mot de passe incorrect!</b>"}';
    // $_SESSION['errorMessage']= "Login ou Password Incorrect!";
    // header("Location:index.php?errorMessage=Erreur de connexion");
}
?>
