AjaxDispatcher.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * @defgroup Ajax Ajax
  4. *
  5. * @file
  6. * @ingroup Ajax
  7. * Handle ajax requests and send them to the proper handler.
  8. */
  9. if( !(defined( 'MEDIAWIKI' ) && $wgUseAjax ) ) {
  10. die( 1 );
  11. }
  12. require_once( 'AjaxFunctions.php' );
  13. /**
  14. * Object-Oriented Ajax functions.
  15. * @ingroup Ajax
  16. */
  17. class AjaxDispatcher {
  18. /** The way the request was made, either a 'get' or a 'post' */
  19. private $mode;
  20. /** Name of the requested handler */
  21. private $func_name;
  22. /** Arguments passed */
  23. private $args;
  24. /** Load up our object with user supplied data */
  25. function __construct() {
  26. wfProfileIn( __METHOD__ );
  27. $this->mode = "";
  28. if (! empty($_GET["rs"])) {
  29. $this->mode = "get";
  30. }
  31. if (!empty($_POST["rs"])) {
  32. $this->mode = "post";
  33. }
  34. switch( $this->mode ) {
  35. case 'get':
  36. $this->func_name = isset( $_GET["rs"] ) ? $_GET["rs"] : '';
  37. if (! empty($_GET["rsargs"])) {
  38. $this->args = $_GET["rsargs"];
  39. } else {
  40. $this->args = array();
  41. }
  42. break;
  43. case 'post':
  44. $this->func_name = isset( $_POST["rs"] ) ? $_POST["rs"] : '';
  45. if (! empty($_POST["rsargs"])) {
  46. $this->args = $_POST["rsargs"];
  47. } else {
  48. $this->args = array();
  49. }
  50. break;
  51. default:
  52. wfProfileOut( __METHOD__ );
  53. return;
  54. # Or we could throw an exception:
  55. #throw new MWException( __METHOD__ . ' called without any data (mode empty).' );
  56. }
  57. wfProfileOut( __METHOD__ );
  58. }
  59. /** Pass the request to our internal function.
  60. * BEWARE! Data are passed as they have been supplied by the user,
  61. * they should be carefully handled in the function processing the
  62. * request.
  63. */
  64. function performAction() {
  65. global $wgAjaxExportList, $wgOut;
  66. if ( empty( $this->mode ) ) {
  67. return;
  68. }
  69. wfProfileIn( __METHOD__ );
  70. if (! in_array( $this->func_name, $wgAjaxExportList ) ) {
  71. wfDebug( __METHOD__ . ' Bad Request for unknown function ' . $this->func_name . "\n" );
  72. wfHttpError( 400, 'Bad Request',
  73. "unknown function " . (string) $this->func_name );
  74. } else {
  75. wfDebug( __METHOD__ . ' dispatching ' . $this->func_name . "\n" );
  76. if ( strpos( $this->func_name, '::' ) !== false ) {
  77. $func = explode( '::', $this->func_name, 2 );
  78. } else {
  79. $func = $this->func_name;
  80. }
  81. try {
  82. $result = call_user_func_array($func, $this->args);
  83. if ( $result === false || $result === NULL ) {
  84. wfDebug( __METHOD__ . ' ERROR while dispatching '
  85. . $this->func_name . "(" . var_export( $this->args, true ) . "): "
  86. . "no data returned\n" );
  87. wfHttpError( 500, 'Internal Error',
  88. "{$this->func_name} returned no data" );
  89. }
  90. else {
  91. if ( is_string( $result ) ) {
  92. $result= new AjaxResponse( $result );
  93. }
  94. $result->sendHeaders();
  95. $result->printText();
  96. wfDebug( __METHOD__ . ' dispatch complete for ' . $this->func_name . "\n" );
  97. }
  98. } catch (Exception $e) {
  99. wfDebug( __METHOD__ . ' ERROR while dispatching '
  100. . $this->func_name . "(" . var_export( $this->args, true ) . "): "
  101. . get_class($e) . ": " . $e->getMessage() . "\n" );
  102. if (!headers_sent()) {
  103. wfHttpError( 500, 'Internal Error',
  104. $e->getMessage() );
  105. } else {
  106. print $e->getMessage();
  107. }
  108. }
  109. }
  110. wfProfileOut( __METHOD__ );
  111. $wgOut = null;
  112. }
  113. }