<?php

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


/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | myPHPNuke Version 188_8 RC2                                          |
// +----------------------------------------------------------------------+
// | 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@dev.illearth.net >                     |
// +----------------------------------------------------------------------+
// $Id: core_SupportFunc.php,v 1.12.2.27 2004/08/15 12:57:27 kirilt Exp $

/**
 * Table Wrapper 1
 *
 * Formatted in Master CSS AND Theme CSS
 *
 * @return void
 */

function OpenTable( $return = FALSE )
{
    global $bgcolor1, $bgcolor2;

    $content = "<table class='opentable1' summary='' bgcolor='$bgcolor2' width='100%'><tr><td>\n";
    $content .= "<table class='opentable1a' summary='' bgcolor='$bgcolor1' width='100%'><tr><td>\n";

    if ( ! $return )
    {
        echo $content;
        return;
    }

    return $content;

} // End Function

/**
 * Table Wrapper 2
 *
 * Formatted in Master CSS AND Theme CSS\
 * This Table Serves As A Wrapper ONLY
 *
 * @return void
 */

function OpenTable2( $return = FALSE )
{
    $content = "<table class='opentable2' summary=''><tr><td>\n";
    $content .= "<table class='opentable2a' summary=''><tr><td>\n";

    if ( ! $return )
    {
        echo $content;
        return;
    }

    return $content;

} // End Function

/**
 * Table Wrappers Close
 *
 * Closes The Above Tables
 *
 * @return void
 */

function CloseTable( $return = FALSE )
{
    $content = "</td></tr></table></td></tr></table>\n";

    if ( ! $return )
    {
        echo $content;
        return;
    }

    return $content;

} // End Function

/**
 * Format Aid Header For Themes.
 *
 * @author Kiril Threndor
 * @return mixed ( string -> content for 188_8+ themes, void for legacy themes )
 */

function formatAidHeader( $aid )
{
     global $mpnTables;

     $content = '';

     $sql = "SELECT url, email FROM " . $mpnTables['authors'] . " WHERE aid = '$aid'";
     $result = mysql_query( $sql );

     list( $url, $email ) = mysql_fetch_row( $result );

     if ( isset( $url ) && $url != '' )
     {
         $content .= "<a class='notes' href='$url' alt='" . translate( 'Website' ) . "'>$aid</a>";
     }
     else if ( isset( $email ) && $email != '' )
     {
         $content .= "<a class='notes' href='mailto:{$email}' alt='" . translate( 'Email' ) . "'>$aid</a>";
     }
     else
     {
         $content .= $aid;
     }

     if ( defined( '_T_LAYOUT_CSS' ) )
     {
         return $content;
     }
     else
     {
         echo $content;
     }

     return;

} // End Function

/**
 * Generates Preview For Form Submission.
 *
 * Used in articles, sections, reviews
 *
 * @author Kiril Threndor
 * @return void
 */

function themepreview( $title, $hometext, $bodytext = '', $notes = '', $space = '' )
{
    global $bgcolor3, $textcolor2, $textcolor1;

    echo "<div id='themepreview' style='background-color: $bgcolor3; color: $textcolor1;'>\n";

        echo "<h3>$title</h3><br />\n";

        echo "<p class='notes'>" . translate( 'Text To Be Published On Home Page' ) . "</p><br />\n";

        if ( $space != '' ) { echo "$space\n"; }

        echo "<p class='previewhome'>$hometext</p><br />\n";

        if ( ( _MPN_USER || _MPN_ADMIN ) && $bodytext != '' )
        {
            echo "<p class='notes'>" . translate( 'Text To Be Published In Full Article View' ) . "</p><br />\n";

            if ( $space != '' ) { echo "$space\n"; }

            echo "<p class='previewbody'>$bodytext</p><br />\n";
        }

        if ( _MPN_ADMIN && $notes != '' )
        {
            echo "<p class='notes'>" . translate( 'Administrative Notes' ) . "</p><br />\n";

            if ( $space != '' ) { echo "$space\n"; }

            echo "<p class='previewnotes'>$notes</p><br />\n";
        }

    echo "</div>\n";

    return;

} // End Function

/**
 * Return To Previous Page On Error.
 *
 * @author Kiril Threndor
 * @param string ( $err_msg -> Message to display )
 * @param bool   ( $exit -> Show Fatal Error Message And Exit )
 * @param bool   ( $return_info -> Create a return form button that contains post data )
 * @return void
 */

