<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 租售宝（经纪人版本）管理系统
 *
 * 基于Codeigniter的经纪人房源管理系统
 *
 * 租售宝（经纪人版本）管理系统是服务于房产经纪人的后台房源管理系统
 *
 *
 * @package         ZSB
 * @author          xz
 * @copyright       Copyright (c) 2006 - 2012, HOUSE365.com.
 * @link            http://nj.zsb.house365.com/
 * @version         4.0
 */

// ------------------------------------------------------------------------

/**
 *  模型类基类
 * （Codeigniter所有的模型类都必须继承CI_Model类，但CI_Model类位于esf_system目录下，
 *  不方便修改，所以创建MY_Model，用来继承CI_Model）
 *
 * 所有的模型类都继承MY_Model， MY_Model主要实现数据库的初始化连接以及一些公用方法
 *
 * @package         admincp
 * @subpackage      core
 * @category        MY_Model
 * @author          xz
 */
class MY_Model extends CI_Model
{

    /**
     * 主库
     *
     * @access protected
     * @var Object
     */
    protected $db = NULL;

    /**
     * 从库
     *
     * @access protected
     * @var Object
     */
    protected $dbback = NULL;

    /**
     * 从库
     *
     * @access protected
     * @var Object
     */
    protected $userdb = NULL;

    /**
     * Constructor
     */
    public function __construct($needDb=0,$needDbBack=0,$needMc=0)
    {
        parent::__construct();

        if(GB_CITY!='dz')
        {
            $this->is_post_die();
        }

        //加载memcached
        if($needMc==1){
            $this->load->library('My_memcached','','mc');
        }elseif($needMc==2){
            $this->load->library('My_memcached2','','mc');
        }elseif($needMc==3){
            $this->load->library('My_memcached3','','mc');
        }elseif($needMc==0){

        }else{
            $this->load->library('My_memcached','','mc');
        }

        //加载db
        if($needDb==1){
            $this->load->library('My_DB','','mydb');
            $this->db = $this->mydb->get_db_obj('db');
        }


        //加载dbback
        if($needDbBack==1){
            $this->load->library('My_DB','','mydbback');
            $this->dbback = $this->mydbback->get_db_obj('dbback');
        }


    }


    public function __destruct()
    {
        if(is_object($this->db))
        {
            //if($_GET['xue']==1){
            //var_dump($this->db);
            //}
            $this->db->close();
            //if($_GET['xue']==1){
            //	var_dump($this->db);exit();
            //}
        }

        if(is_object($this->dbback))
        {
            $this->dbback->close();
        }
    }

    public function write_log($par,$file_name='test2.txt'){
        $myfile = fopen("./".$file_name, "w+") or die("Unable to open file!");
        if(!$par){
            $par='1111';
        }
        fwrite($myfile, $par);
        //fwrite($myfile, $par);
//记得关闭流
        fclose($myfile);
    }

