ExternalTransformationsPlugin.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/Abs/ExternalTransformationsPlugin.php'
  31. . ' 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. The second option should be blank for historical reasons.'
  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. if (isset($options[1]) && strlen((string) $options[1]) > 0) {
  100. trigger_error(sprintf(
  101. __(
  102. 'You are using the external transformation command line options field, which has been deprecated for security reasons. '
  103. . 'Add all command line options directly to the definition in %s.'
  104. ),
  105. '[code]libraries/classes/Plugins/Transformations/Abs/ExternalTransformationsPlugin.php[/code]'
  106. ), E_USER_DEPRECATED);
  107. }
  108. // needs PHP >= 4.3.0
  109. $newstring = '';
  110. $descriptorspec = array(
  111. 0 => array("pipe", "r"),
  112. 1 => array("pipe", "w"),
  113. );
  114. $process = proc_open($program . ' ' . $options[1], $descriptorspec, $pipes);
  115. if (is_resource($process)) {
  116. fwrite($pipes[0], $buffer);
  117. fclose($pipes[0]);
  118. while (!feof($pipes[1])) {
  119. $newstring .= fgets($pipes[1], 1024);
  120. }
  121. fclose($pipes[1]);
  122. // we don't currently use the return value
  123. proc_close($process);
  124. }
  125. if ($options[2] == 1 || $options[2] == '2') {
  126. $retstring = htmlspecialchars($newstring);
  127. } else {
  128. $retstring = $newstring;
  129. }
  130. return $retstring;
  131. }
  132. /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
  133. /**
  134. * Gets the transformation name of the specific plugin
  135. *
  136. * @return string
  137. */
  138. public static function getName()
  139. {
  140. return "External";
  141. }
  142. }