From 8f49e4a99c2a35f71b763177b32c2cf1fef9a8c6 Mon Sep 17 00:00:00 2001
From: Thomas Bruederli <thomas@roundcube.net>
Date: Thu, 04 Jul 2013 17:56:26 -0400
Subject: [PATCH] Check filetype detection in installer and update script (#1489193)

---
 installer/rcube_install.php |   45 ++++++++++++++++++++++
 bin/update.sh               |   10 +++++
 installer/test.php          |   36 ++++++++++++++++++
 3 files changed, 91 insertions(+), 0 deletions(-)

diff --git a/bin/update.sh b/bin/update.sh
index 05956b9..15edc28 100755
--- a/bin/update.sh
+++ b/bin/update.sh
@@ -143,6 +143,16 @@
     }
   }
 
+  // check file type detection
+  if ($RCI->check_mime_detection()) {
+    echo "WARNING: File type detection doesn't work properly!\n";
+    echo "Please check the 'mime_magic' config option or the finfo functions of PHP andrun this script again.\n";
+  }
+  if ($RCI->check_mime_extensions()) {
+    echo "WARNING: Mimetype to file extension mapping doesn't work properly!\n";
+    echo "Please check the 'mime_types' config option and run this script again.\n";
+  }
+
   // check database schema
   if ($RCI->config['db_dsnw']) {
     echo "Executing database schema update.\n";
diff --git a/installer/rcube_install.php b/installer/rcube_install.php
index 637808e..ab79677 100644
--- a/installer/rcube_install.php
+++ b/installer/rcube_install.php
@@ -413,6 +413,51 @@
     return $schema;
   }
 
+  /**
+   * Try to detect some file's mimetypes to test the correct behavior of fileinfo
+   */
+  function check_mime_detection()
+  {
+    $files = array(
+      'installer/images/roundcube_logo.png' => 'image/png',
+      'program/resources/blank.tif' => 'image/tiff',
+      'skins/larry/templates/login.html' => 'text/html',
+    );
+
+    $errors = array();
+    foreach ($files as $path => $expected) {
+      $mimetype = rcube_mime::file_content_type(INSTALL_PATH . $path, basename($path));
+      if ($mimetype != $expected) {
+        $errors[] = array($path, $mimetype, $expected);
+      }
+    }
+
+    return $errors;
+  }
+
+  /**
+   * Check the correct configuration of the 'mime_types' mapping option
+   */
+  function check_mime_extensions()
+  {
+    $types = array(
+      'application/zip'   => 'zip',
+      'application/x-tar' => 'tar',
+      'application/java-archive' => 'jar',
+      'image/bmp'     => 'bmp',
+      'image/svg+xml' => 'svg',
+    );
+
+    $errors = array();
+    foreach ($types as $mimetype => $expected) {
+      $ext = rcube_mime::get_mime_extensions($mimetype);
+      if ($ext[0] != $expected) {
+        $errors[] = array($mimetype, $ext, $expected);
+      }
+    }
+
+    return $errors;
+  }
 
   /**
    * Getter for the last error message
diff --git a/installer/test.php b/installer/test.php
index fb3e7e9..75dac47 100644
--- a/installer/test.php
+++ b/installer/test.php
@@ -225,6 +225,42 @@
 
 ?>
 
+<h3>Test filetype detection</h3>
+
+<p>
+<?php
+
+if ($errors = $RCI->check_mime_detection()) {
+  $RCI->fail('Fileinfo/mime_content_type configuration');
+  if (!empty($RCI->config['mime_magic'])) {
+    echo '<p class="hint">Try setting the <tt>mime_magic</tt> config option to <tt>null</tt>.</p>';
+  }
+  else {
+    echo '<p class="hint">Check the <a href="http://www.php.net/manual/en/function.finfo-open.php">Fileinfo functions</a> of your PHP installation.<br/>';
+    echo 'The path to the magic.mime file can be set using the <tt>mime_magic</tt> config option in Roundcube.</p>';
+  }
+}
+else {
+  $RCI->pass('Fileinfo/mime_content_type configuration');
+}
+
+?>
+</p>
+<p>
+<?php
+
+if ($errors = $RCI->check_mime_extensions()) {
+  $RCI->fail('Mimetype to file extension mapping');
+  echo '<p class="hint">Please set a valid path to your webserver\'s mime.types file to the <tt>mime_types</tt> config option.<br/>';
+  echo 'If you can\'t find such a file, download it from <a href="http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types">svn.apache.org</a>.</p>';
+}
+else {
+  $RCI->pass('Mimetype to file extension mapping');
+}
+
+?>
+
+
 <h3>Test SMTP config</h3>
 
 <p>

--
Gitblit v1.9.1