<?php

$tables = DB::execute_sql('SHOW TABLES FROM `' . System::$conf->dbinfo['db'] . '`');
if (!$tables) {
    to_index('Warning: Couldn\'t retrieve table information.');
}
$db_tables_path = SYSROOT . 'database/tables/';
delete_files($db_tables_path);
foreach ($tables as $table) {
    $table_data = array();
    $table = current($table);
    $data = DB::execute_sql("DESCRIBE {$table}");
    foreach ($data as $d) {
        $table_data[$d['Field']] = array('type' => $d['Type']);
    }
    $idxs = db::execute_sql("SHOW INDEX FROM {$table}");
    $table_indexes = $pri = $uni = array();
    if ($idxs) {
        foreach ($idxs as $idx) {
            if ($idx['Key_name'] == 'PRIMARY') {
                $pri[] = $idx['Column_name'];
            } elseif ($idx['Non_unique'] === '0') {
                $uni[] = $idx['Column_name'];
            }
        }
    }
    if ($pri) {
        $table_indexes['PRI'] = $pri;
    } elseif ($uni) {
        $table_indexes['UNI'] = $uni;
    }
示例#2
0
<?php

define('ROOT', str_replace('\\', '/', dirname(__FILE__)) . '/');
define('SYSROOT', ROOT . 'system/');
include ROOT . 'config/config.php';
include SYSROOT . 'config.php';
include SYSROOT . 'database/initialize.php';
include SYSROOT . 'load_functions.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    set_time_limit(0);
    $queries = array("CREATE TABLE IF NOT EXISTS `bans` (\n      `user_id` int(11) NOT NULL,\n      `reason` text NOT NULL,\n      `expires_at` datetime NULL,\n      `banned_by` int(11) NOT NULL,\n      `old_level` int(11) NOT NULL,\n      `delete_me` int(11) NOT NULL,\n      KEY `user_id` (`user_id`),\n      KEY `delete_me` (`delete_me`)\n    ) ENGINE=InnoDB DEFAULT CHARSET=utf8", "CREATE TABLE IF NOT EXISTS `comments` (\n      `id` int(11) NOT NULL AUTO_INCREMENT,\n      `post_id` int(11) NOT NULL,\n      `user_id` int(11) NOT NULL,\n      `ip_addr` varchar(16) NOT NULL,\n      `created_at` datetime DEFAULT '0000-00-00 00:00:00',\n      `body` text NOT NULL,\n      `updated_at` datetime NULL,\n      PRIMARY KEY (`id`),\n      KEY `image_id` (`post_id`),\n      KEY `owner_ip` (`ip_addr`),\n      KEY `posted` (`created_at`),\n      KEY `fk_comments__user_id` (`user_id`)\n    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ", "CREATE TABLE IF NOT EXISTS `favorites` (\n      `id` int(11) NOT NULL AUTO_INCREMENT,\n      `post_id` int(11) NOT NULL,\n      `user_id` int(11) NOT NULL,\n      `created_at` datetime DEFAULT '0000-00-00 00:00:00',\n      PRIMARY KEY (`id`),\n      UNIQUE KEY `image_id` (`post_id`,`user_id`)\n    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ", "CREATE TABLE IF NOT EXISTS `flagged_post_details` (\n      `created_at` datetime NULL,\n      `post_id` int(11) NOT NULL,\n      `user_id` int(11) NOT NULL,\n      `reason` varchar(512) NOT NULL,\n      `is_resolved` tinyint(1) NOT NULL DEFAULT '0',\n      KEY `post_id` (`post_id`),\n      KEY `fk_flag_post_details__user_id` (`user_id`)\n    ) ENGINE=InnoDB DEFAULT CHARSET=utf8", "CREATE TABLE IF NOT EXISTS `notes` (\n      `id` int(11) NOT NULL AUTO_INCREMENT,\n      `created_at` DATETIME NULL,\n      `updated_at` DATETIME NULL,\n      `user_id` int(11) NOT NULL,\n      `x` int(11) NOT NULL,\n      `y` int(11) NOT NULL,\n      `width` int(11) NOT NULL,\n      `height` int(11) NOT NULL,\n      `ip_addr` varchar(64) NOT NULL,\n      `version` int(11) NOT NULL DEFAULT '1',\n      `is_active` tinyint(1) NOT NULL DEFAULT '1',\n      `post_id` int(11) NOT NULL,\n      `body` text NOT NULL,\n      PRIMARY KEY (`id`),\n      KEY `post_id` (`post_id`),\n      KEY `user_id` (`user_id`)\n    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ", "CREATE TABLE IF NOT EXISTS `note_versions` (\n      `id` int(11) NOT NULL AUTO_INCREMENT,\n      `created_at` DATETIME NULL,\n      `updated_at` DATETIME NULL,\n      `x` int(11) NOT NULL,\n      `y` int(11) NOT NULL,\n      `width` int(11) NOT NULL,\n      `height` int(11) NOT NULL,\n      `body` int(11) NOT NULL,\n      `version` int(11) NOT NULL,\n      `ip_addr` varchar(64) NOT NULL,\n      `is_active` tinyint(1) NOT NULL DEFAULT '1',\n      `note_id` int(11) NOT NULL,\n      `post_id` int(11) NOT NULL,\n      `user_id` int(11) NOT NULL,\n      PRIMARY KEY (`id`),\n      KEY `user_id` (`user_id`),\n      KEY `fk_note_versions__note_id` (`note_id`)\n    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ", "CREATE TABLE IF NOT EXISTS `pools` (\n      `id` int(11) NOT NULL AUTO_INCREMENT,\n      `name` varchar(255) NOT NULL,\n      `description` varchar(128) NOT NULL,\n      `user_id` int(11) NOT NULL,\n      `is_active` tinyint(1) NOT NULL DEFAULT '1',\n      `created_at` DATETIME NULL,\n      `updated_at` DATETIME NULL,\n      `post_count` int(3) NOT NULL DEFAULT '0',\n      `is_public` binary(1) NOT NULL DEFAULT '1',\n      PRIMARY KEY (`id`),\n      UNIQUE KEY `pool_name` (`name`)\n    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ", "CREATE TABLE IF NOT EXISTS `pools_posts` (\n      `id` int(11) NOT NULL AUTO_INCREMENT,\n      `post_id` int(11) NOT NULL,\n      `pool_id` int(11) NOT NULL,\n      `sequence` varchar(16) NOT NULL,\n      `next_post_id` int(11) DEFAULT NULL,\n      `prev_post_id` int(11) DEFAULT NULL,\n      PRIMARY KEY (`id`),\n      KEY `post_id` (`post_id`),\n      KEY `fk_pools_posts__next_post_id` (`next_post_id`),\n      KEY `fk_pools_posts__prev_post_id` (`prev_post_id`),\n      KEY `fk_pools_posts__pool_id` (`pool_id`)\n    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ", "DROP TRIGGER IF EXISTS `pools_posts_insert_trg`", "CREATE TRIGGER `pools_posts_insert_trg` BEFORE INSERT ON `pools_posts`\n     FOR EACH ROW UPDATE pools SET post_count = post_count + 1 WHERE id = NEW.pool_id", "DROP TRIGGER IF EXISTS `pools_posts_delete_trg`", "CREATE TRIGGER `pools_posts_delete_trg` BEFORE DELETE ON `pools_posts`\n     FOR EACH ROW UPDATE pools SET post_count = post_count - 1 WHERE id = OLD.pool_id", "CREATE TABLE IF NOT EXISTS `posts` (\n      `id` int(11) NOT NULL AUTO_INCREMENT,\n      `user_id` int(11) NOT NULL,\n      `ip_addr` varchar(64) NOT NULL,\n      `file_size` int(11) NOT NULL,\n      `md5` varchar(32) NOT NULL,\n      `last_commented_at` datetime DEFAULT NULL,\n      `file_ext` varchar(4) NOT NULL,\n      `last_noted_at` datetime DEFAULT NULL,\n      `source` varchar(249) DEFAULT NULL,\n      `cached_tags` text NOT NULL,\n      `width` int(11) NOT NULL,\n      `height` int(11) NOT NULL,\n      `created_at` datetime NULL,\n      `rating` char(1) NOT NULL DEFAULT 'q',\n      `note` varchar(255) NOT NULL,\n      `preview_width` int(3) NOT NULL,\n      `preview_height` int(3) NOT NULL,\n      `actual_preview_width` int(3) NOT NULL,\n      `actual_preview_height` int(3) NOT NULL,\n      `score` int(3) NOT NULL,\n      `is_shown_in_index` tinyint(1) NOT NULL DEFAULT '1',\n      `is_held` tinyint(1) NOT NULL DEFAULT '0',\n      `has_children` tinyint(1) NOT NULL DEFAULT '0',\n      `status` enum('deleted','flagged','pending','active') CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT 'active',\n      `is_rating_locked` tinyint(1) NOT NULL DEFAULT '0',\n      `is_note_locked` tinyint(1) NOT NULL DEFAULT '0',\n      `parent_id` int(11) DEFAULT NULL,\n      `sample_width` int(5) DEFAULT NULL,\n      `sample_height` int(5) DEFAULT NULL,\n      `sample_size` int(11) DEFAULT NULL,\n      `index_timestamp` datetime NULL,\n      `jpeg_width` int(11) DEFAULT NULL,\n      `jpeg_height` int(11) DEFAULT NULL,\n      `jpeg_size` int(11) DEFAULT NULL,\n      `random` int(11) NOT NULL,\n      PRIMARY KEY (`id`),\n      UNIQUE KEY `images__hash` (`md5`),\n      KEY `images__owner_id` (`user_id`),\n      KEY `images__width` (`width`),\n      KEY `images__height` (`height`),\n      KEY `fk_posts__parent_id` (`parent_id`)\n    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ", "DROP TRIGGER IF EXISTS `trg_posts__insert`", "CREATE TRIGGER `trg_posts__insert` AFTER INSERT ON `posts`\n     FOR EACH ROW UPDATE table_data SET row_count = row_count + 1 WHERE name = 'posts'", "DROP TRIGGER IF EXISTS `trg_posts__delete`", "CREATE TRIGGER `trg_posts__delete` AFTER DELETE ON `posts`\n    FOR EACH ROW\n    BEGIN\n      UPDATE pools SET post_count = post_count - 1 WHERE id IN (SELECT pool_id FROM pools_posts WHERE post_id = OLD.id);\n      UPDATE table_data SET row_count = row_count - 1 WHERE name = 'posts';\n    END", "CREATE TABLE IF NOT EXISTS `posts_tags` (\n      `post_id` int(11) NOT NULL,\n      `tag_id` int(11) NOT NULL,\n      UNIQUE KEY `post_id` (`post_id`,`tag_id`),\n      KEY `fk_posts_tags__post_id` (`post_id`),\n      KEY `fk_posts_tags__tag_id` (`tag_id`)\n    ) ENGINE=InnoDB DEFAULT CHARSET=utf8", "DROP TRIGGER IF EXISTS `trg_posts_tags__insert`", "CREATE TRIGGER `trg_posts_tags__insert` BEFORE INSERT ON `posts_tags`\n     FOR EACH ROW UPDATE tags SET post_count = post_count + 1 WHERE tags.id = NEW.tag_id", "DROP TRIGGER IF EXISTS `trg_posts_tags__delete`", "CREATE TRIGGER `trg_posts_tags__delete` BEFORE DELETE ON `posts_tags`\n     FOR EACH ROW UPDATE tags SET post_count = post_count - 1 WHERE tags.id = OLD.tag_id", "CREATE TABLE IF NOT EXISTS `post_votes` (\n      `post_id` int(11) NOT NULL,\n      `user_id` int(11) NOT NULL,\n      `score` int(1) DEFAULT '0',\n      `updated_at` datetime NULL DEFAULT '0000-00-00 00:00:00',\n      UNIQUE KEY `post_id` (`post_id`,`user_id`),\n      KEY `score` (`score`),\n      KEY `fk_user_id__users_id` (`user_id`)\n    ) ENGINE=InnoDB DEFAULT CHARSET=utf8", "CREATE TABLE IF NOT EXISTS `table_data` (\n      `name` varchar(11) CHARACTER SET ucs2 NOT NULL,\n      `row_count` int(11) NOT NULL DEFAULT '0',\n      KEY `name` (`name`)\n    ) ENGINE=InnoDB DEFAULT CHARSET=utf8", "CREATE TABLE IF NOT EXISTS `tags` (\n      `id` int(11) NOT NULL AUTO_INCREMENT,\n      `name` varchar(64) NOT NULL,\n      `post_count` int(11) NOT NULL DEFAULT '0',\n      `cached_related` text,\n      `cached_related_expires_on` datetime DEFAULT NULL,\n      `tag_type` smallint(6) NOT NULL,\n      `is_ambiguous` tinyint(1) NOT NULL DEFAULT '0',\n      PRIMARY KEY (`id`),\n      UNIQUE KEY `tags__name` (`name`)\n    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ", "CREATE TABLE IF NOT EXISTS `tag_aliases` (\n      `id` int(11) NOT NULL AUTO_INCREMENT,\n      `name` varchar(64) NOT NULL,\n      `alias_id` int(11) NOT NULL,\n      `is_pending` tinyint(1) NOT NULL DEFAULT '0',\n      `reason` varchar(128) NOT NULL,\n      PRIMARY KEY (`id`),\n      UNIQUE KEY `alias_unique` (`name`,`alias_id`),\n      KEY `name` (`name`),\n      KEY `alias_id` (`alias_id`)\n    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ", "CREATE TABLE IF NOT EXISTS `tag_implications` (\n      `id` int(11) NOT NULL AUTO_INCREMENT,\n      `predicate_id` int(11) NOT NULL,\n      `consequent_id` int(11) NOT NULL,\n      `is_pending` tinyint(1) NOT NULL DEFAULT '0',\n      `reason` varchar(128) NOT NULL,\n      PRIMARY KEY (`id`),\n      UNIQUE KEY `implication_unique` (`predicate_id`,`consequent_id`),\n      KEY `fk_consequent_id` (`consequent_id`)\n    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ", "CREATE TABLE IF NOT EXISTS `tag_subscriptions` (\n      `id` int(11) NOT NULL AUTO_INCREMENT,\n      `user_id` int(11) NOT NULL,\n      `tag_query` text CHARACTER SET latin1 NOT NULL,\n      `cached_post_ids` text CHARACTER SET latin1 NOT NULL,\n      `name` varchar(32) CHARACTER SET latin1 NOT NULL,\n      `is_visible_on_profile` tinyint(1) NOT NULL DEFAULT '1',\n      PRIMARY KEY (`id`),\n      KEY `user_id` (`user_id`),\n      KEY `name` (`name`)\n    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ", "CREATE TABLE IF NOT EXISTS `users` (\n      `id` int(11) NOT NULL AUTO_INCREMENT,\n      `name` varchar(32) NOT NULL,\n      `password_hash` varchar(32) DEFAULT NULL,\n      `created_at` datetime NULL,\n      `level` int(11) NOT NULL DEFAULT '20',\n      `email` varchar(249) DEFAULT NULL,\n      `avatar_post_id` int(11) DEFAULT NULL,\n      `avatar_width` double DEFAULT NULL,\n      `avatar_height` double DEFAULT NULL,\n      `avatar_top` double DEFAULT NULL,\n      `avatar_bottom` double DEFAULT NULL,\n      `avatar_left` double DEFAULT NULL,\n      `avatar_right` double DEFAULT NULL,\n      `avatar_timestamp` datetime NULL,\n      `my_tags` text,\n      `show_samples` tinyint(1) NOT NULL DEFAULT '1',\n      `show_advanced_editing` tinyint(1) NOT NULL DEFAULT '0',\n      `pool_browse_mode` tinyint(1) NOT NULL DEFAULT '0',\n      `use_browser` tinyint(1) NOT NULL DEFAULT '0',\n      `always_resize_images` tinyint(1) NOT NULL DEFAULT '0',\n      `last_logged_in_at` datetime NULL,\n      `last_commented_read_at` datetime NULL,\n      `last_deleted_post_seen_at` datetime NULL,\n      `language` text NOT NULL,\n      `secondary_languages` text NOT NULL,\n      `receive_dmails` tinyint(1) NOT NULL DEFAULT '1',\n      PRIMARY KEY (`id`),\n      UNIQUE KEY `users__name` (`name`),\n      KEY `fk_users__avatar_post_id` (`avatar_post_id`)\n    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ", "CREATE TABLE IF NOT EXISTS `user_blacklisted_tags` (\n      `user_id` int(11) NOT NULL,\n      `tags` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,\n      UNIQUE KEY `user_id` (`user_id`)\n    ) ENGINE=InnoDB DEFAULT CHARSET=utf8", "ALTER TABLE `comments`\n      ADD CONSTRAINT `fk_comments__post_id` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`) ON DELETE CASCADE,\n      ADD CONSTRAINT `fk_comments__user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE", "ALTER TABLE `flagged_post_details`\n      ADD CONSTRAINT `fk_flag_post_details__user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`),\n      ADD CONSTRAINT `fk_flag_post_det__post_id` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`) ON DELETE CASCADE", "ALTER TABLE `pools_posts`\n      ADD CONSTRAINT `fk_pools_posts__next_post_id` FOREIGN KEY (`next_post_id`) REFERENCES `posts` (`id`) ON DELETE SET NULL,\n      ADD CONSTRAINT `fk_pools_posts__pool_id` FOREIGN KEY (`pool_id`) REFERENCES `pools` (`id`) ON DELETE CASCADE,\n      ADD CONSTRAINT `fk_pools_posts__post_id` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`) ON DELETE CASCADE,\n      ADD CONSTRAINT `fk_pools_posts__prev_post_id` FOREIGN KEY (`prev_post_id`) REFERENCES `posts` (`id`) ON DELETE SET NULL", "ALTER TABLE `posts`\n      ADD CONSTRAINT `fk_parent_id__posts_id` FOREIGN KEY (`parent_id`) REFERENCES `posts` (`id`) ON DELETE SET NULL", "ALTER TABLE `posts_tags`\n      ADD CONSTRAINT `fk_posts_tags__post_id` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`) ON DELETE CASCADE,\n      ADD CONSTRAINT `fk_posts_tags__tag_id` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`) ON DELETE CASCADE", "ALTER TABLE `post_votes`\n      ADD CONSTRAINT `fk_post_id__posts_id` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`) ON DELETE CASCADE,\n      ADD CONSTRAINT `fk_user_id__users_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE", "ALTER TABLE `tag_aliases`\n      ADD CONSTRAINT `fk_alias_id` FOREIGN KEY (`alias_id`) REFERENCES `tags` (`id`) ON DELETE CASCADE", "ALTER TABLE `tag_implications`\n      ADD CONSTRAINT `fk_consequent_id` FOREIGN KEY (`consequent_id`) REFERENCES `tags` (`id`) ON DELETE CASCADE,\n      ADD CONSTRAINT `fk_predicate_id` FOREIGN KEY (`predicate_id`) REFERENCES `tags` (`id`) ON DELETE CASCADE", "ALTER TABLE `user_blacklisted_tags`\n      ADD CONSTRAINT `fk_user_bl_tags__user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE", "INSERT INTO `table_data` VALUES ('posts', 0)", "INSERT INTO `table_data` VALUES ('users', 0)", "INSERT INTO `table_data` VALUES ('non-explicit_posts', 0)");
    foreach ($queries as $query) {
        DB::execute_sql($query);
    }
    extract($_POST);
    $password_hash = md5($name . $password);
    $user_id = DB::insert('users (created_at, name, password_hash, level, show_advanced_editing) VALUES (?, ?, ?, ?, ?)', gmd(), $name, $password_hash, 50, 1);
    DB::insert('user_blacklisted_tags VALUES (?, ?)', $user_id, implode("\r\n", CONFIG::$default_blacklists));
    DB::update("table_data set row_count = row_count + 1 where name = 'users'");
    $dp = ROOT . 'public/data/';
    foreach (array($dp, "{$dp}/avatars", "{$dp}/export", "{$dp}/image", "{$dp}/import", "{$dp}/jpeg", "{$dp}/preview", "{$dp}/sample") as $dir) {
        @mkdir($dir);
    }
    unlink('index.php');
    rename('index_.php', 'index.php');
    cookie_put('login', $name);
    cookie_put('pass_hash', $password_hash);
    notice('Installation completed');
    header('Location: /');
    exit;
}
示例#3
0
 static function find_by_sql($sql_params, $params = array())
 {
     $sql = array_shift($sql_params);
     $data = DB::execute_sql($sql, $sql_params);
     self::parse_calc_rows_param($params);
     self::retrieve_calc_rows();
     if (isset($params['return_array'])) {
         return $data;
     }
     $params['model_name'] = self::cn(true);
     $collection = new Collection($data, $params);
     !get_object_vars($collection) && ($collection = array());
     if (!count($collection)) {
         $collection = array();
     }
     return $collection;
 }
示例#4
0
 function decrement_count()
 {
     DB::execute_sql("UPDATE table_data SET row_count = row_count - 1 WHERE name = 'posts'");
 }