<?php
$vercode=''; //此处取值请自行修改
if(isset($_POST['vercode'])){
if ($_POST['vercode']!=$vercode){
 echo("[err]invalid vercode[/err]");
 exit();
}
}

//require_once('admin.php');
//以下为替换admin.php代码

if ( ! defined('WP_ADMIN') )
	define('WP_ADMIN', TRUE);

if ( ! defined('WP_NETWORK_ADMIN') )
	define('WP_NETWORK_ADMIN', FALSE);

if ( ! defined('WP_USER_ADMIN') )
	define('WP_USER_ADMIN', FALSE);

if ( ! WP_NETWORK_ADMIN && ! WP_USER_ADMIN ) {
	define('WP_BLOG_ADMIN', TRUE);
}

if ( isset($_GET['import']) && !defined('WP_LOAD_IMPORTERS') )
	define('WP_LOAD_IMPORTERS', true);

require_once(dirname(dirname(__FILE__)) . '/wp-load.php');

//nocache_headers();

//if ( get_option('db_upgraded') ) {
//	flush_rewrite_rules();
// update_option( 'db_upgraded',  false );
//
// /**
//  * Runs on the next page load after successful upgrade
//  *
//  * @since 2.8
//  */
// do_action('after_db_upgrade');
//} else
if ( get_option('db_version') != $wp_db_version && empty($_POST) ) {
	if ( !is_multisite() ) {
		//wp_redirect(admin_url('upgrade.php?_wp_http_referer=' . urlencode(stripslashes($_SERVER['REQUEST_URI']))));
		echo('[err]db_version error[/err]');
		exit;
//	} elseif ( apply_filters( 'do_mu_upgrade', true ) ) {
//		/**
//		 * On really small MU installs run the upgrader every time,
//		 * else run it less often to reduce load.
//		 *
//		 * @since 2.8.4b
//		 */
//		$c = get_blog_count();
//		// If 50 or fewer sites, run every time. Else, run "about ten percent" of the time. Shh, don't check that math.
//		if ( $c <= 50 || ( $c > 50 && mt_rand( 0, (int)( $c / 50 ) ) == 1 ) ) {
//			require_once( ABSPATH . WPINC . '/http.php' );
//			$response = wp_remote_get( admin_url( 'upgrade.php?step=1' ), array( 'timeout' => 120, 'httpversion' => '1.1' ) );
//			do_action( 'after_mu_upgrade', $response );
//			unset($response);
//		}
//		unset($c);
	}
}

require_once(ABSPATH . 'wp-admin/includes/admin.php');

//ET增加------
$etuser = wp_signon();
if (is_wp_error($etuser))
{
 echo('login error');
 exit;
}
$_POST['user_ID']=$etuser->ID;


