<?php
/*
*-------------------------------phpMyBitTorrent--------------------------------*
*--- The Ultimate BitTorrent Tracker and BMS (Bittorrent Management System) ---*
*--------------   Created By Antonio Anzivino (aka DJ Echelon)   --------------*
*-------------               http://www.p2pmania.it               -------------*
*----------         Developed By Emad Hossam (aka emadness)          ----------*
*-------------               http://www.fulldls.com               -------------*
*------------ Based on the Bit Torrent Protocol made by Bram Cohen ------------*
*-------------              http://www.bittorrent.com             -------------*
*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*
*--   This program is free software; you can redistribute it and/or modify   --*
*--   it under the terms of the GNU General Public License as published by   --*
*--   the Free Software Foundation; either version 2 of the License, or      --*
*--   (at your option) any later version.                                    --*
*--                                                                          --*
*--   This program is distributed in the hope that it will be useful,        --*
*--   but WITHOUT ANY WARRANTY; without even the implied warranty of         --*
*--   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          --*
*--   GNU General Public License for more details.                           --*
*--                                                                          --*
*--   You should have received a copy of the GNU General Public License      --*
*--   along with this program; if not, write to the Free Software            --*
*-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA --*
*--                                                                          --*
*------------------------------------------------------------------------------*
*------              ©2005 phpMyBitTorrent Development Team              ------*
*-----------               http://phpmybittorrent.com               -----------*
*------------------------------------------------------------------------------*
*/

if (!file_exists("include/config.php")) die("You must run this file from your phpMyBitTorrent ROOT directory, where index.php is!!");

function result($status) {
        if ($status == "ok") echo "<font color=\"#00FF00\">Done</font>";
        else echo "<font color=\"#FF0000\">Error!</font>";
}

function sql_run($sql, $descr, $failtext = "") {
        global $db;
        echo "<p>".$descr."...";
        if ($db->sql_query($sql)) result("ok");
        else {
                result("error");
                $err = Array();
                $err = $db->sql_error();
                echo("<br>");
                echo(_btsqlerror2.$err["code"]);
                echo("<br>");
                echo(_btsqlerror3.$err["message"]);

                $db->sql_query("ROLLBACK;"); //works only if in a transactional environment
                if (!empty($failtext)) echo "<br />".$failtext;
                die()
        }
        echo "</p>";
}

include("include/config.php");
include("language/english.php");
ob_implicit_flush(1);

echo "<h1>Upgrade from 0.6 to 0.10</h1>";

echo "<p>Detecting if in a transactional environment...";
if ($db->sql_query("START TRANSACTION;")) result("ok");
else {
        result("error");
        echo "<br>If anything goes wrong you'll have to manually upgrade your database";
}
echo "</p>";


sql_run("DROP TABLE `torrent_download`;","Dropping old download log");

sql_run("ALTER TABLE `torrent_torrents` ADD `private` ENUM( 'true', 'false' ) DEFAULT 'false' NOT NULL AFTER `banned` , ADD `min_ratio` FLOAT( 3 ) UNSIGNED DEFAULT '0.00' NOT NULL AFTER `private` ;","Adding new private options to Torrents");
sql_run("ALTER TABLE `torrent_torrents` ADD `password` VARCHAR( 255 ) AFTER `banned` ;","Adding the password field to Torrents");
sql_run("ALTER TABLE `torrent_torrents` ADD `tracker_list` TEXT AFTER `tracker` ;","Adding tracker list field for torrents");
sql_run("ALTER TABLE `torrent_torrents` CHANGE `info_hash` `info_hash` VARCHAR( 20 ) BINARY DEFAULT NULL;","Making info hash binary safe");
sql_run("ALTER TABLE `torrent_torrents` ADD `backup_tracker` ENUM( 'true', 'false' ) DEFAULT 'false' NOT NULL AFTER `dht` ;","Adding the backup tracker field to Torrents");
sql_run("ALTER TABLE `torrent_torrents` ADD INDEX ( `leechers` ) ;","Adding leechers index");
sql_run("ALTER TABLE `torrent_torrents` ADD INDEX ( `tot_peer` ) ;","Adding tot peer index");
sql_run("ALTER TABLE `torrent_torrents` ADD INDEX ( `banned` ) ;","Adding banned index");
sql_run("ALTER TABLE `torrent_torrents` ADD INDEX ( `password` ) ;","Adding password index");
sql_run("ALTER TABLE `torrent_torrents` ADD INDEX ( `tracker` ) ;","Adding tracker index");
sql_run("ALTER TABLE `torrent_torrents` ADD INDEX ( `evidence` ) ;","Adding evidence index");
sql_run("ALTER TABLE `torrent_torrents` ADD INDEX `rating` ( `numratings` , `ratingsum` );","Adding rating index");
sql_run("ALTER TABLE `torrent_torrents` ADD INDEX ( `numfiles` ) ;","Adding numfiles index");
sql_run("ALTER TABLE `torrent_torrents` ADD INDEX ( `downloaded` ) ;","Adding downloaded index");
sql_run("ALTER TABLE `torrent_torrents` DROP INDEX `category_visible` ;","Dropping category+visible index");
sql_run("ALTER TABLE `torrent_torrents` ADD INDEX ( `category` ) ;","Adding category index");
sql_run("ALTER TABLE `torrent_torrents` ADD INDEX ( `type` ) ;","Adding type index");


sql_run("ALTER TABLE `torrent_categories` ADD UNIQUE (`name`);","Making category name unique");
sql_run("ALTER TABLE `torrent_categories` ADD INDEX ( `sort_index` );","Adding category sort index");

sql_run("ALTER TABLE `torrent_files` ADD UNIQUE (`torrent` ,`filename`);","Making torrent filename unique");


sql_run("ALTER TABLE `torrent_config` ADD `upload_level` ENUM( 'all', 'user', 'premium' ) DEFAULT 'user' NOT NULL AFTER `gfx_check` ;","Adding the upload level field");
sql_run("ALTER TABLE `torrent_config` ADD `allow_backup_tracker` ENUM( 'true', 'false' ) DEFAULT 'false' NOT NULL AFTER `minupload_size_file` ;","Adding the backup tracker field");
sql_run("ALTER TABLE `torrent_config` ADD `allow_html` ENUM( 'true', 'false' ) NOT NULL AFTER `welcome_message` ;","Adding the option to allow HTML or not");
sql_run("ALTER TABLE `torrent_config` ADD `torrent_prefix` VARCHAR( 255 ) AFTER `rewrite_engine` ;","Adding the torrent prefix option");
sql_run("ALTER TABLE `torrent_config` ADD `global_min_ratio` FLOAT UNSIGNED DEFAULT '0.00' NOT NULL AFTER `min_share_seed` ;","Adding the global minimum ratio option");
sql_run("ALTER TABLE `torrent_config` DROP `lang_level`;","Removing the language level option");
sql_run("ALTER TABLE `torrent_config` ADD `announce_text` VARCHAR( 255 ) AFTER `welcome_message` ;","Adding the announce message option");
sql_run("ALTER TABLE `torrent_config` ADD `torrent_complaints` ENUM( 'true', 'false' ) DEFAULT 'false' NOT NULL AFTER `down_limit` ;","Adding the torrent complaint option");
sql_run("ALTER TABLE `torrent_config`  DROP `max_num_file_day_e`, DROP `max_num_file_week_e`;","Dropping unused options");
sql_run("ALTER TABLE `torrent_config` DROP `maxupload_day_num` ;","Dropping unused option: maxupload day num");
sql_run("ALTER TABLE `torrent_config` DROP `maxupload_day_share` ;","Dropping unused option: maxupload day share");


sql_run("ALTER TABLE `torrent_users` ADD `banreason` VARCHAR( 255 ) AFTER `newpasswd` ;","Adding the ban reason field to user");
sql_run("ALTER TABLE `torrent_users` ADD `ban` INT( 1 ) UNSIGNED DEFAULT '0' NOT NULL AFTER `active` ;","Adding the ban field to user");
sql_run("UPDATE torrent_users SET level = 'user', ban = 1 WHERE level = 'banned';","Users that were of \"banned\" class become users with ban flag enabled");
sql_run("ALTER TABLE `torrent_users` CHANGE `level` `level` ENUM( 'user', 'premium', 'moderator', 'admin' ) NOT NULL DEFAULT 'user';","Then, removing the banned class and adding the moderator class");
sql_run("ALTER TABLE `torrent_users` ADD `passkey` VARCHAR( 32 ) NULL AFTER `act_key` ;","Adding the passkey field");
sql_run("ALTER TABLE `torrent_users` ADD UNIQUE (`passkey`);","Making Passkey unique");
sql_run("ALTER TABLE `torrent_users` CHANGE `lastip` `lastip` INT( 10 ) UNSIGNED DEFAULT '0' NOT NULL;","Changing the format of user last ip");
sql_run("ALTER TABLE `torrent_users` CHANGE `lasthost` `lasthost` VARCHAR( 255 ) NOT NULL;","Changing the format of user last host");
sql_run("ALTER TABLE `torrent_users` CHANGE `lasthost` `lasthost` VARCHAR( 255 ) NOT NULL;","Changing the format of user last host");
sql_run("ALTER TABLE `torrent_users` ADD `jabber` VARCHAR( 255 ) AFTER `icq` ;","Adding Jabber contact");
sql_run("ALTER TABLE `torrent_users` ADD `skype` VARCHAR( 255 ) AFTER `msn` ;","Adding Skype contact");
sql_run("ALTER TABLE `torrent_users` ADD INDEX `banned` (`ban`) ;","Adding banned index");

