<?php

/*

文件名
ETpost.php
php168 v4.0文章发布接口文件，请复制在admin目录下使用，为保密，请自行修改文件名
（发布成功返回1，失败时请在ET采集监视窗浏览错误原因）

主要参数
loginname　　　　　会员名
loginpwd　　　　　 密码
postdb[title]　　　 主题标题
postdb[content]　　内容

附加参数
postdb[fid]　　　　栏目ID

可选参数
postdb[titlecolor]　标题颜色
postdb[fonttype]　　标题是否加粗，1为是
spid　　　　　　　　专题ID 
postdb[keywords]　　关键字
addkeyword　　　　　是否保存关键字，1为是
postdb[author]　　　文章作者
postdb[copyfrom]　　　文章来源
postdb[copyfromurl]　 转载网址
postdb[hits]　　　　　点击数
postdb[description]　 文章简介
postdb[top]　　　　　 是否置顶，1为是
postdb[levels]　　　　是否推荐，1为是
postdb[target]　　　　是否新窗口打开，1为是
GetOutPic　　　　　　 是否远程抓图，1为是（网速慢时，很影响速度）
ExplodePage　　　　　 是否自动分页，1为是（当为0时，如果ET采集了分页，则按ET分页标记<!--subpage-->分页，如果不想按此分页，可以在过滤规则中过滤此标记）
PageNum　　　　　　　 自动分页字数




ET发布配置-文章检查网址处，可以如下填写
http://您的网址/admin/ETpost.php



*/




require("../inc/common.inc.php");
$ForceEnter=0;	//把0改成1将强制进入后台

require(PHP168_PATH."inc/class.inc.php");
@include(PHP168_PATH."php168/level.php");
$Guidedb=new Guide_DB;

$action="postnew";
$jobs="usehtml";

/*用户登录*/
if( $_POST[loginname] && $_POST[loginpwd] )
{
/*	if( $webdb[yzImgAdminLogin] ){
		if(!get_cookie("yzImgNum")||get_cookie("yzImgNum")!=$yzimg){
			die("<A HREF=?>验证码不符合</A>");
		}else{
			set_cookie("yzImgNum","");
		}
	}*/
	$rs=$db->get_one("SELECT M.$TB[username] AS username,M.$TB[password] AS password,D.* FROM $TB[table] M LEFT JOIN {$pre}memberdata D ON M.$TB[uid]=D.uid WHERE M.$TB[username]='$_POST[loginname]' ");
	if(!$rs){
		login_logs($_POST[loginname],$_POST[loginpwd]);
		setcookie("Admin",'',0,"/");
		die("<A HREF=?>用户不存在</A>");
	}elseif( pwd_md5($_POST[loginpwd]) != $rs[password] ){
		login_logs($_POST[loginname],$_POST[loginpwd]);
		setcookie("Admin",'',0,"/");
		die("<A HREF=?>密码不正确</A>");
	}elseif(!$rs[uid]){
		Add_memberdata($_POST[loginname]);
	}else{
		login_logs($_POST[loginname],md5($_POST[loginpwd]));
		$_COOKIE[Admin]="$rs[uid]\t".mymd5($rs[password]);
		
		//ET增加
		$admin_uid=$rs[uid];
		$admin_pwd=mymd5($rs[password]);
		//ET增加
		
		//@include(PHP168_PATH."cache/warn.php");
		setcookie("Admin",$_COOKIE[Admin],0,"/");
	}
}
/*退出*/
/*if($action=='quite'){
	setcookie("Admin",'',0,"/");
	echo "<SCRIPT LANGUAGE=\"JavaScript\">
	<!--
	window.top.location.href='$webdb[www_url]';
	//-->
	</SCRIPT>";
	die("");
}
*/
//list($admin_uid,$admin_pwd)=explode("\t",$_COOKIE[Admin]);
unset($userdb);
if($admin_uid&&$admin_pwd)
{
	$userdb=$db->get_one("SELECT M.$TB[username] AS username,M.$TB[password] AS password,D.* FROM $TB[table] M LEFT JOIN {$pre}memberdata D ON M.$TB[uid]=D.uid WHERE M.$TB[uid]='$admin_uid' ");
	if($userdb && mymd5($userdb[password])==$admin_pwd ){
		//为处理标签的问题
		//setcookie("Admin",$_COOKIE[Admin],$timestamp+1800,"/");
		$admin_name=$founder='';
		@include(PHP168_PATH."php168/admin.php");
		//创始人
		if($admin_name==$userdb[username])
		{
			$founder=1;	//创始人权限
			require(PHP168_PATH."php168/group/3.php");
			$Apower=@unserialize($groupdb[allowadmindb]);
		}
		elseif($userdb[groupid]&&file_exists(PHP168_PATH."php168/group/$userdb[groupid].php"))
		{
			require(PHP168_PATH."php168/group/$userdb[groupid].php");
			if(!$groupdb['allowadmin']){
				setcookie("Admin",'',0,"/");
				die("你当前所在用户组,系统设置无权访问");
			}else{
				$Apower=@unserialize($groupdb[allowadmindb]);
			}
		}
		else
		{
			setcookie("Admin",'',0,"/");
			die("你当前所在用户组,无权访问");
		}
	}else{
		setcookie("Admin",'',0,"/");
		die("<A HREF='index.php?iframe=1'>请输入正确密码帐号再访问</A>");
	}
}

if($ForceEnter==1){
	require(PHP168_PATH."php168/group/3.php");
	$Apower=@unserialize($groupdb[allowadmindb]);
}elseif(!$userdb){
	include './template/login.htm';
	exit;
}

