ExternalTransformationsPlugin.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * Abstract class for the external 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 function __;
  10. use function count;
  11. use function fclose;
  12. use function feof;
  13. use function fgets;
  14. use function fwrite;
  15. use function htmlspecialchars;
  16. use function proc_close;
  17. use function proc_open;
  18. use function sprintf;
  19. use function strlen;
  20. use function trigger_error;
  21. use const E_USER_DEPRECATED;
  22. /**
  23. * Provides common methods for all of the external transformations plugins.
  24. */
  25. abstract class ExternalTransformationsPlugin extends TransformationsPlugin
  26. {
  27. /**
  28. * Gets the transformation description of the specific plugin
  29. *
  30. * @return string
  31. */
  32. public static function getInfo()
  33. {
  34. return __(
  35. 'LINUX ONLY: Launches an external application and feeds it the column'
  36. . ' data via standard input. Returns the standard output of the'
  37. . ' application. The default is Tidy, to pretty-print HTML code.'
  38. . ' For security reasons, you have to manually edit the file'
  39. . ' libraries/classes/Plugins/Transformations/Abs/ExternalTransformationsPlugin.php'
  40. . ' and list the tools you want to make available.'
  41. . ' The first option is then the number of the program you want to'
  42. . ' use. The second option should be blank for historical reasons.'
  43. . ' The third option, if set to 1, will convert the output using'
  44. . ' htmlspecialchars() (Default 1). The fourth option, if set to 1,'
  45. . ' will prevent wrapping and ensure that the output appears all on'
  46. . ' one line (Default 1).'
  47. );
  48. }
  49. /**
  50. * Enables no-wrapping
  51. *
  52. * @param array $options transformation options
  53. */
  54. public function applyTransformationNoWrap(array $options = []): bool
  55. {
  56. if (! isset($options[3]) || $options[3] == '') {
  57. $nowrap = true;
  58. } elseif ($options[3] == '1' || $options[3] == 1) {
  59. $nowrap = true;
  60. } else {
  61. $nowrap = false;
  62. }
  63. return $nowrap;
  64. }
  65. /**
  66. * Does the actual work of each specific transformations plugin.
  67. *
  68. * @param string $buffer text to be transformed
  69. * @param array $options transformation options
  70. * @param FieldMetadata|null $meta meta information
  71. *
  72. * @return string
  73. */
  74. public function applyTransformation($buffer, array $options = [], ?FieldMetadata $meta = null)
  75. {
  76. // possibly use a global transform and feed it with special options
  77. // further operations on $buffer using the $options[] array.
  78. $allowed_programs = [];
  79. // WARNING:
  80. //
  81. // It's up to administrator to allow anything here. Note that users may
  82. // specify any parameters, so when programs allow output redirection or
  83. // any other possibly dangerous operations, you should write wrapper
  84. // script that will publish only functions you really want.
  85. //
  86. // Add here program definitions like (note that these are NOT safe
  87. // programs):
  88. //
  89. //$allowed_programs[0] = '/usr/local/bin/tidy';
  90. //$allowed_programs[1] = '/usr/local/bin/validate';
  91. // no-op when no allowed programs
  92. if (count($allowed_programs) === 0) {
  93. return $buffer;
  94. }
  95. $cfg = $GLOBALS['cfg'];
  96. $options = $this->getOptions($options, $cfg['DefaultTransformations']['External']);
  97. if (isset($allowed_programs[$options[0]])) {
  98. $program = $allowed_programs[$options[0]];
  99. } else {
  100. $program = $allowed_programs[0];
  101. }
  102. if (isset($options[1]) && strlen((string) $options[1]) > 0) {
  103. trigger_error(sprintf(
  104. __(
  105. 'You are using the external transformation command line'
  106. . ' options field, which has been deprecated for security reasons.'
  107. . ' Add all command line options directly to the definition in %s.'
  108. ),
  109. '[code]libraries/classes/Plugins/Transformations/Abs/ExternalTransformationsPlugin.php[/code]'
  110. ), E_USER_DEPRECATED);
  111. }
  112. // needs PHP >= 4.3.0
  113. $newstring = '';
  114. $descriptorspec = [
  115. 0 => [
  116. 'pipe',
  117. 'r',
  118. ],
  119. 1 => [
  120. 'pipe',
  121. 'w',
  122. ],
  123. ];
  124. $process = proc_open($program . ' ' . $options[1], $descriptorspec, $pipes);
  125. if ($process !== false) {
  126. fwrite($pipes[0], $buffer);
  127. fclose($pipes[0]);
  128. while (! feof($pipes[1])) {
  129. $newstring .= fgets($pipes[1], 1024);
  130. }
  131. fclose($pipes[1]);
  132. // we don't currently use the return value
  133. proc_close($process);
  134. }
  135. if ($options[2] == 1 || $options[2] == '2') {
  136. $retstring = htmlspecialchars($newstring);
  137. } else {
  138. $retstring = $newstring;
  139. }
  140. return $retstring;
  141. }
  142. /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
  143. /**
  144. * Gets the transformation name of the specific plugin
  145. *
  146. * @return string
  147. */
  148. public static function getName()
  149. {
  150. return 'External';
  151. }
  152. }