<?php

@( (bool)FALSE === strpos( $_SERVER['REQUEST_URI'], basename( __FILE__ ) ) ) OR exit( '!! ACCESS DENIED !!' );
error_reporting( _E_VALUE );

/* vim: set expandtab tabstop=4 shiftwidth=4: */

// ***** BEGIN LICENSE BLOCK ( DO NOT REMOVE ) *****

// +----------------------------------------------------------------------+
// | WSCL -> Forum Content Maintenance                                    |
// |                                                                      |
// | -> Maintains A Forums Database Entires                               |
// +----------------------------------------------------------------------+
// | VERSION: 0.1 Alpha                                                   |
// +----------------------------------------------------------------------+
// | LICENSE: LGPL 2.1 (see ./docs/LICENSE_LGPL)                          |
// +----------------------------------------------------------------------+
// | AUTHOR(s):                                                           |
// |                                                                      |
// | The Original Code is the WebSanitizer For MPN 188_8.                 |
// | The Initial Developer of the Original Code is Eric Haddix.           |
// |                                                                      |
// | Portions created by Eric Haddix or Kiril Threndor                    |
// | <support@websanitizer.illearth.net> are                              |
// | Copyright (C) 2003 Eric Haddix.  All Rights Reserved.                |
// |                                                                      |
// | Portions created by Arjan van Waay or avw                            |
// | <arjan@vanwaay.net> are                                              |
// | Copyright (C) 2004 Arjan van Waay.  All Rights Reserved.             |
// +----------------------------------------------------------------------+
// | CONTRIBUTOR(s):                                                      |
// |                                                                      |
// |                                                                      |
// +----------------------------------------------------------------------+
// | MODIFICATIONS:                                                       |
// |                                                                      |
// |                                                                      |
// +----------------------------------------------------------------------+
// | REQUIRED FILES:                                                      |
// |                                                                      |
// +----------------------------------------------------------------------+
// | NOTES:                                                               |
// |   This Class is Currently Being Built.                               |
// |   The only use in this alpha version is maint. of forum database     |
// |   tables when users have been deleted.                               |
// +----------------------------------------------------------------------+
// | SUPPORT:                                                             |
// |   WEB:   http://myphpnuke.com/                                       |
// |   EMAIL: support@websanitizer.illearth.net                           |
// +----------------------------------------------------------------------+

/**
 * MPN_188_8_Classes
 *
 * @package MPN_188_8_Classes
 * @subpackage Content_Maintenance
 * @category forum_maintenance
 * @version $Id: core_LIB_ForumMaint.subclass.php,v 1.1.2.8 2004/08/15 12:58:23 kirilt Exp $
 *
 * @author Kiril Threndor <support@websanitizer.project-php.com>
 * @author avw <arjan@vanwaay.net>
 * @copyright Copyright (C) 2003 Eric Haddix and Arjan van Waay
 * @license http://www.gnu.org/licenses/lgpl.html LGPL 2.1
 * @support http://websanitizer.project-php.com/
 *
 * @tutorial
 * @filesource
 */

// ***** END LICENSE AND DOCUMENTATION BLOCKS ( DO NOT REMOVE ) *****

/**
 * Content Maintenance Base Class
 *
 * @package MPN_188_8_Classes
 * @subpackage Content_Maintenance
 * @category forum_maintenance
 * @version 0.1 Alpha
 */

class ForumMaint extends ContentMaint
{
    var $global_admin_uid = -1;

