<?php
if(!defined('IN_MYMPS')) exit('Forbidden');
require MYMPS_INC."/upfile.fun.php";
$ac = in_array($ac,array('list','new','edit')) ? $ac : 'list';
$documenttype = get_member_docutype();
if(submit_check('document_submit')){
	
	$name_file 	= 'docu_image';
	
	if(in_array($ac,array('new','edit'))){
		//require MYMPS_DATA.'/config.inc.php';
		$title = isset($title) ? trim($title) : '';
		$author = isset($author) ? trim($author) : '';
		$source = isset($source) ? trim($source) : '';
		$tid = isset($tid) ? intval($tid) : '';
		$content = isset($content) ? trim($content) : '';
		$content = maddslashes($content);
		$content = preg_replace("/<a[^>]+>(.+?)<\/a>/i","$1",$content);
		if(empty($title)) write_msg('','?m=document&type=corp&ac='.$ac.'&error=30&tid='.$tid.'&id='.$id);
		if(empty($author)) write_msg('','?m=document&type=corp&ac='.$ac.'&error=31&tid='.$tid.'&id='.$id);
		if(empty($author)) write_msg('','?m=document&type=corp&ac='.$ac.'&error=32&tid='.$tid.'&id='.$id);
		if(empty($content)) write_msg('','?m=document&type=corp&ac='.$ac.'&error=33&tid='.$tid.'&id='.$id);
		if(empty($tid)) write_msg('','?m=document&type=corp&ac='.$ac.'&error=34&tid='.$tid.'&id='.$id);
	}
	
	if($ac == 'new'){
		$article_maxpost = $group['article_maxpost'];
		$cur_article=mymps_count("member_docu"," WHERE userid = '$s_uid'");
		if($cur_article >= $article_maxpost && $article_maxpost > 0){
			write_msg('','?m=document&type=corp&ac=list&error=50');
			exit;
		}
		
		$result 	= verify_badwords_filter($title,$content);
		$title 		= $result['title'];
		$content 	= $result['content'];
		$content 	= GetCurSinImg($content);
		$content	= preg_replace("/<a[^>]+>(.+?)<\/a>/i","$1",$content);
		unset($result);
		
		if($documenttype[$tid]['arrid'] == '2' && $_FILES[$name_file]['name']){
			check_upimage($name_file);
			$destination = "/document/".date('Ymd')."/";
			$mymps_image = start_upload($name_file,$destination,$mymps_global['cfg_upimg_watermark'],$mymps_mymps['cfg_normal_limit']['width'],$mymps_mymps['cfg_normal_limit']['height']);
			$imgpath 	 = $mymps_image[0];
			$pre_imgpath = $mymps_image[1];
			$db->query("INSERT INTO `{$db_mymps}member_docu` (title,keywords,author,source,pubtime,userid,content,imgpath,pre_imgpath,typeid,if_check) VALUES ('$title','$keywords','$author','$source','$timestamp','$s_uid','$content','$imgpath','$pre_imgpath','$tid','0')");
		} else {
			$db->query("INSERT INTO `{$db_mymps}member_docu` (title,keywords,author,source,pubtime,userid,content,typeid,if_check) VALUES ('$title','$keywords','$author','$source','$timestamp','$s_uid','$content','$tid','0')");
		}
		
		$id = $db -> insert_id();
				
		write_msg('','?m=document&type=corp&success=8&ac=list');
		
	} elseif($ac == 'edit' && !empty($id)) {
		
		$result 	= verify_badwords_filter($title,$content);
		$title 		= $result['title'];
		$content 	= $result['content'];
		$content	= preg_replace("/<a[^>]+>(.+?)<\/a>/i","$1",$content);
		unset($result);
		
		if(!$row = $db -> getRow("SELECT typeid,id FROM `{$db_mymps}member_docu` $where AND id = '$id'")) write_msg('','?m=document&type=corp&ac=list&error=1');
		if($docutype_arr[$row['typeid']]['arrid'] == '2' && $_FILES[$name_file]['name']){
			$destination = "/document/".date('Ymd')."/";
			check_upimage($name_file);
			$mymps_image = start_upload($name_file,$destination,$mymps_global['cfg_upimg_watermark'],$mymps_mymps['cfg_normal_limit']['width'],$mymps_mymps['cfg_normal_limit']['height'],$imgpath,$pre_imgpath);
			$imgpath 	 = $mymps_image[0];
			$pre_imgpath = $mymps_image[1];
			$db->query("UPDATE `{$db_mymps}member_docu` SET title = '$title',keywords='$keywords',content = '$content',author = '$author', source = '$source' ,pubtime = '$timestamp' ,imgpath = '$imgpath' , pre_imgpath = '$pre_imgpath' , if_check = '0' $where AND id = '$id'");
		} else {
			$db->query("UPDATE `{$db_mymps}member_docu` SET title = '$title',keywords='$keywords',content = '$content',author = '$author', source = '$source' ,pubtime = '$timestamp' , if_check = '0' $where AND id = '$id'");
		}
		
		write_msg('','?m=document&type=corp&success=8&tid='.$row[tid]);
		
	} else {
		empty($selectedids) && write_msg('','?m=document&type=corp&error=1&page='.$page.'&tid='.$tid.'&ac='.$ac);
		$create_in  = create_in($selectedids);
		$query = $db -> query("SELECT * FROM `{$db_mymps}member_docu` $where AND id ".$create_in);
		while($row = $db -> fetchRow($query)){
			$delete[$row['id']]['pre_imgpath'] = $row['pre_imgpath'];
			$delete[$row['id']]['imgpath'] = $row['imgpath'];
		}
		foreach($delete as $k => $v){
			delete_mayi_img($v['imgpath']);
			delete_mayi_img($v['pre_imgpath']);
		}
		$db -> query("DELETE FROM `{$db_mymps}member_docu` {$where} AND id ".$create_in);
		unset($delete,$row,$query,$create_in);
		write_msg('','?m=document&type=corp&success=3&page='.$page.'&tid='.$tid.'&ac='.$ac);
	}
	
} else {
	
	$tid = isset($tid) ? intval($tid) : '';
	$ifcheck_arr  = array('0'=>'<font color=red>待审</font>','1'=>'<font color=green>正常</font>');
	
	if($ac == 'list'){
		$tid == '' && $tid = $db -> getOne("SELECT MIN(typeid) FROM `{$db_mymps}member_docutype` WHERE ifview = 2");
		$where .= !empty($tid) ? " AND typeid = '$tid'" : "";
		$rows_num = mymps_count("member_docu",$where);
		$param	  = setParam(array('m','type','tid','ac'));
		$document  = page1("SELECT * FROM `{$db_mymps}member_docu` {$where} ORDER BY id DESC");
	} elseif($ac == 'new') {
		$article_maxpost = $group['article_maxpost'];
		$cur_article=mymps_count("member_docu"," WHERE userid = '$s_uid'");
		if($cur_article >= $article_maxpost && $article_maxpost > 0){
			write_msg('','?m=document&type=corp&ac=list&error=50');
			exit;
		}
		$acontent 	= get_editor('content','Member','','90%','400px');
	} elseif($ac == 'edit') {
		if(empty($id)) write_msg('','?m=document&type=corp&error=1&ac='.$ac.'&page='.$page);
		$edit = $db -> getRow("SELECT * FROM `{$db_mymps}member_docu` $where AND id = '$id'");
		$tid  = $edit['typeid'];
		$edit[content] = str_replace(array('\"\\&quot;','\\&quot;\"','\\'),array('"','"',''),$edit[content]);
		$acontent 	= get_editor('content','Member',$edit['content'],'90%','400px');
	}
	$location = location('corp');
	include in_array($ac,array('new','edit')) ? mayi_tpl('document') : mayi_tpl('document_'.$ac);
	
}

