<?php
/**
 * @package iCMS
 * @copyright 2007-2010, iDreamSoft
 * @license http://www.idreamsoft.cn iDreamSoft
 * @author coolmoo <idreamsoft@qq.com>
 */
!defined('iPATH') && exit('What are you doing?');
include iPATH.'include/catalog.class.php';
switch($operation){
case 'add':
	$Admin->MP(array("menu_index_article_add","menu_article_add"));
	$catalog =new catalog();
	include(iPATH."include/fckeditor.php");
	$id=(int)$_GET['id'];
	$rs=array();
	$id && $rs=$iCMS->db->getRow("SELECT a.*,ad.tpl,ad.body,ad.subtitle FROM `#iCMS@__article` a LEFT JOIN `#iCMS@__articledata` ad ON a.id=ad.aid WHERE a.id='$id'",ARRAY_A);
	$editor = new FCKeditor('content') ;
	$editor->Value= $rs['body'];
	$rs['pubdate']=empty($id)?get_date('',"Y-m-d H:i:s"):get_date($rs['pubdate'],'Y-m-d H:i:s');
	$rootid=empty($rs['cid'])?intval($_GET['cid']):$rs['cid'];
	$cata_option=$catalog->select($rootid,0,1,'channel=1&list');
	empty($rs['editor']) && $rs['editor']=empty($Admin->admin->name)?$Admin->admin->username:$Admin->admin->name;
	empty($rs['userid']) && $rs['userid']=$Admin->uId;
	include iCMS_admincp_tpl("article.add");
break;
case 'manage':
		$mtime = microtime();
		$mtime = explode(' ', $mtime);
		$time_start = $mtime[1] + $mtime[0];
	
	$Admin->MP(array("menu_article_manage","menu_article_draft","menu_article_user_manage","menu_article_user_draft"));
	$catalog =new catalog();

	$cid	= (int)$_GET['cid'];
	$act	= $_GET['act'];
	$sql	=" where ";
	$sql.=$_GET['type']=='draft'?"`status` ='0'":"`status` ='1'";//
	$_ptxt=array(0=>'发布',1=>'转成草稿');
	switch($act){//postype: 0 用户正常 1 管理员正常 status:0 postype:2 用户删除
		case 'user':
			$sql.=" AND `postype`='0'";
			$_ptxt=array(0=>'通过审核',1=>'取消审核');
		break;
		case 'trash':
			$sql.=" AND `postype`='2'";
			$_ptxt=array(0=>'还原',1=>'放入回收站');
		break;
		default:$sql.=" AND `postype`='1'";
	}
	if($_GET['st']=="title"){
		$_GET['keywords'] 		&& $sql.=" AND `title` REGEXP '{$_GET['keywords']}'";
	}else if($_GET['st']=="tkd"){
		$_GET['keywords'] 		&& $sql.=" AND CONCAT(title,keywords,description) REGEXP '{$_GET['keywords']}'";
	}
	$_GET['title'] 			&& $sql.=" AND `title` like '%{$_GET['title']}%'";
	$_GET['tag'] 			&& $sql.=" AND `tags` REGEXP '[[:<:]]".preg_quote(rawurldecode($_GET['tag']),'/')."[[:>:]]'";
	(isset($_GET['at']) && $_GET['at']!='-1') && $sql.=" AND `type` ='".$_GET['at']."'";
	isset($_GET['userid']) 	&& $sql.=" AND `userid`='".(int)$_GET['userid']."'";
	$cid=$Admin->CP($cid)?$cid:"0";
	if($cid){
		$cidIN=$catalog->id($cid).$cid;
		if(isset($_GET['sub']) && strstr($cidIN,',')){
			$sql.=" AND cid IN(".$cidIN.")";
		}else{
			$sql.=" AND cid ='$cid'";
		}
//		$sql.=" OR `vlink` REGEXP '[[:<:]]".preg_quote($cid, '/')."[[:>:]]')";
	}else{
		$Admin->cpower && $sql.=" AND cid IN(".implode(',',$Admin->cpower).")";
	}
	isset($_GET['nopic'])&&$sql.=" AND `isPic` ='0'";
	$_GET['starttime'] 	&& $sql.=" and `pubdate`>='".strtotime($_GET['starttime'])."'";
	$_GET['endtime'] 	&& $sql.=" and `pubdate`<='".strtotime($_GET['endtime'])."'";
	
	$act=='user' && $uri.='&act=user';
	$_GET['type']=='draft' && $uri.='&type=draft';
	isset($_GET['userid']) && $uri.='&userid='.(int)$_GET['userid'];
	isset($_GET['keyword']) && $uri.='&keyword='.$_GET['keyword'];
	isset($_GET['tag']) && $uri.='&tag='.$_GET['tag'];
	
	$orderby=$_GET['orderby']?$_GET['orderby']:"id DESC";
	$maxperpage =(int)$_GET['perpage']>0?$_GET['perpage']:20;
	$total=$iCMS->db->getValue("SELECT count(*) FROM `#iCMS@__article` {$sql} order by {$orderby}");
	page($total,$maxperpage,"篇文章");
	$rs=$iCMS->db->getArray("SELECT * FROM `#iCMS@__article` {$sql} order by {$orderby} LIMIT {$firstcount} , {$maxperpage}");
//echo $iCMS->db->last_query;
//$iCMS->db->last_query='explain '.$iCMS->db->last_query;
//$explain=$iCMS->db->getRow($iCMS->db->last_query);
//var_dump($explain);
	$_count=count($rs);
	include iCMS_admincp_tpl("article.manage");
//		$mtime = microtime();
//		$mtime = explode(' ', $mtime);
//		$time_end = $mtime[1] + $mtime[0];
//		echo  "<h1>".($time_end - $time_start);
break;
case 'status':
	$id	= (int)$_GET['id'];
	$s	= (int)$_GET['s'];
	$pt	= (int)$_GET['pt'];
	if($pt=="2") $setSQL=" ,`postype` = '0'";
	$iCMS->db->query("UPDATE `#iCMS@__article` SET `status` = '".($s=='1'?0:1)."'{$setSQL} WHERE `id` ='$id'");
	_Header();
break;
case 'delvlink':
	$id	= (int)$_GET['id'];
	$cid= (int)$_GET['cid'];
	$id && $vlink=$iCMS->db->getValue("SELECT vlink FROM `#iCMS@__article` WHERE `id`='$id'");
	$vlinkArray	= explode(',',$vlink);
	$key		= array_search($cid,$vlinkArray);
	unset($vlinkArray[$key]);
	$vlink		= implode(',',$vlinkArray);
	$iCMS->db->query("UPDATE `#iCMS@__article` SET `vlink` = '$vlink' WHERE `id` ='$id'");
	_Header();
break;
case 'updateHTML':
	!$_GET['id'] && alert("请选择要更新的文章");
	MakeArticleHtm(intval($_GET['id'])) && alert('更新完成!',"url:1");
break;
case 'del':
	$id=intval($_GET['id']);
	!$id && alert("请选择要删除的文章");
	delArticle($id) && alert('成功删除!',"url:1");
break;
case 'post':
	switch($action){
	case 'save':
		set_time_limit(0);
	    $aid		= intval($_POST['aid']);
	    $cid		= intval($_POST['catalog']);
	    $userid		= intval($_POST['userid']);
	    $top		= _int($_POST['top']);
	    $type		= intval($_POST['type']);
	    $vlink		= empty($_POST['vlink'])?"":implode(',',$_POST['vlink']);
		$title		= dhtmlspecialchars($_POST['title']);
		$subtitle	= dhtmlspecialchars($_POST['subtitle']);
		$stitle		= dhtmlspecialchars($_POST['stitle']);
		$pic		= dhtmlspecialchars($_POST['pic']);
	    $source		= dhtmlspecialchars($_POST['source']);
	    $author		= dhtmlspecialchars($_POST['author']);
	    $editor		= dhtmlspecialchars($_POST['editor']);
	    $description= dhtmlspecialchars($_POST['description']);
	    $keywords	= dhtmlspecialchars($_POST['keywords']);
	    $tags		= dhtmlspecialchars($_POST['tag']);
	    $clink		= dhtmlspecialchars($_POST['clink']);
	    $url		= dhtmlspecialchars($_POST['url']);
	    $tpl		= dhtmlspecialchars($_POST['template']);
	    $related	= empty($_POST['related'])?"":implode(',',$_POST['related']);
	    $pubdate	= _strtotime($_POST['pubdate']);
	    
	    $remote		= isset($_POST['remote'])	?true:false;
	    $dellink	= isset($_POST['dellink'])	?true:false;
	    $autopic	= isset($_POST['autopic'])	?true:false;
	    $status	= isset($_POST['draft'])?"0":"1";
		$postype	= $_POST['postype']?$_POST['postype']:"1";
	    $body		= str_replace(array("\n","\r","\t"),"",$_POST['content']);
	    //FF 分页
	    $body		= str_replace('<div style=\"page-break-after: always;\"><span style=\"display: none;\">&nbsp;</span></div>',
	    '<div style=\"page-break-after: always\"><span style=\"display: none\">&nbsp;</span></div>',$body);
	    
	    empty($title) && alert('标题不能为空！');
	    empty($cid) && alert('请选择所属栏目');
	    empty($body) && empty($url) && alert('文章内容不能为空！');
		WordFilter($title) && alert('标题包含被系统屏蔽的字符，请返回重新填写。');
		WordFilter($body) && alert('文章内容包含被系统屏蔽的字符，请返回重新填写。');
	    
	    if($clink){
			for($i=0;$i<strlen($clink);$i++){
				!preg_match("/[a-zA-Z0-9_\-~".preg_quote($iCMS->config['CLsplit'],'/')."]/",$clink{$i}) && alert('自定链接只能由英文字母、数字或_-~组成(不支持中文)');
			}
		}
	    isset($_POST['keywordToTag']) && $tags=$keywords;
	    if($iCMS->config['autodesc']=="1" && !empty($iCMS->config['descLen']) && empty($description) && empty($url)){
	    	 $description=csubstr(HtmToText($body),$iCMS->config['descLen']);
	    }
	    $remote && remote($body);
	    (!$remote&&$autopic) && remote($body,true);
	    
		empty($clink)&& $clink=pinyin($title,$iCMS->config['CLsplit']);
		$isPic=empty($pic)?0:1;
		$dellink && $body = add_magic_quotes(preg_replace ("'<a[^>]*?>(.*?)<\/a>'si", "\\1", stripslashes($body)));
		if(empty($aid)){
			empty($userid) && $userid=$Admin->uId;
			$hits=$good=$bad=$comments=0;
		    $checkCL=$iCMS->db->getValue("SELECT `id` FROM `#iCMS@__article` where `clink` ='$clink'");
		    if($iCMS->config['repeatitle']){
		    	$iCMS->db->getValue("SELECT `id` FROM `#iCMS@__article` where `title` = '$title'") && alert('该标题的文章已经存在!请检查是否重复');
		    	$checkCL && alert('该自定链接已经存在!请另选一个');
		    }else{
				$checkCL && $clink.=$iCMS->config['CLsplit'].random(6,1);
		    }
		    $iCMS->db->insert('article',compact('cid','title','stitle','clink','url','source','author','editor','userid','postype','keywords','tags','description','related','isPic','pic','pubdate','hits' ,'good','bad','comments','type','vlink','top','status'));
			$aid	= $iCMS->db->insert_id;
		    if(empty($url)){
			    $iCMS->db->insert('articledata',compact('aid','subtitle','tpl','body'));
				insert_db_remote($body,$aid);
			}
	    	addtags($tags,$aid);
	    	//tags_cache();
	    	$vlink=empty($vlink)?$cid:$vlink.','.$cid;
	    	vlinkDiff($vlink,'',$aid);
			if($iCMS->config['ishtm'] && $status){
				MakeArticleHtm($aid);
			}
			$iCMS->db->query("UPDATE `#iCMS@__catalog` SET `count` = count+1 WHERE `id` ='$cid' LIMIT 1 ");
			$moreaction=array(
				array("text"=>"编辑该文章","url"=>__SELF__."?do=article&operation=add&id=".$aid),
				array("text"=>"继续添加文章","url"=>__SELF__."?do=article&operation=add&cid=".$cid),
				array("text"=>"查看该文章","url"=>$iCMS->iurl('show',array('id'=>$aid,'link'=>$clink,'url'=>$url,'cid'=>$cid,'dir'=>$catalog->catalog[$cid]['dir'],'domain'=>$catalog->catalog[$cid]['domain'],'pubdate'=>$pubdate,'htmlext'=>$catalog->catalog[$cid]['htmlext']))->href,"o"=>'target="_blank"'),
				array("text"=>"查看网站首页","url"=>"../index.php","o"=>'target="_blank"')
			);
			redirect("文章添加完成!",__SELF__."?do=article&operation=manage",'10',$moreaction);
		}else{
		    $checkCL=$iCMS->db->getValue("SELECT `id` FROM `#iCMS@__article` where `clink` ='$clink' AND `id` !='$aid'");
		    if($iCMS->config['repeatitle']){
		     	$checkCL && alert('该自定链接已经存在!请另选一个');
		    }else{
		    	$checkCL && $clink.=$iCMS->config['CLsplit'].random(6,1);
		    }
			$art=$iCMS->db->getRow("SELECT `cid`,`tags`,`vlink` FROM `#iCMS@__article` where `id` ='$aid'");
			TagsDiff($tags,$art->tags,$aid);
			//tags_cache();
			$iCMS->db->update('article',compact('cid','title','stitle','clink','url','source','author','editor','userid','postype','keywords','tags','description','related','isPic','pic','pubdate','type','vlink','top','status'),array('id'=>$aid));
			$vlink=empty($vlink)?$cid:$vlink.','.$cid;
			vlinkDiff($vlink,$art->vlink,$aid);

			if(empty($url)){
				if($iCMS->db->getValue("SELECT `id` FROM `#iCMS@__articledata` where `aid` ='$aid'")){
					$iCMS->db->update('articledata',compact('tpl','subtitle','body'),compact('aid'));
				}else{
					$iCMS->db->insert('articledata',compact('aid','subtitle','tpl','body'));
				}
				insert_db_remote($body,$aid);
			}
			if($iCMS->config['ishtm'] && $status){
				MakeArticleHtm($aid);
			}
			if($art->cid!=$cid) {
				$iCMS->db->query("UPDATE `#iCMS@__catalog` SET `count` = count-1 WHERE `id` ='{$art->cid}' LIMIT 1 ");
				$iCMS->db->query("UPDATE `#iCMS@__catalog` SET `count` = count+1 WHERE `id` ='$cid' LIMIT 1 ");
			}
			redirect("文章编辑完成!",__SELF__."?do=article&operation=manage",'3');
		}
	break;
	case 'del':
		empty($_POST['id']) && alert("请选择要删除的文章");
		if(is_array($_POST['id'])){
			foreach($_POST['id'] AS $id){delArticle($id);}
		}
		alert('全部成功删除!',"url:1");
	break;
	case 'passed':
		empty($_POST['id']) && alert("请选择要显示的文章");
		if(is_array($_POST['id'])){
			$ids=implode(',',$_POST['id']);
			$iCMS->db->query("UPDATE `#iCMS@__article` SET `status` = '1' WHERE `id` IN ($ids)");
		}
		_Header();
	break;
	case 'top':
		empty($_POST['id']) && alert("请选择要设置置顶权重的文章");
		$top=$_POST['top'];
		if(is_array($_POST['id'])){
			$ids=implode(',',$_POST['id']);
			$iCMS->db->query("UPDATE `#iCMS@__article` SET `top` = '$top' WHERE `id` IN ($ids)");
		}
		_Header();
	break;
	case 'passTimeALL':
		$iCMS->db->query("UPDATE `#iCMS@__article` SET `status` = '1',pubdate='".time()."' WHERE `status` = '0'");		
		_Header(__REF__);
	break;
	case 'passALL':
		$iCMS->db->query("UPDATE `#iCMS@__article` SET `status` = '1' WHERE `status` = '0'");		
		_Header(__REF__);
	break;
	case 'TimeALL':
		$iCMS->db->query("UPDATE `#iCMS@__article` SET pubdate='".time()."' WHERE `status` = '0'");		
		_Header(__REF__);
	break;
	case 'cancel':
		empty($_POST['id']) && alert("请选择要隐藏的文章");
		if(is_array($_POST['id'])){
			$ids=implode(',',$_POST['id']);
			$iCMS->db->query("UPDATE `#iCMS@__article` SET `status` = '0' WHERE `id` IN ($ids)");
		}
		_Header();
	break;
	case 'updateHTML':
		empty($_POST['id']) && alert("请选择要更新的文章");
		$i=0;
		if(is_array($_POST['id'])){
			foreach($_POST['id'] AS $aid){
				MakeArticleHtm($aid) && $i++;
			}
		}
		alert($i.'个文件更新完成!',"url:1");
	break;
	case 'contentype':
		empty($_POST['id']) && alert("请选择要更改的文章");
	    $type = $_POST['type'];
	    $ids=implode(',',$_POST['id']);
		$iCMS->db->query("UPDATE `#iCMS@__article` SET `type` = '$type' WHERE `id` IN ($ids)");
		alert('文章属性更改完成!',"url:1");
	break;
	case 'keyword':
		empty($_POST['id']) && alert("请选择文章");
		$ids=implode(',',$_POST['id']);
		if($_POST['pattern']=='replace'){
			$iCMS->db->query("UPDATE `#iCMS@__article` SET `keywords` = '".dhtmlspecialchars($_POST['keyword'])."' WHERE `id` IN ($ids)");
		}elseif($_POST['pattern']=='addto'){
			$iCMS->db->query("UPDATE `#iCMS@__article` SET `keywords` = CONCAT(keywords,',".dhtmlspecialchars($_POST['keyword'])."') WHERE `id` IN ($ids)");
		}
		alert('文章关键字更改完成!',"url:1");
	break;
	case 'tag':
		empty($_POST['id']) && alert("请选择文章");
		$ids=implode(',',$_POST['id']);
		if($_POST['pattern']=='replace'){
			$iCMS->db->query("UPDATE `#iCMS@__article` SET `tags` = '".dhtmlspecialchars($_POST['tag'])."' WHERE `id` IN ($ids)");
		}elseif($_POST['pattern']=='addto'){
			$iCMS->db->query("UPDATE `#iCMS@__article` SET `tags` = CONCAT(tags,',".dhtmlspecialchars($_POST['tag'])."') WHERE `id` IN ($ids)");
		}
		alert('文章标签更改完成!',"url:1");
	break;
	case 'vlink':
		empty($_POST['id']) && alert("请选择文章");
		$vlink	= empty($_POST['vlink'])?"":implode(',',$_POST['vlink']);
		$ids=implode(',',$_POST['id']);
		if($_POST['pattern']=='replace'){
			$iCMS->db->query("UPDATE `#iCMS@__article` SET `vlink` = '".$vlink."' WHERE `id` IN ($ids)");
		}elseif($_POST['pattern']=='addto'){
			$iCMS->db->query("UPDATE `#iCMS@__article` SET `vlink` = CONCAT(vlink,',".$vlink."') WHERE `id` IN ($ids)");
		}
		alert('文章虚拟链接更改完成!',"url:1");
	break;
	case 'thumb':
		empty($_POST['id']) && alert("请选择要提取缩略图的文章");
		if(is_array($_POST['id'])){
			foreach($_POST['id'] AS $id){
				$content	= $iCMS->db->getValue("SELECT body FROM `#iCMS@__articledata` WHERE aid='$id'");
				$img 		= array();
				preg_match_all("/<img.*?src\s*=[\"|'|\s]*(http:\/\/.*?\.(gif|jpg|jpeg|bmp|png)).*?>/is",$content,$img);
				$_array = array_unique($img[1]);
				foreach($_array as $key =>$value){
					$value = uploadpath($value,'http2iPATH');
					if(file_exists($value)){
						$value = uploadpath($value,'-iPATH');
						$iCMS->db->query("UPDATE `#iCMS@__article` SET `isPic`='1',`pic` = '$value' WHERE `id` = '$id'");
						break;
					}
				}
			}
		}
		alert('成功提取缩略图',"url:1");
	break;
	case 'move':
		empty($_POST['id']) && alert("请选择要移动的文章");
		!$_POST['cataid'] && alert("请选择目标栏目");
		$cid=intval($_POST['cataid']);
		if(is_array($_POST['id'])){
			foreach($_POST['id'] AS $id){
				$id=intval($id);
				$acid=$iCMS->db->getValue("SELECT `cid` FROM `#iCMS@__article` where `id` ='$id'");
				$iCMS->db->query("UPDATE `#iCMS@__article` SET cid='$cid' WHERE `id` ='$id'");
				if($acid!=$cid) {
					$iCMS->db->query("UPDATE `#iCMS@__catalog` SET `count` = count-1 WHERE `id` ='{$acid}' LIMIT 1 ");
					$iCMS->db->query("UPDATE `#iCMS@__catalog` SET `count` = count+1 WHERE `id` ='{$cid}' LIMIT 1 ");
				}
			}
		}
		alert('成功移动到目标栏目',"url:1");
	break;
	default:
		foreach($_POST['order'] AS $id=>$order){
			$iCMS->db->query("UPDATE `#iCMS@__article` SET `order` = '$order' WHERE `id` ='$id'");
		}
		_Header();
	}
break;
}

