Add wp_upload_bits(). Attempt to fix mw_newMediaObject().

git-svn-id: http://svn.automattic.com/wordpress/trunk@3255 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2005-12-01 22:51:40 +00:00
parent 9de108b9bf
commit 30a8b614b2
2 changed files with 57 additions and 64 deletions

View File

@ -778,4 +778,37 @@ function get_post_mime_type($ID = '') {
function get_attached_file($attachment_id) { function get_attached_file($attachment_id) {
return get_post_meta($attachment_id, '_wp_attached_file', true); return get_post_meta($attachment_id, '_wp_attached_file', true);
} }
function wp_upload_bits($name, $type, $bits) {
if ( empty($name) )
return array('error' => "Empty filename");
$upload = wp_upload_dir();
if ( $upload['error'] !== false )
return $upload;
$number = '';
$filename = $name;
while ( file_exists($upload['path'] . "/$filename") )
$filename = str_replace("$number.$ext", ++$number . ".$ext", $filename);
$new_file = $uploads['path'] . "/$filename";
$ifp = @ fopen($new_file, 'wb');
if ( ! $ifp )
return array('error' => "Could not write file $new_file.");
$success = @ fwrite($ifp, $bits);
fclose($ifp);
// Set correct file permissions
$stat = @ stat(dirname($new_file));
$perms = $stat['mode'] & 0000777;
@ chmod($new_file, $perms);
// Compute the URL
$url = $upload['url'] . "/$filename";
return array('file' => $new_file, 'url' => $url);
}
?> ?>

View File

@ -825,83 +825,43 @@ class wp_xmlrpc_server extends IXR_Server {
/* metaweblog.newMediaObject uploads a file, following your settings */ /* metaweblog.newMediaObject uploads a file, following your settings */
function mw_newMediaObject($args) { function mw_newMediaObject($args) {
// adapted from a patch by Johann Richard // adapted from a patch by Johann Richard
// http://mycvs.org/archives/2004/06/30/file-upload-to-wordpress-in-ecto/ // http://mycvs.org/archives/2004/06/30/file-upload-to-wordpress-in-ecto/
global $wpdb; global $wpdb;
$blog_ID = $wpdb->escape($args[0]); $blog_ID = $wpdb->escape($args[0]);
$user_login = $wpdb->escape($args[1]); $user_login = $wpdb->escape($args[1]);
$user_pass = $wpdb->escape($args[2]); $user_pass = $wpdb->escape($args[2]);
$data = $args[3]; $data = $args[3];
$name = $data['name']; $name = $data['name'];
$type = $data['type']; $type = $data['type'];
$bits = $data['bits']; $bits = $data['bits'];
$file_realpath = get_settings('fileupload_realpath'); logIO('O', '(MW) Received '.strlen($bits).' bytes');
$file_url = get_settings('fileupload_url');
logIO('O', '(MW) Received '.strlen($bits).' bytes'); if ( !$this->login_pass_ok($user_login, $user_pass) )
return $this->error;
if (!$this->login_pass_ok($user_login, $user_pass)) { $user = new WP_User($user_login);
return $this->error;
}
$user_data = get_userdatabylogin($user_login); if ( !$user->has_cap('upload_files') ) {
logIO('O', '(MW) User does not have upload_files capability');
$this->error = new IXR_Error(401, 'You are not allowed to upload files to this site.');
return $this->error;
}
if(!get_settings('use_fileupload')) { $upload = wp_upload_bits($name, $type, $bits);
// Uploads not allowed if ( $upload['error'] !== false ) {
logIO('O', '(MW) Uploads not allowed'); logIO('O', '(MW) Could not write file '.$name);
$this->error = new IXR_Error(405, 'No uploads allowed for this site.'); return new IXR_Error(500, 'Could not write file '.$name);
return $this->error; }
}
return array('url' => $upload['url']);
$user = new WP_User($user_login);
if ( !$user->has_cap('upload_files') ) {
logIO('O', '(MW) User does not have upload_files capability');
$this->error = new IXR_Error(401, 'You are not allowed to upload files to this site.');
return $this->error;
}
if(trim($file_realpath) == '' || trim($file_url) == '' ) {
// WordPress is not correctly configured
logIO('O', '(MW) Bad configuration. Real/URL path not defined');
$this->error = new IXR_Error(500, 'Please configure WordPress with valid paths for file upload.');
return $this->error;
}
$prefix = '/';
if(!empty($name)) {
// Create the path
$localpath = $file_realpath.$prefix.$name;
$url = $file_url.$prefix.$name;
if (mkdir_p(dirname($localpath))) {
/* encode & write data (binary) */
$ifp = fopen($localpath, 'wb');
$success = fwrite($ifp, $bits);
fclose($ifp);
@chmod($localpath, 0666);
if($success) {
$resp = array('url' => $url);
return $resp;
} else {
logIO('O', '(MW) Could not write file '.$name.' to '.$localpath);
return new IXR_Error(500, 'Could not write file '.$name);
}
} else {
return new IXR_Error(500, 'Could not create directories for '.$name);
}
}
} }
/* MovableType API functions /* MovableType API functions
* specs on http://www.movabletype.org/docs/mtmanual_programmatic.html * specs on http://www.movabletype.org/docs/mtmanual_programmatic.html
*/ */