<?php
!defined('WAP') && exit('FORBIDDEN');
define('CURSCRIPT','post');

$catid = isset($_REQUEST['catid']) ? intval($_REQUEST['catid']) : '';
$areaid = isset($_REQUEST['areaid']) ? intval($_REQUEST['areaid']) : '';
$streetid = isset($_REQUEST['streetid']) ? intval($_REQUEST['streetid']) : '';
$extra = isset($_POST['extra']) ? mhtmlspecialchars($_POST['extra']) : '';

if(!$cityid){
	redirectmsg("请先选择您所在的分站","index.php?mod=changecity");
	exit;
}

if($action == 'post'){
	$title       = isset($_POST['title']) ? trim(mhtmlspecialchars($_POST['title'])) : '';
	$content     = isset($_POST['content']) ? textarea_post_change($_POST['content']) : '';
	$contact_who = isset($_POST['contact_who']) ? trim(mhtmlspecialchars($_POST['contact_who'])) : '';
	$tel         = isset($_POST['tel']) ? trim(mhtmlspecialchars($_POST['tel'])) : '';
	$qq          = isset($_POST['qq']) ? trim(mhtmlspecialchars($_POST['qq'])) : '';
	$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'];
	$mixcode     = isset($_POST['mixcode']) ? mhtmlspecialchars($_POST['mixcode']) : '';
	$d = $db->getRow("SELECT catname,dir_typename,modid FROM `{$db_mymps}category` WHERE catid = '$catid'");
	$catname = $d['catname'];
	$dir_typename = $d['dir_typename'];
	if(!$mixcode || $mixcode != md5($cookiepre)){
		errormsg('系统判断您的来路不正确！');
	}

	$backurl = "javascript:history.back()";
	if(empty($catid)) redirectmsg('您选择发布的分类不存在!','index.php?mod=category');
	if(!$areaid = $db -> getOne("SELECT areaid FROM `{$db_mymps}area` WHERE areaid = '$areaid'")){
		redirectmsg("您选择发布的地区不存在!","index.php?mod=post&catid=".$catid."&areaid=".$areaid);
	}
	empty($areaid) && redirectmsg("请选择您要发布的地区!","index.php?mod=post&catid=".$catid);
	empty($title) && redirectmsg("请输入信息标题!",$backurl);
	empty($content) && redirectmsg("您还没有输入信息描述!",$backurl);
	empty($contact_who) && redirectmsg("联系人不能为空!",$backurl);
	empty($tel) && redirectmsg("联系电话不能为空!",$backurl);
	
	require_once MYMPS_INC."/upfile.fun.php";
	require_once MYMPS_DATA."/config.inc.php";
	
	mymps_check_upimage_wap("mymps_img_");

	$checkcode = isset($_POST['checkcode']) ? mhtmlspecialchars($_POST['checkcode']) : '';
	if($mobile_settings['authcode'] == 1 && !$randcode = mymps_chk_randcode($checkcode)){
		redirectmsg("验证码输入错误，请返回重新输入",$backurl);
	}
	
	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) && redirectmsg("您的电话号码<b style='color:red'>".$tel."</b> 已被管理员加入黑名单!<br />如果您要继续操作，请联系客服。","index.php?mod=post&catid=".$catid."&cityid=".$cityid."&areaid=".$areaid);
		} elseif($disallow_tel[1] == 0) {
			in_array($tel,$disallow_telarray) && $info_level = 0;
		}
		unset($disallow_tel,$disallow_telarray);
	}

	$ip = GetIP();
	//发布信息IP限制
	if(!empty($mymps_global['cfg_forbidden_post_ip'])){
		foreach(explode(",", $mymps_global['cfg_forbidden_post_ip']) as $ctrlip) {
			if(preg_match("/^(".preg_quote(($ctrlip = trim($ctrlip)), '/').")/", $ip)) {
				$ctrlip = $ctrlip.'%';
				redirectmsg("您当前的IP <b style='color:red'>".$ip."</b> 已被管理员加入黑名单，不允许发布信息！<br />如果您要继续操作，请联系客服。","index.php?mod=post&catid=".$catid."&areaid=".$areaid);
				exit;
			}
		}
	}
	//非该分站用户不能发布
	if($mymps_global['cfg_if_post_othercity'] == 0 && $cityid && is_array($cityarr = get_ip2city($ip))){
		if($cityid != $cityarr[cityid]) errormsg('您的IP不属于该分站，请不要在该分站下发布信息^_^');
	}

	$post_time = 1;
	if(!empty($post_time)){
		$count = mymps_count("information","WHERE ip = '$ip' AND begintime > (".time()." - 1*60)");
		$count >= $post_time && redirectmsg("您的发布时间太快了，休息一会儿。","index.php?mod=zuixin");
	}
	
	$db->getOne("SELECT id FROM `{$db_mymps}information` WHERE title = '$title' ") && redirectmsg("本信息标题已经存在，本站禁止发布重复信息！请更换标题。或者您已经发过同样信息想重复发布，可到帐号管理后台进行刷新操作即可。",$backurl);

	$img_count	= upload_img_num('mymps_img_');
	
	if($iflogin == 1){

		//会员发布信息数量限制
		$per = $db->getRow("SELECT b.perday_maxpost FROM `{$db_mymps}member` AS a LEFT JOIN `{$db_mymps}member_level` AS b ON a.levelid = b.id WHERE a.userid = '$s_uid'");
		$perday_maxpost = $per['perday_maxpost'];
		if(!empty($perday_maxpost)){
			$count = mymps_count("information","WHERE userid LIKE '$s_uid' AND begintime > '".mktime(0,0,0)."'");
			$count >= $perday_maxpost && redirectmsg("很抱歉！您当前的会员级别每天只能发布 <b style='color:red'>".$perday_maxpost."</b> 条信息<br />如果您要继续操作，请联系客服。","index.php?mod=post&catid=".$catid."&areaid=".$areaid);
		}
		
		$userid = trim($s_uid);
		$perpost_money_cost = $mymps_global['cfg_member_perpost_consume'] ? $mymps_global['cfg_member_perpost_consume'] : 0 ;
		$user = $db -> getRow("SELECT qq,email,tel,cname FROM `{$db_mymps}member` WHERE userid = '$userid'");
		$qq = $user['qq'];
		$tel = $user['mobile'];
		$contact_who = $user['cname'];
		/*信息认证情况*/
		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);
		}
	
		$sql = "INSERT INTO `{$db_mymps}information` (title,content,begintime,activetime,endtime,catid,catname,dir_typename,cityid,areaid,streetid,userid,ismember,info_level,qq,email,tel,contact_who,img_count,certify,ip,ip2area) VALUES ('$title','$content','$timestamp','0','0','$catid','$catname','$dir_typename','$cityid','$areaid','$streetid','$userid','1','$info_level','$user[qq]','$user[email]','$tel','$contact_who','$img_count','$certify','$ip','wap')";	
		//金币变化
		if(!empty($perpost_money_cost)){
			$db->query("UPDATE `{$db_mymps}member` SET money_own = money_own - '$perpost_money_cost' WHERE userid = '$userid'");
		}

	}else{
	    //游客发布信息数量限制
		if($mymps_global['cfg_if_nonmember_info'] == 1 && $mymps_global['cfg_nonmember_perday_post'] > 0){
			$count = mymps_count("information","WHERE ip = '$ip' AND begintime > '".mktime(0,0,0)."' AND ismember = '0'");
			$count >= $mymps_global[cfg_nonmember_perday_post] && redirectmsg("很抱歉！游客每天只能发布 <b style='color:red'>".$mymps_global[cfg_nonmember_perday_post]."</b> 条信息<br />如果您要继续操作，请联系客服。","index.php?mod=post&catid=".$catid."&areaid=".$areaid);
		}
		
		$sql = "INSERT INTO `{$db_mymps}information` (title,content,begintime,activetime,endtime,catid,catname,dir_typename,cityid,areaid,streetid,info_level,qq,email,tel,contact_who,img_count,certify,ip,ip2area) VALUES ('$title','$content','$timestamp','0','0','$catid','$catname','$dir_typename','$cityid','$areaid','$streetid','$info_level','$qq','$email','$tel','$contact_who','$img_count','$certify','$ip','wap')";	
	}
	
	$db -> query($sql);
	$id = $db -> insert_id();
	
	$k = $v = NULL;
	if(is_array($extra) && $d['modid'] > 1){
		foreach($extra as $k =>$v){
			$sql1 .= ",`".$k."`";
			$sql2 .= ",'$v'";
		}
		$db->query("INSERT INTO `{$db_mymps}information_{$d[modid]}` (`id`{$sql1})VALUES('$id'{$sql2})");
		$sql1 = $sql2 = NULL;
	}

	//上传图片
	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'");
	}
	
	$msg = $info_level > 0 ? '成功发布一条信息!' : '您的信息审核通过后将显示在网站上!';
	redirectmsg($msg,'index.php?mod=category&catid='.$catid);

} else {
	
	if(!$catid){
		$categories = get_categories_tree(0,'category');
		include mymps_tpl('post_cat');
		exit;
	}
	
	if($catid && !$areaid && $cityid){
		$area_list = $db ->getAll("SELECT * FROM `{$db_mymps}area` WHERE cityid = '$cityid'");
		if(!empty($area_list)){
			include mymps_tpl('post_area');
			exit;
		}
	}
	
	if($areaid && !$streetid && $cityid){
		$street_list = $db ->getAll("SELECT * FROM `{$db_mymps}street` WHERE areaid = '$areaid'");
		if(!empty($street_list)){
			include mymps_tpl('post_street');
			exit;
		}
	}

	
	require_once MYMPS_DATA.'/wap_info.type.inc.php';
	$cat = $db -> getRow("SELECT catid,catname,parentid,modid FROM `{$db_mymps}category` WHERE catid = '$catid'");
	$cat['parentname'] = $db -> getOne("SELECT catname FROM `{$db_mymps}category` WHERE catid = '$cat[parentid]'");
	$cat['parentid'] = $cat['parentid'];
	
	if($cat['parentid'] == 0){
		//如果为根分类
		$categories = get_categories_tree($catid,'category');
		include mymps_tpl('post_cat');
	} elseif($cat['parentid'] > 0){
		//如果为二级分类
		if($mymps_global['cfg_if_nonmember_info'] != 1){
			//游客不能发布信息
			$returnurl = 'index.php?mod=post&cityid='.$cityid.'&areaid='.$areaid.'&streetid='.$streetid;
			$returnurl = urlencode($returnurl);
			redirectmsg("游客不能发布信息！请您登录后再发布！","index.php?mod=login&returnurl=".$returnurl);	
		}
		if($user = $db -> getRow("SELECT qq,email,mobile,cname FROM `{$db_mymps}member` WHERE userid = '$s_uid'")){
			$tel = $user['mobile'];
			$contact_who = $user['cname'];
			$qq = $user['qq'];
		}
		$areaname = $streetid ? $db -> getOne("SELECT streetname FROM `{$db_mymps}street` WHERE streetid = '$streetid'") : $db -> getOne("SELECT areaname FROM `{$db_mymps}area` WHERE areaid = '$areaid'");
		$return_url = 'index.php?mod=post&catid='.$catid.'&areaid='.$areaid.'&cityid='.$cityid;
		$show_mod_option = return_category_info_options($cat['modid']);
		$mixcode = md5($cookiepre);
		include mymps_tpl("post");
	}
	
}

