XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
XCube_FormFile.class.php
1<?php
18
19//The default umask for the root user is 022 result into
20// default directory permissions are 755 and default file permissions are 644.
21
22define("XCUBE_FORMFILE_PREVMASK", "0022");
23define("XCUBE_FORMFILE_CHMOD", "0022");
24
26 public $mName;
27
28 public $mKey;
29
30 public $mContentType;
31
32 public $mFileName;
33 public $mFileSize = 0;
34
35 public $_mTmpFileName;
36
37 public $mUploadFileFlag = false;
38
39 public function __construct( $name = null, $key = null ) {
40 $this->mName = $name;
41 $this->mKey = $key;
42 }
43
47 public function fetch() {
48 if ( $this->mName && isset( $_FILES[ $this->mName ] ) ) {
49 if ( null !== $this->mKey ) {
50 $this->setFileName( $_FILES[ $this->mName ]['name'][ $this->mKey ] );
51 $this->setContentType( $_FILES[ $this->mName ]['type'][ $this->mKey ] );
52 $this->setFileSize( $_FILES[ $this->mName ]['size'][ $this->mKey ] );
53 $this->_mTmpFileName = $_FILES[ $this->mName ]['tmp_name'][ $this->mKey ];
54 } else {
55 $this->setFileName( $_FILES[ $this->mName ]['name'] );
56 $this->setContentType( $_FILES[ $this->mName ]['type'] );
57 $this->setFileSize( $_FILES[ $this->mName ]['size'] );
58 $this->_mTmpFileName = $_FILES[ $this->mName ]['tmp_name'];
59 }
60
61 if ( $this->getFileSize() > 0 ) {
62 $this->mUploadFileFlag = true;
63 }
64 }
65 }
66
67 public function hasUploadFile() {
68 return $this->mUploadFileFlag;
69 }
70
75 public function getContentType() {
76 return $this->mContentType;
77 }
78
79 public function getFileData() {
80 // Now, implementing.
81 }
82
87 public function getFileName() {
88 return $this->mFileName;
89 }
90
95 public function getFileSize() {
96 return $this->mFileSize;
97 }
98
103 public function getExtension() {
104 $ret = null;
105 $filename = $this->getFileName();
106 if ( preg_match( "/\.([a-z0-9\.]+)$/i", $filename, $match ) ) {
107 $ret = $match[1];
108 }
109
110 return $ret;
111 }
112
120 public function setExtension( $ext ) {
121 $filename = $this->getFileName();
122 if ( preg_match( "/(.+)\.\w+$/", $filename, $match ) ) {
123 $this->setFileName( $match[1] . ".{$ext}" );
124 }
125 }
126
132 public function setContentType( $contenttype ) {
133 $this->mContentType = $contenttype;
134 }
135
141 public function setFileName( $filename ) {
142 $this->mFileName = $filename;
143 }
144
150 public function setFileSize( $filesize ) {
151 $this->mFileSize = $filesize;
152 }
153
159 public function setBodyName( $bodyname ) {
160 $this->setFileName( $bodyname . '.' . $this->getExtension() );
161 }
162
167 public function getBodyName() {
168 if ( preg_match( "/(.+)\.\w+$/", $this->getFileName(), $match ) ) {
169 return $match[1];
170 }
171
172 return null;
173 }
174
181 public function setRandomToBodyName( $prefix, $salt = '' ) {
182 $filename = $prefix . $this->_getRandomString( $salt ) . '.' . $this->getExtension();
183 $this->setFileName( $filename );
184 }
185
192 public function setRandomToFilename( $prefix, $salt = '' ) {
193 $filename = $prefix . $this->_getRandomString( $salt );
194 $this->setFileName( $filename );
195 }
196
203 public function _getRandomString( $salt = '' ) {
204 if ( empty( $salt ) ) {
205 $root =& XCube_Root::getSingleton();
206 $salt = $root->getSiteConfig( 'Cube', 'Salt' );
207 }
208
209 mt_srand( microtime(true) * 10000 );
210
211 return md5( $salt . random_int(0, mt_getrandmax()) );
212 }
213
222 public function saveAs( $file ) {
223 $destFile = '';
224 if ( preg_match( "#\/$#", $file ) ) {
225 $destFile = $file . $this->getFileName();
226 } elseif ( is_dir( $file ) ) {
227 $destFile = $file . '/' . $this->getFileName();
228 } else {
229 $destFile = $file;
230 }
231
232 $ret = move_uploaded_file( $this->_mTmpFileName, $destFile );
233
234 // $prevMask = @umask(XCUBE_FORMFILE_PREVMASK);
235 // @umask($prevMask);
236 @chmod( $destFile, XCUBE_FORMFILE_CHMOD );
237
238 return $ret;
239 }
240
252 public function saveAsRandBody( $dir, $prefix = '', $salt = '' ) {
253 $this->setRandomToBodyName( $prefix, $salt );
254
255 return $this->saveAs( $dir );
256 }
257
269 public function saveAsRand( $dir, $prefix = '', $salt = '' ) {
270 $this->setRandomToFileName( $prefix, $salt );
271
272 return $this->saveAs( $dir );
273 }
274}
275
280 public function fetch() {
281 parent::fetch();
282
283 if ( $this->hasUploadFile() && ! $this->_checkFormat() ) {
284 $this->mUploadFileFlag = false;
285 }
286 }
287
292 public function getWidth() {
293 [ $width, $height, $type, $attr ] = getimagesize( $this->_mTmpFileName );
294
295 return $width;
296 }
297
302 public function getHeight() {
303 [ $width, $height, $type, $attr ] = getimagesize( $this->_mTmpFileName );
304
305 return $height;
306 }
307
314 public function _checkFormat() {
315 if ( ! $this->hasUploadFile() ) {
316 return false;
317 }
318
319 [ $width, $height, $type, $attr ] = getimagesize( $this->_mTmpFileName );
320
321 switch ( $type ) {
322 case IMAGETYPE_GIF:
323 $this->setExtension( 'gif' );
324 break;
325
326 case IMAGETYPE_JPEG:
327 $this->setExtension( 'jpg' );
328 break;
329
330 case IMAGETYPE_PNG:
331 $this->setExtension( 'png' );
332 break;
333
334 default:
335 return false;
336 }
337
338 return true;
339 }
340}
setRandomToFilename( $prefix, $salt='')
saveAsRand( $dir, $prefix='', $salt='')
_getRandomString( $salt='')
Generate random string.
setRandomToBodyName( $prefix, $salt='')
saveAsRandBody( $dir, $prefix='', $salt='')
setContentType( $contenttype)