[ Index ] |
|
Code source de LifeType 1.2.4 |
1 <?php 2 3 4 lt_include( PLOG_CLASS_PATH."class/file/fileupload.class.php" ); 5 lt_include( PLOG_CLASS_PATH."class/file/file.class.php" ); 6 lt_include( PLOG_CLASS_PATH."class/file/fileuploads.class.php" ); 7 lt_include( PLOG_CLASS_PATH."class/config/config.class.php" ); 8 lt_include( PLOG_CLASS_PATH."class/gallery/galleryconstants.php" ); 9 10 define( "RESOURCE_STORAGE_STORE_COPY", 1 ); 11 define( "RESOURCE_STORAGE_STORE_MOVE", 2 ); 12 13 /** 14 * \ingroup Gallery 15 * 16 * Takes care of dealing with resource files on disk. This class hides all the intricacies of storing 17 * files in disk, and how they are found and handled later on. 18 */ 19 class GalleryResourceStorage 20 { 21 22 function GalleryResourceStorage() 23 { 24 25 } 26 27 /** 28 * Returns the path to the folder where resources are stored. This method can 29 * be used as static. 30 * 31 * @static 32 * @return Returns a string containing the base path to the resource storage folder. 33 */ 34 function getResourcesStorageFolder() 35 { 36 $config =& Config::getConfig(); 37 $resourcesStorageFolder = $config->getValue( "resources_folder" ); 38 39 // just in case... 40 if( $resourcesStorageFolder == "" ) 41 $resourcesStorageFolder = DEFAULT_RESOURCES_STORAGE_FOLDER; 42 43 // append a forward slash to the folder if we forgot about it... 44 if( $resourcesStorageFolder[strlen($resourcesStorageFolder)-1] != '/') 45 $resourcesStorageFolder .= "/"; 46 47 48 return $resourcesStorageFolder; 49 } 50 51 /** 52 * @private 53 */ 54 function _checkBaseStorageFolder() 55 { 56 $baseFolder = GalleryResourceStorage::getResourcesStorageFolder(); 57 if( $baseFolder[strlen($baseFolder)-1] == "/") { 58 $baseFolder = substr($baseFolder,0,strlen($baseFolder)-1); 59 } 60 61 if( !File::isDir( $baseFolder )) { 62 // folder does not exist, so we should try to create it 63 if( !File::createDir( $baseFolder, 0755 )) { 64 throw( new Exception( "Could not create storage folder for resources: ".$baseFolder)); 65 return false; 66 //die(); 67 } 68 } 69 70 if( !File::isReadable( $baseFolder )) { 71 throw( new Exception( $baseFolder." storage folder exists but it is not readable!" )); 72 return false; 73 //die(); 74 } 75 76 return true; 77 } 78 79 /** 80 * a nicer function that the one above. And it is also meant to be used 81 * by external classes 82 * 83 * @static 84 * @return Returns true if the base storage folder has been created and 85 * it is readable. 86 */ 87 function checkBaseStorageFolder() 88 { 89 return GalleryResourceStorage::_checkBaseStorageFolder(); 90 } 91 92 /** 93 * @private 94 */ 95 function getUserFolder( $ownerId ) 96 { 97 return GalleryResourceStorage::getResourcesStorageFolder().$ownerId."/"; 98 } 99 100 /** 101 * @public 102 */ 103 function getPreviewsFolder( $ownerId ) 104 { 105 return GalleryResourceStorage::getResourcesStorageFolder().$ownerId."/previews/"; 106 } 107 108 /** 109 * @public 110 */ 111 function getMediumSizePreviewsFolder( $ownerId ) 112 { 113 return GalleryResourceStorage::getResourcesStorageFolder().$ownerId."/previews-med/"; 114 } 115 116 /** 117 * @private 118 */ 119 function _checkUserStorageFolder( $ownerId ) 120 { 121 $baseFolder = GalleryResourceStorage::getResourcesStorageFolder(); 122 if( $baseFolder[strlen($baseFolder)-1] == "/") { 123 $baseFolder = substr($baseFolder,0,strlen($baseFolder)-1); 124 } 125 126 $userFolder = GalleryResourceStorage::getUserFolder( $ownerId ); 127 if( $userFolder[strlen($userFolder)-1] == "/") { 128 $userFolder = substr($userFolder,0,strlen($userFolder)-1); 129 } 130 131 if( !File::isDir( $userFolder )) { 132 // folder does not exist, so we should try to create it 133 if( !File::createDir( $userFolder, 0755 )) { 134 throw( new Exception( "Could not create user storage folder for resources: ".$userFolder )); 135 return false; 136 //die(); 137 } 138 } 139 140 if( !File::isReadable( $userFolder )) { 141 //throw( new Exception( $userFolder." user storage folder exists but it is not readable!" )); 142 return false; 143 //die(); 144 } 145 146 return true; 147 } 148 149 function checkUserStorageFolder( $ownerId ) 150 { 151 return GalleryResourceStorage::_checkUserStorageFolder( $ownerId ); 152 } 153 154 /** 155 * @public 156 */ 157 function checkPreviewsStorageFolder( $ownerId ) 158 { 159 $previewsFolder = GalleryResourceStorage::getPreviewsFolder( $ownerId ); 160 if( $previewsFolder[strlen($previewsFolder)-1] == "/") { 161 $previewsFolder = substr($previewsFolder,0,strlen($previewsFolder)-1); 162 } 163 164 if( !File::isDir( $previewsFolder )) { 165 // folder does not exist, so we should try to create it 166 if( !File::createDir( $previewsFolder, 0755 )) { 167 throw( new Exception( "Could not create user storage folder for previews: ".$previewsFolder )); 168 //die(); 169 return false; 170 } 171 } 172 173 if( !File::isReadable( $previewsFolder )) { 174 throw( new Exception( $previewsFolder." user previews storage folder exists but it is not readable!" )); 175 //die(); 176 return false; 177 } 178 179 return true; 180 } 181 182 /** 183 * @public 184 */ 185 function checkMediumSizePreviewsStorageFolder( $ownerId ) 186 { 187 $previewsFolder = GalleryResourceStorage::getMediumSizePreviewsFolder( $ownerId ); 188 if( $previewsFolder[strlen($previewsFolder)-1] == "/") { 189 $previewsFolder = substr($previewsFolder,0,strlen($previewsFolder)-1); 190 } 191 192 if( !File::isDir( $previewsFolder )) { 193 // folder does not exist, so we should try to create it 194 if( !File::createDir( $previewsFolder, 0755 )) { 195 throw( new Exception( "Could not create user storage folder for medium size previews: ".$previewsFolder )); 196 //die(); 197 return false; 198 } 199 200 } 201 202 if( !File::isReadable( $previewsFolder )) { 203 throw( new Exception( $previewsFolder." user previews storage folder exists but it is not readable!" )); 204 //die(); 205 return false; 206 } 207 208 return true; 209 } 210 211 /** 212 * stores a new resource in disk 213 * 214 * @param ownerId The id of the owner of this file 215 * @param albumId The album id to which the 216 * @param upload a FileUpload object with information about the 217 * uploaded file 218 */ 219 function storeUpload( $resourceId, $ownerId, $upload ) 220 { 221 // check that the folders exist 222 if( !$this->_checkBaseStorageFolder()) 223 return false; 224 if( !$this->_checkUserStorageFolder( $ownerId )) 225 return false; 226 227 // new name for the file 228 $filePath = $this->getUserFolder( $ownerId ); 229 230 // move the file to the temporaray folder first 231 $config =& Config::getConfig(); 232 $tmpFolder = $config->getValue( "temp_folder" ); 233 /*$files = HttpVars::getFiles();*/ 234 // we don't need the parameter in the constructor though it is necessary 235 // according to the signature of the method 236 $uploads = new FileUploads( null ); 237 $result = $uploads->processFile( $upload, $tmpFolder ); 238 239 if( $result < 0 ) { 240 return $result; 241 } 242 243 $origFile = $tmpFolder."/".basename( $upload->getTmpName() ); 244 245 //do not use storeFile method because I have change filename in $tmpFolder. 246 //$destFile = $this->storeFile( $resourceId, $ownerId, $origFile, RESOURCE_STORAGE_STORE_MOVE ); 247 //$destFile use $filePath and $fileName generated above. 248 //$destFile = $filePath.$fileName; 249 250 if( $config->getValue( "resources_naming_rule" ) == "encoded_file_name" ) { 251 $fileName = $upload->getFileName(); 252 // new name for the file 253 $fileParts = explode( ".", $fileName); 254 $fileExt = strtolower($fileParts[count($fileParts)-1]); 255 $destFile = $filePath.$ownerId."-".$resourceId.".".$fileExt; 256 } 257 else { 258 $destFile = $filePath.stripslashes($upload->getFileName()); 259 } 260 261 // first of all, check if the file is readable and if not, quit 262 if( !File::isReadable($origFile)) { 263 return false; 264 } 265 266 $res = File::rename( $origFile, $destFile ); 267 268 if( !$res ) { 269 return false; 270 } 271 // check that the permissions are correct 272 File::chMod( $destFile, 0755 ); 273 274 return $destFile; 275 } 276 277 /** 278 * the method above works only with files that have been uploaded while 279 * this one works with files that are anywhere. It will take care of copying 280 * the file to the right destination folder and so on 281 * 282 * @param ownerId The id of the owner of this file 283 * @param albumId The album id to which the 284 * @param fileName full path and name to the file that we're trying to store 285 */ 286 function storeFile( $resourceId, $ownerId, $fileName, $mode = RESOURCE_STORAGE_STORE_COPY ) 287 { 288 // check that the folders exist 289 if( !$this->_checkBaseStorageFolder()) 290 return false; 291 if( !$this->_checkUserStorageFolder( $ownerId )) 292 return false; 293 294 // new name for the file 295 $fileParts = explode( ".", $fileName); 296 $fileExt = strtolower($fileParts[count($fileParts)-1]); 297 298 //$destFile = "$ownerId-$resourceId.$fileExt"; 299 $config =& Config::getConfig(); 300 301 // first of all, remove the resource file itself 302 $filePath = $this->getUserFolder( $ownerId ); 303 if( $config->getValue( "resources_naming_rule" ) == "encoded_file_name" ) 304 $destFile = $ownerId."-".$resourceId.".".$fileExt; 305 else 306 $destFile = basename( $fileName ); 307 308 $destPath = $this->getUserFolder( $ownerId ); 309 310 // first of all, check if the file is readable and if not, quit 311 if( !File::isReadable($fileName)) { 312 return false; 313 } 314 315 $destFile = $destPath.$destFile; 316 if( $mode == RESOURCE_STORAGE_STORE_COPY ) 317 $res = File::copy( $fileName, $destFile ); 318 else 319 $res = File::rename( $fileName, $destFile ); 320 321 if( !$res ) { 322 return false; 323 } 324 // check that the permissions are correct 325 File::chMod( $destFile, 0755 ); 326 327 return $destFile; 328 } 329 330 /** 331 * removes a file from disk 332 * 333 * @param resource A GalleryResource object, representing the resource 334 * we'd like to delete. 335 * @return Returns ok if file was successfully deleted ok or false otherwise. 336 */ 337 function remove( $resource ) 338 { 339 if( $resource ) { 340 $config =& Config::getConfig(); 341 342 // first of all, remove the resource file itself 343 $filePath = $this->getUserFolder( $resource->getOwnerId()); 344 if( $config->getValue( "resources_naming_rule" ) == "encoded_file_name" ) 345 $fullName = $filePath.$resource->getEncodedFileName(); 346 else 347 $fullName = $filePath.$resource->getFileName(); 348 349 if( File::isReadable( $fullName)) { 350 $result = File::delete( $fullName ); 351 } else { 352 $result = false; 353 } 354 355 // and now if preview images are available, remove them too! 356 if( $resource->hasPreview()) { 357 // delete the small thumbnail 358 $previewFile = $this->getPreviewsFolder( $resource->getOwnerId()).$resource->getPreviewFileName(); 359 if( File::isReadable( $previewFile )) 360 File::delete( $previewFile ); 361 362 // and the medium-sized thumbnail 363 $medPreviewFile = $this->getMediumSizePreviewsFolder( $resource->getOwnerId()).$resource->getMediumSizePreviewFileName(); 364 if( File::isReadable( $medPreviewFile )) 365 File::delete( $medPreviewFile ); 366 } 367 } 368 else 369 $result = false; 370 371 return $result; 372 } 373 374 /** 375 * returns the path of a GalleryResource object within the storage area 376 * 377 * @param $resource A GalleryResource object 378 * @return A string containing the path to the file relative to the storage area. 379 */ 380 function getResourcePath( $resource ) 381 { 382 $config =& Config::getConfig(); 383 384 // first of all, remove the resource file itself 385 $filePath = $this->getUserFolder( $resource->getOwnerId()); 386 if( $config->getValue( "resources_naming_rule" ) == "encoded_file_name" ) 387 $fileName = $resource->getEncodedFileName(); 388 else 389 $fileName = $resource->getFileName(); 390 391 $resourcePath = $this->getUserFolder( $resource->getOwnerId()).$fileName; 392 return $resourcePath; 393 } 394 } 395 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 21:04:15 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |