Skip to content

Commit

Permalink
Merge #15557 - Feature #15555 ip2long transformation
Browse files Browse the repository at this point in the history
Pull-request: #15557
Fixes: #15555
Signed-off-by: William Desportes <williamdes@wdes.fr>
  • Loading branch information
williamdes committed Jan 27, 2020
2 parents 94888ee + 5744268 commit 3a1914b
Show file tree
Hide file tree
Showing 8 changed files with 415 additions and 30 deletions.
Expand Up @@ -6,11 +6,11 @@

namespace PhpMyAdmin\Plugins\Transformations\Abs;

use PhpMyAdmin\Utils\FormatConverter;
use PhpMyAdmin\Plugins\TransformationsPlugin;
use PhpMyAdmin\Util;
use stdClass;
use function htmlspecialchars;
use function long2ip;

/**
* Provides common methods for all of the long to IPv4 transformations plugins.
Expand Down Expand Up @@ -41,11 +41,7 @@ public static function getInfo()
*/
public function applyTransformation($buffer, array $options = [], ?stdClass $meta = null)
{
if (! Util::isInteger($buffer) || $buffer < 0 || $buffer > 4294967295) {
return htmlspecialchars($buffer);
}

return long2ip((int) $buffer);
return htmlspecialchars(FormatConverter::longToIp($buffer));
}

/* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
Expand Down
Expand Up @@ -6,12 +6,11 @@

namespace PhpMyAdmin\Plugins\Transformations\Input;

use PhpMyAdmin\Utils\FormatConverter;
use PhpMyAdmin\Plugins\IOTransformationsPlugin;
use stdClass;
use function bin2hex;
use function htmlspecialchars;
use function inet_ntop;
use function inet_pton;
use function pack;
use function strlen;

Expand Down Expand Up @@ -46,12 +45,7 @@ public static function getInfo()
*/
public function applyTransformation($buffer, array $options = [], ?stdClass $meta = null)
{
$val = @inet_pton($buffer);
if ($val !== false) {
return '0x' . bin2hex($val);
}

return $buffer;
return FormatConverter::ipToBinary($buffer);
}

/**
Expand Down
@@ -0,0 +1,131 @@
<?php
/**
* Handles the IPv4/IPv6 to long transformation for text plain
*
* @package PhpMyAdmin-Transformations
* @subpackage IPToLong
*/
declare(strict_types=1);

namespace PhpMyAdmin\Plugins\Transformations\Input;

use PhpMyAdmin\Utils\FormatConverter;
use PhpMyAdmin\Plugins\IOTransformationsPlugin;
use stdClass;

/**
* Handles the IPv4/IPv6 to long transformation for text plain
*
* @package PhpMyAdmin-Transformations
* @subpackage IPToLong
*/
// @codingStandardsIgnoreLine
class Text_Plain_Iptolong extends IOTransformationsPlugin
{
/**
* Gets the transformation description of the plugin
*
* @return string
*/
public static function getInfo()
{
return __(
'Converts an Internet network address in (IPv4/IPv6) format to long.'
);
}

/**
* Does the actual work of each specific transformations plugin.
*
* @param string $buffer text to be transformed. a binary string containing
* an IP address, as returned from MySQL's INET6_ATON
* function
* @param array $options transformation options
* @param stdClass $meta meta information
*
* @return string IP address
*/
public function applyTransformation($buffer, array $options = [], ?stdClass $meta = null)
{
return (string) FormatConverter::ipToLong($buffer);
}

/**
* Returns the html for input field to override default textarea.
* Note: Return empty string if default textarea is required.
*
* @param array $column column details
* @param int $row_id row number
* @param string $column_name_appendix the name attribute
* @param array $options transformation options
* @param string $value Current field value
* @param string $text_dir text direction
* @param int $tabindex tab index
* @param int $tabindex_for_value offset for the values tabindex
* @param int $idindex id index
*
* @return string the html for input field
*/
public function getInputHtml(
array $column,
$row_id,
$column_name_appendix,
array $options,
$value,
$text_dir,
$tabindex,
$tabindex_for_value,
$idindex
) {
$html = '';
if (! empty($value) && $value !== ($val = FormatConverter::longToIp($value))) {
$val = htmlspecialchars($val);
$html = '<input type="hidden" name="fields_prev' . $column_name_appendix
. '" value="' . $val . '"/>';
} else {
$val = '';
}
$class = 'transform_IPToLong';
$html .= '<input type="text" name="fields' . $column_name_appendix . '"'
. ' value="' . $val . '"'
. ' size="40"'
. ' dir="' . $text_dir . '"'
. ' class="' . $class . '"'
. ' id="field_' . ($idindex) . '_3"'
. ' tabindex="' . ($tabindex + $tabindex_for_value) . '" />';

return $html;
}

/* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */

/**
* Gets the transformation name of the plugin
*
* @return string
*/
public static function getName()
{
return 'IPv4/IPv6 To Long';
}

/**
* Gets the plugin`s MIME type
*
* @return string
*/
public static function getMIMEType()
{
return 'Text';
}

/**
* Gets the plugin`s MIME subtype
*
* @return string
*/
public static function getMIMESubtype()
{
return 'Plain';
}
}
Expand Up @@ -6,12 +6,9 @@

