Initial Kohana install
[speedfreak] / Server / system / helpers / upload.php
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3  * Upload helper class for working with the global $_FILES
4  * array and Validation library.
5  *
6  * $Id: upload.php 3769 2008-12-15 00:48:56Z zombor $
7  *
8  * @package    Core
9  * @author     Kohana Team
10  * @copyright  (c) 2007-2008 Kohana Team
11  * @license    http://kohanaphp.com/license.html
12  */
13 class upload_Core {
14
15         /**
16          * Save an uploaded file to a new location.
17          *
18          * @param   mixed    name of $_FILE input or array of upload data
19          * @param   string   new filename
20          * @param   string   new directory
21          * @param   integer  chmod mask
22          * @return  string   full path to new file
23          */
24         public static function save($file, $filename = NULL, $directory = NULL, $chmod = 0644)
25         {
26                 // Load file data from FILES if not passed as array
27                 $file = is_array($file) ? $file : $_FILES[$file];
28
29                 if ($filename === NULL)
30                 {
31                         // Use the default filename, with a timestamp pre-pended
32                         $filename = time().$file['name'];
33                 }
34
35                 if (Kohana::config('upload.remove_spaces') === TRUE)
36                 {
37                         // Remove spaces from the filename
38                         $filename = preg_replace('/\s+/', '_', $filename);
39                 }
40
41                 if ($directory === NULL)
42                 {
43                         // Use the pre-configured upload directory
44                         $directory = Kohana::config('upload.directory', TRUE);
45                 }
46
47                 // Make sure the directory ends with a slash
48                 $directory = rtrim($directory, '/').'/';
49
50                 if ( ! is_dir($directory) AND Kohana::config('upload.create_directories') === TRUE)
51                 {
52                         // Create the upload directory
53                         mkdir($directory, 0777, TRUE);
54                 }
55
56                 if ( ! is_writable($directory))
57                         throw new Kohana_Exception('upload.not_writable', $directory);
58
59                 if (is_uploaded_file($file['tmp_name']) AND move_uploaded_file($file['tmp_name'], $filename = $directory.$filename))
60                 {
61                         if ($chmod !== FALSE)
62                         {
63                                 // Set permissions on filename
64                                 chmod($filename, $chmod);
65                         }
66
67                         // Return new file path
68                         return $filename;
69                 }
70
71                 return FALSE;
72         }
73
74         /* Validation Rules */
75
76         /**
77          * Tests if input data is valid file type, even if no upload is present.
78          *
79          * @param   array  $_FILES item
80          * @return  bool
81          */
82         public static function valid($file)
83         {
84                 return (is_array($file)
85                         AND isset($file['error'])
86                         AND isset($file['name'])
87                         AND isset($file['type'])
88                         AND isset($file['tmp_name'])
89                         AND isset($file['size']));
90         }
91
92         /**
93          * Tests if input data has valid upload data.
94          *
95          * @param   array    $_FILES item
96          * @return  bool
97          */
98         public static function required(array $file)
99         {
100                 return (isset($file['tmp_name'])
101                         AND isset($file['error'])
102                         AND is_uploaded_file($file['tmp_name'])
103                         AND (int) $file['error'] === UPLOAD_ERR_OK);
104         }
105
106         /**
107          * Validation rule to test if an uploaded file is allowed by extension.
108          *
109          * @param   array    $_FILES item
110          * @param   array    allowed file extensions
111          * @return  bool
112          */
113         public static function type(array $file, array $allowed_types)
114         {
115                 if ((int) $file['error'] !== UPLOAD_ERR_OK)
116                         return TRUE;
117
118                 // Get the default extension of the file
119                 $extension = strtolower(substr(strrchr($file['name'], '.'), 1));
120
121                 // Get the mime types for the extension
122                 $mime_types = Kohana::config('mimes.'.$extension);
123
124                 // Make sure there is an extension, that the extension is allowed, and that mime types exist
125                 return ( ! empty($extension) AND in_array($extension, $allowed_types) AND is_array($mime_types));
126         }
127
128         /**
129          * Validation rule to test if an uploaded file is allowed by file size.
130          * File sizes are defined as: SB, where S is the size (1, 15, 300, etc) and
131          * B is the byte modifier: (B)ytes, (K)ilobytes, (M)egabytes, (G)igabytes.
132          * Eg: to limit the size to 1MB or less, you would use "1M".
133          *
134          * @param   array    $_FILES item
135          * @param   array    maximum file size
136          * @return  bool
137          */
138         public static function size(array $file, array $size)
139         {
140                 if ((int) $file['error'] !== UPLOAD_ERR_OK)
141                         return TRUE;
142
143                 // Only one size is allowed
144                 $size = strtoupper($size[0]);
145
146                 if ( ! preg_match('/[0-9]++[BKMG]/', $size))
147                         return FALSE;
148
149                 // Make the size into a power of 1024
150                 switch (substr($size, -1))
151                 {
152                         case 'G': $size = intval($size) * pow(1024, 3); break;
153                         case 'M': $size = intval($size) * pow(1024, 2); break;
154                         case 'K': $size = intval($size) * pow(1024, 1); break;
155                         default:  $size = intval($size);                break;
156                 }
157
158                 // Test that the file is under or equal to the max size
159                 return ($file['size'] <= $size);
160         }
161
162 } // End upload