<?php
  ///////////////////////////////////////////////////
  //Телефонный справочник,
  //обновление
  //2009 (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_update.php";

  $title=$titlepage=$TEXT['update-title'];
  $helppage = $TEXT['update-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'])) puterror($TEXT['error-update1']);
  if(strlen($_POST['dbname'])>50) puterror($TEXT['error-update2']);
  if(strlen($_POST['vers'])>10) puterror($TEXT['search-err3']);

  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-update3']);

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

  ///////////////////////////////////////////////////
  // Подключение к базе данных
  ///////////////////////////////////////////////////

  $dblocation = $_POST['srv'];
  $dbport = "3306";
  $dbname = $_POST['dbname'];
  $dbuser = $_POST['usr'];
  $dbpasswd = $_POST['pswd'];

  $dbcnx = @mysql_connect($dblocation,$dbuser,$dbpasswd);
  if(!$dbcnx)
  {
    echo( "".$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');
  
  if(! @mysql_select_db($dbname,$dbcnx) )
  {
    echo( "".$TEXT['error-conf2']."" );
    exit();
  }

  $action = "215";
    switch($_POST['vers'])
      {
      case 1: //2.0.5-2.1.5
      $action = "215";
      break;
      case 2: //2.1.5-2.2.5
      $action = "225";
      break;
      case 3: //2.2.5-2.3.5
      $action = "235";
      break;
      case 4: //2.3.5-2.4.5
      $action = "245";
      break;
      case 5: //2.4.5-2.5.5
      $action = "255";
      break;
      case 6: //2.5.5-2.6.5
      $action = "265";
      break;
      case 7: //2.6.5-2.7.5
      $action = "275";
      break;
      }

if($action == "215")
 {
  $query2 = "ALTER TABLE `tbl_supp_tema` ADD `shablon` VARCHAR(255) AFTER `position`;";
    $result2 = mysql_query($query2);
     if (!$result2) puterror ($TEXT['error-tbl22']);

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

  $query4 = "ALTER TABLE `tbl_supp_zayavka` ADD `ob_type` enum('0','1','2') NOT NULL default '0' AFTER `org_id`;";
    $result4 = mysql_query($query4);
     if (!$result4) puterror($TEXT['error-tbl26']);

  $query41 = "ALTER TABLE `tbl_resource` ADD `photo` VARCHAR(255) AFTER `bakprog`;";
    $result41 = mysql_query($query41);
     if (!$result41) puterror($TEXT['error-tbl16']);

  $query5 = "CREATE INDEX ind_root_zay ON tbl_ob_zay (root_zay_id);";
    $result5 = mysql_query($query5);
     if (!$result5) puterror($TEXT['error-install36']);

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

  //*************************************
  //Информация для лога
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['update-log'];
  $what = $TEXT['update-log1'];
  $who = $_POST['usr'];
  //*************************************

    $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
        if(mysql_query($query2))
           {
             echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=admin/index.php' />
                   </head></html>";
           } else puterror($TEXT['error-log']);
 }
  //else puterror($TEXT['error-update4']);

if($action == "225")
 {
 $query2 = "DROP TABLE IF EXISTS `system_accounts`,
 `powercounter_arch_clients`,
 `powercounter_arch_clients_month`,
 `powercounter_arch_clients_week`,
 `powercounter_arch_deep`,
 `powercounter_arch_deep_month`,
 `powercounter_arch_deep_week`,
 `powercounter_arch_enterpoint`,
 `powercounter_arch_enterpoint_month`,
 `powercounter_arch_enterpoint_week`,
 `powercounter_arch_hits`,
 `powercounter_arch_hits_month`,
 `powercounter_arch_hits_week`,
 `powercounter_arch_ip`,
 `powercounter_arch_ip_month`,
 `powercounter_arch_ip_week`,
 `powercounter_arch_num_searchquery`,
 `powercounter_arch_num_searchquery_month`,
 `powercounter_arch_num_searchquery_week`,
 `powercounter_arch_refferer`,
 `powercounter_arch_refferer_month`,
 `powercounter_arch_refferer_week`,
 `powercounter_arch_robots`,
 `powercounter_arch_robots_month`,
 `powercounter_arch_robots_week`,
 `powercounter_arch_searchquery`,
 `powercounter_arch_searchquery_month`,
 `powercounter_arch_searchquery_week`,
 `powercounter_arch_time`,
 `powercounter_arch_time_month`,
 `powercounter_arch_time_temp`,
 `powercounter_arch_time_week`,
 `powercounter_cities`,
 `powercounter_ip`,
 `powercounter_ip_compact`,
 `powercounter_pages`,
 `powercounter_refferer`,
 `powercounter_regions`,
 `powercounter_searchquerys`,
 `powercounter_thits`,
 `powercounter_links`;";
    $result2 = mysql_query($query2);
     if(!$result2) puterror ($TEXT['error-tbl22']);

  $query3 = "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)
                         )TYPE=MyISAM
                         CHARACTER SET utf8;";
    $result3 = mysql_query($query3);
     if(!$result3) puterror($TEXT['error-tbl37']);

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

  //*************************************
  //Информация для лога
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['update-log'];
  $what = $TEXT['update-log2'];
  $who = $_POST['usr'];
  //*************************************

    $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
        if(mysql_query($query2))
           {
             echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=admin/index.php' />
                   </head></html>";
           } else puterror($TEXT['error-log']);
 } 
 //else puterror($TEXT['error-update4']);

if($action == "235")
 {
  $query3 = "CREATE TABLE tbl_ondisk(
                         ond_id int(5) unsigned NOT NULL auto_increment primary key,
                         disk_name varchar(255) COLLATE utf8_general_ci,
                         mp varchar(255) COLLATE utf8_general_ci
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result3 = mysql_query($query3);
     if(!$result3) puterror($TEXT['error-tbl38']);

    $query4 = "ALTER TABLE `tbl_supp_zayavka` MODIFY COLUMN `ob_type` ENUM('0','1','2','3') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 0;";
    $result4 = mysql_query($query4);
     if(!$result4) puterror($TEXT['error-tbl26']);

   $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;");

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

  //*************************************
  //Информация для лога
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['update-log'];
  $what = $TEXT['update-log3'];
  $who = $_POST['usr'];
  //*************************************
    $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
        if(mysql_query($query2))
           {
             echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=admin/index.php' />
                   </head></html>";
           } else puterror($TEXT['error-log']);
 }
 //else puterror($TEXT['error-update4']);

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

  $query5 = "CREATE TABLE tbl_zhurn_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;";
    $result5 = mysql_query($query5);
     if(!$result5) puterror($TEXT['error-tbl40']);

  $query6 = "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;";
    $result6 = mysql_query($query6);
     if(!$result6) puterror($TEXT['error-tbl41']);

  $query7 = "ALTER TABLE `tbl_otdel` ADD COLUMN `phone` VARCHAR(20) CHARACTER SET utf8 COLLATE utf8_general_ci AFTER `visible`;";
    $result7 = mysql_query($query7);
     if(!$result7) puterror($TEXT['error-tbl9']);

  $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_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;");

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

  //*************************************
  //Информация для лога
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['update-log'];
  $what = $TEXT['update-log4'];
  $who = $_POST['usr'];
  //*************************************
    $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
        if(mysql_query($query2))
           {
             echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=admin/index.php' />
                   </head></html>";
           } else puterror($TEXT['error-log']);
 }
 //else puterror($TEXT['error-update4']);

