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

$op = (isset($_GET['op'])) ? inCode($_GET['op']) : "" ;
$ok = (isset($_GET['ok'])) ? inCode($_GET['ok']) : false ;
$pg = (isset($_GET['pg'])) ? intval($_GET['pg']) : 1 ;

if (isset($_GET['id'])) {
	$id = intval($_GET['id']);
	$title = "";
} else {
	if (isset($_GET['title'])) { $title = mem_urldecode(inCode($_GET['title'])); } else { $title = ""; }
	$id = "";
}

$ofsppg = 10; //Items per page
$ofsbgn = ($pg*$ofsppg)-$ofsppg;
	
	switch($op) {
		case "openPage":
			openPage($id,$title,$pg);
		break;
		
		default:
			if ($id>0 AND $title!="") { header("Location: index.php?page=mypage&op=openPage&id=$id&title=$title"); die(); }
			myPageMain($ofsbgn,$ofsppg,$pg);
		break;
	}

function myPageMain($ofsbgn,$ofsppg,$pg) {
	global $dblink;
	
	openTable();
	
	$myrank = myRank();
	$result = $dblink->get_list("SELECT id,title FROM memht_mypage WHERE public=1 AND enabled=1 AND rank<=$myrank ORDER BY title");
	foreach ($result as $row) {
		$id = intval($row['id']);
		$title = outCode($row['title']);
		
		echo "<div><img src='images/bullet_paper.gif' alt='Bullet'> <a href=\"index.php?page=mypage&op=openPage&id=$id&title=".mem_urlencode($title)."\" title=\"$title\">$title</a></div>\n";
	}
	
	closeTable();
}

function openPage($id,$title,$pg) {
	global $dblink,$userid,$page;
	
	$id = intval($id);
	$ipg = $pg - 1;
	
	$row = $dblink->get_row("SELECT * FROM memht_mypage WHERE id=$id OR title='$title'");
	$enabled = intval($row['enabled']);
	$rank = intval($row['rank']);
	if ($row AND ($enabled==1 OR isAuth($userid,3))) {
		if (myRank()>=$rank) {
			$title = outCode($row['title']);
			$text = outCode($row['text']);
			
			$dblink->query("UPDATE memht_mypage SET hits=hits+1 WHERE id=$id OR title='$title'");
			
			$text = explode("[-pagebreak-]",$text);
			$mytext = trim($text[$ipg]);
			
			openTable();
				echo $mytext;
			closeTable();
			
			if (sizeof($text)>1) {
				openTable();
					//Pages
					include_once("inc/class/paginationSystem.class.php");
					$ps = new paginationSystem();
					$ps->items = 1;
					$ps->actpg = $pg;
					$ps->override_query = true;
					$ps->tot_items = sizeof($text);
					$ps->url = "index.php?page=mypage&op=openPage&id=$id&title=".mem_urlencode($title)."&pg={{N}}";
					$ps->show();
				closeTable();
			}
		} else {
			openTable();
				echo "<div align='center' id='errorText'><b>"._ACCESSDENIED_."</b></div>";
				if ($rank==1) { echo "<div align='center' class='box'>"._YOUHAVENOPERM_." "._TOACCESSTHISPAGE_."!<br>"._DOTHE_." <a href='index.php?page=users' title='"._LOGIN_."'><b>"._LOGIN_."</b></a> "._OR_." <a href='index.php?page=users&op=register' title='"._REGISTER_."'><b>"._REGISTER_."</b></a> "._FORFREE_."</div>"; }
			closeTable();
		}
	} else {
		openTable();
			echo "<div align='center' id='errorText'><b>"._PAGE_NOEXIST_ORINACTIVE_."</b></div>";
		closeTable();
	}
}

?>