<?php
@$vercode2='';	//安全校验码，此处取值请自行修改，对应检查网址vercode参数


define('PB_PAGE', 'post');
//require_once './include/common.inc.php';
//common.inc.php开始
//error_reporting(E_ALL);
error_reporting(E_ERROR | E_WARNING | E_PARSE);
@set_magic_quotes_runtime(0);

function_exists('date_default_timezone_set') && @date_default_timezone_set('Etc/GMT+0');

$loadTime = explode(' ', microtime());
$loadTime = $loadTime[0] + $loadTime[1];
$timestamp = time()-30*60;

define('IN_PBDIGG', TRUE);
//define('PBDIGG_ROOT', str_replace('\\', '/', substr((dirname(__FILE__)), 0, -7)));
define('PBDIGG_ROOT', str_replace('\\', '/', dirname(__FILE__).'/')); //zzcity add
!defined('PB_PAGE') && define('PB_PAGE', 'PB_PAGE');
define('PBDIGG_CROOT', PBDIGG_ROOT.'data/cache/');
$_attdir = 'attachments';//自定义附件目录，3.0请勿更改
define('PBDIGG_ATTACHMENT', PBDIGG_ROOT.$_attdir.'/');
$_PBENV = array();
$_PBENV['PHP_SELF'] = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : (isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : $_SERVER['ORIG_PATH_INFO']);
$_PBENV['REQUEST_URI'] = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $_PBENV['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
$_DS = DIRECTORY_SEPARATOR;

require_once PBDIGG_ROOT.'include/version.inc.php';

//require_once PBDIGG_ROOT.'include/global.func.php';
//global.func.php开始


!defined('IN_PBDIGG') && exit('Access Denied!');

function addS(&$array)
{
	if (is_array($array))
	{
		foreach ($array as $key => $value)
		{
			addS($array[$key]);
		}
	}
	elseif (is_string($array))
	{
		$array = addslashes($array);
	}
}

function stripS(&$array)
{
   	if (is_array($array))
	{
		foreach ($array as $key => $value)
		{
			stripS($array[$key]);
		}
	}
	elseif (is_string($array))
	{
		$array = stripslashes($array);
	}
}

function reS(&$array)
{
	if (is_array($array))
	{
		foreach ($array as $key => $value)
		{
			reS($array[$key]);
		}
	}
	elseif (is_string($array))
	{
		$array = addslashes(stripslashes($array));
	}
}

function gdate($time = '', $format = '')
{
	global $pb_timezone,$pb_timeformat,$pb_dateformat;
	!$time && $time = time();
	if (!$format)
	{
		//24 OR 12 time format
		$hour = $pb_timeformat ? 'H:i' : 'h:i A';
		$format = $pb_dateformat . ' ' . $hour;
	}
	return gmdate($format, ($time + $pb_timezone * 3600));
}
function loadPluginLang($mark, $cp = FALSE)
{
	global $pb_lang;
	$plugin_message = '';
	$filename = $mark;
	if ($cp) $filename .= '_cp';
	$langPath = PBDIGG_ROOT.'plugins/'.$mark.'/languages/'.$pb_lang.'/'.$filename.'.lang.php';
	if (!file_exists($langPath))
	{
		$langPath = PBDIGG_ROOT.'plugins/'.$mark.'/languages/zh/'.$filename.'.lang.php';
		if (!file_exists($langPath))
		{
			exit('Load Language File Failed!');
		}
	}
	require $langPath;
	return $plugin_message;
}
function &loadLang($type)
{
	global $pb_lang;
	static $lang = array();
	if (isset($lang[$type])) return $lang[$type];
	$langPath = PBDIGG_ROOT.'languages/'.$pb_lang.'/'.$type.'.lang.php';
	if (!file_exists($langPath))
	{
		$langPath = PBDIGG_ROOT.'languages/zh/'.$type.'.lang.php';
		if (!file_exists($langPath))
		{
			exit('Load Language File Failed!');
		}
	}
	require $langPath;
	$lang[$type] = ${$type.'_message'};
	return $lang[$type];
}

function getSingleLang($type, $key)
{
	$lang = &loadLang($type);
	$SingleLang = isset($lang[$key]) ? $lang[$key] : '';
	unset($lang);
	return $SingleLang;
}
function gCookie($var)
{
	global $pb_ckpre;
	$ckpre = $pb_ckpre ? substr(md5($pb_ckpre), 8, 6).'_' : '';
	return isset($_COOKIE[$ckpre.$var]) ? $_COOKIE[$ckpre.$var] : '';
}

/**
 * 设置COOKIE信息
 * 
 * @param $name cookie名称
 * @param $value cookie值
 * @param $expire 过期时间：-1：删除，0：即时，1：永久
 * @return Boolean 设置cookie是否成功
 */
function sCookie($name, $value, $expire = 0, $httponly = true)
{
	global $pb_ckpath, $pb_ckdomain, $pb_ckpre, $timestamp;
	switch ($expire)
	{
		case 0:
			$expire = 0;
			break;
		case 1:
			$expire = $timestamp + 31536000;
			break;
		case -1:
			$expire = $timestamp - 31536000;
			break;
		default:
			$expire += $timestamp;
			break;
	}
	!$pb_ckpath && $pb_ckpath = '/';
	$secure = ($_SERVER['SERVER_PORT'] == '443') ? 1 : 0;
	$ckpre = $pb_ckpre ? substr(md5($pb_ckpre), 8, 6).'_' : '';
	if (PHP_VERSION >= '5.2.0')
	{
		return setcookie($ckpre.$name, $value, $expire, $pb_ckpath, $pb_ckdomain, $secure, ($httponly ? 1 : 0));	
	}
	else
	{
		return setcookie($ckpre.$name, $value, $expire, $pb_ckpath, $pb_ckdomain, $secure);
	}
}

/**
 * 清除cookies
 */
function clearcookie()
{
	sCookie('pb_sid', '', -1);
	sCookie('pb_auth', '', -1);
	sCookie('pb_lastvisit', '', -1);
	sCookie('pb_tdigged', '', -1);
	sCookie('pb_tburied', '', -1);
	sCookie('pb_rdigged', '', -1);
	sCookie('pb_rburied', '', -1);
}

function getIP()
{
	$isagent = TRUE;
	if (isset ($_SERVER['HTTP_X_FORWARDED_FOR']))
	{
		$currentIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
	}
	elseif (isset ($_SERVER['HTTP_CLIENT_IP']))
	{
		$currentIP = $_SERVER['HTTP_CLIENT_IP'];
	}
	else
	{
		$currentIP = $_SERVER['REMOTE_ADDR'];
		$isagent = FALSE;
	}
	return array((preg_match('~[\d\.]{7,15}~', $currentIP, $match) ? $match[0] : 'unknow'), $isagent);
}
function redirect($msg, $url, $time = 2)
{
	showMsg($msg, $url, $time * 1000, 1);
}
/*
function showMsg($msg, $url = '', $time = 0, $redirect = 0)
{
	@extract($GLOBALS, EXTR_SKIP);
	define('MSG', TRUE);
	ob_end_clean();
	obStart();
	if (defined('IN_ADMIN'))
	{
		$admin_msg = loadLang('admin') + loadLang('common');
		if (defined('IN_PLUGIN')) $admin_msg += loadPluginLang(IN_PLUGIN, TRUE);
		if (defined('IN_MODULE')) $admin_msg += loadLang(IN_MODULE);
		isset($admin_msg[$msg]) && eval("\$msg = \"".addcslashes($admin_msg[$msg], '"')."\";");
	}
	else
	{
		isset($common_message[$msg]) && eval("\$msg = \"".addcslashes($common_message[$msg], '"')."\";");
		$pb_seotitle = strip_tags($msg);
	}

	if (PB_PAGE == 'ajax') return $msg;
	require_once pt_fetch('msg');
	PBOutPut();
}
*/

function showMsg($msg, $url = '', $time = 0, $redirect = 0)
{
	@extract($GLOBALS, EXTR_SKIP);
	define('MSG', TRUE);
	if (defined('IN_ADMIN'))
	{
		$admin_msg = loadLang('admin') + loadLang('common');
		if (defined('IN_PLUGIN')) $admin_msg += loadPluginLang(IN_PLUGIN, TRUE);
		if (defined('IN_MODULE')) $admin_msg += loadLang(IN_MODULE);
		isset($admin_msg[$msg]) && eval("\$msg = \"".addcslashes($admin_msg[$msg], '"')."\";");
	}
	else
	{
		isset($common_message[$msg]) && eval("\$msg = \"".addcslashes($common_message[$msg], '"')."\";");
		$pb_seotitle = strip_tags($msg);
	}

	echo('[err]'.$msg.'[/err]');
	exit;
}


function PEncode($txt, $key)
{
	srand((double)microtime() * 1000000);
	$encrypt_key = md5(rand(0, 32000));
	$ctr = 0;
	$tmp = '';
	for($i = 0;$i < strlen($txt); $i++) 
	{
		$ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
		$tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]);
	}
	return base64_encode(PKey($tmp, $key));
}

function PDecode($txt, $key) 
{
	$txt = PKey(base64_decode($txt), $key);
	$tmp = '';
	for ($i = 0;$i < strlen($txt); $i++)
	{
		$md5 = $txt[$i];
		$tmp .= $txt[++$i] ^ $md5;
	}
	return $tmp;
}

function PKey($txt, $encrypt_key)
{
	$encrypt_key = md5($encrypt_key);
	$ctr = 0;
	$tmp = '';
	for($i = 0; $i < strlen($txt); $i++)
	{
		$ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
		$tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
	}
	return $tmp;
}

function spendTime()
{
	global $loadTime;
	$mtime = explode(' ', microtime());
	return number_format(($mtime[1] + $mtime[0] - $loadTime), 6);
}

function writeSafeContent($filename, $content, $mod)
{
	PWriteFile($filename, "<?php\r\n!defined('IN_PBDIGG') && exit('Access Denied!');\r\n".$content."\r\n?>", $mod);
}

function PWriteFile($filename, $content, $mode = 'ab', $chmod = 1)
{
	strpos($filename, '..') !== FALSE && exit('Access Denied!');

	$fp = @fopen($filename, $mode);
	if ($fp)
	{
		flock($fp, LOCK_EX);
		fwrite($fp, $content);
		fclose($fp);
		$chmod && @chmod($filename, 0666);
		return TRUE;
	}
	return FALSE;
}

function PReadFile($filename, $mode = 'rb')
{
	strpos($filename, '..') !== FALSE && exit('Access Denied!');
	if ($fp = @ fopen($filename, $mode))
	{
		flock($fp, LOCK_SH);
		$filedata = @ fread($fp, filesize($filename));
		fclose($fp);
	}
	return $filedata;
}
function PCopy($source, $dest)
{
	return @copy($source, $dest) || PWriteFile($dest, PReadFile($source), 'wb');
}
function PMove($source, $dest)
{
	if (@copy($source, $dest) || PWriteFile($dest, PReadFile($source), 'wb'))
	{
		PDel($source);
		return true;
	}
}
function PDel($var)
{
	return strpos($var, '..') === FALSE && is_file($var) && @unlink($var) ? TRUE : FALSE;
}

function PDelDir($var)
{
	return strpos($var, '..') === FALSE && is_dir($var) && @rmdir($var) ? TRUE : FALSE;
}

function emptyDir($dir)
{
	$newdir = '';
	if (is_dir($dir))
	{
		if ($dh = opendir($dir))
		{
	        while (($file = readdir($dh)) !== FALSE)
	        {
	            if ($file != '..' && $file != '.')
	            {
	            	$newdir = $dir.'/'.$file;
	            	if (is_dir($newdir))
	            	{
	            		emptyDir($newdir, FALSE);
	            	}
	            	else
	            	{
	            		@unlink($dir.'/'.$file);
	            	}
	            }
	        }
	        closedir($dh);
    	}
    	@rmdir($dir);
	}
}

function PListFile($var, $type = array())
{
	if (is_dir($var))
	{
		$rs = @opendir($var);
		while (($file = readdir($rs)) !== FALSE)
		{
			if ($file != '..' && $file != '.')
			{
				if ($type && in_array(Fext($file), $type))
				$rt[] = $file;
			}
		}
		return $rt;
	}
	return FALSE;
}

function Pmkdir($var, $basedir, $force = FALSE)
{
	if (strpos($var, '..') !== FALSE || strpos($basedir, '..') !== FALSE)
	{
		exit('Access Denied!');
	}
	if (!is_dir($basedir.$var))
	{
		$var = preg_replace('/\/{2,}/', '/', str_replace('\\', '/', $var));
		$basedir = preg_replace('/\/{2,}/', '/', str_replace('\\', '/', $basedir));
		$temp = explode('/',$var);
		$dirnum = count($temp);
		$cur_dir = $basedir.'/';
		for($i = 0; $i < $dirnum; $i++)
		{
			$cur_dir .= $temp[$i].'/';
			if (!is_dir($cur_dir))
			{
				if (!mkdir($cur_dir, 0777) && $force)
				{
					showMsg('attachment_mkdir_failed');
				}
			} 
		}
	}
	return TRUE;
}

function random($length, $isNum = FALSE)
{
	$random = '';
	$str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
	$num = '0123456789';
	if ($isNum)
	{
		$sequece = 'num';
	}
	else
	{
		$sequece = 'str';
	}
	$max = strlen($$sequece) - 1;
	for ($i = 0; $i < $length; $i++)
	{
		$random .= ${$sequece}{mt_rand(0, $max)};
	}
	return $random;
}

function getUserHash($uid)
{
	return substr(md5(intval($uid).$GLOBALS['pb_sitehash'].ipFragment($GLOBALS['_PBENV']['PB_IP'])), 8, 8);
}

function HConvert($var)
{
	if (is_array($var))
	{
		foreach ($var as $key => $value)
		{
			$var[$key] = HConvert($value);
		}
	}
	else
	{
		do
		{
			$clean = $var;
			$var = preg_replace('~&(?!(#[0-9]+|[a-z]+);)~is', '&amp;', $var);
			$var = preg_replace(array('~%0[0-8]~','~%1[124-9]~','~%2[0-9]~','~%3[0-1]~','~[\x00-\x08\x0b\x0c\x0e-\x1f]~'), '', $var);
		}
		while ($clean != $var);
		
		$var = str_replace(array('"', '\'', '<', '>', "\t", "\r"), array('&quot;', '&#39;', '&lt;', '&gt;', '&nbsp;&nbsp;', ''), $var);
	}
	return $var;
}

/**
 * HTML危险代码过滤
 * 
 * @param String $string 过滤字符
 */
function safeConvert(&$string)
{
	require_once PBDIGG_ROOT.'include/safehtml.class.php';
	$safehtml = & new HTML_Safe();
	$string = $safehtml->parse($string);
}

/**
 * 字符截取函数
 * 
 * @param String $text 内容
 * @param Integer $limit 截取长度
 * @param String $add 更多标记
 */
