<?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 public message MAXIMUS directly...');


function public_message() {
	global $public_msg, $prefix, $user_prefix, $db, $user, $admin, $p_msg, $cookie, $broadcast_msg;
	if ($broadcast_msg == 1) {
		if (defined('USER_ALLOW')) {
			cookiedecode($user);
			$result = $db->sql_query("SELECT `broadcast` FROM `".TABLE_USERS."` WHERE `username`='$cookie[1]'");
			$row   = $db->sql_fetchrow($result);
			$upref = $db->XMFnum($row['broadcast']);
			if ($upref == 1) {
				$t_off = "<br /><p align=\"right\">[ <a href=\"modules.php?name=Your_Account&amp;op=edithome\"><font color=\"FFFFFF\" size=\"2\">"._TURNOFFMSG."</font></a> ]</font>";
				$pm_show = 1;
			} else {
				$pm_show = 0;
			}
		} else {
			$t_off = "";
		}
		if (!defined('USER_ALLOW') OR (defined('USER_ALLOW') AND ($pm_show == 1))) {
			$c_mid = base64_decode($p_msg);
			$c_mid = addslashes($c_mid);
			$c_mid = $db->XMFnum($c_mid);
			$result2 = $db->sql_query("SELECT `mid`, `content`, `date`, `who` FROM `".$prefix."_public_messages` WHERE `mid` > '$c_mid' ORDER BY `date` ASC LIMIT 1");
			$row2 = $db->sql_fetchrow($result2);
			$mid = $db->XMFnum($row2['mid']);
			$content = stripslashes($row2['content']);
			$tdate = stripslashes($row2['date']);
			$who = $db->XMFnum($row2['who']);
			if ((!isset($c_mid)) OR ($c_mid = $mid)) {
				$public_msg = "<br /><table width=\"90%\" border=\"1\" cellspacing=\"2\" cellpadding=\"0\" bgcolor=\"FFFFFF\" align=\"center\"><tr><td>\n";
				$public_msg .= "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"2\" bgcolor=\"FF0000\"><tr><td>\n";
				$public_msg .= "<font color=\"FFFFFF\" size=\"3\"><b>"._BROADCASTFROM." <a href=\"modules.php?name=Your_Account&amp;op=userinfo&amp;username=$who\"><font color=\"FFFFFF\" size=\"3\">$who</font></a>: \"$content\"</b>";
				$public_msg .= "$t_off";
				$public_msg .= "</td></tr></table>";
				$public_msg .= "</td></tr></table>";
				$ref_date = $tdate+600;
				$actual_date = time();
				if ($actual_date >= $ref_date) {
					$public_msg = "";
					$numrows = $db->sql_numrows($db->sql_query("SELECT `mid` FROM `".$prefix."_public_messages`"));
					if ($numrows == 1) {
						$db->sql_query("DELETE FROM `".$prefix."_public_messages`");
						$mid = 0;
					} else {
						$db->sql_query("DELETE FROM `".$prefix."_public_messages` WHERE `mid`='$mid'");
					}
				}
				if ($mid == 0 OR $mid == "") {
					@setcookie("p_msg");
				} else {
					$mid = base64_encode($mid);
					$mid = addslashes($mid);
					@setcookie("p_msg",$mid,time()+600);
				}
			}
		}
	} else {
		$public_msg = "";
	}
	return($public_msg);
}