### Eclipse Workspace Patch 1.0
#P wordpress-trunk bare
Index: app.php
===================================================================
--- app.php	(revision 4794)
+++ app.php	(working copy)
@@ -1,31 +1,37 @@
 <?php
-/* 
- * app.php - Atom Publishing Protocol support for WordPress
- * Original code by: Elias Torres, http://torrez.us/archives/2006/08/31/491/
- * Modified by: Dougal Campbell, http://dougal.gunters.org/
- *
- * Version: 1.0.0-dc
- */
-	
-define('APP_REQUEST', true);
+// vim:sw=2:sts=2:ts=2:noet:sta:tw=200
+/*
+An Atom Publishing Protocol implementation for WordPress.
+Author: Elias Torres <elias@torrez.us>
+*/ 
 
-require_once('wp-config.php');
+/*
 
-$use_querystring = 1;
+  Copyright 2006 Elias Torres <elias@torrez.us>
 
-// If using querystring, we need to put the path together manually:
-if ($use_querystring) {
-	$_GLOBALS['use_querystring'] = $use_querystring;
-	$action = $_GET['action'];
-	$eid = (int) $_GET['eid'];
+  This program is free software; you can redistribute it and/or modify 
+  it under the terms of the GNU General Public License as published by 
+  the Free Software Foundation; either version 2 of the License, or 
+  (at your option) any later version. 
 
-	$_SERVER['PATH_INFO'] = $action;
+  This program is distributed in the hope that it will be useful, 
+  but WITHOUT ANY WARRANTY; without even the implied warranty of 
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
+  GNU General Public License for more details. 
+
+  You should have received a copy of the GNU General Public License 
+  along with this program; if not, write to the Free Software 
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
+
+*/
 	