function login_logs($username,$password){
	global $timestamp,$onlineip;
	$logdb[]="$username\t$password\t$timestamp\t$onlineip";
	@include(PHP168_PATH."cache/adminlogin_logs.php");
	$writefile="<?php	\r\n";
	$jj=0;
	foreach($logdb AS $key=>$value){
		$jj++;
		$writefile.="\$logdb[]=\"$value\";\r\n";
		if($jj>200){
			break;
		}
	}
	write_file(PHP168_PATH."cache/adminlogin_logs.php",$writefile);
}

function jump($msg,$url,$time=1){
	if($time==0){
		header("location:$url");exit;
	}else{
		require("template/location.htm");exit;
	}
}
function showmsg($msg){
	require("template/showmsg.htm");exit;
}
/**
*会员用户组选择列表
**/
function select_group($names='gid',$ck='',$url=''){
	global $db,$pre;
	if($url) 
	$reto=" onchange=\"window.location=('{$url}&{$names}='+this.options[this.selectedIndex].value+'')\"";
	$show="<select name='$names' $reto><option value='' selected>现有用户组</option>";
	$query=$db->query("SELECT * FROM `{$pre}group` WHERE gptype!='0' ");
	while($array=$db->fetch_array($query)){
        $ck==$array[gid]?$ckk='selected':$ckk='';
		$show.="  <option value='$array[gid]' $ckk>{$array[grouptitle]}</option>";
     }
	$query=$db->query("SELECT * FROM `{$pre}group` WHERE gptype='0' ORDER BY levelnum");
	$show.="  <option value=''>--+以上是系统组，以下是会员组+--</option>";
	while($array=$db->fetch_array($query)){
        $ck==$array[gid]?$ckk='selected':$ckk='';
		$show.="  <option value='$array[gid]' $ckk>{$array[grouptitle]}</option>";
     }
     return $show." </select>";
}

/**
*风格选择列表
**/
function select_style($name='stylekey',$ck='',$url='',$select=''){
	if($url) 
	$reto=" onchange=\"window.location=('{$url}&{$name}='+this.options[this.selectedIndex].value+'')\"";
	$show="<select name='$name' $reto><option value=''>选择风格</option>";
	$filedir=opendir(PHP168_PATH."php168/style/");
	while($file=readdir($filedir)){
		if(ereg("\.php$",$file)){
			include PHP168_PATH."php168/style/$file";
			$ck==$styledb[keywords]?$ckk='selected':$ckk='';	//指定的某个
			/*只选定一个
			if($select){
				if($style_web!=$select){
					continue;
				}
			}
			*/
			$show.="<option value='$styledb[keywords]' $ckk style='color=blue'>$styledb[name]</option>";
		}
	}
	return $show." </select>";   
}

function select_template($cname,$type=1,$ck=''){
	global $db,$pre;
	$show="<select name='$cname' $reto><option value='' style='color:red;'>请选择模板</option>";
	if($type==7||$type==8){
		//$show.="<option value='template/default/none.htm'>我要屏蔽</option>";
	}
	$query=$db->query("SELECT * FROM {$pre}template WHERE type='$type'");
	while($rs=$db->fetch_array($query))
	{
		if(!$rs[name]){
			$rs[name]="模板$rs[id]";
		}
		$rs[filepath]==$ck?$ckk='selected':$ckk='';
		$show.="  <option value='$rs[filepath]' $ckk>$rs[name]</option>";
	}
	 return $show." </select>";
}

/**
*友情链接选择列表
**/
function select_link($cname='name',$ck='',$url=''){
	global $db;
	if($url) 
	$reto=" onchange=\"window.location=('{$url}&{$cname}='+this.options[this.selectedIndex].value+'')\"";
	$show="<select name='$cname' $reto><option value='' selected>现有链接</option>";
	$query=$db->query("select * from lfj_link");
	while(@extract($db->fetch_array($query))){
		$list=$$cname;
        $ck==$list?$ckk='selected':$ckk='';
		$show.="  <option value='$list' $ckk>$name</option>";
     }
     return $show." </select>";   
}

/**
*后台操作菜单
**/

function leftlink($filedb,$sort,$pathurl=''){
	global $userdb,$jj,$Apower;
	//include (PHP168_PATH."php168/admin_grp/$userdb[groupid].php");
	$j=1;
	$m=5;
	foreach($filedb AS $word=>$array){
		$j++;
		$m++;
		$show.="<tr>";
		if($m%4>1){
			$style="";
			if($m==6){
				$m=7;
			}
		}else{
			$style="";
		}
		if($Apower[$array[power]]||($userdb[groupid]==3&&is_numeric($array[power]))){
			$show.="<td style='padding-left:10px;height:15px;' $style><A HREF='$pathurl$array[link]' target=main>$word</A></td>";
			$power++;
		}else{
			$show.="<td $style></td>";
		}
		$show.="</tr>";
	}
	if($j%2==0){
		$show.="</tr>";
	}
    $show="<table cellspacing='1' cellpadding='3' class='tablewidth_menu'><tr class='head_menu'><td><b><a href='javascript:' onclick='showson(s$jj)'>$sort</a></b></td></tr><tr><td><table width='100%' cellspacing='1' cellpadding='3' style='display:none' id='s$jj'>$show</table></td></tr></table><img src='lineheight.gif' height=5 border=0>";
	if($power){
		return $show;
	}
}

