<?php

/**
 * @package eggblog
 * @author eggblog.net, tenfourzero@users.sourceforge.net
 * @version $Id: v4.0RC3 2008/01/21 11:25:04 tenfourzero Exp $
 * @copyright Copyright (c) 2005, http://www.eggblog.net
 * @license http://www.gnu.org/licenses/gpl.html GNU Lesser General Public License
 */


/**
* print list of years-month article titles
* @param int $n number of articles to preview, 0 for all
* @return string
*/
function eb_articlelist($n) {
	global $lang;
	settype($n,"integer");
	$month=date("n");
	$year=date("Y");

	$count=0;
	$total=mysql_result(mysql_query("SELECT COUNT(article_id) FROM eb_articles WHERE article_flag!=0"),0);
	if($n>0 && $total>$n) $total=$n;
	if($n>0) $output="\t\t<div class=\"box\">\r\n\t\t\t<strong><a href=\"articles.php\">".ucwords($lang['articles'])."</a></strong>\r\n\t\t\t<ul>\r\n";
	else $output="\t\t<h3>".ucwords($lang['articles'])."</h3>\r\n\t\t<ul>\r\n";
	while($count<$total) {
		$first=mktime(0,0,0,$month,1,$year);
		$last=mktime(23,59,59,$month,31,$year);
		$limit=$total-$count;

		$query=mysql_query("SELECT article_id,article_title FROM eb_articles WHERE article_flag!=0 AND article_date>\"".$first."\" AND article_date<\"".$last."\" ORDER BY article_date DESC LIMIT ".$limit);
		$cnt=mysql_num_rows($query);
		$count+=$cnt;
		if ($cnt>0) {
			$print_m=ucwords($lang['month'.date("n",mktime(0,0,0,$month,1,$year))]);
			$print_y=date("Y",mktime(0,0,0,1,1,$year));
			$output.="\t\t\t\t<li><b>".$print_m." ".$print_y."</b>\r\n";
			$output.="\t\t\t\t\t<ul>\r\n";
			while($row=mysql_fetch_array($query)) {
				$output.="\t\t\t\t\t\t<li><a href=\"news.php?id=".$row[0]."\">".$row[1]."</a></li>\r\n";
			}
			$output.="\t\t\t\t\t</ul>\r\n\t\t\t\t</li>\r\n";
			unset($print_m,$print_y);
		}
		if($month==1) {
			$month=12;
			$year--;
		}
		else $month--;
	}
	if($n!=0)$output.="\t\t\t</ul>\r\n\t\t</div>\r\n";
	else $output.="\t\t</ul>\r\n";
	return $output;
}


/**
* return sticky articles as h3's
* @return string
*/
function eb_stickys() {
	if(!strpos($_SERVER['REQUEST_URI'],"install.php") && !strpos($_SERVER['REQUEST_URI'],"upgrade_3to4.php")) {
		$output="";
		$sql="SELECT article_id,article_title FROM eb_articles WHERE article_flag=2 ORDER BY article_title";
		$query=mysql_query($sql);
		if(mysql_num_rows($query)>0) {
			$output.="\t\t<ul id=\"stickys\">\r\n";
			while($row=mysql_fetch_array($query)) $output.="\t\t\t<li><a href=\"news.php?id=".$row['article_id']."\">".$row['article_title']."</a></li>\r\n";
			$output.="\t\t</ul>\r\n";
		}
	}
	else $output="";
	return $output;
}


/**
* return $n number of recent articles
* @param int $n number of articles to preview
* @return array
*/
function eb_articles($n) {
	settype($n,"integer");
	$output=array();
	$sql="SELECT article_id FROM eb_articles WHERE article_flag=1 ORDER BY article_date DESC LIMIT ".$n;
	$query=mysql_query($sql);
	while($row=mysql_fetch_array($query)) $output[]=$row['article_id'];
	return $output;
}


