TextImageLinkTransformationsPlugin.php 2.2 KB

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