DownloadTransformationsPlugin.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Abstract class for the download 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\Url;
  10. use function __;
  11. use function array_merge;
  12. use function htmlspecialchars;
  13. /**
  14. * Provides common methods for all of the download transformations plugins.
  15. */
  16. abstract class DownloadTransformationsPlugin 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 a link to download the binary data of the column. You can'
  27. . ' use the first option to specify the filename, or use the second'
  28. . ' option as the name of a column which contains the filename. If'
  29. . ' you use the second option, you need to set the first option to'
  30. . ' the empty string.'
  31. );
  32. }
  33. /**
  34. * Does the actual work of each specific transformations plugin.
  35. *
  36. * @param string $buffer text to be transformed
  37. * @param array $options transformation options
  38. * @param FieldMetadata|null $meta meta information
  39. *
  40. * @return string
  41. */
  42. public function applyTransformation($buffer, array $options = [], ?FieldMetadata $meta = null)
  43. {
  44. global $row, $fields_meta;
  45. if (isset($options[0]) && ! empty($options[0])) {
  46. $cn = $options[0]; // filename
  47. } else {
  48. if (isset($options[1]) && ! empty($options[1])) {
  49. foreach ($fields_meta as $key => $val) {
  50. if ($val->name == $options[1]) {
  51. $pos = $key;
  52. break;
  53. }
  54. }
  55. if (isset($pos)) {
  56. $cn = $row[$pos];
  57. }
  58. }
  59. if (empty($cn)) {
  60. $cn = 'binary_file.dat';
  61. }
  62. }
  63. $link = '<a href="' . Url::getFromRoute(
  64. '/transformation/wrapper',
  65. array_merge($options['wrapper_params'], [
  66. 'ct' => 'application/octet-stream',
  67. 'cn' => $cn,
  68. ])
  69. );
  70. $link .= '" title="' . htmlspecialchars($cn);
  71. $link .= '" class="disableAjax">' . htmlspecialchars($cn);
  72. $link .= '</a>';
  73. return $link;
  74. }
  75. /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
  76. /**
  77. * Gets the transformation name of the specific plugin
  78. *
  79. * @return string
  80. */
  81. public static function getName()
  82. {
  83. return 'Download';
  84. }
  85. }