Exemplo n.º 1
0
function postie_media_handle_upload($part, $post_id, $poster, $generate_thubnails = true)
{
    $post_data = array();
    $overrides = array('test_form' => false);
    $tmpFile = tempnam(get_temp_dir(), 'postie');
    $fp = fopen($tmpFile, 'w');
    if ($fp) {
        fwrite($fp, $part->body);
        fclose($fp);
    } else {
        EchoInfo("could not write to temp file: '{$tmpFile}' ");
    }
    //special case to deal with older png implementations
    $namecs = "";
    if (property_exists($part, "ctype_secondary")) {
        $namecs = strtolower($part->ctype_secondary);
        if ($namecs == 'x-png') {
            DebugEcho("postie_media_handle_upload: x-png found, renamed to png");
            $part->ctype_secondary = 'png';
        }
    }
    $name = 'postie-media.' . $namecs;
    if (property_exists($part, 'ctype_parameters') && is_array($part->ctype_parameters)) {
        if (array_key_exists('name', $part->ctype_parameters) && $part->ctype_parameters['name'] != '') {
            $name = $part->ctype_parameters['name'];
        }
        if (array_key_exists('filename', $part->ctype_parameters) && $part->ctype_parameters['filename'] != '') {
            $name = $part->ctype_parameters['filename'];
        }
    }
    if (property_exists($part, 'd_parameters') && is_array($part->d_parameters) && array_key_exists('filename', $part->d_parameters) && $part->d_parameters['filename'] != '') {
        $name = $part->d_parameters['filename'];
    }
    DebugEcho("pre-sanitize name: {$name}, size: " . filesize($tmpFile));
    $name = sanitize_file_name($name);
    DebugEcho("post sanitize name: {$name}");
    //DebugDump($part);
    $the_file = array('name' => $name, 'tmp_name' => $tmpFile, 'size' => filesize($tmpFile), 'error' => '');
    if (stristr('.zip', $name)) {
        DebugEcho("exploding zip file");
        $parts = explode('.', $name);
        $ext = $parts[count($parts) - 1];
        $type = $part->primary . '/' . $part->secondary;
        $the_file['ext'] = $ext;
        $the_file['type'] = $type;
        $overrides['test_type'] = false;
    }
    $time = current_time('mysql');
    $post = get_post($post_id);
    if (substr($post->post_date, 0, 4) > 0) {
        DebugEcho("using post date");
        $time = $post->post_date;
    }
    $file = postie_handle_upload($the_file, $overrides, $time);
    //unlink($tmpFile);
    if (isset($file['error'])) {
        DebugDump($file['error']);
        //throw new Exception($file['error']);
        return new WP_Error('upload_error', $file['error']);
    }
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = preg_replace('/\\.[^.]+$/', '', basename($file));
    $content = '';
    // use image exif/iptc data for title and caption defaults if possible
    if (file_exists(ABSPATH . '/wp-admin/includes/image.php')) {
        include_once ABSPATH . '/wp-admin/includes/image.php';
        include_once ABSPATH . '/wp-admin/includes/media.php';
        if ($image_meta = @wp_read_image_metadata($file)) {
            if (trim($image_meta['title'])) {
                $title = $image_meta['title'];
                DebugEcho("Using metadata title: {$title}");
            }
            if (trim($image_meta['caption'])) {
                $content = $image_meta['caption'];
                DebugEcho("Using metadata caption: {$caption}");
            }
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_excerpt' => $content, 'post_content' => $content, 'post_author' => $poster), $post_data);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    DebugEcho("attachement id: {$id}");
    if (!is_wp_error($id)) {
        if ($generate_thubnails) {
            $amd = wp_generate_attachment_metadata($id, $file);
            DebugEcho("wp_generate_attachment_metadata");
            //DebugDump($amd);
            wp_update_attachment_metadata($id, $amd);
            DebugEcho("wp_update_attachment_metadata complete");
        } else {
            DebugEcho("thumbnail generation disabled");
        }
    } else {
        EchoInfo("There was an error adding the attachement: " . $id->get_error_message());
    }
    return $id;
}
Exemplo n.º 2
0
function postie_media_handle_upload($part, $post_id, $post_data = array())
{
    $overrides = array('test_form' => false);
    //$overrides = array('test_form'=>false, 'test_size'=>false,
    //                  'test_type'=>false);
    $tmpFile = tempnam(getenv('TEMP'), 'postie');
    if (!is_writable($tmpFile)) {
        $uploadDir = wp_upload_dir();
        $tmpFile = tempnam($uploadDir['path'], 'postie');
    }
    $fp = fopen($tmpFile, 'w');
    if ($fp) {
        fwrite($fp, $part->body);
        fclose($fp);
    } else {
        echo "could not write to temp file: '{$tmpFile}' ";
    }
    echo "wrote to temp file\n";
    //print_r($part->ctype_parameters);
    if ($part->ctype_parameters['name'] == '') {
        if ($part->ctype_parameters['filename'] != '') {
            $name = $part->d_parameters['filename'];
        } else {
            $name = 'postie-media.' . $part->ctype_secondary;
        }
    } else {
        $name = $part->ctype_parameters['name'];
    }
    $the_file = array('name' => $name, 'tmp_name' => $tmpFile, 'size' => filesize($tmpFile), 'error' => '');
    print_r($the_file);
    if (stristr('.zip', $name)) {
        $parts = explode('.', $name);
        $ext = $parts[count($parts) - 1];
        $type = $part->primary . '/' . $part->secondary;
        $the_file['ext'] = $ext;
        $the_file['type'] = $type;
        $overrides['test_type'] = false;
    }
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $file = postie_handle_upload($the_file, $overrides, $time);
    echo "finished postie_handle_upload\n";
    print_r($file);
    //unlink($tmpFile);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = preg_replace('/\\.[^.]+$/', '', basename($file));
    $content = '';
    // use image exif/iptc data for title and caption defaults if possible
    include_once ABSPATH . '/wp-admin/includes/image.php';
    if ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title'])) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    echo "read image meta data";
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_excerpt' => $content, 'post_content' => $content), $post_data);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}