Пример #1
0
/**
 * Handle importer uploading and add attachment.
 *
 * @since 2.0.0
 *
 * @return array Uploaded file's details on success, error message on failure
 */
function nxt_import_handle_upload()
{
    if (!isset($_FILES['import'])) {
        $file['error'] = __('File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini.');
        return $file;
    }
    $overrides = array('test_form' => false, 'test_type' => false);
    $_FILES['import']['name'] .= '.txt';
    $file = nxt_handle_upload($_FILES['import'], $overrides);
    if (isset($file['error'])) {
        return $file;
    }
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $filename = basename($file);
    // Construct the object array
    $object = array('post_title' => $filename, 'post_content' => $url, 'post_mime_type' => $type, 'guid' => $url, 'context' => 'import', 'post_status' => 'private');
    // Save the data
    $id = nxt_insert_attachment($object, $file);
    // schedule a cleanup for one day from now in case of failed import or missing nxt_import_cleanup() call
    nxt_schedule_single_event(time() + 86400, 'importer_scheduled_cleanup', array($id));
    return array('file' => $file, 'id' => $id);
}
Пример #2
0
/**
 * woothemes_metabox_handle function.
 * 
 * @access public
 * @return void
 */
function woothemes_metabox_handle()
{
    $pID = '';
    global $globals, $post;
    $woo_metaboxes = get_option('woo_custom_template');
    $seo_metaboxes = get_option('woo_custom_seo_template');
    if (!empty($seo_metaboxes) && get_option('seo_woo_hide_fields') != 'true') {
        $woo_metaboxes = array_merge((array) $woo_metaboxes, (array) $seo_metaboxes);
    }
    // Sanitize post ID.
    if (isset($_POST['post_ID'])) {
        $pID = intval($_POST['post_ID']);
    }
    // End IF Statement
    // Don't continue if we don't have a valid post ID.
    if ($pID == 0) {
        return;
    }
    // End IF Statement
    $upload_tracking = array();
    if (isset($_POST['action']) && $_POST['action'] == 'editpost') {
        foreach ($woo_metaboxes as $k => $woo_metabox) {
            // On Save.. this gets looped in the header response and saves the values submitted
            if (isset($woo_metabox['type']) && in_array($woo_metabox['type'], woothemes_metabox_fieldtypes())) {
                $var = $woo_metabox['name'];
                // Get the current value for checking in the script.
                $current_value = '';
                $current_value = get_post_meta($pID, $var, true);
                if (isset($_POST[$var])) {
                    // Sanitize the input.
                    $posted_value = '';
                    $posted_value = $_POST[$var];
                    // If it doesn't exist, add the post meta.
                    if (get_post_meta($pID, $var) == "") {
                        add_post_meta($pID, $var, $posted_value, true);
                    } elseif ($posted_value != get_post_meta($pID, $var, true)) {
                        update_post_meta($pID, $var, $posted_value);
                    } elseif ($posted_value == "") {
                        delete_post_meta($pID, $var, get_post_meta($pID, $var, true));
                    }
                    // End IF Statement
                } elseif (!isset($_POST[$var]) && $woo_metabox['type'] == 'checkbox') {
                    update_post_meta($pID, $var, 'false');
                } else {
                    delete_post_meta($pID, $var, $current_value);
                    // Deletes check boxes OR no $_POST
                }
                // End IF Statement
            } else {
                if ($woo_metabox['type'] == 'timestamp') {
                    // Timestamp save logic.
                    // It is assumed that the data comes back in the following format:
                    // date: month/day/year
                    // hour: int(2)
                    // minute: int(2)
                    // second: int(2)
                    $var = $woo_metabox['name'];
                    // Format the data into a timestamp.
                    $date = $_POST[$var]['date'];
                    $hour = $_POST[$var]['hour'];
                    $minute = $_POST[$var]['minute'];
                    $second = $_POST[$var]['second'];
                    $day = substr($date, 3, 2);
                    $month = substr($date, 0, 2);
                    $year = substr($date, 6, 4);
                    $timestamp = mktime($hour, $minute, $second, $month, $day, $year);
                    update_post_meta($pID, $var, $timestamp);
                } elseif (isset($woo_metabox['type']) && $woo_metabox['type'] == 'upload') {
                    // So, the upload inputs will do this rather
                    $id = $woo_metabox['name'];
                    $override['action'] = 'editpost';
                    if (!empty($_FILES['attachement_' . $id]['name'])) {
                        //New upload
                        $_FILES['attachement_' . $id]['name'] = preg_replace('/[^a-zA-Z0-9._\\-]/', '', $_FILES['attachement_' . $id]['name']);
                        $uploaded_file = nxt_handle_upload($_FILES['attachement_' . $id], $override);
                        $uploaded_file['option_name'] = $woo_metabox['label'];
                        $upload_tracking[] = $uploaded_file;
                        update_post_meta($pID, $id, $uploaded_file['url']);
                    } elseif (empty($_FILES['attachement_' . $id]['name']) && isset($_POST[$id])) {
                        // Sanitize the input.
                        $posted_value = '';
                        $posted_value = $_POST[$id];
                        update_post_meta($pID, $id, $posted_value);
                    } elseif ($_POST[$id] == '') {
                        delete_post_meta($pID, $id, get_post_meta($pID, $id, true));
                    }
                    // End IF Statement
                }
            }
            // End IF Statement
            // Error Tracking - File upload was not an Image
            update_option('woo_custom_upload_tracking', $upload_tracking);
        }
        // End FOREACH Loop
    }
    // End IF Statement
}
Пример #3
0
/**
 * Handles avatar uploading.
 *
 * The functions starts off by checking that the file has been uploaded properly using bp_core_check_avatar_upload().
 * It then checks that the file size is within limits, and that it has an accepted file extension (jpg, gif, png).
 * If everything checks out, crop the image and move it to its real location.
 *
 * @global object $bp BuddyPress global settings
 * @param array $file The appropriate entry the from $_FILES superglobal.
 * @param string $upload_dir_filter A filter to be applied to upload_dir
 * @return bool Success/failure
 * @see bp_core_check_avatar_upload()
 * @see bp_core_check_avatar_type()
 */
