Location: PHPKode > scripts > Ramui web hosting directory script > Ramui web hosting directory script/gb/include/clsdatabase.php
<?php
/*
Copyright (c) 2011 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
{
private $uid;
function __construct($root)
{
        parent::__construct($root);
        if((($this->site['blocksite'])&&(empty($this->admin)))||($this->error)){
			$message=$this->error;
			include "gb/include/blocksite.php";exit;}
		if(isset($_SESSION['fw_login'])){
            $ip=$_SERVER['REMOTE_ADDR'];
            $login=$_SESSION['fw_login'];
            $query=sprintf("SELECT id FROM ".$this->prefix."user WHERE login = '$ip' AND session = '%s'",mysql_real_escape_string($login));
            $result=@mysql_query($query);
			$row =@mysql_fetch_array($result, MYSQL_ASSOC);
            if(!empty($row['id'])){$this->uid=$row['id'];}}
}

public function get_page_array()
{
        $p=$_POST['find'];
		$q=htmlentities($_GET['q']);
		$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($q))||(!empty($p))){$row=(empty($p))? $this->search_query($q) : $this->search();}
		else{
			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'];}
		if(empty($row['title'])){$row['title']=$this->site['sitename'];}
        return $row;
}
private function search_query($str)
{
		$arr=array();
		$str=trim($str);
		$l=strlen($str);
		$body='<div id="fw_searchresult"><h1>';
		if(($l>80)||($l<3)){$body.=(($l>80)? 'Too long':'Too short').' search query.</h1>';$arr['content']=$body.'</div>';return $arr;}
		$str_escape=mysql_real_escape_string($str);
		$query="SELECT id, title, description, pid, cid, MATCH (title,body,url,keywords,description) AGAINST ('%s' IN NATURAL LANGUAGE MODE) AS score ";
		$query.="FROM ".$this->prefix."search WHERE (MATCH (title,body,url,keywords,description) AGAINST ('%s' IN NATURAL LANGUAGE MODE))>.5 LIMIT 0,50";
		$query=sprintf($query,$str_escape,$str_escape);
		$result=@mysql_query($query);
		$num=@mysql_num_rows($result);
		$body.='Search: <span>'.$str.'</span></h1><p>Match found: '.$num.'</p></div>';
		$arr['title']='Search result: '.$str;
		if(!empty($num)){
			$body.='<ul id="fw_list">';
			while($row = @mysql_fetch_array($result, MYSQL_ASSOC)){
				$body.='<li><ul><li><h2><a href="index.php?pid='.$row['pid'].((empty($row['cid']))? '':'#'.$row['cid']).'">'.$row['title'].'</a></h2></li>';
				if(!empty($row['description'])){$body.='<li>'.$row['description'].'</li>';}
				$body.='</ul></li>';
			}
			$body.='</ul>';}
		$arr['content']=$body;
		$dt=time();
		$query=sprintf("UPDATE ".$this->prefix."searchkey SET hit = hit+1, date = $dt WHERE keywords = '%s'",strtolower($str_escape));
		@mysql_query($query);
		if(mysql_affected_rows()==0){@mysql_query(sprintf("INSERT INTO ".$this->prefix."searchkey(keywords, hit, date) VALUES('%s',1,$dt)",strtolower($str_escape)));}
		return $arr;
}
private function search()
{
		$arr=array();
		$query="SELECT id, title, description FROM ".$this->prefix."post WHERE ";
		$bw=trim(fw_strip_slashes($_POST["bw"]));
		$query.=($bw=='')? '':'bw >='.round($bw,2).' AND ';
		$usd=trim(fw_strip_slashes($_POST["usd"]));
		$query.=($usd=='')? '':'usd <='.round($usd,2).' AND ';
		$space=trim(fw_strip_slashes($_POST["space"]));
		$query.=($space=='')? '':'space >='.round($space,2).' AND ';
		$uptime=trim(fw_strip_slashes($_POST["uptime"]));
		$query.=($uptime=='')? '':'uptime >='.round($uptime,2).' AND ';
		$query.=empty($_POST["freedomain"])? '':'freedomain = 1 AND ';
		$server=round($_POST['server']);
		$query.=(empty($server))? '': "server = $server AND ";
		$nodomain=round(trim(fw_strip_slashes($_POST["noofdomain"])));
		$query.="noofdomain >= $nodomain ORDER BY view Desc LIMIT 0,50";
		$result=@mysql_query($query);
		$num=@mysql_num_rows($result);
		$body='<div id="fw_searchresult"><h1>Search: '.((empty($num))? "No match found" : "Match found: $num").'</h1>';
		$body.="Bandwidth: => <b>$bw</b>;&nbsp;&nbsp;USD per year: <= <b>$usd</b>;&nbsp;&nbsp;Space: => <b>$space</b>;&nbsp;&nbsp;Uptime: => <b>$uptime%</b>;&nbsp;&nbsp;";
		$body.="Addon domain: => <b>$nodomain</b>;&nbsp;&nbsp;Include free domain: <b>".(empty($freedomain)? "-":"Yes")."</b>;&nbsp;&nbsp;";
		$server_arr=array('Any','Linux','Windows','Solaris','FreeBSD','Mac OSX');
		$body.="Server: <b>$server_arr[$server]</b></p></div>";
		$arr['title']='Search result';
		if(!empty($num)){
			$body.='<ul id="fw_list">';
			while($row = @mysql_fetch_array($result, MYSQL_ASSOC)){
				$body.='<li><ul><li><h2><a href="index.php?pid='.$row['id'].'">'.$row['title'].'</a></h2></li>';
				$body.='<li>'.$row['description'].'</li></ul></li>';}
			$body.="</ul>";
			}
		$arr['content']=$body; return $arr;
}

private function post($id)
{
        $query=sprintf("UPDATE ".$this->prefix."post SET view = view+1 WHERE id = %u",$id);
		@mysql_query($query);
		$page=@$_GET['page'];
		$page=round($page);
		if($page<2){
			$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']);
				$nofollow=(($this->site['nofollow']=='Y')||($this->site['nofollow']=='A'));
				$bb= new bbcode($nofollow,0);
				$content=$bb->format_text($row['content'],$autolink);
				unset($bb);
				$str='<table class="fw_item"><tr><td class="fw_heading"><h1><a '.(($this->site['nofollow']=='A')? 'rel="nofollow" ' : '').'href="'.$row['url'].'" '.(($this->site['afflink'])?  'onclick="return fwRedirectLink('.$id.')"' : 'target="_blank"').'>'.$row['title'].'</a></h1><span class="fw_url">'.$row['url'].'</span></td></tr><tr><td class="fw_content">';
				$str.=$content.'</td></tr>';
				$str.='<tr><td class="fw_tags">Tags: '.$row['keywords'].'</td></tr>';
				$str.='<tr><td class="fw_author"><b>Posted by:</b> '.(empty($row['website'])? $row['user'] : '<a target="_blank" '.(($this->site['nofollow']=='A')? 'rel="nofollow" ' : '').'href="'.$row['website'].'">'.$row['user'].'</a>').', '.gmdate("M d, H:i",$row['date']).';&nbsp;&nbsp;&nbsp;<b>Updated on:</b> '.gmdate("M d, H:i",$row['updated']);
				$str.='<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" />';
					$report='<img title="Report" src="'.$path.'report.gif" />';
				}
				else{$l='Locked';$m='Contact';$mi=$m;$r='Reply';$e='Edit';$report='Report';}
				$str.='<li class="report"><a href="javascript:fwOpenMail(\'7M'.$row['id'].'\')">'.$report.'</a></li>';
				$str.=(($row['locked']=='Y')? '<li class="inactive">'.$l.'</li>':'<li class="reply"><a target="_blank" href="gb/user/index.php?query=2M1M'.$row['id'].'">'.$r.'</a></li>').((($row['allowemail']=='Y')&&($this->site['allowemail']))? '<li class="email"><a href="javascript:fwOpenMail(\'1M'.$row['uid'].'\')">'.$m.'</a></li>' : '<li class="inactive">'.$mi.'</li>');
				if((($this->site['allowedit'])&&(!empty($this->uid))&&($this->uid==$row['uid'])&&($row['locked']!='Y'))||($this->admin)){$str.='<li class="edit"><a target="_blank" href="gb/user/index.php?query=1M3M'.$row['id'].'">'.$e.'</a></li>';}
				$str.='</ul></td></tr></table>';
				$row['content']=$str;}}
		else{
			$query=sprintf("SELECT title, locked 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'];}
		if($row['locked']!='Y'){$row['editlink']='gb/user/index.php?query=2M1M'.$id;}
		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['commentcount'];
		$totalpage=fw_total_page($num_rows, $this->site['commentcount']);
		$query="SELECT tc.*, tu.user, tu.website, tu.allowemail, tp.locked FROM ".$this->prefix."comments tc, ".$this->prefix."user tu, ".$this->prefix."post tp ";
		$query.="WHERE tc.uid=tu.id AND tc.pid=tp.id AND tc.pid = %d AND tc.publish = 'Y' ORDER BY tc.id Asc LIMIT ".$start_count.", ".$this->site['commentcount'];
		$query=sprintf($query,$id);
		$result= @mysql_query($query);
        $str='';
		$nofollow=(($this->site['nofollow']=='Y')||($this->site['nofollow']=='A'));
		$bb= new bbcode($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_comment">'.$content.'</td></tr>';
			$str.='<tr><td class="fw_author"><b>Posted by:</b> '.(empty($row['website'])? $row['user'] : '<a target="_blank" '.(($this->site['nofollow']=='A')? 'rel="nofollow" ' : '').'href="'.$row['website'].'">'.$row['user'].'</a>').', '.gmdate("M d, H:i",$row['date']).';&nbsp;&nbsp;&nbsp;<b>Updated on:</b> '.gmdate("M d, H:i",$row['updated']);
			$str.='<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" />';
				$report='<img title="Report" src="'.$path.'report.gif" />';
			}
			else{$m='Contact';$mi=$m;$e='Edit';$report='Report';}
			$str.='<li class="report"><a href="javascript:fwOpenMail(\'8M'.$id.'M'.$row['id'].'\')">'.$report.'</a></li>';
			$str.=((($row['allowemail']=='Y')&&($this->site['allowemail']))? '<li class="email"><a href="javascript:fwOpenMail(\'1M'.$row['uid'].'\')">'.$m.'</a></li>' : '<li class="inactive">'.$mi.'</li>');
			if((($this->site['allowedit'])&&(!empty($this->uid))&&($this->uid==$row['uid'])&&($row['locked']!='Y'))||($this->admin)){$str.='<li class="edit"><a target="_blank" href="gb/user/index.php?query=2M3M'.$row['id'].'">'.$e.'</a></li>';}
			$str.='</ul></td></tr></table>';}
		unset($bb);
		$arr=array();
		$arr['content']=$str;
		if($totalpage >1){
			$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?pid='.$id.(($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['pagelink']=$pagelink;}
        return $arr;
}

private function get_index()
{
		$page=@$_GET['page'];
		if(empty($page)||!is_numeric($page)){$page=1;}
		$sort=round($_GET['sort']);
		if(empty($sort)){$sort='tp.id';}
		else{$sort=(($sort==1)? 'tp.updated':'tp.view');}
		$num_rows=$this->totalpost();
		$start_count=($page-1)*($this->site['listcount']);
		$totalpage=fw_total_page($num_rows, ($this->site['listcount']));
		$query="SELECT tp.id, tp.title, tp.description, tp.updated, tu.user, tp.view, 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.="$sort Desc LIMIT ".$start_count.", ".$this->site['listcount'];
        $result= @mysql_query($query);
		$str='<ul id="fw_list">';
        while($row = @mysql_fetch_array($result, MYSQL_ASSOC)){
			$str.='<li><ul><li><h2><a href="index.php?pid='.$row['id'].'">'.$row['title'].'</a></h2></li>';
			$str.='<li class="fw_author">Posted by: '.$row['user'].', '.gmdate("M d, H:i",$row['updated']).'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>view:</b> '.$row['view'].' &nbsp;&nbsp; <b>comment:</b> '.$row['c'].'</li>';
			if(!empty($row['description'])){$str.='<li>'.$row['description'].'</li>';}$str.='</ul></li>';}
		$str.='</ul>';
		$arr=array();
		$arr['content']=$str;
		$arr['totalpost']=$num_rows;
		if($totalpage >1){
			$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['pagelink']=$pagelink;}
        return $arr;
}

public 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);
}
public function getkey($n)
{
		$query=sprintf("SELECT keywords FROM ".$this->prefix."searchkey ORDER BY hit Desc LIMIT 0,%u",$n);
		$result= @mysql_query($query);
		$str='<ul id="fw_keymenu">';
		while($row = @mysql_fetch_array($result, MYSQL_ASSOC)){
			$link='index.php?q='.urlencode($row['keywords']);
			$anchor=(strlen($row['keywords'])>32)? substr($row['keywords'],0,30).'..' : $row['keywords'];
			$str.='<li><a title="'.$row['keywords'].'" href="'.$link.'">'.$anchor.'</a></li>';
		}
		return $str.'</ul>';
}
}
?>
Return current item: Ramui web hosting directory script