~budgester/irm/trunk

« back to all changes in this revision

Viewing changes to lib/class.httpupload.php

  • Committer: budgester at budgester
  • Date: 2008-03-05 23:14:13 UTC
  • Revision ID: budgester@budgester.com-20080305231413-k5vqfuckfo09ju42
Initial import of IRM codebase

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?
 
2
 
 
3
/**
 
4
 * class.httpupload.php
 
5
 * a smart class help you in checking and saving uploads
 
6
 * Visit http://en.vietapi.com/wiki/index.php/PHP:_HttpUpload for more information
 
7
 * @package Misc
 
8
 * @author Nguyen Quoc Bao <quocbao.coder@gmail.com>
 
9
 * @version 1.0
 
10
 **/
 
11
 
 
12
define('HTTPUPLOAD_ERROR_OK' , 1);
 
13
define('HTTPUPLOAD_ERROR_NO_FILE' , -1);
 
14
define('HTTPUPLOAD_ERROR_INI_SIZE' , -2); //php size limit
 
15
define('HTTPUPLOAD_ERROR_FORM_SIZE' , -3); //form size limit
 
16
define('HTTPUPLOAD_ERROR_SIZE' , -4); //class size limit
 
17
define('HTTPUPLOAD_ERROR_IMG' , -5); //image size limit
 
18
define('HTTPUPLOAD_ERROR_EXT' , -6); //extension is not allowed
 
19
define('HTTPUPLOAD_ERROR_MIME' , -7); //mime is not allowed
 
20
define('HTTPUPLOAD_ERROR_WRITE' , -8); //there was a problem during processing file
 
21
define('HTTPUPLOAD_ERROR_PARTIAL' , -9); //The uploaded file was only partially uploaded
 
