<?php
/************************************************************************/
/* 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 message box MAXIMUS directly...');

function message_box() {
  global $bgcolor1, $bgcolor2, $textcolor2, $multilingual, $currentlang, $admin_file;
  $currentlang = $GLOBALS['db']->XMFlang($currentlang);
  if($multilingual == 1) {
    $querylang = "AND (`mlanguage`='$currentlang' OR `mlanguage`='')";
  } else {
    $querylang = "";
  }
  $sql = "SELECT `mid`, `title`, `content`, `date`, `expire`, `view` FROM `".TABLE_MESSAGE."` WHERE `active`='1' $querylang";
  if ( !($result = $GLOBALS['db']->sql_query($sql)) )
{ query_die(GENERAL_ERROR, 'Could not obtain config messages', '', __LINE__, __FILE__, $sql);}

  if($numrows = $GLOBALS['db']->sql_numrows($result) == 0) {
  $GLOBALS['db']->sql_freeresult($result);
    return;
  } else {
    while($row = $GLOBALS['db']->sql_fetchrow($result)) {
      $mid     = $GLOBALS['db']->XMFnum($row['mid']);
      $title   = stripslashes(check_html($row['title'], "nohtml"));
      $content = stripslashes($row['content']);
      $mdate   = stripslashes($row['date']);
      $expire  = $GLOBALS['db']->XMFnum($row['expire']);
      $view    = $GLOBALS['db']->XMFnum($row['view']);
      if($title != "" && $content != "") {
        if($expire == 0) {
          $remain = _UNLIMITED;
        } else {
          $etime = (($mdate+$expire)-time())/3600;
          $etime = (int)$etime;
          if($etime < 1) {
            $remain = _EXPIRELESSHOUR;
          } else {
            $remain = ""._EXPIREIN." $etime "._HOURS."";
          }
        }
        $alink = "";
        if($view == 5 AND paid()) {
          if(defined('ADMIN_ALLOW')) {
            $alink = "<br /><br /><center><font class='content'>[ "._MVIEWSUBUSERS." - $remain - <a href=\"".$admin_file.".php?op=editmsg&amp;mid=$mid\">"._EDIT."</a> ]</font></center>";
          }
          $content .= $alink;
          if(function_exists('thememessagebox')) {
            thememessagebox($title, $content);
          } else {
            themecenterbox($title, $content);
          }
        } elseif($view == 4 AND defined('ADMIN_ALLOW')) {
          $alink = "<br /><br /><center><font class='content'>[ "._MVIEWADMIN." - $remain - <a href=\"".$admin_file.".php?op=editmsg&amp;mid=$mid\">"._EDIT."</a> ]</font></center>";
          $content .= $alink;
          if(function_exists('thememessagebox')) {
            thememessagebox($title, $content);
          } else {
            themecenterbox($title, $content);
          }
        } elseif($view == 3 AND defined('USER_ALLOW') || defined('ADMIN_ALLOW')) {
          if(defined('ADMIN_ALLOW')) {
            $alink = "<br /><br /><center><font class='content'>[ "._MVIEWUSERS." - $remain - <a href=\"".$admin_file.".php?op=editmsg&amp;mid=$mid\">"._EDIT."</a> ]</font></center>";
          }
          $content .= $alink;
          if(function_exists('thememessagebox')) {
            thememessagebox($title, $content);
          } else {
            themecenterbox($title, $content);
          }
        } elseif($view == 2 AND !defined('USER_ALLOW') || defined('ADMIN_ALLOW')) {
          if(defined('ADMIN_ALLOW')) {
            $alink = "<br /><br /><center><font class='content'>[ "._MVIEWANON." - $remain - <a href=\"".$admin_file.".php?op=editmsg&amp;mid=$mid\">"._EDIT."</a> ]</font></center>";
          }
          $content .= $alink;
          if(function_exists('thememessagebox')) {
            thememessagebox($title, $content);
          } else {
            themecenterbox($title, $content);
          }
        } elseif($view == 1) {
          if(defined('ADMIN_ALLOW')) {
            $alink = "<br /><br /><center><font class='content'>[ "._MVIEWALL." - $remain - <a href=\"".$admin_file.".php?op=editmsg&amp;mid=$mid\">"._EDIT."</a> ]</font></center>";
          }
          $content .= $alink;
          if(function_exists('thememessagebox')) {
            thememessagebox($title, $content);
          } else {
            themecenterbox($title, $content);
          }
        }
        if($expire != 0) {
          $past = time()-$expire;
          if($mdate < $past) {
            $GLOBALS['db']->sql_query("UPDATE `".TABLE_MESSAGE."` SET `active`='0' WHERE `mid`='$mid'");
          }
        }
      }
    }
  }
}