This repository has been archived by the owner on Sep 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-app.php
1612 lines (1377 loc) · 39.3 KB
/
wp-app.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Atom Publishing Protocol support for WordPress
*
* @version 1.0.5-dc
*/
/**
* WordPress is handling an Atom Publishing Protocol request.
*
* @var bool
*/
define('APP_REQUEST', true);
/** Set up WordPress environment */
require_once('./wp-load.php');
/** Atom Publishing Protocol Class */
require_once(ABSPATH . WPINC . '/atomlib.php');
/** Admin Image API for metadata updating */
require_once(ABSPATH . '/wp-admin/includes/image.php');
$_SERVER['PATH_INFO'] = preg_replace( '/.*\/wp-app\.php/', '', $_SERVER['REQUEST_URI'] );
/**
* Whether to enable Atom Publishing Protocol Logging.
*
* @name app_logging
* @var int|bool
*/
$app_logging = 0;
/**
* Whether to always authenticate user. Permanently set to true.
*
* @name always_authenticate
* @var int|bool
* @todo Should be an option somewhere
*/
$always_authenticate = 1;
/**
* Writes logging info to a file.
*
* @since 2.2.0
* @uses $app_logging
* @package WordPress
* @subpackage Logging
*
* @param string $label Type of logging
* @param string $msg Information describing logging reason.
*/
function log_app($label,$msg) {
global $app_logging;
if ($app_logging) {
$fp = fopen( 'wp-app.log', 'a+');
$date = gmdate( 'Y-m-d H:i:s' );
fwrite($fp, "\n\n$date - $label\n$msg\n");
fclose($fp);
}
}
/**
* Filter to add more post statuses.
*
* @since 2.2.0
*
* @param string $where SQL statement to filter.
* @return string Filtered SQL statement with added post_status for where clause.
*/
function wa_posts_where_include_drafts_filter($where) {
$where = str_replace("post_status = 'publish'","post_status = 'publish' OR post_status = 'future' OR post_status = 'draft' OR post_status = 'inherit'", $where);
return $where;
}
add_filter('posts_where', 'wa_posts_where_include_drafts_filter');
/**
* WordPress AtomPub API implementation.
*
* @package WordPress
* @subpackage Publishing
* @since 2.2.0
*/
class AtomServer {
/**
* ATOM content type.
*
* @since 2.2.0
* @var string
*/
var $ATOM_CONTENT_TYPE = 'application/atom+xml';
/**
* Categories ATOM content type.
*
* @since 2.2.0
* @var string
*/
var $CATEGORIES_CONTENT_TYPE = 'application/atomcat+xml';
/**
* Service ATOM content type.
*
* @since 2.3.0
* @var string
*/
var $SERVICE_CONTENT_TYPE = 'application/atomsvc+xml';
/**
* ATOM XML namespace.
*
* @since 2.3.0
* @var string
*/
var $ATOM_NS = 'http://www.w3.org/2005/Atom';
/**
* ATOMPUB XML namespace.
*
* @since 2.3.0
* @var string
*/
var $ATOMPUB_NS = 'http://www.w3.org/2007/app';
/**
* Entries path.
*
* @since 2.2.0
* @var string
*/
var $ENTRIES_PATH = "posts";
/**
* Categories path.
*
* @since 2.2.0
* @var string
*/
var $CATEGORIES_PATH = "categories";
/**
* Media path.
*
* @since 2.2.0
* @var string
*/
var $MEDIA_PATH = "attachments";
/**
* Entry path.
*
* @since 2.2.0
* @var string
*/
var $ENTRY_PATH = "post";
/**
* Service path.
*
* @since 2.2.0
* @var string
*/
var $SERVICE_PATH = "service";
/**
* Media single path.
*
* @since 2.2.0
* @var string
*/
var $MEDIA_SINGLE_PATH = "attachment";
/**
* ATOMPUB parameters.
*
* @since 2.2.0
* @var array
*/
var $params = array();
/**
* Supported ATOMPUB media types.
*
* @since 2.3.0
* @var array
*/
var $media_content_types = array('image/*','audio/*','video/*');
/**
* ATOMPUB content type(s).
*
* @since 2.2.0
* @var array
*/
var $atom_content_types = array('application/atom+xml');
/**
* ATOMPUB methods.
*
* @since 2.2.0
* @var unknown_type
*/
var $selectors = array();
/**
* Whether to do output.
*
* Support for head.
*
* @since 2.2.0
* @var bool
*/
var $do_output = true;
/**
* Constructor - Sets up object properties.
*
* @since 2.2.0
* @return AtomServer
*/
function __construct() {
$var_by_ref = explode( '/', $_SERVER['SCRIPT_NAME'] );
$this->script_name = array_pop( $var_by_ref );
$this->app_base = site_url( $this->script_name . '/' );
$this->selectors = array(
'@/service$@' =>
array('GET' => 'get_service'),
'@/categories$@' =>
array('GET' => 'get_categories_xml'),
'@/post/(\d+)$@' =>
array('GET' => 'get_post',
'PUT' => 'put_post',
'DELETE' => 'delete_post'),
'@/posts/?(\d+)?$@' =>
array('GET' => 'get_posts',
'POST' => 'create_post'),
'@/attachments/?(\d+)?$@' =>
array('GET' => 'get_attachment',
'POST' => 'create_attachment'),
'@/attachment/file/(\d+)$@' =>
array('GET' => 'get_file',
'PUT' => 'put_file',
'DELETE' => 'delete_file'),
'@/attachment/(\d+)$@' =>
array('GET' => 'get_attachment',
'PUT' => 'put_attachment',
'DELETE' => 'delete_attachment'),
);
}
/**
* Handle ATOMPUB request.
*
* @since 2.2.0
*/
function handle_request() {
global $always_authenticate;
if ( !empty( $_SERVER['ORIG_PATH_INFO'] ) )
$path = $_SERVER['ORIG_PATH_INFO'];
else
$path = $_SERVER['PATH_INFO'];
$method = $_SERVER['REQUEST_METHOD'];
log_app('REQUEST',"$method $path\n================");
$this->process_conditionals();
//$this->process_conditionals();
// exception case for HEAD (treat exactly as GET, but don't output)
if ($method == 'HEAD') {
$this->do_output = false;
$method = 'GET';
}
// redirect to /service in case no path is found.
if (strlen($path) == 0 || $path == '/')
$this->redirect($this->get_service_url());
// check to see if AtomPub is enabled
if ( !get_option( 'enable_app' ) )
$this->forbidden( sprintf( __( 'AtomPub services are disabled on this site. An admin user can enable them at %s' ), admin_url('options-writing.php') ) );
// dispatch
foreach ( $this->selectors as $regex => $funcs ) {
if ( preg_match($regex, $path, $matches) ) {
if ( isset($funcs[$method]) ) {
// authenticate regardless of the operation and set the current
// user. each handler will decide if auth is required or not.
if ( !$this->authenticate() ) {
if ( $always_authenticate )
$this->auth_required('Credentials required.');
}
array_shift($matches);
call_user_func_array(array(&$this,$funcs[$method]), $matches);
exit();
} else {
// only allow what we have handlers for...
$this->not_allowed(array_keys($funcs));
}
}
}
// oops, nothing found
$this->not_found();
}
/**
* Retrieve XML for ATOMPUB service.
*
* @since 2.2.0
*/
function get_service() {
log_app('function','get_service()');
if ( !current_user_can( 'edit_posts' ) )
$this->auth_required( __( 'Sorry, you do not have the right to access this site.' ) );
$entries_url = esc_attr($this->get_entries_url());
$categories_url = esc_attr($this->get_categories_url());
$media_url = esc_attr($this->get_attachments_url());
$accepted_media_types = '';
foreach ($this->media_content_types as $med) {
$accepted_media_types = $accepted_media_types . "<accept>" . $med . "</accept>";
}
$atom_prefix="atom";
$atom_blogname = get_bloginfo('name');
$service_doc = <<<EOD
<service xmlns="$this->ATOMPUB_NS" xmlns:$atom_prefix="$this->ATOM_NS">
<workspace>
<$atom_prefix:title>$atom_blogname Workspace</$atom_prefix:title>
<collection href="$entries_url">
<$atom_prefix:title>$atom_blogname Posts</$atom_prefix:title>
<accept>$this->ATOM_CONTENT_TYPE;type=entry</accept>
<categories href="$categories_url" />
</collection>
<collection href="$media_url">
<$atom_prefix:title>$atom_blogname Media</$atom_prefix:title>
$accepted_media_types
</collection>
</workspace>
</service>
EOD;
$this->output($service_doc, $this->SERVICE_CONTENT_TYPE);
}
/**
* Retrieve categories list in XML format.
*
* @since 2.2.0
*/
function get_categories_xml() {
log_app('function','get_categories_xml()');
if ( !current_user_can( 'edit_posts' ) )
$this->auth_required( __( 'Sorry, you do not have the right to access this site.' ) );
$home = esc_attr(get_bloginfo_rss('url'));
$categories = "";
$cats = get_categories(array('hierarchical' => 0, 'hide_empty' => 0));
foreach ( (array) $cats as $cat ) {
$categories .= " <category term=\"" . esc_attr($cat->name) . "\" />\n";
}
$output = <<<EOD
<app:categories xmlns:app="$this->ATOMPUB_NS"
xmlns="$this->ATOM_NS"
fixed="yes" scheme="$home">
$categories
</app:categories>
EOD;
$this->output($output, $this->CATEGORIES_CONTENT_TYPE);
}
/**
* Create new post.
*
* @since 2.2.0
*/
function create_post() {
global $user_ID;
$this->get_accepted_content_type($this->atom_content_types);
$parser = new AtomParser();
if ( !$parser->parse() )
$this->client_error();
$entry = array_pop($parser->feed->entries);
log_app('Received entry:', print_r($entry,true));
$catnames = array();
foreach ( $entry->categories as $cat ) {
array_push($catnames, $cat["term"]);
}
$wp_cats = get_categories(array('hide_empty' => false));
$post_category = array();
foreach ( $wp_cats as $cat ) {
if ( in_array($cat->name, $catnames) )
array_push($post_category, $cat->term_id);
}
$publish = ! ( isset( $entry->draft ) && 'yes' == trim( $entry->draft ) );
$cap = ($publish) ? 'publish_posts' : 'edit_posts';
if ( !current_user_can($cap) )
$this->auth_required(__('Sorry, you do not have the right to edit/publish new posts.'));
$blog_ID = get_current_blog_id();
$post_status = ($publish) ? 'publish' : 'draft';
$post_author = (int) $user_ID;
$post_title = $entry->title[1];
$post_content = $entry->content[1];
$post_excerpt = $entry->summary[1];
$pubtimes = $this->get_publish_time($entry->published);
$post_date = $pubtimes[0];
$post_date_gmt = $pubtimes[1];
if ( isset( $_SERVER['HTTP_SLUG'] ) )
$post_name = $_SERVER['HTTP_SLUG'];
$post_data = compact('blog_ID', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'post_name');
$this->escape($post_data);
log_app('Inserting Post. Data:', print_r($post_data,true));
$postID = wp_insert_post($post_data);
if ( is_wp_error( $postID ) )
$this->internal_error($postID->get_error_message());
if ( !$postID )
$this->internal_error(__('Sorry, your entry could not be posted. Something wrong happened.'));
// getting warning here about unable to set headers
// because something in the cache is printing to the buffer
// could we clean up wp_set_post_categories or cache to not print
// this could affect our ability to send back the right headers
@wp_set_post_categories($postID, $post_category);
do_action( 'atompub_create_post', $postID, $entry );
$output = $this->get_entry($postID);
log_app('function',"create_post($postID)");
$this->created($postID, $output);
}
/**
* Retrieve post.
*
* @since 2.2.0
*
* @param int $postID Post ID.
*/
function get_post($postID) {
global $entry;
if ( !current_user_can( 'edit_post', $postID ) )
$this->auth_required( __( 'Sorry, you do not have the right to access this post.' ) );
$this->set_current_entry($postID);
$output = $this->get_entry($postID);
log_app('function',"get_post($postID)");
$this->output($output);
}
/**
* Update post.
*
* @since 2.2.0
*
* @param int $postID Post ID.
*/
function put_post($postID) {
// checked for valid content-types (atom+xml)
// quick check and exit
$this->get_accepted_content_type($this->atom_content_types);
$parser = new AtomParser();
if ( !$parser->parse() )
$this->bad_request();
$parsed = array_pop($parser->feed->entries);
log_app('Received UPDATED entry:', print_r($parsed,true));
// check for not found
global $entry;
$this->set_current_entry($postID);
if ( !current_user_can('edit_post', $entry['ID']) )
$this->auth_required(__('Sorry, you do not have the right to edit this post.'));
$publish = ! ( isset($parsed->draft) && 'yes' == trim($parsed->draft) );
$post_status = ($publish) ? 'publish' : 'draft';
extract($entry);
$post_title = $parsed->title[1];
$post_content = $parsed->content[1];
$post_excerpt = $parsed->summary[1];
$pubtimes = $this->get_publish_time($entry->published);
$post_date = $pubtimes[0];
$post_date_gmt = $pubtimes[1];
$pubtimes = $this->get_publish_time($parsed->updated);
$post_modified = $pubtimes[0];
$post_modified_gmt = $pubtimes[1];
$postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'post_date', 'post_date_gmt', 'post_modified', 'post_modified_gmt');
$this->escape($postdata);
$result = wp_update_post($postdata);
if ( !$result )
$this->internal_error(__('For some strange yet very annoying reason, this post could not be edited.'));
do_action( 'atompub_put_post', $ID, $parsed );
log_app('function',"put_post($postID)");
$this->ok();
}
/**
* Remove post.
*
* @since 2.2.0
*
* @param int $postID Post ID.
*/
function delete_post($postID) {
// check for not found
global $entry;
$this->set_current_entry($postID);
if ( !current_user_can('edit_post', $postID) )
$this->auth_required(__('Sorry, you do not have the right to delete this post.'));
if ( $entry['post_type'] == 'attachment' ) {
$this->delete_attachment($postID);
} else {
$result = wp_delete_post($postID);
if ( !$result ) {
$this->internal_error(__('For some strange yet very annoying reason, this post could not be deleted.'));
}
log_app('function',"delete_post($postID)");
$this->ok();
}
}
/**
* Retrieve attachment.
*
* @since 2.2.0
*
* @param int $postID Optional. Post ID.
*/
function get_attachment($postID = null) {
if ( !current_user_can( 'upload_files' ) )
$this->auth_required( __( 'Sorry, you do not have permission to upload files.' ) );
if ( !isset($postID) ) {
$this->get_attachments();
} else {
$this->set_current_entry($postID);
$output = $this->get_entry($postID, 'attachment');
log_app('function',"get_attachment($postID)");
$this->output($output);
}
}
/**
* Create new attachment.
*
* @since 2.2.0
*/
function create_attachment() {
$type = $this->get_accepted_content_type();
if ( !current_user_can('upload_files') )
$this->auth_required(__('You do not have permission to upload files.'));
$fp = fopen("php://input", "rb");
$bits = null;
while ( !feof($fp) ) {
$bits .= fread($fp, 4096);
}
fclose($fp);
$slug = '';
if ( isset( $_SERVER['HTTP_SLUG'] ) )
$slug = $_SERVER['HTTP_SLUG'];
elseif ( isset( $_SERVER['HTTP_TITLE'] ) )
$slug = $_SERVER['HTTP_TITLE'];
elseif ( empty( $slug ) ) // just make a random name
$slug = substr( md5( uniqid( microtime() ) ), 0, 7);
$ext = preg_replace( '|.*/([a-z0-9]+)|', '$1', $_SERVER['CONTENT_TYPE'] );
$slug = sanitize_file_name( "$slug.$ext" );
$file = wp_upload_bits( $slug, NULL, $bits);
log_app('wp_upload_bits returns:',print_r($file,true));
$url = $file['url'];
$file = $file['file'];
do_action('wp_create_file_in_uploads', $file); // replicate
// Construct the attachment array
$attachment = array(
'post_title' => $slug,
'post_content' => $slug,
'post_status' => 'attachment',
'post_parent' => 0,
'post_mime_type' => $type,
'guid' => $url
);
// Save the data
$postID = wp_insert_attachment($attachment, $file);
if (!$postID)
$this->internal_error(__('Sorry, your entry could not be posted. Something wrong happened.'));
$output = $this->get_entry($postID, 'attachment');
$this->created($postID, $output, 'attachment');
log_app('function',"create_attachment($postID)");
}
/**
* Update attachment.
*
* @since 2.2.0
*
* @param int $postID Post ID.
*/
function put_attachment($postID) {
// checked for valid content-types (atom+xml)
// quick check and exit
$this->get_accepted_content_type($this->atom_content_types);
$parser = new AtomParser();
if (!$parser->parse()) {
$this->bad_request();
}
$parsed = array_pop($parser->feed->entries);
// check for not found
global $entry;
$this->set_current_entry($postID);
if ( !current_user_can('edit_post', $entry['ID']) )
$this->auth_required(__('Sorry, you do not have the right to edit this post.'));
extract($entry);
$post_title = $parsed->title[1];
$post_content = $parsed->summary[1];
$pubtimes = $this->get_publish_time($parsed->updated);
$post_modified = $pubtimes[0];
$post_modified_gmt = $pubtimes[1];
$postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'post_modified', 'post_modified_gmt');
$this->escape($postdata);
$result = wp_update_post($postdata);
if ( !$result )
$this->internal_error(__('For some strange yet very annoying reason, this post could not be edited.'));
log_app('function',"put_attachment($postID)");
$this->ok();
}
/**
* Remove attachment.
*
* @since 2.2.0
*
* @param int $postID Post ID.
*/
function delete_attachment($postID) {
log_app('function',"delete_attachment($postID). File '$location' deleted.");
// check for not found
global $entry;
$this->set_current_entry($postID);
if ( !current_user_can('edit_post', $postID) )
$this->auth_required(__('Sorry, you do not have the right to delete this post.'));
$location = get_post_meta($entry['ID'], '_wp_attached_file', true);
$filetype = wp_check_filetype($location);
if ( !isset($location) || 'attachment' != $entry['post_type'] || empty($filetype['ext']) )
$this->internal_error(__('Error occurred while accessing post metadata for file location.'));
// delete file
@unlink($location);
// delete attachment
$result = wp_delete_post($postID);
if ( !$result )
$this->internal_error(__('For some strange yet very annoying reason, this post could not be deleted.'));
log_app('function',"delete_attachment($postID). File '$location' deleted.");
$this->ok();
}
/**
* Retrieve attachment from post.
*
* @since 2.2.0
*
* @param int $postID Post ID.
*/
function get_file($postID) {
// check for not found
global $entry;
$this->set_current_entry($postID);
// then whether user can edit the specific post
if ( !current_user_can('edit_post', $postID) )
$this->auth_required(__('Sorry, you do not have the right to edit this post.'));
$location = get_post_meta($entry['ID'], '_wp_attached_file', true);
$location = get_option ('upload_path') . '/' . $location;
$filetype = wp_check_filetype($location);
if ( !isset($location) || 'attachment' != $entry['post_type'] || empty($filetype['ext']) )
$this->internal_error(__('Error occurred while accessing post metadata for file location.'));
status_header('200');
header('Content-Type: ' . $entry['post_mime_type']);
header('Connection: close');
if ( $fp = fopen($location, "rb") ) {
status_header('200');
header('Content-Type: ' . $entry['post_mime_type']);
header('Connection: close');
while ( !feof($fp) ) {
echo fread($fp, 4096);
}
fclose($fp);
} else {
status_header ('404');
}
log_app('function',"get_file($postID)");
exit;
}
/**
* Upload file to blog and add attachment to post.
*
* @since 2.2.0
*
* @param int $postID Post ID.
*/
function put_file($postID) {
// first check if user can upload
if ( !current_user_can('upload_files') )
$this->auth_required(__('You do not have permission to upload files.'));
// check for not found
global $entry;
$this->set_current_entry($postID);
// then whether user can edit the specific post
if ( !current_user_can('edit_post', $postID) )
$this->auth_required(__('Sorry, you do not have the right to edit this post.'));
$upload_dir = wp_upload_dir( );
$location = get_post_meta($entry['ID'], '_wp_attached_file', true);
$filetype = wp_check_filetype($location);
$location = "{$upload_dir['basedir']}/{$location}";
if (!isset($location) || 'attachment' != $entry['post_type'] || empty($filetype['ext']))
$this->internal_error(__('Error occurred while accessing post metadata for file location.'));
$fp = fopen("php://input", "rb");
$localfp = fopen($location, "w+");
while ( !feof($fp) ) {
fwrite($localfp,fread($fp, 4096));
}
fclose($fp);
fclose($localfp);
$ID = $entry['ID'];
$pubtimes = $this->get_publish_time($entry->published);
$post_date = $pubtimes[0];
$post_date_gmt = $pubtimes[1];
$pubtimes = $this->get_publish_time($parsed->updated);
$post_modified = $pubtimes[0];
$post_modified_gmt = $pubtimes[1];
$post_data = compact('ID', 'post_date', 'post_date_gmt', 'post_modified', 'post_modified_gmt');
$result = wp_update_post($post_data);
if ( !$result )
$this->internal_error(__('Sorry, your entry could not be posted. Something wrong happened.'));
wp_update_attachment_metadata( $postID, wp_generate_attachment_metadata( $postID, $location ) );
log_app('function',"put_file($postID)");
$this->ok();
}
/**
* Retrieve entries URL.
*
* @since 2.2.0
*
* @param int $page Page ID.
* @return string
*/
function get_entries_url($page = null) {
if ( isset($GLOBALS['post_type']) && ( $GLOBALS['post_type'] == 'attachment' ) )
$path = $this->MEDIA_PATH;
else
$path = $this->ENTRIES_PATH;
$url = $this->app_base . $path;
if ( isset($page) && is_int($page) )
$url .= "/$page";
return $url;
}
/**
* Display entries URL.
*
* @since 2.2.0
*
* @param int $page Page ID.
*/
function the_entries_url($page = null) {
echo $this->get_entries_url($page);
}
/**
* Retrieve categories URL.
*
* @since 2.2.0
*
* @param mixed $deprecated Not used.
* @return string
*/
function get_categories_url($deprecated = '') {
if ( !empty( $deprecated ) )
_deprecated_argument( __FUNCTION__, '2.5' );
return $this->app_base . $this->CATEGORIES_PATH;
}
/**
* Display category URL.
*
* @since 2.2.0
*/
function the_categories_url() {
echo $this->get_categories_url();
}
/**
* Retrieve attachment URL.
*
* @since 2.2.0
*
* @param int $page Page ID.
* @return string
*/
function get_attachments_url($page = null) {
$url = $this->app_base . $this->MEDIA_PATH;
if (isset($page) && is_int($page)) {
$url .= "/$page";
}
return $url;
}
/**
* Display attachment URL.
*
* @since 2.2.0
*
* @param int $page Page ID.
*/
function the_attachments_url($page = null) {
echo $this->get_attachments_url($page);
}
/**
* Retrieve service URL.
*
* @since 2.3.0
*
* @return string
*/
function get_service_url() {
return $this->app_base . $this->SERVICE_PATH;
}
/**
* Retrieve entry URL.
*
* @since 2.7.0
*
* @param int $postID Post ID.
* @return string
*/
function get_entry_url($postID = null) {
if (!isset($postID)) {
global $post;
$postID = (int) $post->ID;
}
$url = $this->app_base . $this->ENTRY_PATH . "/$postID";
log_app('function',"get_entry_url() = $url");
return $url;
}
/**
* Display entry URL.
*
* @since 2.7.0
*
* @param int $postID Post ID.
*/
function the_entry_url($postID = null) {
echo $this->get_entry_url($postID);
}
/**
* Retrieve media URL.
*
* @since 2.2.0
*
* @param int $postID Post ID.
* @return string
*/
function get_media_url($postID = null) {
if (!isset($postID)) {
global $post;
$postID = (int) $post->ID;
}
$url = $this->app_base . $this->MEDIA_SINGLE_PATH ."/file/$postID";
log_app('function',"get_media_url() = $url");
return $url;
}
/**
* Display the media URL.
*
* @since 2.2.0
*
* @param int $postID Post ID.
*/
function the_media_url($postID = null) {
echo $this->get_media_url($postID);
}
/**
* Set the current entry to post ID.
*
* @since 2.2.0
*
* @param int $postID Post ID.
*/
function set_current_entry($postID) {
global $entry;
log_app('function',"set_current_entry($postID)");
if (!isset($postID)) {
// $this->bad_request();