TextImageLinkTransformationsPlugin.php 2.2 KB

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