<?php
#
# This file will upgrade the user_avatar field to suit the
# phpBB2 forum. As multible avatar options are possible
# in the forum, the path is also stored in the database
# as part of the avatar name. This will add "gallery/"
# to the user_avatar value as the regular nuke avatar
# icons are kept in that folder.
#
# Please coppy all your regular nuke avatars into the
# gallery folder.
#
# If the path is different from the default, please change
# the code to suit.
#
# Tu use, just call it from your browser.
#
#

require_once("mainfile.php");
global $uname, $avatar_path;
$result = sql_query("SELECT MAX(uid) AS total from ".$user_prefix."_users", $dbi);
list($max_users) = sql_fetch_row($result, $dbi);
echo "Found $max_users users on the system.<br><br>";
for($i = 2; $i <= $max_users; $i++) {
    $result = sql_query("SELECT uname, user_avatar from ".$user_prefix."_users where uid='$i'", $dbi);
    if(!$result) {
    // Looks like we have a deleted user here, let's not do anything
    echo "found a deleted user, Skipping";
    } else {
	list($uname, $user_avatar) = sql_fetch_row($result, $dbi);
        if (substr($user_avatar,0, 8) != "gallery/") {
	    if ($user_avatar == "") {
		// The user doesn't seem to have an avatar, let's set the blank one.
		$user_avatar = "blank.gif";
	    }
	    $user_avatar = "gallery/".$user_avatar;
	    $result2 = sql_query("update ".$user_prefix."_users set user_avatar='$user_avatar' where uid='$i'", $dbi);
	    if(!$result2) {
		echo "Had a problem updating the avatar path for $uname<br>";
	    } else {
		echo "Updated the avatar path for $uname sucessfully<br>";
	    }
	} else {
	    echo "$uname already had the correct avatar path, skipping<br>";
	}
    }
}

$file="./upgrade_avatar_path.php";
if (file_exists($file)) {
    unlink("$file");
    echo "Avatar path upgrade complete and the upgrade was was deleted";
} else {
    echo "Avatarpath upgrade complete upgrade-avatar_path.php."
        ."Please delete this file manually.";
}
?>