<?php
//织梦数据库调用代码

$addarticletype = $applicationarray["addarticletype"];

if(is_array($addarticletype["dede"])) {
	$open_dede=$addarticletype["dede"]["open_dede"];
	$folder_dede=$addarticletype["dede"]["folder_dede"];
	$guolv_dede="0,".$addarticletype["dede"]["guolv_dede"].",";
}

if(is_array($addarticletype["discuz"])) {
	$open_discuz=$addarticletype["discuz"]["open_discuz"];
	$folder_discuz=$addarticletype["discuz"]["folder_discuz"];
	$guolv_discuz="0,".$addarticletype["discuz"]["guolv_discuz"].",";
}
if(is_array($addarticletype["discuzx2"])) {
	$open_discuzx2=$addarticletype["discuzx2"]["open_discuzx2"];
	$folder_discuzx2=$addarticletype["discuzx2"]["folder_discuzx2"];
	$guolv_discuzx2="0,".$addarticletype["discuzx2"]["guolv_discuzx2"].",";
}
if(is_array($addarticletype["wordpress"])) {
	$open_wordpress=$addarticletype["wordpress"]["open_wordpress"];
	$folder_wordpress=$addarticletype["wordpress"]["folder_wordpress"];
	$guolv_wordpress="0,".$addarticletype["wordpress"]["guolv_wordpress"].",";
}



$limit_article = empty($addarticletype["num_article"])?1:$addarticletype["num_article"];

$newslist = array();

