public static function download($sURL, $iTimeOut = 300) { if (false === filter_var($sURL, FILTER_VALIDATE_URL)) { return false; } $_sTmpFileName = self::setTempPath(self::getBaseNameOfURL($sURL)); if (!$_sTmpFileName) { return false; } $_aoResponse = wp_safe_remote_get($sURL, array('timeout' => $iTimeOut, 'stream' => true, 'filename' => $_sTmpFileName)); if (is_wp_error($_aoResponse)) { unlink($_sTmpFileName); return false; } if (200 != wp_remote_retrieve_response_code($_aoResponse)) { unlink($_sTmpFileName); return false; } $_sContent_md5 = wp_remote_retrieve_header($_aoResponse, 'content-md5'); if ($_sContent_md5) { $_boIsMD5 = verify_file_md5($_sTmpFileName, $_sContent_md5); if (is_wp_error($_boIsMD5)) { unlink($_sTmpFileName); return false; } } return $_sTmpFileName; }
/** * 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_safe_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 ) ) ); } $content_md5 = wp_remote_retrieve_header( $response, 'content-md5' ); if ( $content_md5 ) { $md5_check = verify_file_md5( $tmpfname, $content_md5 ); if ( is_wp_error( $md5_check ) ) { unlink( $tmpfname ); return $md5_check; } } return $tmpfname; }
/** * Downloads a resource identified by the parameters. * * If only the first parameter is passed, and it is an instance of a cache * file, downloads the resource described by it. * If it is a URL, the request timeout and target path will be computed by this instance. * Otherwise, they will be overridden by the specified values, respectively. * * If the 'content-md5' header is present in the responce, MD5 checksum * verification will take place. * * @since 4.7.3 * @see get_download_request_timeout() * @see WPRSS_Image_Cache_Image::get_download_request_timeout() * @see get_unique_filename() * @see WPRSS_Image_Cache_Image::get_current_path() * @see get_tmp_dir() * @see wp_mkdir_p() * @see wp_safe_remote_get() * @see verify_file_md5() * @param WPRSS_Image_Cache_Image|string $image An instance of a cache file, or the URL to download. * @param int|null $request_timeout The timeout for the download request. * @param string|null $target_path The relative path to the target file. * @return string| The absolute local path to the downloaded file, * or false if checksum verification failed. * @throws Exception If the URL is invalid, or the destination path is not writable, * or the file download library cannot be read, or any other error happens during download. */ public function download_image($image, $request_timeout = null, $target_path = null) { if ($image instanceof WPRSS_Image_Cache_Image) { $url = $image->get_url(); $timeout = $image->get_download_request_timeout(); $path = $image->get_current_path(); } else { $url = $image; $timeout = $this->get_download_request_timeout(); $path = $this->get_unique_filename($url); } if (!$url) { throw new Exception(sprintf(__('Invalid URL provided: "%1$s"'), $url)); } if (!is_null($target_path)) { $path = $target_path; } if (is_null($request_timeout)) { $timeout = $request_timeout; } // Absolute path to the cache file $tmpfname = $image instanceof WPRSS_Image_Cache_Image ? $image->get_tmp_dir($path) : $this->get_tmp_dir($path); //WARNING: The file is not automatically deleted, The script must unlink() the file. $dirname = dirname($tmpfname); if (!wp_mkdir_p($dirname)) { throw new Exception(sprintf(__('Could not create directory: "%1$s". Filename: "%2$s"'), $dirname, $tmpfname)); } // Getting file download lib $file_lib_path = ABSPATH . 'wp-admin/includes/file.php'; if (!is_readable($file_lib_path)) { throw new Exception(sprintf(__('The file library cannot be read from %1$s'), $file_lib_path)); } require_once $file_lib_path; // Retrieving the remote resource $response = wp_safe_remote_get($url, array('timeout' => $timeout, 'stream' => true, 'filename' => $tmpfname)); // Could not retrieve if (is_wp_error($response)) { @unlink($tmpfname); throw new Exception($response->get_error_message()); } // Retrieved, but remote server served error instead of resource if (200 != wp_remote_retrieve_response_code($response)) { @unlink($tmpfname); throw new Exception(trim(wp_remote_retrieve_response_message($response))); } // Checksum verification $content_md5 = wp_remote_retrieve_header($response, 'content-md5'); if ($content_md5) { $md5_check = verify_file_md5($tmpfname, $content_md5); if (is_wp_error($md5_check)) { unlink($tmpfname); return $md5_check; } } return $tmpfname; }
public function sideload() { //setup temp dir $temp_file = wp_tempnam($this->_upload_from); if (!$temp_file) { EE_Error::add_error(__('Something went wrong with the upload. Unable to create a tmp file for the uploaded file on the server', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__); return false; } do_action('AHEE__EEH_Sideloader__sideload__before', $this, $temp_file); $wp_remote_args = apply_filters('FHEE__EEH_Sideloader__sideload__wp_remote_args', array('timeout' => 500, 'stream' => true, 'filename' => $temp_file), $this, $temp_file); $response = wp_safe_remote_get($this->_upload_from, $wp_remote_args); if (is_wp_error($response) || 200 != wp_remote_retrieve_response_code($response)) { unlink($temp_file); if (defined('WP_DEBUG') && WP_DEBUG) { EE_Error::add_error(sprintf(__('Unable to upload the file. Either the path given to upload from is incorrect, or something else happened. Here is the response returned:<br />%s<br />Here is the path given: %s', 'event_espresso'), var_export($response, true), $this->_upload_from), __FILE__, __FUNCTION__, __LINE__); } return false; } //possible md5 check $content_md5 = wp_remote_retrieve_header($response, 'content-md5'); if ($content_md5) { $md5_check = verify_file_md5($temp_file, $content_md5); if (is_wp_error($md5_check)) { unlink($temp_file); EE_Error::add_error($md5_check->get_error_message(), __FILE__, __FUNCTION__, __LINE__); return false; } } $file = $temp_file; //now we have the file, let's get it in the right directory with the right name. $path = apply_filters('FHEE__EEH_Sideloader__sideload__new_path', $this->_upload_to . $this->_new_file_name, $this); //move file in if (false === @rename($file, $path)) { unlink($temp_file); EE_Error::add_error(sprintf(__('Unable to move the file to new location (possible permissions errors). This is the path the class attempted to move the file to: %s', 'event_espresso'), $path), __FILE__, __FUNCTION__, __LINE__); return false; } //set permissions $permissions = apply_filters('FHEE__EEH_Sideloader__sideload__permissions_applied', $this->_permissions, $this); chmod($path, $permissions); //that's it. let's allow for actions after file uploaded. do_action('AHEE__EE_Sideloader__sideload_after', $this, $path); //unlink tempfile @unlink($temp_file); return true; }