Index: wp-app.php
===================================================================
--- wp-app.php	(revision 16544)
+++ wp-app.php	(working copy)
@@ -16,7 +16,7 @@
 require_once('./wp-load.php');
 
 /** Atom Publishing Protocol Class */
-require_once(ABSPATH . WPINC . '/atomlib.php');
+require_once(ABSPATH . WPINC . '/class-simplepie.php');
 
 /** Admin Image API for metadata updating */
 require_once(ABSPATH . '/wp-admin/includes/image.php');
@@ -76,7 +76,93 @@
 }
 add_filter('posts_where', 'wa_posts_where_include_drafts_filter');
 
+define('SIMPLEPIE_NAMESPACE_ATOMPUB', 'http://www.w3.org/2007/app');
+
 /**
+ * SimplePie Helper for AtomPub
+ *
+ * @package WordPress
+ * @subpackage Publishing
+ * @since 3.1
+ */
+class SimplePieAtomPub_Item extends SimplePie_Item {
+	/**
+	 * Constructor
+	 */
+	function SimplePieAtomPub_Item($feed, $data) {
+		parent::SimplePie_Item($feed, $data);
+	}
+
+	/**
+	 * Get the status of the entry
+	 *
+	 * @return bool True if the item is a draft, false otherwise
+	 */
+	function get_draft_status() {
+		$draft = false;
+		if (($control = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOMPUB, 'control')) && !empty($control[0]['child'][SIMPLEPIE_NAMESPACE_ATOMPUB]['draft'][0]['data'])) {
+			$draft = ('yes' == $control[0]['child'][SIMPLEPIE_NAMESPACE_ATOMPUB]['draft'][0]['data']);
+		}
+		return $draft;
+	}
+
+	/**
+	 * Get the GMT timestamp of the entry
+	 *
+	 * @param string $format date() format
+	 * @return int|string|null
+	 */
+	function get_gmdate($format = 'j F Y, g:i a') {
+		return gmdate($format, $this->get_date('U'));
+	}
+
+	/**
+	 * Get the updated timestamp of the entry
+	 *
+	 * AtomPub needs the distinction between "created" and "updated".
+	 * @param string $format date() format
+	 * @return string|int|null
+	 */
+	function get_updated($format = 'j F Y, g:i a') {
+		if ($updated = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'updated')) {
+			$return = date('Y-m-d H:i:s', $updated[0]['data']);
+		}
+		else {
+			return null;
+		}
+
+		if ($return) {
+			$parser = SimplePie_Parse_Date::get();
+			$parsed = $parser->parse($return);
+			$date_format = (string) $date_format;
+			switch ($date_format) {
+				case '':
+					return $this->sanitize($return, SIMPLEPIE_CONSTRUCT_TEXT);
+
+				case 'U':
+					return $parsed;
+
+				default:
+					return date($date_format, $parsed);
+			}
+		}
+		else {
+			return null;
+		}
+	}
+
+	/**
+	 * Get the updated GMT timestamp of the entry
+	 *
+	 * @param string $format date() format
+	 * @return int|string|null
+	 */
+	function get_gmupdated($format = 'j F Y, g:i a') {
+		return gmdate($format, $this->get_updated('U'));
+	}
+}
+
+/**
  * WordPress AtomPub API implementation.
  *
  * @package WordPress
@@ -258,8 +344,6 @@
 	 * @since 2.2.0
 	 */
 	function handle_request() {
-		global $always_authenticate;
-
 		if ( !empty( $_SERVER['ORIG_PATH_INFO'] ) )
 			$path = $_SERVER['ORIG_PATH_INFO'];
 		else
@@ -294,8 +378,7 @@
 					// authenticate regardless of the operation and set the current
 					// user. each handler will decide if auth is required or not.
 					if ( !$this->authenticate() ) {
-						if ( $always_authenticate )
-							$this->auth_required('Credentials required.');
+						$this->auth_required('Credentials required.');
 					}
 
 					array_shift($matches);
@@ -390,17 +473,14 @@
 		global $user_ID;
 		$this->get_accepted_content_type($this->atom_content_types);
 
-		$parser = new AtomParser();
-		if ( !$parser->parse() )
-			$this->client_error();
+		$feed = $this->get_parser();
+		$entry = $feed->get_item(0);
 
-		$entry = array_pop($parser->feed->entries);
+		log_app('Received entry:', $entry->get_title());
 
-		log_app('Received entry:', print_r($entry,true));
-
 		$catnames = array();
-		foreach ( $entry->categories as $cat ) {
-			array_push($catnames, $cat["term"]);
+		foreach ( (array) $entry->get_categories() as $category ) {
+			array_push($catnames, $category->get_term());
 		}
 
 		$wp_cats = get_categories(array('hide_empty' => false));
@@ -412,7 +492,7 @@
 				array_push($post_category, $cat->term_id);
 		}
 
-		$publish = ! ( isset( $entry->draft ) && 'yes' == trim( $entry->draft ) );
+		$publish = !$entry->get_draft_status();
 
 		$cap = ($publish) ? 'publish_posts' : 'edit_posts';
 
@@ -422,12 +502,11 @@
 		$blog_ID = get_current_blog_id();
 		$post_status = ($publish) ? 'publish' : 'draft';
 		$post_author = (int) $user_ID;
-		$post_title = $entry->title[1];
-		$post_content = $entry->content[1];
-		$post_excerpt = $entry->summary[1];
-		$pubtimes = $this->get_publish_time($entry->published);
-		$post_date = $pubtimes[0];
-		$post_date_gmt = $pubtimes[1];
+		$post_title = $entry->get_title();
+		$post_content = $entry->get_content();
+		$post_excerpt = $entry->get_description();
+		$post_date = $entry->get_date('Y-m-d H:i:s');
+		$post_date_gmt = $entry->get_gmdate('Y-m-d H:i:s');
 
 		if ( isset( $_SERVER['HTTP_SLUG'] ) )
 			$post_name = $_SERVER['HTTP_SLUG'];
@@ -490,14 +569,11 @@
 		// quick check and exit
 		$this->get_accepted_content_type($this->atom_content_types);
 
-		$parser = new AtomParser();
-		if ( !$parser->parse() )
-			$this->bad_request();
+		$feed = $this->get_parser();
+		$parsed = $feed->get_item(0);
 
-		$parsed = array_pop($parser->feed->entries);
+		log_app('Received UPDATED entry:', $parsed->get_title());
 
-		log_app('Received UPDATED entry:', print_r($parsed,true));
-
 		// check for not found
 		global $entry;
 		$this->set_current_entry($postID);
@@ -505,21 +581,21 @@
 		if ( !current_user_can('edit_post', $entry['ID']) )
 			$this->auth_required(__('Sorry, you do not have the right to edit this post.'));
 
-		$publish = ! ( isset($parsed->draft) && 'yes' == trim($parsed->draft) );
+		$publish = !$parsed->get_draft_status();
 		$post_status = ($publish) ? 'publish' : 'draft';
 
 		extract($entry);
 
-		$post_title = $parsed->title[1];
-		$post_content = $parsed->content[1];
-		$post_excerpt = $parsed->summary[1];
-		$pubtimes = $this->get_publish_time($entry->published);
-		$post_date = $pubtimes[0];
-		$post_date_gmt = $pubtimes[1];
-		$pubtimes = $this->get_publish_time($parsed->updated);
-		$post_modified = $pubtimes[0];
-		$post_modified_gmt = $pubtimes[1];
+		$post_title = $parsed->get_title();
+		$post_content = $parsed->get_content();
+		$post_excerpt = $parsed->get_description();
 
+		$post_date = $parsed->get_date('Y-m-d H:i:s');
+		$post_date_gmt = $parsed->get_gmdate('Y-m-d H:i:s');
+
+		$post_modified =  $parsed->get_updated('Y-m-d H:i:s');
+		$post_modified_gmt =  $parsed->get_gmupdated('Y-m-d H:i:s');
+
 		$postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'post_date', 'post_date_gmt', 'post_modified', 'post_modified_gmt');
 		$this->escape($postdata);
 
