ImageSQL.class.php 3.58 KB
<?php
require_once dirname(__FILE__).'/../../db/DbMysqli.class.php';
class ImageSQL
{
    private $mDBConn;
    private $mTableName = '';
    public function __construct()
    {
        $this->mDBConn = new DbMysqli(APP_DBHOST, APP_DBUSER, APP_DBPASS, APP_DBNAME, APP_DBPORT);
        $this->mTableName = ' fileSys_files ';
    }
    
    /**
     * 根据条件获取信息
     * 
     * @param int  $userID
     * @param array $imageTypes
     * @param string $fileMD5
     * @param string $startTime
     * @param string $endTime
     * @return array
     * 		(
     * 			ID, filePath
     * 	   )
     */
    public function getImageInfo($startPos, $length, $fileMD5='' ,$userID=0, $imageTypes = array(), $startTime='', $endTime='')
    {
       $sql = "SELECT id, filePath FROM ".$this->mTableName;
       //查询条件
       $sqlWhere = "WHERE ";
       //查询条件组
       $sqlWheres = array(
       					'userID'=>'','imageTypes'=>'',
       					'startTime'=>'','endTime'=>''
       				);
       //如果文件的md5存在,查找
       if(!empty($fileMD5))
       {
       	    $sql.="WHERE uniqueMd5=".$this->escape($fileMD5);
       	    $result = $this->mDBConn->getOne($sql);
       	    return array($result);
       }
       //根据userID
       if(!empty($userID) && is_numeric($userID))
       {
       		$sqlWheres['userID'] = "uploadUser=".$userID;
       }
       //开始时间
       if(!empty($startTime))
       {
          $sqlWheres['startTime'] = " uploadTime>=".$this->escape($startTime);
       }
       //结束时间
       if(!empty($endTime))
       {
       	  $sqlWheres['endTime'] = " uploadTime<=".$this->escape($endTime);
       }
       //临时总计
        $tmpPos = 0;
       //图片类型
       if(count($imageTypes)&& !empty($imageTypes))
       {
       		foreach($imageTypes as $imageType)
       		{
       			if(!empty($imageType))
       			{
       		  	 	$tmpPos++;
       		   		if($tmpPos==1)
       		   		{
       		   			$sqlWheres['imageTypes'] = "twoLevelName=".$this->escape($imageType);
       		   		}
       		   		else
       		   		{
       		   	  		$sqlWheres['imageTypes'].=" OR twoLevelName=".$this->escape($imageType);
       		   		}
       			}	
       		}
       }
       //临时总计
       $tmpPos = 0;
       //查询条件组处理
       foreach($sqlWheres as $where)
       {
       		if(!empty($where))
       		{
       			$tmpPos++;
       			if($tmpPos==1)
       			{
       				$sqlWhere.=" (".$where.") ";
       			}
       			else
       			{
       				$sqlWhere.=" AND (".$where.") ";
       			}
       		}
       }
       //处理限制过滤
       if($startPos<0)
       {
       		$startPos = 0;
       }
       if($length>1000)
       {
       		$length = 1000;
       }
       if(!empty($tmpPos))
       {
       		$sql.= $sqlWhere;
       }
       $limit = "$startPos, $length";
       $result = $this->mDBConn->getMore($sql, $limit);
       return $result;
    }
    
	/**
 	* 针对SQL语句的变量进行反斜线过滤,并两边添加单引号
 	*
 	* @param mixed $var 过滤前变量
 	* @param boolean $strip 数据是否经过stripslashes处理
 	* @param boolean $is_array 变量是否为数组
 	* @return mixed 过滤后变量
 	*/
	private function escape($var, $strip = true, $is_array=false)
 	{
		if (is_array($var))
		{
			if (!$is_array) return " '' ";
			foreach ($var as $key => $value) 
			{
				$var[$key] = trim($this->escape($value,$strip));
			}
			return $var;
		}
		elseif (is_numeric($var)) 
		{
			return " '".$var."' ";
		} 
		else 
		{
			return " '".addslashes($strip ? stripslashes($var) : $var)."' ";
		}
	}
}
?>