function check_upimage_wap($file="filename")
{
	global $mymps_global;
	$size=$mymps_global['cfg_upimg_size']*1024;
	$upimg_allow = explode(',',$mymps_global['cfg_upimg_type']);
	if($_FILES[$file]['size']>$size){
		redirectmsg('上传文件应小于'.$mymps_global['cfg_upimg_size'].'KB','javascript:history.back()');
	}
	
	if(!in_array(FileExt($_FILES[$file]['name']),$upimg_allow)){
		redirectmsg('系统只允许上传'.$mymps_global['cfg_upimg_type'].'格式的图片！','javascript:history.back()');
	}
	
	if(!preg_match('/^image\//i',$_FILES[$file]['type'])){
		redirectmsg ('很抱歉，系统无法识别您上传的文件的格式，请换一张图片上传！','javascript:history.back()');
	}
	return true;
}

function mymps_check_upimage_wap($file="filename")
{
	if(is_array($_FILES)){
		for($i=0;$i<count($_FILES);$i++){
			if($_FILES[$file.$i]['name']){
				check_upimage_wap($file.$i);
			}
		}
	}
}

function get_upload_image_view_wap($if_upimg = 1 , $infoid = '')
{
	global $mymps_global,$db,$db_mymps;
	if($if_upimg == 1){
		$cfg_upimg_number = $mymps_global[cfg_upimg_number]?$mymps_global[cfg_upimg_number]:'3';
		for($i=0;$i<$cfg_upimg_number;$i++){;
			$mymps .= '<input class="input" style="width:210px;overflow: hidden;padding:5px 0;" type="file" name="mymps_img_'.$i.'" datatype="filter" msg="图片文件格式不正确">';
		}
	}
	return $mymps;
}
?>