DownloadTransformationsPlugin.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Abstract class for the download transformations plugins
  5. *
  6. * @package PhpMyAdmin-Transformations
  7. * @subpackage Download
  8. */
  9. declare(strict_types=1);
  10. namespace PhpMyAdmin\Plugins\Transformations\Abs;
  11. use PhpMyAdmin\Plugins\TransformationsPlugin;
  12. use stdClass;
  13. /**
  14. * Provides common methods for all of the download transformations plugins.
  15. *
  16. * @package PhpMyAdmin
  17. */
  18. abstract class DownloadTransformationsPlugin extends TransformationsPlugin
  19. {
  20. /**
  21. * Gets the transformation description of the specific plugin
  22. *
  23. * @return string
  24. */
  25. public static function getInfo()
  26. {
  27. return __(
  28. 'Displays a link to download the binary data of the column. You can'
  29. . ' use the first option to specify the filename, or use the second'
  30. . ' option as the name of a column which contains the filename. If'
  31. . ' you use the second option, you need to set the first option to'
  32. . ' the empty string.'
  33. );
  34. }
  35. /**
  36. * Does the actual work of each specific transformations plugin.
  37. *
  38. * @param string $buffer text to be transformed
  39. * @param array $options transformation options
  40. * @param stdClass|null $meta meta information
  41. *
  42. * @return string
  43. */
  44. public function applyTransformation($buffer, array $options = [], ?stdClass $meta = null)
  45. {
  46. global $row, $fields_meta;
  47. if (isset($options[0]) && ! empty($options[0])) {
  48. $cn = $options[0]; // filename
  49. } else {
  50. if (isset($options[1]) && ! empty($options[1])) {
  51. foreach ($fields_meta as $key => $val) {
  52. if ($val->name == $options[1]) {
  53. $pos = $key;
  54. break;
  55. }
  56. }
  57. if (isset($pos)) {
  58. $cn = $row[$pos];
  59. }
  60. }
  61. if (empty($cn)) {
  62. $cn = 'binary_file.dat';
  63. }
  64. }
  65. return sprintf(
  66. '<a href="transformation_wrapper.php%s&amp;ct=application'
  67. . '/octet-stream&amp;cn=%s" title="%s" class="disableAjax">%s</a>',
  68. $options['wrapper_link'],
  69. htmlspecialchars(urlencode($cn)),
  70. htmlspecialchars($cn),
  71. htmlspecialchars($cn)
  72. );
  73. }
  74. /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
  75. /**
  76. * Gets the transformation name of the specific plugin
  77. *
  78. * @return string
  79. */
  80. public static function getName()
  81. {
  82. return "Download";
  83. }
  84. }