本文整理汇总了PHP中RequestContext::sanitizeLangCode方法的典型用法代码示例。如果您正苦于以下问题:PHP RequestContext::sanitizeLangCode方法的具体用法?PHP RequestContext::sanitizeLangCode怎么用?PHP RequestContext::sanitizeLangCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RequestContext
的用法示例。
在下文中一共展示了RequestContext::sanitizeLangCode方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
/**
* Constructs an instance of ApiMain that utilizes the module and format specified by $request.
*
* @param IContextSource|WebRequest $context If this is an instance of
* FauxRequest, errors are thrown and no printing occurs
* @param bool $enableWrite Should be set to true if the api may modify data
*/
public function __construct($context = null, $enableWrite = false)
{
if ($context === null) {
$context = RequestContext::getMain();
} elseif ($context instanceof WebRequest) {
// BC for pre-1.19
$request = $context;
$context = RequestContext::getMain();
}
// We set a derivative context so we can change stuff later
$this->setContext(new DerivativeContext($context));
if (isset($request)) {
$this->getContext()->setRequest($request);
}
$this->mInternalMode = $this->getRequest() instanceof FauxRequest;
// Special handling for the main module: $parent === $this
parent::__construct($this, $this->mInternalMode ? 'main_int' : 'main');
if (!$this->mInternalMode) {
// Impose module restrictions.
// If the current user cannot read,
// Remove all modules other than login
global $wgUser;
if ($this->lacksSameOriginSecurity()) {
// If we're in a mode that breaks the same-origin policy, strip
// user credentials for security.
wfDebug("API: stripping user credentials when the same-origin policy is not applied\n");
$wgUser = new User();
$this->getContext()->setUser($wgUser);
}
}
$uselang = $this->getParameter('uselang');
if ($uselang === 'user') {
// Assume the parent context is going to return the user language
// for uselang=user (see T85635).
} else {
if ($uselang === 'content') {
global $wgContLang;
$uselang = $wgContLang->getCode();
}
$code = RequestContext::sanitizeLangCode($uselang);
$this->getContext()->setLanguage($code);
if (!$this->mInternalMode) {
global $wgLang;
$wgLang = $this->getContext()->getLanguage();
RequestContext::getMain()->setLanguage($wgLang);
}
}
$config = $this->getConfig();
$this->mModuleMgr = new ApiModuleManager($this);
$this->mModuleMgr->addModules(self::$Modules, 'action');
$this->mModuleMgr->addModules($config->get('APIModules'), 'action');
$this->mModuleMgr->addModules(self::$Formats, 'format');
$this->mModuleMgr->addModules($config->get('APIFormatModules'), 'format');
Hooks::run('ApiMain::moduleManager', array($this->mModuleMgr));
$this->mResult = new ApiResult($this->getConfig()->get('APIMaxResultSize'));
$this->mErrorFormatter = new ApiErrorFormatter_BackCompat($this->mResult);
$this->mResult->setErrorFormatter($this->mErrorFormatter);
$this->mResult->setMainForContinuation($this);
$this->mContinuationManager = null;
$this->mEnableWrite = $enableWrite;
$this->mSquidMaxage = -1;
// flag for executeActionWithErrorHandling()
$this->mCommit = false;
}
示例2: setLanguage
/**
* Set the Language object
*
* @param Language|string $l Language instance or language code
* @throws MWException
* @since 1.19
*/
public function setLanguage($l)
{
if ($l instanceof Language) {
$this->lang = $l;
} elseif (is_string($l)) {
$l = RequestContext::sanitizeLangCode($l);
$obj = Language::factory($l);
$this->lang = $obj;
} else {
throw new MWException(__METHOD__ . " was passed an invalid type of data.");
}
}
示例3: __construct
/**
* Constructs an instance of ApiMain that utilizes the module and format specified by $request.
*
* @param IContextSource|WebRequest $context If this is an instance of
* FauxRequest, errors are thrown and no printing occurs
* @param bool $enableWrite Should be set to true if the api may modify data
*/
public function __construct($context = null, $enableWrite = false)
{
if ($context === null) {
$context = RequestContext::getMain();
} elseif ($context instanceof WebRequest) {
// BC for pre-1.19
$request = $context;
$context = RequestContext::getMain();
}
// We set a derivative context so we can change stuff later
$this->setContext(new DerivativeContext($context));
if (isset($request)) {
$this->getContext()->setRequest($request);
} else {
$request = $this->getRequest();
}
$this->mInternalMode = $request instanceof FauxRequest;
// Special handling for the main module: $parent === $this
parent::__construct($this, $this->mInternalMode ? 'main_int' : 'main');
$config = $this->getConfig();
if (!$this->mInternalMode) {
// Log if a request with a non-whitelisted Origin header is seen
// with session cookies.
$originHeader = $request->getHeader('Origin');
if ($originHeader === false) {
$origins = [];
} else {
$originHeader = trim($originHeader);
$origins = preg_split('/\\s+/', $originHeader);
}
$sessionCookies = array_intersect(array_keys($_COOKIE), MediaWiki\Session\SessionManager::singleton()->getVaryCookies());
if ($origins && $sessionCookies && (count($origins) !== 1 || !self::matchOrigin($origins[0], $config->get('CrossSiteAJAXdomains'), $config->get('CrossSiteAJAXdomainExceptions')))) {
LoggerFactory::getInstance('cors')->warning('Non-whitelisted CORS request with session cookies', ['origin' => $originHeader, 'cookies' => $sessionCookies, 'ip' => $request->getIP(), 'userAgent' => $this->getUserAgent(), 'wiki' => wfWikiID()]);
}
// If we're in a mode that breaks the same-origin policy, strip
// user credentials for security.
if ($this->lacksSameOriginSecurity()) {
global $wgUser;
wfDebug("API: stripping user credentials when the same-origin policy is not applied\n");
$wgUser = new User();
$this->getContext()->setUser($wgUser);
}
}
$uselang = $this->getParameter('uselang');
if ($uselang === 'user') {
// Assume the parent context is going to return the user language
// for uselang=user (see T85635).
} else {
if ($uselang === 'content') {
global $wgContLang;
$uselang = $wgContLang->getCode();
}
$code = RequestContext::sanitizeLangCode($uselang);
$this->getContext()->setLanguage($code);
if (!$this->mInternalMode) {
global $wgLang;
$wgLang = $this->getContext()->getLanguage();
RequestContext::getMain()->setLanguage($wgLang);
}
}
$this->mModuleMgr = new ApiModuleManager($this);
$this->mModuleMgr->addModules(self::$Modules, 'action');
$this->mModuleMgr->addModules($config->get('APIModules'), 'action');
$this->mModuleMgr->addModules(self::$Formats, 'format');
$this->mModuleMgr->addModules($config->get('APIFormatModules'), 'format');
Hooks::run('ApiMain::moduleManager', [$this->mModuleMgr]);
$this->mResult = new ApiResult($this->getConfig()->get('APIMaxResultSize'));
$this->mErrorFormatter = new ApiErrorFormatter_BackCompat($this->mResult);
$this->mResult->setErrorFormatter($this->mErrorFormatter);
$this->mContinuationManager = null;
$this->mEnableWrite = $enableWrite;
$this->mSquidMaxage = -1;
// flag for executeActionWithErrorHandling()
$this->mCommit = false;
}
示例4: getLanguage
/**
* @return string
*/
public function getLanguage()
{
if ($this->language === null) {
// Must be a valid language code after this point (bug 62849)
$this->language = RequestContext::sanitizeLangCode($this->request->getVal('lang'));
}
return $this->language;
}
示例5: execute
//.........这里部分代码省略.........
return;
}
// Load memc data
$wikiid = wfWikiID();
$key = CentralAuthUser::memcKey('centralautologin-token', $token, $wikiid);
$memcData = $wgMemc->get($key);
$wgMemc->delete($key);
// Check memc data
if (!$memcData || $memcData['wikiid'] !== $wikiid || !isset($memcData['userName']) || !isset($memcData['token'])) {
$this->doFinalOutput(false, 'Lost session');
return;
}
// Load and check CentralAuthUser. But don't check if it's
// attached, because then if the user is missing en.site they
// won't be auto logged in to any of the non-en versions either.
$centralUser = new CentralAuthUser($memcData['userName']);
if (!$centralUser->getId() || $centralUser->getId() != $memcData['gu_id']) {
$msg = "Wrong user: expected {$memcData['gu_id']}, got {$centralUser->getId()}";
wfDebug(__METHOD__ . ": {$msg}\n");
$this->doFinalOutput(false, 'Lost session');
return;
}
$loginResult = $centralUser->authenticateWithToken($memcData['token']);
if ($loginResult != 'ok') {
$msg = "Bad token: {$loginResult}";
wfDebug(__METHOD__ . ": {$msg}\n");
$this->doFinalOutput(false, 'Lost session');
return;
}
// Set a new session cookie, Just In Case™
wfResetSessionID();
// Set central cookies too, with a refreshed sessionid. Also, check if we
// need to override the default cookie security policy
$secureCookie = $memcData['secureCookies'];
$centralUser->setGlobalCookies($memcData['remember'], $memcData['sessionId'], $secureCookie, array('finalProto' => $memcData['finalProto'], 'secureCookies' => $memcData['secureCookies'], 'remember' => $memcData['remember']));
// Now, figure out how to report this back to the user.
// First, set to redo the edge login on the next pageview
$request->setSessionData('CentralAuthDoEdgeLogin', true);
// If it's not a script callback, just go for it.
if ($request->getVal('type') !== 'script') {
$this->doFinalOutput(true, 'success');
return;
}
// If it is a script callback, then we do want to create the user
// if it doesn't already exist locally (and fail if that can't be
// done).
if (!User::idFromName($centralUser->getName())) {
$user = new User();
$user->setName($centralUser->getName());
if (CentralAuthHooks::attemptAddUser($user)) {
$centralUser->invalidateCache();
}
}
if (!$centralUser->isAttached()) {
$this->doFinalOutput(false, 'Local user is not attached', self::getInlineScript('anon-set.js'));
return;
}
$script = self::getInlineScript('anon-remove.js');
// If we're returning to returnto, do that
if ($request->getCheck('return')) {
global $wgRedirectOnLogin;
if ($wgRedirectOnLogin !== null) {
$returnTo = $wgRedirectOnLogin;
$returnToQuery = array();
} else {
$returnTo = $request->getVal('returnto', '');
$returnToQuery = wfCgiToArray($request->getVal('returntoquery', ''));
}
$returnToTitle = Title::newFromText($returnTo);
if (!$returnToTitle) {
$returnToTitle = Title::newMainPage();
$returnToQuery = array();
}
$redirectUrl = $returnToTitle->getFullURL($returnToQuery);
$script .= "\n" . 'location.href = ' . Xml::encodeJsVar($redirectUrl) . ';';
$this->doFinalOutput(true, 'success', $script);
return;
}
// Otherwise, we need to rewrite p-personal and maybe notify the user too
global $wgCentralAuthUseEventLogging;
if ($wgCentralAuthUseEventLogging) {
EventLogging::logEvent('CentralAuth', 5690875, array('version' => 1, 'userId' => $centralUser->getId(), 'action' => 'sul2-autologin-fallbacklogin'));
}
// Add a script to the page that will pull in the user's toolslist
// via ajax, and update the UI. Don't write out the tools here (bug 57081).
$code = $this->getUser()->getOption('language');
$code = RequestContext::sanitizeLangCode($code);
Hooks::run('UserGetLanguageObject', array($this->getUser(), &$code, $this->getContext()));
$script .= "\n" . Xml::encodeJsCall('mediaWiki.messages.set', array(array('centralauth-centralautologin-logged-in' => wfMessage('centralauth-centralautologin-logged-in')->inLanguage($code)->plain(), 'centralauth-centralautologin-logged-in-nouser' => wfMessage('centralauth-centralautologin-logged-in-nouser')->inLanguage($code)->plain(), 'centralautologin' => wfMessage('centralautologin')->inLanguage($code)->plain())));
$script .= "\n" . self::getInlineScript('autologin.js');
// And for good measure, add the edge login HTML images to the page.
$script .= "\n" . Xml::encodeJsCall("jQuery( 'body' ).append", array(CentralAuthHooks::getEdgeLoginHTML()));
$this->doFinalOutput(true, 'success', $script);
return;
default:
$this->setHeaders();
$this->getOutput()->addWikiMsg('centralauth-centralautologin-desc');
return;
}
}