<?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 (stristr(htmlentities($_SERVER['PHP_SELF']), "inc_functions.php")) {
	Header("Location: ../../index.php");
	die();
}

function isAuthorized($forumid,$operation) {
	global $dblink,$userid,$userInfo;

	$forumid = intval($forumid);
	
	$row = $dblink->get_row("SELECT auth_view,auth_read,auth_write,auth_delete FROM memht_forum_forums WHERE id=$forumid");
	$auth_view = intval($row['auth_view']);
	$auth_read = intval($row['auth_read']);
	$auth_write = intval($row['auth_write']);
	$auth_delete = intval($row['auth_delete']);
	
	$myrank = 0;
	if (isUser($userid)) { $myrank = 1; }
	if (isModerator($forumid,$userid)) { $myrank = 2; }
	if (isAuth($userid,3)) { $myrank = 3; }
	
	switch($operation) {
		case "view":
			if ($auth_view==0 OR $myrank>=$auth_view) { return true; } else { return false; }
		break;
		case "read":
			if ($auth_read==0 OR $myrank>=$auth_read) { return true; } else { return false; }
		break;
		case "write":
			if ($auth_write==0 OR $myrank>=$auth_write) { return true; } else { return false; }
		break;
		case "delete":
			if ($auth_delete==0 OR $myrank>=$auth_delete) { return true; } else { return false; }
		break;
	}
}

function getNumbers($id) {
	global $dblink;
	
	$row = $dblink->get_row("SELECT COUNT(id) AS replies, (SELECT hits FROM memht_forum_posts WHERE id=$id) AS views FROM memht_forum_posts WHERE parent=$id");
	return array(intval($row['replies']),intval($row['views']));
}

function getThreadsNumber($forumid) {
	global $dblink;
	$forumid = intval($forumid);
	
	return $dblink->get_num("SELECT id FROM memht_forum_posts WHERE forum='$forumid' AND parent=0 AND flag!=2");
}

function getPostsNumber($forumid) {
	global $dblink;
	$forumid = intval($forumid);
	
	return $dblink->get_num("SELECT id FROM memht_forum_posts WHERE forum='$forumid' AND flag!=2");
}

function getRepliesNumber($threadid) {
	global $dblink;
	$threadid = intval($threadid);
	
	return $dblink->get_num("SELECT id FROM memht_forum_posts WHERE parent='$threadid'");
}

function getViewsNumber($threadid) {
	global $dblink;
	$threadid = intval($threadid);
	
	$row = $dblink->get_row("SELECT hits FROM memht_forum_posts WHERE id='$threadid'");
	return intval($row['hits']);
}

function getTitle($post) {
	global $dblink;
	$post = intval($post);
	
	$titlerow = $dblink->get_row("SELECT title FROM memht_forum_titles WHERE $post>=min AND $post<max");
	return outCode($titlerow['title']);
	
}

function getLastPostInfoByForum($forumid) {
	global $dblink,$siteConfig;

	$forumid = intval($forumid);
	
	if ($lpirow = $dblink->get_row("SELECT id,parent,name,author,flag,DATE_FORMAT(date, '".$siteConfig['timestamp']."') as date FROM memht_forum_posts WHERE forum='$forumid' ORDER BY id DESC LIMIT 1")) {
		$postid = intval($lpirow['id']);
		$parentid = intval($lpirow['parent']);
		$postname = outCode($lpirow['name'],0);
		$postauthor = outCode($lpirow['author']);
		$postdate = $lpirow['date'];
		$flag = intval($lpirow['flag']);
		if ($flag==2) { return "-"; }
		
		if ($parentid!=0) {
			$ltirow = $dblink->get_row("SELECT id,name FROM memht_forum_posts WHERE id='$parentid'");
			$threadid = intval($ltirow['id']);
			$threadname = outCode($ltirow['name'],0);
			$shortthreadname = trimString($threadname,18,1);
			return "<a href='index.php?page=forum&op=viewThread&id=$threadid&title=".mem_urlencode($threadname)."#post$postid' title='$threadname'>$shortthreadname<br><b>$postauthor</b><br>$postdate</a>";
		} else {
			$threadid = $postid;
			$threadname = $postname;
			$shortthreadname = trimString($threadname,18,1);
			return "<a href='index.php?page=forum&op=viewThread&id=$threadid&title=".mem_urlencode($threadname)."' title='$threadname'>$shortthreadname<br><b>$postauthor</b><br>$postdate</a>";
		}
	} else {
		return "-";
	}
}

