<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 二手房后台系统共用函数文件
 *
 * @package     CodeIgniter
 * @subpackage  Helpers
 * @category    Helpers
 * @author      HOUSE365 ESF Dev Team
 * @link        http://nj.sell.house365.com/
 */

if( !function_exists('get_infofrom_by_agentocde') )
{
    /**
     * 根据agentcode判断来源
     * @param type $agentcode
     */
    function get_infofrom_by_agentocde($agentcode)
    {
        $infofrom = 2;
        if($agentcode == "Z000") 
        {
            $infofrom = 1;
        } else if ($agentcode == "B000")
        {
            $infofrom = 5;
        }
        return $infofrom;
    }

}

/**
 * 房源备注允许的html标签
 * @param type $str
 * @return type
 */
if( !function_exists('get_allowedhtml') )
{
    function get_allowedhtml($str) {
        $allow_tags = "<img><table><tbody><tr><td><div><span><p><ul><li><font><strong><b><em><u><sub><sup><embed><object><param>";

        $ss = strip_tags($str, $allow_tags);

        return $ss;
    }
}
/**
 * 换行符转换
 * @param type $str
 * @return type
 */
if( !function_exists('phpStrToHtml') )
{
    function phpStrToHtml($str) {
        if (strpos($str, "\n")) {
            $str = str_replace("\n", "<br>", $str);
        }
        return $str;
    }
}

//过滤多空格为一个空格
if( !function_exists('filterSpace') )
{   
    function filterSpace($str)
    {   
        //过滤多空格为一个空格  update by breathing 2011-08-03
        $str = implode(' ',array_filter(explode(' ',$str)));
        //过滤多空格为一个空格  update by 
        $str = implode(' ',array_filter(explode('&nbsp;',$str)));
        return $str;
    }
}

//计算距离到期日还有多少天
if( !function_exists('to_expire_day_new') )
{
    function to_expire_day_new($time) 
    {   
        $limit_day = '';
        $nowtime = strtotime( date( 'Y-m-d 00:00:00' ) );

        $dif = $time - $nowtime;

        $d = intval( $dif / 86400);

        if( $d < 0 )
        {
            $limit_day =  '已到期';
        }
        else if( $d <= 3)
        {
            $limit_day =  '<font style="color:red;">'.$d.'天</font>';
        }  
        else 
        {
            $limit_day =  $d.'天';
        }

        return $limit_day;
    }
}


if ( !function_exists('get_attr_select') )
{
    function get_attr_select($select)
    {
        $str = '';
        if (is_array($select) && !empty($select))
        {
            foreach($select as $k => $v)
            {
                $str .= '<dd><a class="link" oid="'. $k . '" href="####">' 
                        . $v . '</a></dd>';
            }
        }
        return $str;
    }
}

//刷新房源接口
if ( !function_exists('refresh_house') )
{
    function refresh_house($uid, $tbl, $id)
    {

        $bd = curl_get_contents(REFRESH_URL."&app=".$tbl."&uid=".$uid."&hid=".$id); 
        return $bd;
    }
}


//刷新房源接口
if ( !function_exists('refresh_house_wang') )
{
    function refresh_house_wang($uid, $tbl, $id)
    {
		//echo REFRESH_URL_WANG."&app=".$tbl."&uid=".$uid."&hid=".$id;die;
        $bd = curl_get_contents(REFRESH_URL_WANG."&app=".$tbl."&uid=".$uid."&hid=".$id); 
        return $bd;
    }
}


//批量刷新房源接口
if ( !function_exists('refresh_house_s') )
{
    function refresh_house_s($uid, $tbl, $ids)
    {
        $bd = curl_get_contents(REFRESH_HOUSE_S_URL."&app=".$tbl."&uid=".$uid."&hids=".$ids); 
        return $bd;
    }
}

function publish_banwordCheck( $string, $fileName )
{
	if ( !($words = file_get_contents( $fileName )) ){
		return false;
	}
	//echo $string;
	//$string = iconv('GBK','UTF-8',strtolower($string));
	$arrWords = explode('|', $words);
	foreach ($arrWords as $key => $value) {
		$words = iconv('GBK', 'UTF-8', $value);
		$matched = preg_match('/'.$words.'/i', $string, $result);
		if ( $matched && isset($result[0]) && strlen($result[0]) > 0 )
		{
			if ( strlen($result[0]) == 2 ){
				$matched = preg_match('/'.$words.'/iU', $string, $result);
			}	
			if ( $matched && isset($result[0]) && strlen($result[0]) > 0 ){
				return true;
			}	
		}
	}
	return false;
}

//屏蔽关键词
function check_word($word,$fileName)
{
	
	//获取关键词
	$word = iconv("GBK","UTF-8",$word);
	$words = file_get_contents($filePath.$fileName);
	$words = explode('|', $words);
	//匹配替换
	foreach($words as $k => $v) {
		$v = iconv("GBK","UTF-8",$v);
		$word = preg_replace("#$v#",'***',$word);
	}
	unset($words);
	$word = iconv("UTF-8","GBK",$word);
	return $word;
}


