<?
$style = $webdb['style'];//所使用的网站风格



//导航菜单
function get_menudb(){
global $webdb;
$query="SELECT * FROM `w6_menu` WHERE `hide` = '0' ORDER BY `list` DESC";
$result=mysql_query($query) or die("Could not connect: " . mysql_error());

$i = 0;
while($rs=mysql_fetch_array($result)){
	  $menu[$i]['name'] = $rs['name'];
	  $menu[$i]['linkurl'] = $rs['linkurl'];
	  if($menu[$i]['linkurl'] == '/'){ $menu[$i]['linkurl'] = str_replace('/',$webdb['www_url'],$menu[$i]['linkurl']); }
	  $menu[$i]['color'] = $rs['color'];
	  $menu[$i]['target'] = $rs['target'];
	  $menu[$i]['m_bz_1'] = $rs['m_bz_1'];
	  $menu[$i]['m_bz_2'] = $rs['m_bz_2'];
	  
	  $i++;
	}
	
	return $menu;

}	

/*
作用：取得栏目列表数组
使用方法：
$fid = 70;
$mid = 32;
$hot_db = get_listdb( $fid ,' ORDER BY `count` DESC LIMIT 0 , 8 ' );
*/
function get_listdb( $fid , $sql = '' , $qm_sql = "`shows` = '1' AND" ){
	global $webdb,$web126_imgUrl,$tpbdh_imgUrl,$tpbdh_imgUrl2;
	//本函数使用的缓存文件
	//$cachefile = './cache/cache_'.make_3_rand().'.php';
	$cachefile = './cache/cache_list_f-'.$fid.'.php';
	
	if ( !file_exists( $cachefile ) ) {//如果缓存文件不存在则创建缓存
	
	
	
	
		//取得 mid
	$query="SELECT * FROM `w6_sort` WHERE `id`='".$fid."'";
	$result=mysql_query($query);
	$mid       =  mysql_result($result,"0","mid");
	//取type
	$type       =  mysql_result($result,"0","type");

	//先取得本栏目需解析的字段
	$query="SELECT * FROM `w6_module` WHERE `module_id`='".$mid."'";
	$result=mysql_query($query);
	$module_config       =  mysql_result($result,"0","module_config");
	$array = unserialize($module_config);

	unset($strdb);
	$set_str_s_b_photo = '';//是否有图片缩略图字段存在
	$strdb[]= '$listdb[$i][\'title\'] = $rs[\'title\']';
	$strdb[]= '$listdb[$i][\'count\'] = $rs[\'count\']';
	$strdb[]= '$listdb[$i][\'posttime\'] = date("Y-m-d H:i",$rs[\'posttime\'])';
	foreach( $array AS $key=>$value){
			$strdb[]='$listdb[$i][\''.$value['field_id'].'\'] = stripslashes($rs[\''.$value['field_id'].'\'])';
			
			if( $value['form_type'] == 'upphoto' )
			{
				$set_str_s_b_photo = '1';//是否有图片缩略图字段存在
				$str_s_b_photo = '$s_b_photo_file = explode( "@@@" , $rs[\''.$value['field_id'].'\'] );
				$listdb[$i][\'s_'.$value['field_id'].'\'] = $s_b_photo_file[\'0\'];
				$listdb[$i][\'b_'.$value['field_id'].'\'] = $s_b_photo_file[\'1\'];';
				//将图片地址完整化 如不是http开头说明是本站地f址 前面加上 http:// $webdb['www_url'] /upload/
				//if( !eregi("^http://",$url) ) $playurl = 'http://'.$webdb['www_url'].'/upload/'.$url;
				$str_s_b_photo .= '
				if( !eregi("^http://",$listdb[$i][\'s_'.$value['field_id'].'\']) )$listdb[$i][\'s_'.$value['field_id'].'\'] = \''.$webdb['www_url'].'/upload/\'.$listdb[$i][\'s_'.$value['field_id'].'\'];
				if( !eregi("^http://",$listdb[$i][\'b_'.$value['field_id'].'\']) )$listdb[$i][\'b_'.$value['field_id'].'\'] = \''.$webdb['www_url'].'/upload/\'.$listdb[$i][\'b_'.$value['field_id'].'\'];
				';
			}
			
			if( $value['form_type'] == 'upfile' || $value['form_type'] == 'upfile_big' )
			{
				$set_str_s_b_photo = '1';//是否有图片缩略图字段存在
				//将图片地址完整化 如不是http开头说明是本站地f址 前面加上 http:// $webdb['www_url'] /upload/
				//if( !eregi("^http://",$url) ) $playurl = 'http://'.$webdb['www_url'].'/upload/'.$url;
				$str_s_b_photo .= '
				if( !eregi("^http://",$listdb[$i][\''.$value['field_id'].'\']) )$listdb[$i][\''.$value['field_id'].'\'] = \''.$webdb['www_url'].'/upload/\'.$listdb[$i][\''.$value['field_id'].'\'];
				';
			}
			
			if( $value['form_type'] == 'ieedit' )
			{
				$set_str_web_ieedit = '1';
				
				//图片本地化的网络地址还原
				$str_ieedit .= "\$listdb[\$i]['". $value['field_id'] . "']" ." = str_replace('$tpbdh_imgUrl','$tpbdh_imgUrl2',". "\$listdb[\$i]['" .$value['field_id'] . "']" .");";
				
				$str_ieedit .= "\$listdb[\$i]['". $value['field_id'] . "']" ." = str_replace('$web126_imgUrl','".$webdb['www_url']."',". "\$listdb[\$i]['" .$value['field_id'] . "']" .");";//图片网络地址还原
				
				
			}
			
	}
		$str=implode(";",$strdb).";";
		
		if( $set_str_s_b_photo == '1' || $set_str_web_ieedit == '1' ){//是否有图片缩略图字段存在
			$str = $str.$str_s_b_photo.$str_ieedit;
		}
		
		$str='$i = 0;
	while($rs=mysql_fetch_array($result)){
		  $listdb[$i][\'aid\'] = $rs[\'aid\'];'.$str.'
		  $i++;
	}
		  ';
		  
		
		//w6_sort 表加入 type 字段 1 - 大分类(不可发内容)  0 - 小栏目 2 - 单篇文章(一个栏目即一篇文章，适合于作公司联系方式，公司介绍等)
	//取数据  SELECT * FROM `w6_module_content_32` WHERE `fid` = 72 || `fid` = 70 
	$fids_str = '';
	if( $type == "1" ){
		$fids = getFid ( $fid );
		//将数组转换为字串
		foreach($fids AS $value){
			$fids_str .= " || `fid` = '".$value."' ";
		}
		
	}////////////////////////
	
		  
	$str0 = "\$result=mysql_query(\"SELECT * FROM `w6_module_content_$mid` WHERE $qm_sql ( `fid` = '$fid' $fids_str ) \$sql\") or die(\"Could not connect: \" . mysql_error());";
	$str="<?php\n".$str0.$str;
	writetofile_w( $cachefile , $str );



	
	
	
	///如果缓存文件不存在则创建缓存	
	}


	include($cachefile);

	
	//删除缓存文件
	//unlink($cachefile);
	return $listdb;
}

/*
作用：取得 专题 栏目列表数组
使用方法：
$fid = 70;
$mid = 32;
$hot_db = get_listdb( $fid ,' ORDER BY `count` DESC LIMIT 0 , 8 ' );
*/
function get_Special_listdb( $fid , $sql ,$star_page ){
	global $webdb;
	//本函数使用的缓存文件
	//$cachefile = './cache/cache_'.make_3_rand().'.php';
	$cachefile = './cache/cache_list_f-'.$fid.'.php';
	
	if ( !file_exists( $cachefile ) ) {//如果缓存文件不存在则创建缓存
	
	
	
		//取得 mid
	$query="SELECT * FROM `w6_sort` WHERE `id`='".$fid."'";
	$result=mysql_query($query);
	$mid       =  mysql_result($result,"0","mid");
	//取type
	$type       =  mysql_result($result,"0","type");

	//先取得本栏目需解析的字段
	$query="SELECT * FROM `w6_module` WHERE `module_id`='".$mid."'";
	$result=mysql_query($query);
	$module_config       =  mysql_result($result,"0","module_config");
	$array = unserialize($module_config);

	unset($strdb);
	$set_str_s_b_photo = '';//是否有图片缩略图字段存在
	$strdb[]= '$listdb[$i][\'title\'] = $rs[\'title\']';
	$strdb[]= '$listdb[$i][\'count\'] = $rs[\'count\']';
	$strdb[]= '$listdb[$i][\'posttime\'] = date("Y-m-d H:i",$rs[\'posttime\']);';
	foreach( $array AS $key=>$value){
			$strdb[]='$listdb[$i][\''.$value['field_id'].'\'] = $rs[\''.$value['field_id'].'\']';
			
			if( $value['form_type'] == 'upphoto' )
			{
				$set_str_s_b_photo = '1';//是否有图片缩略图字段存在
				$str_s_b_photo = '$s_b_photo_file = explode( "@@@" , $rs[\''.$value['field_id'].'\'] );
				$listdb[$i][\'s_'.$value['field_id'].'\'] = $s_b_photo_file[\'0\'];
				$listdb[$i][\'b_'.$value['field_id'].'\'] = $s_b_photo_file[\'1\'];';
				//将图片地址完整化 如不是http开头说明是本站地f址 前面加上 http:// $webdb['www_url'] /upload/
				//if( !eregi("^http://",$url) ) $playurl = 'http://'.$webdb['www_url'].'/upload/'.$url;
				$str_s_b_photo .= '
				if( !eregi("^http://",$listdb[$i][\'s_'.$value['field_id'].'\']) )$listdb[$i][\'s_'.$value['field_id'].'\'] = \''.$webdb['www_url'].'/upload/\'.$listdb[$i][\'s_'.$value['field_id'].'\'];
				if( !eregi("^http://",$listdb[$i][\'b_'.$value['field_id'].'\']) )$listdb[$i][\'b_'.$value['field_id'].'\'] = \''.$webdb['www_url'].'/upload/\'.$listdb[$i][\'b_'.$value['field_id'].'\'];
				';
			}
			
			if( $value['form_type'] == 'upfile' || $value['form_type'] == 'upfile_big' )
			{
				$set_str_s_b_photo = '1';//是否有图片缩略图字段存在
				//将图片地址完整化 如不是http开头说明是本站地f址 前面加上 http:// $webdb['www_url'] /upload/
				//if( !eregi("^http://",$url) ) $playurl = 'http://'.$webdb['www_url'].'/upload/'.$url;
				$str_s_b_photo .= '
				if( !eregi("^http://",$listdb[$i][\''.$value['field_id'].'\']) )$listdb[$i][\''.$value['field_id'].'\'] = \''.$webdb['www_url'].'/upload/\'.$listdb[$i][\''.$value['field_id'].'\'];
				';
			}
	}
		$str=implode(";",$strdb).";";
		
		if( $set_str_s_b_photo == '1' ){//是否有图片缩略图字段存在
			$str = $str.$str_s_b_photo;
		}
		
		$str='$i = 0;
	while($rs=mysql_fetch_array($result)){
		  $listdb[$i][\'aid\'] = $rs[\'aid\'];'.$str.'
		  $i++;
	}
		  ';
		  
	/*	
		//w6_sort 表加入 type 字段 1 - 大分类(不可发内容)  0 - 小栏目 2 - 单篇文章(一个栏目即一篇文章，适合于作公司联系方式，公司介绍等)
	//取数据  SELECT * FROM `w6_module_content_32` WHERE `fid` = 72 || `fid` = 70 
	$fids_str = '';
	if( $type == "1" ){
		$fids = getFid ( $fid );
		//将数组转换为字串
		foreach($fids AS $value){
			$fids_str .= " || `fid` = '".$value."' ";
		}
		
	}////////////////////////
	*/
		  
	$str0 = "\$result=mysql_query(\"$sql\");";
	$str="<?php\n".$str0.$str;
	writetofile_w( $cachefile , $str );



	
	
	
	///如果缓存文件不存在则创建缓存	
	}


	include($cachefile);

	
	//删除缓存文件
	//unlink($cachefile);
	return $listdb;
}


/*
作用：取得内容页数组
使用方法：
$fid = 70;
$mid = 32;
$hot_db = get_listdb( $fid ,' ORDER BY `count` DESC LIMIT 0 , 8 ' );
*/
function get_showdb( $aid , $mid ){
	global $webdb,$web126_imgUrl,$tpbdh_imgUrl,$tpbdh_imgUrl2;
	//本函数使用的缓存文件
	//$cachefile = './cache/cache_'.make_3_rand().'.php';
	$cachefile = './cache/cache_show_m-'.$mid.'.php';
	
	if ( !file_exists( $cachefile ) ) {//如果缓存文件不存在则创建缓存



	//先取得本栏目需解析的字段
	$query="SELECT * FROM `w6_module` WHERE `module_id`='".$mid."'";
	$result=mysql_query($query);
	$module_config       =  mysql_result($result,"0","module_config");
	$array = unserialize($module_config);
	
	//去除 field_set 元素
	unset($array[field_set]);
    //print_r($array);

	unset($strdb);
	$set_str_s_b_photo = '';//是否有图片缩略图字段存在
	$set_str_web_description ='';//文章简介
	$strdb[]= '$showdb[\'title\']        =  stripslashes(mysql_result($result,"0","title"))';
	$strdb[]= '$showdb[\'count\']        =  stripslashes(mysql_result($result,"0","count"))';
	$strdb[]= '$showdb[\'posttime\']        =  stripslashes(mysql_result($result,"0","posttime"))';
	foreach( $array AS $key=>$value){
			$strdb[]='$showdb[\''.$value['field_id'].'\']	=	stripslashes(mysql_result($result,"0","'.$value['field_id'].'"))';
			
			if( $value['form_type'] == 'upphoto' )
			{
				$set_str_s_b_photo = '1';//是否有图片缩略图字段存在
				$str_s_b_photo = '$s_b_photo_file = explode( "@@@" , $showdb[\''.$value['field_id'].'\'] );
				$showdb[\'s_'.$value['field_id'].'\'] = $s_b_photo_file[\'0\'];
				$showdb[\'b_'.$value['field_id'].'\'] = $s_b_photo_file[\'1\'];';
				if( !eregi("^http://",$url) ) $playurl = $webdb['www_url'].'/upload/'.$url;
				$str_s_b_photo .= '
				if( !eregi("^http://", $showdb[\'s_'.$value['field_id'].'\'] ) ) $showdb[\'s_'.$value['field_id'].'\'] = \''.$webdb['www_url'].'/upload/\'.$showdb[\'s_'.$value['field_id'].'\'];
				if( !eregi("^http://", $showdb[\'b_'.$value['field_id'].'\'] ) ) $showdb[\'b_'.$value['field_id'].'\'] = \''.$webdb['www_url'].'/upload/\'.$showdb[\'b_'.$value['field_id'].'\'];
				';
			}
			
			if( $value['form_type'] == 'upfile' || $value['form_type'] == 'upfile_big' )
			{
				$set_str_s_b_photo = '1';//是否有图片缩略图字段存在
				//将图片地址完整化 如不是http开头说明是本站地f址 前面加上 http:// $webdb['www_url'] /upload/
				//if( !eregi("^http://",$url) ) $playurl = 'http://'.$webdb['www_url'].'/upload/'.$url;
				$str_s_b_photo .= '
				if( !eregi("^http://",$showdb[\''.$value['field_id'].'\']) )$showdb[\''.$value['field_id'].'\'] = \''.$webdb['www_url'].'/upload/\'.$showdb[\''.$value['field_id'].'\'];
				';
			}
			
			if( $value['form_type'] == 'ieedit' )
			{
				$set_str_web_ieedit = '1';
				
				//图片本地化的网络地址还原
				$str_ieedit .= "\$showdb['". $value['field_id'] . "']" ." = str_replace('$tpbdh_imgUrl','$tpbdh_imgUrl2',". "\$showdb['" .$value['field_id'] . "']" .");";
				
				$str_ieedit .= '$showdb[\''.$value['field_id'].'\'] = str_replace(\''.$web126_imgUrl.'\',\''.$webdb['www_url'].'\',$showdb[\''.$value['field_id'].'\']);';//图片网络地址还原
				
				
			}
			
			if( $value['metashow'] == '1' )
			{
				//在展示页的php文件中会处理字数
				$set_str_web_description = '1';
				$str_web_description .= '$showdb[\''.$value['field_id'].'\'] = str_replace(\'www_126cms_com\',substr(\''.$webdb['www_url'].'\',7,22),$showdb[\''.$value['field_id'].'\']);';
				$str_web_description .= '$showdb[\'web_description\'] .= $showdb[\''.$value['field_id'].'\'];';
			}

	}
		$str=implode(";",$strdb).";";
		
		if( $set_str_s_b_photo == '1' || $set_str_web_description == '1' || $set_str_web_ieedit == '1' ){//是否有图片缩略图字段存在
			$str = $str.$str_s_b_photo.$str_ieedit.$str_web_description;
		}


	//取数据
	$str0 = "\$result=mysql_query(\"SELECT * FROM `w6_module_content_".$mid."` WHERE `aid` = '\$aid'\") or die(\"Could not connect: \" . mysql_error());";
	
	$str="<?php\n".$str0.$str;
	writetofile_w( $cachefile , $str );
	
	
	
	
	
	///如果缓存文件不存在则创建缓存	
	}
	

	include($cachefile);
	
	$showdb['posttime'] = date("Y-m-d H:i",$showdb['posttime']);

	
	//删除缓存文件
	//unlink($cachefile);
	return $showdb;

}

//从全部数组中取相似内容
function getSimilar($title,$array,$num='')//$title当前标题，$arrayTitle为需要查找的数组 
{ 
	foreach( $array AS $key=>$rs){	
		@$array2[$rs['aid']]=$rs;	
	}

	//数据调序
	foreach($array2 AS $key=>$rs){
		$rs['similar_num'] = similar_text($rs['title'],$title);//取得两个字符串相似的字节数
		$row[$rs['aid']] = $rs; //以 aid 为键的所有元素
		$row_ch[$rs['aid']] = $rs['similar_num'];	//以 aid 为键 相似值为内容 的所有元素
	}

	@arsort($row_ch);//按照相似的字节数由高到低排序

	foreach($row_ch AS $key=>$value){//数据调序
		@$list_db[$key] = $row[$key];
	}
	
	if($num){//取出前几个元素
		$listdb = array_slice($list_db, 0, $num);  
	}else{
		$listdb = $list_db;;
	}
	
  return $listdb;
  
}
function getSimilar_num( $num='8' ){
	global $show_db,$all_list_db;
	$listdb = getSimilar( $show_db['title'] , $all_list_db , $num );
	return $listdb;
}

function include126cmsTpl($str_tpl){
	global $webdb,$style;
	echo '126cms-->';
	include("./tpl/$style/".$str_tpl);
	echo '<!--126cms';
}
//静态链接处理
function make_list_html_Function($fid,$linkcode=''){
	global $webdb;
	
	//取得 list_html
	$query     =  "SELECT id,list_html FROM `w6_sort` WHERE `id`='".$fid."'";
	$result    =  mysql_query($query);
	$list_html =  mysql_result($result,"0","list_html");
	$list_html = str_replace('{$fid}',$fid,$list_html);
	$dir_array = explode('/',$list_html);
	$count =count($dir_array);
	
	$dir = '/';
	for($i=0;$i<$count;$i++){
		if( $i < ($count-1) ){
			$dir .= $dir_array[$i].'/';
		}
	}
		
		$dirfile   =  $dir;

	if($linkcode == ' href='){
		$linkcode = ' href="'.$webdb['www_url'];
	}else{
		$linkcode=str_replace(' href=',' href="',$linkcode);
	}
	
	$url = $linkcode.$dirfile;

	return $url;
}
//伪静态链接处理
function make_list_WJT_Function($fid,$linkcode=''){
	global $webdb;
	
		$webdb['listWJT'] = str_replace("-{\$page}", "", $webdb['listWJT']);
		$dirfile = '/'.$webdb['listWJT'];
		eval("\$dirfile = \"$dirfile\";");

	if($linkcode == ' href='){
		$linkcode = ' href="'.$webdb['www_url'];
	}else{
		$linkcode=str_replace(' href=',' href="',$linkcode);
	}
	
	$url = $linkcode.$dirfile;

	return $url;
}


//列表页有分页
function make_list_page_html_Function($fid,$page,$linkcode=''){
	global $webdb;
	
	//取得 list_html
	$query     =  "SELECT id,list_html FROM `w6_sort` WHERE `id`='".$fid."'";
	$result    =  mysql_query($query);
	$list_html =  mysql_result($result,"0","list_html");
	
	$list_html = str_replace('{$fid}',$fid,$list_html);
	$dir_array = explode('/',$list_html);
	$count =count($dir_array);
	
	$dir = '/';
	for($i=0;$i<$count;$i++){
		if( $i < ($count-1) ){
			$dir .= $dir_array[$i].'/';
		}else{
			$filename = $dir_array[$i];
		}
	}
		if($page == 1){
			$filename = '';
		}else{
			$filename = str_replace( '{$page}' , 'index-'.$page , $filename );
		}
		
		$dirfile   =  $dir . $filename;

	if($linkcode == ' href='){
		$linkcode = ' href="'.$webdb['www_url'];
	}else{
		$linkcode=str_replace(' href=',' href="',$linkcode);
	}
	
	$url = $linkcode.$dirfile;

	return $url;
}
//列表页有分页
function make_list_page_WJT_Function($fid,$page,$linkcode=''){
	global $webdb;
	
		$dirfile = '/'.$webdb['listWJT'];
		eval("\$dirfile = \"$dirfile\";");

	if($linkcode == ' href='){
		$linkcode = ' href="'.$webdb['www_url'];
	}else{
		$linkcode=str_replace(' href=',' href="',$linkcode);
	}
	
	$url = $linkcode.$dirfile;

	return $url;
}

function make_show_html_Function($aid,$linkcode=''){
	global $webdb;
	
	//取得 fid
	$query   =  "SELECT id,fid FROM `w6_module_content_index` WHERE `id`='".$aid."'";
	$result  =  mysql_query($query);
	$fid     =  mysql_result($result,"0","fid");
	
	//取得 list_html
	$query     =  "SELECT id,list_html,spe_htmlname,spe_htmlname_set FROM `w6_sort` WHERE `id`='".$fid."'";
	$result    =  mysql_query($query);
	$list_html =  mysql_result($result,"0","list_html");
	$spe_htmlname_set   =  stripslashes(mysql_result($result,"0","spe_htmlname_set"));//开启特殊样式
	$spe_htmlname   =  stripslashes(mysql_result($result,"0","spe_htmlname"));
	
	$list_html = str_replace('{$fid}',$fid,$list_html);
	$list_html = str_replace('list','s',$list_html);
	$dir_array = explode('/',$list_html);
	$count =count($dir_array);
	
	$dir = '/';
	for($i=0;$i<$count;$i++){
		if( $i < ($count-1) ){
			$dir .= $dir_array[$i].'/';
		}else{
			$filename = $dir_array[$i];
		}
	}
	//如果文件名开启特殊样式
	eval("\$spe_htmlname = \"\$$spe_htmlname\";");
	if( $spe_htmlname_set == 1 ){
	$filename = str_replace( '{$page}' , $spe_htmlname , $filename );
	}else{
	$filename = str_replace( '{$page}' , $aid , $filename );
	}

	$dirfile   =  $dir . $filename;

	if($linkcode == ' href='){
		$linkcode = ' href="'.$webdb['www_url'];
	}else{
		$linkcode=str_replace(' href=',' href="',$linkcode);
	}
	
	$url = $linkcode.$dirfile;

	return $url;
}

function make_show_WJT_Function($aid,$linkcode=''){
	global $webdb;
	
		$dirfile = '/'.$webdb['showWJT'];
		eval("\$dirfile = \"$dirfile\";");

	if($linkcode == ' href='){
		$linkcode = ' href="'.$webdb['www_url'];
	}else{
		$linkcode=str_replace(' href=',' href="',$linkcode);
	}
	
	$url = $linkcode.$dirfile;

	return $url;
}



//写入文件,当文件存在时替换
function writetofile_w($filename,$book)
{
$fd=fopen("$filename","w");
fwrite($fd,$book);
fclose($fd);
return true;
}