Location: PHPKode > projects > SWG MiniBlog > sub/classes.php
<?php
include_once("Textile.php");
class blog
{
	function addBlogEntry($subject, $entry, $tags)
	{		
		global $mysql_blog_table;
		$sql_query = "insert into ${mysql_blog_table} (subject, entry, tags) values ('$subject','$entry','$tags')";		
		$sql_reply = mysql_query($sql_query);
	}
	
	function addBlogComment($user, $comment, $postid)
	{		
		global $mysql_comments_table, $lang;
		if ($user=="") $user = $lang['anonymous'];
		$sql_query = "insert into ${mysql_comments_table} (username, comment, post_id) values ('$user','$comment','$postid')";	
		$sql_reply = mysql_query($sql_query);
	}
	
	function changeBlogEntry($id, $subject, $entry, $tags)
	{
		global $mysql_blog_table;
		$sql_query = "update ${mysql_blog_table} set subject = '${subject}', entry = '${entry}', tags= '${tags}' where id = ${id}";
		$sql_reply = mysql_query($sql_query);
	}
	
	function getEntryById($id)
	{
		global $mysql_blog_table;
		$sql_query = "select entry from ${mysql_blog_table} where id = ${id}";
		$sql_reply = mysql_query($sql_query);
		$row = mysql_fetch_object($sql_reply);
		$entry = $row->entry;
		mysql_free_result($sql_reply);
		return $entry;
	}
	
	function getSubjectById($id)
	{
		global $mysql_blog_table;
		$sql_query = "select subject from ${mysql_blog_table} where id = ${id}";
		$sql_reply = mysql_query($sql_query);
		$row = mysql_fetch_object($sql_reply);
		$entry = $row->subject;
		mysql_free_result($sql_reply);
		return $entry;
	}
	
	function getPostIdByCommentId($cid)
	{
		global $mysql_comments_table;
		$sql_query = "select post_id from {$mysql_comments_table} where id={$cid}";
		$sql_result = mysql_query($sql_query);
		$row = mysql_fetch_object($sql_result);
		$pid = $row->post_id;
		mysql_free_result($sql_result);
		return $pid;
	}
	
	function getTagsById($id)
	{
		global $mysql_blog_table;
		$sql_query = "select tags from ${mysql_blog_table} where id = ${id}";
		$sql_reply = mysql_query($sql_query);
		$row = mysql_fetch_object($sql_reply);
		$entry = $row->tags;
		mysql_free_result($sql_reply);
		return $entry;
	}
	
	
	function getCommentForm($id)
	{
		$out = html::commentTextarea("newcomment",$id);
		return html::divId($out, "InputBox");
	}
	
