web/wp-admin/async-upload.php
author ymh <ymh.work@gmail.com>
Mon, 22 Mar 2010 16:36:28 +0100
changeset 5 ac511f1ccc8e
parent 1 0d28b7c10758
permissions -rw-r--r--
add hgignore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
0d28b7c10758 First commit
ymh
parents:
diff changeset
     1
<?php
0d28b7c10758 First commit
ymh
parents:
diff changeset
     2
/**
0d28b7c10758 First commit
ymh
parents:
diff changeset
     3
 * Accepts file uploads from swfupload or other asynchronous upload methods.
0d28b7c10758 First commit
ymh
parents:
diff changeset
     4
 *
0d28b7c10758 First commit
ymh
parents:
diff changeset
     5
 * @package WordPress
0d28b7c10758 First commit
ymh
parents:
diff changeset
     6
 * @subpackage Administration
0d28b7c10758 First commit
ymh
parents:
diff changeset
     7
 */
0d28b7c10758 First commit
ymh
parents:
diff changeset
     8
0d28b7c10758 First commit
ymh
parents:
diff changeset
     9
define('WP_ADMIN', true);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    10
0d28b7c10758 First commit
ymh
parents:
diff changeset
    11
if ( defined('ABSPATH') )
0d28b7c10758 First commit
ymh
parents:
diff changeset
    12
	require_once(ABSPATH . 'wp-load.php');
0d28b7c10758 First commit
ymh
parents:
diff changeset
    13
else
0d28b7c10758 First commit
ymh
parents:
diff changeset
    14
	require_once('../wp-load.php');
0d28b7c10758 First commit
ymh
parents:
diff changeset
    15
0d28b7c10758 First commit
ymh
parents:
diff changeset
    16
// Flash often fails to send cookies with the POST or upload, so we need to pass it in GET or POST instead
0d28b7c10758 First commit
ymh
parents:
diff changeset
    17
if ( is_ssl() && empty($_COOKIE[SECURE_AUTH_COOKIE]) && !empty($_REQUEST['auth_cookie']) )
0d28b7c10758 First commit
ymh
parents:
diff changeset
    18
	$_COOKIE[SECURE_AUTH_COOKIE] = $_REQUEST['auth_cookie'];
0d28b7c10758 First commit
ymh
parents:
diff changeset
    19
elseif ( empty($_COOKIE[AUTH_COOKIE]) && !empty($_REQUEST['auth_cookie']) )
0d28b7c10758 First commit
ymh
parents:
diff changeset
    20
	$_COOKIE[AUTH_COOKIE] = $_REQUEST['auth_cookie'];
0d28b7c10758 First commit
ymh
parents:
diff changeset
    21
if ( empty($_COOKIE[LOGGED_IN_COOKIE]) && !empty($_REQUEST['logged_in_cookie']) )
0d28b7c10758 First commit
ymh
parents:
diff changeset
    22
	$_COOKIE[LOGGED_IN_COOKIE] = $_REQUEST['logged_in_cookie'];
0d28b7c10758 First commit
ymh
parents:
diff changeset
    23
unset($current_user);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    24
require_once('admin.php');
0d28b7c10758 First commit
ymh
parents:
diff changeset
    25
0d28b7c10758 First commit
ymh
parents:
diff changeset
    26
header('Content-Type: text/plain; charset=' . get_option('blog_charset'));
0d28b7c10758 First commit
ymh
parents:
diff changeset
    27
0d28b7c10758 First commit
ymh
parents:
diff changeset
    28
if ( !current_user_can('upload_files') )
0d28b7c10758 First commit
ymh
parents:
diff changeset
    29
	wp_die(__('You do not have permission to upload files.'));
0d28b7c10758 First commit
ymh
parents:
diff changeset
    30
0d28b7c10758 First commit
ymh
parents:
diff changeset
    31
// just fetch the detail form for that attachment
0d28b7c10758 First commit
ymh
parents:
diff changeset
    32
if ( isset($_REQUEST['attachment_id']) && ($id = intval($_REQUEST['attachment_id'])) && $_REQUEST['fetch'] ) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    33
	if ( 2 == $_REQUEST['fetch'] ) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    34
		add_filter('attachment_fields_to_edit', 'media_single_attachment_fields_to_edit', 10, 2);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    35
		echo get_media_item($id, array( 'send' => false, 'delete' => true ));
0d28b7c10758 First commit
ymh
parents:
diff changeset
    36
	} else {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    37
		add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    38
		echo get_media_item($id);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    39
	}
0d28b7c10758 First commit
ymh
parents:
diff changeset
    40
	exit;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    41
}
0d28b7c10758 First commit
ymh
parents:
diff changeset
    42
0d28b7c10758 First commit
ymh
parents:
diff changeset
    43
check_admin_referer('media-form');
0d28b7c10758 First commit
ymh
parents:
diff changeset
    44
0d28b7c10758 First commit
ymh
parents:
diff changeset
    45
$id = media_handle_upload('async-upload', $_REQUEST['post_id']);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    46
if (is_wp_error($id)) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    47
	echo '<div id="media-upload-error">'.esc_html($id->get_error_message()).'</div>';
0d28b7c10758 First commit
ymh
parents:
diff changeset
    48
	exit;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    49
}
0d28b7c10758 First commit
ymh
parents:
diff changeset
    50
0d28b7c10758 First commit
ymh
parents:
diff changeset
    51
if ( $_REQUEST['short'] ) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    52
	// short form response - attachment ID only
0d28b7c10758 First commit
ymh
parents:
diff changeset
    53
	echo $id;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    54
}
0d28b7c10758 First commit
ymh
parents:
diff changeset
    55
else {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    56
	// long form response - big chunk o html
0d28b7c10758 First commit
ymh
parents:
diff changeset
    57
	$type = $_REQUEST['type'];
0d28b7c10758 First commit
ymh
parents:
diff changeset
    58
	echo apply_filters("async_upload_{$type}", $id);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    59
}
0d28b7c10758 First commit
ymh
parents:
diff changeset
    60
0d28b7c10758 First commit
ymh
parents:
diff changeset
    61
?>