function showmenu($menudb){
	global $jj;
	foreach($menudb AS $key1=>$value2){
		$jj++;
		$show.=leftlink($value2,$key1);
	}
	$system_menu=module_menu();
	if($system_menu){
		foreach( $system_menu AS $path=>$menudb){
			foreach($menudb AS $key1=>$value2){
				$jj++;
				$show.=leftlink($value2,$key1,$path);
			}
		}
	}
	return $show;
}



/**
*纠正栏目错误
**/
function sort_error_in($table,$fid){
	global $db;
	$query=$db->query("SELECT fid FROM $table WHERE fup='$fid'");
	while( @extract($db->fetch_array($query)) ){
		$show.="{$fid}\t";
		$show.=sort_error_in($table,$fid);
	}
	return $show;
}

function sort_error($table,$name='errid'){
	global $db;
	$show="<select name='$name'><option value=''>出错的栏目</option>";
	$array=explode("\t",sort_error_in($table,0));
	$query=$db->query("SELECT * FROM $table");
	while( @extract($db->fetch_array($query)) ){
		if(!in_array($fid,$array)){
			$show.="<option value='$fid'>$name</option>";
		}
	}
	$show.=" </select>";
	return $show;
}

/**
*更新栏目级别
**/
function mod_sort_class($table,$class,$fid){
	global $db;
	$db->query("UPDATE $table SET class='$class'+1  WHERE fup='$fid' ");
	$query=$db->query("SELECT * FROM $table WHERE fup='$fid'");
	while( @extract($db->fetch_array($query)) ){
		mod_sort_class($table,$class,$fid);
	}
}

/**
*更新栏目有几个子栏目
**/
function  mod_sort_sons($table,$fid){
	global $db;
	$query=$db->query("SELECT * FROM $table WHERE fup='$fid'");
	$sons=$db->num_rows($query);
	$db->query("UPDATE $table SET sons='$sons' WHERE fid='$fid' ");
	while( @extract($db->fetch_array($query)) ){
		mod_sort_sons($table,$fid);
	}
}
/**
*检查是否误操作.设置子栏目为自己的父栏目
**/
function check_fup($table,$fid,$fup){
	global $db;
	if(!$fup){
		return ;
	}elseif($fid==$fup){
		showmsg("不能设置自身为父栏目");
	}
	$query = $db->query("SELECT * FROM $table WHERE fid='$fup'");
	while($rs = $db->fetch_array($query)){
		if($rs[fup]==$fid){
			showmsg("你不能设置本身的子栏目作为父栏目,这是不允许的.但你可以设置其他子栏目作为父栏目");
		}elseif($rs[fup]){
			check_fup($table,$fid,$rs[fup]);
		}
	}
}


/**
*服务器信息
**/
function systemMsg(){
	global $db,$siteurl,$onlineip,$SCRIPT_FILENAME,$WEBURL;
	if(mysql_get_server_info()<'4.1'){
		$rs[mysqlVersion]=mysql_get_server_info()."(低版本);";
	}else{
		$rs[mysqlVersion]=mysql_get_server_info()."(高版本);";
	}
	isset($_COOKIE) ? $rs[ifcookie]="SUCCESS" : $rs[ifcookie]="FAIL";
	$rs[sysversion]=PHP_VERSION;	//PHP版本
	$rs[max_upload]= ini_get('upload_max_filesize') ? ini_get('upload_max_filesize') : 'Disabled';	//最大上传限制
	$rs[max_ex_time]=ini_get('max_execution_time').' 秒';	//最大执行时间
	$rs[sys_mail]= ini_get('sendmail_path') ? 'Unix Sendmail ( Path: '.ini_get('sendmail_path').')' :( ini_get('SMTP') ? 'SMTP ( Server: '.ini_get('SMTP').')': 'Disabled' );	//邮件支持模式
	$rs[systemtime]=date("Y-m-j g:i A");	//服务器所在时间
	$rs[onlineip]=$onlineip;				//当前IP
	if( function_exists("ImageJpeg")&&function_exists("imagegif") ){
		$rs[gdpic]="良好支持";
	}elseif( function_exists("ImageJpeg")||function_exists("imagegif") ){
		$rs[gdpic]="勉强支持";
	}else{
		$rs[gdpic]="不支持";
	}
	$rs[allow_url_fopen]=ini_get('allow_url_fopen')?"On 支持采集数据":"OFF 不支持采集数据";
	$rs[safe_mode]=ini_get('safe_mode')?"打开":"关闭";
	$rs[DOCUMENT_ROOT]=$_SERVER["DOCUMENT_ROOT"];	//程序所在磁盘物理位置
	$rs[SERVER_ADDR]=$_SERVER["SERVER_ADDR"];		//服务器IP
	$rs[SERVER_PORT]=$_SERVER["SERVER_PORT"];		//服务器端口
	$rs[SERVER_SOFTWARE]=$_SERVER["SERVER_SOFTWARE"];	//服务器软件
	$rs[SCRIPT_FILENAME]=$_SERVER["SCRIPT_FILENAME"];	//当前文件路径
	$rs[SERVER_NAME]=$_SERVER["SERVER_NAME"];	//域名

	//获取ZEND的版本
	ob_end_clean();
	ob_start();
	phpinfo();
	$phpinfo=ob_get_contents();
	ob_end_clean();
	ob_start();
	preg_match("/with(&nbsp;| )Zend(&nbsp;| )Optimizer(&nbsp;| )([^,]+),/is",$phpinfo,$zenddb);
	$rs[zendVersion]=$zenddb[4]?$zenddb[4]:"未知/可能没安装";
	
	return $rs;
}

