<?php
if (!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
if (!isset($_G['cache']['plugin'])) {
	loadcache('plugin');
}
$zhiwu55_dingshi = $_G['cache']['plugin']['csdn123_news']['zhiwu55_dingshi'];
if ($zhiwu55_dingshi != 1) {
	echo '// ' . lang('plugin/csdn123_news', 'disable_auto');
	exit;
}
if(!defined('DISCUZ_VERSION')) {
	require_once './source/discuz_version.php';
}
require './source/plugin/csdn123_news/function_common.php';
$hzw_appid = wwwzhiwu55com_appid();
$zhiwu55_cron = DB::fetch_first("SELECT * FROM %t ORDER BY catchtime ASC LIMIT 1",array('zhiwu55cn_cron'));
if (is_numeric($zhiwu55_cron['catchtime'])) {
	$zhiwu55_diffTime = time() - $zhiwu55_cron['catchtime'];
	if ($zhiwu55_diffTime < 300) {
		echo '// ' .  lang('plugin/csdn123_news', 'cron_interval_seconds');
		exit;
	}
}
$wwwzhiwu55com_chk = DB::fetch_first('SELECT push_date FROM %t ORDER BY push_date DESC LIMIT 1',array('zhiwu55cn_news'));
if (is_numeric($wwwzhiwu55com_chk['push_date'])) {
	$zhiwu55_diffTime2 = time() - $wwwzhiwu55com_chk['push_date'];
	if ($zhiwu55_diffTime2 < 300) {
		echo '// ' .  lang('plugin/csdn123_news', 'cron_interval_seconds');
		exit;
	}
}
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))
{
	echo '// ' .  lang('plugin/csdn123_news', 'zhiwu5comcn_10');
	exit;
}
$zhiwu55_firt_news = DB::fetch_first("SELECT * FROM %t WHERE tid_aid=0 AND release_now='yes' AND del=0 AND forum_portal IS NOT NULL ORDER BY ID ASC LIMIT 1",array('zhiwu55cn_news'));
if (empty($zhiwu55_cron) == false && empty($zhiwu55_firt_news) == true) {

	DB::query('UPDATE %t SET catchnum=catchnum+1,catchtime=%d WHERE ID=%d',array('zhiwu55cn_cron',TIMESTAMP,$zhiwu55_cron["ID"]));
	$keyword=$zhiwu55_cron['keyword'];
	$keyword=trim($keyword);
	$keyword=diconv($keyword,CHARSET,'UTF-8');
	$signArr=array();
	$signArr['hzw_appid']=$hzw_appid;
	$signArr['appsecret']=$zhiwu55_appsecret;
	$signArr['keyword']=$keyword;
	$signArr['t']=time();
	$sign=hzw_sign($signArr);
	$dataUrl="http://www.zhiwu55.com/catch_data/catch_keyword_content_list.php?hzw_appid=".$hzw_appid.'&keyword='.urlencode($keyword).'&t='.$signArr['t'].'&sign='.$sign;
	$resultStr=dfsockopen($dataUrl);
	if($resultStr=='parameter_error') {

		echo '//'.lang('plugin/csdn123_news', 'zhiwu5comcn_01');
		exit;

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

		echo '//'.lang('plugin/csdn123_news', 'zhiwu5comcn_02');
		exit;

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

		echo '//'.lang('plugin/csdn123_news', 'zhiwu5comcn_03');
		exit;

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

		echo '//'.lang('plugin/csdn123_news', 'zhiwu5comcn_04');
		exit;

	} elseif($resultStr=='sign_error') {

		echo '//'.lang('plugin/csdn123_news', 'zhiwu5comcn_05');
		exit;

	} elseif(strlen($resultStr)<20) {

		echo '//'.lang('plugin/csdn123_news', 'collection_result_null');
		exit;
	}
	$resultStr=trim($resultStr);
	$resultStr=base64_decode($resultStr);
	$resultArr=dunserialize($resultStr);
	if(empty($resultArr) || !is_array($resultArr))
	{
		echo '//'.lang('plugin/csdn123_news', 'collection_result_null');
		exit;
	}
	foreach($resultArr as $resultLinkItem)
	{
		$newsArr=array();
		$title=diconv($resultLinkItem['title'],'UTF-8',CHARSET);
		$title=str_replace('"','&quot;',$title);
		$newsArr['title']=daddslashes($title);
		$newsArr['source_link']=daddslashes($resultLinkItem['fromurl']);
		$newsArr['forum_portal']=daddslashes($zhiwu55_cron['forum_portal']);
		$newsArr['fid']=intval($zhiwu55_cron['fid']);
		$newsArr['threadtypeid']=intval($zhiwu55_cron['threadtypeid']);
		$newsArr['portal_catid']=intval($zhiwu55_cron['portal_catid']);
		$first_uid_arr=explode(',',$zhiwu55_cron['first_uid']);
		shuffle($first_uid_arr);
		$newsArr['first_uid']=$first_uid_arr[0];
		$newsArr['reply_uid']=daddslashes($zhiwu55_cron['reply_uid']);
		$newsArr['display_link']=intval($zhiwu55_cron['display_link']);
		$newsArr['pseudo_original']=intval($zhiwu55_cron['pseudo_original']);
		$newsArr['chinese_encoding']=intval($zhiwu55_cron['chinese_encoding']);
		$newsArr['views']=rand($zhiwu55_cron['min_views'],$zhiwu55_cron['max_views']);
		$newsArr['group_fid']=intval($zhiwu55_cron['group_fid']);
		$newsArr['intval_time']=intval($zhiwu55_cron['intval_time']);
		$newsArr['filter_image']=intval($zhiwu55_cron['filter_image']);
		$newsArr['image_center']=intval($zhiwu55_cron['image_center']);
		$newsArr['hzw_remotefile']=intval($zhiwu55_cron['hzw_remotefile']);
		$newsArr['replaynum']=intval($zhiwu55_cron['replaynum']);
		$newsArr['release_now']=daddslashes($zhiwu55_cron['release_now']);
		if($zhiwu55_cron['sendtime']==1)
		{
			$newsArr['send_datetime']=time();
		} else {
			$newsArr['send_datetime']=$resultLinkItem['sendtime'];
		}
		$chk = DB::fetch_first("SELECT * FROM %t WHERE source_link=%s LIMIT 1",array('zhiwu55cn_news',$newsArr['source_link']));
		if (empty($chk) || count($chk) == 0) {
			DB::insert('zhiwu55cn_news', $newsArr);
		}
	}
	echo '// ' .  lang('plugin/csdn123_news', 'cron_collection_keyword');
	exit;

} elseif (empty($zhiwu55_firt_news) == false) {

	DB::update('zhiwu55cn_news', array('del'=>1,'push_date'=>TIMESTAMP), array('ID' => $zhiwu55_firt_news['ID']));
	require_once './source/function/function_forum.php';
	$signArr=array();
	$signArr['hzw_appid']=$hzw_appid;
	$signArr['appsecret']=$zhiwu55_appsecret;
	$signArr['url']=$zhiwu55_firt_news['source_link'];
	$signArr['chinese_encoding']=$zhiwu55_firt_news['chinese_encoding'];
	$signArr['t']=time();
	$sign=hzw_sign($signArr);
	$title=$zhiwu55_firt_news['title'];
	$title=diconv($title,CHARSET,'UTF-8');
	$dataUrl="http://www.zhiwu55.com/catch_data/show_content_202005.php?title=".urlencode($title)."&hzw_appid=".$hzw_appid.'&url='.urlencode($zhiwu55_firt_news['source_link']).'&chinese_encoding='.$zhiwu55_firt_news['chinese_encoding'].'&sign='.$sign.'&t='.$signArr['t'];
	$htmlcodeStr=dfsockopen($dataUrl);
	if($htmlcodeStr=='parameter_error') {

		echo '// ' .  lang('plugin/csdn123_news', 'zhiwu5comcn_01');
		exit;

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

		echo '// ' .  lang('plugin/csdn123_news', 'zhiwu5comcn_02');
		exit;

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

		echo '// ' .  lang('plugin/csdn123_news', 'zhiwu5comcn_03');
		exit;

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

		echo '// ' .  lang('plugin/csdn123_news', 'zhiwu5comcn_04');
		exit;

	} elseif($htmlcodeStr=='sign_error') {

		echo '// ' .  lang('plugin/csdn123_news', 'zhiwu5comcn_05');
		exit;

	} elseif(strlen($htmlcodeStr)<20) {

		echo '// ' .  lang('plugin/csdn123_news', 'collection_result_null');
		exit;
	}
	$htmlcodeStr=diconv($htmlcodeStr,'UTF-8');
	$htmlcode=explode('__zhiwu55.net__',$htmlcodeStr);
	if(strpos($htmlcode[1],'__zhiwu55.com__')==false)
	{

		$htmlcode['firstPost']=$htmlcode[1];
		$commentArr=Array();

	} else {

		$htmlcode['firstPost']=substr($htmlcode[1],0,strpos($htmlcode[1],'__zhiwu55.com__'));
		$commentStr=substr($htmlcode[1],strpos($htmlcode[1],'__zhiwu55.com__')+15);
		$commentArr=explode('__zhiwu55.cn__',$commentStr);

	}
	$zhiwu55_firt_news['title'] = $htmlcode[0];
	$zhiwu55_firt_news['firstPost0'] = $htmlcode['firstPost'];
	$zhiwu55_firt_news['replayArrCount'] = count($commentArr);
	for($i=1;$i<$zhiwu55_firt_news['replayArrCount'];$i++)
	{
		$zhiwu55_firt_news['firstPost'.$i] = $commentArr[$i];
	}
	$zhiwu55_firt_news['topHref'] = $zhiwu55_firt_news['source_link'];
	$zhiwu55_firt_news['sendtime'] = date('Y-m-d H:i:s',$zhiwu55_firt_news['send_datetime']);
	$status_code = send_thread_zhiwu55fun($zhiwu55_firt_news);
	if (stripos($status_code,'http')==0) {
		DB::update('zhiwu55cn_news', array('catch_way' => 'autocatch','push_date'=>TIMESTAMP,'del'=>0), array('ID' => $zhiwu55_firt_news['ID']));
	}
	$tid_aid_rs = DB::fetch_first("SELECT browse_url FROM %t WHERE ID=%d LIMIT 1",array('zhiwu55cn_news',$zhiwu55_firt_news['ID']));
	echo '// ' . lang('plugin/csdn123_news', 'collection_success') . $tid_aid_rs['browse_url'];
	exit;

}