<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<?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";}

  require_once "utils/top_install.php";

  $title=$TEXT['install-title'];
  
  if(empty($_POST['srv'])) {puterror($_POST['srv'], "<br />".$TEXT['error-install1']."");}
  if(strlen($_POST['srv'])>50) {puterror($_POST['srv'], "<br />".$TEXT['error-install2']."");}
  if(empty($_POST['usr'])) {puterror($_POST['usr'], "<br />".$TEXT['error-install3']."");}
  if(strlen($_POST['usr'])>50) {puterror($_POST['usr'], "<br />".$TEXT['error-install4']."");}
  if(strlen($_POST['pswd'])>100) {puterror($_POST['pswd'], "<br />".$TEXT['error-install5']."");}
  if(empty($_POST['dbname'])) {$_POST['dbname'] = "db_manydhcp";}
  if(strlen($_POST['dbname'])>50) {puterror($TEXT['error-update2']);}

  if(isset($_POST['srv'])) {$srv=trim($_POST['srv']);}
    else {$srv="";}
     if(empty($srv)) {puterror($_POST['srv'], "<br />".$TEXT['error-install6']."");}

  if(isset($_POST['usr'])) {$usr=trim($_POST['usr']);}
    else {$usr="";}
     if(empty($usr)) {puterror($_POST['usr'], "<br />".$TEXT['error-install7']."");}

  if(isset($_POST['dbname'])) {$dbname=trim($_POST['usr']);}
    else {$dbname="";}
     if(empty($dbname)) {puterror($TEXT['error-update4']);}

  $_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'];

  // Устанавливаем соединение с базой данных
  $dbcnx = mysqli_connect($dblocation,$dbuser,$dbpasswd);
  if(!$dbcnx)
  {
    echo( "".$TEXT['error-conf1']."" );
    exit();
  }
  mysqli_query($dbcnx,"SET CHARACTER SET 'utf8'") or exit('SET CHARACTER SET error');
  mysqli_query($dbcnx,"SET character_set_client='utf8'") or exit('SET character_set_client error');
  mysqli_query($dbcnx,"SET character_set_results='utf8'") or exit('SET character_set_results error');
  mysqli_query($dbcnx,"SET collation_connection='utf8_general_ci'") or exit('SET collation_connection error');
  mysqli_query($dbcnx,"SET NAMES `utf8`") or exit('SET NAMES error');
  mysqli_query($dbcnx,"SET SQL_MODE=''") or exit('SET SQL_MODE error');

  $query = "Create database ".$dbname." DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;";
    $result = mysqli_query($dbcnx,$query);
     if(!$result) {puterror($TEXT['error-install8']);}

  if(!mysqli_select_db($dbcnx,$dbname))
  {
    echo( "".$TEXT['error-conf2']."" );
    exit();
  }

  $query2 = "CREATE TABLE tbl_srv(
                         srv_id int(5) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
                         name varchar(255),
                         descr varchar(255),
                         def_lease varchar(25),
                         max_lease varchar(25),
                         ddns_upd_style varchar(255),
                         log_facility varchar(255),
                         opt_ons varchar(255),
                         subnts text(1000),
                         autorit enum('0','1') DEFAULT '1',
                         vkl enum('0','1') DEFAULT '0',
                         sshport int(5),
                         shablon varchar(15),
                         end_opt_ons varchar(255),
                         ping_ok enum('yes','no') DEFAULT 'yes'
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result2 = mysqli_query($dbcnx,$query2);
     if(!$result2) {puterror($TEXT['error-tbl1']);}

  $query3 = "CREATE TABLE tbl_ip(
                         ip_id bigint UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
                         srv_id int(5),
                         name varchar(255),
                         ip_addr varchar(15),
                         hw varchar(12),
                         descr varchar(255),
                         vkl_ip enum('0','1') DEFAULT '0',
                         data date DEFAULT '1970-01-01',
                         state_addr enum('0','1') DEFAULT '0',
                         last_acc date,
                         pods_id int NOT NULL DEFAULT 0,
                         UNIQUE KEY `uc_srv_idip_addr` (`srv_id`, `ip_addr`) USING BTREE,
                         UNIQUE KEY `uc_srv_idhw` (`srv_id`, `hw`) USING BTREE,
                         UNIQUE KEY `uc_srv_idname` (`srv_id`, `name`) USING BTREE
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result3 = mysqli_query($dbcnx,$query3);
     if(!$result3) {puterror($TEXT['error-tbl2']);}

  $query4 = "CREATE TABLE tbl_upr(
                         addr_id int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
                         srv_id int(5),
                         usr varchar(255),
                         pwd blob,
                         comanda enum('scp','cp') DEFAULT 'scp',
                         vkl_upr enum('0','1') DEFAULT '0',
                         locpath varchar(255) NOT NULL DEFAULT '/var/www/html/manydhcp/temp'
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result4 = mysqli_query($dbcnx,$query4);
     if(!$result4) {puterror($TEXT['error-tbl3']);}

  $query5 = "CREATE TABLE tbl_logins(
                         login_id bigint UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
                         login varchar(255),
                         pass varchar(255),
                         administrator enum ('0','1') DEFAULT '0',
                         user enum ('0','1') DEFAULT '0',
                         skin varchar(100),
                         familiya varchar(255),
                         name varchar(255),
                         otchestvo varchar(255),
                         phone varchar(20),
                         e_mail varchar(40),
                         dom_id int(5),
                         fltr_size int(4),
                         chk_upd enum('yes','no') DEFAULT 'no',
                         maintenance enum('yes','no') DEFAULT 'no'
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result5 = mysqli_query($dbcnx,$query5);
     if(!$result5) {puterror($TEXT['error-tbl4']);}

  $query6 = "CREATE TABLE tbl_logs(
                         log_id int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
                         data timestamp NOT NULL,
                         ip varchar(50) NOT NULL,
                         who varchar(250),
                         doing varchar(250),
                         what text(1000),
                         KEY `who_ind` (who(10))
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result6 = mysqli_query($dbcnx,$query6);
     if(!$result6) {puterror($TEXT['error-tbl5']);}

