<?PHP

$GLOBALS["G_ErrMsg"] = Upgrade230_UpdateForumMessages();

function Upgrade230_UpdateForumMessages()
{
    # use last link opened by mysql_connect()
    $Result = mysql_query("
        SELECT * FROM Messages
        WHERE Body LIKE '%<br><br><small>This message edited by%'");

    # not authenticated or another issue, so return an error
    if ($Result === FALSE)
    {
        return "Failed to update the Messages table.";
    }

    while (FALSE !== ($Row = mysql_fetch_assoc($Result)))
    {
        $MessageId = $Row["MessageId"];
        $Body = $Row["Body"];
        $DatePosted = $Row["DatePosted"];

        # get the editor's user name and the date the message was edited
        preg_match(
            '/<br><br><small>This message edited by (.*?) on (.*?) at (.*?)<\/small>/',
            $Body,
            $Matches);

        $EditorName = $Matches[1];
        $DateTime = $Matches[2] . " " . $Matches[3];
        $PostedTimestamp = strtotime($DatePosted);
        $EditedTimestamp = strtotime($DateTime);

        # the posting date used to be modified whenever the post was edited, so
        # make sure the edited date is at least that timestamp since seconds
        # weren't added to the "Edited By" message
        if ($PostedTimestamp >= $EditedTimestamp)
        {
            $EditedTimestamp = $PostedTimestamp;
        }

        $DateEdited = date("YmdHis", $EditedTimestamp);

        # strip the edited-by message from the body
        $NewBody = trim(preg_replace(
            '/(\n\n)?<br><br><small>This message edited by .*?<\/small>/',
            "",
            $Body));

        # message bodies previously allowed some HTML, so remove any if it's there
        $NewBody = strip_tags($NewBody);

        $UserData = Upgrade230_GetUserData($EditorName);

        # the user name is valid, so update the editor-related values along with
        # the body of the message
        if (is_array($UserData))
        {
            $EditorId = $UserData["UserId"];

            # use last link opened by mysql_connect()
            mysql_query("
                UPDATE Messages
                SET Body = '".addslashes($NewBody)."',
                EditorId = '".addslashes($EditorId)."',
                DateEdited = '".addslashes($DateEdited)."'
                WHERE MessageId = '".addslashes($MessageId)."'");
        }

        # the user name is invalid, maybe the user no longer exists, so only update
        # the body of the message
        else
        {
            # use last link opened by mysql_connect()
            mysql_query("
                UPDATE Messages
                SET Body = '".addslashes($NewBody)."'
                WHERE MessageId = '".addslashes($MessageId)."'");
        }
    }
}

function Upgrade230_GetUserData($Name)
{
    # use last link opened by mysql_connect()
    $Result = mysql_query("
        SELECT * FROM APUsers
        WHERE UserName ='".addslashes($Name)."'");

    # error occurred
    if ($Result === FALSE)
    {
        return NULL;
    }

    # no such user or error occurred when getting the number of rows
    if (mysql_num_rows($Result) < 1)
    {
        return NULL;
    }

    $UserData = mysql_fetch_assoc($Result);

    return $UserData;
}