-	if ($eid) {
-		$_SERVER['PATH_INFO'] .= "/$eid";
-	}
-} else {
-	$_SERVER['PATH_INFO'] = str_replace( '/app.php', '', $_SERVER['REQUEST_URI'] );
+define('APP_REQUEST', true);
+
+require_once('wp-config.php');
+
+if (!isset($PATH_INFO)) {
+	$PATH_INFO = substr($_SERVER['REQUEST_URI'],strlen($_SERVER['SCRIPT_NAME']), 
+		strlen($_SERVER['REQUEST_URI']));
 }
 
 $app_logging = 1;
@@ -91,7 +97,7 @@
 		global $app_logging;
 		array_unshift($this->ns_contexts, array());
 
-		$parser = xml_parser_create_ns();
+		$parser = xml_parser_create_ns("UTF-8");
 		xml_set_object($parser, $this);
 		xml_set_element_handler($parser, "start_element", "end_element");
 		xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
@@ -109,12 +115,9 @@
 		 
 			if($app_logging) $contents .= $line;
 
-			if(!xml_parse($parser, $line)) {
+			if(!xml_parse($parser, $line, feof($fp))) {
 				log_app("xml_parse_error", "line: $line");
-				$this->error = sprintf("XML error: %s at line %d\n",
-					xml_error_string(xml_get_error_code($xml_parser)),
-					xml_get_current_line_number($xml_parser));
-				log_app("xml_parse_error", $this->error);
+				xml_parser_free($parser);
 				return false;
 			}
 		}
@@ -290,7 +293,7 @@
 				array('GET' => 'get_posts', 
 						'POST' => 'create_post'),
 			'@/attachments/?(\d+)?@' => 
-				array('GET' => 'get_attachment', 
+				array('GET' => 'get_attachments', 
 						'POST' => 'create_attachment'),
 			'@/attachment/file/(\d+)@' => 
 				array('GET' => 'get_file', 
@@ -308,10 +311,8 @@
 		$path = $_SERVER['PATH_INFO'];
 		$method = $_SERVER['REQUEST_METHOD'];
 
-		log_app('REQUEST',"$method $path\n================");
+		$this->process_conditionals();
 
-		//$this->process_conditionals();
-
 		// exception case for HEAD (treat exactly as GET, but don't output)
 		if($method == 'HEAD') {
 			$this->do_output = false;
@@ -319,7 +320,7 @@
 		}
 
 		// lame. 
-		if(strlen($path) == 0 || $path == '/') {
+		if(strlen($path) == 0) {
 			$path = '/service';
 		}
 
@@ -346,18 +347,17 @@
 	}
 
 	function get_service() {
-		log_app('function','get_service()');
 		$entries_url = $this->get_entries_url();
 		$media_url = $this->get_attachments_url();
 		$accepted_content_types = join(',',$this->media_content_types);
 		$introspection = <<<EOD
 <service xmlns="http://purl.org/atom/app#" xmlns:atom="http://www.w3.org/2005/Atom">
 	<workspace title="WordPress Experimental Workspace">
-		<collection href="$entries_url" title="WordPress Posts">
+		<collection href="$entries_url">
 			<accept>entry</accept>
 			<atom:title>WordPress Posts</atom:title>
 		</collection>
-		<collection href="$media_url" title="WordPress Media">
+		<collection href="$media_url">
 			<accept>$accepted_content_types</accept>
 			<atom:title>WordPress Media</atom:title>
 		</collection>
@@ -394,11 +394,10 @@
 		$post_author = $user->ID;
 		$post_title = $entry->title;
 		$post_content = $entry->content;
-		$post_excerpt = $entry->summary;
 		$post_date = current_time('mysql');
 		$post_date_gmt = current_time('mysql', 1);
 
-		$post_data = compact('blog_ID', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt');
+		$post_data = compact('blog_ID', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status');
 
 		$postID = wp_insert_post($post_data);
 
@@ -408,7 +407,6 @@
 
 		$output = $this->get_entry($postID);
 
-		log_app('function',"create_post($postID)");
 		$this->created($postID, $output);
 	}
 
@@ -417,7 +415,6 @@
 		global $entry;
 		$this->set_current_entry($postID);
 		$output = $this->get_entry($postID);
-		log_app('function',"get_post($postID)");
 		$this->output($output);
 
 	}
@@ -449,7 +446,6 @@
 
 		$post_title = $parsed->title;
 		$post_content = $parsed->content;
-		$post_excerpt = $parsed->summary;
 
 		// let's not go backwards and make something draft again.
 		if(!$publish && $post_status == 'draft') {
@@ -464,7 +460,6 @@
 			$this->internal_error('For some strange yet very annoying reason, this post could not be edited.');
 		}
 
-		log_app('function',"put_post($postID)");
 		$this->ok();
 	}
 
@@ -477,35 +472,16 @@
 		if(!current_user_can('edit_post', $postID)) {
 			$this->auth_required('Sorry, you do not have the right to delete this post.');
 		}
-		
-		if ($entry['post_type'] == 'attachment') {
-			$this->delete_attachment($postID);		
-		} else {
-			$result = wp_delete_post($postID);
 
+		$result = wp_delete_post($postID);
+
 			if (!$result) {
 				$this->internal_error('For some strange yet very annoying reason, this post could not be deleted.');
 			}
 
-			log_app('function',"delete_post($postID)");
 			$this->ok();
-		}
-
 	}
-	
-	function get_attachment($postID = NULL) {
 
-		global $entry;
-		if (!isset($postID)) {
-			$this->get_attachments();
-		} else {
-			$this->set_current_entry($postID);
-			$output = $this->get_entry($postID, 'attachment');
-			log_app('function',"get_attachment($postID)");
-			$this->output($output);
-		}
-	}
-
 	function create_attachment() {
 
 		$type = $this->get_accepted_content_type();
@@ -519,28 +495,17 @@
 			$bits .= fread($fp, 4096);
 		}
 		fclose($fp);
-		
-		$slug = '';
-		if ( isset( $_SERVER['HTTP_SLUG'] ) )
-			$slug = sanitize_file_name( $_SERVER['HTTP_SLUG'] );
-		elseif ( isset( $_SERVER['HTTP_TITLE'] ) )
-			$slug = sanitize_file_name( $_SERVER['HTTP_TITLE'] );
-		elseif ( empty( $slug ) ) // just make a random name
-			$slug = substr( md5( uniqid( microtime() ) ), 0, 7);;
-		$ext = preg_replace( '|.*/([a-z]+)|', '$1', $_SERVER['CONTENT_TYPE'] );
-		$slug = "$slug.$ext";
-		$file = wp_upload_bits( $slug, NULL, $bits);
 
+		$file = wp_upload_bits('noah.jpg',NULL,$bits);
+
 		$url = $file['url'];
 		$file = $file['file'];
 		$filename = basename($file);
 
-		$header = apply_filters('wp_create_file_in_uploads', $file); // replicate
-
 		// Construct the attachment array
 		$attachment = array(
-			'post_title' => $slug,
-			'post_content' => $slug,
+			'post_title' => $imgtitle ? $imgtitle : $filename,
+			'post_content' => $descr,
 			'post_status' => 'attachment',
 			'post_parent' => 0,
 			'post_mime_type' => $type,
@@ -557,7 +522,6 @@
 		$output = $this->get_entry($postID, 'attachment');
 
 		$this->created($postID, $output, 'attachment');
-		log_app('function',"create_attachment($postID)");
 	}
 
 	function put_attachment($postID) {
@@ -596,12 +560,10 @@
 			$this->internal_error('For some strange yet very annoying reason, this post could not be edited.');
 		}
 
-		log_app('function',"put_attachment($postID)");
 		$this->ok();
 	}
 
 	function delete_attachment($postID) {
-		log_app('function',"delete_attachment($postID). File '$location' deleted.");
 
 		// check for not found
 		global $entry;
@@ -623,7 +585,6 @@
 			$this->internal_error('For some strange yet very annoying reason, this post could not be deleted.');
 		}
 
-		log_app('function',"delete_attachment($postID). File '$location' deleted.");
 		$this->ok();
 	}
 
