changeset 816:95c3e0a1058e

Add new helper function stProbeFileInfo().
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 24 Nov 2014 21:33:58 +0200
parents 4df1bfd11131
children 41f7ab5eddc3
files msite.inc.php
diffstat 1 files changed, 38 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/msite.inc.php	Mon Nov 24 21:31:04 2014 +0200
+++ b/msite.inc.php	Mon Nov 24 21:33:58 2014 +0200
@@ -988,6 +988,44 @@
 }
 
 
+//
+// Probe file type information
+//
+function stProbeFileInfo($filename)
+{
+  global $previewFileTypeList;
+
+  // Get file magic info
+  if (($finfo = finfo_open()) === false)
+    return stError("Internal error. Failed to initialize finfo().");
+
+  $sdata = @finfo_file($finfo, $filename, FILEINFO_NONE);
+  $smime = @finfo_file($finfo, $filename, FILEINFO_MIME_TYPE);
+  finfo_close($finfo);
+
+  // Did we get anything?
+  if ($sdata === FALSE || $smime === FALSE)
+    return stError("Internal error, failed to probe file.");
+
+  // Match through our supported types ..
+  foreach ($previewFileTypeList as $fid => $fdata)
+  {
+    $fdata["id"] = $fid;
+
+    if (isset($fdata["test"]))
+    {
+      if (preg_match("/".$fdata["test"]."/", $sdata))
+        return $fdata;
+    }
+    else
+    if ($fdata["mime"] == $smime)
+      return $fdata;
+  }
+
+  return stError("No matching allowed file type found.");
+}
+
+
 // Get link helper function
 function stGetMainPageLink($id, $name, $show = TRUE)
 {