function express_uploadFile($args)
{
    global $nxtdb;
    global $nxt_xmlrpc_server;
    $blog_ID = (int) $args[0];
    $username = $nxtdb->escape($args[1]);
    $password = $nxtdb->escape($args[2]);
    $data = $args[3];
    $name = sanitize_file_name($data['name']);
    $type = $data['type'];
    $bits = $data['bits'];
    logIO('O', '(MW) Received ' . strlen($bits) . ' bytes');
    if (!($user = $nxt_xmlrpc_server->login($username, $password))) {
        return $nxt_xmlrpc_server->error;
    }
    do_action('xmlrpc_call', 'metaWeblog.newMediaObject');
    if (!current_user_can('upload_files')) {
        logIO('O', '(MW) User does not have upload_files capability');
        return new IXR_Error(401, __('You are not allowed to upload files to this site.', 'woothemes'));
    }
    if ($upload_err = apply_filters("pre_upload_error", false)) {
        return new IXR_Error(500, $upload_err);
    }
    if (!empty($data["overwrite"]) && $data["overwrite"] == true) {
        // Get postmeta info on the object.
        $old_file = $nxtdb->get_row("\n\t\t\tSELECT ID\n\t\t\tFROM {$nxtdb->posts}\n\t\t\tWHERE post_title = '{$name}'\n\t\t\t\tAND post_type = 'attachment'\n\t\t");
        // Delete previous file.
        nxt_delete_attachment($old_file->ID);
        // Make sure the new name is different by pre-pending the
        // previous post id.
        $filename = preg_replace("/^nxtid\\d+-/", "", $name);
        $name = "nxtid{$old_file->ID}-{$filename}";
    }
    $upload = nxt_upload_bits($name, $type, $bits);
    if (!empty($upload['error'])) {
        $errorString = sprintf(__('Could not write file %1$s (%2$s)', 'woothemes'), $name, $upload['error']);
        logIO('O', '(MW) ' . $errorString);
        return new IXR_Error(500, $errorString);
    }
    // Construct the attachment array
    // attach to post_id 0
    $post_id = 0;
    $attachment = array('post_title' => $name, 'post_content' => '', 'post_type' => 'attachment', 'post_parent' => $post_id, 'post_mime_type' => $type, 'guid' => $upload['url']);
    // Save the data
    $id = nxt_insert_attachment($attachment, $upload['file'], $post_id);
    nxt_update_attachment_metadata($id, nxt_generate_attachment_metadata($id, $upload['file']));
    return apply_filters('nxt_handle_upload', array('file' => $name, 'url' => $upload['url'], 'type' => $type, 'id' => $id));
}
Example #2
0
 /**
  * Attempt to download a remote file attachment
  *
  * @param string $url URL of item to fetch
  * @param array $post Attachment details
  * @return array|nxt_Error Local file location details on success, nxt_Error otherwise
  */
 function fetch_remote_file($url, $post)
 {
     // extract the file name and extension from the url
     $file_name = basename($url);
     // get placeholder file in the upload dir with a unique, sanitized filename
     $upload = nxt_upload_bits($file_name, 0, '', $post['upload_date']);
     if ($upload['error']) {
         return new nxt_Error('upload_dir_error', $upload['error']);
     }
     // fetch the remote url and write it to the placeholder file
     $headers = nxt_get_http($url, $upload['file']);
     // request failed
     if (!$headers) {
         @unlink($upload['file']);
         return new nxt_Error('import_file_error', __('Remote server did not respond', 'nxtclass-importer'));
     }
     // make sure the fetch was successful
     if ($headers['response'] != '200') {
         @unlink($upload['file']);
         return new nxt_Error('import_file_error', sprintf(__('Remote server returned error response %1$d %2$s', 'nxtclass-importer'), esc_html($headers['response']), get_status_header_desc($headers['response'])));
     }
     $filesize = filesize($upload['file']);
     if (isset($headers['content-length']) && $filesize != $headers['content-length']) {
         @unlink($upload['file']);
         return new nxt_Error('import_file_error', __('Remote file is incorrect size', 'nxtclass-importer'));
     }
     if (0 == $filesize) {
         @unlink($upload['file']);
         return new nxt_Error('import_file_error', __('Zero size file downloaded', 'nxtclass-importer'));
     }
     $max_size = (int) $this->max_attachment_size();
     if (!empty($max_size) && $filesize > $max_size) {
         @unlink($upload['file']);
         return new nxt_Error('import_file_error', sprintf(__('Remote file is too large, limit is %s', 'nxtclass-importer'), size_format($max_size)));
     }
     // keep track of the old and new urls so we can substitute them later
     $this->url_remap[$url] = $upload['url'];
     $this->url_remap[$post['guid']] = $upload['url'];
     // r13735, really needed?
     // keep track of the destination if the remote url is redirected somewhere else
     if (isset($headers['x-final-location']) && $headers['x-final-location'] != $url) {
         $this->url_remap[$headers['x-final-location']] = $upload['url'];
     }
     return $upload;
 }