function upgrade_bans_table() { $dbi = new ThornDBI(); // First verify if the table is already upgraded. $result = $dbi->myquery("SHOW COLUMNS FROM `" . THbans_table . "` LIKE 'ip_octet3'"); if (mysql_num_rows($result) > 0) { die("Database has already been modified!"); } // Get all of the old bans from the DB $bans = $dbi->mymultiarray("SELECT * FROM `" . THbans_table . "` WHERE 1"); // This could take a while. // Store them in a temp file file_put_contents(THpath . "upgrade_install_temp.php", var_export($bans, true), FILE_TEXT) or die("Could not open upgrade_install_temp.php for writing."); // Convert to a new type $bans_new = array(); // This will hold the converted ones. $octets = array(); // Used to hold IP octets $single_ban = array(); // Used to hold a converted ban foreach ($bans as $old_ban) { // Convert the IP (in long integer format) from the old ban, // and segment it into the new octet fields $octets = explode(".", long2ip($old_ban['ip']), 4); $single_ban['ip_octet1'] = intval($octets[0]); $single_ban['ip_octet2'] = intval($octets[1]); $single_ban['ip_octet3'] = intval($octets[2]); // If subnet in the old ban is true, set the // new ban's 4th octet to be the wildcard value of -1, // otherwise proceed as normal if ($old_ban['subnet'] != 0) { $single_ban['ip_octet4'] = -1; } else { $single_ban['ip_octet4'] = intval($octets[3]); } // Everything else is a straight copyover $single_ban['publicreason'] = $old_ban['publicreason']; $single_ban['privatereason'] = $old_ban['privatereason']; $single_ban['adminreason'] = $old_ban['adminreason']; $single_ban['postdata'] = $old_ban['postdata']; $single_ban['duration'] = $old_ban['duration']; $single_ban['bantime'] = $old_ban['bantime']; $single_ban['bannedby'] = $old_ban['bannedby']; $bans_new[] = $single_ban; // Add it into the array } $bans = null; // Clean up // Drop old bans table $result = $dbi->myquery("DROP TABLE `" . THbans_table . "`"); if ($result === null) { die("DROP Error " . mysql_errno($dbi->cxn) . ": " . mysql_error($dbi->cxn) . "\n"); } // Insert new bans table $query = "CREATE TABLE `" . THbans_table . "` \r\n\t\t( \r\n\t\t`id` int unsigned NOT NULL auto_increment, \r\n\t\t`ip_octet1` int NOT NULL, \r\n\t\t`ip_octet2` int NOT NULL, \r\n\t\t`ip_octet3` int NOT NULL, \r\n\t\t`ip_octet4` int NOT NULL, \r\n\t\t`publicreason` text NOT NULL, \r\n\t\t`privatereason` text NOT NULL, \r\n\t\t`adminreason` text NOT NULL, \r\n\t\t`postdata` longtext NOT NULL, \r\n\t\t`duration` int(11) NOT NULL default '-1', \r\n\t\t`bantime` int(11) unsigned NOT NULL, \r\n\t\t`bannedby` varchar(100) NOT NULL, \r\n\t\tPRIMARY KEY (`id`) \r\n\t\t) ENGINE=MyISAM character set utf8 collate utf8_unicode_ci;"; $result = $dbi->myquery($query); if ($result === null) { die("CREATE Error " . mysql_errno($dbi->cxn) . ": " . mysql_error($dbi->cxn) . "\n"); } // Insert converted bans $successful = 1; // set to 0 when one of these insert queries fails foreach ($bans_new as $insert) { $banquery = "insert into `" . THbans_table . "` \r\n\t\t\tset ip_octet1=" . $insert['ip_octet1'] . ",\r\n\t\t\tip_octet2=" . $insert['ip_octet2'] . ",\r\n\t\t\tip_octet3=" . $insert['ip_octet3'] . ",\r\n\t\t\tip_octet4=" . $insert['ip_octet4'] . ",\r\n\t\t\tprivatereason='" . $dbi->clean($insert['privatereason']) . "', \r\n\t\t\tpublicreason='" . $dbi->clean($insert['publicreason']) . "', \r\n\t\t\tadminreason='" . $dbi->clean($insert['adminreason']) . "', \r\n\t\t\tpostdata='" . $dbi->clean($insert['postdata']) . "', \r\n\t\t\tduration=" . $insert['duration'] . ", \r\n\t\t\tbantime=" . $insert['bantime'] . ", \r\n\t\t\tbannedby='" . $dbi->clean($insert['bannedby']) . "'"; $result = $dbi->myquery($banquery); if ($result === null) { printf("Insert Error for %d.%d.%d.%s: #%d: %s<br />\n", $insert['ip_octet1'], $insert['ip_octet2'], $insert['ip_octet3'], $insert['ip_octet4'] == -1 ? "*" : $insert['ip_octet4'], mysql_errno($dbi->cxn), mysql_error($dbi->cxn)); $successful = 0; // One bad insert ruins the lot. } } // Did it work? if ($successful == 1) { echo "Success!"; unlink("upgrade_install_temp.php"); } }