<?php
if (!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
	exit('Access Denied');
}
require './source/plugin/csdn123_news/function_common.php';
$server_url = 'action=plugins&operation=config&do=' . $pluginid . '&identifier=csdn123_news&pmod=pending';
$success_Url = 'action=plugins&operation=config&do=' . $pluginid . '&identifier=csdn123_news&pmod=released';
$modify_Url = 'action=plugins&operation=config&do=' . $pluginid . '&identifier=csdn123_news&pmod=chrome&zhiwu55_send_form=yes&formhash='.FORMHASH;
if (!isset($_G['cache']['csdn123_news_send'])) {
	loadcache('csdn123_news_send');
}
$zhiwu55_data=array();
$zhiwu55_data = dunserialize($_G['cache']['csdn123_news_send']);
if ($_GET['formhash'] == FORMHASH && $_GET['selall'] == 'yes') {

	if (empty($_GET['idarray'])) {
		cpmsg('csdn123_news:select_empty', '' , 'error');
	}
	if (empty($_GET['seldelete']) == false) {
		$idstr = dimplode($_GET['idarray']);
		DB::update('zhiwu55cn_news', array('del' => 1), 'ID in (' . $idstr . ')');
		cpmsg('csdn123_news:succeed', $server_url . '&page=' . $_GET['page'], 'succeed');
	}
	if (empty($_GET['selimport']) == false) {

		if (empty($_GET['num'])) {
			$num = 0;
		} else {
			$num = $_GET['num'];
			$num = intval($num);
		}
		if (is_array($_GET['idarray'])) {
			$idarray = $_GET['idarray'];
			$count_num = count($idarray);
			$idstr = dimplode($_GET['idarray']);
			$idstr = str_replace("'","",$idstr);
		} else {
			$idstr = $_GET['idarray'];
			$idstr = str_replace("'","",$idstr);
			$idarray = explode(',', $idstr);
			$count_num = count($idarray);
		}
		$ID = $idarray[$num];
		$ID = str_replace("'","",$ID);
		if (is_numeric($ID) == false) {
			cpmsg('csdn123_news:succeed', $success_Url, 'succeed');
		}
		$num++;
		$nextCatchUrl = '?' . $server_url . '&selimport=yes&selall=yes&formhash=' . FORMHASH . '&idarray=' . $idstr . '&num=' . $num;
		$statusStr = lang('plugin/csdn123_news', 'import_status');
		$statusStr = str_replace('count', $count_num, $statusStr);
		$statusStr = str_replace('num', $num, $statusStr);
		$news = DB::fetch_first('SELECT * FROM %t WHERE ID=%d',array("zhiwu55cn_news",$ID));
		DB::update('zhiwu55cn_news',array('del'=>1),array('ID'=>$ID));
		if(empty($news['forum_portal']))
		{
			$zhiwu55Url = $news['source_link'] . '#news_id=' . $ID;
			echo '<div style="font-size:28px;margin-top:64px;text-align:center;color:red"><br>' . lang('plugin/csdn123_news','zhiwu55_com_10') . '<br><br><br>' . $statusStr . '</div>';
			echo '<script>';
			echo 'window.open("' . $zhiwu55Url . '","www_zhiwu55_com_blank");';
			echo 'setTimeout(function(){ window.location.href="' . $nextCatchUrl . '" },10000);';
			echo '</script>';

		} else {

			$zhiwu55Url = '?'.$server_url.'&send_id='.$ID.'&formhash='.FORMHASH.'&nextCatchUrl='.base64_encode(urlencode($nextCatchUrl));
			echo '<div style="font-size:28px;margin-top:64px;text-align:center;color:red"><br>' .$statusStr.'</div>';
			echo '<script>';
			echo 'setTimeout(function(){ window.location.href="' . $zhiwu55Url . '" },5000);';
			echo '</script>';

		}

	}

} elseif ($_GET['formhash'] == FORMHASH && empty($_GET['send_all_news']) == false && $_GET['send_all_news']=='yes') {

	if(empty($_GET['news_id']) || !is_numeric($_GET['news_id']))
	{
		$news_id = 0;
	} else {
		$news_id = $_GET['news_id'];
	}
	$news = DB::fetch_first("SELECT * FROM %t WHERE del=0 AND push_date=0 AND ID>%d ORDER BY ID ASC LIMIT 1",array("zhiwu55cn_news",$news_id));
	if(empty($news)) {
		cpmsg('csdn123_news:succeed', $success_Url, 'succeed');
	}
	DB::update('zhiwu55cn_news',array('del'=>1),array('ID'=>$news['ID']));
	$nextCatchUrl = '?' . $server_url . '&send_all_news=yes&formhash=' . FORMHASH . '&news_id=' . $news['ID'];
	if(empty($news['forum_portal']))
	{
		$zhiwu55Url = $news['source_link'] . '#news_id=' . $news['ID'];
		echo '<div style="font-size:28px;margin-top:64px;text-align:center;color:red"><br><br>'.lang('plugin/csdn123_news','zhiwu55_com_10').'<br><br>' . lang('plugin/csdn123_news', 'zhiwu55_com_07') . '<br><br>'.lang('plugin/csdn123_news', 'zhiwu5comcn_14').$news['title'].'</div>';
		echo '<script>';
		echo 'window.open("' . $zhiwu55Url . '","www_zhiwu55_com_blank");';
		echo 'setTimeout(function(){ window.location.href="' . $nextCatchUrl . '" },10000);';
		echo '</script>';

	} else {

		$zhiwu55Url = '?'.$server_url.'&send_id='.$news['ID'].'&formhash='.FORMHASH.'&nextCatchUrl='.base64_encode(urlencode($nextCatchUrl));
		echo '<div style="font-size:28px;margin-top:64px;text-align:center;color:red"><br><br>' . lang('plugin/csdn123_news', 'zhiwu55_com_07') . '<br><br>'.lang('plugin/csdn123_news', 'zhiwu5comcn_14').$news['title'].'</div>';
		echo '<script>';
		echo 'setTimeout(function(){ window.location.href="' . $zhiwu55Url . '" },5000);';
		echo '</script>';

	}


} elseif ($_GET['formhash'] == FORMHASH && is_numeric($_GET['modify_id'])) {

	$modify_id = $_GET['modify_id'];
	$page = $_GET['page'];
	if(!empty($_GET['modify']) && $_GET['modify']=='yes')
	{

		$updateArr=array();
		$updateArr['title']=daddslashes($_GET['title']);
		$updateArr['forum_portal']=daddslashes($_GET['forum_portal']);
		$updateArr['fid']=intval($_GET['fid']);
		$updateArr['threadtypeid']=intval($_GET['threadtypeid']);
		$updateArr['portal_catid']=intval($_GET['portal_catid']);
		$updateArr['first_uid']=daddslashes($_GET['first_uid']);
		$updateArr['reply_uid']=daddslashes($_GET['reply_uid']);
		$updateArr['display_link']=intval($_GET['display_link']);
		$updateArr['pseudo_original']=intval($_GET['pseudo_original']);
		$updateArr['chinese_encoding']=intval($_GET['chinese_encoding']);
		$updateArr['views']=$_GET['views'];
		$updateArr['group_fid']=intval($_GET['group_fid']);
		$updateArr['hzw_remotefile']=intval($_GET['hzw_remotefile']);
		$updateArr['intval_time']=intval($_GET['intval_time']);
		$updateArr['send_datetime']=strtotime($_GET['send_datetime']);
		$updateArr['filter_image']=intval($_GET['filter_image']);
		$updateArr['image_center']=intval($_GET['image_center']);
		$updateArr['replaynum']=intval($_GET['replaynum']);
		$updateArr['release_now']=daddslashes($_GET['release_now']);
		DB::update('zhiwu55cn_news',$updateArr,array('ID'=>$modify_id));
		cpmsg('csdn123_news:succeed',$server_url.'&page='.$page, 'succeed');

	}
	$news = DB::fetch_first("SELECT * FROM %t WHERE del=0 AND ID=%d",array("zhiwu55cn_news",$modify_id));
	require_once libfile('function/forumlist');
	require_once libfile('function/portalcp');
	require_once libfile('function/group');
	$grouplistArr=grouplist('displayorder',false,3000);
	$typeclassArr = C::t('forum_threadclass')->fetch_all_by_fid($news['fid']);
	include template("csdn123_news:pending_update");

} elseif ($_GET['formhash'] == FORMHASH && is_numeric($_GET['delete_id'])) {

	$delete_id = $_GET['delete_id'];
	DB::update('zhiwu55cn_news',array('del'=>1),array('ID'=>$delete_id));
	cpmsg("csdn123_news:succeed",$server_url.'&page='.$_GET['page'],"succeed");

} elseif ($_GET['formhash'] == FORMHASH && is_numeric($_GET['send_id'])) {

	$send_id = $_GET['send_id'];
	$hzw_news = DB::fetch_first("SELECT * FROM %t WHERE ID=%d",array("zhiwu55cn_news",$send_id));
	if(empty($hzw_news))
	{
		cpmsg('csdn123_news:zhiwu5comcn_11','', 'error');
	}
	$hzw_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))
	{
		cpmsg('csdn123_news:zhiwu5comcn_10','', 'error');
	}
	$signArr=array();
	$signArr['hzw_appid']=$hzw_appid;
	$signArr['appsecret']=$zhiwu55_appsecret;
	$signArr['url']=$hzw_news['source_link'];
	$signArr['chinese_encoding']=$hzw_news['chinese_encoding'];
	$signArr['t']=time();
	$sign=hzw_sign($signArr);
	$title=$hzw_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($hzw_news['source_link']).'&chinese_encoding='.$hzw_news['chinese_encoding'].'&sign='.$sign.'&t='.$signArr['t'];
	$htmlcodeStr=dfsockopen($dataUrl);
	if($htmlcodeStr=='parameter_error') {

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

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

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

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

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

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

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

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

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

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

		cpmsg("csdn123_news:collection_result_null","","error");
	}
	$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);

	}
	$hzw_news['title'] = $htmlcode[0];
	$hzw_news['firstPost0'] = $htmlcode['firstPost'];
	$hzw_news['replayArrCount'] = count($commentArr);
	for($i=1;$i<$hzw_news['replayArrCount'];$i++)
	{
		$hzw_news['firstPost'.$i] = $commentArr[$i];
	}
	$hzw_news['topHref'] = $hzw_news['source_link'];
	$hzw_news['sendtime'] = date('Y-m-d H:i:s',$hzw_news['send_datetime']);
	$browse_url=send_thread_zhiwu55fun($hzw_news);
	if(!empty($_GET['nextCatchUrl']))
	{
		$success_Url = $_GET['nextCatchUrl'];
		$success_Url = base64_decode($success_Url);
		$success_Url = urldecode($success_Url);
		if(stripos($browse_url,'http')==0)
		{
			
			echo '<script>window.location.href="'.$success_Url.'";</script>';
			exit;
			
		} else {
			
			cpmsg("csdn123_news:fail","","error");
			
		}
	}
	if(stripos($browse_url,'http')==0)
	{
		cpmsg("csdn123_news:succeed",$success_Url,"succeed");
	} else {
		cpmsg("csdn123_news:fail","","error");
	}

} elseif ($_GET['formhash'] == FORMHASH && empty($_GET['clears_all']) == false && $_GET['clears_all'] == 'yes') {

	DB::delete('zhiwu55cn_news','push_date=0 and del=0');
	cpmsg('csdn123_news:succeed', $server_url, 'succeed');

} else {

	if (!isset($_G['cache']['plugin'])) {
		loadcache('plugin');
	}
	$zhiwu55_dingshi=$_G['cache']['plugin']['csdn123_news']['zhiwu55_dingshi'];
	if(empty($_GET['page']) || is_numeric($_GET['page'])==false)
	{
		$page=1;
	} else {
		$page=$_GET['page'];
	}
	$page=max(1,$page);
	$newsCount = DB::result_first('SELECT count(*) FROM %t WHERE push_date=0 and del=0',array('zhiwu55cn_news'));
	if($newsCount>0)
	{
		$TotalNumber = lang('plugin/csdn123_news', 'total_number');
		$TotalPageNumber = $newsCount/20;
		$TotalPageNumber = @ceil($TotalPageNumber);
		$TotalNumber = str_replace('x',$TotalPageNumber,$TotalNumber);
		$TotalNumber = str_replace('y',$newsCount,$TotalNumber);
		$TotalNumber = str_replace('z',$page,$TotalNumber);
		$page = min($TotalPageNumber,$page);
		$startNum = ($page - 1) * 20;
		$homePage = $server_url . '&page=1';
		$nextPage = $server_url . '&page=' . ($page + 1);
		$prePage = $server_url . '&page=' . ($page - 1);
		$endPage = $server_url . '&page=' . $TotalPageNumber;
		$news_list = DB::fetch_all('SELECT * FROM %t WHERE push_date=0 and del=0 ORDER BY ID DESC LIMIT %d,20',array('zhiwu55cn_news',$startNum));
		$start = $page - 4;
		$start = min($TotalPageNumber - 8,$start);
		$start = max($start,1);
		$end = $start + 8;
		$end = min($end,$TotalPageNumber);
		$showPage="";
		for($i=$start;$i<=$end;$i++)
		{
			if($i==$page)
			{
				$showPage=$showPage . '<strong>' . $i . '</strong>';
			} else {
				$showPage=$showPage . '<a href="?' . $server_url . '&page=' . $i . '&formhash=' . FORMHASH . '">' . $i . '</a>';
			}
		}
	}
	include template("csdn123_news:pending");

}