url_replace_filter.module 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. $form['#submit'][] = '_url_replace_filter_settings_form_submit';
  67. $settings = _url_replace_filter_get_settings($format);
  68. $index = 0;
  69. foreach ($settings as $index => $setting) {
  70. _url_replace_filter_row_form($form, $format, $index, $setting['original'], $setting['replacement']);
  71. if (!$setting['original']) {
  72. $empty++;
  73. }
  74. }
  75. // Append some empty fields
  76. while ($empty < 3) {
  77. $index++;
  78. $empty++;
  79. _url_replace_filter_row_form($form, $format, $index, '', '');
  80. }
  81. return $form;
  82. }
  83. /**
  84. * Submit handler for _url_replace_filter_settings() form.
  85. *
  86. * - Remove useless empty settings to keep variable as small as possible.
  87. */
  88. function _url_replace_filter_settings_form_submit($form, &$form_state) {
  89. $format = $form_state['values']['format'];
  90. $format_id = 'url_replace_filter_' . $format;
  91. if (!isset($form_state['values'][$format_id])) {
  92. return;
  93. }
  94. // Remove empty sets.
  95. foreach ($form_state['values'][$format_id] as $key => $value) {
  96. if (empty($value['original']) && empty($value['replacement'])) {
  97. unset($form_state['values'][$format_id][$key]);
  98. }
  99. }
  100. if (empty($form_state['values'][$format_id])) {
  101. drupal_set_message(t('URL Replace filter configuration is empty : you could <a href="!edit">remove it</a> from this input format.', array(
  102. '!edit' => check_url(url('admin/settings/filters/1')),
  103. )), 'warning');
  104. }
  105. }
  106. function _url_replace_filter_row_form(&$form, $format, $index, $original, $replacement) {
  107. $form['url_replace_filter_'. $format][$index]['original'] = array(
  108. '#type' => 'textfield',
  109. '#size' => 50,
  110. '#default_value' => $original,
  111. );
  112. $form['url_replace_filter_'. $format][$index]['replacement'] = array(
  113. '#type' => 'textfield',
  114. '#size' => 50,
  115. '#default_value' => $replacement,
  116. );
  117. }
  118. function _url_replace_filter_get_settings($format) {
  119. return variable_get('url_replace_filter_'. $format, array(
  120. 0 => array('original' => '', 'replacement' => ''),
  121. ));
  122. }
  123. /**
  124. * Return the list of input formats containing the URL Replace filter.
  125. *
  126. * @return array
  127. * The list of input formats, keyed by format_id
  128. */
  129. function _url_replace_filter_get_formats() {
  130. $filter_id = 'url_replace_filter/0';
  131. $formats = filter_formats();
  132. $ret = array();
  133. foreach ($formats as $format_id => $format) {
  134. $format_filters = filter_list_format($format_id);
  135. $format->{$filter_id} = isset($format_filters[$filter_id]);
  136. $ret[$format_id] = $format;
  137. }
  138. return $ret;
  139. }
  140. /**
  141. * Theme for the filter settings form.
  142. *
  143. * @param array $form
  144. * A form array
  145. *
  146. * @return string
  147. */
  148. function theme_url_replace_filter_settings_form($form) {
  149. $header = array(t('Original'), t('Replacement'));
  150. foreach (element_children($form) as $index) {
  151. $row = array();
  152. foreach (element_children($form[$index]) as $key) {
  153. $row[] = drupal_render($form[$index][$key]);
  154. }
  155. $rows[] = $row;
  156. }
  157. $output = '<p>'. t('This filter allows you to replace the base URL in &lt;img&gt; and &lt;a&gt; elements.') .'</p>';
  158. $output .= theme('table', $header, $rows);
  159. $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>');
  160. $output .= drupal_render($form);
  161. return $output;
  162. }
  163. /**
  164. * Implementation of hook_form_FORM_ID_alter().
  165. *
  166. * Insert configuration links in modules page.
  167. */
  168. function url_replace_filter_form_system_modules_alter(array &$form) {
  169. // No way to know what alternate themes do with this form, so play it safe.
  170. if (isset($form['#theme']) && $form['#theme'] != 'confirm_form' && $form['#theme'] != 'module_filter_system_modules') {
  171. return;
  172. }
  173. // Users without filter access can not access the module settings.
  174. if (!user_access('administer filters')) {
  175. return;
  176. }
  177. $machine_name = 'url_replace_filter';
  178. $human_name = $form['name'][$machine_name]['#value'];
  179. // Description might have been altered, so do not rely on its default value.
  180. $description = isset($form['description'][$machine_name]['#value'])
  181. ? $form['description'][$machine_name]['#value']
  182. : $human_name;
  183. $format_ids = _url_replace_filter_get_formats();
  184. $links_array = array();
  185. $filter_id = "$machine_name/0";
  186. foreach ($format_ids as $format_id => $format) {
  187. if ($format->{$filter_id}) {
  188. $text = '!link (present)';
  189. $link = l($format->name, "admin/settings/filters/$format_id/configure");
  190. }
  191. else {
  192. $text = '!link (absent)';
  193. $link = l($format->name, "admin/settings/filters/$format_id");
  194. }
  195. $links_array[$format_id] = t($text, array('!link' => $link));
  196. }
  197. $links = implode(', ', $links_array);
  198. $form['description'][$machine_name]['#value'] = $description .' '.
  199. t('(Configure in !urls)', array('!urls' => $links));
  200. }
  201. /**
  202. * Implementation of hook_theme().
  203. *
  204. * @return array
  205. */
  206. function url_replace_filter_theme() {
  207. $ret = array(
  208. 'url_replace_filter_settings_form' => array(
  209. 'arguments' => array('form' => NULL),
  210. ),
  211. );
  212. return $ret;
  213. }