function remote(&$content,$one=false){
	global $iCMS,$title;
	$content = stripslashes($content);
	$img = array();
	preg_match_all("/<img.*?src\s*=[\"|'|\s]*(http:\/\/.*?\.(gif|jpg|jpeg|bmp|png)).*?>/is",$content,$match);
	$_array = array_unique($match[1]);
	$uri 	= parse_url($iCMS->config['uploadURL']);
	if($_array)foreach($_array AS $_k=> $imgurl){
		if(strstr(strtolower($imgurl),$uri['host'])) unset($_array[$_k]);
	}
	if(empty($_array)) return;

	set_time_limit(0);
	$RootPath		= FS::path_join(iPATH,$iCMS->config['uploadfiledir']).'/';//绝对路径
	$_CreateDir = "";
	if($iCMS->config['savedir']){
		$_CreateDir = str_replace(array('Y','y','m','n','d','j','EXT'),
		array(get_date('','Y'),get_date('','y'),get_date('','m'),get_date('','n'),get_date('','d'),get_date('','j'),$FileExt),
		$iCMS->config['savedir'])."/";
	}
	$RootPath		= $RootPath.$_CreateDir;
	$milliSecond = 'remote_'.get_date('',"YmdHis").rand(1,99999);
	FS::mkdir($RootPath);
	require_once(iPATH.'include/snoopy.class.php');
	require_once(iPATH.'include/upload.class.php');
	$Snoopy = new Snoopy;
	$Snoopy->agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5";
	$Snoopy->accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
	foreach($_array as $key =>$value){
		$FileExt=strtolower(FS::getExt($value));//&#316;&#701;
		iUpload::CheckValidExt($FileExt);//判断文件类型
		//过滤文件;
		strstr($FileExt, 'ph') && $FileExt="phpfile";
		in_array($FileExt,array('cer','htr','cdx','asa','asp','jsp','aspx','cgi')) && $FileExt.="file";
		
		$FileName		= $milliSecond.$key.".".$FileExt;
		$FilePath		= $_CreateDir.$FileName;
		$FileRootPath	= $RootPath.$FileName;
		$Snoopy->fetch($value);
		if($Snoopy->results){
			FS::write($FileRootPath,$Snoopy->results);
			if(in_array($FileExt,array('gif','jpg','jpeg','png'))){
				if($iCMS->config['isthumb'] &&($iCMS->config['thumbwidth']||$iCMS->config['thumbhight'])){
					list($width, $height,$imagetype) = getimagesize($FileRootPath);
					if ( $width > $iCMS->config['thumbwidth'] || $height >$iCMS->config['thumbhight'] ) { 
						FS::mkdir($RootPath."thumb");
					}
					$Thumbnail=iUpload::thumbnail($RootPath, $FileRootPath, $milliSecond.$key);
					!empty($Thumbnail['filepath']) && iUpload::watermark($Thumbnail['filepath']);
				}
				iUpload::watermark($FileRootPath);
			}
			$_FileSize = @filesize($FileRootPath);
			empty($_FileSize) && $_FileSize=0;
			$iCMS->db->query("INSERT INTO `#iCMS@__file` (`filename`,`ofilename`,`path`,`intro`,`ext`,`size` ,`time`,`type`) VALUES ('$FileName', '$value', '$FilePath','$title', '$FileExt', '$_FileSize', '".time()."', '1') ");
			$content = str_replace($value,uploadpath($FilePath,'+http'),$content);
			if($one && $key==0)break;
		}
	}
	$content = add_magic_quotes($content);
}
function insert_db_remote($content,$aid){
	global $iCMS,$autopic;
	$content = stripslashes($content);
	$img = array();
	preg_match_all("/<img.*?src\s*=[\"|'|\s]*(http:\/\/.*?\.(gif|jpg|jpeg|bmp|png)).*?>/is",$content,$match);
	$_array = array_unique($match[1]);
	set_time_limit(0);
	foreach($_array as $key =>$value){
		$value = uploadpath($value,'-http');
		$filename = basename($value);
		$pic=$iCMS->db->getValue("SELECT `pic` FROM `#iCMS@__article` WHERE `id` = '$aid'");
		($autopic && $key==0 && empty($pic)) && $iCMS->db->query("UPDATE `#iCMS@__article` SET `isPic`='1',`pic` = '$value' WHERE `id` = '$aid'");
		$faid=$iCMS->db->getValue("SELECT `aid` FROM `#iCMS@__file` WHERE `filename` ='$filename'");
		empty($faid) && $iCMS->db->query("UPDATE `#iCMS@__file` SET `aid` = '$aid' WHERE `filename` ='$filename'");
	}
}
?>
