TextLinkTransformationsPlugin.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Abstract class for the link transformations plugins
  5. *
  6. * @package PhpMyAdmin-Transformations
  7. * @subpackage Link
  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 link transformations plugins.
  16. *
  17. * @package PhpMyAdmin
  18. */
  19. abstract class TextLinkTransformationsPlugin 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 a link; the column contains the filename. The first option'
  30. . ' is a URL prefix like "https://www.example.com/". The second option'
  31. . ' is a title for the link.'
  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']['TextLink']);
  47. $url = (isset($options[0]) ? $options[0] : '') . ((isset($options[2]) && $options[2]) ? '' : $buffer);
  48. /* Do not allow javascript links */
  49. if (! Sanitize::checkLink($url, true, true)) {
  50. return htmlspecialchars($url);
  51. }
  52. return '<a href="'
  53. . htmlspecialchars($url)
  54. . '" title="'
  55. . htmlspecialchars(isset($options[1]) ? $options[1] : '')
  56. . '" target="_blank" rel="noopener noreferrer">'
  57. . htmlspecialchars(isset($options[1]) ? $options[1] : $buffer)
  58. . '</a>';
  59. }
  60. /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
  61. /**
  62. * Gets the transformation name of the specific plugin
  63. *
  64. * @return string
  65. */
  66. public static function getName()
  67. {
  68. return "TextLink";
  69. }
  70. }