JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Fix db_get_value after mysql->mysqli upgrade
[wfpl.git] / upload.php
index f52f0cf..b21af6f 100644 (file)
@@ -1,19 +1,9 @@
 <?php
 
-#  Copyright (C) 2007 Jason Woofenden
-#
-#  This program is free software: you can redistribute it and/or modify
-#  it under the terms of the GNU General Public License as published by
-#  the Free Software Foundation, either version 3 of the License, or
-#  (at your option) any later version.
-#  
-#  This program is distributed in the hope that it will be useful,
-#  but WITHOUT ANY WARRANTY; without even the implied warranty of
-#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-#  GNU General Public License for more details.
-#  
-#  You should have received a copy of the GNU General Public License
-#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+# This program is in the public domain within the United States. Additionally,
+# we waive copyright and related rights in the work worldwide through the CC0
+# 1.0 Universal public domain dedication, which can be found at
+# http://creativecommons.org/publicdomain/zero/1.0/
 
 
 # This file contains functions to accept files being uplodad with the <input
@@ -34,9 +24,9 @@
 # Example:
 # 
 # <form action="foo.php" enctype="multipart/form-data" method="post">
-# <input type="hidden" name="MAX_FILE_SIZE" value="2097152" />
-# <input type="file" name="photo" />
-# <input type="submit" name="save" value="Save" />
+# <input type="hidden" name="MAX_FILE_SIZE" value="2097152">
+# <input type="file" name="photo">
+# <input type="submit" name="save" value="Save">
 # </form>
 # 
 # #######
@@ -90,11 +80,44 @@ function upload_max_filesize() {
        }
 }
 
+# return the extension this path should have WITHOUT the period
+function path_or_mime_to_ext($path, $mime = 'text/plain', $default = 'txt') {
+       $last_dot = strrpos($path, '.');
+       if($last_dot === false || $last_dot === 0 || $last_dot === (strlen($path) - 1)) {
+               # no extension
+               if(isset($GLOBALS['mime_to_ext'][$mime])) {
+                       return $GLOBALS['mime_to_ext'][$mime];
+               } else {
+                       return $default;
+               }
+       } else {
+               $ext = strtolower(substr($path, $last_dot + 1));
+               if(isset($GLOBALS['ext_to_ext'][$ext])) {
+                       return $GLOBALS['ext_to_ext'][$ext];
+               }
+               return $ext;
+       }
+}
+
+# Add or fix extension on path
+# This just does string manipulation (ie  doesn't move/open/etc any files.)
+# Mime type used to generate extension ONLY IF it doesn't have one already.
+function path_fix_ext($path, $mime = 'text/plain', $default_ext = '.txt') {
+       $last_dot = strrpos($path, '.');
+       if($last_dot === false || $last_dot === 0) { # no extension
+               $path .= '.' . path_or_mime_to_ext($path, $mime, $default_ext);
+       } else {
+               $basename = substr($path, 0, $last_dot + 1); # keep dot
+               $path = $basename . path_or_mime_to_ext($path, $mime, $default_ext);
+       }
+
+       return $path;
+}
 
 # pass in the client's path that came from an html <input type="file"/> tag
 #
 # mime time used to generate extension ONLY IF it doesn't have one already.