namespace PhpMyAdmin\Plugins\Transformations\Output;

use PhpMyAdmin\Utils\FormatConverter;
use PhpMyAdmin\Plugins\TransformationsPlugin;
use stdClass;
use function hex2bin;
use function inet_ntop;
use function strpos;
use function substr;

/**
* Handles the binary to IPv4/IPv6 transformation for text plain
Expand Down Expand Up @@ -45,18 +42,7 @@ public static function getInfo()
*/
public function applyTransformation($buffer, array $options = [], ?stdClass $meta = null)
{
if (strpos($buffer, '0x') !== 0) {
return $buffer;
}

$ipHex = substr($buffer, 2);
$ipBin = hex2bin($ipHex);

if ($ipBin === false) {
return $buffer;
}

return @inet_ntop($ipBin);
return FormatConverter::binaryToIp($buffer);
}

/* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
Expand Down
97 changes: 97 additions & 0 deletions libraries/classes/Utils/FormatConverter.php
@@ -0,0 +1,97 @@
<?php
/**
* Format converter
*
* @package PhpMyAdmin
*/
declare(strict_types=1);

namespace PhpMyAdmin\Utils;

use PhpMyAdmin\Util;

use function hex2bin;
use function inet_ntop;
use function strpos;
use function substr;

/**
* Format converter
*
* @package PhpMyAdmin
*/
class FormatConverter
{
/**
* Transforms a binary to an IP
*
* @param mixed $buffer Data to transform
*
* @return false|string
*/
public static function binaryToIp($buffer)
{
if (0 !== strpos($buffer, '0x')) {
return $buffer;
}

$ipHex = substr($buffer, 2);
$ipBin = hex2bin($ipHex);

if (false === $ipBin) {
return $buffer;
}

return @inet_ntop($ipBin);
}

/**
* Transforms an IP to a binary
*
* @param mixed $buffer Data to transform
*
* @return string
*/
public static function ipToBinary($buffer)
{
$val = @inet_pton($buffer);
if ($val !== false) {
return '0x' . bin2hex($val);
}

return $buffer;
}

/**
* Transforms an IP to a long
*
* @param string $buffer Data to transform
*
* @return int|string
*/
public static function ipToLong(string $buffer)
{
$ipLong = ip2long($buffer);
if ($ipLong === false) {
return $buffer;
}

return $ipLong;
}

/**
* Transforms a long to an IP
*
* @param mixed $buffer Data to transform
*
* @return string
*/
public static function longToIp($buffer)
{
if (! Util::isInteger($buffer) || $buffer < 0 || $buffer > 4294967295) {
return $buffer;
}

return long2ip((int) $buffer);
}
}
21 changes: 21 additions & 0 deletions test/classes/Plugins/Transformations/TransformationPluginsTest.php
Expand Up @@ -9,6 +9,7 @@
use PhpMyAdmin\Config;
use PhpMyAdmin\Plugins\Transformations\Input\Image_JPEG_Upload;
use PhpMyAdmin\Plugins\Transformations\Input\Text_Plain_FileUpload;
use PhpMyAdmin\Plugins\Transformations\Input\Text_Plain_Iptolong;
use PhpMyAdmin\Plugins\Transformations\Input\Text_Plain_RegexValidation;
use PhpMyAdmin\Plugins\Transformations\Output\Application_Octetstream_Download;
use PhpMyAdmin\Plugins\Transformations\Output\Application_Octetstream_Hex;
Expand Down Expand Up @@ -1066,6 +1067,26 @@ public function transformationDataProvider()
['<my ip>'],
'&lt;my ip&gt;',
],
[
new Text_Plain_Iptolong(),
['10.11.12.13'],
168496141,
],
[
new Text_Plain_Iptolong(),
['10.11.12.913'],
'10.11.12.913',
],
[
new Text_Plain_Iptolong(),
['my ip'],
'my ip',
],
[
new Text_Plain_Iptolong(),
['<my ip>'],
'<my ip>',
],
];

if (function_exists('imagecreatetruecolor')) {
Expand Down
2 changes: 2 additions & 0 deletions test/classes/TransformationsTest.php
Expand Up @@ -165,6 +165,7 @@ public function testGetTypes()
'Image/JPEG: Upload',
'Text/Plain: FileUpload',
'Text/Plain: Iptobinary',
'Text/Plain: Iptolong',
'Text/Plain: JsonEditor',
'Text/Plain: RegexValidation',
'Text/Plain: SqlEditor',
Expand All @@ -178,6 +179,7 @@ public function testGetTypes()
'Input/Image_JPEG_Upload.php',
'Input/Text_Plain_FileUpload.php',
'Input/Text_Plain_Iptobinary.php',
'Input/Text_Plain_Iptolong.php',
'Input/Text_Plain_JsonEditor.php',
'Input/Text_Plain_RegexValidation.php',
'Input/Text_Plain_SqlEditor.php',
Expand Down

0 comments on commit 3a1914b

Please sign in to comment.