/phpMyAdmin/libraries/plugins/transformations/abstract/ExternalTransformationsPlugin.class.php

https://bitbucket.org/subhan_12/mwi-panel · PHP · 182 lines · 92 code · 22 blank · 68 comment · 32 complexity · 5b6712cb50986cfd29940eebce089aba MD5 · raw file

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