raw
mp-wp_genesis           1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * Atom Publishing Protocol support for WordPress
mp-wp_genesis 4 *
mp-wp_genesis 5 * @author Original by Elias Torres <http://torrez.us/archives/2006/08/31/491/>
mp-wp_genesis 6 * @author Modified by Dougal Campbell <http://dougal.gunters.org/>
mp-wp_genesis 7 * @version 1.0.5-dc
mp-wp_genesis 8 */
mp-wp_genesis 9
mp-wp_genesis 10 /**
mp-wp_genesis 11 * WordPress is handling an Atom Publishing Protocol request.
mp-wp_genesis 12 *
mp-wp_genesis 13 * @var bool
mp-wp_genesis 14 */
mp-wp_genesis 15 define('APP_REQUEST', true);
mp-wp_genesis 16
mp-wp_genesis 17 /** Set up WordPress environment */
mp-wp_genesis 18 require_once('./wp-load.php');
mp-wp_genesis 19
mp-wp_genesis 20 /** Post Template API */
mp-wp_genesis 21 require_once(ABSPATH . WPINC . '/post-template.php');
mp-wp_genesis 22
mp-wp_genesis 23 /** Atom Publishing Protocol Class */
mp-wp_genesis 24 require_once(ABSPATH . WPINC . '/atomlib.php');
mp-wp_genesis 25
mp-wp_genesis 26 /** Feed Handling API */
mp-wp_genesis 27 require_once(ABSPATH . WPINC . '/feed.php');
mp-wp_genesis 28
mp-wp_genesis 29 $_SERVER['PATH_INFO'] = preg_replace( '/.*\/wp-app\.php/', '', $_SERVER['REQUEST_URI'] );
mp-wp_genesis 30
mp-wp_genesis 31 /**
mp-wp_genesis 32 * Whether to enable Atom Publishing Protocol Logging.
mp-wp_genesis 33 *
mp-wp_genesis 34 * @name app_logging
mp-wp_genesis 35 * @var int|bool
mp-wp_genesis 36 */
mp-wp_genesis 37 $app_logging = 0;
mp-wp_genesis 38
mp-wp_genesis 39 /**
mp-wp_genesis 40 * Whether to always authenticate user. Permanently set to true.
mp-wp_genesis 41 *
mp-wp_genesis 42 * @name always_authenticate
mp-wp_genesis 43 * @var int|bool
mp-wp_genesis 44 * @todo Should be an option somewhere
mp-wp_genesis 45 */
mp-wp_genesis 46 $always_authenticate = 1;
mp-wp_genesis 47
mp-wp_genesis 48 /**
mp-wp_genesis 49 * Writes logging info to a file.
mp-wp_genesis 50 *
mp-wp_genesis 51 * @since 2.2.0
mp-wp_genesis 52 * @uses $app_logging
mp-wp_genesis 53 * @package WordPress
mp-wp_genesis 54 * @subpackage Logging
mp-wp_genesis 55 *
mp-wp_genesis 56 * @param string $label Type of logging
mp-wp_genesis 57 * @param string $msg Information describing logging reason.
mp-wp_genesis 58 */
mp-wp_genesis 59 function log_app($label,$msg) {
mp-wp_genesis 60 global $app_logging;
mp-wp_genesis 61 if ($app_logging) {
mp-wp_genesis 62 $fp = fopen( 'wp-app.log', 'a+');
mp-wp_genesis 63 $date = gmdate( 'Y-m-d H:i:s' );
mp-wp_genesis 64 fwrite($fp, "\n\n$date - $label\n$msg\n");
mp-wp_genesis 65 fclose($fp);
mp-wp_genesis 66 }
mp-wp_genesis 67 }
mp-wp_genesis 68
mp-wp_genesis 69 if ( !function_exists('wp_set_current_user') ) :
mp-wp_genesis 70 /**
mp-wp_genesis 71 * @ignore
mp-wp_genesis 72 */
mp-wp_genesis 73 function wp_set_current_user($id, $name = '') {
mp-wp_genesis 74 global $current_user;
mp-wp_genesis 75
mp-wp_genesis 76 if ( isset($current_user) && ($id == $current_user->ID) )
mp-wp_genesis 77 return $current_user;
mp-wp_genesis 78
mp-wp_genesis 79 $current_user = new WP_User($id, $name);
mp-wp_genesis 80
mp-wp_genesis 81 return $current_user;
mp-wp_genesis 82 }
mp-wp_genesis 83 endif;
mp-wp_genesis 84
mp-wp_genesis 85 /**
mp-wp_genesis 86 * Filter to add more post statuses.
mp-wp_genesis 87 *
mp-wp_genesis 88 * @since 2.2.0
mp-wp_genesis 89 *
mp-wp_genesis 90 * @param string $where SQL statement to filter.
mp-wp_genesis 91 * @return string Filtered SQL statement with added post_status for where clause.
mp-wp_genesis 92 */
mp-wp_genesis 93 function wa_posts_where_include_drafts_filter($where) {
mp-wp_genesis 94 $where = str_replace("post_status = 'publish'","post_status = 'publish' OR post_status = 'future' OR post_status = 'draft' OR post_status = 'inherit'", $where);
mp-wp_genesis 95 return $where;
mp-wp_genesis 96
mp-wp_genesis 97 }
mp-wp_genesis 98 add_filter('posts_where', 'wa_posts_where_include_drafts_filter');
mp-wp_genesis 99
mp-wp_genesis 100 /**
mp-wp_genesis 101 * WordPress AtomPub API implementation.
mp-wp_genesis 102 *
mp-wp_genesis 103 * @package WordPress
mp-wp_genesis 104 * @subpackage Publishing
mp-wp_genesis 105 * @since 2.2.0
mp-wp_genesis 106 */
mp-wp_genesis 107 class AtomServer {
mp-wp_genesis 108
mp-wp_genesis 109 /**
mp-wp_genesis 110 * ATOM content type.
mp-wp_genesis 111 *
mp-wp_genesis 112 * @since 2.2.0
mp-wp_genesis 113 * @var string
mp-wp_genesis 114 */
mp-wp_genesis 115 var $ATOM_CONTENT_TYPE = 'application/atom+xml';
mp-wp_genesis 116
mp-wp_genesis 117 /**
mp-wp_genesis 118 * Categories ATOM content type.
mp-wp_genesis 119 *
mp-wp_genesis 120 * @since 2.2.0
mp-wp_genesis 121 * @var string
mp-wp_genesis 122 */
mp-wp_genesis 123 var $CATEGORIES_CONTENT_TYPE = 'application/atomcat+xml';
mp-wp_genesis 124
mp-wp_genesis 125 /**
mp-wp_genesis 126 * Service ATOM content type.
mp-wp_genesis 127 *
mp-wp_genesis 128 * @since 2.3.0
mp-wp_genesis 129 * @var string
mp-wp_genesis 130 */
mp-wp_genesis 131 var $SERVICE_CONTENT_TYPE = 'application/atomsvc+xml';
mp-wp_genesis 132
mp-wp_genesis 133 /**
mp-wp_genesis 134 * ATOM XML namespace.
mp-wp_genesis 135 *
mp-wp_genesis 136 * @since 2.3.0
mp-wp_genesis 137 * @var string
mp-wp_genesis 138 */
mp-wp_genesis 139 var $ATOM_NS = 'http://www.w3.org/2005/Atom';
mp-wp_genesis 140
mp-wp_genesis 141 /**
mp-wp_genesis 142 * ATOMPUB XML namespace.
mp-wp_genesis 143 *
mp-wp_genesis 144 * @since 2.3.0
mp-wp_genesis 145 * @var string
mp-wp_genesis 146 */
mp-wp_genesis 147 var $ATOMPUB_NS = 'http://www.w3.org/2007/app';
mp-wp_genesis 148
mp-wp_genesis 149 /**
mp-wp_genesis 150 * Entries path.
mp-wp_genesis 151 *
mp-wp_genesis 152 * @since 2.2.0
mp-wp_genesis 153 * @var string
mp-wp_genesis 154 */
mp-wp_genesis 155 var $ENTRIES_PATH = "posts";
mp-wp_genesis 156
mp-wp_genesis 157 /**
mp-wp_genesis 158 * Categories path.
mp-wp_genesis 159 *
mp-wp_genesis 160 * @since 2.2.0
mp-wp_genesis 161 * @var string
mp-wp_genesis 162 */
mp-wp_genesis 163 var $CATEGORIES_PATH = "categories";
mp-wp_genesis 164
mp-wp_genesis 165 /**
mp-wp_genesis 166 * Media path.
mp-wp_genesis 167 *
mp-wp_genesis 168 * @since 2.2.0
mp-wp_genesis 169 * @var string
mp-wp_genesis 170 */
mp-wp_genesis 171 var $MEDIA_PATH = "attachments";
mp-wp_genesis 172
mp-wp_genesis 173 /**
mp-wp_genesis 174 * Entry path.
mp-wp_genesis 175 *
mp-wp_genesis 176 * @since 2.2.0
mp-wp_genesis 177 * @var string
mp-wp_genesis 178 */
mp-wp_genesis 179 var $ENTRY_PATH = "post";
mp-wp_genesis 180
mp-wp_genesis 181 /**
mp-wp_genesis 182 * Service path.
mp-wp_genesis 183 *
mp-wp_genesis 184 * @since 2.2.0
mp-wp_genesis 185 * @var string
mp-wp_genesis 186 */
mp-wp_genesis 187 var $SERVICE_PATH = "service";
mp-wp_genesis 188
mp-wp_genesis 189 /**
mp-wp_genesis 190 * Media single path.
mp-wp_genesis 191 *
mp-wp_genesis 192 * @since 2.2.0
mp-wp_genesis 193 * @var string
mp-wp_genesis 194 */
mp-wp_genesis 195 var $MEDIA_SINGLE_PATH = "attachment";
mp-wp_genesis 196
mp-wp_genesis 197 /**
mp-wp_genesis 198 * ATOMPUB parameters.
mp-wp_genesis 199 *
mp-wp_genesis 200 * @since 2.2.0
mp-wp_genesis 201 * @var array
mp-wp_genesis 202 */
mp-wp_genesis 203 var $params = array();
mp-wp_genesis 204
mp-wp_genesis 205 /**
mp-wp_genesis 206 * Supported ATOMPUB media types.
mp-wp_genesis 207 *
mp-wp_genesis 208 * @since 2.3.0
mp-wp_genesis 209 * @var array
mp-wp_genesis 210 */
mp-wp_genesis 211 var $media_content_types = array('image/*','audio/*','video/*');
mp-wp_genesis 212
mp-wp_genesis 213 /**
mp-wp_genesis 214 * ATOMPUB content type(s).
mp-wp_genesis 215 *
mp-wp_genesis 216 * @since 2.2.0
mp-wp_genesis 217 * @var array
mp-wp_genesis 218 */
mp-wp_genesis 219 var $atom_content_types = array('application/atom+xml');
mp-wp_genesis 220
mp-wp_genesis 221 /**
mp-wp_genesis 222 * ATOMPUB methods.
mp-wp_genesis 223 *
mp-wp_genesis 224 * @since 2.2.0
mp-wp_genesis 225 * @var unknown_type
mp-wp_genesis 226 */
mp-wp_genesis 227 var $selectors = array();
mp-wp_genesis 228
mp-wp_genesis 229 /**
mp-wp_genesis 230 * Whether to do output.
mp-wp_genesis 231 *
mp-wp_genesis 232 * Support for head.
mp-wp_genesis 233 *
mp-wp_genesis 234 * @since 2.2.0
mp-wp_genesis 235 * @var bool
mp-wp_genesis 236 */
mp-wp_genesis 237 var $do_output = true;
mp-wp_genesis 238
mp-wp_genesis 239 /**
mp-wp_genesis 240 * PHP4 constructor - Sets up object properties.
mp-wp_genesis 241 *
mp-wp_genesis 242 * @since 2.2.0
mp-wp_genesis 243 * @return AtomServer
mp-wp_genesis 244 */
mp-wp_genesis 245 function AtomServer() {
mp-wp_genesis 246
mp-wp_genesis 247 $this->script_name = array_pop(explode('/',$_SERVER['SCRIPT_NAME']));
mp-wp_genesis 248 $this->app_base = get_bloginfo('url') . '/' . $this->script_name . '/';
mp-wp_genesis 249 if ( isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ) {
mp-wp_genesis 250 $this->app_base = preg_replace( '/^http:\/\//', 'https://', $this->app_base );
mp-wp_genesis 251 }
mp-wp_genesis 252
mp-wp_genesis 253 $this->selectors = array(
mp-wp_genesis 254 '@/service$@' =>
mp-wp_genesis 255 array('GET' => 'get_service'),
mp-wp_genesis 256 '@/categories$@' =>
mp-wp_genesis 257 array('GET' => 'get_categories_xml'),
mp-wp_genesis 258 '@/post/(\d+)$@' =>
mp-wp_genesis 259 array('GET' => 'get_post',
mp-wp_genesis 260 'PUT' => 'put_post',
mp-wp_genesis 261 'DELETE' => 'delete_post'),
mp-wp_genesis 262 '@/posts/?(\d+)?$@' =>
mp-wp_genesis 263 array('GET' => 'get_posts',
mp-wp_genesis 264 'POST' => 'create_post'),
mp-wp_genesis 265 '@/attachments/?(\d+)?$@' =>
mp-wp_genesis 266 array('GET' => 'get_attachment',
mp-wp_genesis 267 'POST' => 'create_attachment'),
mp-wp_genesis 268 '@/attachment/file/(\d+)$@' =>
mp-wp_genesis 269 array('GET' => 'get_file',
mp-wp_genesis 270 'PUT' => 'put_file',
mp-wp_genesis 271 'DELETE' => 'delete_file'),
mp-wp_genesis 272 '@/attachment/(\d+)$@' =>
mp-wp_genesis 273 array('GET' => 'get_attachment',
mp-wp_genesis 274 'PUT' => 'put_attachment',
mp-wp_genesis 275 'DELETE' => 'delete_attachment'),
mp-wp_genesis 276 );
mp-wp_genesis 277 }
mp-wp_genesis 278
mp-wp_genesis 279 /**
mp-wp_genesis 280 * Handle ATOMPUB request.
mp-wp_genesis 281 *
mp-wp_genesis 282 * @since 2.2.0
mp-wp_genesis 283 */
mp-wp_genesis 284 function handle_request() {
mp-wp_genesis 285 global $always_authenticate;
mp-wp_genesis 286
mp-wp_genesis 287 if( !empty( $_SERVER['ORIG_PATH_INFO'] ) )
mp-wp_genesis 288 $path = $_SERVER['ORIG_PATH_INFO'];
mp-wp_genesis 289 else
mp-wp_genesis 290 $path = $_SERVER['PATH_INFO'];
mp-wp_genesis 291
mp-wp_genesis 292 $method = $_SERVER['REQUEST_METHOD'];
mp-wp_genesis 293
mp-wp_genesis 294 log_app('REQUEST',"$method $path\n================");
mp-wp_genesis 295
mp-wp_genesis 296 $this->process_conditionals();
mp-wp_genesis 297 //$this->process_conditionals();
mp-wp_genesis 298
mp-wp_genesis 299 // exception case for HEAD (treat exactly as GET, but don't output)
mp-wp_genesis 300 if($method == 'HEAD') {
mp-wp_genesis 301 $this->do_output = false;
mp-wp_genesis 302 $method = 'GET';
mp-wp_genesis 303 }
mp-wp_genesis 304
mp-wp_genesis 305 // redirect to /service in case no path is found.
mp-wp_genesis 306 if(strlen($path) == 0 || $path == '/') {
mp-wp_genesis 307 $this->redirect($this->get_service_url());
mp-wp_genesis 308 }
mp-wp_genesis 309
mp-wp_genesis 310 // check to see if AtomPub is enabled
mp-wp_genesis 311 if( !get_option( 'enable_app' ) )
mp-wp_genesis 312 $this->forbidden( sprintf( __( 'AtomPub services are disabled on this blog. An admin user can enable them at %s' ), admin_url('options-writing.php') ) );
mp-wp_genesis 313
mp-wp_genesis 314 // dispatch
mp-wp_genesis 315 foreach($this->selectors as $regex => $funcs) {
mp-wp_genesis 316 if(preg_match($regex, $path, $matches)) {
mp-wp_genesis 317 if(isset($funcs[$method])) {
mp-wp_genesis 318
mp-wp_genesis 319 // authenticate regardless of the operation and set the current
mp-wp_genesis 320 // user. each handler will decide if auth is required or not.
mp-wp_genesis 321 if(!$this->authenticate()) {
mp-wp_genesis 322 if ($always_authenticate) {
mp-wp_genesis 323 $this->auth_required('Credentials required.');
mp-wp_genesis 324 }
mp-wp_genesis 325 }
mp-wp_genesis 326
mp-wp_genesis 327 array_shift($matches);
mp-wp_genesis 328 call_user_func_array(array(&$this,$funcs[$method]), $matches);
mp-wp_genesis 329 exit();
mp-wp_genesis 330 } else {
mp-wp_genesis 331 // only allow what we have handlers for...
mp-wp_genesis 332 $this->not_allowed(array_keys($funcs));
mp-wp_genesis 333 }
mp-wp_genesis 334 }
mp-wp_genesis 335 }
mp-wp_genesis 336
mp-wp_genesis 337 // oops, nothing found
mp-wp_genesis 338 $this->not_found();
mp-wp_genesis 339 }
mp-wp_genesis 340
mp-wp_genesis 341 /**
mp-wp_genesis 342 * Retrieve XML for ATOMPUB service.
mp-wp_genesis 343 *
mp-wp_genesis 344 * @since 2.2.0
mp-wp_genesis 345 */
mp-wp_genesis 346 function get_service() {
mp-wp_genesis 347 log_app('function','get_service()');
mp-wp_genesis 348
mp-wp_genesis 349 if( !current_user_can( 'edit_posts' ) )
mp-wp_genesis 350 $this->auth_required( __( 'Sorry, you do not have the right to access this blog.' ) );
mp-wp_genesis 351
mp-wp_genesis 352 $entries_url = attribute_escape($this->get_entries_url());
mp-wp_genesis 353 $categories_url = attribute_escape($this->get_categories_url());
mp-wp_genesis 354 $media_url = attribute_escape($this->get_attachments_url());
mp-wp_genesis 355 foreach ($this->media_content_types as $med) {
mp-wp_genesis 356 $accepted_media_types = $accepted_media_types . "<accept>" . $med . "</accept>";
mp-wp_genesis 357 }
mp-wp_genesis 358 $atom_prefix="atom";
mp-wp_genesis 359 $atom_blogname=get_bloginfo('name');
mp-wp_genesis 360 $service_doc = <<<EOD
mp-wp_genesis 361 <service xmlns="$this->ATOMPUB_NS" xmlns:$atom_prefix="$this->ATOM_NS">
mp-wp_genesis 362 <workspace>
mp-wp_genesis 363 <$atom_prefix:title>$atom_blogname Workspace</$atom_prefix:title>
mp-wp_genesis 364 <collection href="$entries_url">
mp-wp_genesis 365 <$atom_prefix:title>$atom_blogname Posts</$atom_prefix:title>
mp-wp_genesis 366 <accept>$this->ATOM_CONTENT_TYPE;type=entry</accept>
mp-wp_genesis 367 <categories href="$categories_url" />
mp-wp_genesis 368 </collection>
mp-wp_genesis 369 <collection href="$media_url">
mp-wp_genesis 370 <$atom_prefix:title>$atom_blogname Media</$atom_prefix:title>
mp-wp_genesis 371 $accepted_media_types
mp-wp_genesis 372 </collection>
mp-wp_genesis 373 </workspace>
mp-wp_genesis 374 </service>
mp-wp_genesis 375
mp-wp_genesis 376 EOD;
mp-wp_genesis 377
mp-wp_genesis 378 $this->output($service_doc, $this->SERVICE_CONTENT_TYPE);
mp-wp_genesis 379 }
mp-wp_genesis 380
mp-wp_genesis 381 /**
mp-wp_genesis 382 * Retrieve categories list in XML format.
mp-wp_genesis 383 *
mp-wp_genesis 384 * @since 2.2.0
mp-wp_genesis 385 */
mp-wp_genesis 386 function get_categories_xml() {
mp-wp_genesis 387 log_app('function','get_categories_xml()');
mp-wp_genesis 388
mp-wp_genesis 389 if( !current_user_can( 'edit_posts' ) )
mp-wp_genesis 390 $this->auth_required( __( 'Sorry, you do not have the right to access this blog.' ) );
mp-wp_genesis 391
mp-wp_genesis 392 $home = attribute_escape(get_bloginfo_rss('home'));
mp-wp_genesis 393
mp-wp_genesis 394 $categories = "";
mp-wp_genesis 395 $cats = get_categories("hierarchical=0&hide_empty=0");
mp-wp_genesis 396 foreach ((array) $cats as $cat) {
mp-wp_genesis 397 $categories .= " <category term=\"" . attribute_escape($cat->name) . "\" />\n";
mp-wp_genesis 398 }
mp-wp_genesis 399 $output = <<<EOD
mp-wp_genesis 400 <app:categories xmlns:app="$this->ATOMPUB_NS"
mp-wp_genesis 401 xmlns="$this->ATOM_NS"
mp-wp_genesis 402 fixed="yes" scheme="$home">
mp-wp_genesis 403 $categories
mp-wp_genesis 404 </app:categories>
mp-wp_genesis 405 EOD;
mp-wp_genesis 406 $this->output($output, $this->CATEGORIES_CONTENT_TYPE);
mp-wp_genesis 407 }
mp-wp_genesis 408
mp-wp_genesis 409 /**
mp-wp_genesis 410 * Create new post.
mp-wp_genesis 411 *
mp-wp_genesis 412 * @since 2.2.0
mp-wp_genesis 413 */
mp-wp_genesis 414 function create_post() {
mp-wp_genesis 415 global $blog_id, $user_ID;
mp-wp_genesis 416 $this->get_accepted_content_type($this->atom_content_types);
mp-wp_genesis 417
mp-wp_genesis 418 $parser = new AtomParser();
mp-wp_genesis 419 if(!$parser->parse()) {
mp-wp_genesis 420 $this->client_error();
mp-wp_genesis 421 }
mp-wp_genesis 422
mp-wp_genesis 423 $entry = array_pop($parser->feed->entries);
mp-wp_genesis 424
mp-wp_genesis 425 log_app('Received entry:', print_r($entry,true));
mp-wp_genesis 426
mp-wp_genesis 427 $catnames = array();
mp-wp_genesis 428 foreach($entry->categories as $cat)
mp-wp_genesis 429 array_push($catnames, $cat["term"]);
mp-wp_genesis 430
mp-wp_genesis 431 $wp_cats = get_categories(array('hide_empty' => false));
mp-wp_genesis 432
mp-wp_genesis 433 $post_category = array();
mp-wp_genesis 434
mp-wp_genesis 435 foreach($wp_cats as $cat) {
mp-wp_genesis 436 if(in_array($cat->name, $catnames))
mp-wp_genesis 437 array_push($post_category, $cat->term_id);
mp-wp_genesis 438 }
mp-wp_genesis 439
mp-wp_genesis 440 $publish = (isset($entry->draft) && trim($entry->draft) == 'yes') ? false : true;
mp-wp_genesis 441
mp-wp_genesis 442 $cap = ($publish) ? 'publish_posts' : 'edit_posts';
mp-wp_genesis 443
mp-wp_genesis 444 if(!current_user_can($cap))
mp-wp_genesis 445 $this->auth_required(__('Sorry, you do not have the right to edit/publish new posts.'));
mp-wp_genesis 446
mp-wp_genesis 447 $blog_ID = (int ) $blog_id;
mp-wp_genesis 448 $post_status = ($publish) ? 'publish' : 'draft';
mp-wp_genesis 449 $post_author = (int) $user_ID;
mp-wp_genesis 450 $post_title = $entry->title[1];
mp-wp_genesis 451 $post_content = $entry->content[1];
mp-wp_genesis 452 $post_excerpt = $entry->summary[1];
mp-wp_genesis 453 $pubtimes = $this->get_publish_time($entry->published);
mp-wp_genesis 454 $post_date = $pubtimes[0];
mp-wp_genesis 455 $post_date_gmt = $pubtimes[1];
mp-wp_genesis 456
mp-wp_genesis 457 if ( isset( $_SERVER['HTTP_SLUG'] ) )
mp-wp_genesis 458 $post_name = $_SERVER['HTTP_SLUG'];
mp-wp_genesis 459
mp-wp_genesis 460 $post_data = compact('blog_ID', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'post_name');
mp-wp_genesis 461
mp-wp_genesis 462 $this->escape($post_data);
mp-wp_genesis 463 log_app('Inserting Post. Data:', print_r($post_data,true));
mp-wp_genesis 464
mp-wp_genesis 465 $postID = wp_insert_post($post_data);
mp-wp_genesis 466 if ( is_wp_error( $postID ) )
mp-wp_genesis 467 $this->internal_error($postID->get_error_message());
mp-wp_genesis 468
mp-wp_genesis 469 if (!$postID)
mp-wp_genesis 470 $this->internal_error(__('Sorry, your entry could not be posted. Something wrong happened.'));
mp-wp_genesis 471
mp-wp_genesis 472 // getting warning here about unable to set headers
mp-wp_genesis 473 // because something in the cache is printing to the buffer
mp-wp_genesis 474 // could we clean up wp_set_post_categories or cache to not print
mp-wp_genesis 475 // this could affect our ability to send back the right headers
mp-wp_genesis 476 @wp_set_post_categories($postID, $post_category);
mp-wp_genesis 477
mp-wp_genesis 478 $output = $this->get_entry($postID);
mp-wp_genesis 479
mp-wp_genesis 480 log_app('function',"create_post($postID)");
mp-wp_genesis 481 $this->created($postID, $output);
mp-wp_genesis 482 }
mp-wp_genesis 483
mp-wp_genesis 484 /**
mp-wp_genesis 485 * Retrieve post.
mp-wp_genesis 486 *
mp-wp_genesis 487 * @since 2.2.0
mp-wp_genesis 488 *
mp-wp_genesis 489 * @param int $postID Post ID.
mp-wp_genesis 490 */
mp-wp_genesis 491 function get_post($postID) {
mp-wp_genesis 492 global $entry;
mp-wp_genesis 493
mp-wp_genesis 494 if( !current_user_can( 'edit_post', $postID ) )
mp-wp_genesis 495 $this->auth_required( __( 'Sorry, you do not have the right to access this post.' ) );
mp-wp_genesis 496
mp-wp_genesis 497 $this->set_current_entry($postID);
mp-wp_genesis 498 $output = $this->get_entry($postID);
mp-wp_genesis 499 log_app('function',"get_post($postID)");
mp-wp_genesis 500 $this->output($output);
mp-wp_genesis 501
mp-wp_genesis 502 }
mp-wp_genesis 503
mp-wp_genesis 504 /**
mp-wp_genesis 505 * Update post.
mp-wp_genesis 506 *
mp-wp_genesis 507 * @since 2.2.0
mp-wp_genesis 508 *
mp-wp_genesis 509 * @param int $postID Post ID.
mp-wp_genesis 510 */
mp-wp_genesis 511 function put_post($postID) {
mp-wp_genesis 512 // checked for valid content-types (atom+xml)
mp-wp_genesis 513 // quick check and exit
mp-wp_genesis 514 $this->get_accepted_content_type($this->atom_content_types);
mp-wp_genesis 515
mp-wp_genesis 516 $parser = new AtomParser();
mp-wp_genesis 517 if(!$parser->parse()) {
mp-wp_genesis 518 $this->bad_request();
mp-wp_genesis 519 }
mp-wp_genesis 520
mp-wp_genesis 521 $parsed = array_pop($parser->feed->entries);
mp-wp_genesis 522
mp-wp_genesis 523 log_app('Received UPDATED entry:', print_r($parsed,true));
mp-wp_genesis 524
mp-wp_genesis 525 // check for not found
mp-wp_genesis 526 global $entry;
mp-wp_genesis 527 $this->set_current_entry($postID);
mp-wp_genesis 528
mp-wp_genesis 529 if(!current_user_can('edit_post', $entry['ID']))
mp-wp_genesis 530 $this->auth_required(__('Sorry, you do not have the right to edit this post.'));
mp-wp_genesis 531
mp-wp_genesis 532 $publish = (isset($parsed->draft) && trim($parsed->draft) == 'yes') ? false : true;
mp-wp_genesis 533 $post_status = ($publish) ? 'publish' : 'draft';
mp-wp_genesis 534
mp-wp_genesis 535 extract($entry);
mp-wp_genesis 536
mp-wp_genesis 537 $post_title = $parsed->title[1];
mp-wp_genesis 538 $post_content = $parsed->content[1];
mp-wp_genesis 539 $post_excerpt = $parsed->summary[1];
mp-wp_genesis 540 $pubtimes = $this->get_publish_time($entry->published);
mp-wp_genesis 541 $post_date = $pubtimes[0];
mp-wp_genesis 542 $post_date_gmt = $pubtimes[1];
mp-wp_genesis 543 $pubtimes = $this->get_publish_time($parsed->updated);
mp-wp_genesis 544 $post_modified = $pubtimes[0];
mp-wp_genesis 545 $post_modified_gmt = $pubtimes[1];
mp-wp_genesis 546
mp-wp_genesis 547 $postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'post_date', 'post_date_gmt', 'post_modified', 'post_modified_gmt');
mp-wp_genesis 548 $this->escape($postdata);
mp-wp_genesis 549
mp-wp_genesis 550 $result = wp_update_post($postdata);
mp-wp_genesis 551
mp-wp_genesis 552 if (!$result) {
mp-wp_genesis 553 $this->internal_error(__('For some strange yet very annoying reason, this post could not be edited.'));
mp-wp_genesis 554 }
mp-wp_genesis 555
mp-wp_genesis 556 log_app('function',"put_post($postID)");
mp-wp_genesis 557 $this->ok();
mp-wp_genesis 558 }
mp-wp_genesis 559
mp-wp_genesis 560 /**
mp-wp_genesis 561 * Remove post.
mp-wp_genesis 562 *
mp-wp_genesis 563 * @since 2.2.0
mp-wp_genesis 564 *
mp-wp_genesis 565 * @param int $postID Post ID.
mp-wp_genesis 566 */
mp-wp_genesis 567 function delete_post($postID) {
mp-wp_genesis 568
mp-wp_genesis 569 // check for not found
mp-wp_genesis 570 global $entry;
mp-wp_genesis 571 $this->set_current_entry($postID);
mp-wp_genesis 572
mp-wp_genesis 573 if(!current_user_can('edit_post', $postID)) {
mp-wp_genesis 574 $this->auth_required(__('Sorry, you do not have the right to delete this post.'));
mp-wp_genesis 575 }
mp-wp_genesis 576
mp-wp_genesis 577 if ($entry['post_type'] == 'attachment') {
mp-wp_genesis 578 $this->delete_attachment($postID);
mp-wp_genesis 579 } else {
mp-wp_genesis 580 $result = wp_delete_post($postID);
mp-wp_genesis 581
mp-wp_genesis 582 if (!$result) {
mp-wp_genesis 583 $this->internal_error(__('For some strange yet very annoying reason, this post could not be deleted.'));
mp-wp_genesis 584 }
mp-wp_genesis 585
mp-wp_genesis 586 log_app('function',"delete_post($postID)");
mp-wp_genesis 587 $this->ok();
mp-wp_genesis 588 }
mp-wp_genesis 589
mp-wp_genesis 590 }
mp-wp_genesis 591
mp-wp_genesis 592 /**
mp-wp_genesis 593 * Retrieve attachment.
mp-wp_genesis 594 *
mp-wp_genesis 595 * @since 2.2.0
mp-wp_genesis 596 *
mp-wp_genesis 597 * @param int $postID Optional. Post ID.
mp-wp_genesis 598 */
mp-wp_genesis 599 function get_attachment($postID = null) {
mp-wp_genesis 600 if( !current_user_can( 'upload_files' ) )
mp-wp_genesis 601 $this->auth_required( __( 'Sorry, you do not have permission to upload files.' ) );
mp-wp_genesis 602
mp-wp_genesis 603 if (!isset($postID)) {
mp-wp_genesis 604 $this->get_attachments();
mp-wp_genesis 605 } else {
mp-wp_genesis 606 $this->set_current_entry($postID);
mp-wp_genesis 607 $output = $this->get_entry($postID, 'attachment');
mp-wp_genesis 608 log_app('function',"get_attachment($postID)");
mp-wp_genesis 609 $this->output($output);
mp-wp_genesis 610 }
mp-wp_genesis 611 }
mp-wp_genesis 612
mp-wp_genesis 613 /**
mp-wp_genesis 614 * Create new attachment.
mp-wp_genesis 615 *
mp-wp_genesis 616 * @since 2.2.0
mp-wp_genesis 617 */
mp-wp_genesis 618 function create_attachment() {
mp-wp_genesis 619
mp-wp_genesis 620 $type = $this->get_accepted_content_type();
mp-wp_genesis 621
mp-wp_genesis 622 if(!current_user_can('upload_files'))
mp-wp_genesis 623 $this->auth_required(__('You do not have permission to upload files.'));
mp-wp_genesis 624
mp-wp_genesis 625 $fp = fopen("php://input", "rb");
mp-wp_genesis 626 $bits = null;
mp-wp_genesis 627 while(!feof($fp)) {
mp-wp_genesis 628 $bits .= fread($fp, 4096);
mp-wp_genesis 629 }
mp-wp_genesis 630 fclose($fp);
mp-wp_genesis 631
mp-wp_genesis 632 $slug = '';
mp-wp_genesis 633 if ( isset( $_SERVER['HTTP_SLUG'] ) )
mp-wp_genesis 634 $slug = sanitize_file_name( $_SERVER['HTTP_SLUG'] );
mp-wp_genesis 635 elseif ( isset( $_SERVER['HTTP_TITLE'] ) )
mp-wp_genesis 636 $slug = sanitize_file_name( $_SERVER['HTTP_TITLE'] );
mp-wp_genesis 637 elseif ( empty( $slug ) ) // just make a random name
mp-wp_genesis 638 $slug = substr( md5( uniqid( microtime() ) ), 0, 7);
mp-wp_genesis 639 $ext = preg_replace( '|.*/([a-z0-9]+)|', '$1', $_SERVER['CONTENT_TYPE'] );
mp-wp_genesis 640 $slug = "$slug.$ext";
mp-wp_genesis 641 $file = wp_upload_bits( $slug, NULL, $bits);
mp-wp_genesis 642
mp-wp_genesis 643 log_app('wp_upload_bits returns:',print_r($file,true));
mp-wp_genesis 644
mp-wp_genesis 645 $url = $file['url'];
mp-wp_genesis 646 $file = $file['file'];
mp-wp_genesis 647
mp-wp_genesis 648 do_action('wp_create_file_in_uploads', $file); // replicate
mp-wp_genesis 649
mp-wp_genesis 650 // Construct the attachment array
mp-wp_genesis 651 $attachment = array(
mp-wp_genesis 652 'post_title' => $slug,
mp-wp_genesis 653 'post_content' => $slug,
mp-wp_genesis 654 'post_status' => 'attachment',
mp-wp_genesis 655 'post_parent' => 0,
mp-wp_genesis 656 'post_mime_type' => $type,
mp-wp_genesis 657 'guid' => $url
mp-wp_genesis 658 );
mp-wp_genesis 659
mp-wp_genesis 660 // Save the data
mp-wp_genesis 661 $postID = wp_insert_attachment($attachment, $file);
mp-wp_genesis 662
mp-wp_genesis 663 if (!$postID)
mp-wp_genesis 664 $this->internal_error(__('Sorry, your entry could not be posted. Something wrong happened.'));
mp-wp_genesis 665
mp-wp_genesis 666 $output = $this->get_entry($postID, 'attachment');
mp-wp_genesis 667
mp-wp_genesis 668 $this->created($postID, $output, 'attachment');
mp-wp_genesis 669 log_app('function',"create_attachment($postID)");
mp-wp_genesis 670 }
mp-wp_genesis 671
mp-wp_genesis 672 /**
mp-wp_genesis 673 * Update attachment.
mp-wp_genesis 674 *
mp-wp_genesis 675 * @since 2.2.0
mp-wp_genesis 676 *
mp-wp_genesis 677 * @param int $postID Post ID.
mp-wp_genesis 678 */
mp-wp_genesis 679 function put_attachment($postID) {
mp-wp_genesis 680 // checked for valid content-types (atom+xml)
mp-wp_genesis 681 // quick check and exit
mp-wp_genesis 682 $this->get_accepted_content_type($this->atom_content_types);
mp-wp_genesis 683
mp-wp_genesis 684 $parser = new AtomParser();
mp-wp_genesis 685 if(!$parser->parse()) {
mp-wp_genesis 686 $this->bad_request();
mp-wp_genesis 687 }
mp-wp_genesis 688
mp-wp_genesis 689 $parsed = array_pop($parser->feed->entries);
mp-wp_genesis 690
mp-wp_genesis 691 // check for not found
mp-wp_genesis 692 global $entry;
mp-wp_genesis 693 $this->set_current_entry($postID);
mp-wp_genesis 694
mp-wp_genesis 695 if(!current_user_can('edit_post', $entry['ID']))
mp-wp_genesis 696 $this->auth_required(__('Sorry, you do not have the right to edit this post.'));
mp-wp_genesis 697
mp-wp_genesis 698 extract($entry);
mp-wp_genesis 699
mp-wp_genesis 700 $post_title = $parsed->title[1];
mp-wp_genesis 701 $post_content = $parsed->content[1];
mp-wp_genesis 702 $pubtimes = $this->get_publish_time($parsed->updated);
mp-wp_genesis 703 $post_modified = $pubtimes[0];
mp-wp_genesis 704 $post_modified_gmt = $pubtimes[1];
mp-wp_genesis 705
mp-wp_genesis 706 $postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'post_modified', 'post_modified_gmt');
mp-wp_genesis 707 $this->escape($postdata);
mp-wp_genesis 708
mp-wp_genesis 709 $result = wp_update_post($postdata);
mp-wp_genesis 710
mp-wp_genesis 711 if (!$result) {
mp-wp_genesis 712 $this->internal_error(__('For some strange yet very annoying reason, this post could not be edited.'));
mp-wp_genesis 713 }
mp-wp_genesis 714
mp-wp_genesis 715 log_app('function',"put_attachment($postID)");
mp-wp_genesis 716 $this->ok();
mp-wp_genesis 717 }
mp-wp_genesis 718
mp-wp_genesis 719 /**
mp-wp_genesis 720 * Remove attachment.
mp-wp_genesis 721 *
mp-wp_genesis 722 * @since 2.2.0
mp-wp_genesis 723 *
mp-wp_genesis 724 * @param int $postID Post ID.
mp-wp_genesis 725 */
mp-wp_genesis 726 function delete_attachment($postID) {
mp-wp_genesis 727 log_app('function',"delete_attachment($postID). File '$location' deleted.");
mp-wp_genesis 728
mp-wp_genesis 729 // check for not found
mp-wp_genesis 730 global $entry;
mp-wp_genesis 731 $this->set_current_entry($postID);
mp-wp_genesis 732
mp-wp_genesis 733 if(!current_user_can('edit_post', $postID)) {
mp-wp_genesis 734 $this->auth_required(__('Sorry, you do not have the right to delete this post.'));
mp-wp_genesis 735 }
mp-wp_genesis 736
mp-wp_genesis 737 $location = get_post_meta($entry['ID'], '_wp_attached_file', true);
mp-wp_genesis 738 $filetype = wp_check_filetype($location);
mp-wp_genesis 739
mp-wp_genesis 740 if(!isset($location) || 'attachment' != $entry['post_type'] || empty($filetype['ext']))
mp-wp_genesis 741 $this->internal_error(__('Error ocurred while accessing post metadata for file location.'));
mp-wp_genesis 742
mp-wp_genesis 743 // delete file
mp-wp_genesis 744 @unlink($location);
mp-wp_genesis 745
mp-wp_genesis 746 // delete attachment
mp-wp_genesis 747 $result = wp_delete_post($postID);
mp-wp_genesis 748
mp-wp_genesis 749 if (!$result) {
mp-wp_genesis 750 $this->internal_error(__('For some strange yet very annoying reason, this post could not be deleted.'));
mp-wp_genesis 751 }
mp-wp_genesis 752
mp-wp_genesis 753 log_app('function',"delete_attachment($postID). File '$location' deleted.");
mp-wp_genesis 754 $this->ok();
mp-wp_genesis 755 }
mp-wp_genesis 756
mp-wp_genesis 757 /**
mp-wp_genesis 758 * Retrieve attachment from post.
mp-wp_genesis 759 *
mp-wp_genesis 760 * @since 2.2.0
mp-wp_genesis 761 *
mp-wp_genesis 762 * @param int $postID Post ID.
mp-wp_genesis 763 */
mp-wp_genesis 764 function get_file($postID) {
mp-wp_genesis 765
mp-wp_genesis 766 // check for not found
mp-wp_genesis 767 global $entry;
mp-wp_genesis 768 $this->set_current_entry($postID);
mp-wp_genesis 769
mp-wp_genesis 770 // then whether user can edit the specific post
mp-wp_genesis 771 if(!current_user_can('edit_post', $postID)) {
mp-wp_genesis 772 $this->auth_required(__('Sorry, you do not have the right to edit this post.'));
mp-wp_genesis 773 }
mp-wp_genesis 774
mp-wp_genesis 775 $location = get_post_meta($entry['ID'], '_wp_attached_file', true);
mp-wp_genesis 776 $filetype = wp_check_filetype($location);
mp-wp_genesis 777
mp-wp_genesis 778 if(!isset($location) || 'attachment' != $entry['post_type'] || empty($filetype['ext']))
mp-wp_genesis 779 $this->internal_error(__('Error ocurred while accessing post metadata for file location.'));
mp-wp_genesis 780
mp-wp_genesis 781 status_header('200');
mp-wp_genesis 782 header('Content-Type: ' . $entry['post_mime_type']);
mp-wp_genesis 783 header('Connection: close');
mp-wp_genesis 784
mp-wp_genesis 785 $fp = fopen($location, "rb");
mp-wp_genesis 786 while(!feof($fp)) {
mp-wp_genesis 787 echo fread($fp, 4096);
mp-wp_genesis 788 }
mp-wp_genesis 789 fclose($fp);
mp-wp_genesis 790
mp-wp_genesis 791 log_app('function',"get_file($postID)");
mp-wp_genesis 792 exit;
mp-wp_genesis 793 }
mp-wp_genesis 794
mp-wp_genesis 795 /**
mp-wp_genesis 796 * Upload file to blog and add attachment to post.
mp-wp_genesis 797 *
mp-wp_genesis 798 * @since 2.2.0
mp-wp_genesis 799 *
mp-wp_genesis 800 * @param int $postID Post ID.
mp-wp_genesis 801 */
mp-wp_genesis 802 function put_file($postID) {
mp-wp_genesis 803
mp-wp_genesis 804 // first check if user can upload
mp-wp_genesis 805 if(!current_user_can('upload_files'))
mp-wp_genesis 806 $this->auth_required(__('You do not have permission to upload files.'));
mp-wp_genesis 807
mp-wp_genesis 808 // check for not found
mp-wp_genesis 809 global $entry;
mp-wp_genesis 810 $this->set_current_entry($postID);
mp-wp_genesis 811
mp-wp_genesis 812 // then whether user can edit the specific post
mp-wp_genesis 813 if(!current_user_can('edit_post', $postID)) {
mp-wp_genesis 814 $this->auth_required(__('Sorry, you do not have the right to edit this post.'));
mp-wp_genesis 815 }
mp-wp_genesis 816
mp-wp_genesis 817 $location = get_post_meta($entry['ID'], '_wp_attached_file', true);
mp-wp_genesis 818 $filetype = wp_check_filetype($location);
mp-wp_genesis 819
mp-wp_genesis 820 if(!isset($location) || 'attachment' != $entry['post_type'] || empty($filetype['ext']))
mp-wp_genesis 821 $this->internal_error(__('Error ocurred while accessing post metadata for file location.'));
mp-wp_genesis 822
mp-wp_genesis 823 $fp = fopen("php://input", "rb");
mp-wp_genesis 824 $localfp = fopen($location, "w+");
mp-wp_genesis 825 while(!feof($fp)) {
mp-wp_genesis 826 fwrite($localfp,fread($fp, 4096));
mp-wp_genesis 827 }
mp-wp_genesis 828 fclose($fp);
mp-wp_genesis 829 fclose($localfp);
mp-wp_genesis 830
mp-wp_genesis 831 $ID = $entry['ID'];
mp-wp_genesis 832 $pubtimes = $this->get_publish_time($entry->published);
mp-wp_genesis 833 $post_date = $pubtimes[0];
mp-wp_genesis 834 $post_date_gmt = $pubtimes[1];
mp-wp_genesis 835 $pubtimes = $this->get_publish_time($parsed->updated);
mp-wp_genesis 836 $post_modified = $pubtimes[0];
mp-wp_genesis 837 $post_modified_gmt = $pubtimes[1];
mp-wp_genesis 838
mp-wp_genesis 839 $post_data = compact('ID', 'post_date', 'post_date_gmt', 'post_modified', 'post_modified_gmt');
mp-wp_genesis 840 $result = wp_update_post($post_data);
mp-wp_genesis 841
mp-wp_genesis 842 if (!$result) {
mp-wp_genesis 843 $this->internal_error(__('Sorry, your entry could not be posted. Something wrong happened.'));
mp-wp_genesis 844 }
mp-wp_genesis 845
mp-wp_genesis 846 log_app('function',"put_file($postID)");
mp-wp_genesis 847 $this->ok();
mp-wp_genesis 848 }
mp-wp_genesis 849
mp-wp_genesis 850 /**
mp-wp_genesis 851 * Retrieve entries URL.
mp-wp_genesis 852 *
mp-wp_genesis 853 * @since 2.2.0
mp-wp_genesis 854 *
mp-wp_genesis 855 * @param int $page Page ID.
mp-wp_genesis 856 * @return string
mp-wp_genesis 857 */
mp-wp_genesis 858 function get_entries_url($page = null) {
mp-wp_genesis 859 if($GLOBALS['post_type'] == 'attachment') {
mp-wp_genesis 860 $path = $this->MEDIA_PATH;
mp-wp_genesis 861 } else {
mp-wp_genesis 862 $path = $this->ENTRIES_PATH;
mp-wp_genesis 863 }
mp-wp_genesis 864 $url = $this->app_base . $path;
mp-wp_genesis 865 if(isset($page) && is_int($page)) {
mp-wp_genesis 866 $url .= "/$page";
mp-wp_genesis 867 }
mp-wp_genesis 868 return $url;
mp-wp_genesis 869 }
mp-wp_genesis 870
mp-wp_genesis 871 /**
mp-wp_genesis 872 * Display entries URL.
mp-wp_genesis 873 *
mp-wp_genesis 874 * @since 2.2.0
mp-wp_genesis 875 *
mp-wp_genesis 876 * @param int $page Page ID.
mp-wp_genesis 877 */
mp-wp_genesis 878 function the_entries_url($page = null) {
mp-wp_genesis 879 echo $this->get_entries_url($page);
mp-wp_genesis 880 }
mp-wp_genesis 881
mp-wp_genesis 882 /**
mp-wp_genesis 883 * Retrieve categories URL.
mp-wp_genesis 884 *
mp-wp_genesis 885 * @since 2.2.0
mp-wp_genesis 886 *
mp-wp_genesis 887 * @param mixed $deprecated Optional, not used.
mp-wp_genesis 888 * @return string
mp-wp_genesis 889 */
mp-wp_genesis 890 function get_categories_url($deprecated = '') {
mp-wp_genesis 891 return $this->app_base . $this->CATEGORIES_PATH;
mp-wp_genesis 892 }
mp-wp_genesis 893
mp-wp_genesis 894 /**
mp-wp_genesis 895 * Display category URL.
mp-wp_genesis 896 *
mp-wp_genesis 897 * @since 2.2.0
mp-wp_genesis 898 */
mp-wp_genesis 899 function the_categories_url() {
mp-wp_genesis 900 echo $this->get_categories_url();
mp-wp_genesis 901 }
mp-wp_genesis 902
mp-wp_genesis 903 /**
mp-wp_genesis 904 * Retrieve attachment URL.
mp-wp_genesis 905 *
mp-wp_genesis 906 * @since 2.2.0
mp-wp_genesis 907 *
mp-wp_genesis 908 * @param int $page Page ID.
mp-wp_genesis 909 * @return string
mp-wp_genesis 910 */
mp-wp_genesis 911 function get_attachments_url($page = null) {
mp-wp_genesis 912 $url = $this->app_base . $this->MEDIA_PATH;
mp-wp_genesis 913 if(isset($page) && is_int($page)) {
mp-wp_genesis 914 $url .= "/$page";
mp-wp_genesis 915 }
mp-wp_genesis 916 return $url;
mp-wp_genesis 917 }
mp-wp_genesis 918
mp-wp_genesis 919 /**
mp-wp_genesis 920 * Display attachment URL.
mp-wp_genesis 921 *
mp-wp_genesis 922 * @since 2.2.0
mp-wp_genesis 923 *
mp-wp_genesis 924 * @param int $page Page ID.
mp-wp_genesis 925 */
mp-wp_genesis 926 function the_attachments_url($page = null) {
mp-wp_genesis 927 echo $this->get_attachments_url($page);
mp-wp_genesis 928 }
mp-wp_genesis 929
mp-wp_genesis 930 /**
mp-wp_genesis 931 * Retrieve service URL.
mp-wp_genesis 932 *
mp-wp_genesis 933 * @since 2.3.0
mp-wp_genesis 934 *
mp-wp_genesis 935 * @return string
mp-wp_genesis 936 */
mp-wp_genesis 937 function get_service_url() {
mp-wp_genesis 938 return $this->app_base . $this->SERVICE_PATH;
mp-wp_genesis 939 }
mp-wp_genesis 940
mp-wp_genesis 941 /**
mp-wp_genesis 942 * Retrieve entry URL.
mp-wp_genesis 943 *
mp-wp_genesis 944 * @since 2.7.0
mp-wp_genesis 945 *
mp-wp_genesis 946 * @param int $postID Post ID.
mp-wp_genesis 947 * @return string
mp-wp_genesis 948 */
mp-wp_genesis 949 function get_entry_url($postID = null) {
mp-wp_genesis 950 if(!isset($postID)) {
mp-wp_genesis 951 global $post;
mp-wp_genesis 952 $postID = (int) $post->ID;
mp-wp_genesis 953 }
mp-wp_genesis 954
mp-wp_genesis 955 $url = $this->app_base . $this->ENTRY_PATH . "/$postID";
mp-wp_genesis 956
mp-wp_genesis 957 log_app('function',"get_entry_url() = $url");
mp-wp_genesis 958 return $url;
mp-wp_genesis 959 }
mp-wp_genesis 960
mp-wp_genesis 961 /**
mp-wp_genesis 962 * Display entry URL.
mp-wp_genesis 963 *
mp-wp_genesis 964 * @since 2.7.0
mp-wp_genesis 965 *
mp-wp_genesis 966 * @param int $postID Post ID.
mp-wp_genesis 967 */
mp-wp_genesis 968 function the_entry_url($postID = null) {
mp-wp_genesis 969 echo $this->get_entry_url($postID);
mp-wp_genesis 970 }
mp-wp_genesis 971
mp-wp_genesis 972 /**
mp-wp_genesis 973 * Retrieve media URL.
mp-wp_genesis 974 *
mp-wp_genesis 975 * @since 2.2.0
mp-wp_genesis 976 *
mp-wp_genesis 977 * @param int $postID Post ID.
mp-wp_genesis 978 * @return string
mp-wp_genesis 979 */
mp-wp_genesis 980 function get_media_url($postID = null) {
mp-wp_genesis 981 if(!isset($postID)) {
mp-wp_genesis 982 global $post;
mp-wp_genesis 983 $postID = (int) $post->ID;
mp-wp_genesis 984 }
mp-wp_genesis 985
mp-wp_genesis 986 $url = $this->app_base . $this->MEDIA_SINGLE_PATH ."/file/$postID";
mp-wp_genesis 987
mp-wp_genesis 988 log_app('function',"get_media_url() = $url");
mp-wp_genesis 989 return $url;
mp-wp_genesis 990 }
mp-wp_genesis 991
mp-wp_genesis 992 /**
mp-wp_genesis 993 * Display the media URL.
mp-wp_genesis 994 *
mp-wp_genesis 995 * @since 2.2.0
mp-wp_genesis 996 *
mp-wp_genesis 997 * @param int $postID Post ID.
mp-wp_genesis 998 */
mp-wp_genesis 999 function the_media_url($postID = null) {
mp-wp_genesis 1000 echo $this->get_media_url($postID);
mp-wp_genesis 1001 }
mp-wp_genesis 1002
mp-wp_genesis 1003 /**
mp-wp_genesis 1004 * Set the current entry to post ID.
mp-wp_genesis 1005 *
mp-wp_genesis 1006 * @since 2.2.0
mp-wp_genesis 1007 *
mp-wp_genesis 1008 * @param int $postID Post ID.
mp-wp_genesis 1009 */
mp-wp_genesis 1010 function set_current_entry($postID) {
mp-wp_genesis 1011 global $entry;
mp-wp_genesis 1012 log_app('function',"set_current_entry($postID)");
mp-wp_genesis 1013
mp-wp_genesis 1014 if(!isset($postID)) {
mp-wp_genesis 1015 // $this->bad_request();
mp-wp_genesis 1016 $this->not_found();
mp-wp_genesis 1017 }
mp-wp_genesis 1018
mp-wp_genesis 1019 $entry = wp_get_single_post($postID,ARRAY_A);
mp-wp_genesis 1020
mp-wp_genesis 1021 if(!isset($entry) || !isset($entry['ID']))
mp-wp_genesis 1022 $this->not_found();
mp-wp_genesis 1023
mp-wp_genesis 1024 return;
mp-wp_genesis 1025 }
mp-wp_genesis 1026
mp-wp_genesis 1027 /**
mp-wp_genesis 1028 * Display posts XML.
mp-wp_genesis 1029 *
mp-wp_genesis 1030 * @since 2.2.0
mp-wp_genesis 1031 *
mp-wp_genesis 1032 * @param int $page Optional. Page ID.
mp-wp_genesis 1033 * @param string $post_type Optional, default is 'post'. Post Type.
mp-wp_genesis 1034 */
mp-wp_genesis 1035 function get_posts($page = 1, $post_type = 'post') {
mp-wp_genesis 1036 log_app('function',"get_posts($page, '$post_type')");
mp-wp_genesis 1037 $feed = $this->get_feed($page, $post_type);
mp-wp_genesis 1038 $this->output($feed);
mp-wp_genesis 1039 }
mp-wp_genesis 1040
mp-wp_genesis 1041 /**
mp-wp_genesis 1042 * Display attachment XML.
mp-wp_genesis 1043 *
mp-wp_genesis 1044 * @since 2.2.0
mp-wp_genesis 1045 *
mp-wp_genesis 1046 * @param int $page Page ID.
mp-wp_genesis 1047 * @param string $post_type Optional, default is 'attachment'. Post type.
mp-wp_genesis 1048 */
mp-wp_genesis 1049 function get_attachments($page = 1, $post_type = 'attachment') {
mp-wp_genesis 1050 log_app('function',"get_attachments($page, '$post_type')");
mp-wp_genesis 1051 $GLOBALS['post_type'] = $post_type;
mp-wp_genesis 1052 $feed = $this->get_feed($page, $post_type);
mp-wp_genesis 1053 $this->output($feed);
mp-wp_genesis 1054 }
mp-wp_genesis 1055
mp-wp_genesis 1056 /**
mp-wp_genesis 1057 * Retrieve feed XML.
mp-wp_genesis 1058 *
mp-wp_genesis 1059 * @since 2.2.0
mp-wp_genesis 1060 *
mp-wp_genesis 1061 * @param int $page Page ID.
mp-wp_genesis 1062 * @param string $post_type Optional, default is post. Post type.
mp-wp_genesis 1063 * @return string
mp-wp_genesis 1064 */
mp-wp_genesis 1065 function get_feed($page = 1, $post_type = 'post') {
mp-wp_genesis 1066 global $post, $wp, $wp_query, $posts, $wpdb, $blog_id;
mp-wp_genesis 1067 log_app('function',"get_feed($page, '$post_type')");
mp-wp_genesis 1068 ob_start();
mp-wp_genesis 1069
mp-wp_genesis 1070 if(!isset($page)) {
mp-wp_genesis 1071 $page = 1;
mp-wp_genesis 1072 }
mp-wp_genesis 1073 $page = (int) $page;
mp-wp_genesis 1074
mp-wp_genesis 1075 $count = get_option('posts_per_rss');
mp-wp_genesis 1076
mp-wp_genesis 1077 wp('what_to_show=posts&posts_per_page=' . $count . '&offset=' . ($count * ($page-1) . '&orderby=modified'));
mp-wp_genesis 1078
mp-wp_genesis 1079 $post = $GLOBALS['post'];
mp-wp_genesis 1080 $posts = $GLOBALS['posts'];
mp-wp_genesis 1081 $wp = $GLOBALS['wp'];
mp-wp_genesis 1082 $wp_query = $GLOBALS['wp_query'];
mp-wp_genesis 1083 $wpdb = $GLOBALS['wpdb'];
mp-wp_genesis 1084 $blog_id = (int) $GLOBALS['blog_id'];
mp-wp_genesis 1085 log_app('function',"query_posts(# " . print_r($wp_query, true) . "#)");
mp-wp_genesis 1086
mp-wp_genesis 1087 log_app('function',"total_count(# $wp_query->max_num_pages #)");
mp-wp_genesis 1088 $last_page = $wp_query->max_num_pages;
mp-wp_genesis 1089 $next_page = (($page + 1) > $last_page) ? NULL : $page + 1;
mp-wp_genesis 1090 $prev_page = ($page - 1) < 1 ? NULL : $page - 1;
mp-wp_genesis 1091 $last_page = ((int)$last_page == 1 || (int)$last_page == 0) ? NULL : (int) $last_page;
mp-wp_genesis 1092 $self_page = $page > 1 ? $page : NULL;
mp-wp_genesis 1093 ?><feed xmlns="<?php echo $this->ATOM_NS ?>" xmlns:app="<?php echo $this->ATOMPUB_NS ?>" xml:lang="<?php echo get_option('rss_language'); ?>">
mp-wp_genesis 1094 <id><?php $this->the_entries_url() ?></id>
mp-wp_genesis 1095 <updated><?php echo mysql2date('Y-m-d\TH:i:s\Z', get_lastpostmodified('GMT')); ?></updated>
mp-wp_genesis 1096 <title type="text"><?php bloginfo_rss('name') ?></title>
mp-wp_genesis 1097 <subtitle type="text"><?php bloginfo_rss("description") ?></subtitle>
mp-wp_genesis 1098 <link rel="first" type="<?php echo $this->ATOM_CONTENT_TYPE ?>" href="<?php $this->the_entries_url() ?>" />
mp-wp_genesis 1099 <?php if(isset($prev_page)): ?>
mp-wp_genesis 1100 <link rel="previous" type="<?php echo $this->ATOM_CONTENT_TYPE ?>" href="<?php $this->the_entries_url($prev_page) ?>" />
mp-wp_genesis 1101 <?php endif; ?>
mp-wp_genesis 1102 <?php if(isset($next_page)): ?>
mp-wp_genesis 1103 <link rel="next" type="<?php echo $this->ATOM_CONTENT_TYPE ?>" href="<?php $this->the_entries_url($next_page) ?>" />
mp-wp_genesis 1104 <?php endif; ?>
mp-wp_genesis 1105 <link rel="last" type="<?php echo $this->ATOM_CONTENT_TYPE ?>" href="<?php $this->the_entries_url($last_page) ?>" />
mp-wp_genesis 1106 <link rel="self" type="<?php echo $this->ATOM_CONTENT_TYPE ?>" href="<?php $this->the_entries_url($self_page) ?>" />
mp-wp_genesis 1107 <rights type="text">Copyright <?php echo mysql2date('Y', get_lastpostdate('blog')); ?></rights>
mp-wp_genesis 1108 <?php the_generator( 'atom' ); ?>
mp-wp_genesis 1109 <?php if ( have_posts() ) {
mp-wp_genesis 1110 while ( have_posts() ) {
mp-wp_genesis 1111 the_post();
mp-wp_genesis 1112 $this->echo_entry();
mp-wp_genesis 1113 }
mp-wp_genesis 1114 }
mp-wp_genesis 1115 ?></feed>
mp-wp_genesis 1116 <?php
mp-wp_genesis 1117 $feed = ob_get_contents();
mp-wp_genesis 1118 ob_end_clean();
mp-wp_genesis 1119 return $feed;
mp-wp_genesis 1120 }
mp-wp_genesis 1121
mp-wp_genesis 1122 /**
mp-wp_genesis 1123 * Display entry XML.
mp-wp_genesis 1124 *
mp-wp_genesis 1125 * @since 2.2.0
mp-wp_genesis 1126 *
mp-wp_genesis 1127 * @param int $postID Post ID.
mp-wp_genesis 1128 * @param string $post_type Optional, default is post. Post type.
mp-wp_genesis 1129 * @return string.
mp-wp_genesis 1130 */
mp-wp_genesis 1131 function get_entry($postID, $post_type = 'post') {
mp-wp_genesis 1132 log_app('function',"get_entry($postID, '$post_type')");
mp-wp_genesis 1133 ob_start();
mp-wp_genesis 1134 switch($post_type) {
mp-wp_genesis 1135 case 'post':
mp-wp_genesis 1136 $varname = 'p';
mp-wp_genesis 1137 break;
mp-wp_genesis 1138 case 'attachment':
mp-wp_genesis 1139 $varname = 'attachment_id';
mp-wp_genesis 1140 break;
mp-wp_genesis 1141 }
mp-wp_genesis 1142 query_posts($varname . '=' . $postID);
mp-wp_genesis 1143 if ( have_posts() ) {
mp-wp_genesis 1144 while ( have_posts() ) {
mp-wp_genesis 1145 the_post();
mp-wp_genesis 1146 $this->echo_entry();
mp-wp_genesis 1147 log_app('$post',print_r($GLOBALS['post'],true));
mp-wp_genesis 1148 $entry = ob_get_contents();
mp-wp_genesis 1149 break;
mp-wp_genesis 1150 }
mp-wp_genesis 1151 }
mp-wp_genesis 1152 ob_end_clean();
mp-wp_genesis 1153
mp-wp_genesis 1154 log_app('get_entry returning:',$entry);
mp-wp_genesis 1155 return $entry;
mp-wp_genesis 1156 }
mp-wp_genesis 1157
mp-wp_genesis 1158 /**
mp-wp_genesis 1159 * Display post content XML.
mp-wp_genesis 1160 *
mp-wp_genesis 1161 * @since 2.3.0
mp-wp_genesis 1162 */
mp-wp_genesis 1163 function echo_entry() { ?>
mp-wp_genesis 1164 <entry xmlns="<?php echo $this->ATOM_NS ?>"
mp-wp_genesis 1165 xmlns:app="<?php echo $this->ATOMPUB_NS ?>" xml:lang="<?php echo get_option('rss_language'); ?>">
mp-wp_genesis 1166 <id><?php the_guid($GLOBALS['post']->ID); ?></id>
mp-wp_genesis 1167 <?php list($content_type, $content) = prep_atom_text_construct(get_the_title()); ?>
mp-wp_genesis 1168 <title type="<?php echo $content_type ?>"><?php echo $content ?></title>
mp-wp_genesis 1169 <updated><?php echo get_post_modified_time('Y-m-d\TH:i:s\Z', true); ?></updated>
mp-wp_genesis 1170 <published><?php echo get_post_time('Y-m-d\TH:i:s\Z', true); ?></published>
mp-wp_genesis 1171 <app:edited><?php echo get_post_modified_time('Y-m-d\TH:i:s\Z', true); ?></app:edited>
mp-wp_genesis 1172 <app:control>
mp-wp_genesis 1173 <app:draft><?php echo ($GLOBALS['post']->post_status == 'draft' ? 'yes' : 'no') ?></app:draft>
mp-wp_genesis 1174 </app:control>
mp-wp_genesis 1175 <author>
mp-wp_genesis 1176 <name><?php the_author()?></name>
mp-wp_genesis 1177 <?php if (get_the_author_url() && get_the_author_url() != 'http://') { ?>
mp-wp_genesis 1178 <uri><?php the_author_url()?></uri>
mp-wp_genesis 1179 <?php } ?>
mp-wp_genesis 1180 </author>
mp-wp_genesis 1181 <?php if($GLOBALS['post']->post_type == 'attachment') { ?>
mp-wp_genesis 1182 <link rel="edit-media" href="<?php $this->the_media_url() ?>" />
mp-wp_genesis 1183 <content type="<?php echo $GLOBALS['post']->post_mime_type ?>" src="<?php the_guid(); ?>"/>
mp-wp_genesis 1184 <?php } else { ?>
mp-wp_genesis 1185 <link href="<?php the_permalink_rss() ?>" />
mp-wp_genesis 1186 <?php if ( strlen( $GLOBALS['post']->post_content ) ) :
mp-wp_genesis 1187 list($content_type, $content) = prep_atom_text_construct(get_the_content()); ?>
mp-wp_genesis 1188 <content type="<?php echo $content_type ?>"><?php echo $content ?></content>
mp-wp_genesis 1189 <?php endif; ?>
mp-wp_genesis 1190 <?php } ?>
mp-wp_genesis 1191 <link rel="edit" href="<?php $this->the_entry_url() ?>" />
mp-wp_genesis 1192 <?php foreach(get_the_category() as $category) { ?>
mp-wp_genesis 1193 <category scheme="<?php bloginfo_rss('home') ?>" term="<?php echo $category->name?>" />
mp-wp_genesis 1194 <?php } ?>
mp-wp_genesis 1195 <?php list($content_type, $content) = prep_atom_text_construct(get_the_excerpt()); ?>
mp-wp_genesis 1196 <summary type="<?php echo $content_type ?>"><?php echo $content ?></summary>
mp-wp_genesis 1197 </entry>
mp-wp_genesis 1198 <?php }
mp-wp_genesis 1199
mp-wp_genesis 1200 /**
mp-wp_genesis 1201 * Set 'OK' (200) status header.
mp-wp_genesis 1202 *
mp-wp_genesis 1203 * @since 2.2.0
mp-wp_genesis 1204 */
mp-wp_genesis 1205 function ok() {
mp-wp_genesis 1206 log_app('Status','200: OK');
mp-wp_genesis 1207 header('Content-Type: text/plain');
mp-wp_genesis 1208 status_header('200');
mp-wp_genesis 1209 exit;
mp-wp_genesis 1210 }
mp-wp_genesis 1211
mp-wp_genesis 1212 /**
mp-wp_genesis 1213 * Set 'No Content' (204) status header.
mp-wp_genesis 1214 *
mp-wp_genesis 1215 * @since 2.2.0
mp-wp_genesis 1216 */
mp-wp_genesis 1217 function no_content() {
mp-wp_genesis 1218 log_app('Status','204: No Content');
mp-wp_genesis 1219 header('Content-Type: text/plain');
mp-wp_genesis 1220 status_header('204');
mp-wp_genesis 1221 echo "Deleted.";
mp-wp_genesis 1222 exit;
mp-wp_genesis 1223 }
mp-wp_genesis 1224
mp-wp_genesis 1225 /**
mp-wp_genesis 1226 * Display 'Internal Server Error' (500) status header.
mp-wp_genesis 1227 *
mp-wp_genesis 1228 * @since 2.2.0
mp-wp_genesis 1229 *
mp-wp_genesis 1230 * @param string $msg Optional. Status string.
mp-wp_genesis 1231 */
mp-wp_genesis 1232 function internal_error($msg = 'Internal Server Error') {
mp-wp_genesis 1233 log_app('Status','500: Server Error');
mp-wp_genesis 1234 header('Content-Type: text/plain');
mp-wp_genesis 1235 status_header('500');
mp-wp_genesis 1236 echo $msg;
mp-wp_genesis 1237 exit;
mp-wp_genesis 1238 }
mp-wp_genesis 1239
mp-wp_genesis 1240 /**
mp-wp_genesis 1241 * Set 'Bad Request' (400) status header.
mp-wp_genesis 1242 *
mp-wp_genesis 1243 * @since 2.2.0
mp-wp_genesis 1244 */
mp-wp_genesis 1245 function bad_request() {
mp-wp_genesis 1246 log_app('Status','400: Bad Request');
mp-wp_genesis 1247 header('Content-Type: text/plain');
mp-wp_genesis 1248 status_header('400');
mp-wp_genesis 1249 exit;
mp-wp_genesis 1250 }
mp-wp_genesis 1251
mp-wp_genesis 1252 /**
mp-wp_genesis 1253 * Set 'Length Required' (411) status header.
mp-wp_genesis 1254 *
mp-wp_genesis 1255 * @since 2.2.0
mp-wp_genesis 1256 */
mp-wp_genesis 1257 function length_required() {
mp-wp_genesis 1258 log_app('Status','411: Length Required');
mp-wp_genesis 1259 header("HTTP/1.1 411 Length Required");
mp-wp_genesis 1260 header('Content-Type: text/plain');
mp-wp_genesis 1261 status_header('411');
mp-wp_genesis 1262 exit;
mp-wp_genesis 1263 }
mp-wp_genesis 1264
mp-wp_genesis 1265 /**
mp-wp_genesis 1266 * Set 'Unsupported Media Type' (415) status header.
mp-wp_genesis 1267 *
mp-wp_genesis 1268 * @since 2.2.0
mp-wp_genesis 1269 */
mp-wp_genesis 1270 function invalid_media() {
mp-wp_genesis 1271 log_app('Status','415: Unsupported Media Type');
mp-wp_genesis 1272 header("HTTP/1.1 415 Unsupported Media Type");
mp-wp_genesis 1273 header('Content-Type: text/plain');
mp-wp_genesis 1274 exit;
mp-wp_genesis 1275 }
mp-wp_genesis 1276
mp-wp_genesis 1277 /**
mp-wp_genesis 1278 * Set 'Forbidden' (403) status header.
mp-wp_genesis 1279 *
mp-wp_genesis 1280 * @since 2.6.0
mp-wp_genesis 1281 */
mp-wp_genesis 1282 function forbidden($reason='') {
mp-wp_genesis 1283 log_app('Status','403: Forbidden');
mp-wp_genesis 1284 header('Content-Type: text/plain');
mp-wp_genesis 1285 status_header('403');
mp-wp_genesis 1286 echo $reason;
mp-wp_genesis 1287 exit;
mp-wp_genesis 1288 }
mp-wp_genesis 1289
mp-wp_genesis 1290 /**
mp-wp_genesis 1291 * Set 'Not Found' (404) status header.
mp-wp_genesis 1292 *
mp-wp_genesis 1293 * @since 2.2.0
mp-wp_genesis 1294 */
mp-wp_genesis 1295 function not_found() {
mp-wp_genesis 1296 log_app('Status','404: Not Found');
mp-wp_genesis 1297 header('Content-Type: text/plain');
mp-wp_genesis 1298 status_header('404');
mp-wp_genesis 1299 exit;
mp-wp_genesis 1300 }
mp-wp_genesis 1301
mp-wp_genesis 1302 /**
mp-wp_genesis 1303 * Set 'Not Allowed' (405) status header.
mp-wp_genesis 1304 *
mp-wp_genesis 1305 * @since 2.2.0
mp-wp_genesis 1306 */
mp-wp_genesis 1307 function not_allowed($allow) {
mp-wp_genesis 1308 log_app('Status','405: Not Allowed');
mp-wp_genesis 1309 header('Allow: ' . join(',', $allow));
mp-wp_genesis 1310 status_header('405');
mp-wp_genesis 1311 exit;
mp-wp_genesis 1312 }
mp-wp_genesis 1313
mp-wp_genesis 1314 /**
mp-wp_genesis 1315 * Display Redirect (302) content and set status headers.
mp-wp_genesis 1316 *
mp-wp_genesis 1317 * @since 2.3.0
mp-wp_genesis 1318 */
mp-wp_genesis 1319 function redirect($url) {
mp-wp_genesis 1320
mp-wp_genesis 1321 log_app('Status','302: Redirect');
mp-wp_genesis 1322 $escaped_url = attribute_escape($url);
mp-wp_genesis 1323 $content = <<<EOD
mp-wp_genesis 1324 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
mp-wp_genesis 1325 <html>
mp-wp_genesis 1326 <head>
mp-wp_genesis 1327 <title>302 Found</title>
mp-wp_genesis 1328 </head>
mp-wp_genesis 1329 <body>
mp-wp_genesis 1330 <h1>Found</h1>
mp-wp_genesis 1331 <p>The document has moved <a href="$escaped_url">here</a>.</p>
mp-wp_genesis 1332 </body>
mp-wp_genesis 1333 </html>
mp-wp_genesis 1334
mp-wp_genesis 1335 EOD;
mp-wp_genesis 1336 header('HTTP/1.1 302 Moved');
mp-wp_genesis 1337 header('Content-Type: text/html');
mp-wp_genesis 1338 header('Location: ' . $url);
mp-wp_genesis 1339 echo $content;
mp-wp_genesis 1340 exit;
mp-wp_genesis 1341
mp-wp_genesis 1342 }
mp-wp_genesis 1343
mp-wp_genesis 1344 /**
mp-wp_genesis 1345 * Set 'Client Error' (400) status header.
mp-wp_genesis 1346 *
mp-wp_genesis 1347 * @since 2.2.0
mp-wp_genesis 1348 */
mp-wp_genesis 1349 function client_error($msg = 'Client Error') {
mp-wp_genesis 1350 log_app('Status','400: Client Error');
mp-wp_genesis 1351 header('Content-Type: text/plain');
mp-wp_genesis 1352 status_header('400');
mp-wp_genesis 1353 exit;
mp-wp_genesis 1354 }
mp-wp_genesis 1355
mp-wp_genesis 1356 /**
mp-wp_genesis 1357 * Set created status headers (201).
mp-wp_genesis 1358 *
mp-wp_genesis 1359 * Sets the 'content-type', 'content-location', and 'location'.
mp-wp_genesis 1360 *
mp-wp_genesis 1361 * @since 2.2.0
mp-wp_genesis 1362 */
mp-wp_genesis 1363 function created($post_ID, $content, $post_type = 'post') {
mp-wp_genesis 1364 log_app('created()::$post_ID',"$post_ID, $post_type");
mp-wp_genesis 1365 $edit = $this->get_entry_url($post_ID);
mp-wp_genesis 1366 switch($post_type) {
mp-wp_genesis 1367 case 'post':
mp-wp_genesis 1368 $ctloc = $this->get_entry_url($post_ID);
mp-wp_genesis 1369 break;
mp-wp_genesis 1370 case 'attachment':
mp-wp_genesis 1371 $edit = $this->app_base . "attachments/$post_ID";
mp-wp_genesis 1372 break;
mp-wp_genesis 1373 }
mp-wp_genesis 1374 header("Content-Type: $this->ATOM_CONTENT_TYPE");
mp-wp_genesis 1375 if(isset($ctloc))
mp-wp_genesis 1376 header('Content-Location: ' . $ctloc);
mp-wp_genesis 1377 header('Location: ' . $edit);
mp-wp_genesis 1378 status_header('201');
mp-wp_genesis 1379 echo $content;
mp-wp_genesis 1380 exit;
mp-wp_genesis 1381 }
mp-wp_genesis 1382
mp-wp_genesis 1383 /**
mp-wp_genesis 1384 * Set 'Auth Required' (401) headers.
mp-wp_genesis 1385 *
mp-wp_genesis 1386 * @since 2.2.0
mp-wp_genesis 1387 *
mp-wp_genesis 1388 * @param string $msg Status header content and HTML content.
mp-wp_genesis 1389 */
mp-wp_genesis 1390 function auth_required($msg) {
mp-wp_genesis 1391 log_app('Status','401: Auth Required');
mp-wp_genesis 1392 nocache_headers();
mp-wp_genesis 1393 header('WWW-Authenticate: Basic realm="WordPress Atom Protocol"');
mp-wp_genesis 1394 header("HTTP/1.1 401 $msg");
mp-wp_genesis 1395 header('Status: 401 ' . $msg);
mp-wp_genesis 1396 header('Content-Type: text/html');
mp-wp_genesis 1397 $content = <<<EOD
mp-wp_genesis 1398 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
mp-wp_genesis 1399 <html>
mp-wp_genesis 1400 <head>
mp-wp_genesis 1401 <title>401 Unauthorized</title>
mp-wp_genesis 1402 </head>
mp-wp_genesis 1403 <body>
mp-wp_genesis 1404 <h1>401 Unauthorized</h1>
mp-wp_genesis 1405 <p>$msg</p>
mp-wp_genesis 1406 </body>
mp-wp_genesis 1407 </html>
mp-wp_genesis 1408
mp-wp_genesis 1409 EOD;
mp-wp_genesis 1410 echo $content;
mp-wp_genesis 1411 exit;
mp-wp_genesis 1412 }
mp-wp_genesis 1413
mp-wp_genesis 1414 /**
mp-wp_genesis 1415 * Display XML and set headers with content type.
mp-wp_genesis 1416 *
mp-wp_genesis 1417 * @since 2.2.0
mp-wp_genesis 1418 *
mp-wp_genesis 1419 * @param string $xml Display feed content.
mp-wp_genesis 1420 * @param string $ctype Optional, default is 'atom+xml'. Feed content type.
mp-wp_genesis 1421 */
mp-wp_genesis 1422 function output($xml, $ctype = 'application/atom+xml') {
mp-wp_genesis 1423 status_header('200');
mp-wp_genesis 1424 $xml = '<?xml version="1.0" encoding="' . strtolower(get_option('blog_charset')) . '"?>'."\n".$xml;
mp-wp_genesis 1425 header('Connection: close');
mp-wp_genesis 1426 header('Content-Length: '. strlen($xml));
mp-wp_genesis 1427 header('Content-Type: ' . $ctype);
mp-wp_genesis 1428 header('Content-Disposition: attachment; filename=atom.xml');
mp-wp_genesis 1429 header('Date: '. date('r'));
mp-wp_genesis 1430 if($this->do_output)
mp-wp_genesis 1431 echo $xml;
mp-wp_genesis 1432 log_app('function', "output:\n$xml");
mp-wp_genesis 1433 exit;
mp-wp_genesis 1434 }
mp-wp_genesis 1435
mp-wp_genesis 1436 /**
mp-wp_genesis 1437 * Sanitize content for database usage.
mp-wp_genesis 1438 *
mp-wp_genesis 1439 * @since 2.2.0
mp-wp_genesis 1440 *
mp-wp_genesis 1441 * @param array $array Sanitize array and multi-dimension array.
mp-wp_genesis 1442 */
mp-wp_genesis 1443 function escape(&$array) {
mp-wp_genesis 1444 global $wpdb;
mp-wp_genesis 1445
mp-wp_genesis 1446 foreach ($array as $k => $v) {
mp-wp_genesis 1447 if (is_array($v)) {
mp-wp_genesis 1448 $this->escape($array[$k]);
mp-wp_genesis 1449 } else if (is_object($v)) {
mp-wp_genesis 1450 //skip
mp-wp_genesis 1451 } else {
mp-wp_genesis 1452 $array[$k] = $wpdb->escape($v);
mp-wp_genesis 1453 }
mp-wp_genesis 1454 }
mp-wp_genesis 1455 }
mp-wp_genesis 1456
mp-wp_genesis 1457 /**
mp-wp_genesis 1458 * Access credential through various methods and perform login.
mp-wp_genesis 1459 *
mp-wp_genesis 1460 * @since 2.2.0
mp-wp_genesis 1461 *
mp-wp_genesis 1462 * @return bool
mp-wp_genesis 1463 */
mp-wp_genesis 1464 function authenticate() {
mp-wp_genesis 1465 log_app("authenticate()",print_r($_ENV, true));
mp-wp_genesis 1466
mp-wp_genesis 1467 // if using mod_rewrite/ENV hack
mp-wp_genesis 1468 // http://www.besthostratings.com/articles/http-auth-php-cgi.html
mp-wp_genesis 1469 if(isset($_SERVER['HTTP_AUTHORIZATION'])) {
mp-wp_genesis 1470 list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) =
mp-wp_genesis 1471 explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
mp-wp_genesis 1472 } else if (isset($_SERVER['REDIRECT_REMOTE_USER'])) {
mp-wp_genesis 1473 // Workaround for setups that do not forward HTTP_AUTHORIZATION
mp-wp_genesis 1474 // See http://trac.wordpress.org/ticket/7361
mp-wp_genesis 1475 list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) =
mp-wp_genesis 1476 explode(':', base64_decode(substr($_SERVER['REDIRECT_REMOTE_USER'], 6)));
mp-wp_genesis 1477 }
mp-wp_genesis 1478
mp-wp_genesis 1479 // If Basic Auth is working...
mp-wp_genesis 1480 if(isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
mp-wp_genesis 1481 log_app("Basic Auth",$_SERVER['PHP_AUTH_USER']);
mp-wp_genesis 1482 $user = wp_authenticate($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);
mp-wp_genesis 1483 if ( $user && !is_wp_error($user) ) {
mp-wp_genesis 1484 wp_set_current_user($user->ID);
mp-wp_genesis 1485 log_app("authenticate()", $_SERVER['PHP_AUTH_USER']);
mp-wp_genesis 1486 return true;
mp-wp_genesis 1487 }
mp-wp_genesis 1488 }
mp-wp_genesis 1489
mp-wp_genesis 1490 return false;
mp-wp_genesis 1491 }
mp-wp_genesis 1492
mp-wp_genesis 1493 /**
mp-wp_genesis 1494 * Retrieve accepted content types.
mp-wp_genesis 1495 *
mp-wp_genesis 1496 * @since 2.2.0
mp-wp_genesis 1497 *
mp-wp_genesis 1498 * @param array $types Optional. Content Types.
mp-wp_genesis 1499 * @return string
mp-wp_genesis 1500 */
mp-wp_genesis 1501 function get_accepted_content_type($types = null) {
mp-wp_genesis 1502
mp-wp_genesis 1503 if(!isset($types)) {
mp-wp_genesis 1504 $types = $this->media_content_types;
mp-wp_genesis 1505 }
mp-wp_genesis 1506
mp-wp_genesis 1507 if(!isset($_SERVER['CONTENT_LENGTH']) || !isset($_SERVER['CONTENT_TYPE'])) {
mp-wp_genesis 1508 $this->length_required();
mp-wp_genesis 1509 }
mp-wp_genesis 1510
mp-wp_genesis 1511 $type = $_SERVER['CONTENT_TYPE'];
mp-wp_genesis 1512 list($type,$subtype) = explode('/',$type);
mp-wp_genesis 1513 list($subtype) = explode(";",$subtype); // strip MIME parameters
mp-wp_genesis 1514 log_app("get_accepted_content_type", "type=$type, subtype=$subtype");
mp-wp_genesis 1515
mp-wp_genesis 1516 foreach($types as $t) {
mp-wp_genesis 1517 list($acceptedType,$acceptedSubtype) = explode('/',$t);
mp-wp_genesis 1518 if($acceptedType == '*' || $acceptedType == $type) {
mp-wp_genesis 1519 if($acceptedSubtype == '*' || $acceptedSubtype == $subtype)
mp-wp_genesis 1520 return $type . "/" . $subtype;
mp-wp_genesis 1521 }
mp-wp_genesis 1522 }
mp-wp_genesis 1523
mp-wp_genesis 1524 $this->invalid_media();
mp-wp_genesis 1525 }
mp-wp_genesis 1526
mp-wp_genesis 1527 /**
mp-wp_genesis 1528 * Process conditionals for posts.
mp-wp_genesis 1529 *
mp-wp_genesis 1530 * @since 2.2.0
mp-wp_genesis 1531 */
mp-wp_genesis 1532 function process_conditionals() {
mp-wp_genesis 1533
mp-wp_genesis 1534 if(empty($this->params)) return;
mp-wp_genesis 1535 if($_SERVER['REQUEST_METHOD'] == 'DELETE') return;
mp-wp_genesis 1536
mp-wp_genesis 1537 switch($this->params[0]) {
mp-wp_genesis 1538 case $this->ENTRY_PATH:
mp-wp_genesis 1539 global $post;
mp-wp_genesis 1540 $post = wp_get_single_post($this->params[1]);
mp-wp_genesis 1541 $wp_last_modified = get_post_modified_time('D, d M Y H:i:s', true);
mp-wp_genesis 1542 $post = NULL;
mp-wp_genesis 1543 break;
mp-wp_genesis 1544 case $this->ENTRIES_PATH:
mp-wp_genesis 1545 $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0).' GMT';
mp-wp_genesis 1546 break;
mp-wp_genesis 1547 default:
mp-wp_genesis 1548 return;
mp-wp_genesis 1549 }
mp-wp_genesis 1550 $wp_etag = md5($wp_last_modified);
mp-wp_genesis 1551 @header("Last-Modified: $wp_last_modified");
mp-wp_genesis 1552 @header("ETag: $wp_etag");
mp-wp_genesis 1553
mp-wp_genesis 1554 // Support for Conditional GET
mp-wp_genesis 1555 if (isset($_SERVER['HTTP_IF_NONE_MATCH']))
mp-wp_genesis 1556 $client_etag = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']);
mp-wp_genesis 1557 else
mp-wp_genesis 1558 $client_etag = false;
mp-wp_genesis 1559
mp-wp_genesis 1560 $client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE']);
mp-wp_genesis 1561 // If string is empty, return 0. If not, attempt to parse into a timestamp
mp-wp_genesis 1562 $client_modified_timestamp = $client_last_modified ? strtotime($client_last_modified) : 0;
mp-wp_genesis 1563
mp-wp_genesis 1564 // Make a timestamp for our most recent modification...
mp-wp_genesis 1565 $wp_modified_timestamp = strtotime($wp_last_modified);
mp-wp_genesis 1566
mp-wp_genesis 1567 if ( ($client_last_modified && $client_etag) ?
mp-wp_genesis 1568 (($client_modified_timestamp >= $wp_modified_timestamp) && ($client_etag == $wp_etag)) :
mp-wp_genesis 1569 (($client_modified_timestamp >= $wp_modified_timestamp) || ($client_etag == $wp_etag)) ) {
mp-wp_genesis 1570 status_header( 304 );
mp-wp_genesis 1571 exit;
mp-wp_genesis 1572 }
mp-wp_genesis 1573 }
mp-wp_genesis 1574
mp-wp_genesis 1575 /**
mp-wp_genesis 1576 * Convert RFC3339 time string to timestamp.
mp-wp_genesis 1577 *
mp-wp_genesis 1578 * @since 2.3.0
mp-wp_genesis 1579 *
mp-wp_genesis 1580 * @param string $str String to time.
mp-wp_genesis 1581 * @return bool|int false if format is incorrect.
mp-wp_genesis 1582 */
mp-wp_genesis 1583 function rfc3339_str2time($str) {
mp-wp_genesis 1584
mp-wp_genesis 1585 $match = false;
mp-wp_genesis 1586 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))
mp-wp_genesis 1587 return false;
mp-wp_genesis 1588
mp-wp_genesis 1589 if($match[3] == 'Z')
mp-wp_genesis 1590 $match[3] == '+0000';
mp-wp_genesis 1591
mp-wp_genesis 1592 return strtotime($match[1] . " " . $match[2] . " " . $match[3]);
mp-wp_genesis 1593 }
mp-wp_genesis 1594
mp-wp_genesis 1595 /**
mp-wp_genesis 1596 * Retrieve published time to display in XML.
mp-wp_genesis 1597 *
mp-wp_genesis 1598 * @since 2.3.0
mp-wp_genesis 1599 *
mp-wp_genesis 1600 * @param string $published Time string.
mp-wp_genesis 1601 * @return string
mp-wp_genesis 1602 */
mp-wp_genesis 1603 function get_publish_time($published) {
mp-wp_genesis 1604
mp-wp_genesis 1605 $pubtime = $this->rfc3339_str2time($published);
mp-wp_genesis 1606
mp-wp_genesis 1607 if(!$pubtime) {
mp-wp_genesis 1608 return array(current_time('mysql'),current_time('mysql',1));
mp-wp_genesis 1609 } else {
mp-wp_genesis 1610 return array(date("Y-m-d H:i:s", $pubtime), gmdate("Y-m-d H:i:s", $pubtime));
mp-wp_genesis 1611 }
mp-wp_genesis 1612 }
mp-wp_genesis 1613
mp-wp_genesis 1614 }
mp-wp_genesis 1615
mp-wp_genesis 1616 /**
mp-wp_genesis 1617 * AtomServer
mp-wp_genesis 1618 * @var AtomServer
mp-wp_genesis 1619 * @global object $server
mp-wp_genesis 1620 */
mp-wp_genesis 1621 $server = new AtomServer();
mp-wp_genesis 1622 $server->handle_request();
mp-wp_genesis 1623
mp-wp_genesis 1624 ?>