AjaxDispatcher.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * Handle ajax requests and send them to the proper handler.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Ajax
  22. */
  23. use MediaWiki\MediaWikiServices;
  24. // Use superglobals, but since it's deprecated, it's not worth fixing
  25. // phpcs:disable MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
  26. /**
  27. * @defgroup Ajax Ajax
  28. */
  29. /**
  30. * Object-Oriented Ajax functions.
  31. * @ingroup Ajax
  32. */
  33. class AjaxDispatcher {
  34. /**
  35. * The way the request was made, either a 'get' or a 'post'
  36. * @var string $mode
  37. */
  38. private $mode;
  39. /**
  40. * Name of the requested handler
  41. * @var string $func_name
  42. */
  43. private $func_name;
  44. /** Arguments passed
  45. * @var array $args
  46. */
  47. private $args;
  48. /**
  49. * @var Config
  50. */
  51. private $config;
  52. /**
  53. * Load up our object with user supplied data
  54. * @param Config $config
  55. */
  56. function __construct( Config $config ) {
  57. $this->config = $config;
  58. $this->mode = "";
  59. if ( !empty( $_GET["rs"] ) ) {
  60. $this->mode = "get";
  61. }
  62. if ( !empty( $_POST["rs"] ) ) {
  63. $this->mode = "post";
  64. }
  65. switch ( $this->mode ) {
  66. case 'get':
  67. $this->func_name = $_GET["rs"] ?? '';
  68. if ( !empty( $_GET["rsargs"] ) ) {
  69. $this->args = $_GET["rsargs"];
  70. } else {
  71. $this->args = [];
  72. }
  73. break;
  74. case 'post':
  75. $this->func_name = $_POST["rs"] ?? '';
  76. if ( !empty( $_POST["rsargs"] ) ) {
  77. $this->args = $_POST["rsargs"];
  78. } else {
  79. $this->args = [];
  80. }
  81. break;
  82. default:
  83. return;
  84. # Or we could throw an exception:
  85. # throw new MWException( __METHOD__ . ' called without any data (mode empty).' );
  86. }
  87. }
  88. /**
  89. * Pass the request to our internal function.
  90. * BEWARE! Data are passed as they have been supplied by the user,
  91. * they should be carefully handled in the function processing the
  92. * request.
  93. *
  94. * phan-taint-check triggers as it is not smart enough to understand
  95. * the early return if func_name not in AjaxExportList.
  96. * @suppress SecurityCheck-XSS
  97. * @param User $user
  98. */
  99. function performAction( User $user ) {
  100. if ( empty( $this->mode ) ) {
  101. return;
  102. }
  103. $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
  104. if ( !in_array( $this->func_name, $this->config->get( 'AjaxExportList' ) ) ) {
  105. wfDebug( __METHOD__ . ' Bad Request for unknown function ' . $this->func_name . "\n" );
  106. wfHttpError(
  107. 400,
  108. 'Bad Request',
  109. "unknown function " . $this->func_name
  110. );
  111. } elseif ( !$permissionManager->isEveryoneAllowed( 'read' ) &&
  112. !$permissionManager->userHasRight( $user, 'read' ) ) {
  113. wfHttpError(
  114. 403,
  115. 'Forbidden',
  116. 'You are not allowed to view pages.' );
  117. } else {
  118. wfDebug( __METHOD__ . ' dispatching ' . $this->func_name . "\n" );
  119. try {
  120. $result = call_user_func_array( $this->func_name, $this->args );
  121. if ( $result === false || $result === null ) {
  122. wfDebug( __METHOD__ . ' ERROR while dispatching ' .
  123. $this->func_name . "(" . var_export( $this->args, true ) . "): " .
  124. "no data returned\n" );
  125. wfHttpError( 500, 'Internal Error',
  126. "{$this->func_name} returned no data" );
  127. } else {
  128. if ( is_string( $result ) ) {
  129. $result = new AjaxResponse( $result );
  130. }
  131. // Make sure DB commit succeeds before sending a response
  132. $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
  133. $lbFactory->commitMasterChanges( __METHOD__ );
  134. $result->sendHeaders();
  135. $result->printText();
  136. wfDebug( __METHOD__ . ' dispatch complete for ' . $this->func_name . "\n" );
  137. }
  138. } catch ( Exception $e ) {
  139. wfDebug( __METHOD__ . ' ERROR while dispatching ' .
  140. $this->func_name . "(" . var_export( $this->args, true ) . "): " .
  141. get_class( $e ) . ": " . $e->getMessage() . "\n" );
  142. if ( !headers_sent() ) {
  143. wfHttpError( 500, 'Internal Error',
  144. $e->getMessage() );
  145. } else {
  146. print $e->getMessage();
  147. }
  148. }
  149. }
  150. }
  151. }