function PBSubstr($text, $limit, $add = '&#8230;')
{
	global $db_charset;
	$strlen = strlen($text);
	$db_charset = strtolower($db_charset);
	if($strlen <= $limit) return $text;
//	$text = str_replace(array('&nbsp;', '&quot;', '&#039;'), array(' ', '"', "'"), $text);
	$rtext = '';
	if ($db_charset == 'utf-8')
	{
		$n = $tn = $noc = 0;
		while ($n < $strlen)
		{
			$t = ord($text{$n});
			if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126))
			{
				$tn = 1; $n++; $noc++;
			}
			elseif (194 <= $t && $t <= 223)
			{
				$tn = 2; $n += 2; $noc += 2;
			}
			elseif (224 <= $t && $t < 239)
			{
				$tn = 3; $n += 3; $noc += 2;
			}
			elseif (240 <= $t && $t <= 247)
			{
				$tn = 4; $n += 4; $noc += 2;
			}
			elseif (248 <= $t && $t <= 251)
			{
				$tn = 5; $n += 5; $noc += 2;
			}
			elseif ($t == 252 || $t == 253)
			{
				$tn = 6; $n += 6; $noc += 2;
			}
			else
			{
				$n++;
			}
			if($noc >= $limit) break;
		}
		if($noc > $limit) $n -= $tn;
		$rtext = substr($text, 0, $n);
	}
	else
	{
		$addlen = strlen($add);
		$limit -= $addlen - 1;
		for ($i = 0; $i < $limit; $i++)
		{
			$rtext .= ord($text[$i]) > 127 ? $text[$i] . $text[++$i] : $text[$i];
		}
	}
//	$rtext = str_replace(array('"', "'"), array('&quot;', '&#039;'), $rtext);
	return $rtext.$add;
}

/**
 * Convert character encoding
 * 
 * @param String $from_encoding 原始编码
 * @param String $to_encoding 目标编码
 * @param String $str 编码字符串
 */
function convert_encoding($from_encoding, $to_encoding, $str)
{
	if(empty($str) || (strtolower($from_encoding) == strtolower($to_encoding))) return $str;
//	$from_encoding = str_replace('gbk', 'gb2312', $from_encoding);
//	$to_encoding = str_replace('gbk', 'gb2312', $to_encoding);

	if(function_exists('mb_convert_encoding'))
	{
		return mb_convert_encoding($str, $to_encoding, $from_encoding);
	}
	elseif (function_exists('iconv'))
	{
		return iconv($from_encoding, $to_encoding.'//IGNORE', $str);
	}
	else
	{
		global $CHS;
		if (!is_object($CHS))
		{
			require_once PBDIGG_ROOT.'include/Chinese.class.php';
			$CHS = new Chinese($from_encoding, $to_encoding);
		}
		return $CHS->Convert($str);
	}
}

function ipControl($ip)
{
	global $pb_ipallow, $pb_ipdeny;
	$ipExist = FALSE;
	if ($pb_ipallow)
	{
		$allowip = explode("\n", unserialize($pb_ipallow));
		foreach ($allowip as $value)
		{
			if (preg_match("~^{$value}[\d\.]*~i", $ip))
			{
				$ipExist = TRUE;
				break;
			}
		}
		if (!$ipExist)
		{
			showMsg('ip_limit');
		}
	}
	if ($pb_ipdeny)
	{
		$ipdeny = explode("\n", unserialize($pb_ipdeny));
		foreach ($ipdeny as $value)
		{
			preg_match("~^{$value}[\d\.]*~i", $ip) && showMsg('ip_limit');
		}
	}
}

function parCate($cate, &$parCate, $cid = 0)
{
	if (!$cate[$cid]['cup']) return;
	foreach ($cate as $key => $value)
	{
		if ($value['cid'] == $cate[$cid]['cup'])
		{
			$parCate[] = $value['cid'];
			parCate($cate, $parCate, $value['cid']);
		}
	}
}
function subCate($cate, &$subCate, $cid = 0)
{
	foreach ($cate as $key => $value)
	{
		if ($value['cup'] == $cid)
		{
			$subCate[] = $value['cid'];
			subCate($cate, $subCate, $value['cid']);
		}
	}
}

function str_parcate($cate, &$str_parcate, $startID = 0)
{
	if (!$cate[$startID]['cup']) return;
	foreach ($cate as $key => $value)
	{
		if ($value['cid'] == $cate[$startID]['cup'])
		{
			$str_parcate .= $value['cid'].',';
			str_parcate($cate, $str_parcate, $value['cid']);
		}
	}
}
function str_subcate($cate, &$str_subcate, $startID = 0)
{
	foreach ($cate as $key => $value)
	{
		if ($value['cup'] == $startID)
		{
			$str_subcate .= $value['cid'].',';
			str_subcate($cate, $str_subcate, $value['cid']);
		}
	}
}

function cateOption($cate, &$option, $startID = 0, $index = 0, $level = 0)
{
	foreach ($cate as $key => $value)
	{
		if ($value['cup'] == $startID)
		{
			$option .= '<option value="'.$value['cid'].'"';
			$value['cid'] == $index && $option .= ' selected="select" style="background:#ffffde"';
			$option .= '>'.str_repeat('&nbsp;', $level).'| - '.htmlspecialchars($value['name']).'</option>';
			cateOption($cate, $option, $value['cid'], $index, $level + 1);
		}
	}
}

/**
 * URL重写
 * 
 * @param String $str
 */
function rewrite(&$str)
{
	if (!$str) return;
	$str = preg_replace("~<a href\=\"index.php(\?p=([0-9]{1,}))?\"([^\>]*)\>~e", "'<a href=\"'.rewriteIndex('\\2').'\"'.stripslashes('\\3').'>'", $str);
	$str = preg_replace("~<a href\=\"category.php\?cid=([0-9]{1,})(&amp;p=([0-9]{1,}))?\"([^\>]*)\>~e", "'<a href=\"'.rewriteCate('\\1', '\\3').'\"'.stripslashes('\\4').'>'", $str);
	$str = preg_replace("~<a href\=\"show.php\?tid=([0-9]{1,})(&amp;p=([0-9]{1,}))?\"([^\>]*)\>~e", "'<a href=\"'.rewriteThread('\\1', '\\3').'\"'.stripslashes('\\4').'>'", $str);
	$str = preg_replace("~<a href\=\"user.php\?uid=([0-9]{1,})(&amp;p=([0-9]{1,}))?\"([^\>]*)\>~e", "'<a href=\"'.rewriteUser('\\1', '\\3').'\"'.stripslashes('\\4').'>'", $str);
}
function rewriteIndex($page)
{
	global $pb_rewriteext, $pb_extenable, $pb_sitedir;
	return $page > 1 ? $pb_sitedir.'index'.($pb_extenable ? '_'.$page.'.'.$pb_rewriteext : $page) : $pb_sitedir;
}
function rewriteCate($cid, $page = 0)
{
	global $pb_rewriteext, $pb_extenable, $pb_sitedir, $_categories, $pb_chtmldir;
	return $pb_sitedir.($_categories[$cid]['dir'] ? $_categories[$cid]['dir'] : $pb_chtmldir.'/'.$cid).($page > 1 ? ('/'.$page.($pb_extenable ? '.'.$pb_rewriteext : '')) : '');
}
function rewriteThread($tid, $page = 0)
{
	global $pb_rewriteext, $pb_extenable, $pb_sitedir, $pb_shtmldir;
	return $pb_sitedir.$pb_shtmldir.'/'.$tid.($page > 1 ? ($pb_extenable ? '_'.$page.'.'.$pb_rewriteext : '/'.$page) : ($pb_extenable ? '.'.$pb_rewriteext : ''));
}
function rewriteUser($uid, $page = 0)
{
	global $pb_rewriteext, $pb_extenable, $pb_sitedir;
	if (!$uid) return '#';
	return $pb_sitedir.'user/'.$uid.($page > 1 ? ($pb_extenable ? '_'.$page.'.'.$pb_rewriteext : '/'.$page) : ($pb_extenable ? '.'.$pb_rewriteext : ''));
}

/**
 * 将数组转换为可通过 url 传递的字符串连接
 *
 * @param array $args
 * @return string
 */
function gURL($args)
{
    $str = $amp = '';
    foreach ($args as $key => $value)
    {
        if (is_array($value))
        {
            $str .= $amp . gURL($value);
        }
        else
        {
            $str .= $amp . rawurlencode($key) . '=' . rawurlencode($value);
        }
        $amp = '&amp;';
    }
    return $str;
}

/**
 * 返回GET OR POST变量值
 */
function GP($value, $method = 'G')
{
	if($method == 'G' && isset($_GET[$value]))
	{
		return $_GET[$value];
	}
	return $_POST[$value];
}

function formatPostTime($seconds)
{
	global $common_message, $pb_dformat, $timestamp;
	if ($pb_dformat)
	{
		$timestr = gdate($seconds);
	}
	else
	{
		$seconds = $timestamp - $seconds;
		$days = $hours = $timestr = '';
		$days = floor($seconds / 86400);
		if ($days > 0)
		{
			$seconds -= $days * 86400;
			$timestr .= $days.$common_message['format_post_day'].' ';
		}
		$hours = floor($seconds / 3600);
		if ($hours > 0)
		{
			$seconds -= $hours * 3600;
			$timestr .= $hours.$common_message['format_post_hour'].' ';
		}
		$timestr .= floor($seconds / 60).$common_message['format_post_minutebefore'];
	}
	return $timestr;
}

function Fext($filename)
{
	return strtolower(trim(substr(strrchr($filename, '.'), 1)));
}

function radioChecked($prefix, $index = 0, $number = 2)
{
	for ($i = 0; $i < $number; $i++)
	{
		$GLOBALS[$prefix.$i] = $i == $index ? 'checked="checked"' : '';
	}
}

function html_radio($params, $name, $index = '')
{
	$radio = '';
    foreach ($params as $key => $value)
    {
        $radio .= '<input type="radio" name="'.$name.'" value="'.htmlspecialchars($key).'"';
        $index == $key && $radio .= ' checked="checked"';
        $radio .= ' />&nbsp;'.htmlspecialchars($value).'&nbsp;&nbsp;';
    }
   return $radio;
}

function html_select($params, $name, $index = '', $extra = '')
{
    $select = "<select name=\"{$name}\" {$extra}>";
    foreach ($params as $key => $value)
    {
        $select .= '<option value="'.htmlspecialchars($key).'"';
        $index == $key && $select .= ' selected="selected"';
        $select .= '>'.htmlspecialchars($value)."</option>";
    }
   $select .= "</select>";
   return $select;
}

function html_checkbox($params, $name, $index = array(), $newline = 0, $extra = '')
{
	$checkbox = '';
	$i = 1;
	foreach ($params as $key => $value)
	{
		$checkbox .= '<input type="checkbox" name="'.$name.'" value="'.$key.'"';
		if (in_array($key, $index))
		{
			$checkbox .= ' checked="checked"';
		}
		$checkbox .= $extra.' />'.htmlspecialchars($value).'&nbsp;';
		if ($newline && ($i % $newline == 0))
		{
			$checkbox .= '<br />';
		}
		$i ++;
	}
	return $checkbox;
	
}

function multLink($currentPage, $totalRecords, $url, $pageSize = 10)
{
	$lang_prev = getSingleLang('common', 'prev');
	$lang_next = getSingleLang('common', 'next');
	if ($totalRecords <= $pageSize) return '';
	$mult = '';
	$totalPages = ceil($totalRecords / $pageSize);
	$mult .= '<div class="pages"><div class="nextprev">';
	$currentPage < 1 && $currentPage = 1;
	if ($currentPage > 1)
	{
		$mult .= '<a href="'.$url.'p='.($currentPage - 1).'">'.$lang_prev.'</a>';
	}
	else
	{
		$mult .= '<span class="nextprev">'.$lang_prev.'</span>';	
	}
	if ($totalPages < 13)
	{
		for ($counter = 1; $counter <= $totalPages; $counter++)
		{
			if ($counter == $currentPage)
			{
				$mult .= '<span class="current">'.$counter.'</span>';	
			}
			else
			{
				$mult .= '<a href="'.$url.'p='.$counter.'">'.$counter.'</a>';
			}
		}
	}
	elseif ($totalPages > 11)
	{
		if($currentPage < 7)		
		{
			for ($counter = 1; $counter < 10; $counter++)
			{
				if ($counter == $currentPage)
				{
					$mult .= '<span class="current">'.$counter.'</span>';
				}
				else
				{
					$mult .= '<a href="'.$url.'p='.$counter.'">'.$counter.'</a>';
				}	
			}
			$mult .= '<span>&#8230;</span><a href="'.$url.'p='.($totalPages-1).'">'.($totalPages-1).'</a><a href="'.$url.'p='.$totalPages.'">'.$totalPages.'</a>';	
		}
		elseif($totalPages - 6 > $currentPage && $currentPage > 6)
		{
			$mult .= '<a href="'.$url.'p=1">1</a><a href="'.$url.'p=2">2</a><span>&#8230;</span>';
			for ($counter = $currentPage - 3; $counter <= $currentPage + 3; $counter++)
			{
				if ($counter == $currentPage)
				{
					$mult .= '<span class="current">'.$counter.'</span>';	
				}
				else
				{
					$mult .= '<a href="'.$url.'p='.$counter.'">'.$counter.'</a>';
				}					
			}
			$mult .= '<span>&#8230;</span><a href="'.$url.'p='.($totalPages-1).'">'.($totalPages-1).'</a><a href="'.$url.'p='.$totalPages.'">'.$totalPages.'</a>';		
		}
		else
		{
			$mult .= '<a href="'.$url.'p=1">1</a><a href="'.$url.'p=2">2</a><span>&#8230;</span>';
			for ($counter = $totalPages - 8; $counter <= $totalPages; $counter++)
			{
				if ($counter == $currentPage)
				{
					$mult .= '<span class="current">'.$counter.'</span>';	
				}
				else
				{
					$mult .= '<a href="'.$url.'p='.$counter.'">'.$counter.'</a>';
				}
			}
		}
	}
	if ($currentPage < $counter - 1)
	{
		$mult .= '<a href="'.$url.'p='.($currentPage + 1).'" class="nextprev">'.$lang_next.'</a>';
	}
	else
	{
		$mult .= '<span class="nextprev">'.$lang_next.'</span>';
	}
	$mult .= '</div></div>';
	return $mult;
}
function commentMultLink($currentPage, $totalRecords, $func, $pageSize = 10)
{
	$lang_prev = getSingleLang('common', 'prev');
	$lang_next = getSingleLang('common', 'next');
	if ($totalRecords <= $pageSize) return '';
	$mult = '';
	$totalPages = ceil($totalRecords / $pageSize);
	$mult .= '<div class="pages"><div class="nextprev">';
	$currentPage < 1 && $currentPage = 1;
	if ($currentPage > 1)
	{
		$mult .= '<a href="javascript:void();" onclick="'.$func.'('.($currentPage - 1).');">'.$lang_prev.'</a>';
	}
	else
	{
		$mult .= '<span class="nextprev">'.$lang_prev.'</span>';	
	}
	if ($totalPages < 13)
	{
		for ($counter = 1; $counter <= $totalPages; $counter++)
		{
			if ($counter == $currentPage)
			{
				$mult .= '<span class="current">'.$counter.'</span>';	
			}
			else
			{
				$mult .= '<a href="javascript:void();" onclick="'.$func.'('.$counter.')">'.$counter.'</a>';
			}
		}
	}
	elseif ($totalPages > 11)
	{
		if($currentPage < 7)		
		{
			for ($counter = 1; $counter < 10; $counter++)
			{
				if ($counter == $currentPage)
				{
					$mult .= '<span class="current">'.$counter.'</span>';
				}
				else
				{
					$mult .= '<a href="javascript:void();" onclick="'.$func.'('.$counter.')">'.$counter.'</a>';
				}	
			}
			$mult .= '<span>&#8230;</span><a href="javascript:void();" onclick="'.$func.'('.($totalPages-1).')">'.($totalPages-1).'</a><a href="javascript:void();" onclick="'.$func.'('.$totalPages.')">'.$totalPages.'</a>';	
		}
		elseif($totalPages - 6 > $currentPage && $currentPage > 6)
		{
			$mult .= '<a href="javascript:void();" onclick="'.$func.'(1)">1</a><a href="javascript:void();" onclick="'.$func.'(2)">2</a><span>&#8230;</span>';
			for ($counter = $currentPage - 3; $counter <= $currentPage + 3; $counter++)
			{
				if ($counter == $currentPage)
				{
					$mult .= '<span class="current">'.$counter.'</span>';	
				}
				else
				{
					$mult .= '<a href="javascript:void();" onclick="'.$func.'('.$counter.')">'.$counter.'</a>';
				}					
			}
			$mult .= '<span>&#8230;</span><a href="javascript:void();" onclick="'.$func.'('.($totalPages-1).')">'.($totalPages-1).'</a><a href="javascript:void();" onclick="'.$func.'('.$totalPages.')">'.$totalPages.'</a>';		
		}
		else
		{
			$mult .= '<a href="javascript:void();" onclick="'.$func.'(1)">1</a><a href="javascript:void();" onclick="'.$func.'(2)">2</a><span>&#8230;</span>';
			for ($counter = $totalPages - 8; $counter <= $totalPages; $counter++)
			{
				if ($counter == $currentPage)
				{
					$mult .= '<span class="current">'.$counter.'</span>';	
				}
				else
				{
					$mult .= '<a href="javascript:void();" onclick="'.$func.'('.$counter.')">'.$counter.'</a>';
				}
			}
		}
	}
	if ($currentPage < $counter - 1)
	{
		$mult .= '<a href="javascript:void();" onclick="'.$func.'('.($currentPage + 1).')" class="nextprev">'.$lang_next.'</a>';
	}
	else
	{
		$mult .= '<span class="nextprev">'.$lang_next.'</span>';
	}
	$mult .= '</div></div>';
	return $mult;
}
function is_Page($page)
{
	return !empty($page) && preg_match ('/^([0-9]+)$/', $page);
}

