]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/async-upload.php
e43fb58449d19265e053741b7576e96f031c146c
[autoinstalls/wordpress.git] / wp-admin / async-upload.php
1 <?php
2 /**
3  * Accepts file uploads from swfupload or other asynchronous upload methods.
4  *
5  * @package WordPress
6  * @subpackage Administration
7  */
8
9 if ( isset( $_REQUEST['action'] ) && 'upload-attachment' === $_REQUEST['action'] ) {
10         define( 'DOING_AJAX', true );
11 }
12
13 define('WP_ADMIN', true);
14
15 if ( defined('ABSPATH') )
16         require_once(ABSPATH . 'wp-load.php');
17 else
18         require_once( dirname( dirname( __FILE__ ) ) . '/wp-load.php' );
19
20 if ( ! ( isset( $_REQUEST['action'] ) && 'upload-attachment' == $_REQUEST['action'] ) ) {
21         // Flash often fails to send cookies with the POST or upload, so we need to pass it in GET or POST instead
22         if ( is_ssl() && empty($_COOKIE[SECURE_AUTH_COOKIE]) && !empty($_REQUEST['auth_cookie']) )
23                 $_COOKIE[SECURE_AUTH_COOKIE] = $_REQUEST['auth_cookie'];
24         elseif ( empty($_COOKIE[AUTH_COOKIE]) && !empty($_REQUEST['auth_cookie']) )
25                 $_COOKIE[AUTH_COOKIE] = $_REQUEST['auth_cookie'];
26         if ( empty($_COOKIE[LOGGED_IN_COOKIE]) && !empty($_REQUEST['logged_in_cookie']) )
27                 $_COOKIE[LOGGED_IN_COOKIE] = $_REQUEST['logged_in_cookie'];
28         unset($current_user);
29 }
30
31 require_once( ABSPATH . 'wp-admin/admin.php' );
32
33 if ( !current_user_can('upload_files') )
34         wp_die(__('You do not have permission to upload files.'));
35
36 header('Content-Type: text/html; charset=' . get_option('blog_charset'));
37
38 if ( isset( $_REQUEST['action'] ) && 'upload-attachment' === $_REQUEST['action'] ) {
39         include ABSPATH . 'wp-admin/includes/ajax-actions.php';
40
41         send_nosniff_header();
42         nocache_headers();
43
44         wp_ajax_upload_attachment();
45         die( '0' );
46 }
47
48 // just fetch the detail form for that attachment
49 if ( isset($_REQUEST['attachment_id']) && ($id = intval($_REQUEST['attachment_id'])) && $_REQUEST['fetch'] ) {
50         $post = get_post( $id );
51         if ( 'attachment' != $post->post_type )
52                 wp_die( __( 'Unknown post type.' ) );
53         if ( ! current_user_can( 'edit_post', $id ) )
54                 wp_die( __( 'You are not allowed to edit this item.' ) );
55
56         switch ( $_REQUEST['fetch'] ) {
57                 case 3 :
58                         if ( $thumb_url = wp_get_attachment_image_src( $id, 'thumbnail', true ) )
59                                 echo '<img class="pinkynail" src="' . esc_url( $thumb_url[0] ) . '" alt="" />';
60                         echo '<a class="edit-attachment" href="' . esc_url( get_edit_post_link( $id ) ) . '" target="_blank">' . _x( 'Edit', 'media item' ) . '</a>';
61                         $title = $post->post_title ? $post->post_title : wp_basename( $post->guid ); // title shouldn't ever be empty, but use filename just in cas.e
62                         echo '<div class="filename new"><span class="title">' . esc_html( wp_html_excerpt( $title, 60, '&hellip;' ) ) . '</span></div>';
63                         break;
64                 case 2 :
65                         add_filter('attachment_fields_to_edit', 'media_single_attachment_fields_to_edit', 10, 2);
66                         echo get_media_item($id, array( 'send' => false, 'delete' => true ));
67                         break;
68                 default:
69                         add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2);
70                         echo get_media_item($id);
71                         break;
72         }
73         exit;
74 }
75
76 check_admin_referer('media-form');
77
78 $post_id = 0;
79 if ( isset( $_REQUEST['post_id'] ) ) {
80         $post_id = absint( $_REQUEST['post_id'] );
81         if ( ! get_post( $post_id ) || ! current_user_can( 'edit_post', $post_id ) )
82                 $post_id = 0;
83 }
84
85 $id = media_handle_upload( 'async-upload', $post_id );
86 if ( is_wp_error($id) ) {
87         echo '<div class="error-div">
88         <a class="dismiss" href="#" onclick="jQuery(this).parents(\'div.media-item\').slideUp(200, function(){jQuery(this).remove();});">' . __('Dismiss') . '</a>
89         <strong>' . sprintf(__('&#8220;%s&#8221; has failed to upload due to an error'), esc_html($_FILES['async-upload']['name']) ) . '</strong><br />' .
90         esc_html($id->get_error_message()) . '</div>';
91         exit;
92 }
93
94 if ( $_REQUEST['short'] ) {
95         // short form response - attachment ID only
96         echo $id;
97 } else {
98         // long form response - big chunk o html
99         $type = $_REQUEST['type'];
100
101         /**
102          * Filter the returned ID of an uploaded attachment.
103          *
104          * The dynamic portion of the hook name, $type, refers to the attachment type,
105          * such as 'image', 'audio', 'video', 'file', etc.
106          *
107          * @since 2.5.0
108          *
109          * @param int $id Uploaded attachment ID.
110          */
111         echo apply_filters( "async_upload_{$type}", $id );
112 }