/**
  * 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;
 }
Esempio n. 2
0
 /**
  * @since 4.6.10
  * @param WPRSS_Image_Cache_Image $image
  */
 protected function _prepare_image($image)
 {
     $image->set_download_request_timeout($this->get_download_request_timeout());
 }