/*
auth_redirect();

// Schedule trash collection
if ( !wp_next_scheduled('wp_scheduled_delete') && !defined('WP_INSTALLING') )
	wp_schedule_event(time(), 'daily', 'wp_scheduled_delete');

set_screen_options();

$date_format = get_option('date_format');
$time_format = get_option('time_format');

wp_reset_vars(array('profile', 'redirect', 'redirect_url', 'a', 'text', 'trackback', 'pingback'));

wp_enqueue_script( 'common' );

$editing = false;

if ( isset($_GET['page']) ) {
	$plugin_page = stripslashes($_GET['page']);
	$plugin_page = plugin_basename($plugin_page);
}

if ( isset( $_REQUEST['post_type'] ) && post_type_exists( $_REQUEST['post_type'] ) )
	$typenow = $_REQUEST['post_type'];
else
	$typenow = '';

if ( isset( $_REQUEST['taxonomy'] ) && taxonomy_exists( $_REQUEST['taxonomy'] ) )
	$taxnow = $_REQUEST['taxonomy'];
else
	$taxnow = '';

if ( WP_NETWORK_ADMIN )
	require(ABSPATH . 'wp-admin/network/menu.php');
elseif ( WP_USER_ADMIN )
	require(ABSPATH . 'wp-admin/user/menu.php');
else
	require(ABSPATH . 'wp-admin/menu.php');

if ( current_user_can( 'manage_options' ) )
	@ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );

do_action('admin_init');

if ( isset($plugin_page) ) {
	if ( !empty($typenow) )
		$the_parent = $pagenow . '?post_type=' . $typenow;
	else
		$the_parent = $pagenow;
	if ( ! $page_hook = get_plugin_page_hook($plugin_page, $the_parent) ) {
		$page_hook = get_plugin_page_hook($plugin_page, $plugin_page);
		// backwards compatibility for plugins using add_management_page
		if ( empty( $page_hook ) && 'edit.php' == $pagenow && '' != get_plugin_page_hook($plugin_page, 'tools.php') ) {
			// There could be plugin specific params on the URL, so we need the whole query string
			if ( !empty($_SERVER[ 'QUERY_STRING' ]) )
				$query_string = $_SERVER[ 'QUERY_STRING' ];
			else
				$query_string = 'page=' . $plugin_page;
			wp_redirect( admin_url('tools.php?' . $query_string) );
			exit;
		}
	}
	unset($the_parent);
}

$hook_suffix = '';
if ( isset($page_hook) )
	$hook_suffix = $page_hook;
else if ( isset($plugin_page) )
	$hook_suffix = $plugin_page;
else if ( isset($pagenow) )
	$hook_suffix = $pagenow;

set_current_screen();

// Handle plugin admin pages.
if ( isset($plugin_page) ) {
	if ( $page_hook ) {
		do_action('load-' . $page_hook);
		if (! isset($_GET['noheader']))
			require_once(ABSPATH . 'wp-admin/admin-header.php');

		do_action($page_hook);
	} else {
		if ( validate_file($plugin_page) )
			wp_die(__('Invalid plugin page'));

		if ( !( file_exists(WP_PLUGIN_DIR . "/$plugin_page") && is_file(WP_PLUGIN_DIR . "/$plugin_page") ) && !( file_exists(WPMU_PLUGIN_DIR . "/$plugin_page") && is_file(WPMU_PLUGIN_DIR . "/$plugin_page") ) )
			wp_die(sprintf(__('Cannot load %s.'), htmlentities($plugin_page)));

  do_action('load-' . $plugin_page);

  if (! isset($_GET['noheader']))
   require_once(ABSPATH . 'wp-admin/admin-header.php');

		if ( file_exists(WPMU_PLUGIN_DIR . "/$plugin_page") )
			include(WPMU_PLUGIN_DIR . "/$plugin_page");
		else
			include(WP_PLUGIN_DIR . "/$plugin_page");
	}

 include(ABSPATH . 'wp-admin/admin-footer.php');

 exit();
} else if (isset($_GET['import'])) {

 $importer = $_GET['import'];

 if ( ! current_user_can('import') )
  wp_die(__('You are not allowed to import.'));

	if ( validate_file($importer) ) {
		wp_redirect( admin_url( 'import.php?invalid=' . $importer ) );
		exit;
	}

	if ( ! isset($wp_importers[$importer]) || ! is_callable($wp_importers[$importer][2]) ) {
		wp_redirect( admin_url( 'import.php?invalid=' . $importer ) );
		exit;
	}

	do_action( 'load-importer-' . $importer );

	$parent_file = 'tools.php';
	$submenu_file = 'import.php';
	$title = __('Import');

 if (! isset($_GET['noheader']))
  require_once(ABSPATH . 'wp-admin/admin-header.php');

 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

 define('WP_IMPORTING', true);

	if ( apply_filters( 'force_filtered_html_on_import', false ) )
		kses_init_filters();  // Always filter imported data with kses on multisite.

	call_user_func($wp_importers[$importer][2]);

 include(ABSPATH . 'wp-admin/admin-footer.php');

	// Make sure rules are flushed
	flush_rewrite_rules(false);

	exit();
} else {
	do_action("load-$pagenow");
	// Backwards compatibility with old load-page-new.php, load-page.php,
	// and load-categories.php actions.
	if ( $typenow == 'page' ) {
		if ( $pagenow == 'post-new.php' )
			do_action( 'load-page-new.php' );
		elseif ( $pagenow == 'post.php' )
			do_action( 'load-page.php' );
	}  elseif ( $pagenow == 'edit-tags.php' ) {
		if ( $taxnow == 'category' )
			do_action( 'load-categories.php' );
		elseif ( $taxnow == 'link_category' )
			do_action( 'load-edit-link-categories.php' );
	}
}

if ( !empty($_REQUEST['action']) )
	do_action('admin_action_' . $_REQUEST['action']);

*/
//admin.php结束------------------

function et_wp_translate_postdata( $update = false, $post_data = null ) {

 if ( empty($post_data) )
  $post_data = &$_POST;

 if ( $update )
  $post_data['ID'] = (int) $post_data['post_ID'];
	$ptype = get_post_type_object( $post_data['post_type'] );

/*	if ( $update && ! current_user_can( $ptype->cap->edit_post, $post_data['ID'] ) ) {
		if ( 'page' == $post_data['post_type'] )
			return new WP_Error( 'edit_others_pages', __( 'You are not allowed to edit pages as this user.' ) );
		else
			return new WP_Error( 'edit_others_posts', __( 'You are not allowed to edit posts as this user.' ) );
	} elseif ( ! $update && ! current_user_can( $ptype->cap->create_posts ) ) {
		if ( 'page' == $post_data['post_type'] )
			return new WP_Error( 'edit_others_pages', __( 'You are not allowed to create pages as this user.' ) );
		else
			return new WP_Error( 'edit_others_posts', __( 'You are not allowed to create posts as this user.' ) );
	}
*/
	if ( isset( $post_data['content'] ) )
		$post_data['post_content'] = $post_data['content'];

	if ( isset( $post_data['excerpt'] ) )
		$post_data['post_excerpt'] = $post_data['excerpt'];

	if ( isset( $post_data['parent_id'] ) )
		$post_data['post_parent'] = (int) $post_data['parent_id'];
 if ( isset($post_data['trackback_url']) )
  $post_data['to_ping'] = $post_data['trackback_url'];
/*	if ( !isset($post_data['user_ID']) )
		$post_data['user_ID'] = $GLOBALS['user_ID'];
*/
 if (!empty ( $post_data['post_author_override'] ) ) {
  $post_data['post_author'] = (int) $post_data['post_author_override'];
 } else {
  if (!empty ( $post_data['post_author'] ) ) {
   $post_data['post_author'] = (int) $post_data['post_author'];
  } else {
   $post_data['post_author'] = (int) $post_data['user_ID'];
  }
 }

/*	if ( ! $update && isset( $post_data['user_ID'] ) && ( $post_data['post_author'] != $post_data['user_ID'] )
		 && ! current_user_can( $ptype->cap->edit_others_posts ) ) {

		if ( 'page' == $post_data['post_type'] )
			return new WP_Error( 'edit_others_pages', __( 'You are not allowed to create pages as this user.' ) );
		else
			return new WP_Error( 'edit_others_posts', __( 'You are not allowed to create posts as this user.' ) );
	}
*/
 // What to do based on which button they pressed
// if ( isset($post_data['saveasdraft']) && '' != $post_data['saveasdraft'] )
//  $post_data['post_status'] = 'draft';
// if ( isset($post_data['saveasprivate']) && '' != $post_data['saveasprivate'] )
//  $post_data['post_status'] = 'private';
// if ( isset($post_data['publish']) && ( '' != $post_data['publish'] ) && ( !isset($post_data['post_status']) || $post_data['post_status'] != 'private' ) )
//  $post_data['post_status'] = 'publish';
// if ( isset($post_data['advanced']) && '' != $post_data['advanced'] )
//  $post_data['post_status'] = 'draft';
// if ( isset($post_data['pending']) && '' != $post_data['pending'] )
//  $post_data['post_status'] = 'pending';

 if ( isset( $post_data['ID'] ) )
  $post_id = $post_data['ID'];
 else
  $post_id = false;
 $previous_status = $post_id ? get_post_field( 'post_status', $post_id ) : false;
 
 // Posts 'submitted for approval' present are submitted to $_POST the same as if they were being published.
 // Change status from 'publish' to 'pending' if user lacks permissions to publish or to resave published posts.
// if ( isset($post_data['post_status']) && ('publish' == $post_data['post_status'] && !current_user_can( $ptype->cap->publish_posts )) )
//  if ( $previous_status != 'publish' || !current_user_can( 'edit_post', $post_id ) )
//   $post_data['post_status'] = 'pending';

 if ( ! isset($post_data['post_status']) )
  $post_data['post_status'] = 'publish';

 if (!isset( $post_data['comment_status'] ))
  $post_data['comment_status'] = 'closed';

 if (!isset( $post_data['ping_status'] ))
  $post_data['ping_status'] = 'closed';

 foreach ( array('aa', 'mm', 'jj', 'hh', 'mn') as $timeunit ) {
  if ( !empty( $post_data['hidden_' . $timeunit] ) && $post_data['hidden_' . $timeunit] != $post_data[$timeunit] ) {
   $post_data['edit_date'] = '1';
   break;
  }
 }

 if ( !empty( $post_data['edit_date'] ) ) {
  $aa = $post_data['aa'];
  $mm = $post_data['mm'];
  $jj = $post_data['jj'];
  $hh = $post_data['hh'];
  $mn = $post_data['mn'];
  $ss = $post_data['ss'];
  $aa = ($aa <= 0 ) ? date('Y') : $aa;
  $mm = ($mm <= 0 ) ? date('n') : $mm;
  $jj = ($jj > 31 ) ? 31 : $jj;
  $jj = ($jj <= 0 ) ? date('j') : $jj;
  $hh = ($hh > 23 ) ? $hh -24 : $hh;
  $mn = ($mn > 59 ) ? $mn -60 : $mn;
  $ss = ($ss > 59 ) ? $ss -60 : $ss;
  $post_data['post_date'] = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss );
		$valid_date = wp_checkdate( $mm, $jj, $aa, $post_data['post_date'] );
		if ( !$valid_date ) {
			return new WP_Error( 'invalid_date', __( 'Whoops, the provided date is invalid.' ) );
		}
  $post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] );
 }

 return $post_data;
}

