WordPress.org

Make WordPress Core

Changeset 3255


Ignore:
Timestamp:
12/01/2005 10:51:40 PM (16 years ago)
Author:
ryan
Message:

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

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/functions-post.php

    r3203 r3255  
    779779    return get_post_meta($attachment_id, '_wp_attached_file', true);
    780780}
     781
     782function wp_upload_bits($name, $type, $bits) {
     783    if ( empty($name) )
     784        return array('error' => "Empty filename");
     785
     786    $upload = wp_upload_dir();
     787   
     788    if ( $upload['error'] !== false )
     789        return $upload;
     790
     791    $number = '';
     792    $filename = $name;
     793    while ( file_exists($upload['path'] . "/$filename") )
     794        $filename = str_replace("$number.$ext", ++$number . ".$ext", $filename);
     795
     796    $new_file = $uploads['path'] . "/$filename";
     797    $ifp = @ fopen($new_file, 'wb');
     798    if ( ! $ifp )
     799        return array('error' => "Could not write file $new_file.");
     800       
     801    $success = @ fwrite($ifp, $bits);
     802    fclose($ifp);
     803    // Set correct file permissions
     804    $stat = @ stat(dirname($new_file));
     805    $perms = $stat['mode'] & 0000777;
     806    @ chmod($new_file, $perms);
     807
     808    // Compute the URL
     809    $url = $upload['url'] . "/$filename";
     810
     811    return array('file' => $new_file, 'url' => $url);
     812}
     813
    781814?>
  • trunk/xmlrpc.php

    r3180 r3255  
    826826    /* metaweblog.newMediaObject uploads a file, following your settings */
    827827    function mw_newMediaObject($args) {
    828       // adapted from a patch by Johann Richard
    829       // http://mycvs.org/archives/2004/06/30/file-upload-to-wordpress-in-ecto/
     828        // adapted from a patch by Johann Richard
     829        // http://mycvs.org/archives/2004/06/30/file-upload-to-wordpress-in-ecto/
    830830
    831831        global $wpdb;
    832832
    833       $blog_ID     = $wpdb->escape($args[0]);
    834       $user_login  = $wpdb->escape($args[1]);
     833        $blog_ID     = $wpdb->escape($args[0]);
     834        $user_login  = $wpdb->escape($args[1]);
    835835        $user_pass   = $wpdb->escape($args[2]);
    836       $data        = $args[3];
    837 
    838       $name = $data['name'];
    839       $type = $data['type'];
    840       $bits = $data['bits'];
    841 
    842       $file_realpath = get_settings('fileupload_realpath');
    843       $file_url = get_settings('fileupload_url');
    844 
    845       logIO('O', '(MW) Received '.strlen($bits).' bytes');
    846 
    847       if (!$this->login_pass_ok($user_login, $user_pass)) {
    848         return $this->error;
    849       }
    850 
    851       $user_data = get_userdatabylogin($user_login);
    852 
    853       if(!get_settings('use_fileupload')) {
    854         // Uploads not allowed
    855         logIO('O', '(MW) Uploads not allowed');
    856         $this->error = new IXR_Error(405, 'No uploads allowed for this site.');
    857         return $this->error;
    858       }
    859 
    860       $user = new WP_User($user_login);
    861       if ( !$user->has_cap('upload_files') ) {
    862         logIO('O', '(MW) User does not have upload_files capability');
    863         $this->error = new IXR_Error(401, 'You are not allowed to upload files to this site.');
    864         return $this->error;
    865       }
    866 
    867       if(trim($file_realpath) == '' || trim($file_url) == '' ) {
    868         // WordPress is not correctly configured
    869         logIO('O', '(MW) Bad configuration. Real/URL path not defined');
    870         $this->error = new IXR_Error(500, 'Please configure WordPress with valid paths for file upload.');
    871         return $this->error;
    872       }
    873 
    874       $prefix = '/';
    875 
    876       if(!empty($name)) {
    877         // Create the path
    878         $localpath = $file_realpath.$prefix.$name;
    879         $url = $file_url.$prefix.$name;
    880 
    881         if (mkdir_p(dirname($localpath))) {
    882 
    883           /* encode & write data (binary) */
    884           $ifp = fopen($localpath, 'wb');
    885           $success = fwrite($ifp, $bits);
    886           fclose($ifp);
    887           @chmod($localpath, 0666);
    888 
    889           if($success) {
    890             $resp = array('url' => $url);
    891             return $resp;
    892           } else {
    893             logIO('O', '(MW) Could not write file '.$name.' to '.$localpath);
    894             return new IXR_Error(500, 'Could not write file '.$name);
    895           }
    896 
    897         } else {
    898           return new IXR_Error(500, 'Could not create directories for '.$name);
    899         }
    900       }
    901     }
    902 
     836        $data        = $args[3];
     837
     838        $name = $data['name'];
     839        $type = $data['type'];
     840        $bits = $data['bits'];
     841
     842        logIO('O', '(MW) Received '.strlen($bits).' bytes');
     843
     844        if ( !$this->login_pass_ok($user_login, $user_pass) )
     845            return $this->error;
     846
     847        $user = new WP_User($user_login);
     848
     849        if ( !$user->has_cap('upload_files') ) {
     850            logIO('O', '(MW) User does not have upload_files capability');
     851            $this->error = new IXR_Error(401, 'You are not allowed to upload files to this site.');
     852            return $this->error;
     853        }
     854
     855        $upload = wp_upload_bits($name, $type, $bits);
     856        if ( $upload['error'] !== false ) {
     857            logIO('O', '(MW) Could not write file '.$name);
     858            return new IXR_Error(500, 'Could not write file '.$name);
     859        }
     860       
     861        return array('url' => $upload['url']);
     862    }
    903863
    904864
Note: See TracChangeset for help on using the changeset viewer.