url_replace_filter.module 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * @file
  4. * URL replace filter module.
  5. *
  6. * @author David Lesieur
  7. * @author Frédéric G. MARAND <fgm@osinet.fr>
  8. *
  9. * @license Licensed under the General Public License version 2.0 or later.
  10. */
  11. /**
  12. * Implementation of hook_filter().
  13. */
  14. function url_replace_filter_filter($operation, $delta = 0, $format = -1, $text = '') {
  15. if ($delta !== 0) {
  16. watchdog('url_replace_filter', '@function invoked with non-zero delta: @delta. This should never happen.', array(
  17. '@function' => __FUNCTION__,
  18. '@delta' => $delta,
  19. ), WATCHDOG_ERROR);
  20. }
  21. switch ($operation) {
  22. case 'list':
  23. return array(0 => t('URL Replace Filter'));
  24. case 'description':
  25. return t('Allows administrators to replace the base URL in &lt;img&gt; and &lt;a&gt; elements.');
  26. case 'settings':
  27. return _url_replace_filter_settings($format);
  28. case 'process':
  29. $text = _url_replace_filter_process($text, $format);
  30. return $text;
  31. default:
  32. return $text;
  33. }
  34. }
  35. /**
  36. * Filter the given text.
  37. */
  38. function _url_replace_filter_process($text, $format) {
  39. $settings = _url_replace_filter_get_settings($format);
  40. foreach ($settings as $setting) {
  41. if ($setting['original']) {
  42. $pattern = '!((<a\s[^>]*href)|(<img\s[^>]*src))\s*=\s*"'. preg_quote($setting['original']) .'!iU';
  43. if (preg_match_all($pattern, $text, $matches)) {
  44. $replacement = str_replace('%baseurl', rtrim(base_path(), '/'), $setting['replacement']);
  45. foreach ($matches[0] as $key => $match) {
  46. $text = str_replace($match, $matches[1][$key] .'="'. $replacement, $text);
  47. }
  48. }
  49. }
  50. }
  51. return $text;
  52. }
  53. /**
  54. * Settings for the HTML filter.
  55. */
  56. function _url_replace_filter_settings($format) {
  57. $empty = 0;
  58. $form['url_replace_filter_'. $format] = array(
  59. '#type' => 'fieldset',
  60. '#title' => t('URL Replace Filter'),
  61. '#collapsible' => TRUE,
  62. '#collapsed' => FALSE,
  63. '#theme' => 'url_replace_filter_settings_form',
  64. '#tree' => TRUE,
  65. );
  66. $settings = _url_replace_filter_get_settings($format);
  67. foreach ($settings as $index => $setting) {
  68. _url_replace_filter_row_form($form, $format, $index, $setting['original'], $setting['replacement']);
  69. if (!$setting['original']) {
  70. $empty++;
  71. }
  72. }
  73. // Append some empty fields
  74. while ($empty < 3) {
  75. $index++;
  76. $empty++;
  77. _url_replace_filter_row_form($form, $format, $index, '', '');
  78. }
  79. return $form;
  80. }
  81. function _url_replace_filter_row_form(&$form, $format, $index, $original, $replacement) {
  82. $form['url_replace_filter_'. $format][$index]['original'] = array(
  83. '#type' => 'textfield',
  84. '#size' => 50,
  85. '#default_value' => $original,
  86. );
  87. $form['url_replace_filter_'. $format][$index]['replacement'] = array(
  88. '#type' => 'textfield',
  89. '#size' => 50,
  90. '#default_value' => $replacement,
  91. );
  92. }
  93. function _url_replace_filter_get_settings($format) {
  94. return variable_get('url_replace_filter_'. $format, array(0 => array('original' => '', 'replacement' => '')));
  95. }
  96. /**
  97. * Return the list of input formats containing the URL Replace filter.
  98. *
  99. * @return array
  100. * The list of input formats, keyed by format_id
  101. */
  102. function _url_replace_filter_get_formats() {
  103. $filter_id = 'url_replace_filter/0';
  104. $formats = filter_formats();
  105. $ret = array();
  106. foreach ($formats as $format_id => $format) {
  107. $format_filters = filter_list_format($format_id);
  108. $format->{$filter_id} = isset($format_filters[$filter_id]);
  109. $ret[$format_id] = $format;
  110. }
  111. return $ret;
  112. }
  113. /**
  114. * Theme for the filter settings form.
  115. *
  116. * @param array $form
  117. * A form array
  118. *
  119. * @return string
  120. */
  121. function theme_url_replace_filter_settings_form($form) {
  122. $header = array(t('Original'), t('Replacement'));
  123. foreach (element_children($form) as $index) {
  124. $row = array();
  125. foreach (element_children($form[$index]) as $key) {
  126. $row[] = drupal_render($form[$index][$key]);
  127. }
  128. $rows[] = $row;
  129. }
  130. $output = '<p>'. t('This filter allows you to replace the base URL in &lt;img&gt; and &lt;a&gt; elements.') .'</p>';
  131. $output .= theme('table', $header, $rows);
  132. $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>');
  133. $output .= drupal_render($form);
  134. return $output;
  135. }
  136. /**
  137. * Implementation of hook_form_FORM_ID_alter().
  138. *
  139. * Insert configuration links in modules page.
  140. */
  141. function url_replace_filter_form_system_modules_alter(array &$form) {
  142. // No way to know what alternate themes do with this form, so play it safe.
  143. if (isset($form['#theme']) && $form['#theme'] != 'confirm_form' && $form['#theme'] != 'module_filter_system_modules') {
  144. return;
  145. }
  146. // Users without filter access can not access the module settings.
  147. if (!user_access('administer filters')) {
  148. return;
  149. }
  150. $machine_name = 'url_replace_filter';
  151. $human_name = $form['name'][$machine_name]['#value'];
  152. // Description might have been altered, so do not rely on its default value.
  153. $description = isset($form['description'][$machine_name]['#value'])
  154. ? $form['description'][$machine_name]['#value']
  155. : $human_name;
  156. $format_ids = _url_replace_filter_get_formats();
  157. $links_array = array();
  158. $filter_id = "$machine_name/0";
  159. foreach ($format_ids as $format_id => $format) {
  160. if ($format->{$filter_id}) {
  161. $text = '!link (present)';
  162. $link = l($format->name, "admin/settings/filters/$format_id/configure");
  163. }
  164. else {
  165. $text = '!link (absent)';
  166. $link = l($format->name, "admin/settings/filters/$format_id");
  167. }
  168. $links_array[$format_id] = t($text, array('!link' => $link));
  169. }
  170. $links = implode(', ', $links_array);
  171. $form['description'][$machine_name]['#value'] = $description
  172. . t(' (Configure in !urls)', array('!urls' => $links));
  173. }
  174. /**
  175. * Implementation of hook_theme().
  176. *
  177. * @return array
  178. */
  179. function url_replace_filter_theme() {
  180. $ret = array(
  181. 'url_replace_filter_settings_form' => array(
  182. 'arguments' => array('form' => NULL),
  183. ),
  184. );
  185. return $ret;
  186. }