diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 1446cb753c..90f3b8f982 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -1115,17 +1115,28 @@ function wp_upload_bits($name, $type, $bits, $overwrite = false) { $ext = ''; else $ext = ".$ext"; - while ( file_exists($upload['path'] . "/$filename") && !$overwrite ) { + while ( file_exists($upload['path'] . "/$filename") ) { if ( '' == "$number$ext" ) $filename = $filename . ++$number . $ext; else $filename = str_replace("$number$ext", ++$number . $ext, $filename); } - $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); + // If we are asked to over write the file then make sure + // the $name has the complete path and is writable. + if($overwrite) { + 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'); @@ -1140,8 +1151,11 @@ function wp_upload_bits($name, $type, $bits, $overwrite = false) { $perms = $perms & 0000666; @ chmod($new_file, $perms); - // Compute the URL + // Compute the URL if this is a new file. $url = $upload['url'] . "/$filename"; + if($overwrite) { + $url = $name; + } return array('file' => $new_file, 'url' => $url, 'error' => false); } diff --git a/xmlrpc.php b/xmlrpc.php index 373d6d5607..3283944715 100644 --- a/xmlrpc.php +++ b/xmlrpc.php @@ -149,7 +149,7 @@ class wp_xmlrpc_server extends IXR_Server { function escape(&$array) { global $wpdb; - if(is_string($array)) { + if(!is_array($array)) { return($wpdb->escape($array)); } else { @@ -1100,7 +1100,7 @@ class wp_xmlrpc_server extends IXR_Server { } // 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"]; } @@ -1166,11 +1166,11 @@ class wp_xmlrpc_server extends IXR_Server { $to_ping = $content_struct['mt_tb_ping_urls']; if ( is_array($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 $dateCreatedd = $content_struct['dateCreated']; if (!empty($dateCreatedd)) { @@ -1387,6 +1387,25 @@ class wp_xmlrpc_server extends IXR_Server { $overwrite = false; if(!empty($data["overwrite"]) && ($data["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'); @@ -1421,13 +1440,21 @@ class wp_xmlrpc_server extends IXR_Server { 'guid' => $upload[ 'url' ] ); - // Only make a database entry if this is a new file. - if(!$overwrite) { - // Save the data - $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $post_id ); - wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) ); + // If we are over writing then set the correct post_id and URL + // instead of getting new ones. + if($overwrite) { + $post_id = $old_meta->post_id; + $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 ) ); }