function etwp_insert_post($postarr, $wp_error = false) {
	global $wpdb, $user_ID;

	$defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_ID,
		'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
		'menu_order' => 0, 'to_ping' =>  '', 'pinged' => '', 'post_password' => '',
		'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0,
		'post_content' => '', 'post_title' => '');

	$postarr = wp_parse_args($postarr, $defaults);

	unset( $postarr[ 'filter' ] );

	$postarr = sanitize_post($postarr, 'raw'); //zzcity modi

	// export array as variables

	extract($postarr, EXTR_SKIP);
	// Are we updating or creating?
	$update = false;
	if ( !empty($ID) ) {
		$update = true;
		$previous_status = get_post_field('post_status', $ID);
	} else {
		$previous_status = 'new';
	}
	


	$maybe_empty = ! $post_content && ! $post_title && ! $post_excerpt && post_type_supports( $post_type, 'editor' )
		&& post_type_supports( $post_type, 'title' ) && post_type_supports( $post_type, 'excerpt' );
	if ( apply_filters( 'wp_insert_post_empty_content', $maybe_empty, $postarr ) ) {
		if ( $wp_error )
			return new WP_Error( 'empty_content', __( 'Content, title, and excerpt are empty.' ) );
		else
			return 0;
	}
	
	if ( empty($post_type) )
		$post_type = 'post';

	if ( empty($post_status) )
		$post_status = 'draft';

	if ( !empty($post_category) )
		$post_category = array_filter($post_category); // Filter out empty terms

	// Make sure we set a valid category.
	if ( empty($post_category) || 0 == count($post_category) || !is_array($post_category) ) {
		// 'post' requires at least one category.
		if ( 'post' == $post_type && 'auto-draft' != $post_status )
			$post_category = array( get_option('default_category') );
		else
			$post_category = array();
	}

	if ( empty($post_author) )
		$post_author = $user_ID;

	$post_ID = 0;

	// Get the post ID and GUID
	if ( $update ) {
		$post_ID = (int) $ID;
		$guid = get_post_field( 'guid', $post_ID );
		$post_before = get_post($post_ID);
	}

	// Don't allow contributors to set the post slug for pending review posts
	if ( 'pending' == $post_status && !current_user_can( 'publish_posts' ) )
		$post_name = '';

	// Create a valid post name. Drafts and pending posts are allowed to have an empty
	// post name.
	if ( empty($post_name) ) {
		if ( !in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) )
			$post_name = sanitize_title($post_title);
		else
			$post_name = '';
	} else {
		// On updates, we need to check to see if it's using the old, fixed sanitization context.
		$check_name = sanitize_title( $post_name, '', 'old-save' );
		if ( $update && strtolower( urlencode( $post_name ) ) == $check_name && get_post_field( 'post_name', $ID ) == $check_name )
			$post_name = $check_name;
		else // new post, or slug has changed.
			$post_name = sanitize_title($post_name);
	}

	// If the post date is empty (due to having been new or a draft) and status is not 'draft' or 'pending', set date to now
	if ( empty($post_date) || '0000-00-00 00:00:00' == $post_date )
		$post_date = current_time('mysql');

		// validate the date
		$mm = substr( $post_date, 5, 2 );
		$jj = substr( $post_date, 8, 2 );
		$aa = substr( $post_date, 0, 4 );
		$valid_date = wp_checkdate( $mm, $jj, $aa, $post_date );
		if ( !$valid_date ) {
			if ( $wp_error )
				return new WP_Error( 'invalid_date', __( 'Whoops, the provided date is invalid.' ) );
			else
				return 0;
		}

	if ( empty($post_date_gmt) || '0000-00-00 00:00:00' == $post_date_gmt ) {
		if ( !in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) )
			$post_date_gmt = get_gmt_from_date($post_date);
		else
			$post_date_gmt = '0000-00-00 00:00:00';
	}

	if ( $update || '0000-00-00 00:00:00' == $post_date ) {
		$post_modified     = current_time( 'mysql' );
		$post_modified_gmt = current_time( 'mysql', 1 );
	} else {
		$post_modified     = $post_date;
		$post_modified_gmt = $post_date_gmt;
	}

	if ( 'publish' == $post_status ) {
		$now = gmdate('Y-m-d H:i:59');
		if ( mysql2date('U', $post_date_gmt, false) > mysql2date('U', $now, false) )
			$post_status = 'future';
	} elseif( 'future' == $post_status ) {
		$now = gmdate('Y-m-d H:i:59');
		if ( mysql2date('U', $post_date_gmt, false) <= mysql2date('U', $now, false) )
			$post_status = 'publish';
	}

	if ( empty($comment_status) ) {
		if ( $update )
			$comment_status = 'closed';
		else
			$comment_status = get_option('default_comment_status');
	}
	if ( empty($ping_status) )
		$ping_status = get_option('default_ping_status');

	if ( isset($to_ping) )
		$to_ping = sanitize_trackback_urls( $to_ping );
	else
		$to_ping = '';

	if ( ! isset($pinged) )
		$pinged = '';

	if ( isset($post_parent) )
		$post_parent = (int) $post_parent;
	else
		$post_parent = 0;

	// Check the post_parent to see if it will cause a hierarchy loop
	$post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, compact( array_keys( $postarr ) ), $postarr );

	if ( isset($menu_order) )
		$menu_order = (int) $menu_order;
	else
		$menu_order = 0;

	if ( !isset($post_password) || 'private' == $post_status )
		$post_password = '';

	$post_name = wp_unique_post_slug($post_name, $post_ID, $post_status, $post_type, $post_parent);

	// expected_slashed (everything!)
	$data = compact( array( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order', 'guid' ) );

	$data = apply_filters('wp_insert_post_data', $data, $postarr);

	$data = stripslashes_deep( $data );
	$where = array( 'ID' => $post_ID );

	if ( $update ) {
		do_action( 'pre_post_update', $post_ID );
		if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) {
			if ( $wp_error )
				return new WP_Error('db_update_error', __('Could not update post in the database'), $wpdb->last_error);
			else
				return 0;
		}
	} else {
		if ( isset($post_mime_type) )
			$data['post_mime_type'] = stripslashes( $post_mime_type ); // This isn't in the update
		// If there is a suggested ID, use it if not already present
		if ( !empty($import_id) ) {
			$import_id = (int) $import_id;
			if ( ! $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id) ) ) {
				$data['ID'] = $import_id;
			}
		}
		if ( false === $wpdb->insert( $wpdb->posts, $data ) ) {
			if ( $wp_error )
				return new WP_Error('db_insert_error', __('Could not insert post into the database'), $wpdb->last_error);
			else
				return 0;
		}
		$post_ID = (int) $wpdb->insert_id;

		// use the newly generated $post_ID
		$where = array( 'ID' => $post_ID );
	}

	if ( empty($data['post_name']) && !in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
		$data['post_name'] = sanitize_title($data['post_title'], $post_ID);
		$wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where );
	}

	if ( is_object_in_taxonomy($post_type, 'category') )
		wp_set_post_categories( $post_ID, $post_category );

	if ( isset( $tags_input ) && is_object_in_taxonomy($post_type, 'post_tag') )
		wp_set_post_tags( $post_ID, $tags_input );

	// new-style support for all custom taxonomies
	if ( !empty($tax_input) ) {
		foreach ( $tax_input as $taxonomy => $tags ) {
			$taxonomy_obj = get_taxonomy($taxonomy);
			if ( is_array($tags) ) // array = hierarchical, string = non-hierarchical.
				$tags = array_filter($tags);
			if ( current_user_can($taxonomy_obj->cap->assign_terms) )
				wp_set_post_terms( $post_ID, $tags, $taxonomy );
		}
	}

	$current_guid = get_post_field( 'guid', $post_ID );

	// Set GUID
	if ( !$update && '' == $current_guid )
		$wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where );

	clean_post_cache( $post_ID );

	$post = get_post($post_ID);

	if ( !empty($page_template) && 'page' == $data['post_type'] ) {
		$post->page_template = $page_template;
		$page_templates = wp_get_theme()->get_page_templates();
		if ( 'default' != $page_template && ! isset( $page_templates[ $page_template ] ) ) {
			if ( $wp_error )
				return new WP_Error('invalid_page_template', __('The page template is invalid.'));
			else
				return 0;
		}
		update_post_meta($post_ID, '_wp_page_template',  $page_template);
	}

	wp_transition_post_status($data['post_status'], $previous_status, $post);

	if ( $update ) {
		do_action('edit_post', $post_ID, $post);
		$post_after = get_post($post_ID);
		do_action( 'post_updated', $post_ID, $post_after, $post_before);
	}

	do_action('save_post', $post_ID, $post);
	do_action('wp_insert_post', $post_ID, $post);

	return $post_ID;
}


