2006-03-27 18:53:15 +00:00
|
|
|
<?php
|
2007-05-05 00:06:20 +00:00
|
|
|
/**
|
2012-05-22 18:06:30 +00:00
|
|
|
* Handle ajax requests and send them to the proper handler.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Ajax
|
2012-05-22 18:06:30 +00:00
|
|
|
*/
|
|
|
|
|
|
2017-02-07 13:31:46 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
|
2018-05-19 21:41:41 +00:00
|
|
|
// Use superglobals, but since it's deprecated, it's not worth fixing
|
|
|
|
|
// phpcs:disable MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
|
|
|
|
|
|
2012-05-22 18:06:30 +00:00
|
|
|
/**
|
|
|
|
|
* @defgroup Ajax Ajax
|
2007-05-05 00:06:20 +00:00
|
|
|
*/
|
2006-03-27 18:53:15 +00:00
|
|
|
|
2007-04-04 05:22:37 +00:00
|
|
|
/**
|
2007-04-24 06:53:31 +00:00
|
|
|
* Object-Oriented Ajax functions.
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Ajax
|
2007-04-04 05:22:37 +00:00
|
|
|
*/
|
2006-03-27 18:53:15 +00:00
|
|
|
class AjaxDispatcher {
|
2012-04-15 00:12:01 +00:00
|
|
|
/**
|
|
|
|
|
* The way the request was made, either a 'get' or a 'post'
|
2019-11-29 13:33:43 +00:00
|
|
|
* @var string
|
2012-04-15 00:12:01 +00:00
|
|
|
*/
|
2010-08-13 21:39:51 +00:00
|
|
|
private $mode;
|
|
|
|
|
|
2012-04-15 00:12:01 +00:00
|
|
|
/**
|
|
|
|
|
* Name of the requested handler
|
2019-11-29 13:33:43 +00:00
|
|
|
* @var string
|
2012-04-15 00:12:01 +00:00
|
|
|
*/
|
2010-08-13 21:39:51 +00:00
|
|
|
private $func_name;
|
2007-05-05 00:06:20 +00:00
|
|
|
|
2012-04-15 00:12:01 +00:00
|
|
|
/** Arguments passed
|
2019-11-29 13:33:43 +00:00
|
|
|
* @var array
|
2012-04-15 00:12:01 +00:00
|
|
|
*/
|
2010-08-13 21:39:51 +00:00
|
|
|
private $args;
|
2006-03-27 18:53:15 +00:00
|
|
|
|
2014-08-26 02:14:41 +00:00
|
|
|
/**
|
|
|
|
|
* @var Config
|
|
|
|
|
*/
|
|
|
|
|
private $config;
|
|
|
|
|
|
2012-04-15 00:12:01 +00:00
|
|
|
/**
|
|
|
|
|
* Load up our object with user supplied data
|
2017-09-09 20:47:04 +00:00
|
|
|
* @param Config $config
|
2012-04-15 00:12:01 +00:00
|
|
|
*/
|
2019-11-30 23:03:59 +00:00
|
|
|
public function __construct( Config $config ) {
|
2014-08-26 02:14:41 +00:00
|
|
|
$this->config = $config;
|
|
|
|
|
|
2010-08-13 21:39:51 +00:00
|
|
|
$this->mode = "";
|
|
|
|
|
|
2014-07-21 12:47:42 +00:00
|
|
|
if ( !empty( $_GET["rs"] ) ) {
|
2010-08-13 21:39:51 +00:00
|
|
|
$this->mode = "get";
|
2006-03-27 18:53:15 +00:00
|
|
|
}
|
2010-08-13 21:39:51 +00:00
|
|
|
|
|
|
|
|
if ( !empty( $_POST["rs"] ) ) {
|
|
|
|
|
$this->mode = "post";
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-26 14:42:31 +00:00
|
|
|
switch ( $this->mode ) {
|
2010-08-13 21:39:51 +00:00
|
|
|
case 'get':
|
2017-10-06 22:17:58 +00:00
|
|
|
$this->func_name = $_GET["rs"] ?? '';
|
2014-07-21 12:47:42 +00:00
|
|
|
if ( !empty( $_GET["rsargs"] ) ) {
|
2010-08-13 21:39:51 +00:00
|
|
|
$this->args = $_GET["rsargs"];
|
|
|
|
|
} else {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->args = [];
|
2010-08-13 21:39:51 +00:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'post':
|
2017-10-06 22:17:58 +00:00
|
|
|
$this->func_name = $_POST["rs"] ?? '';
|
2014-07-21 12:47:42 +00:00
|
|
|
if ( !empty( $_POST["rsargs"] ) ) {
|
2010-08-13 21:39:51 +00:00
|
|
|
$this->args = $_POST["rsargs"];
|
|
|
|
|
} else {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->args = [];
|
2010-08-13 21:39:51 +00:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return;
|
|
|
|
|
# Or we could throw an exception:
|
|
|
|
|
# throw new MWException( __METHOD__ . ' called without any data (mode empty).' );
|
2006-03-27 18:53:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-15 00:12:01 +00:00
|
|
|
/**
|
|
|
|
|
* Pass the request to our internal function.
|
2007-05-05 00:06:20 +00:00
|
|
|
* BEWARE! Data are passed as they have been supplied by the user,
|
|
|
|
|
* they should be carefully handled in the function processing the
|
|
|
|
|
* request.
|
2014-08-26 02:14:41 +00:00
|
|
|
*
|
2018-07-14 22:02:01 +00:00
|
|
|
* phan-taint-check triggers as it is not smart enough to understand
|
|
|
|
|
* the early return if func_name not in AjaxExportList.
|
|
|
|
|
* @suppress SecurityCheck-XSS
|
2014-08-26 02:14:41 +00:00
|
|
|
* @param User $user
|
2007-05-05 00:06:20 +00:00
|
|
|
*/
|
2019-11-30 22:32:44 +00:00
|
|
|
public function performAction( User $user ) {
|
2010-08-13 21:39:51 +00:00
|
|
|
if ( empty( $this->mode ) ) {
|
2006-03-27 18:53:15 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2010-02-14 22:07:30 +00:00
|
|
|
|
2019-08-16 18:13:56 +00:00
|
|
|
$permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
|
2014-08-26 02:14:41 +00:00
|
|
|
if ( !in_array( $this->func_name, $this->config->get( 'AjaxExportList' ) ) ) {
|
2008-06-28 19:40:14 +00:00
|
|
|
wfDebug( __METHOD__ . ' Bad Request for unknown function ' . $this->func_name . "\n" );
|
2010-05-30 14:28:54 +00:00
|
|
|
wfHttpError(
|
|
|
|
|
400,
|
|
|
|
|
'Bad Request',
|
2013-08-31 16:36:02 +00:00
|
|
|
"unknown function " . $this->func_name
|
2010-05-30 14:28:54 +00:00
|
|
|
);
|
2019-08-16 18:13:56 +00:00
|
|
|
} elseif ( !$permissionManager->isEveryoneAllowed( 'read' ) &&
|
|
|
|
|
!$permissionManager->userHasRight( $user, 'read' ) ) {
|
2011-11-28 23:18:55 +00:00
|
|
|
wfHttpError(
|
|
|
|
|
403,
|
|
|
|
|
'Forbidden',
|
2013-07-12 15:06:41 +00:00
|
|
|
'You are not allowed to view pages.' );
|
2006-03-27 18:53:15 +00:00
|
|
|
} else {
|
2008-06-28 19:40:14 +00:00
|
|
|
wfDebug( __METHOD__ . ' dispatching ' . $this->func_name . "\n" );
|
2006-08-29 15:43:34 +00:00
|
|
|
try {
|
2012-07-17 14:28:58 +00:00
|
|
|
$result = call_user_func_array( $this->func_name, $this->args );
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2009-12-11 21:07:27 +00:00
|
|
|
if ( $result === false || $result === null ) {
|
2015-06-19 18:52:43 +00:00
|
|
|
wfDebug( __METHOD__ . ' ERROR while dispatching ' .
|
|
|
|
|
$this->func_name . "(" . var_export( $this->args, true ) . "): " .
|
|
|
|
|
"no data returned\n" );
|
2008-06-28 19:40:14 +00:00
|
|
|
|
2007-02-21 01:02:47 +00:00
|
|
|
wfHttpError( 500, 'Internal Error',
|
|
|
|
|
"{$this->func_name} returned no data" );
|
2010-05-30 14:28:54 +00:00
|
|
|
} else {
|
2006-08-29 15:43:34 +00:00
|
|
|
if ( is_string( $result ) ) {
|
2010-02-14 22:07:30 +00:00
|
|
|
$result = new AjaxResponse( $result );
|
2006-08-29 15:43:34 +00:00
|
|
|
}
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2016-01-12 21:23:39 +00:00
|
|
|
// Make sure DB commit succeeds before sending a response
|
2017-02-07 13:31:46 +00:00
|
|
|
$lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
|
|
|
|
|
$lbFactory->commitMasterChanges( __METHOD__ );
|
2016-01-12 21:23:39 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
$result->sendHeaders();
|
|
|
|
|
$result->printText();
|
2008-06-28 19:40:14 +00:00
|
|
|
|
|
|
|
|
wfDebug( __METHOD__ . ' dispatch complete for ' . $this->func_name . "\n" );
|
2006-08-29 15:43:34 +00:00
|
|
|
}
|
2010-02-14 22:07:30 +00:00
|
|
|
} catch ( Exception $e ) {
|
2015-06-19 18:52:43 +00:00
|
|
|
wfDebug( __METHOD__ . ' ERROR while dispatching ' .
|
|
|
|
|
$this->func_name . "(" . var_export( $this->args, true ) . "): " .
|
|
|
|
|
get_class( $e ) . ": " . $e->getMessage() . "\n" );
|
2008-06-28 19:40:14 +00:00
|
|
|
|
2010-02-14 22:07:30 +00:00
|
|
|
if ( !headers_sent() ) {
|
2007-02-21 01:02:47 +00:00
|
|
|
wfHttpError( 500, 'Internal Error',
|
|
|
|
|
$e->getMessage() );
|
2006-08-29 15:43:34 +00:00
|
|
|
} else {
|
|
|
|
|
print $e->getMessage();
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-03-27 18:53:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|