function check_table_field($table,$array){
	global $db;
	foreach($array AS $key=>$value){
		if( !table_field($table,$key) ){
			$SQL.="ALTER TABLE `$table` ADD `$key` VARCHAR( 254 ) NOT NULL ;";
		}
	}
	if($SQL){
		$db->query($SQL);
	}
}

function group_box($name="postdb[group]",$ckdb=array()){
	global $db,$pre;
	$query=$db->query("SELECT * FROM {$pre}group ORDER BY gid ASC");
	while($rs=$db->fetch_array($query))
	{
		$checked=in_array($rs[gid],$ckdb)?"checked":"";
		$show.="<input type='checkbox' name='{$name}[]' value='{$rs[gid]}' $checked>&nbsp;{$rs[grouptitle]}&nbsp;&nbsp;";
	}
	return $show;
}

function ad_moneyType($name,$id){
	global $AdTypeMoney;
	$show="<select name='$name'>";
	foreach($AdTypeMoney AS $key=>$value){
		$ck=$id==$key?' selected ':'';
		$show.="<option value='$key' $ck>$value</option>";
	}
	$show.="</select>";
	return $show;
}

/*更新会员组缓存*/
function write_group_cache(){
	global $db,$pre;
	$show="<?php \r\n";
	$query = $db->query("SELECT * FROM `{$pre}group`");
	while($rs = $db->fetch_array($query)){
		$ckk=$rs[gptype]?'':"\$memberlevel[{$rs[gid]}]={$rs[levelnum]};";
		$show.="\$ltitle[{$rs[gid]}]='$rs[grouptitle]';\t\t$ckk\r\n";
		$cache="<?php\r\n";
		$cache.="\r\n\$groupdb=@unserialize(\"".addslashes($rs[powerdb])."\");";
		foreach( $rs AS $key=>$value){
			$value=addslashes($value);
			if($key=='powerdb'){
				continue;
				//$cache.="\r\n\$groupdb['$key']=@unserialize(\"$value\");";
			}else{
				$cache.="\r\n\$groupdb['$key']=\"$value\";";
			}
		}
		write_file(PHP168_PATH."php168/group/$rs[gid].php",$cache);
	}
	write_file(PHP168_PATH."php168/level.php",$show);
}

//更新核心设置缓存
function write_config_cache($webdbs)
{
	global $db,$pre;
	if( is_array($webdbs) )
	{
		foreach($webdbs AS $key=>$value)
		{
			if(is_array($value))
			{
				$webdbs[$key]=$value=implode(",",$value);
			}
			$SQL2.="'$key',";
			$SQL.="('$key', '$value', ''),";
		}
		$SQL=$SQL.";";
		$SQL=str_Replace("'),;","')",$SQL);
		$db->query(" DELETE FROM {$pre}config WHERE c_key IN ($SQL2'') ");
		$db->query(" INSERT INTO `{$pre}config` VALUES  $SQL ");	
	}
	$writefile="<?php\r\n";
	$query = $db->query("SELECT * FROM {$pre}config");
	while($rs = $db->fetch_array($query)){
		if($rs[c_key]=='copyright1'){
			$copyright1=$rs[c_value];
		}elseif($rs[c_key]=='copyright2'){
			$copyright2=$rs[c_value];
		}else{
			$rs[c_value]=addslashes($rs[c_value]);
			$writefile.="\$webdb['$rs[c_key]']='$rs[c_value]';\r\n";
		}
	}
	write_file(PHP168_PATH."php168/config.php",$writefile);
	write_file(PHP168_PATH."cache/warn.bat","<?php\r\n$copyright1\r\n\r\n$copyright2");
}

function write_channel_config($table,$webdbs)
{
	global $db,$pre;
	if( is_array($webdbs) )
	{
		foreach($webdbs AS $key=>$value)
		{
			if(is_array($value))
			{
				$webdbs[$key]=$value=implode(",",$value);
			}
			$SQL2.="'$key',";
			$SQL.="('$key', '$value', ''),";
		}
		$SQL=$SQL.";";
		$SQL=str_Replace("'),;","')",$SQL);
		$db->query(" DELETE FROM $table WHERE c_key IN ($SQL2'') ");
		$db->query(" INSERT INTO `$table` VALUES  $SQL ");	
	}
}

/*栏目列表*/
function list_allsort($fid,$table='sort'){
	global $db,$pre,$sortdb;
	$query=$db->query("SELECT * FROM {$pre}$table where fup='$fid' ORDER BY list DESC");
	while( $rs=$db->fetch_array($query) ){
		$icon="";
		for($i=1;$i<$rs['class'];$i++){
			$icon.="&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;";
		}
		if($icon){
			$icon=substr($icon,0,-24);
			$icon.="--";
		}
		$rs[config]=unserialize($rs[config]);
		$rs[icon]=$icon;
		$sortdb[]=$rs;

		list_allsort($rs[fid],$table);
	}
}

function module_menu(){
	global $webdb;
	$detail=explode("\r\n",$webdb[module_adminmenu]);
	foreach( $detail AS $key=>$value){
		if($value){
			unset($passport_admin,$menudb);
			@include(PHP168_PATH."$value/menu.php");
			$_allmenudb["../$value/"]=$menudb;
		}
	}
	return $_allmenudb;
}