-function generate_filename($path, $mime = 'text/plain') {
+function generate_filename($path, $mime = 'text/plain', $default_ext = '.txt') {
        # lower case
        $filename = strtolower($path);
 
@@ -111,32 +134,23 @@ function generate_filename($path, $mime = 'text/plain') {
        }
 
        # replace symbols with underscores
-       $filename = ereg_replace('[^a-z0-9_.]', '_', $filename);
-
-       # remove dots from the beginning (no invisible files)
-       $filename = ereg_replace('^\.*', '', $filename);
+       $filename = preg_replace('|[^a-z0-9_.]|', '_', $filename);
 
+       # limit length
        if(strlen($filename > 80)) {
                $filename = substr($filename, -80);
        }
 
-       # fix extension
-       $last_dot = strrpos($filename, '.');
-       if($last_dot === false) {
-               #no extension
-               if(isset($GLOBALS['mime_to_ext'][$mime])) {
-                       $filename .= '.' . $GLOBALS['mime_to_ext'][$mime];
-               } else {
-                       $filename .= '.bin';
-               }
-       } else {
-               $basename = substr($filename, 0, $last_dot);
-               $ext = substr($filename, $last_dot + 1);
-               if(isset($GLOBALS['ext_to_ext'][$ext])) {
-                       $ext .= $GLOBALS['ext_to_ext'][$ext];
-               }
-               $filename = $basename . '.' . $ext;
+       # remove dots from the beginning (no invisible files)
+       $filename = preg_replace('|^\.*|', '', $filename);
+
+       # make sure there's something before the extension
+       if ($filename == '') {
+               return '_';
        }
+
+       $filename = path_fix_ext($filename, $mime, $default_ext);
+
        return $filename;
 }
 
@@ -152,7 +166,7 @@ function generate_filename($path, $mime = 'text/plain') {
 # the client's name appended. Otherwise $path will be used as the filename
 # exactly as is, even if extensions differ between the client's name and $path.
 #
-# where user uploads "c:\foo\Bar baz.PDF" at <input name="in" type="file" />
+# where user uploads "c:\foo\Bar baz.PDF" at <input name="in" type="file">
 #    save_uploaded_file('in', 'uploaded_pdfs/'); yeilds:
 #       "uploaded_pdfs/bar_baz.pdf"
 #    save_uploaded_file('in', 'uploaded_pdfs/prefix.'); yeilds:
@@ -181,8 +195,8 @@ function save_uploaded_file($key, $path) {
 # standard places (like /usr/bin or /usr/local bin) and PHP's PATH environment
 # variable is not set appropriately.
 function path_to($prog, $or_die = true) {
-       $prog = ereg_replace('[^a-zA-Z0-9_.-]', '', $prog);
-       $prog = ereg_replace('^[-.]*', '', $prog);
+       $prog = preg_replace('|[^a-z0-9_.-]|i', '', $prog);
+       $prog = preg_replace('|^[-.]*|', '', $prog);
        if($prog == '') {
                die('Invalid argument to path_to()');
        }
@@ -224,7 +238,7 @@ function gif_to_png($filename, $new_filename = 'just change extension') {
                $new_filename .= '.png';
        }
 
-       imagemagick_convert($filename, $new_filename, "$convert -colorspace RGB", 'GIF to PNG conversion');
+       imagemagick_convert($filename.'[0]', $new_filename, "-colorspace sRGB", 'GIF to PNG conversion');
 
        unlink($filename);
        return $new_filename;
@@ -258,7 +272,7 @@ function make_thumbnail($filename, $max_width = '70', $max_height = '70') {
 function exec_or_die($command, $doing_what) {
        exec($command, $dummy, $ret);
        if($ret != 0) {
-               $base = basename(ereg_replace(' .*', '', $command));
+               $base = basename(preg_replace('| .*|', '', $command));
                die("$doing_what failed. $base called exit($ret)");
        }
 }
@@ -280,7 +294,7 @@ function imagemagick_mogrify($in_filename, $args, $doing_what = "Image conversio
 }
 
 function format_int_70($str) {
-       $str = ereg_replace('[^0-9]', '', $str);
+       $str = preg_replace('|[^0-9]|', '', $str);
        if($str == '') {
                $str = '70';
        }
@@ -331,7 +345,7 @@ function image_w_h_or_die($filename) {
 
 
 # Like save_uploaded_file() (above) except that it converts all images to PNG
-# or JPEG, converts to RGB colorspace, and optionally scales and/or creates a
+# or JPEG, converts to sRGB colorspace, and optionally scales and/or creates a
 # thumbnail. And, if $path ends with a period, the correct extension will be
 # appended.
 #
@@ -352,7 +366,7 @@ function image_w_h_or_die($filename) {
 # and names, call convert_uploaded_image().
 function save_uploaded_image($key, $path, $image_width = 0, $image_height = 0, $thumbnail_width = 0, $thumbnail_height = 0) {
      $image_w_h_thumb_w_h = convert_uploaded_image($key, $path, $image_width, $image_height, $thumbnail_width, $thumbnail_height);
-     return ereg_replace(' .*', '', $image_w_h_thumb_w_h);
+     return preg_replace('| .*|', '', $image_w_h_thumb_w_h);
 }
 
 function ext_to_web_image_ext($in) {
@@ -392,11 +406,11 @@ function convert_uploaded_image($key, $path, $image_width = 0, $image_height = 0
                $filename = $path;
        }
 
-       $convert_params = '-colorspace RGB';
+       $convert_params = '-colorspace sRGB -auto-orient';
        if($image_width > 0 && $image_height > 0) {
                $convert_params .= " -geometry ${image_width}x$image_height";
        }
-       imagemagick_convert($tmp_filename, $filename, $convert_params);
+       imagemagick_convert($tmp_filename.'[0]', $filename, $convert_params);
        unlink($tmp_filename);
        list($w, $h) = image_w_h_or_die($filename);
        $ret = "$filename $w $h";