<?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.4 to 0.6</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_config`  DROP `comments_per_page`,  DROP `autoclean_interval`,  DROP `max_dead_torrent_time`,  DROP `src_url`;","Dropping unused configuration parameters");
sql_run("ALTER TABLE `torrent_config` DROP `timeout_repeat_tu`;","Dropping unused configuration parameters");
sql_run("ALTER TABLE `torrent_config` DROP `default_limit` ;","Dropping unused configuration parameters");
sql_run("ALTER TABLE `torrent_torrents` ADD `dht` ENUM( 'yes', 'no' ) DEFAULT 'no' NOT NULL AFTER `exeem` ;","Adding DHT field to Torrent table");
sql_run("ALTER TABLE `torrent_torrents` ADD `plen` BIGINT( 6 ) UNSIGNED NOT NULL AFTER `descr` ;","Adding Piece Length field to Torrent table");
sql_run("DROP TABLE `torrent_filter`;","Deleting old keyword filter table");
sql_run("CREATE TABLE `torrent_filter` (`id` INT( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT ,`keyword` VARCHAR( 50 ) NOT NULL ,`reason` VARCHAR( 255 ) NOT NULL ,PRIMARY KEY ( `id` ) ,UNIQUE (`keyword`)) TYPE = MYISAM ;","Rebuilding Keyword Filter table");
sql_run("ALTER TABLE `torrent_peers` ADD `unique_id` VARCHAR( 20 ) AFTER `peer_id` ;","Adding Peer Unique ID Support");
sql_run("ALTER TABLE `torrent_peers` ADD `uid` INT( 11 ) AFTER `id` ;","User ID automatically stored in peer table");
sql_run("TRUNCATE TABLE `torrent_peers`;","Resetting peer table to avoid problems on next upgrade. Don't worry, peers will just reconnect");
sql_run("ALTER TABLE `torrent_peers` CHANGE `ip` `ip` INT( 10 ) NOT NULL DEFAULT '0' ;","IP is stored as integer value");
sql_run("ALTER TABLE `torrent_peers` ADD `real_ip` INT( 10 ) NOT NULL AFTER `port` ;","Declared and real IP now different");
sql_run("ALTER TABLE `torrent_peers` ADD `client` VARCHAR( 60 ) ;","Adding client support to peer table");
sql_run("ALTER TABLE `torrent_users` CHANGE `lastip` `lastip` INT( 10 ) NOT NULL DEFAULT '0';","Last IP in user table stored as integer");
sql_run("ALTER TABLE `torrent_config` ADD `announce_interval_min` INT( 10 ) UNSIGNED NOT NULL AFTER `max_torrent_size` ;","Adding Minimum Announce Interval configuration parameter");
sql_run("ALTER TABLE `torrent_config` ADD `dead_torrent_interval` INT( 10 ) UNSIGNED NOT NULL AFTER `announce_interval` ;";"Adding Dead Torrent Interval configuration parameter");
sql_run("ALTER TABLE `torrent_config` CHANGE `lang_level` `lang_level` ENUM( 'auto', 'fixed' ) NOT NULL DEFAULT 'auto';","Removing \"custom\" setting for Language Setting");
sql_run("ALTER TABLE `torrent_config` ADD `welcome_message` LONGTEXT AFTER `theme` ;","Adding Welcome Message configuration parameter");
sql_run("CREATE TABLE `torrent_online_users` (`id` INT( 60 ) UNSIGNED NOT NULL ,`page` VARCHAR( 255 ) NOT NULL ,`logged_in` DATETIME NOT NULL ,`last_action` DATETIME NOT NULL ,UNIQUE (`id`)) TYPE = MYISAM ;","Adding Online Users Table for future usage");
sql_run("ALTER TABLE `torrent_bans` CHANGE `from` `ipstart` INT( 10 ) UNSIGNED NOT NULL DEFAULT '0';","Renaming fields in Bans table for SQL compliance");
sql_run("ALTER TABLE `torrent_bans` CHANGE `to` `ipend` INT( 10 ) UNSIGNED NOT NULL DEFAULT '0';","Renaming fields in Bans table for SQL compliance");
sql_run("ALTER TABLE `torrent_torrents` ADD `torrent_descr` TEXT NOT NULL AFTER `descr` ;","Adding Original Torrent Description in Torrent table");
sql_run("ALTER TABLE `torrent_torrents` ADD FULLTEXT (`torrent_descr`);","Original Torrent Description set as FULLTEXT index");
sql_run("ALTER TABLE `torrent_peers` ADD `version` VARCHAR( 10 ) NOT NULL AFTER `client` , ADD `user_agent` VARCHAR( 255 ) NOT NULL AFTER `version` ;","Adding Client Version and full User Agent to Peer table for future administrative usage");

echo "<p>&nbsp;</p>";
echo "<p>Please stand by while retrieving DHT and Piece Length Info from Torrents...</p>";

$torrenthandle = opendir($torrent_dir);

while ($tfile = readdir($torrenthandle)) {
        if (!preg_match("/^([0-9]*)\.torrent$/",$tfile,$idmatch)) continue;
        $id = $idmatch[1];
        $fp = fopen($torrent_dir."/".$tfile,"rb");
        for ($page = ""; !feof($fp); $page .= fread($fp,1000));
        $torrent = bdec($page);
        unset($page);
        $dht = "no";
        if (dict_exists($torrent,"azureus_properties")) {
                list ($azureus_properties) = dict_check($torrent,"azureus_properties");
                if (dict_exists($azureus_properties,"dht_backup_enable")) {
                        list ($dht_backup_enable) = dict_check($azureus_properties,"dht_backup_enable(integer)");
                        if ($dht_backup_enable == 1) $dht = "yes";
                        unset($dht_backup_enable);
                }
                unset($azureus_properties);
        }
        $info = dict_check($torrent,"info");
        $plen = dict_check($torrent,"piece length"]);
        unset($torrent);
        $db->sql_query("UPDATE ".$db_prefix."_torrents SET dht = '".$dht."', plen = '".$plen."' WHERE id = '".$id."';");
}
closedir($torrenthandle);

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

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

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