function etwp_write_post() {
 global $user_ID;

/* 	if ( isset($_POST['post_type']) )
		$ptype = get_post_type_object($_POST['post_type']);
	else
		$ptype = get_post_type_object('post');

	if ( !current_user_can( $ptype->cap->edit_posts ) ) {
		if ( 'page' == $ptype->name )
			return new WP_Error( 'edit_pages', __( 'You are not allowed to create pages on this site.' ) );
		else
			return new WP_Error( 'edit_posts', __( 'You are not allowed to create posts or drafts on this site.' ) );
	}
*/
	$_POST['post_mime_type'] = '';

	// Clear out any data in internal vars.
	unset( $_POST['filter'] );

/*	// Edit don't write if we have a post id.
	if ( isset( $_POST['post_ID'] ) )
		return edit_post();
*/

 if (!isset( $_POST['publish'] ))
  $_POST['publish'] = 'publish';
  
 if (!isset( $_POST['visibility'] ))
  $_POST['visibility'] = 'public';
  
 if (empty( $_POST['post_status'] ))
  $_POST['post_status'] = 'publish';
  
 if (!isset( $_POST['comment_status'] ))
  $_POST['comment_status'] = 'open';
  
 $translated =et_wp_translate_postdata( false );
 if ( is_wp_error($translated) )
  return $translated;

 if ( isset($_POST['visibility']) ) {
  switch ( $_POST['visibility'] ) {
   case 'public' :
    $_POST['post_password'] = '';
    break;
   case 'password' :
    unset( $_POST['sticky'] );
    break;
   case 'private' :
    $_POST['post_status'] = 'private';
    $_POST['post_password'] = '';
    unset( $_POST['sticky'] );
    break;
  }
 }




 // Create the post.
 $post_ID = etwp_insert_post( $_POST );
 if ( is_wp_error( $post_ID ) )
  return $post_ID;

 if ( empty($post_ID) )
  return 0;
 
 //add_meta( $post_ID );
 //zzcity add 
 if ( isset($_POST['meta']) && $_POST['meta'] ) {
  foreach ( $_POST['meta'] as $key => $value )
	 add_post_meta( $post_ID, $value['key'], $value['value']);
 }
 add_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID );


 // Now that we have an ID we can fix any attachment anchor hrefs
 _fix_attachment_links( $post_ID );


	wp_set_post_lock( $post_ID );

 return $post_ID;
}

