[ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 <?php 2 // 3 // Definition of eZFSFileHandler class 4 // 5 // Created on: <09-Mar-2006 16:40:46 vs> 6 // 7 // SOFTWARE NAME: eZ publish 8 // SOFTWARE RELEASE: 3.9.0 9 // BUILD VERSION: 17785 10 // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS 11 // SOFTWARE LICENSE: GNU General Public License v2.0 12 // NOTICE: > 13 // This program is free software; you can redistribute it and/or 14 // modify it under the terms of version 2.0 of the GNU General 15 // Public License as published by the Free Software Foundation. 16 // 17 // This program is distributed in the hope that it will be useful, 18 // but WITHOUT ANY WARRANTY; without even the implied warranty of 19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 // GNU General Public License for more details. 21 // 22 // You should have received a copy of version 2.0 of the GNU General 23 // Public License along with this program; if not, write to the Free 24 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 25 // MA 02110-1301, USA. 26 // 27 // 28 29 /*! \file ezfsfilehandler.php 30 */ 31 32 require_once ( 'lib/ezutils/classes/ezdebugsetting.php' ); 33 34 class eZFSFileHandler 35 { 36 /** 37 * Constructor. 38 * 39 * $filePath File path. If specified, file metadata is fetched in the constructor. 40 */ 41 function eZFSFileHandler( $filePath = false ) 42 { 43 eZDebugSetting::writeDebug( 'kernel-clustering', "fs::ctor( '$filePath' )" ); 44 $this->metaData['name'] = $filePath; 45 $this->loadMetaData(); 46 } 47 48 /*! 49 \public 50 Load file meta information. 51 */ 52 function loadMetaData() 53 { 54 if ( $this->metaData['name'] !== false ) 55 { 56 // fill $this->metaData 57 $filePath = $this->metaData['name']; 58 eZDebug::accumulatorStart( 'dbfile', false, 'dbfile' ); 59 $this->metaData = @stat( $filePath ); 60 eZDebug::accumulatorStop( 'dbfile' ); 61 $this->metaData['name'] = $filePath; 62 } 63 } 64 65 /** 66 * Fetches file from db and saves it in FS under the same name. 67 * 68 * In case of fetching from filesystem does nothing. 69 * 70 * \public 71 * \static 72 */ 73 function fileFetch( $filePath ) 74 { 75 eZDebugSetting::writeDebug( 'kernel-clustering', "fs::fileFetch( '$filePath' )" ); 76 } 77 78 /** 79 * Fetches file from db and saves it in FS under the same name. 80 * 81 * In case of fetching from filesystem does nothing. 82 * 83 * \public 84 */ 85 function fetch() 86 { 87 $filePath = $this->metaData['name']; 88 eZDebugSetting::writeDebug( 'kernel-clustering', "fs::fetch( '$filePath' )" ); 89 } 90 91 /** 92 * Store file. 93 * 94 * In case of storing to filesystem does nothing. 95 * 96 * \public 97 * \static 98 * \param $filePath Path to the file being stored. 99 * \param $scope Means something like "file category". May be used to clean caches of a certain type. 100 * \param $delete true if the file should be deleted after storing. 101 */ 102 function fileStore( $filePath, $scope = false, $delete = false, $datatype = false ) 103 { 104 $delete = (int) $delete; 105 eZDebugSetting::writeDebug( 'kernel-clustering', "fs::fileStore( '$filePath' )" ); 106 } 107 108 /** 109 * Store file contents. 110 * 111 * \public 112 * \static 113 */ 114 function fileStoreContents( $filePath, $contents, $scope = false, $datatype = false ) 115 { 116 eZDebugSetting::writeDebug( 'kernel-clustering', "fs::fileStoreContents( '$filePath' )" ); 117 118 eZDebug::accumulatorStart( 'dbfile', false, 'dbfile' ); 119 120 if ( !( $fh = fopen( $filePath, 'w' ) ) ) 121 { 122 eZDebug::writeError( "Cannot open file '$filePath'", 'ezfsfilehandler::fileStoreContents()' ); 123 return false; 124 } 125 126 if ( fwrite( $fh, $contents ) === false ) 127 { 128 eZDebug::writeError( "Cannot write to '$filePath'", 'ezfsfilehandler::fileStoreContents()' ); 129 return false; 130 } 131 132 fclose( $fh ); 133 134 eZDebug::accumulatorStop( 'dbfile' ); 135 } 136 137 /** 138 * Store file contents. 139 * 140 * \public 141 * \static 142 */ 143 function storeContents( $contents, $scope = false, $datatype = false ) 144 { 145 $filePath = $this->metaData['name']; 146 147 eZDebugSetting::writeDebug( 'kernel-clustering', "fs::storeContents( '$filePath' )" ); 148 149 eZDebug::accumulatorStart( 'dbfile', false, 'dbfile' ); 150 151 include_once ( 'lib/ezfile/classes/ezfile.php' ); 152 eZFile::create( basename( $filePath ), dirname( $filePath ), $contents ); 153 154 eZDebug::accumulatorStop( 'dbfile' ); 155 } 156 157 /** 158 * Returns file contents. 159 * 160 * \public 161 * \static 162 * \return contents string, or false in case of an error. 163 */ 164 function fileFetchContents( $filePath ) 165 { 166 eZDebugSetting::writeDebug( 'kernel-clustering', "fs::fileFetchContents( '$filePath' )" ); 167 168 eZDebug::accumulatorStart( 'dbfile', false, 'dbfile' ); 169 $rslt = file_get_contents( $filePath ); 170 eZDebug::accumulatorStop( 'dbfile' ); 171 172 return $rslt; 173 } 174 175 /** 176 * Returns file contents. 177 * 178 * \public 179 * \return contents string, or false in case of an error. 180 */ 181 function fetchContents() 182 { 183 $filePath = $this->metaData['name']; 184 eZDebugSetting::writeDebug( 'kernel-clustering', "fs::fetchContents( '$filePath' )" ); 185 186 eZDebug::accumulatorStart( 'dbfile', false, 'dbfile' ); 187 $rslt = file_get_contents( $filePath ); 188 eZDebug::accumulatorStop( 'dbfile' ); 189 190 return $rslt; 191 } 192 193 194 /** 195 * Returns file metadata. 196 * 197 * \public 198 */ 199 function stat() 200 { 201 eZDebugSetting::writeDebug( 'kernel-clustering', "fs::stat()" ); 202 return $this->metaData; 203 } 204 205 /** 206 * Returns file size. 207 * 208 * \public 209 */ 210 function size() 211 { 212 eZDebugSetting::writeDebug( 'kernel-clustering', "fs::size()" ); 213 return isset( $this->metaData['size'] ) ? $this->metaData['size'] : null; 214 } 215 216 /** 217 * Returns file modification time. 218 * 219 * \public 220 */ 221 function mtime() 222 { 223 eZDebugSetting::writeDebug( 'kernel-clustering', "fs::mtime()" ); 224 return isset( $this->metaData['mtime'] ) ? $this->metaData['mtime'] : null; 225 } 226 227 /** 228 * Returns file name. 229 * 230 * \public 231 */ 232 function name() 233 { 234 eZDebugSetting::writeDebug( 'kernel-clustering', "fs::name()" ); 235 return isset( $this->metaData['name'] ) ? $this->metaData['name'] : null; 236 } 237 238 /** 239 * Delete files matching regex $fileRegex under directory $dir. 240 * 241 * \public 242 * \static 243 * \sa fileDeleteByWildcard() 244 */ 245 function fileDeleteByRegex( $dir, $fileRegex ) 246 { 247 eZDebugSetting::writeDebug( 'kernel-clustering', "fs::fileDeleteByRegex( '$dir', '$fileRegex' )" ); 248 249 eZDebug::accumulatorStart( 'dbfile', false, 'dbfile' ); 250 251 if ( !file_exists( $dir ) ) 252 { 253 //eZDebugSetting::writeDebug( 'kernel-clustering', "Dir '$dir' does not exist", 'dir' ); 254 eZDebug::accumulatorStop( 'dbfile' ); 255 return; 256 } 257 258 $dirHandle = opendir( $dir ); 259 if ( !$dirHandle ) 260 { 261 eZDebug::writeError( "opendir( '$dir' ) failed." ); 262 eZDebug::accumulatorStop( 'dbfile' ); 263 return; 264 } 265 266 while ( ( $file = readdir( $dirHandle ) ) !== false ) 267 { 268 if ( $file == '.' or 269 $file == '..' ) 270 continue; 271 if ( preg_match( "/^$fileRegex/", $file ) ) 272 { 273 //eZDebugSetting::writeDebug( 'kernel-clustering', "\$file = eZDir::path( array( '$dir', '$file' ) );" ); 274 $file = eZDir::path( array( $dir, $file ) ); 275 eZDebugSetting::writeDebug( 'kernel-clustering', "Removing cache file '$file'", 'eZFSFileHandler::deleteRegex' ); 276 unlink( $file ); 277 278 // Write log message to storage.log 279 include_once ( 'lib/ezutils/classes/ezlog.php' ); 280 eZLog::writeStorageLog( $file ); 281 } 282 } 283 closedir( $dirHandle ); 284 285 eZDebug::accumulatorStop( 'dbfile' ); 286 } 287 288 /** 289 * Delete files matching given wildcard. 290 * 291 * Note that this method is faster than fileDeleteByRegex(). 292 * 293 * \public 294 * \static 295 * \sa fileDeleteByRegex() 296 */ 297 function fileDeleteByWildcard( $wildcard ) 298 { 299 eZDebugSetting::writeDebug( 'kernel-clustering', "fs::fileDeleteByWildcard( '$wildcard' )" ); 300 301 eZDebug::accumulatorStart( 'dbfile', false, 'dbfile' ); 302 array_map( 'unlink', glob( $wildcard, GLOB_BRACE ) ); 303 eZDebug::accumulatorStop( 'dbfile' ); 304 } 305 306 307 /** 308 * Delete files located in a directories from dirList, with common prefix specified by 309 * commonPath, and common suffix with added wildcard at the end 310 * 311 * \public 312 * \static 313 * \sa fileDeleteByRegex() 314 */ 315 function fileDeleteByDirList( $dirList, $commonPath, $commonSuffix ) 316 { 317 $dirs = implode( ',', $dirList ); 318 $wildcard = "$commonPath/\{$dirs}/$commonSuffix*"; 319 320 eZDebugSetting::writeDebug( 'kernel-clustering', "fs::fileDeleteByDirList( '$dirList', '$commonPath', '$commonSuffix' )" ); 321 322 eZDebug::accumulatorStart( 'dbfile', false, 'dbfile' ); 323 array_map( 'unlink', glob( $wildcard, GLOB_BRACE ) ); 324 eZDebug::accumulatorStop( 'dbfile' ); 325 } 326 327 /** 328 * \public 329 * \static 330 */ 331 function fileDelete( $path ) 332 { 333 eZDebugSetting::writeDebug( 'kernel-clustering', "fs::fileDelete( '$path' )" ); 334 335 eZDebug::accumulatorStart( 'dbfile', false, 'dbfile' ); 336 337 if ( is_file( $path ) ) 338 { 339 include_once ( 'lib/ezfile/classes/ezfilehandler.php' ); 340 $handler =& eZFileHandler::instance( false ); 341 $handler->unlink( $path ); 342 if ( file_exists( $path ) ) 343 eZDebug::writeError( "File still exists after removal: '$path'", 'fs::fileDelete' ); 344 } 345 else 346 { 347 include_once ( 'lib/ezfile/classes/ezdir.php' ); 348 eZDir::recursiveDelete( $path ); 349 } 350 351 eZDebug::accumulatorStop( 'dbfile' ); 352 } 353 354 /** 355 * Deletes specified file/directory. 356 * 357 * If a directory specified it is deleted recursively. 358 * 359 * \public 360 * \static 361 */ 362 function delete() 363 { 364 $path = $this->metaData['name']; 365 366 eZDebugSetting::writeDebug( 'kernel-clustering', "fs::delete( '$path' )" ); 367 368 // FIXME: cut&paste from fileDelete() 369 370 eZDebug::accumulatorStart( 'dbfile', false, 'dbfile' ); 371 372 if ( is_file( $path ) ) 373 { 374 include_once ( 'lib/ezfile/classes/ezfilehandler.php' ); 375 $handler =& eZFileHandler::instance( false ); 376 $handler->unlink( $path ); 377 if ( file_exists( $path ) ) 378 eZDebug::writeError( "File still exists after removal: '$path'", 'fs::fileDelete' ); 379 } 380 elseif ( is_dir( $path ) ) 381 { 382 include_once ( 'lib/ezfile/classes/ezdir.php' ); 383 eZDir::recursiveDelete( $path ); 384 } 385 386 eZDebug::accumulatorStop( 'dbfile' ); 387 } 388 389 /** 390 * Deletes a file that has been fetched before. 391 * 392 * In case of fetching from filesystem does nothing. 393 * 394 * \public 395 * \static 396 */ 397 function fileDeleteLocal( $path ) 398 { 399 eZDebugSetting::writeDebug( 'kernel-clustering', "fs::fileDeleteLocal( '$path' )" ); 400 } 401 402 /** 403 * Deletes a file that has been fetched before. 404 * 405 * In case of fetching from filesystem does nothing. 406 * 407 * \public 408 */ 409 function deleteLocal() 410 { 411 $path = $this->metaData['name']; 412 eZDebugSetting::writeDebug( 'kernel-clustering', "fs::deleteLocal( '$path' )" ); 413 } 414 415 /** 416 * Check if given file/dir exists. 417 * 418 * \public 419 * \static 420 */ 421 function fileExists( $path ) 422 { 423 eZDebugSetting::writeDebug( 'kernel-clustering', "fs::fileExists( '$path' )" ); 424 425 eZDebug::accumulatorStart( 'dbfile', false, 'dbfile' ); 426 $rc = file_exists( $path ); 427 eZDebug::accumulatorStop( 'dbfile' ); 428 429 return $rc; 430 } 431 432 /** 433 * Check if given file/dir exists. 434 * 435 * NOTE: this function does not interact with filesystem. 436 * Instead, it just returns existance status determined in the constructor. 437 * 438 * \public 439 */ 440 function exists() 441 { 442 $path = $this->metaData['name']; 443 $rc = isset( $this->metaData['mtime'] ); 444 eZDebugSetting::writeDebug( 'kernel-clustering', "fs::exists( '$path' ): " . ( $rc ? 'true' :'false' ) ); 445 446 return $rc; 447 } 448 449 /** 450 * Outputs file contents prepending them with appropriate HTTP headers. 451 * 452 * \public 453 */ 454 function passthrough() 455 { 456 $path = $this->metaData['name']; 457 458 eZDebugSetting::writeDebug( 'kernel-clustering', "fs::passthrough()" ); 459 460 eZDebug::accumulatorStart( 'dbfile', false, 'dbfile' ); 461 462 include_once ( 'lib/ezutils/classes/ezmimetype.php' ); 463 $mimeData = eZMimeType::findByFileContents( $path ); 464 $mimeType = $mimeData['name']; 465 $contentLength = filesize( $path ); 466 467 header( "Content-Length: $contentLength" ); 468 header( "Content-Type: $mimeType" ); 469 header( "Expires: ". gmdate('D, d M Y H:i:s', time() + 6000) . 'GMT'); 470 header( "Connection: close" ); 471 472 readfile( $path ); 473 474 eZDebug::accumulatorStop( 'dbfile' ); 475 } 476 477 /** 478 * Copy file. 479 * 480 * \public 481 * \static 482 */ 483 function fileCopy( $srcPath, $dstPath ) 484 { 485 eZDebugSetting::writeDebug( 'kernel-clustering', "fs::fileCopy( '$srcPath', '$dstPath' )" ); 486 487 eZDebug::accumulatorStart( 'dbfile', false, 'dbfile' ); 488 require_once ( 'lib/ezfile/classes/ezfilehandler.php' ); 489 eZFileHandler::copy( $srcPath, $dstPath ); 490 eZDebug::accumulatorStop( 'dbfile', false, 'dbfile' ); 491 } 492 493 /** 494 * Create symbolic or hard link to file. 495 * 496 * \public 497 * \static 498 */ 499 function fileLinkCopy( $srcPath, $dstPath, $symLink ) 500 { 501 eZDebugSetting::writeDebug( 'kernel-clustering', "fs::fileLinkCopy( '$srcPath', '$dstPath' )" ); 502 503 eZDebug::accumulatorStart( 'dbfile', false, 'dbfile' ); 504 require_once ( 'lib/ezfile/classes/ezfilehandler.php' ); 505 eZFileHandler::linkCopy( $srcPath, $dstPath, $symLink ); 506 eZDebug::accumulatorStop( 'dbfile', false, 'dbfile' ); 507 } 508 509 /** 510 * Move file. 511 * 512 * \public 513 * \static 514 */ 515 function fileMove( $srcPath, $dstPath ) 516 { 517 eZDebugSetting::writeDebug( 'kernel-clustering', "fs::fileMove( '$srcPath', '$dstPath' )" ); 518 519 eZDebug::accumulatorStart( 'dbfile', false, 'dbfile' ); 520 require_once ( 'lib/ezfile/classes/ezfilehandler.php' ); 521 eZFileHandler::move( $srcPath, $dstPath ); 522 eZDebug::accumulatorStop( 'dbfile' ); 523 } 524 525 /** 526 * Move file. 527 * 528 * \public 529 */ 530 function move( $dstPath ) 531 { 532 $srcPath = $this->metaData['name']; 533 534 eZDebugSetting::writeDebug( 'kernel-clustering', "fs::move( '$srcPath', '$dstPath' )" ); 535 536 eZDebug::accumulatorStart( 'dbfile', false, 'dbfile' ); 537 require_once ( 'lib/ezfile/classes/ezfilehandler.php' ); 538 eZFileHandler::move( $srcPath, $dstPath ); 539 eZDebug::accumulatorStop( 'dbfile' ); 540 } 541 542 var $metaData = null; 543 } 544 545 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 10:30:04 2007 | par Balluche grâce à PHPXref 0.7 |