function bp_core_avatar_handle_upload($file, $upload_dir_filter)
{
    global $bp;
    /***
     * You may want to hook into this filter if you want to override this function.
     * Make sure you return false.
     */
    if (!apply_filters('bp_core_pre_avatar_handle_upload', true, $file, $upload_dir_filter)) {
        return true;
    }
    require_once ABSPATH . '/nxt-admin/includes/image.php';
    require_once ABSPATH . '/nxt-admin/includes/file.php';
    $uploadErrors = array(0 => __("There is no error, the file uploaded with success", 'buddypress'), 1 => __("Your image was bigger than the maximum allowed file size of: ", 'buddypress') . size_format(bp_core_avatar_original_max_filesize()), 2 => __("Your image was bigger than the maximum allowed file size of: ", 'buddypress') . size_format(bp_core_avatar_original_max_filesize()), 3 => __("The uploaded file was only partially uploaded", 'buddypress'), 4 => __("No file was uploaded", 'buddypress'), 6 => __("Missing a temporary folder", 'buddypress'));
    if (!bp_core_check_avatar_upload($file)) {
        bp_core_add_message(sprintf(__('Your upload failed, please try again. Error was: %s', 'buddypress'), $uploadErrors[$file['file']['error']]), 'error');
        return false;
    }
    if (!bp_core_check_avatar_size($file)) {
        bp_core_add_message(sprintf(__('The file you uploaded is too big. Please upload a file under %s', 'buddypress'), size_format(bp_core_avatar_original_max_filesize())), 'error');
        return false;
    }
    if (!bp_core_check_avatar_type($file)) {
        bp_core_add_message(__('Please upload only JPG, GIF or PNG photos.', 'buddypress'), 'error');
        return false;
    }
    // Filter the upload location
    add_filter('upload_dir', $upload_dir_filter, 10, 0);
    $bp->avatar_admin->original = nxt_handle_upload($file['file'], array('action' => 'bp_avatar_upload'));
    // Move the file to the correct upload location.
    if (!empty($bp->avatar_admin->original['error'])) {
        bp_core_add_message(sprintf(__('Upload Failed! Error was: %s', 'buddypress'), $bp->avatar_admin->original['error']), 'error');
        return false;
    }
    // Get image size
    $size = @getimagesize($bp->avatar_admin->original['file']);
    // Check image size and shrink if too large
    if ($size[0] > bp_core_avatar_original_max_width()) {
        $thumb = nxt_create_thumbnail($bp->avatar_admin->original['file'], bp_core_avatar_original_max_width());
        // Check for thumbnail creation errors
        if (is_nxt_error($thumb)) {
            bp_core_add_message(sprintf(__('Upload Failed! Error was: %s', 'buddypress'), $thumb->get_error_message()), 'error');
            return false;
        }
        // Thumbnail is good so proceed
        $bp->avatar_admin->resized = $thumb;
    }
    // We only want to handle one image after resize.
    if (empty($bp->avatar_admin->resized)) {
        $bp->avatar_admin->image->dir = str_replace(bp_core_avatar_upload_path(), '', $bp->avatar_admin->original['file']);
    } else {
        $bp->avatar_admin->image->dir = str_replace(bp_core_avatar_upload_path(), '', $bp->avatar_admin->resized);
        @unlink($bp->avatar_admin->original['file']);
    }
    // Check for nxt_Error on what should be an image
    if (is_nxt_error($bp->avatar_admin->image->dir)) {
        bp_core_add_message(sprintf(__('Upload failed! Error was: %s', 'buddypress'), $bp->avatar_admin->image->dir->get_error_message()), 'error');
        return false;
    }
    // Set the url value for the image
    $bp->avatar_admin->image->url = bp_core_avatar_url() . $bp->avatar_admin->image->dir;
    return true;
}
Пример #4
0
/**
 * {@internal Missing Short Description}}
 *
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link nxt_handle_upload()} behavior
 * @return int the ID of the attachment
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = nxt_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new nxt_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    // use image exif/iptc data for title and caption defaults if possible
    if ($image_meta = @nxt_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = nxt_insert_attachment($attachment, $file, $post_id);
    if (!is_nxt_error($id)) {
        nxt_update_attachment_metadata($id, nxt_generate_attachment_metadata($id, $file));
    }
    return $id;
}
Пример #5
0
 function __construct($form, $urlholder)
 {
     if (empty($_FILES[$form]['name']) && empty($_GET[$urlholder])) {
         nxt_die(__('Please select a file'));
     }
     //Handle a newly uploaded file, Else assume its already been uploaded
     if (!empty($_FILES)) {
         $overrides = array('test_form' => false, 'test_type' => false);
         $file = nxt_handle_upload($_FILES[$form], $overrides);
         if (isset($file['error'])) {
             nxt_die($file['error']);
         }
         $this->filename = $_FILES[$form]['name'];
         $this->package = $file['file'];
         // Construct the object array
         $object = array('post_title' => $this->filename, 'post_content' => $file['url'], 'post_mime_type' => $file['type'], 'guid' => $file['url'], 'context' => 'upgrader', 'post_status' => 'private');
         // Save the data
         $this->id = nxt_insert_attachment($object, $file['file']);
         // schedule a cleanup for 2 hours from now in case of failed install
         nxt_schedule_single_event(time() + 7200, 'upgrader_scheduled_cleanup', array($this->id));
     } elseif (is_numeric($_GET[$urlholder])) {
         // Numeric Package = previously uploaded file, see above.
         $this->id = (int) $_GET[$urlholder];
         $attachment = get_post($this->id);
         if (empty($attachment)) {
             nxt_die(__('Please select a file'));
         }
         $this->filename = $attachment->post_title;
         $this->package = get_attached_file($attachment->ID);
     } else {
         // Else, It's set to something, Back compat for plugins using the old (pre-3.3) File_Uploader handler.
         if (!(($uploads = nxt_upload_dir()) && false === $uploads['error'])) {
             nxt_die($uploads['error']);
         }
         $this->filename = $_GET[$urlholder];
         $this->package = $uploads['basedir'] . '/' . $this->filename;
     }
 }
Пример #6
0
    /**
     * Display second step of custom header image page.
     *
     * @since 2.1.0
     */
    function step_2()
    {
        check_admin_referer('custom-header-upload', '_nxtnonce-custom-header-upload');
        if (!current_theme_supports('custom-header-uploads')) {
            nxt_die(__('Cheatin’ uh?'));
        }
        $overrides = array('test_form' => false);
        $file = nxt_handle_upload($_FILES['import'], $overrides);
        if (isset($file['error'])) {
            nxt_die($file['error'], __('Image Upload Error'));
        }
        $url = $file['url'];
        $type = $file['type'];
        $file = $file['file'];
        $filename = basename($file);
        // Construct the object array
        $object = array('post_title' => $filename, 'post_content' => $url, 'post_mime_type' => $type, 'guid' => $url, 'context' => 'custom-header');
        // Save the data
        $id = nxt_insert_attachment($object, $file);
        list($width, $height, $type, $attr) = getimagesize($file);
        if ($width == HEADER_IMAGE_WIDTH && $height == HEADER_IMAGE_HEIGHT) {
            // Add the meta-data
            nxt_update_attachment_metadata($id, nxt_generate_attachment_metadata($id, $file));
            update_post_meta($id, '_nxt_attachment_is_custom_header', get_option('stylesheet'));
            set_theme_mod('header_image', esc_url($url));
            do_action('nxt_create_file_in_uploads', $file, $id);
            // For replication
            return $this->finished();
        } elseif ($width > HEADER_IMAGE_WIDTH) {
            $oitar = $width / HEADER_IMAGE_WIDTH;
            $image = nxt_crop_image($file, 0, 0, $width, $height, HEADER_IMAGE_WIDTH, $height / $oitar, false, str_replace(basename($file), 'midsize-' . basename($file), $file));
            if (is_nxt_error($image)) {
                nxt_die(__('Image could not be processed.  Please go back and try again.'), __('Image Processing Error'));
            }
            $image = apply_filters('nxt_create_file_in_uploads', $image, $id);
            // For replication
            $url = str_replace(basename($url), basename($image), $url);
            $width = $width / $oitar;
            $height = $height / $oitar;
        } else {
            $oitar = 1;
        }
        ?>

<div class="wrap">
<?php 
        screen_icon();
        ?>
<h2><?php 
        _e('Crop Header Image');
        ?>
</h2>

<form method="post" action="<?php 
        echo esc_attr(add_query_arg('step', 3));
        ?>
">
	<p class="hide-if-no-js"><?php 
        _e('Choose the part of the image you want to use as your header.');
        ?>
</p>
	<p class="hide-if-js"><strong><?php 
        _e('You need Javascript to choose a part of the image.');
        ?>
</strong></p>

	<div id="crop_image" style="position: relative">
		<img src="<?php 
        echo esc_url($url);
        ?>
" id="upload" width="<?php 
        echo $width;
        ?>
" height="<?php 
        echo $height;
        ?>
" />
	</div>

	<input type="hidden" name="x1" id="x1" value="0"/>
	<input type="hidden" name="y1" id="y1" value="0"/>
	<input type="hidden" name="width" id="width" value="<?php 
        echo esc_attr($width);
        ?>
"/>
	<input type="hidden" name="height" id="height" value="<?php 
        echo esc_attr($height);
        ?>
"/>
	<input type="hidden" name="attachment_id" id="attachment_id" value="<?php 
        echo esc_attr($id);
        ?>
" />
	<input type="hidden" name="oitar" id="oitar" value="<?php 
        echo esc_attr($oitar);
        ?>
" />
	<?php 
        nxt_nonce_field('custom-header-crop-image');
        ?>

	<?php 
        submit_button(__('Crop and Publish'));
        ?>
	</p>
</form>
</div>
		<?php 
    }