22
 
 
23
class httpupload {
 
24
 
 
25
        var $uploadDir = '';
 
26
        var $uploadName = '';
 
27
        var $uploadIndex = '';
 
28
        var $maxSize = 0;
 
29
        var $seperator = "/";
 
30
        var $handler = '';
 
31
        var $handlerType = ''; //move , copy , data
 
32
        var $targetName = '';
 
33
        var $savedName = '';
 
34
        var $maxWidth = '';
 
35
        var $maxHeight = '';
 
36
        var $allowExt = array();
 
37
        var $allowMime = array();
 
38
        var $fileCHMOD = 0777;
 
39
        var $prefix = false;
 
40
        var $extension = false;
 
41
        var $error_code = 0;
 
42
        var $error_lang = array(
 
43
                        HTTPUPLOAD_ERROR_NO_FILE => "No file was submited.",
 
44
                        HTTPUPLOAD_ERROR_INI_SIZE => "The uploaded file exceeds the upload_max_filesize directive in php.ini.",
 
45
                        HTTPUPLOAD_ERROR_FORM_SIZE => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.",
 
46
                        HTTPUPLOAD_ERROR_SIZE => "The uploaded file exceeds the max file size directive that was specified in class setting.",
 
47
                        HTTPUPLOAD_ERROR_IMG => "The uploaded file exceeds the max image width or max image height directive that was specified in class setting.",
 
48
                        HTTPUPLOAD_ERROR_EXT => "File extension is not allowed.",
 
49
                        HTTPUPLOAD_ERROR_MIME => "File mime is not allowed.",
 
50
                        HTTPUPLOAD_ERROR_WRITE => "There was an error writing the file on this system. Maybe the file name you're trying to upload already exists, or your webserver cannot write to the file or directory. This is likely a permission problem - make sure the webserver can write to the upload directory..",
 
51
                        HTTPUPLOAD_ERROR_PARTIAL => "The uploaded file was only partially uploaded.",
 
52
        );
 
53
 
 
54
        /**
 
55
         * Set upload directory
 
56
         * @param string $dir upload directory
 
57
         **/
 
58
        function setuploaddir($dir) {
 
59
                $this->uploadDir = $dir;
 
60
        }
 
61
 
 
62
        /**
 
63
         * Set upload name
 
64
         * @param string $name $HTTP_POST_VARS[$name]
 
65
         **/
 
66
        function setuploadname($name) {
 
67
                $this->uploadName = $name;
 
68
        }
 
69
        
 
70
        /**
 
71
         * Set upload name index
 
72
         * @param string $name $HTTP_POST_VARS[$name][$index]
 
73
         **/
 
74
        function setuploadindex($index) {
 
75
                $this->uploadIndex = $index;
 
76
        }
 
77
 
 
78
        /**
 
79
         * Set upload filename
 
80
         * @param string $name File name
 
81
         **/
 
82
        function settargetfile($name) {
 
83
                $this->targetName = $name;
 
84
        }
 
85
        
 
86
        /**
 
87
         * Set upload image size
 
88
         * @param string $w Max image width
 
89
         * @param string $h Max image height
 
90
         **/
 
91
        function setimagemaxsize($w = null , $h = null) {
 
92
                $this->maxWidth = $w;
 
93
                $this->maxHeight = $h;
 
94
        }
 
95
 
 
96
        /**
 
97
         * Set upload mime filter
 
98
         * @param mixed $a Filter data
 
99
         * @param string $s Text seperator (for string filter data)
 
100
         **/
 
101
        function setallowmime($a , $s = '|') {
 
102
                if (!is_array($a)) {
 
103
                        if (strpos($a , $s) === false) {
 
104
                                $a = array($a);
 
105
                        } else {
 
106
                                $a = explode($s , $a);
 
107
                        }
 
108
                }
 
109
                $this->allowMime = array();
 
110
                foreach ($a as $val) {
 
111
                        $this->allowMime[] = strtolower(trim($val));
 
112
                }
 
113
        }
 
114
        
 
115
        /**
 
116
         * Set upload extension filter
 
117
         * @param mixed $a Filter data
 
118
         * @param string $s Text seperator (for string filter data)
 
119
         **/
 
120
        function setallowext($a , $s = '|') {
 
121
                if (!is_array($a)) {
 
122
                        if (strpos($a , $s) === false) {
 
123
                                $a = array($a);
 
124
                        } else {
 
125
                                $a = explode($s , $a);
 
126
                        }
 
127
                }
 
128
                $this->allowExt = array();
 
129
                foreach ($a as $val) {
 
130
                        $val = trim($val);
 
131
                        if (substr($val , 0 , 1) != ".") $val = ".$val";
 
132
                        $this->allowExt[] = strtolower($val);
 
133
                }
 
134
        }
 
135
        
 
136
        
 
137
        /**
 
138
         * Set max file size
 
139
         * @param int $size Max file size
 
140
         **/
 
141
        function setmaxsize($size) {
 
142
                $this->maxSize = intval($size);
 
143
        }
 
144
        
 
145
        function httpupload($dir = '' , $name = '' , $index = '') {
 
146
                $this->uploadDir = $dir;
 
147
                $this->uploadName = $name;
 
148
                $this->uploadIndex = $index;
 
149
        }
 
150
        
 
151
        function getuploadname() {
 
152
                $FILE = $this->getuploadinfo($this->uploadName , $this->uploadIndex);
 
153
                if ($FILE === false) return false;
 
154
                return @$FILE['name'];
 
155
        }
 
156
 
 
157
        function getuploadsize() {
 
158
                $FILE = $this->getuploadinfo($this->uploadName , $this->uploadIndex);
 
159
                if ($FILE === false) return false;
 
160
                return @$FILE['size'];
 
161
        }
 
162
        
 
163
        function getuploadtype() {
 
164
                $FILE = $this->getuploadinfo($this->uploadName , $this->uploadIndex);
 
165
                if ($FILE === false) return false;
 
166
                return @$FILE['type'];
 
167
        }
 
168
        
 
169
        function getuploadtmp() {
 
170
                $FILE = $this->getuploadinfo($this->uploadName , $this->uploadIndex);
 
171
                if ($FILE === false) return false;
 
172
                return @$FILE['tmp_name'];
 
173
        }
 
174
        
 
175
        function getsavedname($fullpath=true) {
 
176
                $FILE = $this->getuploadinfo($this->uploadName , $this->uploadIndex);
 
177
                if ($FILE === false || $this->savedName == '') return false;
 
178
                return ($fullpath ? $this->uploadDir . "/" : "") . $this->savedName;
 
179
        }
 
180
        
 
181
        function isempty() {
 
182
                $FILE = $this->getuploadinfo($this->uploadName , $this->uploadIndex);
 
183
                if ($FILE === false) return true;
 
184
                return ($FILE['size'] == 0);
 
185
        }
 
186
        
 
187
        function hasupload() {
 
188
                $FILE = $this->getuploadinfo($this->uploadName , $this->uploadIndex);
 
189
                if ($FILE === false) return false;
 
190
                return (isset($FILE['name']));
 
191
        }
 
192
        
 
193
        /**
 
194
         * Default file upload handler
 
195
         * @access private
 
196
         **/
 
197
        function processfile($b , $t , $mod , $overWrite = false) {
 
198
                $FILE = $this->getuploadinfo($this->uploadName , $this->uploadIndex);
 
199
                if ($FILE === false) return false;
 
200
                $p = '';
 
201
                $p2 = $FILE['tmp_name'];
 
202
                if (trim($b) == '') {
 
203
                        $p = $t;
 
204
                } else {
 
205
                        if (substr($b , strlen($b) - 1 , 1) != $this->seperator) $p = $b . $this->seperator;
 
206
                        else $p = $b;
 
207
                        $p .= $t;
 
208
                }
 
209
                if (file_exists($p)) {
 
210
                        //exist file , have to check
 
211
                        if (is_dir($p)) return false;
 
212
                        if (!$overWrite) return false;
 
213
                }
 
214
                if  (!@copy($p2 , $p)) return false;
 
215
                @chmod($p , $mod);
 
216
                $this->savedName = $t;
 
217
                return true;
 
218
        }
 
219
        
 
220
        /**
 
221
         * Check and Save upload file
 
222
         * @param bool $overWrite Overwrite existed file
 
223
         * @return bool
 
224
         **/
 
225
        function upload($overWrite = false) {
 
226
                $this->savedName = '';
 
227
                $this->set_error(0);
 
228
                if (!$this->hasUpload()) return $this->set_error(HTTPUPLOAD_ERROR_NO_FILE);
 
229
                $FILE = $this->getuploadinfo($this->uploadName , $this->uploadIndex);
 
230
                switch ($FILE['error']) {
 
231
                        case 1:
 
232
                                return $this->set_error(HTTPUPLOAD_ERROR_INI_SIZE);
 
233
                        break;
 
234
                        case 2:
 
235
                                return $this->set_error(HTTPUPLOAD_ERROR_FORM_SIZE);
 
236
                        break;
 
237
                        case 3:
 
238
                                return $this->set_error(HTTPUPLOAD_ERROR_PARTIAL);
 
239
                        break;
 
240
                        case 4:
 
241
                                return $this->set_error(HTTPUPLOAD_ERROR_NO_FILE);
 
242
                        break;
 
243
                }
 
244
                $ext = ".";
 
245
                if (!(strpos($FILE['name'] , ".")) === false) {
 
246
                        $ext = explode("." , $FILE['name']);
 
247
                        $ext = "." . $ext[count($ext) - 1];
 
248
                }
 
249
                $ext = strtolower($ext);
 
250
                //check max file size
 
251
                if (intval($this->maxSize) > 0 && $FILE['size'] > $this->maxSize) return $this->set_error(HTTPUPLOAD_ERROR_SIZE);
 
252
                //check extension
 
253
                if (is_array($this->allowExt) && count($this->allowExt) > 0 && !in_array($ext , $this->allowExt)) return $this->set_error(HTTPUPLOAD_ERROR_EXT);
 
254
                //check mime
 
255
                if (is_array($this->allowMime) && count($this->allowMime) > 0 && !in_array($FILE['type'] , $this->allowMime)) return $this->set_error(HTTPUPLOAD_ERROR_MIME);
 
256
                //check image size
 
257
                if (intval($this->maxWidth) > 0 || intval($this->maxHeight) > 0) {
 
258
                        $imageSize = @getimagesize($FILE['tmp_name']);
 
259
                        if ($imageSize === false) return false;
 
260
                        if (intval($this->maxWidth) > 0 && $imageSize[0] > intval($this->maxWidth)) return $this->set_error(HTTPUPLOAD_ERROR_IMG);
 
261
                        if (intval($this->maxHeight) > 0 && $imageSize[1] > intval($this->maxHeight)) return $this->set_error(HTTPUPLOAD_ERROR_IMG);
 
262
                }
 
263
                //process file
 
264
                if (trim($this->targetName) == '') {
 
265
                        //Self Generator
 
266
                        if ($this->prefix === false) {
 
267
                                $f = $FILE['name'];
 
268
                                if (!(strpos($f , ".") === false)) {
 
269
                                        $f = explode("." , $f);
 
270
                                        unset($f[count($f) -1]);
 
271
                                        $f = implode("." , $f);
 
272
                                }
 
273
                        } else {
 
274
                                $f = uniqid(trim($this->prefix));
 
275
                        }
 
276
                        if ($this->extension === false) {
 
277
                                if ($ext != '.') $f .= $ext;
 
278
                        } else {
 
279
                                if (substr($this->extension , 0 , 1) != ".") $this->extension = "." . $this->extension;
 
280
                                if (trim($this->extension) != '.') $f .= $this->extension;
 
281
                        }
 
282
                } else {
 
283
                        //User name
 
284
                        $f = trim($this->targetName);
 
285
                }
 
286
                $this->savedName = $f;
 
287
                //ok , now process copy
 
288
                if ($this->handlerType == '' || $this->handler == '') {
 
289
                        //process default handler
 
290
                        if ($this->processfile($this->uploadDir , $f , $this->fileCHMOD , $overWrite)) return $this->set_error(HTTPUPLOAD_ERROR_OK);
 
291
                        else return $this->set_error(HTTPUPLOAD_ERROR_WRITE);
 
292
                } else {
 
293
                        //process user handler
 
294
                        $b = $this->uploadDir;
 
295
                        if (trim($b) == '') {
 
296
                                $p = $f;
 
297
                        } else {
 
298
                                if (substr($b , strlen($b) - 1 , 1) != $this->seperator) $p = $b . $this->seperator;
 
299
                                else $p = $b;
 
300
                                $p .= $f;
 
301
                        }
 
302
                        $f = $this->handler;
 
303
                        switch (trim(strtolower($this->handlerType))) {
 
304
                                case 'copy':
 
305
                                case 'move':
 
306
                                        // function (src , tgt , CHMOD)
 
307
                                        /*
 
308
                                        if (is_array($f)) {
 
309
                                                return $f[0]->$f[1]($p , $FILE['tmp_name'] , $this->fileCHMOD);
 
310
                                        }
 
311
                                        else return $f($p , $FILE['tmp_name'] , $this->fileCHMOD);
 
312
                                        */
 
313
                                        if (@call_user_func($f , $p , $FILE['tmp_name'] , $this->fileCHMOD)) return $this->set_error(HTTPUPLOAD_ERROR_OK);
 
314
                                        else return $this->set_error(HTTPUPLOAD_ERROR_WRITE);
 
315
                                break;
 
316
                                case 'data':
 
317
                                        // function (targetFile , data , CHMOD)
 
318
                                        /*
 
319
                                        if (is_readable($FILE['tmp_name'])) $data = implode("" , file($FILE['tmp_name']));
 
320
                                        else return false;
 
321
                                        if (is_array($f)) {
 
322
                                                return $f[0]->$f[1]($p , $data , $this->fileCHMOD);
 
323
                                        }
 
324
                                        else return $f($p , $data , $this->fileCHMOD);
 
325
                                        */
 
326
                                        if (@call_user_func($f , $p , $data , $this->fileCHMOD)) return $this->set_error(HTTPUPLOAD_ERROR_OK);
 
327
                                        else return $this->set_error(HTTPUPLOAD_ERROR_WRITE);
 
328
                                break;
 
329
                                default:
 
330
                                        if ($this->processfile($this->uploadDir , $f , $this->fileCHMOD , $overWrite)) return $this->set_error(HTTPUPLOAD_ERROR_OK);
 
331
                                        else return $this->set_error(HTTPUPLOAD_ERROR_WRITE);
 
332
                        }
 
333
                }
 
334
        }
 
335
        
 
336
        //multi-file upload
 
337
        function upload_ex($name,$overwrite=false) {
 
338
                $FILES = $this->getuploadinfo($name,null);
 
339
                if (isset($FILES['name']) && is_array($FILES['name'])) {
 
340
                        $results = array();
 
341
                        $old_index = $this->uploadIndex;
 
342
                        $old_name = $this->uploadName;
 
343
                        
 
344
                        $this->uploadName = $name;
 
345
                        
 
346
                        foreach ($FILES['name'] as $index => $dummy) {
 
347
                                //handle each file
 
348
                                $this->uploadIndex = $index;
 
349
                                $this->upload($overwrite);
 
350
                                
 
351
                                $results[] = array(
 
352
                                        'error_code' => $this->error_code ,
 
353
                                        'name' => $this->getuploadname() ,
 
354
                                        'size' => $this->getuploadsize() ,
 
355
                                        'type' => $this->getuploadtype() , 
 
356
                                        'tmp_name' => $this->getuploadtmp() ,
 
357
                                        'file' => $this->getsavedname(false) ,
 
358
                                        'fullpath' => $this->getsavedname(true) ,
 
359
                                        'index' => $index ,
 
360
                                );
 
361
                        }
 
362
                        $this->uploadIndex = $old_index;
 
363
                        $this->uploadName = $old_name;
 
364
                        return $results;
 
365
                } else return false;
 
366
        }
 
367
        
 
368
        function &getuploadinfo($name , $index = '') {
 
369
                global $HTTP_POST_FILES;
 
370
                if ($index == '' && !($index === 0)) {
 
371
                        if (isset($HTTP_POST_FILES[$name])) {
 
372
                                return $HTTP_POST_FILES[$name];
 
373
                        }
 
374
                } else {
 
375
                        if (isset($HTTP_POST_FILES[$name]['name'][$index])) {
 
376
                                return array(
 
377
                                        'name' => $HTTP_POST_FILES[$name]['name'][$index],
 
378
                                        'tmp_name' => $HTTP_POST_FILES[$name]['tmp_name'][$index],
 
379
                                        'size' => $HTTP_POST_FILES[$name]['size'][$index],
 
380
                                        'type' => $HTTP_POST_FILES[$name]['type'][$index],
 
381
                                        'error' => $HTTP_POST_FILES[$name]['error'][$index],
 
382
                                );
 
383
                        }
 
384
                }
 
385
                return false;
 
386
        }
 
387
        
 
388
        function set_error($code) {
 
389
                $this->error_code = $code;
 
390
                if ($code != HTTPUPLOAD_ERROR_OK) return false;
 
391
                return true;
 
392
        }
 
393
        
 
394
        function get_error($code = null) {
 
395
                if ($code === null) $code = $this->error_code;
 
396
                return @$this->error_lang[$code];
 
397
        }
 
398
        
 
399
}
 
400
 
 
401
?>
 
 
b'\\ No newline at end of file'