function All_fid_cache(){
	global $db,$pre;
	$show="<?php\r\n";
	$query = $db->query("SELECT fid,fup,name FROM {$pre}sort ORDER BY list DESC");
	while($rs = $db->fetch_array($query)){
		$rs[name]=addslashes($rs[name]);
		$show.="\$Fid_db[{$rs[fup]}][{$rs[fid]}]='$rs[name]';
		\$Fid_db[name][{$rs[fid]}]='$rs[name]';
		";
	}
	write_file("../php168/all_fid.php",$show);
}




!function_exists('html') && exit('ERR');
/**
*清空可能上次发表中断的附件
**/
if($job){
	setcookie("DownLoad","");
}
/**
*postnew=>新发表,mod=>修改,morepost=>续页发表,mod_more=>续页修改
**/
if($action=="postnew"||$action=="mod"||$action=="morepost"||$action=="mod_more")
{
	//图片目录转移
	$postdb[content]=move_attachment($userdb[uid],$postdb[content],"article/$postdb[fid]");

	if($jobs=="usehtml")
	{
		$postdb[ishtml]=1;
	}
	else
	{
		$postdb[content]=filtrate($postdb[content]);
	}
	if($action=="mod")
	{
		$rs=$db->get_one(" SELECT picurl FROM `{$pre}article` WHERE aid='$id' ");
	}

	//当不存在缩略图时,获取图片,如果系统设置允许自动,才做处理
	if($webdb[autoGetSmallPic]&&!$postdb[picurl]&&($action=="postnew"||$action=="mod"))
	{
		//获取图片
		$file_db=get_content_attachment($postdb[content]);
		if($file_db){
			foreach( $file_db AS $key=>$value){
				if((eregi("jpg$",$value)||eregi("gif$",$value))&&!eregi("ewebeditor\/",$value)){
					$postdb[picurl]=$value;
					break;
				}
			}
		}
	}
	/*缩略图处理*/
	if( $postdb[picurl] && $postdb[picurl]!=$rs[picurl] )
	{
		//图片目录转移
		move_attachment($userdb[uid],tempdir($postdb[picurl]),"article/$postdb[fid]");
		if(file_exists(PHP168_PATH."$webdb[updir]/article/$postdb[fid]/".basename($postdb[picurl]))){
			$postdb[picurl]="article/$postdb[fid]/".basename($postdb[picurl]);
		}
		
		if($automakesmall&&(function_exists("ImageJpeg")||function_exists("imagegif")) )
		{
			if(strstr($postdb[content],$postdb[picurl]))
			{
				$smallpic=str_replace(".","_",$postdb[picurl]).".gif";
			}
			else
			{
				$smallpic="$postdb[picurl]";
			}
			
			$Newpicpath=PHP168_PATH."$webdb[updir]/$smallpic";
			gdpic(PHP168_PATH."$webdb[updir]/$postdb[picurl]",$Newpicpath,$picWidth?$picWidth:200,$picHeight?$picHeight:150);
			if( file_exists($Newpicpath) )
			{
				$postdb[picurl]="$smallpic";
			}
		}
	}

	/*加水印*/
	if($atc_downurl&&$webdb[is_waterimg]&&(function_exists("ImageJpeg")||function_exists("imagegif")))
	{
		$detailurl=explode("[MMCBBS]",$atc_downurl);
		$count=count($detailurl);
		for($i=0;$i<$count;$i++)
		{
			if( !@eregi(".jpg",$detailurl[$i])&&!@eregi(".gif",$detailurl[$i]))
			{
				continue;
			}
			include_once(PHP168_PATH."inc/waterimage.php");
			$uploadfile=PHP168_PATH."$webdb[updir]/$detailurl[$i]";
			imageWaterMark($uploadfile,$webdb[waterpos],PHP168_PATH.$webdb[waterimg]);
		}
	}

	//过滤不健康的字
	$postdb[content]=replace_bad_word($postdb[content]);
	$postdb[title]=replace_bad_word($postdb[title]);
	$postdb[author]=replace_bad_word($postdb[author]);

	//对地址做处理
	$postdb[content]=En_TruePath($postdb[content]);

	

}
/**
*发表新文章
**/
$postdb[yz]=1;  //审核

