DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
  `id` INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `username` CHAR(32) NOT NULL,
  `passhash` CHAR(32) NOT NULL,
  `salt` CHAR(16) NOT NULL,
  `email` CHAR(64) NOT NULL UNIQUE KEY,
  `class` INT UNSIGNED NOT NULL DEFAULT 2,
  `regdate` INT UNSIGNED NOT NULL,
  `regip` INT UNSIGNED NOT NULL,
  `lastdate` INT UNSIGNED NOT NULL DEFAULT 0,
  `lastip` INT UNSIGNED NOT NULL DEFAULT 0,
  `lastaction` INT UNSIGNED NOT NULL DEFAULT 0,
  `lang` CHAR(16) NOT NULL DEFAULT 'zh-cn',
  `skin` SMALLINT UNSIGNED NOT NULL DEFAULT 1,
  `disk_usage` BIGINT UNSIGNED NOT NULL DEFAULT 0,
  `disk_max` BIGINT UNSIGNED NOT NULL DEFAULT 0,
  `disk_ext` BIGINT UNSIGNED NOT NULL DEFAULT 0,
  `files` INT UNSIGNED NOT NULL DEFAULT 0,
  `folders` INT UNSIGNED NOT NULL DEFAULT 0,
  `msgs` INT UNSIGNED NOT NULL DEFAULT 0,
  `msg_max` INT UNSIGNED NOT NULL DEFAULT 0,
  `mr0` INT UNSIGNED NOT NULL DEFAULT 0,
  INDEX `users_class` (`class`),
  UNIQUE KEY `users_name` (`username`)
) ENGINE=MyISAM AUTO_INCREMENT = 1001 DEFAULT CHARSET='utf8' COLLATE=`utf8_general_ci`;

