[Solar-svn] Revision 3099

pmjones at solarphp.com pmjones at solarphp.com
Fri Apr 11 09:07:57 CDT 2008


Solar_Filter_SanitizeUpload: [NEW] Sanitizes a file-upload information array.


Added: trunk/Solar/Filter/SanitizeUpload.php
===================================================================
--- trunk/Solar/Filter/SanitizeUpload.php	                        (rev 0)
+++ trunk/Solar/Filter/SanitizeUpload.php	2008-04-11 14:07:52 UTC (rev 3099)
@@ -0,0 +1,86 @@
+<?php
+/**
+ * 
+ * Sanitizes a file-upload information array.
+ * 
+ * @category Solar
+ * 
+ * @package Solar_Filter
+ * 
+ * @author Paul M. Jones <pmjones at solarphp.com>
+ * 
+ * @license http://opensource.org/licenses/bsd-license.php BSD
+ * 
+ * @version $Id: ValidateWord.php 2926 2007-11-09 16:25:44Z pmjones $
+ * 
+ */
+
+/**
+ * 
+ * Sanitizes a file-upload information array.
+ * 
+ * @category Solar
+ * 
+ * @package Solar_Filter
+ * 
+ */
+class Solar_Filter_SanitizeUpload extends Solar_Filter_Abstract {
+    
+    /**
+     * 
+     * Sanitizes a file-upload information array.  If no file has been 
+     * uploaded, the information will be returned as null.
+     * 
+     * @param array $value An array of file-upload information.
+     * 
+     * @return mixed The sanitized upload information array, or null.
+     * 
+     */
+    public function sanitizeUpload($value)
+    {
+        // if the value is not required, and is blank, sanitize to null
+        $null = ! $this->_filter->getRequire() &&
+                $this->_filter->validateBlank($value);
+                
+        if ($null) {
+            return null;
+        }
+        
+        // has to be an array
+        if (! is_array($value)) {
+            return null;
+        }
+        
+        // presorted list of expected keys
+        $expect = array('error', 'name', 'size', 'tmp_name', 'type');
+        
+        // remove unexpected keys
+        foreach ($value as $key => $val) {
+            if (! in_array($key, $expect)) {
+                unset($value[$key]);
+            }
+        }
+        
+        // sort the list of remaining actual keys
+        $actual = array_keys($value);
+        sort($actual);
+        
+        // make sure the expected and actual keys match up
+        if ($expect != $actual) {
+            return null;
+        }
+        
+        // if all the non-error values are empty, still null
+        $empty = empty($value['name']) &&
+                 empty($value['size']) &&
+                 empty($value['tmp_name']) &&
+                 empty($value['type']);
+                 
+        if ($empty) {
+            return null;
+        }
+        
+        // everything looks ok, return as-is
+        return $value;
+    }
+}
\ No newline at end of file




More information about the Solar-svn mailing list