MediaWiki  1.27.2
WebRequestTest.php
Go to the documentation of this file.
1 <?php
2 
7  protected $oldServer;
8 
9  protected function setUp() {
10  parent::setUp();
11 
12  $this->oldServer = $_SERVER;
14  }
15 
16  protected function tearDown() {
17  $_SERVER = $this->oldServer;
19 
20  parent::tearDown();
21  }
22 
27  public function testDetectServer( $expected, $input, $description ) {
28  $_SERVER = $input;
30  $this->assertEquals( $expected, $result, $description );
31  }
32 
33  public static function provideDetectServer() {
34  return [
35  [
36  'http://x',
37  [
38  'HTTP_HOST' => 'x'
39  ],
40  'Host header'
41  ],
42  [
43  'https://x',
44  [
45  'HTTP_HOST' => 'x',
46  'HTTPS' => 'on',
47  ],
48  'Host header with secure'
49  ],
50  [
51  'http://x',
52  [
53  'HTTP_HOST' => 'x',
54  'SERVER_PORT' => 80,
55  ],
56  'Default SERVER_PORT',
57  ],
58  [
59  'http://x',
60  [
61  'HTTP_HOST' => 'x',
62  'HTTPS' => 'off',
63  ],
64  'Secure off'
65  ],
66  [
67  'http://y',
68  [
69  'SERVER_NAME' => 'y',
70  ],
71  'Server name'
72  ],
73  [
74  'http://x',
75  [
76  'HTTP_HOST' => 'x',
77  'SERVER_NAME' => 'y',
78  ],
79  'Host server name precedence'
80  ],
81  [
82  'http://[::1]:81',
83  [
84  'HTTP_HOST' => '[::1]',
85  'SERVER_NAME' => '::1',
86  'SERVER_PORT' => '81',
87  ],
88  'Apache bug 26005'
89  ],
90  [
91  'http://localhost',
92  [
93  'SERVER_NAME' => '[2001'
94  ],
95  'Kind of like lighttpd per commit message in MW r83847',
96  ],
97  [
98  'http://[2a01:e35:2eb4:1::2]:777',
99  [
100  'SERVER_NAME' => '[2a01:e35:2eb4:1::2]:777'
101  ],
102  'Possible lighttpd environment per bug 14977 comment 13',
103  ],
104  ];
105  }
106 
111  public function testGetIP( $expected, $input, $squid, $xffList, $private, $description ) {
112  $_SERVER = $input;
113  $this->setMwGlobals( [
114  'wgSquidServersNoPurge' => $squid,
115  'wgUsePrivateIPs' => $private,
116  'wgHooks' => [
117  'IsTrustedProxy' => [
118  function ( &$ip, &$trusted ) use ( $xffList ) {
119  $trusted = $trusted || in_array( $ip, $xffList );
120  return true;
121  }
122  ]
123  ]
124  ] );
125 
126  $request = new WebRequest();
127  $result = $request->getIP();
128  $this->assertEquals( $expected, $result, $description );
129  }
130 
131  public static function provideGetIP() {
132  return [
133  [
134  '127.0.0.1',
135  [
136  'REMOTE_ADDR' => '127.0.0.1'
137  ],
138  [],
139  [],
140  false,
141  'Simple IPv4'
142  ],
143  [
144  '::1',
145  [
146  'REMOTE_ADDR' => '::1'
147  ],
148  [],
149  [],
150  false,
151  'Simple IPv6'
152  ],
153  [
154  '12.0.0.1',
155  [
156  'REMOTE_ADDR' => 'abcd:0001:002:03:4:555:6666:7777',
157  'HTTP_X_FORWARDED_FOR' => '12.0.0.1, abcd:0001:002:03:4:555:6666:7777',
158  ],
159  [ 'ABCD:1:2:3:4:555:6666:7777' ],
160  [],
161  false,
162  'IPv6 normalisation'
163  ],
164  [
165  '12.0.0.3',
166  [
167  'REMOTE_ADDR' => '12.0.0.1',
168  'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
169  ],
170  [ '12.0.0.1', '12.0.0.2' ],
171  [],
172  false,
173  'With X-Forwaded-For'
174  ],
175  [
176  '12.0.0.1',
177  [
178  'REMOTE_ADDR' => '12.0.0.1',
179  'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
180  ],
181  [],
182  [],
183  false,
184  'With X-Forwaded-For and disallowed server'
185  ],
186  [
187  '12.0.0.2',
188  [
189  'REMOTE_ADDR' => '12.0.0.1',
190  'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
191  ],
192  [ '12.0.0.1' ],
193  [],
194  false,
195  'With multiple X-Forwaded-For and only one allowed server'
196  ],
197  [
198  '10.0.0.3',
199  [
200  'REMOTE_ADDR' => '12.0.0.2',
201  'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
202  ],
203  [ '12.0.0.1', '12.0.0.2' ],
204  [],
205  false,
206  'With X-Forwaded-For and private IP (from cache proxy)'
207  ],
208  [
209  '10.0.0.4',
210  [
211  'REMOTE_ADDR' => '12.0.0.2',
212  'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
213  ],
214  [ '12.0.0.1', '12.0.0.2', '10.0.0.3' ],
215  [],
216  true,
217  'With X-Forwaded-For and private IP (allowed)'
218  ],
219  [
220  '10.0.0.4',
221  [
222  'REMOTE_ADDR' => '12.0.0.2',
223  'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
224  ],
225  [ '12.0.0.1', '12.0.0.2' ],
226  [ '10.0.0.3' ],
227  true,
228  'With X-Forwaded-For and private IP (allowed)'
229  ],
230  [
231  '10.0.0.3',
232  [
233  'REMOTE_ADDR' => '12.0.0.2',
234  'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
235  ],
236  [ '12.0.0.1', '12.0.0.2' ],
237  [ '10.0.0.3' ],
238  false,
239  'With X-Forwaded-For and private IP (disallowed)'
240  ],
241  [
242  '12.0.0.3',
243  [
244  'REMOTE_ADDR' => '12.0.0.1',
245  'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
246  ],
247  [],
248  [ '12.0.0.1', '12.0.0.2' ],
249  false,
250  'With X-Forwaded-For'
251  ],
252  [
253  '12.0.0.2',
254  [
255  'REMOTE_ADDR' => '12.0.0.1',
256  'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
257  ],
258  [],
259  [ '12.0.0.1' ],
260  false,
261  'With multiple X-Forwaded-For and only one allowed server'
262  ],
263  [
264  '12.0.0.2',
265  [
266  'REMOTE_ADDR' => '12.0.0.2',
267  'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2'
268  ],
269  [],
270  [ '12.0.0.2' ],
271  false,
272  'With X-Forwaded-For and private IP and hook (disallowed)'
273  ],
274  [
275  '12.0.0.1',
276  [
277  'REMOTE_ADDR' => 'abcd:0001:002:03:4:555:6666:7777',
278  'HTTP_X_FORWARDED_FOR' => '12.0.0.1, abcd:0001:002:03:4:555:6666:7777',
279  ],
280  [ 'ABCD:1:2:3::/64' ],
281  [],
282  false,
283  'IPv6 CIDR'
284  ],
285  [
286  '12.0.0.3',
287  [
288  'REMOTE_ADDR' => '12.0.0.1',
289  'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
290  ],
291  [ '12.0.0.0/24' ],
292  [],
293  false,
294  'IPv4 CIDR'
295  ],
296  ];
297  }
298 
304  // ensure that local install state doesn't interfere with test
305  $this->setMwGlobals( [
306  'wgSquidServersNoPurge' => [],
307  'wgSquidServers' => [],
308  'wgUsePrivateIPs' => false,
309  'wgHooks' => [],
310  ] );
311 
312  $request = new WebRequest();
313  # Next call throw an exception about lacking an IP
314  $request->getIP();
315  }
316 
317  public static function provideLanguageData() {
318  return [
319  [ '', [], 'Empty Accept-Language header' ],
320  [ 'en', [ 'en' => 1 ], 'One language' ],
321  [ 'en, ar', [ 'en' => 1, 'ar' => 1 ], 'Two languages listed in appearance order.' ],
322  [
323  'zh-cn,zh-tw',
324  [ 'zh-cn' => 1, 'zh-tw' => 1 ],
325  'Two equally prefered languages, listed in appearance order per rfc3282. Checks c9119'
326  ],
327  [
328  'es, en; q=0.5',
329  [ 'es' => 1, 'en' => '0.5' ],
330  'Spanish as first language and English and second'
331  ],
332  [ 'en; q=0.5, es', [ 'es' => 1, 'en' => '0.5' ], 'Less prefered language first' ],
333  [ 'fr, en; q=0.5, es', [ 'fr' => 1, 'es' => 1, 'en' => '0.5' ], 'Three languages' ],
334  [ 'en; q=0.5, es', [ 'es' => 1, 'en' => '0.5' ], 'Two languages' ],
335  [ 'en, zh;q=0', [ 'en' => 1 ], "It's Chinese to me" ],
336  [
337  'es; q=1, pt;q=0.7, it; q=0.6, de; q=0.1, ru;q=0',
338  [ 'es' => '1', 'pt' => '0.7', 'it' => '0.6', 'de' => '0.1' ],
339  'Preference for Romance languages'
340  ],
341  [
342  'en-gb, en-us; q=1',
343  [ 'en-gb' => 1, 'en-us' => '1' ],
344  'Two equally prefered English variants'
345  ],
346  ];
347  }
348 
353  public function testAcceptLang( $acceptLanguageHeader, $expectedLanguages, $description ) {
354  $_SERVER = [ 'HTTP_ACCEPT_LANGUAGE' => $acceptLanguageHeader ];
355  $request = new WebRequest();
356  $this->assertSame( $request->getAcceptLang(), $expectedLanguages, $description );
357  }
358 }
testGetIpLackOfRemoteAddrThrowAnException()
MWException WebRequest::getIP.
testAcceptLang($acceptLanguageHeader, $expectedLanguages, $description)
provideLanguageData WebRequest::getAcceptLang
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
static provideGetIP()
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message.Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item.Return false to stop further processing of the tag $reader:XMLReader object $logInfo:Array of information 'ImportHandlePageXMLTag':When parsing a XML tag in a page.Return false to stop further processing of the tag $reader:XMLReader object &$pageInfo:Array of information 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision.Return false to stop further processing of the tag $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information 'ImportHandleToplevelXMLTag':When parsing a top level XML tag.Return false to stop further processing of the tag $reader:XMLReader object 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload.Return false to stop further processing of the tag $reader:XMLReader object $revisionInfo:Array of information 'ImportLogInterwikiLink':Hook to change the interwiki link used in log entries and edit summaries for transwiki imports.&$fullInterwikiPrefix:Interwiki prefix, may contain colons.&$pageTitle:String that contains page title. 'ImportSources':Called when reading from the $wgImportSources configuration variable.Can be used to lazy-load the import sources list.&$importSources:The value of $wgImportSources.Modify as necessary.See the comment in DefaultSettings.php for the detail of how to structure this array. 'InfoAction':When building information to display on the action=info page.$context:IContextSource object &$pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect.&$title:Title object for the current page &$request:WebRequest &$ignoreRedirect:boolean to skip redirect check &$target:Title/string of redirect target &$article:Article object 'InternalParseBeforeLinks':during Parser's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings.&$parser:Parser object &$text:string containing partially parsed text &$stripState:Parser's internal StripState object 'InternalParseBeforeSanitize':during Parser's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings.Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments.&$parser:Parser object &$text:string containing partially parsed text &$stripState:Parser's internal StripState object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not.Return true without providing an interwiki to continue interwiki search.$prefix:interwiki prefix we are looking for.&$iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InvalidateEmailComplete':Called after a user's email has been invalidated successfully.$user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification.Callee may modify $url and $query, URL will be constructed as $url.$query &$url:URL to index.php &$query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) &$article:article(object) being checked 'IsTrustedProxy':Override the result of IP::isTrustedProxy() &$ip:IP being check &$result:Change this value to override the result of IP::isTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from &$allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of Sanitizer::validateEmail(), for instance to return false if the domain name doesn't match your organization.$addr:The e-mail address entered by the user &$result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user &$result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we're looking for a messages file for &$file:The messages file path, you can override this to change the location. 'LanguageGetMagic':DEPRECATED!Use $magicWords in a file listed in $wgExtensionMessagesFiles instead.Use this to define synonyms of magic words depending of the language &$magicExtensions:associative array of magic words synonyms $lang:language code(string) 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces.Do not use this hook to add namespaces.Use CanonicalNamespaces for that.&$namespaces:Array of namespaces indexed by their numbers 'LanguageGetSpecialPageAliases':DEPRECATED!Use $specialPageAliases in a file listed in $wgExtensionMessagesFiles instead.Use to define aliases of special pages names depending of the language &$specialPageAliases:associative array of magic words synonyms $lang:language code(string) 'LanguageGetTranslatedLanguageNames':Provide translated language names.&$names:array of language code=> language name $code:language of the preferred translations 'LanguageLinks':Manipulate a page's language links.This is called in various places to allow extensions to define the effective language links for a page.$title:The page's Title.&$links:Associative array mapping language codes to prefixed links of the form"language:title".&$linkFlags:Associative array mapping prefixed links to arrays of flags.Currently unused, but planned to provide support for marking individual language links in the UI, e.g.for featured articles. 'LanguageSelector':Hook to change the language selector available on a page.$out:The output page.$cssClassName:CSS class name of the language selector. 'LinkBegin':Used when generating internal and interwiki links in Linker::link(), before processing starts.Return false to skip default processing and return $ret.See documentation for Linker::link() for details on the expected meanings of parameters.$skin:the Skin object $target:the Title that the link is pointing to &$html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:1796
static detectServer()
Work out an appropriate URL prefix containing scheme and host, based on information detected from $_S...
Definition: WebRequest.php:190
testDetectServer($expected, $input, $description)
provideDetectServer WebRequest::detectServer
WebRequest.
static provideDetectServer()
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2418
static provideLanguageData()
testGetIP($expected, $input, $squid, $xffList, $private, $description)
provideGetIP WebRequest::getIP
static clearCaches()
Clears precomputed data used for proxy support.
Definition: IP.php:767
setMwGlobals($pairs, $value=null)