url_replace_filter.module 3.7 KB

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