xmlrpc updates from Joseph Scott.

git-svn-id: http://svn.automattic.com/wordpress/trunk@4961 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2007-03-03 16:56:46 +00:00
parent b7f6a96abb
commit 7f1945b310
2 changed files with 58 additions and 17 deletions

View File

@ -1115,17 +1115,28 @@ function wp_upload_bits($name, $type, $bits, $overwrite = false) {
$ext = ''; $ext = '';
else else
$ext = ".$ext"; $ext = ".$ext";
while ( file_exists($upload['path'] . "/$filename") && !$overwrite ) { while ( file_exists($upload['path'] . "/$filename") ) {
if ( '' == "$number$ext" ) if ( '' == "$number$ext" )
$filename = $filename . ++$number . $ext; $filename = $filename . ++$number . $ext;
else else
$filename = str_replace("$number$ext", ++$number . $ext, $filename); $filename = str_replace("$number$ext", ++$number . $ext, $filename);
} }
$new_file = $upload['path'] . "/$filename"; // If we are asked to over write the file then make sure
if ( ! wp_mkdir_p( dirname($new_file) ) ) { // the $name has the complete path and is writable.
$message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), dirname($new_file)); if($overwrite) {
return array('error' => $message); if(!is_writable($name)) {
return(array("error" => __("Can not over write file.")));
}
$new_file = $name;
$filename = basename($name);
}
else {
$new_file = $upload['path'] . "/$filename";
if ( ! wp_mkdir_p( dirname($new_file) ) ) {
$message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), dirname($new_file));
return array('error' => $message);
}
} }
$ifp = @ fopen($new_file, 'wb'); $ifp = @ fopen($new_file, 'wb');
@ -1140,8 +1151,11 @@ function wp_upload_bits($name, $type, $bits, $overwrite = false) {
$perms = $perms & 0000666; $perms = $perms & 0000666;
@ chmod($new_file, $perms); @ chmod($new_file, $perms);
// Compute the URL // Compute the URL if this is a new file.
$url = $upload['url'] . "/$filename"; $url = $upload['url'] . "/$filename";
if($overwrite) {
$url = $name;
}
return array('file' => $new_file, 'url' => $url, 'error' => false); return array('file' => $new_file, 'url' => $url, 'error' => false);
} }

View File

@ -149,7 +149,7 @@ class wp_xmlrpc_server extends IXR_Server {
function escape(&$array) { function escape(&$array) {
global $wpdb; global $wpdb;
if(is_string($array)) { if(!is_array($array)) {
return($wpdb->escape($array)); return($wpdb->escape($array));
} }
else { else {
@ -1100,7 +1100,7 @@ class wp_xmlrpc_server extends IXR_Server {
} }
// Only set a post parent if one was given. // Only set a post parent if one was given.
if(!empty($content_struct["wp_page_parent_id"])) { if(isset($content_struct["wp_page_parent_id"])) {
$post_parent = $content_struct["wp_page_parent_id"]; $post_parent = $content_struct["wp_page_parent_id"];
} }
@ -1166,11 +1166,11 @@ class wp_xmlrpc_server extends IXR_Server {
$to_ping = $content_struct['mt_tb_ping_urls']; $to_ping = $content_struct['mt_tb_ping_urls'];
if ( is_array($to_ping) ) if ( is_array($to_ping) )
$to_ping = implode(' ', $to_ping); $to_ping = implode(' ', $to_ping);
$comment_status = (!isset($content_struct['mt_allow_comments'])) ?
get_option('default_comment_status')
: $content_struct['mt_allow_comments'];
if(isset($content_struct["mt_allow_comments"])) {
$comment_status = $content_struct["mt_allow_comments"];
}
// Do some timestamp voodoo // Do some timestamp voodoo
$dateCreatedd = $content_struct['dateCreated']; $dateCreatedd = $content_struct['dateCreated'];
if (!empty($dateCreatedd)) { if (!empty($dateCreatedd)) {
@ -1387,6 +1387,25 @@ class wp_xmlrpc_server extends IXR_Server {
$overwrite = false; $overwrite = false;
if(!empty($data["overwrite"]) && ($data["overwrite"] == true)) { if(!empty($data["overwrite"]) && ($data["overwrite"] == true)) {
$overwrite = true; $overwrite = true;
// If the file isn't writable then error out now.
if(!is_writable($data["name"])) {
return(new IXR_Error(500, "File is not writable, over write failed."));
}
// We now know the file is good so don't use the sanitized name.
$name = $data["name"];
// Get postmeta info on the object.
$old_meta = $wpdb->get_row("
SELECT *
FROM {$wpdb->postmeta}
WHERE meta_key = '_wp_attached_file'
AND meta_value = '{$name}'
");
// Get post info on the object.
$old_post = get_post($old_meta->post_id);
} }
logIO('O', '(MW) Received '.strlen($bits).' bytes'); logIO('O', '(MW) Received '.strlen($bits).' bytes');
@ -1421,13 +1440,21 @@ class wp_xmlrpc_server extends IXR_Server {
'guid' => $upload[ 'url' ] 'guid' => $upload[ 'url' ]
); );
// Only make a database entry if this is a new file. // If we are over writing then set the correct post_id and URL
if(!$overwrite) { // instead of getting new ones.
// Save the data if($overwrite) {
$id = wp_insert_attachment( $attachment, $upload[ 'file' ], $post_id ); $post_id = $old_meta->post_id;
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) ); $attachment["post_parent"] = $old_meta->post_id;
$attachment["ID"] = $old_meta->post_id;
$upload["url"] = $old_post->guid;
$attachment["guid"] = $old_post->guid;
} }
// Save the data
$id = wp_insert_attachment( $attachment, $upload[ 'file' ], $post_id );
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
return apply_filters( 'wp_handle_upload', array( 'file' => $upload[ 'file' ], 'url' => $upload[ 'url' ], 'type' => $type ) ); return apply_filters( 'wp_handle_upload', array( 'file' => $upload[ 'file' ], 'url' => $upload[ 'url' ], 'type' => $type ) );
} }