function GetContentImg($goods_desc)
{
	global $mymps_global;
	$host = $mymps_global['SiteUrl'];
    $body = stripslashes($goods_desc);
    $img_array = array();

	preg_match_all("|src=(.*) |U", htmlspecialchars_decode($body), $img_array);
    $img_array = array_unique($img_array[1]);
    $imgurl = "/editor/".date('Ymd');
    $imgpath = MYMPS_UPLOAD.$imgurl;
    $millisecond = date("YmdHms").random(5);
	mkFolder($imgpath);
    if(!is_array($img_array))
    {
       return false;
    }
    else
    {

        foreach($img_array as $key =>$value)
        {
			file_put_contents('file.log',$value."\r\n",FILE_APPEND);
			if(strpos($value,$host)===0 || strpos($value,'attachment/editor')) continue; //如果是本网页就不处理
            $value = trim(str_replace('\"','',$value));
			
            $imgAttr = get_headers($value,true);
            switch($imgAttr['Content-Type']){
                case 'image/png' :
                    $ext = 'png';
                    break;
                case 'image/jpeg' :
                    $ext = 'jpg';
                    break;
                case 'image/gif' :
                    $ext = 'gif';
                    break;
                default:
                    $ext = 'jpg';
            }
            $filesz=$imgAttr[Content-Length][1];
			if($filesz<=200*1024){
				$get_file = @file_get_contents($value);
				$rndfilename = $imgpath."/".$millisecond.$key.".".$ext;
				$fileurl = $host.'/attachment'.$imgurl."/".$millisecond.$key.".".$ext;
				
				if($get_file)
				{
					$fp = @fopen($rndfilename,"w");
					@fwrite($fp,$get_file);
					@fclose($fp);
					//WaterImg($rndfilename);
				}
				$body = str_replace($value,$fileurl,$body);
			}
        }
    }
    $body = addslashes($body);
    return $body;
}
?>