MediaWiki  master
AjaxDispatcher.php
Go to the documentation of this file.
1 <?php
25 
26 // Use superglobals, but since it's deprecated, it's not worth fixing
27 // phpcs:disable MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
28 
42  private $mode;
43 
48  private $func_name;
49 
53  private $args;
54 
58  private $config;
59 
64  public function __construct( Config $config ) {
65  $this->config = $config;
66 
67  $this->mode = "";
68 
69  if ( !empty( $_GET["rs"] ) ) {
70  $this->mode = "get";
71  }
72 
73  if ( !empty( $_POST["rs"] ) ) {
74  $this->mode = "post";
75  }
76 
77  switch ( $this->mode ) {
78  case 'get':
79  $this->func_name = $_GET["rs"] ?? '';
80  if ( !empty( $_GET["rsargs"] ) ) {
81  $this->args = $_GET["rsargs"];
82  } else {
83  $this->args = [];
84  }
85  break;
86  case 'post':
87  $this->func_name = $_POST["rs"] ?? '';
88  if ( !empty( $_POST["rsargs"] ) ) {
89  $this->args = $_POST["rsargs"];
90  } else {
91  $this->args = [];
92  }
93  break;
94  default:
95  return;
96  # Or we could throw an exception:
97  # throw new MWException( __METHOD__ . ' called without any data (mode empty).' );
98  }
99  }
100 
112  public function performAction( User $user ) {
113  if ( empty( $this->mode ) ) {
114  return;
115  }
116 
117  $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
118  if ( !in_array( $this->func_name, $this->config->get( 'AjaxExportList' ) ) ) {
119  wfDebug( __METHOD__ . ' Bad Request for unknown function ' . $this->func_name );
120  wfHttpError(
121  400,
122  'Bad Request',
123  "unknown function " . $this->func_name
124  );
125  } elseif ( !$permissionManager->isEveryoneAllowed( 'read' ) &&
126  !$permissionManager->userHasRight( $user, 'read' )
127  ) {
128  wfHttpError(
129  403,
130  'Forbidden',
131  'You are not allowed to view pages.' );
132  } else {
133  wfDebug( __METHOD__ . ' dispatching ' . $this->func_name );
134  try {
135  $result = call_user_func_array( $this->func_name, $this->args );
136 
137  if ( $result === false || $result === null ) {
138  wfDebug( __METHOD__ . ' ERROR while dispatching ' .
139  $this->func_name . "(" . var_export( $this->args, true ) . "): " .
140  "no data returned" );
141 
142  wfHttpError( 500, 'Internal Error',
143  "{$this->func_name} returned no data" );
144  } else {
145  if ( is_string( $result ) ) {
146  $result = new AjaxResponse( $result );
147  }
148 
149  // Make sure DB commit succeeds before sending a response
150  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
151  $lbFactory->commitMasterChanges( __METHOD__ );
152 
153  $result->sendHeaders();
154  $result->printText();
155 
156  wfDebug( __METHOD__ . ' dispatch complete for ' . $this->func_name );
157  }
158  } catch ( Exception $e ) {
159  wfDebug( __METHOD__ . ' ERROR while dispatching ' .
160  $this->func_name . "(" . var_export( $this->args, true ) . "): " .
161  get_class( $e ) . ": " . $e->getMessage() );
162 
163  if ( !headers_sent() ) {
164  wfHttpError( 500, 'Internal Error',
165  $e->getMessage() );
166  } else {
167  print $e->getMessage();
168  }
169  }
170  }
171  }
172 }
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:154
AjaxDispatcher\$config
Config $config
Definition: AjaxDispatcher.php:58
AjaxDispatcher\$mode
string $mode
The way the request was made, either a 'get' or a 'post'.
Definition: AjaxDispatcher.php:42
Config
Interface for configuration instances.
Definition: Config.php:30
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:909
AjaxDispatcher
Object-Oriented Ajax functions.
Definition: AjaxDispatcher.php:37
AjaxResponse
Handle responses for Ajax requests (send headers, print content, that sort of thing)
Definition: AjaxResponse.php:31
AjaxDispatcher\__construct
__construct(Config $config)
Load up our object with user supplied data.
Definition: AjaxDispatcher.php:64
wfHttpError
wfHttpError( $code, $label, $desc)
Provide a simple HTTP error.
Definition: GlobalFunctions.php:1595
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:55
AjaxDispatcher\$args
array $args
Arguments passed.
Definition: AjaxDispatcher.php:53
AjaxDispatcher\$func_name
string $func_name
Name of the requested handler.
Definition: AjaxDispatcher.php:48
AjaxDispatcher\performAction
performAction(User $user)
Pass the request to our internal function.
Definition: AjaxDispatcher.php:112