<?php

/**
 * @package eggblog
 * @author eggblog.net, tenfourzero@users.sourceforge.net
 * @version $Id: global.php,v 4.0.0RC1 2007/12/03 10:26:12 tenfourzero Exp $
 * @copyright Copyright (c) 2005, http://www.eggblog.net
 * @license http://www.gnu.org/licenses/gpl.html GNU Lesser General Public License
 */


/**
 * return array of $n recent forum posts starting at record $i
 * @param $n int number of topic/posts to return
 * @param $i int record number to start from
 * @return array
 */
function eb_recentforum($n,$i) {
	$sql="SELECT p.topic_id,t.topic_title,p.post_body,p.post_date,u.user_name ".
	 "FROM eb_posts AS p ".
	 "LEFT JOIN eb_topics AS t ON t.topic_id=p.topic_id ".
	 "LEFT JOIN eb_users AS u ON u.user_id=p.author_id ".
	 "INNER JOIN ( ".
	 "SELECT topic_id,MAX(post_date) AS max_date ".
	 "FROM eb_posts ".
	 "GROUP BY topic_id ".
	 ") AS m ".
	 "ON m.topic_id=p.topic_id ".
	 "AND m.max_date=p.post_date ".
	 "ORDER BY p.post_date DESC ".
	 "LIMIT ".$i.",".$n;
	$query=mysql_query($sql);
	if(mysql_num_rows($query)>0) while($row=mysql_fetch_array($query)) $output[]=$row;
	else $output=array();
	return $output;
}


/**
 * return total number of forum topics
 * @return int
 */
function eb_num_topics() {
	$sql="SELECT COUNT(topic_id) FROM eb_topics";
	$query=mysql_query($sql);
	return mysql_result($query,0);
}


/**
 * print $n topics starting at topic number $i
 * @param $n int number of topic to return
 * @param $i int record number to start from
 * @return string
 */
function eb_forum($n,$i) {
	global $config,$lang;
	if($config['forums']==0) header("Location: index.php");

	$posts=eb_recentforum($n,($i-1)*10);
	$count=count($posts);
	$output ="\t\t\t<h3>".ucwords($lang['forums'])."</h3>\r\n";
	$output.="\t\t\t<p><a href=\"forum.php?newtopic\">".$lang['create_topic']."</a>.</p>\r\n";
	if($count>0) {
		foreach($posts as $post) {
			$output.="\t\t\t<p><a href=\"forum.php?topic=".$post['topic_id']."\" class=\"topictitle\">".$post['topic_title']."</a><br />".
			eb_formatsmilies(eb_textpreview(strip_tags($post['post_body']),140))."<br /><b>".$post['user_name']."</b> @ <b>".
			date("g:ia",$post['post_date']).", ".date("jS",$post['post_date'])." ".ucwords($lang['month'.date("n",$post['post_date'])])." ".date("Y",$post['post_date'])."</b></p>\r\n";
		}
		$output.=eb_pages($n,$i,eb_num_topics(),"forum.php?");
	}
	return $output;
}


/**
 * print topic id $id
 * @param $id int topic id to show
 * @return string
 */
function eb_topic($id) {
	global $lang;
	$output ="\t\t<h3>".eb_topic_title($id)."</h3>\r\n";
	if(!empty($_SESSION['user_id'])) {
		if(eb_checkadmin($_SESSION['user_id'])) $output.="\t\t<p><a href=\"admin.php?edittopic=".$id."\">".$lang['edit']."</a> | <a href=\"admin.php?deletetopic=".$id."\">".$lang['delete']."</a></p>\r\n";
	}
	$output.="\t\t<div class=\"topic\">\r\n";
	$sql="SELECT p.post_body,p.post_date,u.user_name,p.post_id FROM eb_posts AS p LEFT JOIN eb_users AS u ON u.user_id=p.author_id WHERE p.topic_id=".$id." ORDER BY p.post_date";
	$query=mysql_query($sql);
	while($row=mysql_fetch_array($query)) {
		$output.="\t\t\t<p class=\"comment\">".eb_formatsmilies(eb_linkable(str_replace("\r","",str_replace("\n","<br />",$row[0]))))."</p>\r\n";
		$output.="\t\t\t<p class=\"commentdetails\"><b>".htmlentities($row[2],ENT_QUOTES)."</b> - <b>".date("g:ia",$row[1]).", ".date("jS",$row[1])." ".ucwords($lang['month'.date("n",$row[1])])." ".date("Y",$row[1])."</b>";
		if(!empty($_SESSION['user_id'])) {
			if(eb_checkadmin($_SESSION['user_id'])) $output.=" <a href=\"admin.php?editpost=".$row[3]."&topic=".$id."\">".$lang['edit']."</a> | <a href=\"admin.php?deletepost=".$row[3]."&topic=".$id."\">".$lang['delete']."</a>";
		}
		$output.="</p>\r\n";
	}
	$output.="\t\t\t<hr />\r\n";
	$output.="\t\t\t<p><b>".$lang['submit_post']."</b></p>\r\n";
	if(!empty($_SESSION['user_id'])) {
		$output.="\t\t\t<a name=\"post\"></a>\r\n";
		$output.="\t\t\t<form name=\"post\" action=\"forum.php?topic=".$id."\" method=\"post\">\r\n";
		$output.="\t\t\t\t<script type=\"text/javascript\">function smilie(emote) { document.post.commtext.value += emote; document.post.commtext.focus(); }</script>\r\n";
		$output.="\t\t\t\t<ul id=\"smilies\">\r\n";
		foreach(eb_smilies() as $smilie) $output.="\t\t\t\t\t<li><a href=\"#post\" onclick=\"smilie(' [".$smilie."] '); return false;\"><img src=\"smilies/".$smilie.".gif\" alt=\"".$smilie."\" width=\"16\" height=\"16\" /></a></li>\n";
		$output.="\t\t\t\t</ul>\r\n";
		$output.="\t\t\t\t<textarea id=\"commtext\" name=\"text\"></textarea>\r\n";
		$output.="\t\t\t\t<p><input type=\"submit\" name=\"submit\" value=\"".ucwords($lang['save'])."\" /></p>\r\n";
		$output.="\t\t\t</form>\r\n";
	}
	else $output.="\t\t\t<p>".$lang['not_loggedin']."</p>\r\n";
	$output.="\t\t</div>\r\n";
	return $output;
}


