<?php
if(!defined('IN_MYMPS')) exit('FORBIDDEN');

$catid = isset($catid) ? intval($catid) : '';

if($action == 'input'){

	/*如果为分类选择页*/
	if(!$catid){

		$loc 		= get_location('post','','选择分类 - 发布分类信息');
		$page_title = $loc['page_title'];
		tpl_assign();
		$smarty -> assign('categories',get_categories_tree(0,'category'));
		$smarty -> display(mymps_tpl("info_post"));
		
	}else{
		
		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,$ip2area,$address,$ipdata,$allow_post_areas,$i);
		}
		
		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.'%';
					write_msg("您当前的IP <b style='color:red'>".$ip."</b> 已被管理员加入黑名单，不允许发布信息！");
					exit;
				}
			}
		}
		
		$cat = post_cat_info($catid);
		if($cat['parentid'] == 0) {
			$loc 		= get_location('post','','选择分类 - 发布'.$cat[catname].'信息');
			$page_title = $loc['page_title'];
			tpl_assign();
			$smarty -> assign('categories',get_categories_tree($catid,'category'));
			$smarty -> display(mymps_tpl("info_post"),'input');
			exit;
		}elseif($db->getOne("SELECT COUNT(catid) FROM `{$db_mymps}category` WHERE parentid = '$catid'")){
			//如果为最底层分类
			$cat_option = $db->getAll("SELECT catid,catname FROM `{$db_mymps}category` WHERE parentid = '$catid' ORDER BY catorder ASC");
			$smarty -> assign('cat_option',$cat_option);
		}
		
		require_once MYMPS_DATA."/info_lasttime.php";
		require_once MYMPS_DATA."/info.type.inc.php";
		require_once MYMPS_INC."/member.class.php";
		if($log = $member_log->chk_in()) chk_member_purview("purview_info");
		
		if($mymps_global['cfg_post_editor'] == 1){
			$acontent 	= get_editor('content','information','','400px','300px','include/kindeditor');
		} else {
			$acontent = "<textarea name=\"content\" style=\"width:400px;height:300px;\" class=\"input\" require=\"true\" datatype=\"limit\" msg=\"请填写信息内容描述\"></textarea>";
		}
		
		if($log){
			//判断金额是否足够
			$memberinfo			= $member_log -> get_info();
			$his_money 			= $memberinfo['money_own'];
			($his_money - $mymps_global['cfg_member_perpost_consume']) < 0 && write_msg('您的用户余额 <font color=red><b>'.$his_money.'</b></font>过低 不能再发布信息，请联系管理员充值');
			$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 && write_msg("很抱歉！您当前的会员级别每天只能发布".$perday_maxpost."条信息");
			}
			$onload 				= '';
			$areaid 				= $memberinfo['areaid'];
			$post['tel'] 			= empty($memberinfo['tel']) ? '' : $memberinfo['tel'];
			$post['qq'] 			= empty($memberinfo['qq']) ? '' : $memberinfo['qq'];
			$post['email'] 			= $memberinfo['email'];
			$post['userid'] 		= $memberinfo['userid'];
			$post['contact_who']  	= $memberinfo['cname'];
			$post['ismember'] 		= 1;
			$post['manage_pwd'] 	= '';
			$post['imgcode']= $authcodesettings['memberpost'] == 1 ? 1 : '';
			
		}else{
			if(!empty($mymps_global['cfg_nonmember_perday_post'])){
				$count = mymps_count("information","WHERE ip = '$ip' AND begintime > '".mktime(0,0,0)."' AND ismember = '0'");
				$count >= $mymps_global[cfg_nonmember_perday_post] && write_msg("很抱歉！游客每天只能发布".$mymps_global[cfg_nonmember_perday_post]."条信息");
			}
			
			$mymps_global['cfg_if_nonmember_info']=='0' && write_msg("对不起，您还没有登录！请您登录后再发布信息！",$mymps_global['cfg_member_logfile']."?url=".urlencode(getUrl()));
			
			$onload = ($mymps_global['cfg_if_nonmember_info_box'] == 1)?"javascript:setbg('建议您登陆后再进行此次操作',450,70,'box.php?part=memberinfopost&url=".urlencode(urlencode(getUrl()))."')":"";
			
			$post['manage_pwd'] =  1;
			$post['ismember'] = 0;
			$post['imgcode']= $authcodesettings['post'] == 1 ? 1 : '';
			//write_msg('请您登录后再发布信息！','login.php?url='.urlencode(GetUrl()));
		}
			
		$post['mymps_extra_value'] 	 = return_category_info_options($cat['modid']);
		$post['mymps_extra_value']   = is_array($post['mymps_extra_value']) ? $post['mymps_extra_value'] : array();
		$post['upload_img'] 		 = $cat['if_upimg'] == 1 ? get_upload_image_view(1,$id) : '';
		$post['GetInfoLastTime']	 = GetInfoLastTime();
		$post['action']		 	  	 ="input";
		$post['submit']		  	 	 = "提交发布";
		$post['ip']				 	 = $ip;
		$post['catid']			  	 = $catid;
		$post['mixcode']			 = md5($cookiepre);
		$loc 						 = get_location('post','','填写内容 - 发布分类信息');
		$page_title 				 = $loc['page_title'];
		
		tpl_assign();
		$smarty->assign('cat',$cat);
		$smarty->assign('area_option',cat_list('area'));
		$smarty->assign('post',$post);
		$smarty->assign('onload',$onload);
		$smarty->assign('acontent',$acontent);
		
		/*验证回答设置*/
		$whenpost = '';
		$whenpost = $db -> getOne("SELECT value FROM `{$db_mymps}config` WHERE description = 'whenpost' AND type = 'checkanswe'");
		if($whenpost == '1' && $checkanswer = read_static_cache('checkanswer')){
			$checkquestion['id']		= $randid = array_rand($checkanswer,1);
			$checkquestion['question']  = $checkanswer[$randid]['question'];
			$checkquestion['answer']	= $checkanswer[$randid]['answer'];
			$smarty -> assign('checkquestion',$checkquestion);
		}

		$smarty -> display(mymps_tpl("info_post_write"));
	}

} elseif ($action == 'edit') {

	require_once MYMPS_DATA."/info_lasttime.php";
	require_once MYMPS_DATA."/info.type.inc.php";
	require_once MYMPS_INC."/member.class.php";
	
	$id 	= intval($id);
	if(!$post = is_member_info($id)) write_msg('操作失败！你所指定的信息不存在或者已被删除！');
	$catid 	= $post['catid'];
	$areaid = $post['areaid'];
	$cat = $db->getRow("SELECT a.if_upimg,a.modid,b.catid FROM `{$db_mymps}category` AS a LEFT JOIN `{$db_mymps}category` AS b ON a.parentid = b.catid WHERE a.catid = '$catid'");
	
	if($post['ismember'] == 1){
		if(!$log = $member_log -> chk_in()){
			write_msg('',$mymps_global['SiteUrl'].'/'.$mymps_global['cfg_member_logfile'].'?url='.urlencode($mymps_global['cfg_postfile'].'?action=edit&id='.$id));
		}elseif($log && $s_uid != $post['userid']){
			write_msg('操作失败！该信息不是您发布的！','olmsg');
		}
		$nav_bar 		= '<a href="info.php?id='.$id.'">'.$post['title'].'</a> &raquo; 修改信息';
	}elseif($post[ismember] == 0 &&!empty($manage_pwd)){
		if(mymps_count("information","WHERE id = '$id' AND manage_pwd = '".md5($manage_pwd)."' AND ismember = 0") == 0){
			write_msg("操作失败！您输入的管理密码不正确！");
		}
		$post['manage_pwd']= "<tr><td class=\"tdr\">管理密码：</td><td><input type=\"password\" name=\"manage_pwd\" class=\"text\"/>
如不修改，请留空</td></tr>";
		$post[ismember] = 0;
		$nav_bar 		= '<a href="info.php?id='.$id.'">'.$post['title'].'</a> &raquo; <a href="../member/info.php?part=edit&id='.$id.'">输入管理密码</a> &raquo; 修改信息';
	}elseif($post[ismember] == 0 && empty($manage_pwd)){
		write_pwd_smarty("修改");
		exit;
	}
	
	if($mymps_global['cfg_post_editor'] == 1){
		$acontent 	= get_editor('content','information',$post[content],'400px','300px','include/kindeditor');
	} else {
		$acontent = "<textarea name=\"content\" style='width:400px;height:300px;'>".de_textarea_post_change($post[content])."</textarea>";
	}

	$page_title 	  = "修改信息内容 - ".$post['title'];
	$post['GetInfoLastTime']	 = GetInfoLastTime($post['activetime']);
	$post['mymps_extra_value'] 	 = return_category_info_options($cat['modid'],$id);
	$post['upload_img'] 		 = get_upload_image_edit($cat['if_upimg'],$id,'yes');
	$post['action']				 = "edit";
	$post['submit'] 			 = "保存修改";
	$post['mixcode']			 = md5($cookiepre);
	$cat 					 	 = post_cat_info($catid);
	
	tpl_assign();
	$smarty->assign('cat',$cat);
	$smarty->assign('area_option',cat_list('area',0,$areaid));
	$smarty->assign('post',$post);
	$smarty->assign('onload',$onload);
	$smarty->assign('acontent',$acontent);
	$smarty->display(mymps_tpl("info_post_write"));
	
} elseif($action == 'ok'){

	$ok['id']	   	= intval($id);
	$ok['catid']	= intval($catid);
	$ok['filepath'] = trim(mhtmlspecialchars($filepath));
	$ok['title']	= trim(mhtmlspecialchars($title)); 
	$ok['level']	= intval($level);
	$ok['content']  = ($level==0) ? "":
	$ok['info_uri'] = Rewrite('info',array('id'=>$ok['id'],'catid'=>$ok['catid'],'html_path'=>$ok['filepath']));
	
	if(!$title || !$id) exit('Access Denied!');
	
	tpl_assign();
	$smarty->assign('nav_bar','信息发布状态提示');
	$smarty->assign('ok',$ok);
	$smarty->display(mymps_tpl("info_post_write_ok"));
	
}

function post_cat_info($catid){
	return $GLOBALS['db'] -> getRow("SELECT a.catid,a.modid,a.if_upimg,a.catname,b.catid as parentid,a.if_mappoint,b.catname as parentname FROM `{$GLOBALS['db_mymps']}category` AS a LEFT JOIN `{$GLOBALS['db_mymps']}category` AS b ON a.parentid = b.catid WHERE a.catid = '$catid'");
}
?>