[ Index ] |
|
Code source de LifeType 1.2.4 |
1 <?php 2 3 lt_include( PLOG_CLASS_PATH."class/data/validator/validator.class.php" ); 4 lt_include( PLOG_CLASS_PATH."class/config/config.class.php" ); 5 lt_include( PLOG_CLASS_PATH."class/misc/glob.class.php" ); 6 7 define( "UPLOAD_MAX_SIZE", 2000000 ); 8 9 define( "UPLOAD_VALIDATOR_ERROR_UPLOAD_TOO_BIG", -1 ); 10 define( "UPLOAD_VALIDATOR_ERROR_FORBIDDEN_EXTENSION", -2 ); 11 define( "UPLOAD_VALIDATOR_ERROR_NOT_WHITELISTED_EXTENSION", -10 ); 12 13 /** 14 * \ingroup Validator 15 * 16 * Given a FileUpload object, checks that it is not bigger than the maximum size allowed 17 * and that it does not have one of the forbidden extensions. 18 * 19 * The maximum size allowed is controlled via the 'maximum_file_upload_size' configuration setting 20 * and the list of forbidden extensions can be configured via the upload_forbidde_files configuration 21 * setting. 22 * 23 * If the validation is unsuccessful, the error codes UPLOAD_VALIDATOR_ERROR_UPLOAD_TOO_BIG or 24 * UPLOAD_VALIDATOR_ERROR_FORBIDDEN_EXTENSION will be set. 25 */ 26 class UploadValidator extends Validator 27 { 28 29 /** 30 * Constructor. Initializes the rule. 31 */ 32 function UploadValidator() 33 { 34 $this->Validator(); 35 } 36 37 /** 38 * Returns true if the file is a valid upload, after making it go through all 39 * our tests. 40 * 41 * @param upload An FileUpload object containing information about the file 42 * @return Returns true if it is valid or a negative value meaning an error otherwise: 43 * <ul><li>ERROR_UPLOAD_TOO_BIG (-1): The file bigger than the maximum allowed size</li> 44 * <li>ERROR_FORBIDDEN_EXTENSION: The file has a forbidden extension.</li></ul> 45 */ 46 function validate( $upload ) 47 { 48 $config =& Config::getConfig(); 49 50 $forbiddenFilesStr = $config->getValue( "upload_forbidden_files" ); 51 $allowedFilesStr = $config->getValue( "upload_allowed_files" ); 52 $maxUploadSize = $config->getValue( "maximum_file_upload_size" ); 53 54 // check if we received an object of the right type, or else just quit 55 if( $upload == null ) { 56 return false; 57 } 58 59 // first of all, check the size 60 if( $maxUploadSize != 0 && $upload->getSize() > $maxUploadSize ) { 61 return UPLOAD_VALIDATOR_ERROR_UPLOAD_TOO_BIG; 62 } 63 64 if( $allowedFilesStr != "" ) 65 $result = $this->validateWhitelist( $upload, $allowedFilesStr ); 66 elseif( $forbiddenFilesStr != "" ) 67 $result = $this->validateBlacklist( $upload, $forbiddenFilesStr ); 68 else 69 $result = true; 70 71 return( $result ); 72 } 73 74 /** 75 * @private 76 * Validates the given uploaded file agains a blackist/list of forbidden extensions 77 * @return Returns true if successful or false otherwise 78 */ 79 function validateBlacklist( $upload, $forbiddenFilesStr ) 80 { 81 // check if the filename extension is forbidden or not 82 $fileName = basename($upload->getFileName()); 83 foreach( explode( " ", $forbiddenFilesStr ) as $file ) { 84 if( Glob::myFnmatch( $file, $fileName )) { 85 return UPLOAD_VALIDATOR_ERROR_FORBIDDEN_EXTENSION; 86 } 87 } 88 89 return true; 90 } 91 92 /** 93 * @private 94 * Validates the given uploaded file agains a whitelist/list of allowed extensions 95 * @return Returns true if successful or false otherwise 96 */ 97 function validateWhitelist( $upload, $allowedFilesStr ) 98 { 99 // check if the filename extension is one of the allowed ones or not 100 $fileName = basename($upload->getFileName()); 101 foreach( explode( " ", $allowedFilesStr ) as $file ) { 102 if( Glob::myFnmatch( $file, $fileName )) { 103 print("it's a valid file!"); 104 return true; 105 } 106 } 107 108 return UPLOAD_VALIDATOR_ERROR_NOT_WHITELISTED_EXTENSION; 109 } 110 } 111 ?>
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 |
![]() |