<?
/**
 * VoiceOne Setup
 *
 * Setup of voiceone
 * @package voiceone
 * @subpackage  setup
 */
  session_start();
  $setup_procedure=true;
  $step=$_GET["step"];
  if(($step!="")&&($step!="1")){
  	require("admin/config/cfg.php");
		global $cwlang, $cwLanguage;
  }
  if((isset($_POST['license_agreement']))&&($_POST['license_agreement']==1)){
    $_SESSION['license_agreement']=1;
    header('location: setup.php?step=3');
    die();
  }
/******************************************************************************/
/*************************  LABELS  *****************************/
/******************************************************************************/
  $title					=		"VoiceOne Installation";
  $subTitle				=		"The installation";
  $step1a_title		=		"System Setup (Requirements)";
  $step1a_subtitle=		"Voiceone Interface Permissions";
  $step1b_title		=		"System Setup (Requirements)";
  $step1b_subtitle=		"Voiceone Web Services Permissions";
  $step2_title		=		"System Setup (2/6)";
  $step2_subtitle	=		"License Agreement";
  $step2_accept   =   "I accept terms and conditions";
  $step3_title		=		"System Setup (3/6)";
  $step3_subtitle	=		"Voiceone Installation";
  $root_needed		=		"Voiceone has been already installed on the system. ".
   										"To continue the installation you must have superuser privileges. ".
                      "Please fill the form with superuser login.";
  $root_delete		=		"This user will be deleted as well as other settings during installation.";
  $keyGeneration  =   "Generate RSA keys? ";
  $keyHelp 				=		"(if checked key will be generated)";
	$username 			=   "Username"; 
  $password 			=   "Password";
  $step4_title		=		"System Setup (4/6)";
  $step4_subtitle1=		"Internal System Passwords";
  $step4_desc     =   "Voiceone system dispose a few services, some of them can be accessed from outside the system through a socket connection.<br/>".
                      "This services are reported follow. Please change the secury password for each one to prevent malicious connections.";
  $pass_api       =		"Asterisk API";
  $pass_db        =		"Mysql";
  $pass_ws        =		"Web Services";
  $pass_ssh       =		"SSH";
  $pass_error     =		"Passwords must be almost 8 characters, widthout spaces and equal to confirm";
  $save_error     =   "Passwords edit faild. Please try again with a new password";
  $step5_title		=		"System Setup (5/6)";
  $step5_subtitle =		"Register Superuser";
  $step5_desc     =   "Define the Voiceons's superuser and its login informations";
  $confirm  			=   "Confirm";
  $language				=   "Language";
  $login_incorect = 	"Login incorrect";
  $sysnameerror =   "Name can not be null";
  $usrerror =   "Username can not be null";
  $pwdempty =   "Password cannot be null";
  $pwderror =   "Password doesn't match";
  $step6_title		=		"System Setup (6/6)";
  $step6_desc     =   "The last step is set some system parameters:";
  $step6_subtitle1=		"System Configuration";
  $step6_subtitle2=		"Network Configuration";
  $step6_subtitle3=   "DHCP Configuration";
  $step6_subtitle4=   "VPN Configuration";
  $step6_subtitle5=   "NTP Configuration";
  $step6_subtitle6=   "SMTP Configuration";
  $system_label  	=   "System Label";
  $host_name  	  =   "Host Name";
  $domain_name  	=   "Domain Name";
  $address        =   "Address";
  $netmask        =   "Netmask";
  $network        =   "Network";
  $broadcast      =   "Broadcast";
  $gateway        =   "Gateway";
  $dns1           =   "DNS 1";
  $dns2           =   "DNS 2";
  $dhcp_status    =   "Status";
  $range_from     =   "Range Start";
  $range_to       =   "Range End";
  $ntp_host       =   "Host";
  $ntp_zone       =   "Zone";
  $ntp_time       =   "Time";
  $smtp_host      =   "Host";
  $smtp_protocol  =   "Protocol";
  $smtp_port      =   "Port";
  $smtp_from      =   "From Address";
  $smtp_auth      =   "Authentication";
  $smtp_user      =   "Username"; 
  $smtp_password  =   "Password";
  $range_start_label= " - DHCP Range Start -";
  $range_end_label=   " - DHCP Range End -";
  $conf_generic_error = "Invalid field value";
  $roll_msg =   "Please wait...<br/>(this operation may takes few minutes)";
  $img_ok="<img src=\"admin/public/img/ok.gif\" />";
  $img_no="<img src=\"admin/public/img/shutdown.png\" />";
  
  switch($step){
/******************************************************************************/
/***********************  STEP 1a: Check Voiceone  ****************************/
/******************************************************************************/
    case "1":
    default:	
      //================ PAGE LOGIC ================
      $alert="";
      $error=false;
      //check the config.inc.php permissions
      $alert.="<tr>";
      if(!(
      			(@fileperms("admin/config/config.inc.php") & 0x0004)&&
      			(@fileperms("admin/config/config.inc.php") & 0x0002)
           )
        ){
      	$error=true;
      	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_no."</td>";
        $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:red; font-size:smaller; margin:0;\"><strong>Create</strong> configuration file with <strong>write</strong> permission:<br/> <strong>".realpath("admin/config/")."/config.inc.php</strong></p></td>";
      }else{
      	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_ok."</td>";
        $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:black; font-size:smaller; margin:0;\">Permissions on Config File</p></td>";      
      }
      $alert.="</tr>";
      //check the smarty cache folder permissions
      $alert.="<tr>";
      if(!(
      			(@fileperms("admin/public/template/cache") & 0x0004)&&
      			(@fileperms("admin/public/template/cache") & 0x0002)&&
            (@fileperms("admin/public/template/cache") & 0x0001)
           )
        ){
      	$error=true;
      	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_no."</td>";
        $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:red; font-size:smaller; margin:0;\">Enable the <strong>read/write/execute</strong> permission to Smarty Cache folder<br/><strong>".@realpath("admin/public/template/cache")."</strong></p></td>";
      }else{
      	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_ok."</td>";
        $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:black; font-size:smaller; margin:0;\">Permissions on Smarty Cache folder</p></td>";      
      }
      $alert.="</tr>";
      //check the sandobox folder permissions
      $alert.="<tr>";
      if(!(
      			(@fileperms("sandbox") & 0x0004)&&
      			(@fileperms("sandbox") & 0x0002)&&
            (@fileperms("sandbox") & 0x0001)
           )
        ){
      	$error=true;
      	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_no."</td>";
        $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:red; font-size:smaller; margin:0;\">Enable the <strong>read/write/execute</strong> permission to Sandbox folder<br/><strong>".@realpath("sandbox")."</strong></p></td>";
      }else{
      	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_ok."</td>";
        $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:black; font-size:smaller; margin:0;\">Permissions on Sandbox folder</p></td>";      
      }
      $alert.="</tr>";
      //check the plugins folder permissions
      $alert.="<tr>";
      if(!(
      			(@fileperms("sandbox/plugins") & 0x0004)&&
      			(@fileperms("sandbox/plugins") & 0x0002)&&
            (@fileperms("sandbox/plugins") & 0x0001)
           )
        ){
      	$error=true;
      	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_no."</td>";
        $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:red; font-size:smaller; margin:0;\">Enable the <strong>read/write/execute</strong> permission to Plugin folder<br/> <strong>".@realpath("sandbox/plugins")."</strong></p></td>";
      }else{
      	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_ok."</td>";
        $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:black; font-size:smaller; margin:0;\">Permissions on Plugins folder</p></td>";      
      }
      $alert.="</tr>";
      //button
      if($error===false) $button="<input type=\"image\" src=\"admin/public/img/en/b-next.gif\" alt=\"Next\" onclick=\"location.href='setup.php?step=1b';\" />";
      else $button="<img src=\"admin/public/img/en/b-next_disabled.gif\" alt=\"Next\" />";
      //================== HTML ==================
      ob_start();
      ?>
      <div id="loginFirstTime">
        <h1><img src="admin/public/img/VoiceOne-logo.gif" /></h1>
        <h2><?=$title?></h2>
        <p><?=$subTitle?></p>
      </div>
      <div id="Dialog">
        <h1 style="margin:0;margin-bottom:15px;"><?=$step1a_title?></h1>
        <h3 style="margin:0;margin-bottom:10px;padding-left:30px;"><?=$step1a_subtitle?></h3>
				<table width="100%" cellpadding="15" cellspacing="10">
        	<?=$alert?>
        </table>
        <div style="width:96%;text-align:right;margin-top:20px;">
        	<?=$button?>
        </div>  
      </div>
      <?
      $contents=ob_get_contents();
      ob_end_clean();
    break;
/******************************************************************************/
/*********************  STEP 1b: Check WebSerivices  **************************/
/******************************************************************************/
    case "1b":
      //================ PAGE LOGIC ================
      $alert="";
      $error=false;
      //check interface folders permissions
      if((!((fileperms("admin/config/config.inc.php") & 0x0004)&&(fileperms("admin/config/config.inc.php") & 0x0002))) ||
      	 (!((fileperms("admin/public/template/cache") & 0x0001)&&(fileperms("admin/public/template/cache") & 0x0002)&&(fileperms("admin/public/template/cache") & 0x0004))) ||
      	 (!((fileperms("sandbox") & 0x0001)&&(fileperms("sandbox") & 0x0002)&&(fileperms("sandbox") & 0x0004)))											||
         (!((fileperms("sandbox/plugins") & 0x0001)&&(fileperms("sandbox/plugins") & 0x0002)&&(fileperms("sandbox/plugins") & 0x0004)))){
      	header("location: setup.php?step=1a;");
      	die;
      }
     	$config_path="~voiceone_webservices/config/config.inc.php";
      //check the config.inc.php permissions
      $alert.="<tr>";
      if($asterisk===false){
      	$error=true;
      	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_no."</td>";
        $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:red; font-size:smaller; margin:0;\"><strong>Create</strong> configuration file:<br/> <strong>".$config_path."</strong></p></td>";
      }else{
      	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_ok."</td>";
        $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:black; font-size:smaller; margin:0;\">Permissions on Config File</p></td>";      
      }
      $alert.="</tr>";
      //check the script install.sh permissions
      $alert.="<tr>";
      if($asterisk!==false){
      	$ret="";
        $ret=$asterisk->checkDatabaseAccess();
        if($ret!="1"){
        	$error=true;
        	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_no."</td>";
          $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:red; font-size:smaller; margin:0;\"><strong>Edit</strong> database settings in config file:<br/> <strong>".$config_path."</strong></p></td>";
        }else{
        	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_ok."</td>";
          $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:black; font-size:smaller; margin:0;\">Access to Voiceone database</p></td>";      
        }
      }
      $alert.="</tr>";
      //check the script install.sh permissions
      $alert.="<tr>";
      if($asterisk!==false){
      	$ret="";
        $ret=$asterisk->isVotoolsExecutable();
        if($ret!=1){
        	$error=true;
        	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_no."</td>";
          $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:red; font-size:smaller; margin:0;\">Enable the <strong>execute</strong> permission to script:<br/> <strong>".$ret."</strong></p></td>";
        }else{
        	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_ok."</td>";
          $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:black; font-size:smaller; margin:0;\">Permissions on Vo-tools script</p></td>";      
        }
      }
      $alert.="</tr>";
			//button
      if($error===false) $button="<input type=\"image\" src=\"admin/public/img/en/b-next.gif\" alt=\"Next\" onclick=\"location.href='setup.php?step=2';\" />";
      else $button="<img src=\"admin/public/img/en/b-next_disabled.gif\" alt=\"Next\" />";
      //================== HTML ==================
      ob_start();
      ?>
      <div id="loginFirstTime">
        <h1><img src="admin/public/img/VoiceOne-logo.gif" /></h1>
        <h2><?=$title?></h2>
        <p><?=$subTitle?></p>
      </div>
      <div id="Dialog">
        <h1 style="margin:0;margin-bottom:15px;"><?=$step1b_title?></h1>
        <h3 style="margin:0;margin-bottom:10px;padding-left:30px;"><?=$step1b_subtitle?></h3>
				<table width="90%" cellpadding="15" cellspacing="10">
        	<?=$alert?>
        </table>
        <div style="width:96%;text-align:right;margin-top:20px;">
        	<?=$button?>
        </div>
      </div>
      <?
      $contents=ob_get_contents();
      ob_end_clean();
    break;
/******************************************************************************/
/***********************  STEP 2: License Agreement  **************************/
/******************************************************************************/
    case "2":
      //check access to ws
    	if($asterisk===false){
     		header("location: setup.php?step=1b");
      	die;
      }
      unset($_SESSION['license_agreement']);
      //================== HTML ==================
      ob_start();
      ?>
        <div id="loginFirstTime">
          <h1><img src="admin/public/img/VoiceOne-logo.gif" /></h1>
          <h2><?=$title?></h2>
          <p><?=$subTitle?></p>
        </div>
        <div id="Dialog">
          <h1 style="margin:0;margin-bottom:15px;"><?=$step2_title?></h1>
          <h3 style="margin:0;margin-bottom:10px;padding-left:30px;"><?=$step2_subtitle?></h3>
  				<textarea style="width:100%;height:138px;font-size:11px;"><?include('LICENSE')?></textarea>
          <div style="width:100%;margin-top:20px;overflow:hidden;">
            <form action="setup.php?step=3" method="post">
              <input style="float:right;padding:0;display:none;" id="but-cont" type="image" src="admin/public/img/en/b-next.gif" alt="Next" />
              <img style="float:right;" id="but-cont-hidden" src="admin/public/img/en/b-next_disabled.gif" alt="Next" />
              <input type="checkbox" name="license_agreement" value="1" onclick="switchContinue(this.checked);" />
              <p style="display:inline"><?=$step2_accept?></p>
            </form>
          </div>  
        </div>
        <script>
          function switchContinue(checked){
            if(checked==true){
              document.getElementById('but-cont-hidden').style.display='none';
              document.getElementById('but-cont').style.display='block';
            }else{
              document.getElementById('but-cont').style.display='none';
              document.getElementById('but-cont-hidden').style.display='block';
            }
          }
        </script>
      <?
      $contents=ob_get_contents();
      ob_end_clean();
    break;
/******************************************************************************/
/************************  STEP 3: Init Voiceone  *****************************/
/******************************************************************************/
    case "3":
    	//check license agreement
      if(!isset($_SESSION['license_agreement'])){
        header("location: setup.php?step=2");
      	die;
      }
    	//check users
      $userArr=$asterisk->getUser("");
      if((is_array($userArr))&&(count($userArr)!=0)){
      	foreach($userArr as $u){
        	if($u["type"]==3){
           	$superuserArr[]=array("username"=>$u["username"],"password"=>$u["password"]); 
          }
        }
      }
    	//================ POST & GET ================
    	$continue=true;;
      if(count($_POST)>0){
      	if((is_array($superuserArr))&&(count($superuserArr)>0)){ 
        	$continue=false;
      		$user_name=$_POST["username"];
        	$user_pass=$_POST["password"];
          if((trim($user_name)!="")&&(trim($user_pass)!="")){
            foreach($superuserArr as $su){
            	if(($su["username"]==$user_name)&&($su["password"]==$user_pass)){
              	$continue=true;
                break;
              }
            }
          }
      	}
        if($continue===true){
          if(isset($_POST["keys"])) $keys=1; else $keys=0;
          $ret=$asterisk->initialize($keys);
          $_SESSION["step3_success"]=true;
          header("location: setup.php?step=4");
        	die;
       	}
      }
      //================ PAGE LOGIC ================
      $alert="";
      $error=false;
      //check interface folders permissions
      if((!((fileperms("admin/config/config.inc.php") & 0x0004)&&(fileperms("admin/config/config.inc.php") & 0x0002))) ||
      	 (!((fileperms("admin/public/template/cache") & 0x0001)&&(fileperms("admin/public/template/cache") & 0x0002)&&(fileperms("admin/public/template/cache") & 0x0004))) ||
      	 (!((fileperms("sandbox") & 0x0001)&&(fileperms("sandbox") & 0x0002)&&(fileperms("sandbox") & 0x0004)))											||
         (!((fileperms("sandbox/plugins") & 0x0001)&&(fileperms("sandbox/plugins") & 0x0002)&&(fileperms("sandbox/plugins") & 0x0004)))){
      	header("location: setup.php?step=1;");
      	die;
      }
      //check web services permissions
      $ret0=$asterisk->checkDatabaseAccess();
      $ret1=$asterisk->isVotoolsExecutable();
      if(($ret0==0)||($ret1!="1")){
      	header("location: setup.php?step=2");
      	die;
      }
			//button
      if($error===false) $button="<input type=\"image\" src=\"admin/public/img/en/b-next.gif\" alt=\"Next\" onclick=\"location.href='setup.php?step=3';\" />";
      else $button="<img src=\"admin/public/img/en/b-next_disabled.gif\" alt=\"Next\" />";
      //================== HTML ==================
      ob_start();
      ?>
			<div id="loginFirstTime">
        <h1><img src="admin/public/img/VoiceOne-logo.gif" /></h1>
        <h2><?=$title?></h2>
        <p><?=$subTitle?></p>
      </div>      
      <div id="Dialog">
        <h1 style="margin:0;margin-bottom:15px;"><?=$step3_title?></h1>
        <h3 style="margin:0;margin-bottom:10px;padding-left:30px;"><?=$step3_subtitle?></h3>
				<form action="<?echo getenv("REQUEST_URI");?>" method="post" onsubmit="addRoll();">
          <dl>
      		  <dd style="margin-bottom:0;">
            	<?=$keyGeneration?>
              <input name="keys" type="checkbox" value="1" checked="checked" />
              <span><?=$keyHelp?></span>
            </dd>
          </dl>
          <? if((is_array($superuserArr))&&(count($superuserArr)>0)){ ?>
            <p style="padding:5px 10px 10px 30px;"><?=$root_needed?></p>
            <? if($continue===false){ ?>
            <div style="float:right;width:170px;margin-top:30px;">
            	<img src="admin/public/img/shutdown.png" alt="error" />
            	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$login_incorect?></span>
            </div>
            <? } ?>
            <dl style="padding-left:80px;width:280px;">
              <dt style="width:100px;"><label style="font-weight:bold;"><?=$username?>:</label></dt>
              <dd><input type="text" class="password" size="20" name="username"/></dd>
              <dt style="width:100px;"><label style="font-weight:bold;"><?=$password?>:</label></dt>
              <dd><input type="password" class="password" size="20" name="password"/></dd>
            </dl>  
        	  <p style="padding:10px 10px 5px 30px;"><?=$root_delete?></p>
          <? } ?>
          <div id="roll" style="display:none;padding-top:15px;text-align:center;">
            <img style="vertical-align:bottom;" src="admin/public/img/setup_working.gif" alt="working" />
            <p style="margin:0;"><?=$roll_msg?></p>
          </div>
          <!-- Button -->
          <div style="width:96%;text-align:right;margin-top:10px;">
        		<input id="button_next" type="image" src="admin/public/img/en/b-next.gif" alt="Next" />
        	</div>
        </form>
      </div>
      <script>
      function addRoll(){
        document.getElementById('roll').style.display='block';
        document.getElementById('button_next').src='admin/public/img/en/b-next_disabled.gif';
        document.getElementById('button_next').disabled=true;
      }
      </script>
      <?
      $contents=ob_get_contents();
      ob_end_clean();
    break;
/******************************************************************************/
/***************************  STEP 4: System Passwords  *******************************/
/******************************************************************************/    
    case "4":
      if((!isset($_SESSION["step3_success"]))||($_SESSION["step3_success"]!==true)){
        header("location: setup.php?step=3");
      	die;
      }
    	//================ POST & GET ================
      function _spacesFree($string){
        return true;
      }
      if(count($_POST)>0){
        //voiceone doesn't controll the system: set password for api
        if($_POST['config']==0){
          $pass=$_POST["pass"];
          //check passwords
          if((is_array($pass["ws"]))&&($pass["ws"]["password"]!="")&&($pass["ws"]["password"]==$pass["ws"]["confirm"])&&(_spacesFree($pass["ws"]["password"]))){
            $entry=array(
                'voiceone_ws'   => $pass["ws"]["password"]
              );
            //write password server side
            $ret=$asterisk->setSystemPasswords($entry);
            if($ret>0){
              $interface_config_file="admin/config/config.inc.php";
              //wrtie password client side
              $changed=false;
              if(@is_writable($interface_config_file)){
                $fileArr=@file($interface_config_file);
                if(is_array($fileArr)){
                  $newfile=array();
                  foreach($fileArr as $line){
                    if((strstr($line,"\$soapPassword ="))||(strstr($line,"\$soapPassword="))){
                      $newfile[]="\$soapPassword = \"".$pass["ws"]["password"]."\";\n";
                      $changed=true;
                    }else{
                      $newfile[]=$line;
                    }
                  }
                }
              }
              $write_error=false;
              if($changed===true){
                if((!$file = @fopen($interface_config_file,"w")) ||
                   (!@fwrite($file,implode("",$newfile))) ||
                   (!@fclose($file)) ){
                   $write_error=true;
                }
              }
              if(!$write_error){
              	//go to next page
                unset($_SESSION["step3_success"]);
                $_SESSION["step4_success"]=true;
                header("location: setup.php?step=5");
          	    die;
              }
            }
          } 
        //voiceone controlls the system: set password for api-mysql-webservices-ssh
        }else if((is_array($_POST["pass"]))&&(count($_POST["pass"]))){
          $pass=$_POST["pass"];
          //check passwords
          if((is_array($pass["api"]))&&($pass["api"]["password"]!="")&&($pass["api"]["password"]==$pass["api"]["confirm"])&&(_spacesFree($pass["api"]["password"])) &&
             (is_array($pass["db"]))&&($pass["db"]["password"]!="")&&($pass["db"]["password"]==$pass["db"]["confirm"])&&(_spacesFree($pass["db"]["password"]))      &&
             (is_array($pass["ws"]))&&($pass["ws"]["password"]!="")&&($pass["ws"]["password"]==$pass["ws"]["confirm"])&&(_spacesFree($pass["ws"]["password"]))      &&
             (is_array($pass["ssh"]))&&($pass["ssh"]["password"]!="")&&($pass["ssh"]["password"]==$pass["ssh"]["confirm"])&&(_spacesFree($pass["ssh"]["password"])) ){
            $entry=array(
                'asterisk_api'	=> $pass["api"]["password"],
                'voiceone_db'   => $pass["db"]["password"],
                'voiceone_ws'   => $pass["ws"]["password"],
                'ssh_access'  	=> $pass["ssh"]["password"]
              );
            //write password server side
            $ret=$asterisk->setSystemPasswords($entry);
            if($ret>0){
              $interface_config_file="admin/config/config.inc.php";
              //wrtie password client side
              $changed=false;
              if(@is_writable($interface_config_file)){
                $fileArr=@file($interface_config_file);
                if(is_array($fileArr)){
                  $newfile=array();
                  foreach($fileArr as $line){
                    if((strstr($line,"\$soapPassword ="))||(strstr($line,"\$soapPassword="))){
                      $newfile[]="\$soapPassword = \"".$pass["ws"]["password"]."\";\n";
                      $changed=true;
                    }else{
                      $newfile[]=$line;
                    }
                  }
                }
              }
              $write_error=false;
              if($changed===true){
                if((!$file = @fopen($interface_config_file,"w")) ||
                   (!@fwrite($file,implode("",$newfile))) ||
                   (!@fclose($file)) ){
                   $write_error=true;
                }
              }
              if(!$write_error){
                //verify db accessibility
                $ret=$asterisk->checkDatabaseAccess();
                if($ret>0){
                	//go to next page
                  unset($_SESSION["step3_success"]);
                  $_SESSION["step4_success"]=true;
                  header("location: setup.php?step=5");
            	    die;
                }
              }
            }
          } 
        }
        $_SESSION["cp_error"]=true;
      }
      //================ PAGE LOGIC ================
      unset($_SESSION["password_changed"]);
      if((isset($_SESSION["cp_error"]))&&($_SESSION["cp_error"]===true)){
        $display_error="display:block;";
      }else{
        $display_error="display:none;";
      }
      unset($_SESSION["cp_error"]);
      //check system configuration
      $ret=$asterisk->getSystemConfig();
      if(is_array($ret)){
        foreach($ret as $p){
          $confArr[$p["key"]]=$p["value"];
        }
      }
      if((is_array($confArr))&&(isset($confArr["network_success"]))) $config=$confArr["network_success"]; else $config="0";
      ob_start();
      ?>
      <div id="loginFirstTime">
        <h1><img src="admin/public/img/VoiceOne-logo.gif" /></h1>
        <h2><?=$title?></h2>
        <p><?=$subTitle?></p>
      </div>   
      <div id="Dialog">
        <h1 style="margin:0;margin-bottom:15px;"><?=$step4_title?></h1>
        <form action="<?echo getenv("REQUEST_URI");?>" method="post">
          <input type="hidden" name="config" value="<?=$config?>"/>
          <h3 style="margin:0;margin-bottom:0px;padding-left:30px;"><?=$step4_subtitle1?></h3>
          <p style="margin: 3px 30px 0 30px;"><?=$step4_desc?></p>
          <table style="margin: 15px auto 5px auto;" cellspacing="0">
            <tr>
              <th style="padding:7px;background:#CBE1F0;font-size:11px;font-weight:bold;text-align:center;border-bottom:1px solid #999;border-right:1px solid #999;border-top:1px solid #999;border-left:1px solid #999;">Service</th>
              <th style="padding:7px;background:#CBE1F0;font-size:11px;font-weight:bold;text-align:center;border-bottom:1px solid #999;border-right:1px solid #999;border-top:1px solid #999;">Port</th>
              <th style="padding:7px;background:#CBE1F0;font-size:11px;font-weight:bold;text-align:center;border-bottom:1px solid #999;border-right:1px solid #999;border-top:1px solid #999;">Username</th>
              <th style="padding:7px;background:#CBE1F0;font-size:11px;font-weight:bold;text-align:center;border-bottom:1px solid #999;border-right:1px solid #999;border-top:1px solid #999;">Password</th>
              <th style="padding:7px;background:#CBE1F0;font-size:11px;font-weight:bold;text-align:center;border-bottom:1px solid #999;border-right:1px solid #999;border-top:1px solid #999;">Confirm</th>
            </tr>
            <? if($config!=0){ ?>
              <tr bgcolor="#ffffff">
                <td style="font-size:9px;border-right:1px solid #999;border-bottom:1px solid #999;border-left:1px solid #999;text-align:center;padding:0 5px"><?=$pass_api?></td>
                <td style="font-size:9px;border-right:1px solid #999;border-bottom:1px solid #999;text-align:center;">5042</td>
                <td style="font-size:10px;border-right:1px solid #999;border-bottom:1px solid #999;text-align:center;">voiceone</td>
                <td style="border-right:1px solid #999;border-bottom:1px solid #999;"><input class="password" style="border:1px solid #7F9DB9;" type="password" id="pass[api][password]" name="pass[api][password]" value=""/></td>
                <td style="border-right:1px solid #999;border-bottom:1px solid #999;"><input class="password" style="border:1px solid #7F9DB9;" type="password" id="pass[api][confirm]" name="pass[api][confirm]" value=""/></td>
              </tr>
            <? } ?>
            <? if($config!=0){ ?>
              <tr bgcolor="#ffffff">
                <td style="font-size:9px;border-right:1px solid #999;border-bottom:1px solid #999;border-left:1px solid #999;text-align:center;padding:0 5px"><?=$pass_db?></td>
                <td style="font-size:9px;border-right:1px solid #999;border-bottom:1px solid #999;text-align:center;">3306</td>
                <td style="font-size:10px;border-right:1px solid #999;border-bottom:1px solid #999;text-align:center;">voiceone</td>
                <td style="border-right:1px solid #999;border-bottom:1px solid #999;"><input class="password" style="border:1px solid #7F9DB9;" type="password" id="pass[db][password]" name="pass[db][password]" value=""/></td>
                <td style="border-right:1px solid #999;border-bottom:1px solid #999;"><input class="password" style="border:1px solid #7F9DB9;" type="password" id="pass[db][confirm]" name="pass[db][confirm]" value=""/></td>
              </tr>
            <? } ?>
            <tr bgcolor="#ffffff">
              <td style="font-size:9px;border-right:1px solid #999;border-bottom:1px solid #999;border-left:1px solid #999;text-align:center;padding:0 5px"><?=$pass_ws?></td>
              <td style="font-size:9px;border-right:1px solid #999;border-bottom:1px solid #999;text-align:center;">443</td>
              <td style="font-size:10px;border-right:1px solid #999;border-bottom:1px solid #999;text-align:center;">voiceone</td>
              <td style="border-right:1px solid #999;border-bottom:1px solid #999;"><input class="password" style="border:1px solid #7F9DB9;" type="password" id="pass[ws][password]" name="pass[ws][password]" value=""/></td>
              <td style="border-right:1px solid #999;border-bottom:1px solid #999;"><input class="password" style="border:1px solid #7F9DB9;" type="password" id="pass[ws][confirm]" name="pass[ws][confirm]" value=""/></td>
            </tr>
            <? if($config!=0){ ?>
              <tr bgcolor="#ffffff">
                <td style="font-size:9px;border-right:1px solid #999;border-bottom:1px solid #999;border-left:1px solid #999;text-align:center;padding:0 5px;"><?=$pass_ssh?></td>
                <td style="font-size:9px;border-right:1px solid #999;border-bottom:1px solid #999;text-align:center;">22</td>
                <td style="font-size:10px;border-right:1px solid #999;border-bottom:1px solid #999;text-align:center;">voiceone</td>
                <td style="border-right:1px solid #999;border-bottom:1px solid #999;"><input class="password" style="border:1px solid #7F9DB9;" type="password" id="pass[ssh][password]" name="pass[ssh][password]" value=""/></td>
                <td style="border-right:1px solid #999;border-bottom:1px solid #999;"><input class="password" style="border:1px solid #7F9DB9;" type="password" id="pass[ssh][confirm]" name="pass[ssh][confirm]" value=""/></td>
              </tr>
            <? } ?>
          </table>
          <div id="save_error" style="margin-left:30px;<?=$display_error?>">
            <img style="vertical-align:bottom;" src="admin/public/img/shutdown.png" alt="error" /> <p style="display:inline;font-size:10px;color:red;font-weight:bold;"><?=$save_error?><p>
          </div>
          <div id="password_error" style="margin-left:30px;display:none;">
            <img style="vertical-align:bottom;" src="admin/public/img/shutdown.png" alt="error" /> <p style="display:inline;font-size:10px;color:red;font-weight:bold;"><?=$pass_error?><p>
          </div>
        	<!-- Button -->
          <div style="width:96%;text-align:right;margin-top:15px;">
        		<input type="image" src="admin/public/img/en/b-next.gif" alt="Next" onclick="return checkPasswords();"/>
        	</div>
        </form>
      </div>     
      <script>
      function hasWhiteSpace(s){
        reWhiteSpace = new RegExp(/\s+/);
        if(reWhiteSpace.test(s)){
          return true;
        }
        else return false;
      }
      function checkPasswords(){
        var returnFalse=false;
        //check api
        <? if($config!=0){ ?>
          password=document.getElementById('pass[api][password]').value;
          pconfirm=document.getElementById('pass[api][confirm]').value;
            if((password=='')||(pconfirm=='')||(password!=pconfirm)||(password.length<8)||(hasWhiteSpace(password))||(hasWhiteSpace(pconfirm))){
            returnFalse=true;
            document.getElementById('pass[api][password]').style.border='2px solid red';
            document.getElementById('pass[api][confirm]').style.border='2px solid red';
          }else{
            document.getElementById('pass[api][password]').style.border='1px solid #7F9DB9';
            document.getElementById('pass[api][confirm]').style.border='1px solid #7F9DB9';
          }
        <? } ?>
        //check db
        <? if($config!=0){ ?>
          password=document.getElementById('pass[db][password]').value;
          pconfirm=document.getElementById('pass[db][confirm]').value;
          if((password=='')||(pconfirm=='')||(password!=pconfirm)||(password.length<8)||(hasWhiteSpace(password))||(hasWhiteSpace(pconfirm))){
            returnFalse=true;
            document.getElementById('pass[db][password]').style.border='2px solid red';
            document.getElementById('pass[db][confirm]').style.border='2px solid red';
          }else{
            document.getElementById('pass[db][password]').style.border='1px solid #7F9DB9';
            document.getElementById('pass[db][confirm]').style.border='1px solid #7F9DB9';
          }
        <? } ?>
        //check ws
        password=document.getElementById('pass[ws][password]').value;
        pconfirm=document.getElementById('pass[ws][confirm]').value;
          if((password=='')||(pconfirm=='')||(password!=pconfirm)||(password.length<8)||(hasWhiteSpace(password))||(hasWhiteSpace(pconfirm))){
          returnFalse=true;
          document.getElementById('pass[ws][password]').style.border='2px solid red';
          document.getElementById('pass[ws][confirm]').style.border='2px solid red';
        }else{
          document.getElementById('pass[ws][password]').style.border='1px solid #7F9DB9';
          document.getElementById('pass[ws][confirm]').style.border='1px solid #7F9DB9';
        }
        //check ssh
        <? if($config!=0){ ?>
          password=document.getElementById('pass[ssh][password]').value;
          pconfirm=document.getElementById('pass[ssh][confirm]').value;
            if((password=='')||(pconfirm=='')||(password!=pconfirm)||(password.length<8)||(hasWhiteSpace(password))||(hasWhiteSpace(pconfirm))){
            returnFalse=true;
            document.getElementById('pass[ssh][password]').style.border='2px solid red';
            document.getElementById('pass[ssh][confirm]').style.border='2px solid red';
          }else{
            document.getElementById('pass[ssh][password]').style.border='1px solid #7F9DB9';
            document.getElementById('pass[ssh][confirm]').style.border='1px solid #7F9DB9';
          }
        <? } ?>
				//return
        if(returnFalse==true){
          document.getElementById('password_error').style.display='block';
          document.getElementById('save_error').style.display='none';
          return false;
        }else{
          document.getElementById('password_error').style.display='none';
          document.getElementById('save_error').style.display='none';
          return true;
        }
      }
      </script>
      <?
      $contents=ob_get_contents();
      ob_end_clean();
    break;  
/******************************************************************************/
/***************************  STEP 5: Log root  *******************************/
/******************************************************************************/    
    case "5":
      //check users
      $userArr=$asterisk->getUser("");
      if((is_array($userArr))&&(count($userArr)!=0)){
      	foreach($userArr as $u){
        	if($u["type"]==3){
           	$superuserArr[]=array("username"=>$u["username"],"password"=>$u["password"]); 
          }
        }
      }
     	if((is_array($superuserArr))&&(count($superuserArr)>0)){
      	header("location: setup.php?step=3");
      	die;
      }
      if((!isset($_SESSION["step4_success"]))||($_SESSION["step4_success"]!==true)){
        header("location: setup.php?step=4");
      	die;
      }
    	//================ POST & GET ================
      if(count($_POST)>0) {
        $username=$_POST["username"];
        $password=$_POST["password"];
				$lang=$_POST["lang"];
        $entry=Array(
        		"entity_id" => "0",
        		"username" => "$username", 
            "password" => "$password", 
            "lang" => "$lang",
            "type" => USER_SUPERUSER
        	);
        $ret=$asterisk->putUser($entry);
        if($ret>0){
          unset($_SESSION["step4_success"]);
          $_SESSION["step5_success"]=true;
          header("location: setup.php?step=6");
          die();
        }
      }
      //================ PAGE LOGIC ================
			//create the language list
      $availLang=getAvailLang();
      if(is_array($availLang)){
        foreach($availLang as $l) {
          if($l['key']==$lang) $s="selected=\"selected\""; else $s="";
          $lang.="<option value=\"".$l['key']."\" $s>".$l['value']."</option>\n";
        }
      }
      ob_start();
      ?>
      <div id="loginFirstTime">
        <h1><img src="admin/public/img/VoiceOne-logo.gif" /></h1>
        <h2><?=$title?></h2>
        <p><?=$subTitle?></p>
      </div>   
      <div id="Dialog">
        <h1 style="margin:0;margin-bottom:15px;"><?=$step5_title?></h1>
        <form action="<?echo getenv("REQUEST_URI");?>" method="post">
          <h3 style="margin:0;margin-top:20px;padding-left:30px;"><?=$step5_subtitle?></h3>
          <p style="margin: 3px 30px 0 30px;"><?=$step5_desc?></p>
        	<dl style="padding-left:100px;">
        		<dt style="width:100px;"><label style="font-weight:bold;"><?=$username?>:</label></dt>
        		<dd>
            	<input name="username" size="20" type="text" id="username" value=""/>
              <div id="user_check" style="display:none;">
              	<img src="admin/public/img/shutdown.png" alt="error" />
              	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$usrerror?></span>
              </div>
            </dd>
        		<dt style="width:100px;"><label style="font-weight:bold;"><?=$password?>:</label></dt>
        		<dd>
            	<input name="password" size="20" type="password" id="password" value=""/>
              <div id="pass_check" style="display:none;">
              	<img src="admin/public/img/shutdown.png" alt="error" />
              	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$pwdempty?></span>
              </div>
            </dd>          
            <dt style="width:100px;"><label style="font-weight:bold;"><?=$confirm?>:</label></dt>
        		<dd>
            	<input name="confirm" size="20" type="password" id="confirm" value=""/> 
              <div id="confirm_check" style="display:none;">
              	<img src="admin/public/img/shutdown.png" alt="error" />	
                <span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$pwderror?></span>
            	</div>
            </dd>
  					<dt style="width:100px;"><label style="font-weight:bold;"><?=$language?>:</label></dt>
        		<dd><select name="lang" id="lang"><?=$lang?></select></dd>
        	</dl>
        	<!-- Button -->
          <div style="width:96%;text-align:right;margin-top:20px;">
        		<input type="image" src="admin/public/img/en/b-next.gif" alt="Save" onclick=" return checkForm();"/>
        	</div>
        </form>
      </div>     
      <script>
      function checkForm(){
        var returnFalse=false;
        //check Username
        if(document.getElementById('username').value==''){
          document.getElementById('user_check').style.display='inline';
          returnFalse=true;
        }else{
        	document.getElementById('user_check').style.display='none';
        }
        //check Password
        if(document.getElementById('password').value==''){
          document.getElementById('pass_check').style.display='inline';
          document.getElementById('confirm_check').style.display='none';
          returnFalse=true;
        }else{
        	document.getElementById('pass_check').style.display='none';
          if(document.getElementById('password').value!=document.getElementById('confirm').value) { 
            document.getElementById('confirm_check').style.display='inline';
            returnFalse=true;
          }else{
          	document.getElementById('confirm_check').style.display='none';
          } 
        }
				//return
        if(returnFalse==true) return false;
        else return true;
      }
      </script>
      <?
      $contents=ob_get_contents();
      ob_end_clean();
    break;
/******************************************************************************/
/*********************  STEP 6: Network configuration  ************************/
/******************************************************************************/    
    case "6":
      //check users
      $userArr=$asterisk->getUser("");
      if((is_array($userArr))&&(count($userArr)!=0)){
      	foreach($userArr as $u){
        	if($u["type"]==3){
           	$superuserArr[]=array("username"=>$u["username"],"password"=>$u["password"]); 
          }
        }
      }
     	if((!is_array($superuserArr))||(count($superuserArr)==0)){
      	header("location: setup.php?step=4");
      	die;
      }
      if((!isset($_SESSION["step5_success"]))||($_SESSION["step5_success"]!==true)){
        header("location: setup.php?step=4");
      	die;
      }
    	//================ POST & GET ================
      if(count($_POST)>0){
        //config appliance network
        if($_POST["config"]=="1"){
          if(is_array($_POST["net"])){
            if(trim($_POST["net"]["dhcp_status"])==1){
            	$_POST["net"]["vpn_range_from"]=$_POST["net"]["dhcp_range_from"];
              $_POST["net"]["vpn_range_to"]=$_POST["net"]["dhcp_range_to"];
            }
            $assArr=array();
            foreach($_POST["net"] as $k=>$v){
              $assArr[]=array("key"=>trim($k),"value"=>trim($v));
            }
            $ret=$asterisk->putSystemConfig($assArr);
          }
          //error: net configuration missing
          else{
            $ret=0;
          }
        }
        //skip network config and setup only system label
        else{ 
          $ret=1;
        }
        //end setup procedure
        if($ret>0){
          //config only system name
          $ret=$asterisk->setSystemName($_POST["system_label"]);
          unset($_SESSION["step5_success"]);
          unset($_SESSION["hostname"]);
          unset($_SESSION["voiceone_version"]);
          unset($_SESSION["voiceone_release"]);
          unset($_SESSION["cwSetup"]);
          header("location: ./");
        }
      }
      //================ PAGE LOGIC ================
      //get system info
      $systemOptionArr=$asterisk->getOptionByName("hostname","SYSTEM");
      if((is_array($systemOptionArr))&&(is_array($systemOptionArr[0]))){
        $sys_label=$systemOptionArr[0]["value"];
      }
      //get configuration
      $confArr=array();
			$ret=$asterisk->getSystemConfig();
      if(is_array($ret)){
        foreach($ret as $p){
          $confArr[$p["key"]]=$p["value"];
        }
      }
      if((is_array($confArr))&&(isset($confArr["network_success"]))) $config=$confArr["network_success"]; else $config="0";
      //create timezone list
      $tz_html="";
      if((is_array($confArr))&&(isset($confArr["ntp_zone"]))&&(trim($confArr["ntp_zone"])!="")){
        $ntp_zone_now=$confArr["ntp_zone"];
      }else{
        $ntp_zone_now="Europe/Rome";
      }
      $timezoneArray=getTimeZones();
      if(is_array($timezoneArray)){
        foreach($timezoneArray as $zone=>$zoneArr){
          $tz_html.="<optgroup label=\"".$zone."\">\n";
          if(is_array($zoneArr)){
            foreach($zoneArr as $z){
              if($ntp_zone_now==$zone."/".$z) $s="selected=\"selected\""; else $s="";
              $tz_html.="<option value=\"".$zone."/".$z."\" $s>".substr(str_replace("_"," ",$z),0,15)."</option>\n";
            }
          }
          $tz_html.="</optgroup>\n"; 
        }
      }
      //create protocol list
      $protocolOpt="";
      if((is_array($confArr))&&(isset($confArr["smtp_protocol"]))&&(trim($confArr["smtp_protocol"])!="")){
        $protocol=$confArr["smtp_protocol"];
        $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";
          }
        }
      }
      //create authentication methods list
      $mathodOpt="";
      if((is_array($confArr))&&(isset($confArr["smtp_auth"]))){
        if(trim($confArr["smtp_auth"])!="") $auth=$confArr["smtp_auth"]; else $auth="off";
        if($auth=="off"){
          $disableAuth="disabled=\"disabled\"";
          $confArr["smtp_user"]="";
          $confArr["smtp_password"]=""; 
        }else $disableAuth="";
        $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";
          }
        }
      }
      //check Hostname
      if((!isset($confArr['host']))||(trim($confArr['host'])=="")) $confArr['host']="voiceone";
      if((!isset($confArr['domain']))||(trim($confArr['domain'])=="")) $confArr['domain']="localdomain";
      ob_start();
      ?>
      <div id="loginFirstTime">
        <h1><img src="admin/public/img/VoiceOne-logo.gif" /></h1>
        <h2><?=$title?></h2>
        <p><?=$subTitle?></p>
      </div>   
      <div id="Dialog">
        <h1 style="margin:0;margin-bottom:15px;"><?=$step6_title?></h1>
        <form action="<?echo getenv("REQUEST_URI");?>" method="post">
          <input type="hidden" name="config" value="<?=$config?>"/>
          <p style="margin: 0 0 10px 29px;"><?=$step6_desc?></p>
          <h3 style="margin:0;padding-left:30px;"><?=$step6_subtitle1?></h3>
            <dl style="padding-left:70px;margin-top:0;">
          		<dt style="width:130px;font-size:12px;clear:left;"><label style="font-weight:bold;"><?=$system_label?>:</label></dt>
          		<dd style="margin-bottom:0;">
              	<input style="padding:1px;" id="system_label" name="system_label" size="20" type="text" value="<?=$sys_label?>"/>
                <div id="label_check" style="display:none;">
                	<img src="admin/public/img/shutdown.png" alt="error" />
                	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$conf_generic_error?></span>
                </div>
              </dd>
            </dl>
          <? if($config!=0){ ?>
            <!-- System -->
            <dl style="padding-left:70px;margin-top:0;">
          		<dt style="width:130px;font-size:12px;clear:left;"><label style="font-weight:bold;"><?=$host_name?>:</label></dt>
          		<dd style="margin-bottom:0;">
              	<input style="padding:1px;" id="net[host]" name="net[host]" size="20" type="text" value="<?=$confArr['host']?>"/>
                <div id="host_check" style="display:none;">
                	<img src="admin/public/img/shutdown.png" alt="error" />
                	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$conf_generic_error?></span>
                </div>
              </dd>
              <dt style="width:130px;font-size:12px;clear:left;"><label style="font-weight:bold;"><?=$domain_name?>:</label></dt>
          		<dd style="margin-bottom:0;">
              	<input style="padding:1px;" id="net[domain]" name="net[domain]" size="20" type="text" value="<?=$confArr['domain']?>"/>
                <div id="domain_check" style="display:none;">
                	<img src="admin/public/img/shutdown.png" alt="error" />
                	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$conf_generic_error?></span>
                </div>
              </dd>
            </dl>
            <!-- Network -->
            <h3 style="margin:10px 0 0 0;padding-left:30px;"><?=$step6_subtitle2?></h3>
            <dl style="padding-left:70px;margin-top:0;">
          		<dt style="width:130px;font-size:12px;clear:left;"><label style="font-weight:bold;"><?=$address?>:</label></dt>
          		<dd style="margin-bottom:0;">
              	<input style="padding:1px;" id="net[address]" name="net[address]" size="20" type="text" value="<?=$confArr['address']?>"/>
                <div id="address_check" style="display:none;">
                	<img src="admin/public/img/shutdown.png" alt="error" />
                	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$conf_generic_error?></span>
                </div>
              </dd>
              <dt style="width:130px;font-size:12px;clear:left;"><label style="font-weight:bold;"><?=$netmask?>:</label></dt>
          		<dd style="margin-bottom:0;">
              	<input style="padding:1px;" id="net[netmask]" name="net[netmask]" size="20" type="text" value="<?=$confArr['netmask']?>"/>
                <div id="netmask_check" style="display:none;">
                	<img src="admin/public/img/shutdown.png" alt="error" />
                	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$conf_generic_error?></span>
                </div>
              </dd>
              <dt style="width:130px;font-size:12px;clear:left;"><label style="font-weight:bold;"><?=$network?>:</label></dt>
          		<dd style="margin-bottom:0;">
              	<input style="padding:1px;" id="net[network]" name="net[network]" size="20" type="text" value="<?=$confArr['network']?>"/>
                <div id="network_check" style="display:none;">
                	<img src="admin/public/img/shutdown.png" alt="error" />
                	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$conf_generic_error?></span>
                </div>
              </dd>
              <dt style="width:130px;font-size:12px;clear:left;"><label style="font-weight:bold;"><?=$broadcast?>:</label></dt>
          		<dd style="margin-bottom:0;">
              	<input style="padding:1px;" id="net[broadcast]" name="net[broadcast]" size="20" type="text" value="<?=$confArr['broadcast']?>"/>
                <div id="broadcast_check" style="display:none;">
                	<img src="admin/public/img/shutdown.png" alt="error" />
                	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$conf_generic_error?></span>
                </div>
              </dd>
              <dt style="width:130px;font-size:12px;clear:left;"><label style="font-weight:bold;"><?=$gateway?>:</label></dt>
          		<dd style="margin-bottom:0;">
              	<input style="padding:1px;" id="net[gateway]" name="net[gateway]" size="20" type="text" value="<?=$confArr['gateway']?>"/>
                <div id="gateway_check" style="display:none;">
                	<img src="admin/public/img/shutdown.png" alt="error" />
                	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$conf_generic_error?></span>
                </div>
              </dd>
              <dt style="width:130px;font-size:12px;clear:left;"><label style="font-weight:bold;"><?=$dns1?>:</label></dt>
          		<dd style="margin-bottom:0;">
              	<input style="padding:1px;" id="net[dns1]" name="net[dns1]" size="20" type="text" value="<?=$confArr['dns1']?>"/>
                <div id="dns1_check" style="display:none;">
                	<img src="admin/public/img/shutdown.png" alt="error" />
                	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$conf_generic_error?></span>
                </div>
              </dd>
              <dt style="width:130px;font-size:12px;clear:left;"><label style="font-weight:bold;"><?=$dns2?>:</label></dt>
          		<dd style="margin-bottom:0;">
              	<input style="padding:1px;" id="net[dns2]" name="net[dns2]" size="20" type="text" value="<?=$confArr['dns2']?>"/>
                <div id="dns2_check" style="display:none;">
                	<img src="admin/public/img/shutdown.png" alt="error" />
                	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$conf_generic_error?></span>
                </div>
              </dd>
            </dl>
            <!-- DHCP -->
            <h3 style="margin:10px 0 0 0;padding-left:30px;"><?=$step6_subtitle3?></h3>
            <dl style="padding-left:70px;margin-top:0;">
          		<dt style="width:130px;font-size:12px;clear:left;"><label style="font-weight:bold;"><?=$dhcp_status?>:</label></dt>
          		<dd style="margin-bottom:0;">
              	<input type="radio" id="net[dhcp_status]" name="net[dhcp_status]" value="1" onclick="disableVPNRange();" <?if ($dhcp_status==1) echo "checked=\"checked\""; ?> /> ON
                <input type="radio" id="net[dhcp_status]" name="net[dhcp_status]" value="0" onclick="enableVPNRange();" <?if ($dhcp_status!=1) echo "checked=\"checked\""; ?> /> OFF
                <div id="dhcp_status_check" style="display:none;">
                	<img src="admin/public/img/shutdown.png" alt="error" />
                	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$conf_generic_error?></span>
                </div>
              </dd>
              <dt style="width:130px;font-size:12px;clear:left;"><label style="font-weight:bold;"><?=$range_from?>:</label></dt>
          		<dd style="margin-bottom:0;">
              	<input style="padding:1px;" id="net[dhcp_range_from]" name="net[dhcp_range_from]" size="20" type="text" value="<?=$confArr['dhcp_range_from']?>"/>
                <div id="dhcp_range_from_check" style="display:none;">
                	<img src="admin/public/img/shutdown.png" alt="error" />
                	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$conf_generic_error?></span>
                </div>
              </dd>
              <dt style="width:130px;font-size:12px;clear:left;"><label style="font-weight:bold;"><?=$range_to?>:</label></dt>
          		<dd style="margin-bottom:0;">
              	<input style="padding:1px;" id="net[dhcp_range_to]" name="net[dhcp_range_to]" size="20" type="text" value="<?=$confArr['dhcp_range_to']?>"/>
                <div id="dhcp_range_to_check" style="display:none;">
                	<img src="admin/public/img/shutdown.png" alt="error" />
                	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$conf_generic_error?></span>
                </div>
              </dd>
            </dl>
            <!-- VPN -->
            <h3 style="margin:10px 0 0 0;padding-left:30px;"><?=$step6_subtitle4?></h3>
            <dl style="padding-left:70px;margin-top:0;">
              <dt style="width:130px;font-size:12px;clear:left;"><label style="font-weight:bold;"><?=$range_from?>:</label></dt>
          		<dd style="margin-bottom:0;">
              	<input style="padding:1px;" id="net[vpn_range_from]" name="net[vpn_range_from]" size="20" type="text" value="<?=$confArr['vpn_range_from']?>"/>
                <div id="vpn_range_from_check" style="display:none;">
                	<img src="admin/public/img/shutdown.png" alt="error" />
                	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$conf_generic_error?></span>
                </div>
              </dd>
              <dt style="width:130px;font-size:12px;clear:left;"><label style="font-weight:bold;"><?=$range_to?>:</label></dt>
          		<dd style="margin-bottom:0;">
              	<input style="padding:1px;" id="net[vpn_range_to]" name="net[vpn_range_to]" size="20" type="text" value="<?=$confArr['vpn_range_to']?>"/>
                <div id="vpn_range_to_check" style="display:none;">
                	<img src="admin/public/img/shutdown.png" alt="error" />
                	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$conf_generic_error?></span>
                </div>
              </dd>
            </dl>
            <!-- NTP -->
            <h3 style="margin:10px 0 0 0;padding-left:30px;"><?=$step6_subtitle5?></h3>
            <dl style="padding-left:70px;margin-top:0;">
              <dt style="width:130px;font-size:12px;clear:left;"><label style="font-weight:bold;"><?=$ntp_host?>:</label></dt>
          		<dd style="margin-bottom:0;">
              	<input style="padding:1px;" id="net[ntp_host]" name="net[ntp_host]" size="20" type="text" value="<?=$confArr['ntp_host']?>"/>
                <div id="ntp_host_check" style="display:none;">
                	<img src="admin/public/img/shutdown.png" alt="error" />
                	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$conf_generic_error?></span>
                </div>
              </dd>
              <dt style="width:130px;font-size:12px;clear:left;"><label style="font-weight:bold;"><?=$ntp_zone?>:</label></dt>
          		<dd style="margin-bottom:0;">
              	<select id="net[ntp_zone]" name="net[ntp_zone]"><?=$tz_html?></select>
                <div id="ntp_zone_check" style="display:none;">
                	<img src="admin/public/img/shutdown.png" alt="error" />
                	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$conf_generic_error?></span>
                </div>
              </dd>
              <dt style="width:130px;font-size:12px;clear:left;"><label style="font-weight:bold;"><?=$ntp_time?>:</label></dt>
          		<dd style="margin-bottom:0;">
              	<input style="padding:1px;" id="net[ntp_time]" name="net[ntp_time]" size="20" type="text" value="<?=$confArr['ntp_time']?>"/>
                <div id="ntp_time_check" style="display:none;">
                	<img src="admin/public/img/shutdown.png" alt="error" />
                	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$conf_generic_error?></span>
                </div>
              </dd>
            </dl>
            <!-- SMTP -->
            <h3 style="margin:10px 0 0 0;padding-left:30px;"><?=$step6_subtitle6?></h3>
            <dl style="padding-left:70px;margin-top:0;">
              <dt style="width:130px;font-size:12px;clear:left;"><label style="font-weight:bold;"><?=$smtp_host?>:</label></dt>
          		<dd style="margin-bottom:0;">
              	<input style="padding:1px;" id="net[smtp_host]" name="net[smtp_host]" size="20" type="text" value="<?=$confArr['smtp_host']?>"/>
                <div id="smtp_host_check" style="display:none;">
                	<img src="admin/public/img/shutdown.png" alt="error" />
                	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$conf_generic_error?></span>
                </div>
              </dd>
              <dt style="width:130px;font-size:12px;clear:left;"><label style="font-weight:bold;"><?=$smtp_protocol?>:</label></dt>
          		<dd style="margin-bottom:0;">
              	<select id="net[smtp_protocol]" name="net[smtp_protocol]"><?=$protocolOpt?></select>
                <div id="smtp_protocol_check" style="display:none;">
                	<img src="admin/public/img/shutdown.png" alt="error" />
                	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$conf_generic_error?></span>
                </div>
              </dd>
              <dt style="width:130px;font-size:12px;clear:left;"><label style="font-weight:bold;"><?=$smtp_port?>:</label></dt>
          		<dd style="margin-bottom:0;">
              	<input style="padding:1px;" id="net[smtp_port]" name="net[smtp_port]" size="20" type="text" value="<?=$confArr['smtp_port']?>"/>
                <div id="smtp_port_check" style="display:none;">
                	<img src="admin/public/img/shutdown.png" alt="error" />
                	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$conf_generic_error?></span>
                </div>
              </dd>
              <dt style="width:130px;font-size:12px;clear:left;"><label style="font-weight:bold;"><?=$smtp_from?>:</label></dt>
          		<dd style="margin-bottom:0;">
              	<input style="padding:1px;" id="net[smtp_from]" name="net[smtp_from]" size="20" type="text" value="<?=$confArr['smtp_from']?>"/>
                <div id="smtp_from_check" style="display:none;">
                	<img src="admin/public/img/shutdown.png" alt="error" />
                	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$conf_generic_error?></span>
                </div>
              </dd>
              <dt style="width:130px;font-size:12px;clear:left;"><label style="font-weight:bold;"><?=$smtp_auth?>:</label></dt>
          		<dd style="margin-bottom:0;">
              	<select id="net[smtp_auth]" name="net[smtp_auth]" onChange="if(this.selectedIndex==0) disableUserPass(); else enableUserPass();"><?=$mathodOpt?></select>
                <div id="sysname_check" style="display:none;">
                	<img src="admin/public/img/shutdown.png" alt="error" />
                	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$conf_generic_error?></span>
                </div>
              </dd>
              <dt style="width:130px;font-size:12px;clear:left;"><label style="font-weight:bold;"><?=$smtp_user?>:</label></dt>
          		<dd style="margin-bottom:0;">
              	<input style="padding:1px;" id="net[smtp_user]" name="net[smtp_user]" size="20" type="text" value="<?=$confArr['smtp_user']?>" <?=$disableAuth?>/>
                <div id="sysname_check" style="display:none;">
                	<img src="admin/public/img/shutdown.png" alt="error" />
                	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$conf_generic_error?></span>
                </div>
              </dd>
              <dt style="width:130px;font-size:12px;clear:left;"><label style="font-weight:bold;"><?=$smtp_password?>:</label></dt>
          		<dd style="margin-bottom:0;">
              	<input style="padding:1px;" id="net[smtp_password]" name="net[smtp_password]" size="20" type="text" value="<?=$confArr['smtp_password']?>" <?=$disableAuth?>/>
                <div id="sysname_check" style="display:none;">
                	<img src="admin/public/img/shutdown.png" alt="error" />
                	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$conf_generic_error?></span>
                </div>
              </dd>
            </dl>
          <? } ?>
        	<!-- Button -->
          <div style="width:96%;text-align:right;margin-top:20px;">
        		<input type="image" src="admin/public/img/en/b-end.gif" alt="Save" onclick=" return checkForm();"/>
        	</div>
        </form>
      </div>     
      <script>
      function isHostnameNotValid(hostname){
        if(hostname.length<1) return true;
        for(var j=0;j<hostname.length;j++){
          ch=hostname.charCodeAt(j);
          if(!(((ch>96)&&(ch<123))||((ch>64)&&(ch<91))||((ch>47)&&(ch<58))||(ch==46)||(ch==45))) return true;
        }
        return false;
      }
      function isIpNotValid(ip){
      	if(ip.indexOf(' ')>-1) return true;
        if(ip.indexOf('.')==-1) return true;
        var stringArr=ip.split('.');
        if(stringArr.length!=4) return true;
        var num;
        for(var i=0;i<stringArr.length;i++){
          for(var j=0;j<stringArr[i].length;j++){
            ch=stringArr[i].charCodeAt(j);
            if(!((ch>47)&&(ch<58))) return true;
          }
          num=parseInt(stringArr[i]);
        	if((num<0)||(num>255)) return true;
        }
        return false;
      }
      function isNotNumeric(num){
        if(num.length<1) return true;
        for(var j=0;j<num.length;j++){
          ch=num.charCodeAt(j);
          if(!((ch>47)&&(ch<58))) return true;
        }
        return false;
      }
      function checkForm(){
        var returnFalse=false;
        //check host
        if(isHostnameNotValid(document.getElementById('net[host]').value)){
          document.getElementById('host_check').style.display='inline';
          returnFalse=true;
        }else	document.getElementById('host_check').style.display='none';
        //check domain
        if(isHostnameNotValid(document.getElementById('net[domain]').value)){
          document.getElementById('domain_check').style.display='inline';
          returnFalse=true;
        }else document.getElementById('domain_check').style.display='none';
        //check address
        if(isIpNotValid(document.getElementById('net[address]').value)){
          document.getElementById('address_check').style.display='inline';
          returnFalse=true;
        }else document.getElementById('address_check').style.display='none';
        //check netmask
        if(isIpNotValid(document.getElementById('net[netmask]').value)){
          document.getElementById('netmask_check').style.display='inline';
          returnFalse=true;
        }else document.getElementById('netmask_check').style.display='none';
        //check network
        if(isIpNotValid(document.getElementById('net[network]').value)){
          document.getElementById('network_check').style.display='inline';
          returnFalse=true;
        }else document.getElementById('network_check').style.display='none';
        //check broadcast
        if(isIpNotValid(document.getElementById('net[broadcast]').value)){
          document.getElementById('broadcast_check').style.display='inline';
          returnFalse=true;
        }else document.getElementById('broadcast_check').style.display='none';
        //check gateway
        if(isIpNotValid(document.getElementById('net[gateway]').value)){
          document.getElementById('gateway_check').style.display='inline';
          returnFalse=true;
        }else document.getElementById('gateway_check').style.display='none';
        //check dns1
        if(isIpNotValid(document.getElementById('net[dns1]').value)){
          document.getElementById('dns1_check').style.display='inline';
          returnFalse=true;
        }else document.getElementById('dns1_check').style.display='none';
        //check dns2
        if((document.getElementById('net[dns2]').value!='')&&(isIpNotValid(document.getElementById('net[dns2]').value))){
          document.getElementById('dns2_check').style.display='inline';
          returnFalse=true;
        }else document.getElementById('dns2_check').style.display='none';
        //check dhcp_range_from
        if(isIpNotValid(document.getElementById('net[dhcp_range_from]').value)){
          document.getElementById('dhcp_range_from_check').style.display='inline';
          returnFalse=true;
        }else document.getElementById('dhcp_range_from_check').style.display='none';
        //check dhcp_range_to
        if(isIpNotValid(document.getElementById('net[dhcp_range_to]').value)){
          document.getElementById('dhcp_range_to_check').style.display='inline';
          returnFalse=true;
        }else document.getElementById('dhcp_range_to_check').style.display='none';
        //check vpn_range_from
        if((isIpNotValid(document.getElementById('net[vpn_range_from]').value))&&
           (document.getElementById('net[vpn_range_from]').value!='<?=$range_start_label?>')){
          document.getElementById('vpn_range_from_check').style.display='inline';
          returnFalse=true;
        }else document.getElementById('vpn_range_from_check').style.display='none';
        //check vpn_range_to
        if((isIpNotValid(document.getElementById('net[vpn_range_to]').value))&&
           (document.getElementById('net[vpn_range_to]').value!='<?=$range_end_label?>')){
          document.getElementById('vpn_range_to_check').style.display='inline';
          returnFalse=true;
        }else document.getElementById('vpn_range_to_check').style.display='none';
        //check ntp_host
        if(isHostnameNotValid(document.getElementById('net[ntp_host]').value)){
          document.getElementById('ntp_host_check').style.display='inline';
          returnFalse=true;
        }else document.getElementById('ntp_host_check').style.display='none';
        //check smtp_host
        if(isHostnameNotValid(document.getElementById('net[smtp_host]').value)){
          document.getElementById('smtp_host_check').style.display='inline';
          returnFalse=true;
        }else document.getElementById('smtp_host_check').style.display='none';
        //check smtp_from
        if(document.getElementById('net[smtp_from]').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '')==''){
          document.getElementById('smtp_from_check').style.display='inline';
          returnFalse=true;
        }else document.getElementById('smtp_from_check').style.display='none';
        //check smtp_port
        if(isNotNumeric(document.getElementById('net[smtp_port]').value)){
          document.getElementById('smtp_port_check').style.display='inline';
          returnFalse=true;
        }else document.getElementById('smtp_port_check').style.display='none';
				//return
        if(returnFalse==true) return false;
        else return true;
      }
      function disableVPNRange(){
      	document.getElementById("net[vpn_range_from]").value="<?=$range_start_label?>";
        document.getElementById("net[vpn_range_to]").value="<?=$range_end_label?>";
        document.getElementById("net[vpn_range_from]").disabled="disabled";
        document.getElementById("net[vpn_range_to]").disabled="disabled";
      }
      function enableVPNRange(){
      	document.getElementById("net[vpn_range_from]").value="<?=$confArr['vpn_range_from']?>";
        document.getElementById("net[vpn_range_to]").value="<?=$confArr['vpn_range_to']?>";
        document.getElementById("net[vpn_range_from]").disabled="";
        document.getElementById("net[vpn_range_to]").disabled="";
      }
    	function disableUserPass(){
        document.getElementById("net[smtp_user]").disabled="disabled";
        document.getElementById("net[smtp_password]").disabled="disabled";
      }
      function enableUserPass(){
        document.getElementById("net[smtp_user]").disabled="";
        document.getElementById("net[smtp_password]").disabled="";
      }
      </script>
      <?
      $contents=ob_get_contents();
      ob_end_clean();
    break;
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html;charset=ISO-8859-1" />
    <title>VoiceOne Setup</title>
    <link rel="shortcut icon" href="admin/public/img/favicon.ico" />
    <link rel="Stylesheet" href="admin/public/css/index.css" type="text/css" media="screen" />
    <script src="admin/public/js/functions.js"></script>
</head>

<body class="login">
  <div class="logincontainer" style="width:600px;">
    <?=$contents?>
    <div id="Footer">
      <a href="http://www.voiceone.it" target="_blank"><img align="right"  src="admin/public/img/callware.gif" alt="Powered by Callware" border="0" align="absmiddle" hspace="20"/></a>
      <a href="http://www.voiceone.it/support/" target="_blank">Get support</a> | 
      <a href="http://www.voiceone.it/licence/" target="_blank"><?=$loginTerms?></a>
    </div>
  </div>
</body>
</html>