	function getBlogEntries($limit,$tag)
	{
		global $mysql_blog_table, $mysql_comments_table, $lang, $page_self, $tag, $pagination, $comments;
		/*die("Pro seite..: {$pagination}");*/
		$sql_query = "select id, time, subject, entry, tags from {$mysql_blog_table} ";
		if ($tag!="")
		{
			$sql_query .= "where tags LIKE '% $tag %' OR tags LIKE '$tag %' OR tags LIKE '% $tag' OR tags = '$tag' ";
			$backbutton = html::href($lang['back'], "{$page_self}");
		}
		$sql_query .= "order by id desc ";
		if ($limit>0)
		{
			 $sql_query .= "limit {$limit}";
		}
		$sql_reply = mysql_query($sql_query);
		while ($row = mysql_fetch_object($sql_reply))
		{
			$entry = $row->entry;
			$textile = new Textile;
			$html = $textile->process($entry);
			$blogitemcontents = html::divClass($html, "BlogItemContent");
			$time = html::spanClass($row->time, "TimeStamp");
			$subject = html::spanClass($row->subject, "Subject");
			$entryid = $row->id;
			$dellink = html::href($lang['del'], "{$page_self}?action=delblog&id=${entryid}");
			$editlink = html::href($lang['edit'], "{$page_self}?action=enterchange&id=${entryid}");
			$newcommlink = html::href($lang['newcomm'], "{$page_self}?action=entercomment&id=${entryid}");
			$query3 = "select count(comment) from {$mysql_comments_table} where post_id={$entryid}";
			$q3result = mysql_query($query3);
			$commentcount = mysql_result($q3result,0);
			$showcommlink = html::href($lang['showcomm']." ({$commentcount})", "{$page_self}?action=showcomments&id=${entryid}");
			mysql_free_result($q3result);
			$barcontents = html::divClass("${newcommlink} ${showcommlink}" ,"CommentButtons");
			if ($_SESSION['loggedin'])
			{				
				$barcontents .= html::divClass("${dellink} ${editlink}" ,"EditButtons");
			}
			$barcontents .= "{$subject}<br />written on: {$time}<br />filed under: {$row->tags}";
			$blogitembar = html::divClass($barcontents ,"BlogItemBar");	
			if ($comments == $entryid)
			{
				$sql_query = "select id, username, comment from {$mysql_comments_table} where post_id={$entryid}";
				$sql_reply2 = mysql_query($sql_query);
				while ($row2 = mysql_fetch_object($sql_reply2))
				{
					if ($_SESSION['loggedin'])
					{
						$admincommentbuttons = html::divClass(html::href($lang['del'], "{$page_self}?action=delcomment&id={$row2->id}"),"AdminCommentButtons");
					}
					$html_comment = $textile->process($row2->comment);
					$comment_out .= html::divClass($admincommentbuttons."by: ".html::spanClass($row2->username,"Username")."<br />{$html_comment}","BlogComment");
				}
				mysql_free_result($sql_reply2);
			}
			else {$comment_out = "";}
			$forged .= html::divClass("{$blogitembar}{$blogitemcontents}{$comment_out}" ,"BlogItem");		
		}
		mysql_free_result($sql_reply);		
		return "$forged {$backbutton}";
	}
	
	function delBlogEntry($id)
	{
		global $mysql_blog_table, $mysql_comments_table;
		$sql_query = "delete from {$mysql_blog_table} where id = ${id}";
		$sql_reply = mysql_query($sql_query);
		$sql_query = "delete from {$mysql_comments_table} where post_id = ${id}";
		$sql_reply = mysql_query($sql_query);
	}
	
	function delComment($id)
	{
		global $mysql_comments_table;
		$sql_query = "delete from {$mysql_comments_table} where id = ${id}";
		$sql_reply = mysql_query($sql_query);
	}
	
	function getBlogInputBox($id)
	{
		if (isset($_SESSION['loggedin']))
		{
			if ($id != -1)
			{
				$out = html::makeTextarea("modblog",$this->getSubjectById($id),$this->getEntryById($id),$this->getTagsById($id),$id);
			}
			else
			{
				$out = html::makeTextarea("addblog","","","",0);
			}
		}
		return html::divId($out, "InputBox");
	}
	
	function array_unique_compact($a)
	{
	  $tmparr = array_unique($a);
	  $i=0;
	  foreach ($tmparr as $v) {
	    $newarr[$i] = $v;
	    $i++;
	  }
	  return $newarr;
	}
	
	function getTagCloud()
	{
		global $mysql_blog_table;
		$types = array();
		$sql_reply = mysql_query("select tags from {$mysql_blog_table}");
	  $forged = "Tags:<br />";
	  while($ftypes = mysql_fetch_array($sql_reply))
	  {
	    $types = array_merge($types, explode(" ", $ftypes[0]));
	  }

	  $types = $this->array_unique_compact($types);

	  for($i=0;$i<count($types);$i++)
	  {
	    $forged .= html::href($types[$i],"{$page_self}?action=showbytags&amp;t={$types[$i]}");
	  }
	  mysql_free_result($sql_reply);
		return html::DivId($forged,"TagCloud");
	}		
	
