Ticket #6336: 6336.02.patch
File 6336.02.patch, 4.7 KB (added by , 9 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 8c6ca3a..2099e05 100644
function bp_core_check_avatar_size( $file ) { 783 783 } 784 784 785 785 /** 786 * Get allowed avatar types 787 * 788 * @since BuddyPress (2.3.0) 789 */ 790 function bp_core_get_allowed_avatar_types() { 791 $allowed_types = array( 'jpeg', 'gif', 'png' ); 792 793 /** 794 * Use this filter to restrict image types 795 * 796 * @since BuddyPress (2.3.0) 797 * 798 * @param array list of image types 799 */ 800 $avatar_types = (array) apply_filters( 'bp_core_get_allowed_avatar_types', $allowed_types ); 801 802 if ( empty( $avatar_types ) ) { 803 $avatar_types = $allowed_types; 804 } else { 805 $avatar_types = array_intersect( $allowed_types, $avatar_types ); 806 } 807 808 return array_values( $avatar_types ); 809 } 810 811 /** 812 * Get allowed avatar mime types 813 * 814 * @since BuddyPress (2.3.0) 815 */ 816 function bp_core_get_allowed_avatar_mimes() { 817 $allowed_types = bp_core_get_allowed_avatar_types(); 818 $validate_mimes = wp_match_mime_types( join( ',', $allowed_types ), wp_get_mime_types() ); 819 $allowed_mimes = array_map( 'implode', $validate_mimes ); 820 821 /** 822 * Include jpg type if needed so that bp_core_check_avatar_type() 823 * will check for jpeg and jpg extensions. 824 */ 825 if ( isset( $allowed_mimes['jpeg'] ) ) { 826 $allowed_mimes['jpg'] = $allowed_mimes['jpeg']; 827 } 828 829 return $allowed_mimes; 830 } 831 832 /** 786 833 * Does the current avatar upload have an allowed file type? 787 834 * 788 835 * Permitted file types are JPG, GIF and PNG. … … function bp_core_check_avatar_size( $file ) { 790 837 * @param array $file The $_FILES array. 791 838 * @return bool True if the file extension is permitted, otherwise false. 792 839 */ 793 function bp_core_check_avatar_type($file) { 794 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'] ) ) 795 return false; 840 function bp_core_check_avatar_type( $file ) { 841 $avatar_filetype = wp_check_filetype_and_ext( $file['file']['tmp_name'], $file['file']['name'], bp_core_get_allowed_avatar_mimes() ); 796 842 797 return true; 843 if ( ! empty( $avatar_filetype['ext'] ) && ! empty( $avatar_filetype['type'] ) ) { 844 return true; 845 } 846 847 return false; 798 848 } 799 849 800 850 /** -
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 }