Ticket #6336: 6336.01.patch
File 6336.01.patch, 4.5 KB (added by , 10 years ago) |
---|
-
src/bp-core/bp-core-avatars.php
diff --git src/bp-core/bp-core-avatars.php src/bp-core/bp-core-avatars.php index 44fe3c3..0e1b2ec 100644
function bp_core_check_avatar_size( $file ) { 782 782 } 783 783 784 784 /** 785 * Get allowed avatar types 786 * 787 * @since BuddyPress (2.3.0) 788 */ 789 function bp_core_get_allowed_avatar_types() { 790 $allowed_types = array( 'jpeg', 'gif', 'png' ); 791 792 /** 793 * Use this filter to restrict image types 794 * 795 * @since BuddyPress (2.3.0) 796 * 797 * @param array list of image types 798 */ 799 $avatar_types = (array) apply_filters( 'bp_core_get_allowed_avatar_types', $allowed_types ); 800 801 if ( empty( $avatar_types ) ) { 802 $avatar_types = $allowed_types; 803 } else { 804 $avatar_types = array_intersect( $allowed_types, $avatar_types ); 805 } 806 807 return array_values( $avatar_types ); 808 } 809 810 /** 811 * Get allowed avatar mime types 812 * 813 * @since BuddyPress (2.3.0) 814 */ 815 function bp_core_get_allowed_avatar_mimes() { 816 $allowed_types = bp_core_get_allowed_avatar_types(); 817 $allowed_mimes = wp_match_mime_types( join( ',', $allowed_types ), wp_get_mime_types() ); 818 return array_map( 'implode', $allowed_mimes ); 819 } 820 821 /** 785 822 * Does the current avatar upload have an allowed file type? 786 823 * 787 824 * Permitted file types are JPG, GIF and PNG. … … function bp_core_check_avatar_size( $file ) { 789 826 * @param array $file The $_FILES array. 790 827 * @return bool True if the file extension is permitted, otherwise false. 791 828 */ 792 function bp_core_check_avatar_type($file) { 793 if ( ( !empty( $file['file']['type'] ) && !preg_match('/(jpe?g|gif|png)$/i', $file['file']['type'] ) ) || !preg_match( '/(jpe?g|gif|png)$/i', $file['file']['name'] ) ) 794 return false; 829 function bp_core_check_avatar_type( $file ) { 830 $avatar_filetype = wp_check_filetype_and_ext( $file['file']['tmp_name'], $file['file']['name'], bp_core_get_allowed_avatar_mimes() ); 795 831 796 return true; 832 if ( ! empty( $avatar_filetype['ext'] ) && ! empty( $avatar_filetype['type'] ) ) { 833 return true; 834 } 835 836 return false; 797 837 } 798 838 799 839 /** -
tests/phpunit/testcases/core/avatars.php
diff --git tests/phpunit/testcases/core/avatars.php tests/phpunit/testcases/core/avatars.php index d37fe1a..2c9f844 100644
class BP_Tests_Avatars extends BP_UnitTestCase { 204 204 $classes = explode( ' ', $matches[1] ); 205 205 $this->assertSame( $expected, array_intersect_key( $expected, $classes ) ); 206 206 } 207 208 /** 209 * @group bp_core_check_avatar_type 210 */ 211 public function test_bp_core_check_avatar_type() { 212 $file = array( 213 'file' => array( 214 'name' => 'buddypress_logo.pdf', 215 'type' => 'application/pdf', 216 'tmp_name' => BP_TESTS_DIR . 'assets/files/buddypress_logo.pdf', 217 ) 218 ); 219 220 $this->assertFalse( bp_core_check_avatar_type( $file ) ); 221 222 $file = array( 223 'file' => array( 224 'name' => 'disc.png', 225 'type' => 'image/png', 226 'tmp_name' => BP_TESTS_DIR . 'assets/files/disc.png', 227 ) 228 ); 229 230 $this->assertTrue( bp_core_check_avatar_type( $file ) ); 231 232 $file = array( 233 'file' => array( 234 'name' => 'avatar.jpg', 235 'type' => 'image/jpeg', 236 'tmp_name' => BP_TESTS_DIR . 'assets/group-extensions.php', 237 ) 238 ); 239 240 $this->assertFalse( bp_core_check_avatar_type( $file ) ); 241 } 242 243 /** 244 * @group bp_core_check_avatar_type 245 * @group bp_core_get_allowed_avatar_types() 246 */ 247 public function test_bp_core_get_allowed_avatar_types_filter() { 248 add_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_add_type' ) ); 249 250 $this->assertEquals( array( 'jpeg', 'gif', 'png' ), bp_core_get_allowed_avatar_types() ); 251 252 remove_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_add_type' ) ); 253 254 add_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_remove_type' ) ); 255 256 $this->assertEquals( array( 'gif', 'png' ), bp_core_get_allowed_avatar_types() ); 257 258 remove_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_remove_type' ) ); 259 260 add_filter( 'bp_core_get_allowed_avatar_types', '__return_empty_array' ); 261 262 $this->assertEquals( array( 'jpeg', 'gif', 'png' ), bp_core_get_allowed_avatar_types() ); 263 264 remove_filter( 'bp_core_get_allowed_avatar_types', '__return_empty_array' ); 265 } 266 267 public function avatar_types_filter_add_type( $types ) { 268 $types[] = 'bmp'; 269 270 return $types; 271 } 272 273 public function avatar_types_filter_remove_type( $types ) { 274 $jpeg = array_shift( $types ); 275 276 return $types; 277 } 207 278 }