url_replace_filter.module 4.1 KB

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