123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- /**
- * Implementation of hook_filter().
- */
- function url_replace_filter_filter($op, $delta = 0, $format = -1, $text = '') {
- switch ($op) {
- case 'list':
- return array(0 => t('URL Replace Filter'));
- case 'description':
- return t('Allows administrators to replace the base URL in <img> and <a> elements.');
- case 'settings':
- return _url_replace_filter_settings($format);
- case 'process':
- $text = _url_replace_filter_process($text, $format);
- return $text;
- default:
- return $text;
- }
- }
- /**
- * Filter the given text.
- */
- function _url_replace_filter_process($text, $format) {
- $settings = _url_replace_filter_get_settings($format);
- foreach ($settings as $index => $setting) {
- if ($setting['original']) {
- $pattern = '!((<a\s[^>]*href)|(<img\s[^>]*src))\s*=\s*"'. preg_quote($setting['original']) .'!iU';
- if (preg_match_all($pattern, $text, $matches)) {
- $replacement = str_replace('%baseurl', rtrim(base_path(), '/'), $setting['replacement']);
- foreach ($matches[0] as $key => $match) {
- $text = str_replace($match, $matches[1][$key] .'="'. $replacement, $text);
- }
- }
- }
- }
- return $text;
- }
- /**
- * Settings for the HTML filter.
- */
- function _url_replace_filter_settings($format) {
- $form['#tree'] = TRUE;
- $form['url_replace_filter_'. $format] = array(
- '#type' => 'fieldset',
- '#title' => t('URL Replace Filter'),
- '#collapsible' => TRUE,
- '#collapsed' => FALSE,
- '#theme' => 'url_replace_filter_settings_form',
- );
- $settings = _url_replace_filter_get_settings($format);
- foreach ($settings as $index => $setting) {
- _url_replace_filter_row_form($form, $format, $index, $setting['original'], $setting['replacement']);
- if (!$setting['original']) {
- $empty++;
- }
- }
- // Append some empty fields
- while ($empty < 3) {
- $index++;
- $empty++;
- _url_replace_filter_row_form($form, $format, $index, '', '');
- }
- return $form;
- }
- function _url_replace_filter_row_form(&$form, $format, $index, $original, $replacement) {
- $form['url_replace_filter_'. $format][$index]['original'] = array(
- '#type' => 'textfield',
- '#size' => 50,
- '#default_value' => $original,
- );
- $form['url_replace_filter_'. $format][$index]['replacement'] = array(
- '#type' => 'textfield',
- '#size' => 50,
- '#default_value' => $replacement,
- );
- }
- function _url_replace_filter_get_settings($format) {
- return variable_get('url_replace_filter_'. $format, array(0 => array('original' => '', 'replacement' => '')));
- }
- function theme_url_replace_filter_settings_form(&$form) {
- $header = array(t('Original'), t('Replacement'));
- foreach (element_children($form) as $index) {
- $row = array();
- foreach (element_children($form[$index]) as $key) {
- $row[] = drupal_render($form[$index][$key]);
- }
- $rows[] = $row;
- }
- $output .= '<p>'. t('This filter allows you to replace the base URL in <img> and <a> elements.') .'</p>';
- $output .= theme('table', $header, $rows);
- $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>');
- $output .= drupal_render($form);
- return $output;
- }
|