    var $show_modsonly = TRUE;

/**
 * Constructor.
 *
 * Class Constructor
 *
 * @param array ( $dbtables -> db table array )
 * @return object
 */

function ForumMaint( $dbtables )
    {
        $this -> __construct( $dbtables );

        $forum_tables = array
        (
            'config'    => $this -> dbtables['forumconfig']
          , 'forums'    => $this -> dbtables['forums']
          , 'topics'    => $this -> dbtables['forumtopics']
          , 'posts'     => $this -> dbtables['posts']
          , 'users'     => $this -> dbtables['users']
          , 'ustatus'   => $this -> dbtables['users_status']
          , 'pm'        => $this -> dbtables['priv_msgs']
          , 'guestbook' => $this -> dbtables['guestbook']
        );

        $this -> cmtbls = array_merge( $this -> cmtbls, $forum_tables );

        return $this;

    } // End Constructor

/**
 * Automatic Forum Tables Fixing When User Has Been Deleted.
 *
 * This is intended for eventual integration
 * with a new content management script.
 *
 * @param integer ( $uid -> user ID for the post )
 * @global array ( $this -> dbtables -> DB table array )
 * @return void
 */

function fixAllTables( $uid, $newuid = NULL, $topicid = FALSE, $topic_only = FALSE, $action = 'del' )
    {
        $this -> checkNewUID( $uid, $newuid );

        $results = array
        (
            'topics'    => $this -> fixTopics( $topicid, $uid, $newuid, $topic_only )

          , 'pm'        => $this -> fixPMTable( $topicid, $uid, $newuid, $topic_only )

          , 'guestbook' => $this -> fixGuestBookTable( $uid, $newuid, $action )
        );

        if ( $this -> checkResults( $results ) )
        {
            $msg = 'Method fixAllTables -> One Or More Database Queries Failed';
            $this -> _ERROR_( $msg, $sql, $results, __FILE__, __LINE__, TRUE );
        }

        return (bool)TRUE;

    } // End Method fixForumTables

/**
 * Fix Forum Topic & Posts Table When User Has Been Deleted.
 *
 * @param integer ( $topicid -> topic ID to fix if not fixing all of them )
 * @param integer ( $uid -> user ID for the post )
 * @param integer ( $newuid -> user ID to assign )
 * @param bool ( $topic_only -> fix just one topic or all of them )
 * @return bool
 */

function fixTopics( $topicid, $uid, $newuid = NULL, $topic_only = FALSE )
    {
        $this -> checkNewUID( $uid, $newuid );
        $add_stamp = ( $newuid > 1 || $newuid == $this -> global_admin_uid );

        $sql_1 = 'UPDATE ' . $this -> cmtbls['topics'] . '
                  SET topic_poster = "' . $newuid . '"
                  WHERE topic_poster = "' . $uid . '"
                      && ( ' . $uid . ' + topic_poster ) != 0';

        $sql_2 = 'UPDATE ' . $this -> cmtbls['posts'] . '
                  SET poster_id = ' . $newuid;

        if ( $add_stamp )
        {
            $msg = '<br /><br />&#045;&#045;&#045;&#032;Admin Reclaimed Post&#032;&#045;&#045;&#045;<br /><br />';

            $sql_2 .= ', post_text = CONCAT( post_text, "' . $msg . '" ) WHERE ( INSTR( post_text, "' . $msg . '" ) = 0 ) && ';
        }
        else
        {
            $sql_2 .= ' WHERE ';
        }

        $sql_2 .= '( ' . $uid . ' + poster_id ) != 0
                        && poster_id = ' . $uid;

        if ( $topic_only )
        {
            $sql_1 .= ' && topic_id = "' . $topicid . '"';
            $sql_2 .= ' && topic_id = "' . $topicid . '"';
        }

        $results = $this -> execSQLQueries( array( 'topics' => $sql_1, 'posts' => $sql_2 ) );

        if ( $this -> checkResults( $results ) )
        {
            $msg = 'Method fixTopics -> One Or More Database Queries Failed';
            $sql = $sql_1 . '<br />' . $sql_2;
            $this -> _ERROR_( $msg, $sql, $results, __FILE__, __LINE__ );
        }

        return (bool)TRUE;

    } // End Method fixTopics

/**
 * Fix Forum Topics Table When User Has Been Deleted.
 *
 * @param integer ( $topicid -> topic ID to fix if not fixing all of them )
 * @param integer ( $uid -> user ID for the post )
 * @param integer ( $newuid -> user ID to assign )
 * @param bool ( $topic_only -> fix just one topic or all of them )
 * @return bool
 */

function fixPMTable( $uid, $newuid = NULL, $action = 'del' )
    {
        $this -> checkNewUID( $uid, $newuid );

        $_EXEC = ( $action == 'del' ) ? ( 'DELETE FROM' ) : ( 'UPDATE' );

        $sql = $_EXEC . $this -> cmtbls['pm'];

        if ( $action != 'del' )
        {
            $sql .= ' SET to_userid = "' . $newuid . '", from_userid = "' . $newuid . '"';
        }

        $sql .= ' WHERE ( to_userid = "' . $uid . '" || from_userid = "' . $uid . '" )
                      && ( ( ' . $uid . ' + to_userid ) != 0 || ( ' . $uid . ' + from_userid ) != 0 )';

        $results = $this -> execSQLQuery( $sql );

        if ( $this -> checkResults( $results ) )
        {
            $msg = 'Method fixPostTable -> Update Or Delete Query Failed';
            $this -> _ERROR_( $msg, $sql, $result, __FILE__, __LINE__ );
        }

        return (bool)TRUE;

    } // End Method fixTopics

/**
 * Fix Forum Topics Table When User Has Been Deleted.
 *
 * @param integer ( $topicid -> topic ID to fix if not fixing all of them )
 * @param integer ( $uid -> user ID for the post )
 * @param integer ( $newuid -> user ID to assign )
 * @param bool ( $topic_only -> fix just one topic or all of them )
 * @return bool
 */

function fixGuestBookTable( $uid, $newuid = NULL, $action = 'del' )
    {
        $this -> checkNewUID( $uid, $newuid );

        $_EXEC = ( $action == 'del' ) ? ( 'DELETE FROM' ) : ( 'UPDATE' );

        $sql = $_EXEC . $this -> cmtbls['guestbook'];

        if ( $action != 'del' )
        {
            $sql .= ' SET guest_poster = "' . $newuid . '"';
        }

        $sql .= ' WHERE guest_poster = "' . $uid . '"';

        $results = $this -> execSQLQuery( $sql );

        if ( $this -> checkResults( $results ) )
        {
            $msg = 'Method fixGuestBookTable -> Update Or Delete Query Failed';
            $this -> _ERROR_( $msg, $sql, $result, __FILE__, __LINE__ );
        }

        return (bool)TRUE;

    } // End Method fixTopics

/**
 * Generate An Admin Interface
 *
 * @return $string
 */

function getAdminInterface( $puid, $topicid )
    {
        if ( ! $this -> cm_admin ) { return ''; }

        $content = '';

        $content .= '<form action="' . $_SERVER['REQUEST_URI'] . '" method="post">' . "\n";

        $content .= extractQuery();

        $content .= '<input type="hidden" name="fmaint" value="1">' . "\n";
        $content .= '<input type="hidden" name="topicid" value="' . $topicid . '">' . "\n";
        $content .= '<input type="hidden" name="puid" value="' . $puid . '">' . "\n";

        $content .= '<label>' . translate( 'Assign To' ) . ' :</label>' . "\n";

        $content .= '<select name="newuid">' . "\n";

            if ( $puid > 1 )
            {
                $content .= '<option value="' . $puid . '" selected="1">' . translate( 'Tag For Later Action' ) . '</option>' . "\n";
            }

            $content .= '<option value="' . $this -> global_admin_uid . '">' . translate( 'Global Admin User' ) . '</option>' . "\n";

            if ( $this -> show_modsonly )
            {
                $sql = 'SELECT t1.uid, t1.uname
                        FROM ' . $this -> cmtbls['users'] . ' AS t1,
                             ' . $this -> cmtbls['ustatus'] . ' AS t2
                        WHERE t1.uid = t2.uid
                            && t2.level > 1';
            }
            else
            {
                $sql = 'SELECT uid, uname
                        FROM ' . $this -> cmtbls['users'];
            }

            $result = $this -> execSQLQuery( $sql );

            $content .= '<option></option>' . "\n";

            if ( $this -> checkResults( $result ) )
            {
                for ( $i = 0; $i < @mysql_num_rows( $result ); $i++ )
                {
                    if (    ! @mysql_data_seek( $result, $i )
                         || ! @( $tmp = mysql_fetch_row( $result ) ) )
                    {
                        continue;
                    }

                   $content .= '<option value="' . $tmp[0] . '">' . $tmp[1] . '</option>' . "\n";
                }

                @mysql_free_result ( $result );
            }

        $content .= '</select>' . "\n";

        $content .= '&nbsp;&nbsp;<label>' . translate( 'Apply To' ) . ' :</label>' . "\n";

        $content .= '<select name="action">' . "\n";

            $content .= '<option value="0" selected="1">' . translate( 'This Topic Only' ) . '</option>' . "\n";

            $content .= '<option value="1">' . translate( 'All Topics' ) . '</option>' . "\n";

            //$content .= '<option value="2" >' . translate( 'All Related Tables' ) . '</option>' . "\n";

        $content .= '</select>' . "\n";

        $content .= '<input class="button" type="submit" value="' . translate( 'Go' ) . '"></input>' . "\n";

        $content .= '</form>' . "\n";

        if ( $puid < - 1 )
        {
            $content .= '<em class="look">' . translate( 'This Message Has Already Been Tagged For Later Action' ) . '</em><br />';
       }

        return $content;

    } // End Method getAdminInterface

/**
 * Check New User ID And Modifies If Necessary
 *
 * @return void ( $newuid passed by reference )
 */

function checkNewUID( $uid, &$newuid )
    {
        if ( $uid < - 1 && ( is_null( $newuid ) || $newuid < - 1 ) )
        {
            $this -> _ERROR_( 'Invalid Arguments. User Already Tagged For Later Action', NULL, NULL, __FILE__, __LINE__ );
        }

        if ( is_null( $newuid ) || $newuid == $uid ) { $newuid = - $uid; }

        if ( $newuid == $this -> global_admin_uid )
        {
            $this -> checkAdminUser( 'users', 'uid' );
        }

        return;

    } // End Method checkNewUID

} // End Class ForumMaint

?>