url_replace_filter.module 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. // $Id$
  3. /**
  4. * Implementation of hook_filter().
  5. */
  6. function url_replace_filter_filter($op, $delta = 0, $format = -1, $text = '') {
  7. switch ($op) {
  8. case 'list':
  9. return array(0 => t('URL Replace Filter'));
  10. case 'description':
  11. return t('Allows administrators to replace the base URL in &lt;img&gt; and &lt;a&gt; elements.');
  12. case 'settings':
  13. return _url_replace_filter_settings($format);
  14. case 'process':
  15. $text = _url_replace_filter_process($text, $format);
  16. return $text;
  17. default:
  18. return $text;
  19. }
  20. }
  21. /**
  22. * Filter the given text.
  23. */
  24. function _url_replace_filter_process($text, $format) {
  25. $settings = _url_replace_filter_get_settings($format);
  26. foreach ($settings as $index => $setting) {
  27. if ($setting['original']) {
  28. $pattern = '!((<a\s[^>]*href)|(<img\s[^>]*src))\s*=\s*"'. preg_quote($setting['original']) .'!iU';
  29. if (preg_match_all($pattern, $text, $matches)) {
  30. $replacement = str_replace('%baseurl', rtrim(base_path(), '/'), $setting['replacement']);
  31. foreach ($matches[0] as $key => $match) {
  32. $text = str_replace($match, $matches[1][$key] .'="'. $replacement, $text);
  33. }
  34. }
  35. }
  36. }
  37. return $text;
  38. }
  39. /**
  40. * Settings for the HTML filter.
  41. */
  42. function _url_replace_filter_settings($format) {
  43. $form['#tree'] = TRUE;
  44. $form['url_replace_filter_'. $format] = array(
  45. '#type' => 'fieldset',
  46. '#title' => t('URL Replace Filter'),
  47. '#collapsible' => TRUE,
  48. '#collapsed' => FALSE,
  49. '#theme' => 'url_replace_filter_settings_form',
  50. );
  51. $settings = _url_replace_filter_get_settings($format);
  52. foreach ($settings as $index => $setting) {
  53. _url_replace_filter_row_form($form, $format, $index, $setting['original'], $setting['replacement']);
  54. if (!$setting['original']) {
  55. $empty++;
  56. }
  57. }
  58. // Append some empty fields
  59. while ($empty < 3) {
  60. $index++;
  61. $empty++;
  62. _url_replace_filter_row_form($form, $format, $index, '', '');
  63. }
  64. return $form;
  65. }
  66. function _url_replace_filter_row_form(&$form, $format, $index, $original, $replacement) {
  67. $form['url_replace_filter_'. $format][$index]['original'] = array(
  68. '#type' => 'textfield',
  69. '#size' => 50,
  70. '#default_value' => $original,
  71. );
  72. $form['url_replace_filter_'. $format][$index]['replacement'] = array(
  73. '#type' => 'textfield',
  74. '#size' => 50,
  75. '#default_value' => $replacement,
  76. );
  77. }
  78. function _url_replace_filter_get_settings($format) {
  79. return variable_get('url_replace_filter_'. $format, array(0 => array('original' => '', 'replacement' => '')));
  80. }
  81. function theme_url_replace_filter_settings_form(&$form) {
  82. $header = array(t('Original'), t('Replacement'));
  83. foreach (element_children($form) as $index) {
  84. $row = array();
  85. foreach (element_children($form[$index]) as $key) {
  86. $row[] = drupal_render($form[$index][$key]);
  87. }
  88. $rows[] = $row;
  89. }
  90. $output .= '<p>'. t('This filter allows you to replace the base URL in &lt;img&gt; and &lt;a&gt; elements.') .'</p>';
  91. $output .= theme('table', $header, $rows);
  92. $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>');
  93. $output .= drupal_render($form);
  94. return $output;
  95. }