00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 define('OR_FILE_DEFAULT_MIMETYPE','application/octet-stream');
00025
00026
00034 class File extends Object
00035 {
00036 var $fileid;
00037
00038 var $size = 0;
00039 var $value = '';
00040 var $extension = '';
00041 var $log_filenames = array();
00042 var $fullFilename = '';
00043 var $publish = null;
00044 var $mime_type = '';
00045 var $width = null;
00046 var $height = null;
00047
00048 var $tmpfile;
00049
00050
00051
00057 var $storeValueAsBase64 = false;
00058
00059
00060
00066 function File( $objectid='' )
00067 {
00068 global $conf,$SESS;
00069
00070 $db = Session::getDatabase();
00071 $this->storeValueAsBase64 = $db->conf['base64'];
00072
00073 $this->Object( $objectid );
00074 $this->isFile = true;
00075 }
00076
00077
00078
00084 function full_filename()
00085 {
00086 if ( !empty($this->fullFilename) )
00087 return $this->fullFilename;
00088
00089 $filename = parent::full_filename();
00090
00091 if ( !empty($this->extension) )
00092 $filename .= '.'.$this->extension;
00093
00094 $this->fullFilename = $filename;
00095 return $filename;
00096 }
00097
00098
00099
00105 function filenameWithExtension()
00106 {
00107 if ( $this->extension != '' )
00108 return $this->filename.'.'.$this->extension;
00109 else return $this->filename;
00110 }
00111
00112
00113
00119 function getProperties()
00120 {
00121 return array_merge( parent::getProperties(),
00122 array('full_filename'=>$this->fullFilename,
00123 'extension' =>$this->extension,
00124 'size' =>$this->size,
00125 'mimetype' =>$this->mimetype() ) );
00126 }
00127
00128
00129
00133 function getFileObjectIdsByExtension( $extension )
00134 {
00135 global $SESS;
00136 $db = db_connection();
00137
00138 $sqlquery = 'SELECT * FROM {t_object} ';
00139
00140 if ( $extension != '' )
00141 {
00142 $sqlquery .= " WHERE extension='";
00143
00144 $ext = explode(',',$extension);
00145 $sqlquery .= implode( "' OR extension='",$ext );
00146 $sqlquery .= "' AND is_file=1 AND projectid={projectid}";
00147 }
00148 else
00149 {
00150 $sqlquery .= " WHERE is_file=1 AND projectid={projectid}";
00151 }
00152
00153 $sql = new Sql( $sqlquery );
00154 $sql->setInt( 'projectid',$SESS['projectid'] );
00155
00156 return $db->getCol( $sql->query );
00157 }
00158
00159
00160
00167 function getObjectIdsByExtension( $extension )
00168 {
00169 $db = db_connection();
00170
00171 $sql = new Sql( 'SELECT {t_file}.objectid FROM {t_file} '.
00172 ' LEFT JOIN {t_object} '.
00173 ' ON {t_object}.id={t_file}.objectid'.
00174 ' WHERE {t_file}.extension={extension}'.
00175 ' AND {t_object}.projectid={projectid}' );
00176 $sql->setInt ( 'projectid',$this->projectid );
00177 $sql->setString( 'extension',$extension );
00178
00179 return $db->getCol( $sql->query );
00180 }
00181
00182
00183
00189 function mimeType()
00190 {
00191 if ( !empty( $this->mime_type ) )
00192 return $this->mime_type;
00193
00194 global $conf;
00195 $mime_types = $conf['mime-types'];
00196
00197 if ( !empty($this->extension))
00198 {
00199 $ext = $this->extension;
00200 }
00201 else
00202 {
00203 $pos = strrpos($this->filename,'.');
00204 if ( $pos === false )
00205 $ext = '';
00206 else
00207 $ext = substr($this->filename,$pos+1);
00208 }
00209
00210 $ext = strtolower($ext);
00211
00212 if ( !empty($mime_types[$ext]) )
00213 $this->mime_type = $mime_types[$ext];
00214 else
00215
00216 $this->mime_type = OR_FILE_DEFAULT_MIMETYPE;
00217
00218 return( $this->mime_type );
00219 }
00220
00221
00222
00227 function getImageSize()
00228 {
00229 if ( is_null($this->width) )
00230 {
00231 $this->write();
00232
00233
00234 $size = getimagesize( $this->tmpfile() );
00235
00236
00237 $this->width = $size[0];
00238 $this->height = $size[1];
00239 }
00240 }
00241
00242
00243
00256 function imageResize( $newWidth,$newHeight,$factor,$oldformat,$newformat,$jpegquality )
00257 {
00258 global $conf;
00259
00260 $this->write();
00261
00262
00263 $size = getimagesize( $this->tmpfile() );
00264
00265
00266 $oldWidth = $size[0];
00267 $oldHeight = $size[1];
00268 $aspectRatio = $oldHeight / $oldWidth;
00269
00270
00271 if ( $newWidth == 0 && $newHeight == 0)
00272 {
00273 if ( $factor != 0 && $factor != 1 )
00274 {
00275 $newWidth = $oldWidth * $factor;
00276 $newHeight = $oldHeight * $factor;
00277 $resizing = true;
00278 }
00279 else
00280 {
00281 $newWidth = $oldWidth;
00282 $newHeight = $oldHeight;
00283 $resizing = false;
00284 }
00285 }
00286 else
00287 {
00288 $resizing = true;
00289 }
00290
00291
00292
00293 if ( $newWidth == 0 )
00294 $newWidth = $newHeight / $aspectRatio;
00295
00296 if ( $newHeight == 0 )
00297 $newHeight = $newWidth * $aspectRatio;
00298
00299
00300 switch( $oldformat )
00301 {
00302 case IMG_GIF:
00303
00304 $oldImage = ImageCreateFromGIF( $this->tmpfile );
00305 break;
00306
00307 case IMG_JPG:
00308
00309 $oldImage = ImageCreateFromJPEG($this->tmpfile);
00310 break;
00311
00312 case IMG_PNG:
00313
00314 $oldImage = imagecreatefrompng($this->tmpfile);
00315 break;
00316
00317 default:
00318 die('unsupported image format "'.$this->extension.'", cannot load image. resize failed');
00319 }
00320
00321
00322 global $conf;
00323 $hasTrueColor = $conf['image']['truecolor'];
00324
00325 switch( $newformat )
00326 {
00327 case IMG_GIF:
00328
00329 if ( $resizing )
00330 {
00331 $newImage = ImageCreate($newWidth,$newHeight);
00332 ImageCopyResized($newImage,$oldImage,0,0,0,0,$newWidth,
00333 $newHeight,$oldWidth,$oldHeight);
00334 }
00335 else
00336 {
00337 $newImage = &$oldImage;
00338 }
00339
00340 ImageGIF($newImage, $this->tmpfile() );
00341 $this->extension = 'gif';
00342
00343 break;
00344
00345 case IMG_JPG:
00346
00347 if ( !$resizing )
00348 {
00349 $newImage = &$oldImage;
00350 }
00351 elseif ( $hasTrueColor )
00352 {
00353
00354 $newImage = imageCreateTrueColor( $newWidth,$newHeight );
00355 ImageCopyResampled($newImage,$oldImage,0,0,0,0,$newWidth,
00356 $newHeight,$oldWidth,$oldHeight);
00357 }
00358 else
00359 {
00360
00361 $newImage = ImageCreate($newWidth,$newHeight);
00362
00363 ImageCopyResized($newImage,$oldImage,0,0,0,0,$newWidth,
00364 $newHeight,$oldWidth,$oldHeight);
00365 }
00366
00367 ImageJPEG($newImage, $this->tmpfile,$jpegquality );
00368 $this->extension = 'jpeg';
00369
00370 break;
00371
00372 case IMG_PNG:
00373
00374 if ( !$resizing )
00375 {
00376 $newImage = &$oldImage;
00377 }
00378 elseif ( $hasTrueColor )
00379 {
00380
00381 $newImage = imageCreateTrueColor( $newWidth,$newHeight );
00382
00383 ImageCopyResampled($newImage,$oldImage,0,0,0,0,$newWidth,
00384 $newHeight,$oldWidth,$oldHeight);
00385 }
00386 else
00387 {
00388
00389 $newImage = ImageCreate($newWidth,$newHeight);
00390
00391 ImageCopyResized($newImage,$oldImage,0,0,0,0,$newWidth,
00392 $newHeight,$oldWidth,$oldHeight);
00393 }
00394
00395 imagepng( $newImage,$this->tmpfile() );
00396 $this->extension = 'png';
00397
00398 break;
00399
00400 default:
00401 die('unsupported image format "'.$newformat.'", cannot resize');
00402 }
00403
00404 $f = fopen( $this->tmpfile(), "r" );
00405 $this->value = fread( $f,filesize($this->tmpfile()) );
00406 fclose( $f );
00407
00408 imagedestroy( $oldImage );
00409
00410 }
00411
00412
00413
00414 function load()
00415 {
00416 $db = db_connection();
00417
00418 $sql = new Sql( 'SELECT id,extension,size'.
00419 ' FROM {t_file}'.
00420 ' WHERE objectid={objectid}' );
00421 $sql->setInt( 'objectid',$this->objectid );
00422 $row = $db->getRow( $sql->query );
00423
00424 if ( count($row)!=0 )
00425 {
00426 $this->fileid = $row['id' ];
00427 $this->extension = $row['extension'];
00428 $this->size = $row['size' ];
00429 }
00430
00431 $this->objectLoad();
00432 }
00433
00434
00435
00436 function delete()
00437 {
00438 $db = db_connection();
00439
00440
00441 $sql = new Sql( 'DELETE FROM {t_file} '.
00442 ' WHERE objectid={objectid}' );
00443 $sql->setInt( 'objectid',$this->objectid );
00444 $db->query( $sql->query );
00445
00446 $this->objectDelete();
00447 }
00448
00449
00453 function isImage()
00454 {
00455 return substr($this->mimeType(),0,6)=='image/';
00456 }
00457
00458
00459 function extension()
00460 {
00461 if ($this->extension != '')
00462 return $this->extension;
00463
00464 $this->load();
00465 return $this->extension;
00466 }
00467
00468
00469
00470 function parse_filename($filename)
00471 {
00472 $filename = basename($filename);
00473
00474 $p = strrpos($filename, '.');
00475 if ($p !== false)
00476 {
00477 $this->extension = substr($filename, $p +1);
00478 $this->filename = substr($filename, 0, $p);
00479 }
00480 else
00481 {
00482 $this->extension = '';
00483 $this->filename = $filename;
00484 }
00485 }
00486
00487
00488 function save()
00489 {
00490 global $SESS;
00491 $db = db_connection();
00492
00493 $sql = new Sql( <<<EOF
00494 UPDATE {t_file} SET
00495 size = {size},
00496 extension = {extension}
00497 WHERE objectid={objectid}
00498 EOF
00499 );
00500 $sql->setString('size' ,$this->size );
00501 $sql->setString('extension',$this->extension );
00502 $sql->setString('objectid' ,$this->objectid );
00503 $db->query( $sql->query );
00504
00505 $this->objectSave();
00506 }
00507
00508
00513 function copyValueFromFile( $otherfileid )
00514 {
00515 $of = new File( $otherfileid );
00516 $this->value = $of->loadValue();
00517 $this->saveValue();
00518 }
00519
00520
00524 function loadValue()
00525 {
00526 if ( is_file($this->tmpfile()))
00527 return implode('',file($this->tmpfile()));
00528
00529 $db = db_connection();
00530
00531 $sql = new Sql( 'SELECT size,value'.
00532 ' FROM {t_file}'.
00533 ' WHERE objectid={objectid}' );
00534 $sql->setInt( 'objectid',$this->objectid );
00535 $row = $db->getRow( $sql->query );
00536
00537 if ( count($row) != 0 )
00538 {
00539 $this->value = $row['value'];
00540 $this->size = $row['size' ];
00541 }
00542
00543 if ( $this->storeValueAsBase64 )
00544 $this->value = base64_decode( $this->value );
00545
00546
00547 $f = fopen( $this->tmpfile(),'w' );
00548 fwrite( $f,$this->value );
00549 fclose( $f );
00550
00551 return $this->value;
00552 }
00553
00554
00558 function saveValue( $value = '' )
00559 {
00560 $db = db_connection();
00561
00562 $sql = new Sql( 'UPDATE {t_file}'.
00563 ' SET value={value}, '.
00564 ' size={size} '.
00565 ' WHERE objectid={objectid}' );
00566 $sql->setString( 'objectid' ,$this->objectid );
00567 $sql->setInt ( 'size' ,strlen($this->value) );
00568
00569 if ( $this->storeValueAsBase64 )
00570 $sql->setString( 'value',base64_encode($this->value) );
00571 else
00572 $sql->setString( 'value',$this->value );
00573
00574 $db->query( $sql->query );
00575 }
00576
00577
00581 function write()
00582 {
00583 if ( !is_file($this->tmpfile()) )
00584 $this->loadValue();
00585 }
00586
00587
00588 function add()
00589 {
00590 $db = db_connection();
00591
00592 $this->objectAdd();
00593
00594 $sql = new Sql('SELECT MAX(id) FROM {t_file}');
00595 $this->fileid = intval($db->getOne($sql->query))+1;
00596
00597 $sql = new Sql('INSERT INTO {t_file}'.
00598 ' (id,objectid,extension,size,value)'.
00599 " VALUES( {fileid},{objectid},{extension},0,'' )" );
00600 $sql->setInt ('fileid' ,$this->fileid );
00601 $sql->setInt ('objectid' ,$this->objectid );
00602 $sql->setString('extension',$this->extension );
00603
00604 $db->query( $sql->query );
00605
00606 $this->saveValue();
00607 }
00608
00609
00610 function publish()
00611 {
00612 if ( ! is_object($this->publish) )
00613 $this->publish = new Publish();
00614
00615 $this->write();
00616 $this->publish->copy( $this->tmpfile(),$this->full_filename() );
00617
00618 $this->publish->publishedObjects[] = $this->getProperties();
00619 }
00620
00621
00625 function tmpfile()
00626 {
00627 if ( $this->tmpfile == '' )
00628 {
00629 $db = db_connection();
00630 $this->tmpfile = $this->getTempDir().'/openrat_db'.$db->id.'_'.$this->objectid.'.tmp';
00631 }
00632 return $this->tmpfile;
00633 }
00634
00635
00636 function setTimestamp()
00637 {
00638 @unlink( $this->tmpfile() );
00639
00640 parent::setTimestamp();
00641 }
00642
00643 }
00644
00645 ?>