Index: pathfilter_auto.info
===================================================================
--- pathfilter_auto.info	(revision 0)
+++ pathfilter_auto.info	(revision 0)
@@ -0,0 +1,4 @@
+name=Path Fiter Auto
+description=Automatically converts URLs to use the pathfilter syntax.
+dependencies=pathfilter
+
Index: pathfilter_auto.module
===================================================================
--- pathfilter_auto.module	(revision 0)
+++ pathfilter_auto.module	(revision 0)
@@ -0,0 +1,113 @@
+<?php
+
+
+/**
+ * @file 
+ * This module compliments pathfilter.  When forms are submitted with
+ * URLs which could have use pathfilter's "internal:..." syntax, but did not,
+ * those URLs are automatically changed to use the pathfilter syntax.
+ * 
+ * Specifically, this module helps when pathfilter is enabled and IMCE is
+ * used.  Normally IMCE enters an entire path to an image file.  This module
+ * will force something like "internal:path/to/image" to be used instead.
+ * 
+ * There are currently several limitations to this module.  (1) It replaces
+ * URLs whether or not pathfilter is actually used.  That is, if the input
+ * format does not include pathfilter. (2) It replaces URLs in all textareas.
+ * That is, it does not discriminate whether input formats even apply. (3) It
+ * is not clean_url aware.  Specifically, if clean urls are not enabled, links
+ * to files will be broken.
+ * 
+ * Because of these limitations, it is recommended to enable this module only
+ * when a site is installed on a temporary URL.  That is, if you know the URL
+ * will change in the future, but you want your links to work anyway.
+ * 
+ * Author: original implementation by Dave Cohen http://www.dave-cohen.com
+ */
+
+/**
+ * This function replaces all URLs which can use the "internal:..." syntax.
+ * 
+ * @param
+ * $before is the text to perform replacement on.  It will be changed by this function.
+ * 
+ * @return
+ * TRUE if any portion of the text passed in has been replaced.
+ */
+function pathfilter_auto_replace(&$before) {
+  // Substitute for Drupal's base_url and base_path 
+  $base_url = $GLOBALS['base_url'];
+  $base_path = base_path();
+
+  // Replace links to the front page.
+  $patterns[] = "|\"$base_url/?\"|";
+  $replacements[] = "\"internal:<front>\"";
+  $patterns[] = "|\"$base_path\"|";
+  $replacements[] = "\"internal:<front>\"";
+  
+  // Replace paths which use ?q= syntax (not clean urls).
+  $patterns[] = "|\"$base_url/?\?q=([^&\"]+)[^\"]*\"|";
+  $replacements[] = "\"internal:$1\"";
+  $patterns[] = "|\"$base_path\?q=([^&\"]+)[^\"]*\"|";
+  $replacements[] = "\"internal:$1\"";
+
+  // Replace paths which use clean url syntax.  This will include files, when
+  // the file directory is a subdirectory of Drupal's root.
+  $patterns[] = "|\"$base_url/([^\?\"]+)[^\"]*\"|";
+  $replacements[] = "\"internal:$1\"";
+  $patterns[] = "|\"$base_path([^\?\"]+)[^\"]*\"|";
+  $replacements[] = "\"internal:$1\"";
+  
+  
+  $count = 0;
+  $after = preg_replace($patterns, $replacements, $before);
+  if ($after != $before) {
+    //dpm($after, 'pathfilter_auto_replace after replacement'); // debug
+    $before = $after; // Replace the text that was passed in.
+    return TRUE;
+  }
+
+}
+
+/**
+ * Implementation of hook_form_alter.
+ * 
+ * Here, we ensure our validation callback is invoked for all forms.
+ */
+function pathfilter_auto_form_alter($form_id, &$form) {
+
+  $validate_data = array('pathfilter_auto_validate_callback' => array());
+  if (is_array($form['#validate']))
+    $form['#validate'] = $validate_data + $form['#validate'];
+  else
+    $form['#validate'] = $validate_data;
+  }
+
+function pathfilter_auto_validate_callback($form_id, $values, $form) {
+  //dpm(func_get_args(), 'pathfilter_auto_validate_callback');
+
+  _pathfilter_auto_validate($form);
+}
+
+/**
+ * This function finds all textareas in a form and performs our replacement on
+ * only those fields.
+ */
+function _pathfilter_auto_validate(&$elements, $k = NULL) {
+  foreach (element_children($elements) as $key) {
+    if (isset($elements[$key]) && $elements[$key]) {
+      _pathfilter_auto_validate($elements[$key], $key);
+    }
+  }
+  if ($elements['#type'] == 'textarea') {
+    //dpm($elements, '_pathfilter_auto_validate');
+    $text = $elements['#value'];
+    $replaced = pathfilter_auto_replace($text);
+    if ($replaced) {
+      form_set_value($elements, $text);
+      // TODO: perhaps set a message here to let the user know replacement has occurred.
+    }
+  }
+}
+
+?>
\ No newline at end of file
