<?php
if (!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
	exit('Access Denied');
}
$server_url='action=plugins&operation=config&do=' . $pluginid . '&identifier=csdn123_news&pmod=batch';
$regvest_url='action=plugins&operation=config&do=' . $pluginid . '&identifier=csdn123_news&pmod=adminvest&subpmod=regvest&formhash=' . FORMHASH;
require './source/plugin/csdn123_news/function_common.php';
$hzw_appid=wwwzhiwu55com_appid();
if($_GET['formhash'] == FORMHASH && !empty($_GET['zhiwu55_agrs']) && $_GET['zhiwu55_agrs']=='yes')
{
	if(empty($_GET['keyword']) || strlen($_GET['keyword'])<4)
	{
		cpmsg("csdn123_news:keyword_cannot_empty", '', "error");
	} else {
		$keyword=$_GET['keyword'];
		$keyword=trim($keyword);
		$keyword=diconv($keyword,CHARSET,'UTF-8');
	}
	if(empty($_GET['first_uid']))
	{
		cpmsg("csdn123_news:uid_error","","error");
	} else {
		$first_uid=$_GET['first_uid'];
	}
	if(empty($_GET['reply_uid']))
	{
		cpmsg("csdn123_news:uid_error","","error");
	} else {
		$reply_uid=$_GET['reply_uid'];
	}
	if(preg_match('/[a-z]/i',$first_uid)==1 || preg_match('/[a-z]/i',$reply_uid)==1)
	{
		cpmsg("csdn123_news:uid_error","","error");
	}
	if (!isset($_G['cache']['plugin'])) {
		loadcache('plugin');
	}
	$signArr=array();
	$signArr['hzw_appid']=$hzw_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'];
		}
		$signArr['appsecret']=$zhiwu55com_authorization_arr['hzw_appsecret'];

	} else {

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

	}
	if(empty($signArr['appsecret']))
	{
		cpmsg("csdn123_news:zhiwu5comcn_10","","error");
	}
	$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'].'&limit='.$_GET['limit'].'&sign='.$sign;
	$resultStr=dfsockopen($dataUrl);
	if($resultStr=='parameter_error') {

		cpmsg("csdn123_news:zhiwu5comcn_01","","error");

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

		cpmsg("csdn123_news:zhiwu5comcn_02","","error");

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

		cpmsg("csdn123_news:zhiwu5comcn_03","","error");

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

		cpmsg("csdn123_news:zhiwu5comcn_04","","error");

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

		cpmsg("csdn123_news:zhiwu5comcn_05","","error");

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

		cpmsg("csdn123_news:collection_result_null","","error");
	}
	$resultStr=trim($resultStr);
	$resultStr=base64_decode($resultStr);
	$resultArr=dunserialize($resultStr);
	if(empty($resultArr) || !is_array($resultArr))
	{
		cpmsg("csdn123_news:collection_result_null","","error");
	}
	$x=0;
	$y=0;
	$z=0;
	$resultArr=array_reverse($resultArr);
	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($_GET['forum_portal']);
		$newsArr['fid']=intval($_GET['fid']);
		$newsArr['threadtypeid']=intval($_GET['threadtypeid']);
		$newsArr['portal_catid']=intval($_GET['portal_catid']);
		$first_uid_arr=explode(',',$_GET['first_uid']);
		shuffle($first_uid_arr);
		$newsArr['first_uid']=$first_uid_arr[0];
		$newsArr['reply_uid']=daddslashes($_GET['reply_uid']);
		$newsArr['display_link']=intval($_GET['display_link']);
		$newsArr['pseudo_original']=intval($_GET['pseudo_original']);
		$newsArr['chinese_encoding']=intval($_GET['chinese_encoding']);
		$newsArr['views']=rand($_GET['min_views'],$_GET['max_views']);
		$newsArr['group_fid']=intval($_GET['group_fid']);
		$newsArr['hzw_remotefile']=intval($_GET['hzw_remotefile']);
		$newsArr['intval_time']=intval($_GET['intval_time']);
		$newsArr['filter_image']=intval($_GET['filter_image']);
		$newsArr['image_center']=intval($_GET['image_center']);
		$newsArr['replaynum']=intval($_GET['replaynum']);
		$newsArr['release_now']=daddslashes($_GET['release_now']);
		if($_GET['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']));
		$x++;
		if (empty($chk) || count($chk) == 0) {
			$z++;
			DB::insert('zhiwu55cn_news', $newsArr);
		}
	}
	$succeed_url='action=plugins&operation=config&do=' . $pluginid . '&identifier=csdn123_news&pmod=pending';
	$y=$x-$z;
	$collection_success_info=lang('plugin/csdn123_news', 'collection_success_info');
	$collection_success_info=str_replace('x',$x,$collection_success_info);
	$collection_success_info=str_replace('y',$y,$collection_success_info);
	$collection_success_info=str_replace('z',$z,$collection_success_info);
	cpmsg($collection_success_info,$succeed_url,"succeed");


} else {

	require_once libfile('function/forumlist');
	require_once libfile('function/portalcp');
	require_once libfile('function/group');
	$grouplistArr=grouplist('displayorder',false,3000);
	include template('csdn123_news:batch');

}