MediaWiki  REL1_24
IP Class Reference

A collection of public static functions to play with IP address and IP blocks. More...

List of all members.

Static Public Member Functions

static canonicalize ($addr)
 Convert some unusual representations of IPv4 addresses to their canonical dotted quad representation.
static clearCaches ()
 Clears precomputed data used for proxy support.
static combineHostAndPort ($host, $port, $defaultPort=false)
 Given a host name and a port, combine them into host/port string like you might find in a URL.
static formatHex ($hex)
 Convert an IPv4 or IPv6 hexadecimal representation back to readable format.
static hexToOctet ($ip_hex)
 Converts a hexadecimal number to an IPv6 address in octet notation.
static hexToQuad ($ip_hex)
 Converts a hexadecimal number to an IPv4 address in quad-dotted notation.
static isConfiguredProxy ($ip)
 Checks if an IP matches a proxy we've configured.
static isInRange ($addr, $range)
 Determine if a given IPv4/IPv6 address is in a given CIDR network.
static isIPAddress ($ip)
 Determine if a string is as valid IP address or network (CIDR prefix).
static isIPv4 ($ip)
 Given a string, determine if it as valid IP in IPv4 only.
static isIPv6 ($ip)
 Given a string, determine if it as valid IP in IPv6 only.
static isPublic ($ip)
 Determine if an IP address really is an IP address, and if it is public, i.e.
static isTrustedProxy ($ip)
 Checks if an IP is a trusted proxy provider.
static isValid ($ip)
 Validate an IP address.
static isValidBlock ($ipblock)
 Validate an IP Block (valid address WITH a valid prefix).
static parseCIDR ($range)
 Convert a network specification in CIDR notation to an integer network and a number of bits.
static parseRange ($range)
 Given a string range in a number of formats, return the start and end of the range in hexadecimal.
static prettifyIP ($ip)
 Prettify an IP for display to end users.
static sanitizeIP ($ip)
 Convert an IP into a verbose, uppercase, normalized form.
static sanitizeRange ($range)
 Gets rid of unneeded numbers in quad-dotted/octet IP strings For example, 127.111.113.151/24 -> 127.111.113.0/24.
static splitHostAndPort ($both)
 Given a host/port string, like one might find in the host part of a URL per RFC 2732, split the hostname part and the port part and return an array with an element for each.
static toHex ($ip)
 Return a zero-padded upper case hexadecimal representation of an IP address.

Static Private Member Functions

static IPv6ToRawHex ($ip)
 Given an IPv6 address in octet notation, returns a pure hex string.
static parseCIDR6 ($range)
 Convert a network specification in IPv6 CIDR notation to an integer network and a number of bits.
static parseRange6 ($range)
 Given a string range in a number of formats, return the start and end of the range in hexadecimal.

Static Private Attributes

static $proxyIpSet = null

Detailed Description

A collection of public static functions to play with IP address and IP blocks.

Definition at line 65 of file IP.php.


Member Function Documentation

static IP::canonicalize ( addr) [static]

Convert some unusual representations of IPv4 addresses to their canonical dotted quad representation.

This currently only checks a few IPV4-to-IPv6 related cases. More unusual representations may be added later.

Parameters:
string$addrSomething that might be an IP address
Returns:
string Valid dotted quad IPv4 address or null

Definition at line 639 of file IP.php.

Referenced by IPTest\testIPCanonicalizeMappedAddress(), and IPTest\testIPCanonicalizeOnValidIp().

static IP::clearCaches ( ) [static]

Clears precomputed data used for proxy support.

Use this only for unit tests.

Definition at line 733 of file IP.php.

Referenced by WebRequestTest\setUp(), and WebRequestTest\tearDown().

static IP::combineHostAndPort ( host,
port,
defaultPort = false 
) [static]

Given a host name and a port, combine them into host/port string like you might find in a URL.

If the host contains a colon, wrap it in square brackets like in RFC 2732. If the port matches the default port, omit the port specification

Parameters:
string$host
int$port
bool | int$defaultPort
Returns:
string

Definition at line 292 of file IP.php.

Referenced by IPTest\testCombineHostAndPort().

static IP::formatHex ( hex) [static]

Convert an IPv4 or IPv6 hexadecimal representation back to readable format.

Parameters:
string$hexNumber, with "v6-" prefix if it is IPv6
Returns:
string Quad-dotted (IPv4) or octet notation (IPv6)

Definition at line 309 of file IP.php.

