Issue #2099205 by Wim Leers, zero2one, Gábor Hojtsy: When uploading and inserting an image trough the WYSIWYG plugin a relative path should be used for the image source (src) .

8.0.x
webchick 2013-11-23 12:12:07 -08:00
parent d13ac6367c
commit d5906706f5
4 changed files with 69 additions and 10 deletions

View File

@ -428,6 +428,7 @@ function file_stream_wrapper_get_instance_by_scheme($scheme) {
* could not be found to generate an external URL, then FALSE is returned.
*
* @see http://drupal.org/node/515192
* @see file_url_transform_relative()
*/
function file_create_url($uri) {
// Allow the URI to be altered, e.g. to serve a file from a CDN or static
@ -469,6 +470,41 @@ function file_create_url($uri) {
}
}
/**
* Transforms an absolute URL of a local file to a relative URL.
*
* May be useful to prevent problems on multisite set-ups and prevent mixed
* content errors when using HTTPS + HTTP.
*
* @param string $file_url
* A file URL of a local file as generated by file_create_url().
*
* @return string
* If the file URL indeed pointed to a local file and was indeed absolute,
* then the transformed, relative URL to the local file. Otherwise: the
* original value of $file_url.
*
* @see file_create_url()
*/
function file_url_transform_relative($file_url) {
// Unfortunately, we pretty much have to duplicate Symfony's
// Request::getHttpHost() method because Request::getPort() may return NULL
// instead of a port number.
$http_host = '';
$request = \Drupal::request();
$host = $request->getHost();
$scheme = $request->getScheme();
$port = $request->getPort() ?: 80;
if (('http' == $scheme && $port == 80) || ('https' == $scheme && $port == 443)) {
$http_host = $host;
}
else {
$http_host = $host . ':' . $port;
}
return preg_replace('|^https?://' . $http_host . '|', '', $file_url);
}
/**
* Checks that the directory exists and is writable.
*

View File

@ -199,7 +199,11 @@ class EditorImageDialog extends FormBase {
// attributes.
if (!empty($form_state['values']['fid'][0])) {
$file = file_load($form_state['values']['fid'][0]);
$form_state['values']['attributes']['src'] = file_create_url($file->getFileUri());
$file_url = file_create_url($file->getFileUri());
// Transform absolute image URLs to relative image URLs: prevent problems
// on multisite set-ups and prevent mixed content errors.
$file_url = file_url_transform_relative($file_url);
$form_state['values']['attributes']['src'] = $file_url;
$form_state['values']['attributes']['data-editor-file-uuid'] = $file->uuid();
}

View File

@ -1377,10 +1377,9 @@ function _filter_html_image_secure_process($text) {
$images = $html_dom->getElementsByTagName('img');
foreach ($images as $image) {
$src = $image->getAttribute('src');
// Remove absolute URLs pointing to the local domain to prevent mixed
// content errors.
$request = \Drupal::request();
$image->setAttribute('src', preg_replace('|^https?://' . $request->getHttpHost() . '|', '', $src));
// Transform absolute image URLs to relative image URLs: prevent problems on
// multisite set-ups and prevent mixed content errors.
$image->setAttribute('src', file_url_transform_relative($src));
// Verify that $src starts with $base_path.
// This also ensures that external images cannot be referenced.

View File

@ -2,7 +2,7 @@
/**
* @file
* Definition of Drupal\system\Tests\File\UrlRewritingTest.
* Contains Drupal\system\Tests\File\UrlRewritingTest.
*/
namespace Drupal\system\Tests\File;
@ -28,7 +28,7 @@ class UrlRewritingTest extends FileTestBase {
}
/**
* Test the generating of rewritten shipped file URLs.
* Tests the rewriting of shipped file URLs by hook_file_url_alter().
*/
function testShippedFileURL() {
// Test generating an URL to a shipped file (i.e. a file that is part of
@ -63,10 +63,10 @@ class UrlRewritingTest extends FileTestBase {
}
/**
* Test the generating of rewritten public created file URLs.
* Tests the rewriting of public managed file URLs by hook_file_url_alter().
*/
function testPublicCreatedFileURL() {
// Test generating an URL to a created file.
function testPublicManagedFileURL() {
// Test generating an URL to a managed file.
// Test alteration of file URLs to use a CDN.
\Drupal::state()->set('file_test.hook_file_url_alter', 'cdn');
@ -87,4 +87,24 @@ class UrlRewritingTest extends FileTestBase {
$url = file_create_url($uri);
$this->assertEqual('/' . base_path() . '/' . $public_directory_path . '/' . drupal_basename($uri), $url, 'Correctly generated a protocol-relative URL for a created file.');
}
/**
* Test file_url_transform_relative().
*/
function testRelativeFileURL() {
// Disable file_test.module's hook_file_url_alter() implementation.
\Drupal::state()->set('file_test.hook_file_url_alter', NULL);
// Shipped file.
$filepath = 'core/assets/vendor/jquery/jquery.js';
$url = file_create_url($filepath);
$this->assertIdentical(base_path() . $filepath, file_url_transform_relative($url));
// Managed file.
$uri = $this->createUri();
$url = file_create_url($uri);
$public_directory_path = file_stream_wrapper_get_instance_by_scheme('public')->getDirectoryPath();
$this->assertIdentical(base_path() . $public_directory_path . '/' . rawurlencode(drupal_basename($uri)), file_url_transform_relative($url));
}
}