<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<?php
  ///////////////////////////////////////////////////
  //Телефонный справочник, инсталяция
  //2007 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////

  $lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0,2);
  if(is_file ("lang/".$lang.".php")) include "lang/".$lang.".php";
     else include "lang/ru.php";

  require_once "utils/top_install.php";

  $title=$titlepage=$TEXT['install-title'];
  $helppage = $TEXT['install-help2'];

  if(empty($_POST['srv'])) puterror($TEXT['error-install1']);
  if(strlen($_POST['srv'])>50) puterror($TEXT['error-install2']);
  if(empty($_POST['usr'])) puterror($TEXT['error-install3']);
  if(strlen($_POST['usr'])>50) puterror($TEXT['error-install4']);
  if(strlen($_POST['pswd'])>100) puterror($TEXT['error-install5']);
  if(empty($_POST['dbname'])) $_POST['dbname'] = "personal";
  if(strlen($_POST['dbname'])>50) puterror($TEXT['error-update2']);

  if(isset($_POST['srv'])) $srv=trim($_POST['srv']);
    else $srv="";
     if(empty($srv)) puterror($TEXT['error-install6']);

  if(isset($_POST['usr'])) $usr=trim($_POST['usr']);
    else $usr="";
     if(empty($usr)) puterror($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 = @mysql_connect($dblocation,$dbuser,$dbpasswd);
  if(!$dbcnx)
  {
    puterror($TEXT['error-conf1']);
    exit();
  }
  mysql_query("SET CHARACTER SET 'utf8'") or exit('SET CHARACTER SET error');
  mysql_query("SET character_set_client='utf8'") or exit('SET character_set_client error');
  mysql_query("SET character_set_results='utf8'") or exit('SET character_set_results error');
  mysql_query("SET collation_connection='utf8_general_ci'") or exit('SET collation_connection error');
  mysql_query("SET NAMES `utf8`") or exit('SET NAMES error');
  mysql_query("SET SQL_MODE=''") or exit('SET SQL_MODE error');

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

  if(! @mysql_select_db($dbname,$dbcnx) )
  {
    puterror($TEXT['error-conf2']);
    exit();
  }

  $query2 = "CREATE TABLE tbl_upravlenie(
                          upr_id int unsigned NOT NULL auto_increment primary key,
                          upravlenie text(300) NOT NULL,
                          position SMALLINT(5) NOT NULL default '0',
                          visible enum('show','hide') default 'show',
                          UNIQUE KEY uc_upravlenie (upravlenie(300))
                          )ENGINE=MyISAM
                          CHARACTER SET utf8;";
    $result2 = mysql_query($query2);
     if(!$result2) puterror($TEXT['error-tbl9']);

  $query3 = "CREATE TABLE tbl_otdel(
                         otd_id int unsigned NOT NULL auto_increment primary key,
                         otdel varchar(250) NOT NULL,
                         position SMALLINT(5) NOT NULL default '0',
                         visible enum('show','hide') default 'show',
                         phone varchar(20),
                         UNIQUE KEY uc_otdel (otdel(250))
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result3 = mysql_query($query3);
     if(!$result3) puterror($TEXT['error-tbl8']);

  $query4 = "CREATE TABLE tbl_dolzhnost(
                         dol_id int unsigned NOT NULL auto_increment primary key,
                         dolzhnost varchar(120) NOT NULL,
                         position SMALLINT(5) NOT NULL default '0',
                         UNIQUE KEY uc_dolzhnost (dolzhnost(120))
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result4 = mysql_query($query4);
     if(!$result4) puterror($TEXT['error-tbl6']);

  $query5 = "CREATE TABLE tbl_city(
                         city_id int unsigned NOT NULL auto_increment primary key,
                         city varchar(120) NOT NULL,
                         UNIQUE KEY uc_city (city(120))
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result5 = mysql_query($query5);
     if(!$result5) puterror($TEXT['error-tbl1']);

  $query6 = "CREATE TABLE tbl_street(
                         str_id int unsigned NOT NULL auto_increment primary key,
                         street varchar(200) NOT NULL,
                         UNIQUE KEY uc_street (street(200))
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result6 = mysql_query($query6);
     if(!$result6) puterror($TEXT['error-tbl3']);

  $query7 = "CREATE TABLE tbl_house(
                         house_id int unsigned NOT NULL auto_increment primary key,
                         house varchar(80) NOT NULL,
                         UNIQUE KEY uc_house (house(80))
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result7 = mysql_query($query7);
     if(!$result7) puterror($TEXT['error-tbl4']);

  $query8 = "CREATE TABLE tbl_organizaciya(
                         org_id int unsigned NOT NULL auto_increment primary key,
                         organizaciya text(300) NOT NULL,
                         position SMALLINT(3) NOT NULL default '0',
                         visible enum('show','hide') NOT NULL default 'show',
                         UNIQUE KEY uc_organizaciya (organizaciya(300))
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result8 = mysql_query($query8);
     if(!$result8) puterror($TEXT['error-tbl7']);

  $query9 = "CREATE TABLE tbl_domain(
                         dom_id int unsigned NOT NULL auto_increment primary key,
                         domain varchar(200) NOT NULL,
                         UNIQUE KEY uc_domain (domain(200))
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result9 = mysql_query($query9);
     if(!$result9) puterror($TEXT['error-tbl5']);

  $query10 = "CREATE TABLE tbl_users(
                         user_id int unsigned NOT NULL auto_increment primary key,
                         first_b varchar(4) NOT NULL,
                         familiya varchar(200) NOT NULL,
                         name varchar(200) NOT NULL,
                         otchestvo varchar(200),
                         position SMALLINT(5) NOT NULL default '0',
                         dom_id int,
                         mail varchar(220),
                         phone_mob varchar(30),
                         phone_int varchar(20),
                         phone_city varchar(30),
                         dol_id int,
                         otd_id int,
                         otd_pos SMALLINT(5) NOT NULL default '0',
                         upr_id int,
                         upr_pos SMALLINT(5) NOT NULL default '0',
                         org_id int,
                         org_pos SMALLINT(3) NOT NULL default '0',
                         room varchar(16),
                         house_id int,
                         str_id int,
                         city_id int,
                         photo varchar(255),
                         subscribe enum('scribe','unscribe') NOT NULL default 'unscribe',
                         visible enum('show','hide') NOT NULL default 'show',
                         KEY ind_dom_id (dom_id),
                         KEY ind_dol_id (dol_id),
                         KEY ind_otd_id (otd_id),
                         KEY ind_upr_id (upr_id),
                         KEY ind_org_id (org_id),
                         KEY ind_house_id (house_id),
                         KEY ind_str_id (str_id),
                         KEY ind_city_id (city_id),
                         KEY ind_familiya (familiya(5)),
                         KEY ind_name (name(5)),
                         KEY ind_first_b (first_b(4)),
                         KEY ind_room (room(5))
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result10 = mysql_query($query10);
     if(!$result10) puterror($TEXT['error-tbl2']);

  $query11 = "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
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result11 = mysql_query($query11);
     if(!$result11) puterror($TEXT['error-tbl10']);

  $query13 = "CREATE TABLE tbl_photo(
                         photo_id int unsigned not null auto_increment primary key,
                         name text(300) NOT NULL,
                         small tinytext NOT NULL,
                         big tinytext NOT NULL,
                         description text,
                         position int NOT NULL default '0',
                         visible enum('show','hide') NOT NULL default 'show',
                         mer_id int
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result13 = mysql_query($query13);
     if(!$result13) puterror($TEXT['error-tbl13']);

  $query14 = "CREATE TABLE tbl_meropriyatiya(
                         mer_id int unsigned not null auto_increment primary key,
                         meropriyatie text NOT NULL,
                         position smallint NOT NULL default '0',
                         visible enum('show','hide') NOT NULL default 'show',
                         org_id int
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result14 = mysql_query($query14);
     if(!$result14) puterror($TEXT['error-tbl12']);

  $query15 = "CREATE TABLE tbl_documents(
                         doc_id int unsigned not null auto_increment primary key,
                         name text NOT NULL,
                         path tinytext NOT NULL,
                         description text,
                         position int NOT NULL default '0',
                         data timestamp not null,
                         visible enum('show','hide') NOT NULL default 'show',
                         razd_id int,
                         count_view int,
                         KEY ind_name (name(10))
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result15 = mysql_query($query15);
     if(!$result15) puterror($TEXT['error-tbl14']);

  $query16 = "CREATE TABLE tbl_razdel(
                         razd_id int unsigned not null auto_increment primary key,
                         razdel text NOT NULL,
                         position smallint NOT NULL default '0',
                         visible enum('show','hide') NOT NULL default 'show',
                         org_id int
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result16 = mysql_query($query16);
     if(!$result16) puterror($TEXT['error-tbl15']);

  $query17 = "CREATE TABLE tbl_instructions(
                         instr_id int unsigned not null auto_increment primary key,
                         name text NOT NULL,
                         path tinytext NOT NULL,
                         description text,
                         position int NOT NULL default '0',
                         photo text NOT NULL,
                         visible enum('show','hide') NOT NULL default 'show',
                         razd_id int,
                         count_view int,
                         KEY ind_instr (name(10))
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result17 = mysql_query($query17);
     if(!$result17) puterror($TEXT['error-tbl18']);

  $query18 = "CREATE TABLE tbl_razdel_instr(
                         razd_id int unsigned not null auto_increment primary key,
                         photo tinytext NOT NULL,
                         razdel text NOT NULL,
                         position smallint(11) NOT NULL default '0',
                         visible enum('show','hide') NOT NULL default 'show'
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result18 = mysql_query($query18);
     if(!$result18) puterror($TEXT['error-tbl19']);

  $query19 = "CREATE TABLE tbl_resource(
                         res_id int unsigned not null auto_increment primary key,
                         resource text NOT NULL,
                         position smallint NOT NULL,
                         user_id int NOT NULL,
                         description text,
                         visible enum('show','hide') NOT NULL default 'show',
                         org_id int,
                         ukk varchar(255),
                         data date,
                         srvname varchar(100),
                         srvput varchar(255),
                         gread text(1000),
                         gwrite text(1000),
                         sppubl varchar(100),
                         srvkvota int(10),
                         size_id int(2),
                         bakup ENUM('yes','no') NOT NULL DEFAULT 'yes',
                         rezim varchar(255),
                         bakrazm varchar(255),
                         bakprog varchar(255),
			             photo varchar(255),
                         tip_id int,
                         res_name varchar(255),
                         size_now int,
                         KEY ind_res (resource(10)) 
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result19 = mysql_query($query19);
     if(!$result19) puterror($TEXT['error-tbl16']);

  $query20 = "CREATE TABLE tbl_news(
                         new_id int unsigned not null auto_increment primary key,
                         org_id int,
                         header text(300) NOT NULL,
                         news text NOT NULL,
                         position smallint(11) NOT NULL default '0',
                         description text(300),
                         url text,
                         url_text text(300),
                         picture tinytext,
                         picture_big tinytext,
                         data timestamp not null,
                         visible enum('show','hide') NOT NULL default 'show',
                         KEY ind_new (header(10))
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result20 = mysql_query($query20);
     if(!$result20) puterror($TEXT['error-tbl17']);

  $query21 = "CREATE TABLE tbl_logins(
                         login_id int unsigned not null auto_increment primary key,
                         login varchar(30) NOT NULL,
                         fullname varchar(255),
                         pass varchar(255),
                         moderator enum('0','1') NOT NULL default '0',
                         administrator enum('0','1') NOT NULL default '0',
                         user enum('0','1') NOT NULL default '0',
                         manager enum('0','1') NOT NULL default '0',
                         master enum('0','1') NOT NULL default '0',
                         otvetstvenniy enum('0','1') NOT NULL default '0',
                         usr_id int,
                         chk_upd enum('yes','no') DEFAULT 'no',
                         UNIQUE KEY uc_login (login(30))
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result21 = mysql_query($query21);
     if(!$result21) puterror($TEXT['error-tbl20']);

  $query22 = "CREATE TABLE tbl_forecast(
                         id int unsigned not null auto_increment primary key,
                         ind mediumint(9) default NULL,
                         station varchar(255) default NULL,
                         country varchar(255) default NULL,
                         region varchar(255) default NULL,
                         dat date default NULL,
                         tday smallint(6) default NULL,
                         tnight smallint(6) default NULL,
                         prec smallint(6) default NULL,
                         prec_prob smallint(6) default NULL,
                         wind_dir smallint(6) default NULL,
                         wind_speed smallint(6) default NULL,
                         weather_conditions varchar(255) default NULL,
                         pday smallint(6) default NULL,
                         pnight smallint(6) default NULL
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result22 = mysql_query($query22);
     if(!$result22) puterror($TEXT['error-tbl21']);

    $query23 = "CREATE TABLE tbl_ob_zay(
                         ob_zay_id int unsigned NOT NULL auto_increment primary key,
                         root_zay_id int,
                         par_zay_id int,
                         KEY ind_root_zay (root_zay_id)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result23 = mysql_query($query23);
     if(!$result23) puterror($TEXT['error-tbl36']);

    $query24 = "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;";
    $result24 = mysql_query($query24);
     if(!$result24) puterror($TEXT['error-tbl38']);

    $query25 = "CREATE TABLE tbl_kritichnost(
                         krit_id int unsigned not null auto_increment primary key,
                         kritichnost varchar(255),
                         pic_krit tinytext,
                         position smallint(11) NOT NULL,
                         UNIQUE KEY uc_kritichnost (kritichnost(255))
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result25 = mysql_query($query25);
     if(!$result25) puterror($TEXT['error-tbl39']);

    $query26 = "CREATE TABLE tbl_zhurn_strukt(
                         zh_str_id int unsigned NOT NULL auto_increment primary key,
                         strukt_name varchar(255) NOT NULL,
                         parent int,
                         position smallint NOT NULL default '0',
                         visible enum('show','hide') NOT NULL default 'show'
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result26 = mysql_query($query26);
     if(!$result26) puterror($TEXT['error-tbl40']);

    $query27 = "CREATE TABLE tbl_zhurnal(
                         zhurn_id int unsigned NOT NULL auto_increment primary key,
                         krit_id int,
                         zh_str_id int,
                         zh_tema varchar(255),
                         zh_soobschenie text(4000),
                         position smallint NOT NULL default '0',
                         visible enum('show','hide') NOT NULL default 'show',
                         data timestamp not null,
                         login_id int,
                         KEY krit_id_ind (krit_id),
                         KEY zh_str_id_ind (zh_str_id)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result27 = mysql_query($query27);
     if(!$result27) puterror($TEXT['error-tbl41']);

    $query28 = "CREATE TABLE tbl_marsh(
                         mar_id int unsigned NOT NULL auto_increment primary key,
                         mar_name varchar(255),
                         position smallint NOT NULL default '0',
                         visible enum('show','hide') NOT NULL default 'show'
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result28 = mysql_query($query28);
     if(!$result28) puterror($TEXT['error-tbl42']);

    $query29 = "CREATE TABLE tbl_step(
                         step_id int unsigned NOT NULL auto_increment primary key,
                         mar_id int,
                         step_name varchar(255),
                         position smallint NOT NULL default '0',
                         login_id int,
                         logika enum('1','2','3') NOT NULL default '1',
                         tema_id int
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result29 = mysql_query($query29);
     if(!$result29) puterror($TEXT['error-tbl43']);

    $query30 = "CREATE TABLE tbl_tip_resurs(
                         tip_id int unsigned not null auto_increment primary key,
                         tip_name varchar(255),
                         pic_tip tinytext,
                         position smallint(11) NOT NULL,
                         prefix varchar(255)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result30 = mysql_query($query30);
     if(!$result30) puterror($TEXT['error-tbl38']);

  $query31 = "CREATE TABLE tbl_srv_param(
                         srvp_id int unsigned NOT NULL auto_increment primary key,
                         ldp enum('0','1') default '0',
                         ldp_dn varchar(255),
                         ldp_user varchar(255),
                         ldp_pass varchar(255),
                         dlp_srv varchar(255),
                         ldp_port int(5),
                         exim_shell varchar(255),
                         exim_uid int(5),
                         exim_gid int(5),
                         exim_home varchar(255),
                         exim_kvota int(5),
                         exim_pass int(5),
                         admin_mail_rss varchar(255),
                         admin_mail_news varchar(255),
                         admin_mail varchar(255),
                         spr_pass int(5),
                         glob_w int(5),
                         glob_h int(5),
                         user_w int(5),
                         user_h int(5),
                         exim_w int(5),
                         exim_h int(5),
                         res_w int(5),
                         res_h int(5),
                         repl enum('0','1'),
                         rep_org_id int,
                         prx_set varchar(21),
                         repl_mail enum('yes','no') DEFAULT 'no'
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result31 = mysql_query($query31);
     if(!$result31) puterror($TEXT['error-tbl44']);

  $query32 = "CREATE TABLE tbl_zhnaniy_strukt(
                         zh_str_id int unsigned NOT NULL auto_increment primary key,
                         strukt_name varchar(255) NOT NULL COLLATE utf8_general_ci,
                         parent int,
                         position smallint NOT NULL default '0',
                         visible enum('show','hide') NOT NULL default 'show'
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result32 = mysql_query($query32);
     if(!$result32) puterror($TEXT['error-tbl45']);

  $query33 = "CREATE TABLE tbl_bd_zhnaniy(
                         zhurn_id int unsigned NOT NULL auto_increment primary key,
                         krit_id int,
                         zh_str_id int,
                         zh_tema varchar(255),
                         zh_soobschenie text,
                         position smallint NOT NULL default '0',
                         visible enum('show','hide') NOT NULL default 'show',
                         data timestamp not null,
                         login_id int,
                         primechanie text(4000),
                         KEY zn_krit_id_ind (krit_id),
                         KEY zhn_str_id_ind (zh_str_id),
                         FULLTEXT INDEX `fti_soobschenie` (`zh_soobschenie` ASC)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result33 = mysql_query($query33);
     if(!$result33) puterror($TEXT['error-tbl46']);

    $query34 = "CREATE TABLE tbl_know_prim(
                         tr_id int unsigned NOT NULL auto_increment primary key,
                         zhurn_id int,
                         login_id int,
                         zh_primechanie text(4000),
                         data timestamp not null,
                         KEY zhurn_id_ind (zhurn_id),
                         KEY login_id_ind (login_id)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result34 = mysql_query($query34);
      if(!$result34) puterror($TEXT['error-tbl47']);

    $query35 = "CREATE TABLE tbl_tmp_ofc(
                         tmp_ofc_id int unsigned NOT NULL auto_increment primary key,
                         user varchar(255),
                         yea decimal(4,0),
                         mon decimal(2,0),
                         dy decimal(2,0)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result35 = mysql_query($query35);
      if(!$result35) puterror(mysql_error());

    $query36 = "CREATE TABLE tbl_prn_logs(
                         prn_log_id int unsigned NOT NULL auto_increment primary key,
                         prn_data datetime,
                         usr_login_id int,
                         usr_name_id int,
                         pc_id int,
                         prn_id int,
                         port_id int,
                         size int,
                         pages int,
                         document varchar(255),
                         srv_comp_id int,
                         KEY ind_usr_login(usr_login_id),
                         KEY ind_usr_name(usr_name_id),
                         KEY ind_pc(pc_id),
                         KEY ind_prn(prn_id),
                         KEY ind_port(port_id),
                         KEY ind_pages(pages),
                         KEY ind_srv_comp(srv_comp_id)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result36 = mysql_query($query36);
      if(!$result36) puterror(mysql_error());

    $query37 = "CREATE TABLE tbl_prn_pc(
                         prn_pc_id int unsigned NOT NULL auto_increment primary key,
                         pc_name varchar(255)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result37 = mysql_query($query37);
      if(!$result37) puterror(mysql_error());

    $query38 = "CREATE TABLE tbl_prn_usr_login(
                         usr_login_id int unsigned NOT NULL auto_increment primary key,
                         login_name varchar(255)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result38 = mysql_query($query38);
      if(!$result38) puterror(mysql_error());

    $query39 = "CREATE TABLE tbl_prn_usr_name(
                         usr_name_id int unsigned NOT NULL auto_increment primary key,
                         usr_name varchar(255)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result39 = mysql_query($query39);
      if(!$result39) puterror(mysql_error());

    $query40 = "CREATE TABLE tbl_prn_name(
                         prn_id int unsigned NOT NULL auto_increment primary key,
                         prn_name varchar(255)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result40 = mysql_query($query40);
      if(!$result40) puterror(mysql_error());

    $query41 = "CREATE TABLE tbl_prn_port(
                         port_id int unsigned NOT NULL auto_increment primary key,
                         port_name varchar(255)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result41 = mysql_query($query41);
      if(!$result41) puterror(mysql_error());

    $query42 = "CREATE TABLE tbl_prn_srv_comp(
                         srv_comp_id int unsigned NOT NULL auto_increment primary key,
                         srv_name varchar(255)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result42 = mysql_query($query42);
      if(!$result42) puterror(mysql_error());

    $query79 = "CREATE TABLE tbl_supp_tema(
                         tema_id int unsigned not null auto_increment primary key,
                         tema varchar(255),
                         position smallint(11) NOT NULL,
                         shablon varchar(255),
                         visible enum('show','hide','it') default 'show'
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result79 = mysql_query($query79);
     if(!$result79) puterror($TEXT['error-tbl22']);

    $query80 = "CREATE TABLE tbl_supp_sostoyanie(
                         sost_id int unsigned not null auto_increment primary key,
                         sostoyanie varchar(255),
                         pic_sost tinytext,
                         position smallint(11) NOT NULL
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result80 = mysql_query($query80);
     if(!$result80) puterror($TEXT['error-tbl23']);

    $query81 = "CREATE TABLE tbl_supp_srochnost(
                         sroch_id int unsigned not null auto_increment primary key,
                         srochnost varchar(255),
                         pic_sr tinytext,
                         position smallint(11) NOT NULL
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result81 = mysql_query($query81);
     if(!$result81) puterror($TEXT['error-tbl24']);

    $query82 = "CREATE TABLE tbl_supp_archive(
                         arch_id int unsigned not null auto_increment primary key,
                         data date,
                         master varchar(255),
                         user varchar(255),
                         manager varchar(255),
                         tema varchar(255),
                         organizaciya text(300),
                         time_isp decimal(10,0),
                         zay_id int,
                         yea decimal(4,0),
                         mon decimal(2,0),
                         dy decimal(2,0),
                         problema text NOT NULL,
                         pic_tr tinytext,
                         reshenie text NOT NULL,
                         mar_id int DEFAULT 0,
                         step smallint DEFAULT 0,
                         parent int DEFAULT 0,
                         KEY ind_master (master(10)),
                         KEY ind_user (user(10)),
                         KEY ind_manager (manager(10)),
                         KEY ind_tema (tema(20)),
                         KEY ind_organizaciya (organizaciya(10)),
                         KEY ind_mar (mar_id),
                         KEY ind_step (step),
                         KEY ind_parent (parent)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result82 = mysql_query($query82);
     if(!$result82) puterror($TEXT['error-tbl25']);

    $query83 = "CREATE TABLE tbl_supp_zayavka(
                         zay_id int unsigned not null auto_increment primary key,
                         data_post datetime,
                         data_k_isp datetime,
                         data_close datetime,
                         man_id int,
                         mast_id int,
                         usr_id int,
                         sost_id int,
                         sroch_id int,
                         tema_id int,
                         problema text NOT NULL,
                         pic_tr tinytext,
                         reshenie text NOT NULL,
                         kod text,
                         time_isp decimal(10,0),
                         org_id int,
			             ob_type enum('0','1','2','3','4','5') NOT NULL default '0',
                         otv enum('0','1') default '0',
                         mar_id int DEFAULT '0',
                         step smallint DEFAULT '0',
                         max_step smallint DEFAULT '0',
                         parent int DEFAULT '0'
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result83 = mysql_query($query83);
     if(!$result83) puterror($TEXT['error-tbl26']);

    $query84 = "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;";
    $result84 = mysql_query($query84);
     if(!$result84) puterror($TEXT['error-tbl37']);

    $query85 = "CREATE TABLE tbl_meet_room(
                         room_id int(5) unsigned NOT NULL auto_increment primary key,
                         room_name varchar(255),
                         room_descr text(1000),
                         room_photo varchar(255),
                         room_visible enum('yes','no') default 'yes',
                         org_id int,
                         position smallint NOT NULL,
                         KEY ind_org_id(org_id)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result85 = mysql_query($query85);
     if(!$result85) puterror($TEXT['error-tbl48']);

    $query86 = "CREATE TABLE tbl_meet_event(
                         evnt_id int unsigned NOT NULL auto_increment primary key,
                         meet_name varchar(255),
                         meet_descr text(1000),
                         meet_visible enum('yes','no') default 'yes',
                         usr_id int,
                         meet_data date,
                         time_start int(2),
                         time_stop int(2),
                         meet_color varchar(50),
                         room_id int(5),
                         time_start_min int(2),
                         time_stop_min int(2),
                         KEY ind_room_id(room_id)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result86 = mysql_query($query86);
     if(!$result86) puterror($TEXT['error-tbl49']);

    $query87 = "CREATE TABLE tbl_meet_users(
                         meet_usr_id int(5) unsigned NOT NULL auto_increment primary key,
                         evnt_id int,
                         usr_id int,
                         KEY ind_evnt_id(evnt_id)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result87 = mysql_query($query87);
     if(!$result87) puterror($TEXT['error-tbl50']);

    $query88 = "CREATE TABLE tbl_pass_strukt(
                         pas_str_id int unsigned NOT NULL auto_increment primary key,
                         strukt_name varchar(255) NOT NULL,
                         parent int,
                         position smallint NOT NULL default '0',
                         visible enum('show','hide') NOT NULL default 'show',
                         pic_tr tinytext
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result88 = mysql_query($query88);
     if(!$result88) puterror($TEXT['error-tbl51']);

    $query89 = "CREATE TABLE tbl_pass_keep(
                         pas_id int unsigned NOT NULL auto_increment primary key,
                         pas_str_id int,
                         fio varchar(255),
                         usr_id int,
                         dom_lgn blob,
                         dom_pass blob,
                         mail_lgn blob,
                         mail_pass blob,
                         primechanie text(1000),
                         visible enum('show','hide') NOT NULL default 'show',
                         data_create datetime,
                         data_edt datetime,
                         best_before date,
                         login_id int,
                         login_edt_id int,
                         mast_edt enum('yes','no') NOT NULL default 'no',
                         korzina int(1),
                         mail_form varchar(100),
                         mail_pass_form varchar(100),
                         pic_tr tinytext,
                         dom_form varchar(100),
                         dom_pass_form varchar(100),
                         KEY pas_str_id_ind (pas_str_id)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result89 = mysql_query($query89);
     if(!$result89) puterror($TEXT['error-tbl52']);

    $query90 = "CREATE TABLE tbl_p_tmp(
                         ptmp_id int unsigned NOT NULL auto_increment primary key,
                         usr_id int,
                         p_tmp varchar(255),
                         stat int(1)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result90 = mysql_query($query90);
     if(!$result90) puterror($TEXT['error-tbl53']);

    $query111 = "CREATE TABLE aliases(
                          local_part varchar(64) NOT NULL,
                          domain varchar(200) NOT NULL,
                          recipients text(2000),
                          PRIMARY KEY (local_part,domain)
                          )ENGINE=MyISAM
                          CHARACTER SET utf8;";
    $result111 = mysql_query($query111);
     if(!$result111) puterror($TEXT['error-tbl32']);

    $query112 = "CREATE TABLE blacklists(
                          address varchar(64) NOT NULL,
                          type enum('SENDER','HOSTREJ','REJRCPT') NOT NULL default 'SENDER',
                          message varchar(255),
                          PRIMARY KEY (address,type)
                          )ENGINE=MyISAM
                          CHARACTER SET utf8;";
    $result112 = mysql_query($query112);
     if(!$result112) puterror($TEXT['error-tbl28']);

    $query113 = "CREATE TABLE callbackdomains(
                          domain varchar(200) NOT NULL,
                          type enum('NOENV','NOHDR','NORND','NODNS') NOT NULL default 'NOENV',
                          PRIMARY KEY (domain,type)
                          )ENGINE=MyISAM
                          CHARACTER SET utf8;";
    $result113 = mysql_query($query113);
     if(!$result113) puterror($TEXT['error-tbl31']);

    $query114 = "CREATE TABLE domains(
                          domain varchar(200) NOT NULL,
                          type enum('LOCAL','RELAY','VIRTUAL') default 'LOCAL',
                          PRIMARY KEY (domain,type)
                          )ENGINE=MyISAM
                          CHARACTER SET utf8;";
    $result114 = mysql_query($query114);
     if(!$result114) puterror($TEXT['error-tbl27']);

    $query115 = "CREATE TABLE hostlists(
                          hostname varchar(64) NOT NULL,
                          type enum('AVOIDTLS','TRYAUTH','NOCALL') NOT NULL default 'AVOIDTLS',
                          PRIMARY KEY (hostname,type)
                          )ENGINE=MyISAM
                          CHARACTER SET utf8;";
    $result115 = mysql_query($query115);
     if(!$result115) puterror($TEXT['error-tbl30']);

    $query116 = "CREATE TABLE userforward(
                          local_part varchar(64) NOT NULL,
                          domain varchar(200) NOT NULL,
                          recipients text(2000),
                          PRIMARY KEY (local_part,domain)
                          )ENGINE=MyISAM
                          CHARACTER SET utf8;";
    $result116 = mysql_query($query116);
     if(!$result116) puterror($TEXT['error-tbl33']);

    $query117 = "CREATE TABLE users(
                          id varchar(200) NOT NULL,
                          name varchar(255),
                          user_id int NOT NULL,
                          crypt varchar(255),
                          passwd varchar(255),
                          uid int(10) NOT NULL,
                          gid int(10) NOT NULL,
                          mbox_host varchar(128) NOT NULL,
                          shell varchar(32) NOT NULL,
                          home varchar(128) NOT NULL,
                          quota int(7),
                          active enum('Y','N') NOT NULL default 'Y',
                          PRIMARY KEY (id,mbox_host)
                          )ENGINE=MyISAM
                          CHARACTER SET utf8;";
    $result117 = mysql_query($query117);
     if(!$result117) puterror($TEXT['error-tbl34']);

    $query118 = "CREATE TABLE whitelist(
                          address varchar(64) NOT NULL,
                          PRIMARY KEY (address)
                          )ENGINE=MyISAM
                          CHARACTER SET utf8;";
    $result118 = mysql_query($query118);
     if(!$result118) puterror($TEXT['error-tbl29']);

    $query119 = "CREATE TABLE tbl_sizes(
      			size_id int(2) unsigned NOT NULL auto_increment primary key,
      			size_name varchar(100)
     			 )ENGINE=MyISAM
      			CHARACTER SET utf8;";
    $result119 = mysql_query($query119);
     if(!$result119) puterror($TEXT['error-tbl35']);

