<?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>");
}

	openTable();

		echo "<img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php' title='"._HOME_."'><b>"._HOME_."</b></a><br><br>\n";

		//Pagine
		$myrank = myRank();
		$result = $dblink->get_list("SELECT nome,titolo FROM memht_pagine WHERE enabled=1 AND rank<=$myrank ORDER BY titolo");
		foreach ($result as $row) {
			$nome = outCode($row['nome']);
			$titolo = outCode($row['titolo']);
			
			if (!stristr("banners",$nome) AND !stristr("error",$nome) AND !stristr("messages",$nome)) {
				echo "<img src='images/bullet_paper.gif' border='0' alt='-'> <a href=\"index.php?page=$nome\" title=\"$titolo\"><b>$titolo</b></a><br>\n";
				switch($nome) {
					case "articles":
						articles();
					break;
					case "blog":
						blog();
					break;
					case "download":
						download();
					break;
					case "faq":
						faq();
					break;
					case "forum":
						forum();
					break;
					case "guide":
						guide();
					break;
					case "mypage":
						mypage();
					break;
					case "news":
						news();
					break;
				}
			}
		}
		
		if ($siteConfig['virtualpages']==1) {
			echo "<br><br>";
			$result = $dblink->get_list("SELECT name,title FROM memht_virtualpages WHERE enabled=1 AND rank<=$myrank ORDER BY title");
			foreach ($result as $row) {
				$name = outCode($row['name']);
				$title = outCode($row['title']);
				
				echo "<img src='images/bullet_paper.gif' border='0' alt='-'> <a href=\"index.php?page=$name\" title=\"$title\"><b>$title</b></a><br>\n";
			}
		}

	closeTable();

function articles() {
	global $dblink;
	
	$result_art = $dblink->get_list("SELECT id,nome FROM memht_articoli WHERE enabled=1 ORDER BY id DESC LIMIT 50");
	foreach ($result_art as $row_art) {
		$id = intval($row_art['id']);
		$nome = outCode($row_art['nome']);
		
		echo "&nbsp;&nbsp;- <span class='small'><a href=\"index.php?page=articles&op=readArticle&id=$id&title=".mem_urlencode($nome)."\" title=\"$nome\">$nome</a></span><br>\n";
	}
}

function blog() {
	global $siteConfig,$dblink,$now;
	
	$result = $dblink->get_list("SELECT id,title FROM memht_blog_posts WHERE enabled=1 ORDER BY id DESC LIMIT 50");
	foreach ($result as $row) {
		$id = intval($row['id']);
		$title = outCode($row['title']);
		
		echo "&nbsp;&nbsp;- <span class='small'><a href=\"index.php?page=blog&id=$id&title=".mem_urlencode($title)."\" title=\"$title\">$title</a></span><br>\n";
	}
}

function download() {
	global $dblink;
	
	$result_dw = $dblink->get_list("SELECT id,nome FROM memht_download ORDER BY id DESC LIMIT 50");
	foreach ($result_dw as $row_dw) {
		$id = intval($row_dw['id']);
		$nome = outCode($row_dw['nome']);
		
		echo "&nbsp;&nbsp;- <span class='small'><a href=\"index.php?page=download&op=getFile&id=$id&title=".mem_urlencode($nome)."\" title=\"$nome\">$nome</a></span><br>\n";
	}
}

function faq() {
	global $dblink;
	
	$result_faq = $dblink->get_list("SELECT id,argomento,domanda FROM memht_faq WHERE enabled=1 ORDER BY id DESC LIMIT 50");
	foreach ($result_faq as $row_faq) {
		$id = intval($row_faq['id']);
		$domanda = outCode($row_faq['domanda']);
				
		echo "&nbsp;&nbsp;- <span class='small'><a href='index.php?page=faq&op=read&id=$id' title=\"$domanda\">$domanda</a></span><br>\n";
	}
}