DROP TABLE IF EXISTS `groups`;
CREATE TABLE `groups` (
  `id` INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `disk_max` BIGINT UNSIGNED NOT NULL DEFAULT 0,
  `msg_max` INT UNSIGNED NOT NULL DEFAULT 0,
  `filesize_limit` INT UNSIGNED NOT NULL DEFAULT 0,
  `file_ext` VARCHAR(256) NOT NULL DEFAULT '',
  `name` VARCHAR(16) NOT NULL,
  `vols` VARCHAR(255) NOT NULL DEFAULT '1',
  `mr0` INT UNSIGNED NOT NULL DEFAULT 0,
  INDEX `groups_name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET='utf8';

INSERT INTO `groups`(`name`, `disk_max`, `msg_max`, `mr0`) VALUES
('管理员', 1099511627776, 1024, 4294967295),
('待验证用户', 0, 0, 0),
('正式用户', 16777216, 32, 0);

DROP TABLE IF EXISTS `skins`;
CREATE TABLE `skins` (
  `id` INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `name` VARCHAR(32) NOT NULL,
  `foldername` VARCHAR(32) NOT NULL,
  `mr0` INT UNSIGNED NOT NULL DEFAULT 0
) ENGINE=MyISAM DEFAULT CHARSET='utf8' COLLATE=`utf8_general_ci`;

DROP TABLE IF EXISTS `iplogs`;
CREATE TABLE `iplogs` (
  `ip` INT UNSIGNED NOT NULL, -- Long Integer made by PHP function ip2long
  `user` INT UNSIGNED NOT NULL, -- Who used this IP.
  `date` INT UNSIGNED NOT NULL -- The time that IP was used.
) ENGINE=MyISAM DEFAULT CHARSET='utf8';

DROP TABLE IF EXISTS `iplimits`;
CREATE TABLE `iplimits` (
  `ip` INT UNSIGNED NOT NULL PRIMARY KEY, -- Long Integer made by PHP function ip2long
  `type` TINYINT UNSIGNED NOT NULL DEFAULT 0, -- 0 equal to BLOCKED, 1 equal to ALLOWED
  `operator` INT UNSIGNED NOT NULL,
  `expire` INT UNSIGNED NOT NULL,
  `info` CHAR(128) NOT NULL,
  `date` INT UNSIGNED NOT NULL
) ENGINE=MyISAM;

DROP TABLE IF EXISTS `ip_temp`;
CREATE TABLE `ip_temp` (
  `ip` INT UNSIGNED NOT NULL PRIMARY KEY, -- Long Integer made by PHP function ip2long
  `type` TINYINT UNSIGNED NOT NULL DEFAULT 0, -- 0 equal to BLOCKED, 1 equal to ALLOWED
  `expire` INT UNSIGNED NOT NULL DEFAULT 0
) ENGINE=MEMORY;

DROP TABLE IF EXISTS `messages`;
CREATE TABLE `messages` (
  `id` INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `title` VARCHAR(64) NOT NULL,
  `text` TEXT NOT NULL,
  `from` INT UNSIGNED NOT NULL,
  `to` INT UNSIGNED NOT NULL,
  `time` INT UNSIGNED NOT NULL,
  `box` TINYINT UNSIGNED NOT NULL DEFAULT 0,
  `type` TINYINT UNSIGNED NOT NULL DEFAULT 0,
  `mr0` INT UNSIGNED NOT NULL DEFAULT 0
) ENGINE=MyISAM DEFAULT CHARSET='utf8' COLLATE=`utf8_general_ci`;

DROP TABLE IF EXISTS `vfiles`;
CREATE TABLE `vfiles` (
  `id` INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `size` BIGINT UNSIGNED NOT NULL,
  `md5` CHAR(32) NOT NULL DEFAULT '',
  `sha1` CHAR(40) NOT NULL DEFAULT '',
  `mr0` INT UNSIGNED NOT NULL DEFAULT 0,
  `serv0` INT UNSIGNED NOT NULL DEFAULT 0,
  `serv1` INT UNSIGNED NOT NULL DEFAULT 0,
  `serv2` INT UNSIGNED NOT NULL DEFAULT 0,
  `serv3` INT UNSIGNED NOT NULL DEFAULT 0,
  INDEX (`md5`),
  INDEX (`sha1`)
) ENGINE=MyISAM DEFAULT CHARSET='utf8' COLLATE=`utf8_general_ci`;

DROP TABLE IF EXISTS `files`;
CREATE TABLE `files` (
  `key` CHAR(8) NOT NULL COLLATE `utf8_bin`,
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(128) NOT NULL,
  `name_ext` CHAR(16) NOT NULL DEFAULT '',
  `size` BIGINT UNSIGNED NOT NULL,
  `vfid` INT UNSIGNED NOT NULL,
  `owner` INT UNSIGNED NOT NULL,
  `parent` INT UNSIGNED NOT NULL,
  `path` TEXT NOT NULL,
  `paths` TEXT NOT NULL,
  `depth` INT UNSIGNED NOT NULL DEFAULT 0,
  `mr0` INT UNSIGNED NOT NULL DEFAULT 0,
  `added` INT UNSIGNED NOT NULL,
  `lastedited` INT UNSIGNED NOT NULL,
  `downloads` INT UNSIGNED NOT NULL DEFAULT '0',
  INDEX `files_owner` (`owner`),
  UNIQUE INDEX `files_unq_name` (`parent`, `name`, `owner`),
  INDEX `files_parent` (`parent`),
  INDEX `files_depth` (`depth`),
  INDEX `files_name` (`name`),
  INDEX `files_name_ext` (`name_ext`),
  PRIMARY KEY `files_primary_key` (`key`),
  UNIQUE KEY `files_id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET='utf8' COLLATE=`utf8_general_ci`;

DROP TABLE IF EXISTS `folders`;
CREATE TABLE `folders` (
  `key` CHAR(8) NOT NULL COLLATE `utf8_bin`,
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` CHAR(128) NOT NULL,
  `size` BIGINT UNSIGNED NOT NULL DEFAULT 0,
  `items` INT UNSIGNED NOT NULL DEFAULT 0,
  `folders` INT UNSIGNED NOT NULL DEFAULT 0,
  `files` INT UNSIGNED NOT NULL DEFAULT 0,
  `owner` INT UNSIGNED NOT NULL,
  `parent` INT UNSIGNED NOT NULL,
  `path` TEXT NOT NULL,
  `paths` TEXT NOT NULL,
  `depth` INT UNSIGNED NOT NULL DEFAULT 0,
  `mr0` INT UNSIGNED NOT NULL DEFAULT 0,
  `added` INT UNSIGNED NOT NULL,
  `lastedited` INT UNSIGNED NOT NULL,
  INDEX `folders_owner` (`owner`),
  INDEX `folders_parent` (`parent`),
  INDEX `folders_depth` (`depth`),
  INDEX `folders_name` (`name`),
  UNIQUE INDEX `folders_unq_name` (`parent`, `name`, `owner`),
  PRIMARY KEY `folders_primary_key` (`key`),
  UNIQUE KEY `folders_id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET='utf8' COLLATE=`utf8_general_ci`;

DROP TABLE IF EXISTS `keys`;
CREATE TABLE `keys` (
  `key` CHAR(32) NOT NULL PRIMARY KEY,
  `expire` INT UNSIGNED NOT NULL DEFAULT 0,
  `user` INT UNSIGNED NOT NULL DEFAULT 0,
  `module` INT UNSIGNED NOT NULL,
  `usage` INT UNSIGNED NOT NULL DEFAULT 0,
  `times` INT NOT NULL DEFAULT 1,
  `using` INT UNSIGNED NOT NULL DEFAULT 0,
  `x` INT NOT NULL DEFAULT 0,
  `y` INT NOT NULL DEFAULT 0,
  INDEX `keys_user` (`user`),
  INDEX `keys_usage` (`usage`),
  INDEX `keys_module` (`module`)
) ENGINE=MyISAM DEFAULT CHARSET='utf8' COLLATE=`utf8_bin`;

DROP TABLE IF EXISTS `modules`;
CREATE TABLE `modules` (
  `id` INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `key` CHAR(64) NOT NULL UNIQUE KEY,
  `type` TINYINT UNSIGNED NOT NULL DEFAULT 2,
  `info` CHAR(128) NULL
) ENGINE=MyISAM DEFAULT CHARSET='utf8';

INSERT INTO `modules`(`type`, `key`, `info`) VALUES
(0, 'XKRL_CORE', 'SleX.PHP.Core'),
(0, 'XKRL_EXCEPTION', 'SleX.PHP.Exception'),
(0, 'XKRL_UTILS', 'SleX.PHP.Utilities'),
(0, 'XKRL_MYSQL', 'SleX.PHP.MySQL'),
(0, 'XKRL_MEMCHACE', 'SleX.PHP.MemCache'),
(0, 'XKRL_CACHE', 'SleX.PHP.Cache'),
(0, 'XKRL_LIBMGR', 'SleX.PHP.LibMgr'),
(1, 'XCOM_NET', 'SleX.PHP.Net'),
(1, 'XCOM_IPMGR', 'SleX.PHP.IPMgr'),
(1, 'XCOM_AUTOSESSION', 'SleX.PHP.AutoSession'),
(1, 'XCOM_USERMGR', 'SleX.PHP.UserMgr'),
(1, 'XCOM_LANG', 'SleX.PHP.Lang'),
(1, 'XCOM_UI', 'SleX.PHP.UI'),
(1, 'XCOM_KEYMGR', 'SleX.PHP.KeyMgr'),
(1, 'XCOM_GC', 'SleX.PHP.GC'),
(1, 'FCOM_FILEMGR', 'Filiant.PHP.FileMgr'),
(1, 'FCOM_VOLMGR', 'Filiant.PHP.VolMgr'),
(1, 'FCOM_SHAREMGR', 'Filiant.PHP.ShareMgr'),
(1, 'FCOM_DOWNLOADED', 'Filiant.PHP.Downloader'),
(1, 'XCOM_CREDIT', 'SleX.PHP.Credit');

DROP TABLE IF EXISTS `servers`;
CREATE TABLE `servers` ( -- File Servers.
  `id` INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `host` CHAR(64) NOT NULL UNIQUE KEY,
  `info` CHAR(128) NOT NULL,
  `size` BIGINT UNSIGNED NOT NULL DEFAULT 0,
  `rsize` BIGINT UNSIGNED NOT NULL DEFAULT 0,
  `type` INT UNSIGNED NOT NULL DEFAULT 0,
  `stype` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `access_key` VARCHAR(64) NOT NULL DEFAULT '',
  `secret_key` VARCHAR(64) NOT NULL DEFAULT '',
  `mr0` INT UNSIGNED NOT NULL DEFAULT 0
) ENGINE=MyISAM DEFAULT CHARSET='utf8' COLLATE=`utf8_general_ci`;

DROP TABLE IF EXISTS `downloads`;
CREATE TABLE `downloads` ( -- File downloaded logs.
  `file` INT UNSIGNED NOT NULL,
  `user` INT UNSIGNED NOT NULL,
  `date` INT UNSIGNED NOT NULL,
  PRIMARY KEY (`file`, `user`)
) ENGINE=MyISAM DEFAULT CHARSET='utf8' COLLATE=`utf8_general_ci`;

INSERT INTO `servers` (`id` ,`host` ,`info` ,`size` ,`rsize` ,`type` ,`mr0`) VALUES
(NULL , '127.0.0.1', '', '1073741824', '1073741824', '0', '0');

DROP TABLE IF EXISTS `logs`;
CREATE TABLE `logs` (
  `id` INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `logtext` text NOT NULL,
  `script` TEXT NOT NULL,
  `date` INT UNSIGNED NOT NULL,
  `user` INT UNSIGNED NOT NULL
) ENGINE=ARCHIVE DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `sessions`;
CREATE TABLE `sessions` (
  `id` CHAR(32) NOT NULL PRIMARY KEY, -- Session ID
  `user` INT UNSIGNED NOT NULL, -- User ID
  `lastdate` INT UNSIGNED NOT NULL, -- Last visited date
  `interval` INT UNSIGNED NOT NULL DEFAULT 0, -- Average visit time gap.
  `return` INT UNSIGNED NOT NULL DEFAULT 0, -- How many times returned.
  `ip` INT UNSIGNED NOT NULL -- User's IP
) ENGINE=MyISAM DEFAULT CHARSET='utf8' COLLATE=`utf8_general_ci`;

DROP TABLE IF EXISTS `cursessions`;
CREATE TABLE `cursessions` (
  `id` CHAR(32) NOT NULL PRIMARY KEY, -- Session ID
  `user` INT UNSIGNED NOT NULL, -- User ID
  `lastdate` INT UNSIGNED NOT NULL, -- Last visited date
  `interval` INT UNSIGNED NOT NULL DEFAULT 0, -- Average visit time gap.
  `ip` INT UNSIGNED NOT NULL -- User's IP
) ENGINE=Memory DEFAULT CHARSET='utf8' COLLATE=`utf8_general_ci`;

DROP TABLE IF EXISTS `shares`;
CREATE TABLE `shares` (
  `id` INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `resid` INT UNSIGNED NOT NULL,
  `owner` INT UNSIGNED NOT NULL,
  `to` INT UNSIGNED NULL,
  `restype` TINYINT UNSIGNED NOT NULL DEFAULT 0,
  `totype` TINYINT UNSIGNED NULL DEFAULT 0,
  `shared` INT UNSIGNED NOT NULL,
  `expires` INT UNSIGNED NULL DEFAULT 0,
  `password` CHAR(32) NOT NULL DEFAULT '',
  `mr0` INT UNSIGNED NOT NULL DEFAULT 0,
  INDEX `shares_restype` (`restype`),
  INDEX `shares_resid` (`resid`),
  INDEX `shares_owner` (`owner`),
  UNIQUE INDEX `shares_unq` (`resid`, `owner`, `to`, `restype`, `totype`)
) ENGINE=MyISAM DEFAULT CHARSET='utf8' COLLATE=`utf8_general_ci`;

DROP TABLE IF EXISTS `volume_vfiles`;
CREATE TABLE IF NOT EXISTS `volume_vfiles` (
  `id` int(10) unsigned NOT NULL,
  `size` bigint(20) unsigned NOT NULL,
  `path` varchar(32) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET='utf8' COLLATE=`utf8_general_ci`;

DROP TABLE IF EXISTS `volume_token`;
CREATE TABLE IF NOT EXISTS `volume_token` (
  `token` char(32) NOT NULL,
  `vid` int(10) unsigned NOT NULL,
  `date` int(10) unsigned NOT NULL,
  `filename` varchar(128) NOT NULL,
  PRIMARY KEY (`token`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=`utf8_general_ci`;

DROP TABLE IF EXISTS `credits`;
CREATE TABLE IF NOT EXISTS `credits` (
  `id` INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `name` CHAR(32) NOT NULL,
  `type` TINYINT UNSIGNED NOT NULL,
  `ratio` DOUBLE NOT NULL DEFAULT 1.0,
  `base` DOUBLE NOT NULL DEFAULT 0.0
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=`utf8_general_ci`;

