<?php
//数据库连接对象 mydb
//数据表操作对象 mytb

class mydb
{	
	public  $conn;
	public  $db;
	public  $mysqli;
	
	function __construct($dbname2="") {
		include('conn_var.php');
		if($dbname2!="")$dbname=$dbname2;
		$this->mysqli=new mysqli($host,$username,$password,$dbname) ;
		/* check connection */
		if ($this->mysqli->connect_errno) {
		   printf("Can't connect $dbname : ", $mysqli->connect_error);
 		   exit();
		}else{
			$this->mysqli->set_charset('utf8');
		}
   }
   function __destruct() {
	  unset($this->db);
	  unset($this->mysqli);

   }

	function query($sqlstr)
	{
		$result=@$this->mysqli->query($sqlstr) or die ($_SESSION['cdebug']?"\n $this->mydbname - Query Error: <br/>   ".mysqli_error($this->mysqli)."<br/>   '".$sqlstr."'" :" error2");
		$errormsg=mysqli_error($this->mysqli);
		if(!$_SESSION['cdebug'] && $errormsg!="") error_log(date('y-m-d H:m:s')."  ".$dbname." - $sqlstr \r\nQuery Error: ".$errormsg."\r\n",3,'errors.log');
		if($errormsg!="") die ($_SESSION['cdebug']?"\n $this->mydbname - Query Error: <br/>   ".mysqli_error($this->mysqli)."<br/>   '".$sqlstr."'" :" error2");
		
		
		if($result) return $result; else return false; 
 	}

     //返回所有数据的二维数组
	public function AllRows($sqlstr) {
		$result = $this->mysqli->query($sqlstr);
		$list =array();
/*		while($row =  mysqli_fetch_array($result,MYSQLI_NUM)) {	$list[]= $row ;	} */
		while($row = mysqli_fetch_assoc($result))
		{	$list[]= $row ; } //array_push($list, $row);
		return $list;
	} 
	// 返回查询的一行数据，一维数组
	public function OneRow($sqlstr) {
		$result = $this->mysqli->query($sqlstr);
		$one =mysqli_fetch_row($result); //mysqli_fetch_assoc($result);// $result->fetch_row();
		return $one;
	}

	function close()
	{
		$this->mysqli->close();
	}
	
	function result($result,$row=0)  //返回最后相关行第一个字段的值
	{
		mysqli_data_seek($result,$row);
    	$row=mysqli_fetch_row($result);
    	return $row[0] ;
  	}

	function affected_id()			//返回操作影响的最后一行的ID
  	{
   		return  $this->mysqli->affected_rows;
 	}

	function affectedrows()			//返回操作影响的行数
  	{
   		return  $this->mysqli->affected_rows; 
 	}

	function insert_id()			//兼容 返回操作影响的最后一行的ID $mysqli->affected_rows
  	{
   		return  $this->mysqli->insert_id;
 	}

};


/*
	表操作类：上移，下移，允许，禁止，删除，
	$opertb->table="ls_download";
	$opertb->id=$id;
	switch ($ls)
	{
		case "up":
			$ls_msg=$opertb->up($nowNum);
			break;

		case "down":
			$ls_msg=$opertb->down($nowNum);
			break;
			
	 	case "unenable":
			$ls_msg=$opertb->unenable();
			break; 
			
		case "enable":
			$ls_msg=$opertb->enable();
			break;
			
		case "del":
			$ls_msg=$opertb->delwfile($downloadFolders,"","downloadName");
			break;

	}
*/
class mytb
{
	public $table;		//当前表
	public $id;			//当前id
	public $opendb;		//打开数据库
	public $ls_msg="";  //反馈信息
	private $delFile=""; //删除的文件
	
   function __construct() {
		
		$this->opendb=new mydb();
   }

   function __destruct() {
   }
	//上移
	// $nowNum  当前序号
	public function up($nowNum){
	$down=$nowNum+1;
	$down_sql="update ".$this->table." set Num=".$nowNum." where Num=".$down." ";
	$down_sql2="update ".$this->table." set Num=Num+1 where id=".$this->id." ";
	//echo $down_sql;
	$down_query=$this->opendb->query($down_sql);
	$down_query=$this->opendb->query($down_sql2);
	$this->ls_msg="上移操作成功！";
	return $this->ls_msg;
	}
	
	//下移
	// $nowNum  当前序号
	public function down($nowNum){
	$up=$nowNum-1;
	$up_sql="update ".$this->table." set Num=".$nowNum." where Num=".$up."  ";
	$up_sql2="update ".$this->table." set Num=Num-1 where id=".$this->id." ";
	
	//echo $up_sql;
	$up_query=$this->opendb->query($up_sql);
	$up_query=$this->opendb->query($up_sql2);
  
	$this->ls_msg="下移操作成功！";
	return $this->ls_msg;
  
	}
  
	//禁止
	public function unenable(){
	$unenable_sql="update ".$this->table." set enable=0 where id=".$this->id." ";
	//echo $unenable_sql;
	$unenable_query=$this->opendb->query($unenable_sql);
	$this->ls_msg="禁止操作成功！";
	return $this->ls_msg;
  
	}
	
	//允许
	public function enable(){
	$enable_sql="update ".$this->table." set enable=1 where id=".$this->id." ";
	//echo $enable_sql;
	$enable_query=$this->opendb->query($enable_sql);
	$this->ls_msg="允许操作成功！";
	return $this->ls_msg;
  
	}

	//删除记录和文件
	public function delwfile($delFolder1,$delFolder2,$delFieldName){
	
	$this->del($delFieldName);  //删除记录
	$path="../".$delFolder1."/".$this->delFile;
	unlink($path);	
	//删除文件
	if($delFolder2!="")
	{
		$path="../".$delFolder2."/".$this->delFile;
		unlink($path);	
	}
	//删除文件
	
	return $this->ls_msg;
	}

	//删除记录
	//$delField  文档的字段名，配合 delwfile
	public function del($delFieldName){
	$sel_sql="select * from ".$this->table." where ID=".$this->id."";
	$sel_query=$this->opendb->query($sel_sql);
	$sel_rows=mysqli_fetch_assoc($sel_query);
	$num_sel="update ".$this->table." set Num=Num-1 where Num>".$sel_rows['Num']." ";//重新排序
	$down_query=$this->opendb->query($num_sel);
	 
	$this->delFile=$sel_rows[$delFieldName];
  
	$del_sql="delete from ".$this->table." where ID=".$this->id."";
	//echo $del_sql;
	$del_query=$this->opendb->query($del_sql);  		//删除记录
	
	$this->ls_msg="删除操作成功！";
	writelog($_SERVER['PHP_SELF'],"delete ".$this->table."  ","ID: ".$this->id);
	
	return $this->ls_msg;
  
	}

public function addData($data,$checkWhereSql=1){
        
        if(!$this->checkData($checkWhereSql)){
            $this->error = "重复，不能添加";
            return false;
        }

        $key= " ";
        $list = " ";
        $tbField = $this->getFields();
        foreach($data as $k=>$v){
            $v = addslashes($v);
            for($i=0,$j=count($tbField);$i<$j;$i++){
                if($tbField[$i]==$k){
                    $key .=$tbField[$i].",";
                    $list .= "'$v',";
                }
            }
        }
        $key = substr($key,0,strlen($key)-1);
        $list = substr($list,0,strlen($list)-1);
        $sql = "insert into ".$this->table." ($key) values ($list)";
		
        return $this->opendb->query($sql);
    }

    //修改管理员的信息
    public function  modiData($id,$data){
        
        $list = " ";
        $tbField = $this->getFields("ls_admin");
        foreach($data as $k=>$v){
            $v = addslashes($v);
            for($i=0,$j=count($tbField);$i<$j;$i++){
                if($tbField[$i] == $k){
                    $list .= $k . "="."'$v',";
                }
            }
        }
        $list = substr($list,0,strlen($list)-1);
        $sql = "update ".$this->table." set $list where ID=$id";
        if($this->opendb->query($sql)){
            return true;
        }else{
            return false;
        }
    }

    //检查用户名是否存在
    public function checkData($checkWhereSql){
        $sql = "select * from ".$this->table." where ".$checkWhereSql." ";
        $rs = $this->opendb->query($sql);
        if(mysqli_num_rows($rs)==0){
            return true;
        }else{
            return false;
        }
    }


    //获得字段
    public function getFields(){
        $data=array();
        $sql = "show fields from ".$this->table;//最主要是这SQL得到全表信息
        $query = $this->opendb->query($sql);
        while($row=mysqli_fetch_array($query)){
        $data[]=$row['Field'];
        //print_r($row);
        }
        return $data;
    }


    public  function getAll($page=''){
        if($page==''){$page=1;}
        $row = array();

        if($this->keywords!=''){
        $key= " ";
        $tbField = $this->getFields();
		for($i=0,$j=count($tbField);$i<$j;$i++){
			$key .=$tbField[$i]." like '%".$this->keywords."%' or ";
        }
        $key = substr($key,0,strlen($key)-4);
		   
		$sql_str = " where  $key  "; //搜索串
        }

        $sql = "select ID from ".$this->table." ".$sql_str ;
        $rs = $this->opendb->AllRows($sql);
        $this->allpage =  count($rs);
        $pagesize = $this->pagesize;
        $n = $pagesize*($page-1);
        $sql = "select * from ".$this->table." ".$sql_str." limit $n,$pagesize";
 		//echo  $sql ;exit;
       $rs = $this->opendb->AllRows($sql);
        return $rs ;

    }

 //分页方法
    public function getPageStr($page=''){
        if($page<1){$page=1;}
        
        if($this->allpage <= $this->pagesize){
            $page_count = 1;
        }else if($this->allpage%$this->pagesize == 0){
            $page_count = $this->allpage/$this->pagesize;
        }else{
            $page_count = $this->allpage/$this->pagesize +1;
        }

        $page_count = (int)$page_count;
        // 翻页链接
        $page_string = '';
            
        if( $page == 1 ){
           $page_string .= '首页 | 上一页 |';
        }
        else{
           $page_string .= '<a href="?keywords='.$this->keywords.'&page=1"><u><font color=\"#0000FF\">首页</font></u></a> | <a href=?keywords='.$this->keywords.'&page='.($page-1).'><u><font color=\"#0000FF\">上一页</font></u></a> | ';
        }
        if( ($page == $page_count) || ($page_count == 0) ){
           $page_string .= ' 下一页 | 尾页';
        }
        else{
           $page_string .= ' <a href=?keywords='.$this->keywords.'&page='.($page+1).'><u><font color=\"#0000FF\">下一页</font></u></a> | <a href=?keywords='.$this->keywords.'&page='.$page_count.'><u><font color=\"#0000FF\">尾页</font></u></a>';
        }
             $page_string .= " &nbsp;&nbsp;页次：<font color='#FF0000'><b>".$page."</b></font>/<b>".$page_count."</b>页";
             $page_string .= " &nbsp;&nbsp;<b>".$this->pagesize."</b>个每页";

             echo $page_string;
    }

};

/*
	cktb 检查数据库中表和字段并更新
	$opertb->table="ls_download";
	$opertb->id=$id;
*/
class cktb
{
	public $table;		//当前表
	public $opendb;		//打开数据库
	public $ls_msg="";  //反馈信息
	
   function __construct() {
		
		$this->opendb=new mydb();
   }

   function __destruct() {
   }

	//检查数据表是否存在，否则增加
	public function checktable($tableName,$addTBSQL){
			
		$sql="show tables like '".$tableName."' ";
		$query=$this->opendb->query($sql);
		if(mysqli_num_rows($query)==0) 
		{				
			$query=$this->opendb->query($addTBSQL);
			if($query)
			{
				$this->ls_msg="表 ".$tableName." 已添加";
				return true;
			}
			else
			{return false;}

		}
			
	}
	
	// 根据sql语句操作
	public function qureytable($qureyTBSQL){
			
		$query=$this->opendb->query($qureyTBSQL);
		if($query)
		{
			$this->ls_msg="查询已执行";
			return true;
		}
		else
		{return false;}
	}
		
	//检查字段是否存在，否则增加
	public function checkfield($tableName,$fieldName,$addFDSQL)
	{
		  
	  if(!$this->issetfield($tableName,$fieldName))
	  {
		  $query=$this->opendb->query($addFDSQL);
		  
		  if($query)
		  {
			  $this->ls_msg="表 ".$tableName." 的 字段 ".$fieldName." 已添加";
			  return true;
		  }
		  else
		  {return false;}

	  }
	}
	

	//字段是否在表里面
	public function issetfield($tableName,$fieldName)
	{	//
	
  		$sql   = " describe  ".$tableName." ".$fieldName; 
		$query   =  $this->opendb->query($sql); 
		if(mysqli_num_rows($query)==1) 
		{	return true; }
		else 
		{	return false; }
	
	}	
	
	public function valueidfield($tableName,$fieldName,$id)
	{	// 返回值 或者 false
	
  		$sql   = " select $fieldName from   `".$tableName."` where ID=$id"; 
		$query   =  $this->opendb->query($sql); 
		$rows=mysqli_fetch_assoc($query);
		if($query) 
		{	return $rows[$fieldName]; }
		else 
		{	return false; }
	
	}	
	
 
	public function setvalueidfield($tableName,$fieldName,$id,$value=1)
	{	// 返回值 或者 false
	
  		$sql   = " update   `".$tableName."` set  $fieldName = ".$value." where ID=$id"; 
		$query   =  $this->opendb->query($sql); 
		if($query) 
		{	return true; }
		else 
		{	return false; }
	
	}	

};

/*
	mytips 查询ls_tips表提示内容和提示内容更新
	$mytips->table="ls_download";
	$mytips->id=$id;
*/
class mytips
{
	public $table;		//当前表
	public $ls_msg="";  //反馈信息
	
   function __construct() {
		
		$this->opendb=new mydb();
   }

   function __destruct() {
   }

	//  
	public function gettips($tipstitle,$title="text"){
			$qureyTBSQL="select * from ls_tips where tablename='".$this->table."' and title='".$tipstitle."' ";
			$query=$this->opendb->query($qureyTBSQL);
			$select_rows2=mysqli_num_rows($query); //总数

			if($select_rows2==0)
			{
				$this->ls_msg=$tipstitle."没有提示";
				return false;
			}
			else
			{
				$tips=mysqli_fetch_assoc($query);
				if($title=="text")return htmlspecialchars_decode($tips['text']);
				if($title=="pic")return $tips['pic'];
				if($title=="video")return $tips['video'];
			}
	}
	
	// 
	public function puttips($tipstitle,$tipstext){
			$qureyTBSQL="update   ls_tips set text='".htmlspecialchars($tipstext)."' where tablename='".$this->table."' and title='".$tipstitle."' ";
			$query=$this->opendb->query($qureyTBSQL);
			if($query)
			{
				$this->ls_msg=$tipstitle."提示已更新";
				return true;
			}
			else
			{return false;}
	}
				
};



//mysql 与 mysqli 转换函数
if(!function_exists('mysqli_connect')){
	
}

?>