/**
* print an article
* @param int $comments 1 to show comments, 0 no comments
* @param array $id array of article_id's to print
*/
function eb_article($comments,$id) {
	global $config,$lang;
	settype($comments,"integer");
	$output="";
	$id=implode($id," OR article_id=");
	$sql="SELECT article_id,article_date,article_comments,article_title,article_body,user_name FROM eb_articles AS a LEFT JOIN eb_users AS u ON a.author_id=u.user_id WHERE article_id=".$id." ORDER BY article_date DESC";
	$query=mysql_query($sql);
	while($row=mysql_fetch_array($query)) {
		$output.="\t\t<div class=\"article\">\r\n";
		$output.="\t\t\t<h3><a href=\"news.php?id=".$row[0]."\">".$row[3]."</a></h3>\r\n";
		$day=date("w",$row[1]);
		$month=date("n",$row[1]);
		$output.="\t\t\t<p class=\"published\">".$lang['posted_by']." <b>".$row[5]."</b> @ <b>".date("g:ia",$row[1])."</b>, <b>".ucwords($lang['day'.date("w",$row[1])])." ".date("j",$row[1])." ".ucwords($lang['month'.$month])." ".date("Y",$row[1])."</b>.";
		if(!empty($_SESSION['user_id'])) {
			if (eb_checkadmin($_SESSION['user_id'])) $output.=" <a href=\"admin.php?editarticle=".$row[0]."\">".$lang['edit']."</a> | <a href=\"admin.php?deletearticle=".$row[0]."\">".$lang['delete']."</a>";
		}
		$output.="</p>\r\n";
		if(substr($row[4],0,1)=="&") $output.="\t\t\t".html_entity_decode($row[4])."\r\n";
		else $output.="\t\t\t<p>".str_replace("\n\n","<br />",str_replace("\n\n","</p>\r\n\t\t\t<p>",str_replace("\r","",$row[4])))."</p>\r\n";

		if($comments==1) {
			$output.="\t\t<ul id=\"bmarks\">\r\n";
			$output.="\t\t\t<li><a href=\"http://del.icio.us/post?url=".urlencode($config['uri']."news.php?id=".$id)."&title=".urlencode($row[3])."\"><img src=\"themes/".$config['theme']."/bmark_delicious.gif\" alt=\"Del.icio.us\" /></a></li>\r\n";
			$output.="\t\t\t<li><a href=\"http://digg.com/submit?phase=2&url=".urlencode($config['uri']."news.php?id=".$id)."\"><img src=\"themes/".$config['theme']."/bmark_digg.gif\" alt=\"Digg\" /></a></li>\r\n";
			$output.="\t\t\t<li><a href=\"http://technorati.com/cosmos/search.html?url=".urlencode($config['uri']."news.php?id=".$id)."\"><img src=\"themes/".$config['theme']."/bmark_technorati.gif\" alt=\"Technorati\" /></a></li>\r\n";
			$output.="\t\t\t<li><a href=\"http://blinklist.com/index.php?Action=Blink/addblink.php&Quick=true&Url=".urlencode($config['uri']."news.php?id=".$id)."&Title=".urlencode($row[3])."\"><img src=\"themes/".$config['theme']."/bmark_blinklist.gif\" alt=\"Blinklist\" /></a></li>\r\n";
			$output.="\t\t\t<li><a href=\"http://furl.net/storeIt.jsp?u=".urlencode($config['uri']."news.php?id=".$id)."&t=".urlencode($row[3])."\"><img src=\"themes/".$config['theme']."/bmark_furl.gif\" alt=\"Furl\" /></a></li>\r\n";
			$output.="\t\t\t<li><a href=\"http://reddit.com/submit?url=".urlencode($config['uri']."news.php?id=".$id)."&title=".urlencode($row[3])."\"><img src=\"themes/".$config['theme']."/bmark_reddit.gif\" alt=\"Reddit\" /></a></li>\r\n";
			$output.="\t\t</ul>\r\n";
		}

		// if showing comments and comments are allowed
		if(eb_checkcomments($row[0]) && $comments==1) {
			$output.="\t\t\t\t<div class=\"hr\"><hr /></div>\r\n";
			$output.="\t\t\t<a name=\"comments\"></a>\r\n";
			$output.="\t\t\t<div id=\"comments\">\r\n";
			$output.="\t\t\t\t<p><strong>".ucwords($lang['comments'])."</strong></p>\r\n";
			$sql="SELECT comment_body,comment_date,user_name,comment_id FROM eb_comments LEFT JOIN eb_users ON eb_users.user_id=eb_comments.author_id WHERE article_id=".$row[0]." ORDER BY comment_date";
			$query=mysql_query($sql);
			if(mysql_num_rows($query)>0) {
				while($comment=mysql_fetch_array($query)) {
					$output.="\t\t\t\t<p class=\"comment\">".str_replace("\r","",str_replace("\n","<br />",eb_linkable($comment[0])))."</p>\r\n";
					$output.="\t\t\t\t<p class=\"commentdetails\"><b>".htmlentities($comment[2],ENT_QUOTES)."</b> - <b>".date("g:ia",$comment[1])."</b>, <b>".ucwords($lang['day'.date("w",$comment[1])])." ".date("j",$comment[1])." ".ucwords($lang['month'.date("n",$comment[1])])." ".date("Y",$comment[1])."</b>.";
					// print admin edit/delete links if logged in as admin
					if(!empty($_SESSION['user_id'])) {
						if(eb_checkadmin($_SESSION['user_id'])) $output.=" <a href=\"admin.php?editcomment=".$comment[3]."&article=".$row[0]."\">".$lang['edit']."</a> | <a href=\"admin.php?deletecomment=".$comment[3]."&article=".$row[0]."\">".$lang['delete']."</a>";
					}
					$output.="</p>\r\n";
				}
			}
			$output.="\t\t\t\t<p><strong>".$lang['comment_post']."</strong></p>\r\n";
			// print comment form if logged in
			if(!empty($_SESSION['user_id'])) {
				$output.="\t\t\t\t<form action=\"news.php?id=".$id."\" method=\"post\">\r\n";
				$output.="\t\t\t\t\t<textarea id=\"commtext\" name=\"comment\"></textarea>\r\n";
				$output.="\t\t\t\t\t<p><input type=\"submit\" name=\"submit\" value=\"".ucwords($lang['save'])."\" /></p>\r\n";
				$output.="\t\t\t\t</form>\r\n";
			}
			else $output.="\t\t\t\t<p>".$lang['not_loggedin'].".</p>\r\n";
			$output.="\t\t\t</div>\r\n";
		}
		elseif(eb_checkcomments($row[0])) {
			$sql="SELECT COUNT(comment_id) FROM eb_comments WHERE article_id=".$row[0];
			$count=mysql_result(mysql_query($sql),0);
			$output.="\t\t\t<p><a href=\"news.php?id=".$row[0]."#comments\">".$lang['comments']." (".$count.")</a></p>\r\n";
		}
		$output.="\t\t</div>\r\n";
	}
	return $output;
}



