4 * WordPress AtomPub API implementation.
7 * @subpackage Publishing
10 class wp_atom_server {
18 var $ATOM_CONTENT_TYPE = 'application/atom+xml';
21 * Categories ATOM content type.
26 var $CATEGORIES_CONTENT_TYPE = 'application/atomcat+xml';
29 * Service ATOM content type.
34 var $SERVICE_CONTENT_TYPE = 'application/atomsvc+xml';
42 var $ATOM_NS = 'http://www.w3.org/2005/Atom';
45 * ATOMPUB XML namespace.
50 var $ATOMPUB_NS = 'http://www.w3.org/2007/app';
58 var $ENTRIES_PATH = "posts";
66 var $CATEGORIES_PATH = "categories";
74 var $MEDIA_PATH = "attachments";
82 var $ENTRY_PATH = "post";
90 var $SERVICE_PATH = "service";
98 var $MEDIA_SINGLE_PATH = "attachment";
101 * ATOMPUB parameters.
106 var $params = array();
109 * Supported ATOMPUB media types.
114 var $media_content_types = array('image/*','audio/*','video/*');
117 * ATOMPUB content type(s).
122 var $atom_content_types = array('application/atom+xml');
130 var $selectors = array();
133 * Whether to do output.
140 var $do_output = true;
143 * Constructor - Sets up object properties.
148 function __construct() {
150 $var_by_ref = explode( '/', $_SERVER['SCRIPT_NAME'] );
151 $this->script_name = array_pop( $var_by_ref );
152 $this->app_base = site_url( $this->script_name . '/' );
154 $this->selectors = array(
156 array('GET' => 'get_service'),
158 array('GET' => 'get_categories_xml'),
160 array('GET' => 'get_post',
162 'DELETE' => 'delete_post'),
163 '@/posts/?(\d+)?$@' =>
164 array('GET' => 'get_posts',
165 'POST' => 'create_post'),
166 '@/attachments/?(\d+)?$@' =>
167 array('GET' => 'get_attachment',
168 'POST' => 'create_attachment'),
169 '@/attachment/file/(\d+)$@' =>
170 array('GET' => 'get_file',
172 'DELETE' => 'delete_file'),
173 '@/attachment/(\d+)$@' =>
174 array('GET' => 'get_attachment',
175 'PUT' => 'put_attachment',
176 'DELETE' => 'delete_attachment'),
181 * Handle ATOMPUB request.
185 function handle_request() {
187 if ( !empty( $_SERVER['ORIG_PATH_INFO'] ) )
188 $path = $_SERVER['ORIG_PATH_INFO'];
190 $path = $_SERVER['PATH_INFO'];
192 $method = $_SERVER['REQUEST_METHOD'];
194 $this->process_conditionals();
195 //$this->process_conditionals();
197 // exception case for HEAD (treat exactly as GET, but don't output)
198 if ($method == 'HEAD') {
199 $this->do_output = false;
203 // redirect to /service in case no path is found.
204 if (strlen($path) == 0 || $path == '/')
205 $this->redirect($this->get_service_url());
207 // check to see if AtomPub is enabled
208 if ( !get_option( 'enable_app' ) )
209 $this->forbidden( sprintf( __( 'AtomPub services are disabled on this site. An admin user can enable them at %s' ), admin_url('options-writing.php') ) );
212 foreach ( $this->selectors as $regex => $funcs ) {
213 if ( preg_match($regex, $path, $matches) ) {
214 if ( isset($funcs[$method]) ) {
216 // authenticate regardless of the operation and set the current
217 // user. each handler will decide if auth is required or not.
218 if ( !$this->authenticate() ) {
219 $this->auth_required('Credentials required.');
222 array_shift($matches);
223 call_user_func_array(array($this,$funcs[$method]), $matches);
226 // only allow what we have handlers for...
227 $this->not_allowed(array_keys($funcs));
232 // oops, nothing found
237 * Retrieve XML for ATOMPUB service.
241 function get_service() {
243 if ( !current_user_can( 'edit_posts' ) )
244 $this->auth_required( __( 'Sorry, you do not have the right to access this site.' ) );
246 $entries_url = esc_attr($this->get_entries_url());
247 $categories_url = esc_attr($this->get_categories_url());
248 $media_url = esc_attr($this->get_attachments_url());
249 $accepted_media_types = '';
250 foreach ($this->media_content_types as $med) {
251 $accepted_media_types = $accepted_media_types . "<accept>" . $med . "</accept>";
254 $atom_blogname = get_bloginfo('name');
255 $service_doc = <<<EOD
256 <service xmlns="$this->ATOMPUB_NS" xmlns:$atom_prefix="$this->ATOM_NS">
258 <$atom_prefix:title>$atom_blogname Workspace</$atom_prefix:title>
259 <collection href="$entries_url">
260 <$atom_prefix:title>$atom_blogname Posts</$atom_prefix:title>
261 <accept>$this->ATOM_CONTENT_TYPE;type=entry</accept>
262 <categories href="$categories_url" />
264 <collection href="$media_url">
265 <$atom_prefix:title>$atom_blogname Media</$atom_prefix:title>
266 $accepted_media_types
273 $this->output($service_doc, $this->SERVICE_CONTENT_TYPE);
277 * Retrieve categories list in XML format.
281 function get_categories_xml() {
283 if ( !current_user_can( 'edit_posts' ) )
284 $this->auth_required( __( 'Sorry, you do not have the right to access this site.' ) );
286 $home = esc_attr(get_bloginfo_rss('url'));
289 $cats = get_categories(array('hierarchical' => 0, 'hide_empty' => 0));
290 foreach ( (array) $cats as $cat ) {
291 $categories .= " <category term=\"" . esc_attr($cat->name) . "\" />\n";
294 <app:categories xmlns:app="$this->ATOMPUB_NS"
295 xmlns="$this->ATOM_NS"
296 fixed="yes" scheme="$home">
300 $this->output($output, $this->CATEGORIES_CONTENT_TYPE);
308 function create_post() {
310 $this->get_accepted_content_type($this->atom_content_types);
312 $parser = new AtomParser();
313 if ( !$parser->parse() )
314 $this->client_error();
316 $entry = array_pop($parser->feed->entries);
319 if ( !empty( $entry->categories ) ) {
320 foreach ( $entry->categories as $cat ) {
321 array_push($catnames, $cat["term"]);
325 $wp_cats = get_categories(array('hide_empty' => false));
327 $post_category = array();
329 foreach ( $wp_cats as $cat ) {
330 if ( in_array($cat->name, $catnames) )
331 array_push($post_category, $cat->term_id);
334 $publish = ! ( isset( $entry->draft ) && 'yes' == trim( $entry->draft ) );
336 $cap = ($publish) ? 'publish_posts' : 'edit_posts';
338 if ( !current_user_can($cap) )
339 $this->auth_required(__('Sorry, you do not have the right to edit/publish new posts.'));
341 $blog_ID = get_current_blog_id();
342 $post_status = ($publish) ? 'publish' : 'draft';
343 $post_author = (int) $user_ID;
350 if ( isset( $entry->title ) && is_array( $entry->title ) && !empty( $entry->title[1] ) )
351 $post_title = (string) $entry->title[1];
352 if ( isset( $entry->content ) && is_array( $entry->content ) && !empty( $entry->content[1] ) )
353 $post_content = (string) $entry->content[1];
354 if ( isset( $entry->summary ) && is_array( $entry->summary ) && !empty( $entry->summary[1] ) )
355 $post_excerpt = (string) $entry->summary[1];
356 if ( !empty( $entry->published ) )
357 $pubtimes = (string) $entry->published;
359 $pubtimes = $this->get_publish_time( $pubtimes );
361 $post_date = $pubtimes[0];
362 $post_date_gmt = $pubtimes[1];
364 if ( isset( $_SERVER['HTTP_SLUG'] ) )
365 $post_name = $_SERVER['HTTP_SLUG'];
367 $post_data = compact('blog_ID', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'post_name');
369 $this->escape($post_data);
371 $postID = wp_insert_post($post_data);
372 if ( is_wp_error( $postID ) )
373 $this->internal_error($postID->get_error_message());
376 $this->internal_error(__('Sorry, your entry could not be posted. Something wrong happened.'));
378 // getting warning here about unable to set headers
379 // because something in the cache is printing to the buffer
380 // could we clean up wp_set_post_categories or cache to not print
381 // this could affect our ability to send back the right headers
382 @wp_set_post_categories($postID, $post_category);
384 do_action( 'atompub_create_post', $postID, $entry );
386 $output = $this->get_entry($postID);
388 $this->created($postID, $output);
396 * @param int $postID Post ID.
398 function get_post($postID) {
401 if ( !current_user_can( 'edit_post', $postID ) )
402 $this->auth_required( __( 'Sorry, you do not have the right to access this post.' ) );
404 $this->set_current_entry($postID);
405 $output = $this->get_entry($postID);
406 $this->output($output);
415 * @param int $postID Post ID.
417 function put_post($postID) {
418 // checked for valid content-types (atom+xml)
419 // quick check and exit
420 $this->get_accepted_content_type($this->atom_content_types);
422 $parser = new AtomParser();
423 if ( !$parser->parse() )
424 $this->bad_request();
426 $parsed = array_pop($parser->feed->entries);
428 // check for not found
430 $this->set_current_entry($postID);
432 if ( !current_user_can('edit_post', $entry['ID']) )
433 $this->auth_required(__('Sorry, you do not have the right to edit this post.'));
435 $publish = ! ( isset($parsed->draft) && 'yes' == trim($parsed->draft) );
436 $post_status = ($publish) ? 'publish' : 'draft';
440 $post_title = $parsed->title[1];
441 $post_content = $parsed->content[1];
442 $post_excerpt = $parsed->summary[1];
443 $pubtimes = $this->get_publish_time($entry->published);
444 $post_date = $pubtimes[0];
445 $post_date_gmt = $pubtimes[1];
446 $pubtimes = $this->get_publish_time($parsed->updated);
447 $post_modified = $pubtimes[0];
448 $post_modified_gmt = $pubtimes[1];
450 $postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'post_date', 'post_date_gmt', 'post_modified', 'post_modified_gmt');
451 $this->escape($postdata);
453 $result = wp_update_post($postdata);
456 $this->internal_error(__('For some strange yet very annoying reason, this post could not be edited.'));
458 do_action( 'atompub_put_post', $ID, $parsed );
468 * @param int $postID Post ID.
470 function delete_post($postID) {
472 // check for not found
474 $this->set_current_entry($postID);
476 if ( !current_user_can('edit_post', $postID) )
477 $this->auth_required(__('Sorry, you do not have the right to delete this post.'));
479 if ( $entry['post_type'] == 'attachment' ) {
480 $this->delete_attachment($postID);
482 $result = wp_delete_post($postID);
485 $this->internal_error(__('For some strange yet very annoying reason, this post could not be deleted.'));
494 * Retrieve attachment.
498 * @param int $postID Optional. Post ID.
500 function get_attachment($postID = null) {
501 if ( !current_user_can( 'upload_files' ) )
502 $this->auth_required( __( 'You do not have permission to upload files.' ) );
504 if ( !isset($postID) ) {
505 $this->get_attachments();
507 $this->set_current_entry($postID);
508 $output = $this->get_entry($postID, 'attachment');
509 $this->output($output);
514 * Create new attachment.
518 function create_attachment() {
520 $type = $this->get_accepted_content_type();
522 if ( !current_user_can('upload_files') )
523 $this->auth_required( __( 'You do not have permission to upload files.' ) );
525 $fp = fopen("php://input", "rb");
527 while ( !feof($fp) ) {
528 $bits .= fread($fp, 4096);
533 if ( isset( $_SERVER['HTTP_SLUG'] ) )
534 $slug = $_SERVER['HTTP_SLUG'];
535 elseif ( isset( $_SERVER['HTTP_TITLE'] ) )
536 $slug = $_SERVER['HTTP_TITLE'];
537 elseif ( empty( $slug ) ) // just make a random name
538 $slug = substr( md5( uniqid( microtime() ) ), 0, 7);
539 $ext = preg_replace( '|.*/([a-z0-9]+)|', '$1', $_SERVER['CONTENT_TYPE'] );
540 $slug = sanitize_file_name( "$slug.$ext" );
541 $file = wp_upload_bits( $slug, null, $bits);
544 $file = $file['file'];
546 do_action('wp_create_file_in_uploads', $file); // replicate
548 // Construct the attachment array
550 'post_title' => $slug,
551 'post_content' => $slug,
552 'post_status' => 'attachment',
554 'post_mime_type' => $type,
559 $postID = wp_insert_attachment($attachment, $file);
562 $this->internal_error(__('Sorry, your entry could not be posted. Something wrong happened.'));
564 $output = $this->get_entry($postID, 'attachment');
566 $this->created($postID, $output, 'attachment');
574 * @param int $postID Post ID.
576 function put_attachment($postID) {
577 // checked for valid content-types (atom+xml)
578 // quick check and exit
579 $this->get_accepted_content_type($this->atom_content_types);
581 $parser = new AtomParser();
582 if (!$parser->parse()) {
583 $this->bad_request();
586 $parsed = array_pop($parser->feed->entries);
588 // check for not found
590 $this->set_current_entry($postID);
592 if ( !current_user_can('edit_post', $entry['ID']) )
593 $this->auth_required(__('Sorry, you do not have the right to edit this post.'));
597 $post_title = $parsed->title[1];
598 $post_content = $parsed->summary[1];
599 $pubtimes = $this->get_publish_time($parsed->updated);
600 $post_modified = $pubtimes[0];
601 $post_modified_gmt = $pubtimes[1];
603 $postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'post_modified', 'post_modified_gmt');
604 $this->escape($postdata);
606 $result = wp_update_post($postdata);
609 $this->internal_error(__('For some strange yet very annoying reason, this post could not be edited.'));
619 * @param int $postID Post ID.
621 function delete_attachment($postID) {
623 // check for not found
625 $this->set_current_entry($postID);
627 if ( !current_user_can('edit_post', $postID) )
628 $this->auth_required(__('Sorry, you do not have the right to delete this post.'));
630 $location = get_post_meta($entry['ID'], '_wp_attached_file', true);
631 $filetype = wp_check_filetype($location);
633 if ( !isset($location) || 'attachment' != $entry['post_type'] || empty($filetype['ext']) )
634 $this->internal_error(__('Error occurred while accessing post metadata for file location.'));
640 $result = wp_delete_post($postID);
643 $this->internal_error(__('For some strange yet very annoying reason, this post could not be deleted.'));
649 * Retrieve attachment from post.
653 * @param int $postID Post ID.
655 function get_file($postID) {
657 // check for not found
659 $this->set_current_entry($postID);
661 // then whether user can edit the specific post
662 if ( !current_user_can('edit_post', $postID) )
663 $this->auth_required(__('Sorry, you do not have the right to edit this post.'));
665 $location = get_post_meta($entry['ID'], '_wp_attached_file', true);
666 $location = get_option ('upload_path') . '/' . $location;
667 $filetype = wp_check_filetype($location);
669 if ( !isset($location) || 'attachment' != $entry['post_type'] || empty($filetype['ext']) )
670 $this->internal_error(__('Error occurred while accessing post metadata for file location.'));
672 status_header('200');
673 header('Content-Type: ' . $entry['post_mime_type']);
674 header('Connection: close');
676 if ( $fp = fopen($location, "rb") ) {
677 status_header('200');
678 header('Content-Type: ' . $entry['post_mime_type']);
679 header('Connection: close');
681 while ( !feof($fp) ) {
682 echo fread($fp, 4096);
687 status_header ('404');
694 * Upload file to blog and add attachment to post.
698 * @param int $postID Post ID.
700 function put_file($postID) {
702 // first check if user can upload
703 if ( !current_user_can('upload_files') )
704 $this->auth_required(__('You do not have permission to upload files.'));
706 // check for not found
708 $this->set_current_entry($postID);
710 // then whether user can edit the specific post
711 if ( !current_user_can('edit_post', $postID) )
712 $this->auth_required(__('Sorry, you do not have the right to edit this post.'));
714 $upload_dir = wp_upload_dir( );
715 $location = get_post_meta($entry['ID'], '_wp_attached_file', true);
716 $filetype = wp_check_filetype($location);
718 $location = "{$upload_dir['basedir']}/{$location}";
720 if (!isset($location) || 'attachment' != $entry['post_type'] || empty($filetype['ext']))
721 $this->internal_error(__('Error occurred while accessing post metadata for file location.'));
723 $fp = fopen("php://input", "rb");
724 $localfp = fopen($location, "w+");
725 while ( !feof($fp) ) {
726 fwrite($localfp,fread($fp, 4096));
732 $pubtimes = $this->get_publish_time($entry->published);
733 $post_date = $pubtimes[0];
734 $post_date_gmt = $pubtimes[1];
735 $pubtimes = $this->get_publish_time($parsed->updated);
736 $post_modified = $pubtimes[0];
737 $post_modified_gmt = $pubtimes[1];
739 $post_data = compact('ID', 'post_date', 'post_date_gmt', 'post_modified', 'post_modified_gmt');
740 $result = wp_update_post($post_data);
743 $this->internal_error(__('Sorry, your entry could not be posted. Something wrong happened.'));
745 wp_update_attachment_metadata( $postID, wp_generate_attachment_metadata( $postID, $location ) );
751 * Retrieve entries URL.
755 * @param int $page Page ID.
758 function get_entries_url($page = null) {
759 if ( isset($GLOBALS['post_type']) && ( $GLOBALS['post_type'] == 'attachment' ) )
760 $path = $this->MEDIA_PATH;
762 $path = $this->ENTRIES_PATH;
763 $url = $this->app_base . $path;
764 if ( isset($page) && is_int($page) )
770 * Display entries URL.
774 * @param int $page Page ID.
776 function the_entries_url($page = null) {
777 echo $this->get_entries_url($page);
781 * Retrieve categories URL.
785 * @param mixed $deprecated Not used.
788 function get_categories_url($deprecated = '') {
789 if ( !empty( $deprecated ) )
790 _deprecated_argument( __FUNCTION__, '2.5' );
791 return $this->app_base . $this->CATEGORIES_PATH;
795 * Display category URL.
799 function the_categories_url() {
800 echo $this->get_categories_url();
804 * Retrieve attachment URL.
808 * @param int $page Page ID.
811 function get_attachments_url($page = null) {
812 $url = $this->app_base . $this->MEDIA_PATH;
813 if (isset($page) && is_int($page)) {
820 * Display attachment URL.
824 * @param int $page Page ID.
826 function the_attachments_url($page = null) {
827 echo $this->get_attachments_url($page);
831 * Retrieve service URL.
837 function get_service_url() {
838 return $this->app_base . $this->SERVICE_PATH;
842 * Retrieve entry URL.
846 * @param int $postID Post ID.
849 function get_entry_url($postID = null) {
850 if (!isset($postID)) {
852 $postID = (int) $post->ID;
855 $url = $this->app_base . $this->ENTRY_PATH . "/$postID";
865 * @param int $postID Post ID.
867 function the_entry_url($postID = null) {
868 echo $this->get_entry_url($postID);
872 * Retrieve media URL.
876 * @param int $postID Post ID.
879 function get_media_url($postID = null) {
880 if (!isset($postID)) {
882 $postID = (int) $post->ID;
885 $url = $this->app_base . $this->MEDIA_SINGLE_PATH ."/file/$postID";
891 * Display the media URL.
895 * @param int $postID Post ID.
897 function the_media_url($postID = null) {
898 echo $this->get_media_url($postID);
902 * Set the current entry to post ID.
906 * @param int $postID Post ID.
908 function set_current_entry($postID) {
911 if (!isset($postID)) {
912 // $this->bad_request();
916 $entry = wp_get_single_post($postID,ARRAY_A);
918 if (!isset($entry) || !isset($entry['ID']))
929 * @param int $page Optional. Page ID.
930 * @param string $post_type Optional, default is 'post'. Post Type.
932 function get_posts($page = 1, $post_type = 'post') {
933 $feed = $this->get_feed($page, $post_type);
934 $this->output($feed);
938 * Display attachment XML.
942 * @param int $page Page ID.
943 * @param string $post_type Optional, default is 'attachment'. Post type.
945 function get_attachments($page = 1, $post_type = 'attachment') {
946 $GLOBALS['post_type'] = $post_type;
947 $feed = $this->get_feed($page, $post_type);
948 $this->output($feed);
956 * @param int $page Page ID.
957 * @param string $post_type Optional, default is post. Post type.
960 function get_feed($page = 1, $post_type = 'post') {
961 global $post, $wp, $wp_query, $posts, $wpdb, $blog_id;
964 $this->ENTRY_PATH = $post_type;
971 $count = get_option('posts_per_rss');
973 wp('posts_per_page=' . $count . '&offset=' . ($count * ($page-1)) . '&orderby=modified&post_status=any');
975 $post = $GLOBALS['post'];
976 $posts = $GLOBALS['posts'];
977 $wp = $GLOBALS['wp'];
978 $wp_query = $GLOBALS['wp_query'];
979 $wpdb = $GLOBALS['wpdb'];
980 $blog_id = (int) $GLOBALS['blog_id'];
982 $last_page = $wp_query->max_num_pages;
983 $next_page = (($page + 1) > $last_page) ? null : $page + 1;
984 $prev_page = ($page - 1) < 1 ? null : $page - 1;
985 $last_page = ((int)$last_page == 1 || (int)$last_page == 0) ? null : (int) $last_page;
986 $self_page = $page > 1 ? $page : null;
987 ?><feed xmlns="<?php echo $this->ATOM_NS ?>" xmlns:app="<?php echo $this->ATOMPUB_NS ?>" xml:lang="<?php bloginfo_rss( 'language' ); ?>" <?php do_action('app_ns'); ?> >
988 <id><?php $this->the_entries_url() ?></id>
989 <updated><?php echo mysql2date('Y-m-d\TH:i:s\Z', get_lastpostmodified('GMT'), false); ?></updated>
990 <title type="text"><?php bloginfo_rss('name') ?></title>
991 <subtitle type="text"><?php bloginfo_rss("description") ?></subtitle>
992 <link rel="first" type="<?php echo $this->ATOM_CONTENT_TYPE ?>" href="<?php $this->the_entries_url() ?>" />
993 <?php if (isset($prev_page)): ?>
994 <link rel="previous" type="<?php echo $this->ATOM_CONTENT_TYPE ?>" href="<?php $this->the_entries_url($prev_page) ?>" />
996 <?php if (isset($next_page)): ?>
997 <link rel="next" type="<?php echo $this->ATOM_CONTENT_TYPE ?>" href="<?php $this->the_entries_url($next_page) ?>" />
999 <link rel="last" type="<?php echo $this->ATOM_CONTENT_TYPE ?>" href="<?php $this->the_entries_url($last_page) ?>" />
1000 <link rel="self" type="<?php echo $this->ATOM_CONTENT_TYPE ?>" href="<?php $this->the_entries_url($self_page) ?>" />
1001 <rights type="text">Copyright <?php echo date('Y'); ?></rights>
1002 <?php do_action('app_head'); ?>
1003 <?php if ( have_posts() ) {
1004 while ( have_posts() ) {
1006 $this->echo_entry();
1011 $feed = ob_get_contents();
1017 * Display entry XML.
1021 * @param int $postID Post ID.
1022 * @param string $post_type Optional, default is post. Post type.
1025 function get_entry($postID, $post_type = 'post') {
1027 switch($post_type) {
1032 $this->ENTRY_PATH = 'attachment';
1033 $varname = 'attachment_id';
1036 query_posts($varname . '=' . $postID);
1037 if ( have_posts() ) {
1038 while ( have_posts() ) {
1040 $this->echo_entry();
1041 $entry = ob_get_contents();
1051 * Display post content XML.
1055 function echo_entry() { ?>
1056 <entry xmlns="<?php echo $this->ATOM_NS ?>"
1057 xmlns:app="<?php echo $this->ATOMPUB_NS ?>" xml:lang="<?php bloginfo_rss( 'language' ); ?>">
1058 <id><?php the_guid( $GLOBALS['post']->ID ); ?></id>
1059 <?php list($content_type, $content) = prep_atom_text_construct(get_the_title()); ?>
1060 <title type="<?php echo $content_type ?>"><?php echo $content ?></title>
1061 <updated><?php echo get_post_modified_time('Y-m-d\TH:i:s\Z', true); ?></updated>
1062 <published><?php echo get_post_time('Y-m-d\TH:i:s\Z', true); ?></published>
1063 <app:edited><?php echo get_post_modified_time('Y-m-d\TH:i:s\Z', true); ?></app:edited>
1065 <app:draft><?php echo ($GLOBALS['post']->post_status == 'draft' ? 'yes' : 'no') ?></app:draft>
1068 <name><?php the_author()?></name>
1069 <?php if ( get_the_author_meta('url') && get_the_author_meta('url') != 'http://' ) { ?>
1070 <uri><?php the_author_meta('url') ?></uri>
1073 <?php if ($GLOBALS['post']->post_type == 'attachment') { ?>
1074 <link rel="edit-media" href="<?php $this->the_media_url() ?>" />
1075 <content type="<?php echo $GLOBALS['post']->post_mime_type ?>" src="<?php the_guid() ; ?>"/>
1077 <link href="<?php the_permalink_rss() ?>" />
1078 <?php if ( strlen( $GLOBALS['post']->post_content ) ) :
1079 list($content_type, $content) = prep_atom_text_construct(get_the_content()); ?>
1080 <content type="<?php echo $content_type ?>"><?php echo $content ?></content>
1083 <link rel="edit" href="<?php $this->the_entry_url() ?>" />
1084 <?php the_category_rss( 'atom' ); ?>
1085 <?php list($content_type, $content) = prep_atom_text_construct(get_the_excerpt()); ?>
1086 <summary type="<?php echo $content_type ?>"><?php echo $content ?></summary>
1087 <?php do_action('app_entry'); ?>
1092 * Set 'OK' (200) status header.
1097 header('Content-Type: text/plain');
1098 status_header('200');
1103 * Set 'No Content' (204) status header.
1107 function no_content() {
1108 header('Content-Type: text/plain');
1109 status_header('204');
1110 echo "Moved to Trash.";
1115 * Display 'Internal Server Error' (500) status header.
1119 * @param string $msg Optional. Status string.
1121 function internal_error($msg = 'Internal Server Error') {
1122 header('Content-Type: text/plain');
1123 status_header('500');
1129 * Set 'Bad Request' (400) status header.
1133 function bad_request() {
1134 header('Content-Type: text/plain');
1135 status_header('400');
1140 * Set 'Length Required' (411) status header.
1144 function length_required() {
1145 header("HTTP/1.1 411 Length Required");
1146 header('Content-Type: text/plain');
1147 status_header('411');
1152 * Set 'Unsupported Media Type' (415) status header.
1156 function invalid_media() {
1157 header("HTTP/1.1 415 Unsupported Media Type");
1158 header('Content-Type: text/plain');
1163 * Set 'Forbidden' (403) status header.
1167 function forbidden($reason='') {
1168 header('Content-Type: text/plain');
1169 status_header('403');
1175 * Set 'Not Found' (404) status header.
1179 function not_found() {
1180 header('Content-Type: text/plain');
1181 status_header('404');
1186 * Set 'Not Allowed' (405) status header.
1190 function not_allowed($allow) {
1191 header('Allow: ' . join(',', $allow));
1192 status_header('405');
1197 * Display Redirect (302) content and set status headers.
1201 function redirect($url) {
1202 $escaped_url = esc_attr($url);
1204 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
1207 <title>302 Found</title>
1211 <p>The document has moved <a href="$escaped_url">here</a>.</p>
1216 header('HTTP/1.1 302 Moved');
1217 header('Content-Type: text/html');
1218 header('Location: ' . $url);
1225 * Set 'Client Error' (400) status header.
1229 function client_error($msg = 'Client Error') {
1230 header('Content-Type: text/plain');
1231 status_header('400');
1236 * Set created status headers (201).
1238 * Sets the 'content-type', 'content-location', and 'location'.
1242 function created($post_ID, $content, $post_type = 'post') {
1243 $edit = $this->get_entry_url($post_ID);
1244 switch($post_type) {
1246 $ctloc = $this->get_entry_url($post_ID);
1249 $edit = $this->app_base . "attachments/$post_ID";
1252 header("Content-Type: $this->ATOM_CONTENT_TYPE");
1254 header('Content-Location: ' . $ctloc);
1255 header('Location: ' . $edit);
1256 status_header('201');
1262 * Set 'Auth Required' (401) headers.
1266 * @param string $msg Status header content and HTML content.
1268 function auth_required($msg) {
1270 header('WWW-Authenticate: Basic realm="WordPress Atom Protocol"');
1271 header("HTTP/1.1 401 $msg");
1272 header('Status: 401 ' . $msg);
1273 header('Content-Type: text/html');
1275 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
1278 <title>401 Unauthorized</title>
1281 <h1>401 Unauthorized</h1>
1292 * Display XML and set headers with content type.
1296 * @param string $xml Display feed content.
1297 * @param string $ctype Optional, default is 'atom+xml'. Feed content type.
1299 function output($xml, $ctype = 'application/atom+xml') {
1300 status_header('200');
1301 $xml = '<?xml version="1.0" encoding="' . strtolower(get_option('blog_charset')) . '"?>'."\n".$xml;
1302 header('Connection: close');
1303 header('Content-Length: '. strlen($xml));
1304 header('Content-Type: ' . $ctype);
1305 header('Content-Disposition: attachment; filename=atom.xml');
1306 header('Date: '. date('r'));
1307 if ($this->do_output)
1313 * Sanitize content for database usage.
1317 * @param array $array Sanitize array and multi-dimension array.
1319 function escape(&$array) {
1322 foreach ($array as $k => $v) {
1324 $this->escape($array[$k]);
1325 } else if (is_object($v)) {
1328 $array[$k] = $wpdb->escape($v);
1334 * Access credential through various methods and perform login.
1340 function authenticate() {
1342 // if using mod_rewrite/ENV hack
1343 // http://www.besthostratings.com/articles/http-auth-php-cgi.html
1344 if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
1345 list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) =
1346 explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
1347 } else if (isset($_SERVER['REDIRECT_REMOTE_USER'])) {
1348 // Workaround for setups that do not forward HTTP_AUTHORIZATION
1349 // See http://trac.wordpress.org/ticket/7361
1350 list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) =
1351 explode(':', base64_decode(substr($_SERVER['REDIRECT_REMOTE_USER'], 6)));
1354 // If Basic Auth is working...
1355 if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
1357 $user = wp_authenticate($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);
1358 if ( $user && !is_wp_error($user) ) {
1359 wp_set_current_user($user->ID);
1368 * Retrieve accepted content types.
1372 * @param array $types Optional. Content Types.
1375 function get_accepted_content_type($types = null) {
1377 if (!isset($types)) {
1378 $types = $this->media_content_types;
1381 if (!isset($_SERVER['CONTENT_LENGTH']) || !isset($_SERVER['CONTENT_TYPE'])) {
1382 $this->length_required();
1385 $type = $_SERVER['CONTENT_TYPE'];
1386 list($type,$subtype) = explode('/',$type);
1387 list($subtype) = explode(";",$subtype); // strip MIME parameters
1389 foreach($types as $t) {
1390 list($acceptedType,$acceptedSubtype) = explode('/',$t);
1391 if ($acceptedType == '*' || $acceptedType == $type) {
1392 if ($acceptedSubtype == '*' || $acceptedSubtype == $subtype)
1393 return $type . "/" . $subtype;
1397 $this->invalid_media();
1401 * Process conditionals for posts.
1405 function process_conditionals() {
1407 if (empty($this->params)) return;
1408 if ($_SERVER['REQUEST_METHOD'] == 'DELETE') return;
1410 switch($this->params[0]) {
1411 case $this->ENTRY_PATH:
1413 $post = wp_get_single_post($this->params[1]);
1414 $wp_last_modified = get_post_modified_time('D, d M Y H:i:s', true);
1417 case $this->ENTRIES_PATH:
1418 $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0).' GMT';
1423 $wp_etag = md5($wp_last_modified);
1424 @header("Last-Modified: $wp_last_modified");
1425 @header("ETag: $wp_etag");
1427 // Support for Conditional GET
1428 if (isset($_SERVER['HTTP_IF_NONE_MATCH']))
1429 $client_etag = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']);
1431 $client_etag = false;
1433 $client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE']);
1434 // If string is empty, return 0. If not, attempt to parse into a timestamp
1435 $client_modified_timestamp = $client_last_modified ? strtotime($client_last_modified) : 0;
1437 // Make a timestamp for our most recent modification...
1438 $wp_modified_timestamp = strtotime($wp_last_modified);
1440 if ( ($client_last_modified && $client_etag) ?
1441 (($client_modified_timestamp >= $wp_modified_timestamp) && ($client_etag == $wp_etag)) :
1442 (($client_modified_timestamp >= $wp_modified_timestamp) || ($client_etag == $wp_etag)) ) {
1443 status_header( 304 );
1449 * Convert RFC3339 time string to timestamp.
1453 * @param string $str String to time.
1454 * @return bool|int false if format is incorrect.
1456 function rfc3339_str2time($str) {
1459 if (!preg_match("/(\d{4}-\d{2}-\d{2})T(\d{2}\:\d{2}\:\d{2})\.?\d{0,3}(Z|[+-]+\d{2}\:\d{2})/", $str, $match))
1462 if ($match[3] == 'Z')
1463 $match[3] = '+0000';
1465 return strtotime($match[1] . " " . $match[2] . " " . $match[3]);
1469 * Retrieve published time to display in XML.
1473 * @param string $published Time string.
1476 function get_publish_time($published) {
1478 $pubtime = $this->rfc3339_str2time($published);
1481 return array(current_time('mysql'),current_time('mysql',1));
1483 return array(date("Y-m-d H:i:s", $pubtime), gmdate("Y-m-d H:i:s", $pubtime));