    public function is_post_die(){

        if(!isset($_SERVER['HTTP_USER_AGENT']) && !in_array($_SERVER['SERVER_ADDR'],array('172.17.1.22','172.17.1.56','172.17.1.57'))){
            $msg = date('y-m-d H:i:s')."::".print_r($_POST,true)."::".print_r($_SERVER,true)." \n";
            $log = './applications/zsb/logs/zhou3'."_".date('Y-m-d');
            //file_put_contents($log,$msg,FILE_APPEND );
            die();
        }else{
            // die();
            $msg = date('y-m-d H:i:s')."::".print_r($_POST,true)."::".print_r($_SERVER,true)." \n";
            $log = './applications/zsb/logs/zhou2'."_".date('Y-m-d');
            //file_put_contents($log,$msg,FILE_APPEND );
        }

        $server1 = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727)";
        $server2 = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3)";
        $server3 = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)";
        $server4 = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SV1; SE 2.X MetaSr 1.0)";
        $server5 = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0";
        $server6 = "Mozilla/5.0 (iPhone; CPU iPhone OS 8_3 like Mac OS X; zh-CN) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/12F70 UCBrowser/10.5.5.611 Mobile";
        $server7= "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36 2345Explorer/6.1.0.8256";
        $server8= "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E; GJBiz/3.3)";

        if( ( $_SERVER['HTTP_USER_AGENT'] == $server1 or $_SERVER['HTTP_USER_AGENT']==$server2 or $_SERVER['HTTP_USER_AGENT']==$server3 or $_SERVER['HTTP_USER_AGENT']==$server4 or $_SERVER['HTTP_USER_AGENT']==$server5 or $_SERVER['HTTP_USER_AGENT']==$server6 or $_SERVER['HTTP_USER_AGENT']==$server7 or $_SERVER['HTTP_USER_AGENT']==$server8) && strlen($_SERVER['HTTP_COOKIE'])<100 ) {
            die();
        }

        $ipAddress = $this->getIPaddress();



        $ipAddress = explode(',', $ipAddress);
        $ipArr = explode('.', $ipAddress[0]);

        /*if (($ipArr[0] == '122' && $ipArr[1]=='246'&& $ipArr[2]=='95') || ($ipArr[0] == '111' && $ipArr[1]=='78'&& $ipArr[2]=='18') || ($ipArr[0] == '111' && $ipArr[1]=='77'&& $ipArr[2]=='247') || ($ipArr[0] == '182' && $ipArr[1]=='111'&& $ipArr[2]=='222') || ($ipArr[0] == '111' && $ipArr[1]=='77'&& $ipArr[2]=='247')|| ($ipArr[0] == '125' && $ipArr[1]=='111'&& $ipArr[2]=='118')|| ($ipArr[0] == '27' && $ipArr[1]=='154'&& $ipArr[2]=='44')||($ipArr[0] == '121' && $ipArr[1]=='41') ||($ipArr[0] == '103' && $ipArr[1]=='27')  ||($ipArr[0] == '120' && $ipArr[1]=='39') ||($ipArr[0] == '120' && $ipArr[1]=='55') ||($ipArr[0] == '112' && $ipArr[1]=='126') ||($ipArr[0] == '123' && $ipArr[1]=='56') ||($ipArr[0] == '42' && $ipArr[1]=='62') ||($ipArr[0] == '115' && $ipArr[1]=='28') ||($ipArr[0] == '125' && $ipArr[1]=='77') || ($ipArr[0] == '59' && $ipArr[1]=='56') || ($ipArr[0] == '110' && $ipArr[1]=='83')|| ($ipArr[0] == '218' && $ipArr[1]=='85')||($ipArr[0] == '220' && $ipArr[1]=='160')||($ipArr[0] == '110'&& $ipArr[1]=='80')||($ipArr[0] == '110' && $ipArr[1]=='86')||($ipArr[0] == '110' && $ipArr[1]=='200')||($ipArr[0] == '222' && $ipArr[1]=='76')||($ipArr[0] == '27')||($ipArr[0] == '110' && $ipArr[1]=='84')||($ipArr[0] == '117' && $ipArr[1]=='30')||($ipArr[0] == '101' && $ipArr[1]=='200')||($ipArr[0] == '27' && $ipArr[1]=='154'&& $ipArr[2]=='44')||($ipArr[0] == '110' && $ipArr[1]=='86'&&$ipArr[2]=='71')|| ($ipArr[0] == '103' && $ipArr[1]=='27'&& $ipArr[2]=='4') || ($ipArr[0] == '120' && $ipArr[1]=='55'&& $ipArr[2]=='166') || ($ipArr[0] == '110' && $ipArr[1]=='84'&& $ipArr[2]=='31') || ($ipArr[0] == '110' && $ipArr[1]=='83'&& $ipArr[2]=='80') || ($ipArr[0] == '139' && $ipArr[1]=='196'&&$ipArr[2]=='9')|| ($ipArr[0] == '222' && $ipArr[1]=='76'&&$ipArr[2]=='154')||
            ($ipArr[0] == '110' && $ipArr[1]=='86'&&$ipArr[2]=='36')||($ipArr[0] == '222' && $ipArr[1]=='76'&&$ipArr[2]=='235')||($ipArr[0] == '42' && $ipArr[1]=='96'&&$ipArr[2]=='210')||($ipArr[0] == '59' && $ipArr[1]=='57')||($ipArr[0] == '222' && $ipArr[1]=='79')||($ipArr[0] == '127' && $ipArr[1]=='77')||($ipArr[0] == '61' && $ipArr[1]=='131')||($ipArr[0] == '112' && $ipArr[1]=='74') ||($ipArr[0] == '123' && $ipArr[1]=='57') ||($ipArr[0] == '120' && in_array($ipArr[1],array(24,25,26,27,36,39,42)) ) || ($ipArr[0] == '182' && $ipArr[1]=='92') || ($ipArr[0] == '121' && in_array($ipArr[1],array(40,41,42,43)))  || ($ipArr[0] == '125' && in_array($ipArr[1],array(111))) || ($ipArr[0] == '27' && in_array($ipArr[1],array(8,152))) || ($ipArr[0] == '120' && in_array($ipArr[1],array(41))))
        {
            exit;
        }*/




        /*$msg = date('y-m-d H:i:s')."::"."::".print_r($ipAddress,true)." \n";
            $log = './applications/zsb/logs/post_sell_ip'."_".date('y-m-d');
            file_put_contents($log,$msg,FILE_APPEND );*/

        if(($_SERVER['PHP_SELF'] =='/sell/manage/active/' or $_SERVER['PHP_SELF'] =='/rent/manage/active/')&& $_SERVER['REQUEST_METHOD']=='POST'){
            $msg = date('y-m-d H:i:s')."::".print_r($_POST,true)."::".print_r($_SERVER,true)." \n";
            $log = './applications/zsb/logs/post_sell'."_".date('y-m-d');
            //file_put_contents($log,$msg,FILE_APPEND );

            //  die();
        }


    }

    public function getIPaddress()
    {

        $IPaddress='';
        if (isset($_SERVER)){
            if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
                $IPaddress = $_SERVER["HTTP_X_FORWARDED_FOR"];
            } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
                $IPaddress = $_SERVER["HTTP_CLIENT_IP"];
            } else {
                $IPaddress = $_SERVER["REMOTE_ADDR"];
            }
        } else {
            if (getenv("HTTP_X_FORWARDED_FOR")){
                $IPaddress = getenv("HTTP_X_FORWARDED_FOR");
            } else if (getenv("HTTP_CLIENT_IP")) {
                $IPaddress = getenv("HTTP_CLIENT_IP");
            } else {
                $IPaddress = getenv("REMOTE_ADDR");
            }
        }
        return $IPaddress;
    }


    public function getSql($die=0){
        echo  '<pre>';
        error_reporting(7);
        ini_set("display_errors",'on');

        //打印dbBackSql
        $this->load->library('My_DB','','mydbback');
        $this->dbback = $this->mydbback->get_db_obj('dbback');
        echo  'dbback sql : <br>';
        var_dump($this->dbback->last_query()); //->dbback->last_query()

        //打印dbSql
        $this->load->library('My_DB','','mydb');
        $this->db = $this->mydb->get_db_obj('db');
        echo  'db sql : <br>';
        var_dump($this->db->last_query()); //->dbback->last_query()
        $die && die();
    }



















}

/* End of file MY_Model.php */
/* Location: ./applications/core/MY_Model.php */