<?php
if (!isset($_SESSION)) {
 	session_start();
	session_regenerate_id(TRUE);
}
global $ashu_option;
if ( is_user_logged_in() || ($ashu_option['user']['allow_gust_post']=='on')) {

if ( isset( $_POST['user_post_new_submit'] ) ) {
	if( empty($_POST['token']) || empty($_SESSION['token']) || ($_POST['token'] =! $_SESSION['token'] )){
		/*if(!isset($_SESSION['token'])){
			$notice='无session';
		}elseif($_SESSION['token']==''){
			$notice='session为空';
		}elseif($_POST['token'] != $_SESSION['token']){
			$notice= $_POST['token'].'<br />'.$_SESSION['token'];
		}*/
		$notice='请不要重复刷新！';
		unset($_SESSION['token']);
	}else{
	//如果提交了文章，检查密匙
	$nonce = $_REQUEST['_wpnonce'];
	if ( !wp_verify_nonce( $nonce, 'user-add-post' ) ) {
		wp_die( '大侠,请高抬贵手...' );
	}
	global $userdata;//获取用户数据
        $errors = array();
        $title = trim( $_POST['user_post_title'] ); //标题
        $buy_link = trim( $_POST['_buy_link'] ); //标题
        $content = trim( $_POST['user_post_content'] ); //内容


        //判断标题是否有效
        if ( empty( $title ) ) {
            $errors[] = '标题不能为空';
        } else {
            $title = trim( strip_tags( $title ) );
        }
		
		//判断标题是否有效
        if ( empty( $buy_link ) ) {
            $errors[] = '购买链接不能为空';
        } else {
            $buy_link = trim( strip_tags( $buy_link ) );
        }

        //判断内容是否有效
        if ( empty( $content ) ) {
            $errors[] = '内容不能为空';
        } else {
            $content = trim( $content );
        }

        //文章类型
        $post_type = 'detail';

        //输出错误信息
        if ( $errors ) {
            echo user_error_msg( $errors );
            return;
        }

        $post_stat = 'draft'; //设置选项-发布文章的默认状态
		//作者
		if(is_user_logged_in()){
			$post_author = $userdata->ID;
		}elseif( $ashu_option['user']['allow_gust_post'] ){
			$post_author=1;
		}

        $my_post = array(
            'post_title' => $title,
            'post_content' => $content,
            'post_status' => $post_stat,
            'post_author' => $post_author,
            'post_type' => $post_type,
        );
		//插入文章
        $post_id = wp_insert_post( $my_post );

        if ( $post_id ) { //插入成功

            //设置选项-发邮件通知管理员
            if ( $ashu_option['user']['post_notification'] == 'yes' ) {
                user_notify_post_mail( $userdata, $post_id );
            }

            
			if ( $buy_link ) {
				add_post_meta( $post_id, '_buy_link', $buy_link, true );
            }
			

			if ( $post_id ) {
                /*$redirect = apply_filters( 'user_after_post_redirect', get_permalink( $post_id ), $post_id );
				
                wp_redirect( $redirect );
                exit;
				*/
				$notice = '投稿成功！';
				if(is_user_logged_in()){
					$notice .= '<a href="'.smzdm_my_account_link().'">查看已发布文章</a>';
				}
            }
        }
	}
}
}
?>
<?php
$token = md5(uniqid(rand(), true));
$_SESSION['token'] = $token;
get_header();?>
<section>
<div class="container">
<div class="left_main" id="left_side">
	<div class="content_out">
	<div class="content">
	<div class="perContentBox">
	<div class="contentBg">
		<div class="con_title">
			<span class="conName">投稿</span>
			<div class="clear"></div>
		</div>
		<div class="showLeft">
		<div class="conBox" id="content_420">
		<?php
		if ( is_user_logged_in() || ($ashu_option['user']['allow_gust_post']=='on')) {
		if( $ashu_option['user']['allow_post'] == 'on' ){
		
		global $userdata,$ashu_option;
        $userdata = get_user_by( 'id', $userdata->ID ); //获取用户数据

        $info = __( "Post It!", 'user' );

		$can_post = 'yes';
        $info = apply_filters( 'user_addpost_notice', $info ); //过滤钩子、判断用户是否被锁定

        $title = '';//默认标题
        $description = '';//默认内容
		if(isset($notice)){
			echo '<div class="notice">'.$notice.'</div>';
		}
        if ( $can_post == 'yes' ) {
            ?>
            <div id="user-post-area">
                <form id="user_new_post_form" name="user_new_post_form" action="" enctype="multipart/form-data" method="POST">
                    <?php wp_nonce_field( 'user-add-post' ); //启用安全密匙 ?>

                    <ul class="user-post-form">
                        <li>
                            <label for="new-post-title">
                               文章标题<span class="required">*</span>
                            </label>
                            <input class="requiredField" type="text" value="<?php echo $title; ?>" name="user_post_title" id="new-post-title" minlength="2"/>
                            <div class="clear"></div>
                        </li>
						<li>
                            <label for="_buy_link">
                               购买地址<span class="required">*</span>
                            </label>
                            <input class="requiredField" type="text" value="<?php echo $title; ?>" name="_buy_link" id="_buy_link" minlength="2"/>
                            <div class="clear"></div>
                        </li>
                        <li>
                            <label for="user_post_content">
                               产品描述<span class="required">*</span>
                            </label>
                                <textarea name="user_post_content" class="requiredField" id="user_post_content" cols="60" rows="8"><?php echo esc_textarea( $description ); ?></textarea>
                            <div class="clear"></div>
                        </li>
                        <li>
							<label>&nbsp;</label>
							<input class="user_submit" type="submit" name="user_new_post_submit" value="提交">
							<input type="hidden" name="user_post_type" value="<?php echo $post_type; ?>" />
							<input type="hidden" name="user_post_new_submit" value="yes" />
							<input type="hidden" name="token" value="<?php echo $token; ?>" />
                        </li>
                    </ul>
                </form>
            </div>
				<?php } 
			}else{
				echo '对不起，投稿功能已经关闭';
			}
			} else {
					printf( "对不起没有查看权限，请先 %s 再试.", wp_loginout( get_permalink(), false ) );
				}
				?>
			</div>
		</div>
		<div class="clear"></div>
	</div>
	</div>
	<div class="clear"></div>
	</div>
	</div>
</div>
<?php get_sidebar();?>
<div class="clear"></div>
</div>
</section>
<?php get_footer('post');?>