TextLinkTransformationsPlugin.php 2.1 KB

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