<?php

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

/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | myPHPNuke Version 188_8 -> Security Core                             |
// +----------------------------------------------------------------------+
// | Copyright (c) 2003-2004 myPHPNuke Development Team                   |
// | Copyright (c) 2003-2004 Kiril Threndor                               |
// +----------------------------------------------------------------------+
// | License: GNU/GPL - http://www.gnu.org/copyleft/gpl.html              |
// +----------------------------------------------------------------------+
// | Support: http://myphpnuke.com/                                       |
// +----------------------------------------------------------------------+
// | Author: Kiril Threndor < mpnx@illearth.net >                         |
// +----------------------------------------------------------------------+
// $Id: core_SecureMPN.php,v 1.20.2.16 2004/08/15 12:57:27 kirilt Exp $

// Define Path To Default Sanitizing Parameter Arrays

if ( is_admin() )
{
    define( '_DPF_SANITY' , __ROOT__ . 'classes/core_SanitizeAdmin.param.php' );
}
else
{
    define( '_DPF_SANITY' , __ROOT__ . 'classes/core_Sanitize.param.php' );
}

// Check Admin Configuration And Sensitive Post Content Access

if ( (bool)FALSE !== strpos( strrchr( $_SERVER['SCRIPT_NAME'], '/' ), 'admin.php' ) && ! empty( $_POST ) )
{
    if ( ( isset( $_REQUEST['fct'] ) || ( isset( $_REQUEST['op'] ) && $_REQUEST['op'] != 'login' ) ) )
    {
        checkConfigAccess();
    }
}

// Require Parent Class Sanitize And Child Class CheckInput

@( require __ROOT__ . 'classes/core_Sanitize.class.php' );
@( require __ROOT__ . 'classes/subs/sanitizer/core_CheckInput.subclass.php' );


// Set User Constant

is_user();

/**
 * Syntax Defaults For Object CheckRequest
 *
 * See ./classes/core_SanitizeParam.php For Default Allowed Tag Array
 *
 *  CheckRequest(   $allow_html   = TRUE  ( allow/disallow HTML )
 *                , $param_arrays = FALSE ( path & filename to custom sanitizing param arrays )
 *                , $allowed_tags = NULL  ( array of custom allowed / disallowed tags )
 *                , $censor       = FALSE ( censor bad words )
 *              );
 *
 */

// Set Global Sanitizing Objects For Use In All Scripts

$_SERVER['SAN'] = new CheckRequest();

/**
 * Sanitize ALL Request Variables
 *
 * Syntax Defaults For Method checkInput
 *
 * checkInput(   $set_param
 *             , $chkRvar  = NULL  ( Array -> restricted gPc variable names )
 *             , $chkGPvar = NULL  ( Array -> restricted gP variable names )
 *             , $chkuri   = TRUE  ( check requested URI for malicious code )
 *           );
 */

$_SERVER['SAN'] -> checkInput(   buildParamArray()
                                  , array( 'cookie', 'admin_html', 'user_html', 'anon_html', 'admin_bbcode', 'user_bbcode', 'anon_bbcode', 'tag_list', 'rm_tags_with_content', 'rm_all_att', 'rm_attnames', 'bad_attvals', 'allow_http_attr_vals', 'add_attr_to_tag' )
                                  , array( 'user', 'admin', 'LastVisit' )
                                );


// Check If Files Are Being Uploaded

if ( ! empty( $_FILES ) && ! isset( $_POST['upld_update'] ) )
{
    if ( __SAFEMODE__ ) { goBack( 'File Uploads Has Been Disabled Due To Server Configuration' ); }

    if ( _MPN_USER || _MPN_ADMIN )
    {
        // REF: checkFileUpload( $idx = NULL, $restr_scps = array() )

        $_SERVER['F_LIB'] -> checkFileUploads();
    }
    else
    {
        exit( 'Access Denied.  Invalid Operation.' );
    }
}


// Load Site Share Project Classes If Required

if ( ! empty( $_REQUEST['enpp'] ) && (bool)FALSE !== strpos( strrchr( $_SERVER['SCRIPT_NAME'], '/' ), 'npp_' ) )
{
    loadNestedPage();
}

/**
 * Verify Access To Potentially Harmful File and DB Writting
 *
 * return array ( sanitizing parameters )
 */

function buildParamArray()
{
    require __ROOT__ . 'classes/subs/sanitizer/core_AllowArrays.param.php';

    if ( _MPN_ADMIN )
    {
        $allow_html   = $admin_html;
        $allow_bbcode = $admin_bbcode;
    }
    else if ( _MPN_USER )
    {
        $allow_html   = $user_html;
        $allow_bbcode = $user_bbcode;
    }
    else
    {
        $allow_html   = $anon_html;
        $allow_bbcode = $anon_bbcode;
    }

    $set_param = array();

    // Determine How To Process Initial Security Scan

    if ( ! empty( $allow_html ) )
    {
        $set_param['allow_html'] = $allow_html;
    }

    if ( ! empty( $allow_bbcode ) )
    {
        $set_param['allow_bbcode'] = $allow_bbcode;
    }

    if ( isset( $_POST['clickit_form'] ) )
    {
        $set_param['clickit_form'] = $_POST['clickit_form'];
    }

    if ( isset( $_POST['stripit_form'] ) )
    {
        $set_param['stripit_form'] = $_POST['stripit_form'];
    }

    if ( isset( $_POST['smilies'] ) || isset( $_POST['smile'] ) )
    {
        $set_param['smile'] = (bool)TRUE;
    }

    if ( isset( $_POST['mpn_sectype'] ) )
    {
        $set_param['mpn_sectype'] = $_POST['mpn_sectype'];
    }

    if ( isset( $_POST['mpn_encode'] ) || isset( $_POST['bb_encode'] ) )
    {
        $set_param['mpn_encode'] = (bool)TRUE;
    }

    if ( isset( $_POST['mpn_data'] ) )
    {
        $set_param['mpn_data'] = (bool)TRUE;
    }

    if ( isset( $_POST['mpn_binary'] ) )
    {
       $set_param['mpn_binary'] = (bool)TRUE;
    }

    if ( isset( $_POST['posttype'] ) )
    {
        if ( $_POST['posttype'] == 'exttrans' )
        {
            $set_param['literal'] = (bool)TRUE;
        }
        else if ( $_POST['posttype'] == 'html' )
        {
            $set_param['purehtml'] = (bool)TRUE;
        }
    }

    return $set_param;

} // End Function buildParamArray

