From 2cc7b8cceab297aaf83ea42c0f3e30eaee731cbd Mon Sep 17 00:00:00 2001
From: Steven Wittens <steven@10.no-reply.drupal.org>
Date: Thu, 31 Aug 2006 02:23:55 +0000
Subject: [PATCH] #65765: Merge URLfilter.module into core filter.module

---
 modules/filter/filter.module  | 90 ++++++++++++++++++++++++++++++++++-
 modules/system/system.install | 26 ++++++++--
 2 files changed, 111 insertions(+), 5 deletions(-)

diff --git a/modules/filter/filter.module b/modules/filter/filter.module
index e32ff4149b25..1eb7865518e3 100644
--- a/modules/filter/filter.module
+++ b/modules/filter/filter.module
@@ -298,6 +298,9 @@ function filter_filter_tips($delta, $format, $long = FALSE) {
         case 1:
           return t('Lines and paragraphs are automatically recognized. The &lt;br /&gt; line break, &lt;p&gt; paragraph and &lt;/p&gt; close paragraph tags are inserted automatically. If paragraphs are not recognized simply add a couple blank lines.');
       }
+
+    case 3:
+       return t('Web page addresses and e-mail addresses turn into links automatically.');
   }
 }
 
@@ -979,7 +982,7 @@ function theme_filter_tips($tips, $long = FALSE, $extra = '') {
 function filter_filter($op, $delta = 0, $format = -1, $text = '') {
   switch ($op) {
     case 'list':
-      return array(0 => t('HTML filter'), 1 => t('PHP evaluator'), 2 => t('Line break converter'));
+      return array(0 => t('HTML filter'), 1 => t('PHP evaluator'), 2 => t('Line break converter'), 3 => t('URL filter'));
 
     case 'no cache':
       return $delta == 1; // No caching for the PHP evaluator.
@@ -992,6 +995,8 @@ function filter_filter($op, $delta = 0, $format = -1, $text = '') {
           return t('Runs a piece of PHP code. The usage of this filter should be restricted to administrators only!');
         case 2:
           return t('Converts line breaks into HTML (i.e. &lt;br&gt; and &lt;p&gt; tags).');
+        case 3:
+          return t('Turns web and e-mail addresses into clickable links.');
         default:
           return;
       }
@@ -1004,6 +1009,8 @@ function filter_filter($op, $delta = 0, $format = -1, $text = '') {
           return drupal_eval($text);
         case 2:
           return _filter_autop($text);
+        case 3:
+          return _filter_url($text, $format);
         default:
           return $text;
       }
@@ -1012,6 +1019,8 @@ function filter_filter($op, $delta = 0, $format = -1, $text = '') {
       switch ($delta) {
         case 0:
           return _filter_html_settings($format);
+        case 3:
+          return _filter_url_settings($format);
         default:
           return;
       }
@@ -1054,6 +1063,85 @@ function _filter_html($text, $format) {
   return trim($text);
 }
 
+/**
+ * Settings for URL filter.
+ */
+function _filter_url_settings($format) {
+  $form['filter_urlfilter'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('URL filter'),
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+  );
+  $form['filter_urlfilter']['filter_url_length_'. $format] = array(
+    '#type' => 'textfield',
+    '#title' => t('Maximum link text length'),
+    '#default_value' => variable_get('filter_url_length_'. $format, 72),
+    '#maxlength' => 4,
+    '#description' => t('URLs longer than this number of characters will be truncated to prevent long strings that break formatting. The link itself will be retained, just the text portion of the link will be truncated.'),
+  );
+  return $form;
+}
+
+/**
+ * URL filter. Automatically converts text web addresses (URLs, e-mail addresses,
+ * ftp links, etc.) into hyperlinks.
+ */
+function _filter_url($text, $format) {
+  // Pass length to regexp callback
+  _filter_url_trim(NULL, variable_get('filter_url_length_'. $format, 72));
+
+  $text   = ' '. $text .' ';
+
+  // Match absolute URLs.
+  $text = preg_replace_callback("!(<p>|<li>|<br\s*/?>|[ \n\r\t\(])((http://|https://|ftp://|mailto:|smb://|afp://|file://|gopher://|news://|ssl://|sslv2://|sslv3://|tls://|tcp://|udp://)([a-zA-Z0-9@:%_+*~#?&=.,/;-]*[a-zA-Z0-9@:%_+*~#&=/;-]))([.,?]?)(?=(</p>|</li>|<br\s*/?>|[ \n\r\t\)]))!i", '_filter_url_parse_full_links', $text);
+  
+  // Match e-mail addresses.
+  $text = preg_replace("!(<p>|<li>|<br\s*/?>|[ \n\r\t\(])([A-Za-z0-9._-]+@[A-Za-z0-9._+-]+\.[A-Za-z]{2,4})([.,?]?)(?=(</p>|</li>|<br\s*/?>|[ \n\r\t\)]))!i", '\1<a href="mailto:\2">\2</a>\3', $text);
+
+  // Match www domains/addresses.
+  $text = preg_replace_callback("!(<p>|<li>|[ \n\r\t\(])(www\.[a-zA-Z0-9@:%_+*~#?&=.,/;-]*[a-zA-Z0-9@:%_+~#\&=/;-])([.,?]?)(?=(</p>|</li>|<br\s*/?>|[ \n\r\t\)]))!i", '_filter_url_parse_partial_links', $text);
+  $text = substr($text, 1, -1);
+
+  return $text;
+}
+
+/**
+ * Make links out of absolute URLs. 
+ */
+function _filter_url_parse_full_links($match) {
+  $match[2] = decode_entities($match[2]);
+  $caption = check_plain(_filter_url_trim($match[2]));
+  $match[2] = check_url($match[2]);
+  return $match[1] . '<a href="'. $match[2] .'" title="'. $match[2] .'">'. $caption .'</a>'. $match[5];
+}
+
+/**
+ * Make links out of domain names starting with "www."
+ */
+function _filter_url_parse_partial_links($match) {
+  $match[2] = decode_entities($match[2]);
+  $caption = check_plain(_filter_url_trim($match[2]));
+  $match[2] = check_plain($match[2]);
+  return $match[1] . '<a href="http://'. $match[2] .'" title="'. $match[2] .'">'. $caption .'</a>'. $match[3];
+}
+
+/**
+ * Shortens long URLs to http://www.example.com/long/url...
+ */
+function _filter_url_trim($text, $length = NULL) {
+  static $_length;
+  if ($length !== NULL) {
+    $_length = $length;
+  }
+
+  if (strlen($text) > $_length) {
+    $text = substr($text, 0, $_length) .'...';
+  }
+
+  return $text;
+}
+
 /**
  * Convert line breaks into <p> and <br> in an intelligent fashion.
  * Based on: http://photomatt.net/scripts/autop
diff --git a/modules/system/system.install b/modules/system/system.install
index b0ebbe7ca0c3..6335d08f2628 100644
--- a/modules/system/system.install
+++ b/modules/system/system.install
@@ -879,14 +879,19 @@ function system_install() {
 
   db_query("INSERT INTO {node_type} (type, name, module, description, help, has_title, title_label, has_body, body_label, min_word_count, custom, modified, locked, orig_type) VALUES ('page', 'page', 'node', 'If you want to add a static page, like a contact page or an about page, use a page.', '', 1, 'Title', 1, 'Body', 0, 1, 1, 0, 'page')");
   db_query("INSERT INTO {node_type} (type, name, module, description, help, has_title, title_label, has_body, body_label, min_word_count, custom, modified, locked, orig_type) VALUES ('story', 'story', 'node', 'Stories are articles in their simplest form: they have a title, a teaser and a body, but can be extended by other modules. The teaser is part of the body too. Stories may be used as a personal blog or for news articles.', '', 1, 'Title', 1, 'Body', 0, 1, 1, 0, 'story')");
-
   db_query("INSERT INTO {filter_formats} (name, roles, cache) VALUES ('Filtered HTML',',1,2,',1)");
   db_query("INSERT INTO {filter_formats} (name, roles, cache) VALUES ('PHP code','',0)");
   db_query("INSERT INTO {filter_formats} (name, roles, cache) VALUES ('Full HTML','',1)");
-  db_query("INSERT INTO {filters} VALUES (1,'filter',0,0)");
-  db_query("INSERT INTO {filters} VALUES (1,'filter',2,1)");
+
+  db_query("INSERT INTO {filters} VALUES (1,'filter',3,0)");
+  db_query("INSERT INTO {filters} VALUES (1,'filter',0,1)");
+  db_query("INSERT INTO {filters} VALUES (1,'filter',2,2)");
+
   db_query("INSERT INTO {filters} VALUES (2,'filter',1,0)");
-  db_query("INSERT INTO {filters} VALUES (3,'filter',2,0)");
+
+  db_query("INSERT INTO {filters} VALUES (3,'filter',3,0)");
+  db_query("INSERT INTO {filters} VALUES (3,'filter',2,1)");
+
   db_query("INSERT INTO {variable} (name,value) VALUES ('filter_html_1','i:1;')");
 
   db_query("INSERT INTO {variable} (name, value) VALUES ('node_options_forum', '%s')", 'a:1:{i:0;s:6:"status";}');
@@ -3227,6 +3232,19 @@ function system_update_1009() {
  return $ret;
 }
 
+function system_update_1010() {
+  $ret = array();
+
+  // Disable urlfilter.module, if it exists.
+  if (module_exists('urlfilter')) {
+    module_disable('urlfilter');
+    $ret[] = update_sql("UPDATE {filter_formats} SET module = 'filter', delta = 3 WHERE module = 'urlfilter'");
+    $ret[] = t('URL Filter module was disabled; this functionality has now been added to core.');
+  }
+
+  return $ret;
+}
+
 /**
  * @} End of "defgroup updates-4.7-to-x.x"
  * The next series of updates should start at 2000.
-- 
GitLab