ExternalTransformationsPlugin.php 4.6 KB

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