@@ -651,7 +612,6 @@
 		}
 		fclose($fp);
 
-		log_app('function',"get_file($postID)");
 		$this->ok();
 	}
 
@@ -685,24 +645,15 @@
 		fclose($fp);
 		fclose($localfp);
 	
-		log_app('function',"put_file($postID)");
 		$this->ok();
 	}
 
 	function get_entries_url($page = NULL) {
-		global $use_querystring;
 		$url = get_bloginfo('url') . '/' . $this->script_name;
-		if ($use_querystring) {
-			$url .= '?action=/' . $this->ENTRIES_PATH;
-			if(isset($page) && is_int($page)) {
-				$url .= "&eid=$page";
-			}
-		} else {
 			$url .= '/' . $this->ENTRIES_PATH;
 			if(isset($page) && is_int($page)) {
 				$url .= "/$page";
 			}
-		}
 		return $url;
 	}
 
@@ -712,19 +663,11 @@
 	}
 
 	function get_attachments_url($page = NULL) {
-		global $use_querystring;
 		$url = get_bloginfo('url') . '/' . $this->script_name;
-		if ($use_querystring) {
-			$url .= '?action=/' . $this->MEDIA_PATH;
-			if(isset($page) && is_int($page)) {
-				$url .= "&eid=$page";
-			}
-		} else {
 			$url .= '/' . $this->MEDIA_PATH;
 			if(isset($page) && is_int($page)) {
 				$url .= "/$page";
 			}
-		}
 		return $url;
 	}
 
@@ -735,19 +678,12 @@
 
 
 	function get_entry_url($postID = NULL) {
-		global $use_querystring;
 		if(!isset($postID)) {
 			global $post;
 			$postID = $post->ID;
 		}
-		
-		if ($use_querystring) {
-			$url = get_bloginfo('url') . '/' . $this->script_name . '?action=/' . $this->ENTRY_PATH . "&eid=$postID";
-		} else {
-			$url = get_bloginfo('url') . '/' . $this->script_name . '/' . $this->ENTRY_PATH . "/$postID";
-		}
 
-		log_app('function',"get_entry_url() = $url");
+		$url = get_bloginfo('url') . '/' . $this->script_name . '/' . $this->ENTRY_PATH . "/$postID";
 		return $url;
 	}
 
