<?php
class listubb{
var $mk;           //模块
var $cid;      //栏目
var $listnum;      //列表数
var $type;         //方式
var $titlenum;     //标题限制
var $style;        //特殊样式
var $siteid;       //所属网站
var $where;        //所属栏目
var $start;        //列表开始的偏移量
function __construct($siteid,$mk,$cid,$listnum,$type,$titlenum,$style,$start){
	$this->siteid    =   $siteid;
	$this->mk        =   $mk;
	$this->listnum   =   $listnum;
	$this->type      =   $type;
	$this->style     =   $style;
	$this->cid   =   $cid;
	$this->titlenum  =   $titlenum;
	if($start==NULL||$start>0||$start==''){
			$this->start   = 0;
		}else{
			$this->start   = $start;
		}
		if($cid == 0){
			$this->where = 'siteid='.$this->siteid;
		}else{
			$html = preg_split('/\|/',$cid,-1);
			if($html){
		$i = 1;
		foreach($html as $value){
			if($i == 1){
		$s = NULL;
			}else{
		$s = ',';
			}
			$in .= $s.$value;
		$i++;
	}
	$this->where = 'gro IN('.$in.')';
		}else{
	$this->where = 'gro='.$cid.' AND siteid='.$siteid;
		}
	}
}


function list_style($class,$gro,$id,$title,$urlbs){
//$class 栏目名称
//$gro   所属ID
//$id    资源ID
//$title 标题
//$urlbs 标示名称
	$style=$this->style;
			if($style == NULL || $style == 0){
		$list = '<a href="http://'.$_SERVER["HTTP_HOST"].'/'.$urlbs.'/content.php?cid='.$gro.'&id='.$id.'">'.$title.'</a><br>';
			}elseif($style==2){
		$list = '<a href="http://'.$_SERVER["HTTP_HOST"].'/'.$urlbs.'/content.php?cid='.$gro.'&id='.$id.'">['.$class.']'.$title.'</a><br>';	 
			}elseif($style==3){
		$list = '[<a href="http://'.$_SERVER["HTTP_HOST"].'/index.php?cid='.$cid.'">'.$class.'</a>]<a href="http://'.$_SERVER["HTTP_HOST"].'/'.$urlbs.'/content.php?cid='.$gro.'&id='.$id.'">'.$title.'</a><br>';	 
			}elseif($style==4){
		$list = '<li><a href="http://'.$_SERVER["HTTP_HOST"].'/'.$urlbs.'/content.php?cid='.$gro.'&id='.$id.'">'.$title.'</a></li>';	 
			}
		return $list;	 
}

function max($from,$order){
	$result = mysql_query("SELECT {$order} FROM {$from} WHERE {$this->where} ORDER BY {$order} DESC LIMIT 20");      
		if($result && mysql_num_rows($result) > 0){
	$i = 1;
		while(list($lbbsid) = mysql_fetch_row($result)){
	    if($i!=1){
	    break;
	    }
		$limit_id = $lbbsid;
		$i++;
		}
	}
	return $limit_id;
}


function article(){
	$max_limit = 'AND bbsid<='.$this->max('article','articleid');
	$type=$this->type;
			if($type==0){
		$type='rand()';
			}elseif($type == 1){
		$type = 'articleid';
			}elseif($type == 2){
		$type = 'v';
			}elseif($type == 2){
		$type = 'rand()';
	}
	$sql="SELECT articleid,gro,title,v,content,time FROM article WHERE {$this->where} ORDER BY {$this->type} DESC LIMIT {$this->start},{$this->listnum}"; 
	$result = mysql_query($sql);      
				if($result && mysql_num_rows($result) > 0){
			$i = 0;
			while(list($articleid,$gro,$title,$v,$content,$time) = mysql_fetch_row($result)){
				$sql="SELECT cn,class FROM class WHERE cid='{$gro}'";
				$results = mysql_query($sql);
				@list($cn,$class) = mysql_fetch_row($results);
			if($this->titlenum == 0){
		$title = $title;
			}else{
		$title = mb_substr($title,0,$this->titlenum,"UTF-8");
			}
		$list.=$this->list_style($cn,$gro,$articleid,$title,'article');
		}
		mysql_free_result($result);
		}else{
	$list = NULL;
}

return $list;
}

//友情链接UBB调用函数
function sitelink(){
	$type=$this->type;
	$max_limit = 'AND bbsid<='.$this->max('sitelink','sitelinkid');
		if($type == 0){
	$type='rand()';
	$max_limit=NULL;
		}elseif($type == 1){
	$type = 'sitelinkid';
		}elseif($type == 2){
	$type = 'v';
		}elseif($type ==3){
	$type='rand()';
	}
	$sql = "SELECT sitelinkid,gro,title,v,content,time,list,siteid,url FROM sitelink WHERE {$this->where} ORDER BY {$this->type} DESC LIMIT {$this->start},{$this->listnum}";
	/*执行数据查询语句*/  
	$result = mysql_query($sql);
	if($result && mysql_num_rows($result) > 0){
	$i=0;
	while(list($sitelinkid,$gro,$title,$v,$content,$time,$list,$siteid,$url) = mysql_fetch_row($result)){
		$sql="SELECT cn,class FROM class WHERE cid='{$gro}'";
		$results = mysql_query($sql);
		@list($cn,$class) = mysql_fetch_row($results);
		if($this->titlenum == 0){
			$title = $title;
		}else{
			$title = mb_substr($title,0,$this->titlenum,"UTF-8");
		}
		$list2.=$this->list_style($cn,$gro,$sitlinkid,$title,'sitelink');
	}
mysql_free_result($result);
}else{
		$list2 = NULL;
	}

//返回数据
return $list2; 
} 



function bbs(){
	$type=$this->type;
	$max_limit = 'AND bbsid<='.$this->max('bbs','bbsid');
			if($type==0){
		$type='rand()';
		$max_limit = NULL;
			}elseif($type== 1){
		$type = 'bbsid';
			}elseif($type== 2){
		$type = 'v';
			}elseif($type==3){
		$type='rand()';
			}elseif($type==7){
		$type = 'view_time';
	}

	$sql="SELECT bbsid,gro,title,v,content,time,view_time FROM bbs WHERE {$this->where}{$max_limit} ORDER BY {$this->type} DESC LIMIT {$this->start},{$this->listnum}"; 
		$result = mysql_query($sql);      
		if($result && mysql_num_rows($result) > 0){
		$i = 0;
			while(list($bbsid,$gro,$title,$v,$content,$time,$view_time) = mysql_fetch_row($result)){
				$sql="SELECT cn,cid FROM class WHERE cid='{$gro}'";
				$results = mysql_query($sql);
				@list($cn,$cid) = mysql_fetch_row($results);
				//文章列表标题字数限制
					if($this->titlenum == 0){
				$title = $title;
					}else{
				$title = mb_substr($title,0,$this->titlenum,"UTF-8");
					}
			$list.=$this->list_style($cn,$gro,$bbsid,$title,'bbs');
			}
		mysql_free_result($result);
		}else{
			$list = NULL;
		}
		//返回数据
		return $list;
	}



function download(){
	$sql="SELECT fileid,siteid,gro,file_name,file_path,file_ver,file_time,file_end,file_content,file_v,file_down,file_pic,file_auter,file_lan,file_pt FROM download WHERE {$this->where}";
		$result = mysql_query($sql);      
			if($result && mysql_num_rows($result) > 0){
			$i = 0;
			while(list($fileid,$siteid,$gro,$file_name,$file_path,$file_ver,$file_time,$file_end,$file_content,$file_v,$file_down,$file_pic,$file_auter,$file_lan,$file_pt) = mysql_fetch_row($result)){
				$sql="SELECT cn,cid FROM class WHERE cid='{$gro}'";
				$results = mysql_query($sql);
				@list($cn,$cid) = mysql_fetch_row($results);
			if($this->titlenum == 0){
				$title = $file_name;
			}else{
				$title = mb_substr($file_name,0,$this->titlenum,"UTF-8");
			}
				$list .= $this->list_style($cn,$gro,$fileid,$title,'download');
			}
		mysql_free_result($result);
	}else{
	$list = NULL;
}

return $list;
}


//最终选择函数
function listhtml(){
			if($this->mk == 'article'){
		$echo = $this->article();
			}elseif($this->mk == 'bbs'){
		$echo = $this->bbs();
			}elseif($this->mk == 'sitelink'){
		$echo = $this->sitelink();
			}elseif($this->mk =='download'){
		$echo = $this->download();
			}
		 return $echo;
	}
	
	
}
?>