function goBack( $err_msg = NULL, $exit = TRUE, $return_info = NULL, $back = 1 )
{
    @( include_once __ROOT__ . 'header.php' )
      OR goBack( translate( 'Include Error' ) );

    echo "<div class='center'>\n";

        if ( $exit )
        {
            echo "<p class='error'>\n";

                echo translate( '!!! FATAL ERROR !!!' );

            echo "</p>\n";
        }

        if ( ! empty( $err_msg ) )
        {
            echo "<br /><br />\n";

            if ( _ADMIN_TRUSTED )
            {
                $srch = array( '***', '**' );
                $repl = array( '<br /><br />ON LINE: ', '<br /><br />FILE: ' );
                $err_msg = str_replace( $srch, $repl, $err_msg );
            }
            else
            {
                $stop = ( (bool)FALSE !== ( $pos = strpos( $err_msg, '**' ) ) )
                      ? ( $pos )
                      : ( strpos( $err_msg, '<br' ) );

                if ( $stop == 0 ) { $stop = strlen( $err_msg ); }

                $err_msg = substr( $err_msg, 0, $stop );
            }

            echo "<p class='pagetitle'>$err_msg</p>\n";
        }

        if ( ! empty( $return_info ) )
        {
            echo "<br /><br />\n";

            echo "<form action='{$return_info['page']}' method='post'>\n";

                if ( isset( $_POST ) )
                {
                    foreach( $_POST AS $key => $v )
                    {
                        if ( $key != 'op' && $key != 'fct' && $key != 'submit' )
                        {
                            echo "<input type='hidden' name='$key' value='$v' />\n";
                        }
                    }
                }

                if ( isset( $return_info['fct'] ) )
                {
                    echo "<input type='hidden' name='fct' value='{$return_info['fct']}' />\n";
                }

                if ( isset( $return_info['op'] ) )
                {
                    echo "<input type='hidden' name='op' value='{$return_info['op']}' />\n";
                }

                echo "<input class='button' name='goback' type='submit' value='" . translate( 'Go Back' ) . "' />\n";

            echo "</form>\n";
        }
        else
        {
            echo "<form method='get'><input type='hidden' name='goback' value='1' /></form>\n";
            echo "<b>&#091;&#160;<a href=\"javascript:history.go(-" . $back . ")\">" . translate( 'Go Back' ) . "</a>&#160;&#093;</b>\n";
        }

    echo "</div>\n";

    if ( _ADMIN_TRUSTED )
    {
        echo "<div class='debug'>\n";

            echo "<br /><br />\n";

            if ( mysql_error() != '' )
            {
                echo '<h2>MySQL Error ' . mysql_errno() . ':</h2><br /><br /><p>' . mysql_error() . '</p>';
            }

            echo "<br /><br />\n";

            echo '<h2>Debug Back Trace:</h2><br /><hr class="w80" /><br />';

            print_r_html( debug_backtrace() );

            echo '<br /><br />' . "\n";

        echo '</div>' . "\n";
    }

    if ( $exit )
    {
        echo '</div></div>';

        CloseTable();
        CloseTable();

        @( include_once __ROOT__ . 'footer.php' )
          OR goBack( translate( 'Include Error' ) );

        exit;
    }

    return;

} // End Function

/**
 * Provides A Return Home Link.
 *
 * Additionally Can Be Used To Redirect To Another Page
 * After A given Time Has Expired.
 *
 * @author Kiril Threndor
 * @param $refresh   -> bool   ( Use Redirect Option )
 * @param $msg       -> string ( Message to display )
 * @param $uri       -> string ( Redirect URI )
 * @param $auto_time -> string ( Time is secs until auto refresh takes place )
 * @return void
 */

function goHome( $msg = NULL, $refresh = FALSE, $uri = 'index.php', $auto_time = 0 )
{
    // Just Refresh

    if ( (bool)TRUE === $refresh )
    {
        if ( is_integer( $auto_time ) && $auto_time > 0 && $auto_time < 5 )
        {
            sleep( $auto_time );
        }

        $_SERVER['F_LIB'] -> redirectRequest( $uri );

        exit;
    }

    // Print Out A Homepage Return Link With Message

    $homepage = '<a class="subtitle1" href="./index.php" title="' . translate( 'Return To Homepage' ) . '">' . translate( 'Return To Homepage' ) . '</a>';

    @( include_once __ROOT__ . 'header.php' )
      OR goBack( translate( 'Include Error' ) );

    echo "<div class='center'>\n";

       if ( ! empty( $msg ) && $msg != '' )
        {
            echo '<br /><br />' . "\n";

            if ( _ADMIN_TRUSTED )
            {
                $srch = array( '***', '**' );
                $repl = array( '<br /><br />ON LINE: ', '<br /><br />FILE: ' );
                $msg = str_replace( $srch, $repl, $msg );
            }
            else
            {
                $stop = ( (bool)FALSE !== ( $pos = strpos( $msg, '**' ) ) )
                      ? ( $pos )
                      : ( strpos( $msg, '<br' ) );

                $msg = substr( $msg, 0, $stop );
            }

            echo '<p class="pagetitle">' . $msg . '</p>' . "\n";
        }
        else
        {
           echo '<p class="pagetitle">' . "\n";

            echo translate( 'Thank You For Your Participation' );

           echo '</p>' . "\n";
        }

        echo "<br /><br />\n";

        echo $homepage;

    echo "</div>\n";

    CloseTable();
    CloseTable();

    @( include_once __ROOT__ . 'footer.php' )
      OR goBack( translate( 'Include Error' ) );

    exit;

}  // End Function

