Text_Plain_Iptobinary.php 3.9 KB

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