<?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 RC2                                          |
// +----------------------------------------------------------------------+
// | License: GNU/GPL - http://www.gnu.org/copyleft/gpl.html              |
// +----------------------------------------------------------------------+
// | Support: http://myphpnuke.com/                                       |
// +----------------------------------------------------------------------+
// | Author: myPHPNuke Dev Team                                           |
// +----------------------------------------------------------------------+
// | Copyright: © 2001-2004 myPHPNuke Dev Team                            |
// +----------------------------------------------------------------------+
// $Id: phpbbforum.php,v 1.9.2.9 2004/08/15 12:58:04 kirilt Exp $




/************************************************************************/
/* PHP-NUKE: Web Portal System                                          */
/* ===========================                                          */
/*                                                                      */
/* Copyright (c) 2001 by Francisco Burzi (fburzi@ncc.org.ve)            */
/* http://phpnuke.org                                                   */
/*                                                                      */
/* =========================                                            */
/* Part of phpBB integration                                            */
/* Copyright (c) 2001 by                                                */
/*    Richard Tirtadji AKA King Richard (rtirtadji@hotmail.com)         */
/*    Hutdik Hermawan AKA hotFix (hutdik76@hotmail.com)                 */
/* http://www.phpnuke.web.id                                            */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/

checkAccess( 'forum', array( 'forum' ) );

/*********************************************************/
/* PHPBB Forum Admin Function                            */
/*********************************************************/

