TextLinkTransformationsPlugin.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * Abstract class for the link transformations plugins
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins\Transformations\Abs;
  7. use PhpMyAdmin\FieldMetadata;
  8. use PhpMyAdmin\Plugins\TransformationsPlugin;
  9. use PhpMyAdmin\Sanitize;
  10. use function __;
  11. use function htmlspecialchars;
  12. /**
  13. * Provides common methods for all of the link transformations plugins.
  14. */
  15. abstract class TextLinkTransformationsPlugin extends TransformationsPlugin
  16. {
  17. /**
  18. * Gets the transformation description of the specific plugin
  19. *
  20. * @return string
  21. */
  22. public static function getInfo()
  23. {
  24. return __(
  25. 'Displays a link; the column contains the filename. The first option'
  26. . ' is a URL prefix like "https://www.example.com/". The second option'
  27. . ' is a title for the link.'
  28. );
  29. }
  30. /**
  31. * Does the actual work of each specific transformations plugin.
  32. *
  33. * @param string $buffer text to be transformed
  34. * @param array $options transformation options
  35. * @param FieldMetadata|null $meta meta information
  36. *
  37. * @return string
  38. */
  39. public function applyTransformation($buffer, array $options = [], ?FieldMetadata $meta = null)
  40. {
  41. $cfg = $GLOBALS['cfg'];
  42. $options = $this->getOptions($options, $cfg['DefaultTransformations']['TextLink']);
  43. $url = ($options[0] ?? '') . (isset($options[2]) && $options[2] ? '' : $buffer);
  44. /* Do not allow javascript links */
  45. if (! Sanitize::checkLink($url, true, true)) {
  46. return htmlspecialchars($url);
  47. }
  48. return '<a href="'
  49. . htmlspecialchars($url)
  50. . '" title="'
  51. . htmlspecialchars($options[1] ?? '')
  52. . '" target="_blank" rel="noopener noreferrer">'
  53. . htmlspecialchars($options[1] ?? $buffer)
  54. . '</a>';
  55. }
  56. /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
  57. /**
  58. * Gets the transformation name of the specific plugin
  59. *
  60. * @return string
  61. */
  62. public static function getName()
  63. {
  64. return 'TextLink';
  65. }
  66. }