//Дамп таблиц
$query_dump = mysql_query("INSERT INTO `tbl_organizaciya` VALUES (1, '-', 1, 'hide');");
$query_dump = mysql_query("INSERT INTO `tbl_otdel` VALUES (1, '-', 1, 'hide', '');");
$query_dump = mysql_query("INSERT INTO `tbl_sizes` VALUES (1, 'Байт'),(2, 'Килобайт'),(3, 'Мегабайт'),(4, 'Гигабайт'),(5, 'Терабайт');");
$query_dump = mysql_query("INSERT INTO `tbl_upravlenie` VALUES (1, '-', 1, 'hide');");
$query_dump = mysql_query("INSERT INTO `tbl_house` VALUES (1, '-');");
$query_dump = mysql_query("INSERT INTO `tbl_supp_sostoyanie` VALUES (1, 'Новое','../photo/sost_Novoe.png','1'),(2, 'Рассматривается','../photo/sost_Rassmatrivaetsya.png','2'),(3, 'В работе','../photo/sost_V_rabote.png','3'),(4, 'На доработку','../photo/sost_Na_dorabotku.png','4'),(5, 'Перенаправить','../photo/sost_Perenapravit.png','5'),(6, 'Напоминания','../photo/sost_Napominaniya.png','6'),(7, 'Завершено','../photo/sost_Zaversheno.png','7');");
$query_dump = mysql_query("INSERT INTO `tbl_supp_srochnost` VALUES (1, 'Низкая','../photo/vazh_Nizkaya.png','1'),(2, 'Средняя','../photo/vazh_Srednyaya.png','2'),(3, 'Высокая','../photo/vazh_Vysokaya.png','3'),(4, 'Срочно','../photo/vazh_Srochno.png','4');");
$query_dump = mysql_query("INSERT INTO `tbl_kritichnost` VALUES (1,'Для информации','../photo/krit_Dlya_informatsii_20100618114949.png',1),(2,'Не критично','../photo/krit_Ne_kritichno_20100618123543.png',2),(3,'Средне','../photo/krit_Sredne_20100618124557.png',3),(4,'Критично','../photo/krit_Kritichno_20100618123452.png',4);");
$query_dump = mysql_query("INSERT INTO `tbl_logins` VALUES ('1','admin','administrator','21232f297a57a5a743894a0e4a801fc3','0','1','0','0','0','0','0','no'),('2','robot','Robot','d5d105353066809420558caa5f2e0a04','0','0','0','0','1','0','0','no');");
$query_dump = mysql_query("INSERT INTO `tbl_tip_resurs` VALUES (1, 'HTTP','../photo/tip_inet.png',1,'http'),(2, 'HTTPS','../photo/tip_inets.png',2,'https'),(3, 'FTP','../photo/tip_ftp.png',3,'ftp'),(4, 'SMB','../photo/tip_smb.png',4,'smb'),(5, 'NFS','../photo/tip_nfs.png',5,'nfs'),(6, 'Ящик рассылки','../photo/tip_mailbox.png',6,'mailto'),(7, 'Сетевая папка','../photo/tip_net.png',7,'');");
$query_dump = mysql_query("INSERT INTO `tbl_srv_param` VALUES (NULL,'0','ou=people,dc=mycompany,dc=local','cn=admin,dc=mycompany,dc=local','123456','10.10.10.10','389','/sbin/nologin','8','12','/home/vmail/domains','512','15','root@localhost','root@localhost','root@localhost','12','1024','768','500','230','550','310','700','530','0','0','','no');");