function forum() {
	global $dblink,$siteConfig,$admin,$user;
	
	require_once("pages/forum/inc_functions.php");
	
	$result = $dblink->get_list("SELECT p.id, p.parent, p.forum, p.name, p.author,DATE_FORMAT(p.date,'".$siteConfig['timestamp']."') as date,p.pname, f.auth_view, f.auth_read FROM (SELECT i.id,i.parent,i.forum,i.name,i.author,i.date,(SELECT name FROM memht_forum_posts WHERE id=i.parent) AS pname FROM memht_forum_posts AS i WHERE parent>0 AND flag <2 ORDER BY id DESC) AS p JOIN memht_forum_forums AS f ON p.forum=f.id GROUP BY p.parent ORDER BY p.id DESC LIMIT 50");
	$result_p = $dblink->get_list("SELECT p.id, p.parent, p.forum, p.name, p.author,DATE_FORMAT(p.date,'".$siteConfig['timestamp']."') as date,p.name AS pname, f.auth_view, f.auth_read FROM memht_forum_posts AS p JOIN memht_forum_forums AS f ON p.forum=f.id WHERE p.parent=0 ORDER BY p.id DESC LIMIT 50");	
	
	for ($i=0;$i<sizeof($result_p);$i++) {
		if (!deep_in_array($result_p[$i]['id'],$result)) {
			$result[] = $result_p[$i];
		}
	}
	
	usort($result,"cmps");
	
	for ($i=0;$i<sizeof($result);$i++) {
		$bid = intval($result[$i]['id']);
		$bparent = intval($result[$i]['parent']);
		$bforum = intval($result[$i]['forum']);
		$btitle = outCode($result[$i]['pname'],0);
		$bauthor = outCode($result[$i]['author']);
		$bdate = $result[$i]['date'];
		$bauthv = intval($result[$i]['auth_view']);
		$bauthr = intval($result[$i]['auth_read']);
		
		if ($bparent==0) {
			$threadid = $bid;
			$link = "index.php?page=forum&op=viewThread&id=$threadid&title=".mem_urlencode($btitle);
		} else {
			$threadid = $bparent;
			
			$pstnum = $dblink->get_num("SELECT id FROM memht_forum_posts WHERE id='$threadid' OR parent='$threadid'");
			if ($pstnum>10) { $plus = "&pg=".ceil($pstnum/10); } else { $plus = ""; }

			$link = "index.php?page=forum&op=viewThread&id=$threadid&title=".mem_urlencode($btitle)."$plus#post$bid";
		}
		
		$myrank = myRank();
		
		if ($myrank>=$bauthv AND $myrank>=$bauthr) {
			echo "&nbsp;&nbsp;- <span class='small'><a href=\"$link\" title=\"$btitle\">$btitle</a></span><br>\n";
		}
		if ($i>=49) { break; }
	}
}

function cmps($a, $b) {
	if ($a == $b) {
		return 0;
	}
	return ($a > $b) ? -1 : 1;
}

function guide() {
	global $dblink;
	
	$result_gui = $dblink->get_list("SELECT id,nome FROM memht_guide WHERE enabled=1 ORDER BY id DESC LIMIT 50");
	foreach ($result_gui as $row_gui) {
		$id = intval($row_gui['id']);
		$nome = outCode($row_gui['nome']);
				
		echo "&nbsp;&nbsp;- <span class='small'><a href=\"index.php?page=guide&op=readGuide&id=$id&title=".mem_urlencode($nome)."\" title=\"$nome\">$nome</a></span><br>\n";
	}
}

function mypage() {
	global $dblink;
	
	$myrank = myRank();
	$result = $dblink->get_list("SELECT id,title FROM memht_mypage WHERE public=1 AND enabled=1 AND rank<=$myrank ORDER BY id LIMIT 50");
	foreach ($result as $row) {
		$id = intval($row['id']);
		$title = outCode($row['title']);
		
		echo "&nbsp;&nbsp;- <span class='small'><a href=\"index.php?page=mypage&op=openPage&id=$id&title=".mem_urlencode($title)."\" title=\"$title\">$title</a></span><br>\n";
	}
}

function news() {
	global $dblink;
	
	$result_new = $dblink->get_list("SELECT id,nome FROM memht_news WHERE enabled=1 ORDER BY id DESC LIMIT 50");
	foreach ($result_new as $row_new) {
		$id = intval($row_new['id']);
		$nome = outCode($row_new['nome']);
				
		echo "&nbsp;&nbsp;- <span class='small'><a href=\"index.php?page=news&op=readNews&id=$id&title=".mem_urlencode($nome)."\" title=\"$nome\">$nome</a></span><br>\n";
	}
}

?>