<?php

/********************************************************************************
	- MemHT Portal -
	
	Copyright (C) 2007-2008 by Miltenovik Manojlo
	http://www.memht.com
	
	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, or
	(at your opinion) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, see <http://www.gnu.org/licenses/> (GPLv2)
	or write to the Free Software Foundation, Inc., 51 Franklin Street,
	Fifth Floor, Boston, MA02110-1301, USA.
		
********************************************************************************/

if (!defined("_LOAD_PAGE_")) {
	die("<table style='padding: 2px; border: 1px solid #999; background-color: #EEE; font-family: Verdana; font-size: 10px;' align='center'><tr><td><b>Error:</b> This file cannot be opened directly!</td></tr></table>");
}

if (isSet($_GET['op'])) { $op = inCode($_GET['op']); } else { $op = ""; }
if (isSet($_GET['id'])) { $id = inCode($_GET['id']); } else { $id = ""; }
if (isSet($_GET['ok'])) { $ok = inCode($_GET['ok']); } else { $ok = false; }

if (isSet($_GET['pg'])) { $pg = inCode($_GET['pg']); } else { $pg = 1; }
$ofsppg = 20; //Items per page
$ofsbgn = ($pg*$ofsppg)-$ofsppg;

function mexList($ofsbgn,$ofsppg,$pg) {
	global $dblink,$siteConfig;

	$n = 0;
	echo "<table width='100%' align='center' cellspacing='1' cellpadding='0' class='std_nicetable'>";
	echo "<thead>\n";
	echo "<tr><td width='15%'>"._AUTHOR_."</td><td width='15%'>"._DATE_."</td><td>"._TEXT_."</td><td width='1%'>&nbsp;</td></tr>\n";
	echo "</thead>\n";
	echo "<tbody>\n";
	if ($result = $dblink->get_list("SELECT m.*,DATE_FORMAT(m.data, '".$siteConfig['timestamp']."') as data,u.id AS uid FROM memht_messagebox AS m JOIN memht_utenti AS u ON m.autore=u.user ORDER BY m.id DESC LIMIT $ofsbgn,$ofsppg")) {
		foreach ($result as $row) {
			$m_id = intval($row['id']);
			$uid = intval($row['uid']);
			$m_autore = outCode($row['autore']);
			$m_data = $row['data'];
			$m_testo = censure(bb2html(outCode($row['testo'])));
			$m_ip = outCode($row['ip']);
			$m_guest = intval($row['guest']);
			
			if ($m_guest==0) {
				$m_autore = "<a href='index.php?page=users&op=userInfo&uid=$uid' title='$m_autore'>$m_autore</a>";
			}
	
			$info = " <a href='admin.php?page=security&op=findip&ok=true&ip=$m_ip' title='Whois $m_ip' target='_blank'><img src='images/ip.gif' alt='Ip' border='0'></a>";
			$info .= " <a href='admin.php?page=messagebox&op=deleteMessage&id=$m_id' title='"._DELETE_."'><img src='images/delete.gif' title='"._DELETE_."' alt='Delete' border='0'></a>";
				
			$class = (($n++%2)!=0) ? "hlight" : "clean" ;
			echo "<tr><td class='$class' valign='top'><b>$m_autore</b></td><td class='$class' valign='top'>$m_data</td><td class='$class'>$m_testo</td><td class='$class' nowrap>$info</td></tr>\n";
		}
	} else {
		echo "<tr><td align='center' id='errorText' colspan='4' class='clean'><b>"._EMPTY_."</b></td></tr>";
	}
	
	echo "</tbody>\n";
	echo "</table>";
	
	//Pages
	include_once("inc/class/paginationSystem.class.php");
	$ps = new paginationSystem();
	$ps->items = $ofsppg;
	$ps->actpg = $pg;
	$ps->query = "SELECT id FROM memht_messagebox";
	$ps->url = "admin.php?page=messagebox&op=showMessages&pg={{N}}";
	$ps->show();
}

function delete($id,$ok=false) {
	global $dblink;
	
	if ($ok) {
		adminLog("[page:MessageBox][op:delete()][id:$id][query:".getenv('QUERY_STRING')."][END]");
		$dblink->query("DELETE FROM memht_messagebox WHERE id='$id'");
		echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=messagebox'>";		
	} else {
		echo "<div align='center'><b>"._SUREDELETEMESSAGE_."</b><br><a href='admin.php?page=messagebox&op=deleteMessage&id=$id&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='admin.php?page=messagebox' title='"._NO_."'>"._NO_."</a></div>";
	}
}

function deleteAll($ok=false) {
	global $dblink;
	
	if ($ok) {
		adminLog("[page:MessageBox][op:deleteAll()][query:".getenv('QUERY_STRING')."][END]");
		$dblink->query("DELETE FROM memht_messagebox");
		echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=messagebox'>";		
	} else {
		echo "<div align='center'><b>"._SURERESETMESSAGES_."</b><br><a href='admin.php?page=messagebox&op=reset&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='admin.php?page=messagebox' title='"._NO_."'>"._NO_."</a></div>";
	}
}

require_once("admin/inc/inc_header.php");
	admin_page_title($page);
							
	openTable();
		echo "<div align='center' class='box'>";
			echo "<a href='admin.php?page=messagebox' title='"._MESSAGES_."'><img src='admin/icons/messagebox.png' border='0' title='"._MESSAGES_."' align='top'> "._MESSAGES_."</a>";
			echo " - <a href='admin.php?page=messagebox&op=reset' title='"._RESET_."'><img src='admin/icons/attention.png' border='0' title='"._RESET_."' align='top'> "._RESET_."</a>";
		echo "</div>";
	closeTable();
	
	openTable();
	
		switch($op) {
			case "deleteMessage":
				delete($id,$ok);
			break;
			
			case "reset":
				deleteAll($ok);
			break;
		
			case "showMessages":
				mexList($ofsbgn,$ofsppg,$pg);
			break;
		
			default:
				mexList($ofsbgn,$ofsppg,$pg);
			break;
		}
	
	closeTable();
require_once("admin/inc/inc_footer.php");
?>