	function Buttons()
	{
		global $lang, $page_self, $pagination;
		if (isset($_SESSION['loggedin']))
		{
			$out = html::href($lang['logout'], "{$page_self}?action=logout");
		}
		else
		{
			if (!($_GET['action']=="login"))
			{
				$out = html::href($lang['login'], "{$page_self}?action=login");
			}
		}
		if ($pagination==0)
		{
			 $out .= html::href($lang['shortview'], "{$page_self}");
		}
		else
		{
			 $out .= html::href($lang['oldposts'], "{$page_self}?action=showall");
		}
		return html::divClass($out, "LogButtons");
	}
}

class sql
{
	static function connectDb($mysql_host, $mysql_user,$mysql_pass, $mysql_db)
	{
		mysql_connect($mysql_host, $mysql_user,$mysql_pass) or die("Keine Verbindung zum MySQL Server moeglich.");
	  mysql_select_db($mysql_db) or die("Die Datenbank existiert nicht.");
	}
}
class html
{
	static function header($page_title)
	{	
		return "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"de\">\n<script src=\"sub/focus_input.js\" type=\"text/javascript\"></script>\n<script src=\"sub/keysend.js\" type=\"text/javascript\"></script>\n<head>\n<title>{$page_title}</title>\n<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\" />\n<style type=\"text/css\">\hide@address.com url(\"css/layout.css\");\hide@address.com url(\"css/color.css\");\n</style>\n</head>\n<body onload=\"setfocusn(1);\">\n<h1>{$page_title}</h1>\n";		
	}	
	static function loginForm()
	{
		global $lang,$page_self;
		$loginform = <<<EOF
<form action="{$page_self}?action=login" method="post"><table border="0" cellpadding="0" cellspacing="4"><tr><td align="right">{$lang['password']}:</td><td><input name="password" type="password" size="12" maxlength="12" /></td></tr><tr><td align="right"></td><td><input type="submit" class="StyleButtons" value="{$lang['login']}" /></td></tr></table></form>
EOF;
		return html::divId($loginform, "LoginForm");	
	}
	static function makeTextarea($action, $subject ,$content, $tags, $arg)
	{
		global $lang,$page_self;
		return "<form action=\"{$page_self}?action=${action}&id=${arg}\" method=\"post\"><table>
		<tr><td>Subject:</td><td><input type=\"text\" name=\"subject\" size=\"66\" value=\"{$subject}\" /></td></tr>
		<tr><td>Entry:</td><td><textarea name=\"blogpost\" cols=\"76\" rows=\"13\" onKeyPress=\"checkSubmit(event,0)\">${content}</textarea></td></tr>
		<tr><td>Tags:</td><td><input type=\"text\" name=\"tags\" size=\"66\" value=\"{$tags}\" /></td></tr>		
		<tr><td></td><td><input type=\"submit\" value=\"{$lang['add']}\" /></td></tr>
		</table></form>";
	}
	static function commentTextarea($action, $arg)
	{
		global $lang;
		return "<form action=\"miniblog.php?action=${action}&id=${arg}\" method=\"post\"><table>
		<tr><td>Username:</td><td><input type=\"text\" name=\"username\" size=\"32\" /></td></tr>
		<tr><td>Entry:</td><td><textarea name=\"comment\" cols=\"76\" rows=\"13\" onKeyPress=\"checkSubmit(event,0)\"></textarea></td></tr>		
		<tr><td></td><td><input type=\"submit\" value=\"{$lang['add']}\" /></td></tr>
		</table></form>";
	}
	static function divClass($string, $class)
	{
		return "<div class=\"{$class}\">{$string}</div>\n";
	}
	static function href($string, $dest)
	{
		return "<a href=\"{$dest}\">{$string}</a>";
	}
	static function spanClass($string, $class)
	{
		return "<span class=\"{$class}\">{$string}</span>";
	}
	static function divId($string, $id)
	{
		return "<div id=\"{$id}\">{$string}</div>\n";
	}
	static function paragraph($string)
	{
		return "<p>{$string}</p>";
	}
}
class userManager
{
	static function logoutUser()
	{
		session_unset();
		session_destroy();
	}		
}
?>
Return current item: SWG MiniBlog