Referenced by ApiQueryBlocks\execute().

static IP::hexToOctet ( ip_hex) [static]

Converts a hexadecimal number to an IPv6 address in octet notation.

Parameters:
string$ip_hexPure hex (no v6- prefix)
Returns:
string (of format a:b:c:d:e:f:g:h)

Definition at line 323 of file IP.php.

Referenced by IPTest\testHexToOctet().

static IP::hexToQuad ( ip_hex) [static]

Converts a hexadecimal number to an IPv4 address in quad-dotted notation.

Parameters:
string$ip_hexPure hex
Returns:
string (of format a.b.c.d)

Definition at line 343 of file IP.php.

Referenced by IPTest\testHexToQuad().

static IP::IPv6ToRawHex ( ip) [static, private]

Given an IPv6 address in octet notation, returns a pure hex string.

Parameters:
string$ipOctet ipv6 IP address.
Returns:
string|bool Pure hex (uppercase); false on failure

Definition at line 424 of file IP.php.

static IP::isConfiguredProxy ( ip) [static]

Checks if an IP matches a proxy we've configured.

Since:
1.24
Parameters:
string$ip
Returns:
bool

Definition at line 710 of file IP.php.

Referenced by wfIsConfiguredProxy().

static IP::isInRange ( addr,
range 
) [static]

Determine if a given IPv4/IPv6 address is in a given CIDR network.

Parameters:
string$addrThe address to check against the given range.
string$rangeThe range to check the given address against.
Returns:
bool Whether or not the given address is in the given range.

Definition at line 621 of file IP.php.

Referenced by Autopromote\checkCondition(), and IPTest\testIPIsInRange().

static IP::isIPAddress ( ip) [static]

Determine if a string is as valid IP address or network (CIDR prefix).

SIIT IPv4-translated addresses are rejected. Note: canonicalize() tries to convert translated addresses to IPv4.

Parameters:
string$ipPossible IP address
Returns:
bool

Definition at line 77 of file IP.php.

Referenced by DeletedContributionsPage\__construct(), PopulateLogSearch\doDBUpdates(), SpecialContributions\execute(), SpecialLog\execute(), RevDelList\setVisibility(), and IPTest\testisIPAddress().

static IP::isIPv4 ( ip) [static]

Given a string, determine if it as valid IP in IPv4 only.

Note: Unlike isValid(), this looks for networks too.

Parameters:
string$ipPossible IP address
Returns:
bool

Definition at line 99 of file IP.php.

Referenced by ApiQueryBlocks\execute(), IPTest\testisIPv4(), and wfErrorLog().

static IP::isIPv6 ( ip) [static]

Given a string, determine if it as valid IP in IPv6 only.

Note: Unlike isValid(), this looks for networks too.

Parameters:
string$ipPossible IP address
Returns:
bool

Definition at line 88 of file IP.php.

Referenced by ApiQueryBlocks\execute(), FileCacheBase\incrMissesRecent(), and IPTest\testisIPv6().

static IP::isPublic ( ip) [static]

Determine if an IP address really is an IP address, and if it is public, i.e.

not RFC 1918 or similar

Parameters:
string$ip
Returns:
bool

Definition at line 365 of file IP.php.

Referenced by IPTest\testPrivateIPs().

static IP::isTrustedProxy ( ip) [static]

Checks if an IP is a trusted proxy provider.

Useful to tell if X-Forwarded-For data is possibly bogus. Squid cache servers for the site are whitelisted.

Since:
1.24
Parameters:
string$ip
Returns:
bool

Definition at line 697 of file IP.php.

Referenced by wfIsTrustedProxy().

static IP::isValid ( ip) [static]

Validate an IP address.

Ranges are NOT considered valid. SIIT IPv4-translated addresses are rejected. Note: canonicalize() tries to convert translated addresses to IPv4.

Parameters:
string$ip
Returns:
bool True if it is valid

Definition at line 111 of file IP.php.

Referenced by GetLagTimes\execute(), FileCacheBase\incrMissesRecent(), IPTest\testInvalidIPs(), IPTest\testValidIPs(), and XmlDumpWriter\writeContributor().

static IP::isValidBlock ( ipblock) [static]

Validate an IP Block (valid address WITH a valid prefix).

SIIT IPv4-translated addresses are rejected. Note: canonicalize() tries to convert translated addresses to IPv4.

Parameters:
string$ipblock
Returns:
bool True if it is valid

