<?php
/*
EditorTools文章采集发布助手
----------------------------------
文件名
ETpost.php
wordpress2.2.3 文章发布文件，请复制在wp-admin目录使用，为保密，请自行修改文件名

主要参数
log 		用户参数名
pwd 		密码参数名
post_title	标题参数名
content 	内容参数名

附加参数


可选参数
post_type		   发布类型，日志则取值为post，页面取值为page 
excerpt			   摘要
trackback_url      发送 Trackbac网址(多个 Trackback 地址请用空格隔开。) 
post_category[x]   分类编号，若属于一个分类，则x=0，否则x=0到分类个数减一：如post_category[0]，post_category[1]……
comment_status	   是否允许评论，不允许则为closed，允许则为open，默认不允许
ping_status        是否允许Ping，不允许则为closed，允许则为open，默认不允许
post_password	   文章访问密码
parent_id		   上级页面编号（页面类型‘post_type=page’时适用）

ET发布配置-文章检查网址处，可以如下填写
http://您的网址/wp-admin/ETpost.php

*/

//require_once('admin.php');

//以下是admin.php源码
if ( defined('ABSPATH') )
	require_once( ABSPATH . 'wp-config.php');
else
    require_once('../wp-config.php');

if ( get_option('db_version') != $wp_db_version )
	wp_die(sprintf(__("Your database is out-of-date.  Please <a href='%s'>upgrade</a>."), get_option('siteurl') . '/wp-admin/upgrade.php'));
    
require_once(ABSPATH . 'wp-admin/admin-functions.php');
require_once(ABSPATH . 'wp-admin/admin-db.php');
require_once(ABSPATH . WPINC . '/registration.php');


/*以下是增加登陆代码*/

	$user_login = '';
	$user_pass = '';
	$using_cookie = FALSE;

	/*
	if ( !isset( $_REQUEST['redirect_to'] ) )
		$redirect_to = 'wp-admin/';
	else
		$redirect_to = $_REQUEST['redirect_to'];
	

	if ( $_POST ) {
		$user_login = $_POST['log'];
		$user_login = sanitize_user( $user_login );
		$user_pass  = $_POST['pwd'];
		$rememberme = $_POST['rememberme'];
	} else {
		$cookie_login = wp_get_cookie_login();
		if ( ! empty($cookie_login) ) {
			$using_cookie = true;
			$user_login = $cookie_login['login'];
			$user_pass = $cookie_login['password'];
		}
	}
	*/
	$user_login = $_REQUEST['log'];
	$user_login = sanitize_user( $user_login );
	$user_pass  = $_REQUEST['pwd'];	
	
	do_action_ref_array('wp_authenticate', array(&$user_login, &$user_pass));

	if ( $user_login && $user_pass && empty( $errors ) ) {
		$user = new WP_User(0, $user_login);
		
		// If the user can't edit posts, send them to their profile.
		/*
		if ( !$user->has_cap('edit_posts') && ( empty( $redirect_to ) || $redirect_to == 'wp-admin/' ) )
			$redirect_to = get_option('siteurl') . '/wp-admin/profile.php';
		*/
		if ( wp_login($user_login, $user_pass, false) ) {
			//if ( !$using_cookie )
			//	wp_setcookie($user_login, $user_pass, false, '', '', $rememberme);
			do_action('wp_login', $user_login);
			//wp_redirect($redirect_to);
			//exit();
		} else {
			//if ( $using_cookie )
			//	$errors['expiredsession'] = __('Your session has expired.');
			echo('[err]The username field or The password field  is error[/err]');
			exit;			
		}
	}
	
	if ( empty( $user_login ) ){
		echo('[err]The username field is empty[/err]');
		exit;
		}
	if ( $_POST && empty( $user_pass ) ){
		echo('[err]The password field is empty[/err]');
		exit;
		}

	$_POST['user_ID']=$user->ID;
	

/*以上是增加登陆代码*/


//auth_redirect();

nocache_headers();

update_category_cache();

