<?php
define('IN_SMT',true);
define('CURSCRIPT','post');
define('IN_MYMPS', true);
define('IN_MANAGE',true);

require_once dirname(__FILE__)."/include/global.php";
require_once dirname(__FILE__)."/data/config.php";

ifsiteopen();
require_once MYMPS_DATA."/config.db.php";
require_once MYMPS_INC."/db.class.php";
require_once MYMPS_SMARTY."/include.php";
require_once MYMPS_INC."/upfile.fun.php";
require_once MYMPS_DATA."/config.inc.php";
$data = '';
@include MYMPS_DATA.'/caches/authcodesettings.php';
$authcodesettings = $data;
$data = NULL;

!in_array($action,array('input','edit','ok')) && $action = 'input';
$action = isset($action) ? trim($action) : '';

if($action != 'ok')
{
	$ip = '';
	$ip = GetIP();
	$ip2area 	= $address = $ipdata = '';
	require_once MYMPS_INC.'/ip.class.php';
	$ipdata  = new ip();
	$address = $ipdata -> getaddress($ip);
	$ip2area = $address['area1'].$address['area2'];
	unset($ipdata,$address);
}

if($act == 'dopost') {
	
	if(!$mixcode || $mixcode != md5($cookiepre)){
		die('FORBIDDEN');
		exit();
	}
	
	empty($areaid) && write_msg('请选择您要发布的地区！');
	empty($title) && write_msg("请输入信息标题!");
	empty($content) && write_msg("您还没有输入信息描述!");
	empty($tel) && write_msg("联系电话不能为空！");
	empty($contact_who) && write_msg("联系人不能为空！");
	
	mymps_check_upimage("mymps_img_");
	$id = $action == 'edit' ? intval($id) : '';
	$userid 	= isset($userid) ? mhtmlspecialchars($userid) : '';
	$manage_pwd	= isset($manage_pwd) ? trim($manage_pwd) : '';
	$catid 		= intval($catid);
	if(empty($catid)) write_msg('您未指定发布的信息栏目');
	$areaid 	= intval($areaid);
	$title 		= trim(mhtmlspecialchars($title));
	$content	= $mymps_global['cfg_post_editor'] == 1 ? $content : textarea_post_change($content) ;
	$begintime 	= $timestamp;
	$activetime	= $endtime 	= intval($endtime);
	$endtime 	= ($endtime == 0)?0:(($endtime*3600*24)+$begintime);
	$ismember 	= intval($ismember);
	$mappoint   = isset($mappoint) ? trim(mhtmlspecialchars($mappoint)) : '';
	$tel		= isset($tel) ? trim(mhtmlspecialchars($tel)) : '';
	$qq			= isset($qq) ? trim(mhtmlspecialchars($qq)) : '';
	$web_address= trim(mhtmlspecialchars($web_address));
	$email		= isset($email) ? trim(mhtmlspecialchars($email)) : '';
	$result 	= verify_badwords_filter($mymps_global['cfg_if_info_verify'],$title,$content);
	$title 		= $result['title'];
	$content 	= $result['content'];
	$content	= preg_replace("/<a[^>]+>(.+?)<\/a>/i","$1",$content);;//去除超链接文字和代码
	$info_level = $result['level'];
	unset($result);
	$extra		= isset($extra) ? mhtmlspecialchars($extra) : '';
	
	if($action == 'input'){
	
		if(!empty($mymps_global['cfg_allow_post_area']) && !empty($ip2area)){
			$i = 1;
			$allow_post_area = array();
			$allow_post_area = explode('=',$mymps_global['cfg_allow_post_area']);
			$allow_post_areas = explode(',',$allow_post_area[0]);
			foreach($allow_post_areas as $k => $v){
				if(strstr($ip2area,$v)) {
					$i=$i+1;
				}
			}
			if($allow_post_area[1] == '-1' && $i == 1){
				write_msg("系统判断您的IP并非<b style='color:red'>".$allow_post_area[0]."</b>本地IP！<br />如果您要继续操作，请联系客服。");
				exit;
			} elseif($allow_post_area[1] == 0 && $i == 1) {
				$info_level = 0;
			}
			unset($allow_post_area,$address,$ipdata,$allow_post_areas,$i);
		}
		
		$checkquestion = isset($checkquestion) ? $checkquestion : '';
		$data = '';
		@include MYMPS_DATA.'/caches/checkanswer_settings.php';
		if(is_array($data)){
			$whenpost = $data['whenpost'];
			$result   = read_static_cache('checkanswer');
			if($whenpost == 1 && is_array($result)){
				if(!is_array($checkquestion) || empty($checkquestion['answer']) || empty($checkquestion['id'])){
					write_msg('您还没有输入验证问题！');
					exit;
				}
				if($result[$checkquestion['id']]['answer'] != $checkquestion['answer']){
					write_msg('您输入的验证问题答案不正确，请重新输入！');
				}
			}
			$result = $checkquestion = $whenpost = $data = NULL;
		}
		
		$img_count	= upload_img_num('mymps_img_');

		/*
		ajax已验证过了，不再重复验证
		if(is_array($extra)){
			$row = '';
			foreach ($extra as $k => $v){
				$row = $db -> getRow("SELECT title,required FROM `{$db_mymps}info_typeoptions` WHERE identifier = '$k'");
				if($row['required'] == 'on' && empty($v)) write_msg($row['title'].'不能为空！');
			}
			$k = $v = NULL;
		}*/
		
		if(!empty($mymps_global['cfg_disallow_post_tel']) && !empty($tel)){
			$disallow_tel = array();
			$disallow_tel = explode('=',$mymps_global['cfg_disallow_post_tel']);
			$disallow_telarray = explode(',',$disallow_tel[0]);
			if($disallow_tel[1] == -1){
				in_array($tel,$disallow_telarray) && write_msg("您的电话号码<b style='color:red'>".$tel."</b> 已被管理员加入黑名单！<br />如果您要继续操作，请联系客服。");
			} elseif($disallow_tel[1] == 0) {
				in_array($tel,$disallow_telarray) && $info_level = 0;
			}
			unset($disallow_tel,$disallow_telarray);
		}
		
		if (empty($ismember)){
			($mymps_global['cfg_if_nonmember_info'] == '0') && write_msg('对不起，您还没有登录！请您登录后再发布信息！');
			empty($manage_pwd) && write_msg("请输入您的管理密码！以便于以后对该信息的修改和删除");
			empty($contact_who) && write_msg("请填写联系人！");
			$manage_pwd = md5($manage_pwd);
			
			if($authcodesettings['post'] == 1 && !$randcode = mymps_chk_randcode($checkcode)){
				write_msg('验证码输入错误，请返回重新输入');
			}
			
			$sql = "INSERT INTO `{$db_mymps}information` (title,content,catid,areaid,begintime,activetime,endtime,manage_pwd,ismember,ip,ip2area,info_level,qq,email,tel,contact_who,img_count,mappoint)VALUES('$title','$content','$catid','$areaid','$begintime','$activetime','$endtime','$manage_pwd','$ismember','$ip','$ip2area','$info_level','$qq','$email','$tel','$contact_who','$img_count','$mappoint')";
		}elseif($ismember == 1){
			$s_uid = '';
			require_once MYMPS_INC."/member.class.php";
			if(!$member_log->chk_in()) write_msg("对不起,您还没有登录！");
			chk_member_purview("purview_info");
			$perpost_money_cost = $mymps_global['cfg_member_perpost_consume'];
			$userid = trim($s_uid);
			/*信息认证情况*/
			if($userid){
				$row = $db ->getRow("SELECT per_certify,com_certify FROM `{$db_mymps}member` WHERE userid = '$userid'");
				if($row['per_certify'] == 1 || $row['com_certify'] == 1){
					$certify = 1;
				}else{
					$certify = 0;
				}
				unset($row);
			}
			
			if($authcodesettings['memberpost'] == 1 && !$randcode = mymps_chk_randcode($checkcode)){
				write_msg('验证码输入错误，请返回重新输入');
			}
			
			$sql = "INSERT INTO `{$db_mymps}information` (title,content,begintime,activetime,endtime,catid,areaid,userid,ismember,ip,ip2area,info_level,qq,email,tel,contact_who,img_count,mappoint,certify) Values ('$title','$content','$begintime','$activetime','$endtime','$catid','$areaid','$userid','$ismember','$ip','$ip2area','$info_level','$qq','$email','$tel','$contact_who','$img_count','$mappoint','$certify')";
			
			
			/*积分变化*/
			$score_change = get_credit_score();
			$score_changer = $score_change['score']['rank']['information'];
			if($score_changer){
				$db->query("UPDATE `{$db_mymps}member` SET score = score".$score_changer." WHERE userid = '$userid'");
			}
			$score_change = $score_changer = NULL;
			
			/*金币变化*/
			if(!empty($perpost_money_cost)){
				$db->query("UPDATE `{$db_mymps}member` SET money_own = money_own - '$perpost_money_cost' WHERE userid = '$userid'");
			}
		} else {
			exit('Access Denied!');
		}
		
		$db -> query($sql);
		$id = $db -> insert_id();
		
		$k = $v = NULL;
		if(is_array($extra)){
			mymps_delete("info_extra","WHERE infoid = '$id'");
			foreach ($extra as $k => $v){
				if(is_array($v)){
					$v = !empty($v) ? join(',', $v) : 0;
					$v = trim(mhtmlspecialchars($v));
				}
				$db->query("INSERT INTO `{$db_mymps}info_extra` (infoid,name,value)VALUES('$id','$k','$v')");
			}
		}
		
		if($img_count > 0){
			for($i=0;$i<$img_count;$i++){
				$name_file = "mymps_img_".$i;
				if($_FILES[$name_file]['name']){
					$destination="/information/".date('Ym')."/";
					$mymps_image = start_upload($name_file,$destination,$mymps_global['cfg_upimg_watermark'],$mymps_mymps['cfg_information_limit']['width'],$mymps_mymps['cfg_information_limit']['height']);
					$db -> query("INSERT INTO `{$db_mymps}info_img` (image_id,path,prepath,infoid,uptime) VALUES ('$i','$mymps_image[0]','$mymps_image[1]','$id','$timestamp')");
				}
			}
			$db -> query("UPDATE `{$db_mymps}information` SET img_path = '$mymps_image[1]' WHERE id = '$id'");
		}
		
		//清除信息数据统计缓存
		clear_cache_files('information_count');
		
		/*生成HTML*/
		$seo = get_seoset();
		if($seo['seo_force_info'] == 'html' && $info_level > 0){
			/*生成信息页HTML*/
			include MYMPS_ASS.'/info.php';
			$template_info = $db -> getOne("SELECT template_info FROM `{$db_mymps}category` WHERE catid = '$catid'");
			$infopath = $filepath = $seo['seo_htmldir'].replace_html_style($mymps_global['glb_html_information'],$id);
			$html_c = $smarty -> fetch(mymps_tpl($template_info ? $template_info : 'info','smarty'));
			$html_m = $smarty -> MakeHtmlFile(MYMPS_ROOT.$filepath,$html_c);
			$template_info = NULL;
			$db -> query("UPDATE `{$db_mymps}information` SET html_path = '$filepath' WHERE id = '$id'");
			
			if($seo['seo_html_make'] == 'index') {
				include MYMPS_ASS.'/index.php';
				$filepath = '/index'.$seo['seo_htmlext'];
				$html_c = $smarty -> fetch(mymps_tpl($tpl_index['banmian'] ? $tpl_index['banmian'] : 'simple','smarty'));
				$html_m = $smarty -> MakeHtmlFile(MYMPS_ROOT.$filepath,$html_c);
				$tpl_index = NULL;
			} elseif($seo['seo_html_make'] == 'category') {
				$html_make_row = $db -> getRow("SELECT b.html_dir,a.parentid,b.template FROM `{$db_mymps}category` AS a LEFT JOIN `{$db_mymps}category` AS b ON a.parentid = b.catid WHERE a.catid = '$catid'");
				$areaid = NULL;
				/*生成根栏目列表页HTML*/
				$catid = $html_make_row['parentid'];
				include MYMPS_ASS.'/info_list.php';
				$filepath = $seo['seo_htmldir'].$html_make_row['html_dir'].'index'.$seo['seo_htmlext'];
				$html_c = $smarty -> fetch(mymps_tpl($html_make_row['template'] ? $html_make_row['template'] :'list','smarty'));
				$html_m = $smarty -> MakeHtmlFile(MYMPS_ROOT.$filepath,$html_c);
			} elseif($seo['seo_html_make'] == 'index_category') {
				include MYMPS_ASS.'/index.php';
				$filepath = '/index'.$seo['seo_htmlext'];
				$html_c = $smarty -> fetch(mymps_tpl($tpl_index['banmian'] ? $tpl_index['banmian'] : 'simple','smarty'));
				$html_m = $smarty -> MakeHtmlFile(MYMPS_ROOT.$filepath,$html_c);
				$areaid = NULL;
				$html_make_row = $db -> getRow("SELECT b.template,b.html_dir,a.parentid FROM `{$db_mymps}category` AS a LEFT JOIN `{$db_mymps}category` AS b ON a.parentid = b.catid WHERE a.catid = '$catid'");
				/*生成根栏目列表页HTML*/
				$catid = $html_make_row['parentid'];
				include MYMPS_ASS.'/info_list.php';
				$filepath = $seo['seo_htmldir'].$html_make_row['html_dir'].'index'.$seo['seo_htmlext'];
				$html_c = $smarty -> fetch(mymps_tpl($html_make_row['template'] ? $html_make_row['template'] :'list','smarty'));
				$html_m = $smarty -> MakeHtmlFile(MYMPS_ROOT.$filepath,$html_c);
			}
			unset($html_c,$html_m,$filepath,$html_make_row);
		}
		write_msg("","?action=ok&id=".$id."&catid=".$catid."&title=".urlencode($title)."&level=".$info_level."&filepath=".$infopath);
		
	} elseif($action == 'edit'){
		
		if(is_array($_FILES)){
			for($i=0;$i<count($_FILES);$i++){
				$name_file = "mymps_img_".$i;
				if($_FILES[$name_file]['name']){
					$destination = "/information/".date('Ym')."/";
					$mymps_image = start_upload($name_file,$destination,$mymps_global['cfg_upimg_watermark'],$mymps_mymps['cfg_information_limit']['width'],$mymps_mymps['cfg_information_limit']['height']);
					if($row = $db -> getRow("SELECT path,prepath FROM `{$db_mymps}info_img` WHERE infoid = '$id' AND image_id = '$i'")){
						@unlink(MYMPS_ROOT.$row['path']);
						@unlink(MYMPS_ROOT.$row['prepath']);
						$db->query("UPDATE `{$db_mymps}info_img` SET image_id = '$i' , path = '$mymps_image[0]' , prepath = '$mymps_image[1]' , uptime = '$timestamp' WHERE image_id = '$i' AND infoid = '$id'");
					} else {
						$db->query("INSERT INTO `{$db_mymps}info_img` (image_id,path,prepath,infoid,uptime) VALUES ('$i','$mymps_image[0]','$mymps_image[1]','$id','$timestamp')");
					}
					$db -> query("UPDATE `{$db_mymps}information` SET img_path = '$mymps_image[1]' WHERE id = '$id'");
				}
			}
		}
		
		if(is_array($delinfoimg)){
			foreach($delinfoimg as $key => $val){
				if($val == 'on'){
					$infoimgrow = $db -> getRow("SELECT id,path,prepath FROM `{$db_mymps}info_img` WHERE image_id = '$key' AND infoid = '$id'");
					if($infoimgrow){
						@unlink(MYMPS_ROOT.$infoimgrow['path']);
						//@unlink(MYMPS_ROOT.$infoimgrow['prepath']);
						mymps_delete("info_img","WHERE id = '$infoimgrow[id]'");
					}
					unset($infoimgrow);
				}
			}
		}

		$k = $v = NULL;
		if(is_array($extra)){
			$extra = array_reverse($extra);
			mymps_delete("info_extra","WHERE infoid = '$id'");
			foreach ($extra as $k => $v){
				/*$row = $db -> getRow("SELECT title,required FROM `{$db_mymps}info_typeoptions` WHERE identifier = '$k'");*/
				/*if($row['required'] == 'on' && empty($v)) write_msg ($row[title]."不能为空！");*/
				//if(strlen($v)>254) write_msg($row[title]."不得超过255个字符！");
				is_array($v) && $v = !empty($v) ? join(',', $v) : 0;
				$db->query("INSERT INTO `{$db_mymps}info_extra` (infoid,name,value)VALUES('$id','$k','$v')");
			}
		}
		
		$manage_pwd = empty($manage_pwd) ? "" : "manage_pwd='".md5($manage_pwd)."',";
		$userid 	= empty($userid) ? "" : "userid='$userid',";
		$img_count 	= mymps_count("info_img","WHERE infoid = '$id'");
		$img_path	= $mymps_image[1] ? $mymps_image[1] : '';
		
		$sql 		= "UPDATE `{$db_mymps}information` SET {$manage_pwd} {$userid} title = '$title',content = '$content',catid = '$catid', areaid = '$areaid', activetime = '$activetime', endtime = '$endtime', ismember = '$ismember' , ip = '$ip' , ip2area = '$ip2area' , info_level = '$info_level' , qq = '$qq' , email = '$email' , tel = '$tel' , contact_who = '$contact_who' , img_count = '$img_count' , mappoint = '$mappoint' WHERE id = '$id'";
		$db->query($sql);
		
		clear_tpl_files(2);//删除smarty模板缓存
		
		/*生成HTML*/
		$seo = get_seoset();
		if($seo['seo_force_info'] == 'html' && $info_level > 0){
			/*生成信息页HTML*/
			include MYMPS_ASS.'/info.php';
			$infopath = $filepath = $seo['seo_htmldir'].replace_html_style($mymps_global['glb_html_information'],$id);
			$html_c = $smarty -> fetch(mymps_tpl('info','smarty'));
			$html_m = $smarty -> MakeHtmlFile(MYMPS_ROOT.$filepath,$html_c);
			$db -> query("UPDATE `{$db_mymps}information` SET html_path = '$filepath' WHERE id = '$id'");
			unset($html_c,$html_m,$filepath,$html_make_row);
		}
		write_msg("操作成功！您已经成功修改该信息！<br />若信息内容无变化请刷新浏览器！",Rewrite('info',array('id'=>$id,'catid'=>$catid,'html_path'=>$infopath)));
	}
} else {
	include MYMPS_ASS.'/'.CURSCRIPT.'.php';
}

is_object($db) && $db->Close();
$mymps_global = $smarty = $authcodesettings = $mymps_mymps = $ip = $ip2area = $db = $seo = $extra = $member_log = NULL;
?>