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'),
171 '@/attachment/(\d+)$@' =>
172 array('GET' => 'get_attachment',
173 'PUT' => 'put_attachment',
174 'DELETE' => 'delete_attachment'),
179 * Handle ATOMPUB request.
183 function handle_request() {
185 if ( !empty( $_SERVER['ORIG_PATH_INFO'] ) )
186 $path = $_SERVER['ORIG_PATH_INFO'];
188 $path = $_SERVER['PATH_INFO'];
190 $method = $_SERVER['REQUEST_METHOD'];
192 $this->process_conditionals();
193 //$this->process_conditionals();
195 // exception case for HEAD (treat exactly as GET, but don't output)
196 if ($method == 'HEAD') {
197 $this->do_output = false;
201 // redirect to /service in case no path is found.
202 if (strlen($path) == 0 || $path == '/')
203 $this->redirect($this->get_service_url());
205 // check to see if AtomPub is enabled
206 if ( !get_option( 'enable_app' ) )
207 $this->forbidden( sprintf( __( 'AtomPub services are disabled on this site. An admin user can enable them at %s' ), admin_url('options-writing.php') ) );
210 foreach ( $this->selectors as $regex => $funcs ) {
211 if ( preg_match($regex, $path, $matches) ) {
212 if ( isset($funcs[$method]) ) {
214 // authenticate regardless of the operation and set the current
215 // user. each handler will decide if auth is required or not.
216 if ( !$this->authenticate() ) {
217 $this->auth_required('Credentials required.');
220 array_shift($matches);
221 call_user_func_array(array($this,$funcs[$method]), $matches);
224 // only allow what we have handlers for...
225 $this->not_allowed(array_keys($funcs));
230 // oops, nothing found
235 * Retrieve XML for ATOMPUB service.
239 function get_service() {
241 if ( !current_user_can( 'edit_posts' ) )
242 $this->auth_required( __( 'Sorry, you do not have the right to access this site.' ) );
244 $entries_url = esc_attr($this->get_entries_url());
245 $categories_url = esc_attr($this->get_categories_url());
246 $media_url = esc_attr($this->get_attachments_url());
247 $accepted_media_types = '';
248 foreach ($this->media_content_types as $med) {
249 $accepted_media_types = $accepted_media_types . "<accept>" . $med . "</accept>";
252 $atom_blogname = get_bloginfo('name');
253 $service_doc = <<<EOD
254 <service xmlns="$this->ATOMPUB_NS" xmlns:$atom_prefix="$this->ATOM_NS">
256 <$atom_prefix:title>$atom_blogname Workspace</$atom_prefix:title>
257 <collection href="$entries_url">
258 <$atom_prefix:title>$atom_blogname Posts</$atom_prefix:title>
259 <accept>$this->ATOM_CONTENT_TYPE;type=entry</accept>
260 <categories href="$categories_url" />
262 <collection href="$media_url">
263 <$atom_prefix:title>$atom_blogname Media</$atom_prefix:title>
264 $accepted_media_types
271 $this->output($service_doc, $this->SERVICE_CONTENT_TYPE);
275 * Retrieve categories list in XML format.
279 function get_categories_xml() {
281 if ( !current_user_can( 'edit_posts' ) )
282 $this->auth_required( __( 'Sorry, you do not have the right to access this site.' ) );
284 $home = esc_attr(get_bloginfo_rss('url'));
287 $cats = get_categories(array('hierarchical' => 0, 'hide_empty' => 0));
288 foreach ( (array) $cats as $cat ) {
289 $categories .= " <category term=\"" . esc_attr($cat->name) . "\" />\n";
292 <app:categories xmlns:app="$this->ATOMPUB_NS"
293 xmlns="$this->ATOM_NS"
294 fixed="yes" scheme="$home">
298 $this->output($output, $this->CATEGORIES_CONTENT_TYPE);
306 function create_post() {
308 $this->get_accepted_content_type($this->atom_content_types);
310 $parser = new AtomParser();
311 if ( !$parser->parse() )
312 $this->client_error();
314 $entry = array_pop($parser->feed->entries);
316 $publish = ! ( isset( $entry->draft ) && 'yes' == trim( $entry->draft ) );
317 $cap = ($publish) ? 'publish_posts' : 'edit_posts';
319 if ( !current_user_can($cap) )
320 $this->auth_required(__('Sorry, you do not have the right to edit/publish new posts.'));
323 if ( !empty( $entry->categories ) ) {
324 foreach ( $entry->categories as $cat ) {
325 array_push($catnames, $cat["term"]);
329 $wp_cats = get_categories(array('hide_empty' => false));
331 $post_category = array();
333 foreach ( $wp_cats as $cat ) {
334 if ( in_array($cat->name, $catnames) )
335 array_push($post_category, $cat->term_id);
338 $blog_ID = get_current_blog_id();
339 $post_status = ($publish) ? 'publish' : 'draft';
340 $post_author = (int) $user_ID;
347 if ( isset( $entry->title ) && is_array( $entry->title ) && !empty( $entry->title[1] ) )
348 $post_title = (string) $entry->title[1];
349 if ( isset( $entry->content ) && is_array( $entry->content ) && !empty( $entry->content[1] ) )
350 $post_content = (string) $entry->content[1];
351 if ( isset( $entry->summary ) && is_array( $entry->summary ) && !empty( $entry->summary[1] ) )
352 $post_excerpt = (string) $entry->summary[1];
353 if ( !empty( $entry->published ) )
354 $pubtimes = (string) $entry->published;
356 $pubtimes = $this->get_publish_time( $pubtimes );
358 $post_date = $pubtimes[0];
359 $post_date_gmt = $pubtimes[1];
361 if ( isset( $_SERVER['HTTP_SLUG'] ) )
362 $post_name = $_SERVER['HTTP_SLUG'];
364 $post_data = compact('blog_ID', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'post_name');
366 $this->escape($post_data);
368 $postID = wp_insert_post($post_data);
369 if ( is_wp_error( $postID ) )
370 $this->internal_error($postID->get_error_message());
373 $this->internal_error(__('Sorry, your entry could not be posted. Something wrong happened.'));
375 // getting warning here about unable to set headers
376 // because something in the cache is printing to the buffer
377 // could we clean up wp_set_post_categories or cache to not print
378 // this could affect our ability to send back the right headers
379 @wp_set_post_categories($postID, $post_category);
381 do_action( 'atompub_create_post', $postID, $entry );
383 $output = $this->get_entry($postID);
385 $this->created($postID, $output);
393 * @param int $postID Post ID.
395 function get_post($postID) {
398 if ( ! get_post( $postID ) || ! current_user_can( 'edit_post', $postID ) )
399 $this->auth_required( __( 'Sorry, you do not have the right to access this post.' ) );
401 $this->set_current_entry($postID);
402 $output = $this->get_entry($postID);
403 $this->output($output);
412 * @param int $postID Post ID.
414 function put_post($postID) {
415 // checked for valid content-types (atom+xml)
416 // quick check and exit
417 $this->get_accepted_content_type($this->atom_content_types);
419 $parser = new AtomParser();
420 if ( !$parser->parse() )
421 $this->bad_request();
423 $parsed = array_pop($parser->feed->entries);
425 // check for not found
427 $this->set_current_entry($postID);
429 if ( !current_user_can('edit_post', $postID) )
430 $this->auth_required(__('Sorry, you do not have the right to edit this post.'));
432 $publish = ! ( isset($parsed->draft) && 'yes' == trim($parsed->draft) );
434 if ( $publish && ! current_user_can( 'publish_posts' ) )
435 $this->auth_required( __( 'Sorry, you do not have the right to publish this post.' ) );
437 $post_status = ($publish) ? 'publish' : 'draft';
441 $post_title = $parsed->title[1];
442 $post_content = $parsed->content[1];
443 $post_excerpt = $parsed->summary[1];
444 $pubtimes = $this->get_publish_time($entry->published);
445 $post_date = $pubtimes[0];
446 $post_date_gmt = $pubtimes[1];
447 $pubtimes = $this->get_publish_time($parsed->updated);
448 $post_modified = $pubtimes[0];
449 $post_modified_gmt = $pubtimes[1];
451 $postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'post_date', 'post_date_gmt', 'post_modified', 'post_modified_gmt');
452 $this->escape($postdata);
454 $result = wp_update_post($postdata);
457 $this->internal_error(__('For some strange yet very annoying reason, this post could not be edited.'));
459 do_action( 'atompub_put_post', $ID, $parsed );
469 * @param int $postID Post ID.
471 function delete_post($postID) {
473 // check for not found
475 $this->set_current_entry($postID);
477 if ( !current_user_can('delete_post', $postID) )
478 $this->auth_required(__('Sorry, you do not have the right to delete this post.'));
480 if ( $entry['post_type'] == 'attachment' ) {
481 $this->delete_attachment($postID);
483 $result = wp_delete_post($postID);
486 $this->internal_error(__('For some strange yet very annoying reason, this post could not be deleted.'));
495 * Retrieve attachment.
499 * @param int $postID Optional. Post ID.
501 function get_attachment($postID = null) {
502 if ( !current_user_can( 'upload_files' ) )
503 $this->auth_required( __( 'You do not have permission to upload files.' ) );
505 if ( !isset($postID) ) {
506 $this->get_attachments();
508 if ( ! current_user_can( 'edit_post', $postID ) )
509 $this->auth_required( __( 'Sorry, you do not have the right to edit this post.' ) );
511 $this->set_current_entry($postID);
512 $output = $this->get_entry($postID, 'attachment');
513 $this->output($output);
518 * Create new attachment.
522 function create_attachment() {
524 $type = $this->get_accepted_content_type();
526 if ( !current_user_can('upload_files') )
527 $this->auth_required( __( 'You do not have permission to upload files.' ) );
529 $fp = fopen("php://input", "rb");
531 while ( !feof($fp) ) {
532 $bits .= fread($fp, 4096);
537 if ( isset( $_SERVER['HTTP_SLUG'] ) )
538 $slug = $_SERVER['HTTP_SLUG'];
539 elseif ( isset( $_SERVER['HTTP_TITLE'] ) )
540 $slug = $_SERVER['HTTP_TITLE'];
541 elseif ( empty( $slug ) ) // just make a random name
542 $slug = substr( md5( uniqid( microtime() ) ), 0, 7);
543 $ext = preg_replace( '|.*/([a-z0-9]+)|', '$1', $_SERVER['CONTENT_TYPE'] );
544 $slug = sanitize_file_name( "$slug.$ext" );
545 $file = wp_upload_bits( $slug, null, $bits);
548 $file = $file['file'];
550 do_action('wp_create_file_in_uploads', $file); // replicate
552 // Construct the attachment array
554 'post_title' => $slug,
555 'post_content' => $slug,
556 'post_status' => 'attachment',
558 'post_mime_type' => $type,
563 $postID = wp_insert_attachment($attachment, $file);
566 $this->internal_error(__('Sorry, your entry could not be posted. Something wrong happened.'));
568 $output = $this->get_entry($postID, 'attachment');
570 $this->created($postID, $output, 'attachment');
578 * @param int $postID Post ID.
580 function put_attachment($postID) {
581 // checked for valid content-types (atom+xml)
582 // quick check and exit
583 $this->get_accepted_content_type($this->atom_content_types);
585 $parser = new AtomParser();
586 if (!$parser->parse()) {
587 $this->bad_request();
590 $parsed = array_pop($parser->feed->entries);
592 // check for not found
594 $this->set_current_entry($postID);
596 if ( !current_user_can('edit_post', $entry['ID']) || 'attachment' != $entry['post_type'] )
597 $this->auth_required(__('Sorry, you do not have the right to edit this post.'));
601 $post_title = $parsed->title[1];
602 $post_content = $parsed->summary[1];
603 $pubtimes = $this->get_publish_time($parsed->updated);
604 $post_modified = $pubtimes[0];
605 $post_modified_gmt = $pubtimes[1];
607 $postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'post_modified', 'post_modified_gmt');
608 $this->escape($postdata);
610 $result = wp_update_post($postdata);
613 $this->internal_error(__('For some strange yet very annoying reason, this post could not be edited.'));
623 * @param int $postID Post ID.
625 function delete_attachment($postID) {
627 // check for not found
629 $this->set_current_entry($postID);
631 if ( !current_user_can('delete_post', $postID) )
632 $this->auth_required(__('Sorry, you do not have the right to delete this post.'));
634 $location = get_post_meta($entry['ID'], '_wp_attached_file', true);
635 $filetype = wp_check_filetype($location);
637 if ( !isset($location) || 'attachment' != $entry['post_type'] || empty($filetype['ext']) )
638 $this->internal_error(__('Error occurred while accessing post metadata for file location.'));
641 $result = wp_delete_attachment($postID);
644 $this->internal_error(__('For some strange yet very annoying reason, this post could not be deleted.'));
650 * Retrieve attachment from post.
654 * @param int $postID Post ID.
656 function get_file($postID) {
658 // check for not found
660 $this->set_current_entry($postID);
662 // then whether user can edit the specific post
663 if ( !current_user_can('edit_post', $postID) )
664 $this->auth_required(__('Sorry, you do not have the right to edit this post.'));
666 $location = get_post_meta($entry['ID'], '_wp_attached_file', true);
667 $location = get_option ('upload_path') . '/' . $location;
668 $filetype = wp_check_filetype($location);
670 if ( !isset($location) || 'attachment' != $entry['post_type'] || empty($filetype['ext']) )
671 $this->internal_error(__('Error occurred while accessing post metadata for file location.'));
673 status_header('200');
674 header('Content-Type: ' . $entry['post_mime_type']);
675 header('Connection: close');
677 if ( $fp = fopen($location, "rb") ) {
678 status_header('200');
679 header('Content-Type: ' . $entry['post_mime_type']);
680 header('Connection: close');
682 while ( !feof($fp) ) {
683 echo fread($fp, 4096);
688 status_header ('404');
695 * Upload file to blog and add attachment to post.
699 * @param int $postID Post ID.
701 function put_file($postID) {
703 // first check if user can upload
704 if ( !current_user_can('upload_files') )
705 $this->auth_required(__('You do not have permission to upload files.'));
707 // check for not found
709 $this->set_current_entry($postID);
711 // then whether user can edit the specific post
712 if ( !current_user_can('edit_post', $postID) )
713 $this->auth_required(__('Sorry, you do not have the right to edit this post.'));
715 $upload_dir = wp_upload_dir( );
716 $location = get_post_meta($entry['ID'], '_wp_attached_file', true);
717 $filetype = wp_check_filetype($location);
719 $location = "{$upload_dir['basedir']}/{$location}";
721 if (!isset($location) || 'attachment' != $entry['post_type'] || empty($filetype['ext']))
722 $this->internal_error(__('Error occurred while accessing post metadata for file location.'));
724 $fp = fopen("php://input", "rb");
725 $localfp = fopen($location, "w+");
726 while ( !feof($fp) ) {
727 fwrite($localfp,fread($fp, 4096));
733 $pubtimes = $this->get_publish_time($entry->published);
734 $post_date = $pubtimes[0];
735 $post_date_gmt = $pubtimes[1];
736 $pubtimes = $this->get_publish_time($parsed->updated);
737 $post_modified = $pubtimes[0];
738 $post_modified_gmt = $pubtimes[1];
740 $post_data = compact('ID', 'post_date', 'post_date_gmt', 'post_modified', 'post_modified_gmt');
741 $result = wp_update_post($post_data);
744 $this->internal_error(__('Sorry, your entry could not be posted. Something wrong happened.'));
746 wp_update_attachment_metadata( $postID, wp_generate_attachment_metadata( $postID, $location ) );
752 * Retrieve entries URL.
756 * @param int $page Page ID.
759 function get_entries_url($page = null) {
760 if ( isset($GLOBALS['post_type']) && ( $GLOBALS['post_type'] == 'attachment' ) )
761 $path = $this->MEDIA_PATH;
763 $path = $this->ENTRIES_PATH;
764 $url = $this->app_base . $path;
765 if ( isset($page) && is_int($page) )
771 * Display entries URL.
775 * @param int $page Page ID.
777 function the_entries_url($page = null) {
778 echo $this->get_entries_url($page);
782 * Retrieve categories URL.
786 * @param mixed $deprecated Not used.
789 function get_categories_url($deprecated = '') {
790 if ( !empty( $deprecated ) )
791 _deprecated_argument( __FUNCTION__, '2.5' );
792 return $this->app_base . $this->CATEGORIES_PATH;
796 * Display category URL.
800 function the_categories_url() {
801 echo $this->get_categories_url();
805 * Retrieve attachment URL.
809 * @param int $page Page ID.
812 function get_attachments_url($page = null) {
813 $url = $this->app_base . $this->MEDIA_PATH;
814 if (isset($page) && is_int($page)) {
821 * Display attachment URL.
825 * @param int $page Page ID.
827 function the_attachments_url($page = null) {
828 echo $this->get_attachments_url($page);
832 * Retrieve service URL.
838 function get_service_url() {
839 return $this->app_base . $this->SERVICE_PATH;
843 * Retrieve entry URL.
847 * @param int $postID Post ID.
850 function get_entry_url($postID = null) {
851 if (!isset($postID)) {
853 $postID = (int) $post->ID;
856 $url = $this->app_base . $this->ENTRY_PATH . "/$postID";
866 * @param int $postID Post ID.
868 function the_entry_url($postID = null) {
869 echo $this->get_entry_url($postID);
873 * Retrieve media URL.
877 * @param int $postID Post ID.
880 function get_media_url($postID = null) {
881 if (!isset($postID)) {
883 $postID = (int) $post->ID;
886 $url = $this->app_base . $this->MEDIA_SINGLE_PATH ."/file/$postID";
892 * Display the media URL.
896 * @param int $postID Post ID.
898 function the_media_url($postID = null) {
899 echo $this->get_media_url($postID);
903 * Set the current entry to post ID.
907 * @param int $postID Post ID.
909 function set_current_entry($postID) {
912 if (!isset($postID)) {
913 // $this->bad_request();
917 $entry = wp_get_single_post($postID,ARRAY_A);
919 if (!isset($entry) || !isset($entry['ID']))
930 * @param int $page Optional. Page ID.
931 * @param string $post_type Optional, default is 'post'. Post Type.
933 function get_posts($page = 1, $post_type = 'post') {
934 $feed = $this->get_feed($page, $post_type);
935 $this->output($feed);
939 * Display attachment XML.
943 * @param int $page Page ID.
944 * @param string $post_type Optional, default is 'attachment'. Post type.
946 function get_attachments($page = 1, $post_type = 'attachment') {
947 $GLOBALS['post_type'] = $post_type;
948 $feed = $this->get_feed($page, $post_type);
949 $this->output($feed);
957 * @param int $page Page ID.
958 * @param string $post_type Optional, default is post. Post type.
961 function get_feed($page = 1, $post_type = 'post') {
962 global $post, $wp, $wp_query, $posts, $wpdb, $blog_id;
965 $this->ENTRY_PATH = $post_type;
972 $count = get_option('posts_per_rss');
974 wp('posts_per_page=' . $count . '&offset=' . ($count * ($page-1)) . '&orderby=modified&perm=readable');
976 $post = $GLOBALS['post'];
977 $posts = $GLOBALS['posts'];
978 $wp = $GLOBALS['wp'];
979 $wp_query = $GLOBALS['wp_query'];
980 $wpdb = $GLOBALS['wpdb'];
981 $blog_id = (int) $GLOBALS['blog_id'];
983 $last_page = $wp_query->max_num_pages;
984 $next_page = (($page + 1) > $last_page) ? null : $page + 1;
985 $prev_page = ($page - 1) < 1 ? null : $page - 1;
986 $last_page = ((int)$last_page == 1 || (int)$last_page == 0) ? null : (int) $last_page;
987 $self_page = $page > 1 ? $page : null;
988 ?><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'); ?> >
989 <id><?php $this->the_entries_url() ?></id>
990 <updated><?php echo mysql2date('Y-m-d\TH:i:s\Z', get_lastpostmodified('GMT'), false); ?></updated>
991 <title type="text"><?php bloginfo_rss('name') ?></title>
992 <subtitle type="text"><?php bloginfo_rss("description") ?></subtitle>
993 <link rel="first" type="<?php echo $this->ATOM_CONTENT_TYPE ?>" href="<?php $this->the_entries_url() ?>" />
994 <?php if (isset($prev_page)): ?>
995 <link rel="previous" type="<?php echo $this->ATOM_CONTENT_TYPE ?>" href="<?php $this->the_entries_url($prev_page) ?>" />
997 <?php if (isset($next_page)): ?>
998 <link rel="next" type="<?php echo $this->ATOM_CONTENT_TYPE ?>" href="<?php $this->the_entries_url($next_page) ?>" />
1000 <link rel="last" type="<?php echo $this->ATOM_CONTENT_TYPE ?>" href="<?php $this->the_entries_url($last_page) ?>" />
1001 <link rel="self" type="<?php echo $this->ATOM_CONTENT_TYPE ?>" href="<?php $this->the_entries_url($self_page) ?>" />
1002 <rights type="text">Copyright <?php echo date('Y'); ?></rights>
1003 <?php do_action('app_head'); ?>
1004 <?php if ( have_posts() ) {
1005 while ( have_posts() ) {
1007 $this->echo_entry();
1012 $feed = ob_get_contents();
1018 * Display entry XML.
1022 * @param int $postID Post ID.
1023 * @param string $post_type Optional, default is post. Post type.
1026 function get_entry($postID, $post_type = 'post') {
1028 switch($post_type) {
1033 $this->ENTRY_PATH = 'attachment';
1034 $varname = 'attachment_id';
1037 query_posts($varname . '=' . $postID);
1038 if ( have_posts() ) {
1039 while ( have_posts() ) {
1041 $this->echo_entry();
1042 $entry = ob_get_contents();
1052 * Display post content XML.
1056 function echo_entry() { ?>
1057 <entry xmlns="<?php echo $this->ATOM_NS ?>"
1058 xmlns:app="<?php echo $this->ATOMPUB_NS ?>" xml:lang="<?php bloginfo_rss( 'language' ); ?>">
1059 <id><?php the_guid( $GLOBALS['post']->ID ); ?></id>
1060 <?php list($content_type, $content) = prep_atom_text_construct(get_the_title()); ?>
1061 <title type="<?php echo $content_type ?>"><?php echo $content ?></title>
1062 <updated><?php echo get_post_modified_time('Y-m-d\TH:i:s\Z', true); ?></updated>
1063 <published><?php echo get_post_time('Y-m-d\TH:i:s\Z', true); ?></published>
1064 <app:edited><?php echo get_post_modified_time('Y-m-d\TH:i:s\Z', true); ?></app:edited>
1066 <app:draft><?php echo ($GLOBALS['post']->post_status == 'draft' ? 'yes' : 'no') ?></app:draft>
1069 <name><?php the_author()?></name>
1070 <?php if ( get_the_author_meta('url') && get_the_author_meta('url') != 'http://' ) { ?>
1071 <uri><?php the_author_meta('url') ?></uri>
1074 <?php if ($GLOBALS['post']->post_type == 'attachment') { ?>
1075 <link rel="edit-media" href="<?php $this->the_media_url() ?>" />
1076 <content type="<?php echo $GLOBALS['post']->post_mime_type ?>" src="<?php the_guid() ; ?>"/>
1078 <link href="<?php the_permalink_rss() ?>" />
1079 <?php if ( strlen( $GLOBALS['post']->post_content ) ) :
1080 list($content_type, $content) = prep_atom_text_construct(get_the_content()); ?>
1081 <content type="<?php echo $content_type ?>"><?php echo $content ?></content>
1084 <link rel="edit" href="<?php $this->the_entry_url() ?>" />
1085 <?php the_category_rss( 'atom' ); ?>
1086 <?php list($content_type, $content) = prep_atom_text_construct(get_the_excerpt()); ?>
1087 <summary type="<?php echo $content_type ?>"><?php echo $content ?></summary>
1088 <?php do_action('app_entry'); ?>
1093 * Set 'OK' (200) status header.
1098 header('Content-Type: text/plain');
1099 status_header('200');
1104 * Set 'No Content' (204) status header.
1108 function no_content() {
1109 header('Content-Type: text/plain');
1110 status_header('204');
1111 echo "Moved to Trash.";
1116 * Display 'Internal Server Error' (500) status header.
1120 * @param string $msg Optional. Status string.
1122 function internal_error($msg = 'Internal Server Error') {
1123 header('Content-Type: text/plain');
1124 status_header('500');
1130 * Set 'Bad Request' (400) status header.
1134 function bad_request() {
1135 header('Content-Type: text/plain');
1136 status_header('400');
1141 * Set 'Length Required' (411) status header.
1145 function length_required() {
1146 header("HTTP/1.1 411 Length Required");
1147 header('Content-Type: text/plain');
1148 status_header('411');
1153 * Set 'Unsupported Media Type' (415) status header.
1157 function invalid_media() {
1158 header("HTTP/1.1 415 Unsupported Media Type");
1159 header('Content-Type: text/plain');
1164 * Set 'Forbidden' (403) status header.
1168 function forbidden($reason='') {
1169 header('Content-Type: text/plain');
1170 status_header('403');
1176 * Set 'Not Found' (404) status header.
1180 function not_found() {
1181 header('Content-Type: text/plain');
1182 status_header('404');
1187 * Set 'Not Allowed' (405) status header.
1191 function not_allowed($allow) {
1192 header('Allow: ' . join(',', $allow));
1193 status_header('405');
1198 * Display Redirect (302) content and set status headers.
1202 function redirect($url) {
1203 $escaped_url = esc_attr($url);
1205 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
1208 <title>302 Found</title>
1212 <p>The document has moved <a href="$escaped_url">here</a>.</p>
1217 header('HTTP/1.1 302 Moved');
1218 header('Content-Type: text/html');
1219 header('Location: ' . $url);
1226 * Set 'Client Error' (400) status header.
1230 function client_error($msg = 'Client Error') {
1231 header('Content-Type: text/plain');
1232 status_header('400');
1237 * Set created status headers (201).
1239 * Sets the 'content-type', 'content-location', and 'location'.
1243 function created($post_ID, $content, $post_type = 'post') {
1244 $edit = $this->get_entry_url($post_ID);
1245 switch($post_type) {
1247 $ctloc = $this->get_entry_url($post_ID);
1250 $edit = $this->app_base . "attachments/$post_ID";
1253 header("Content-Type: $this->ATOM_CONTENT_TYPE");
1255 header('Content-Location: ' . $ctloc);
1256 header('Location: ' . $edit);
1257 status_header('201');
1263 * Set 'Auth Required' (401) headers.
1267 * @param string $msg Status header content and HTML content.
1269 function auth_required($msg) {
1271 header('WWW-Authenticate: Basic realm="WordPress Atom Protocol"');
1272 header("HTTP/1.1 401 $msg");
1273 header('Status: 401 ' . $msg);
1274 header('Content-Type: text/html');
1276 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
1279 <title>401 Unauthorized</title>
1282 <h1>401 Unauthorized</h1>
1293 * Display XML and set headers with content type.
1297 * @param string $xml Display feed content.
1298 * @param string $ctype Optional, default is 'atom+xml'. Feed content type.
1300 function output($xml, $ctype = 'application/atom+xml') {
1301 status_header('200');
1302 $xml = '<?xml version="1.0" encoding="' . strtolower(get_option('blog_charset')) . '"?>'."\n".$xml;
1303 header('Connection: close');
1304 header('Content-Length: '. strlen($xml));
1305 header('Content-Type: ' . $ctype);
1306 header('Content-Disposition: attachment; filename=atom.xml');
1307 header('Date: '. date('r'));
1308 if ($this->do_output)
1314 * Sanitize content for database usage.
1318 * @param array $array Sanitize array and multi-dimension array.
1320 function escape(&$array) {
1323 foreach ($array as $k => $v) {
1325 $this->escape($array[$k]);
1326 } else if (is_object($v)) {
1329 $array[$k] = $wpdb->escape($v);
1335 * Access credential through various methods and perform login.
1341 function authenticate() {
1343 // if using mod_rewrite/ENV hack
1344 // http://www.besthostratings.com/articles/http-auth-php-cgi.html
1345 if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
1346 list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) =
1347 explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
1348 } else if (isset($_SERVER['REDIRECT_REMOTE_USER'])) {
1349 // Workaround for setups that do not forward HTTP_AUTHORIZATION
1350 // See http://trac.wordpress.org/ticket/7361
1351 list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) =
1352 explode(':', base64_decode(substr($_SERVER['REDIRECT_REMOTE_USER'], 6)));
1355 // If Basic Auth is working...
1356 if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
1358 $user = wp_authenticate($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);
1359 if ( $user && !is_wp_error($user) ) {
1360 wp_set_current_user($user->ID);
1369 * Retrieve accepted content types.
1373 * @param array $types Optional. Content Types.
1376 function get_accepted_content_type($types = null) {
1378 if (!isset($types)) {
1379 $types = $this->media_content_types;
1382 if (!isset($_SERVER['CONTENT_LENGTH']) || !isset($_SERVER['CONTENT_TYPE'])) {
1383 $this->length_required();
1386 $type = $_SERVER['CONTENT_TYPE'];
1387 list($type,$subtype) = explode('/',$type);
1388 list($subtype) = explode(";",$subtype); // strip MIME parameters
1390 foreach($types as $t) {
1391 list($acceptedType,$acceptedSubtype) = explode('/',$t);
1392 if ($acceptedType == '*' || $acceptedType == $type) {
1393 if ($acceptedSubtype == '*' || $acceptedSubtype == $subtype)
1394 return $type . "/" . $subtype;
1398 $this->invalid_media();
1402 * Process conditionals for posts.
1406 function process_conditionals() {
1408 if (empty($this->params)) return;
1409 if ($_SERVER['REQUEST_METHOD'] == 'DELETE') return;
1411 switch($this->params[0]) {
1412 case $this->ENTRY_PATH:
1414 $post = wp_get_single_post($this->params[1]);
1415 $wp_last_modified = get_post_modified_time('D, d M Y H:i:s', true);
1418 case $this->ENTRIES_PATH:
1419 $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0).' GMT';
1424 $wp_etag = md5($wp_last_modified);
1425 @header("Last-Modified: $wp_last_modified");
1426 @header("ETag: $wp_etag");
1428 // Support for Conditional GET
1429 if (isset($_SERVER['HTTP_IF_NONE_MATCH']))
1430 $client_etag = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']);
1432 $client_etag = false;
1434 $client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE']);
1435 // If string is empty, return 0. If not, attempt to parse into a timestamp
1436 $client_modified_timestamp = $client_last_modified ? strtotime($client_last_modified) : 0;
1438 // Make a timestamp for our most recent modification...
1439 $wp_modified_timestamp = strtotime($wp_last_modified);
1441 if ( ($client_last_modified && $client_etag) ?
1442 (($client_modified_timestamp >= $wp_modified_timestamp) && ($client_etag == $wp_etag)) :
1443 (($client_modified_timestamp >= $wp_modified_timestamp) || ($client_etag == $wp_etag)) ) {
1444 status_header( 304 );
1450 * Convert RFC3339 time string to timestamp.
1454 * @param string $str String to time.
1455 * @return bool|int false if format is incorrect.
1457 function rfc3339_str2time($str) {
1460 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))
1463 if ($match[3] == 'Z')
1464 $match[3] = '+0000';
1466 return strtotime($match[1] . " " . $match[2] . " " . $match[3]);
1470 * Retrieve published time to display in XML.
1474 * @param string $published Time string.
1477 function get_publish_time($published) {
1479 $pubtime = $this->rfc3339_str2time($published);
1482 return array(current_time('mysql'),current_time('mysql',1));
1484 return array(date("Y-m-d H:i:s", $pubtime), gmdate("Y-m-d H:i:s", $pubtime));