function etwrite_post() {
 $result = etwp_write_post();
 if( is_wp_error( $result ) )
  wp_die( $result->get_error_message() );
 else
  return $result;
}




//$parent_file = 'edit.php';
//$submenu_file = 'edit.php';

$_POST['action']='post';
$_POST['post_type']='post';
$_POST['advanced_view']=1;
$_POST['post_pingback']=1;

$tz = get_option('timezone_string');
if ( $tz ) {
	date_default_timezone_set( $tz );
}

$zztime=0;
if (isset($_POST['zzdelay']) ) {
	$zzdelay=(int)$_POST['zzdelay'];
	$zztime=time()+$zzdelay*60*60;
}

if (!empty($_POST['zztime']) ) {
	$zztime=strtotime($_POST['zztime']);
}
if($zztime>0){
	$_POST['hidden_aa']=date('Y');	
	$_POST['hidden_mm']=date('n');	
	$_POST['hidden_jj']=date('j');	
	$_POST['hidden_hh']=date('H');	
	$_POST['hidden_mn']=date('i');	
	$_POST['hidden_ss']=date('s');	

//	$zztime=date('Y-m-d H:i:s',$zztime);
//	$zztime=get_gmt_from_date($zztime);
//	$zztime=strtotime($zztime);
	$_POST['aa']=date('Y',$zztime);	
	$_POST['mm']=date('n',$zztime);	
	$_POST['jj']=date('j',$zztime);	
	$_POST['hh']=date('H',$zztime);	
	$_POST['mn']=date('i',$zztime);	
	$_POST['ss']=date('s',$zztime);	
}

wp_reset_vars(array('action', 'safe_mode', 'withcomments', 'posts', 'content', 'edited_post_title', 'comment_error', 'profile', 'trackback_url', 'excerpt', 'showcomments', 'commentstart', 'commentend', 'commentorder'));


$post_ID = etwrite_post() ;

if ( '1' == $_POST['sticky'] )
	stick_post( $post_ID );

// Post Formats
if ( isset( $_POST['post_format'] ) ) {
	if ( $_POST['post_format']!=='0'  ){
		set_post_format( $post_ID, $_POST['post_format'] );
		}
}


if (is_numeric($post_ID)){
 echo('[reply]comment_post_ID='.$post_ID.'[/reply]');}
else{
 echo('[err]POST failure[/err]');
 exit;
}
$attachs=explode(',',$_POST['etattachs']);
$flagthumb=0;
foreach($attachs as $aindex => $afile){
	if(trim($afile)!=''){
		$thumbnail_id = et_media_handle_upload($afile, $post_ID);
	
		if($_POST['litpic']==$afile){
			set_post_thumbnail( $post_ID, $thumbnail_id );
		}else{
			$aext=zzfileext($afile);
			if((zzis_image_ext($aext))&&($flagthumb==0)){
				set_post_thumbnail( $post_ID, $thumbnail_id );
				$flagthumb=1;
			}
		}
	}
}


//附件

function zzfileext($filename) {
	return strtolower(substr(strrchr($filename, '.'), 1, 10));
}

function zzis_image_ext($ext) {
	static $imgext  = array('jpg', 'jpeg', 'gif', 'png', 'bmp');
	return in_array($ext, $imgext) ? true : false;
}

function mime($file) {
    $mime = '';
    if (!file_exists($file)) {
        return '';
    }
		if (function_exists('finfo_open')) {
				$finfo = finfo_open(FILEINFO_MIME);
				$mime = finfo_file($finfo, $file);
				finfo_close($finfo);
		} elseif (function_exists('mime_content_type')) {
				$mime = mime_content_type($file);
		}	elseif (function_exists('exif_imagetype')){
				$mime = image_type_to_mime_type(exif_imagetype($file));				
		}
    return $mime;
}