$query8 ="CREATE TABLE tbl_domain(
                             dom_id int(5) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
                             domain varchar(200),
                             UNIQUE KEY `uc_dom` (`domain`)
                           )ENGINE=MyISAM
                           CHARACTER SET utf8;";
    $result8 = mysqli_query($dbcnx,$query8);
     if(!$result8) {puterror($TEXT['error-tbl7']);}

    $query9 = "CREATE TABLE tbl_clear(
                         cl_id int(5) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
                         col_tbl_name varchar(255),
                         col_name varchar(255),
                         by_data int(20),
                         by_count int(20)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result9 = mysqli_query($dbcnx,$query9);
     if(!$result9) {puterror($TEXT['error-tbl8']);}

    $query10 = "CREATE TABLE tbl_ondisk(
                         ond_id int(5) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
                         disk_name varchar(255),
                         mp varchar(255)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result10 = mysqli_query($dbcnx,$query10);
     if(!$result10) {puterror($TEXT['error-tbl9']);}

     $query11 = "CREATE TABLE tbl_house(
                         house_id int(5) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
                         house varchar(255),
                         UNIQUE KEY `uc_house` (`house`)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result11 = mysqli_query($dbcnx,$query11);
     if(!$result11) {puterror($TEXT['error-tbl10']);}

  $query12 = "CREATE TABLE tbl_etazh(
                         etazh_id int(5) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
                         etazh varchar(20),
                         UNIQUE KEY `uc_etazh` (`etazh`)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result12 = mysqli_query($dbcnx,$query12);
     if(!$result12) {puterror($TEXT['error-tbl11']);}

   $query13 = "CREATE TABLE tbl_oborud(
                         obor_id int(5) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
                         oborud varchar(255),
                         UNIQUE KEY `uc_oborud` (`oborud`)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result13 = mysqli_query($dbcnx,$query13);
     if(!$result13) {puterror($TEXT['error-tbl12']);}

   $query14 = "CREATE TABLE tbl_podset(
                         `pods_id` int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
                         `srv_id` int(5),
                         `house_id` int(5),
                         `etazh_id` int(5),
                         `obor_id` int(5),
                         `podset` varchar(15),
                         `prefix` varchar(255),
                         `begin_subn` varchar(255),
                         `end_subn` varchar(255),
                         `opthstnm` enum('0','1'),
                         KEY `srv_id_ind` (`srv_id`),
                         KEY `house_id_ind` (`house_id`),
                         KEY `etazh_id_ind` (`etazh_id`),
                         KEY `obor_id_ind` (`obor_id`)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result14 = mysqli_query($dbcnx,$query14);
     if(!$result14) {puterror($TEXT['error-tbl13']);}

   $query15 = "CREATE TABLE tbl_srvparam(
                         `sp_id` int(5) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
                         `sams` enum('0','1'),
                         `usrpass` int(5)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result15 = mysqli_query($dbcnx,$query15);
     if(!$result15) {puterror($TEXT['error-tbl14']);}

  $query_pwd = mysqli_query($dbcnx,"CREATE USER 'manydhcp_user'@'%' IDENTIFIED BY 'manydhcp_user';");
  $query_pwd1 = mysqli_query($dbcnx,"CREATE USER 'manydhcp_user'@'localhost' IDENTIFIED BY 'manydhcp_user';");
  $query_pwd2 = mysqli_query($dbcnx,"CREATE USER 'manydhcp_admin'@'%' IDENTIFIED BY 'Pgt7d34smb9';");
  $query_pwd3 = mysqli_query($dbcnx,"CREATE USER 'manydhcp_admin'@'localhost' IDENTIFIED BY 'Pgt7d34smb9';");

  $query_rights = mysqli_query($dbcnx,"GRANT SELECT ON db_manydhcp.* TO manydhcp_user@'%';");
  $query_rights1 = mysqli_query($dbcnx,"GRANT SELECT ON db_manydhcp.* TO manydhcp_user@localhost;");
  $query_rights2 = mysqli_query($dbcnx,"GRANT SELECT,INSERT,UPDATE,DELETE,DROP,LOCK TABLES ON db_manydhcp.* TO manydhcp_admin@'%';");
  $query_rights3 = mysqli_query($dbcnx,"GRANT SELECT,INSERT,UPDATE,DELETE,DROP,LOCK TABLES ON db_manydhcp.* TO manydhcp_admin@localhost;");
  $query_rights4 = mysqli_query($dbcnx,"GRANT SELECT,INSERT,DELETE ON db_manydhcp.tbl_logs TO manydhcp_user@'%';");
  $query_rights5 = mysqli_query($dbcnx,"GRANT SELECT,INSERT,DELETE ON db_manydhcp.tbl_logs TO manydhcp_user@localhost;");
  $query_rights6 = mysqli_query($dbcnx,"GRANT SELECT,INSERT,UPDATE,DELETE ON db_manydhcp.tbl_ip TO manydhcp_user@'%';");
  $query_rights7 = mysqli_query($dbcnx,"GRANT SELECT,INSERT,UPDATE,DELETE ON db_manydhcp.tbl_ip TO manydhcp_user@localhost;");
 
  $query34 = "FLUSH PRIVILEGES;";
    $result34 = mysqli_query($dbcnx,$query34);
     //if(!$result34) {puterror($TEXT['error-install17']);}

  // Дамп таблиц
  $query_dump = mysqli_query($dbcnx,"INSERT INTO `tbl_logins` VALUES (1,'admin','21232f297a57a5a743894a0e4a801fc3','1','0','base','','Администратор','','','','',20,'no','no');");
  $query_dump1 = mysqli_query($dbcnx,"INSERT INTO `tbl_srvparam` VALUES (1,'0',12);");

  echo "<html><head>
       <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=index.php' />
       </head></html>";

  function puterror($message)
  {
    echo("<p align='center'><img align=middle src='skins/base/stop.png'>&nbsp;&nbsp;".$message."</p>");
    exit();
  }
  require_once "utils/bottom.php";
?>