/** * Fetch an image with wp_remote_get(), save it to $fullpath with a unique name. * Will return an empty string if something went wrong. * * @param $url string * @param $fullpath string * * @return string filename */ protected function fetchAndSaveUniqueImage($url, $fullpath) { // Cheap cache static $already_done = array(); if (isset($already_done[$url])) { return $already_done[$url]; } $response = wp_remote_get($url, array('timeout' => $this->timeout)); // WordPress error? if (is_wp_error($response)) { // TODO: handle $response->get_error_message(); $already_done[$url] = ''; return ''; } // Basename without query string $filename = explode('?', basename($url)); // isolate latex image service from WP, add file extension if ('s.wordpress.com' == parse_url($url, PHP_URL_HOST) && 'latex.php' == $filename[0]) { $filename = md5(array_pop($filename)); // content-type = 'image/png' $type = explode('/', $response['headers']['content-type']); $type = array_pop($type); $filename = $filename . "." . $type; } else { $filename = array_shift($filename); $filename = sanitize_file_name(urldecode($filename)); $filename = Sanitize\force_ascii($filename); } $tmp_file = \PressBooks\Utility\create_tmp_file(); file_put_contents($tmp_file, wp_remote_retrieve_body($response)); if (!\PressBooks\Image\is_valid_image($tmp_file, $filename)) { $already_done[$url] = ''; return ''; // Not an image } if ($this->compressImages) { $format = explode('.', $filename); $format = strtolower(end($format)); // Extension \PressBooks\Image\resize_down($format, $tmp_file); } // Check for duplicates, save accordingly if (!file_exists("{$fullpath}/{$filename}")) { copy($tmp_file, "{$fullpath}/{$filename}"); } elseif (md5(file_get_contents($tmp_file)) != md5(file_get_contents("{$fullpath}/{$filename}"))) { $filename = wp_unique_filename($fullpath, $filename); copy($tmp_file, "{$fullpath}/{$filename}"); } $already_done[$url] = $filename; return $filename; }
/** * Load remote url of image into WP using media_handle_sideload() * Will return an empty string if something went wrong. * * @param string $url * * @see media_handle_sideload * * @return string filename */ protected function fetchAndSaveUniqueImage($url) { if (!filter_var($url, FILTER_VALIDATE_URL)) { return ''; } $remote_img_location = $url; // Cheap cache static $already_done = array(); if (isset($already_done[$remote_img_location])) { return $already_done[$remote_img_location]; } /* Process */ // Basename without query string $filename = explode('?', basename($url)); $filename = array_shift($filename); $filename = sanitize_file_name(urldecode($filename)); if (!preg_match('/\\.(jpe?g|gif|png)$/i', $filename)) { // Unsupported image type $already_done[$remote_img_location] = ''; return ''; } $tmp_name = download_url($remote_img_location); if (is_wp_error($tmp_name)) { // Download failed $already_done[$remote_img_location] = ''; return ''; } if (!\PressBooks\Image\is_valid_image($tmp_name, $filename)) { try { // changing the file name so that extension matches the mime type $filename = $this->properImageExtension($tmp_name, $filename); if (!\PressBooks\Image\is_valid_image($tmp_name, $filename)) { throw new \Exception('Image is corrupt, and file extension matches the mime type'); } } catch (\Exception $exc) { // Garbage, don't import $already_done[$remote_img_location] = ''; unlink($tmp_name); return ''; } } $pid = media_handle_sideload(array('name' => $filename, 'tmp_name' => $tmp_name), 0); $src = wp_get_attachment_url($pid); if (!$src) { $src = ''; } // Change false to empty string $already_done[$remote_img_location] = $src; @unlink($tmp_name); return $src; }
/** * Extract url from zip and load into WP using media_handle_sideload() * Will return an empty string if something went wrong. * * @param string $href original filename, with (relative) path * * @see media_handle_sideload * * @return string filename * @throws \Exception */ protected function fetchAndSaveUniqueImage($href) { $img_location = $href; // Cheap cache static $already_done = array(); if (isset($already_done[$img_location])) { return $already_done[$img_location]; } /* Process */ // Basename without query string $filename = explode('?', basename($href)); $filename = array_shift($filename); $filename = sanitize_file_name(urldecode($filename)); if (!preg_match('/\\.(jpe?g|gif|png)$/i', $filename)) { // Unsupported image type $already_done[$img_location] = ''; return ''; } $image_content = $this->getZipContent($img_location, false); if (!$image_content) { $already_done[$img_location] = ''; return ''; } $tmp_name = $this->createTmpFile(); file_put_contents($tmp_name, $image_content); if (!\PressBooks\Image\is_valid_image($tmp_name, $filename)) { try { // changing the file name so that extension matches the mime type $filename = $this->properImageExtension($tmp_name, $filename); if (!\PressBooks\Image\is_valid_image($tmp_name, $filename)) { throw new \Exception('Image is corrupt, and file extension matches the mime type'); } } catch (\Exception $exc) { // Garbage, Don't import $already_done[$img_location] = ''; return ''; } } $pid = media_handle_sideload(array('name' => $filename, 'tmp_name' => $tmp_name), 0); $src = wp_get_attachment_url($pid); if (!$src) { $src = ''; } // Change false to empty string $already_done[$img_location] = $src; return $src; }
/** * Extract url from zip and load into WP using media_handle_sideload() * Will return an empty string if something went wrong. * * @param $url string * @param string $href original filename, with (relative) path * * @see media_handle_sideload * * @return string filename * @throws \Exception */ protected function fetchAndSaveUniqueImage($url, $href) { $path_parts = pathinfo($href); $dir = @$path_parts['dirname']; $img_location = $dir ? "{$dir}/{$url}" : $url; // Cheap cache static $already_done = array(); if (isset($already_done[$img_location])) { return $already_done[$img_location]; } /* Process */ // Basename without query string $filename = explode('?', basename($url)); $filename = array_shift($filename); $filename = sanitize_file_name(urldecode($filename)); if (!preg_match('/\\.(jpe?g|gif|png)$/i', $filename)) { // Unsupported image type $already_done[$img_location] = ''; return ''; } $image_content = $this->getZipContent("{$dir}/{$url}", false); if (!$image_content) { // Could not find image? try { // case where $url is '../Images/someimage.jpg' $trimUrl = ltrim($url, './'); $image_content = $this->getZipContent($this->basedir . $trimUrl, false); if (!$image_content) { throw new \Exception('Could not import images from EPUB'); } } catch (\Exception $e) { $already_done[$img_location] = ''; return ''; } } $tmp_name = $this->createTmpFile(); file_put_contents($tmp_name, $image_content); if (!\PressBooks\Image\is_valid_image($tmp_name, $filename)) { try { // changing the file name so that extension matches the mime type $filename = $this->properImageExtension($tmp_name, $filename); if (!\PressBooks\Image\is_valid_image($tmp_name, $filename)) { throw new \Exception('Image is corrupt, and file extension matches the mime type'); } } catch (\Exception $exc) { // Garbage, Don't import $already_done[$img_location] = ''; return ''; } } $pid = media_handle_sideload(array('name' => $filename, 'tmp_name' => $tmp_name), 0); $src = wp_get_attachment_url($pid); if (!$src) { $src = ''; } // Change false to empty string $already_done[$img_location] = $src; return $src; }
/** * Extract url from zip and load into WP using media_handle_sideload() * Will return an empty string if something went wrong. * * @param string $img_id * * @return string */ protected function fetchAndSaveUniqueImage($img_id) { // Cheap cache static $already_done = array(); if (isset($already_done[$img_id])) { return $already_done[$img_id]; } /* Process */ // Get target path $img_location = $this->getTargetPath(self::IMAGE_SCHEMA, $img_id); // Basename without query string $filename = explode('?', basename($img_location)); $filename = array_shift($filename); $filename = sanitize_file_name(urldecode($filename)); if (!preg_match('/\\.(jpe?g|gif|png)$/i', $filename)) { // Unsupported image type $already_done[$img_id] = ''; return ''; } $image_content = $this->getZipContent($img_location, false); if (!$image_content) { // try a different directory in the zip container try { $alt_img_location = 'word/' . $img_location; $image_content = $this->getZipContent($alt_img_location, false); if (!$image_content) { throw new \Exception('Image could not be retrieved in the DOCX file with PressBooks\\Import\\Ooxml\\fetchAndSaveUniqueImage()'); } } catch (\Exception $exc) { $this->log($exc->getMessage()); $already_done[$img_location] = ''; return ''; } } $tmp_name = $this->createTmpFile(); file_put_contents($tmp_name, $image_content); if (!\PressBooks\Image\is_valid_image($tmp_name, $filename)) { try { // changing the file name so that extension matches the mime type $filename = $this->properImageExtension($tmp_name, $filename); if (!\PressBooks\Image\is_valid_image($tmp_name, $filename)) { throw new \Exception('Image is corrupt, and file extension matches the mime type'); } } catch (\Exception $exc) { // Garbage, Don't import $already_done[$img_location] = ''; return ''; } } $pid = media_handle_sideload(array('name' => $filename, 'tmp_name' => $tmp_name), 0); $src = wp_get_attachment_url($pid); if (!$src) { $src = ''; } // Change false to empty string $already_done[$img_location] = $src; return $src; }