Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
415 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
libraries/classes/Plugins/Transformations/Input/Text_Plain_Iptolong.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.