/**
* return an article title
* @param int $id article id
* @return string
*/
function eb_article_title($id) {
	settype($id,"integer");
	$sql="SELECT article_title FROM eb_articles WHERE article_id=".$id;
	$query=mysql_query($sql);
	return mysql_result($query,0);
}


/**
* check if comments are allowed for an article
* @param int $id article id
* @return boolean
*/
function eb_checkcomments($id) {
	$sql="SELECT article_comments FROM eb_articles WHERE article_id=".$id;
	$query=mysql_query($sql);
	if(mysql_result($query,0)==1) return true;
	else return false;
}


/**
* submit comment to an article
* @param int $id article id
* @param string $text comment text, HTML encoded
* @return boolean
*/
function eb_comments_post($id,$text) {
	eb_pre();
	if(!eb_checkcomments($id) && empty($_SESSION['user_id'])) header("Location: news.php?id=".$id);
	$sql="INSERT INTO eb_comments SET author_id=".$_SESSION['user_id'].",article_id=".$id.",comment_ip=\"".$_SERVER['REMOTE_ADDR']."\",comment_date=\"".time()."\",comment_body=\"".str_replace("&Acirc;","",$text)."\"";
	@mysql_query($sql);
	header("Location: news.php?id=".$id);
}


/**
 * print a list of article tags
 * @param $n int number of tags to return
 */
function eb_tags($n) {
	global $lang;
	$sql="SELECT tag_name,COUNT(eb_tags_links.article_id) as cnt FROM eb_tags_links LEFT JOIN eb_tags ON eb_tags.tag_id=eb_tags_links.tag_id LEFT JOIN eb_articles ON eb_articles.article_id=eb_tags_links.article_id WHERE article_flag!=0 GROUP BY eb_tags.tag_id ORDER BY cnt";
	$query=mysql_query($sql);
	if(mysql_num_rows($query)>0) {
		$total=mysql_result(mysql_query("SELECT COUNT(article_id) FROM eb_tags_links"),0);
		while($row=mysql_fetch_array($query)) $array[]=array($row[0],$row[1]);
		asort($array);
	}
	else $array=array();

	if($n==0) echo "\t\t<h3>".ucwords($lang['tags'])."</h3>\r\n";
	echo "\t\t\t<ul id=\"tags\">\r\n";
	foreach($array as $tag) {
		$perc=($tag[1]/$total)*160;
		if($perc>26) $perc=26;
		if($perc<9) $perc=9;
		settype($perc,"integer");
		echo "\t\t\t\t<li><a href=\"tags.php?tag=".$tag[0]."\" style=\"font-size:".$perc."px;\">".$tag[0]."</a></li>\r\n";
	}
	echo "\t\t\t</ul>\r\n";
}


/**
 * print a list of article with a given tag
 * @param $tag varchar tag name
 */
function eb_tag($tag) {
	global $lang;
	echo "\t\t<h3>".ucwords($lang['tags'])." - ".$tag."</h3>\r\n";
	echo "\t\t<ul>\r\n";
	$sql="SELECT eb_articles.article_id,article_title FROM eb_articles LEFT JOIN eb_tags_links ON eb_articles.article_id=eb_tags_links.article_id LEFT JOIN eb_tags ON eb_tags.tag_id=eb_tags_links.tag_id WHERE tag_name=\"".$tag."\" AND article_flag!=0 ORDER BY article_title";
	$query=mysql_query($sql);
	while($row=mysql_fetch_array($query)) {
		echo "\t\t\t<li><a href=\"news.php?id=".$row[0]."\">".$row[1]."</a></li>\r\n";
	}
	echo "\t\t</ul>\r\n";
}

?>