<?
/**
 * 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
 */
 
// Sessions are enabled for default and disabled in download scripts, 
// like stream.php for sounds and backups
if((!isset($noSession))||($noSession===false)){
	session_start();
}
if($setup_procedure!==true) $setup_procedure=false;
//Get Voiceone dir
$dir = dirname(__FILE__) . '/';
//Verify config files accessibility
if(
   (!file_exists($dir . "config.inc.php")) ||
   (!((is_dir($dir.'../public/template/cache'))&&(is_writable($dir.'../public/template/cache')))) ||
   (!((is_dir($dir.'../../sandbox'))&&(is_writable($dir.'../../sandbox')))) ||
   (!((is_dir($dir.'../../sandbox/plugins'))&&(is_writable($dir.'../../sandbox/plugins'))))
  ){
  header ("location: /voiceone/setup.php");
  die();
}
//Include statements
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/en.php";
require_once $dir . '../lib/gettext/i18n.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->clearCompiledTemplate(); 
$smarty->assign("cw_public",$relBasePath."public/");
//read the template Translation
foreach($cwlang["template"] as $cwk=>$cwv) $smarty->assign($cwk,$cwv);
//SOAP wrapper ON:create a nuSoap Client
$forcelabelreload=false;
if($soapWrapper===true){
	if(!is_file($webserviceAbsPath."config/config.inc.php")) $asterisk=false;
  else{
    //include all the needed file
    include_once ($webserviceAbsPath."config/cfg.php");
    //Include webservice's functions
    $base_folder=$webserviceAbsPath."modules";
    //die($base_folder);
    if($handle=opendir($base_folder)){
      while(false !== ($file = readdir($handle))){
        if(($file==".")||($file=="..")) continue;
        if((is_dir($base_folder."/".$file))&&(is_file($base_folder."/".$file."/functions/methods.php")) ){
          require_once $base_folder."/".$file."/functions/methods.php";
        }
      }
      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");
      $forcelabelreload=true;
    }
    //read the proxy class wrapper and evaluate it
    $proxyClass=file_get_contents($proxyClassFile);
  	eval($proxyClass);
  	//eval("\$asterisk = new soap_proxy();");
  	eval("\$asterisk = new nusoap_proxy();");
  }
}
//SOAP wrapper OFF
else{
  require_once dirname(__FILE__) . '/../lib/nusoap/nusoap.php';
  //$soapClient = new soapclient($soapWsdl, true);
  $soapClient = new nusoap_client($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);
}
//redirect to setup if no users are defined on the system
if($setup_procedure===false){ //skip redirect on setup
  $userArr=$asterisk->getUser("");
  if((!is_array($userArr))||(count($userArr)==0)){
  	header("location: /voiceone/setup.php");
    die;
  }
}
//if setup is running don't check user login and return;
if (strstr($_SERVER["PHP_SELF"],"setup.php")!==false) return;
//check for authentication (don't ask if session is turned off -> see row 14)
if(($_COOKIE["cwUserId"]=="")&&(($_SESSION["cwUserId"]=="")&&((!isset($noSession))||($noSession===false)))){
  //  no session and no cookie - user must login first
  if (isset($_POST["user_name"])) {
    // user has typed his name and password
    $uid=checkUserAccount($_POST["user_name"],$_POST["password"]);
    if ($uid>0) {
      // user account is valid. So the user can request to set a cookie
      if ($_POST["remember_me"]=="1") {
          setcookie ("cwUserId", $uid,time()+3600*24*365,"/");
      }
      else {
          // if no cookie, than we set the session id
          $_SESSION['cwUserId'] = $uid;
      }
      // redirect to index.php and exit
      header ("location: index.php");
      die();
    } else {
        // user account is invalid - set errortext
        $alert="<div class=\"AlertBad\">" . $cwlang["generic"]["loginErrror"]. "</div>";
    }
  }  // end if  post username

  // set header to 403
  header('HTTP/1.0 401 Unauthorized');
  header('Status: 401 Unauthorized');
  $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"]);
//Initialize the Localization-API
if (trim($cwUserInfo["lang"])!=""){ 
  $lang=strtolower($cwUserInfo["lang"])."_".strtoupper($cwUserInfo["lang"]); 
}else $lang=$cw_lang;
T_setlocale(LC_MESSAGES, $lang);
T_bindtextdomain($domain, $absBasePath."/public/lang");
T_bind_textdomain_codeset($domain, $encoding);
T_textdomain($domain);
//OLD LANGUAGE IMPLEMENTATION: set the user language 
$_SESSION["cwUserLang"]=substr($lang,0,2);
$cwLanguage=substr($lang,0,2);
foreach($cwlang["template"] as $cwk=>$cwv) $smarty->assign($cwk,$cwv); 
//check the pbx status
$ret=$asterisk->pbxSwitch("status");
if ($ret=="ON") $smarty->assign("pbxStatus", $relBasePath."/public/img/tl-green-small.gif");
else $smarty->assign("pbxStatus", $relBasePath."/public/img/tl-red-small.gif");
//set the hostname
if(($forcelabelreload===true)||(!isset($_SESSION["hostname"]))){
  $systemOptionArr=$asterisk->getOptionByName("hostname","SYSTEM");
  if((is_array($systemOptionArr))&&(is_array($systemOptionArr[0]))){
    $_SESSION["hostname"]=$systemOptionArr[0]["value"];
  }
}
$smarty->assign("hostname", $_SESSION["hostname"]);
//set system version
if(($forcelabelreload===true)||(!isset($_SESSION["voiceone_version"]))){
  $systemOptionArr=$asterisk->getOptionByName("voiceone_version","SYSTEM");
  if((is_array($systemOptionArr))&&(is_array($systemOptionArr[0]))){
    $_SESSION["voiceone_version"]=$systemOptionArr[0]["value"];
  }
}
$smarty->assign("voiceone_version", $_SESSION["voiceone_version"]);
//set system release
if(($forcelabelreload===true)||(!isset($_SESSION["voiceone_release"]))){
  $systemOptionArr=$asterisk->getOptionByName("voiceone_release","SYSTEM");
  if((is_array($systemOptionArr))&&(is_array($systemOptionArr[0]))){
    $_SESSION["voiceone_release"]=$systemOptionArr[0]["value"];
  }
}
$smarty->assign("voiceone_release", $_SESSION["voiceone_release"]);
//logout
if (isset($_GET["cwLogOut"]) or !is_array($cwUserInfo) ) {
  setcookie ("cwUserId", $uid,time()-3600,"/");
  unset($_SESSION['cwUserId']);
  header ("location: index.php");
  die();
}
?>