<?php
if ( !defined( "SAXUE_CORE_INCLUDE" ) ) {
	include "../../core.php";
} 
$id = intval( $_REQUEST['id'] );
if ( !$id && SAXUE_ANTICODE != '' && SAXUE_ANTICODE != $_REQUEST['anticode'] ) {
	include SAXUE_WEB_PATH . "/404.php";
} 
$catid = intval( $_REQUEST['catid'] );
saxue_getconfigs( 'category', 'info' );
if ( $catid && !isset( $saxueCategory['info'][$catid] ) ) {
	saxue_printfail( "分类不存在" );
} 
$ac = $_REQUEST['ac'] ? $_REQUEST['ac'] : '';
if ( $catid && $saxueCategory['info'][$catid]['child'] ) {
	$ac = '';
} 
$modcate = intval( $_REQUEST['modcate'] );
include_once( SAXUE_WEB_PATH . "/member/header.php" );
if ( $ac == 'post' ) {
	// 配置缓存
	$uip = saxue_userip();
	if ( SAXUE_CHECK_BANIP && empty( $_SESSION['saxueUserId'] ) && saxue_banip( $uip ) ) {
		saxue_printfail( "您的IP被禁止发布信息" );
	} 
	saxue_getconfigs( 'area' );
	saxue_getconfigs( 'configs', 'info' );
	saxue_getconfigs( 'expand', 'info' );
	include SAXUE_ROOT_PATH . "/common/funpost.php"; 
	// 信息模型
	include_once( SAXUE_ROOT_PATH . "/model/info.php" );
	$info_handler = saxueinfohandler :: getinstance( "saxueinfohandler" );
	include SAXUE_ROOT_PATH . "/model/info_data.php";
	$data_handler = saxueinfodatahandler :: getinstance( "saxueinfodatahandler" ); 
	// 信息修改
	$row = array();
	$istop = 0;
	if ( $id ) {
		$info = $info_handler -> get( $id );
		if ( !is_object( $info ) ) {
			saxue_printfail( "信息不存在" );
		} 
		$row = $info -> getvars( 'n' );
		if ( !saxue_checkedit( $row ) ) {
			saxue_printfail( "你没有权限修改此信息" );
		} 
		if ( $row['toptotime'] > SAXUE_NOW_TIME ) {
			$istop = 1;
			$checkright = array( $saxueUsersGroup, 'top' );
		} else {
			$checkright = $saxueUsersGroup;
		} 
	} else {
		$row['tel'] = $_SESSION['saxueUserMobile'];
		$row['qq'] = $_SESSION['saxueUserQQ'];
		$row['weixin'] = $_SESSION['saxueUserWeixin'];
		$row['contacts'] = $_SESSION['saxueUserTruename'];
		include SAXUE_ROOT_PATH . "/model/member_limit.php";
		$limit_handler = saxuememberlimithandler :: getinstance( "saxuememberlimithandler" );
		$limit = $limit_handler -> getlimit( 'infonum', $uip );
		$checkright = $saxueUsersGroup;
	} 
	// 读取权限配置
	$right = array();
	$right['postfee'] = 0;
	$right['istop'] = $istop;
	$right['post'] = saxue_checkright( 'info', 'post', $checkright );
	$right['audit'] = saxue_checkright( 'info', 'audit', $checkright );
	$right['checkcode'] = saxue_checkright( 'info', 'checkcode', $checkright );
	$right['titlecolor'] = saxue_checkright( 'info', 'titlecolor', $checkright );
	$right['password'] = saxue_checkright( 'info', 'password', $checkright );
	$right['expand'] = saxue_checkright( 'info', 'expand', $checkright );
	$right['infodetail'] = saxue_checkright( 'info', 'infodetail', $checkright );
	$right['detaileditor'] = saxue_checkright( 'info', 'detaileditor', $checkright );
	$right['picnum'] = saxue_checkright( 'info', 'picnum', $checkright );
	$right['picsize'] = saxue_checkright( 'info', 'picsize', $checkright );
	$right['istrade'] = saxue_checkright( 'info', 'istrade', $checkright );
	$right['infonum'] = saxue_checkright( 'info', 'infonum', $saxueUsersGroup );
	$right['attachsurl'] = saxue_uploadurl();
	if ( !$id ) {
		$right['authmobile'] = saxue_checkright( 'info', 'authmobile', $saxueUsersGroup );
		$right['postfee'] = saxue_checkright( 'info', 'postfee', $saxueUsersGroup );
	} 
	// 信息提交
	if ( isset( $_POST['dosubmit'] ) ) {
		if ( SAXUE_CHECK_BANMOBILE && saxue_banmobile( $_POST['tel'] ) ) {
			exit( json_encode( array( 'flag' => 0, 'msg' => '您的手机被禁止使用' ) ) );
		}
		$postfee = 0;
		if ( !$id ) {
			// 发布权限检查
			if ( !$right['post'] ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '对不起，您所在会员组没有权限发布信息！' ) ) );
			} 
			if ( $right['authmobile'] && !$_SESSION['saxueUserAuthMobile'] ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '验证手机号码后，才能发布信息！' ) ) );
			} 
			// 发布数量检查
			if ( $right['infonum'] <= $limit['val'] ) {
				if ( !$right['postfee'] ) {
					exit( json_encode( array( 'flag' => 0, 'msg' => '对不起，您的账户每天只能发布 ' . $right['infonum'] . ' 条信息！' ) ) );
				} 
				if ( $right['postfee'] > $_SESSION['saxueUserMoney'] ) {
					exit( json_encode( array( 'flag' => 0, 'msg' => '对不起，您的账户每天只能发布 ' . $right['infonum'] . ' 条信息<br>付费发布信息需要 ' . $right['postfee'] . '元/条，您的账户余额不足！' ) ) );
				} 
				if ( !isset( $_POST['postfee'] ) ) {
					exit( json_encode( array( 'flag' => 0, 'msg' => '发布信息需支付 ' . $right['postfee'] . ' 元，请确认！' ) ) );
				} 
				$postfee = 1;
			} 
			// HASH检查
			if ( empty( $_SESSION['saxueHash'] ) || $_SESSION['saxueHash'] != trim( $_POST['hash'] ) ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '对不起，请勿重复或非法提交信息！' ) ) );
			} 
		} 
		// 交易信息检查
		$_POST['price'] = saxue_dround( trim( $_POST['price'] ) );
		$_POST['freight'] = intval( $_POST['freight'] );
		if ( SAXUE_TRADE && $right['istrade'] ) {
			$_POST['istrade'] = intval( $_POST['istrade'] );
		} else {
			$_POST['istrade'] = 0;
		} 
		if ( $_POST['istrade'] ) {
			if ( !$_SESSION['saxueUserAuthMobile'] ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '手机未认证，不能发布在线交易信息！' ) ) );
			} 
			if ( $_POST['price'] <= 0 ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '在线交易信息，商品价格不能为空！' ) ) );
			} 
			saxue_getconfigs( 'configs', 'trade' );
			if ( $_POST['price'] > saxue_dround( $saxueConfigs['trade']['maxprice'] ) ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '不能发布价格大于 ' . $saxueConfigs['trade']['maxprice'] . ' 的在线交易信息！' ) ) );
			} 
		} 
		// 验证码检查
		if ( $right['checkcode'] && strtolower( $_POST['checkcode'] ) != strtolower( $_SESSION['saxueCheckCode'] ) ) {
			exit( json_encode( array( 'flag' => 0, 'msg' => '对不起，验证码错误！' ) ) );
		} 
		// 地区检查
		$_POST['areaid'] = intval( $_POST['areaid'] );
		if ( !isset( $saxueArea[$_POST['areaid']] ) ) {
			exit( json_encode( array( 'flag' => 0, 'msg' => '对不起，地区错误！' ) ) );
		} 
		// 标题检查
		$_POST['title'] = saxue_htmlstr( trim( $_POST['title'] ) );
		if ( strlen( $_POST['title'] ) == 0 ) {
			exit( json_encode( array( 'flag' => 0, 'msg' => '对不起，标题不能为空！' ) ) );
		} 
		$banmod = false;
		$banword = new saxue_banword();
		if ( SAXUE_CHECK_BANWORD ) {
			$banret = $banword -> check( $_POST['title'] );
			if ( $banret == SAXUE_WORD_BANNED ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '对不起，标题包含禁用词语“' . implode( '，', $banword -> foundword ) . '”' ) ) );
			} 
			if ( $banret == SAXUE_WORD_MOD ) {
				$banmod = true;
			} 
		} 
		// 标题颜色检查
		$_POST['style'] = saxue_htmlstr( trim( $_POST['style'] ) );
		if ( !$right['titlecolor'] ) {
			$_POST['style'] = '';
		} 
		// 详细内容检查
		if ( !$saxueConfigs['info']['usebox'] || $right['infodetail'] ) {
			$_POST['content'] = saxue_safecontent( $_POST['content'] );
			if ( !$saxueConfigs['info']['usebox'] && strlen( strip_tags( $_POST['content'] ) ) == 0 ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '对不起，详细描述不能为空！' ) ) );
			} 
			if ( saxue_checkcontact( $_POST['content'] ) ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '对不起，详细描述不能填写电话、QQ、邮箱等联系方式！' ) ) );
			} 
			if ( SAXUE_CHECK_BANWORD ) {
				$banret = $banword -> check( $_POST['content'] );
				if ( $banret == SAXUE_WORD_BANNED ) {
					exit( json_encode( array( 'flag' => 0, 'msg' => '对不起，详细描述包含禁用词语“' . implode( '，', $banword -> foundword ) . '”' ) ) );
				} 
				if ( $banret == SAXUE_WORD_MOD ) {
					$banmod = true;
				} 
			} 
		} else {
			$_POST['content'] = '';
		} 
		// 单元格内容检查
		if ( $saxueConfigs['info']['usebox'] ) {
			if ( $right['istop'] ) {
				$_POST['topcont'] = $_cont = saxue_safecontent( $_POST['topcont'] );
			} else {
				$_POST['intro'] = $_cont = strip_tags( $_POST['intro'] );
				if ( mb_strlen( $_cont, 'utf8' ) > 255 ) {
					exit( json_encode( array( 'flag' => 0, 'msg' => '对不起，单元格内容不能超过255个字符！' ) ) );
				} 
				if ( saxue_checkcontact( $_cont ) ) {
					exit( json_encode( array( 'flag' => 0, 'msg' => '对不起，单元格内容不能填写电话、QQ、邮箱等联系方式！' ) ) );
				} 
			} 
			if ( strlen( strip_tags( $_cont ) ) == 0 ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '对不起，单元格内容不能为空！' ) ) );
			} 
			if ( SAXUE_CHECK_BANWORD ) {
				$banret = $banword -> check( $_cont );
				if ( $banret == SAXUE_WORD_BANNED ) {
					exit( json_encode( array( 'flag' => 0, 'msg' => '对不起，单元格内容包含禁用词语“' . implode( '，', $banword -> foundword ) . '”' ) ) );
				} 
				if ( $banret == SAXUE_WORD_MOD ) {
					$banmod = true;
				} 
			} 
		} elseif ( !empty( $_POST['content'] ) ) {
			$_POST['intro'] = saxue_substr( strip_tags( $_POST['content'] ), 0, 200 );
		} 
		// 扩展属性检查
		if ( $right['expand'] && isset( $saxueExpand['info'] ) ) {
			foreach ( $saxueExpand['info'] as $k => $v ) {
				if ( $v['catids'] == '' || false !== strpos( $v['catids'], ',' . $catid . ',' ) ) {
					switch ( $v['type'] ) {
						case 10 :
							if ( $v['required'] && ( !is_array( $_POST['expand'][$k] ) || count( $_POST['expand'][$k] ) == 0 ) ) {
								exit( json_encode( array( 'flag' => 0, 'msg' => '对不起，' . $v['title'] . '不能为空！' ) ) );
							} 
							break;
						default :
							$_POST['expand'][$k] = strip_tags( trim( $_POST['expand'][$k] ) );
							if ( $v['required'] && strlen( $_POST['expand'][$k] ) == 0 ) {
								exit( json_encode( array( 'flag' => 0, 'msg' => '对不起，' . $v['title'] . '不能为空！' ) ) );
							} 
							break;
					} 
				} 
			} 
		} 
		if ( empty( $_POST['expand'] ) || !is_array( $_POST['expand'] ) ) {
			$_POST['expand'] = array();
		} 
		$_POST['expand'] = serialize( $_POST['expand'] ); 
		// 图片处理
		$_POST['thumb'] = trim( $_POST['thumb'] );
		$_POST['pics'] = array();
		if ( $right['picnum'] > 0 ) {
			$_POST['pic'] = trim( $_POST['pic'] );
			$_POST['pictag'] = trim( $_POST['pictag'] );
			if ( !empty( $_POST['pic'] ) ) {
				$_pics = explode( '|', $_POST['pic'] );
				$_tags = explode( '|', $_POST['pictag'] );
				foreach ( $_pics as $k => $_pic ) {
					if ( empty( $_POST['thumb'] ) ) $_POST['thumb'] = saxue_uploadurl() . $_pic;
					$_POST['pics'][$k]['url'] = $_pic;
					$_POST['pics'][$k]['tag'] = $_tags[$k];
					$_POST['pics'][$k]['desc'] = strip_tags( trim( $_POST['picdesc' . $k] ) );
				} 
			} 
		} 
		$_POST['picnum'] = count( $_POST['pics'] );
		$_POST['pics'] = serialize( $_POST['pics'] ); 
		// 其他内容处理
		$_POST['uip'] = $uip;
		$_POST['contacts'] = saxue_htmlstr( trim( $_POST['contacts'] ) );
		$_POST['tel'] = saxue_htmlstr( trim( $_POST['tel'] ) );
		$_POST['qq'] = saxue_htmlstr( trim( $_POST['qq'] ) );
		$_POST['weixin'] = saxue_htmlstr( trim( $_POST['weixin'] ) );
		$_POST['password'] = trim( $_POST['password'] );
		$_POST['expired'] = trim( $_POST['expired'] );
		if ( !empty( $_POST['expired'] ) ) {
			$_POST['expired'] = strtotime( $_POST['expired'] . ' 23:59:59' );
		} else {
			$_POST['expired'] = SAXUE_FOREVER;
		} 
		// 数据入库
		if ( $id ) {
			// 信息修改
			if ( $banmod || $row['status'] != 1 ) {
				$_POST['status'] = 0;
			} else {
				$_POST['status'] = $right['audit'];
			} 
			$_POST['url'] = saxue_geturl( 'info_show', $id, $_POST['catid'], $row['posttime'] );
			if ( !empty( $_POST['password'] ) ) {
				$_POST['password'] = md5( $_POST['password'] );
			} else {
				$_POST['password'] = $row['password'];
			} 
			$newinfo = $info_handler -> create( false );
			if ( !$info_handler -> insert( $newinfo ) ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '信息修改失败' ) ) );
			} 
			// 详细内容、图片、扩展
			$data = $data_handler -> create( false );
			$data_handler -> insert( $data );
			unset( $_SESSION['saxueInfopass'] );
		} else {
			// 管理密码检查
			if ( $right['password'] && strlen( $_POST['password'] ) == 0 ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '对不起，管理密码不能为空！' ) ) );
			} 
			if ( !empty( $_POST['password'] ) ) {
				$_POST['password'] = md5( $_POST['password'] );
			} 
			// 新信息
			$score = intval( $saxueConfigs['info']['postscore'] );
			if ( $right['audit'] && $_POST['uid'] > 0 && $score > 0 ) {
				$_POST['reward'] = 1;
			} 
			if ( $banmod ) {
				$_POST['status'] = 0;
			} else {
				$_POST['status'] = $right['audit'];
			} 
			$_POST['posttime'] = SAXUE_NOW_TIME;
			$_POST['updatetime'] = SAXUE_NOW_TIME;
			$_POST['uid'] = intval( $_SESSION['saxueUserId'] );
			if ( SAXUE_IN_MOBILE ) {
				$_POST['fromdevice'] = 2;
			} else {
				$_POST['fromdevice'] = 1;
			}
			$newinfo = $info_handler -> create();
			if ( !$info_handler -> insert( $newinfo ) ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '信息保存失败' ) ) );
			} 
			$id = $newinfo -> getvar( 'id' ); 
			// 付费发布
			include SAXUE_ROOT_PATH . "/model/member_users.php";
			$users_handler = saxuememberusershandler :: getinstance( "saxuememberusershandler" );
			if ( $postfee && !$users_handler -> changemoney( $_POST['uid'], -$right['postfee'], 33, $id ) ) {
				$info_handler -> delete( $id );
				exit( json_encode( array( 'flag' => 0, 'msg' => '扣费失败，请检查账户余额' ) ) );
			} 
			// 信息URL
			$up = array();
			$up['url'] = saxue_geturl( 'info_show', $id, $_POST['catid'], SAXUE_NOW_TIME );
			$info_handler -> updatefields( $up, 'id=' . $id ); 
			// 详细内容、图片、扩展
			$data = $data_handler -> create();
			$data -> setvar( 'id', $id );
			$data_handler -> insert( $data ); 
			// 发布信息奖励积分
			if ( $_POST['status'] && $_POST['uid'] > 0 && $score > 0 ) {
				$users_handler -> changescore( $_POST['uid'], $score, 21, $id );
			} 
			// 更新信息发布数
			++$limit['val'];
			$limit_handler -> setlimit( $limit['lid'], $limit['val'], $uip );
			if ( $_POST['uid'] && $_POST['status'] ) {
				$users_handler -> updatefields( 'posts=posts+1', 'uid=' . $_POST['uid'] );
			} 
			unset( $_SESSION['saxueHash'] );
			if ( $_POST['status'] == 1 ) {
				$saxuepluginmanager -> trigger( 'postInfoEnd', array( 'id' => $id, 'catid' => $_POST['catid'], 'title' => $_POST['title'] ) );
			} 
		} 
		++$_POST['status'];
		exit( json_encode( array( 'flag' => $_POST['status'], 'id' => $id ) ) );
	} 
	// 读取信息
	if ( $id ) {
		// 分类路径
		if ( !$catid ) {
			$catid = $row['catid'];
		} 
		if ( $row['expired'] < SAXUE_FOREVER ) {
			$row['expired'] = date( 'Y-m-d', $row['expired'] );
		} else {
			$row['expired'] = '';
		} 
		$data = $data_handler -> get( $id );
		$row['content'] = $data -> getvar( 'content', 'n' );
		$row['topcont'] = $data -> getvar( 'topcont', 'n' );
		if ( $istop && empty( $row['topcont'] ) ) $row['topcont'] = nl2br( $row['intro'] ) . '<br>电话：' . $row['tel'];
		$row['pics'] = $data -> getvar( 'pics', 'n' );
		$row['pics'] = unserialize( $row['pics'] );
		$row['expand'] = $data -> getvar( 'expand', 'n' );
		$row['expand'] = unserialize( $row['expand'] );
	} 
	// 读取扩展属性
	if ( $right['expand'] && isset( $saxueExpand['info'] ) ) {
		$expand = array();
		foreach ( $saxueExpand['info'] as $k => $v ) {
			if ( $v['catids'] == '' || false !== strpos( $v['catids'], ',' . $catid . ',' ) ) {
				$expand[$k] = $v;
				if ( $id ) $expand[$k]['value'] = $row['expand'][$k];
				else $expand[$k]['value'] = '';
			} 
		} 
		if ( count( $expand ) > 0 ) {
			$saxueTpl -> assign( "expand", $expand );
		} else {
			$right['expand'] = 0;
		} 
	} else {
		$right['expand'] = 0;
	} 
	// 读取置顶属性
	if ( $right['istop'] ) {
		include_once( SAXUE_ROOT_PATH . "/model/info_top.php" );
		$top_handler = saxueinfotophandler :: getinstance( "saxueinfotophandler" );
		$top = $top_handler -> get( $id, 'id' );
		$saxueTpl -> assign( "top", $top -> getvars( 'n' ) );
	} else {
		$top['cells'] = 0;
		$saxueTpl -> assign( "top", $top );
	} 
	if ( false === strpos( $saxueConfigs['info']['boxadd'], 'contacts' ) ) {
		$saxueConfigs['info']['boxadd_contacts'] = 0;
	} else {
		$saxueConfigs['info']['boxadd_contacts'] = 1;
	} 
	if ( false === strpos( $saxueConfigs['info']['boxadd'], 'tel' ) || 0 < $saxueConfigs['info']['showtel'] ) {
		$saxueConfigs['info']['boxadd_tel'] = 0;
	} else {
		$saxueConfigs['info']['boxadd_tel'] = 1;
	} 
	if ( false === strpos( $saxueConfigs['info']['boxadd'], 'qq' ) ) {
		$saxueConfigs['info']['boxadd_qq'] = 0;
	} else {
		$saxueConfigs['info']['boxadd_qq'] = 1;
	} 
	$arrpid = array();
	$saxueCategory['info'][$catid]['pid'] && $arrpid[] = $saxueCategory['info'][$catid]['pid'];
	$arrpid[] = $catid;
	$saxueTpl -> assign( "id", $id );
	$saxueTpl -> assign( "catid", $catid );
	$saxueTpl -> assign( "check", $_SESSION );
	$saxueTpl -> assign( "row", $row );
	$saxueTpl -> assign( "right", $right );
	$saxueTpl -> assign( "limit", $limit );
	$saxueTpl -> assign( "area", $saxueArea );
	$saxueTpl -> assign( "arrpid", array_flip( $arrpid ) );
	$saxueTpl -> assign( "category", $saxueCategory['info'] );
	$saxueTpl -> assign( "config", $saxueConfigs['info'] );
	if ( SAXUE_TRADE && $right['istrade'] ) {
		saxue_getconfigs( 'configs', 'trade' );
		$saxueTpl -> assign( "tradefee", $saxueConfigs['trade']['fee'] );
	}
	$saxueTset['saxue_page_template'] = SAXUE_THEME_PATH . '/member/info/post.html';
} else {
	$_SESSION['saxueHash'] = md5( SAXUE_NOW_TIME );
	$saxueTpl -> assign( "id", $id );
	$saxueTpl -> assign( "catid", $catid );
	$saxueTpl -> assign( "category", $saxueCategory['info'] );
	$saxueTset['saxue_page_template'] = SAXUE_THEME_PATH . '/member/info/post_category.html';
} 
include_once( SAXUE_WEB_PATH . "/member/footer.php" );
