Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / ProxyLookup.php
blob3a3243a5d61a3da8e0a6f1876e11bcf6f76616c0
1 <?php
3 /**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
19 * @file
22 use IPSet\IPSet;
24 /**
25 * @since 1.28
27 class ProxyLookup {
29 /**
30 * @var string[]
32 private $proxyServers;
34 /**
35 * @var string[]
37 private $proxyServersComplex;
39 /**
40 * @var IPSet|null
42 private $proxyIPSet;
44 /**
45 * @param string[] $proxyServers Simple list of IPs
46 * @param string[] $proxyServersComplex Complex list of IPs/ranges
48 public function __construct( $proxyServers, $proxyServersComplex ) {
49 $this->proxyServers = $proxyServers;
50 $this->proxyServersComplex = $proxyServersComplex;
53 /**
54 * Checks if an IP matches a proxy we've configured
56 * @param string $ip
57 * @return bool
59 public function isConfiguredProxy( $ip ) {
60 // Quick check of known singular proxy servers
61 if ( in_array( $ip, $this->proxyServers ) ) {
62 return true;
65 // Check against addresses and CIDR nets in the complex list
66 if ( !$this->proxyIPSet ) {
67 $this->proxyIPSet = new IPSet( $this->proxyServersComplex );
69 return $this->proxyIPSet->match( $ip );
72 /**
73 * Checks if an IP is a trusted proxy provider.
74 * Useful to tell if X-Forwarded-For data is possibly bogus.
75 * CDN cache servers for the site are whitelisted.
77 * @param string $ip
78 * @return bool
80 public function isTrustedProxy( $ip ) {
81 $trusted = $this->isConfiguredProxy( $ip );
82 Hooks::run( 'IsTrustedProxy', [ &$ip, &$trusted ] );
83 return $trusted;