@@ -657,13 +733,9 @@
 		// quick check and exit
 		$this->get_accepted_content_type($this->atom_content_types);
 
-		$parser = new AtomParser();
-		if (!$parser->parse()) {
-			$this->bad_request();
-		}
+		$feed = $this->get_parser();
+		$parsed = $feed->get_item(0);
 
-		$parsed = array_pop($parser->feed->entries);
-
 		// check for not found
 		global $entry;
 		$this->set_current_entry($postID);
@@ -673,11 +745,10 @@
 
 		extract($entry);
 
-		$post_title = $parsed->title[1];
-		$post_content = $parsed->summary[1];
-		$pubtimes = $this->get_publish_time($parsed->updated);
-		$post_modified = $pubtimes[0];
-		$post_modified_gmt = $pubtimes[1];
+		$post_title = $parsed->get_title();
+		$post_content = $parsed->get_description();
+		$post_modified = $parsed->get_date('Y-m-d H:i:s');
+		$post_modified_gmt = $parsed->get_gmdate('Y-m-d H:i:s');
 
 		$postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'post_modified', 'post_modified_gmt');
 		$this->escape($postdata);
@@ -1232,6 +1303,7 @@
 		log_app('Status','400: Bad Request');
 		header('Content-Type: text/plain');
 		status_header('400');
+		echo $msg;
 		exit;
 	}
 
@@ -1336,6 +1408,7 @@
 		log_app('Status','400: Client Error');
 		header('Content-Type: text/plain');
 		status_header('400');
+		echo $msg;
 		exit;
 	}
 
@@ -1448,6 +1521,8 @@
 	 * @return bool
 	 */
 	function authenticate() {
+		global $always_authenticate;
+
 		log_app("authenticate()",print_r($_ENV, true));
 
 		// if using mod_rewrite/ENV hack
@@ -1474,6 +1549,12 @@
 			}
 		}
 
+		// If we're forcing admin abilities
+		if (!$always_authenticate) {
+			wp_set_current_user(1);
+			return true;
+		}
+
 		return false;
 	}
 
@@ -1598,6 +1679,27 @@
 		}
 	}
 
+	/**
+	 * Create a SimplePie parser with POST data
+	 *
+	 * @return SimplePie
+	 */
+	function &get_parser() {
+		$data = file_get_contents('php://input');
+		// SimplePie expects the feed element to be the top element
+		// This could probably be improved
+		if (strpos($data, '<feed') === false) {
+			$data = str_replace('<entry', '<feed xmlns="' . SIMPLEPIE_NAMESPACE_ATOM_10 . '"><entry', $data);
+			$data = str_replace('</entry>', '</entry></feed>', $data);
+		}
+		$feed = new SimplePie();
+		$feed->set_item_class('SimplePieAtomPub_Item');
+		$feed->set_raw_data($data);
+		$feed->init();
+		if ( $feed->error() )
+			$this->bad_request($feed->error());
+		return $feed;
+	}
 }
 
 /**
