<?php
/**
 * $Source: /home/cvs/nukescripts/kernel/blocks_phpnuke.php,v $
 * $Revision: 1.1.0a $
 * $Author: cyril $
 * $Date: 2006/12/29 10:55:03 $
 * Fix gestion des groupes Killermanlille le 28/12/2006
 */
/************************************************************************/
/* Maximus CMS: Web Portal System                                       */
/* ==============================                                       */
/*                                                                      */
/* Copyright (c) 2002 - 2006                                            */
/* http://www.php-maximus.fr                                            */
/* dev@php-maximus.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.       */
/************************************************************************/
/* Based on PHP-NUKE: Web Portal System                                 */
/************************************************************************/
defined('MAXIMUS') or die ('Access denied blocks phpnuke MAXIMUS directly...');

function userblock($side="") {
  global $cookie, $db;
  if(defined('USER_ALLOW') AND ($cookie[8])) {
    $sql = "SELECT `ublock` FROM `".TABLE_USERS."` WHERE `user_id`='$cookie[0]'";
if ( !($result = $db->sql_query($sql)) )
{ query_die(GENERAL_ERROR, 'Could not obtain users', '', __LINE__, __FILE__, $sql);}
    $row    = $db->sql_fetchrow($result);
    $ublock = stripslashes($row['ublock']);
    $title  = ""._MENUFOR." $cookie[1]";
    if($side == "c") {
      themecenterbox("".$title."", "".$ublock."");
    } elseif ($side == "d") {
      themecenterbox_low("".$title."", "".$ublock."");
    } elseif($side == "l" AND function_exists('themeleftbox')) {
      themeleftbox("".$title."", "".$ublock."");
    } elseif($side == "r" AND function_exists('themerightbox')) {
      themerightbox("".$title."", "".$ublock."");
    } else {
      themesidebox("".$title."", "".$ublock."");
    }
  }
}

function render_blocks($side, $blockfile, $title, $content, $bid, $url) {
  if (!defined('BLOCK_FILE')) { define('BLOCK_FILE', true); }
  if($url == "") {
    if($blockfile == "") {
      if($side == "c") {
        themecenterbox("".$title."", "".$content."");
      } elseif ($side == "d") {
        themecenterbox_low("".$title."", "".$content."");
      } elseif($side == "l" AND function_exists('themeleftbox')) {
        themeleftbox("".$title."", "".$content."");
      } elseif($side == "r" AND function_exists('themerightbox')) {
        themerightbox("".$title."", "".$content."");
      } else {
        themesidebox("".$title."", "".$content."");
      }
    } else {
      if($side == "c") {
        blockfileinc("".$title."", "".$blockfile."", 1);
       } elseif ($side == "d") {
        blockfileinc("".$title."", "".$blockfile."", 4);
      } elseif($side == "l") {
        blockfileinc("".$title."", "".$blockfile."", 2);
      } elseif($side == "r") {
        blockfileinc("".$title."", "".$blockfile."", 3);
      } else {
        blockfileinc("".$title."", "".$blockfile."");
      }
    }
  } else {
if (is_file("kernel/addons/headlines.php")) {include_once("kernel/addons/headlines.php");}
    if($side == "c" OR $side == "d") {
      headlines($bid, 1);
    } elseif($side == "l") {
      headlines($bid, 2);
    } elseif($side == "r") {
      headlines($bid, 3);
    } else {
      headlines($bid);
    }
  }
}

