Text_Plain_Iptolong.php 3.6 KB

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