<?php
  ///////////////////////////////////////////////////
  //Управление серверами DHCP,
  //инсталяция
  //2010 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////

  //определяем локаль пришедшего и подставляем нужный язык.
  $lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0,2);
  if(is_file ("lang/".$lang."/".$lang.".php")) {include "lang/".$lang."/".$lang.".php";}
     else {include "lang/ru/ru.php";}

  $title=$TEXT['install-title'];
  $helppage = $TEXT['install-help'];

  require_once "utils/top_install.php";

  $button = $TEXT['btn-check'];
  $action = "install.php";

  if(extension_loaded('ssh2')) {$ssh2_ok = "<img src='skins/base/active.png' />";}
   else {$ssh2_ok = "<img src='skins/base/no_active.png' />";}

  if(extension_loaded('mysqli')) {$mysqli_ok = "<img src='skins/base/active.png' />";}
   else {$mysqli_ok = "<img src='skins/base/no_active.png' />";}

  if(function_exists('mail')) {$mail_ok = "<img src='skins/base/active.png' />";}
   else {$mail_ok = "<img src='skins/base/no_active.png' />";}

  if(strlen($_POST['srv'])>50) {puterror($TEXT['error-install2']);}
  if(strlen($_POST['usr'])>50) {puterror($TEXT['error-install4']);}
  if(strlen($_POST['pswd'])>100) {puterror($TEXT['error-install5']);}
  if(empty($_POST['dbname'])) {$_POST['dbname'] = "db_manydhcp";}
  if(empty($_POST['srv'])) {$_POST['srv'] = "localhost";}
  if(empty($_POST['usr'])) {$_POST['usr'] = "root";}
  if(strlen($_POST['dbname'])>50) {puterror($TEXT['error-update2']);}

  $_POST['srv'] = str_replace("'","`",$_POST['srv']);
  $_POST['usr'] = str_replace("'","`",$_POST['usr']);
  $_POST['dbname'] = str_replace("'","`",$_POST['dbname']);
  $_POST['srv'] = htmlspecialchars(stripslashes($_POST['srv']));
  $_POST['usr'] = htmlspecialchars(stripslashes($_POST['usr']));
  $_POST['dbname'] = htmlspecialchars(stripslashes($_POST['dbname']));

  $dblocation = $_POST['srv'];
  $dbport = "3306";
  $dbname = $_POST['dbname'];
  $dbuser = $_POST['usr'];
  $dbpasswd = $_POST['pswd'];
  $chk_dbase = "<img alt='no checking' src='skins/base/wait.png' />";

  // Устанавливаем соединение с базой данных
  if(!empty($_POST['srv']) && !empty($_POST['usr']) && !empty($_POST['dbname']) && !empty($_POST['pswd']))
  {
  $dbcnx = mysqli_connect($dblocation,$dbuser,$dbpasswd);
  if(!$dbcnx)
   {
    echo $TEXT['error-conf1'];
    exit();
   }

  if(!mysqli_select_db($dbcnx,$dbname))
   {
    $chk_dbase = "<img alt='Ok' src='skins/base/active.png' />";
    $button = $TEXT['btn_inst'];
    $action = "install_run.php";
   }
   else {$chk_dbase = "<img alt='DB exist' src='skins/base/warn.png' />";}
  }
?>
<table align="center" width="100%" border="0" cellspacing="0" cellpadding="20">
<tr><td>&nbsp;</td></tr>
</table>
<table align="center" width="65%" border="0" cellspacing="0" cellpadding="0">
  <tr>
  <td width="16">&nbsp;</td>
  <td></td>
  <td width="16">&nbsp;</td>
 </tr>
 <tr>
  <td class="osn_top_r"></td>
  <td class="osn_top"></td>
  <td class="osn_top_l"></td>
 </tr>
 <tr>
  <td class="osn_left">&nbsp;</td>
  <td class="osn_center">
<table width="100%"  border="0" cellspacing="0" cellpadding="30">
 <tr>
  <td>
   <form enctype='multipart/form-data' action=<?php echo $action; ?> method=post>
    <table border="0" align="center">
     <tr><td align="center" class="zagolovok"><?php echo $TEXT['install-title']; ?></td></tr>
     <tr><td class="upd"><?php echo $TEXT['install-help']; ?></td></tr>
    </table>
    <br />
    <table border="0" align="center">
     <tr><td align="right" class="add_field_op"><?php echo $TEXT['install-srv']; ?></td><td class=useraddedit title="<?php echo $TEXT['global-mandatory']; ?>"><input size=30 maxlength=50 class=add_field type=text name=srv value="<?php echo $_POST['srv']; ?>"></td></tr>
     <tr><td align="right" class="add_field_op"><?php echo $TEXT['update-db']; ?></td><td class="useraddedit" title="<?php echo $TEXT['global-mandatory']; ?>"><input size=30 maxlength=50 class=add_field type=text name=dbname value="<?php echo $_POST['dbname']; ?>"></td></tr>
     <tr><td align="right" class="add_field_op"><?php echo $TEXT['install-usr']; ?></td><td class=useraddedit title="<?php echo $TEXT['global-mandatory']; ?>"><input size=30 maxlength=30 class=add_field type=text name=usr value="<?php echo $_POST['usr']; ?>"></td></tr>
     <tr><td align="right" class="add_field_op"><?php echo $TEXT['install-pswd']; ?></td><td class=useraddedit title="<?php echo $TEXT['global-mandatory']; ?>"><input size=30 maxlength=100 class=add_field type=password name=pswd value="<?php echo $_POST['pswd']; ?>"></td></tr>
     <tr><td>&nbsp;</td><td><input class="button" type=submit value='<?php echo $button; ?>'></td></tr>
    </table>
    <table border="0" align="left">
     <tr><td>
      <fieldset>
      <legend align="left" title="<?php echo $TEXT['install-mod-tit']; ?>"><?php echo $TEXT['install-mod']; ?></legend>
       <table align="center" border="0">
        <tr><td align="right" class="add_field_op"><?php echo $TEXT['install-mod-ssh']; ?></td><td><?php echo $ssh2_ok; ?></td></tr>
        <tr><td align="right" class="add_field_op"><?php echo $TEXT['install-mod-mysql']; ?></td><td><?php echo $mysqli_ok; ?></td></tr>
        <tr><td align="right" class="add_field_op"><?php echo $TEXT['install-mod-mail']; ?></td><td><?php echo $mail_ok; ?></td></tr>
        <tr><td align="right" class="add_field_op"><?php echo $TEXT['update-db']; ?></td><td><?php echo $chk_dbase; ?></td></tr>
       </table>
      </fieldset>
     </td></tr>
    </table>
   </form>
  </td>
 </tr>
</table>
<table border="0" align="right">
 <tr><td align="right" class="upd"><?php echo $TEXT['global-vertion']; ?></td></tr>
</table>
 </td>
 <td class="osn_right">&nbsp;</td>
</tr>
<tr>
 <td class="osn_bottom_r"></td>
 <td class="osn_bottom"></td>
 <td class="osn_bottom_l"></td>
</tr>
</table>
</body>
</html>