1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11:
12: define("XCUBE_FORMFILE_CHMOD", 0644);
13:
14: 15: 16: 17: 18: 19: 20: 21: 22:
23: class XCube_FormFile
24: {
25: var $mName=null;
26:
27: var $mKey = null;
28:
29: var $mContentType=null;
30:
31: var $mFileName=null;
32: var $mFileSize=0;
33:
34: var $_mTmpFileName=null;
35:
36: var $mUploadFileFlag=false;
37:
38: function XCube_FormFile($name = null, $key = null)
39: {
40: $this->mName = $name;
41: $this->mKey = $key;
42: }
43:
44: 45: 46:
47: function fetch()
48: {
49: if($this->mName && isset($_FILES[$this->mName])) {
50: if ($this->mKey != null) {
51: $this->setFileName($_FILES[$this->mName]['name'][$this->mKey]);
52: $this->setContentType($_FILES[$this->mName]['type'][$this->mKey]);
53: $this->setFileSize($_FILES[$this->mName]['size'][$this->mKey]);
54: $this->_mTmpFileName = $_FILES[$this->mName]['tmp_name'][$this->mKey];
55: }
56: else {
57: $this->setFileName($_FILES[$this->mName]['name']);
58: $this->setContentType($_FILES[$this->mName]['type']);
59: $this->setFileSize($_FILES[$this->mName]['size']);
60: $this->_mTmpFileName = $_FILES[$this->mName]['tmp_name'];
61: }
62:
63: if($this->getFileSize()>0)
64: $this->mUploadFileFlag=true;
65: }
66: }
67:
68: function hasUploadFile()
69: {
70: return $this->mUploadFileFlag;
71: }
72:
73: 74: 75: 76:
77: function getContentType()
78: {
79: return $this->mContentType;
80: }
81:
82: function getFileData()
83: {
84:
85: }
86:
87: 88: 89: 90:
91: function getFileName()
92: {
93: return $this->mFileName;
94: }
95:
96: 97: 98: 99:
100: function getFileSize()
101: {
102: return $this->mFileSize;
103: }
104:
105: 106: 107: 108:
109: function getExtension()
110: {
111: $ret = null;
112: $filename=$this->getFileName();
113: if (preg_match("/\.([a-z\.]+)$/i", $filename, $match)) {
114: $ret=$match[1];
115: }
116:
117: return $ret;
118: }
119:
120: 121: 122: 123:
124: function setExtension($ext)
125: {
126: $filename=$this->getFileName();
127: if(preg_match("/(.+)\.\w+$/",$filename,$match))
128: $this->setFileName($match[1].".${ext}");
129: }
130:
131: 132: 133: 134:
135: function setContentType($contenttype)
136: {
137: $this->mContentType=$contenttype;
138: }
139:
140: 141: 142: 143:
144: function setFileName($filename)
145: {
146: $this->mFileName = $filename;
147: }
148:
149: 150: 151: 152:
153: function setFileSize($filesize)
154: {
155: $this->mFileSize = $filesize;
156: }
157:
158: 159: 160: 161:
162: function setBodyName($bodyname)
163: {
164: $this->setFileName($bodyname.".".$this->getExtension());
165: }
166:
167: 168: 169: 170:
171: function getBodyName()
172: {
173: if(preg_match("/(.+)\.\w+$/",$this->getFileName(),$match)) {
174: return $match[1];
175: }
176:
177: return null;
178: }
179:
180: 181: 182: 183: 184:
185: function setRandomToBodyName($prefix,$salt='')
186: {
187: $filename = $prefix . $this->_getRandomString($salt) . "." . $this->getExtension();
188: $this->setFileName($filename);
189: }
190:
191: 192: 193: 194: 195:
196: function setRandomToFilename($prefix,$salt='')
197: {
198: $filename = $prefix . $this->_getRandomString($salt);
199: $this->setFileName($filename);
200: }
201:
202: 203: 204: 205: 206:
207: function _getRandomString($salt='')
208: {
209: if (empty($salt)) {
210: $root=&XCube_Root::getSingleton();
211: $salt = $root->getSiteConfig('Cube', 'Salt');
212: }
213: srand( microtime() *1000000);
214: return md5($salt . rand());
215: }
216:
217: 218: 219: 220: 221: 222: 223:
224: function saveAs($file)
225: {
226:
227: $destFile = "";
228: if(preg_match("#\/$#",$file)) {
229: $destFile = $file . $this->getFileName();
230: }
231: elseif(is_dir($file)) {
232: $destFile = $file . "/" . $this->getFileName();
233: }
234: else {
235: $destFile = $file;
236: }
237:
238: $ret = move_uploaded_file($this->_mTmpFileName, $destFile);
239:
240:
241:
242: @chmod($destFile, XCUBE_FORMFILE_CHMOD);
243:
244: return $ret;
245: }
246:
247: 248: 249: 250: 251: 252: 253: 254: 255:
256: function saveAsRandBody($dir,$prefix='',$salt='')
257: {
258: $this->setRandomToBodyName($prefix,$salt);
259: return $this->saveAs($dir);
260: }
261:
262: 263: 264: 265: 266: 267: 268: 269: 270:
271: function saveAsRand($dir,$prefix='',$salt='')
272: {
273: $this->setRandomToFileName($prefix,$salt);
274: return $this->saveAs($dir);
275: }
276: }
277:
278: 279: 280:
281: class XCube_FormImageFile extends XCube_FormFile
282: {
283: function fetch()
284: {
285: parent::fetch();
286:
287: if ($this->hasUploadFile()) {
288: if (!$this->_checkFormat()) {
289: $this->mUploadFileFlag = false;
290: }
291: }
292: }
293:
294: 295: 296: 297:
298: function getWidth()
299: {
300: list($width,$height,$type,$attr)=getimagesize($this->_mTmpFileName);
301: return $width;
302: }
303:
304: 305: 306: 307:
308: function getHeight()
309: {
310: list($width,$height,$type,$attr)=getimagesize($this->_mTmpFileName);
311: return $height;
312: }
313:
314: 315: 316: 317: 318:
319: function _checkFormat()
320: {
321: if(!$this->hasUploadFile())
322: return false;
323:
324: list($width,$height,$type,$attr)=getimagesize($this->_mTmpFileName);
325:
326: switch($type) {
327: case IMAGETYPE_GIF:
328: $this->setExtension("gif");
329: break;
330:
331: case IMAGETYPE_JPEG:
332: $this->setExtension("jpg");
333: break;
334:
335: case IMAGETYPE_PNG:
336: $this->setExtension("png");
337: break;
338:
339: default:
340: return false;
341: }
342:
343: return true;
344: }
345: }
346:
347: ?>