function getLastPostInfoByPost($threadid) {
	global $dblink,$siteConfig,$admin;

	$threadid = intval($threadid);
	
	$lpres = "SELECT id,name,author,DATE_FORMAT(date, '".$siteConfig['timestamp']."') as date FROM memht_forum_posts WHERE parent='$threadid' ORDER BY id DESC LIMIT 1";
	if ($dblink->get_num($lpres)>0) {
		$lpirow = $dblink->get_row($lpres);
		$postid = intval($lpirow['id']);
		$name = outCode($lpirow['name'],0);
		$author = outCode($lpirow['author']);
		$date = $lpirow['date'];
		
		$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 = ""; }
		
		return "<a href='index.php?page=forum&op=viewThread&id=$threadid&title=".mem_urlencode($name)."$plus#post$postid' title='$name'><b>$author</b><br>$date</a>";
	} else {
		$lpirow = $dblink->get_row("SELECT name,author,DATE_FORMAT(date, '".$siteConfig['timestamp']."') as date FROM memht_forum_posts WHERE id='$threadid'");
		$name = outCode($lpirow['name'],0);
		$author = outCode($lpirow['author']);
		$date = $lpirow['date'];
		
		return "<a href='index.php?page=forum&op=viewThread&id=$threadid&title=".mem_urlencode($name)."' title='$name'><b>$author</b><br>$date</a>";
	}
}

function getTitleByCategory($catid) {
	global $dblink;
	$catid = intval($catid);
	
	$row_c = $dblink->get_row("SELECT name FROM memht_forum_categories WHERE id='$catid'");
	$c_name = outCode($row_c['name']);
	
	return "<a href='index.php?page=forum' title='"._MAIN_."'>"._MAIN_."</a> > <a href='index.php?page=forum&op=viewCategory&id=$catid&title=".mem_urlencode($c_name)."' title='$c_name'>$c_name</a>";
}

function getTitleByForum($forumid) {
	global $dblink;
	$forumid = intval($forumid);
	
	$row = $dblink->get_row("SELECT parent,category,name FROM memht_forum_forums WHERE id=$forumid");
	$parent = intval($row['parent']);
	$cat_id = intval($row['category']);
	$forumname = outCode($row['name'],0);
	
	if ($parent>0) {
		$row = $dblink->get_row("SELECT name FROM memht_forum_forums WHERE id=$parent");
		$parent_forumname = outCode($row['name']);
	}
	
	$row_c = $dblink->get_row("SELECT id,name FROM memht_forum_categories WHERE id=$cat_id");
	$c_id = intval($row_c['id']);
	$c_name = outCode($row_c['name']);
	
	$link = "<a href='index.php?page=forum' title='"._HOME_."'>"._HOME_."</a> > <a href='index.php?page=forum&op=viewCategory&id=$c_id&title=".mem_urlencode($c_name)."' title='$c_name'>$c_name</a> > ";
	if ($parent>0) {
		$link .= "<a href='index.php?page=forum&op=viewForum&id=$parent&title=".mem_urlencode($parent_forumname)."' title='$parent_forumname'>$parent_forumname</a> > ";
	}
	$link .= "<a href='index.php?page=forum&op=viewForum&id=$forumid&title=".mem_urlencode($forumname)."' title='$forumname'>$forumname</a>";
	return $link;
}

function getTitleByThread($threadid) {
	global $dblink;
	$threadid = intval($threadid);
	
	$row = $dblink->get_row("SELECT forum,name FROM memht_forum_posts WHERE id='$threadid'");
	$forumid = intval($row['forum']);
	$thname = outCode($row['name'],0);
	
	$row_f = $dblink->get_row("SELECT parent,category,name FROM memht_forum_forums WHERE id=$forumid");
	$parent = intval($row_f['parent']);
	$cat_id = intval($row_f['category']);
	$forumname = outCode($row_f['name'],0);
	
	if ($parent>0) {
		$row = $dblink->get_row("SELECT name FROM memht_forum_forums WHERE id=$parent");
		$parent_forumname = outCode($row['name']);
	}
	
	$row_c = $dblink->get_row("SELECT id,name FROM memht_forum_categories WHERE id='$cat_id'");
	$c_id = intval($row_c['id']);
	$c_name = outCode($row_c['name'],0);
	
	$link = "<a href='index.php?page=forum' title='"._HOME_."'>"._HOME_."</a> > <a href='index.php?page=forum&op=viewCategory&id=$c_id&title=".mem_urlencode($c_name)."' title='$c_name'>$c_name</a> > ";
	if ($parent>0) {
		$link .= "<a href='index.php?page=forum&op=viewForum&id=$parent&title=".mem_urlencode($parent_forumname)."' title='$parent_forumname'>$parent_forumname</a> > ";
	}
	$link .= "<a href='index.php?page=forum&op=viewForum&id=$forumid&title=".mem_urlencode($forumname)."' title='$forumname'>$forumname</a> > <a href='index.php?page=forum&op=viewThread&id=$threadid&title=".mem_urlencode($thname)."' title='$thname'>$thname</a>";
	
	return $link;
}