wp_get_current_user();


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

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

wp_enqueue_script( 'fat' );

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

require(ABSPATH . '/wp-admin/menu.php');

// Handle plugin admin pages.
if (isset($plugin_page)) {
	$page_hook = get_plugin_page_hook($plugin_page, $pagenow);

	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(ABSPATH . PLUGINDIR . "/$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');

		include(ABSPATH . PLUGINDIR . "/$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_die(__('Invalid importer.'));
	}

	if (! file_exists(ABSPATH . "wp-admin/import/$importer.php"))
		wp_die(__('Cannot load importer.'));

	include(ABSPATH . "wp-admin/import/$importer.php");

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

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

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

	define('WP_IMPORTING', true);

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

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

	exit();
} else {
	do_action("load-$pagenow");
}
*/


//以上是admin.php源码


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

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


//$action=post

	//$parent_file = 'post-new.php';
	//$submenu_file = 'post-new.php';
	//check_admin_referer('add-post');

	
	ETwrite_post();
	
/*ET修改的write_post*/	

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


function ETwp_insert_post($postarr = array()) {
	global $wpdb, $wp_rewrite, $allowedtags, $user_ID;

	if ( is_object($postarr) )
		$postarr = get_object_vars($postarr);

	// export array as variables
	extract($postarr);

	// Are we updating or creating?
	$update = false;
	if ( !empty($ID) ) {
		$update = true;
		$post = & get_post($ID);
		$previous_status = $post->post_status;
	}

	// Get the basics.
	if ( empty($no_filter) ) {
		$post_content    = apply_filters('content_save_pre',   $post_content);
		$post_content_filtered = apply_filters('content_filtered_save_pre',   $post_content_filtered);
		$post_excerpt    = apply_filters('excerpt_save_pre',   $post_excerpt);
		$post_title      = apply_filters('title_save_pre',     $post_title);
		$post_category   = apply_filters('category_save_pre',  $post_category);
		$post_status     = apply_filters('status_save_pre',    $post_status);
		$post_name       = apply_filters('name_save_pre',      $post_name);
		$comment_status  = apply_filters('comment_status_pre', $comment_status);
		$ping_status     = apply_filters('ping_status_pre',    $ping_status);
	}

	if ( ('' == $post_content) && ('' == $post_title) && ('' == $post_excerpt) )
		return 0;

	// Make sure we set a valid category
	if (0 == count($post_category) || !is_array($post_category)) {
		$post_category = array(get_option('default_category'));
	}
	$post_cat = $post_category[0];

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

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

	if ( empty($post_type) )
		$post_type = 'post';

	// Get the post ID.
	if ( $update )
		$post_ID = (int) $ID;

	// Create a valid post name.  Drafts are allowed to have an empty
	// post name.
	if ( empty($post_name) ) {
		if ( 'draft' != $post_status )
			$post_name = sanitize_title($post_title);
	} else {
		$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', set date to now
	if (empty($post_date)) {
		if ( 'draft' != $post_status )
			$post_date = current_time('mysql');
	}

	if (empty($post_date_gmt)) {
		if ( 'draft' != $post_status )
			$post_date_gmt = get_gmt_from_date($post_date);
	}

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

	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 ( empty($post_pingback) )
		$post_pingback = get_option('default_pingback_flag');

	if ( isset($to_ping) )
		$to_ping = preg_replace('|\s+|', "\n", $to_ping);
	else
		$to_ping = '';

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

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

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

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

	if ( 'draft' != $post_status ) {
		$post_name_check = $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$post_name' AND post_type = '$post_type' AND ID != '$post_ID' AND post_parent = '$post_parent' LIMIT 1");

		if ($post_name_check || in_array($post_name, $wp_rewrite->feeds) ) {
			$suffix = 2;
			do {
				$alt_post_name = $post_name . "-$suffix";
				$post_name_check = $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$alt_post_name' AND post_type = '$post_type' AND ID != '$post_ID' AND post_parent = '$post_parent' LIMIT 1");
				$suffix++;
			} while ($post_name_check);
			$post_name = $alt_post_name;
		}
	}

	if ($update) {
		$wpdb->query(
			"UPDATE IGNORE $wpdb->posts SET
			post_author = '$post_author',
			post_date = '$post_date',
			post_date_gmt = '$post_date_gmt',
			post_content = '$post_content',
			post_content_filtered = '$post_content_filtered',
			post_title = '$post_title',
			post_excerpt = '$post_excerpt',
			post_status = '$post_status',
			post_type = '$post_type',
			comment_status = '$comment_status',
			ping_status = '$ping_status',
			post_password = '$post_password',
			post_name = '$post_name',
			to_ping = '$to_ping',
			pinged = '$pinged',
			post_modified = '".current_time('mysql')."',
			post_modified_gmt = '".current_time('mysql',1)."',
			post_parent = '$post_parent',
			menu_order = '$menu_order'
			WHERE ID = $post_ID");
	} else {
		$wpdb->query(
			"INSERT IGNORE INTO $wpdb->posts
			(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, post_mime_type)
			VALUES
			('$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_date', '$post_date_gmt', '$post_parent', '$menu_order', '$post_mime_type')");
			$post_ID = (int) $wpdb->insert_id;
	}

	if ( empty($post_name) && 'draft' != $post_status ) {
		$post_name = sanitize_title($post_title, $post_ID);
		$wpdb->query( "UPDATE $wpdb->posts SET post_name = '$post_name' WHERE ID = '$post_ID'" );
	}

	wp_set_post_categories($post_ID, $post_category);

	if ( 'page' == $post_type ) {
		clean_page_cache($post_ID);
		$wp_rewrite->flush_rules();
	} else {
		clean_post_cache($post_ID);
	}

	// Set GUID
	if ( ! $update )
		$wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'");

	if ( $update) {
		if ($previous_status != 'publish' && $post_status == 'publish') {
			// Reset GUID if transitioning to publish.
			$wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'");
			do_action('private_to_published', $post_ID);
		}

		do_action('edit_post', $post_ID);
	}

	if ($post_status == 'publish' && $post_type == 'post') {
		do_action('publish_post', $post_ID);
		if ( defined('XMLRPC_REQUEST') )
			do_action('xmlrpc_publish_post', $post_ID);
		if ( defined('APP_REQUEST') )
			do_action('app_publish_post', $post_ID);

		if ( !defined('WP_IMPORTING') ) {
			if ( $post_pingback )
				$result = $wpdb->query("
					INSERT INTO $wpdb->postmeta
					(post_id,meta_key,meta_value)
					VALUES ('$post_ID','_pingme','1')
				");
			$result = $wpdb->query("
				INSERT INTO $wpdb->postmeta
				(post_id,meta_key,meta_value)
				VALUES ('$post_ID','_encloseme','1')
			");
			wp_schedule_single_event(time(), 'do_pings');
		}
	} else if ($post_type == 'page') {
		if ( !empty($page_template) )
			if ( ! update_post_meta($post_ID, '_wp_page_template',  $page_template))
				add_post_meta($post_ID, '_wp_page_template',  $page_template, true);

		if ( $post_status == 'publish' )
			do_action('publish_page', $post_ID);
	}

	// Always clears the hook in case the post status bounced from future to draft.
	wp_clear_scheduled_hook('publish_future_post', $post_ID);

	// Schedule publication.
	if ( 'future' == $post_status )
		wp_schedule_single_event(strtotime($post_date_gmt. ' GMT'), 'publish_future_post', array($post_ID));

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

	return $post_ID;
}


// Creates a new post from the "Write Post" form using $_POST information.
function ETwp_write_post() {
	global $user_ID;

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

/*	// Check for autosave collisions
	if ( isset($_POST['temp_ID']) ) {
		$temp_id = (int) $_POST['temp_ID'];
		if ( !$draft_ids = get_user_option( 'autosave_draft_ids' ) )
			$draft_ids = array();
		foreach ( $draft_ids as $temp => $real )
			if ( time() + $temp > 86400 ) // 1 day: $temp is equal to -1 * time( then )
				unset($draft_ids[$temp]);

		if ( isset($draft_ids[$temp_id]) ) { // Edit, don't write
			$_POST['post_ID'] = $draft_ids[$temp_id];
			unset($_POST['temp_ID']);
			relocate_children( $temp_id, $_POST['post_ID'] );
			update_user_option( $user_ID, 'autosave_draft_ids', $draft_ids );
			return edit_post();
		}
	}
	*/
	if (!isset( $_POST['post_type'] ))
		$_POST['post_type'] = 'post';

	// Rename.
	/*if (get_magic_quotes_gpc()) {
	$_POST['post_content'] = stripslashes($_POST['content']);
	}else{
	$_POST['post_content']= mysql_real_escape_string($_POST['content']);
	}
	*/
	$_POST['post_content'] = $_POST['content'];

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

	}
*/
	$_POST['post_author'] = (int) $_POST['user_ID'];
/*
	if ( $_POST['post_author'] != $_POST['user_ID'] ) {
		if ( 'page' == $_POST['post_type'] ) {
			if ( !current_user_can( 'edit_others_pages' ) )
				return new WP_Error( 'edit_others_pages', __( 'You are not allowed to create pages as this user.' ) );
		} else {
			if ( !current_user_can( 'edit_others_posts' ) )
				return new WP_Error( 'edit_others_posts', __( 'You are not allowed to post as this user.' ) );

		}
	}
*/
/*
	// What to do based on which button they pressed
	if ('' != $_POST['saveasdraft'] )
		$_POST['post_status'] = 'draft';
	if ('' != $_POST['saveasprivate'] )
		$_POST['post_status'] = 'private';
	if ('' != $_POST['publish'] )
		$_POST['post_status'] = 'publish';
	if ('' != $_POST['advanced'] )
		$_POST['post_status'] = 'draft';
*/
		$_POST['post_status'] = 'publish';
/*		
	if ( 'page' == $_POST['post_type'] ) {
		if ('publish' == $_POST['post_status'] && !current_user_can( 'publish_pages' ) )
			$_POST['post_status'] = 'draft';
	} else {
		if ('publish' == $_POST['post_status'] && !current_user_can( 'publish_posts' ) )
			$_POST['post_status'] = 'draft';
	}
*/
	if (!isset( $_POST['comment_status'] ))
		$_POST['comment_status'] = 'closed';

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

	if (!isset( $_POST['originalaction'] ))
		$_POST['originalaction'] = 'post';
		
	if (!isset( $_POST['advanced_view'] ))
		$_POST['advanced_view'] = '1';
		
		
		
/*
	if (!empty ( $_POST['edit_date'] ) ) {
		$aa = $_POST['aa'];
		$mm = $_POST['mm'];
		$jj = $_POST['jj'];
		$hh = $_POST['hh'];
		$mn = $_POST['mn'];
		$ss = $_POST['ss'];
		$jj = ($jj > 31 ) ? 31 : $jj;
		$hh = ($hh > 23 ) ? $hh -24 : $hh;
		$mn = ($mn > 59 ) ? $mn -60 : $mn;
		$ss = ($ss > 59 ) ? $ss -60 : $ss;
		$_POST['post_date'] = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss );
		$_POST['post_date_gmt'] = get_gmt_from_date( $_POST['post_date'] );
	}
*/	
	// Create the post.
	$post_ID = ETwp_insert_post( $_POST );
	add_meta( $post_ID );
	// Reunite any orphaned attachments with their parent
	// Update autosave collision detection
	if ( $temp_id ) {
		relocate_children( $temp_id, $post_ID );
		$draft_ids[$temp_id] = $post_ID;
		update_user_option( $user_ID, 'autosave_draft_ids', $draft_ids );
	}

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

/*以上是ET修改的write_post*/
	echo('1');
	exit();

?>