//织梦的整合代码
if($open_dede=="1" && is_file(WEBROOT.$folder_dede."/include/common.inc.php")) {
	require_once(WEBROOT.$folder_dede.'/include/common.inc.php');
	require_once(WEBROOT.$folder_dede.'/include/arc.listview.class.php');
	
	$dede_list = array();
	
	$dsql->SetQuery("Select id,typename,typedir,siteurl,sitepath 
	                                        from `#@__arctype` where ispart<>2 And ishidden<>1 order by sortrank asc,id desc");
	$dsql->Execute();
	
	$clist  = array();
	while($rowArtType = $dsql->GetArray()) {
		if(strpos($guolv_dede,$rowArtType["id"])==0){
			$clist[] = $rowArtType;
		}
	}


		foreach( $clist as $rowArtType){
			
			$rowArtType["typedir"] = str_replace("{cmspath}",$rootroad."/".$folder_dede,$rowArtType["typedir"]);
 			$artlist = array();
			
			$arttype = array();
			$arttype["id"] = $rowArtType["id"];
			$arttype["typename"] = (strtolower($cfg_db_language)=="gbk"?$rowArtType["typename"]:Newiconv("UTF-8","GBK",$rowArtType["typename"]));
			$arttype["typedir"] = $rowArtType["typedir"];
			
			$dsql->setQuery("select id,typeid,click,title,pubdate,color,keywords,description,filename from #@__archives where typeid='".$rowArtType["id"]."' order by id desc limit $limit_article;");
			$dsql->Execute();
			
			while($result = $dsql->getArray())
			{
				$arrtemp = $result;
				$aid = ereg_replace('[^0-9]','',$result["id"]);
			
				//获取主表信息
				$query = "Select arc.*,ch.maintable,ch.addtable,ch.issystem,ch.editcon,
						  tp.typedir,tp.typename,tp.corank,tp.namerule,tp.namerule2,tp.ispart,tp.moresite,tp.sitepath,tp.siteurl
					   From `#@__arctiny` arc
					   left join `#@__arctype` tp on tp.id=arc.typeid
					   left join `#@__channeltype` ch on ch.id=tp.channeltype
					   where arc.id='$aid' ";
				$trow = $dsql->GetOne($query);
				$trow['maintable'] = ( trim($trow['maintable'])=='' ? '#@__archives' : trim($trow['maintable']) );
				if($trow['issystem'] != -1)
				{
					$arcQuery = "Select arc.*,tp.typedir,tp.typename,tp.corank,tp.namerule,tp.namerule2,tp.ispart,tp.moresite,tp.sitepath,tp.siteurl
							   from `{$trow['maintable']}` arc left join `#@__arctype` tp on arc.typeid=tp.id
							   left join `#@__channeltype` ch on ch.id=arc.channel where arc.id='$aid' ";
					$arcRow = $dsql->GetOne($arcQuery);
				}
				else
				{
					$arcRow['id'] = $aid;
					$arcRow['typeid'] = $trow['typeid'];
					$arcRow['senddate'] = $trow['senddate'];
					$arcRow['title'] = '';
					$arcRow['ismake'] = 1;
					$arcRow['arcrank'] = $trow['corank'];
					$arcRow['namerule'] = $trow['namerule'];
					$arcRow['typedir'] = $trow['typedir'];
					$arcRow['money'] = 0;
					$arcRow['filename'] = '';
					$arcRow['moresite'] = $trow['moresite'];
					$arcRow['siteurl'] = $trow['siteurl'];
					$arcRow['sitepath'] = $trow['sitepath'];
				}
				$arcurl  = GetFileUrl($arcRow['id'],$arcRow['typeid'],$arcRow['senddate'],$arcRow['title'],$arcRow['ismake'],$arcRow['arcrank'],$arcRow['namerule'],$arcRow['typedir'],$arcRow['money'],$arcRow['filename'],$arcRow['moresite'],$arcRow['siteurl'],$arcRow['sitepath']);
				$arcfile = GetFileUrl($arcRow['id'],$arcRow['typeid'],$arcRow['senddate'],$arcRow['title'],$arcRow['ismake'],$arcRow['arcrank'],$arcRow['namerule'],$arcRow['typedir'],$arcRow['money'],$arcRow['filename']);
				if(eregi('^http:',$arcfile))
				{
					//$arcfile = eregi_replace("^http://([^/]*)/",$rootroad."/".$folder_dede.'/',$arcfile);
				}
				$arrtemp["arcurl"] = $arcfile;
				
				$newstmp = array();
				$newstmp["id"] = $arrtemp["id"];
				$newstmp["title"] = (strtolower($cfg_db_language)=="gbk"?$arrtemp["title"]:Newiconv("UTF-8","GBK",$arrtemp["title"]));
				$newstmp["url"] = $arrtemp["arcurl"];
				$newstmp["date"] = $arrtemp["pubdate"];
				
				
				$artlist[] = $newstmp;
			}
			
			$arttype["artlist"] = $artlist;
			
			//if(count($artlist) >= $limit_article) {
				$newslist[] = $arttype;
			//}
			
		}

}

//discuz 的整合代码
if($open_discuz=="1" && is_file(WEBROOT.$folder_discuz."/include/common.inc.php")) {
	require_once WEBROOT.$folder_discuz.'/include/common.inc.php';
	require_once WEBROOT.$folder_discuz.'/include/forum.func.php';
	$sdb = loadmultiserver();
	$discuz_list = array();
	$rewritestatus  = $_DCACHE['settings']['rewritestatus'];
	$query = $sdb->query("SELECT fid,fup,name,status FROM {$tablepre}forums WHERE displayorder!=-1 and type='forum' order by displayorder asc,fid desc");
	$discuz_forums = array();


	$artlist = array();
	

	while($sub = $sdb->fetch_array($query)) {


		if(strpos($guolv_discuz,$sub["fid"])==0){
			$sub['url'] = !($rewritestatus & 1) ? $rootroad."/".$folder_discuz."/forumdisplay.php?tid=".$sub["fid"] : $rootroad."/".$folder_discuz."/forum-".$sub['fid']."-1.html";
	
			$arttype = array();
			$arttype["typename"] = (strtolower($charset)=="gbk"?$sub["name"]:Newiconv("UTF-8","GBK",$sub["name"]));
			$arttype["typedir"] = $sub['url'];
			$arttype["id"] = $sub['fid'];
			$discuz_forums[] = $arttype;
		}

		
	}
	
	foreach($discuz_forums as $arrforums) {
		
		$query = $sdb->query("SELECT tid,fid,typeid,author,subject,dateline,lastpost,lastposter FROM {$tablepre}threads WHERE fid=".$arrforums['id']." and displayorder!=-1 order by tid desc limit $limit_article");
		$discuz_thread = array();
		while($sub = $sdb->fetch_array($query)) {
			$sub['url'] = !($rewritestatus & 2) ? $rootroad."/".$folder_discuz."/viewthread.php?tid=".$sub["tid"] : $rootroad."/".$folder_discuz."/thread-".$sub['tid']."-1-1.html";
				$newstmp = array();
				$newstmp["id"] = $sub["tid"];
				$newstmp["title"] = (strtolower($charset)=="gbk"?$sub["subject"]:Newiconv("UTF-8","GBK",$sub["subject"]));
				$newstmp["url"] = $sub["url"];
				$newstmp["date"] = $sub["dateline"];
				
			$discuz_thread[] = $newstmp;
		}

				
				$arrforums["artlist"] = $discuz_thread;
		
		//if(count($discuz_thread) >= $limit_article) {
			$newslist[] = $arrforums;
		//}
	}
		
}

//discuz X2 的整合代码
if($open_discuzx2=="1" && is_file(WEBROOT.$folder_discuzx2."/config/config_global.php")) {
	require_once WEBROOT.$folder_discuzx2."/config/config_global.php";

	
	$DBlink = mysql_connect($_config['db']['1']['dbhost'],$_config['db']['1']['dbuser'],$_config['db']['1']['dbpw']);
	if(!$DBlink) 
	{
		echo "数据库连接错误！";
		exit();
	}
	mysql_select_db($_config['db']['1']['dbname'],$DBlink);
	mysql_query("SET NAMES ".$_config['db']['1']['dbcharset']);

	$rsupdate = @mysql_query("select  fid,name from ".$_config['db']['1']['tablepre']."forum_forum where type='forum'");
	
	while($result=@mysql_fetch_array($rsupdate))
	{
			$arttype = array();
			$arttype["id"] = $result["fid"];

		if(strpos($guolv_discuzx2,$result["fid"])==0){


			$arttype["typename"] = (strtolower($_config['db']['1']['dbcharset'])=="gbk"?$result["name"]:Newiconv("UTF-8","GBK",$result["name"]));;
			$arttype["typedir"] =  $rootroad."/".$folder_discuzx2."/forum.php?mod=forumdisplay&fid=".$result["fid"];
			$arttype["artlist"] = array();
			
			$rslist = @mysql_query("select tid,subject,dateline from ".$_config['db']['1']['tablepre']."forum_thread where fid='".$result["fid"]."' order by dateline desc limit $limit_article");
				while($resultlist=@mysql_fetch_array($rslist)) {
					
					$newstmp = array();
					$newstmp["id"] = $resultlist["tid"];
					$newstmp["title"] = (strtolower($_config['db']['1']['dbcharset'])=="gbk"?$resultlist["subject"]:Newiconv("UTF-8","GBK",$resultlist["subject"]));
					$newstmp["url"] = $rootroad."/".$folder_discuzx2."/forum.php?mod=viewthread&tid=".$resultlist["tid"];
					$newstmp["date"] = $resultlist["dateline"];
					$arttype["artlist"][] = $newstmp;
				}
				
			//if(count($arttype["artlist"]) >= $limit_article) {
				$newslist[] = $arttype;
			//}
		}
	}
	
}

//wordpress 的整合代码：
if($open_wordpress=="1" && is_file(WEBROOT.$folder_wordpress."/wp-config.php") && $open_dede!="1" ) {
	require_once WEBROOT.$folder_wordpress."/wp-config.php";
	require_once WEBROOT.$folder_wordpress."/wp-includes/link-template.php";
	
	
	$DBlink = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
	if(!$DBlink) 
	{
		echo "数据库连接错误！";
		exit();
	}
	mysql_select_db(DB_NAME,$DBlink);
	mysql_query("SET NAMES ".DB_CHARSET);

	$rsupdate = @mysql_query("select ".$table_prefix."terms.term_id,name from ".$table_prefix."terms inner join ".$table_prefix."term_taxonomy on ".$table_prefix."terms.term_id = ".$table_prefix."term_taxonomy.term_id where taxonomy='category'");
	while($result=@mysql_fetch_array($rsupdate))
	{
			$arttype = array();
			$arttype["id"] = $result["term_id"];

		if(strpos($guolv_wordpress,$arttype["id"])==0){

			$arttype["typename"] = (strtolower(DB_CHARSET)=="gbk"?$result["name"]:Newiconv("UTF-8","GBK",$result["name"]));
			$arttype["typedir"] =  $rootroad."/".$folder_wordpress."/?cat=".$result["term_id"];
			$arttype["typedir"] =  get_category_link($result["term_id"]);
			$arttype["artlist"] = array();
			$sql = "select ID,post_title,UNIX_TIMESTAMP(post_date) as post_date  from ".$table_prefix."posts inner join ".$table_prefix."term_relationships on ".$table_prefix."posts.id=".$table_prefix."term_relationships.object_id where ".$table_prefix."term_relationships.term_taxonomy_id='".$result["term_id"]."' order by ID desc limit $limit_article";
			$rslist = @mysql_query($sql);
				while($resultlist=@mysql_fetch_array($rslist)) {
					
					$newstmp = array();
					$newstmp["id"] = $resultlist["ID"];
					$newstmp["title"] = (strtolower(DB_CHARSET)=="gbk"?$resultlist["post_title"]:Newiconv("UTF-8","GBK",$resultlist["post_title"]));
					$newstmp["url"] = $rootroad."/".$folder_wordpress."/?p=".$resultlist["ID"];
					$newstmp["url"] = get_permalink($resultlist["ID"]);
					$newstmp["date"] = ($resultlist["post_date"]);
					$arttype["artlist"][] = $newstmp;
				}
				
			//if(count($arttype["artlist"]) >= $limit_article) {
				$newslist[] = $arttype;
			//}

		}
	}
	
}

?>