Definition at line 124 of file IP.php.

Referenced by IPTest\testInvalidBlocks(), and IPTest\testValidBlocks().

static IP::parseCIDR ( range) [static]

Convert a network specification in CIDR notation to an integer network and a number of bits.

Parameters:
string$rangeIP with CIDR prefix
Returns:
array(int or string, int)

Definition at line 444 of file IP.php.

Referenced by IPTest\assertFalseCIDR(), IPTest\assertNet(), ApiQueryBlocks\execute(), and IPTest\testCIDRParsing().

static IP::parseCIDR6 ( range) [static, private]

Convert a network specification in IPv6 CIDR notation to an integer network and a number of bits.

Parameters:
string$range
Returns:
array(string, int)

Definition at line 534 of file IP.php.

static IP::parseRange ( range) [static]

Given a string range in a number of formats, return the start and end of the range in hexadecimal.

Formats are: 1.2.3.4/24 CIDR 1.2.3.4 - 1.2.3.5 Explicit range 1.2.3.4 Single IP

2001:0db8:85a3::7344/96 CIDR 2001:0db8:85a3::7344 - 2001:0db8:85a3::7344 Explicit range 2001:0db8:85a3::7344 Single IP

Parameters:
string$rangeIP range
Returns:
array(string, string)

Definition at line 487 of file IP.php.

Referenced by ApiQueryBlocks\execute(), and SpecialBlockList\showList().

static IP::parseRange6 ( range) [static, private]

Given a string range in a number of formats, return the start and end of the range in hexadecimal.

For IPv6.

Formats are: 2001:0db8:85a3::7344/96 CIDR 2001:0db8:85a3::7344 - 2001:0db8:85a3::7344 Explicit range 2001:0db8:85a3::7344/96 Single IP

Parameters:
string$range
Returns:
array(string, string)

Definition at line 575 of file IP.php.

static IP::prettifyIP ( ip) [static]

Prettify an IP for display to end users.

This will make it more compact and lower-case.

Parameters:
string$ip
Returns:
string

Definition at line 190 of file IP.php.

Referenced by IPTest\testPrettifyIP(), and Linker\userLink().

static IP::sanitizeIP ( ip) [static]

Convert an IP into a verbose, uppercase, normalized form.

IPv6 addresses in octet notation are expanded to 8 words. IPv4 addresses are just trimmed.

Parameters:
string$ipIP address in quad or octet form (CIDR or not).
Returns:
string

Definition at line 137 of file IP.php.

Referenced by BlockTest\addXffBlocks(), ApiQueryLogEvents\execute(), User\getName(), and IPTest\testSanitizeIP().

static IP::sanitizeRange ( range) [static]

Gets rid of unneeded numbers in quad-dotted/octet IP strings For example, 127.111.113.151/24 -> 127.111.113.0/24.

Parameters:
string$rangeIP address to normalize
Returns:
string

Definition at line 677 of file IP.php.

Referenced by BlockTest\addXffBlocks(), FileCacheBase\incrMissesRecent(), and IPTest\testSanitizeRange().

static IP::splitHostAndPort ( both) [static]

Given a host/port string, like one might find in the host part of a URL per RFC 2732, split the hostname part and the port part and return an array with an element for each.

If there is no port part, the array will have false in place of the port. If the string was invalid in some way, false is returned.

This was easy with IPv4 and was generally done in an ad-hoc way, but with IPv6 it's somewhat more complicated due to the need to parse the square brackets and colons.

A bare IPv6 address is accepted despite the lack of square brackets.

Parameters:
string$bothThe string with the host and port
Returns:
array

Definition at line 243 of file IP.php.

Referenced by MemcachedPeclBagOStuff\__construct(), DatabaseMysqli\mysqlConnect(), and IPTest\testSplitHostAndPort().

static IP::toHex ( ip) [static]

Return a zero-padded upper case hexadecimal representation of an IP address.

Hexadecimal addresses are used because they can easily be extended to IPv6 support. To separate the ranges, the return value from this function for an IPv6 address will be prefixed with "v6-", a non- hexadecimal string which sorts after the IPv4 addresses.

Parameters:
string$ipQuad dotted/octet IP address.
Returns:
string|bool False on failure

Definition at line 392 of file IP.php.

Referenced by IPTest\testToHex().


Member Data Documentation

IP::$proxyIpSet = null [static, private]

Definition at line 67 of file IP.php.


The documentation for this class was generated from the following file: