<?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("header.php");
ob_implicit_flush(1);

echo "<h1>Upgrade from 0.1 to 0.4</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("ALTER TABLE `torrent_users` ADD INDEX `date` ( `regdate` );","Adding date index to user table");
sql_run("ALTER TABLE `torrent_users` ADD `avatar` VARCHAR( 255 ) AFTER `language` ;","Adding avatar field");
sql_run("ALTER TABLE `torrent_users` CHANGE `theme` `theme` VARCHAR( 255 ) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL;","Setting theme to default NULL");
sql_run("ALTER TABLE `torrent_download` DROP INDEX `torrent`;","Dropping useless index from download table");
sql_run("ALTER TABLE `torrent_download` DROP INDEX `user`;","Dropping useless index from download table");
sql_run("ALTER TABLE `torrent_download` ADD PRIMARY KEY ( `user` , `torrent` );","Adding primary key to download table");
sql_run("ALTER TABLE `torrent_trackers` CHANGE `update` `updated` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00';","Changing last update time field for external trackers");
sql_run("ALTER TABLE `torrent_smiles` DROP INDEX `code`;", "Dropping useless index from smiles table");
sql_run("ALTER TABLE `torrent_smiles` ADD UNIQUE (`code`) ;","Adding unique index in smiles table");
sql_run("ALTER TABLE `torrent_smiles` CHANGE `id` `id` INT( 5 ) UNSIGNED NOT NULL AUTO_INCREMENT;","Changing smile ID field");
sql_run("ALTER TABLE `torrent_users` CHANGE `avatar` `avatar` VARCHAR( 255 ) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL DEFAULT 'blank.gif';","Changing avatar default value");
sql_run("ALTER TABLE `torrent_users` CHANGE `newpasswd` `newpasswd` VARCHAR( 40 ) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL;","Changing new password field in user table");
sql_run("CREATE TABLE torrent_bans (id int(11) unsigned NOT NULL auto_increment,`from` int(10) unsigned NOT NULL default '0', `to` int(10) unsigned NOT NULL default '0', reason varchar(255) NOT NULL default '', `date` datetime NOT NULL default '0000-00-00 00:00:00', PRIMARY KEY  (id), UNIQUE KEY ip_unique (`from`,`to`)) TYPE=MyISAM;","Creating ban table");
sql_run("CREATE TABLE `torrent_shouts` (  `id` bigint(20) unsigned NOT NULL auto_increment,  `user` int(10) unsigned NOT NULL default '0',  `text` varchar(255) NOT NULL default '',  `posted` datetime NOT NULL default '0000-00-00 00:00:00',  PRIMARY KEY  (`id`),  UNIQUE KEY `anti_flood` (`user`,`text`),  KEY `posted` (`posted`)) TYPE=MyISAM AUTO_INCREMENT=18 ;","Creating shoutbox table");
sql_run("ALTER TABLE `torrent_users` ADD `accept_mail` ENUM( 'yes', 'no' ) DEFAULT 'no' NOT NULL AFTER `avatar` , ADD `aim` VARCHAR( 255 ) DEFAULT NULL AFTER `accept_mail` , ADD `icq` VARCHAR( 10 ) DEFAULT NULL AFTER `aim` , ADD `msn` VARCHAR( 255 ) DEFAULT NULL AFTER `icq` , ADD `yahoo` VARCHAR( 255 ) DEFAULT NULL AFTER `msn` ;","Adding user private contacts");
sql_run("UPDATE torrent_users SET theme = NULL, avatar = 'blank.gif';","Updating existing users data");
sql_run("ALTER TABLE `torrent_torrents` CHANGE `info_hash` `info_hash` BINARY( 20 ) NOT NULL;","Changing info hash to binary");
sql_run("ALTER TABLE `torrent_peers` DROP INDEX `torrent_seeder`;","Dropping useless index from peer table");
sql_run("ALTER TABLE `torrent_complaints` ADD PRIMARY KEY ( `torrent` , `user` );","Adding main index to complaint table");
sql_run("ALTER TABLE `torrent_complaints` DROP INDEX `torrent`;","Dropping useless index from complaint table");
sql_run("ALTER TABLE `torrent_config` DROP `time_check`;","Dropping unused config parameter");
sql_run("INSERT INTO `torrent_smiles` ( `code`, `file`, `alt`) VALUES
(':)', 'icon_smile.gif', 'Smile'),
(':-)', 'icon_smile.gif', 'Smile'),
(':smile:', 'icon_smile.gif', 'Smile'),
(':(', 'icon_sad.gif', 'Sad'),
(':-(', 'icon_sad.gif', 'Sad'),
(':sad:', 'icon_sad.gif', 'Sad'),
(':D', 'icon_biggrin.gif', 'Very Happy'),
(':-D', 'icon_biggrin.gif', 'Very Happy'),
(':grin:', 'icon_biggrin.gif', 'Very Happy'),
( ';-)', 'icon_wink.gif', 'Wink'),
( ';)', 'icon_wink.gif', 'Wink'),
( ':wink:', 'icon_wink.gif', 'Wink'),
(':o', 'icon_surprised.gif', 'Surprised'),
(':-o', 'icon_surprised.gif', 'Surprised'),
(':eek:', 'icon_surprised.gif', 'Surprised'),
( ':x', 'icon_mad.gif', 'Mad'),
(':-x', 'icon_mad.gif', 'Mad'),
( ':mad:', 'icon_mad.gif', 'Mad'),
('8O', 'icon_eek.gif', 'Shocked'),
('8-O', 'icon_eek.gif', 'Shocked'),
(':shock:', 'icon_eek.gif', 'Shocked'),
(':?', 'icon_confused.gif', 'Confused'),
(':-?', 'icon_confused.gif', 'Confused'),
(':???:', 'icon_confused.gif', 'Confused'),
('8)', 'icon_cool.gif', 'Cool'),
('8-)', 'icon_cool.gif', 'Cool'),
(':cool:', 'icon_cool.gif', 'Cool'),
(':lol:', 'icon_lol.gif', 'Laughing'),
( ':P', 'icon_razz.gif', 'Razz'),
( ':-P', 'icon_razz.gif', 'Razz'),
( ':razz:', 'icon_razz.gif', 'Razz'),
( ':oops:', 'icon_redface.gif', 'Embarassed'),
( ':cry:', 'icon_cry.gif', 'Crying or Very sad'),
( ':evil:', 'icon_evil.gif', 'Evil or Very Mad'),
( ':twisted:', 'icon_twisted.gif', 'Twisted Evil'),
( ':roll:', 'icon_rolleyes.gif', 'Rolling Eyes'),
( ':!:', 'icon_exclaim.gif', 'Exclamation'),
( ':?:', 'icon_question.gif', 'Question'),
( ':arrow:', 'icon_arrow.gif', 'Arrow'),
( ':|', 'icon_neutral.gif', 'Neutral'),
( ':-|', 'icon_neutral.gif', 'Neutral'),
( ':neutral:', 'icon_neutral.gif', 'Neutral'),
( ':bom:', 'bom.gif', 'bom'),
( ':pale:', 'icon_pale.gif', 'pale'),
( ':pirate:', 'icon_pirat.gif', 'pirate'),
( ':profileleft:', 'icon_profileleft.gif', 'profile_left'),
( ':profileright:', 'icon_profileright.gif', 'profile_right'),
( ':salute:', 'icon_salut.gif', 'salute'),
( ':santa:', 'icon_santa.gif', 'santa'),
( ':scratch:', 'icon_scratch.gif', 'scratch'),
( ':silent:', 'icon_silent.gif', 'silent'),
( ':thumbright:', 'icon_thumleft.gif', 'thumbright'),
( ':mumum:', 'mumum.gif', 'mumum'),
( ':angel4:', 'angel4.gif', 'angel4'),
( ':angry4:', 'angry4.gif', 'angry4'),
( ':banghead:', 'BangHead.gif', 'bandhead'),
( ':coffee:', 'coffee.gif', 'coffee'),
( ':crybaby:', 'crybaby2.gif', 'crybaby'),
( ':director:', 'director.gif', 'director'),
( ':evil1:', 'evil1.gif', 'evil1'),
( ':glasses1:', 'glasses1.gif', 'glasses1'),
( ':iconbiggrin:', 'icon_biggrin.gif', 'icon_biggrin'),
( ':icon_smile:', 'icon_smile.gif', 'icon_smile'),
(':laughing6:', 'laughing7.gif', 'laughing6'),
( ':occasion2:', 'occasion5.gif', 'occasion2'),
( ':occasion5:', 'occasion14.gif', 'occasion5'),
( ':protest:', 'protest.gif', 'protest'),
(':violent1:', 'violent1.gif', 'violent1');","Inserting default smileys");

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

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

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