if($action == "255")
 {
  $query3 = "ALTER TABLE `tbl_logins` ADD COLUMN `otvetstvenniy` ENUM('0','1') NOT NULL DEFAULT '0' AFTER `master`, 
      ADD COLUMN `chk_upd` enum('yes','no') DEFAULT 'no' AFTER `usr_id`;";
    $result3 = mysql_query($query3);
     if(!$result3) puterror($TEXT['error-tbl20']);

  $query4 = "ALTER TABLE `tbl_users`
        ADD INDEX `ind_dom_id` (`dom_id` ASC),
        ADD INDEX `ind_dol_id` (`dol_id` ASC),
        ADD INDEX `ind_otd_id` (`otd_id` ASC),
        ADD INDEX `ind_upr_id` (`upr_id` ASC),
        ADD INDEX `ind_org_id` (`org_id` ASC),
        ADD INDEX `ind_house_id` (`house_id` ASC),
        ADD INDEX `ind_str_id` (`str_id` ASC),
        ADD INDEX `ind_city_id` (`city_id` ASC);";
    $result4 = mysql_query($query4);
     if(!$result4) puterror($TEXT['error-tbl2']);

  $query5 = "ALTER TABLE `tbl_supp_tema` ADD COLUMN `visible` ENUM('show','hide') NOT NULL DEFAULT 'show' AFTER `shablon`;";
    $result5 = mysql_query($query5);
     if(!$result5) puterror($TEXT['error-tbl40']);

  $query6 = "ALTER TABLE `tbl_supp_zayavka` ADD COLUMN `otv` enum('0','1') DEFAULT '0' AFTER `ob_type`,
             ADD COLUMN `mar_id` int DEFAULT '0' AFTER `otv`,
             ADD COLUMN `step` smallint DEFAULT '0' AFTER `mar_id`,
             ADD COLUMN `max_step` smallint DEFAULT '0' AFTER `step`,
             ADD COLUMN `parent` int DEFAULT '0' AFTER `max_step`;";
    $result6 = mysql_query($query6);
     if(!$result6) puterror($TEXT['error-tbl26']);

  $query7 = "ALTER TABLE `tbl_supp_archive` ADD COLUMN `problema` TEXT NOT NULL AFTER `dy`,
             ADD COLUMN `pic_tr` TINYTEXT NOT NULL AFTER `problema`,
             ADD COLUMN `reshenie` TEXT NOT NULL AFTER `pic_tr`,
             ADD COLUMN `mar_id` INT NOT NULL DEFAULT 0 AFTER `reshenie`,
             ADD COLUMN `step` SMALLINT NOT NULL DEFAULT 0 AFTER `mar_id`,
             ADD COLUMN `parent` INT NOT NULL DEFAULT 0 AFTER `step`,
             ADD INDEX ind_parent(parent),
             ADD INDEX ind_step(step),
             ADD INDEX ind_mar(mar_id);";
    $result7 = mysql_query($query7);
     if(!$result7) puterror($TEXT['error-tbl25']);

  $query8 = "ALTER TABLE tbl_supp_zayavka MODIFY COLUMN `ob_type` ENUM('0','1','2','3','4','5') NOT NULL DEFAULT 0;";
    $result8 = mysql_query($query8);
     if(!$result8) puterror($TEXT['error-tbl26']);

  $query9 = "ALTER TABLE `tbl_supp_tema` MODIFY COLUMN `visible` ENUM('show','hide','it') DEFAULT 'show';";
    $result9 = mysql_query($query9);
     if(!$result9) puterror($TEXT['error-tbl22']);

  $query9 = "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;";
    $result9 = mysql_query($query9);
     if(!$result9) puterror($TEXT['error-tbl42']);

  $query10 = "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;";
    $result10 = mysql_query($query10);
     if(!$result10) puterror($TEXT['error-tbl43']);

  $query11 = "ALTER TABLE `tbl_instructions` ADD COLUMN `count_view` int NOT NULL DEFAULT 0 AFTER `razd_id`;";
    $result11 = mysql_query($query11);
     if(!$result11) puterror($TEXT['error-tbl18']);

  $query12 = "ALTER TABLE `tbl_documents` ADD COLUMN `count_view` int NOT NULL DEFAULT 0 AFTER `razd_id`;";
    $result12 = mysql_query($query12);
     if(!$result12) puterror($TEXT['error-tbl14']);

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

  $query14 = "ALTER TABLE `tbl_resource` ADD COLUMN `tip_id` INT NOT NULL AFTER `photo`, ADD COLUMN `res_name` VARCHAR(255) NOT NULL AFTER `tip_id`, ADD COLUMN `size_now` INT NOT NULL AFTER `res_name`;";
    $result14 = mysql_query($query14);
     if(!$result14) puterror($TEXT['error-tbl16']);

  $query15 = "ALTER TABLE `users` MODIFY COLUMN `quota` INT(7) DEFAULT NULL;";
    $result15 = mysql_query($query15);
     if(!$result15) puterror($TEXT['error-tbl34']);

  $query16 = "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;";
    $result16 = mysql_query($query16);
     if(!$result16) puterror($TEXT['error-tbl44']);

  $query17 = "CREATE TABLE tbl_zhnaniy_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;";
    $result17 = mysql_query($query17);
     if(!$result17) puterror($TEXT['error-tbl45']);

  $query18 = "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;";
    $result18 = mysql_query($query18);
     if(!$result18) puterror($TEXT['error-tbl46']);

  $query19 = "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;";
    $result19 = mysql_query($query19);
     if(!$result19) puterror($TEXT['error-tbl47']);

    $query20 = "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;";
    $result20 = mysql_query($query20);
      if(!$result20) puterror(mysql_error());

    $query21 = "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;";
    $result21 = mysql_query($query21);
      if(!$result21) puterror(mysql_error());

    $query22 = "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;";
    $result22 = mysql_query($query22);
      if(!$result22) puterror(mysql_error());

    $query23 = "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;";
    $result23 = mysql_query($query23);
      if(!$result23) puterror(mysql_error());

    $query24 = "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;";
    $result24 = mysql_query($query24);
      if(!$result24) puterror(mysql_error());

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

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

    $query27 = "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;";
    $result27 = mysql_query($query27);
      if(!$result27) puterror(mysql_error());

    $query28 = "DROP TABLE IF EXISTS `tbl_start`;";
    $result28 = mysql_query($query28);

  $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_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,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_dump = mysql_query("INSERT INTO `tbl_logins` VALUES (NULL,'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','172.30.1.83','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');");

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

  //*************************************
  //Информация для лога
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['update-log'];
  $what = $TEXT['update-log5'];
  $who = $_POST['usr'];
  //*************************************
    $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
        if(mysql_query($query2))
           {
             echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=admin/index.php' />
                   </head></html>";
           } else puterror($TEXT['error-log']);
 }
 //else puterror($TEXT['error-update4']);

if($action == "265")
 {
  $query3 = "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;";
    $result3 = mysql_query($query3);
     if(!$result3) puterror($TEXT['error-tbl48']);

    $query4 = "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;";
    $result4 = mysql_query($query4);
     if(!$result4) puterror($TEXT['error-tbl49']);

  $query5 = "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;";
    $result5 = mysql_query($query5);
     if(!$result5) 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']);

  $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 ;");

  $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;");

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

  //*************************************
  //Информация для лога
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['update-log'];
  $what = $TEXT['update-log6'];
  $who = $_POST['usr'];
  //*************************************
    $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
        if(mysql_query($query2))
           {
             echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=admin/index.php' />
                   </head></html>";
           } else puterror($TEXT['error-log']);
 }
//else puterror($TEXT['error-update4']);

if($action == "275")
 {
  $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;");

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

  //*************************************
  //Информация для лога
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['update-log'];
  $what = $TEXT['update-log7'];
  $who = $_POST['usr'];
  //*************************************
    $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
        if(mysql_query($query2))
           {
             echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=admin/index.php' />
                   </head></html>";
           } else puterror($TEXT['error-log']);
 } else puterror($TEXT['error-update4']);

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