correctly determine file type
authorChristian Weiske <cweiske@cweiske.de>
Sun, 11 Sep 2016 12:42:11 +0000 (14:42 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Sun, 11 Sep 2016 12:42:11 +0000 (14:42 +0200)
build.xml
src/shpub/Command/Note.php

index dfd3c8cb5536c3e000216a8052e92c224879bf1f..1a580726b796e012862c92f745197e2e5b709c6f 100644 (file)
--- a/build.xml
+++ b/build.xml
@@ -53,6 +53,7 @@
 
   <pearPackageFileset id="dep-Console_CommandLine" package="pear.php.net/Console_CommandLine"/>
   <pearPackageFileset id="dep-HTTP_Request2" package="pear.php.net/HTTP_Request2"/>
 
   <pearPackageFileset id="dep-Console_CommandLine" package="pear.php.net/Console_CommandLine"/>
   <pearPackageFileset id="dep-HTTP_Request2" package="pear.php.net/HTTP_Request2"/>
+  <pearPackageFileset id="dep-MIME_Type" package="pear.php.net/MIME_Type"/>
   <pearPackageFileset id="dep-Net_URL2" package="pear.php.net/Net_URL2"/>
   <pearPackageFileset id="dep-PEAR" package="pear.php.net/PEAR">
    <include name="PEAR/Exception.php"/>
   <pearPackageFileset id="dep-Net_URL2" package="pear.php.net/Net_URL2"/>
   <pearPackageFileset id="dep-PEAR" package="pear.php.net/PEAR">
    <include name="PEAR/Exception.php"/>
@@ -61,6 +62,7 @@
   <copy todir="${libdir}">
    <fileset refid="dep-Console_CommandLine"/>
    <fileset refid="dep-HTTP_Request2"/>
   <copy todir="${libdir}">
    <fileset refid="dep-Console_CommandLine"/>
    <fileset refid="dep-HTTP_Request2"/>
+   <fileset refid="dep-MIME_Type"/>
    <fileset refid="dep-Net_URL2"/>
    <fileset refid="dep-PEAR"/>
   </copy>
    <fileset refid="dep-Net_URL2"/>
    <fileset refid="dep-PEAR"/>
   </copy>
index d5231a9d62160fa5723d47ec61391273a20cdf2b..b5df8ba6fe001a1f17353b2d1df672b82c135a12 100644 (file)
@@ -59,37 +59,56 @@ class Command_Note
         }
 
         $files = $command->options['files'];
         }
 
         $files = $command->options['files'];
-        $fileList = [
+        $fileList = $urlList = [
             'audio' => [],
             'audio' => [],
-            'photo' => [],
+            'image' => [],
             'video' => [],
         ];
             'video' => [],
         ];
-        $urls = [];
+
         foreach ($files as $filePath) {
         foreach ($files as $filePath) {
-            if (file_exists($filePath)) {
-                $type = 'photo';
-                $fileList[$type][] = $filePath;
-            } else if (strpos($filePath, '://') !== false) {
+            if (strpos($filePath, '://') !== false) {
                 //url
                 //url
-                $urls[] = $filePath;
+                $mte      = new \MIME_Type_Extension();
+                $mimetype = $mte->getMIMEType($filePath);
+                $media    = \MIME_Type::getMedia($mimetype);
+                if (!isset($urlList[$media])) {
+                    Log::err('File type not allowed: ' . $mimetype);
+                    exit(20);
+                }
+                $urlList[$media][] = $filePath;
+            } else if (file_exists($filePath)) {
+                //file
+                $mimetype = \MIME_Type::autoDetect($filePath);
+                $media    = \MIME_Type::getMedia($mimetype);
+                if (!isset($urlList[$media])) {
+                    Log::err('File type not allowed: ' . $mimetype);
+                    exit(20);
+                }
+                $fileList[$media][] = $filePath;
             } else {
                 Log::err('File does not exist: ' . $filePath);
                 exit(20);
             }
         }
             } else {
                 Log::err('File does not exist: ' . $filePath);
                 exit(20);
             }
         }
-        if (count($urls)) {
+        foreach ($urlList as $type => $urls) {
+            if ($type == 'image') {
+                $type = 'photo';
+            }
             if (count($urls) == 1) {
             if (count($urls) == 1) {
-                $req->req->addPostParameter('photo', reset($urls));
-            } else {
+                $req->req->addPostParameter($type, reset($urls));
+            } else if (count($urls) > 1) {
                 $n = 0;
                 foreach ($urls as $url) {
                     $req->req->addPostParameter(
                 $n = 0;
                 foreach ($urls as $url) {
                     $req->req->addPostParameter(
-                        'photo[' . $n++ . ']', reset($urls)
+                        $type . '[' . $n++ . ']', reset($urls)
                     );
                 }
             }
         }
         foreach ($fileList as $type => $filePaths) {
                     );
                 }
             }
         }
         foreach ($fileList as $type => $filePaths) {
+            if ($type == 'image') {
+                $type = 'photo';
+            }
             if (count($filePaths) == 1) {
                 $req->addUpload($type, reset($filePaths));
             } else if (count($filePaths) > 0) {
             if (count($filePaths) == 1) {
                 $req->addUpload($type, reset($filePaths));
             } else if (count($filePaths) > 0) {