/**
 * return array of smilies
 * @return array
 */
function eb_smilies() {
	return array(
		"angry",
		"cool",
		"cry",
		"dblwink",
		"grin",
		"ill",
		"innocent",
		"sad",
		"shock",
		"smile",
		"soso",
		"straight",
		"tongue",
		"wink"
	);
}


/**
 * return smilie formatted string
 * @param $input string text to be formatted
 * @return $string
 */
function eb_formatsmilies($input) {
	$smilies=eb_smilies();
	foreach($smilies as $smilie) $input=str_replace(" [".$smilie."] "," <img src=\"smilies/".$smilie.".gif\" alt=\"".$smilie."\" width=\"16\" height=\"16\" /> ",$input);
	return $input;
}


/**
 * return a topic title
 * @param int $id article id
 * @return string
 */
function eb_topic_title($id) {
	$sql="SELECT topic_title FROM eb_topics WHERE topic_id=".$id;
	$query=mysql_query($sql);
	return mysql_result($query,0);
}


/**
 * process topic post
 * @param int $id topc id
 * @param string $text html text
 */
function eb_topic_post($id,$text) {
	$sql="INSERT INTO eb_posts SET topic_id=".$id.",post_date=\"".time()."\",post_ip=\"".$_SERVER['REMOTE_ADDR']."\",author_id=".$_SESSION['user_id'].",post_body=\"".str_replace("&Acirc;","",$text)."\"";
	@mysql_query($sql);
	eb_forum_mail($id);
	header("Location: forum.php?topic=".$id);
}


/**
 * print form to create a new topic
 * @return string
 */
function eb_topic_new() {
	global $lang;
	$output="";
	if(!empty($_SESSION['user_id'])) {
		$output.="\t\t\t<form name=\"post\" action=\"forum.php?newtopic\" method=\"post\">\r\n";
		$output.="\t\t\t\t<p><b>".ucwords($lang['topic'])."</b>:<br /><input class=\"inputtext\" type=\"text\" name=\"title\" /></p>\r\n";
		$output.="\t\t\t\t<script type=\"text/javascript\">function smilie(emote) { document.post.commtext.value += emote; document.post.commtext.focus(); }</script>\r\n";
		$output.="\t\t\t\t<ul id=\"smilies\">\r\n";
		foreach(eb_smilies() as $smilie) $output.="\t\t\t\t\t<li><a href=\"#post\" onclick=\"smilie(' [".$smilie."] '); return false;\"><img src=\"smilies/".$smilie.".gif\" alt=\"".$smilie."\" width=\"16\" height=\"16\" /></a></li>\n";
		$output.="\t\t\t\t</ul>\r\n";
		$output.="\t\t\t\t<textarea id=\"commtext\" name=\"text\"></textarea>\r\n";
		$output.="\t\t\t\t<p><input type=\"submit\" name=\"submit\" value=\"".ucwords($lang["save"])."\" /></p>\r\n";
		$output.="\t\t\t</form>\r\n";
	}
	else $output.="\t\t\t<p>".$lang["not_loggedin"]."</p>\r\n";
	return $output;
}


/**
 * create new forum topic
 * @param string $title new topic title 
 * @param string $text new topic text
 */
function eb_topic_new_post($title,$text) {
	$sql="INSERT INTO eb_topics SET topic_title=\"".str_replace("&Acirc;","",$title)."\"";
	@mysql_query($sql);
	$id=mysql_insert_id();
	$sql="INSERT INTO eb_posts SET topic_id=".$id.",post_date=\"".time()."\",post_ip=\"".$_SERVER['REMOTE_ADDR']."\",author_id=".$_SESSION['user_id'].",post_body=\"".str_replace("&Acirc;","",$text)."\"";
	@mysql_query($sql);
	header("Location: forum.php?id=".$id);	
}


/**
 * email forum topic posters
 * @param int $topic topic id
 */
function eb_forum_mail($topic) {
	global $lang,$config;
	settype($topic,"integer");
	$sql="SELECT user_email,topic_title FROM eb_posts LEFT JOIN eb_users ON eb_posts.author_id=eb_users.user_id LEFT JOIN eb_topics ON eb_topics.topic_id=".$topic." WHERE author_id!=".$_SESSION['user_id']." AND eb_posts.topic_id=".$topic." GROUP BY user_email";
	$query=mysql_query($sql);
	while($row=mysql_fetch_array($query)) mail(htmlentities($row[0],ENT_QUOTES),htmlentities($row[1],ENT_QUOTES),$lang['topic_reply'].":\r\n".$config['uri']."forum.php?topic=".$topic,"From: ".htmlentities($config['title'],ENT_QUOTES)."<".$config['email'].">\r\n");
}

?>