@@ -757,19 +693,12 @@
 	}
 
 	function get_media_url($postID = NULL) {
-		global $use_querystring;
 		if(!isset($postID)) {
 			global $post;
 			$postID = $post->ID;
 		}
-		
-		if ($use_querystring) {
-			$url = get_bloginfo('url') . '/' . $this->script_name . '?action=/' . $this->MEDIA_SINGLE_PATH ."&eid=$postID";
-		} else {
-			$url = get_bloginfo('url') . '/' . $this->script_name . '/' . $this->MEDIA_SINGLE_PATH ."/$postID";
-		}
 
-		log_app('function',"get_media_url() = $url");
+			$url = get_bloginfo('url') . '/' . $this->script_name . '/' . $this->MEDIA_SINGLE_PATH ."/$postID";
 		return $url;
 	}
 
@@ -780,11 +709,9 @@
 
 	function set_current_entry($postID) {
 		global $entry;
-		log_app('function',"set_current_entry($postID)");
 
 		if(!isset($postID)) {
-			// $this->bad_request();
-			$this->not_found();
+			$this->bad_request();
 		}
 
 		$entry = wp_get_single_post($postID,ARRAY_A);
@@ -797,25 +724,17 @@
 
 	function get_posts_count() {
 		global $wpdb;
-		log_app('function',"get_posts_count()");
 		return $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_date_gmt < '" . gmdate("Y-m-d H:i:s",time()) . "'");
 	}
 
 
-	function get_posts($page = 1, $post_type = 'post') {
-			log_app('function',"get_posts($page, '$post_type')");
-			$feed = $this->get_feed($page, $post_type);
-			$this->output($feed);
+	function get_posts($page = 1) {
+			$feed = $this->get_feed($page);
+			$this->output($feed, $do_output);
 	}
 
-	function get_attachments($page = 1, $post_type = 'attachment') {
-			log_app('function',"get_attachments($page, '$post_type')");
-			$feed = $this->get_feed($page, $post_type);
-			$this->output($feed);
-	}
-
 	function get_feed($page = 1, $post_type = 'post') {
-		log_app('function',"get_feed($page, '$post_type')");
+		log_app("get_feed", $post_type);
 		ob_start();
 
 		if(!isset($page)) {
@@ -826,19 +745,20 @@
 		$count = get_settings('posts_per_rss');	
 		$query = "paged=$page&posts_per_page=$count&order=DESC";
 		if($post_type == 'attachment') {
-			$query .= "&post_type=$post_type";
+			$query .= "&show_post_type=$post_type";
 		}
 		query_posts($query);
 		global $post;
 
+		$feed_update = get_lastpostmodified('GMT') ? get_lastpostmodified('GMT') : current_time('mysql', 1);
 		$total_count = $this->get_posts_count();
 		$last_page = (int) ceil($total_count / $count);
 		$next_page = (($page + 1) > $last_page) ? NULL : $page + 1;
 		$prev_page = ($page - 1) < 1 ? NULL : $page - 1; 
-		$last_page = ((int)$last_page == 1 || (int)$last_page == 0) ? NULL : (int) $last_page;
+		$last_page = ((int)$last_page <= 1) ? NULL : (int) $last_page;
 ?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:app="http://purl.org/atom/app#" xml:lang="<?php echo get_option('rss_language'); ?>">
 <id><?php $this->the_entries_url() ?></id>
-<updated><?php echo mysql2date('Y-m-d\TH:i:s\Z', get_lastpostmodified('GMT')); ?></updated>
+<updated><?php echo mysql2date('Y-m-d\TH:i:s\Z', $feed_update); ?></updated>
 <title type="text"><?php bloginfo_rss('name') ?></title>
 <subtitle type="text"><?php bloginfo_rss("description") ?></subtitle>
 <link rel="first" type="application/atom+xml" href="<?php $this->the_entries_url() ?>" />
@@ -851,11 +771,11 @@
 <link rel="last" type="application/atom+xml" href="<?php $this->the_entries_url($last_page) ?>" />
 <link rel="self" type="application/atom+xml" href="<?php $this->the_entries_url() ?>" />
 <rights type="text">Copyright <?php echo mysql2date('Y', get_lastpostdate('blog')); ?></rights>
-<generator uri="http://wordpress.com/" version="1.0.0-dc">WordPress.com Atom API</generator>
+<generator uri="http://wordpress.org/" version="<?php bloginfo_rss('version'); ?>">WordPress APP</generator>
 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
 <entry>
 		<id><?php the_guid(); ?></id>
-		<title type="html"><![CDATA[<?php the_title() ?>]]></title>
+		<title type="html"><![CDATA[<?php the_title_rss() ?>]]></title>
 		<updated><?php echo get_post_modified_time('Y-m-d\TH:i:s\Z', true); ?></updated>
 		<published><?php echo get_post_time('Y-m-d\TH:i:s\Z', true); ?></published>
 		<app:control>
@@ -878,9 +798,6 @@
 	<?php foreach(get_the_category() as $category) { ?>
 	 <category scheme="<?php bloginfo_rss('home') ?>" term="<?php echo $category->cat_name?>" />
 	<?php } ?>   <summary type="html"><![CDATA[<?php the_excerpt_rss(); ?>]]></summary>
-	<?php if ( strlen( $post->post_content ) ) : ?>
-	<content type="html"><?php echo get_the_content('', 0, '') ?></content>
-<?php endif; ?>
 	</entry>
 <?php
 	endwhile; 
@@ -889,13 +806,13 @@
 <?php 
 		$feed = ob_get_contents();
 		ob_end_clean();
+		log_app("get_feed", $feed);
 		return $feed;
 	}
 
 	function get_entry($postID, $post_type = 'post') {
-		log_app('function',"get_entry($postID, '$post_type')");
 		ob_start();
-		global $posts, $post, $wp_query;
+		global $post;
 		switch($post_type) {
 			case 'post':
 				$varname = 'p';
@@ -909,7 +826,6 @@
 <entry xmlns="http://www.w3.org/2005/Atom" xmlns:app="http://purl.org/atom/app#" xml:lang="<?php echo get_option('rss_language'); ?>">
 	<id><?php the_guid(); ?></id>
 	<title type="html"><![CDATA[<?php the_title_rss() ?>]]></title>
-
 	<updated><?php echo get_post_modified_time('Y-m-d\TH:i:s\Z', true); ?></updated>
 	<published><?php echo get_post_time('Y-m-d\TH:i:s\Z', true); ?></published>
 	<app:control>
@@ -920,7 +836,7 @@
 		<email><?php the_author_email()?></email>
 		<uri><?php the_author_url()?></uri>
 	</author>
-<?php if($post->post_type == 'attachment') { ?>
+<?php if($post->post_status == 'attachment') { ?>
 	<link rel="edit" href="<?php $this->the_entry_url() ?>" />
 	<link rel="edit-media" href="<?php $this->the_media_url() ?>" />
 	<content type="<?php echo $post->post_mime_type ?>" src="<?php the_guid(); ?>"/>
@@ -933,7 +849,7 @@
 	<summary type="html"><![CDATA[<?php the_excerpt_rss(); ?>]]></summary>
 <?php }
 	if ( strlen( $post->post_content ) ) : ?>
-	<content type="html"><?php echo get_the_content('', 0, '') ?></content>
+	<content type="html"><![CDATA[<?php echo get_the_content('', 0, '') ?>]]></content>
 <?php endif; ?>
 </entry>
 <?php
@@ -945,27 +861,17 @@
 		endif;
 		ob_end_clean();
 
-		log_app('get_entry returning:',$entry);
+		log_app("get_entry", $entry);
 		return $entry; 
 	}
 
 	function ok() { 
-		log_app('Status','200: OK');
 		header('Content-Type: text/plain');
 		status_header('200');
 		exit;
 	}
 
-	function no_content() { 
-		log_app('Status','204: No Content');
-		header('Content-Type: text/plain');
-		status_header('204');
-		echo "Deleted.";
-		exit;
-	}
-
 	function internal_error($msg = 'Internal Server Error') {
-		log_app('Status','500: Server Error');
 		header('Content-Type: text/plain');
 		status_header('500');
 		echo $msg;
@@ -973,14 +879,12 @@
 	}
 
 	function bad_request() {
-		log_app('Status','400: Bad Request');
 		header('Content-Type: text/plain');
 		status_header('400');
 		exit;
 	}
 
 	function length_required() {
-		log_app('Status','411: Length Required');
 		header("HTTP/1.1 411 Length Required");
 		header('Content-Type: text/plain');
 		status_header('411');
@@ -988,47 +892,39 @@
 	}
 
 	function invalid_media() {
-		log_app('Status','415: Unsupported Media Type');
 		header("HTTP/1.1 415 Unsupported Media Type");
 		header('Content-Type: text/plain');
 		exit;
 	}
 	
 	function not_found() {
-		log_app('Status','404: Not Found');
 		header('Content-Type: text/plain');
 		status_header('404');
 		exit;
 	}
 
 	function not_allowed($allow) {
-		log_app('Status','405: Not Allowed');
 		header('Allow: ' . join(',', $allow));
 		status_header('405');
 		exit;
 	}
 
 	function client_error($msg = 'Client Error') {
-		log_app('Status','400: Client Errir');
-		header('Content-Type: text/plain');
 		status_header('400');
+		header('Content-Type: text/plain');
+		header('Status: ' . $msg);
+		echo $msg;
 		exit;
 	}
 	
 	function created($post_ID, $content, $post_type = 'post') {
-		global $use_querystring;
-		log_app('created()::$post_ID',"$post_ID, $post_type");
+		log_app('created()::$post_ID',"$post_ID");
 		$edit = $this->get_entry_url($post_ID);
 		switch($post_type) {
 			case 'post':
 				$ctloc = $this->get_entry_url($post_ID);
 				break;
 			case 'attachment':
-				if ($use_querystring) {
-					$edit = get_bloginfo('url') . '/' . $this->script_name . "?action=/attachments&eid=$post_ID";
-				} else {
-					$edit = get_bloginfo('url') . '/' . $this->script_name . "/attachments/$post_ID";
-				}
 				break;
 		}
 		header('Content-Type: application/atom+xml');
@@ -1041,13 +937,11 @@
 	}
 
 	function auth_required($msg) {
-		log_app('Status','401: Auth Required');
 		nocache_headers();
 		header('WWW-Authenticate: Basic realm="WordPress Atom Protocol"');
-		header('WWW-Authenticate: Form action="' . get_settings('siteurl') . '/wp-login.php"', false); 
 		header("HTTP/1.1 401 $msg");
 		header('Status: ' . $msg);
-		header('Content-Type: plain/text');
+		header('Content-Type: text/plain');
 		echo $msg;
 		exit;
 	}
@@ -1062,7 +956,6 @@
 			header('Date: '. date('r'));
 			if($this->do_output)
 				echo $xml;
-			log_app('function', "output:\n$xml");
 			exit;
 	}
 
@@ -1081,7 +974,6 @@
 	}
 
 
-
 	/*
 	 * Access credential through various methods and perform login
 	 */
@@ -1113,7 +1005,6 @@
 		if (!empty($login_data) && wp_login($login_data['login'], $login_data['password'], $already_md5)) {
 			 $current_user = new WP_User(0, $login_data['login']);
 			 wp_set_current_user($current_user->ID);
-			log_app("authenticate()",$login_data['login']);
 		}
 	}
 
