Text_Plain_Iptobinary.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Handles the IPv4/IPv6 to binary transformation for text plain
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins\Transformations\Input;
  7. use PhpMyAdmin\FieldMetadata;
  8. use PhpMyAdmin\Plugins\IOTransformationsPlugin;
  9. use PhpMyAdmin\Utils\FormatConverter;
  10. use function __;
  11. use function htmlspecialchars;
  12. use function inet_ntop;
  13. use function pack;
  14. use function strlen;
  15. /**
  16. * Handles the IPv4/IPv6 to binary transformation for text plain
  17. */
  18. class Text_Plain_Iptobinary extends IOTransformationsPlugin
  19. {
  20. /**
  21. * Gets the transformation description of the plugin
  22. *
  23. * @return string
  24. */
  25. public static function getInfo()
  26. {
  27. return __('Converts an Internet network address in (IPv4/IPv6) format to binary');
  28. }
  29. /**
  30. * Does the actual work of each specific transformations plugin.
  31. *
  32. * @param string $buffer text to be transformed. a binary string containing
  33. * an IP address, as returned from MySQL's INET6_ATON
  34. * function
  35. * @param array $options transformation options
  36. * @param FieldMetadata|null $meta meta information
  37. *
  38. * @return string IP address
  39. */
  40. public function applyTransformation($buffer, array $options = [], ?FieldMetadata $meta = null)
  41. {
  42. return FormatConverter::ipToBinary($buffer);
  43. }
  44. /**
  45. * Returns the html for input field to override default textarea.
  46. * Note: Return empty string if default textarea is required.
  47. *
  48. * @param array $column column details
  49. * @param int $row_id row number
  50. * @param string $column_name_appendix the name attribute
  51. * @param array $options transformation options
  52. * @param string $value Current field value
  53. * @param string $text_dir text direction
  54. * @param int $tabindex tab index
  55. * @param int $tabindex_for_value offset for the values tabindex
  56. * @param int $idindex id index
  57. *
  58. * @return string the html for input field
  59. */
  60. public function getInputHtml(
  61. array $column,
  62. $row_id,
  63. $column_name_appendix,
  64. array $options,
  65. $value,
  66. $text_dir,
  67. $tabindex,
  68. $tabindex_for_value,
  69. $idindex
  70. ) {
  71. $html = '';
  72. $val = '';
  73. if (! empty($value)) {
  74. $length = strlen($value);
  75. if ($length == 4 || $length == 16) {
  76. $ip = @inet_ntop(pack('A' . $length, $value));
  77. if ($ip !== false) {
  78. $val = $ip;
  79. }
  80. }
  81. $html = '<input type="hidden" name="fields_prev' . $column_name_appendix
  82. . '" value="' . htmlspecialchars($val) . '">';
  83. }
  84. $class = 'transform_IPToBin';
  85. return $html . '<input type="text" name="fields' . $column_name_appendix . '"'
  86. . ' value="' . htmlspecialchars($val) . '"'
  87. . ' size="40"'
  88. . ' dir="' . $text_dir . '"'
  89. . ' class="' . $class . '"'
  90. . ' id="field_' . $idindex . '_3"'
  91. . ' tabindex="' . ($tabindex + $tabindex_for_value) . '">';
  92. }
  93. /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
  94. /**
  95. * Gets the transformation name of the plugin
  96. *
  97. * @return string
  98. */
  99. public static function getName()
  100. {
  101. return 'IPv4/IPv6 To Binary';
  102. }
  103. /**
  104. * Gets the plugin`s MIME type
  105. *
  106. * @return string
  107. */
  108. public static function getMIMEType()
  109. {
  110. return 'Text';
  111. }
  112. /**
  113. * Gets the plugin`s MIME subtype
  114. *
  115. * @return string
  116. */
  117. public static function getMIMESubtype()
  118. {
  119. return 'Plain';
  120. }
  121. }