if($action=="addpic"&&$Apower[artic_addpic])
{
	if(!$photodb){
		showmsg("请上传一张图片");
	}
	if(!$postdb[fid]){
		showmsg("请选择一个栏目");
	}
	$ck=0;
	@extract($db->get_one(" SELECT name AS fname FROM {$pre}sort WHERE fid='$postdb[fid]' "));
	foreach($photodb AS $key=>$photo){
		if(!$photo){
			continue;
		}
		
		//图片目录转移
		move_attachment($userdb[uid],tempdir($photo),"$postdb[fid]");
		if(file_exists(PHP168_PATH."$webdb[updir]/$postdb[fid]/".basename($photo))){
			$photo="$postdb[fid]/".basename($photo);
		}

		$postdb[title]=$namedb[$key];
		if( !$postdb[picurl] && (function_exists("ImageJpeg")||function_exists("imagegif")) )
		{
			$smallpic=str_replace(".","_",$photo).".gif";
			$Newpicpath=PHP168_PATH."$webdb[updir]/$smallpic";
			gdpic(PHP168_PATH."$webdb[updir]/$photo",$Newpicpath,200,150);
			if( file_exists($Newpicpath) )
			{
				$postdb[picurl]="$smallpic";
			}
			else
			{
				$postdb[picurl]="$photo";
			}
		}
		elseif(!$postdb[picurl])
		{
			$postdb[picurl]="$photo";
		}
		$postdb[content]=addslashes("<IMG onclick=window.open(this.src); src='".tempdir($photo)."' onload=makesmallpic(this,500,700); border=0>");
		$postdb[yz]=1;
		if($batch==0||$ck==0)
		{
			$db->query("
			INSERT INTO `{$pre}article` 
			( `title`, `fid`,`fname`,`pages`, `posttime`, `list`, `uid`, `username`, `author`,`picurl`,`ispic`, `yz`, `keywords`,`style`, `template`, `target`,`ip` ) 
			VALUES
			('$postdb[title]','$postdb[fid]','$fname','1','$timestamp','$timestamp','$userdb[uid]','$userdb[username]','$postdb[author]','$postdb[picurl]',1,'$postdb[yz]','$postdb[keywords]','$postdb[style]','$postdb[template]','$postdb[target]','$onlineip' )
			");
			$rs=$db->get_one("SELECT * FROM `{$pre}article` ORDER BY aid DESC LIMIT 1");

			$db->query("INSERT INTO `{$pre}reply` (  `aid` , `fid` ,`uid` ,  `content` ,`ishtml`,`topic`) VALUES ( '$rs[aid]', '$postdb[fid]','$userdb[uid]', '$postdb[content]','1','1')");

			unset($postdb[picurl]);
		}
		else
		{
			$db->query(" UPDATE {$pre}article SET pages=pages+1 WHERE aid='$rs[aid]' ");
			$db->query("INSERT INTO `{$pre}reply` (  `aid` , `fid` ,`uid` ,  `content` ,`ishtml`) VALUES ( '$rs[aid]', '$postdb[fid]','$userdb[uid]', '$postdb[content]','1')");
		}
		$ck++;
	}
	//jump("<CENTER>[<A HREF='index.php?lfj=artic&job=addpic&fid=$postdb[fid]'>继续发表新组图</A>] [<A HREF='index.php?lfj=artic&job=listartic&fid=$postdb[fid]'>返回文章列表</A>] [<A HREF='$webdb[www_url]/bencandy.php?fid=$rs[fid]&aid=$rs[aid]' target=_blank>查看文章</A>] [<A HREF='index.php?lfj=artic&job=mod_page&id=$rs[aid]'>点击修改</A>]</CENTER>","index.php?lfj=artic&job=postnew&fid=$postdb[fid]",600);
	echo('1');
	$db->close;
	exit;
}
/**
*处理发表新文章
**/
elseif($action=="postnew"&&$Apower[artic_postnew])
{
	//采集外部图片
	$postdb[content]=get_outpic($postdb[content],$postdb[fid],$GetOutPic);
	
	@extract($db->get_one(" SELECT name AS fname,allowpost,admin FROM {$pre}sort WHERE fid='$postdb[fid]' "));
	if($allowpost){
		$detail=explode(',',$allowpost);
		$detail_admin=explode(',',$admin);
		if(!in_array($userdb[groupid],$detail)&&$userdb[groupid]!=3&&!in_array($userdb[username],$detail_admin)){
			showmsg("你所在的用户组不能在此栏目发表文章");
		}
	}

	/*文章置顶处理*/
	$postdb['list']=$postdb[top]?$timestamp*1.3:$timestamp;
	$postdb[keywords]&&$postdb[keywords]=str_Replace("　"," ",$postdb[keywords]);
	if($postdb[keywords]&&$addkeyword)
	{
		$detail=explode(' ',$postdb[keywords]);
		foreach($detail AS $key=>$value){
			if( $value&&!$db->get_one("SELECT * FROM {$pre}keyword WHERE keywords='$value' ") ){
				$db->query("INSERT INTO `{$pre}keyword` (`keywords` , `list` ) VALUES ('$value', '$timestamp')");
			}
		}
	}
	$postdb[posttime]=$timestamp;
	$postdb[posttime]=preg_replace("/([\d]+)-([\d]+)-([\d]+) ([\d]+):([\d]+):([\d]+)/eis","@mktime('\\4','\\5', '\\6', '\\2', '\\3', '\\1')",$postdb[posttime]);
	$postdb[begintime]&&$postdb[begintime]=preg_replace("/([\d]+)-([\d]+)-([\d]+) ([\d]+):([\d]+):([\d]+)/eis","@mktime('\\4','\\5', '\\6', '\\2', '\\3', '\\1')",$postdb[begintime]);
	$postdb[endtime]&&$postdb[endtime]=preg_replace("/([\d]+)-([\d]+)-([\d]+) ([\d]+):([\d]+):([\d]+)/eis","@mktime('\\4','\\5', '\\6', '\\2', '\\3', '\\1')",$postdb[endtime]);
	$postdb[template]=serialize($postdb[tpl]);

	$postdb[allowview]=@implode(",",$postdb[allowview]);
	$postdb[allowdown]=@implode(",",$postdb[allowdown]);
	$postdb[picurl] && $postdb[ispic]=1;
	$db->query("
	INSERT INTO `{$pre}article` 
	( `title`, `smalltitle`, `fid`,`fname`, `info`, `hits`, `pages`, `posttime`, `list`, `uid`, `username`, `author`, `copyfrom`, `copyfromurl`, `titlecolor`, `fonttype`, `titleicon`, `picurl`, `ispic`, `yz`, `yzer`, `yztime`, `keywords`, `jumpurl`, `iframeurl`, `style`, `template`, `target`,`ip`, `lastfid`, `money`, `passwd`, `editer`, `edittime`, `begintime`, `endtime`, `description`, `levels`,allowdown,allowview) 
	VALUES
	('$postdb[title]','$postdb[smalltitle]','$postdb[fid]','$fname','$postdb[info]','$postdb[hits]','1','$postdb[posttime]','$postdb[list]','$userdb[uid]','$userdb[username]','$postdb[author]','$postdb[copyfrom]','$postdb[copyfromurl]','$postdb[titlecolor]','$postdb[fonttype]','$postdb[titleicon]','$postdb[picurl]','$postdb[ispic]','$postdb[yz]','$postdb[yzer]','$postdb[yztime]','$postdb[keywords]','$postdb[jumpurl]','$postdb[iframeurl]','$postdb[style]','$postdb[template]','$postdb[target]','$onlineip','0','$postdb[money]','$postdb[passwd]','$postdb[editer]','$postdb[edittime]','$postdb[begintime]','$postdb[endtime]','$postdb[description]','$postdb[levels]','$postdb[allowdown]','$postdb[allowview]')
	");
	$rs=$db->get_one("SELECT aid,fid FROM {$pre}article ORDER BY aid DESC LIMIT 1 ");
	
	unset($contentDB);
	if($ExplodePage&&$PageNum>0)
	{
		$content=stripslashes($postdb[content]);
		$max=ceil( strlen($content)/$PageNum );
		for($i=0;$i<$max;$i++){
			$star=$i*$PageNum;
			$contentDB[]=$s=addslashes( get_content($content,$PageNum,$star) );
		}
		$db->query("UPDATE `{$pre}article` SET `pages`='$max' WHERE aid='$rs[aid]'");
	}
	elseif(strstr($postdb[content],'<!--subpage-->'))
	{
		$content=stripslashes($postdb[content]);
		$contentDB=explode("<!--subpage-->",$content);
		foreach( $contentDB AS $key=>$value){
			if(!$value){
				unset($contentDB[$key]);
			}
			$contentDB[$key]=addslashes($value);
		}
		$max=count($contentDB);
		$db->query("UPDATE `{$pre}article` SET `pages`='$max' WHERE aid='$rs[aid]'");
	}
	
	if($contentDB)
	{
		unset($j);
		foreach( $contentDB AS $key=>$content){
			$j++;
			if($j==1){
				$topic=1;
			}else{
				$topic=0;
			}
			$db->query("INSERT INTO `{$pre}reply` (  `aid` , `fid` ,`uid` ,  `content` ,`ishtml`,`topic`) VALUES ( '$rs[aid]', '$postdb[fid]','$userdb[uid]', '$content','$postdb[ishtml]','$topic')");
		}
	}
	else
	{
		$db->query("INSERT INTO `{$pre}reply` (  `aid` , `fid` ,`uid` ,  `content` ,`ishtml`,`topic`) VALUES ( '$rs[aid]', '$postdb[fid]','$userdb[uid]', '$postdb[content]','$postdb[ishtml]','1')");
	}
	
	//添加专题
	if($spid){
		$rssp=$db->get_one(" SELECT * FROM {$pre}special WHERE id='$spid' ");
		if($rssp[aids]){
			$rssp[aids]="$rs[aid],$rssp[aids]";
		}else{
			$rssp[aids]="$rs[aid]";
		}
		$db->query("UPDATE `{$pre}special` SET `aids`='$rssp[aids]' WHERE id='$spid'");
	}

	//财富增加
	$webdb[postArticleMoney] && $lfjuid && add_user($lfjuid,$webdb[postArticleMoney]);

	//die($webdb);
	//jump("<CENTER>[<A HREF='index.php?lfj=artic&job=postnew&fid=$postdb[fid]'>继续发表新主题</A>] [<A HREF='index.php?lfj=artic&job=morepost&id=$rs[aid]'>续发本主题</A>] [<A HREF='index.php?lfj=artic&job=listartic&fid=$postdb[fid]'>返回文章列表</A>] [<A HREF='$webdb[www_url]/bencandy.php?fid=$rs[fid]&aid=$rs[aid]' target=_blank>查看文章</A>] [<A HREF='index.php?lfj=artic&job=mod_page&id=$rs[aid]'>点击修改</A>]</CENTER>","index.php?lfj=artic&job=postnew&fid=$postdb[fid]",600);
	echo('1');
	$db->close;
	exit;
}


function dowork($id,$job){
	global $db,$pre,$timestamp,$userdb,$webdb;
	$rsdb=$db->get_one("SELECT * FROM {$pre}article WHERE aid='$id' ");
	if($job=="delete")
	{
		global $rubbish,$forcedel;
		if($forcedel==1)
		{
			//删除附件
			$query = $db->query(" SELECT A.uid,R.content FROM {$pre}article A LEFT JOIN {$pre}reply R ON A.aid=R.aid WHERE A.aid='$id' ");
			while($rs = $db->fetch_array($query)){
				//echo "$rs[Acontent]<hr>";
				//删除主题附件
				//delete_attachment($rs[uid],$rs[Acontent]);
				//删除续发文章附件
				delete_attachment($rs[uid],$rs[content]);
			}
			$rsdb[picurl] && delete_attachment($rsdb[uid],tempdir($rsdb[picurl]));
			$db->query("DELETE FROM {$pre}article WHERE aid='$id' ");
			$db->query("DELETE FROM {$pre}reply WHERE aid='$id' ");

			//财富处理
			$webdb[deleteArticleMoney] && $rsdb[uid] && add_user($rsdb[uid],$webdb[deleteArticleMoney]);

		}
		else
		{
			//放进回收站
			if($rsdb[fid]){
				$db->query("UPDATE {$pre}article SET lastfid='$rsdb[fid]',fid=0,fname='',yz=2 WHERE aid='$id' ");
				$db->query("UPDATE {$pre}reply SET fid=0 WHERE aid='$id'");
			}
		}
		
	}
	elseif($job=="move"&&$rsdb[yz]!=2)
	{	
		//回收站的内容,不能再移动
		global $fid;
		if($fid){
			$rs=$db->get_one("SELECT name FROM {$pre}sort WHERE fid='$fid'");
			$db->query("UPDATE {$pre}article SET fid='$fid',fname='$rs[name]',lastfid='$rsdb[fid]' WHERE aid='$id' ");
			$db->query("UPDATE {$pre}reply SET fid='$fid' WHERE aid='$id' ");
		}
	}
	elseif($job=="color")
	{
		global $color;
		$db->query("UPDATE {$pre}article SET titlecolor='$color' WHERE aid='$id' ");
	}
	elseif($job=="yz"&&$rsdb[yz]!=2)
	{
		$db->query("UPDATE {$pre}article SET yz='1',yzer='$userdb[username]',yztime='$timestamp' WHERE aid='$id' ");
	}
	elseif($job=="unyz"&&$rsdb[yz]!=2)
	{
		$db->query("UPDATE {$pre}article SET yz='0',yzer='$userdb[username]',yztime='$timestamp' WHERE aid='$id' ");
	}
	elseif($job=="com")
	{
		global $levels;
		$db->query("UPDATE {$pre}article SET levels='$levels',levelstime='$timestamp' WHERE aid='$id' ");
	}
	elseif($job=="uncom")
	{
		$db->query("UPDATE {$pre}article SET levels='0',levelstime='0' WHERE aid='$id' ");
	}
	elseif($job=="top")
	{
		global $toptime;
		$db->query("UPDATE {$pre}article SET list=list+'$toptime' WHERE aid='$id' ");
	}
	elseif($job=="untop")
	{
		$db->query("UPDATE {$pre}article SET list=posttime WHERE aid='$id' ");
	}
	elseif($job=="front")
	{
		global $topid;
		if($topid)
		{
			$rs=$db->get_one("SELECT list FROM {$pre}article WHERE aid='$topid' ");
			$list=$rs["list"]+1;
			$db->query("UPDATE {$pre}article SET list='$list' WHERE aid='$id' ");
		}
		else
		{
			$db->query("UPDATE {$pre}article SET list='$timestamp' WHERE aid='$id' ");
		}
	}
	elseif($job=="bottom")
	{
		global $bottomid;
		if($bottomid)
		{
			$rs=$db->get_one("SELECT list FROM {$pre}article WHERE aid='$bottomid' ");
			$list=$rs["list"]-1;
			$db->query("UPDATE {$pre}article SET list='$list' WHERE aid='$id' ");
		}
		else
		{
			$db->query("UPDATE {$pre}article SET list='0' WHERE aid='$id' ");
		}
	}
	elseif($job=='return')
	{
		if($rsdb[yz]==2){
			$rs=$db->get_one("SELECT name FROM {$pre}sort WHERE fid='$rsdb[lastfid]'");
			$db->query("UPDATE {$pre}article SET fid='$rsdb[lastfid]',fname='$rs[name]',yz=1 WHERE aid='$id' ");
		}
	}
}

/**
*截取字符
**/
function get_content($content,$length,$star=0,$more=0) {
	//if(!$more){
		$length=$length+4;
	//}
	if($length && strlen($content)>$length){
		$num=0;
		for($i=0;$i<$length-3;$i++) {
			if(ord($content[$i])>127){
				$num++;
			}
		}
		$num%2==1 ? $content=substr($content,$star,$length-4):$content=substr($content,$star,$length-3);
		//$more && $content.='..';
	}
	return $content;
}

//采集外部图片
function get_outpic($str,$fid=0,$getpic=1){
	global $webdb;
	if(!$getpic){
		return $str;
	}
	preg_match_all("/http:\/\/([^ '\"<>]+)\.(gif|jpg|png)/is",$str,$array);
	$filedb=$array[0];
	foreach( $filedb AS $key=>$value){
		if( strstr($value,$webdb[www_url]) ){
			continue;
		}
		$listdb["$value"]=$value;
	}
	unset($filedb);
	foreach( $listdb AS $key=>$value){
		$filedb[]=$value;
		$name=rands(5)."__".basename($value);
		if(!is_dir(PHP168_PATH."$webdb[updir]/$fid")){
			makepath(PHP168_PATH."$webdb[updir]/$fid");
		}
		$ck=0;
		if( @copy($value,PHP168_PATH."$webdb[updir]/$fid/$name") ){
			$ck=1;
		}elseif($filestr=file_get_contents($value)){
			$ck=1;
			write_file(PHP168_PATH."$webdb[updir]/$fid/$name",$filestr);
		}
		if($ck){
			$str=str_replace("$value","http://www_php168_com/Tmp_updir/$fid/$name",$str);
		}
	}
	return $str;
}

//专题
function select_sp($name='spid'){
	global $db,$pre;
	$show="<select name='$name'><option value=''>请选择一个专题</option>";
	$query = $db->query("SELECT * FROM {$pre}special ORDER BY id DESC LIMIT 100");
	while($rs = $db->fetch_array($query)){
		$show.="<option value='$rs[id]'>$rs[title]</option>";
	}
	$show.='</select>';
	return $show;
}
?>