function blocks($side) {
  global $storynum, $multilingual, $currentlang, $db, $name;
  if($multilingual == 1) {
    $querylang = "AND (blanguage='$currentlang' OR blanguage='')";
  } else {
    $querylang = "";
  }
  if(strtolower($side[0]) == "l") {
    $pos = "l";
  } elseif(strtolower($side[0]) == "r") {
    $pos = "r";
  } elseif(strtolower($side[0]) == "c") {
    $pos = "c";
  } elseif  (strtolower($side[0]) == "d") {
    $pos = "d";
  }
  $side = $pos;
  if (strlen($name) != 0) {
 $name = str_replace(' ','_',$name);
 $name = str_replace('%20','_',$name);
 $sql = "SELECT b.bid, b.bkey, b.title, b.content, b.url, b.blockfile, b.view, b.groups, b.expire, b.action, b.subscription FROM ".TABLE_BLOCKS." b, ".TABLE_BLOCKSMANAGER." m WHERE b.bid=m.bid AND m.title='$name' AND m.bposition='$pos' AND b.active='1' $querylang ORDER BY m.weight ASC";
 if ( !($result = $db->sql_query($sql)) )
{ query_die(GENERAL_ERROR, 'Could not obtain config blocks', '', __LINE__, __FILE__, $sql);}
 } else {
 $sql = "SELECT b.bid, b.bkey, b.title, b.content, b.url, b.blockfile, b.view, b.groups, b.expire, b.action, b.subscription FROM ".TABLE_BLOCKS." b, ".TABLE_BLOCKSMANAGER." m WHERE b.bid=m.bid AND m.title='admin' AND m.bposition='$pos' AND b.active='1' $querylang ORDER BY m.weight ASC";
 if ( !($result = $db->sql_query($sql)) )
{ query_die(GENERAL_ERROR, 'Could not obtain config blocks', '', __LINE__, __FILE__, $sql);}
 }
  while($row = $db->sql_fetchrow($result)) {
    $bid       = $db->XMFnum($row['bid']);
    $bkey      = stripslashes($row['bkey']);
    $title     = stripslashes(check_html($row['title'], "nohtml"));
    $content   = stripslashes($row['content']);
    $url       = stripslashes($row['url']);
    $blockfile = stripslashes($row['blockfile']);
    $view      = $db->XMFnum($row['view']);
    $groups    = $db->XMFnum($row['groups']);
    $expire    = $db->XMFnum($row['expire']);
    $action    = stripslashes($row['action']);
    $action    = substr("$action", 0,1);
    $now       = time();
    $sub       = $db->XMFbin($row['subscription']);
    if($sub == 0 OR ($sub == 1 AND !paid())) {
      if($expire != 0 AND $expire <= $now) {
        if($action == "d") {
          $db->sql_query("UPDATE ".TABLE_BLOCKS." SET active='0', expire='0' WHERE bid='$bid'");
          return;
        } elseif($action == "r") {
          $db->sql_query("DELETE FROM ".TABLE_BLOCKS." WHERE bid='$bid'");
          return;
        }
      }
      if($bkey == "admin") {
        adminblock("".$side."");
      } elseif($bkey == "userbox") {
        userblock("".$side."");
      } elseif($bkey == "") {
        if($view == 0) {
          render_blocks("".$side."", "".$blockfile."", "".$title."", "".$content."", "".$bid."", "".$url."");
        } elseif($view == 1 AND defined('USER_ALLOW') || defined('ADMIN_ALLOW')) {
          render_blocks("".$side."", "".$blockfile."", "".$title."", "".$content."", "".$bid."", "".$url."");
        } elseif($view == 2 AND defined('ADMIN_ALLOW')) {
          render_blocks("".$side."", "".$blockfile."", "".$title."", "".$content."", "".$bid."", "".$url."");
        } elseif($view == 3 AND !defined('USER_ALLOW') || defined('ADMIN_ALLOW')) {
          render_blocks("".$side."", "".$blockfile."", "".$title."", "".$content."", "".$bid."", "".$url."");
        } elseif($view > 3 AND in_groups($groups)) {
          render_blocks("".$side."", "".$blockfile."", "".$title."", "".$content."", "".$bid."", "".$url."");
        }
      }
    }
  }
$db->sql_freeresult($result);
}

function blockfileinc($title, $blockfile, $side=0) {
  $blockfiletitle = $title;
  $file = @file("blocks/".$blockfile."");
  if(!$file) { $content = _BLOCKPROBLEM; } else { include_once("blocks/".$blockfile.""); }
  if($content == "") { $content = _BLOCKPROBLEM2; }
  if($side == 1) {
    themecenterbox("".$blockfiletitle."", "".$content."");
  } elseif($side == 2 AND function_exists('themeleftbox')) {
    themeleftbox("".$blockfiletitle."", "".$content."");
  } elseif($side == 3 AND function_exists('themerightbox')) {
    themerightbox("".$blockfiletitle."", "".$content."");
  } elseif($side == 4) {
    themecenterbox_low("".$blockfiletitle."", "".$content."");
  } else {
    themesidebox("".$blockfiletitle."", "".$content."");
  }
}