url_replace_filter.module 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. * Implements hook_filter().
  13. */
  14. function url_replace_filter_filter($operation, $delta = 0, $format = -1, $text = '') {
  15. if (intval($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. /**
  107. * Helper to build rows in the table built by _url_replace_filter_settings().
  108. *
  109. * @param array $form
  110. * The form array.
  111. * @param int $format
  112. * The format index.
  113. * @param int $index
  114. * The format index.
  115. * @param string $original
  116. * The original value to be replaced.
  117. * @param string $replacement
  118. * The replacement for the original value.
  119. */
  120. function _url_replace_filter_row_form(array &$form, $format, $index, $original, $replacement) {
  121. $form['url_replace_filter_' . $format][$index]['original'] = array(
  122. '#type' => 'textfield',
  123. '#size' => 50,
  124. '#default_value' => $original,
  125. );
  126. $form['url_replace_filter_' . $format][$index]['replacement'] = array(
  127. '#type' => 'textfield',
  128. '#size' => 50,
  129. '#default_value' => $replacement,
  130. );
  131. }
  132. /**
  133. * Helper for _url_replace_filter_process().
  134. *
  135. * @param int $format
  136. * The id of the format.
  137. *
  138. * @return mixed
  139. * The replacement information for the format.
  140. */
  141. function _url_replace_filter_get_settings($format) {
  142. return variable_get('url_replace_filter_' . $format, array(
  143. 0 => array('original' => '', 'replacement' => ''),
  144. ));
  145. }
  146. /**
  147. * Return the list of input formats containing the URL Replace filter.
  148. *
  149. * @return array
  150. * The list of input formats, keyed by format_id.
  151. */
  152. function _url_replace_filter_get_formats() {
  153. $filter_id = 'url_replace_filter/0';
  154. $formats = filter_formats();
  155. $ret = array();
  156. foreach ($formats as $format_id => $format) {
  157. $format_filters = filter_list_format($format_id);
  158. $format->{$filter_id} = isset($format_filters[$filter_id]);
  159. $ret[$format_id] = $format;
  160. }
  161. return $ret;
  162. }
  163. /**
  164. * Theme for the filter settings form.
  165. *
  166. * @param array $form
  167. * A form array.
  168. *
  169. * @return string
  170. * The themed form.
  171. */
  172. function theme_url_replace_filter_settings_form(array $form = array()) {
  173. $header = array(t('Original'), t('Replacement'));
  174. foreach (element_children($form) as $index) {
  175. $row = array();
  176. foreach (element_children($form[$index]) as $key) {
  177. $row[] = drupal_render($form[$index][$key]);
  178. }
  179. $rows[] = $row;
  180. }
  181. $output = '<p>' . t('This filter allows you to replace the base URL in &lt;img&gt; and &lt;a&gt; elements.') . '</p>';
  182. $output .= theme('table', $header, $rows);
  183. $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>");
  184. $output .= drupal_render($form);
  185. return $output;
  186. }
  187. /**
  188. * Implements hook_form_FORM_ID_alter().
  189. *
  190. * Insert configuration links in modules page.
  191. */
  192. function url_replace_filter_form_system_modules_alter(array &$form) {
  193. // No way to know what alternate themes do with this form, so play it safe.
  194. if (isset($form['#theme']) && $form['#theme'] != 'confirm_form' && $form['#theme'] != 'module_filter_system_modules') {
  195. return;
  196. }
  197. // Users without filter access can not access the module settings.
  198. if (!user_access('administer filters')) {
  199. return;
  200. }
  201. $machine_name = 'url_replace_filter';
  202. $human_name = $form['name'][$machine_name]['#value'];
  203. // Description might have been altered, so do not rely on its default value.
  204. $description = isset($form['description'][$machine_name]['#value'])
  205. ? $form['description'][$machine_name]['#value']
  206. : $human_name;
  207. $format_ids = _url_replace_filter_get_formats();
  208. $links_array = array();
  209. $filter_id = "$machine_name/0";
  210. foreach ($format_ids as $format_id => $format) {
  211. if ($format->{$filter_id}) {
  212. $text = '!link (present)';
  213. $link = l($format->name, "admin/settings/filters/$format_id/configure");
  214. }
  215. else {
  216. $text = '!link (absent)';
  217. $link = l($format->name, "admin/settings/filters/$format_id");
  218. }
  219. $links_array[$format_id] = t($text, array('!link' => $link));
  220. }
  221. $links = implode(', ', $links_array);
  222. $form['description'][$machine_name]['#value'] = $description . ' ' . t('(Configure in !urls)', array(
  223. '!urls' => $links,
  224. ));
  225. }
  226. /**
  227. * Implements hook_theme().
  228. */
  229. function url_replace_filter_theme() {
  230. $ret = array(
  231. 'url_replace_filter_settings_form' => array(
  232. 'arguments' => array('form' => NULL),
  233. ),
  234. );
  235. return $ret;
  236. }