예제 #1
0
/**
 * Check if a file (or stream) is a valid image type
 *
 * @param string $data Can be a temporary filename (think $_FILE before saving to $filename) or a stream (string). Default is temporary filename
 * @param string $filename
 * @param bool $is_stream (optional)
 *
 * @return bool
 */
function is_valid_image($data, $filename, $is_stream = false)
{
    $format = explode('.', $filename);
    $format = strtolower(end($format));
    // Extension
    if ($format == 'jpg') {
        $format = 'jpeg';
    }
    // Fix stupid mistake
    if (!($format == 'jpeg' || $format == 'gif' || $format == 'png')) {
        return false;
    }
    if ($is_stream) {
        $tmp_image_path = \PressBooks\Utility\create_tmp_file();
        file_put_contents($tmp_image_path, $data);
        $data = $tmp_image_path;
    }
    /* Try to avoid problems with memory limit */
    fudge_factor($format, $data);
    $func = 'imagecreatefrom' . $format;
    $image = @$func($data);
    if ($image === false) {
        return false;
    }
    imagedestroy($image);
    unset($image);
    return true;
}
예제 #2
0
/**
 * Check if a file (or stream) is a valid image type
 *
 * @param string $file Can be a temporary filename (think $_FILE before saving to $filename) or a stream (string). Default is temporary filename
 * @param string $filename
 * @param bool $is_stream (optional)
 *
 * @return bool
 */
function is_valid_image($file, $filename, $is_stream = false)
{
    $format = explode('.', $filename);
    $format = strtolower(end($format));
    // Extension
    if (!($format == 'jpg' || $format == 'jpeg' || $format == 'gif' || $format == 'png')) {
        return false;
    }
    if ($is_stream) {
        $tmp_image_path = \PressBooks\Utility\create_tmp_file();
        file_put_contents($tmp_image_path, $file);
        $file = $tmp_image_path;
    }
    $type = @exif_imagetype($file);
    if (IMAGETYPE_JPEG == $type || IMAGETYPE_GIF == $type || IMAGETYPE_PNG == $type) {
        return true;
    } else {
        return false;
    }
}
예제 #3
0
 /**
  * Fetch a font 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 fetchAndSaveUniqueFont($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));
     $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));
     // TODO: Validate that this is actually a font
     // TODO: Refactor fetchAndSaveUniqueImage() and fetchAndSaveUniqueFont() into a single method, but "inject" different validation
     // 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;
 }
예제 #4
0
 /**
  * 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 = \PressBooks\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;
 }
예제 #5
0
 function convert()
 {
     $this->outputPath = \PressBooks\Utility\create_tmp_file();
     return true;
 }
예제 #6
0
 /**
  * Create a temporary file that automatically gets deleted on __sleep()
  *
  * @return string fullpath
  */
 function createTmpFile()
 {
     return \PressBooks\Utility\create_tmp_file();
 }
예제 #7
0
 /**
  * @covers \PressBooks\Utility\create_tmp_file
  */
 public function test_create_tmp_file()
 {
     $file = \PressBooks\Utility\create_tmp_file();
     $this->assertFileExists($file);
     file_put_contents($file, 'Hello world!');
     $this->assertEquals('Hello world!', file_get_contents($file));
 }
예제 #8
0
 /**
  * Fetch a url with wp_remote_get(), save it to $fullpath with a unique name.
  * Will return an empty string if something went wrong.
  *
  * @param string $url
  * @param string $fullpath
  * @return string|array
  */
 protected function fetchAndSaveUniqueMedia($url, $fullpath)
 {
     if (isset($this->fetchedMediaCache[$url])) {
         return $this->fetchedMediaCache[$url];
     }
     $response = wp_remote_get($url, array('timeout' => $this->timeout));
     // WordPress error?
     if (is_wp_error($response)) {
         // TODO: handle $response->get_error_message();
         $this->fetchedMediaCache[$url] = '';
         return '';
     }
     // Basename without query string
     $filename = explode('?', basename($url));
     $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\Media\is_valid_media($tmp_file, $filename)) {
         $this->fetchedMediaCache[$url] = '';
         return '';
         // Not a valid media type
     }
     // 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}");
     }
     $this->fetchedMediaCache[$url] = $filename;
     return $filename;
 }