function thumb($img, $height, $width, $save_prefix = 'thumb_', $del = false)
{
	if (empty($img) || !gdEnable() || !isImg($img)) return $img;
	$imginfo = @getimagesize($img);
	switch($imginfo[2])
	{
		case 1:
			$tmp_img = @imagecreatefromgif($img);
			break;
		case 2:
			$tmp_img = imagecreatefromjpeg($img);
			break;
		case 3:
			$tmp_img = imagecreatefrompng($img);
			break;
		default:
			$tmp_img = imagecreatefromstring($img);
			break;
	}
	if ($save_prefix)
	{
		$imgpath = substr($img, 0, strrpos($img, '/'));
		$filename = substr($img, strrpos($img, '/')+1);
		$savepath = $imgpath.'/'.$save_prefix.$filename;
	}
	else
	{
		$savepath = $img;
	}
	if(($height >= $imginfo[1] || !$height) && ($width >= $imginfo[0] || !$width))
	{
		if ($save_prefix)
		{
			@copy($img, $savepath) || PWriteFile($savepath, PReadFile($img), 'wb');
			$del && PDel($img);
		}
		return array($savepath, floor($imginfo[1]), floor($imginfo[0]));
	}
	$realscale = $imginfo[1] / $imginfo[0];
	if ($realscale <= 1)
	{
		$width = ($width > $imginfo[0] || !$width) ? $imginfo[0] : $width;
		$height = ($height > $imginfo[1] || !$height) ? $imginfo[1] : $width*$realscale;
	}
	else
	{
		$height = ($height > $imginfo[1] || !$height) ? $imginfo[1] : $height;
		$width = ($width > $imginfo[0] || !$width) ? $imginfo[0] : $height / $realscale;
	}
	$width = floor($width);
	$height = floor($height);
    $dst_image = imagecreatetruecolor($width, $height);
    imagecopyresampled($dst_image, $tmp_img, 0, 0, 0, 0, $width, $height, $imginfo[0], $imginfo[1]);
     switch($imginfo[2])
	{
        case '1':
        	imagegif($dst_image, $savepath);
        	break;
        case '2':
        	imagejpeg($dst_image, $savepath);
        	break;
        case '3':
        	imagepng($dst_image, $savepath);
        	break;
        default :
            imagejpeg($dst_image, $savepath);
            break;
    }
    $save_prefix && $del && PDel($img);
	return array($savepath, $height, $width);
}

/**
 * 编辑器获取函数
 * @param 2维数组 $var array(array('id'=>'xxx','type'='simple','content'='','width'=>'','height'=>''))
 * @return array array[0]:编辑器头部js	array[1]['传递指定id']:编辑器文本框	
 */
function getEditor($var)
{
	global $_PBENV, $editorjs;
	$editor = array();
	$editorjs = '<script type="text/javascript" charset="utf-8" src="'.$_PBENV['PB_URL'].'js/xheditor/xheditor.js"></script>';
	foreach ($var as $v)
	{
		$editor[$v['id']] = '<textarea cols="80" rows="10" id="'.$v['id'].'" name="'.$v['id'].'" style="width: '.(isset($v['width']) && is_int($v['width']) ? $v['width'] : 550).'px;height:'.(isset($v['height']) && is_int($v['height']) ? $v['height'] : 300).'px;">'.rteSafe($v['content']).'</textarea>';
	}
	return $editor;
}

/**
 * returns safe code for preloading in the Editor
 */
function rteSafe($strText)
{
//	$strText = str_replace(chr(145), chr(39), $strText);
//	$strText = str_replace(chr(146), chr(39), $strText);
//	$strText = str_replace("'", "&#39;", $strText);
//	$strText = str_replace(chr(147), chr(34), $strText);
//	$strText = str_replace(chr(148), chr(34), $strText);
//	$strText = str_replace(chr(10), " ", $strText);
//	$strText = str_replace(chr(13), " ", $strText);
	$strText = str_replace('<', '&lt;', $strText);
	$strText = str_replace('>', '&gt;', $strText);
	return $strText;
}

/**
 * ubb代码转html
 */
function ubb_html($str)
{
	$search = array('[strike]', '[/strike]', '[br]','[u]', '[/u]', '[i]', '[/i]', '[b]', '[/b]', '[code]', '[/code]', '[pre]', '[/pre]', '[acronym]', '[/acronym]', '[address]', '[/address]', '[div]', '[/div]', '[p]', '[/p]', '[span]', '[/span]', '[ul]', '[ol]', '[li]', '[/ul]', '[/ol]', '[/li]', '[tr]', '[/tr]', '[td]', '[/td]', '[table]', '[/table]');
	$replace = array('<strike>', '</strike>', '<br />', '<u>', '</u>', '<i>', '</i>', '<b>', '</b>', '<code>', '</code>', '<pre>', '</pre>', '<acronym>', '</acronym>', '<address>', '</address>', '<div>', '</div>', '<p>', '</p>', '<span>', '</span>', '<ul>', '<ol>', '<li>', '</ul>', '</ol>', '</li>', '<tr>', '</tr>', '<td>', '</td>', '<table>', '</table>');
	$str = str_replace($search, $replace, $str);
	$reg_search = array("#\[align=([^\[\<]+?)\](.*?)\[/align]#is",
						"/\[table=(\d{1,4}(px|em|ex|pt|pc|in|mm|cm|%|)?)(,([\(\)%,#\w ]+)?)?\]/ies",
						"#\[td=(\d{1,2}),(\d{1,2})(,(\d{1,3}(px|em|ex|pt|pc|in|mm|cm|%|)?))?\]#is",
						"#\[font=([^\[\<]+?)\](.*?)\[/font\]#is",
						"#\[size=(\d+(\.\d+)?(px|em|ex|pt|pc|in|mm|cm|%|)?)\](.*?)\[/size\]#ies",
						"#\[color=([^\[\<]+?)\](.*?)\[/color\]#is");
	$reg_replace = array("<p style=\"text-align:\\1\">\\2</p>",
						"table_code('\\1','\\4')",
						"<td colspan=\"\\1\" rowspan=\"\\2\" width=\"\\4\">",
						"<span style=\"font-family:\\1\">\\2</span>",
						"font_size('\\1', '\\4', 'content')",
						"<span style=\"color:\\1\">\\2</span>");
	$str = preg_replace($reg_search, $reg_replace, $str);
	if ((strpos($str, '[url=') !== FALSE) && (strpos($str, '[/url]') !== FALSE))
	{
		$str = preg_replace("#\[url=(https?|ftp|gopher|news|telnet|rtsp|mms|callto|ed2k){1}:\/\/([^\[\"']+?)\](.+?)\[\/url\]#is", "<a href=\"\\1://\\2\" target=\"_blank\">\\3</a>", $str);
	}
	if ((strpos($str, '[email=') !== FALSE) && (strpos($str, '[/email]') !== FALSE))
	{
		$str = preg_replace("#\[email=([a-z0-9\-_.+]+)@([a-z0-9\-_]+[.][a-z0-9\-_.]+)\](.+?)\[\/email\]#is", "<a href=\"mailto:\\1@\\2\">\\3</a>", $str);
	}
	if ((strpos($str, '[img]') !== FALSE) && (strpos($str, '[/img]') !== FALSE))
	{
		$str = preg_replace("#\[img\]\s*([^\[\<\r\n]+?)\s*\[/img\]#is", "<img src=\"\\1\" />", $str);
	}
	return $str;
}

function table_code($width, $bgcolor)
{
	if (substr($width, -1) == '%')
	{
		$width = (substr($width, 0, -1) < 98) ? (int)$width.'%' : '98%';
	}
	else
	{
		$width = (int)$width;
		$width = ($width <= 560) ? $width : '98%';
	}
	return '<table'.($width ? ' width="'.$width.'" ' : ' ').'align="center" class="t_table"'.($bgcolor ? ' bgcolor="'.$bgcolor.'" ' : '').'>';
}

function font_size($size, $content, $type)
{
	global $pb_tubbsize;
	if (is_numeric($size))
	{
		if ($type == 'content' || $type == 'comment')
		{
			if ($size > $pb_tubbsize || $size < 1) $size = 3;
		}
		else
		{
			$size = 3;
		}
		return '<font size="'.$size.'">'.$content.'</font>';
	}
	else
	{
		return '<span style="font-size:'.$size.'">'.$content.'</span>';
	}
}

/**
 * 返回附件目录
 */
function getUploadDir()
{
	global $pb_attachdir, $timestamp;
	$path = PBDIGG_ATTACHMENT;
	if ($pb_attachdir == '2' && $GLOBALS['cid'])
	{
		$dir = $GLOBALS['cid'];
	}
	elseif ($pb_attachdir == '0')
	{
		$dir = gdate($timestamp, 'ymd');
	}
	else
	{
		$dir = gdate($timestamp, 'ym');
	}
	if (!is_dir($path . $dir))
	{
		if (!mkdir($path . $dir, 0777)) showMsg('attachment_mkdir_failed');
	}
	return $dir;
}

/**
 * 清除文件缓存
 */
function clearcache($tplmark, $cacheid, $show = FALSE)
{
	global $TPL, $pb_visitcache, $cache_dir, $dir;
	if ($pb_visitcache)
	{
		$TPL->caching = TRUE;
		if ($show)
		{
			$showdir = '';
			$cacheid = (string)$cacheid;
			$tidlen = strlen($cacheid);
			$ctid = 0;
			while ($ctid < $tidlen)
			{
				$showdir .= '/'.$cacheid{$ctid};
				$ctid++;
			}
			$TPL->cache_dir = $cache_dir.$dir.$showdir;
		}
		$TPL->clear_cache($tplmark.'.html', $cacheid);
	}
}

function ckgdcode($postcode)
{
	$ckcode = gCookie('pb_ckcode');
	if (!$postcode || !$ckcode) return FALSE;
	list($t, $n) = explode("\t", $ckcode);
	return md5($postcode.$t) == $n;
}

function ckqa($answer)
{
	return $answer == $GLOBALS['pb_checkanswer'];
}

function getTagStyle()
{
    //自定义字体大小
    $sizearray = array('8','9','10','11','12','20');
    $key = array_rand($sizearray);
    return 'font-size:'.$sizearray[$key].'pt;color:#'.dechex(rand(0,255)).dechex(rand(0,196)).dechex(rand(0,255));
}

function getPostVerify($pre, $actionurl, $end)
{
	$params = '';
	if (($pos = strpos($actionurl, '?')) !== FALSE)
	{
		$queryString = substr($actionurl, $pos + 1);
		if ($queryString)
		{
			$params = explode('&', str_replace('&amp;', '&', $queryString));
			foreach ($params as $k => $v)
			{
				if (substr($v, 0, 6) == 'verify')
				{
					unset($params[$k]);
				}
			}
			$params = implode('&amp;', str_replace(array('"', "'", '<', '>'), array('%22', '%27', '%3C', '%3E'), $params)).'&amp;';
		}
		$actionurl = substr($actionurl, 0, $pos);
	}
	return '<form'.stripslashes($pre).'action="'.$actionurl.'?'.$params.'verify='.urlHash($params).'&amp;"'.stripslashes($end).'>';
}
function urlHash($querystring)
{
	global $db_hash, $customer, $_PBENV;
	$urlhash = '';
	if ($querystring)
	{
		$querystring = str_replace('&amp;', '&', $querystring);
		$querystring{strlen($querystring)-1} == '&' && $querystring = substr($querystring, 0, -1);
		$querystring = explode('&', str_replace('=','&',$querystring));
		foreach ($querystring as $p)
		{
			$urlhash .= $p;
		}
		$urlhash = md5($urlhash);
	}
	return substr(md5($urlhash.$db_hash.ipFragment($_PBENV['PB_IP']).$customer['uid']),8, 8);
}
function checkurlHash($verify)
{
	global $db_hash, $customer, $_PBENV;
	$urlhash = '';
	if ($_GET)
	{
		foreach ($_GET as $k => $v)
		{
			$k != 'verify' && $urlhash .= $k.$v;
		}
		$urlhash = md5($urlhash);
	}
	return substr(md5($urlhash.$db_hash.ipFragment($_PBENV['PB_IP']).$customer['uid']),8, 8) == $verify;
}

function checkPostHash($verify)
{
	return $verify == $GLOBALS['verifyhash'];
}

