00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00029 class FileAction extends ObjectAction
00030 {
00031 var $file;
00032 var $defaultSubAction = 'show';
00033
00037 function FileAction()
00038 {
00039 if ( $this->getRequestId() != 0 )
00040 {
00041 $this->file = new File( $this->getRequestId() );
00042 $this->file->load();
00043 Session::setObject( $this->file );
00044 }
00045 else
00046 {
00047 $this->file = Session::getObject();
00048 }
00049
00050 $this->lastModified( $this->file->lastchangeDate );
00051 }
00052
00053
00057 function replace()
00058 {
00059 $upload = new Upload();
00060
00061 $this->file->filename = $upload->filename;
00062 $this->file->extension = $upload->extension;
00063 $this->file->size = $upload->size;
00064 $this->file->save();
00065
00066 $this->file->value = $upload->value;
00067 $this->file->saveValue();
00068 $this->file->setTimestamp();
00069
00070
00071 $this->addNotice($this->file->getType(),$this->file->name,'VALUE_SAVED','ok');
00072 }
00073
00074
00075 function savevalue()
00076 {
00077 $this->file->value = $this->getRequestVar('value');
00078 $this->file->saveValue();
00079
00080 $this->addNotice($this->file->getType(),$this->file->name,'VALUE_SAVED','ok');
00081 $this->file->setTimestamp();
00082 }
00083
00084
00089 function saveprop()
00090 {
00091
00092 $this->file->filename = $this->getRequestVar('filename' );
00093 $this->file->name = $this->getRequestVar('name' );
00094 $this->file->extension = $this->getRequestVar('extension' );
00095 $this->file->desc = $this->getRequestVar('description');
00096
00097 $this->file->save();
00098 $this->file->setTimestamp();
00099 $this->addNotice($this->file->getType(),$this->file->name,'PROP_SAVED','ok');
00100 }
00101
00102
00107 function show()
00108 {
00109
00110 header('Content-Type: '.$this->file->mimeType() );
00111 header('X-File-Id: '.$this->file->fileid );
00112
00113
00114
00115
00116 header('Content-Disposition: inline; filename='.$this->file->filenameWithExtension() );
00117 header('Content-Transfer-Encoding: binary' );
00118 header('Content-Description: '.$this->file->name );
00119
00120 $this->file->write();
00121
00122
00123
00124 header('Content-Length: '.filesize($this->file->tmpfile()) );
00125
00126
00127 readfile( $this->file->tmpfile() );
00128 exit;
00129 }
00130
00131
00132 function imageFormat()
00133 {
00134 if ( ! function_exists( 'imagetypes' ) )
00135 return 0;
00136
00137 $ext = strtolower($this->file->extension);
00138 $types = imagetypes();
00139 $formats = array( 'gif' =>IMG_GIF,
00140 'jpg' =>IMG_JPG,
00141 'jpeg'=>IMG_JPG,
00142 'png' =>IMG_PNG );
00143
00144 if ( !isset($formats[$ext]) )
00145 return 0;
00146
00147 if ( $types & $formats[$ext] )
00148 return $formats[$ext];
00149
00150 return 0;
00151 }
00152
00153
00154
00155 function imageExt()
00156 {
00157 switch( $this->imageFormat() )
00158 {
00159 case IMG_GIF:
00160 return 'GIF';
00161 case IMG_JPG:
00162 return 'JPEG';
00163 case IMG_PNG:
00164 return 'PNG';
00165 }
00166 }
00167
00168
00169
00170 function imageFormats()
00171 {
00172 if ( ! function_exists( 'imagetypes' ) )
00173 return array();
00174
00175 $types = imagetypes();
00176 $formats = array( IMG_GIF => 'gif',
00177 IMG_JPG => 'jpeg',
00178 IMG_PNG => 'png' );
00179 $formats2 = $formats;
00180
00181 foreach( $formats as $b=>$f )
00182 if ( !($types & $b) )
00183 unset( $formats2[$b] );
00184
00185 return $formats2;
00186 }
00187
00188
00192 function resize()
00193 {
00194 $width = intval($this->getRequestVar('width' ));
00195 $height = intval($this->getRequestVar('height' ));
00196 $jpegcompression = $this->getRequestVar('jpeg_compression') ;
00197 $format = $this->getRequestVar('format' ) ;
00198 $factor = $this->getRequestVar('factor' ) ;
00199
00200 if ( $this->getRequestVar('type') == 'input' &&
00201 ! $this->hasRequestVar('width' ) &&
00202 ! $this->hasRequestVar('height') )
00203 {
00204 $this->addValidationError('width','INPUT_NEW_IMAGE_SIZE' );
00205 $this->addValidationError('height','');
00206 $this->callSubAction('size');
00207 return;
00208 }
00209
00210 if ( $this->hasRequestVar('copy') )
00211 {
00212
00213 $imageFile = new File($this->file->objectid);
00214 $imageFile->load();
00215 $imageFile->name = lang('copy_of').' '.$imageFile->name;
00216 $imageFile->desription = lang('copy_of').' '.$imageFile->description;
00217 $imageFile->filename = $imageFile->filename.'_resized_'.time();
00218 $imageFile->add();
00219 $imageFile->copyValueFromFile( $this->file->objectid );
00220 }
00221 else
00222 {
00223 $imageFile = $this->file;
00224 }
00225
00226 if ( $this->getRequestVar('type') == 'factor')
00227 {
00228 $width = 0;
00229 $height = 0;
00230 }
00231 else
00232 {
00233 $factor = 1;
00234 }
00235
00236 $imageFile->write();
00237
00238 $imageFile->imageResize( intval($width),intval($height),$factor,$this->imageFormat(),$format,$jpegcompression );
00239 $imageFile->setTimestamp();
00240 $imageFile->save();
00241 $imageFile->saveValue();
00242
00243 $this->addNotice($imageFile->getType(),$imageFile->name,'IMAGE_RESIZED','ok');
00244 }
00245
00246
00247 function prop()
00248 {
00249
00250 global $conf;
00251
00252 if ( $this->file->filename == $this->file->objectid )
00253 $this->file->filename = '';
00254
00255
00256 $this->setTemplateVars( $this->file->getProperties() );
00257
00258 $this->setTemplateVar('size',number_format($this->file->size/1000,0,',','.').' kB' );
00259 $this->setTemplateVar('full_filename',$this->file->full_filename());
00260
00261 if ( is_file($this->file->tmpfile()))
00262 {
00263 $this->setTemplateVar('cache_filename' ,$this->file->tmpfile());
00264 $this->setTemplateVar('cache_filemtime',@filemtime($this->file->tmpfile()));
00265 }
00266
00267
00268 $pages = $this->file->getDependentObjectIds();
00269
00270 $list = array();
00271 foreach( $pages as $id )
00272 {
00273 $o = new Object( $id );
00274 $o->load();
00275 $list[$id] = array();
00276 $list[$id]['url' ] = Html::url('main','page',$id);
00277 $list[$id]['name'] = $o->name;
00278 }
00279 asort( $list );
00280 $this->setTemplateVar('pages',$list);
00281 $this->setTemplateVar('edit_filename',$conf['filename']['edit']);
00282 }
00283
00284
00288 function edit()
00289 {
00290 global $conf;
00291
00292 $this->setTemplateVars( $this->file->getProperties() );
00293 }
00294
00295
00299 function upload()
00300 {
00301 }
00302
00303
00307 function editvalue()
00308 {
00309 global $conf;
00310
00311 $this->setTemplateVars( $this->file->getProperties() );
00312 $this->setTemplateVar('value',$this->file->loadValue());
00313 }
00314
00315
00319 function size()
00320 {
00321 $this->setTemplateVars( $this->file->getProperties() );
00322
00323 $format = $this->imageFormat();
00324
00325 if ( $format != 0 )
00326 $formats = $this->imageFormats();
00327 else
00328 $formats = array();
00329
00330 $sizes = array();
00331 foreach( array(10,25,50,75,100,125,150,175,200,250,300,350,400,500,600,800) as $s )
00332 $sizes[strval($s/100)] = $s.'%';
00333
00334 $jpeglist = array();
00335 for ($i=10; $i<=95; $i+=5)
00336 $jpeglist[$i]=$i.'%';
00337
00338 $this->setTemplateVar('factors' ,$sizes );
00339 $this->setTemplateVar('jpeglist' ,$jpeglist );
00340 $this->setTemplateVar('formats' ,$formats );
00341 $this->setTemplateVar('format' ,$format );
00342 $this->setTemplateVar('factor' ,1 );
00343
00344 $this->file->getImageSize();
00345 $this->setTemplateVar('width' ,$this->file->width );
00346 $this->setTemplateVar('height',$this->file->height );
00347 $this->setTemplateVar('type' ,'input' );
00348 }
00349
00350
00354 function extract()
00355 {
00356 $this->setTemplateVars( $this->file->getProperties() );
00357
00358 $imageFormat = $this->imageFormat();
00359 }
00360
00361
00365 function uncompress()
00366 {
00367 }
00368
00369
00373 function compress()
00374 {
00375 $formats = array();
00376 foreach( $this->getCompressionTypes() as $t )
00377 $formats[$t] = lang('compression_'.$t);
00378
00379 $this->setTemplateVar('formats' ,$formats );
00380 }
00381
00382
00386 function douncompress()
00387 {
00388 switch( $this->file->extension )
00389 {
00390 case 'gz':
00391 if ( $this->getRequestVar('replace') )
00392 {
00393 $this->file->value = gzinflate( substr($this->file->loadValue(),10));
00394 $this->file->parse_filename( $this->file->filename );
00395 $this->file->save();
00396 $this->file->saveValue();
00397 }
00398 else
00399 {
00400 $newFile = new File();
00401 $newFile->name = $this->file->name;
00402 $newFile->parentid = $this->file->parentid;
00403 $newFile->value = gzinflate( substr($this->file->loadValue(),10));
00404 $newFile->parse_filename( $this->file->filename );
00405 $newFile->add();
00406 }
00407
00408 break;
00409
00410 case 'bz2':
00411 if ( $this->getRequestVar('replace') )
00412 {
00413 $this->file->value = bzdecompress($this->file->loadValue());
00414 $this->file->parse_filename( $this->file->filename );
00415 $this->file->save();
00416 $this->file->saveValue();
00417 }
00418 else
00419 {
00420 $newFile = new File();
00421 $newFile->name = $this->file->name;
00422 $newFile->parentid = $this->file->parentid;
00423 $newFile->value = bzdecompress( $this->file->loadValue() );
00424 $newFile->parse_filename( $this->file->filename );
00425 $newFile->add();
00426 }
00427
00428 break;
00429
00430 default:
00431 die( 'cannot extract file with extension: '.$this->file->extension );
00432 }
00433 $this->callSubAction('edit');
00434 }
00435
00436
00437
00441 function doextract()
00442 {
00443 switch( $this->file->extension )
00444 {
00445 case 'tar':
00446 $folder = new Folder();
00447 $folder->parentid = $this->file->parentid;
00448 $folder->name = $this->file->name;
00449 $folder->filename = $this->file->filename;
00450 $folder->add();
00451
00452 $tar = new ArchiveTar();
00453 $tar->openTAR( $this->file->loadValue() );
00454
00455 foreach( $tar->files as $file )
00456 {
00457 $newFile = new File();
00458 $newFile->name = $file['name'];
00459 $newFile->parentid = $folder->objectid;
00460 $newFile->value = $file['file'];
00461 $newFile->parse_filename( $file['name'] );
00462 $newFile->lastchangeDate = $file['time'];
00463 $newFile->add();
00464
00465 $this->addNotice('file',$newFile->name,'ADDED');
00466 }
00467
00468 unset($tar);
00469
00470 break;
00471
00472 case 'zip':
00473
00474 $folder = new Folder();
00475 $folder->parentid = $this->file->parentid;
00476 $folder->name = $this->file->name;
00477 $folder->filename = $this->file->filename;
00478 $folder->description = $this->file->fullFilename;
00479 $folder->add();
00480
00481 $zip = new ArchiveUnzip();
00482 $zip->open( $this->file->loadValue() );
00483
00484 $lista = $zip->getList();
00485
00486 if(sizeof($lista)) foreach($lista as $fileName=>$trash){
00487
00488
00489 $newFile = new File();
00490 $newFile->name = basename($fileName);
00491 $newFile->description = 'Extracted: '.$this->file->fullFilename.' -> '.$fileName;
00492 $newFile->parentid = $folder->objectid;
00493 $newFile->parse_filename( basename($fileName) );
00494
00495 $newFile->value = $zip->unzip($fileName);
00496 $newFile->add();
00497
00498 $this->addNotice('file',$newFile->name,'ADDED');
00499 unset($newFile);
00500 }
00501
00502 $zip->close();
00503 unset($zip);
00504
00505 break;
00506
00507 default:
00508 die( 'cannot extract file with extension: '.$this->file->extension );
00509 }
00510 $this->callSubAction('edit');
00511 }
00512
00513
00514
00518 function docompress()
00519 {
00520 $format = $this->getRequestVar('format');
00521
00522 switch( $format )
00523 {
00524 case 'gz':
00525 if ( $this->getRequestVar('replace') )
00526 {
00527 $this->file->value = gzencode( $this->file->loadValue() );
00528 $this->file->parse_filename( $this->file->filename.'.'.$this->file->extension.'.gz' );
00529 $this->file->save();
00530 $this->file->saveValue();
00531
00532 }
00533 else
00534 {
00535 $newFile = new File();
00536 $newFile->name = $this->file->name;
00537 $newFile->parentid = $this->file->parentid;
00538 $newFile->value = gzencode( $this->file->loadValue() );
00539 $newFile->parse_filename( $this->file->filename.'.'.$this->file->extension.'.gz' );
00540 $newFile->add();
00541 }
00542
00543 break;
00544
00545 case 'bzip2':
00546 if ( $this->getRequestVar('replace') )
00547 {
00548 $this->file->value = bzcompress( $this->file->loadValue() );
00549 $this->file->parse_filename( $this->file->filename.'.'.$this->file->extension.'.bz2' );
00550 $this->file->save();
00551 $this->file->saveValue();
00552
00553 }
00554 else
00555 {
00556 $newFile = new File();
00557 $newFile->name = $this->file->name;
00558 $newFile->parentid = $this->file->parentid;
00559 $newFile->value = bzcompress( $this->file->loadValue() );
00560 $newFile->parse_filename( $this->file->filename.'.'.$this->file->extension.'.bz2' );
00561 $newFile->add();
00562 }
00563
00564 break;
00565 default:
00566 die( 'unknown extract type: '.$format );
00567 }
00568
00569 $this->callSubAction('edit');
00570 }
00571
00572
00576 function pub()
00577 {
00578 $this->forward('file_pub');
00579 }
00580
00581
00585 function pubnow()
00586 {
00587 $this->file->publish();
00588 $this->file->publish->close();
00589 $this->addNotice('file',$this->file->fullFilename,'PUBLISHED'.($this->file->publish->ok?'':'_ERROR'),$this->file->publish->ok,array(),$this->file->publish->log);
00590
00591 $this->callSubaction('pub');
00592 }
00593
00594
00595
00596 function getCompressionTypes()
00597 {
00598 $compressionTypes = array();
00599 if ( function_exists('gzencode' ) ) $compressionTypes[] = 'gz';
00600 if ( function_exists('gzencode' ) ) $compressionTypes[] = 'zip';
00601 if ( function_exists('bzipcompress') ) $compressionTypes[] = 'bz2';
00602 return $compressionTypes;
00603 }
00604
00605 function getArchiveTypes()
00606 {
00607 $archiveTypes = array();
00608 $archiveTypes[] = 'tar';
00609 $archiveTypes[] = 'zip';
00610 return $archiveTypes;
00611 }
00612
00613
00614
00615 function checkMenu( $name )
00616 {
00617 $archiveTypes = $this->getArchiveTypes();
00618 $compressionTypes = $this->getCompressionTypes();
00619
00620 switch( $name )
00621 {
00622 case 'uncompress':
00623 return in_array($this->file->extension,$compressionTypes);
00624
00625 case 'compress':
00626 return !in_array($this->file->extension,$compressionTypes);
00627
00628 case 'extract':
00629 return in_array($this->file->extension,$archiveTypes);
00630
00631 case 'size':
00632 return $this->file->isImage();
00633
00634 case 'editvalue':
00635 return substr($this->file->mimeType(),0,5)=='text/';
00636
00637 default:
00638 return true;
00639 }
00640 }
00641 }
00642
00643 ?>