<?php
if (!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
	exit('Access Denied');
}
$server_url='action=plugins&operation=config&do=' . $pluginid . '&identifier=csdn123_news&pmod=automatic';
require './source/plugin/csdn123_news/function_common.php';
if($_GET['formhash'] == FORMHASH && !empty($_GET['add']) && $_GET['add']=='yes')
{
	require_once libfile('function/forumlist');
	require_once libfile('function/portalcp');
	require_once libfile('function/group');
	$grouplistArr=grouplist('displayorder',false,3000);
	include template("csdn123_news:automatic_add");

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

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

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

	$modify_id=$_GET['modify_id'];
	$modifyRs=DB::fetch_first("SELECT * FROM %t WHERE ID=%d LIMIT 1",array('zhiwu55cn_cron',$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($modifyRs['fid']);
	include template("csdn123_news:automatic_modify");

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

	DB::delete("zhiwu55cn_cron","ID=".$_GET['delete_id']);
	cpmsg("csdn123_news:delete_successful",$server_url,"succeed");

} elseif($_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'];
	}
	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");
	}
	$cronArr=array();
	$cronArr['keyword']=daddslashes($_GET['keyword']);
	$cronArr['release_now']=daddslashes($_GET['release_now']);
	$cronArr['replaynum']=intval($_GET['replaynum']);
	$cronArr['first_uid']=daddslashes($_GET['first_uid']);
	$cronArr['reply_uid']=daddslashes($_GET['reply_uid']);
	$cronArr['intval_time']=intval($_GET['intval_time']);
	$cronArr['display_link']=intval($_GET['display_link']);
	$cronArr['filter_image']=intval($_GET['filter_image']);
	$cronArr['image_center']=intval($_GET['image_center']);
	$cronArr['hzw_remotefile']=intval($_GET['hzw_remotefile']);
	$cronArr['pseudo_original']=intval($_GET['pseudo_original']);
	$cronArr['chinese_encoding']=intval($_GET['chinese_encoding']);
	$cronArr['min_views']=intval($_GET['min_views']);
	$cronArr['max_views']=intval($_GET['max_views']);
	$cronArr['sendtime']=intval($_GET['sendtime']);
	$cronArr['forum_portal']=daddslashes($_GET['forum_portal']);
	$cronArr['fid']=intval($_GET['fid']);
	$cronArr['threadtypeid']=intval($_GET['threadtypeid']);
	$cronArr['portal_catid']=intval($_GET['portal_catid']);
	$cronArr['group_fid']=intval($_GET['group_fid']);
	if(!empty($_GET['zhiwu55_agrs_modify_id']) && is_numeric($_GET['zhiwu55_agrs_modify_id']))
	{
		$zhiwu55_agrs_modify_id=intval($_GET['zhiwu55_agrs_modify_id']);
		DB::update('zhiwu55cn_cron', $cronArr,array('ID'=>$zhiwu55_agrs_modify_id));
		cpmsg("csdn123_news:modify_auto_success",$server_url,"succeed");
	} else {
		DB::insert('zhiwu55cn_cron', $cronArr);
		cpmsg("csdn123_news:add_auto_success",$server_url,"succeed");
	}

} elseif(!empty($_GET['off_timer']) && $_GET['off_timer']=='yes' && $_GET['formhash'] == FORMHASH) {
	
	$succeed=lang('plugin/csdn123_news', 'succeed');
	C::t('common_pluginvar')->update_by_variable($pluginid, 'zhiwu55_dingshi', array('value'=>0));
	updatecache(array('plugin', 'setting', 'styles'));
	cleartemplatecache();
	cpmsg($succeed,$server_url,"succeed");

} else {

	if (!isset($_G['cache']['plugin'])) {
		loadcache('plugin');
	}
	$zhiwu55_dingshi = $_G['cache']['plugin']['csdn123_news']['zhiwu55_dingshi'];
	if($zhiwu55_dingshi != 1)
	{
		$disable_auto=lang('plugin/csdn123_news', 'disable_auto');
		echo '<div style="text-align:center;margin:64px;"><a href="?action=plugins&operation=config&do=' . $pluginid . '" style="font-size:24px;color:red;">'.$disable_auto.'</a></div>';
		
	} else {
		
		$pendingCount = DB::result_first('SELECT count(*) FROM %t WHERE tid_aid=0 AND release_now=%s AND del=0 AND forum_portal IS NOT NULL',array('zhiwu55cn_news','yes'));
		$successCount = DB::result_first('SELECT count(*) FROM %t WHERE tid_aid>0 AND catch_way=%s',array('zhiwu55cn_news','autocatch'));
		$cronList=DB::fetch_all("SELECT * FROM %t ORDER BY catchtime DESC,catchnum DESC",array('zhiwu55cn_cron'));
		$cron_news_info = lang('plugin/csdn123_news', 'cron_news_info');
		$cron_news_info = str_replace('x',$pendingCount,$cron_news_info);
		$cron_news_info = str_replace('y',$successCount,$cron_news_info);
		include template("csdn123_news:automatic");
	
	}

}