<?php
if (!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
	exit('Access Denied');
}
$server_url='action=plugins&operation=config&do=' . $pluginid . '&identifier=csdn123_news&pmod=newest';
require './source/plugin/csdn123_news/function_common.php';
if(empty($_GET['page']) || is_numeric($_GET['page'])==false)
{
	$page=1;
} else {
	$page=$_GET['page'];
}
$page=max(1,$page);
$hzw_appid=wwwzhiwu55com_appid();
$zhiwu55_cookie=getcookie('get_update');
if(empty($zhiwu55_cookie) || (!empty($_GET['zhiwu55_update']) && $_GET['zhiwu55_update']=='yes'))
{
	$dataUrl="http://www.zhiwu55.com/catch_data/cache/get_new_data.php?hzw_appid=".$hzw_appid;
	$htmlcodeStr=dfsockopen($dataUrl);
	$htmlcodeStr=trim($htmlcodeStr);
	$updateArr=dunserialize($htmlcodeStr);
	if(empty($updateArr) || !is_array($updateArr))
	{
		cpmsg("csdn123_news:get_failed",$server_url.'&page='.$page,"error");
	}
	foreach($updateArr as $news)
	{
		$chk = DB::fetch_first("SELECT * FROM %t WHERE source_link=%s LIMIT 1",array('zhiwu55cn_update',$news['fromurl']));
		if (empty($chk) || count($chk) == 0) {

			$addArr=array();
			$addArr['title']=diconv($news['title'],'UTF-8',CHARSET);
			$addArr['source_link']=$news['fromurl'];
			$addArr['send_datetime']=$news['sendtime'];
			DB::insert('zhiwu55cn_update', $addArr);
		}

	}
	dsetcookie('get_update','zhiwu55.com',3600);
	cpmsg("csdn123_news:need_new_article",$server_url,"succeed");
	exit;
}
if($_GET['formhash'] == FORMHASH && !empty($_GET['deleteid']) && is_numeric($_GET['deleteid']))
{

	DB::delete("zhiwu55cn_update","ID=".$_GET['deleteid']);
	cpmsg("csdn123_news:delete_successful",$server_url.'&page='.$page,"succeed");

} elseif($_GET['formhash'] == FORMHASH && !empty($_GET['pushid']) && is_numeric($_GET['pushid'])) {

	$pushid=$_GET['pushid'];
	$newsRs=DB::fetch_first('SELECT * FROM %t WHERE ID=%d LIMIT 1',array('zhiwu55cn_update',$pushid));
	if(strlen($newsRs['preview_url'])>10)
	{
		cpmsg("csdn123_news:already_success",$server_url.'&page='.$page,"succeed");
	}
	$newsCHK=DB::fetch_first('SELECT * FROM %t WHERE source_link=%s LIMIT 1',array('zhiwu55cn_news',$newsRs['source_link']));
	if(!empty($newsCHK['tid_aid']) && is_numeric($newsCHK['tid_aid']) && $newsCHK['tid_aid']>0)
	{
		$PublishedSuccessfully=lang('plugin/csdn123_news', 'success_preview_url');
		$PublishedSuccessfully=$PublishedSuccessfully.preview_url($newsCHK['forum_portal'],$newsCHK['tid_aid']);
		$PublishedSuccessfully=str_replace(array('http://','https://'),'',$PublishedSuccessfully);
		cpmsg($PublishedSuccessfully, '', 'succeed');
		exit;
	}
	require_once libfile('function/forumlist');
	require_once libfile('function/portalcp');
	require_once libfile('function/group');
	$grouplistArr=grouplist('displayorder',false,3000);
	include template('csdn123_news:newest_send');

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

	if (empty($_GET['idarray'])) {
		cpmsg('csdn123_news:select_empty', '' , 'error');
	}
	if (empty($_GET['seldelete']) == false) {
		$idstr = implode(',', $_GET['idarray']);
		$idstr = daddslashes($idstr);
		DB::delete('zhiwu55cn_update', 'ID in (' . $idstr . ')');
		cpmsg('csdn123_news:succeed', $server_url . '&page=' . $_GET['page'], 'succeed');
	}
	if (empty($_GET['selimport']) == false) {

		$idstr = implode(',', $_GET['idarray']);
		require_once libfile('function/forumlist');
		require_once libfile('function/portalcp');
		require_once libfile('function/group');
		$grouplistArr=grouplist('displayorder',false,3000);
		include template('csdn123_news:newest_send');

	}

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

	$addNews=array();
	$addNews['forum_portal']=daddslashes($_GET['forum_portal']);
	$addNews['fid']=intval($_GET['fid']);
	$addNews['threadtypeid']=intval($_GET['threadtypeid']);
	$addNews['portal_catid']=intval($_GET['portal_catid']);
	$addNews['reply_uid']=daddslashes($_GET['reply_uid']);
	$addNews['display_link']=intval($_GET['display_link']);
	$addNews['pseudo_original']=intval($_GET['pseudo_original']);
	$addNews['chinese_encoding']=intval($_GET['chinese_encoding']);
	$addNews['group_fid']=intval($_GET['group_fid']);
	$addNews['intval_time']=intval($_GET['intval_time']);
	$addNews['filter_image']=intval($_GET['filter_image']);
	$addNews['image_center']=intval($_GET['image_center']);
	$addNews['hzw_remotefile']=intval($_GET['hzw_remotefile']);
	$addNews['replaynum']=intval($_GET['replaynum']);
	$addNews['release_now']='yes';
	if($_GET['sendtime']==1)
	{
		$addNews['send_datetime']=time();
	}
	$idstr = $_GET['idstr'];
	$idarray = explode(',', $idstr);
	$newIDstr='';
	foreach($idarray as $pushid)
	{
		$updateRs=DB::fetch_first('SELECT * FROM %t WHERE ID=%d LIMIT 1',array('zhiwu55cn_update',$pushid));
		$newsCHK=DB::fetch_first('SELECT * FROM %t WHERE source_link=%s LIMIT 1',array('zhiwu55cn_news',$updateRs['source_link']));
		if(!empty($newsCHK['push_date']) && is_numeric($newsCHK['push_date']) && $newsCHK['push_date']>0 && !empty($newsCHK['browse_url']))
		{
			continue;
		}
		$addNews['source_link']=daddslashes($updateRs['source_link']);
		if($_GET['sendtime']==2)
		{
			$addNews['send_datetime']=$updateRs['send_datetime'];
		}
		if(stripos($_GET['first_uid'],',')!=false)
		{
			$first_uid_arr=explode(',',$_GET['first_uid']);
			shuffle($first_uid_arr);
			$first_uid=$first_uid_arr[0];
		} else {
			$first_uid=$_GET['first_uid'];
		}
		$addNews['first_uid']=intval($first_uid);
		$addNews['title']=daddslashes($updateRs['title']);
		$addNews['views']=rand($_GET['min_views'],$_GET['max_views']);
		if(!empty($newsCHK['source_link']) && strlen($newsCHK['source_link'])>10)
		{
			DB::update('zhiwu55cn_news',$addNews,array('source_link'=>$newsCHK['source_link']));
		} else {
			DB::insert('zhiwu55cn_news',$addNews);
		}
		$newsCHK=DB::fetch_first('SELECT * FROM %t WHERE source_link=%s LIMIT 1',array('zhiwu55cn_news',$updateRs['source_link']));
		$newIDstr=$newIDstr.$newsCHK['ID'].',';
	}
	$idarray=substr($newIDstr,0,-1);
	$sendDataUrl='action=plugins&operation=config&do=' . $pluginid . '&identifier=csdn123_news&pmod=pending&selimport=yes&selall=yes&num=0&formhash='.FORMHASH.'&idarray='.$idarray;
	cpmsg('csdn123_news:bat_sending', $sendDataUrl, 'succeed');


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

	$updateRs=DB::fetch_first('SELECT * FROM %t WHERE ID=%d LIMIT 1',array('zhiwu55cn_update',$_GET['send_pushid']));
	$newsCHK=DB::fetch_first('SELECT * FROM %t WHERE source_link=%s LIMIT 1',array('zhiwu55cn_news',$updateRs['source_link']));
	$addNews=array();
	$addNews['title']=daddslashes($_GET['title']);
	$addNews['source_link']=daddslashes($updateRs['source_link']);
	$addNews['forum_portal']=daddslashes($_GET['forum_portal']);
	$addNews['fid']=intval($_GET['fid']);
	$addNews['threadtypeid']=intval($_GET['threadtypeid']);
	$addNews['portal_catid']=intval($_GET['portal_catid']);
	if(stripos($_GET['first_uid'],',')!=false)
	{
		$first_uid_arr=explode(',',$_GET['first_uid']);
		shuffle($first_uid_arr);
		$first_uid=$first_uid_arr[0];
	} else {
		$first_uid=$_GET['first_uid'];
	}
	$addNews['first_uid']=intval($first_uid);
	$addNews['reply_uid']=daddslashes($_GET['reply_uid']);
	$addNews['display_link']=intval($_GET['display_link']);
	$addNews['pseudo_original']=intval($_GET['pseudo_original']);
	$addNews['chinese_encoding']=intval($_GET['chinese_encoding']);
	$addNews['views']=rand($_GET['min_views'],$_GET['max_views']);
	$addNews['group_fid']=intval($_GET['group_fid']);
	$addNews['intval_time']=intval($_GET['intval_time']);
	$addNews['filter_image']=intval($_GET['filter_image']);
	$addNews['image_center']=intval($_GET['image_center']);
	$addNews['hzw_remotefile']=intval($_GET['hzw_remotefile']);
	$addNews['replaynum']=intval($_GET['replaynum']);
	$addNews['release_now']='yes';
	if($_GET['sendtime']==1)
	{
		$addNews['send_datetime']=time();
	} else {
		$addNews['send_datetime']=$updateRs['send_datetime'];
	}
	if(!empty($newsCHK['source_link']) && strlen($newsCHK['source_link'])>10)
	{
		DB::update('zhiwu55cn_news',$addNews,array('source_link'=>$newsCHK['source_link']));
	} else {
		DB::insert('zhiwu55cn_news',$addNews);
	}
	$newsCHK=DB::fetch_first('SELECT * FROM %t WHERE source_link=%s LIMIT 1',array('zhiwu55cn_news',$updateRs['source_link']));
	$catchUrl='?action=plugins&operation=config&do=' . $pluginid . '&identifier=csdn123_news&pmod=pending&send_id='.$newsCHK['ID'].'&formhash='.FORMHASH;
	$zhiwu5comcn_16=lang('plugin/csdn123_news', 'zhiwu5comcn_16');
	echo '<div style="font-size:28px;margin-top:64px;text-align:center;color:red"><br><br>'.$zhiwu5comcn_16.'</div>';
	echo '<script>window.location.href="'.$catchUrl.'";</script>';
	exit;

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

	DB::delete("zhiwu55cn_update","ID>0");
	cpmsg("csdn123_news:successful_clearance",$server_url,"succeed");

} else {

	$newsCount = DB::result_first('SELECT count(*) FROM %t',array('zhiwu55cn_update'));
	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;
		$update_list = DB::fetch_all('SELECT * FROM %t ORDER BY send_datetime DESC LIMIT %d,20',array('zhiwu55cn_update',$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:newest');

}