function et_media_handle_upload($imageurl, $post_id, $post_data = array(), $overrides = array( 'test_form' => false )) {

	$time = current_time('mysql');
/*	if ( $post = get_post($post_id) ) {
		if ( substr( $post->post_date, 0, 4 ) > 0 )
			$time = $post->post_date;
	}

*/
	$temp = explode('/', $imageurl);

	$name =  trim($temp[count($temp)-1]);

/*	$name = $_FILES[$file_id]['name'];
	$file = wp_handle_upload($_FILES[$file_id], $overrides, $time);

	if ( isset($file['error']) )
		return new WP_Error( 'upload_error', $file['error'] );

	$name_parts = pathinfo($name);
	$name = trim( substr( $name, 0, -(1 + strlen($name_parts['extension'])) ) );
*/
	//$url = $file['url'];
	$url = 'http://'.$_SERVER['HTTP_HOST'].$imageurl;
//	$type = $file['type'];
	$path=dirname(realpath($_SERVER['SCRIPT_FILENAME']));
	$path=$path.'/..'.$imageurl;
	$type = mime($path);
	if($type=='')$type='image/*';

	//$file = $file['file'];
	$file = $path;
	$title = $name;
	$content = '';

	// use image exif/iptc data for title and caption defaults if possible
	if ( $image_meta = @wp_read_image_metadata($file) ) {
		if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
			$title = $image_meta['title'];
		if ( trim( $image_meta['caption'] ) )
			$content = $image_meta['caption'];
	}

	// Construct the attachment array
	$attachment = array_merge( array(
		'post_mime_type' => $type,
		'guid' => $url,
		'post_parent' => $post_id,
		'post_title' => $title,
		'post_content' => $content,
	), $post_data );

	// This should never be set as it would then overwrite an existing attachment.
	if ( isset( $attachment['ID'] ) )
		unset( $attachment['ID'] );

	// Save the data
	$id = wp_insert_attachment($attachment, $file, $post_id);
	if ( !is_wp_error($id) ) {
		wp_update_attachment_metadata( $id,wp_generate_attachment_metadata( $id, $file ));
	}

	return $id;

}

/**
 * Redirect to previous page.
 *
 * @param int $post_ID Optional. Post ID.
 */
/*function redirect_post($post_ID = '') {
 global $action;

 $referredby = '';
 if ( !empty($_POST['referredby']) ) {
  $referredby = preg_replace('|https?://[^/]+|i', '', $_POST['referredby']);
  $referredby = remove_query_arg('_wp_original_http_referer', $referredby);
 }
 $referer = preg_replace('|https?://[^/]+|i', '', wp_get_referer());

 if ( !empty($_POST['mode']) && 'sidebar' == $_POST['mode'] ) {
  if ( isset($_POST['saveasdraft']) )
   $location = 'sidebar.php?a=c';
  elseif ( isset($_POST['publish']) )
   $location = 'sidebar.php?a=b';
 } elseif ( isset($_POST['save']) || isset($_POST['publish']) ) {
  $status = get_post_status( $post_ID );

  if ( isset( $_POST['publish'] ) ) {
   switch ( $status ) {
    case 'pending':
     $message = 8;
     break;
    case 'future':
     $message = 9;
     break;
    default:
     $message = 6;
   }
  } else {
    $message = 'draft' == $status ? 10 : 1;
  }

  $location = add_query_arg( 'message', $message, get_edit_post_link( $post_ID, 'url' ) );
 } elseif ( isset($_POST['addmeta']) && $_POST['addmeta'] ) {
  $location = add_query_arg( 'message', 2, wp_get_referer() );
  $location = explode('#', $location);
  $location = $location[0] . '#postcustom';
 } elseif ( isset($_POST['deletemeta']) && $_POST['deletemeta'] ) {
  $location = add_query_arg( 'message', 3, wp_get_referer() );
  $location = explode('#', $location);
  $location = $location[0] . '#postcustom';
 } elseif ( 'post-quickpress-save-cont' == $_POST['action'] ) {
  $location = "post.php?action=edit&post=$post_ID&message=7";
 } else {
  $location = add_query_arg( 'message', 4, get_edit_post_link( $post_ID, 'url' ) );
 }

 wp_redirect( apply_filters( 'redirect_post_location', $location, $post_ID ) );
}

if ( isset( $_POST['deletepost'] ) )
 $action = 'delete';
elseif ( isset($_POST['wp-preview']) && 'dopreview' == $_POST['wp-preview'] )
 $action = 'preview';

$sendback = wp_get_referer();
if ( strpos($sendback, 'post.php') !== false || strpos($sendback, 'post-new.php') !== false )
 $sendback = admin_url('edit.php');
else
 $sendback = remove_query_arg( array('trashed', 'untrashed', 'deleted', 'ids'), $sendback );
*/
//switch($action) {
//case 'postajaxpost':
//case 'post':
//case 'post-quickpress-publish':
//case 'post-quickpress-save':
// check_admin_referer('add-post');
//
// if ( 'post-quickpress-publish' == $action )
//  $_POST['publish'] = 'publish'; // tell write_post() to publish
//
// if ( 'post-quickpress-publish' == $action || 'post-quickpress-save' == $action ) {
//  $_POST['comment_status'] = get_option('default_comment_status');
//  $_POST['ping_status'] = get_option('default_ping_status');
// }
//
// if ( !empty( $_POST['quickpress_post_ID'] ) ) {
//  $_POST['post_ID'] = (int) $_POST['quickpress_post_ID'];
//  $post_ID = edit_post();
// } else {
//  $post_ID = 'postajaxpost' == $action ? edit_post() : write_post();
// }
//
// if ( 0 === strpos( $action, 'post-quickpress' ) ) {
//  $_POST['post_ID'] = $post_ID;
//  // output the quickpress dashboard widget
//  require_once(ABSPATH . 'wp-admin/includes/dashboard.php');
//  wp_dashboard_quick_press();
//  exit;
// }
//
// redirect_post($post_ID);
// exit();
// break;
//
//case 'edit':
// $editing = true;
//
// if ( empty( $_GET['post'] ) ) {
//  wp_redirect("post.php");
//  exit();
// }
// $post_ID = $p = (int) $_GET['post'];
// $post = get_post($post_ID);
//
// if ( empty($post->ID) )
//  wp_die( __('You attempted to edit a post that doesn&#8217;t exist. Perhaps it was deleted?') );
//
// if ( !current_user_can('edit_post', $post_ID) )
//  wp_die( __('You are not allowed to edit this post.') );
//
// if ( 'trash' == $post->post_status )
//  wp_die( __('You can&#8217;t edit this post because it is in the Trash. Please restore it and try again.') );
//
// if ( 'post' != $post->post_type ) {
//  wp_redirect( get_edit_post_link( $post->ID, 'url' ) );
//  exit();
// }
//
// wp_enqueue_script('post');
// if ( user_can_richedit() )
//  wp_enqueue_script('editor');
// add_thickbox();
// wp_enqueue_script('media-upload');
// wp_enqueue_script('word-count');
// wp_enqueue_script( 'admin-comments' );
// enqueue_comment_hotkeys_js();
//
// if ( $last = wp_check_post_lock( $post->ID ) ) {
//  add_action('admin_notices', '_admin_notice_post_locked' );
// } else {
//  wp_set_post_lock( $post->ID );
//  wp_enqueue_script('autosave');
// }
//
// $title = __('Edit Post');
// $post = get_post_to_edit($post_ID);
//
// include('edit-form-advanced.php');
//
// break;
//
//case 'editattachment':
// $post_id = (int) $_POST['post_ID'];
//
// check_admin_referer('update-attachment_' . $post_id);
//
// // Don't let these be changed
// unset($_POST['guid']);
// $_POST['post_type'] = 'attachment';
//
// // Update the thumbnail filename
// $newmeta = wp_get_attachment_metadata( $post_id, true );
// $newmeta['thumb'] = $_POST['thumb'];
//
// wp_update_attachment_metadata( $post_id, $newmeta );
//
//case 'editpost':
// $post_ID = (int) $_POST['post_ID'];
// check_admin_referer('update-post_' . $post_ID);

