<?php
function replacespace($str)
{
	if($str)
		return preg_replace('/\s/','-',$str);
	else
		return FALSE;
}
function filterSpecial($content)
{
	$content=strip_tags($content);
    $search = array ("'<script[^>]*?>.*?</script>'si","'([\r\n\s])'","'(\')'");                     
    $replace = array ("","","’");
    return preg_replace ($search, $replace, $content);
}
function getUserID()
{
	$user=getSession();
	if($user)
	{
		$u=M('user');
		$rsinfo=$u->where("userid='".$user."'")->find();
		if($rsinfo)
			return $rsinfo['id'];
		else
			return '0';	
	}
}
function getUserAccount()
{
	$user=getSession();
	if($user)
	{
		$u=M('user');
		$rs=$u->where("userid='".$user."'")->find();
		if($rs)
			return $rs['userid'];
		else
			return '0';	
	}
}
function getUserIDName($name)
{
	if($name)
	{
		$u=M('user');
		$rs=$u->where("userid='".$name."'")->getField('id');
		if($rs)
			return $rs;
		else
			return 0;
	}
}
function getUserNameID($uid)
{
	if($uid)
	{
		$u=M('user');
		$rs=$u->field('id,userid,nickname')->where("id=".$uid)->find();
		if($rs)
		{
			if(!empty($rs['nickname']))
				return $rs['nickname'];
			else
				return $rs['userid'];	
		}
		else
			return 0;
	}
}
function getSession()
{
	return Session::get(C('USER_AUTH_KEY'));
}
function getTime($time)
{
	$re="";
	if($time)
	{
		$sec=time()-$time;
		if($sec>=2*24*3600)
			$re=date("Y-m-d H:i",$time);
		else if(($sec>=24*3600)&&($sec<2*24*360))
			$re="1天前";
		else if($sec>3600)
			$re=intval($sec/3600).'小时前';
		else if($sec>120)
			$re=intval($sec/60).'分钟前';
		else
			$re=$sec.'秒前';
	}
	return $re;
}
function getGroupName($id)
{
	if($id)
	{
		$g=M('topic_group');
		$rs=$g->find($id);
		return $rs['groupname'];
	}
	else
		return '未知群组';
}
function getUserHome($username)
{
	if($username)
	{
		$user=M('user');
		$rs=$user->find($username);
		if($rs)
			return $rs['homepage'];
		else
			return "index";
	}
	else
		return "index";
}
function getTagName($tid)
{
	if($tid)
	{
		$tag=M('topic_tag');
		$rs=$tag->find($tid);
		if($rs)
			return $rs['title'];
		else
			return '未知话题';
	}
}
function getReplayCount($id)
{
	if($id)
	{
		$top=M('topic');
		$count=$top->where('parentid='.$id)->count();
		if($count)
			return $count;
		else
			return 0;
	}
	else
		return 0;
}
function checkId32($id)
{
if(preg_match("/^[0-9]*$/",$id))
    return $id;
else
    return 0;
}
function getBlog($content)
{
    $content=preg_replace ("/icon(.+?)\.gif/i","<img src='".getDadengPath(3)."www/images/icon/icon$1.gif' width='60' />  ", $content);//处理表情
	$content=preg_replace ("/@@(.+?)\.swf@@/i","<object  type=\"application/x-shockwave-flash\"  data=\"$1.swf\" width='300' height='270'><param name=\"movie\" value=\"$1.swf\" /><param name=\"wmode\" value=\"transparent\" /></object>", $content);//处理分享swf
	$content=preg_replace ("/@@http:(.+?)\.(jpg|png|gif)@@/i","<a class=\"miniImg artZoom\" href=\"http:$1.$2\" rel=\"http:$1.$2\"><img src='http:$1.$2' width='80' /></a>", $content);//处理分享图片
	$content=preg_replace ("/@@(.+?)\.(jpg|png|gif)@@/i","<a class=\"miniImg artZoom\" href=\"".getShowPath()."/$1.$2\" rel=\"".getShowPath()."/$1.$2\"><img src='".getShowPath()."/thumb_$1.$2' /></a>", $content);
	$content=preg_replace ("/@@(.+?)@@/i","<a href='$1' target='_blank'>$1</a>", $content);//处理分享url
	return $content;
}
function getBlogShow($content)
{
    $content=preg_replace ("/icon(.+?)\.gif/i","<img src='".getDadengPath(3)."www/images/icon/icon$1.gif' />  ", $content);//处理表情
	$content=preg_replace ("/@@(.+?)\.swf@@/i","<object  type=\"application/x-shockwave-flash\"  data=\"$1.swf\" width='300' height='270'><param name=\"movie\" value=\"$1.swf\" /><param name=\"wmode\" value=\"transparent\" /></object>", $content);//处理分享swf
	$content=preg_replace ("/@@http:(.+?)\.(jpg|png|gif)@@/i","<a class=\"miniImg artZoom\" href=\"http:$1.$2\" rel=\"http:$1.$2\"><img src='http:$1.$2' width='80' /></a>", $content);//处理分享图片
	$content=preg_replace ("/@@(.+?)\.(jpg|png|gif)@@/i"," <a class=\"miniImg artZoom\" href=\"".getShowPath()."/$1.$2\" rel=\"".getShowPath()."/$1.$2\"><img src='".getShowPath()."/thumb_$1.$2' /></a>", $content);
	$content=preg_replace ("/@@(.+?)@@/i","<a href='$1' target='_blank'>$1</a>", $content);//处理分享url
	return $content;
}
function getUploadPath()
{
	return APP_PATH.'/images/upload/';
}
function getShowPath()
{
	return getDadengPath(2).APP_PATH.'/images/upload/';
}
function getDadengPath($t=1)
{
	//1=homepage
	//2=upload path
	//3=ajax path
	$site=F('site');
	$domain=$site['domain'];
	$dd_path=$site['app_path']?'/'.$site['app_path'].'/':'/';
	$rewrite=$site['is_rewrite']?$site['is_rewrite']:0;
	if($t==1)
	{
		$path=$rewrite?'':"index.php/";
		return $domain.'/'.$dd_path.$path;
	}
	if($t==2)
	{
		return $dd_path;
	}
	if($t==3)
	{
		return $site['app_path']?'/'.$site['app_path'].'/':'/';
	}
}
function getGroupCount($id)
{
	if($id)
	{
		$topic=M('topic');
		$rs=$topic->where("groupid='".$id."'")->count();
		if($rs)
			return $rs;
		else
			return 0;
	}
}
function getTagCount($id)
{
	if($id)
	{
		$topic=M('topic');
		$rs=$topic->where("tagid='".$id."'")->count();
		if($rs)
			return $rs;
		else
			return 0;
	}
}
function getUserIcon($userid,$type=60)
{
	$user=M('user');
	$rs=$user->find($userid);
	$c="";
	if($type)
		$c=$type.'_';
	if($rs['icon'])
		return "dadeng_".$c.$rs['icon'];
	else
		return 'default_'.$c.'icon.jpg';
	
}
//check e-mail
function ismail($mail)
{
	if ( ereg("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9\-\.])+",$mail))
		return TRUE;
	else
		return FALSE;
}
function getSendTime()
{
	$sconfig=F('site');
	$send_check=$sconfig['send_check'];
	if($send_check)
	{
		$user=M('user');
		$rs=$user->find(getUserID());
		if(time()-$rs['create_time']<$send_check*60)
		{
			echo "新注册用户".$send_check."分钟内禁止发微博！";exit;
		}
	}
}
function getCheckHttp($content)
{
	$sconfig=F('site');
	$http=$sconfig['http_check'];
	if($http)
	{
		if($content)
		{
			if(stripos($content,"http")===FALSE)
			{
				return FALSE;
			}
			else
			{
				return TRUE;
			}
		}
	}
	else
		return FALSE;
}
//add score 
//return void
function addUserScore($usreid,$types,$memo)
{
	$sconfig=F("site");
	if($sconfig['score_ok'])
	{
		$add=0;
		if($types==1)
			$add=$sconfig['create_get'];
		else
			$add=$sconfig['replay_get'];
		$user=M('user');
		$user->setInc('score','id='.$usreid,$add);
		//写入积分日志
		$sclog=M('score');
		$scdata['userid']=$usreid;
		$scdata['types']=2;//2表示+,1表示-
		$scdata['num']=$add;
		$scdata['memo']=$memo;
		$scdata['time']=time();
		$sclog->add($scdata);
	}
}
function delUserScore($userid,$types,$memo)
{
	$sconfig=F("site");
	if($sconfig['score_ok'])
	{
		$add=0;
		if($types==1)
			$add=$sconfig['create_del'];
		else
			$add=$sconfig['replay_del'];
		$user=M('user');
		$user->setDec('score','id='.$userid,$add);
		//写入积分日志
		$sclog=M('score');
		$scdata['userid']=$userid;
		$scdata['types']=1;//2表示+,1表示-
		$scdata['num']=$add;
		$scdata['memo']=$memo;
		$scdata['time']=time();
		$sclog->add($scdata);
	}
}
function execUserScore($userid,$num,$memo)
{
	$sconfig=F("site");
	if($sconfig['score_ok'])
	{
		$user=M('user');
		$user->setDec('score','id='.$userid,$num);
		//写入积分日志
		$sclog=M('score');
		$scdata['userid']=$userid;
		$scdata['types']=1;//2表示+,1表示-
		$scdata['num']=$add;
		$scdata['memo']=$memo;
		$scdata['time']=time();
		$sclog->add($scdata);
	}
}

