<?php
global $config, $sql_global_link, $sql_db_type, $channel_tables;
require_once $config['site_path'] . "/lang/" . $config["lang"] . ".php";

if ($link_identifier == 0) {
    $link_identifier = $sql_global_link;
}
if ($sql_db_type == "mysql") {
    $db_name = "table_schema";
}
if ($sql_db_type == "postgresql") {
    $db_name = "table_catalog";
}
$checks = 0;
$res = run_query("SELECT * FROM information_schema.tables WHERE $db_name='" . $config['dbname'] . "' AND table_name='" . $config['table_prefix'] . "logs_subtable'");
if (sql_num_rows($res)) {
    $check = run_query("SELECT id FROM " . $config['table_prefix'] . "logs_subtable");
    if (!sql_num_rows($check)) {
        $checks = 1;
    }
} else {
    $checks = 1;
}
if ($checks) {
    $cat_row = array();
    $check = run_query(
        "SELECT " . $config['table_prefix'] . "cats.id AS catsid," . $config['table_prefix'] . "addons.id AS addonsid
        FROM " . $config['table_prefix'] . "cats 
        INNER JOIN " . $config['table_prefix'] . "addons ON " . $config['table_prefix'] . "cats.name=" . $config['table_prefix'] . "addons.name 
        WHERE " . $config['table_prefix'] . "cats.name='" . $lang['log_ras_cat'] . "'");
    if (!sql_num_rows($check)) {
        $check2 = run_query("SELECT id FROM " . $config['table_prefix'] . "cats  WHERE name='" . $lang['log_ras_cat'] . "' ORDER BY id DESC LIMIT 1");
        if (!sql_num_rows($check2)) {
            run_query("INSERT INTO " . $config['table_prefix'] . "cats(name,owner_id) VALUES ('" . $lang['log_ras_cat'] . "','1')");
            $cat = run_query("SELECT id FROM " . $config['table_prefix'] . "cats ORDER BY id DESC LIMIT 1");
            $cat_row = sql_fetch_array($cat);
        }
        $cat_row = sql_fetch_array($check2);
    }
    if(empty($cat_row)){
        $cat_row = sql_fetch_array($check);
        $cat_row['id'] = $cat_row['catsid'];
    }
    run_query("INSERT INTO " . $config['table_prefix'] . "addons(name,inactive) VALUES ('" . $lang['log_ras_cat'] . "','1')");
    $addon = run_query("SELECT id FROM " . $config['table_prefix'] . "addons ORDER BY id DESC LIMIT 1");
    $addon_row = sql_fetch_array($addon);
    $res = run_query("SELECT id FROM " . $config['table_prefix'] . "cats ORDER BY id DESC LIMIT 1");
    $row = sql_fetch_array($res);
    $check = run_query("SELECT id FROM " . $config['table_prefix'] . "tables  WHERE cat_id=" . $row['id'] . " AND name_table='" . $lang['log_ras'] . "'");
    if (!sql_num_rows($check)) {
        run_query("INSERT INTO " . $config['table_prefix'] . "tables (cat_id,name_table,full_name,def_sort,lop,slop,fix_search_submit,owner_id,"
            . "auto_entered_field)  VALUES ('" . $row['id'] . "', '" . $lang['log_ras'] . "', '" . $lang['log_ras'] . "','`id` desc','100','100','1','1','1')");
        $res1 = run_query("SELECT id FROM " . $config['table_prefix'] . "tables ORDER BY id DESC LIMIT 1");
        $row1 = sql_fetch_array($res1);
        $new_id = $row1['id']+10;
        run_query("UPDATE " . $config["table_prefix"] . "tables SET id = '" . $new_id . "' WHERE id=" . $row1['id']);
    }
    $check = run_query("SELECT cat_id FROM " . $config['table_prefix'] . "acc_cats  WHERE group_id='1' AND cat_id=" . $row['id']);
    if (!sql_num_rows($check)) {
        run_query("INSERT INTO " . $config['table_prefix'] . "acc_cats(cat_id,group_id,access) VALUES ('" . $row['id'] . "','1','2')");
    }
    $check = run_query("SELECT cat_id FROM " . $config['table_prefix'] . "acc_cats  WHERE group_id='2' AND cat_id=" . $row['id']);
    if (!sql_num_rows($check)) {
        run_query("INSERT INTO " . $config['table_prefix'] . "acc_cats(cat_id,group_id,access) VALUES ('" . $row['id'] . "','2','2')");
    }
    run_query("UPDATE " . $config['table_prefix'] . "acc_cats SET access='2' WHERE cat_id=" . $row['id']);
    $res = run_query("SELECT id FROM " . $config['table_prefix'] . "tables ORDER BY id DESC LIMIT 1");
    $row = sql_fetch_array($res);
    run_query("INSERT INTO " . $config['table_prefix'] . "filters(table_id,num) VALUES ('" . $row['id'] . "','1')");
    $check = run_query("SELECT id FROM " . $config['table_prefix'] . "acc_tables  WHERE group_id='1' AND table_id=" . $row['id']);
    if (!sql_num_rows($check)) {
        run_query("INSERT INTO " . $config['table_prefix'] . "acc_tables(table_id,group_id,acc,vis_acc,arc_acc) VALUES ('" . $row['id'] . "','1','1','1','1')");
    }
    $check = run_query("SELECT id FROM " . $config['table_prefix'] . "acc_tables  WHERE group_id='2' AND table_id=" . $row['id']);
    if (!sql_num_rows($check)) {
        run_query("INSERT INTO " . $config['table_prefix'] . "acc_tables(table_id,group_id,acc,vis_acc,arc_acc) VALUES ('" . $row['id'] . "','2','1','1','1')");
    }
    $check = run_query("SELECT id FROM " . $config['table_prefix'] . "fields  WHERE name_field='ID' AND table_id=" . $row['id']);
    if (!sql_num_rows($check)) {
        run_query("INSERT INTO " . $config['table_prefix'] . "fields(name_field,table_id,field_num,type_field,main,uniq_field,reduce) VALUES ('ID','" . $row['id'] . "','1','10','1','1','46')");
    }
    $check = run_query("SELECT id FROM " . $config['table_prefix'] . "fields  WHERE name_field='" . $lang['added_by'] . "' AND table_id=" . $row['id']);
    if (!sql_num_rows($check)) {
        run_query("INSERT INTO " . $config['table_prefix'] . "fields(name_field,table_id,field_num,type_field,default_value,reduce) VALUES ('" . $lang['added_by'] . "','" . $row['id'] . "','2','11','{current}','46')");
    }
    $check = run_query("SELECT id FROM " . $config['table_prefix'] . "fields  WHERE name_field='" . $lang['addition_time'] . "' AND table_id=" . $row['id']);
    if (!sql_num_rows($check)) {
        run_query("INSERT INTO " . $config['table_prefix'] . "fields(name_field,table_id,field_num,type_field,default_value,reduce) VALUES ('" . $lang['addition_time'] . "','" . $row['id'] . "','3','12','{cur.time}','46')");
    }
    $check = ("SELECT id FROM " . $config['table_prefix'] . "fields  WHERE name_field='" . $lang['record_status'] . "' AND table_id=" . $row['id']);
    if (!sql_num_rows($check)) {
        run_query("INSERT INTO " . $config['table_prefix'] . "fields(name_field,table_id,field_num,type_field,reduce) VALUES ('" . $lang['record_status'] . "','" . $row['id'] . "','4','13','46')");
    }
    $check = run_query("SELECT id FROM " . $config['table_prefix'] . "fields  WHERE name_field='" . $lang['log_ras_name'] . "' AND table_id=" . $row['id']);
    if (!sql_num_rows($check)) {
        run_query("INSERT INTO " . $config['table_prefix'] . "fields(name_field,table_id,field_num,type_field,reduce) VALUES ('" . $lang['log_ras_name'] . "','" . $row['id'] . "','5','3','46')");
        $res2 = run_query("SELECT id FROM " . $config['table_prefix'] . "fields ORDER BY id DESC LIMIT 1");
        $row2 = sql_fetch_array($res2);
    } else {
        $row2 = sql_fetch_array($check);
    }
    $check = run_query("SELECT id FROM " . $config['table_prefix'] . "fields  WHERE name_field='" . $lang['log_ras_result'] . "' AND table_id=" . $row['id']);
    if (!sql_num_rows($check)) {
        run_query("INSERT INTO " . $config['table_prefix'] . "fields(name_field,table_id,field_num,type_field,reduce) VALUES ('" . $lang['log_ras_result'] . "','" . $row['id'] . "','6','3','46')");
        $res3 = run_query("SELECT id FROM " . $config['table_prefix'] . "fields ORDER BY id DESC LIMIT 1");
        $row3 = sql_fetch_array($res3);
    } else {
        $row3 = sql_fetch_array($check);
    }
    $check = run_query("SELECT id FROM " . $config['table_prefix'] . "fields  WHERE name_field='" . $lang['log_ras_date'] . "' AND table_id=" . $row['id']);
    if (!sql_num_rows($check)) {
        run_query("INSERT INTO " . $config['table_prefix'] . "fields(name_field,table_id,field_num,type_field,default_value,reduce,type_value) VALUES ('" . $lang['log_ras_date'] . "','" . $row['id'] . "','7','2','{cur.date}','46','1')");
        $res4 = run_query("SELECT id FROM " . $config['table_prefix'] . "fields ORDER BY id DESC LIMIT 1");
        $row4 = sql_fetch_array($res4);
    } else {
        $row4 = sql_fetch_array($check);
    }
    $check = run_query("SELECT id FROM " . $config['table_prefix'] . "fields  WHERE name_field='" . $lang['log_ras_adress'] . "' AND table_id=" . $row['id']);
    if (!sql_num_rows($check)) {
        run_query("INSERT INTO " . $config['table_prefix'] . "fields(name_field,table_id,field_num,type_field,reduce) VALUES ('" . $lang['log_ras_adress'] . "','" . $row['id'] . "','8','3','46')");
        $res5 = run_query("SELECT id FROM " . $config['table_prefix'] . "fields ORDER BY id DESC LIMIT 1");
        $row5 = sql_fetch_array($res5);
    } else {
        $row5 = sql_fetch_array($check);
    }
    $check = run_query("SELECT id FROM " . $config['table_prefix'] . "acc_fields  WHERE group_id='1' AND field_id=" . $row2['id'] . " AND table_id=" . $row['id']);
    if (!sql_num_rows($check)) {
        run_query("INSERT INTO " . $config['table_prefix'] . "acc_fields(table_id,group_id,field_id,`view`,view_tb) VALUES ('" . $row['id'] . "','1','" . $row2['id'] . "','1','1')");
    }
    $check = run_query("SELECT id FROM " . $config['table_prefix'] . "acc_fields  WHERE group_id='2' AND field_id=" . $row2['id'] . " AND table_id=" . $row['id']);
    if (!sql_num_rows($check)) {
        run_query("INSERT INTO " . $config['table_prefix'] . "acc_fields(table_id,group_id,field_id,`view`,view_tb) VALUES ('" . $row['id'] . "','2','" . $row2['id'] . "','1','1')");
    }
    $check = ("SELECT id FROM " . $config['table_prefix'] . "acc_fields  WHERE group_id='1' AND field_id=" . $row3['id'] . " AND table_id=" . $row['id']);
    if (!sql_num_rows($check)) {
        run_query("INSERT INTO " . $config['table_prefix'] . "acc_fields(table_id,group_id,field_id,`view`,view_tb) VALUES ('" . $row['id'] . "','1','" . $row3['id'] . "','1','1')");
    }
    $check = run_query("SELECT id FROM " . $config['table_prefix'] . "acc_fields  WHERE group_id='2' AND field_id=" . $row3['id'] . " AND table_id=" . $row['id']);
    if (!sql_num_rows($check)) {
        run_query("INSERT INTO " . $config['table_prefix'] . "acc_fields(table_id,group_id,field_id,`view`,view_tb) VALUES ('" . $row['id'] . "','2','" . $row3['id'] . "','1','1')");
    }
    $check = run_query("SELECT id FROM " . $config['table_prefix'] . "acc_fields  WHERE group_id='1' AND field_id=" . $row4['id'] . " AND table_id=" . $row['id']);
    if (!sql_num_rows($check)) {
        run_query("INSERT INTO " . $config['table_prefix'] . "acc_fields(table_id,group_id,field_id,`view`,view_tb) VALUES ('" . $row['id'] . "','1','" . $row4['id'] . "','1','1')");
    }
    $check = run_query("SELECT id FROM " . $config['table_prefix'] . "acc_fields  WHERE group_id='2' AND field_id=" . $row4['id'] . " AND table_id=" . $row['id']);
    if (!sql_num_rows($check)) {
        run_query("INSERT INTO " . $config['table_prefix'] . "acc_fields(table_id,group_id,field_id,`view`,view_tb) VALUES ('" . $row['id'] . "','2','" . $row4['id'] . "','1','1')");
    }
    $check = run_query("SELECT id FROM " . $config['table_prefix'] . "acc_fields  WHERE group_id='1' AND field_id=" . $row5['id'] . " AND table_id=" . $row['id']);
    if (!sql_num_rows($check)) {
        run_query("INSERT INTO " . $config['table_prefix'] . "acc_fields(table_id,group_id,field_id,`view`,view_tb) VALUES ('" . $row['id'] . "','1','" . $row5['id'] . "','1','1')");
    }
    $check = ("SELECT id FROM " . $config['table_prefix'] . "acc_fields  WHERE group_id='2' AND field_id=" . $row5['id'] . " AND table_id=" . $row['id']);
    if (!sql_num_rows($check)) {
        run_query("INSERT INTO " . $config['table_prefix'] . "acc_fields(table_id,group_id,field_id,`view`,view_tb) VALUES ('" . $row['id'] . "','2','" . $row5['id'] . "','1','1')");
    }
    $check = run_query("SELECT id FROM " . $config['table_prefix'] . "addon_parts  WHERE addon_id=" . $addon_row['id'] . " AND part_id=" . $row['id']);
    if (!sql_num_rows($check)) {
        run_query("INSERT INTO " . $config['table_prefix'] . "addon_parts(addon_id,part_type,part_id) VALUES ('" . $addon_row['id'] . "','table','" . $row['id'] . "')");
    }
    $check = run_query("SELECT id FROM " . $config['table_prefix'] . "tips  WHERE table_id=" . $row['id']);
    if (!sql_num_rows($check)) {
        run_query("INSERT INTO " . $config['table_prefix'] . "tips(table_id,name,cond_value,message,bg_color,head_on,notify_on,auto_clean,notify_sound) VALUES ('" . $row['id'] . "','" . $lang['log_ras_unsubscribe'] . "','if a.`f" . $row3['id'] . "`=`UNSUBSCRIBE`','" . $lang['log_ras_unsub'] . " {" . $lang['log_ras_adress'] . "}','','1','1','1','sound1')");
    }
    $tip = run_query("SELECT id FROM " . $config['table_prefix'] . "tips ORDER BY id DESC LIMIT 1");
    $tips = sql_fetch_array($tip);
    $check = run_query("SELECT id FROM " . $config['table_prefix'] . "acc_tips  WHERE group_id='1' AND tip_id=" . $tips['id'] . " AND table_id=" . $row['id']);
    if (!sql_num_rows($check)) {
        run_query("INSERT INTO " . $config['table_prefix'] . "acc_tips(group_id,table_id,tip_id,access) VALUES('1','" . $row['id'] . "','" . $tips['id'] . "','1')");
    }
    $check = run_query("SELECT id FROM " . $config['table_prefix'] . "acc_tips  WHERE group_id='2' AND tip_id=" . $tips['id'] . " AND table_id=" . $row['id']);
    if (!sql_num_rows($check)) {
        run_query("INSERT INTO " . $config['table_prefix'] . "acc_tips(group_id,table_id,tip_id,access) VALUES('2','" . $row['id'] . "','" . $tips['id'] . "','1')");
    }
    run_query('CREATE TABLE IF NOT EXISTS ' . $config['table_prefix'] . 'data' . $row['id'] . " (
        id integer(11) NOT NULL AUTO_INCREMENT,
        user_id integer NOT NULL DEFAULT 0,
        add_time timestamp DEFAULT '0000-00-00 00:00:00',
        status TINYINT( 1 ) NULL DEFAULT '0',
        f" . $row2['id'] . " text character set utf8 collate utf8_general_ci NOT NULL,
        f" . $row3['id'] . " text character set utf8 collate utf8_general_ci NOT NULL,
        f" . $row4['id'] . " timestamp DEFAULT '0000-00-00 00:00:00',
        f" . $row5['id'] . " text character set utf8 collate utf8_general_ci NOT NULL,
        r TINYINT( 1 ) NULL DEFAULT '0',
        u TINYINT( 1 ) NULL DEFAULT '0',
        PRIMARY KEY (`id`),
        KEY `user_id` (`user_id`)
        ) ENGINE=" . $config['db_engine'] . " DEFAULT CHARSET=utf8");
    run_query("CREATE TABLE IF NOT EXISTS  `" . $config['table_prefix'] . "logs_acc` (
        `id` integer(11) NOT NULL AUTO_INCREMENT,
        `form_id` integer NOT NULL DEFAULT 0,
        `status` TINYINT( 1 ) NULL DEFAULT '0',
        `table_id` integer NOT NULL DEFAULT 0,
         PRIMARY KEY (id)
        ) ENGINE=" . $config['db_engine'] . " DEFAULT CHARSET=utf8");
    run_query("CREATE TABLE  IF NOT EXISTS `" . $config['table_prefix'] . "logs_subtable` (
        `id` integer(11) NOT NULL AUTO_INCREMENT,
        `cat_id` integer NOT NULL DEFAULT 0,
        `addon_id` integer NOT NULL DEFAULT 0,
        `table_id` integer NOT NULL DEFAULT 0,
        `names` integer NOT NULL DEFAULT 0,
        `dates` integer NOT NULL DEFAULT 0,
        `result` integer NOT NULL DEFAULT 0,
        `adress` integer NOT NULL DEFAULT 0,
        `tip` integer NOT NULL DEFAULT 0,
         PRIMARY KEY (id)
        ) ENGINE=" . $config['db_engine'] . " DEFAULT CHARSET=utf8");
    $check = run_query("SHOW COLUMNS FROM " . $config['table_prefix'] . "forms LIKE 'log_sub'");
    if (!sql_num_rows($check)) {
        run_query("ALTER TABLE " . $config["table_prefix"] . "forms ADD log_sub TINYINT( 1 ) NULL DEFAULT '0'");
    }
    $tip = run_query("SELECT id FROM " . $config['table_prefix'] . "tips WHERE table_id=" . $row['id']);
    $tips = sql_fetch_array($tip);
    run_query("INSERT INTO " . $config['table_prefix'] . "logs_subtable(cat_id,table_id,names,result,dates,adress,tip) VALUES('" . $cat_row['id'] . "','" . $row['id'] . "','" . $row2['id'] . "','" . $row3['id'] . "','" . $row4['id'] . "','" . $row5['id'] . "','" . $tips['id'] . "')");
}
$res = run_query("SELECT * FROM information_schema.tables WHERE $db_name='" . $config['dbname'] . "' AND table_name='" . $config['table_prefix'] . "logs_subtable'");
if (sql_num_rows($res)) {
    $res = run_query("SELECT * FROM " . $config['table_prefix'] . "logs_subtable");
    if (sql_num_rows($res)) {
        $check = run_query("SHOW COLUMNS FROM " . $config['table_prefix'] . "forms LIKE 'log_sub'");
        if (!sql_num_rows($check)) {
            run_query("ALTER TABLE " . $config["table_prefix"] . "forms ADD log_sub TINYINT( 1 ) NULL DEFAULT '0'");
        }
        run_query("UPDATE " . $config['table_prefix'] . "forms SET log_sub=1");
        $row = sql_fetch_array($res);
        $res1 = run_query("SELECT id FROM " . $config['table_prefix'] . "tables");
        while ($row1 = sql_fetch_assoc($res1)) {
            $count = run_query("SELECT id FROM " . $config['table_prefix'] . "fields WHERE table_id=" . $row['table_id'] . " AND name_field=" . $row1['id']);
            if (sql_num_rows($count) > 1) {
                $count = run_query("SELECT id FROM " . $config['table_prefix'] . "fields WHERE table_id=" . $row['table_id'] . " AND name_field=" . $row1['id'] . " ORDER BY id DESC LIMIT 1");
                $counts = sql_fetch_array($count);
                run_query("DELETE FROM " . $config['table_prefix'] . "fields WHERE table_id=" . $row['table_id'] . " AND name_field=" . $row1['id'] . " AND id != " . $counts['id']);
            }
            $form = run_query("SELECT id FROM " . $config['table_prefix'] . "forms WHERE dest_form='send' AND table_id =" . $row1['id']);
            if (sql_num_rows($form)) {
                $check = run_query("SELECT id FROM " . $config['table_prefix'] . "subtables WHERE table_id=" . $row1['id'] . " AND link_table_id=" . $row['table_id']);
                if (!sql_num_rows($check)) {
                    $field = run_query("SELECT id FROM " . $config['table_prefix'] . "fields ORDER BY id DESC LIMIT 1");
                    $fields = sql_fetch_array($field);
                    $fields['id'] += 10;
                    $field = run_query("SELECT id FROM " . $config['table_prefix'] . "fields WHERE type_field=10 and table_id=".$row1['id']);
                    $fields2 = sql_fetch_array($field);
                    run_query("INSERT INTO " . $config['table_prefix'] . "fields (id,table_id,name_field,type_field,type_value,reduce) VALUES('" . $fields['id'] . "','" . $row['table_id'] . "','" . $row1['id'] . "','5','" . $row1['id'] . "|" . $fields2['id'] . "|0|0|0','46')");
                    $field = run_query("SELECT id FROM " . $config['table_prefix'] . "fields ORDER BY id DESC LIMIT 1");
                    $fields = sql_fetch_array($field);
                    run_query("ALTER TABLE " . $config['table_prefix'] . "data" . $row['table_id'] . " ADD f" . $fields['id'] . " INT NOT NULL");
                    run_query("INSERT INTO " . $config['table_prefix'] . "subtables (table_id,name,num,link_table_id,link_field_id) VALUES('" . $row1['id'] . "','" . $lang['log_ras'] . "','10','" . $row['table_id'] . "','" . $fields['id'] . "')");
                }
                while ($forms = sql_fetch_assoc($form)) {
                    $check = run_query("SELECT id FROM " . $config['table_prefix'] . "logs_acc WHERE table_id =" . $row1['id'] . " AND form_id=" . $forms['id']);
                    if (!sql_num_rows($check)) {
                        run_query("INSERT INTO " . $config['table_prefix'] . "logs_acc (table_id,form_id,status) VALUES('" . $row1['id'] . "','" . $forms['id'] . "','1')");
                    }
                }
            }
        }
    }
}