// $post_ID = edit_post();

// redirect_post($post_ID); // Send user on their way while we keep working

// exit();
// break;

//case 'trash':
// $post_id = isset($_GET['post']) ? intval($_GET['post']) : intval($_POST['post_ID']);
// check_admin_referer('trash-post_' . $post_id);
//
// $post = & get_post($post_id);
//
// if ( !current_user_can('delete_post', $post_id) )
//  wp_die( __('You are not allowed to move this post to the trash.') );
//
// if ( ! wp_trash_post($post_id) )
//  wp_die( __('Error in moving to trash...') );
//
// wp_redirect( add_query_arg( array('trashed' => 1, 'ids' => $post_id), $sendback ) );
// exit();
// break;
//
//case 'untrash':
// $post_id = isset($_GET['post']) ? intval($_GET['post']) : intval($_POST['post_ID']);
// check_admin_referer('untrash-post_' . $post_id);
//
// $post = & get_post($post_id);
//
// if ( !current_user_can('delete_post', $post_id) )
//  wp_die( __('You are not allowed to move this post out of the trash.') );
//
// if ( ! wp_untrash_post($post_id) )
//  wp_die( __('Error in restoring from trash...') );
//
// wp_redirect( add_query_arg('untrashed', 1, $sendback) );
// exit();
// break;
//
//case 'delete':
// $post_id = (isset($_GET['post']))  ? intval($_GET['post']) : intval($_POST['post_ID']);
// check_admin_referer('delete-post_' . $post_id);
//
// $post = & get_post($post_id);
//
// if ( !current_user_can('delete_post', $post_id) )
//  wp_die( __('You are not allowed to delete this post.') );
//
// $force = !EMPTY_TRASH_DAYS;
// if ( $post->post_type == 'attachment' ) {
//  $force = ( $force || !MEDIA_TRASH );
//  if ( ! wp_delete_attachment($post_id, $force) )
//   wp_die( __('Error in deleting...') );
// } else {
//  if ( !wp_delete_post($post_id, $force) )
//   wp_die( __('Error in deleting...') );
// }
//
// wp_redirect( add_query_arg('deleted', 1, $sendback) );
// exit();
// break;
//
//case 'preview':
// check_admin_referer( 'autosave', 'autosavenonce' );
//
// $url = post_preview();
//
// wp_redirect($url);
// exit();
// break;
//
//default:
// wp_redirect('edit.php');
// exit();
// break;
//} // end switch
//include('admin-footer.php');
?>
