diff --git a/imageinfo_cache.admin.inc b/imageinfo_cache.admin.inc index a3474cc..7fcf21f 100644 --- a/imageinfo_cache.admin.inc +++ b/imageinfo_cache.admin.inc @@ -42,6 +42,14 @@ function imageinfo_cache_admin_settings_form($form, $form_state) { '#default_value' => variable_get('imageinfo_cache_pseudo_image_toolkit', IMAGEINFO_CACHE_PSEUDO_IMAGE_TOOLKIT), '#description' => t('Useful if your filesystem is not local. Uses a wrapper around calls to the real image toolkit; putting caching in optimal places.'), ); + if (variable_get('image_allow_insecure_derivatives', FALSE)) { + $form['imageinfo_cache_strip_image_token'] = array( + '#type' => 'checkbox', + '#title' => t('Remove itok from images'), + '#default_value' => variable_get('imageinfo_cache_strip_image_token', IMAGEINFO_CACHE_STRIP_IMAGE_TOKEN), + '#description' => t('Sometimes the ?itok=___ query parameter can cause issues. Enabling this will remove itok from image style output.'), + ); + } $form['pre_generation'] = array( '#type' => 'fieldset', diff --git a/imageinfo_cache.module b/imageinfo_cache.module index 813b865..25b53d3 100644 --- a/imageinfo_cache.module +++ b/imageinfo_cache.module @@ -15,6 +15,11 @@ define('IMAGEINFO_CACHE_USE_HTTPRL', TRUE); */ define('IMAGEINFO_CACHE_PSEUDO_IMAGE_TOOLKIT', FALSE); +/** + * Default value is to not cache getimagesize(). + */ +define('IMAGEINFO_CACHE_STRIP_IMAGE_TOKEN', FALSE); + // Core Hooks. /** @@ -92,6 +97,35 @@ function imageinfo_cache_form_system_image_toolkit_settings_alter(&$form, &$form } /** + * Implements hook_file_url_alter(). + * + * Strip itok from image style images. + */ +function imageinfo_cache_file_url_alter(&$uri) { + // If filename does not contain itok= + // OR image_allow_insecure_derivatives is FALSE + // OR imageinfo_cache_strip_image_token is FALSE + if ( strpos($uri, IMAGE_DERIVATIVE_TOKEN . '=') === FALSE + || !variable_get('image_allow_insecure_derivatives', FALSE) + || !variable_get('imageinfo_cache_strip_image_token', IMAGEINFO_CACHE_STRIP_IMAGE_TOKEN) + ) { + return; + } + $parsed = @parse_url($uri); + $query = array(); + parse_str($parsed['query'], $query); + if (!isset($query[IMAGE_DERIVATIVE_TOKEN])) { + return; + } + unset($query[IMAGE_DERIVATIVE_TOKEN]); + $parsed['query'] = http_build_query($query, '', '&'); + if (empty($parsed['query'])) { + unset($parsed['query']); + } + $uri = imageinfo_cache_glue_url($parsed); +} + +/** * Implements hook_field_widget_form_alter(). * * Capture image on upload. @@ -457,3 +491,34 @@ function imageinfo_cache_create_image_styles_call($uri, $instance_field = array( } return $return; } + +/** + * Alt to http_build_url(). + * + * @see http://php.net/parse-url#85963 + * + * @param array $parsed + * array from parse_url() + * + * @return string + * URI is returned. + */ +function imageinfo_cache_glue_url($parsed) { + if (!is_array($parsed)) { + return FALSE; + } + + $uri = isset($parsed['scheme']) ? $parsed['scheme'] . ':' . ((strtolower($parsed['scheme']) == 'mailto') ? '' : '//') : ''; + $uri .= isset($parsed['user']) ? $parsed['user'] . (isset($parsed['pass']) ? ':' . $parsed['pass'] : '') . '@' : ''; + $uri .= isset($parsed['host']) ? $parsed['host'] : ''; + $uri .= !empty($parsed['port']) ? ':' . $parsed['port'] : ''; + + if (isset($parsed['path'])) { + $uri .= (substr($parsed['path'], 0, 1) == '/') ? $parsed['path'] : ((!empty($uri) ? '/' : '') . $parsed['path']); + } + + $uri .= isset($parsed['query']) ? '?' . $parsed['query'] : ''; + $uri .= isset($parsed['fragment']) ? '#' . $parsed['fragment'] : ''; + + return $uri; +}