<?
/**
 * VoiceOne Setup
 *
 * Setup of voiceone
 * @package voiceone
 * @subpackage  setup
 */
session_start();

//check the smarty cache folder permissions
$alert="";
$dir="admin/public/template/cache";
$perms = fileperms($dir);
if (!(($perms & 0x0002) )) $alert="<p>Before proceed into the setup wizard, enable the <strong>write</strong> permission to <strong>".realpath($dir)."</strong></p>";
$dir="sandbox/";
$perms = fileperms($dir);
if (!(($perms & 0x0002) )) $alert="<p>Before proceed into the setup wizard, enable the <strong>write</strong> permission to <strong>".realpath($dir)."</strong></p>";

if ($alert!="") die("<h3>VoiceOne Alert</h3>\n$alert");



//include the configuration file
require("admin/config/cfg.php");
global $cwlang, $cwLanguage;

  $step=$_GET["step"];
  
  switch ($step) {
    default:

      if (count($_POST)>0) {
        if (isset($_POST["keys"])) $keys=1; else $keys=0;
        $ret=$asterisk->initialize($keys);
        echo "<script>location.href=\"$PHP_SELF?step=2\";</script>";
        die;
      }

      //check the script install.sh permissions
      $dir="../voiceone_webservices/config/script/vo-tools.sh";
      $perms = fileperms($dir);
      if (!(($perms & 0x0040) )) $alert.=$cwlang["setup"]["installPermission"]."<br/>".realpath($dir);
      
      ob_start();
      ?>
      <div id="loginFirstTime">
        <h1><img src="<?=$relBasePath?>/public/img/VoiceOne-logo.gif" /></h1>
      
        <h2><?=$cwlang["setup"]["title"]?></h2>
        <p><?=$cwlang["setup"]["subTitle"]?></p>
      </div>
    
      <div id="Dialog">
        <h1><?=$cwlang["setup"]["firstStep"]?></h1>
        <form action="<?echo getenv("REQUEST_URI");?>" method="post">
          <dl>
      		  <dd><?=$cwlang["setup"]["keyGeneration"]?>
      		  <input name="keys" type="checkbox" value="1" /></dd>
            <dd><?if ($alert=="") {?><input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-next.gif" alt="Save" /><?}?></dd>
      	  </dl>
        </form>
        <div style="color: red; font-size: smaller"><?=$alert?></div>
      </div>
      <?
      $contents=ob_get_contents();
      ob_end_clean();
    break;
//*************************************************************************************************    
    case "2":
      if (count($_POST)>0) {
        $username=$_POST["username"];
        $password=$_POST["password"];
        $entry=Array("username" => "$username", "password" => "$password", "entity_id" => "0", "type" => USER_SUPERUSER);
        $ret=$asterisk->putUser($entry);
        unset($_SESSION["cwSetup"]);
        die("<script>alert('".$cwlang["setup"]["finished"]."'); location.href='./';</script>");
      }
      ob_start();

      ?>
      <div id="loginFirstTime">
        <h1><img src="<?=$relBasePath?>/public/img/VoiceOne-logo.gif" /></h1>
      
        <h2><?=$cwlang["setup"]["title"]?></h2>
        <p><?=$cwlang["setup"]["subTitle"]?> </p>
      </div>
    
      <div id="Dialog">
        <h1><?=$cwlang["setup"]["secondSteup"]?></h1>
        <form action="<?echo getenv("REQUEST_URI");?>" method="post">
      	<dl>
      		<dt><?=$cwlang["setup"]["username"]?>:</dt>
      		<dd><input name="username" type="text" id="username" /></dd>
      
      		<dt><?=$cwlang["setup"]["password"]?>:</dt>
      		<dd><input name="password" type="password" id="password" /> <!--<span>(<a href="/login/forgot_password">Ho dimenticato la password</a>)</span>--></dd>
      
      		<dd><input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-end.gif" alt="Save" /></dd>
      	</dl>
      
        </form>
      </div>
      <?
      $contents=ob_get_contents();
      ob_end_clean();
    break;
  }
    
  $smarty->assign("alert", $alert);
  $smarty->assign("contents", $contents);
  $smarty->display('setup.tpl');
  die;
  
?>