123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- /**
- * @file
- * URL replace filter module.
- *
- * @author David Lesieur
- * @author Frédéric G. MARAND <fgm@osinet.fr>
- *
- * @license Licensed under the General Public License version 2.0 or later.
- */
- /**
- * Implementation of hook_filter().
- */
- function url_replace_filter_filter($operation, $delta = 0, $format = -1, $text = '') {
- if ($delta !== 0) {
- watchdog('url_replace_filter', '@function invoked with non-zero delta: @delta. This should never happen.', array(
- '@function' => __FUNCTION__,
- '@delta' => $delta,
- ), WATCHDOG_ERROR);
- }
- switch ($operation) {
- 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 $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) {
- $empty = 0;
- $form['url_replace_filter_'. $format] = array(
- '#type' => 'fieldset',
- '#title' => t('URL Replace Filter'),
- '#collapsible' => TRUE,
- '#collapsed' => FALSE,
- '#theme' => 'url_replace_filter_settings_form',
- '#tree' => TRUE,
- );
- $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' => '')));
- }
- /**
- * Return the list of input formats containing the URL Replace filter.
- *
- * @return array
- * The list of input formats, keyed by format_id
- */
- function _url_replace_filter_get_formats() {
- $filter_id = 'url_replace_filter/0';
- $formats = filter_formats();
- $ret = array();
- foreach ($formats as $format_id => $format) {
- $format_filters = filter_list_format($format_id);
- $format->{$filter_id} = isset($format_filters[$filter_id]);
- $ret[$format_id] = $format;
- }
- return $ret;
- }
- /**
- * Theme for the filter settings form.
- *
- * @param array $form
- * A form array
- *
- * @return string
- */
- 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;
- }
- /**
- * Implementation of hook_form_FORM_ID_alter().
- *
- * Insert configuration links in modules page.
- */
- function url_replace_filter_form_system_modules_alter(array &$form) {
- // No way to know what alternate themes do with this form, so play it safe.
- if (isset($form['#theme']) && $form['#theme'] != 'confirm_form' && $form['#theme'] != 'module_filter_system_modules') {
- return;
- }
- // Users without filter access can not access the module settings.
- if (!user_access('administer filters')) {
- return;
- }
- $machine_name = 'url_replace_filter';
- $human_name = $form['name'][$machine_name]['#value'];
- // Description might have been altered, so do not rely on its default value.
- $description = isset($form['description'][$machine_name]['#value'])
- ? $form['description'][$machine_name]['#value']
- : $human_name;
- $format_ids = _url_replace_filter_get_formats();
- $links_array = array();
- $filter_id = "$machine_name/0";
- foreach ($format_ids as $format_id => $format) {
- if ($format->{$filter_id}) {
- $text = '!link (present)';
- $link = l($format->name, "admin/settings/filters/$format_id/configure");
- }
- else {
- $text = '!link (absent)';
- $link = l($format->name, "admin/settings/filters/$format_id");
- }
- $links_array[$format_id] = t($text, array('!link' => $link));
- }
- $links = implode(', ', $links_array);
- $form['description'][$machine_name]['#value'] = $description
- . t(' (Configure in !urls)', array('!urls' => $links));
- }
- /**
- * Implementation of hook_theme().
- *
- * @return array
- */
- function url_replace_filter_theme() {
- $ret = array(
- 'url_replace_filter_settings_form' => array(
- 'arguments' => array('form' => NULL),
- ),
- );
- return $ret;
- }
|