function ForumAdmin() {
    global $hlpfile, $admin, $bgcolor2, $bgcolor4, $textcolor2, $mpnTables,$phpEx;
    include ("config.$phpEx");
    include ("header.$phpEx");
    GraphicAdmin($hlpfile);
    OpenTable();
    echo "
    <center><font size=4><b>".translate("Forum Categories")."</b></font></center>
    <form action=admin.$phpEx method=post>
    <center><table border=1 width=100%><tr>
    <td bgcolor=$bgcolor2><font color=$textcolor2><center><b>".translate("ID")."</b></td>
    <td bgcolor=$bgcolor2><font color=$textcolor2><center><b>".translate("Categories")."</b></td>
    <td bgcolor=$bgcolor2><font color=$textcolor2><center><b>".translate("No. of Forum(s)")."</b></td>
    <td bgcolor=$bgcolor2><font color=$textcolor2><center><b>".translate("Functions")."</b></td></tr>";

    $result = mysql_query("select cat_id, cat_title from ".$mpnTables['categories']." order by cat_id");
    while(list($cat_id, $cat_title) = mysql_fetch_row($result)) {

    $gets = mysql_query("select count(*) as total from ".$mpnTables['forums']." where cat_id=$cat_id");
    $numbers= mysql_fetch_array($gets);
        echo "
    <td align=center>$cat_id</td>
    <td align=center>$cat_title</td>
    <td align=center>" . $numbers['total'] . "</td>
    <td align=center>[ <a href=\"admin.$phpEx?op=ForumGo&cat_id=$cat_id\">" .translate("Edit Forum(s)")."</a> | <a href=admin.$phpEx?op=ForumCatEdit&cat_id=$cat_id>".translate("Edit")."</a> | <a href=admin.$phpEx?op=ForumCatDel&cat_id=$cat_id&ok=0>".translate("Delete")."</a> ]</td><tr>";
    }
    echo "</form></td></tr></table>
    <br><br>
    </center><font size=4><b>".translate("Add Categories")."</b><br><br>
    <font size=2>
    <form action=admin.$phpEx method=post>
    <table border=0 width=100%><tr><td>
    ".translate("Categories:")." </td><td><input type=text name=catagories size=31></td></tr><tr><td>
    </td></tr></table>
    <input type=hidden name=op value=ForumCatAdd>
    <input class='button' type=submit value=".translate("Add").">
    </form>
    </td></tr></table></td></tr></table>";
    include("footer.$phpEx");
}

//function ForumGo($cat_id,$ctg) {
function ForumGo($cat_id) {
    global $hlpfile, $admin, $bgcolor2, $textcolor2, $mpnTables, $phpEx;
    include ("config.$phpEx");
    include ("header.$phpEx");
    @( include_once __ROOT__ . 'core_ForumEntry.php' )
      OR goBack( translate( 'Include Error' ) );
    //updateLastVisitCookie();
//    if ((!isset($ctg)) || ($ctg == "")) {
    list($ctg) = mysql_fetch_row(mysql_query("select cat_title from ".$mpnTables['categories']." where cat_id=$cat_id"));
//    }

    GraphicAdmin($hlpfile);
    OpenTable();
    echo "
    <center><font size=4><b>".translate("Forum(s) listed at")." $ctg</b></font></center>
    <form action=admin.$phpEx method=post>
    <center><table border=1 width=100%><tr>
    <td bgcolor=$bgcolor2><font color=$textcolor2><center><b>".translate("Forum Name")."</b></td>
    <td bgcolor=$bgcolor2><font color=$textcolor2><center><b>".translate("Description")."</b></td>
    <td bgcolor=$bgcolor2><font color=$textcolor2><center><b>".translate("Moderator")."</b></td>
    <td bgcolor=$bgcolor2><font color=$textcolor2><center><b>".translate("Access")."</b></td>
    <td bgcolor=$bgcolor2><font color=$textcolor2><center><b>".translate("Type")."</b></td>
    <td bgcolor=$bgcolor2><font color=$textcolor2><center><b>".translate("Functions")."</b></td></tr>";

    $result = mysql_query("select forum_id, forum_name, forum_desc, forum_access, forum_moderator, forum_type from ".$mpnTables['forums']." where cat_id='$cat_id'");
    while(list($forum_id, $forum_name, $forum_desc, $forum_access, $forum_moderator, $forum_type) = mysql_fetch_row($result)) {

/*
    $userdata = mysql_query("select uname from users where uid='$forum_moderator'");
        list($uname) = mysql_fetch_row($userdata);
*/

    //$moderators = get_moderators($forum_id);

    echo "
    <tr>
    <td align=center>$forum_name</td>
    <td align=center>$forum_desc</td>";

    echo "<td align=center>";

   /* $count = 0;
    if (is_array($moderators)) {
        reset ($moderators);
        while(list($null, $mods) = each($moderators)) {
            while(list($mod_id, $mod_name) = each($mods)) {
                if($count > 0) { echo ", "; }
                echo "<a href=\"user.$phpEx?op=userinfo&uname=$mod_name\">$mod_name</a>";
                $count++;
            }
        }
    }*/

    // Kiril ( 2004-02-06 ) : Moderator List Recode

    echo getModerators( $forum_id );

    echo "</td>";

    switch($forum_access) {
    case (0):
    echo "<td align=center>Anonymous Posting</td>";
    break;
    case (1):
    echo "<td align=center>Registered User</td>";
    break;
    case (2):
    echo "<td align=center>Moderators/Administrator</td>";
    break;
    }
    if ($forum_type==0) {
    echo "<td align=center>Public</td>";
    }
    else {
    echo "<td align=center>Private</td>";
    }
    echo "
    <td align=center><a href=admin.$phpEx?op=ForumGoEdit&forum_id=$forum_id>".translate("Edit")."</a> | <a href=admin.$phpEx?op=ForumGoDel&forum_id=$forum_id&ok=0>".translate("Delete")."</a></td></tr>";
    }
    echo "</form></td></tr></table>
    <br><br>
    </center><font size=4><b>".translate("Add More Forum for")." $ctg</b><br><br>
    <font size=2>
    <form action=admin.$phpEx method=post>
    <table border=0 width=100%>
    <tr><td>".translate("Forum Name:")." </td><td><input type=text name=forum_name size=31></td></tr>
    <tr><td>".translate("Description:")." </td><td><textarea name=forum_desc cols=60 rows=5></textarea></td></tr>
    <tr><td> ".translate("Moderator(s):")." </td>
    <td>";

    // echo "<b>Add:</b><br>";
    $sql = "SELECT u.uid, u.uname FROM ".$mpnTables['users']." u, ".$mpnTables['users_status']." s WHERE u.uid = s.uid AND u.uid > 1 AND s.level != -1 AND u.uid = s.uid ORDER BY u.uname";

    // echo "<SELECT NAME=forum_moderator>";
    echo "<SELECT NAME=\"forum_mods[]\" size=\"5\" multiple>";

    if(!$r = mysql_query($sql))
        die("An Error Occurred<HR>Could not connect to the database. Please check the config file.");
    if($row = mysql_fetch_array($r)) {
        do {
        $s = "";
//        if($row['uid'] == $myrow['forum_moderator']) { $s = "SELECTED"; }
        echo "<OPTION VALUE=\"" . $row['uid'] . "\" $s>" . $row['uname'] . "</OPTION>\n";
    } while($row = mysql_fetch_array($r));
    } else {
        echo "<OPTION VALUE=\"0\">None</OPTION>\n";
    }

    echo "</select></td></tr>";
    echo "<tr><td>".translate("Access Level:")." </td>
    <td><SELECT NAME=forum_access>
        <OPTION VALUE=0>Anonymous Posting</OPTION>
        <OPTION VALUE=1>Registered users only</OPTION>
        <OPTION VALUE=2>Moderators/Administrators only</OPTION>
    </SELECT>
    </td></tr>
    <tr><td>".translate("Type:")." </td>
    <td><SELECT NAME=forum_type>
            <OPTION VALUE=0>Public</OPTION>
            <OPTION VALUE=1>Private</OPTION>
        </SELECT>
    </td></tr>
    <tr><td>".translate("Password (if private):")." </td><td><input type=text name=forum_pass size=31></td></tr>
    </table>
    <input type=hidden name=cat_id value=\"$cat_id\">
    <input type=hidden name=op value=ForumGoAdd>
    <input class='button' type=submit value=".translate("Add").">
    </form>
    </td></tr></table></td></tr></table>";
    include("footer.$phpEx");
}

function ForumGoEdit($forum_id) {
    global $hlpfile, $admin, $mpnTables,$phpEx, $mpn_anonymous_id;
    include ("config.$phpEx");
    include ("header.$phpEx");
    GraphicAdmin($hlpfile);
    $result = mysql_query("select forum_id, forum_name, forum_desc, forum_access, cat_id, forum_type, forum_pass from ".$mpnTables['forums']." where forum_id='$forum_id'");
    list($forum_id, $forum_name, $forum_desc, $forum_access, $cat_id_1, $forum_type, $forum_pass) = mysql_fetch_row($result);
    OpenTable();
    echo "
    <center><font size=4><b>".translate("Edit")." $forum_name</b></font></center>
    <form action=admin.$phpEx method=post>
    <input type=hidden name=forum_id value=$forum_id>
    <table border=0 width=100%><tr><td>
    <tr><td>".translate("Forum Name:")." </td><td><input type=text name=forum_name size=31 value=\"$forum_name\"></td></tr>
    <tr><td>".translate("Description:")." </td><td><textarea name=forum_desc cols=60 rows=5>$forum_desc</textarea></td></tr>
    <tr><td> ".translate("Moderator:")." </td>
    <td>";

    echo "<b>" .translate("Current:")."</b><br>";

    $sql = "SELECT u.uname, u.uid FROM ".$mpnTables['users']." u, ".$mpnTables['forummods']." f WHERE f.forum_id = '$forum_id' AND u.uid = f.user_id";

    if(!$r = mysql_query($sql))
        die("Error connecting to the database.");

    $current_mods = array();

    if($row = mysql_fetch_array($r)) {
        do {
            echo $row['uname'] . " (<input type=\"checkbox\" name=\"rem_mods[]\" value=\"" . $row['uid'] . "\"> " .translate("Remove").")<BR>";
            $current_mods[] = $row['uid'];
        } while($row = mysql_fetch_array($r));

        echo "<BR>";
     } else {
        echo "" .translate("No Moderators Assigned")."<BR><BR>\n";
     }

    echo "<b>" .translate("Add").":</b><br>";

    if (is_array($current_mods)) {
        reset($current_mods);
    }
    $sql = "SELECT u.uid, u.uname FROM ".$mpnTables['users']." u, ".$mpnTables['users_status']." s WHERE u.uid = s.uid AND u.uid > 1 AND s.level != -1 AND u.uid = s.uid ";
    if (is_array($current_mods)) {
        while(list($null, $currMod) = each($current_mods)) {
            $sql .= "AND u.uid != $currMod ";
        }
    }
    $sql .= "ORDER BY u.uname";

    echo "<SELECT NAME=\"forum_mods[]\" size=\"5\" multiple>";

    if(!$r = mysql_query($sql))
        die("An Error Occurred<HR>Could not connect to the database. Please check the config file.");
    if($row = mysql_fetch_array($r)) {
        do {
        $s = "";
//        if($row['uid'] == $myrow['forum_moderator']) { $s = "SELECTED"; }
        echo "<OPTION VALUE=\"" . $row['uid'] . "\" $s>" . $row['uname'] . "</OPTION>\n";
    } while($row = mysql_fetch_array($r));
    } else {
        echo "<OPTION VALUE=\"0\">None</OPTION>\n";
    }

    echo "</select></td></tr>
    <tr><td>".translate("Access Level:")." </td>
    <td><SELECT NAME=forum_access>";

    $selected_option_0 = $selected_option_1 = $selected_option_2 = null;

    if ($forum_access == 0) {
        $selected_option_0 = " selected";
    }
    if ($forum_access == 1) {
        $selected_option_1 = " selected";
    }
    if ($forum_access == 2) {
        $selected_option_2 = " selected";
    }

        echo "<OPTION VALUE=0$selected_option_0>Anonymous Posting</OPTION>";
        echo "<OPTION VALUE=1$selected_option_1>Registered users only</OPTION>";
        echo "<OPTION VALUE=2$selected_option_2>Moderators/Administrators only</OPTION>";


    echo"</SELECT>";


    echo"
    </td></tr>
    <tr><td>".translate("Change Categories:")." </td>
    <td><SELECT NAME=cat_id>";
    $result = mysql_query("select cat_id, cat_title from ".$mpnTables['categories']);
    while(list($cat_id, $cat_title) = mysql_fetch_row($result)) {
    if ($cat_id == $cat_id_1) {
    echo "<OPTION VALUE=\"$cat_id\" selected>$cat_title</OPTION>";
    } else {
    echo "<OPTION VALUE=\"$cat_id\">$cat_title</OPTION>";
    }
    }
    echo "
    </SELECT>
    </td></tr>
    <tr><td>".translate("Type:")." </td>
    <td><SELECT NAME=forum_type>";
    if ($forum_type == 0) {
        echo "<OPTION VALUE=0 selected>Public</OPTION>";
    } else {
        echo "<OPTION VALUE=0>Public</OPTION>";
    }

    if ($forum_type == 1) {
    echo "<OPTION VALUE=1 selected>Private</OPTION>";
    } else {
    echo "<OPTION VALUE=1>Private</OPTION>";
    }

    echo "
        </SELECT>
    </td></tr>
    <tr><td>".translate("Password (if private):")." </td><td><input type=text name=forum_pass size=31 value=$forum_pass></td></tr>
    </table>
    <input type=hidden name=op value=ForumGoSave>
    <input class='button' type=submit value=".translate("Save Changes").">
    </form>

    </td></tr></table></td></tr></table>";
    include("footer.$phpEx");
}

function ForumCatEdit($cat_id) {
    global $hlpfile, $admin, $mpnTables,$phpEx;
    include ("config.$phpEx");
    include ("header.$phpEx");
    GraphicAdmin($hlpfile);
    $result = mysql_query("select cat_id, cat_title from ".$mpnTables['categories']." where cat_id='$cat_id'");
    list($cat_id, $cat_title) = mysql_fetch_row($result);
    OpenTable();
    echo "
    <center><font size=4><b>".translate("Edit Categories")."</b></font></center>
    <form action=admin.$phpEx method=post>
    <input type=hidden name=cat_id value=$cat_id>
    <table border=0 width=100%><tr><td>
    ".translate("Categories:")." </td><td><input type=text name=cat_title size=31 value=\"$cat_title\"></td></tr><tr><td>
    </td></tr></table>
    <input type=hidden name=op value=ForumCatSave>
    <input class='button' type=submit value=".translate("Save Changes").">
    </form>
    </td></tr></table></td></tr></table>";
    include("footer.$phpEx");
}


function ForumCatSave($cat_id, $cat_title) {
    global $mpnTables,$phpEx;
    mysql_query("update ".$mpnTables['categories']." set cat_title='$cat_title' where cat_id='$cat_id'");

    $_SERVER['F_LIB'] -> redirectRequest( 'admin.php?op=ForumAdmin' );

}

function ForumGoSave($forum_id, $forum_name, $forum_desc, $forum_access, $forum_mods, $rem_mods, $cat_id, $forum_type, $forum_pass) {
global $mpnTables,$phpEx;
/*  if (!$forum_pass) {
        mysql_query("update ".$mpnTables['forums']." set forum_name='$forum_name', forum_desc='$forum_desc', forum_access='$forum_access', forum_moderator='1', cat_id='$cat_id', forum_type='$forum_type' where forum_id='$forum_id'");
    } else { */
        mysql_query("update ".$mpnTables['forums']." set forum_name='$forum_name', forum_desc='$forum_desc', forum_access='$forum_access', forum_moderator='1', cat_id='$cat_id', forum_type='$forum_type', forum_pass='$forum_pass' where forum_id='$forum_id'");
/*  } */

    $count = 0;
        if (is_array($forum_mods)) {
        while(list($null, $mod) = each($forum_mods)) {

                if (list($ulevel) = mysql_fetch_array(mysql_query("SELECT level FROM ".$mpnTables['users_status']." WHERE uid = $mod"))) {

            if ($ulevel < 2) {

            if (!isset($level_query)) { $level_query = "UPDATE ".$mpnTables['users_status']." SET level = 2 WHERE "; }
            if ($count > 0) { $level_query .= "OR"; }
            $level_query .= "uid = '$mod' ";
            $count++;
            }

        }

        }
        }

    $count = 0;
        if (is_array($forum_mods)) {
        reset($forum_mods);
        while(list($null, $mod) = each($forum_mods)) {

        if (!mysql_query("INSERT INTO ".$mpnTables['forummods']." (forum_id, user_id) VALUES ('$forum_id','$mod')")) {
            echo "Error -- Add Moderator";
            echo mysql_errno() . " --- " . mysql_error() . "<br>";
            return false;
         }
        }
        }

     if (isset($level_query)) {

        if (!mysql_query($level_query)) {
            echo "Error -- Updating Level";
            return false;
        }

     }

    $count = 0;
        if (is_array($rem_mods)) {
        while(list($null, $rem_mod) = each($rem_mods)) {

        if (!mysql_query("DELETE FROM ".$mpnTables['forummods']." WHERE forum_id = '$forum_id' AND user_id = '$rem_mod'")) {
            echo "Error -- Delete Moderator";
            return false;
        }

        }
        }

    $_SERVER['F_LIB'] -> redirectRequest( 'admin.php?op=ForumAdmin' );

}

function ForumCatAdd($catagories) {
    global $mpnTables,$phpEx;
    mysql_query("insert into ".$mpnTables['categories']." values (NULL, '$catagories')");

    $_SERVER['F_LIB'] -> redirectRequest( 'admin.php?op=ForumAdmin' );

}

function ForumGoAdd($forum_name, $forum_desc, $forum_access, $forum_mods, $cat_id, $forum_type, $forum_pass) {
    // mysql_query("insert into forums values (NULL, '$forum_name', '$forum_desc', '$forum_access', '$forum_moderator', '$cat_id', '$forum_type', '$forum_pass')");
    global $mpnTables,$phpEx;
    mysql_query("insert into ".$mpnTables['forums']." values (NULL, '$forum_name', '$forum_desc', '$forum_access', '1', '$cat_id', '$forum_type', '$forum_pass')");

    list($forum_id) = mysql_fetch_array(mysql_query("SELECT forum_id FROM ".$mpnTables['forums']." WHERE forum_name = '$forum_name' AND forum_desc = '$forum_desc' AND cat_id = '$cat_id' AND forum_type = '$forum_type'"));

    $count = 0;
    if (is_array($forum_mods))
    {
    while(list($null, $mod) = each($forum_mods)) {

        if (list($ulevel) = mysql_fetch_array(mysql_query("SELECT level FROM ".$mpnTables['users_status']." WHERE uid = $mod"))) {

            if ($ulevel < 2) {

                if (!isset($level_query)) { $level_query = "UPDATE ".$mpnTables['users_status']." SET level = 2 WHERE "; }
                if ($count > 0) { $level_query .= "OR"; }
                $level_query .= "uid = '$mod' ";
                $count++;
            }

        }

     }

    $count = 0;
    reset($forum_mods);
    while(list($null, $mod) = each($forum_mods)) {

         if (!mysql_query("INSERT INTO ".$mpnTables['forummods']." (forum_id, user_id) VALUES ('$forum_id','$mod')")) {
            echo "Error -- Add Moderator";
            echo mysql_errno() . " --- " . mysql_error() . "<br>";
            return false;
         }
    }

    }

     if (isset($level_query)) {

        if (!mysql_query($level_query)) {
            echo "Error -- Updating Level";
            return false;
        }

     }

     $_SERVER['F_LIB'] -> redirectRequest( 'admin.php?op=ForumGo&cat_id=' . $cat_id );

}

function ForumCatDel($cat_id, $ok=0) {
    global $mpnTables,$phpEx;
    if($ok==1) {
    $result = mysql_query("select forum_id from ".$mpnTables['forums']." where cat_id='$cat_id'");
    while(list($forum_id) = mysql_fetch_row($result)) {
        mysql_query("delete from ".$mpnTables['forumtopics']." where forum_id=$forum_id");
        mysql_query("delete from ".$mpnTables['forummods']." where forum_id=$forum_id");
        mysql_query("delete from ".$mpnTables['posts']." where forum_id=$forum_id");
    }
    mysql_query("delete from ".$mpnTables['forums']." where cat_id=$cat_id");
    mysql_query("delete from ".$mpnTables['categories']." where cat_id=$cat_id");

    $_SERVER['F_LIB'] -> redirectRequest( 'admin.php?op=ForumAdmin' );


    } else {
    include("header.$phpEx");
    GraphicAdmin($hlpfile);
    OpenTable();
    echo "<center><br>";
    echo "<font size=3 color=Red>";
    echo "<b>".translate("WARNING: Are you sure you want to delete this Categories, Forums and all its Topics?")."</b><br><br><font color=Black>";
    }
    echo "[ <a href=admin.$phpEx?op=ForumCatDel&cat_id=$cat_id&ok=1>".translate("Yes")."</a> | <a href=admin.$phpEx?op=ForumAdmin>".translate("No")."</a> ]<br><br>";
    echo "</TD></TR></TABLE></TD></TR></TABLE>";
    include("footer.$phpEx");
}

function ForumGoDel($forum_id, $ok=0) {
    global $mpnTables,$phpEx;
    if($ok==1) {
    mysql_query("delete from ".$mpnTables['forumtopics']." where forum_id=$forum_id");
    mysql_query("delete from ".$mpnTables['forums']." where forum_id=$forum_id");
    mysql_query("delete from ".$mpnTables['forummods']." where forum_id=$forum_id");
    mysql_query("delete from ".$mpnTables['posts']." where forum_id=$forum_id");

    $_SERVER['F_LIB'] -> redirectRequest( 'admin.php?op=ForumAdmin' );

    } else {
    include("header.$phpEx");
    GraphicAdmin($hlpfile);
    OpenTable();
    echo "<center><br>";
    echo "<font size=3 color=Red>";
    echo "<b>".translate("WARNING: Are you sure you want to delete this Forums and all its Topics?")."</b><br><br><font color=Black>";
    }
    echo "[ <a href=admin.$phpEx?op=ForumGoDel&forum_id=$forum_id&ok=1>".translate("Yes")."</a> | <a href=admin.$phpEx?op=ForumAdmin>".translate("No")."</a> ]<br><br>";
    CloseTable();
    include("footer.$phpEx");
}

?>
