--- inline.module.bak	2005-07-26 08:48:37.000000000 +1000
+++ inline.module	2005-09-30 09:52:37.000000000 +1000
@@ -33,13 +33,18 @@
   // All operations besides "list" provide a $delta argument so we know which
   // filter they refer to. We'll switch on that argument now so that we can
   // discuss each filter in turn.
-  switch ($op) {
-    case 'description':
-       return t('Substitutes [inline:xx] tags with the xxth file uploaded with the node.');
-    case 'prepare':
-       return $text;
-    case 'process':
-       return $text;
+  switch ($delta) {
+    case 0:
+      switch ($op) {
+        case 'description':
+          return t('Substitutes [inline:xx] tags with the xxth file uploaded with the node.');
+        case 'no cache': 
+          return FALSE; 
+        case 'prepare':
+          return $text;
+        case 'process':
+          return _inline_substitute_tags($text);
+      }
   }
 }
 
@@ -76,33 +81,20 @@
   }
 }
 
-function inline_nodeapi(&$node, $op, $arg) {
-  if(is_array($node->files) && $op == 'view') {
-    $node->teaser = _inline_substitute_tags($node, 'teaser');
-    $node->body = _inline_substitute_tags($node, 'body');
-  }
-}
-
-function _inline_filename(&$node, $id) {
+function _inline_filename($files, $id) {
   if (is_numeric($id)) {
-    $n=1;
-    foreach ($node->files as $file) {
-      if ($n == $id) {
-        return array($file->filename, $file->filepath);
-      }
-      ++$n;
+    if (count($files) >= $id) {
+      return $files[$id];
     }
-    return NULL;
   }
-  else
-  {
-    foreach ($node->files as $file) {
-      if ($file->filename == $id) {
-        return array($file->filename, $file->filepath);
+  else {
+    foreach ($files as $file) {
+      if ($file['filename'] == $id) {
+        return $file;
       }
     }
-    return NULL;
   }
+  return NULL;
 }
 
 function theme_inline_html($filename, $filepath, $title, $allow_inline_image) {
@@ -150,26 +142,30 @@
   return $html;
 }
 
-function _inline_substitute_tags(&$node, $field) {
-    if (preg_match_all("/\[(inline|file|attachment):([^=\\]]+)=?([^\\]]*)?\]/i", $node->$field, $match)) {
-       foreach ($match[2] as $key => $value) {
-         $map[$value] = $key;
-         $titl = $match[3][$key];
-         $mytype = $match[1][$key];
-         $inline_file = _inline_filename($node, $value);
-         $replace = "";
-         if ($inline_file != NULL) {
-           $replace = theme('inline_html', $inline_file[0], $inline_file[1], $titl, $mytype == "inline");
-         }
-         else {
-           $replace = "<span style=\"color:red; font-weight:bold\">NOT FOUND: $value</span>";
-         }
-         $mtch[] = $match[0][$key];
-         $repl[] = $replace;
-       }
-       return str_replace($mtch, $repl, $node->$field);
+function _inline_substitute_tags($text) {
+  if (preg_match_all("/\[(inline|file|attachment):([^=\\]]+)=?([^\\]]*)?\]/i", $text, $match)) {
+    // retrieve from db all files for this node and store them in an array
+    $result = db_query("SELECT * FROM {files} WHERE nid = '%d'", arg(1)); 
+    for ($i=1; $a_file = db_fetch_array($result); $i++) {
+      $fileinfo[$i] = $a_file;
+    }
+    foreach ($match[2] as $key => $value) {
+      $title = $match[3][$key];
+      $mytype = $match[1][$key];
+      $inline_file = _inline_filename($fileinfo, $value);
+      $replace = "";
+      if ($inline_file != NULL) {
+        $replace = theme('inline_html', $inline_file['filename'], $inline_file['filepath'], $title, $mytype == "inline");
+      }
+      else {
+        $replace = "<span style=\"color:red; font-weight:bold\">NOT FOUND: $value</span>";
+      }
+      $mtch[] = $match[0][$key];
+      $repl[] = $replace;
     }
-    return $node->$field;
+    return str_replace($mtch, $repl, $text);
+  }
+  return $text;
 }
 
 ?>