/**
 * Make A Select Option Box For Textarea Formatting Options.
 *
 * @author Kiril Threndor
 * @param -> $forum_opt -> string ( Is This A BBCode Form )
 * @return void
 */

function addFormatOption( $forum_opt = FALSE, $retcontent = FALSE )
{
    if ( ! $forum_opt )
    {
        $options = array(
                            'text'     => translate( 'Text Only' )
                          , 'html'     => translate( 'Pure HTML' )
                          , 'exttrans' => translate( 'Literal' )
                        );
    }
    else
    {
        $options = array(
                            'text'     => translate( 'Default' )
                          , 'exttrans' => translate( 'Literal' )
                        );
    }

    $sel = '';
    $content = '';

    $content .= "<br /><br />\n";

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

    if ( $forum_opt ) { $content .= '<br /><br />'; }

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

    foreach( $options AS $key => $v )
    {
        $sel = ( $sel != 'selected' && isset( $_POST['posttype'] ) &&  $_POST['posttype'] == $key  ) ? ( 'selected' ) : ( '' );

        $content .= "<option value=\"{$key}\" {$sel}>{$v}</option>\n";
    }

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

    $content .= "<br /><br />\n";


    if ( ! $retcontent )
    {
        echo $content;
        return;
    }

    return $content;

}  // End Function

/**
 * Function To Print Out An Array.
 *
 * Original code taken from php.net
 * user contributed notes for print_r
 * posted by
 *
 * @author warhog@warhog.net
 * @modauthor Kiril Threndor
 */

function print_r_html( $arr )
  {
    static $i = 0;
    $i++;

    echo '<blockquote>' . "\n";

    foreach( $arr AS $key => $val )
    {
        switch ( gettype( $val ) )
        {
            case 'array':

                echo "<a class='subtitle1 indent30' href=\"#\" onclick=\"document.getElementById('_tree$i').style.display = 'block';\">" . htmlspecialchars( $key ) . "</a><br /><br />\n";

                echo '<blockquote id="_tree' . $i . '" style="display: none;">';

                    echo print_r_html( $val );

                echo '</blockquote>' . "\n";

            break;

            case 'integer':

                echo '<b>' . htmlspecialchars( $key ) . '</b> => <i>' . htmlspecialchars( $val ) . '</i><br />' . "\n";
            break;

            case 'double':

                echo '<b>' . htmlspecialchars( $key ) . '</b> => <i>' . htmlspecialchars( $val ) . '</i><br />' . "\n";

            break;

            case 'boolean':

                echo '<b>' . htmlspecialchars( $key ) . '</b> => ';

                if ( $val )
                {
                    echo 'true';
                }
                else
                {
                    echo 'false';
                }

                echo '<br />' . "\n";

            break;

            case 'string':

                echo '<b>' . htmlspecialchars( $key ) . '</b> => <code>' . htmlspecialchars( $val ) . '</code><br />' . "\n";

            break;

            default:

                echo '<b>' . htmlspecialchars( $key ) . '</b> => ' . gettype( $val ) . '<br />' . "\n";

            break;
        }
    }

    echo '</blockquote>' . "\n";

    return;

} // End Function

/**
 * Global User Login Form
 *
 * @author Kiril Threndor
 */