/**
 * Verify Access To Potentially Harmful File and DB Writting
 *
 * return void
 */

function checkConfigAccess()
{
    $trusted = (bool)FALSE;

    // MPN Admin Fct And Op Combo's

    $config_fcts = array
    (
        'page' => array
       (
              'myphpnuke'    => array( 'myPHPNukeConfig' )
            , 'mymsg2'       => array( 'update', 'add' )
            , 'blockcenter'  => array( 'centerblockConfigChange' )
            , 'blocks'       => array( 'BlocksAdd', 'BlocksSave' )
            , 'download'     => array( 'DownloadsConfig' )
            , 'emailuser'    => array( 'html_options' )
            , 'gallery'      => array( 'ConfigSave' )
            , 'guestbook'    => array( 'GuestbookConfigChange' )
            , 'rand_banners' => array( 'randbannersauve' )
        )

      , 'perm' => array
        (
              'myphpnuke'    => array( 'radminmpn' )
            , 'mymsg2'       => array()
            , 'blockcenter'  => array()
            , 'blocks'       => array()
            , 'download'     => array( 'radmindownload' )
            , 'emailuser'    => array()
            , 'gallery'      => array()
            , 'guestbook'    => array( 'radminguestbook' )
            , 'rand_banners' => array()
        )
    );

    // NUKE Ops

    $config_ops = array
    (
        'page' => array
        (
              'ConfigSave'
            , 'Configure'
            , 'secarticleadd'
            , 'secartchange'
            , 'PostStory'
            , 'ChangeStory'
            , 'PreviewAdminStory'
            , 'PostAdminStory'
        )

      , 'perm' => array
        (
              array()
            , array()
            , array( 'radminsection' )
            , array( 'radminsection' )
            , array( 'radminarticle' )
            , array( 'radminarticle' )
            , array( 'radminarticle' )
            , array( 'radminarticle' )
        )
    );

    // Check MPN First Then Check Nuke

    if ( isset( $_REQUEST['fct'] ) && isset( $_REQUEST['op'] ) )
    {
        $allow = NULL;

        if ( isset( $config_fcts['page'][ $_REQUEST['fct'] ] ) && in_array( $_REQUEST['op'], $config_fcts['page'][ $_REQUEST['fct'] ] ) )
        {
            if ( ! empty( $config_fcts['perm'][ $_REQUEST['fct'] ] ) )
            {
                $allow = $config_fcts['perm'][ $_REQUEST['fct'] ];

                $trusted = ( is_admin( NULL, TRUE, $allow ) ) ? ( (bool)TRUE ) : ( exit( '!--! ACCESS DENIED !--!' ) );
            }
            else // Check If Superuser
            {
                $trusted = ( _ADMIN_TRUSTED ) ? ( (bool)TRUE ) : ( exit( '!--! ACCESS DENIED !--!' ) );
            }
        }
    }
    else if ( ! isset( $_REQUEST['fct'] ) && isset( $_REQUEST['op'] ) )
    {
        if ( ! empty( $config_ops['page'] ) && in_array( $_REQUEST['op'], $config_ops['page'] ) )
        {
            $aryflip = array_flip( $config_ops['page'] );
            $allow = $config_ops['perm'][ $aryflip[ $_REQUEST['op'] ] ];

            $trusted = ( is_admin( NULL, TRUE, $allow ) ) ? ( (bool)TRUE ) : ( exit( '!--! ACCESS DENIED !--!' ) );
        }
        else // Check If Superuser
        {
            $trusted = ( _ADMIN_TRUSTED ) ? ( (bool)TRUE ) : ( exit( '!--! ACCESS DENIED !--!' ) );
        }
    }

    // If Trusted Set The Trusted Admin Constant And The Array
    // Of Form Fields To Preserve Original Formatting

    if ( (bool)TRUE === $trusted )
    {
        // Array Of Keys To Preserve Content

        $GLOBALS['protectKeys'] = array( 'bbcontent' );
    }
    else
    {
        $GLOBALS['protectKeys'] = NULL;
    }

    return;

} // End Function

function loadNestedPage()
{
    $shares = unserialize( _MPN_SITE_SHARES );

    if (    isset( $shares[ $_REQUEST['enpp'] ] )
         && ! empty( $shares[ $_REQUEST['enpp'] ] )
         && substr( strrchr( $_SERVER['SCRIPT_NAME'], '/' ), 1 ) === $shares[ $_REQUEST['enpp'] ]['access']
       )
    {
        $npp_request = $shares[ $_REQUEST['enpp'] ];

        // Load The Class And Instantiate Object

        require _DP_CLASS_PAR . 'core_NestedPageProject.class.php';

        require _DP_CLASS_PAR_SUB_NPP . $npp_request['root'] . $npp_request['child'];

        $_SERVER['NPP'] = new $npp_request['name'];

        if ( empty( $_SERVER['NPP'] ) || ! is_object( $_SERVER['NPP'] ) )
        {
            exit( 'A Fatal Error Occured While Loading Required Class' );
        }
    }

} // End Function

?>