<?php
include(dirname(__FILE__)."/../global.php");


include("../../../include/rnd_pic_fun.php");
include("../../../include/js.php");


$wn_action = 'post';	
	
$fid  = $_POST['fid'];//网站栏目
$module_id    = $_POST['module_id'];//栏目模块
$mid    = $module_id;//栏目模块

if(!is_numeric($fid) || !is_numeric($mid)){
	exit ( 'ID出错' );
}

$cachefile = '../../../cache/cachePostInsert_f-'.$fid.'.php';


//发表成功后将此文章的 id fid mid 输入索引表
$query = "insert into `w6_module_content_index` (`fid`,`mid`) values ('$fid','$module_id')";
$result = mysql_query($query) or die(mysql_error());

$aid   = mysql_insert_id();


//取栏目名称
$query       =  "SELECT * FROM `w6_sort` WHERE `id` = '$fid'";
$result      =  mysql_query($query);
$fname       =  stripslashes(mysql_result($result,"0","name"));

$title       =  addslashes( filtrate ( $_POST["title"] ) );
$posttime    =  time();

$yz       =  addslashes( $_POST["yz"] );
$shows       =  addslashes( $_POST["shows"] );
$top       =  addslashes( $_POST["top"] );
$levels       =  addslashes( $_POST["levels"] );


$saveremoteimg = $_POST['saveremoteimg'];//图片本地化
$bdh_name = $_POST['bdh_name'];//图片本地化
//$message        =   rnd_pic_fun( $saveremoteimg , $message );


if ( !file_exists( $cachefile ) ) {//如果缓存文件不存在则创建缓存

	$query="SELECT * FROM `w6_module` WHERE `module_id`='".$module_id."'";
	$result=mysql_query($query);
	$module_config       =  mysql_result($result,"0","module_config");
					  
	$array = unserialize($module_config);
	
	$text = postInsertMoudle( $array , 'w6_module_content_'.$module_id , $aid , $module_id , $fid , $fname , $title ,$yz ,$shows ,$top ,$levels , $posttime , $saveremoteimg , $bdh_name );
	
	$text = "<?php\n$text";
	
	//执行结尾php语法设置
	$end_php = stripslashes( $array['field_set']['end_php'] );
	$text = $text."\n\n\n\n\n".$end_php;
	
	writetofile_w($cachefile,"$text");
	
	include($cachefile);

}else{

	include($cachefile);

}

$result = mysql_query($query) or die(mysql_error());

$id   = mysql_insert_id();

