<?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 -> Content Maintenance                                          |
// |                                                                      |
// | -> Common Maint. Routines Required For Maintaining A CMS             |
// +----------------------------------------------------------------------+
// | 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 Baseclass
 * @version $Id: core_LIB_ContMaint.baseclass.php,v 1.1.2.9 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 Baseclass
 * @version 0.1 Alpha
 */

class ContentMaint
{
    var $dbtype = 'mysql';
    var $dbtables = array();
    var $cmtbls = array();
    var $global_admin_uid = - 1;
    var $cm_admin = _ADMIN_TRUSTED;

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

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

        return;

    } // End Constructor

/**
 * This Method Will Run A Single Query Against A Database
 *
 * @param string ( $sql -> sql query to execute )
 * @param bool ( $retbool -> how to return the result )
 * @see './docs/manual/code/classes/ContentMaint/execSQLQuery_argument_syntax.txt'
 * @return mixed (bool on error, string on error, or object)
 */

function execSQLQuery( $sql, $retbool = FALSE )
    {
        if ( ! ( $result = mysql_query( $sql ) ) )
        {
            $result = ( ! $retbool )
                    ? ( '__' . mysql_error() )
                    : ( (bool)FALSE );
        }

        return $result;

    } // End Method execSQLQueries

/**
 * This Method Will Run A Single Result Query Against A Database
 *
 * @param string ( $sql -> sql query to execute )
 * @param bool ( $retbool -> how to return the result )
 * @see './docs/manual/code/classes/ContentMaint/execSQLQuery_argument_syntax.txt'
 * @return mixed (bool on error, string on error, or bool result)
 */

function execSQLResultQuery( $sql, $retbool = FALSE )
    {
        if ( ! ( $result = @mysql_result( mysql_query( $sql ), 0 ) ) )
        {
            $result = ( ! $retbool )
                    ? ( '__' . mysql_error() )
                    : ( (bool)FALSE );
        }

        return $result;

    } // End Method execSQLResultQuery


/**
 * This Method Will Run A Set Of Queries Against A Database
 *
 * @param array ( array of sql queries to execute )
 * @param bool ( $getresult -> get a result query or an object )
 * @param bool ( $retbool -> how to return the result )
 * @see './docs/manual/code/classes/ContentMaint/execSQLQueries_argument_syntax.txt'
 * @return array (array of results)
 */

function execSQLQueries( $tbl_ary, $syncro = FALSE, $getresult = FALSE, $retbool = FALSE )
    {
        if ( ! is_array( $tbl_ary ) )
        {
           return (bool)FALSE;
        }

        $_METH = ( $getresult ) ? ( 'execSQLResultQuery' ) : ( 'execSQLQuery' );

        $cnt = 0;

        foreach( $tbl_ary AS $tbl => $sql )
        {
            $results[ $tbl ] = $this -> $_METH( $sql, $retbool );
        }

        return $results;

    } // End Method execSQLQueries

/**
 * Check Administrative Global User ID.
 *
 * Returns if uid already exists.
 * If not, attempts to create a new global admin.
 * If successful return, or exit with error.
 *
 * @param string ( $utbl -> users table name )
 * @param string ( $ufld -> user_id field name )
 *
 * @return void
 */

function checkAdminUser( $utbl, $ufld )
    {
        $chk = (bool)FALSE;
        $sql = 'SELECT ( ' . $ufld . '  = "' . $this -> global_admin_uid . '" ) FROM ' . $this -> cmtbls[ $utbl ];

        if ( $this -> execSQLResultQuery( $sql, TRUE ) ) { return; }

        $sql = $this -> getNewGlobalAdminSQL();

        if ( is_array( $sql ) )
        {
            $results = $this -> execSQLQueries( $sql );
        }
        else
        {
            $results = $this -> execSQLQuery( $sql );
        }

        if ( $this -> checkResults( $results ) )
        {
            $msg = 'Method checkAdminUser -> Global Admin User Check / Create Query Failed';
            $this -> _ERROR_( $msg, $sql, $results, __FILE__, __LINE__ );
        }

        return;

    } // End Method checkAdminUser

/**
 * Generate Insert SQL For New Global Admin
 *
 * @return mixed ( string or array depending on database requirements )
 */

function getNewGlobalAdminSQL()
{
    $sql_1 = 'INSERT INTO ' . $this -> cmtbls['users'] . '
              SET   uid = ' . $this -> global_admin_uid . '
                  , uname = "Administrator"
                  , user_regdate = NOW()';

    $sql_2 = 'INSERT INTO ' . $this -> cmtbls['ustatus'] . '
              SET   uid = ' . $this -> global_admin_uid . '
                  , posts = 0
                  , attachsig = 0
                  , rank = 0
                  , level = 3';

    return array( 'users' => $sql_1, 'ustatus' => $sql_2 );

} // End Method getNewGlobalAdminSQL

/**
 * Check Returned Query Results
 *
 * @param mixed ( $results -> string / array / object of results )
 * @return bool
 */

function checkResults( $results )
{
    if ( is_array( $results ) )
    {
        foreach( $results AS $tbl => $result )
        {
            if ( (bool)FALSE !== strpos( $result, '__' ) )
            {
                return (bool)TRUE;
            }
        }
    }
    else
    {
        return ( (bool)FALSE !== strpos( $results, '__' ) );
    }

    return (bool)FALSE;

} // End Method checkResults

/**
 * Error Handling.
 *
 * @return mixed ( bool or exit )
 */

function _ERROR_( $msg, $sql = NULL, $data = NULL, $file = NULL, $line = NULL, $retbool = FALSE )
    {
        if ( $retbool )
        {
            return (bool)FALSE;
        }

        if ( ! is_null( $file ) ) { $msg .= '<br /><br />File: ' . $file; }
        if ( ! is_null( $line ) ) { $msg .= '<br /><br />Line: ' . $line; }
        if ( ! is_null( $sql ) ) { $msg .= '<br /><br />SQL: ' . $sql; }
        if ( ! is_null( $data ) )
        {
            if ( is_array( $data ) )
            {
                foreach( $data AS $key => $v )
                {
                    $msg.= '<br />&nbsp;&nbsp;' . $key . ' => ' . $v;
                }
            }
            else
            {
                $msg .= '<br /><br />Returned Error: ' . $data;
            }
        }

        $msg = '<p class="t-left">' . $msg . '</p>';

        goBack( $msg );
        exit;

    } // End Method _ERROR_

} // End Class ContentMaint

?>