$query_pwd = mysql_query("CREATE USER 'personal_user'@'%' IDENTIFIED BY 'personal_user';");
$query_pwd = mysql_query("CREATE USER 'personal_user'@'localhost' IDENTIFIED BY 'personal_user';");
$query_pwd = mysql_query("CREATE USER 'personal_admin'@'%' IDENTIFIED BY 'Pgt7d34smb9';");
$query_pwd = mysql_query("CREATE USER 'personal_admin'@'localhost' IDENTIFIED BY 'Pgt7d34smb9';");
$query_pwd = mysql_query("CREATE USER 'personal_exim'@'%' IDENTIFIED BY 'dFrt34@sOMiqzFF25';");
$query_pwd = mysql_query("CREATE USER 'personal_exim'@'localhost' IDENTIFIED BY 'dFrt34@sOMiqzFF25';");

$query_rights = mysql_query("GRANT SELECT ON personal.* TO personal_user@'%';");
$query_rights = mysql_query("GRANT SELECT ON personal.* TO personal_user@localhost;");
$query_rights = mysql_query("GRANT SELECT,INSERT ON personal.tbl_supp_archive TO personal_user@'%';");
$query_rights = mysql_query("GRANT SELECT,INSERT ON personal.tbl_supp_archive TO personal_user@localhost;");
$query_rights = mysql_query("GRANT SELECT,INSERT,UPDATE,DELETE ON personal.tbl_supp_zayavka TO personal_user@'%';");
$query_rights = mysql_query("GRANT SELECT,INSERT,UPDATE,DELETE ON personal.tbl_supp_zayavka TO personal_user@localhost;");
$query_rights = mysql_query("GRANT SELECT,INSERT,UPDATE,DELETE ON personal.tbl_ob_zay TO personal_user@'%';");
$query_rights = mysql_query("GRANT SELECT,INSERT,UPDATE,DELETE ON personal.tbl_ob_zay TO personal_user@localhost;");
$query_rights = mysql_query("GRANT SELECT,INSERT,UPDATE,DELETE ON personal.tbl_tmp_ofc TO personal_user@'%';");
$query_rights = mysql_query("GRANT SELECT,INSERT,UPDATE,DELETE ON personal.tbl_tmp_ofc TO personal_user@localhost;");
$query_rights = mysql_query("GRANT SELECT,INSERT ON personal.tbl_logs TO personal_user@'%';");
$query_rights = mysql_query("GRANT SELECT,INSERT ON personal.tbl_logs TO personal_user@localhost;");
$query_rights = mysql_query("GRANT SELECT,INSERT,UPDATE,DELETE,LOCK TABLES ON personal.* TO personal_admin@'%';");
$query_rights = mysql_query("GRANT SELECT,INSERT,UPDATE,DELETE,LOCK TABLES ON personal.* TO personal_admin@localhost;");
$query_rights = mysql_query("GRANT SELECT ON personal.aliases TO personal_exim@'%';");
$query_rights = mysql_query("GRANT SELECT ON personal.blacklists TO personal_exim@'%';");
$query_rights = mysql_query("GRANT SELECT ON personal.callbackdomains TO personal_exim@'%';");
$query_rights = mysql_query("GRANT SELECT ON personal.domains TO personal_exim@'%';");
$query_rights = mysql_query("GRANT SELECT ON personal.hostlists TO personal_exim@'%';");
$query_rights = mysql_query("GRANT SELECT ON personal.userforward TO personal_exim@'%';");
$query_rights = mysql_query("GRANT SELECT ON personal.users TO personal_exim@'%';");
$query_rights = mysql_query("GRANT SELECT ON personal.whitelist TO personal_exim@'%';");
$query_rights = mysql_query("GRANT SELECT ON personal.aliases TO personal_exim@localhost;");
$query_rights = mysql_query("GRANT SELECT ON personal.blacklists TO personal_exim@localhost;");
$query_rights = mysql_query("GRANT SELECT ON personal.callbackdomains TO personal_exim@localhost;");
$query_rights = mysql_query("GRANT SELECT ON personal.domains TO personal_exim@localhost;");
$query_rights = mysql_query("GRANT SELECT ON personal.hostlists TO personal_exim@localhost;");
$query_rights = mysql_query("GRANT SELECT ON personal.userforward TO personal_exim@localhost;");
$query_rights = mysql_query("GRANT SELECT ON personal.users TO personal_exim@localhost;");
$query_rights = mysql_query("GRANT SELECT ON personal.whitelist TO personal_exim@localhost;");
$query_rights = mysql_query("GRANT SELECT,INSERT,UPDATE,DELETE ON personal.tbl_zhurnal TO personal_user@'%';");
$query_rights = mysql_query("GRANT SELECT,INSERT,UPDATE,DELETE ON personal.tbl_zhurnal TO personal_user@localhost;");
$query_rights = mysql_query("GRANT SELECT,UPDATE ON personal.tbl_instructions TO personal_user@'%';");
$query_rights = mysql_query("GRANT SELECT,UPDATE ON personal.tbl_instructions TO personal_user@localhost;");
$query_rights = mysql_query("GRANT SELECT,UPDATE ON personal.tbl_documents TO personal_user@'%';");
$query_rights = mysql_query("GRANT SELECT,UPDATE ON personal.tbl_documents TO personal_user@localhost;");
$query_rights = mysql_query("GRANT SELECT,INSERT,UPDATE,DELETE ON personal.tbl_bd_zhnaniy TO personal_user@'%';");
$query_rights = mysql_query("GRANT SELECT,INSERT,UPDATE,DELETE ON personal.tbl_bd_zhnaniy TO personal_user@localhost;");
$query_rights = mysql_query("GRANT SELECT,INSERT,UPDATE,DELETE ON personal.tbl_know_prim TO personal_user@'%';");
$query_rights = mysql_query("GRANT SELECT,INSERT,UPDATE,DELETE ON personal.tbl_know_prim TO personal_user@localhost;");
$query_rights = mysql_query("GRANT SELECT,INSERT,UPDATE,DELETE ON personal.tbl_meet_event TO personal_user@'%';");
$query_rights = mysql_query("GRANT SELECT,INSERT,UPDATE,DELETE ON personal.tbl_meet_event TO personal_user@localhost;");
$query_rights = mysql_query("GRANT SELECT,INSERT,UPDATE,DELETE ON personal.tbl_meet_users TO personal_user@'%';");
$query_rights = mysql_query("GRANT SELECT,INSERT,UPDATE,DELETE ON personal.tbl_meet_users TO personal_user@localhost;");
$query_rights = mysql_query("GRANT SELECT,INSERT,UPDATE,DELETE ON personal.tbl_pass_strukt TO personal_user@'%';");
$query_rights = mysql_query("GRANT SELECT,INSERT,UPDATE,DELETE ON personal.tbl_pass_strukt TO personal_user@localhost;");
$query_rights = mysql_query("GRANT SELECT,INSERT,UPDATE,DELETE ON personal.tbl_pass_keep TO personal_user@'%';");
$query_rights = mysql_query("GRANT SELECT,INSERT,UPDATE,DELETE ON personal.tbl_pass_keep TO personal_user@localhost;");
$query_rights = mysql_query("GRANT SELECT,INSERT,UPDATE,DELETE ON personal.tbl_p_tmp TO personal_user@'%';");
$query_rights = mysql_query("GRANT SELECT,INSERT,UPDATE,DELETE ON personal.tbl_p_tmp TO personal_user@localhost;");
$query_rights = mysql_query("GRANT SELECT ON personal.tbl_meet_room TO personal_user@'%';");
$query_rights = mysql_query("GRANT SELECT ON personal.tbl_meet_room TO personal_user@localhost;");
$query_rights = mysql_query("GRANT INSERT ON personal.tbl_users TO personal_user@'%';");
$query_rights = mysql_query("GRANT INSERT ON personal.tbl_users TO personal_user@localhost;");

  $query120 = "FLUSH PRIVILEGES;";
    $result120 = mysql_query($query120);
     if(!$result120) puterror($TEXT['error-install17']);

  $query_shedule_enbl = mysql_query("SET GLOBAL event_scheduler = ON;");
  $query_shedule = mysql_query("delimiter |
                                                   CREATE EVENT `clear_p_tmp`
                                                   ON SCHEDULE
                                                   EVERY 1 DAY STARTS '2012-09-12 23:11:00'
                                                   ON COMPLETION PRESERVE
                                                   COMMENT 'Clear temp table'
                                                   DO
                                                   BEGIN
                                                   DELETE FROM tbl_p_tmp WHERE p_tmp != '';
                                                   UPDATE tbl_pass_keep SET korzina = 1, data_edt = NOW(), login_edt_id = (SELECT login_id FROM tbl_logins WHERE fullname = 'Robot' LIMIT 1) WHERE best_before < NOW() AND best_before != '0000-00-00' AND korzina = 0;
                                                   DELETE FROM tbl_pass_keep WHERE korzina = 1 AND data_edt < DATE_ADD(NOW(), INTERVAL -30 DAY);
                                                   END |
                                                   delimiter ;");

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

  function puterror($message)
  {
    echo("<h2><p align='center'>".$message."<br />".mysql_error()."</p></h2>");
    exit();
  }
  require_once "utils/bottom.php";
?>