mysql_query("UPDATE `w6_module_content_$module_id` 
          SET `content_list`='$id' , `adminid`='".$C_126cmsadmindb['adminid']."'
		  WHERE `id`=$id");


if($result){
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<div style=\"font-size: 12px;width: 500px;margin-right: auto;margin-left: auto;margin-top: 100px;text-align: center;\">
  <p> 文章发表成功！ 所属栏目：$fname </p>
  <hr>
  <p>[<a href='$webdb[www_url]/show.php?aid=$aid' target='_blank'>查看文章</a>] [ <a href='post.php?id=$fid&mid=$mid'>继续发表文章</a> ] [<A HREF='../article_modify.php?aid=$aid&fid=$fid&mid=$mid'>点击修改</A>] [<A HREF='../article_list.php?id=$fid&mid=$mid'>返回主题列表</A>] </p>
</div>
";
if($webdb['NewsMakeHtml']=='1'){
	echo "<IFRAME marginWidth=0 marginHeight=0 src='$webdb[www_url]/show.php?job=makeHtml&aid=$aid&updatapage=2' frameBorder=0 width=200 scrolling=no height=50></IFRAME>";//updatapage要生成的静态页数
	//jsGo2Url("$webdb[www_url]/show.php?job=makeHtml&aid=$aid&updatapage=2");//updatapage要生成的静态页数
	exit();
}




}

function postInsertMoudle( $array , $table , $aid , $module_id , $fid , $fname , $title , $yz , $shows , $top , $levels , $posttime , $saveremoteimg , $bdh_name ){
   
    $i=0;//第一次执行，执行完一次自动加一
 foreach($array AS $key=>$rs){

	#####################################################
	//此处编程方法有一定问题，有时会出错
	if( $rs['form_type'] == "select" || $rs['form_type'] == "radio" || $rs['form_type'] == "textarea" || $rs['form_type'] == "upfile" || $rs['form_type'] == "upfile_big" ){
		$rs['form_type'] = "text";
	}
  switch ( $rs['form_type'] ) {
  
    case "checkbox":
	
	    if($i==0){
		  $_show      = "\$".$rs['field_id']."   =  \$_POST[\"".$rs['field_id']."\"];\n";
		  $_show      .= "\$".$rs['field_id']."   =  implode('|',\$".$rs['field_id'].");\n";
	      $_show_sql1 = "`".$rs['field_id']."`";
		  $_show_sql2 = "'\".\$".$rs['field_id'].".\"'";
		}else{
		  $_show      .= "\$".$rs['field_id']."   =  \$_POST[\"".$rs['field_id']."\"];\n";
		  $_show      .= "\$".$rs['field_id']."   =  implode('|',\$".$rs['field_id'].");\n";
	      $_show_sql1 .= ",`".$rs['field_id']."`";
		  $_show_sql2 .= ",'\".\$".$rs['field_id'].".\"'";
		}
		
    $i++;
	break;

    case "text"://一样的还有 select radio ieedit 
	
	    if($i==0){
		  $_show      = "\$".$rs['field_id']."   =  addslashes( filtrate ( \$_POST[\"".$rs['field_id']."\"] ));\n";
	      $_show_sql1 = "`".$rs['field_id']."`";
		  $_show_sql2 = "'\".\$".$rs['field_id'].".\"'";
		}else{
		  $_show      .= "\$".$rs['field_id']."   =  addslashes( filtrate ( \$_POST[\"".$rs['field_id']."\"] ));\n";
	      $_show_sql1 .= ",`".$rs['field_id']."`";
		  $_show_sql2 .= ",'\".\$".$rs['field_id'].".\"'";
		}
		
    $i++;
	break;

    case "ieedit":
	
	    if($i==0){
		  $_show      = "\$".$rs['field_id']."   =  str_replace( \$webdb['www_url'] , \$web126_imgUrl , \$_POST[\"".$rs['field_id']."\"] ) ;\n";//网址替换成特定字符
		  $_show      .= "\$".$rs['field_id']."   =  addslashes( str_replace( \$tpbdh_imgUrl2 , \$tpbdh_imgUrl , \$".$rs['field_id']." ) );\n";//网址替换成特定字符
	      $_show_sql1 = "`".$rs['field_id']."`";
		  $_show_sql2 = "'\".\$".$rs['field_id'].".\"'";
		  
		}else{
		  $_show      .= "\$".$rs['field_id']."   =  str_replace( \$webdb['www_url'] , \$web126_imgUrl , \$_POST[\"".$rs['field_id']."\"] ) ;\n";//网址替换成特定字符
		  $_show      .= "\$".$rs['field_id']."   =  addslashes( str_replace( \$tpbdh_imgUrl2 , \$tpbdh_imgUrl , \$".$rs['field_id']." ) );\n";//网址替换成特定字符
	      $_show_sql1 .= ",`".$rs['field_id']."`";
		  $_show_sql2 .= ",'\".\$".$rs['field_id'].".\"'";
		  
		}
		
    $i++;
	break;
	
	case "upphoto":
	
		if($i==0){

		  
		  //判断，如果不是后台非正常上传，发果有非地址的代码要去掉，征对火车头采集器的功能加强
		  eval("\$upphoto_field_id = \"s_$rs[field_id]\";");
		  if( substr( $_POST["$upphoto_field_id"] , 0 ,1 ) == "<" ){
		  
		  $_show      = "preg_match_all(\"!src='?\\\"?([^   >\\\"']+)\\\"?'?!is\",\$_POST[\"s_".$rs['field_id']."\"],\$s_".$rs['field_id']."_array);\n";
		  $_show      .= "\$s_".$rs['field_id']."_array = array_unique(\$s_".$rs['field_id']."_array[1]);\n";
		  $_show      .= "\$s_".$rs['field_id']." = \$s_".$rs['field_id']."_array[0];\n";
		  
		  $_show      .= "\$".$rs['field_id']."   =  addslashes(\$s_".$rs['field_id'].").'@@@'.addslashes(\$_POST[\"b_".$rs['field_id']."\"]);\n";
		  
		  }else{
		  
		  $_show      = "\$".$rs['field_id']."   =  addslashes(\$_POST[\"s_".$rs['field_id']."\"]).'@@@'.addslashes(\$_POST[\"b_".$rs['field_id']."\"]);\n";
		  
		  }
		  
	      $_show_sql1 = "`".$rs['field_id']."`";
		  $_show_sql2 = "'\".\$".$rs['field_id'].".\"'";
		  
		}else{
		
		  eval("\$upphoto_field_id = \"s_$rs[field_id]\";");
		  if( substr( $_POST["$upphoto_field_id"] , 0 ,1 ) == "<" ){
		  
		  $_show      = "preg_match_all(\"!src='?\\\"?([^   >\\\"']+)\\\"?'?!is\",\$_POST[\"s_".$rs['field_id']."\"],\$s_".$rs['field_id']."_array);\n";
		  $_show      .= "\$s_".$rs['field_id']."_array = array_unique(\$s_".$rs['field_id']."_array[1]);\n";
		  $_show      .= "\$s_".$rs['field_id']." = \$s_".$rs['field_id']."_array[0];\n";
		  
		  $_show      .= "\$".$rs['field_id']."   =  addslashes(\$s_".$rs['field_id'].").'@@@'.addslashes(\$_POST[\"b_".$rs['field_id']."\"]);\n";
		  
		  }else{
		  
		  $_show      = "\$".$rs['field_id']."   =  addslashes(\$_POST[\"s_".$rs['field_id']."\"]).'@@@'.addslashes(\$_POST[\"b_".$rs['field_id']."\"]);\n";
		  		  
		  }
		  
	      $_show_sql1 .= ",`".$rs['field_id']."`";
		  $_show_sql2 .= ",'\".\$".$rs['field_id'].".\"'";
		}
	
    $i++;
	break;
	
  }
  
 } 
  
  $show       = $_show;
  $show_sql1  = $_show_sql1;
  $show_sql2  = $_show_sql2;
  $show_sql   = "\$query = \"insert into `$table`
                          (`aid`,`module_id`,`fid`,`fname`,`title`,`yz`,`shows`,`top`,`levels`,`posttime`,$show_sql1)
                  values
                         ('\$aid','\$module_id','\$fid','\$fname','\$title','\$yz','\$shows','\$top','\$levels','\$posttime',$show_sql2) \";";
						 
  if($saveremoteimg == "1"){
  $str_tpbdh = "\$".$bdh_name." = rnd_pic_fun( ".$saveremoteimg." , "."\$".$bdh_name." );\n";//图片本地化
  }
  
  

  $text       = $show.$str_tpbdh.$show_sql;
  return $text;
  
}


function writetofile_w($filename,$book)
{
$fd=fopen("$filename","w");
fwrite($fd,$book);
fclose($fd);
return true;
}

?>