--- inline.module-orig	2007-09-28 18:11:46.000000000 +0200
+++ inline.module	2007-10-28 13:39:29.000000000 +0100
@@ -86,11 +86,38 @@
     '#type' => 'fieldset',
     '#title' => t('Image dimensions and scaling'),
     '#collapsible' => true,
     '#description' => (module_exists('imagecache') ? t('Select the <a href="!presets">Imagecache presets</a> to use for inlined images.', array('!presets' => url('admin/settings/imagecache'))) : t('<strong>Note:</strong> If <a href="!imagecache">Imagecache</a> module is installed, Inline provides support for image scaling.', array('!imagecache' => url('http://drupal.org/project/imagecache')))),
   );
+
+  $form['inline']['upload']['inline_xls'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('MS-Excel files'),
+    '#collapsible' => true,
+  );
+  $form['inline']['upload']['inline_xls']['path']['inline_xlhtml_path'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Absolute path to executable xlhtml'),
+    '#default_value' => variable_get('inline_xlhtml_path', '/usr/bin/xlhtml'),
+  );
+  $form['inline']['upload']['inline_xls']['opt']['inline_xlhtml_opt'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Options for xlhtml'),
+    '#default_value' => variable_get('inline_xlhtml_opt', '-nh -fw'),
+  );
+  $form['inline']['upload']['inline_xls']['path']['inline_iconv_path'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Absolute path to executable iconv'),
+    '#default_value' => variable_get('inline_iconv_path', '/usr/bin/iconv'),
+  );
+  $form['inline']['upload']['inline_xls']['opt']['inline_iconv_opt'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Options for iconv'),
+    '#default_value' => variable_get('inline_iconv_opt', '-f ISO8859-2 -t UTF8'),
+  );
   
+
   // If Imagecache module exists and is enabled, we assume that we want to use
   // the improved image handling instead of our own.
   if (module_exists('imagecache')) {
     $options     = array();
     $options[''] = 'No Imagecache processing';
@@ -122,14 +149,33 @@
       '#required' => TRUE,
       '#default_value' => variable_get('inline_img_dim', '150,150'),
       '#description' => t('This setting limits the dimensions of displayed images in pixels. They will not be resized. Images exceeding these dimensions are automatically not displayed.', array('!content-types' => url('admin/content/types'))),
     );
   }
+
+  _inline_validate_paths($form);
   
   return system_settings_form($form);
 }
 
+
+/**
+ * Checks the different pathes in the settings
+ */
+function _inline_validate_paths($form) {
+  $errors = 0;
+  foreach ($form['inline']['upload']['inline_xls']['path'] as $name => $details) {
+    $value = variable_get($name, '');
+    if ($value && !is_executable($value) ){
+      $errors += 1;
+      drupal_set_message(t('inline: ' . $value . ' does not exist or is not executable'));
+    }
+  }
+  return $errors == 0;
+}
+
+
 /**
  * Implementation of hook_form_alter().
  * 
  * Allows to enable/disable auto-inline support for each content type.
  */
@@ -258,11 +304,13 @@
     }
     return NULL;
   }
   else {
     // Named file reference.
-    foreach ($node->files as $file) {
+    // when several attachments have the same file name
+    // then return the last occurance
+    foreach ( array_reverse($node->files,TRUE) as $file) {
       $file = (object)$file;
       if ($file->filename == $id) {
         return $file;
       }
     }
@@ -337,10 +385,48 @@
     $html = $image;
   }
   
   return $html;
 }
+ 
+
+/**
+ * for each file type supported there must exist a function 
+ * to create the viewable output
+ *
+ * @param object $file
+ *   A file object of an excel document to insert.
+ * @param string $field
+ *   The field name to prepend with the image (unused)
+**/
+function theme_inline_xls($file, $field) {
+  $errors = 0;
+
+  $xlhtml_path= variable_get('inline_xlhtml_path','');
+  $xlhtml_opt= variable_get('inline_xlhtml_opt','');
+  $iconv_path= variable_get('inline_iconv_path','');
+  $iconv_opt= variable_get('inline_iconv_opt','');
+
+  if ($xlhtml_path && !is_executable($xlhtml_path) ){
+    $errors += 1;
+    drupal_set_message(t('inline: ' . $xlhtml_path . ' does not exist or is not executable'));
+  }
+  if ($iconv_path && !is_executable($iconv_path) ){
+    $errors += 1;
+    drupal_set_message(t('inline: ' . $iconv_path . ' does not exist or is not executable'));
+  }
+
+  if (!$errors && $xlhtml_path && $iconv_path) {	// excel -> html transformation
+    $html = shell_exec($xlhtml_path . ' ' . $xlhtml_opt . ' ' . $file->filepath . ' | ' . $iconv_path . ' ' . $iconv_opt);
+  }
+  else {	// show a link to the file if transformation to html is not possible
+    $html = theme('inline_as_link', $file);
+  }
+
+  return $html;
+}
+
 
 /**
  * Insert an image in front of node teaser.
  * 
  * @param object $node
@@ -443,16 +529,21 @@
         $title = $match[3][$key];
         if (!empty($title)) {
           $file->title = $title;
         }
         // Decide whether to show a link or an image tag.
-        if (_inline_decide_img_tag($file)) {
-          $replace = theme('inline_img', $file, $field);
-        }
-        else {
-          $replace = theme('inline_as_link', $file);
-        }
+	// _inline_decide_inline_tag now returns some kind of document type
+	switch( _inline_decide_inline_tag($file) ) {
+	    case 'img':
+          	$replace = theme('inline_img', $file, $field);
+		break;
+	    case 'xls':
+          	$replace = theme('inline_xls', $file, $field);
+		break;
+	    default:
+		$replace = theme('inline_as_link', $file);
+	}
       }
       else {
         $replace = '<span style="color: red; font-weight: bold;">NOT FOUND: '. $value .'</span>';
       }
       $s[] = $match[0][$key];
@@ -537,5 +628,28 @@
     }
   }
   return FALSE;
 }
 
+
+/**
+ * Decides if a inline tag or a link to a file should be rendered
+ *
+ * @param $file a file object
+ *
+ * @return type of file found or '' if unknown:
+ * 	currently the following types are supported
+ *	'img'	for inlinable images
+ *	'xls'	for MS excel sheet
+ */
+function _inline_decide_inline_tag($file) {
+  if (_inline_decide_img_tag($file)) {
+	return 'img';
+  } else {
+	$inlined = array('xls', 'vnd.ms-excel');
+	$mime = array_pop(explode('/', $file->filemime));
+	if (in_array($mime, $inlined)) {
+	  return 'xls';
+	}
+	return '';
+  }
+}
