<?
/**
 * VoiceOne Settings/System/CLI
 *
 * Frontend to provide access to the system Command Line Interface  
 * @package voiceone
 * @subpackage  settings_system_cli
 */
  include ("../../../admin/config/cfg.php");
  global $cwlang, $cwLanguage;
  require_once("$absBasePath/lib/xajax/xajax.inc.php");
  $alert=getAlert();
  //================ XAJAX ================
  function processFormData($formData){
    global $asterisk,$cwlang,$relBasePath;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    $confirmBeforeSubmit="";
    //extract form params
    $form=$formData["smtp"];
    $host=$form["host"];
    $port=$form["port"];
    $from=$form["from"];
    $user=$form["user"];
    $password=$form["password"];
    $imgErr="<img src=\"".$relBasePath."/public/img/shutdown.png\" alt=\"error\">";
    //Check hostname (not null)&&(no spaces)
    $hostErr=false;
    if(trim($host)==""){
      //host must be not null
      $errOut[]= T_("Host name must be not null");
      $objResponse->addAssign("hostErr","innerHTML",$imgErr);
      $hostErr=true;
    }else{
      //host can not contain spaces
      for($i=0; $i<strlen($host); $i++){
        $ch=ord($host[$i]);
        if($ch==32){
          $errOut[]= T_("Host can not cointan spaces. Please change it");
          $objResponse->addAssign("hostErr","innerHTML",$imgErr);
          $hostErr=true;
          break;
        }
     	}
    }
    if($hostErr===false) $objResponse->addAssign("hostErr","innerHTML","");
    //Check port (not null)&&(numeric)
    $portErr=false;
    if(trim($port)==""){
      //port must be not null
      $errOut[]= T_("Port must be not null");
      $objResponse->addAssign("portErr","innerHTML",$imgErr);
      $portErr=true;
    }else{
      //port must be numeric
      for($i=0; $i<strlen($port); $i++){
        $ch=ord($port[$i]);
        if(!(($ch>47)&&($ch<58))){
          $errOut[]= T_("Port must be numeric. Please change it");
          $objResponse->addAssign("portErr","innerHTML",$imgErr);
          $portErr=true;
          break;
        }
     	}
    }
    if($portErr===false) $objResponse->addAssign("portErr","innerHTML","");
    //Check from address (not null)&&(no spaces)
    $fromErr=false;
    if(trim($from)==""){
      //host must be not null
      $errOut[]= T_("From address must be not null");
      $objResponse->addAssign("fromErr","innerHTML",$imgErr);
      $fromErr=true;
    }else{
      //host can not contain spaces
      for($i=0; $i<strlen($from); $i++){
        $ch=ord($from[$i]);
        if($ch==32){
          $errOut[]= T_("From address can not cointan spaces. Please change it");
          $objResponse->addAssign("fromErr","innerHTML",$imgErr);
          $fromErr=true;
          break;
        }
     	}
    }
    if($fromErr===false) $objResponse->addAssign("fromErr","innerHTML","");
    //Check user (no spaces)
    $userErr=false;
    if($user!=""){
      //user can not contain spaces
      for($i=0; $i<strlen($user); $i++){
        $ch=ord($user[$i]);
        if($ch==32){
          $errOut[]= T_("Username can not cointan spaces. Please change it");
          $objResponse->addAssign("userErr","innerHTML",$imgErr);
          $userErr=true;
          break;
        }
     	}
    }
    if($userErr===false) $objResponse->addAssign("userErr","innerHTML","");
    //Check password (no spaces)
    $passwordErr=false;
    if(trim($password)==""){
      //host can not contain spaces
      for($i=0; $i<strlen($password); $i++){
        $ch=ord($password[$i]);
        if($ch==32){
          $errOut[]= T_("Password can not cointan spaces. Please change it");
          $objResponse->addAssign("passwordErr","innerHTML",$imgErr);
          $passwordErr=true;
          break;
        }
     	}
    }
    if($passwordErr===false) $objResponse->addAssign("passwordErr","innerHTML","");
    // Process Form 
    if(isset($errOut)){
      $error="<div class=\"AlertBad\"><ul>";
      foreach($errOut as $err) $error.="<li>".$err."</li>";
      $error.="</ul></div>";
      $objResponse->addAssign("alert","innerHTML",$error);
      $objResponse->addAssign("loading","innerHTML","");
    }else{
      $objResponse->addAssign("loading","innerHTML","");
      $script=" document.getElementById('mainform').submit(); "; 
      $objResponse->addScript($script);
    }
    // Return the xajaxResponse object
    return $objResponse->getXML();
  }
  $xajax = new xajax();
  $xajax->registerFunction("processFormData");
  $xajax->processRequests();
  //================== POST ====================
  if(count($_POST)>0){
  	//prepare structure
    $smtpArr=$_POST["smtp"];
    $entry=array(
          "smtp_host" => $smtpArr["host"],
          "smtp_protocol" => $smtpArr["protocol"],
          "smtp_port" => $smtpArr["port"],
          "smtp_from" => $smtpArr["from"],
          "smtp_auth" => $smtpArr["auth"],
          "smtp_user" => $smtpArr["user"],
          "smtp_pass" => $smtpArr["password"]
      	);
  	//save configuration
   	$ret=$asterisk->putSmtpConf($entry); 
    if($ret>0) $alert="<div class=\"AlertGood\">".T_('Smtp Configuration has been successfully saved')."</div>";
		else $alert="<div class=\"AlertBad\">".T_('Edit failed')."</div>";
	  //go to main page
    goToMainPage($alert,"./?rnd=".rand(0,99999));
  }
  //================= PAGE LOGIC ==================
  //extract network configuration
  $ret=$asterisk->getSmtpConf();
  if(is_array($ret)){
    $host=$ret["smtp_host"];
    $protocol=$ret["smtp_protocol"];
  	$port=$ret["smtp_port"];
    $from=$ret["smtp_from"];
    $auth=$ret["smtp_auth"];
    $user=$ret["smtp_user"];
    $password=$ret["smtp_pass"];
    if($auth=="off"){
      $disableAuth="disabled=\"disabled\"";
      $user="";
      $password=""; 
    }else $disableAuth="";
  }
  //protocol
  $protocolOpt="";
  $protocolArray=array("smtp","lmtp");
  if(is_array($protocolArray)){
    foreach($protocolArray as $p){
      if($p==$protocol) $s="selected=\"selected\""; else $s="";
      $protocolOpt.="<option value=\"".$p."\" $s>".strtoupper($p)."</option>\n";
    }
  }
  //authentication methods
  $mathodOpt="";
  $mathodArray=array("off","on","plain","cram-md5","digest-md5","gssapi","external","login","ntlm");
  if(is_array($mathodArray)){
    foreach($mathodArray as $m){
      if($m==$auth) $s="selected=\"selected\""; else $s="";
      if($m=="on") $label="auto"; else $label=$m;
      $mathodOpt.="<option value=\"".$m."\" $s>".ucfirst($label)."</option>\n";
    }
  }
  //================ MAIN CONTENTS ================
  ob_start();
  $xajax->printJavascript("../../../admin/lib/xajax/");
  ?>
    <?$sel="smtp";include("../navigation.php");?>
    <div class="SectionHeader">
      <?=$alert?>
      <h3 style="float:right;margin: 7px 7px 0 0;"><img style="cursor:pointer;" src="<?=$relBasePath?>/public/img/reboot.png" alt="Refresh" onclick="location.href='';"/></h3>
      <h1><?=T_("Smtp")?></h1> 
    </div>
    <div id="alert"></div>
    <div id="basic_form">
      <form id="mainform" action="<?=$PHP_SELF?>" method="post">
        <!-- Server -->
        <h2><?=T_('Server settings')?></h2>
        <p class="blockintro"><?=T_('Insert here the smtp settings')?></p>
        <div class="block">
          <dl>  
            <dt class="required"><label for="smtp[protocol]"><?=T_("Protocol")?>:</label></dt>
            <dd><select id="smtp[protocol]" name="smtp[protocol]"><?=$protocolOpt?></select> <span id="protocolErr"></span></dd>
            <dt class="required"><label for="smtp[host]"><?=T_("Host")?>:</label></dt>
            <dd><input type="text" size="25" id="smtp[host]" name="smtp[host]" value="<?=$host?>" /> <span id="hostErr"></span></dd>
            <dt class="required"><label for="smtp[port]"><?=T_("Port")?>:</label></dt>
            <dd><input type="text" size="25" id="smtp[port]" name="smtp[port]" value="<?=$port?>" /> <span id="portErr"></span></dd>
          </dl>
        </div>
        <!-- Account -->
        <h2><?=T_('Authentication')?></h2>
        <p class="blockintro"><?=T_('Insert here the authentication settings')?></p>
        <div class="block">
          <dl>
            <dt class="required"><label for="smtp[from]"><?=T_("From Address")?>:</label></dt>
            <dd><input type="text" size="25" id="smtp[from]" name="smtp[from]" value="<?=$from?>" /> <span id="fromErr"></span></dd>
            <dt class="required"><label for="smtp[auth]"><?=T_("Method")?>:</label></dt>
            <dd><select id="smtp[auth]" name="smtp[auth]" onChange="if(this.selectedIndex==0) disableUserPass(); else enableUserPass();"><?=$mathodOpt?></select> <span id="methodErr"></span></dd>
            <dt class="required"><label for="smtp[user]"><?=T_("Username")?>:</label></dt>
            <dd><input type="text" size="25" id="smtp[user]" name="smtp[user]" value="<?=$user?>" <?=$disableAuth?>/> <span id="usereErr"></span></dd>
            <dt class="required"><label for="smtp[password]"><?=T_("Password")?>:</label></dt>
            <dd><input type="password" size="25" id="smtp[password]" name="smtp[password]" value="<?=$password?>" <?=$disableAuth?>/> <span id="passwordErr"></span></dd>
          </dl>
        </div>
  			<!-- Buttons -->
        <div class="action">
          <a href="#"><img src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-save.gif" alt="Save" onclick=" document.getElementById('alert').innerHTML=''; xajax_processFormData(xajax.getFormValues('mainform')); "/></a>
      		<!--
            <input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-save.gif" alt="Save" onclick="return checkParam(document.getElementById('client[extension]').value, document.getElementById('client[alias]').value, document.getElementById('client[password]').value, document.getElementById('client[confirm]').value);"/>
          -->
        </div>
			</form>
    </div>
    <script>
    	function disableUserPass(){
        document.getElementById("smtp[user]").disabled="disabled";
        document.getElementById("smtp[password]").disabled="disabled";
      }
      function enableUserPass(){
        document.getElementById("smtp[user]").disabled="";
        document.getElementById("smtp[password]").disabled="";
      }
    </script>
  <?
  $contents=ob_get_contents();
  ob_end_clean();
  //================ SIDEBAR ================
  ob_start(); 
  ?>
    <div class="Sidebar">
      <h1><?=T_("Informations")?></h1>
      <p><?=T_("This page shows the SMTP service configuration. To modify this configuration edit parameters and press save button on the end of the page.")?></p>
    </div>         
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();
  //================ SETTINGS ================
  $smarty->assign("section", "system");
  $smarty->assign("subsection", "");
  $smarty->assign("templateType", "TwoColLeft");  //OneCol, TwoColLeft, TwoColRight
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->display('settings.tpl');
?>

