<?php
global $config, $sql_global_link, $sql_db_type, $channel_tables;

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";
}

$result = run_query("SELECT * FROM information_schema.TABLES WHERE $db_name='" . $config['dbname'] . "' AND TABLE_NAME='" . $config["table_prefix"] . "mail_files'");
if ($row = sql_fetch_assoc($result)) {
    $rr = $channel_tables;
    $channel_tables = array();
    $form_id_old = 0;
    $form_name_old = '';
    $form_type_old = '';
    $form_disp_old = '';
    $file_id = 0;
    $part_id = 0;
    $links_id = 0;
    $table_links = $config['table_prefix'] . "mail_files_links";
    $table_info = $config['table_prefix'] . "mail_files_info";
    $table_part = $config['table_prefix'] . "mail_files_part";

    run_query("DROP TABLE IF EXISTS " . $table_links);
    run_query("DROP TABLE IF EXISTS " . $table_info);
    run_query("DROP TABLE IF EXISTS " . $table_part);
    run_query("CREATE TABLE " . $table_links . " (id INT(11) NOT NULL AUTO_INCREMENT, table_id INT(11) NOT NULL, form_id INT(11) NOT NULL, name VARCHAR(255) NOT NULL, file_id INT(11) NOT NULL, PRIMARY KEY (id), KEY table_id (table_id), KEY form_id (form_id)) ENGINE=" . $config['db_engine'] . " DEFAULT CHARSET=utf8");
    run_query("CREATE TABLE " . $table_info . " (id INT(11) NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, type VARCHAR(255) NOT NULL, disp VARCHAR(255) NOT NULL, PRIMARY KEY (id)) ENGINE=" . $config['db_engine'] . " DEFAULT CHARSET=utf8");
    run_query("CREATE TABLE " . $table_part . " (id INT(11) NOT NULL AUTO_INCREMENT, file_id INT(11) NOT NULL, content LONGBLOB NOT NULL, PRIMARY KEY (id)) ENGINE=" . $config['db_engine'] . " DEFAULT CHARSET=utf8");

    try {
        $result = sql_query("SELECT * FROM " . $config["table_prefix"] . "mail_files GROUP BY name, type, disp, content ORDER BY form_id, id ASC");
        while ($row = sql_fetch_assoc($result)) {
            $part_id = $part_id + 10;
            $form_id_new = $row['form_id'];
            $form_name_new = $row['name'];
            $form_type_new = $row['type'];
            $form_disp_new = $row['disp'];
            if (($form_id_new != $form_id_old) OR ($form_name_new != $form_name_old) OR ($form_type_new != $form_type_old) OR ($form_disp_new != $form_disp_old)) {
                $file_id = $file_id + 10;
                $ins_file_info = array();
                $ins_file_info['id'] = $file_id;
                $ins_file_info['name'] = $row['name'];
                $ins_file_info['type'] = $row['type'];
                $ins_file_info['disp'] = $row['disp'];
                $file_id_info = sql_insert($table_info, $ins_file_info);
            }
            $ins_file_part = array();
            $ins_file_part['id'] = $part_id;
            $ins_file_part['file_id'] = $file_id_info;
            $ins_file_part['content'] = $row['content'];
            sql_insert($table_part, $ins_file_part);
            $form_id_old = $form_id_new;
            $form_name_old = $form_name_new;
            $form_type_old = $form_type_new;
            $form_disp_old = $form_disp_new;
        }
        /*
        $result = sql_query("SELECT mail_files.table_id as table_id, mail_files.form_id as form_id, mail_files.name as name, table_part.file_id as file_id
        FROM ".$config['table_prefix']."mail_files as mail_files
        JOIN ".$table_part." as table_part ON mail_files.content=table_part.content
        JOIN ".$table_info." as table_info ON table_part.file_id=table_info.id
        WHERE table_info.name=mail_files.name AND table_info.type=mail_files.type AND table_info.disp=mail_files.disp
        GROUP BY mail_files.table_id, mail_files.form_id, mail_files.name, mail_files.type, mail_files.disp
        ORDER BY mail_files.form_id, mail_files.name ASC");
        while ($row = sql_fetch_assoc($result))
          {
            $links_id = $links_id+10;
            $ins_file_links = array();
            $ins_file_links['id'] = $links_id;
            $ins_file_links['table_id'] = $row['table_id'];
            $ins_file_links['form_id'] = $row['form_id'];
            $ins_file_links['name'] = $row['name'];
            $ins_file_links['file_id'] = $row['file_id'];
            sql_insert($table_links, $ins_file_links);
          }
        */
        $result = sql_query("SELECT table_id, form_id, name, type, disp 
        FROM " . $config['table_prefix'] . "mail_files
        GROUP BY table_id, form_id, name, type, disp 
        ORDER BY form_id, name ASC");
        while ($row = sql_fetch_assoc($result)) {
            $result2 = sql_query("SELECT id, name, type, disp 
            FROM " . $table_info . "
            WHERE name='" . mysqli_real_escape_string($sql_global_link, $row['name']) . "' AND type='" . $row['type'] . "' AND disp='" . $row['disp'] . "'");
            while ($row2 = sql_fetch_assoc($result2)) {
                $result4 = sql_query("SELECT content
                FROM " . $config['table_prefix'] . "mail_files
                WHERE table_id='" . $row['table_id'] . "' AND form_id='" . $row['form_id'] . "' AND NAME='" . mysqli_real_escape_string($sql_global_link, $row['name']) . "' AND type='" . $row['type'] . "' AND disp='" . $row['disp'] . "'
                ORDER BY id ASC LIMIT 1");
                $row4 = sql_fetch_assoc($result4);

                $result3 = sql_query("SELECT file_id, content
                FROM " . $table_part . "
                WHERE file_id='" . $row2['id'] . "'
                ORDER BY id ASC LIMIT 1");
                while ($row3 = sql_fetch_assoc($result3)) {
                    if ($row3['content'] == $row4['content']) {
                        $links_id = $links_id + 10;
                        $ins_file_links = array();
                        $ins_file_links['id'] = $links_id;
                        $ins_file_links['table_id'] = $row['table_id'];
                        $ins_file_links['form_id'] = $row['form_id'];
                        $ins_file_links['name'] = $row['name'];
                        $ins_file_links['file_id'] = $row2['id'];
                        sql_insert($table_links, $ins_file_links);
                    }
                }
            }
        }
    } catch (Exception $e) {
        exit('ERROR revision 15713 CREATE TABLE');
    }
}
try {
    run_query("DROP TABLE IF EXISTS " . $config["table_prefix"] . "mail_files");
} catch (Exception $e) {
    exit('ERROR revision 15713 DROP TABLE');
}
$channel_tables = $rr;