function intConvert($params)
{
	is_array($params) && settype($params, 'array');
	foreach ($params as $v)
	{
		$GLOBALS[$v] = isset($_GET[$v]) ? (int)$_GET[$v] : (isset($_POST[$v]) ? (int)$_POST[$v] : 0);
	}
}
function charConvert($params, $trim = true)
{
	is_array($params) && settype($params, 'array');
	foreach ($params as $v)
	{
		$GLOBALS[$v] = isset($_GET[$v]) ? HConvert($_GET[$v]) : (isset($_POST[$v]) ? HConvert($_POST[$v]) : '');
		$trim && $GLOBALS[$v] = trim($GLOBALS[$v]);
	}
}
function sqlLimit($page, $pagesize = 30)
{
	$page = (int)$page;
	$page <= 0 && $page = 1;
	$pagesize = (int)$pagesize;
	$pagesize <= 0 && $pagesize = 10;
	$GLOBALS['page'] = $page;
	$GLOBALS['pagesize'] = $pagesize;
	return ' LIMIT '.($page - 1) * $pagesize.', '.$pagesize;
}
/**
 * @param numeric/string $var 字符串或者数值
 * @return string 转义后的字符串
 */
function sqlEscape($var)
{
	return " '".(preg_match('~^-?[0-9]+\.?$~', $var) ? (is_int($var) ? (int)$var : (float)$var) : addslashes(stripslashes($var)))."' ";
}
function obStart()
{
	static $gzEnable;
	!isset($gzEnable) && $gzEnable = $GLOBALS['pb_gzip'] && (strpos(strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip') !== FALSE) && function_exists('ob_gzhandler') && !@ini_get('zlib.output_compression') && @ini_get('output_handler') != 'ob_gzhandler';
	$gzEnable ? ob_start('ob_gzhandler') : ob_start();
}
function pStrToTime($string)
{
	$string = strtotime($string);
	if ($string === FALSE || $string == -1)	return 0;
	return function_exists('date_default_timezone_set') ? $string - 3600 * $GLOBALS['pb_timezone'] : $string;
}

function userAgent()
{
	$os = $browser = $robot = '';
	$agent = strtolower(isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : getenv('HTTP_USER_AGENT'));

	if (preg_match('~bot|crawl|spider|slurp|sohu-search|lycos|robozilla~i', $agent))
	{
		$robot = 1;
		if (strpos($agent, 'googlebot')!==  FALSE)
		{
			$browser = 'Googlebot';
		}
		elseif (strpos($agent, 'msnbot') !== FALSE)
		{
			$browser = 'MSNbot';
		}
		elseif (strpos($agent, 'slurp') !== FALSE)
		{
			$browser = 'Yahoobot';
		}
		elseif (strpos($agent,'baiduspider') !==  FALSE)
		{
			$browser = 'Baiduspider';
		}
		elseif (strpos($agent,'yodaobot') !==  FALSE)
		{
			$browser = 'YodaoBot';
		}
		elseif (strpos($agent,'soso') !==  FALSE)
		{
			$browser = 'SosoImageSpider';
		}
		elseif (strpos($agent,'sohu-search') !==  FALSE)
		{
			$browser = 'Sohubot';
		}
		elseif (strpos($agent, 'lycos') !== FALSE)
		{
			$browser = 'Lycos';
		}
		elseif (strpos($agent, 'robozilla')!==  FALSE)
		{
			$browser = 'Robozilla';
		}
		elseif (strpos($agent, 'sogou')!==  FALSE)
		{
			$browser = 'Sogou';
		}
		else
		{
			$browser = 'unknow';
		}
	}
	elseif (strpos($agent, 'msie') !== FALSE)
	{
		$tmp = explode(';', $agent);
		$browser = trim($tmp[1]);
		$os = trim($tmp[2]);
	}
	elseif (strpos($agent, 'chrome') !== FALSE)
	{
		$tmp = explode(';', $agent);
		$os = trim($tmp[2]);
		preg_match('~chrome/[\d\.]+~i', $tmp[3], $m);
		$browser = $m[0];
	}
	elseif (strpos($agent, 'opera') !== FALSE)
	{
		$tmp = explode('(', $agent);
		if (strpos($tmp[0], 'opera') === FALSE)
		{
			$tmp_one = explode(')', $tmp[1]);
			$browser = trim($tmp_one[1]);
		}
		else
		{
			$browser = trim($tmp[0]);
		}
		$tmp_two = explode(';', $tmp[1]);
		$os = strpos($tmp_two[0], 'windows') === FALSE ? trim($tmp_two[1]) : trim($tmp_two[0]);
	}
	elseif (strpos($agent, 'safari') !== FALSE)
	{
		$tmp = explode('(', $agent);
		$tmp_one = explode(';', $tmp[1]);
		$os = trim(substr($tmp_one[2], 0, strpos($tmp_one[2], ')')));
		$tmp_two = explode(' ', $tmp[2]);
		$browser = trim(array_pop($tmp_two));
	}
	elseif (strpos($agent, 'gecko') !== FALSE)
	{
		$tmp = explode(')', $agent);
		$tmp_one = explode(';', $tmp[0]);
		$tmp_two = explode(' ', $tmp[1]);
		$browser = trim($tmp_two[2]);
		$os = trim($tmp_one[2]);
	}

	return array($os, $browser, $robot);
}

function logRobots($robot)
{
	global $pb_sitehash, $timestamp, $_PBENV;
	PWriteFile(PBDIGG_ROOT.'log/robots/'.substr(md5($pb_sitehash), 6, 6).'_'.gdate($timestamp, 'Ymd').'.txt', $robot."\t".gdate($timestamp, 'Y-m-d H:i:s')."\t".$_PBENV['PB_IP']."\t".$_PBENV['REQUEST_URI']."\n");
}

function pbNewPW($password)
{
	return md5(ipFragment($GLOBALS['_PBENV']['PB_IP']).$GLOBALS['pb_sitehash'].$password);
//	return md5($_SERVER['HTTP_USER_AGENT'].ipFragment($GLOBALS['_PBENV']['PB_IP']).$GLOBALS['pb_sitehash'].$password);
}

function ipFragment($ip)
{
	list($ip1,$ip2,) = explode('.', $ip);
	return $ip1.$ip2;
}

function isPost()
{
	return ($_SERVER['REQUEST_METHOD'] == 'POST' && checkurlHash($GLOBALS['verify']) && (empty($_SERVER['HTTP_REFERER']) || preg_replace("~https?:\/\/([^\:\/]+).*~i", "\\1", $_SERVER['HTTP_REFERER']) == preg_replace("~([^\:]+).*~", "\\1", $_SERVER['HTTP_HOST']))) ? 1 : 0;
}
function checkPost()
{
	return ($_SERVER['REQUEST_METHOD'] == 'POST' && checkPostHash($GLOBALS['verify']) && (empty($_SERVER['HTTP_REFERER']) || preg_replace("~https?:\/\/([^\:\/]+).*~i", "\\1", $_SERVER['HTTP_REFERER']) == preg_replace("~([^\:]+).*~", "\\1", $_SERVER['HTTP_HOST']))) ? 1 : 0;
}
function pb_var_export($input)
{
	$output = '';
	if (is_array($input))
	{
		$output .= "array(\r\n";
		foreach ($input as $key => $value)
		{
			$output .= "\t".pb_var_export($key).' => '.pb_var_export($value).",\r\n";
		}
		$output .= ')';
	}
	elseif (is_string($input))
	{
		$output .= "'".str_replace(array('\\','\''),array('\\\\','\\\''),$input)."'";
	}
	elseif (is_int($input) || is_double($input))
	{
		$output .= "'".(string)$input."'";
	}
	elseif (is_bool($input))
	{
		$output .= $input ? 'TRUE' : 'FALSE';
	}
	else
	{
		$output .= 'NULL';
	}
	return $output;
}
function pstrlen($text)
{
	global $db_charset;
	if (function_exists('mb_strlen'))
	{
		return mb_strlen($text, $db_charset);
	}
	elseif (strtolower($db_charset) == 'utf-8')
	{
		return preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $text, $tnum);
	}
	else
	{
		for ($i = 0, $limit = strlen($text), $tnum = 0; $i < $limit; $i++)
		{
			ord($text[$i]) > 127 && ++$i;
			$tnum ++;
		}
		return $tnum;
	}
}

function location($url, $type = 's')
{
	$url = preg_match('~^https?://~i', $url) ? $url : $GLOBALS['_PBENV']['PB_URL'].$url;
	ob_end_clean();
	if ($type == 's')
	{
		header ('Location: '.$url);
		exit;
	}
	else
	{
		exit('<meta http-equiv="refresh" content="0;url='.$url.'">');
	}
}

function forward()
{
	global $_PBENV;
	if (isset($_SERVER['HTTP_REFERER']))
	{
		$parseurl = parse_url($_SERVER['HTTP_REFERER']);
		if (isset($parseurl['host']) && strpos($_PBENV['PB_URL'], $parseurl['host']) !== FALSE && strpos($parseurl['path'], 'login') === FALSE && strpos($parseurl['path'], 'register') === FALSE)
		{
			$forward = $_SERVER['HTTP_REFERER'];
		}
	}
	!isset($forward) && $forward = $_PBENV['PB_URL'];
	return HConvert($forward);
}

function urlconj($url)
{
	return strpos($url, '?') === FALSE ? '?' : ($url{strlen(str_replace('&amp;', '&', $url)) - 1} == '&' ? '' : '&');
}

/**
 * 过滤器函数
 * @param string $dotag 过滤器标识
 * @param $var 过滤内容
 * $_actions = array(
		'reg' => array(
			'myoutput1' => '2',
			'myoutput2' => '2',
			),
		),
	);
 */
function filter_do($dotag, $content = '')
{
	global $_filterhook;
	$args = func_get_args();
	unset($args[0]);

	if (isset($_filterhook['all']))
	{
		$content = call_all_filter($args);
		$args[1] = $content;
	}

	if (isset($_filterhook[$dotag]))
	{
		$currentFilter = &$_filterhook[$dotag];

		foreach ($currentFilter as $hookFunc => $hookArgs)
		{
			if (function_exists($hookFunc))
			{
				$content = call_user_func_array($hookFunc, array_slice($args, 0, (int)$hookArgs));
				$args[1] = $content;
			}
		}
		unset($GLOBALS['_filterhook'][$dotag]);
	}
	return $content;
}

function call_all_filter($args)
{
	global $_filterhook;
	$content = $args[1];
	foreach ($_filterhook['all'] as $hookFunc => $hookArgs)
	{
		function_exists($hookFunc) && $content = call_user_func_array($hookFunc, $args);
	}
	return $content;
}
/**
 * 动作钩子
 * @param string $dotag 过滤器标识
 * $_actionshook = array(
		'reg' => array(
			'函数名' => '传递参数个数',
			),
		),
	);
 */
function action_do($dotag)
{
	global $_actionhook;
	$args = func_get_args();
	unset($args[0]);

	if (isset($_actionhook['all']))
	{
		call_all_action($args);
	}

	if (isset($_actionhook[$dotag]))
	{
		$currentActions = &$_actionhook[$dotag];
		foreach ($currentActions as $hookFunc => $hookArgs)
		{
			function_exists($hookFunc) && call_user_func_array($hookFunc, array_slice($args, 0, (int)$hookArgs));
		}
	}

	unset($GLOBALS['_actionhook'][$dotag]);
}

function call_all_action($args)
{
	global $_actionhook;
	foreach ($_actionhook['all'] as $hookFunc => $hookArgs)
	{
		function_exists($hookFunc) && call_user_func_array($hookFunc, $args);
	}
}
function cutSpilthHtml($html)
{
	$len = strlen($html);
	while ($len >= 0)
	{
		$char = substr($html, --$len, 1);
		if ($char == '>') break;
		if ($char == '<')
		{
			$html = substr($html, 0, $len);
			break;
		}
	}
	return $html;
}
function traceHtml($html, $config = 'summary')
{
	//Copyright (c) 2007-2009 PBDigg.Com

	//空标记
	$emptyElements = array('area','base','basefont','br','col','frame','hr','img','input','isindex','link','meta','param');
	/*** 允许标记 用户可以自行定义 ***/
	//摘要
	$summaryElements = array ('a','abbr','acronym','address','b','big','blockquote','br','center','cite','code','dd','del','div','dl','dt','em','font','h1','h2','h3','h4','h5','h6','hr','i','ins','li','ol','p','pre','small','span','strike','strong','sub','sup','table','tbody','td','tfoot','th','thead','tr','tt','u','ul','img');
	//文章内容
	$contentElements = array('a','abbr','acronym','address','applet','area','b','base','basefont','bdo','big','blockquote','br','button','caption','center','cite','code','col','colgroup','dd','del','dfn','dir','div','dl','dt','em','fieldset','font','form','frame','frameset','h1','h2','h3','h4','h5','h6','hr','i','iframe','img','input','ins','isindex','kbd','label','legend','li','map','menu','noframes','noscript','object','ol','optgroup','option','p','param','pre','q','s','samp','script','select','small','span','strike','strong','sub','sup','table','tbody','td','textarea','tfoot','th','thead','title','tr','tt','u','ul','var');

	$allowedElements = &${$config.'Elements'};

	$html = preg_replace('~<!--(?:.*?)-->~is', '', trim($html));

	$htmlLength = strlen($html);
	$start = 0;
	$stack = $heap = array();
	
	while (true)
	{
		if ($start >= strlen($html)) break;

		$currentChar = $html{$start};

		if ($currentChar == '<')
		{
			$tmp_html = substr($html, $start + 1);
			$gtPosition = strpos($tmp_html, '>');
			$analyzeElement = ltrim(substr($tmp_html, 0, $gtPosition));

			$html_start = substr($html, 0, $start);
			$html_end = substr($html, $start + $gtPosition + 2);

			if ($analyzeElement{0} == '/')
			{
				/**闭合标记分析**/

				//标识
				$realElement = substr($analyzeElement, 1);
				//小写标识
				$realStandardElement = strtolower($realElement);
				//小写转换标识
				$replaceTag = $realElement == $realStandardElement ? FALSE : TRUE;
				//合法标记识别
				$allowedElementskey = array_search($realStandardElement, $allowedElements);
				//空标记识别
				$emptyElementskey = array_search($realStandardElement, $emptyElements);
				//入栈标记
				$stackkey = array_search($realStandardElement, $stack);

				if ($allowedElementskey === FALSE || $emptyElementskey !== FALSE)
				{
					//删除标记
					$html = $html_start . $html_end;
//					$htmlLength -= $gtPosition - 2;
					continue;
				}
				if ($stackkey === FALSE)
				{
					$heap[] = $realStandardElement;
					$start += $gtPosition + 2;
					continue;
				}
				if ($replaceTag)
				{
					//大小写替换处理
					$html = $html_start . "</$realStandardElement>" . $html_end;
				}
				//删除标签
				unset($stack[$stackkey]);
				$start += $gtPosition + 2;
			}
			else
			{
				$whiteSpacePosition = strpos($analyzeElement, ' ');
				$attributes = '';
				if ($whiteSpacePosition)
				{
					$realElement = substr($analyzeElement, 0, $whiteSpacePosition);
					$attributes = rtrim(substr($analyzeElement, $whiteSpacePosition));
				}
				elseif ($analyzeElement == 'br/' || $analyzeElement == 'hr/')
				{
					//修复
					$realElement = substr($analyzeElement, 0, -1);
				}
				else
				{
					$realElement = $analyzeElement;
				}

				//小写标识
				$realStandardElement = strtolower($realElement);
				//小写转换标识
				$replaceTag = $realElement == $realStandardElement ? FALSE : TRUE;
				//合法标记识别
				$allowedElementskey = array_search($realStandardElement, $allowedElements);

				if ($allowedElementskey === FALSE)
				{
					//删除标记
					if (($endKey = strpos($html_end, '</'.$realStandardElement.'>')) !== FALSE)
					{
						$html_end = substr($html_end, $endKey + strlen($realStandardElement) + 3);
					}
					$html = $html_start . $html_end;
					continue;
				}

				//空标记识别
				$emptyElementskey = array_search($realStandardElement, $emptyElements);
				if ($emptyElementskey !== FALSE && (!$attributes || $attributes{strlen($attributes) - 1} != '/'))
				{
					//空标记处理
					$attributes = $attributes.' /';
				}
				$html_middle = $realStandardElement.$attributes;
				$html = $html_start . "<$html_middle>" . $html_end;

				$emptyElementskey === FALSE && $stack[] = $realStandardElement;
				$start += strlen($html_middle) + 2;
			}
		}
		else
		{
			$start++;	
		}
	}
	if ($heap)
	{
		foreach ($heap as $n)
		{
			$html = '<'.$n.'>'.$html;
		}
	}
	if ($stack)
	{
		$stack = array_reverse($stack);
		foreach ($stack as $n)
		{
			$html .= '</'.$n.'>';
		}
	}
	return trim(preg_replace('~<([a-z]+?)><\/\\1>~i', '', str_replace('<a>', '', $html)));
}

function PBOutPut($cacheid = NULL, $compileid = NULL)
{
/*	global $pb_gzip, $DB;

	if (defined('IN_ADMIN'))
	{
		$output = preg_replace(array('~<!--\d{10}-->\n~','~<form([^>]*?)action="([^\"]+?)"([^>]*?)>~ie'),array('',"getPostVerify('\\1','\\2','\\3')"), ob_get_contents());
		ob_end_clean();
		!defined('MSG') && PB_PAGE != 'login' && PB_PAGE != 'admincp' && $output .= '<script type="text/javascript">document.getElementById("debug").innerHTML = "Processed in ' . spendTime() . ' second(s), ' . $DB->queries . ' queries '.($pb_gzip ? 'Gzip Enabled' : 'Gzip Disabled').'";</script>';
	}
	else
	{
		global $pb_exectime, $pb_rewrite, $pb_timezone, $timestamp, $_PBENV;

		$output = preg_replace('~<!--\d{10}-->\n~', '', ob_get_contents());
		ob_end_clean();
		$pb_rewrite && rewrite($output);
//		PB_PAGE != 'post' && $output .= '<script type="text/javascript" src="http://stat.pbdigg.com/stat.php?h='.base64_encode($GLOBALS['pb_sitehash']."\t".$_PBENV['VERSION']).'"></script>';
		$pb_exectime && $output .= '<script type="text/javascript">document.getElementById("debug").innerHTML = "GMT '.($pb_timezone >= 0 ? '+' : '').$pb_timezone.', '.gdate($timestamp, 'Y-m-d H:i').', Processed in ' . spendTime() . ' second(s), ' . ($DB ? $DB->queries : 0) . ' queries '.($pb_gzip ? 'Gzip Enabled' : 'Gzip Disabled').'";</script>';
	}
	obStart();
	echo $output;
	unset($output);*/
	exit;
}

function pb_tpl($tplid)
{
	global $transfer;
	return $transfer->getTplVar($tplid);
}
/**
 * @param $face 头像字符串
 * @param $uid 用户ID
 * @param $size 尺寸
 * @param $trueface 真人头像
 */
function userFace($face, $uid, $size = 'middle', $trueface = false)
{
	global $uc_avatar, $uc_url, $pb_avathight, $pb_avatwidth, $_PBENV;
	if ($uc_avatar)
	{
		$size = in_array($size, array('big', 'middle', 'small')) ? $size : 'middle';
		$uid = abs(intval($uid));
		$uid = sprintf("%09d", $uid);
		$dir1 = substr($uid, 0, 3);
		$dir2 = substr($uid, 3, 2);
		$dir3 = substr($uid, 5, 2);
		$typeadd = $trueface ? '_real' : '';
		$ucAvatarError = ' onerror="this.onerror=null;this.src=\''.$uc_url.'images/noavatar_'.$size.'.gif\'" ';
		$avatar = $uc_url.'data/avatar/'.$dir1.'/'.$dir2.'/'.$dir3.'/'.substr($uid, -2).$typeadd.'_avatar_'.$size.'.jpg" onerror="this.onerror=null;this.src=\''.$uc_url.'images/noavatar_'.$size.'.gif\'';
	}
	elseif ($face)
	{
		list($faceurl, $type) = explode('|', $face);
		//1:upload 2:url 3:default
		$avatar = ($type == 2 && substr($faceurl,0,7) == 'http://') ? $faceurl : ($_PBENV['PB_URL'].'images/'.($type == 1 ? 'avatars' : 'portrait').'/'.$faceurl);
	}
	else
	{
		$avatar = $_PBENV['PB_URL'].'images/portrait/default.png';
	}
	return $avatar;
}

/**
 * 用户空间地址
 */
function userSpace($uid, $ucuid)
{
	global $uc_space, $uc_spaceurl, $pb_sitedir, $pb_rewrite;
	return ($uc_space && $uc_spaceurl && is_numeric($ucuid)) ? $uc_spaceurl.'space.php?uid='.$ucuid : ($pb_rewrite ? rewriteUser($uid) : $pb_sitedir.'user.php?uid='.$uid);
}

function contentCopyCtrl()
{
	$randstr = '';
	for ($i = 0; $i < 10; $i++)
	{
		$randstr .= chr(mt_rand(0, 126));
	}
	$randstr = str_replace('<', '&lt;', $randstr);
	return '<span style="display:none">'.$randstr.'</span>&nbsp;<br />';
}

function articleShield($lang)
{
	return '<div class="articleshield">'.getSingleLang('common', $lang).'</div>';
}

function pbrank($pbrank, $digg = 0, $bury = 0, $views = 0, $comments = 0, $postdate = 0)
{
	global $timestamp;

	$max = 16777215;
	$day = 86400;

	$septime = $timestamp - $postdate;
	$total = 0;
	$total += $digg * 5;
	$total += $bury * 3;
	$total += $views * 0.1;
	$total += $comments * 2;
	if ($septime <= $day)
	{
		$total *= 1.5;
	}
	elseif ($septime <= 2 * $day)
	{
		$total *= 0.7;
	}
	elseif ($septime <= 3 * $day)
	{
		$total *= 0.5;
	}
	else
	{
		$total = 0;
	}
	$total = ($total <= $max && $total >= 0) ? $total : $pbrank;

	return intval(floor($total));
}

function makeTopicImg($file)
{
	global $timestamp, $pb_topicthumbsize;
	$filename = substr($file, strrpos($file, '/') + 1);
	$topicimginfo = @getimagesize($file);
	$topicimgh = floor($topicimginfo[1]);
	$topicimgw = floor($topicimginfo[0]);
	$topicdir = gdate($timestamp, 'ym');
	if ((is_dir(PBDIGG_ATTACHMENT.'topic/'.$topicdir) || @mkdir(PBDIGG_ATTACHMENT.'topic/'.$topicdir)) && PCopy($file, PBDIGG_ATTACHMENT.'topic/'.$topicdir.'/'.$filename))
	{
		list ($_height, $_width) = explode("\t", $pb_topicthumbsize);
		$_imginfo = thumb(PBDIGG_ATTACHMENT.'topic/'.$topicdir.'/'.$filename, $_height, $_width, '');
		$topicimgh = $_imginfo[1];
		$topicimgw = $_imginfo[2];
		return $topicdir.'/'.$filename.'|'.$topicimgh.'|'.$topicimgw.'|0';
	}
	return '';			
}

function getSiteContent($url, $timeout = 10)
{
	$content = '';
	if (function_exists('curl_init'))
	{
		ob_start();
		$ch = curl_init($url);
		curl_setopt ($ch, CURLOPT_REFERER, $url);
		curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
		curl_exec($ch);
		curl_close($ch);
		$content = ob_get_contents();
		ob_end_clean();
		return $content ? $content : false;
	}
	else
	{
		require_once PBDIGG_ROOT.'include/Snoopy.class.php';
		$snoopy = new Snoopy();
		$snoopy->read_timeout = $timeout;
		$snoopy->fetch($url);
		return $snoopy->error ? '' : $snoopy->results;
	}
}
function replace_tag($tag, $content)
{
	global $_tagkeywords;

	foreach ($_tagkeywords as $k => $v)
	{
		if ($v && strpos($content, $v) !== FALSE)
		{
			if (!preg_match('~&[a-z]*?'.preg_quote($v, '~').'[a-z]*?;~i', $content))
			{
				$content = preg_replace('~('.preg_quote($v, '~').')~ie', "replace_single_tag('\\1')", $content);
			}
		}
	}
	return $tag.str_replace('\\"', '"', $content);
}
function replace_single_tag($tag)
{
	global $_tagkeywords, $pb_sitedir;
	if (in_array($tag, $_tagkeywords))
	{
		$key = array_keys($_tagkeywords, $tag);
		if ($key) unset($_tagkeywords[$key[0]]);
		return "<a href=\"{$pb_sitedir}index.php?tag=".rawurlencode($tag)."\"><span style=\"text-decoration:underline;\">{$tag}</span></a>";
	}
	return $tag;
}

function ubb_parse($content)
{
	$content = preg_replace('~\[flash\s*(?:=\s*(\d+)\s*,\s*(\d+)\s*)?\]([\s\S]+?)\[\/flash\]~ie',"flash_parse('\\1','\\2','\\3')", $content);
	$content = preg_replace('~\[media\s*(?:=\s*(\d+)\s*,\s*(\d+)\s*)?\]([\s\S]+?)\[\/media\]~ie',"media_parse('\\1','\\2','\\3')", $content);
	return $content;
}
function ubb_deparse($content)
{
	$content = preg_replace('~<embed((?:\s+[^>]+)?(?:\s+type="application/x-shockwave-flash")[^>]*?)/?>~ie', "media_deparse('\\1')", $content);
	$content = preg_replace('~<embed((?:\s+[^>]+)?(?:\s+type="application/x-mplayer2")[^>]*?)/?>~ie', "media_deparse('\\1','media')", $content);
	return $content;
}
function flash_parse($h, $w, $url)
{
	(!$w || !is_numeric($w)) && $w = 550;
	(!$h || !is_numeric($h)) && $h = 400;
	return '<embed type="application/x-shockwave-flash" src="'.$url.'" wmode="opaque" quality="high" bgcolor="#ffffff" menu="false" play="false" loop="false" width="'.$w.'" height="'.$h.'"/>';
}
function media_parse($h, $w, $url)
{
	(!$w || !is_numeric($w)) && $w = 550;
	(!$h || !is_numeric($h)) && $h = 400;
	return '<embed type="application/x-mplayer2" src="'.$url.'" enablecontextmenu="false" autostart="false" width="'.$w.'" height="'.$h.'"/>';
}
function media_deparse($all, $tag = 'flash')
{
	$all = str_replace('\"', '"', $all);
	if (preg_match('~src\s*=["|\']([^"|\']+)["|\']~i', $all, $m))
	{
		$url = $m[1];
	}
	else
	{
		return '';
	}
	$w = preg_match('~width\s*=["|\'](\d+)["|\']~i', $all, $m) ? $m[1] : 550;
	$h = preg_match('~height\s*=["|\'](\d+)["|\']~i', $all, $m) ? $m[1] : 400;
	return "[{$tag}={$h},{$w}]{$url}[/{$tag}]";
}

require_once PBDIGG_ROOT.'include/usr.func.php';




//global.func.php结束

if (!get_magic_quotes_gpc())
{
	addS($_GET);
	addS($_POST);
	addS($_COOKIE);
}

foreach(array('_GET','_POST') as $_gp)
{
	foreach($$_gp as $_k => $_v)
	{
		($_k{0} == '_' || $_k == 'GLOBALS') && exit;
		$$_k = $_v;
	}
}

require_once PBDIGG_CROOT.'cache_config.php';

$tpl_dir = $pb_style;//当前模板
$pb_timecorrect && $timestamp += $pb_timecorrect;

if (!$pb_siteurl)
{
	//获取当前pb访问地址
	$sHost = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : (isset($_SERVER['SERVER_NAME']) ? (int)$_SERVER['SERVER_NAME'] : (int)getenv('SERVER_NAME'));
	$sPort = isset($_SERVER['SERVER_PORT']) ? (int)$_SERVER['SERVER_PORT'] : (int)getenv('SERVER_PORT');
	$sPort == '80' && $sPort = '';
	$sSecure = (isset($_SERVER['HTTPS']) || $sPort == 433) ? 1 : 0;
	$sDir = trim(dirname($_PBENV['PHP_SELF']));
	$pb_siteurl = 'http'.(isset($_SERVER['HTTPS']) || $sPort == 433 ? 's' : '').'://'.preg_replace('~[\\\\/]{2,}~i', '/', $sHost.($sPort && (!$sSecure && $sPort != 80 || $sSecure && $sPort != 433) && strpos($sHost, ':') === FALSE ? ':'.$sPort : '').$sDir.'/');
}

$_PBENV['PB_URL'] = HConvert($pb_siteurl);

if (isset($_SERVER['HTTP_CLIENT_IP']))
{
	$currentIP = $_SERVER['HTTP_CLIENT_IP'];
}
elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
{
	$currentIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
	$currentIP = $_SERVER['REMOTE_ADDR'];
}
$_PBENV['PB_IP'] = preg_match('~^[\d\.]{7,15}$~', $currentIP, $match) ? $match[0] : 'unknow';
unset($match);

if ($_DS == '/' && $pb_loadavg)
{
	if((function_exists('sys_getloadavg') && $loadavg = sys_getloadavg()) || ($loadavg = explode(' ', @file_get_contents('/proc/loadavg'))))
	{
		(($loadavg[0] > $pb_loadavg) || (!$_COOKIE && !$_SERVER['HTTP_USER_AGENT'])) && exit('Service Unavailable!');
	}
}

$userAgent = userAgent();
$pb_robots && $userAgent[2] && logRobots($userAgent[1]);

obStart();

//变量初始化
$_categories = $_siteFounder = array();

$logStatus = $ucHeader = $jsmore = $cssmore = $gtype = $grouptitle = $uplower = $uphigher = $allowvisit = $allowsort = $allowread = $allowpost = $allowcomment 
= $allowdigg = $allowbury = $allowbsearch = $allowasearch = $searchmax = $allowreport = $allowhtml = $allowmsg 
= $msgmax = $allowfavors = $favorsmax = $allowavatar = $allowaupload = $uploadmax = $uploadtype = $allowadmincp = $alloweditatc = $allowdelatc = $allowcheckatc = $allowlockatc = $allowmoveatc = $allowcopyatc = $allowtopatc = $allowcommend = $allowshield = $allowedip = $allowtitlestyle = '';

require_once PBDIGG_ROOT.'data/sql.inc.php';

PB_PAGE != 'AJAX' && PB_PAGE != 'WAP' && header('Content-Type: text/html; charset='.$db_charset);

$common_message = loadLang('common');

require_once PBDIGG_ROOT.'include/ptemplate.func.php';

if ($pb_lastvisit = gCookie('pb_lastvisit'))
{
	list($lastVisit, $lastPath) = explode("\t", $pb_lastvisit);
	$pb_refreshtime && ($timestamp - $lastVisit < $pb_refreshtime) && ($lastPath == $_PBENV['REQUEST_URI']) && showMsg('refresh_limit');
}

ipControl($_PBENV['PB_IP']);

require_once PBDIGG_ROOT.'include/MySQL.class.php';
$DB = new MySQL($db_host, $db_username, $db_password, $db_name, $db_pconnect);
unset($db_password);

$customer = array('uid'=>0,'username'=>$common_message['anonymity'],'adminid'=>0,'groupid'=>5,'realgroup'=>0,'currency'=>0,'ucuid'=>0,'avatar'=>'');
//$pb_auth = gCookie('pb_auth');
$pb_auth = gCookie('pb_auth') ? gCookie('pb_auth') : (isset($pb_auth) ? $pb_auth : '');
if ($pb_auth)
{
	list($p_uid, $p_pw) = explode("\t", PDecode($pb_auth, $pb_sitehash));
	if (is_numeric($p_uid) && strlen($p_pw) == 32)
	{
		$customer = $DB->fetch_one("SELECT m.uid, m.username, m.password, m.email, m.adminid, m.groupid, m.publicemail, m.gender, m.regip, m.regdate, m.realgroup, m.postnum, m.commentnum, m.diggnum, m.burynum, m.currency, m.lastip, m.lastvisit, m.lastpost, m.lastcomment, m.lastupload, m.lastsearch, m.uploadnum, m.newmsg, m.friendnum, m.collectionnum, m.visitnum, m.ucuid, m.avatar, mx.qq, mx.msn, mx.site, mx.location, mx.birthday, mx.signature, mx.showsign, mx.ctsig FROM {$db_prefix}members m LEFT JOIN {$db_prefix}memberexp mx USING (uid) WHERE m.uid = ".(int)$p_uid);
		if (!$customer || pbNewPW($customer['password']) != $p_pw)
		{
			sCookie('pb_auth', '', -1);
			unset($customer, $pb_auth);
			showMsg('auth_error');
		}
		$customer['uid'] = $customer['safeuid'] = (int)$customer['uid'];
		$customer['groupid'] = (int)$customer['groupid'];
		$customer['adminid'] = (int)$customer['adminid'];
		$customer['newmsg'] && $customer['newmsg'] = '<span class="newmsg">'.$customer['newmsg'].'</span>';
		$customer['groupid'] == -1 && $customer['groupid'] = (int)$customer['realgroup'];
		$logStatus = 1;
	}
}

if ($pb_ucenable)
{
	require_once PBDIGG_ROOT.'include/uc.inc.php';
	$customer['safeuid'] = (int)$customer['ucuid'];
}

require_once PBDIGG_ROOT.'data/cache/cache_usergroup_'.$customer['groupid'].'.php';

if ($customer['adminid'] && ($customer['adminid'] == 1 || $customer['adminid'] == 2))
{
	require_once PBDIGG_CROOT.'cache_admingroup_'.$customer['adminid'].'.php';
	if (in_array($customer['uid'], $_siteFounder))
	{
		define('SUPERMANAGER', TRUE);
		$alloweditatc = $allowdelatc = $allowcheckatc = $allowlockatc = $allowmoveatc = $allowcopyatc = $allowtopatc = $allowcommend = $allowshield = $allowedip = $allowtitlestyle = 1;	
	}
}

!defined('SUPERMANAGER') && define('SUPERMANAGER', FALSE);

if (!$pb_ifopen && !SUPERMANAGER)
{
	clearcookie();
	showMsg($pb_whyclosed);
}
if (!$allowvisit && PB_PAGE != 'login')
{
	clearcookie();
	showMsg('member_visit_denied', 'login.php');
}

$tid = (int)$tid;
$cid = (int)$cid;
$p = (int)$p;

if ($pb_passport && $pb_passporttype == 'client')
{
	$loginurl = $pb_pclienturl.$pb_pclientlogin;
	$logouturl = $pb_pclienturl.$pb_pclientlogout;
	$regurl = $pb_pclienturl.$pb_pclientregister;
}
else
{
	$loginurl = $_PBENV['PB_URL'].'login.php';
	$logouturl = $_PBENV['PB_URL'].'login.php?action=logout&amp;verify='.urlHash('action=logout');
	$regurl = $_PBENV['PB_URL'].'register.php';
}

$rewrite_ext = array('html','htm','shtml','php','asp','jsp','cgi');
$pb_rewriteext = $rewrite_ext[$pb_rewriteext];
$diggbury = $pb_ifdigg || $pb_ifbury;

$customer['avatar'] = userFace($customer['avatar'], $customer['ucuid']);
$customer['uurl'] = userSpace($customer['uid'], $customer['ucuid']);

//$style= gCookie('style') ? gCookie('style') : (isset($_GET['style']) ? $_GET['style'] : (isset($_POST['style']) ? $_POST['style'] : $pb_style));
$styleid= isset($_GET['styleid']) ? $_GET['styleid'] : (gCookie('styleid') ? gCookie('styleid') : '');
if ($styleid && !preg_replace('~[-_a-z0-9]~i', '', $styleid) && 'admin' != strtolower($styleid) && @is_dir(PBDIGG_ROOT.'templates/'.$styleid))
{
	$tpl_dir = $pb_style = $styleid;
	sCookie('styleid', $styleid, 1);
}

require_once PBDIGG_CROOT.'cache_categories.php';
require_once PBDIGG_ROOT.'include/menu.class.php';
require_once PBDIGG_ROOT.'include/module.class.php';
require_once PBDIGG_ROOT.'include/transfer.class.php';
require_once PBDIGG_ROOT.'include/Cache.class.php';

$menu = new menu();
$module = new module();
$transfer = new transfer();
$Cache = new Cache();

sCookie('pb_lastvisit', $timestamp."\t".$_PBENV['REQUEST_URI']);
$verifyhash = getUserHash($customer['uid']);

//action_do('common');
//filter_do('common');

//common.inc.php结束

require_once PBDIGG_ROOT.'include/validate.func.php';
require_once PBDIGG_ROOT.'include/ubb.func.php';
@include_once PBDIGG_CROOT.'cache_specialtpl.php';

//zzcity add
if ($vercode!=$vercode2){
	echo('[err]invalid vercode[/err]');
	exit();
}



$post_message = loadLang('post');
$common_message += $post_message;

//!$pb_ifpost && !SUPERMANAGER && showMsg('post_thread_closed');

//!$allowpost && redirect('post_newthread_nopermission', $customer['groupid'] == '6' ? 'member.php' : 'login.php?forward=post.php');

//!in_array($action, array('add', 'edit')) && $action = 'add';

$action = 'add'; //zzcity add
/*
if ($pb_reposttime)
{
	$pb_lastpost = $customer['uid'] ?  $customer['lastpost'] : gCookie('pb_lastpost');
	$action != 'edit' && !$customer['adminid'] && $pb_lastpost && ($timestamp - $pb_lastpost < $pb_reposttime) && showMsg('post_flood_ctrl');
}
*/
list($title_min, $title_max) = explode("\t", $pb_titlelen);
list($content_min, $content_max) = explode("\t", $pb_contentlen);
$uploadattachment = $allowaupload && $pb_allowupload;
$uploadtopicimg = $uploadattachment && $pb_uploadtopicimg;
$rssurl = '';
			
//header('Cache-control: private, must-revalidate');
$currentCateData = $currentModuleData = $currentModuleObj = $postcontent = $moduleIdentifier = '';
//$moduleid = (int)$moduleid;
$moduleid = 1;

if ($cid && isset($_categories[$cid]))
{
	$currentCateData = &$_categories[$cid];
	//!$allowsort && !$currentCateData['status'] && showMsg('post_cate_newthread_nopermission');
	//!$logStatus && !$currentCateData['anonymity'] && showMsg('post_cate_contribute_nopermission');
	if (count($currentCateData['ttype']) == 1) $moduleid = $currentCateData['ttype'][0];
	if ($currentModuleData = checkModuleID($moduleid))
	{
	//article.class.php开始
	require_once PBDIGG_ROOT.'module/article/include/article_function.php';
	
	class article
	{
	
		var $DB = NULL;
		var $db_prefix;
	
		function article()
		{
			$this->__construct();
		}
	
		function __construct()
		{
			global $DB, $db_prefix;
			$this->DB = $DB;
			$this->db_prefix = $db_prefix;
		}
		/**
		 * 添加主题
		 * 
		 * @param array $article 递交参数信息
		 */
		function add(&$post)
		{
			global $pb_watertype, $pb_tftopicimg, $customer, $pb_reposttime, $action, $timestamp, $_PBENV, $pb_uploadfiletype, $uploadtype, $allowaupload, $pb_allowupload, $pb_uploadtopicimg, $pb_uploadmaxsize, $uploadmax,
				$pb_titlelen, $pb_contentlen, $verifyhash, $logStatus, $pb_gdcheck, $pb_attachnum, $pb_style, $allowurl, $allowtimestamp, $allowinitstatus,
				$allowhtml, $currentCateData, $cid, $moduleid, $pb_tcheck, $pb_anonnews, $pb_urlsaveimg, $pb_creditdb, $initdigg, $initbury, $inithit;
			
			$pb_lastpost = $logStatus ? $customer['lastpost'] : gCookie('pb_lastpost');
			//if (!$customer['adminid'] && $pb_reposttime && ($timestamp - $pb_lastpost < $pb_reposttime)) showMsg('post_flood_ctrl');
			//if (checkPost())
			//{
				$attachment_pattern = $attachment_replace = $attachments = array();
				$uid = (int)$customer['uid'];
				$timesession = (int)$_POST['timesession'];
			/*
				if (($pb_gdcheck & 4) && !ckgdcode($_POST['captcha']))
				{
					showMsg('checkcode_error');
				}
				if ($timesession)
				{
					$fsession = $this->DB->fetch_one("SELECT * FROM {$this->db_prefix}fsession WHERE uid = '$uid' AND timesession = '$timesession'");
					$fsession && $fsession['attachment'] && $attachments = unserialize($fsession['attachment']);
				}
	*/
				$title = checkTitle(HConvert(trim($_POST['title'])));
				$content = checkContent(trim(stripslashes($_POST['content'])));
				$tag = checkTag(HConvert(trim($_POST['tag'])));
				$summary = traceHtml(cutSpilthHtml(PBSubstr(preg_replace(array('~\[\/?[^\]]*\]~im','~(\r\n|\n\r|\r|\n){1,}~im'), '', (strip_tags($_POST['summary']) ? trim(stripslashes($_POST['summary'])) : $content)), 500)));
				safeConvert($summary);
				$author = $logStatus ? addslashes($customer['username']) : HConvert(trim($_POST['author']));
				$anonsite = (!$logStatus && substr($anonsite, 0, 7) == 'http://') ? HConvert($_POST['anonsite']) : '';
				if (strpos($content, 'embed') !== FALSE)
				{
					$content = ubb_deparse($content);
				}
				
				//!$allowhtml && safeConvert($content);
				$ifconvert = $content == conentUBB($content, 't', true) ? 0 : 1;
				$source = ($_POST['source'] && checkURL($_POST['source'])) ? HConvert($_POST['source']) : '';
				$linkhost = ($source && preg_match('~^(https?|ftp|gopher|news|telnet|mms|rtsp):\/\/([^\/]*)~i', $source, $linkhost)) ? addslashes(HConvert($linkhost[2])) : '';
				$ifcheck = (($pb_tcheck && !SUPERMANAGER) || ($pb_anonnews && !$uid)) ? '0' : '1';
	
				$tptime = intval(pStrToTime($_POST['postdate']));
				$postdate = ($allowtimestamp && $_POST['postdate'] && $tptime && ($tptime <= $timestamp)) ? $tptime : $timestamp;
				$digg = ($allowinitstatus && $_POST['digg'] >= 0 && $_POST['digg'] <= $initdigg) ? (int)$_POST['digg'] : 0;
				$bury = ($allowinitstatus && $_POST['bury'] >= 0 && $_POST['bury'] <= $initbury) ? (int)$_POST['bury'] : 0;
				$views = ($allowinitstatus && $_POST['views'] >= 0 && $_POST['views'] <= $inithit) ? (int)$_POST['views'] : 0;
				$pbrank = pbrank(0, $digg, $bury, $views, 0, $postdate);
				$ishtml = 0;
				$realurl = '';
				$keywords = '';
				if ($pb_urlsaveimg && preg_match('~<img.+?src=(?:["|\']?)((?:https?|ftp):\/\/(?:[-_a-z0-9\.\~!$&\'\(\)*+,;=:@|/]+|%[\dA-F]{2})+\.(?:gif|jpg|jpeg|bmp|png))(?:["|\']?)[^>]*?>~i', $content))
				{
					require_once PBDIGG_ROOT.'include/remotefile.func.php';
					$content = get_remote_files($content);
				}
	
				$topicimgsql = $topicimg  = $topicdata = $attanum = $attatid = '';
				$ainfo = array();
	
				if ($allowaupload && $pb_allowupload)
				{
					if ($pb_uploadtopicimg)
					{
						require_once PBDIGG_ROOT.'include/Upload.class.php';
						$Upload = new Upload();
						$attasize = ($pb_uploadmaxsize > $uploadmax) ? $uploadmax : $pb_uploadmaxsize;
						$topicdata = $Upload->moveFile('topic', '', array('gif','jpg','jpeg','bmp','png'), $attasize);
						if ($topicdata)
						{
							$topicimg = gdate($timestamp, 'ym').'/'.$topicdata[0][1].'.'.$topicdata[0][2].'|'.$topicdata[0][7][1].'|'.$topicdata[0][7][2].'|0';
						}
					}
					if ($attachments)
					{
						$uploaddir = getUploadDir();
						$filepath = PBDIGG_ATTACHMENT.$uploaddir.'/';
						foreach ($attachments as $k => $v)
						{
							PMove(PBDIGG_ATTACHMENT.'temp/'.$v[3].'.'.$v[4], $filepath.$v[3].'.'.$v[4]);
							$v[5] && PMove(PBDIGG_ATTACHMENT.'temp/thumb_'.$v[3].'.'.$v[4], $filepath.'thumb_'.$v[3].'.'.$v[4]);
							if ($pb_uploadtopicimg && $pb_tftopicimg && $v[6] && !$topicimg)
							{
								$topicimg = makeTopicImg($filepath.$v[3].'.'.$v[4]);
							}
							$this->DB->db_exec("INSERT INTO {$this->db_prefix}attachments (cid,tid,uid,filename,filetype,filesize,description,filepath,isimg,uploaddate,thumb) VALUES ('$cid',0,'$uid','".addslashes($v[0])."','".addslashes($v[1])."',".intval($v[2]).",'','".addslashes($uploaddir.'/'.$v[3].'.'.$v[4])."',".$v[6].",$timestamp,".$v[5].")");
							$aid = $this->DB->db_insert_id();
							$attachment_pattern[] = '[file='.$k.']';
							$attachment_replace[] = '[attachment='.$aid.']';
							$attatid .= $aid.',';
							$ainfo[$aid] = array('aid'=>$aid,'filename'=>$v[0],'filesize'=>$v[2],'filepath'=>$uploaddir.'/'.$v[3].'.'.$v[4],'filetype'=>$v[1],'isimg'=>$v[6],'thumbpath'=>($v[5] ? $uploaddir.'/thumb_'.$v[3].'.'.$v[4] : ''));
							++$attanum;
						}
						$this->DB->db_exec("DELETE FROM {$this->db_prefix}fsession WHERE uid = '$uid' AND timesession = '$timesession'");
					}
				}
	
				$this->DB->db_exec("INSERT INTO {$this->db_prefix}threads (tid, cid, author, uid, subject, contentlink, linkhost, topicimg, ifcheck, ifshield, iflock, topped, postdate, postip, digg, diggdate, bury, burydate, views, comments, commentdate, keywords, pbrank, commend, commendpic, first, module, realurl, ishtml, summary, titlecolor, titlestyle) VALUES (NULL, '$cid', '$author', '$uid', '$title', '$source', '$linkhost', '$topicimg', '$ifcheck', 0, 0, 0, '$postdate', '".$_PBENV['PB_IP']."', '$digg', '$postdate', '$bury', '$postdate', '$views', 0, '$postdate', '', '$pbrank', 0, '', 0, '$moduleid', '$realurl', '$ishtml', '".addslashes($summary)."', '', 0)");
				$tid = $this->DB->db_insert_id();
				$ainfo = $ainfo ? addslashes(serialize($ainfo)) : '';
	
				$this->DB->db_exec("INSERT INTO {$this->db_prefix}article (tid, content, ainfo, ifconvert, anonsite) VALUES ('$tid', '', '$ainfo', '$ifconvert', '$anonsite')");
				//attachments
				if ($attatid)
				{
					$content = str_replace($attachment_pattern, $attachment_replace, $content);
					$this->DB->db_exec("UPDATE {$this->db_prefix}attachments SET tid = '$tid' WHERE aid IN (".substr($attatid, 0, -1).")");
				}
	
				//tags
				if ($tag)
				{
					$tag = explode(',', $tag);
					$i = 1;
					foreach ($tag as $key => $value)
					{
						$rs = $this->DB->fetch_one("SELECT tagid, usenum, ifopen, ifsys, tagpic FROM {$this->db_prefix}tags WHERE tagname = '$value'");
						if ($rs)
						{
							if ($rs['ifopen'])
							{
								$this->DB->db_exec("UPDATE {$this->db_prefix}tags SET usenum = usenum + 1 WHERE tagid = ".$rs['tagid']);
								$tagid = $rs['tagid'];
								if (!$topicimg && $rs['tagpic'])
								{
									$topicimgsql = ", topicimg = '".addslashes($rs['tagpic'])."|1'";
									$topicimg = true;
								}
							}
							else
							{
								unset($tag[$key]);
								continue;
							}
						}
						else
						{
							$this->DB->db_exec("INSERT INTO {$this->db_prefix}tags (tagname, usenum, hits, ifopen, ifsys, tagpic) VALUES ('$value', 1, 0, 1, 0, '')");
							$tagid = $this->DB->db_insert_id();
						}
						$this->DB->db_exec("INSERT INTO {$this->db_prefix}tagcache (tagid, tid) VALUES ($tagid, $tid)");
						$keywords .= ($keywords ? ',' : '').$value;
						if ($i++ >= 5) break;
					}
				}
	
				$this->DB->db_exec("UPDATE {$this->db_prefix}threads SET keywords = '$keywords' $topicimgsql WHERE tid = '$tid'");
				$this->DB->db_exec("UPDATE {$this->db_prefix}article SET content = '".addslashes($content)."' WHERE tid = '$tid'");
				$this->DB->db_exec("UPDATE {$this->db_prefix}categories SET tnum = tnum + 1 WHERE cid = '$cid'");
				if ($uid)
				{
					$pb_creditdb = explode("\t", $pb_creditdb);
					$this->DB->db_exec("UPDATE {$this->db_prefix}members SET uploadnum = uploadnum + ".(int)$attanum.", postnum = postnum + 1, currency = currency + ".$pb_creditdb[0].", lastpost = '$timestamp' WHERE uid = '$uid'");
				}
				else
				{
					sCookie('pb_lastpost', $timestamp);
				}
				$this->DB->db_exec("UPDATE {$this->db_prefix}sitestat SET artnum = artnum + 1 WHERE id = 1");
				echo('[reply]tid='.$tid.'[/reply]');	//zzcity add
				
				//redirect(($ifcheck ? 'post_newthread_succeed' : 'post_newthread_unchecked'), 'show.php?tid='.$tid);
			/*}
			else
			{
				$post['attaexts'] = implode(',', $this->getUploadExts());
				$post['attanum'] = $this->getUploadNums();
				$post['attasize'] = ceil($this->getUploadSize() / 1024);
				$post['editor'] = getEditor(array(array('id'=>'content','type'=>'PBDigg','content'=>'','width'=>600,'height'=>300),array('id'=>'summary','type'=>'Basic','content'=>'','width'=>400,'height'=>200)));
			}*/
		}
		/**
		 * 删除文章
		 * 
		 * @param array $tid 文章ID数组
		 */
		function del($tid)
		{
			if (empty($tid)) return;
			!is_array($tid) && $tid = (array)$tid;
			$this->DB->db_exec("DELETE FROM {$this->db_prefix}article WHERE tid IN (".implode(',', $tid).")");
		}
		/**
		 * 编辑主题
		 * 
		 * @param int $tid 主题tid
		 */
		function edit(&$post, $tid)
		{
			global $pb_watertype, $pb_tftopicimg, $pb_topicstylesize, $pb_reeditlimit, $customer, $timestamp, $_PBENV, $pb_uploadfiletype, $uploadtype, $allowaupload, $pb_allowupload, $pb_uploadtopicimg, $pb_uploadmaxsize, $uploadmax,
				$pb_titlelen, $pb_contentlen, $verifyhash, $logStatus, $pb_gdcheck, $pb_attachnum, $pb_style, $allowurl, $allowtimestamp, $allowinitstatus,
				$allowhtml, $currentCateData, $cid, $moduleid, $pb_tcheck, $pb_anonnews, $pb_urlsaveimg, $pb_creditdb, $_attdir, $action, $initdigg, $initbury, $inithit, $timesession;
	
			$tid = (int)$tid;
			$uid = (int)$customer['uid'];
			
			$post = $this->DB->fetch_one("SELECT t.*, a.* FROM {$this->db_prefix}threads t LEFT JOIN {$this->db_prefix}article a USING (tid) WHERE t.tid = '$tid'");
			if (!$post) showMsg('post_thread_nonexistence');
			if (!$uid || (!$customer['adminid'] && $post['uid'] != $uid)) showMsg('post_edit_nopermission');
			if (!$customer['adminid'] && $pb_reeditlimit && $timestamp - $post['postdate'] > $pb_reeditlimit * 60) showMsg('post_edit_timelimit');
	
			if (checkPost())
			{
				if (($pb_gdcheck & 4) && !ckgdcode($_POST['captcha']))
				{
					showMsg('checkcode_error');
				}
	
				$updatesql = $updateinfosql = $keywords = '';
				$ainfo = $attachments = array();
				addS($post);
				$timesession = (int)$_POST['timesession'];
				
				if ($timesession)
				{
					$fsession = $this->DB->fetch_one("SELECT * FROM {$this->db_prefix}fsession WHERE uid = '$uid' AND timesession = '$timesession'");
					$fsession && $fsession['attachment'] && $attachments = unserialize($fsession['attachment']);
				}
	
				$title = checkTitle(HConvert(trim($_POST['title'])));
				$post['title'] != $title && $updatesql .= " subject = '$title',";
				$content = checkContent(trim(stripslashes($_POST['content'])));
				$tag = checkTag(HConvert(trim($_POST['tag'])));
				$summary = traceHtml(cutSpilthHtml(PBSubstr(preg_replace(array('~\[\/?[^\]]*\]~im','~(\r\n|\n\r|\r|\n){1,}~im'), '', (strip_tags($_POST['summary']) ? trim(stripslashes($_POST['summary'])) : $content)), 500)));
				safeConvert($summary);
				$summary != $post['summary'] && $updatesql .= " summary = '".addslashes($summary)."',";
				$anonsite = ($customer['adminid'] && substr($anonsite, 0, 7) == 'http://') ? HConvert($_POST['anonsite']) : '';
				$anonsite != $post['anonsite'] && $updateinfosql .= " anonsite = '$anonsite',";
				if (strpos($content, 'embed') !== FALSE)
				{
					$content = ubb_deparse($content);
				}
				!$allowhtml && safeConvert($content);
				$ifconvert = $content == conentUBB($content, 't', true) ? 0 : 1;
				$source = ($_POST['source'] && checkURL($_POST['source'])) ? HConvert($_POST['source']) : '';
				if ($post['source'] != $source)
				{
					$linkhost = ($source && preg_match('~^(https?|ftp|gopher|news|telnet|mms|rtsp):\/\/([^\/]*)~i', $source, $linkhost)) ? addslashes(HConvert($linkhost[2])) : '';
					$updatesql .= " contentlink = '$source', linkhost = '$linkhost',";
				}
				$ifcheck = (($pb_tcheck && !SUPERMANAGER) || ($pb_anonnews && !$customer['uid'])) ? '0' : '1';
	
				$tptime = intval(pStrToTime($_POST['postdate']));
				$allowtimestamp && $_POST['postdate'] && $tptime && ($tptime <= $timestamp) && $updatesql .= " postdate = '$tptime',";
				$allowinitstatus && $_POST['digg'] >= 0 && $_POST['digg'] <= $initdigg && $updatesql .= " digg = '".(int)$_POST['digg']."',";
				$allowinitstatus && $_POST['bury'] >= 0 && $_POST['bury'] <= $initbury && $updatesql .= " bury = '".(int)$_POST['bury']."',";
				$allowinitstatus && $_POST['views'] >= 0 && $_POST['views'] <= $inithit && $updatesql .= " views = '".(int)$_POST['views']."',";
	
				if ($post['keywords'] != $tag)
				{
					$tag = array_filter(array_unique(explode(',', $tag)));
					$oldtag = array();
					$query = $this->DB->db_query("SELECT tg.tagname FROM {$this->db_prefix}tags tg, {$this->db_prefix}tagcache tc WHERE tg.tagid = tc.tagid AND tc.tid = '$tid'");
					while ($rs = $this->DB->fetch_all($query))
					{
						$oldtag[] = $rs['tagname'];
					}
					if ($tag != $oldtag)
					{
						$deltags = $addtags = $rs = $t_tags = array();
						$tagid = $tagname = '';
						$deltags = array_diff($oldtag, $tag);
						$addtags = array_diff($tag, $oldtag);
						if ($deltags)
						{
							foreach ($deltags as $value)
							{
								$tagname .= "'".$value."',";
							}
							$query = $this->DB->db_query("SELECT tagid FROM {$this->db_prefix}tags WHERE tagname IN (".substr($tagname, 0, -1).")");
							while ($rs = $this->DB->fetch_all($query))
							{
								$tagid .= $rs['tagid'].',';
							}
							$tagid = substr($tagid, 0, -1);
							$this->DB->db_exec("DELETE FROM {$this->db_prefix}tagcache WHERE tagid IN ($tagid) AND tid = $tid");
							$this->DB->db_exec("UPDATE {$this->db_prefix}tags SET usenum = usenum - 1 WHERE tagid IN ($tagid)");
						}
						$t_tags = array_diff($oldtag, $deltags);
						$i = count($t_tags);
						$tagid = '';
						foreach ($addtags as $key => $value)
						{
							$rs = $this->DB->fetch_one("SELECT tagid, usenum, ifopen FROM {$this->db_prefix}tags WHERE tagname = '$value'");
							if ($rs)
							{
								if ($rs['ifopen'])
								{
									$this->DB->db_exec("UPDATE {$this->db_prefix}tags SET usenum = usenum + 1 WHERE tagid = ".$rs['tagid']);
									$tagid = $rs['tagid'];
								}
								else
								{
									unset($addtags[$key]);
									continue;
								}
							}
							else
							{
								$this->DB->db_exec("INSERT INTO {$this->db_prefix}tags (tagname, usenum, hits, ifopen, ifsys, tagpic) VALUES ('$value', 1, 0, 1, 0, '')");
								$tagid = $this->DB->db_insert_id();
							}
							$this->DB->db_exec("INSERT INTO {$this->db_prefix}tagcache (tagid, tid) VALUES ($tagid, $tid)");
							if ($i++ >= 5) break;
						}
						$updatesql .= " keywords = '".HConvert(addslashes(implode(',', array_merge($t_tags, $addtags))))."',";
					}
				}
	
				if ($pb_urlsaveimg && preg_match('~<img.+?src=(?:["|\']?)((?:https?|ftp):\/\/(?:[-_a-z0-9\.\~!$&\'\(\)*+,;=:@|/]+|%[\dA-F]{2})+\.(?:gif|jpg|jpeg|bmp|png))(?:["|\']?)[^>]*?>~i', $content))
				{
					require_once PBDIGG_ROOT.'include/remotefile.func.php';
					$content = get_remote_files($content);
				}
				$post['content'] != $content && $updateinfosql .= " content = '".addslashes($content)."',";
				$topicimg  = $topicdata = $attanum = $topicimgpath = $tmph = $tmpw = $topictype = $attatid = '';
				$deltopicimg = false;
				if ($allowaupload && $pb_allowupload)
				{
					if ($post['topicimg'])
					{
						list($topicimgpath,$tmph,$tmpw,$topictype) = explode('|', $post['topicimg']);
					}
					if ($pb_uploadtopicimg && isset($_FILES['topicimg']))
					{
						require_once PBDIGG_ROOT.'include/Upload.class.php';
						$Upload = new Upload();
						$attasize = ($pb_uploadmaxsize > $uploadmax) ? $uploadmax : $pb_uploadmaxsize;
						$topicdata = $Upload->moveFile('topic', '', array('gif','jpg','jpeg','bmp','png'), $attasize);
						if ($topicdata)
						{
							$topicimg = gdate($timestamp, 'ym').'/'.$topicdata[0][1].'.'.$topicdata[0][2].'|'.$topicdata[0][7][1].'|'.$topicdata[0][7][2].'|0';
							$updatesql .= " topicimg = '$topicimg',";
						}
						else
						{
							$updatesql .= " topicimg = '',";
							$deltopicimg = true;
						}
						$post['topicimg'] && $topictype == '0' && PDel(PBDIGG_ATTACHMENT.'topic/'.$topicimgpath);
					}
					if ($attachments)
					{
						$uploaddir = getUploadDir();
						$filepath = PBDIGG_ATTACHMENT.$uploaddir.'/';
						foreach ($attachments as $k => $v)
						{
							PMove(PBDIGG_ATTACHMENT.'temp/'.$v[3].'.'.$v[4], $filepath.$v[3].'.'.$v[4]);
							$v[5] && PMove(PBDIGG_ATTACHMENT.'temp/thumb_'.$v[3].'.'.$v[4], $filepath.'thumb_'.$v[3].'.'.$v[4]);
							if ($pb_uploadtopicimg && $pb_tftopicimg && $v[6] && !$topicimg && !$deltopicimg)
							{
								$topicimg = makeTopicImg($filepath.$v[3].'.'.$v[4]);
								$post['topicimg'] && $topictype == '0' && PDel(PBDIGG_ATTACHMENT.'topic/'.$topicimgpath);
								$updatesql .= " topicimg = '$topicimg',";
							}
							$this->DB->db_exec("INSERT INTO {$this->db_prefix}attachments (cid,tid,uid,filename,filetype,filesize,description,filepath,isimg,uploaddate,thumb) VALUES ('$cid','$tid','$uid','".addslashes($v[0])."','".addslashes($v[1])."',".intval($v[2]).",'','".addslashes($uploaddir.'/'.$v[3].'.'.$v[4])."',".$v[6].",$timestamp,".$v[5].")");
							$aid = $this->DB->db_insert_id();
							$attachment_pattern[] = '[file='.$k.']';
							$attachment_replace[] = '[attachment='.$aid.']';
							$attatid .= $aid.',';
							$ainfo[$aid] = array('aid'=>$aid,'filename'=>$v[0],'filesize'=>$v[2],'filepath'=>$uploaddir.'/'.$v[3].'.'.$v[4],'filetype'=>$v[1],'isimg'=>$v[6],'thumbpath'=>($v[5] ? $uploaddir.'/thumb_'.$v[3].'.'.$v[4] : ''));
							++$attanum;
						}
						if ($ainfo)
						{
							$post['ainfo'] && $ainfo = $ainfo + unserialize(stripslashes($post['ainfo']));
							$updateinfosql .= " ainfo = '".addslashes(serialize($ainfo))."',";
							$updateinfosql = str_replace($attachment_pattern, $attachment_replace, $updateinfosql);
							$this->DB->db_exec("UPDATE {$this->db_prefix}members SET uploadnum = uploadnum + $attanum WHERE uid = '$uid'");
						}
						$this->DB->db_exec("DELETE FROM {$this->db_prefix}fsession WHERE uid = '$uid' AND timesession = '$timesession'");
					}
				}
				$this->DB->db_exec("UPDATE {$this->db_prefix}threads SET $updatesql ifcheck = $ifcheck WHERE tid = '$tid'");
				$this->DB->db_exec("UPDATE {$this->db_prefix}article SET $updateinfosql ifconvert = '$ifconvert' WHERE tid = '$tid'");
				redirect(($ifcheck ? 'post_newthread_succeed' : 'post_newthread_unchecked'), 'show.php?tid='.$tid);
			}
			else
			{
				global $common_message;
				$attachments = '';
	
				$post['attaexts'] = implode(',', $this->getUploadExts());
				$post['attanum'] = $this->getUploadNums();
				$post['attasize'] = ceil($this->getUploadSize() / 1024);
				if ($post['ifconvert'])
				{
					$post['content'] = ubb_parse($post['content']);
				}
				$post['editor'] = getEditor(array(array('id'=>'content','type'=>'PBDigg','content'=>$post['content'],'width'=>600,'height'=>300),array('id'=>'summary','type'=>'Basic','content'=>$post['summary'],'width'=>400,'height'=>200)));
				$post['postdate'] = gdate($post['postdate'], 'Y-m-d H:i:s');
				if ($post['ainfo'])
				{
					$post['ainfo'] = unserialize($post['ainfo']);
					foreach ($post['ainfo'] as $k => $v)
					{
						$attachments .= '<div class="attachment_show" id="e_attachment_'.$k.'"><img src="'.$_PBENV['PB_URL'].'attachments/'.$v['filepath'].'" /><span><a onclick="insertatt(\'[attachment='.$k.']\')" href="javascript:void(0);">'.$common_message['insert'].'</a> | <a onclick="supdelatt('.$k.')" href="javascript:void(0);">'.$common_message['delete'].'</a></span></div>';
					}
				}
				$post['attachments'] = &$attachments;
				if ($post['topicimg'])
				{
					list($topicimg,,,) = explode('|', $post['topicimg']);
					$post['topicimg'] = $_PBENV['PB_URL'].$_attdir.'/topic/'.$topicimg;
				}
			}
		}
	
		function copy($oldtid, $newtid, $targetcid, $replace_atta)
		{
			$data = $this->DB->fetch_one("SELECT content, ainfo, ifconvert, anonsite FROM {$this->db_prefix}article WHERE tid = '$oldtid'");
			$newainfo = array();
			if ($data)
			{
				if ($data['ainfo'] && $replace_atta)
				{
					$ainfo = unserialize($data['ainfo']);
					foreach ($replace_atta as $k => $v)
					{
						$ainfo[$k]['aid'] = $v;
						$newainfo[$v] = $ainfo[$k];
					}
				}
				$replace_atta && $data['content'] = preg_replace('~\[attachment=(\d+?)\]~ie', "replace_atta(\\1, \$replace_atta)", $data['content']);
				$newainfo = $newainfo ? addslashes(serialize($newainfo)) : '';
				$this->DB->db_exec("INSERT INTO {$this->db_prefix}article (tid, content, ainfo, ifconvert, anonsite) VALUES ('$newtid', '".addslashes($data['content'])."', '$newainfo', '".$data['ifconvert']."', '".addslashes($data['anonsite'])."')");
			}
		}
	
		function move($tid, $oldcid, $targetcid)
		{
			return;
		}
	
		/**
		 * 显示列表
		 * 
		 * @param int $cid 分类ID
		 */
		function category($cid, &$article)
		{
			//文章模型无需实现该方法
		}
		/**
		 * 显示主题
		 * 
		 * @param int $tid 主题tid
		 */
		function show($tid, &$article)
		{
			global $common_message, $pb_copyctrl, $pb_taglink, $pb_sitedir, $pb_previewsize, $pb_attoutput, $pb_outputmaxsize, $_attdir, $pb_autoshield, $customer;
			$tinfo = $this->DB->fetch_one("SELECT * FROM {$this->db_prefix}article WHERE tid = '$tid'");
			if (!$tinfo) return;
			$article += $tinfo;
			unset($tinfo);
	
			$contentShield = $shieldNotice = '';
			if ($article['ifshield'] || $article['groupid'] == 3 && $pb_autoshield)
			{
				if ($customer['adminid'])
				{
					$shieldNotice = articleShield('show_admin_shield');
				}
				else
				{
					$article['content'] = articleShield($article['groupid'] == 3 ? 'show_auto_shield' : 'show_content_shield');
	//				$article['subject'] = articleShield('show_title_shield');
					$article['altsubject'] = strip_tags($article['subject']);
					$contentShield = true;
				}
			}
			if (!$contentShield)
			{
				$article['content'] = $shieldNotice.$article['content'];
				//ubb
				$article['ifconvert'] && $article['content'] = conentUBB($article['content']);
				if($pb_taglink && $article['keywords'])
				{
					global $_tagkeywords;
					$_tagkeywords = $article['keywords'];
					$article['content'] = preg_replace('~(^|>)([^<]+)(?=<|$)~ie',"replace_tag('\\1', '\\2')", $article['content']);
	//				foreach ($article['keywords'] as $v)
	//				{
	//					$v && $article['content'] = preg_replace("~(?<=[\s\"\]>()]|[\x7f-\xff]|^)(".preg_quote($v,'~').")([.,:;-?!()\s\"<\[]|[\x7f-\xff]|$)~siU", "<a href=\"".$pb_sitedir."index.php?tag=\\1\"><span style=\"text-decoration:underline;\">\\1</span></a>\\2", $article['content']);
	//				}
				}
				$pb_copyctrl && $article['content'] = preg_replace('~<br \/>~eis', 'contentCopyCtrl()', $article['content']);
			}
			$article['anonsite'] && $article['anonsite'] = '<a href="'.((strtolower(substr($article['anonsite'], 0, 7)) == 'http://') ? $article['anonsite'] : 'http://'.$article['anonsite']).'" target="_blank" class="anonsite">'.HConvert($article['author']).'</a>';
	
			//attachment
			$article['attach_image'] = $article['attach_other'] = array();
			list ($pb_previewh, $pb_previeww) = explode("\t", $pb_previewsize);
			$article['preview_image'] = $pb_previeww ? "\$('#content img').each(function(){if (this.width > {$pb_previeww}){this.height=parseInt(this.height*{$pb_previeww}/this.width);this.width={$pb_previeww};}})" : '';
			if ($article['ainfo'])
			{
				global $ainfo, $onload;
				$ainfo = unserialize($article['ainfo']);
	
				if ($pb_previewh || $pb_previeww)
				{
					$onload = ' onload="';
					$pb_previeww && $onload .= "if(this.width>'".$pb_previeww."')this.width='".$pb_previeww."';";
					$pb_previewh && $onload .= "if(this.height>'".$pb_previewh."')this.height='".$pb_previewh."';";
					$onload .= '"';
				}
				require_once PBDIGG_ROOT.'include/attachment.func.php';
				$article['content'] = attachment($article['content']);
				foreach ($ainfo as $v)
				{
					$extension = strtolower(Fext($v['filename']));
					$isimg = ($v['isimg'] && in_array($extension, array('jpg', 'jpeg', 'gif', 'png', 'bmp'))) ? true : false;
					
					$url = $pb_sitedir.(($isimg || $pb_attoutput || ($pb_outputmaxsize && $v['filesize'] > $pb_outputmaxsize)) ? $_attdir.'/'.$v['filepath'] : 'attachments.php?aid='.$v['aid']);
					$thumburl = $v['thumbpath'] ? $pb_sitedir.$_attdir.'/'.$v['thumbpath'] : $url;
					if ($isimg)
					{
						$article['attach_image'][] = array('thumburl'=>$thumburl,'url'=>$url,'filename'=>htmlspecialchars($v['filename']),'onload'=>$onload);
					}
					else
					{
						$article['attach_other'][] = array('url'=>$url,'filename'=>htmlspecialchars($v['filename']),'icon'=>attachtype($extension));
					}
				}
			}
		}
		/**
		 * 删除分类主题
		 * 
		 * @param array $cid 分类cid数组
		 */
		function delCateArticle($cid)
		{
			if (empty($cid)) return;
			!is_array($cid) && $cid = (array)$cid;
			$query = $this->DB->db_query("SELECT tid FROM {$this->db_prefix}threads WHERE cid IN (".implode(',', $cid).")");
			$tids = '';
			while ($rs = $this->DB->fetch_all($query))
			{
				$tids .= ($tids ? ',' : '').$rs['tid'];
			}
			$tids && $this->DB->db_exec("DELETE FROM {$this->db_prefix}article WHERE tid IN ($tids)");
		}
	
		function delattachment($tid, $aid)
		{
			$article = $this->DB->fetch_one("SELECT ainfo FROM {$this->db_prefix}article WHERE tid = '$tid'");
			if ($article && $article['ainfo'])
			{
				$ainfo = unserialize($article['ainfo']);
				unset($ainfo[$aid]);
				$ainfo = $ainfo ? addslashes(serialize($ainfo)) : '';
				$this->DB->db_exec("UPDATE {$this->db_prefix}article SET ainfo = '$ainfo' WHERE tid = '$tid'");
			}
		}
		
		/**
		 * 返回允许上传的附件类型
		 * @return Array 附件后缀数组
		 */
		function getUploadExts($type = 'attachment')
		{
			global $pb_uploadfiletype, $uploadtype;
			$exts = array();
			if ($type == 'commend') $uploadtype = 'jpg,jpeg,gif,png,bmp';
			if ($pb_uploadfiletype && $uploadtype)
			{
				$haystack = ','.strtolower($pb_uploadfiletype).',';
				$exts = explode(',', strtolower($uploadtype));
				foreach ($exts as $key => $ext)
				{
					if (strpos($haystack, ','.$ext.',') === FALSE)
					{
						unset($exts[$key]);
					}
				}
			}
			return $exts;
		}
		/**
		 * 允许上传的最大附件尺寸
		 */
		function getUploadSize()
		{
			global $pb_uploadmaxsize, $uploadmax;
			return ($pb_uploadmaxsize > $uploadmax) ? $uploadmax : $pb_uploadmaxsize;
		}
		/**
		 * 单次上传附件数
		 */
		function getUploadNums($type = 'attachment')
		{
			global $pb_attachnum;
			return $type == 'attachment' ? intval($pb_attachnum) : '1';
		}
		
		/**
		 * 模型变量调用接口
		 * @param string $field 字段名
		 * @param string $table 表名
		 * @param rel $var 引用参数
		 */
		function transfer($field, $table, &$var)
		{
	//		$this->${'transfer_'.$field}($var);
		}
	}	
	//article.class.php结束
	
	
	
		$post = array();
//		$currentModuleObj = $module->getModuleObject($moduleid);
//		!$currentModuleObj && showMsg('illegal_request');
//		$moduleIdentifier = $currentModuleData['identifier'];
//		$currentModuleObj->$action($post, $tid);

		$currentModuleObj= new article;
		$moduleIdentifier = 'article';
		$currentModuleObj->add($post);
		
	}
	else
	{
		/*
		$moduleList = array();
		$modules = $module->getSingleModuleData();
		foreach ($modules as $k => $v)
		{
			in_array($k, $currentCateData['ttype']) && $v[$v['identifier'].'_status'] && $moduleList[] = $v;
		}
		empty($moduleList) && showMsg('post_module_error');
		*/
		showMsg('post_module_error');//zzcity add
	}
}
else
{
	/*
	$cid = $moduleid = '';
	$chosecate = '';
	unlimitBlockMenu($chosecate);
	*/
	echo('[err]invalid cid[/err]');	//zzcity add
	exit();
	
}


function unlimitBlockMenu(&$menu, $start = 0)
{
	$child = getChild($start);
	$menu .= '<ul>';
	foreach ($child as $k => $v)
	{
		$menu .= '<li><a href="post.php?cid='.$k.'">'.$v['name'].'</a>';
		if (getChild($k))
		{
			unlimitBlockMenu($menu, $k);
		}
		$menu .= '</li>';
	}
	$menu .= '</ul>';
}
function getChild($cid)
{
	global $_categories;
	$child = array();
	foreach($_categories as $k => $v)
	{
		$v['cup'] == $cid && $child[$k] = $v;
	}
	return $child;
}
function checkModuleID($moduleid)
{
	global $module, $currentCateData, $currentModuleData;
	if ($moduleid && $module->checkModuleID($moduleid) && in_array($moduleid, $currentCateData['ttype']))
	{
		$currentModuleData = $module->getSingleModuleData($moduleid);
		if ($currentModuleData[$currentModuleData['identifier'].'_status']) return $currentModuleData;
	}
}

//$pb_seotitle = $post_message['post_'.$action.'_title'];

//require_once pt_fetch('post');

//PBOutPut();

?>