sql_run("CREATE TABLE `torrent_download_completed` (`user` INT( 11 ) UNSIGNED NOT NULL ,`torrent` INT( 15 ) UNSIGNED NOT NULL ,`completed` DATETIME NOT NULL DEFAULT 'NOW()') TYPE = MYISAM ;","Creating completed downloads table");
sql_run("ALTER TABLE `torrent_download_completed` ADD PRIMARY KEY ( `user` , `torrent` ) ;","Defining primary key for the above table");
sql_run("ALTER TABLE `torrent_download_completed` ADD INDEX ( `torrent` ) ;","Adding index key for the above table");
sql_run("ALTER TABLE `torrent_download_completed` DROP INDEX `user` ;","Removing user index for the above table");


sql_run("ALTER TABLE `torrent_peers` CHANGE `uid` `uid` INT( 11 ) UNSIGNED DEFAULT NULL;","Changing format of peer user id field");
sql_run("ALTER TABLE `torrent_peers` CHANGE `ip` `ip` INT( 10 ) UNSIGNED DEFAULT '0' NOT NULL ;","Changing format of peer ip field");
sql_run("ALTER TABLE `torrent_peers` CHANGE `real_ip` `real_ip` INT( 10 ) UNSIGNED DEFAULT '0' NOT NULL ;","Changing format of peer real ip field");
sql_run("ALTER TABLE `torrent_peers` ADD INDEX ( `torrent` , `seeder` );","Adding index on torrent and seeder");
sql_run("ALTER TABLE `torrent_peers` ADD UNIQUE (`torrent` ,`unique_id`);","Unique id is unique for each torrent");
sql_run("ALTER TABLE `torrent_peers` CHANGE `uid` `uid` INT( 11 ) UNSIGNED DEFAULT '0' NOT NULL ;","Changing uid type");


sql_run("DROP TABLE `torrent_log`;","Removing the old useless log table");

sql_run("ALTER TABLE `torrent_trackers` CHANGE `status` `status` ENUM( 'active', 'dead', 'blacklisted' ) NOT NULL DEFAULT 'active';","Adding the blacklisted tracker field");
sql_run("ALTER TABLE `torrent_trackers` ADD `support` ENUM( 'selective', 'global', 'single' ) DEFAULT 'selective' NOT NULL AFTER `url` ;","Adding scrape support for trackers");

sql_run("ALTER TABLE `torrent_online_users` DROP INDEX `id`;","New index in online users table");
sql_run("ALTER TABLE `torrent_online_users` ADD PRIMARY KEY ( `id` ) ;","Defining primary key for online users table");


sql_run("ALTER TABLE `torrent_private_messages` CHANGE `from` `sender` INT( 11 ) UNSIGNED DEFAULT '0' NOT NULL ;","Renaming PM field: sender");
sql_run("ALTER TABLE `torrent_private_messages` CHANGE `to` `recipient` INT( 11 ) UNSIGNED DEFAULT '0' NOT NULL;","Renaming PM field: recipient");
sql_run("ALTER TABLE `torrent_private_messages` CHANGE `read` `is_read` ENUM( 'true', 'false' ) DEFAULT 'false' NOT NULL ;","Renaming PM field: is read");


sql_run("CREATE TABLE `torrent_private_messages_bookmarks` (`master` int(11) unsigned NOT NULL default '0', `slave` int(11) unsigned NOT NULL default '0',  PRIMARY KEY  (`master`,`slave`),  KEY `master` (`master`)) TYPE=MyISAM;","Creating PM bookmarks table");
sql_run("CREATE TABLE `torrent_private_messages_blacklist` ( `master` int(11) unsigned NOT NULL default '0',  `slave` int(11) unsigned NOT NULL default '0',  PRIMARY KEY  (`master`,`slave`),  KEY `master` (`master`)) TYPE=MyISAM;","Creating PM blacklist table");


echo "<p>&nbsp;</p>";
echo "<p>Please stand by while updating md5sum hash info from Torrents...</p>";

$torrenthandle = opendir($torrent_dir);

while ($tfile = readdir($torrenthandle)) {
        if (!preg_match("/^([0-9]*)\.torrent$/",$tfile,$idmatch)) continue;
        $id = $idmatch[1];
        $db->sql_query("UPDATE ".$db_prefix."_torrents SET md5sum = '".md5_file($torrent_dir."/".$tfile)."' WHERE id = '".$id."';");
}
closedir($torrenthandle);

sql_run("UPDATE torrent_config SET version = '1.0';","Finally, updating version number!");

$db->sql_query("COMMIT;");

echo "<h3>Upgrade complete!</h3>";
?>