function userLoginForm( $css_align = 'center', $showlabel = TRUE )
{
    $content = '<div class="' . $css_align . '">' . "\n";

        if ( $showlabel )
        {
            $content .= '<p class="subtitle1">';
            $content .= translate( 'Please Login Or' );
            $content .= ' <a href="./user.php?op=register" title="' . translate( 'New User Registration' ) . '">' . "\n";
            $content .= translate( 'Register' );
            $content .= '</a></p><br /><br />';
        }

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

            $content .= '<input type="hidden" name="global_login" value="1">' . "\n";
            $content .= '<input type="hidden" name="stripit_form" value="uname|pass" />' . "\n";
            $content .= '<input type="hidden" name="mpn_sectype" value="1" />' . "\n";

            $content .= extractQuery();

            $content .= '<p class="forms">' . "\n";

                $content .= '<label>';
                $content .= translate( 'Nickname' ) . '&#058;&nbsp;&nbsp;';
                $content .= '</label>' . "\n";

                $content .= '<input type="text" name="uname" size="25" maxlength="40" />';

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

                $content .= '<label>';
                $content .= translate( 'Password' ) . '&#058;&nbsp;&nbsp;';
                $content .= '</label>' . "\n";

                $content .= '<input type="password" name="pass" size="25" maxlength="25" />';

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

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

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

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

    return $content;

} // End Function

function adminLoginForm()
{
    $content = '<div class="adminlogin">' . "\n";

        $content .= '<h1>' . translate ( 'Administration Login' ) . '</h1><br />' . "\n";

        $content .= OpenTable( TRUE );

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

            $content .= '<input type="hidden" name="op" value="login">' . "\n";
            $content .= '<input type="hidden" name="stripit_form" value="aid|pwd" />' . "\n";
            $content .= '<input type="hidden" name="mpn_sectype" value="1" />' . "\n";

            $content .= extractQuery();

            $content .= '<p class="forms">' . "\n";

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

                $content .= '<input type="text" name="aid" size="20" maxlength="20" />';

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

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

                $content .= '<input type="password" name="pwd" size="20" maxlength="18" />';

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

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

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

        $content .= CloseTable( TRUE );

    $content .= '</div>' . "\n"; // Close Wrapper Div

    return $content;

}  // End Function


/**
 * Get A User Login Page
 *
 *
 */

function getGlobalLoginPage( $title = NULL, $subtitle = NULL )
{
    $title = ( empty( $title ) )
           ? ( translate( 'Registered Users Only' ) )
           : ( $title );

    $subtitle = ( empty( $subtitle ) )
              ? ( translate( 'Please Login Or' ) . ' <a href="./user.php?op=register" title="' . translate( 'New User Registration' ) . '">' . translate( 'Register' ) . '</a>' )
              : ( $title );

    @( include_once __ROOT__ . 'header.php' )
      OR goBack( translate( 'Include Error' ) );

    echo '<h1>' . $title . '</h1><br />' . "\n";

    echo '<h2>' . $subtitle . '</h2>' . "\n";

    echo userLoginForm( 'center', FALSE );

    @( include_once __ROOT__ . 'header.php' )
      OR goBack( translate( 'Include Error' ) );

} // End Function

/**
 * Extract A Request Query String.
 *
 * @author Kiril Threndor
 */

function extractQuery( $query = NULL, $extract_to = NULL )
{
    $cnt = 0;
    $query = ( ! empty( $query ) )
           ? ( $query )
           : ( ( isset( $_SERVER['QUERY_STRING'] ) && $_SERVER['QUERY_STRING'] != '' )
               ? ( $_SERVER['QUERY_STRING'] )
               : ( NULL ) );

    if ( empty( $query ) || $query == '' ) { return ''; }


    $extract_to = ( ! empty( $extract_to ) ) ? ( $extract_to ) : ( 'form' );
    $data = '';

    if ( ! is_array( $query ) )
    {
        $repl = array( '&amp;', '&' );
        $query_tmp = str_replace( $repl, '*|*', $query );
        $query_tmp = explode( '*|*', $query_tmp );
        $query_tmp = array_unique( $query_tmp );
    }
    else
    {
        $query_tmp =& $query;
    }

    foreach ( $query_tmp AS $key => $v )
    {
        if ( $v == '' ) { continue; }

        if ( $extract_to != 'qs' )
        {
            $tmp = explode( '=', $v );
        }
        else
        {
            $cnt++;
        }

        switch( $extract_to )
        {
            case 'sg':

                $_REQUEST[ trim( $tmp[0] ) ] = trim( $tmp[1] );
                if ( ! empty( $_POST ) ) { $_POST[ $tmp[0] ] = $tmp[1]; }
                if ( ! empty( $_GET ) ) { $_GET[ $tmp[0] ] = $tmp[1]; }

            break;

            case 'qs':

                $data .= ( $cnt == 1 ) ? ( '?' ) : ( '&amp;' );
                $data .= $key . '=' . $v;

            break;

            DEFAULT:

                $data .= '<input type="hidden" name="' . $tmp[0] . '" value="' . $tmp[1] . '" />' . "\n";
            break;
        }
    }

    return $data;

}  // End Function

?>