| 1 |
<?php
|
| 2 |
// $Id: url_replace_filter.module,v 1.1.2.2 2007/08/14 03:02:53 davidlesieur Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_filter().
|
| 6 |
*/
|
| 7 |
function url_replace_filter_filter($op, $delta = 0, $format = -1, $text = '') {
|
| 8 |
switch ($op) {
|
| 9 |
case 'list':
|
| 10 |
return array(0 => t('URL Replace Filter'));
|
| 11 |
|
| 12 |
case 'description':
|
| 13 |
return t('Allows administrators to replace the base URL in <img> and <a> elements.');
|
| 14 |
|
| 15 |
case 'settings':
|
| 16 |
return _url_replace_filter_settings($format);
|
| 17 |
|
| 18 |
case 'process':
|
| 19 |
$text = _url_replace_filter_process($text, $format);
|
| 20 |
return $text;
|
| 21 |
|
| 22 |
default:
|
| 23 |
return $text;
|
| 24 |
}
|
| 25 |
}
|
| 26 |
|
| 27 |
/**
|
| 28 |
* Filter the given text.
|
| 29 |
*/
|
| 30 |
function _url_replace_filter_process($text, $format) {
|
| 31 |
$settings = _url_replace_filter_get_settings($format);
|
| 32 |
foreach ($settings as $index => $setting) {
|
| 33 |
if ($setting['original']) {
|
| 34 |
$pattern = '!((<a\s[^>]*href)|(<img\s[^>]*src))\s*=\s*"'. preg_quote($setting['original']) .'!iU';
|
| 35 |
if (preg_match_all($pattern, $text, $matches)) {
|
| 36 |
$replacement = str_replace('%baseurl', rtrim(base_path(), '/'), $setting['replacement']);
|
| 37 |
foreach ($matches[0] as $key => $match) {
|
| 38 |
$text = str_replace($match, $matches[1][$key] .'="'. $replacement, $text);
|
| 39 |
}
|
| 40 |
}
|
| 41 |
}
|
| 42 |
}
|
| 43 |
return $text;
|
| 44 |
}
|
| 45 |
|
| 46 |
/**
|
| 47 |
* Settings for the HTML filter.
|
| 48 |
*/
|
| 49 |
function _url_replace_filter_settings($format) {
|
| 50 |
$form['#tree'] = TRUE;
|
| 51 |
$form['url_replace_filter_'. $format] = array(
|
| 52 |
'#type' => 'fieldset',
|
| 53 |
'#title' => t('URL Replace Filter'),
|
| 54 |
'#collapsible' => TRUE,
|
| 55 |
'#collapsed' => FALSE,
|
| 56 |
'#theme' => 'url_replace_filter_settings_form',
|
| 57 |
);
|
| 58 |
$settings = _url_replace_filter_get_settings($format);
|
| 59 |
foreach ($settings as $index => $setting) {
|
| 60 |
_url_replace_filter_row_form($form, $format, $index, $setting['original'], $setting['replacement']);
|
| 61 |
if (!$setting['original']) {
|
| 62 |
$empty++;
|
| 63 |
}
|
| 64 |
}
|
| 65 |
// Append some empty fields
|
| 66 |
while ($empty < 3) {
|
| 67 |
$index++;
|
| 68 |
$empty++;
|
| 69 |
_url_replace_filter_row_form($form, $format, $index, '', '');
|
| 70 |
}
|
| 71 |
return $form;
|
| 72 |
}
|
| 73 |
|
| 74 |
function _url_replace_filter_row_form(&$form, $format, $index, $original, $replacement) {
|
| 75 |
$form['url_replace_filter_'. $format][$index]['original'] = array(
|
| 76 |
'#type' => 'textfield',
|
| 77 |
'#size' => 50,
|
| 78 |
'#default_value' => $original,
|
| 79 |
);
|
| 80 |
$form['url_replace_filter_'. $format][$index]['replacement'] = array(
|
| 81 |
'#type' => 'textfield',
|
| 82 |
'#size' => 50,
|
| 83 |
'#default_value' => $replacement,
|
| 84 |
);
|
| 85 |
}
|
| 86 |
|
| 87 |
function _url_replace_filter_get_settings($format) {
|
| 88 |
return variable_get('url_replace_filter_'. $format, array(0 => array('original' => '', 'replacement' => '')));
|
| 89 |
}
|
| 90 |
|
| 91 |
function theme_url_replace_filter_settings_form(&$form) {
|
| 92 |
$header = array(t('Original'), t('Replacement'));
|
| 93 |
foreach (element_children($form) as $index) {
|
| 94 |
$row = array();
|
| 95 |
foreach (element_children($form[$index]) as $key) {
|
| 96 |
$row[] = drupal_render($form[$index][$key]);
|
| 97 |
}
|
| 98 |
$rows[] = $row;
|
| 99 |
}
|
| 100 |
$output .= '<p>'. t('This filter allows you to replace the base URL in <img> and <a> elements.') .'</p>';
|
| 101 |
$output .= theme('table', $header, $rows);
|
| 102 |
$output .= t('<p>Enter original base URLs and their replacements. Matching is case-insensitive. You may use %baseurl in the replacement string to insert your site\'s base URL (without the trailing slash).</p><p><strong>Warning</strong>: To avoid unexpected results, you must include trailing slashes in both the original and replacement strings.</p><p><strong>Warning</strong>: Replacements are executed in the order you give them. Place the most specific URLs first. For example, <em>http://example.com/somepath/</em> should be replaced before <em>http://example.com/</em>.</p><p>If you need more replacement rules, more fields will be added after saving the settings.</p>');
|
| 103 |
$output .= drupal_render($form);
|
| 104 |
return $output;
|
| 105 |
}
|