📄 upgrade.php
字号:
$result = query('SHOW TABLES', "Couldn't get list of current tables");
while( $table = $db->sql_fetchrow($result) )
{
$currenttables[] = $table[0];
}
// Check what tables we need to CREATE
while( list($table, $definition) = each($table_def) )
{
if ( !inarray($table, $currenttables) )
{
print " * Creating $table :: ";
query($definition, "Couldn't create table $table");
print "<span class=\"ok\"><b>OK</b></span><br />\n";
}
}
end_step('create_config');
case 'create_config':
print " * Inserting new values into new layout config table :: ";
@reset($inserts);
while( list($table, $inserts_table) = each($inserts) )
{
if ( $table == CONFIG_TABLE )
{
$per_pct = ceil( count($inserts_table) / 40 );
$inc = 0;
while( list($nr, $insert) = each($inserts_table) )
{
query($insert, "Couldn't insert value into config table");
$inc++;
if ( $inc == $per_pct )
{
print ".";
flush();
$inc = 0;
}
}
}
}
print " <span class=\"ok\"><b>OK</b></span><br />\n";
end_step('convert_config');
case 'convert_config':
print " * Converting configuration table :: ";
$sql = "SELECT *
FROM $table_prefix" . "old_config";
$result = query($sql, "Couldn't get info from old config table");
$oldconfig = $db->sql_fetchrow($result);
//
// We don't need several original config types and two others
// have changed name ... so take account of this.
//
$ignore_configs = array("selected", "admin_passwd", "override_themes", "allow_sig");
$rename_configs = array(
"email_from" => "board_email",
"email_sig" => "board_email_sig"
);
while( list($name, $value) = each($oldconfig) )
{
if ( is_int($name) )
{
continue;
}
if ( !inarray($name, $ignore_configs) )
{
$name = ( !empty($rename_configs[$name]) ) ? $rename_configs[$name] : $name;
// phpBB 1.x has some problems with escaping strings in the database. Try to correct for
// this by removing all slashes and then escaping once.
$sql = "REPLACE INTO " . CONFIG_TABLE . " (config_name, config_value)
VALUES ('$name', '".addslashes(stripslashes(stripslashes($value)))."')";
query($sql, "Couldn't update config table with values from old config table");
}
}
$sql = "UPDATE " . CONFIG_TABLE . "
SET config_value = 'dutch'
WHERE config_name = 'default_lang' && config_value = 'nederlands'";
query($sql, "Couldn't rename 'nederlands' to 'dutch' in config table");
print "<span class=\"ok\"><b>OK</b></span><br />\n";
end_step('convert_ips');
case 'convert_ips':
$names = array(
POSTS_TABLE => array(
'id' => 'post_id',
'field' => 'poster_ip'
),
PRIVMSGS_TABLE => array(
'id' => 'msg_id',
'field' => 'poster_ip'
),
BANLIST_TABLE => array(
'id' => 'ban_id',
'field' => 'ban_ip'
)
);
lock_tables(1, array(POSTS_TABLE, PRIVMSGS_TABLE, BANLIST_TABLE));
$batchsize = 2000;
while( list($table, $data_array) = each($names) )
{
$sql = "SELECT MAX(" . $data_array['id'] . ") AS max_id
FROM $table";
$result = query($sql, "Couldn't obtain ip data from $table (" . $fields . ")");
$row = $db->sql_fetchrow($result);
$maxid = $row['max_id'];
for($i = 0; $i <= $maxid; $i += $batchsize)
{
$batchstart = $i;
$batchend = $i + $batchsize;
$field_id = $data_array['id'];
$field = $data_array['field'];
print " * Converting IP format '" . $field . "' / '$table' ( $batchstart to $batchend ) :: ";
flush();
$sql = "SELECT $field_id, $field
FROM $table
WHERE $field_id
BETWEEN $batchstart
AND $batchend";
$result = query($sql, "Couldn't obtain ip data from $table (" . $fields . ")");
$per_pct = ceil( $db->sql_numrows($result) / 40 );
$inc = 0;
while( $row = $db->sql_fetchrow($result) )
{
$sql = "UPDATE $table
SET $field = '" . encode_ip($row[$field]) . "'
WHERE $field_id = " . $row[$field_id];
query($sql, "Couldn't convert IP format of $field in $table with $field_id of " . $rowset[$field_id]);
$inc++;
if ( $inc == $per_pct )
{
print ".";
flush();
$inc = 0;
}
}
print " <span class=\"ok\"><b>OK</b></span><br />\n";
}
}
lock_tables(0);
end_step('convert_dates');
case 'convert_dates':
$names = array(
POSTS_TABLE => array('post_time'),
TOPICS_TABLE => array('topic_time'),
PRIVMSGS_TABLE => array('msg_time')
);
lock_tables(1, array(POSTS_TABLE, TOPICS_TABLE, PRIVMSGS_TABLE));
while( list($table, $fields) = each($names) )
{
print " * Converting date format of $fields[$i] in $table :: ";
flush();
for($i = 0; $i < count($fields); $i++)
{
$sql = "UPDATE $table
SET " . $fields[$i] . " = UNIX_TIMESTAMP(" . $fields[$i] . ")";
query($sql, "Couldn't convert date format of $table(" . $fields[$i] . ")");
}
print "<span class=\"ok\"><b>OK</b></span><br />\n";
}
lock_tables(0);
end_step('fix_addslashes');
case 'fix_addslashes':
$slashfields[TOPICS_TABLE] = array('topic_title');
$slashfields[FORUMS_TABLE] = array('forum_desc', 'forum_name');
$slashfields[CATEGORIES_TABLE] = array('cat_title');
$slashfields[WORDS_TABLE] = array('word', 'replacement');
$slashfields[RANKS_TABLE] = array('rank_title');
$slashfields[DISALLOW_TABLE] = array('disallow_username');
//convert smilies?
$slashes = array(
"\\'" => "'",
"\\\"" => "\"",
"\\\\" => "\\");
$slashes = array(
"\\'" => "'",
"\\\"" => "\"",
"\\\\" => "\\");
lock_tables(1, array(TOPICS_TABLE, FORUMS_TABLE, CATEGORIES_TABLE, WORDS_TABLE, RANKS_TABLE, DISALLOW_TABLE, SMILIES_TABLE));
while( list($table, $fields) = each($slashfields) )
{
print " * Removing slashes from $table table :: ";
flush();
while( list($nr, $field) = each($fields) )
{
@reset($slashes);
while( list($search, $replace) = each($slashes) )
{
$sql = "UPDATE $table
SET $field = REPLACE($field, '" . addslashes($search) . "', '" . addslashes($replace) . "')";
query($sql, "Couldn't remove extraneous slashes from the old data.");
}
}
print "<span class=\"ok\"><b>OK</b></span><br />\n";
}
lock_tables(0);
end_step('remove_topics');
case 'remove_topics':
print " * Removing posts with no corresponding topics :: ";
flush();
$sql = "SELECT p.post_id
FROM " . POSTS_TABLE . " p
LEFT JOIN " . TOPICS_TABLE . " t ON p.topic_id = t.topic_id
WHERE t.topic_id IS NULL";
$result = query($sql, "Couldn't obtain list of deleted topics");
$post_total = $db->sql_numrows($result);
if ( $post_total )
{
$post_id_ary = array();
while( $row = $db->sql_fetchrow($result) )
{
$post_id_ary[] = $row['post_id'];
}
$sql = "DELETE FROM " . POSTS_TABLE . "
WHERE post_id IN (" . implode(", ", $post_id_ary) . ")";
query($sql, "Couldn't update posts to remove deleted user poster_id values");
$sql = "DELETE FROM " . POSTS_TEXT_TABLE . "
WHERE post_id IN (" . implode(", ", $post_id_ary) . ")";
query($sql, "Couldn't update posts to remove deleted user poster_id values");
}
echo "<span class=\"ok\"><b>OK</b></span> ( Removed $post_total posts )<br />\n";
end_step('convert_users');
case 'convert_users':
//
// Completely remove old soft-deleted users
//
$sql = "DELETE FROM " . USERS_TABLE . "
WHERE user_level = -1";
query($sql, "Couldn't delete old soft-deleted users");
$sql = "SELECT COUNT(*) AS total, MAX(user_id) AS maxid
FROM " . USERS_TABLE;
$result = query($sql, "Couldn't get max user_id.");
$row = $db->sql_fetchrow($result);
$totalposts = $row['total'];
$maxid = $row['maxid'];
$sql = "ALTER TABLE " . USERS_TABLE . "
ADD user_sig_bbcode_uid CHAR(10),
MODIFY user_sig text";
query($sql, "Couldn't add user_sig_bbcode_uid field to users table");
$super_mods = array();
$first_admin = -2;
$batchsize = 1000;
for($i = -1; $i <= $maxid; $i += $batchsize)
{
$batchstart = $i;
$batchend = $i + $batchsize;
print " * Converting Users ( $batchstart to $batchend ) :: ";
flush();
$sql = "SELECT *
FROM " . USERS_TABLE . "
WHERE user_id
BETWEEN $batchstart
AND $batchend";
$result = query($sql, "Couldn't get ". USERS_TABLE .".user_id $batchstart to $batchend");
// Array with user fields that we want to check for invalid data (to few characters)
$checklength = array(
'user_occ',
'user_website',
'user_email',
'user_from',
'user_intrest',
'user_aim',
'user_yim',
'user_msnm');
lock_tables(1, array(USERS_TABLE, GROUPS_TABLE, USER_GROUP_TABLE, POSTS_TABLE));
$per_pct = ceil( $db->sql_numrows($result) / 40 );
$inc = 0;
while( $row = $db->sql_fetchrow($result) )
{
$sql = "INSERT INTO " . GROUPS_TABLE . " (group_name, group_description, group_single_user)
VALUES ('" . addslashes($row['username']) . "', 'Personal User', 1)";
query($sql, "Wasn't able to insert user ".$row['user_id']." into table ".GROUPS_TABLE);
$group_id = $db->sql_nextid();
$sql = "INSERT INTO " . USER_GROUP_TABLE . " (group_id, user_id, user_pending)
VALUES ($group_id, " . $row['user_id'] . ", 0)";
query($sql, "Wasn't able to insert user ".$row['user_id']." into table ".USER_GROUP_TABLE);
if ( is_int($row['user_regdate']) )
{
// We already converted this post to the new style BBcode, skip this post.
continue;
}
//
// Nathan's bbcode2 conversion
//
// undo 1.2.x encoding..
$row['user_sig'] = bbdecode(stripslashes($row['user_sig']));
$row['user_sig'] = undo_make_clickable($row['user_sig']);
$row['user_sig'] = str_replace("<BR>", "\n", $row['user_sig']);
// make a uid
$uid = make_bbcode_uid();
// do 2.x first-pass encoding..
$row['user_sig'] = bbencode_first_pass($row['user_sig'], $uid);
$row['user_sig'] = addslashes($row['user_sig']);
// Check for invalid info like '-' and '?' for a lot of fields
@reset($checklength);
while($field = each($checklength))
{
$row[$field[1]] = strlen($row[$field[1]]) < 3 ? '' : $row[$field[1]];
}
preg_match('/(.*?) (\d{1,2}), (\d{4})/', $row['user_regdate'], $parts);
$row['user_regdate'] = gmmktime(0, 0, 0, $months[$parts[1]], $parts[2], $parts[3]);
$website = $row['user_website'];
if ( substr(strtolower($website), 0, 7) != "http://" )
{
$website = "http://" . $website;
}
if( strtolower($website) == 'http://' )
{
$website = '';
}
$row['user_website'] = addslashes($website);
$row['user_icq'] = (ereg("^[0-9]+$", $row['user_icq'])) ? $row['user_icq'] : '';
reset($checklength);
while($field = each($checklength))
{
if ( strlen($row[$field[1]]) < 3 )
{
$row[$field[1]] = '';
}
$row[$field[1]] = addslashes($row[$field[1]]);
}
//
// Is user a super moderator?
//
if( $row['user_level'] == 3 )
{
$super_mods[] = $row['user_id'];
}
$row['user_level'] = ( $row['user_level'] == 4 ) ? ADMIN : USER;
//
// Used to define a 'practical' group moderator user_id
// for super mods a little latter.
//
if( $first_admin == -2 && $row['user_level'] == ADMIN )
{
$first_admin = $row['user_id'];
}
//
// Dutch language files have been renamed from 'nederlands' to 'dutch'
//
if( $row['user_lang'] == 'nederlands' )
{
$row['user_lang'] = 'dutch';
}
$sql = "UPDATE " . USERS_TABLE . "
SET
user_sig = '" . $row['user_sig'] . "',
user_sig_bbcode_uid = '$uid',
user_regdate = '" . $row['user_regdate'] . "',
user_website = '" . $row['user_website'] . "',
user_occ = '" . $row['user_occ'] . "',
user_email = '" . $row['user_email'] . "',
user_from = '" . $row['user_from'] . "',
user_intrest = '" . $row['user_intrest'] . "',
user_aim = '" . $row['user_aim'] . "',
user_yim = '" . $row['user_yim'] . "',
user_msnm = '" . $row['user_msnm'] . "',
user_level = '" . $row['user_level'] . "',
user_desmile = NOT(user_desmile),
user_bbcode = 1,
user_theme = 1
WHERE user_id = " . $row['user_id'];
query($sql, "Couldn't update ".USERS_TABLE." table with new BBcode and regdate for user_id ".$row['user_id']);
$inc++;
if ( $inc == $per_pct )
{
print ".";
flush();
$inc = 0;
}
}
// Set any non-standard (like) email addresses to nothing
// could do this above as a preg_ but this one query may
// be faster
$sql = "UPDATE " . USERS_TABLE . "
SET user_email = ''
WHERE user_email NOT REGEXP '^[a-zA-Z0-9_\+\.\-]+@.*[a-zA-Z0-9\-_]+\.[a-zA-Z]{2,}$'";
query($sql, "Couldn't update ".USERS_TABLE." table non-standard user_email entries");
print " <span class=\"ok\"><b>OK</b></span><br />\n";
lock_tables(0);
}
//
// Handle super-mods, create hidden group for them
//
// Iterate trough access table
if( count($super_mods) && $first_admin != -2 )
{
print "\n<br />\n * Creating new group for super moderators :: ";
flush();
$sql = "INSERT INTO " . GROUPS_TABLE . " (group_type, group_name, group_description, group_moderator, group_single_user)
VALUES (" . GROUP_HIDDEN . ", 'Super Moderators', 'Converted super moderators', $first_admin, 0)";
$result = query($sql, "Couldn't create group for ".$row['forum_name']);
$group_id = $db->sql_nextid();
if ( $group_id <= 0 )
{
print "<font color=\"red\">Group creation failed. Aborting creation of groups...<br></font>\n";
continue 2;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -