<?php
//数据库连接对象 mydb
//数据表操作对象 mytb
//set_error_handler("customError");


class mydb
{	
	var $conn;
	var $db;
	function query($sqlstr)
	{
		require('conn_var.php');
				
		$this->conn=@mysql_connect($host,$username,$password) or die ("can't connect DBS");
		$this->db=mysql_select_db($dbname,$this->conn);
		if($this->conn&&$this->db)
			{
			//echo $sqlstr;
				$query=@mysql_query($sqlstr,$this->conn) or die ("\n Mydb - Query Error: \n   ".mysql_error()."   \n".$sqlstr);
				if($query)
				{
					return $query;
				}
			}
	}

     //返回所有数据的二维数组
	public function AllRows($sqlstr) {
		$rs = $this->query($sqlstr);
		$list = array();
		while($row = mysql_fetch_array($rs)) {
			$list[] = $row;
		}
		return $list;
	} 
	// 返回查询的一行数据，一维数组
	public function OneRow($sqlstr) {
		$rs = $this->query($sqlstr);
		$one = mysql_fetch_assoc($rs);
		return $one;
	}
	

	function close()
	{
		$close_id=@mysql_close($this->conn) or die("Can't close DBS"); 
	}
	
	function result($result,$row=0)  //返回最后相关行第一个字段的值
	{
    	return mysql_result($result,$row) ;
  	}

	function insert_id()			//返回插入的行的ID
  	{
   		return   $this->result($this->query("SELECT last_insert_id()"), 0);
 	}

};
/*
	表操作类：上移，下移，允许，禁止，删除，
	$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();
		$this->opendb->query("SET character_set_client = utf8");
		$this->opendb->query("SET character_set_results = utf8");
		$this->opendb->query("SET character_set_connection = utf8");

   }

   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;

			}
			
			//删除记录
			//$delField  文档的字段名
			public function del($delFieldName){
			$sel_sql="select * from ".$this->table." where ID=".$this->id."";
			$sel_query=$this->opendb->query($sel_sql);
			$sel_rows=mysql_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 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;
			}
			
};

/*
	cktb 检查数据库中表和字段并更新
	$opertb->table="ls_download";
	$opertb->id=$id;


*/
class cktb
{
	public $table;		//当前表
	public $opendb;		//打开数据库
	public $ls_msg="";  //反馈信息
	
   function __construct() {
		
		$this->opendb=new mydb();
		$this->opendb->query("SET character_set_client = utf8");
		$this->opendb->query("SET character_set_results = utf8");
		$this->opendb->query("SET character_set_connection = utf8");

   }

   function __destruct() {
   }

	//检查数据表是否存在，否则增加
	public function checktable($tableName,$addTBSQL){
			
			$sql="show tables like '".$tableName."' ";
			$query=$this->opendb->query($sql);
			if(mysql_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(mysql_num_rows($query)==1) 
		{	return true; }
		else 
		{	return false; }
	
	}	
	
	
};

//自定义错误处理程序
function  customError($errno,$errstr,$errorfile,$errorline,$errorcontext)
{ 
	 echo "<b>Error:</b> [$errno] $errstr<br/>";
	 echo "<b>No:</b> $errorline in $errorfile";
}

?>