//check user status
//return void
function checkUserStatus()
{
	$user=M('user');
	$rs=$user->find(getUserID());
	if($rs['status']!=1)
	{
		echo "你的帐号还没通过审核";exit;
	}
}
function checkCatch()
{
	$site=FALSE;
	$site=F('site');
	if(!$site)
	{
		$s=M('site');
		$rs=$s->find();
		F('site',$rs);
	}
	
}
function replaceNoTag($content)
{	
	$site=F('site');
	$no_tag=$site['no_tag'];
	$arr=split("|",$no_tag);
	for($i=0;$i<count($arr);$i++)
	{
		$content=str_replace($arr[$i],'$$',$content);
	}
	return $content;
}

function noOKUserName($content)
{
	$noname="adminsys,admincp,adminu,blog,index,user,public,install";
	if(stripos($noname,strtolower($content))!==FALSE)
	{
		return TRUE;
	}
	else
		return FALSE;
}
//返回汉字长度1汉字=2
function str_len($str)
{
    $length = strlen(preg_replace('/[\x00-\x7F]/', '', $str));
    if ($length)
    {
        return strlen($str) - $length + intval($length / 3) * 2;
    }
    else
    {
        return strlen($str);
    }
}
//一个汉字=1长度
function getstrlen($str)
{
    $length = strlen(preg_replace('/[\x00-\x7F]/', '', $str));
    if ($length)
    {
        return strlen($str) - $length + intval($length / 3);
    }
    else
    {
        return strlen($str);
    }
}
function getStringLen($str)
{
	if(function_exists("mb_strlen"))
        return mb_strlen($str,'UTF-8');
    elseif(function_exists('iconv_strlen'))
        return iconv_strlen($str,'UTF-8');
	else
		return getstrlen($str);
}
function showResult($v){
	if($v==1){
		return '<font color=\'green\' size=3><b>√</b></font>';
	}else{
		return '<font color=\'#ff000\' size=3><b>×</b></font>';
	}
}
?>