<?php
/* =====================================================================*/
/* Maximus: Advanced Content Management System                          */
/* =====================================================================*/
/* Version d�i� �Maximus                                             */
/* Controle fonctionnement et modifications r�lis�s:                  */
/* Cyril - Benoit - Louis - diabolo                                     */
/* http://www.php-maximus.fr                                            */
/* dev@php-maximus.org                                                  */
/************************************************************************/
/* WB_BlocksManager v1.0                                                */
/* Module for phpnuke 6.x and 7.x by Paulo FERREIRA                     */
/* Copyright (C) 2003 Paulo Ferreira                                    */
/* Web:   http://www.phpnuke-belgique.org/                              */
/* Email: webmaster@phpnuke-belgique.org                                */
/* =====================================================================*/
/* James Johnston                                                       */
/* http://www.techknowpro.com                                           */
/* =====================================================================*/
/* nono                                                                 */
/* http://osc2nuke.org/                                                 */
/* =====================================================================*/
/* PHP-NUKE: Web Portal System                                          */
/* =====================================================================*/
/*                                                                      */
/* Copyright (c) 2002 by Francisco Burzi                                */
/* http://phpnuke.org                                                   */
/*                                                                      */
/* 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.       */
/************************************************************************/
if (!defined('ADMIN_ALLOW') && !defined('ADMIN_MAX')) {
	die ("Access Denied");
}
global $admin_file;
$aid   = substr("$aid", 0,25);
$row   = $GLOBALS['db']->sql_fetchrow($GLOBALS['db']->sql_query("SELECT `radminsuper` FROM `".TABLE_AUTHORS."` WHERE `aid`='$aid'"));
$acces = $GLOBALS['db']->XMFbin($row['radminsuper']);
if ($acces == 1) {

get_adminlang("blocks_manager");

function rssfail() {
    incoh();
    Max_OpenTable();
    echo "<center><font class='title'><b>"._BLOCKSADMIN."</b></font></center>";
    Max_CloseTable();
    br('1');
    Max_OpenTable();
    echo "<center><b>"._RSSFAIL."</b><br /><br />"
	.""._RSSTRYAGAIN."<br /><br />"
	.""._GOBACK."</center>";
    Max_CloseTable();
    incof();
    die;
}


function BlocksManager_FixWeight($wb_name) {
    global $admin_file;
    $position[] = 'l';
    $position[] = 'r';
    $position[] = 'c';
    $position[] = 'd';
    $position[] = '';
    foreach($position as $pKey => $pValue) {
        $result = $GLOBALS['db']->sql_query("SELECT `bid` FROM `".TABLE_BLOCKSMANAGER."` WHERE `title`='$wb_name' AND `bposition`='$pValue' ORDER BY `weight` ASC");
        $weight = 0;
	while( $row = $GLOBALS['db']->sql_fetchrow($result)) {
	    $bid = $GLOBALS['db']->XMFnum($row['bid']);
            $weight++;
            if ($pValue != '') : $GLOBALS['db']->sql_query("UPDATE `".TABLE_BLOCKSMANAGER."` SET `weight`='$weight' WHERE `title`='$wb_name' AND `bid`='$bid'");
            else : $GLOBALS['db']->sql_query("UPDATE `".TABLE_BLOCKSMANAGER."` SET `weight`='$weight', `bposition`='r' WHERE `title`='$wb_name' AND `bid`='$bid'");
            endif;
        }
    }
}

function BlocksManager_BlockPosition($bid, $wb_name, $position) {
    global $admin_file;
    $GLOBALS['db']->sql_query("UPDATE `".TABLE_BLOCKSMANAGER."` SET `bposition`='$position' WHERE `bid`='$bid' AND `title`='$wb_name'");
    BlocksManager_FixWeight($wb_name);
    $GLOBALS['db']->sql_query("OPTIMIZE TABLE `".TABLE_BLOCKSMANAGER."`");
    redirect_url("".$admin_file.".php?op=BlocksManager&wb_name=$wb_name");
}

function BlocksManager_View($bid, $wb_name, $view) {
    global $admin_file;
    $GLOBALS['db']->sql_query("UPDATE `".TABLE_BLOCKS."` SET `view`='$view' WHERE `bid`='$bid'");
    BlocksManager_FixWeight($wb_name);
    $GLOBALS['db']->sql_query("OPTIMIZE TABLE `".TABLE_BLOCKSMANAGER."`");
    redirect_url("".$admin_file.".php?op=BlocksManager&wb_name=$wb_name");
}

function BlocksManager_ChangeStatus($bid, $wb_name, $active) {
    global $admin_file;
    $GLOBALS['db']->sql_query("UPDATE `".TABLE_BLOCKS."` SET `active`='$active' WHERE `bid`='$bid'");
    BlocksManager_FixWeight($wb_name);
    $GLOBALS['db']->sql_query("OPTIMIZE TABLE `".TABLE_BLOCKSMANAGER."`");
    redirect_url("".$admin_file.".php?op=BlocksManager&wb_name=$wb_name");
}

function BlocksManager_BlockOrder($wb_name, $weightrep, $weight, $bidrep, $bidori) {
    global $admin_file;
    $result  = $GLOBALS['db']->sql_query("UPDATE `".TABLE_BLOCKSMANAGER."` SET `weight`='$weight' WHERE `bid`='$bidrep'");
    $result2 = $GLOBALS['db']->sql_query("UPDATE `".TABLE_BLOCKSMANAGER."` SET `weight`='$weightrep' WHERE `bid`='$bidori'");
    BlocksManager_FixWeight($wb_name);
    $GLOBALS['db']->sql_query("OPTIMIZE TABLE `".TABLE_BLOCKSMANAGER."`");
    redirect_url("".$admin_file.".php?op=BlocksManager&wb_name=$wb_name");
}

function OpenHeader($title="") {
    incoh();
    global $admin_file, $wb_name;
    title($title);
    Max_OpenTable();
    if ( $wb_name == 'admin' ) {
    	echo "<center><font color='red'><b>"._HELPSELECTMODULE."</b></font></center>";
    } else {
    	echo "<center>"._HELPBLOCKMANAGER." <font color='green'><b>$wb_name</b></font></center>";
    }
    Max_CloseTable();
    br('1');
    Max_OpenTable();
    echo "<table width=\"80%\" align=\"center\"><td width=\"50%\" align=\"center\" valign=\"top\">"
        ."<a href=\"".$admin_file.".php?op=BlocksManager\">"._BLOCKMANAGEMENT."</a><br />";

if ( !empty($wb_name) ) {
	echo     "<a href=\"".$admin_file.".php?op=FixConflicts&amp;wb_name=$wb_name\">"._FIXBLOCKS." ($wb_name) </a><br />";
}

echo     "<a href=\"".$admin_file.".php\"><i>Administration</i></a></td>"
        ."<td width=\"50%\" align=\"center\" valign=\"top\">"
	."<a href=\"".$admin_file.".php?op=BlocksManager_AddBlock\">"._ADDBLOCK."</a><br />"
	."<a href=\"".$admin_file.".php?op=BlocksManager_AllBlocks\">"._EDITDELETEBLOCK."</a><br />"
	."<a href=\"".$admin_file.".php?op=BlocksManager_AllModules\">"._ALLACTIVESMODULES."</a></td></table>";
    Max_CloseTable();
    br('1');
    Max_OpenTable();

}

function CloseFooter() {
    Max_CloseTable();
    incof();
}

function BlocksManager_Add($bid, $wb_name) {
    global $admin_file;
if ($wb_name != "admin") {

    $sql = "SELECT MAX(weight) FROM `".TABLE_BLOCKSMANAGER."` WHERE `title`='$wb_name'";
    $result = $GLOBALS['db']->sql_query($sql);
    $weight = $GLOBALS['db']->XMFnum($row['weight']);
    foreach($bid as $bKey => $bValue) {
        $weight++;
        $GLOBALS['db']->sql_query("INSERT INTO `".TABLE_BLOCKSMANAGER."` VALUES ('$bValue', '$wb_name', 'l', '$weight')");
    }
    BlocksManager_FixWeight($title);

}
  redirect_url("".$admin_file.".php?op=BlocksManager&wb_name=$wb_name");
}

function BlocksManager_Remove($bid, $wb_name) {
    global $admin_file;
if ($wb_name != "admin") {

    foreach($bid as $bKey => $bValue) {
        $GLOBALS['db']->sql_query("DELETE FROM `".TABLE_BLOCKSMANAGER."` WHERE `bid`='$bValue' AND `title`='$wb_name'");
    }
    $GLOBALS['db']->sql_query("OPTIMIZE TABLE `".TABLE_BLOCKSMANAGER."`");
    BlocksManager_FixWeight($title);

}
    redirect_url("".$admin_file.".php?op=BlocksManager&wb_name=$wb_name");
}

if($admintest && $adminsecu)  {
switch($op) {

    case 'BlocksManager':
    if (!isset($wb_name)) { $wb_name = 'admin'; }
    include_once ('admin/modules/blocks_manager/blocksmanager.php');
    break;

    case 'BlocksManager_AllModules':
    include_once ('admin/modules/blocks_manager/blocksmanager_allmodules.php');
    break;

    case 'BlocksManager_AddBlock':
    include_once ('admin/modules/blocks_manager/blocksmanager_addblock.php');
    break;

    case 'BlocksManager_AllBlocks':
    include_once ('admin/modules/blocks_manager/blocksmanager_allblocks.php');
    break;

    case 'BlocksManager_DelBlock';
    include_once ('admin/modules/blocks_manager/blocksmanager_delblock.php');
    break;

    case 'BlocksManager_BlocksEdit':
    include_once ('admin/modules/blocks_manager/blocksmanager_blocksedit.php');
    break;

    case 'BlocksManager_BlocksEditSave':
    include_once ('admin/modules/blocks_manager/blocksmanager_blockseditsave.php');
    break;

    case 'BlocksManager_Add':
    BlocksManager_Add($bida, $wb_name, $renvoi);
    break;

    case 'BlocksManager_Remove':
    BlocksManager_Remove($bidr, $wb_name);
    break;

    case 'BlocksManager_BlockPosition':
    BlocksManager_BlockPosition($bid, $wb_name, $position);
    break;

    case 'BlocksManager_View':
    BlocksManager_View($bid, $wb_name, $view);
    break;

    case 'BlocksManager_BlockOrder':
    BlocksManager_BlockOrder($wb_name, $weightrep, $weight, $bidrep, $bidori);
    break;

    case 'BlocksAdd':
    include_once ('admin/modules/blocks_manager/blocksadd.php');
    break;

    case 'BlocksManager_ChangeStatus':
    BlocksManager_ChangeStatus($bid, $wb_name, $active);
    break;

    case 'BlocksManager_block_show':
    include_once ('admin/modules/blocks_manager/blocksmanager_block_show.php');
    break;

    case 'HeadlinesAdmin':
    include_once ('admin/modules/blocks_manager/HeadlinesAdmin.php');
    break;

    case 'HeadlinesEdit':
    include_once ('admin/modules/blocks_manager/HeadlinesEdit.php');
    break;

    case 'HeadlinesAdd':
    include_once ('admin/modules/blocks_manager/HeadlinesAdd.php');
    break;

    case 'HeadlinesDel':
    include_once ('admin/modules/blocks_manager/HeadlinesDel.php');
    break;

    case 'HeadlinesSave':
    include_once ('admin/modules/blocks_manager/HeadlinesSave.php');
    break;

    case 'FixConflicts':
    include_once ('admin/modules/blocks_manager/FixConflicts.php');
    break;

}
}
} else {
    echo "Access Denied";
}