| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* img_relocator: a Drupal filter to automatically transform the SRC attribute
|
| 4 |
* of IMG elements to absolute paths, built using the current files directory.
|
| 5 |
* Additional attributes are left unchanged
|
| 6 |
*
|
| 7 |
* Typical use:
|
| 8 |
* <img src="image.png" /> becomes:
|
| 9 |
* <img src="http://www.example.com/path/to/drupal/files/image.png" />
|
| 10 |
*
|
| 11 |
* @copyright 2006-2007 Frederic G. MARAND
|
| 12 |
* @license CeCILL 2.0: http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
|
| 13 |
* @author http://blog.riff.org
|
| 14 |
* @version CVS: $Id$
|
| 15 |
*/
|
| 16 |
|
| 17 |
/**
|
| 18 |
* @param string $section Help section
|
| 19 |
* @return string HTML
|
| 20 |
*/
|
| 21 |
function img_relocator_help($section)
|
| 22 |
{
|
| 23 |
$ret = '';
|
| 24 |
switch ($section) {
|
| 25 |
case 'admin/modules#name':
|
| 26 |
$ret = 'img_relocator';
|
| 27 |
break;
|
| 28 |
case 'admin/help#img_relocator':
|
| 29 |
$ret = t('<p>img_relocator transforms relative paths in IMG elements to absolute paths.</p>');
|
| 30 |
break;
|
| 31 |
case 'admin/modules#description':
|
| 32 |
$ret = t('<p>img_relocator transforms relative paths in IMG elements to absolute paths.</p>');
|
| 33 |
break;
|
| 34 |
default: // ignore, this hook is called all over the place in 4.7b1
|
| 35 |
}
|
| 36 |
return $ret;
|
| 37 |
}
|
| 38 |
|
| 39 |
/**
|
| 40 |
* hook_filter_tips
|
| 41 |
*
|
| 42 |
* @param int $delta
|
| 43 |
* @param int $format Which format we are providing tips for
|
| 44 |
* @param boolean $long
|
| 45 |
* @return string HTML
|
| 46 |
*/
|
| 47 |
function img_relocator_filter_tips($delta, $format, $long = false)
|
| 48 |
{
|
| 49 |
if ($long)
|
| 50 |
{
|
| 51 |
$ret = t('Use <img src="image.png" /> to output <img src="http://www.example.com/path/to/drupal/image.png" />');
|
| 52 |
}
|
| 53 |
else
|
| 54 |
{
|
| 55 |
$ret = t('You may link to images with absolute or relative paths and they will be output as absolute paths.');
|
| 56 |
}
|
| 57 |
return $ret;
|
| 58 |
}
|
| 59 |
|
| 60 |
|
| 61 |
function img_relocator_filter($op, $delta = 0, $format = -1, $text = '')
|
| 62 |
{
|
| 63 |
switch ($op)
|
| 64 |
{
|
| 65 |
case 'list':
|
| 66 |
return array(0 => t('IMG relocator filter'));
|
| 67 |
|
| 68 |
case 'description':
|
| 69 |
return t('Allows users to use IMG elements with a path relative to the files directory and have them relocated to the proper absolute path. ');
|
| 70 |
|
| 71 |
case 'prepare':
|
| 72 |
// Note: we use the bytes 0xFE and 0xFF to replace < > during the filtering process.
|
| 73 |
// These bytes are not valid in UTF-8 data and thus least likely to cause problems.
|
| 74 |
$text = preg_replace('@<img(.+?)/>@s', "\xFEimg\\1/\xFF", $text);
|
| 75 |
return $text;
|
| 76 |
|
| 77 |
case "process":
|
| 78 |
$text = preg_replace('@\xFEimg(.+?)/\xFF@se', "_img_relocator_filter_process('$1')", $text);
|
| 79 |
return $text;
|
| 80 |
|
| 81 |
default:
|
| 82 |
return $text;
|
| 83 |
}
|
| 84 |
}
|
| 85 |
|
| 86 |
function _img_relocator_filter_process($image)
|
| 87 |
{
|
| 88 |
global $base_url;
|
| 89 |
|
| 90 |
$ret = '<img ';
|
| 91 |
$attributes = explode(' ', $image);
|
| 92 |
foreach ($attributes as $attribute)
|
| 93 |
{
|
| 94 |
if (!strlen($attribute))
|
| 95 |
{
|
| 96 |
continue;
|
| 97 |
}
|
| 98 |
if (strpos(strtolower($attribute), 'src') === false)
|
| 99 |
{
|
| 100 |
$ret .= " " . stripslashes($attribute);
|
| 101 |
}
|
| 102 |
else
|
| 103 |
{
|
| 104 |
$ret .= " " . _img_relocator_relocate($attribute);
|
| 105 |
}
|
| 106 |
}
|
| 107 |
$ret .= " />\n";
|
| 108 |
return $ret;
|
| 109 |
}
|
| 110 |
|
| 111 |
/**
|
| 112 |
* Convert an attribute clause from relative to clean and absolute
|
| 113 |
* Normally used with src="someurl", but accepts other forms,
|
| 114 |
*
|
| 115 |
* @param string $attribute of the form src="someurl"
|
| 116 |
*/
|
| 117 |
function _img_relocator_relocate($attribute)
|
| 118 |
{
|
| 119 |
global $base_url;
|
| 120 |
|
| 121 |
$file_path = variable_get('file_directory_path', 'files');
|
| 122 |
|
| 123 |
list($name, $value) = explode('=', $attribute, 2);
|
| 124 |
$value = stripslashes($value); // remove \ before "
|
| 125 |
if ($value[0] == '"' || $value[0] == "'")
|
| 126 |
{
|
| 127 |
$value = trim($value, '"' . "'");
|
| 128 |
}
|
| 129 |
// We now have the raw attribute value.
|
| 130 |
$arPath = parse_url($value);
|
| 131 |
if ((!array_key_exists('scheme', $arPath))
|
| 132 |
&& ($value[0] != '/')) // Then this is a relative url
|
| 133 |
{
|
| 134 |
$value = "$base_url/$file_path/$value";
|
| 135 |
}
|
| 136 |
$ret = 'src="' . $value . '"';
|
| 137 |
return $ret;
|
| 138 |
}
|