DownloadTransformationsPlugin.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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\Plugins\TransformationsPlugin;
  8. use PhpMyAdmin\Url;
  9. use stdClass;
  10. use function array_merge;
  11. use function htmlspecialchars;
  12. /**
  13. * Provides common methods for all of the download transformations plugins.
  14. */
  15. abstract class DownloadTransformationsPlugin extends TransformationsPlugin
  16. {
  17. /**
  18. * Gets the transformation description of the specific plugin
  19. *
  20. * @return string
  21. */
  22. public static function getInfo()
  23. {
  24. return __(
  25. 'Displays a link to download the binary data of the column. You can'
  26. . ' use the first option to specify the filename, or use the second'
  27. . ' option as the name of a column which contains the filename. If'
  28. . ' you use the second option, you need to set the first option to'
  29. . ' the empty string.'
  30. );
  31. }
  32. /**
  33. * Does the actual work of each specific transformations plugin.
  34. *
  35. * @param string $buffer text to be transformed
  36. * @param array $options transformation options
  37. * @param stdClass|null $meta meta information
  38. *
  39. * @return string
  40. */
  41. public function applyTransformation($buffer, array $options = [], ?stdClass $meta = null)
  42. {
  43. global $row, $fields_meta;
  44. if (isset($options[0]) && ! empty($options[0])) {
  45. $cn = $options[0]; // filename
  46. } else {
  47. if (isset($options[1]) && ! empty($options[1])) {
  48. foreach ($fields_meta as $key => $val) {
  49. if ($val->name == $options[1]) {
  50. $pos = $key;
  51. break;
  52. }
  53. }
  54. if (isset($pos)) {
  55. $cn = $row[$pos];
  56. }
  57. }
  58. if (empty($cn)) {
  59. $cn = 'binary_file.dat';
  60. }
  61. }
  62. $link = '<a href="' . Url::getFromRoute(
  63. '/transformation/wrapper',
  64. array_merge($options['wrapper_params'], [
  65. 'ct' => 'application/octet-stream',
  66. 'cn' => $cn,
  67. ])
  68. );
  69. $link .= '" title="' . htmlspecialchars($cn);
  70. $link .= '" class="disableAjax">' . htmlspecialchars($cn);
  71. $link .= '</a>';
  72. return $link;
  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. }