<?php
if (!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
if(!defined('DISCUZ_VERSION')) {
	require_once './source/discuz_version.php';
}
if (!isset($_G['cache']['csdn123_news_send'])) {
	loadcache('csdn123_news_send');
}
if(empty($_G['cache']['csdn123_news_send']))
{
	$zhiwu55com_chrome_info=lang('plugin/csdn123_news', 'zhiwu55_com_09');
	echo $zhiwu55com_chrome_info;
	exit;

} else {

	$zhiwu55_data = dunserialize($_G['cache']['csdn123_news_send']);

}
if(empty($zhiwu55_data['auth_admin']))
{
	$zhiwu55com_chrome_info=lang('plugin/csdn123_news', 'zhiwu55_com_11');
	echo $zhiwu55com_chrome_info;
	exit;
}
if(empty($zhiwu55_data['post_safecode']))
{
	$zhiwu55com_chrome_info=lang('plugin/csdn123_news', 'zhiwu55_com_02');
	echo $zhiwu55com_chrome_info;
	exit;
}
$hzw_safecode = $zhiwu55_data['post_safecode'];
require './source/plugin/csdn123_news/function_common.php';
if(!empty($_GET['zhiwu55com_result']) && $_GET['zhiwu55com_result']=='yes')
{

	if(stripos($zhiwu55_data['auth_admin'],',')==false && $_G['uid']!=$zhiwu55_data['auth_admin'])
	{

		$zhiwu55com_chrome_info=lang('plugin/csdn123_news', 'zhiwu55_com_11');
		echo $zhiwu55com_chrome_info;
		exit;

	} else {

		$auth_admin_arr=explode(',',$zhiwu55_data['auth_admin']);
		if(!in_array($_G['uid'],$auth_admin_arr))
		{
			$zhiwu55com_chrome_info=lang('plugin/csdn123_news', 'zhiwu55_com_11');
			echo $zhiwu55com_chrome_info;
			exit;
		}

	}
	require_once './source/function/function_forum.php';
	if(!empty($_GET['zhiwu55com_result_smt']) && $_GET['zhiwu55com_result_smt']=='yes' && $_GET['formhash']==FORMHASH)
	{
		$just_preview_url = send_thread_zhiwu55fun($_GET);
		if(stripos($just_preview_url,'http')==0)
		{
			header("Location:".$just_preview_url);

		} else {

			echo $just_preview_url;

		}
		exit;
	}
	require_once libfile('function/forumlist');
	require_once libfile('function/portalcp');
	require_once libfile('function/group');
	$grouplistArr=grouplist('displayorder',false,100);
	$views=rand($zhiwu55_data['min_views'],$zhiwu55_data['max_views']);
	if(!empty($zhiwu55_data['fid']))
	{
		$typeclassArr = C::t('forum_threadclass')->fetch_all_by_fid($zhiwu55_data['fid']);
	}
	$replayArr=explode('QQ:155120699',$zhiwu55_data['firstPost']);
	$replayArr=array_slice($replayArr,0,$zhiwu55_data['replaynum']);
	$replayArrCount=count($replayArr);
	$first_uid = getRndUid_zhiwu55fun(30);
	$reply_uid = getRndUid_zhiwu55fun(30);
	$zhiwu55_data['title'] = str_replace('"','&quot;',$zhiwu55_data['title']);
	include template('csdn123_news:zhiwu55com_result');
	exit;
}
if($hzw_safecode!=$_GET['hzw_safecode'])
{
	$zhiwu55com_chrome_info=lang('plugin/csdn123_news', 'zhiwu55_com_03');
	echo $zhiwu55com_chrome_info;
	exit;

}
if(empty($_GET['zhiwu55_content']))
{
	$zhiwu55com_chrome_info=lang('plugin/csdn123_news', 'zhiwu55_com_01');
	echo $zhiwu55com_chrome_info;
	exit;

} else {

	$zhiwu55_content = $_GET['zhiwu55_content'];
	$zhiwu55_content = trim($zhiwu55_content);
	$appid=wwwzhiwu55com_appid();
	if(empty($_G['cache']['plugin']['csdn123_news']['zhiwu55_appsecret']))
	{

		$zhiwu55com_authorization_arr=array();
		if (!isset($_G['cache']['zhiwu55com_authorization'])) {
			loadcache('zhiwu55com_authorization');
		}
		if(!empty($_G['cache']['zhiwu55com_authorization']))
		{
			$zhiwu55com_authorization_arr = $_G['cache']['zhiwu55com_authorization'];
		}
		$zhiwu55_appsecret=$zhiwu55com_authorization_arr['hzw_appsecret'];

	} else {

		$zhiwu55_appsecret=$_G['cache']['plugin']['csdn123_news']['zhiwu55_appsecret'];

	}
	if(empty($zhiwu55_appsecret))
	{
		$zhiwu55com_chrome_info=lang('plugin/csdn123_news', 'zhiwu5comcn_10');
		echo $zhiwu55com_chrome_info;
		exit;
	}
	$zhiwu55_data_post=array();
	$zhiwu55_data_post['hzw_appid']=$appid;
	$zhiwu55_data_post['t']=time();
	$zhiwu55_data_post['htmlcode']=$zhiwu55_content;
	$zhiwu55_data_post['chinese_encoding']=$zhiwu55_data['chinese_encoding'];
	$signArr=array();
	$signArr['hzw_appid']=$appid;
	$signArr['appsecret']=$zhiwu55_appsecret;
	$signArr['chinese_encoding']=$zhiwu55_data['chinese_encoding'];
	$signArr['t']=$zhiwu55_data_post['t'];
	$sign=hzw_sign($signArr);	
	$zhiwu55_data_post['sign']=$sign;
	$zhiwu55_result_str = dfsockopen('http://www.zhiwu55.com/catch_data/csdn123_news.php',0,$zhiwu55_data_post);
	$zhiwu55_result_str = trim($zhiwu55_result_str);
	if($zhiwu55_result_str=='parameter_error') {

		$zhiwu55com_chrome_info=lang('plugin/csdn123_news', 'zhiwu5comcn_01');
		echo $zhiwu55com_chrome_info;
		exit;

	} elseif($zhiwu55_result_str=='time_is_expire') {

		$zhiwu55com_chrome_info=lang('plugin/csdn123_news', 'zhiwu5comcn_02');
		echo $zhiwu55com_chrome_info;
		exit;

	} elseif($zhiwu55_result_str=='hzw_appid_empty') {

		$zhiwu55com_chrome_info=lang('plugin/csdn123_news', 'zhiwu5comcn_03');
		echo $zhiwu55com_chrome_info;
		exit;

	} elseif($zhiwu55_result_str=='frequently') {

		$zhiwu55com_chrome_info=lang('plugin/csdn123_news', 'zhiwu5comcn_04');
		echo $zhiwu55com_chrome_info;
		exit;

	} elseif($zhiwu55_result_str=='htmlcode_empty') {
		
		$zhiwu55com_chrome_info=lang('plugin/csdn123_news', 'zhiwu5comcn_17');
		echo $zhiwu55com_chrome_info;
		exit;
		
	} elseif($zhiwu55_result_str=='sign_error') {

		$zhiwu55com_chrome_info=lang('plugin/csdn123_news', 'zhiwu5comcn_05');
		echo $zhiwu55com_chrome_info;
		exit;

	}
	
	$zhiwu55_result_str = base64_decode($zhiwu55_result_str);
	$zhiwu55_content_arr = dunserialize($zhiwu55_result_str);
	if(!is_array($zhiwu55_content_arr) || count($zhiwu55_content_arr)<2)
	{
		$zhiwu55com_chrome_info=lang('plugin/csdn123_news', 'zhiwu55_com_05');
		echo $zhiwu55com_chrome_info;
		exit;
	}
	$zhiwu55_content_arr['title']=diconv($zhiwu55_content_arr['title'],'UTF-8');
	$zhiwu55_content_arr['firstPost']=diconv($zhiwu55_content_arr['firstPost'],'UTF-8');
	$zhiwu55_content_arr['tags_word']=diconv($zhiwu55_content_arr['tags_word'],'UTF-8');

}
if(empty($zhiwu55_content_arr['title']) && empty($zhiwu55_content_arr['firstPost']))
{
	$zhiwu55_n=0;
	foreach($zhiwu55_content_arr as $zhiwu55_item)
	{
		$source_link = daddslashes($zhiwu55_item);
		$chk = DB::fetch_first("SELECT * FROM %t WHERE source_link=%s LIMIT 1",array('zhiwu55cn_news',$source_link));
		if (empty($chk) && strlen($source_link)>10)
		{
			$newsData=array();
			$newsData['source_link'] = $source_link;
			$newsData['title'] = $source_link;
			DB::insert('zhiwu55cn_news',$newsData);
			$zhiwu55_n++;
		}
	}
	$showInfo = lang('plugin/csdn123_news', 'zhiwu55_com_08');
	$showInfo = str_replace('zhiwu55_n',$zhiwu55_n,$showInfo);
	echo $showInfo;
	exit;

} else {

	$source_link = $zhiwu55_content_arr['topHref'];
	$chk = DB::fetch_first("SELECT * FROM %t WHERE source_link=%s LIMIT 1",array('zhiwu55cn_news',$source_link));
	if(empty($chk['browse_url']))
	{
		if(empty($zhiwu55_content_arr['news_id']))
		{

			$zhiwu55_data=array_merge($zhiwu55_data,$zhiwu55_content_arr);
			$csdn123_news_send = serialize($zhiwu55_data);
			savecache('csdn123_news_send',$csdn123_news_send);
			echo $_G['siteurl'].'plugin.php?id=csdn123_news&zhiwu55com_result=yes';

		} else {

			require_once './source/function/function_forum.php';
			$zhiwu55_data=array_merge($zhiwu55_data,$zhiwu55_content_arr);
			$zhiwu55_data['views']=rand($zhiwu55_data['min_views'],$zhiwu55_data['max_views']);
			$replayArr=explode('QQ:155120699',$zhiwu55_data['firstPost']);
			$replayArr=array_slice($replayArr,0,$zhiwu55_data['replaynum']);
			$zhiwu55_data['replayArrCount']=count($replayArr);
			foreach($replayArr as $zhiwu55_key=>$zhiwu55_value)
			{
				$zhiwu55_data['firstPost'.$zhiwu55_key]=trim($zhiwu55_value);
			}
			$just_preview_url = send_thread_zhiwu55fun($zhiwu55_data);
			echo $just_preview_url;

		}

	} else {

		echo $chk['browse_url'];

	}

}