<?
/**
 * VoiceOne Configuration
 *
 * Callware VoiceOne is an easy to use web based GUI for the Asterisk PBX. 
 * It gives full control over PBX settings from the admin configuration panel. 
 * You can manage extensions, VoIP trunks, users queues and rules sets, 
 * and dynamically create a IVR.
 * @package voiceone
 */

session_start();

// include statements
$dir = dirname(__FILE__) . '/';
require_once $dir . 'config.inc.php';
require_once $dir . 'constants.php';
require_once $dir . '../lib/smarty/libs/Smarty.class.php';
require_once $dir . '../lib/sajax/Sajax.php';
require_once $dir . '../lib/functions.php';
require_once $dir . "../public/lang/".$cwLanguage.".php";

//create a Smarty object
$smarty = new Smarty;
$smarty->template_dir = $dir.'../public/template/';
$smarty->compile_dir = $dir.'../public/template/cache/';
$smarty->config_dir = $dir.'../public/template/config/';
$smarty->cache_dir = $dir.'../public/template/cache/';
$smarty->plugins_dir = array($dir."../public/plugin/"); 
$smarty->caching = false;
$smarty->clear_compiled_tpl();
$smarty->assign("cw_public",$relBasePath."public/");

//read the template Translation
foreach($cwlang["template"] as $cwk=>$cwv) $smarty->assign($cwk,$cwv); 

//create a nuSoap Client
if ($soapWrapper===true) {

  //include all the needed file
  include_once ($webserviceAbsPath."config/cfg.php");
  //include all files in the voiceone_webservices/function/ folder
  if ($handle = opendir($webserviceAbsPath."function/")) { 
    while (false !== ($file = readdir($handle))) 
      if ($file != "." && $file != ".." and substr($file,-4)==".php" ) 
        include ($webserviceAbsPath."function/".$file);
    closedir($handle);
  }
  require_once dirname(__FILE__) . '/../lib/nusoapWrapper/class.soapclientLocal.php';

  //proxy class wrapper filename
  $proxyClassFile=$absBasePath."../sandbox/wsdl.php";

  //create che proxy class wrapper if doesn't exists and cache it in the sandbox/ folder
  if (!file_exists($proxyClassFile)) {
    $soapClient = new soapclientLocal($soapWsdl, true);
    ini_set("soap.wsdl_cache_enabled", "1"); 
    $err = $soapClient->getError();
    if ($err) die('An error occurred while the WSDL was being accessed: '.$err);
    $localSoapClass=$soapClient->_getLocalProxyClassCode();
    file_put_contents($proxyClassFile,$localSoapClass);  
    error_log("CREATED THE WSDL LOCAL WRAPPER: $proxyClassFile");
  }
  
  //read the proxy class wrapper and evaluate it
  $proxyClass=file_get_contents($proxyClassFile);
	eval($proxyClass);
	eval("\$asterisk = new soap_proxy();");
  
} else {
  require_once dirname(__FILE__) . '/../lib/nusoap/nusoap.php';
  $soapClient = new nusoapclient($soapWsdl, true);
  ini_set("soap.wsdl_cache_enabled", "1"); 
  $err = $soapClient->getError();
  if ($err) die('An error occurred while the WSDL was being accessed: '.$err);
  $asterisk=$soapClient->getProxy();
  $asterisk->setCredentials($soapUserName,$soapPassword);
  $asterisk->useHTTPPersistentConnection();
}
  
//bypass the register globals setting
if (!ini_get('register_globals')) {
  $superglobals = array($_SERVER, $_ENV, $_FILES, $_COOKIE, $_POST, $_GET);
  if (isset($_SESSION)) extract($_SESSION);
  foreach ($superglobals as $superglobal) extract($superglobal, EXTR_SKIP);
}

//if setup is running don't check user login and return;
if (strstr($_SERVER["PHP_SELF"],"setup.php")!==false) return;

//check for authentication
if ($_SESSION["cwUserId"]=="" and $_COOKIE["cwUserId"]=="")  { 
  if (isset($_POST["user_name"])) {
    $uid=checkUserAccount($_POST["user_name"],$_POST["password"]);
    if ($uid>0) {
      if ($_POST["remember_me"]=="1") setcookie ("cwUserId", $uid,time()+3600*24*365,"/");
      else $_SESSION['cwUserId'] = $uid;
      header ("location: index.php");
      die();
    } else $alert="<div class=\"AlertBad\">Nome utente o password errati</div>";    
  } 
  $smarty->assign("alert", $alert);
  $smarty->assign("contents", $contents);
  $smarty->display('login.tpl');
  die;
}

//get User Information
$cwUserInfo=@array_pop($asterisk->getUser($cwUserId));
if (is_array($cwUserInfo)) $smarty->assign("cwUser", $cwUserInfo["username"]);
//echo "<pre>"; print_r($cwUserInfo); echo "</pre>";

//set the user language
if (trim($cwUserInfo["lang"])!="") $_SESSION["cwUserLang"]=$cwUserInfo["lang"]; 
else $_SESSION["cwUserLang"]=$cwLanguage;
//die ($_SESSION["cwUserLang"]."-".$cwUserInfo["lang"]);

//include the new user language 
require_once (dirname(__FILE__)."/../public/lang/".$_SESSION["cwUserLang"].".php"); 
foreach($cwlang["template"] as $cwk=>$cwv) $smarty->assign($cwk,$cwv); 
$cwLanguage=$_SESSION["cwUserLang"];

//logout
if (isset($_GET["cwLogOut"]) or !is_array($cwUserInfo) ) {
  setcookie ("cwUserId", $uid,time()-3600,"/");
  unset($_SESSION['cwUserId']);
  header ("location: index.php");
  die();
}


?>