Location: PHPKode > projects > Spam free PHP GuestBook > guestbook/gb/include/clsdatabase.php
<?php
/*
Copyright (c) 2008 http://ramui.com. All right reserved.
This product is protected by copyright and distributed under licenses restricting copying, distribution. Permission is granted to the public to download and use this script provided that this Notice and any statement of authorship are reproduced in every page on all copies of the script.
*/
class database extends connection
{
function __construct($root)
{
        parent::__construct($root);
        if(($this->site['blocksite']=='Y')&&(empty($this->admin))){include "gb/include/blocksite.php";exit;}
}
public function get_page_array()
{
        $id=@$_GET['pid'];
        $navigation='';
		if(fw_get_docroot()!='/'){$navigation='<a href="http://'.getenv('HTTP_HOST').'/">Home</a> ';}
		$navigation.=':: <a href="http://'.getenv('HTTP_HOST').fw_get_docroot().'index.php">'.($this->site['sitename']).'</a>';
		if(empty($id)||(!is_numeric($id))){$row=$this->get_index();}
        else{
			$row=$this->post($id);
			$navigation.=' :: <a href="http://'.getenv('HTTP_HOST').fw_get_docroot().'index.php?pid='.$id.'">'.$row['title'].'</a>';}
		$row['navigation']=$navigation;
		if(empty($row['description'])){$row['description']=$this->site['description'];}
		if(empty($row['keywords'])){$row['keywords']=$this->site['keywords'];}
		else{$row['keywords']=str_replace(' ',',',$row['keywords']);}
		if(empty($row['title'])){$row['title']=$this->site['sitename'];}
		if(!(@array_key_exists('totalpost',$row))){$row['totalpost']=$this->totalpost();}
        return $row;
}

private function post($id)
{
        $page=@$_GET['page'];
		if(empty($page)||!is_numeric($page)){
			$page=1;
			$query  = sprintf("SELECT tp.*, tu.user, tu.website, tu.allowemail FROM ".$this->prefix."post tp, ".$this->prefix."user tu WHERE tp.id = %d AND tp.publish = 'Y' AND tp.uid = tu.id",$id);
			$result= @mysql_query($query);
			$row= @mysql_fetch_array($result, MYSQL_ASSOC);
			if(empty($row)){
				$row['title']='Page not found!';
				$row['content']=false;}
			else{
				$autolink=($row['autolink']=='Y');
				$nofollow=($this->site['nofollow']=='Y');
				$bb= new bbcode($this->site['imagewidth'],$this->site['imageheight'],$nofollow,0);
				$content=$bb->format_text($row['content'],$autolink);
				unset($bb);
				$str='<table class="fw_item"><tr><td><h1>'.$row['title'].'<h1></td></tr><tr><td class="fw_author">Posted by: '.$row['user'].', '.date(DATE_ATOM, $row['date']).'</td></tr><tr><td>';
				$str.=$content.'</td></tr>';
				if(!empty($row['website'])){$str.='<tr><td class="fw_signature"><a target="_blank" '.(empty($this->nofollow)? '' : 'rel="nofollow" ').'href="'.$row['website'].'">'.$row['website'].'</a></td></tr>';}
				$str.='<tr><td><ul class="fw_link">';
				$path='gb/templates/'.$this->site['defaultstyle'].'/images/icon/';
				if(is_dir($path)){
					$l='<img title="Locked" src="'.$path.'lock.gif" />';
					$m='<img title="Contact" src="'.$path.'mail.gif" />';
					$mi='<img title="Disabled" src="'.$path.'mailinactive.gif" />';
					$r='<img title="Reply" src="'.$path.'reply.gif" />';
					$e='<img title="Edit" src="'.$path.'edit.gif" />';
				}
				else{$l='Locked';$m='Contact';$mi=$m;$r='Reply';$e='Edit';}
				$str.=(($row['locked']=='Y')? '<li class="inactive">'.$l.'</li>':'<li class="reply"><a href="gb/user/index.php?query=4M'.$row['id'].'">'.$r.'</a></li>').((($row['allowemail']=='Y')&&($this->site['allowemail']=='Y'))? '<li class="email"><a href="javascript:fwOpenMail('.$row['uid'].')">'.$m.'</a></li>' : '<li class="inactive">'.$mi.'</li>');
				if($this->admin){$str.='<li class="edit"><a href="javascript:fwOpenEdit(\'2M1M'.$row['id'].'\')">'.$e.'</a></li>';}
				$str.='</ul></td></tr></table>';
				$row['content']=$str;}}
		else{
			$query=sprintf("SELECT title FROM ".$this->prefix."post WHERE id = %d AND publish = 'Y'",$id);
			$result= @mysql_query($query);
			$row= @mysql_fetch_array($result, MYSQL_ASSOC);
		}
        $comments=$this->get_comments($id,$page);
		if(!empty($comments)){
			$row['content'].=$comments['content'];
			$row['pagelink']=$comments['pagelink'];}
		return $row;
}

private function get_comments($id,$page)
{
		$query=sprintf("SELECT id FROM ".$this->prefix."comments WHERE pid = %d AND publish = 'Y'",$id);
		$result= @mysql_query($query);
		$row = @mysql_fetch_array($result, MYSQL_ASSOC);
		$num_rows=@mysql_num_rows($result);
		if(empty($num_rows)){return false;}
		$start_count=($page-1)*$this->site['listcount'];
		$totalpage=fw_total_page($num_rows, $this->site['listcount']);
		$query=sprintf("SELECT tc.*, tu.user, tu.website, tu.allowemail FROM ".$this->prefix."comments tc, ".$this->prefix."user tu WHERE tc.pid = %d AND tc.publish = 'Y' AND tc.uid = tu.id ORDER BY tc.id Asc LIMIT ".$start_count.", ".$this->site['listcount'],$id);
		$result= @mysql_query($query);
        $str='';
		$nofollow=($this->site['nofollow']=='Y');
		$bb= new bbcode($this->site['imagewidth'],$this->site['imageheight'],$nofollow,0);
		$path='gb/templates/'.$this->site['defaultstyle'].'/images/icon/';
        while($row = @mysql_fetch_array($result, MYSQL_ASSOC)){
			$autolink=($row['autolink']=='Y');
			$content=$bb->format_text($row['comment'],$autolink);
			$str.='<table class="fw_commentitem"><tr><td><a name="'.$row['id'].'"></a><h2>'.$row['title'].'</h2></td></tr>';
			$str.='<tr><td class="fw_author">Posted by: '.$row['user'].', '.date(DATE_ATOM, $row['date']).'</td></tr>';
			$str.='<tr><td>'.$content.'</td></tr>';
			if(!empty($row['website'])){$str.='<tr><td class="fw_signature"><a target="_blank" '.(empty($this->nofollow)? '' : 'rel="nofollow" ').'href="'.$row['website'].'">'.$row['website'].'</a></td></tr>';}
			$str.='<tr><td><ul class="fw_link">';
			if(is_dir($path)){
				$m='<img title="Contact" src="'.$path.'mail.gif" />';
				$mi='<img title="Disabled" src="'.$path.'mailinactive.gif" />';
				$e='<img title="Edit" src="'.$path.'edit.gif" />';
			}
			else{$m='Contact';$mi=$m;$e='Edit';}
			$str.=((($row['allowemail']=='Y')&&($this->site['allowemail']=='Y'))? '<li class="email"><a href="javascript:fwOpenMail('.$row['uid'].')">'.$m.'</a></li>' : '<li class="inactive">'.$mi.'</li>');
			if($this->admin){$str.='<li class="edit"><a href="javascript:fwOpenEdit(\'9M1M'.$row['id'].'\')">'.$e.'</a></li>';}
			$str.='</ul></td></tr></table>';}
		$pagelink='<div id="fw_pageno"><b>No. of pages:&nbsp;'.$totalpage.'</b>&nbsp;&nbsp;&nbsp;';
		$start=(($page>10)? $page-10 : 1);
		$end=(($totalpage<=21)? $totalpage : $start+20);
		$pagelink.=(($page>1)? '<a href="index.php'.(($page>2)? '?page='.($page-1) : '').'">Back</a>&nbsp;' : '<span>Back</span>&nbsp;');
		for($p=$start; $p<=$end; $p++){
			if($p==$page){$pagelink.='<span>['.$p.']</span>&nbsp;';}
			else{$pagelink.='[<a href="index.php?pid='.$id.(($p>1)? '&page='.$p : '').'">'.$p.'</a>]&nbsp;';}
		}
		$pagelink.=(($page<$totalpage)? '<a href="index.php?pid='.$id.'&page='.($page+1).'">Next</a>&nbsp;' : '<span>Next</span>&nbsp;');
		$pagelink.='</div>';
		$arr=array();
		$arr['content']=$str;
		$arr['pagelink']=$pagelink;
        return $arr;
}

private function get_index()
{
		$page=@$_GET['page'];
		if(empty($page)||!is_numeric($page)){$page=1;}
		$num_rows=$this->totalpost();
		$start_count=($page-1)*($this->site['listcount']);
		$totalpage=fw_total_page($num_rows, ($this->site['listcount']));
		$query="SELECT tp.*, tu.user, tu.website, tu.allowemail, IF(ISNULL(tc.pid),0,COUNT(tc.pid)) AS c FROM ";
		$query.=$this->prefix."post tp LEFT JOIN ".$this->prefix."user tu ON tp.uid = tu.id LEFT JOIN ".$this->prefix."comments tc ";
		$query.="ON (tp.id=tc.pid AND tc.publish='Y') WHERE ";
		$query.="tp.publish = 'Y' GROUP BY tp.id ORDER BY ";
		$query.="tp.id Desc LIMIT ".$start_count.", ".$this->site['listcount'];
        $result= @mysql_query($query);
		$nofollow=($this->site['nofollow']=='Y');
		$bb= new bbcode($this->site['imagewidth'],$this->site['imageheight'],$nofollow,0);
		$path='gb/templates/'.$this->site['defaultstyle'].'/images/icon/';
        while($row = @mysql_fetch_array($result, MYSQL_ASSOC)){
			$autolink=($row['autolink']=='Y');
			$content=$bb->format_text($row['content'],$autolink);
			$str.='<table class="fw_item"><tr><td><h2><a href="index.php?pid='.$row['id'].'">'.$row['title'].'</a></h2></td></tr>';
			$str.='<tr><td class="fw_author">Posted by: '.$row['user'].', '.date(DATE_ATOM, $row['date']).'</td></tr><tr><td>';
			$str.=$content.'</td></tr>';
			if(!empty($row['website'])){$str.='<tr><td class="fw_signature"><a target="_blank" '.(empty($this->nofollow)? '' : 'rel="nofollow" ').'href="'.$row['website'].'">'.$row['website'].'</a></td></tr>';}
			$str.='<tr><td><ul class="fw_link">';
			if(is_dir($path)){
				$l='<img title="Locked" src="'.$path.'lock.gif" />';
				$m='<img title="Contact" src="'.$path.'mail.gif" />';
				$mi='<img title="Disabled" src="'.$path.'mailinactive.gif" />';
				$r='<img title="Reply" src="'.$path.'reply.gif" />';
				$c='<img title="Comments('.$row['c'].')" src="'.$path.'open.gif" />';
			}
			else{$l='Locked';$m='Contact';$mi=$m;$r='Reply';$c='Comment('.$row['c'].')';}
			$str.=((($row['allowemail']=='Y')&&($this->site['allowemail']=='Y'))? '<li class="open"><a href="index.php?pid='.$row['id'].'">'.$c.'</a></li><li class="email"><a href="javascript:fwOpenMail('.$row['uid'].')">'.$m.'</a></li>' : '<li class="inactive">'.$mi.'</li>');
			$str.=(($row['locked']=='Y')? '<li class="locked">'.$l.'</li>':'<li class="reply"><a href="gb/user/index.php?query=4M'.$row['id'].'">'.$r.'</a></li>').'</ul></td></tr></table>';}
		$pagelink='<div id="fw_pageno"><b>No. of pages:&nbsp;'.$totalpage.'</b>&nbsp;&nbsp;&nbsp;';
		$start=(($page>10)? $page-10 : 1);
		$end=(($totalpage<=21)? $totalpage : $start+20);
		$pagelink.=(($page>1)? '<a href="index.php'.(($page>2)? '?page='.($page-1) : '').'">Back</a>&nbsp;' : '<span>Back</span>&nbsp;');
		for($p=$start; $p<=$end; $p++){
			if($p==$page){$pagelink.='<span>['.$p.']</span>&nbsp;';}
			else{$pagelink.='[<a href="index.php'.(($p>1)? '?page='.$p : '').'">'.$p.'</a>]&nbsp;';}
		}
		$pagelink.=(($page<$totalpage)? '<a href="index.php?page='.($page+1).'">Next</a>&nbsp;' : '<span>Next</span>&nbsp;');
		$pagelink.='</div>';
		$arr=array();
		$arr['content']=$str;
		$arr['totalpost']=$num_rows;
		$arr['pagelink']=$pagelink;
        return $arr;
}

private function totalpost()
{
		$query="SELECT id FROM ".$this->prefix."post WHERE publish = 'Y'";
		$result= @mysql_query($query);
		$row = @mysql_fetch_array($result, MYSQL_ASSOC);
		return @mysql_num_rows($result);
}
}
?>
Return current item: Spam free PHP GuestBook