From: Bryan Tong Minh Date: Wed, 29 Jun 2011 20:24:53 +0000 (+0000) Subject: Add wfUnserialize() wrapper around unserialize to prevent E_NOTICE and use it in... X-Git-Tag: 1.31.0-rc.0~29187 X-Git-Url: https://git.cyclocoop.org//%22?a=commitdiff_plain;h=c87574b7b443fdbff9fdb2b2af2918bf7b33febf;p=lhc%2Fweb%2Fwiklou.git Add wfUnserialize() wrapper around unserialize to prevent E_NOTICE and use it in ExifBitmap.php. There are probably many more places that could use this. This should fix Platonides' problem at r90421, but also added a check for $wgShowExif to prevent the test from failing. --- diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index c66b0e89c1..f3f646f4f5 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -3549,3 +3549,17 @@ function wfGetParserCacheStorage() { function wfRunHooks( $event, $args = array() ) { return Hooks::run( $event, $args ); } + +/** + * Unserialize a string to a PHP value without throwing E_NOTICE. Simply a + * wrapper around unserialize() + * + * @param $data string The serialized string + * @return mixed + */ +function wfUnserialize( $data ) { + wfSuppressWarnings(); + $result = unserialize( $data ); + wfRestoreWarnings(); + return $result; +} diff --git a/includes/media/ExifBitmap.php b/includes/media/ExifBitmap.php index b63ef18136..42a2fd6887 100644 --- a/includes/media/ExifBitmap.php +++ b/includes/media/ExifBitmap.php @@ -109,7 +109,7 @@ class ExifBitmapHandler extends BitmapHandler { return false; } - $exif = unserialize( $metadata ); + $exif = wfUnserialize( $metadata ); if ( !$exif ) { return false; } diff --git a/tests/phpunit/includes/GlobalFunctions.php/GlobalTest.php b/tests/phpunit/includes/GlobalFunctions.php/GlobalTest.php index b06dbf1509..0ded96f172 100644 --- a/tests/phpunit/includes/GlobalFunctions.php/GlobalTest.php +++ b/tests/phpunit/includes/GlobalFunctions.php/GlobalTest.php @@ -879,6 +879,12 @@ class GlobalTest extends MediaWikiTestCase { // are less consistent. ); } + + public function testUnserialize() { + $this->assertEquals( '', wfUnserialize( 's:0:"";') ); + $this->assertEquals( false, wfUnserialize( '0' ), + 'Invalid input to unserialize()' ); + } /* TODO: many more! */ } diff --git a/tests/phpunit/includes/media/FormatMetadataTest.php b/tests/phpunit/includes/media/FormatMetadataTest.php index 98c8013e9b..c4c48b52d2 100644 --- a/tests/phpunit/includes/media/FormatMetadataTest.php +++ b/tests/phpunit/includes/media/FormatMetadataTest.php @@ -1,11 +1,17 @@ markTestIncomplete( "This test needs the exif extension." ); + } + $file = UnregisteredLocalFile::newFromPath( dirname( __FILE__ ) . '/broken_exif_date.jpg', 'image/jpeg' ); // Throws an error if bug hit $meta = $file->formatMetadata(); + $this->assertNotEquals( false, $meta, 'Valid metadata extracted' ); // Find date exif entry $this->assertArrayHasKey( 'visible', $meta );