( $do_resize && $size ) {
$old_temp = $tmp_file;
$tmp_file = image_resize( $tmp_file, (int) get_option('large_size_w'), (int) get_option('large_size_h'), 0, 'resized');
if ( ! is_wp_error($tmp_file) ) {
unlink($old_temp);
} else {
$tmp_file = $old_temp;
}
}
// Copy the temporary file into its destination
$new_file = $uploads['path'] . "/$filename";
copy( $tmp_file, $new_file );
unlink($tmp_file);
// Set correct file permissions
$stat = stat( dirname( $new_file ));
$perms = $stat['mode'] & 0000666;
@ chmod( $new_file, $perms );
// Compute the URL
$url = $uploads['url'] . "/$filename";
if ( is_multisite() )
delete_transient( 'dirsize_cache' );
return apply_filters( 'wp_handle_upload', array( 'file' => $new_file, 'url' => $url, 'type' => $type ), 'upload' );
}
/**
* Handle sideloads, which is the process of retrieving a media item from another server instead of
* a traditional media upload. This process involves sanitizing the filename, checking extensions
* for mime type, and moving the file to the appropriate directory within the uploads directory.
*
* @since 2.6.0
*
* @uses wp_handle_upload_error
* @uses apply_filters
* @uses wp_check_filetype_and_ext
* @uses current_user_can
* @uses wp_upload_dir
* @uses wp_unique_filename
* @param array $file an array similar to that of a PHP $_FILES POST array
* @param array $overrides Optional. An associative array of names=>values to override default variables with extract( $overrides, EXTR_OVERWRITE ).
* @return array On success, returns an associative array of file attributes. On failure, returns $overrides['upload_error_handler'](&$file, $message ) or array( 'error'=>$message ).
*/
function wp_handle_sideload( &$file, $overrides = false ) {
// The default error handler.
if (! function_exists( 'wp_handle_upload_error' ) ) {
function wp_handle_upload_error( &$file, $message ) {
return array( 'error'=>$message );
}
}
// You may define your own function and pass the name in $overrides['upload_error_handler']
$upload_error_handler = 'wp_handle_upload_error';
// You may define your own function and pass the name in $overrides['unique_filename_callback']
$unique_filename_callback = null;
// $_POST['action'] must be set and its value must equal $overrides['action'] or this:
$action = 'wp_handle_sideload';
// Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error'].
$upload_error_strings = array( false,
__( "The uploaded file exceeds the upload_max_filesize directive in php.ini." ),
__( "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form." ),
__( "The uploaded file was only partially uploaded." ),
__( "No file was uploaded." ),
'',
__( "Missing a temporary folder." ),
__( "Failed to write file to disk." ),
__( "File upload stopped by extension." ));
// All tests are on by default. Most can be turned off by $overrides[{test_name}] = false;
$test_form = true;
$test_size = true;
// If you override this, you must provide $ext and $type!!!!
$test_type = true;
$mimes = false;
// Install user overrides. Did we mention that this voids your warranty?
if ( is_array( $overrides ) )
extract( $overrides, EXTR_OVERWRITE );
// A correct form post will pass this test.
if ( $test_form && (!isset( $_POST['action'] ) || ($_POST['action'] != $action ) ) )
return $upload_error_handler( $file, __( 'Invalid form submission.' ));
// A successful upload will pass this test. It makes no sense to override this one.
if ( ! empty( $file['error'] ) )
return $upload_error_handler( $file, $upload_error_strings[$file['error']] );
// A non-empty file will pass this test.
if ( $test_size && !(filesize($file['tmp_name']) > 0 ) )
return $upload_error_handler( $file, __( 'File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini.' ));
// A properly uploaded file will pass this test. There should be no reason to override this one.
if (! @ is_file( $file['tmp_name'] ) )
return $upload_error_handler( $file, __( 'Specified file does not exist.' ));
// A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
if ( $test_type ) {
$wp_filetype = wp_check_filetype_and_ext( $file['tmp_name'], $file['name'], $mimes );
extract( $wp_filetype );
// Check to see if wp_check_filetype_and_ext() determined the filename was incorrect
if ( $proper_filename )
$file['name'] = $proper_filename;
if ( ( !$type || !$ext ) && !current_user_can( 'unfiltered_upload' ) )
return $upload_error_handler( $file, __( 'Sorry, this file type is not permitted for security reasons.' ));
if ( !$ext )
$ext = ltrim(strrchr($file['name'], '.'), '.');
if ( !$type )
$type = $file['type'];
}
// A writable uploads dir will pass this test. Again, there's no point overriding this one.
if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
return $upload_error_handler( $file, $uploads['error'] );
$filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback );
// Strip the query strings.
$filename = str_replace('?','-', $filename);
$filename = str_replace('&','-', $filename);
// Move the file to the uploads dir
$new_file = $uploads['path'] . "/$filename";
if ( false === @ rename( $file['tmp_name'], $new_file ) ) {
return $upload_error_handler( $file, sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path'] ) );
}
// Set correct file permissions
$stat = stat( dirname( $new_file ));
$perms = $stat['mode'] & 0000666;
@ chmod( $new_file, $perms );
// Compute the URL
$url = $uploads['url'] . "/$filename";
$return = apply_filters( 'wp_handle_upload', array( 'file' => $new_file, 'url' => $url, 'type' => $type ), 'sideload' );
return $return;
}
/**
* Downloads a url to a local temporary file using the WordPress HTTP Class.
* Please note, That the calling function must unlink() the file.
*
* @since 2.5.0
*
* @param string $url the URL of the file to download
* @param int $timeout The timeout for the request to download the file default 300 seconds
* @return mixed WP_Error on failure, string Filename on success.
*/
function download_url( $url, $timeout = 300 ) {
//WARNING: The file is not automatically deleted, The script must unlink() the file.
if ( ! $url )
return new WP_Error('http_no_url', __('Invalid URL Provided.'));
$tmpfname = wp_tempnam($url);
if ( ! $tmpfname )
return new WP_Error('http_no_file', __('Could not create Temporary file.'));
$response = wp_remote_get( $url, array( 'timeout' => $timeout, 'stream' => true, 'filename' => $tmpfname ) );
if ( is_wp_error( $response ) ) {
unlink( $tmpfname );
return $response;
}
if ( 200 != wp_remote_retrieve_response_code( $response ) ){
unlink( $tmpfname );
return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ) );
}
return $tmpfname;
}
/**
* Unzips a specified ZIP file to a location on the Filesystem via the WordPress Filesystem Abstraction.
* Assumes that WP_Filesystem() has already been called and set up. Does not extract a root-level __MACOSX directory, if present.
*
* Attempts to increase the PHP Memory limit to 256M before uncompressing,
* However, The most memory required shouldn't be much larger than the Archive itself.
*
* @since 2.5.0
*
* @param string $file Full path and filename of zip archive
* @param string $to Full path on the filesystem to extract archive to
* @return mixed WP_Error on failure, True on success
*/
function unzip_file($file, $to) {
global $wp_filesystem;
if ( ! $wp_filesystem || !is_object($wp_filesystem) )
return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
// Unzip can use a lot of memory, but not this much hopefully
@ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );
$needed_dirs = array();
$to = trailingslashit($to);
// Determine any parent dir's needed (of the upgrade directory)
if ( !g appropriate to my dad (lived through the first great depression and WWII) or anyone of his friends. He always seemed to be preparing for the next downturn, the last paycheck, or the next layoff. One generation or so later and this attitude is lost?
This calls for more than an attitude adjustment, it forces us to rethink how institutional memory works so that in another 80 years we don't forget all over again. (I know 'Remember AIG' doesn't quite have the same ring as 'Remember the Alamo' but we can work on it.)
Posted by Fred H Schlegel at March 23, 2009 10:10 PM
Great Comments. I do think there are people around who have values and are trying to teach the next generation - I suspect (as is common) that they are not the noisier elements in society.
(I have just come from a conversation with my 8 year old about the cost/benefits of joining Club Penguin - basically if the lad wants it he can pay for it himself!)
Fred although I think there is some truth in what you say I also believe that we emphasise different elements in society at different times. rather than one type of person simply disappearing.
One thing that still disturbs me though is just how embedded credit culture is in our society and this does not seem to reducing (getting worse if anything). More and more connectivity deals seem have a "free" laptop with them. You generally don't pay the full cost of a mobile phone - you buy that on a form of credit (call charges over time). "Interest Free credit" is another one that sickens me (basically if they can afford to not charge interest they could cut me a better initial deal). Rather than reducing (as I stupidly thought) with the credit problems - it seems to be getting worse.
Probably the most valuable thing we could teach children is not values (only kidding!) but how to calculate interest payments over a 20 year period......
Posted by PaulH at March 24, 2009 2:53 AM
Trevor--somewhat agree, but remember that I will teach my children MY values, not YOURS, or those that someone (who means well, but...) decides I should. Be a role model for your own, but let others decide what is best for them and theirs. I fervently decry the growing prevalence of the socialist nanny-state and forced sense of "community" now being espoused by those who only have the best interests of "our children" at heart. I shall, therefore, teach my children to rail and fight this at every turn and be a proper role model for them to follow.
Judith--yup, it's GONE. Get over being mad at (insert today's villains here) and get on with the rest of your life. Pay attention and you won't suffer so much the next time it happens. And it most certainly shall happen again. No amount of "after the horse is gone" regulation excess will prevent someone finding the next scheme. As Mark Twain said, "nothing is fool-proof because fools are so ingenious."
Posted by Useless Sam Grant at March 26, 2009 10:16 AM
Useless Sam - I reject the premise that nothing will change. This is exactly why "fools" continue in their "ingenious" schemes and WE ALLOW them to do so. As I said, this seemed like a designed perfect storm. One of the biggest problems is how we define and revere some and denigrate and debase others--all being criminals though some escape sentence while others go straight to jail for what some see as far lesser crimes.
It's not that these guys are ingenious; they count on our limited memories and desire for quick fixes. Instant gratification is more important than reform; a return to the same is often preferred in such a time, thus giving those in power continued power without demanding reform through various means. A criminal is a criminal ingenious or stupid. Geithner's call for a risk regulator is important. But, of course, who will appoint and watch this one will be important.
Motive matters in such behaviors as well as the ill effect on society. Clawbacks, lockup, and community service are necessary. Put their "ingenuity" at work in an inner city or rural area under ball and chain--not really. Do not speak to me of silenced anger. "Be angry and do not sin." (Ephesians 4:26)
By the way, whose money is gone?
Posted by Judith Ellis at March 26, 2009 12:19 PM
'Be a role model for your own, but let others decide what is best for them and theirs'
Useless Sam - I say Amen to that my friend .... and speaking personally I hope I've taught my 3 children more about values than I have about money :-)
Posted by Trevor Gay at March 26, 2009 3:52 PM
Before blogging became all the rage, Tom was posting book reviews and Observations (essentially early blog posts) to this site. You can find the archives below.
What we're talking about
on the front page.