diff --git a/libraries/classes/Plugins/Transformations/Abs/LongToIPv4TransformationsPlugin.php b/libraries/classes/Plugins/Transformations/Abs/LongToIPv4TransformationsPlugin.php index 8498d1b7b4..217c46b005 100644 --- a/libraries/classes/Plugins/Transformations/Abs/LongToIPv4TransformationsPlugin.php +++ b/libraries/classes/Plugins/Transformations/Abs/LongToIPv4TransformationsPlugin.php @@ -9,6 +9,7 @@ namespace PhpMyAdmin\Plugins\Transformations\Abs; use PhpMyAdmin\Plugins\TransformationsPlugin; +use PhpMyAdmin\Util; /** * Provides common methods for all of the long to IPv4 transformations plugins. @@ -41,14 +42,13 @@ abstract class LongToIPv4TransformationsPlugin extends TransformationsPlugin */ public function applyTransformation($buffer, array $options = array(), $meta = '') { - if ($buffer < 0 || $buffer > 4294967295) { + if (! Util::isInteger($buffer) || $buffer < 0 || $buffer > 4294967295) { return htmlspecialchars($buffer); } - return long2ip($buffer); + return long2ip((int) $buffer); } - /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ /** diff --git a/libraries/classes/Plugins/Transformations/Output/Text_Plain_Binarytoip.php b/libraries/classes/Plugins/Transformations/Output/Text_Plain_Binarytoip.php index 80e7348840..54e344e3fa 100644 --- a/libraries/classes/Plugins/Transformations/Output/Text_Plain_Binarytoip.php +++ b/libraries/classes/Plugins/Transformations/Output/Text_Plain_Binarytoip.php @@ -45,15 +45,18 @@ class Text_Plain_Binarytoip extends TransformationsPlugin */ public function applyTransformation($buffer, array $options = array(), $meta = '') { - $length = strlen($buffer); - if ($length == 4 || $length == 16) { - $val = @inet_ntop(pack('A' . $length, $buffer)); - if ($val !== false) { - return $val; - } + if (0 !== strpos($buffer, '0x')) { + return $buffer; } - return $buffer; + $ipHex = substr($buffer, 2); + $ipBin = hex2bin($ipHex); + + if (false === $ipBin) { + return $buffer; + } + + return @inet_ntop($ipBin); } diff --git a/libraries/classes/Util.php b/libraries/classes/Util.php index 6461c61812..41f27c9362 100644 --- a/libraries/classes/Util.php +++ b/libraries/classes/Util.php @@ -4741,4 +4741,16 @@ class Util return self::linkOrButton($url, $title . $orderImg, $orderLinkParams); } + + /** + * Check that input is an int or an int in a string + * + * @param mixed $input + * + * @return bool + */ + public static function isInteger($input) + { + return (ctype_digit((string) $input)); + } } diff --git a/test/classes/Plugins/Transformations/TransformationPluginsTest.php b/test/classes/Plugins/Transformations/TransformationPluginsTest.php index 609c908d20..6962980a0b 100644 --- a/test/classes/Plugins/Transformations/TransformationPluginsTest.php +++ b/test/classes/Plugins/Transformations/TransformationPluginsTest.php @@ -944,6 +944,26 @@ class TransformationPluginsTest extends PmaTestCase ), 'suffixMA_suffix' ), + array( + new Text_Plain_Longtoipv4(), + array(168496141), + '10.11.12.13' + ), + array( + new Text_Plain_Longtoipv4(), + array('168496141'), + '10.11.12.13' + ), + array( + new Text_Plain_Longtoipv4(), + array('my ip'), + 'my ip' + ), + array( + new Text_Plain_Longtoipv4(), + array(''), + '<my ip>' + ) ); if (function_exists('imagecreatetruecolor')) { diff --git a/test/classes/UtilTest.php b/test/classes/UtilTest.php index 8b4ed63e7e..047f2b1753 100644 --- a/test/classes/UtilTest.php +++ b/test/classes/UtilTest.php @@ -2128,4 +2128,51 @@ class UtilTest extends PmaTestCase ], ]; } + + /** + * Test for Util::isInteger + * + * @param bool $expected Expected result for a given input + * @param mixed $input Input data to check + * + * @return void + * + * @dataProvider providerIsInteger + */ + public function testIsInteger($expected, $input) + { + $isInteger = Util::isInteger($input); + $this->assertEquals($expected, $isInteger); + } + + /** + * Data provider for Util::isInteger test + * + * @return array + */ + public function providerIsInteger() + { + return [ + [ + true, + 1000, + ], + [ + true, + '1000', + ], + [ + false, + 1000.1, + ], + [ + false, + '1000.1', + ], + [ + false, + 'input', + ], + ]; + } }