public
Description: The ultra-lightweight ultra-flexible blogging engine with a fetish for birds and misspellings.
Homepage: http://chyrp.net/
Clone URL: git://github.com/vito/chyrp.git
Click here to lend your support to: chyrp and make a donation at www.pledgie.com !
Fixed double-extension handling. [#123 state:resolved]
vito (author)
Sat Jun 21 00:00:29 -0700 2008
commit  a129416786805114660f6905a72bd45296d82643
tree    8f9cdef695e36fd8107b907bf249e48f51be74ae
parent  0885375588bab452422789686e960cd124ce81f7
...
14
15
16
17
 
18
19
20
...
14
15
16
 
17
18
19
20
0
@@ -14,7 +14,7 @@
0
     public function submit() {
0
       $filename = "";
0
       if (isset($_FILES['photo']) and $_FILES['photo']['error'] == 0)
0
-        $filename = upload($_FILES['photo'], array("jpg", "jpeg", "png", "gif", "tiff", "bmp"));
0
+        $filename = upload($_FILES['photo'], array("jpg", "jpeg", "png", "gif", "tiff", "bmp", "tar.gz"));
0
       elseif (!empty($_POST['from_url'])) {
0
         $file = tempnam(sys_get_temp_dir(), "chyrp");
0
         file_put_contents($file, get_remote($_POST['from_url']));
...
847
848
849
850
851
852
853
854
855
856
857
858
 
 
 
 
859
860
 
 
 
 
 
 
 
 
 
861
862
863
864
 
865
866
867
868
 
 
 
 
 
869
870
871
...
882
883
884
 
 
 
 
 
 
 
 
 
 
 
 
 
885
886
887
888
 
889
890
891
892
893
894
 
895
896
 
897
898
899
 
 
900
901
902
...
847
848
849
 
 
 
 
 
 
 
 
 
850
851
852
853
854
 
855
856
857
858
859
860
861
862
863
864
 
 
 
865
866
 
 
 
867
868
869
870
871
872
873
874
...
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
 
904
905
906
907
908
909
 
910
911
 
912
913
 
 
914
915
916
917
918
0
@@ -847,25 +847,28 @@
0
    *     $name - A unique version of the given $name.
0
    */
0
   function unique_filename($name, $num = 2) {
0
-    if (file_exists(MAIN_DIR.Config::current()->uploads_path.$name)) {
0
-      $name = explode(".", $name);
0
-
0
-      # Handle "double extensions"
0
-      foreach (array("tar" => "gz", "tar" => "bz", "tar" => "bz2") as $first => $second)
0
-        if ($name[count($name) - 2] == $first and end($name) == $second) {
0
-          $name[count($name) - 2] == $first.".".$second;
0
-          unset($name[count($name) - 1]);
0
-        }
0
+    if (!file_exists(MAIN_DIR.Config::current()->uploads_path.$name))
0
+      return $name;
0
+
0
+    $name = explode(".", $name);
0
 
0
-      $ext = ".".array_pop($name);
0
+    # Handle "double extensions"
0
+    foreach (array("tar.gz", "tar.bz", "tar.bz2") as $extension) {
0
+      list($first, $second) = explode(".", $extension);
0
+      $file_first =& $name[count($name) - 2];
0
+      if ($file_first == $first and end($name) == $second) {
0
+        $file_first = $first.".".$second;
0
+        array_pop($name);
0
+      }
0
+    }
0
 
0
-      $try = implode(".", $name)."-".$num.$ext;
0
-      if (!file_exists(MAIN_DIR.Config::current()->uploads_path.$try))
0
-        return $try;
0
+    $ext = ".".array_pop($name);
0
 
0
-      return unique_filename(implode(".", $name).$ext, $num + 1);
0
-    } else
0
-      return $name;
0
+    $try = implode(".", $name)."-".$num.$ext;
0
+    if (!file_exists(MAIN_DIR.Config::current()->uploads_path.$try))
0
+      return $try;
0
+
0
+    return unique_filename(implode(".", $name).$ext, $num + 1);
0
   }
0
 
0
   /**
0
@@ -882,21 +885,34 @@
0
    */
0
   function upload($file, $extension = null, $path = "", $put = false) {
0
     $file_split = explode(".", $file['name']);
0
+
0
+    $original_ext = end($file_split);
0
+
0
+    # Handle "double extensions"
0
+    foreach (array("tar.gz", "tar.bz", "tar.bz2") as $ext) {
0
+      list($first, $second) = explode(".", $ext);
0
+      $file_first =& $file_split[count($file_split) - 2];
0
+      if ($file_first == $first and end($file_split) == $second) {
0
+        $file_first = $first.".".$second;
0
+        array_pop($file_split);
0
+      }
0
+    }
0
+
0
     $file_ext = end($file_split);
0
 
0
     if (is_array($extension)) {
0
-      if (!in_array(strtolower($file_ext), $extension)) {
0
+      if (!in_array(strtolower($file_ext), $extension) and !in_array(strtolower($original_ext), $extension)) {
0
         $list = "";
0
         for ($i = 0; $i < count($extension); $i++) {
0
           $comma = "";
0
           if (($i + 1) != count($extension)) $comma = ", ";
0
           if (($i + 2) == count($extension)) $comma = ", and ";
0
-          $list.= "*.".$extension[$i].$comma;
0
+          $list.= "<code>*.".$extension[$i]."</code>".$comma;
0
         }
0
-        error(__("Error"), _f("Only %s files are supported.", array($list)));
0
+        error(__("Invalid Extension"), _f("Only %s files are supported.", array($list)));
0
       }
0
-    } elseif (isset($extension) and $file_ext != $extension and $file_ext != strtoupper($extension))
0
-      error(__("Error"), _f("Only %s files are supported.", array("*.".$extension)));
0
+    } elseif (isset($extension) and strtolower($file_ext) != strtolower($extension) and strtolower($original_ext) != strtolower($extension))
0
+      error(__("Invalid Extension"), _f("Only %s files are supported.", array("*.".$extension)));
0
 
0
     array_pop($file_split);
0
     $file_clean = implode(".", $file_split);

Comments