Make WordPress Core

Ticket #16525: app.php

File app.php, 30.3 KB (added by hakre, 16 years ago)

r134; eliast; 2006-08-31 15:25:04 UTC

Line 
1<?php
2
3// vim:sw=2:sts=2:ts=2:noet:sta:tw=200
4
5/*
6
7An Atom Publishing Protocol implementation for WordPress.
8
9Author: Elias Torres <elias@torrez.us>
10
11*/
12
13/*
14
15 Copyright 2006 Elias Torres <elias@torrez.us>
16
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 2 of the License, or
20 (at your option) any later version.
21
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
26
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30
31*/
32
33define('APP_REQUEST', true);
34
35require_once('wp-config.php');
36
37if (!isset($PATH_INFO)) {
38 $PATH_INFO = substr($_SERVER['REQUEST_URI'],strlen($_SERVER['SCRIPT_NAME']),
39 strlen($_SERVER['REQUEST_URI']));
40}
41
42$app_logging = 1;
43
44function log_app($label,$msg) {
45 global $app_logging;
46 if ($app_logging) {
47 $fp = fopen("app.log","a+");
48 $date = gmdate("Y-m-d H:i:s ");
49 fwrite($fp, "\n\n".$date."- $label\n".$msg."\n");
50 fclose($fp);
51 }
52}
53
54#
55#
56
57if ( !function_exists('wp_set_current_user') ) :
58function wp_set_current_user($id, $name = '') {
59 global $current_user;
60
61 if ( isset($current_user) && ($id == $current_user->ID) )
62 return $current_user;
63
64 $current_user = new WP_User($id, $name);
65
66 return $current_user;
67}
68endif;
69
70function wa_posts_where_include_drafts_filter($where) {
71 $where = ereg_replace("post_author = ([0-9]+) AND post_status != 'draft'","post_author = \\1 AND post_status = 'draft'", $where);
72 return $where;
73}
74add_filter('posts_where', 'wa_posts_where_include_drafts_filter');
75
76#
77#
78
79class AtomEntry {
80
81 var $links = array();
82 var $categories = array();
83}
84
85class AtomParser {
86
87 var $ATOM_CONTENT_ELEMENTS = array('content','summary','title','subtitle','rights');
88 var $ATOM_SIMPLE_ELEMENTS = array('id','updated','published','draft');
89
90 var $depth = 0;
91 var $indent = 2;
92 var $in_content;
93 var $ns_contexts = array();
94 var $ns_decls = array();
95 var $is_xhtml = false;
96 var $skipped_div = false;
97
98 var $entry;
99
100 function AtomParser() {
101
102 $this->entry = new AtomEntry();
103 $this->map_attrs_func = create_function('$k,$v', 'return "$k=\"$v\"";');
104 $this->map_xmlns_func = create_function('$p,$n', '$xd = "xmlns"; if(strlen($n[0])>0) $xd .= ":{$n[0]}"; return "{$xd}=\"{$n[1]}\"";');
105 }
106
107 function parse() {
108
109 global $app_logging;
110 array_unshift($this->ns_contexts, array());
111
112 $parser = xml_parser_create_ns("UTF-8");
113 xml_set_object($parser, $this);
114 xml_set_element_handler($parser, "start_element", "end_element");
115 xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
116 xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,0);
117 xml_set_character_data_handler($parser, "cdata");
118 xml_set_default_handler($parser, "_default");
119 xml_set_start_namespace_decl_handler($parser, "start_ns");
120 xml_set_end_namespace_decl_handler($parser, "end_ns");
121
122 $contents = "";
123
124 $fp = fopen("php://input", "r");
125 while(!feof($fp)) {
126 $line = fgets($fp, 4096);
127
128 if($app_logging) $contents .= $line;
129
130 if(!xml_parse($parser, $line, feof($fp))) {
131 log_app("xml_parse_error", "line: $line");
132 xml_parser_free($parser);
133 return false;
134 }
135 }
136 fclose($fp);
137
138 xml_parser_free($parser);
139
140 log_app("AtomParser->parse()",trim($contents));
141
142 return true;
143 }
144
145 function start_element($parser, $name, $attrs) {
146
147 $tag = array_pop(split(":", $name));
148
149 array_unshift($this->ns_contexts, $this->ns_decls);
150
151 $this->depth++;
152
153 #print str_repeat(" ", $this->depth * $this->indent) . "start_element('$name')" ."\n";
154 #print str_repeat(" ", $this->depth+1 * $this->indent) . print_r($this->ns_contexts,true) ."\n";
155
156 if(!empty($this->in_content)) {
157 $attrs_prefix = array();
158
159 // resolve prefixes for attributes
160 foreach($attrs as $key => $value) {
161 $attrs_prefix[$this->ns_to_prefix($key)] = $this->xml_escape($value);
162 }
163 $attrs_str = join(' ', array_map($this->map_attrs_func, array_keys($attrs_prefix), array_values($attrs_prefix)));
164 if(strlen($attrs_str) > 0) {
165 $attrs_str = " " . $attrs_str;
166 }
167
168 $xmlns_str = join(' ', array_map($this->map_xmlns_func, array_keys($this->ns_contexts[0]), array_values($this->ns_contexts[0])));
169 if(strlen($xmlns_str) > 0) {
170 $xmlns_str = " " . $xmlns_str;
171 }
172
173 // handle self-closing tags (case: a new child found right-away, no text node)
174 if(count($this->in_content) == 2) {
175 array_push($this->in_content, ">");
176 }
177
178 array_push($this->in_content, "<". $this->ns_to_prefix($name) ."{$xmlns_str}{$attrs_str}");
179 } else if(in_array($tag, $this->ATOM_CONTENT_ELEMENTS) || in_array($tag, $this->ATOM_SIMPLE_ELEMENTS)) {
180 $this->in_content = array();
181 $this->is_xhtml = $attrs['type'] == 'xhtml';
182 array_push($this->in_content, array($tag,$this->depth));
183 } else if($tag == 'link') {
184 array_push($this->entry->links, $attrs);
185 } else if($tag == 'category') {
186 array_push($this->entry->categories, $attrs);
187 }
188
189 $this->ns_decls = array();
190 }
191
192 function end_element($parser, $name) {
193
194 $tag = array_pop(split(":", $name));
195
196 if(!empty($this->in_content)) {
197 if($this->in_content[0][0] == $tag &&
198 $this->in_content[0][1] == $this->depth) {
199 array_shift($this->in_content);
200 if($this->is_xhtml) {
201 $this->in_content = array_slice($this->in_content, 2, count($this->in_content)-3);
202 }
203 $this->entry->$tag = join('',$this->in_content);
204 $this->in_content = array();
205 } else {
206 $endtag = $this->ns_to_prefix($name);
207 if(strstr($this->in_content[count($this->in_content)-1], "<$endtag")) {
208 array_push($this->in_content, "/>");
209 } else {
210 array_push($this->in_content, "</$endtag>");
211 }
212 }
213 }
214
215 array_shift($this->ns_contexts);
216
217 #print str_repeat(" ", $this->depth * $this->indent) . "end_element('$name')" ."\n";
218
219 $this->depth--;
220 }
221
222 function start_ns($parser, $prefix, $uri) {
223 #print str_repeat(" ", $this->depth * $this->indent) . "starting: " . $prefix . ":" . $uri . "\n";
224 array_push($this->ns_decls, array($prefix,$uri));
225 }
226
227 function end_ns($parser, $prefix) {
228 #print str_repeat(" ", $this->depth * $this->indent) . "ending: #" . $prefix . "#\n";
229 }
230
231 function cdata($parser, $data) {
232 #print str_repeat(" ", $this->depth * $this->indent) . "data: #" . $data . "#\n";
233 if(!empty($this->in_content)) {
234 // handle self-closing tags (case: text node found, need to close element started)
235 if(strstr($this->in_content[count($this->in_content)-1], "<")) {
236 array_push($this->in_content, ">");
237 }
238 array_push($this->in_content, $this->xml_escape($data));
239 }
240 }
241
242 function _default($parser, $data) {
243 # when does this gets called?
244 }
245
246
247 function ns_to_prefix($qname) {
248 $components = split(":", $qname);
249 $name = array_pop($components);
250
251 if(!empty($components)) {
252 $ns = join(":",$components);
253 foreach($this->ns_contexts as $context) {
254 foreach($context as $mapping) {
255 if($mapping[1] == $ns && strlen($mapping[0]) > 0) {
256 return "$mapping[0]:$name";
257 }
258 }
259 }
260 }
261 return $name;
262 }
263
264 function xml_escape($string)
265 {
266 return str_replace(array('&','"',"'",'<','>'),
267 array('&amp;','&quot;','&apos;','&lt;','&gt;'),
268 $string );
269 }
270}
271
272class AtomServer {
273
274 var $ATOM_CONTENT_TYPE = 'application/atom+xml';
275
276 var $INTROSPECTION_CONTENT_TYPE = 'application/atomserv+xml';
277
278 var $ENTRIES_PATH = "posts";
279 var $MEDIA_PATH = "attachments";
280 var $ENTRY_PATH = "post";
281 var $MEDIA_SINGLE_PATH = "attachment";
282
283 var $params = array();
284 var $script_name = "app.php";
285 var $media_content_types = array('image/*','audio/*','video/*');
286 var $atom_content_types = array('application/atom+xml');
287
288 var $selectors = array();
289
290 // support for head
291 var $do_output = true;
292
293 function AtomServer() {
294
295 $this->script_name = array_pop(explode('/',$_SERVER['SCRIPT_NAME']));
296
297 $this->selectors = array(
298 '@/service@' =>
299 array('GET' => 'get_service'),
300 '@/post/(\d+)@' =>
301 array('GET' => 'get_post',
302 'PUT' => 'put_post',
303 'DELETE' => 'delete_post'),
304 '@/posts/?([^/]+)?@' =>
305 array('GET' => 'get_posts',
306 'POST' => 'create_post'),
307 '@/attachments/?(\d+)?@' =>
308 array('GET' => 'get_attachments',
309 'POST' => 'create_attachment'),
310 '@/attachment/file/(\d+)@' =>
311 array('GET' => 'get_file',
312 'PUT' => 'put_file',
313 'DELETE' => 'delete_file'),
314 '@/attachment/(\d+)@' =>
315 array('GET' => 'get_attachment',
316 'PUT' => 'put_attachment',
317 'DELETE' => 'delete_attachment'),
318 );
319 }
320
321 function handle_request() {
322
323 $path = $_SERVER['PATH_INFO'];
324 $method = $_SERVER['REQUEST_METHOD'];
325
326 $this->process_conditionals();
327
328 // exception case for HEAD (treat exactly as GET, but don't output)
329 if($method == 'HEAD') {
330 $this->do_output = false;
331 $method = 'GET';
332 }
333
334 // lame.
335 if(strlen($path) == 0) {
336 $path = '/service';
337 }
338
339 // authenticate regardless of the operation and set the current
340 // user. each handler will decide if auth is required or not.
341 $this->authenticate();
342
343 // dispatch
344 foreach($this->selectors as $regex => $funcs) {
345 if(preg_match($regex, $path, $matches)) {
346 if(isset($funcs[$method])) {
347 array_shift($matches);
348 call_user_func_array(array(&$this,$funcs[$method]), $matches);
349 exit();
350 } else {
351 // only allow what we have handlers for...
352 $this->not_allowed(array_keys($funcs));
353 }
354 }
355 }
356
357 // oops, nothing found
358 $this->not_found();
359 }
360
361 function get_service() {
362 $entries_url = $this->get_entries_url();
363 $media_url = $this->get_attachments_url();
364 $accepted_content_types = join(',',$this->media_content_types);
365 $introspection = <<<EOD
366<service xmlns="http://purl.org/atom/app#" xmlns:atom="http://www.w3.org/2005/Atom">
367 <workspace title="WordPress Experimental Workspace">
368 <collection href="$entries_url">
369 <accept>entry</accept>
370 <atom:title>WordPress Posts</atom:title>
371 </collection>
372 <collection href="$media_url">
373 <accept>$accepted_content_types</accept>
374 <atom:title>WordPress Media</atom:title>
375 </collection>
376 </workspace>
377</service>
378
379EOD;
380 $this->output($introspection, $this->INTROSPECTION_CONTENT_TYPE);
381 }
382
383 /*
384 * Create Post (No arguments)
385 */
386 function create_post() {
387
388 $this->get_accepted_content_type($this->atom_content_types);
389
390 $parser = new AtomParser();
391 if(!$parser->parse()) {
392 $this->client_error();
393 }
394
395 $entry = $parser->entry;
396
397 $publish = (isset($entry->draft) && trim($entry->draft) == 'yes') ? false : true;
398
399 $cap = ($publish) ? 'publish_posts' : 'edit_posts';
400
401 if(!current_user_can($cap))
402 $this->auth_required('Sorry, you do not have the right to edit/publish new posts.');
403
404 $blog_ID = 1;
405 $post_status = ($publish) ? 'publish' : 'draft';
406 $post_author = $user->ID;
407 $post_title = $entry->title;
408 $post_content = $entry->content;
409 $post_date = current_time('mysql');
410 $post_date_gmt = current_time('mysql', 1);
411
412 $post_data = compact('blog_ID', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status');
413
414 $postID = wp_insert_post($post_data);
415
416 if (!$postID) {
417 $this->internal_error('Sorry, your entry could not be posted. Something wrong happened.');
418 }
419
420 $output = $this->get_entry($postID);
421
422 $this->created($postID, $output);
423 }
424
425 function get_post($postID) {
426
427 global $entry;
428 $this->set_current_entry($postID);
429 $output = $this->get_entry($postID);
430 $this->output($output);
431
432 }
433
434 function put_post($postID) {
435
436 // checked for valid content-types (atom+xml)
437 // quick check and exit
438 $this->get_accepted_content_type($this->atom_content_types);
439
440 $parser = new AtomParser();
441 if(!$parser->parse()) {
442 $this->bad_request();
443 }
444
445 $parsed = $parser->entry;
446
447 // check for not found
448 global $entry;
449 $this->set_current_entry($postID);
450 $this->escape($entry);
451
452 if(!current_user_can('edit_post', $entry['ID']))
453 $this->auth_required('Sorry, you do not have the right to edit this post.');
454
455 $publish = (isset($parsed->draft) && trim($parsed->draft) == 'yes') ? false : true;
456
457 extract($entry);
458
459 $post_title = $parsed->title;
460 $post_content = $parsed->content;
461
462 // let's not go backwards and make something draft again.
463 if(!$publish && $post_status == 'draft') {
464 $post_status = ($publish) ? 'publish' : 'draft';
465 }
466
467 $postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt');
468
469 $result = wp_update_post($postdata);
470
471 if (!$result) {
472 $this->internal_error('For some strange yet very annoying reason, this post could not be edited.');
473 }
474
475 $this->ok();
476 }
477
478 function delete_post($postID) {
479
480 // check for not found
481 global $entry;
482 $this->set_current_entry($postID);
483
484 if(!current_user_can('edit_post', $postID)) {
485 $this->auth_required('Sorry, you do not have the right to delete this post.');
486 }
487
488 $result = wp_delete_post($postID);
489
490 if (!$result) {
491 $this->internal_error('For some strange yet very annoying reason, this post could not be deleted.');
492 }
493
494 $this->ok();
495 }
496
497 function create_attachment() {
498
499 $type = $this->get_accepted_content_type();
500
501 if(!current_user_can('upload_files'))
502 $this->auth_required('You do not have permission to upload files.');
503
504 $fp = fopen("php://input", "rb");
505 $bits = NULL;
506 while(!feof($fp)) {
507 $bits .= fread($fp, 4096);
508 }
509 fclose($fp);
510
511 $file = wp_upload_bits('noah.jpg',NULL,$bits);
512
513 $url = $file['url'];
514 $file = $file['file'];
515 $filename = basename($file);
516
517 // Construct the attachment array
518 $attachment = array(
519 'post_title' => $imgtitle ? $imgtitle : $filename,
520 'post_content' => $descr,
521 'post_status' => 'attachment',
522 'post_parent' => 0,
523 'post_mime_type' => $type,
524 'guid' => $url
525 );
526
527 // Save the data
528 $postID = wp_insert_attachment($attachment, $file, $post);
529
530 if (!$postID) {
531 $this->internal_error('Sorry, your entry could not be posted. Something wrong happened.');
532 }
533
534 $output = $this->get_entry($postID, 'attachment');
535
536 $this->created($postID, $output, 'attachment');
537 }
538
539 function put_attachment($postID) {
540
541 // checked for valid content-types (atom+xml)
542 // quick check and exit
543 $this->get_accepted_content_type($this->atom_content_types);
544
545 $parser = new AtomParser();
546 if(!$parser->parse()) {
547 $this->bad_request();
548 }
549
550 $parsed = $parser->entry;
551
552 // check for not found
553 global $entry;
554 $this->set_current_entry($postID);
555 $this->escape($entry);
556
557 if(!current_user_can('edit_post', $entry['ID']))
558 $this->auth_required('Sorry, you do not have the right to edit this post.');
559
560 $publish = (isset($parsed->draft) && trim($parsed->draft) == 'yes') ? false : true;
561
562 extract($entry);
563
564 $post_title = $parsed->title;
565 $post_content = $parsed->content;
566
567 $postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt');
568
569 $result = wp_update_post($postdata);
570
571 if (!$result) {
572 $this->internal_error('For some strange yet very annoying reason, this post could not be edited.');
573 }
574
575 $this->ok();
576 }
577
578 function delete_attachment($postID) {
579
580 // check for not found
581 global $entry;
582 $this->set_current_entry($postID);
583
584 if(!current_user_can('edit_post', $postID)) {
585 $this->auth_required('Sorry, you do not have the right to delete this post.');
586 }
587
588 $location = get_post_meta($entry['ID'], '_wp_attached_file', true);
589
590 // delete file
591 @unlink($location);
592
593 // delete attachment
594 $result = wp_delete_post($postID);
595
596 if (!$result) {
597 $this->internal_error('For some strange yet very annoying reason, this post could not be deleted.');
598 }
599
600 $this->ok();
601 }
602
603 function get_file($postID) {
604
605 // check for not found
606 global $entry;
607 $this->set_current_entry($postID);
608
609 // then whether user can edit the specific post
610 if(!current_user_can('edit_post', $postID)) {
611 $this->auth_required('Sorry, you do not have the right to edit this post.');
612 }
613
614 $location = get_post_meta($entry['ID'], '_wp_attached_file', true);
615
616 if(!isset($location))
617 $this->internal_error('Error ocurred while accessing post metadata for file location.');
618
619 header('Content-Type: ' . $entry['post_mime_type']);
620
621 $fp = fopen($location, "rb");
622 while(!feof($fp)) {
623 echo fread($fp, 4096);
624 }
625 fclose($fp);
626
627 $this->ok();
628 }
629
630 function put_file($postID) {
631
632 $type = $this->get_accepted_content_type();
633
634 // first check if user can upload
635 if(!current_user_can('upload_files'))
636 $this->auth_required('You do not have permission to upload files.');
637
638 // check for not found
639 global $entry;
640 $this->set_current_entry($postID);
641
642 // then whether user can edit the specific post
643 if(!current_user_can('edit_post', $postID)) {
644 $this->auth_required('Sorry, you do not have the right to edit this post.');
645 }
646
647 $location = get_post_meta($entry['ID'], '_wp_attached_file', true);
648
649 if(!isset($location))
650 $this->internal_error('Error ocurred while accessing post metadata for file location.');
651
652 $fp = fopen("php://input", "rb");
653 $localfp = fopen($location, "w+");
654 while(!feof($fp)) {
655 fwrite($localfp,fread($fp, 4096));
656 }
657 fclose($fp);
658 fclose($localfp);
659
660 $this->ok();
661 }
662
663 function get_entries_url($page = NULL) {
664 $url = get_bloginfo('url') . '/' . $this->script_name . '/' . $this->ENTRIES_PATH;
665 if(isset($page) && is_int($page)) {
666 $url .= "/$page";
667 }
668 return $url;
669 }
670
671 function the_entries_url($page = NULL) {
672 $url = $this->get_entries_url($page);
673 echo $url;
674 }
675
676 function get_attachments_url($page = NULL) {
677 $url = get_bloginfo('url') . '/' . $this->script_name . '/' . $this->MEDIA_PATH;
678 if(isset($page) && is_int($page)) {
679 $url .= "/$page";
680 }
681 return $url;
682 }
683
684 function the_attachments_url($page = NULL) {
685 $url = $this->get_attachments_url($page);
686 echo $url;
687 }
688
689
690 function get_entry_url($postID = NULL) {
691 if(!isset($postID)) {
692 global $post;
693 $postID = $post->ID;
694 }
695
696 $url = get_bloginfo('url') . '/' . $this->script_name . '/' . $this->ENTRY_PATH . "/$postID";
697 return $url;
698 }
699
700 function the_entry_url($postID = NULL) {
701 $url = $this->get_entry_url($postID);
702 echo $url;
703 }
704
705 function get_media_url($postID = NULL) {
706 if(!isset($postID)) {
707 global $post;
708 $postID = $post->ID;
709 }
710
711 $url = get_bloginfo('url') . '/' . $this->script_name . '/' . $this->MEDIA_SINGLE_PATH ."/$postID";
712 return $url;
713 }
714
715 function the_media_url($postID = NULL) {
716 $url = $this->get_media_url($postID);
717 echo $url;
718 }
719
720 function set_current_entry($postID) {
721 global $entry;
722
723 if(!isset($postID)) {
724 $this->bad_request();
725 }
726
727 $entry = wp_get_single_post($postID,ARRAY_A);
728
729 if(!isset($entry) || !isset($entry['ID']))
730 $this->not_found();
731
732 return;
733 }
734
735 function get_posts_count() {
736 global $wpdb;
737 return $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_date_gmt < '" . gmdate("Y-m-d H:i:s",time()) . "'");
738 }
739
740
741 function get_posts($page = 1) {
742 $feed = $this->get_feed($page);
743 $this->output($feed, $do_output);
744 }
745
746 function get_feed($page = 1, $post_type = 'post') {
747 log_app("get_feed", $post_type);
748 ob_start();
749
750 if(!isset($page)) {
751 $page = 1;
752 }
753 $page = (int) $page;
754
755 $count = get_settings('posts_per_rss');
756 $query = "paged=$page&posts_per_page=$count&order=DESC";
757 if($post_type == 'attachment') {
758 $query .= "&show_post_type=$post_type";
759 }
760 query_posts($query);
761 global $post;
762
763 $feed_update = get_lastpostmodified('GMT') ? get_lastpostmodified('GMT') : current_time('mysql', 1);
764 $total_count = $this->get_posts_count();
765 $last_page = (int) ceil($total_count / $count);
766 $next_page = (($page + 1) > $last_page) ? NULL : $page + 1;
767 $prev_page = ($page - 1) < 1 ? NULL : $page - 1;
768 $last_page = ((int)$last_page <= 1) ? NULL : (int) $last_page;
769?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:app="http://purl.org/atom/app#" xml:lang="<?php echo get_option('rss_language'); ?>">
770<id><?php $this->the_entries_url() ?></id>
771<updated><?php echo mysql2date('Y-m-d\TH:i:s\Z', $feed_update); ?></updated>
772<title type="text"><?php bloginfo_rss('name') ?></title>
773<subtitle type="text"><?php bloginfo_rss("description") ?></subtitle>
774<link rel="first" type="application/atom+xml" href="<?php $this->the_entries_url() ?>" />
775<?php if(isset($prev_page)): ?>
776<link rel="previous" type="application/atom+xml" href="<?php $this->the_entries_url($prev_page) ?>" />
777<?php endif; ?>
778<?php if(isset($next_page)): ?>
779<link rel="next" type="application/atom+xml" href="<?php $this->the_entries_url($next_page) ?>" />
780<?php endif; ?>
781<link rel="last" type="application/atom+xml" href="<?php $this->the_entries_url($last_page) ?>" />
782<link rel="self" type="application/atom+xml" href="<?php $this->the_entries_url() ?>" />
783<rights type="text">Copyright <?php echo mysql2date('Y', get_lastpostdate('blog')); ?></rights>
784<generator uri="http://wordpress.org/" version="<?php bloginfo_rss('version'); ?>">WordPress APP</generator>
785<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
786<entry>
787 <id><?php the_guid(); ?></id>
788 <title type="html"><![CDATA[<?php the_title_rss() ?>]]></title>
789 <updated><?php echo get_post_modified_time('Y-m-d\TH:i:s\Z', true); ?></updated>
790 <published><?php echo get_post_time('Y-m-d\TH:i:s\Z', true); ?></published>
791 <app:control>
792 <app:draft><?php echo ($post->post_status == 'draft' ? 'yes' : 'no') ?></app:draft>
793 </app:control>
794 <author>
795 <name><?php the_author()?></name>
796 <email><?php the_author_email()?></email>
797<?php if (get_the_author_url() && get_the_author_url() != 'http://') { ?>
798 <uri><?php the_author_url()?></uri>
799<?php } ?>
800 </author>
801<?php if($post->post_status == 'attachment') { ?>
802 <link rel="edit" href="<?php $this->the_entry_url() ?>" />
803 <link rel="edit-media" href="<?php $this->the_media_url() ?>" />
804<?php } else { ?>
805 <link href="<?php permalink_single_rss() ?>" />
806 <link rel="edit" href="<?php $this->the_entry_url() ?>" />
807<?php } ?>
808<?php foreach(get_the_category() as $category) { ?>
809 <category scheme="<?php bloginfo_rss('home') ?>" term="<?php echo $category->cat_name?>" />
810<?php } ?>
811 <summary type="html"><![CDATA[<?php the_excerpt_rss(); ?>]]></summary>
812</entry>
813<?php
814 endwhile;
815 endif;
816?></feed>
817<?php
818 $feed = ob_get_contents();
819 ob_end_clean();
820 log_app("get_feed", $feed);
821 return $feed;
822 }
823
824 function get_entry($postID, $post_type = 'post') {
825 ob_start();
826 global $post;
827 switch($post_type) {
828 case 'post':
829 $varname = 'p';
830 break;
831 case 'attachment':
832 $varname = 'attachment_id';
833 break;
834 }
835 query_posts($varname . '=' . $postID);
836 if ( have_posts() ) : while ( have_posts() ) : the_post();?>
837<entry xmlns="http://www.w3.org/2005/Atom" xmlns:app="http://purl.org/atom/app#" xml:lang="<?php echo get_option('rss_language'); ?>">
838 <id><?php the_guid(); ?></id>
839 <title type="html"><![CDATA[<?php the_title_rss() ?>]]></title>
840 <updated><?php echo get_post_modified_time('Y-m-d\TH:i:s\Z', true); ?></updated>
841 <published><?php echo get_post_time('Y-m-d\TH:i:s\Z', true); ?></published>
842 <app:control>
843 <app:draft><?php echo ($post->post_status == 'draft' ? 'yes' : 'no') ?></app:draft>
844 </app:control>
845 <author>
846 <name><?php the_author()?></name>
847 <email><?php the_author_email()?></email>
848 <uri><?php the_author_url()?></uri>
849 </author>
850<?php if($post->post_status == 'attachment') { ?>
851 <link rel="edit" href="<?php $this->the_entry_url() ?>" />
852 <link rel="edit-media" href="<?php $this->the_media_url() ?>" />
853 <content type="<?php echo $post->post_mime_type ?>" src="<?php the_guid(); ?>"/>
854<?php } else { ?>
855 <link href="<?php permalink_single_rss() ?>" />
856 <link rel="edit" href="<?php $this->the_entry_url() ?>" />
857<?php } ?>
858<?php foreach(get_the_category() as $category) { ?>
859 <category scheme="<?php bloginfo_rss('home') ?>" term="<?php echo $category->cat_name?>" />
860 <summary type="html"><![CDATA[<?php the_excerpt_rss(); ?>]]></summary>
861<?php }
862 if ( strlen( $post->post_content ) ) : ?>
863 <content type="html"><![CDATA[<?php echo get_the_content('', 0, '') ?>]]></content>
864<?php endif; ?>
865</entry>
866<?php
867 $entry = ob_get_contents();
868 break;
869 endwhile;
870 else:
871 $this->auth_required("Access Denied.");
872 endif;
873 ob_end_clean();
874 log_app("get_entry", $entry);
875 return $entry;
876 }
877
878 function ok() {
879 header('Content-Type: text/plain');
880 status_header('200');
881 exit;
882 }
883
884 function internal_error($msg = 'Internal Server Error') {
885 header('Content-Type: text/plain');
886 status_header('500');
887 echo $msg;
888 exit;
889 }
890
891 function bad_request() {
892 header('Content-Type: text/plain');
893 status_header('400');
894 exit;
895 }
896
897 function length_required() {
898 header("HTTP/1.1 411 Length Required");
899 header('Content-Type: text/plain');
900 status_header('411');
901 exit;
902 }
903
904 function invalid_media() {
905 header("HTTP/1.1 415 Unsupported Media Type");
906 header('Content-Type: text/plain');
907 exit;
908 }
909
910 function not_found() {
911 header('Content-Type: text/plain');
912 status_header('404');
913 exit;
914 }
915
916 function not_allowed($allow) {
917 header('Allow: ' . join(',', $allow));
918 status_header('405');
919 exit;
920 }
921
922 function client_error($msg = 'Client Error') {
923 status_header('400');
924 header('Content-Type: text/plain');
925 header('Status: ' . $msg);
926 echo $msg;
927 exit;
928 }
929
930 function created($post_ID, $content, $post_type = 'post') {
931 log_app('created()::$post_ID',"$post_ID");
932 $edit = $this->get_entry_url($post_ID);
933 switch($post_type) {
934 case 'post':
935 $ctloc = $this->get_entry_url($post_ID);
936 break;
937 case 'attachment':
938 break;
939 }
940 header('Content-Type: application/atom+xml');
941 if(isset($ctloc))
942 header('Content-Location: ' . $ctloc);
943 header('Location: ' . $edit);
944 status_header('201');
945 echo $content;
946 exit;
947 }
948
949 function auth_required($msg) {
950 nocache_headers();
951 header('WWW-Authenticate: Basic realm="WordPress Atom Protocol"');
952 header("HTTP/1.1 401 $msg");
953 header('Status: ' . $msg);
954 header('Content-Type: text/plain');
955 echo $msg;
956 exit;
957 }
958
959 function output($xml, $ctype = "application/atom+xml") {
960 status_header('200');
961 $xml = '<?xml version="1.0" encoding="' . get_settings('blog_charset') . '"?>'."\n".$xml;
962 header('Connection: close');
963 header('Content-Length: '. strlen($xml));
964 header('Content-Type: ' . $ctype);
965 header('Content-Disposition: attachment; filename=atom.xml');
966 header('Date: '. date('r'));
967 if($this->do_output)
968 echo $xml;
969 exit;
970 }
971
972 function escape(&$array) {
973 global $wpdb;
974
975 foreach ($array as $k => $v) {
976 if (is_array($v)) {
977 $this->escape($array[$k]);
978 } else if (is_object($v)) {
979 //skip
980 } else {
981 $array[$k] = $wpdb->escape($v);
982 }
983 }
984 }
985
986
987 /*
988 * Access credential through various methods and perform login
989 */
990 function authenticate() {
991 $login_data = array();
992 $already_md5 = false;
993
994 log_app("authenticate()",print_r($_ENV, true));
995
996 // if using mod_rewrite/ENV hack
997 // http://www.besthostratings.com/articles/http-auth-php-cgi.html
998 if(isset($_SERVER['HTTP_AUTHORIZATION'])) {
999 list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) =
1000 explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
1001 }
1002
1003 // If Basic Auth is working...
1004 if(isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
1005 $login_data = array('login' => $_SERVER['PHP_AUTH_USER'], 'password' => $_SERVER['PHP_AUTH_PW']);
1006 } else {
1007 // else, do cookie-based authentication
1008 if (function_exists('wp_get_cookie_login')) {
1009 $login_data = wp_get_cookie_login();
1010 $already_md5 = true;
1011 }
1012 }
1013
1014 // call wp_login and set current user
1015 if (!empty($login_data) && wp_login($login_data['login'], $login_data['password'], $already_md5)) {
1016 $current_user = new WP_User(0, $login_data['login']);
1017 wp_set_current_user($current_user->ID);
1018 }
1019 }
1020
1021 function get_accepted_content_type($types = NULL) {
1022
1023 if(!isset($types)) {
1024 $types = $this->media_content_types;
1025 }
1026
1027 if(!isset($_SERVER['CONTENT_LENGTH']) || !isset($_SERVER['CONTENT_TYPE'])) {
1028 $this->length_required();
1029 }
1030
1031 $type = $_SERVER['CONTENT_TYPE'];
1032 list($type,$subtype) = explode('/',$type);
1033 list($subtype) = explode(";",$subtype); // strip MIME parameters
1034 log_app("get_accepted_content_type", "type=$type, subtype=$subtype");
1035
1036 foreach($types as $t) {
1037 list($acceptedType,$acceptedSubtype) = explode('/',$t);
1038 if($acceptedType == '*' || $acceptedType == $type) {
1039 if($acceptedSubtype == '*' || $acceptedSubtype == $subtype)
1040 return $type;
1041 }
1042 }
1043
1044 $this->invalid_media();
1045 }
1046
1047
1048
1049 function process_conditionals() {
1050
1051 if(empty($this->params)) return;
1052 if($_SERVER['REQUEST_METHOD'] == 'DELETE') return;
1053
1054 switch($this->params[0]) {
1055 case $this->ENTRY_PATH:
1056 global $post;
1057 $post = wp_get_single_post($this->params[1]);
1058 $wp_last_modified = get_post_modified_time('D, d M Y H:i:s', true);
1059 $post = NULL;
1060 break;
1061 case $this->ENTRIES_PATH:
1062 $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0).' GMT';
1063 break;
1064 default:
1065 return;
1066 }
1067 $wp_etag = md5($wp_last_modified);
1068 @header("Last-Modified: $wp_last_modified");
1069 @header("ETag: $wp_etag");
1070
1071 // Support for Conditional GET
1072 if (isset($_SERVER['HTTP_IF_NONE_MATCH']))
1073 $client_etag = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']);
1074 else
1075 $client_etag = false;
1076
1077 $client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE']);
1078 // If string is empty, return 0. If not, attempt to parse into a timestamp
1079 $client_modified_timestamp = $client_last_modified ? strtotime($client_last_modified) : 0;
1080
1081 // Make a timestamp for our most recent modification...
1082 $wp_modified_timestamp = strtotime($wp_last_modified);
1083
1084 if ( ($client_last_modified && $client_etag) ?
1085 (($client_modified_timestamp >= $wp_modified_timestamp) && ($client_etag == $wp_etag)) :
1086 (($client_modified_timestamp >= $wp_modified_timestamp) || ($client_etag == $wp_etag)) ) {
1087 status_header( 304 );
1088 exit;
1089 }
1090 }
1091
1092
1093}
1094
1095$server = new AtomServer();
1096$server->handle_request();
1097
1098?>