function getStatus($id,$where) {
	global $dblink;
	$id = intval($id);

	switch($where) {
		case "forum":
			$row_forum = $dblink->get_row("SELECT status FROM memht_forum_forums WHERE id='$id'");
			return intval($row_forum['status']);
		break;
		case "thread":
			$row_thread = $dblink->get_row("SELECT status FROM memht_forum_posts WHERE id='$id'");
			return intval($row_thread['status']);
		break;		
	}
}

function getIcon($id,$where) {
	global $dblink,$siteConfig;

	$id = intval($id);
	
	switch($where) {
		case "forum":
			$row_forum = $dblink->get_row("SELECT status FROM memht_forum_forums WHERE id='$id'");
			$forum_status = intval($row_forum['status']);
			if ($forum_status>0) {
				if (newPostsCheckFromCookie($id,"forum")) {
					return "<img src='templates/".$siteConfig['template']."/images/forum/forum_new.gif' border='0' alt='New'>";
				} else {
					return "<img src='templates/".$siteConfig['template']."/images/forum/forum_old.gif' border='0' alt='Old'>";
				}
			} else {
				return "<img src='templates/".$siteConfig['template']."/images/forum/forum_locked.gif' border='0' alt='Locked'>";
			}
		break;
		case "thread":
			$row_thread = $dblink->get_row("SELECT status,flag FROM memht_forum_posts WHERE id='$id'");
			$thread_status = intval($row_thread['status']);
			$thread_flag = intval($row_thread['flag']);
			if ($thread_status>0) {
				if (newPostsCheckFromCookie($id,"thread")) {
					if ($thread_flag==1) {
						return "<img src='templates/".$siteConfig['template']."/images/forum/forum_new_sticky.gif' border='0' alt='New sticky'>";
					} else {
						return "<img src='templates/".$siteConfig['template']."/images/forum/forum_new.gif' border='0' alt='New'>";
					}
				} else {
					if ($thread_flag==1) {
						return "<img src='templates/".$siteConfig['template']."/images/forum/forum_old_sticky.gif' border='0' alt='Old sticky'>";
					} else {
						return "<img src='templates/".$siteConfig['template']."/images/forum/forum_old.gif' border='0' alt='Old'>";
					}
				}
			} else {
				return "<img src='templates/".$siteConfig['template']."/images/forum/forum_locked.gif' border='0' alt='Locked'>";
			}
		break;		
	}
}

function isModerator($forumid,$uid) {
	global $dblink;
	
	return ($dblink->get_num("SELECT g.id FROM memht_forum_moderation AS m JOIN memht_groups AS g JOIN memht_groups_members AS u ON m.groupid=g.id AND m.groupid=u.groupid AND u.user=$uid WHERE m.forum=$forumid")>0) ? true : false ;
}

function setLastPostCookie() {
	global $dblink;
	
	if (!isset($_COOKIE['forumtrack'])) {
		$lastrow = $dblink->get_row("SELECT id FROM memht_forum_posts ORDER BY id DESC LIMIT 1");
		$lastid = intval($lastrow['id']);
		
		setcookie("forumtrack",$lastid,time()+86400); //24 hours
	}
}

function addVisitedPostCookie($lastp) {
	if (isset($_COOKIE['forumtrack'])) {
		$cookiecontent = inCode($_COOKIE['forumtrack']);
		$exp = explode("-",$cookiecontent);
		$first = $exp[0];
		if (!in_array($lastp,$exp) AND $lastp>$first) {
			$cookiecontent = implode("-",$exp);
			$cookiecontent .= "-".$lastp;
			setcookie("forumtrack",$cookiecontent,time()+86400); //24 hours
		}
	}
}

function newPostsCheckFromCookie($id,$where) {
	global $dblink,$admin;
	
	if (isset($_COOKIE['forumtrack'])) {
		$cookiecontent = inCode($_COOKIE['forumtrack']);
		$exp = explode("-",$cookiecontent);
		$first = $exp[0];
		
		switch($where) {
			//case "forum":break;
			case "thread":
				if ($row = $dblink->get_row("SELECT id FROM memht_forum_posts WHERE parent=$id ORDER BY id DESC LIMIT 1")) {
					$lastpost = intval($row['id']);
				} else {
					$lastpost = $id;
				}
				return ($lastpost>$first AND !in_array($lastpost,$exp)) ? true : false ;
			break;		
		}
	} else {
		return false;
	}
}

?>