function woo_tumblog_file_upload()
{
    global $nxtdb;
    //Upload overrides
    $filename = $_FILES['userfile'];
    // [name] [tmp_name]
    $override['test_form'] = false;
    $override['action'] = 'nxt_handle_upload';
    //Handle Uploaded File
    $uploaded_file = nxt_handle_upload($filename, $override);
    // [file] [url] [type]
    //Create Attachment Object
    $attachment['post_title'] = $filename['name'];
    //post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type
    $attachment['post_content'] = '';
    $attachment['post_status'] = 'inherit';
    $attachment['post_mime_type'] = $uploaded_file['type'];
    $attachment['guid'] = $uploaded_file['url'];
    //Prepare file attachment
    $wud = nxt_upload_dir();
    // [path] [url] [subdir] [basedir] [baseurl] [error]
    $filename_attach = $wud['basedir'] . $uploaded_file['file'];
    //Insert Attachment
    $attach_id = nxt_insert_attachment($attachment, $filename_attach, 0);
    $attach_data = nxt_generate_attachment_metadata($attach_id, $filename_attach);
    nxt_update_attachment_metadata($attach_id, $attach_data);
    //Handle Errors and Response
    if (!empty($uploaded_file['error'])) {
        echo 'Upload Error: ' . $uploaded_file['error'];
    } else {
        echo $uploaded_file['url'] . '|' . $attach_id . '|';
    }
    // Is the Response
}
Пример #8
0
 /**
  * Handle an Image upload for the background image.
  *
  * @since 3.0.0
  */
 function handle_upload()
 {
     if (empty($_FILES)) {
         return;
     }
     check_admin_referer('custom-background-upload', '_nxtnonce-custom-background-upload');
     $overrides = array('test_form' => false);
     $file = nxt_handle_upload($_FILES['import'], $overrides);
     if (isset($file['error'])) {
         nxt_die($file['error']);
     }
     $url = $file['url'];
     $type = $file['type'];
     $file = $file['file'];
     $filename = basename($file);
     // Construct the object array
     $object = array('post_title' => $filename, 'post_content' => $url, 'post_mime_type' => $type, 'guid' => $url, 'context' => 'custom-background');
     // Save the data
     $id = nxt_insert_attachment($object, $file);
     // Add the meta-data
     nxt_update_attachment_metadata($id, nxt_generate_attachment_metadata($id, $file));
     update_post_meta($id, '_nxt_attachment_is_custom_background', get_option('stylesheet'));
     set_theme_mod('background_image', esc_url($url));
     $thumbnail = nxt_get_attachment_image_src($id, 'thumbnail');
     set_theme_mod('background_image_thumb', esc_url($thumbnail[0]));
     do_action('nxt_create_file_in_uploads', $file, $id);
     // For replication
     $this->updated = true;
 }