MediaWiki  REL1_19
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 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 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 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 sanitizeIP ($ip)
 Convert an IP into a nice standard form.
static sanitizeRange ($range)
 Gets rid of uneeded 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 toOctet ($ip_int)
 Given an unsigned integer, returns an IPv6 address in octet notation.
static toUnsigned ($ip)
 Given an IP address in dotted-quad/octet notation, returns an unsigned integer.

Static Private Member Functions

static IPv6ToRawHex ($ip)
 Given an IPv6 address in octet notation, returns a pure hex string.
static isPublic6 ($ip)
 Determine if an IPv6 address really is an IP address, and if it is public, i.e.
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 toUnsigned6 ($ip)

Detailed Description

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

Definition at line 72 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:
$addrString: something that might be an IP address
Returns:
String: valid dotted quad IPv4 address or null

Definition at line 671 of file IP.php.

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

Here is the caller graph for this function:

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:
$hoststring
$portint
$defaultPortbool|int
Returns:
string

Definition at line 250 of file IP.php.

Referenced by WebRequest\detectServer(), and IPTest\testCombineHostAndPort().

Here is the caller graph for this function:

static IP::formatHex ( hex) [static]

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

Parameters:
$hexString: number, with "v6-" prefix if it is IPv6
Returns:
String: quad-dotted (IPv4) or octet notation (IPv6)

Definition at line 277 of file IP.php.

static IP::hexToOctet ( ip_hex) [static]

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

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

Definition at line 291 of file IP.php.

Referenced by IPTest\testHexToOctet().

Here is the caller graph for this function:

static IP::hexToQuad ( ip_hex) [static]

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

Parameters:
$ip_hexString: pure hex
Returns:
String (of format a.b.c.d)

Definition at line 310 of file IP.php.

Referenced by ApiQueryBlocks\execute(), and IPTest\testHexToQuad().

Here is the caller graph for this function:

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

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

Parameters:
$ipString: octet ipv6 IP address.
Returns:
String: pure hex (uppercase)

Definition at line 423 of file IP.php.

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

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

Parameters:
$addrString: the address to check against the given range.
$rangeString: the range to check the given address against.
Returns:
Boolean: whether or not the given address is in the given range.

Definition at line 654 of file IP.php.

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

Here is the caller graph for this function:

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:
$ipString: possible IP address
Returns:
Boolean

Definition at line 81 of file IP.php.

Referenced by PopulateLogSearch\doDBUpdates(), SpecialContributions\execute(), SpecialLog\execute(), DeletedContributionsPage\execute(), LogEventsList\getExtraInputs(), DeletedContributionsPage\getSubTitle(), ContribsPager\getUserCond(), SpecialContributions\getUserLinks(), User\isBlockedGlobally(), RevDel_List\setVisibility(), and IPTest\testisIPAddress().

Here is the caller graph for this function:

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:
$ipString: possible IP address
Returns:
Boolean

Definition at line 103 of file IP.php.

Referenced by SquidPurgeClient\getIP(), User\inDnsBlacklist(), IPTest\testisIPv4(), SpecialBlock\validateTargetField(), and wfErrorLog().

Here is the caller graph for this function:

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:
$ipString: possible IP address
Returns:
Boolean

Definition at line 92 of file IP.php.

Referenced by SquidPurgeClient\getIP(), FileCacheBase\incrMissesRecent(), User\isIP(), IPTest\testisIPv6(), and SpecialBlock\validateTargetField().

Here is the caller graph for this function:

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 Comes from ProxyTools.php

Parameters:
$ipString
Returns:
Boolean

Definition at line 332 of file IP.php.

Referenced by IPTest\testPrivateIPs().

Here is the caller graph for this function:

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

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

not RFC 4193 or similar

Parameters:
$ipString
Returns:
Boolean

Definition at line 375 of file IP.php.

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:
$ipString
Returns:
Boolean: True if it is valid.

Definition at line 115 of file IP.php.

Referenced by GetLagTimes\execute(), FileCacheBase\incrMissesRecent(), SpecialPasswordReset\onSubmit(), Block\parseTarget(), Article\showViewFooter(), WebInstaller_Options\submit(), IPTest\testInvalidIPs(), IPTest\testValidIPs(), and XmlDumpWriter\writeContributor().

Here is the caller graph for this function:

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:
$ipblockString
Returns:
Boolean: True if it is valid.

Definition at line 128 of file IP.php.

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

Here is the caller graph for this function:

static IP::parseCIDR ( range) [static]

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

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

Definition at line 470 of file IP.php.

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

Here is the caller graph for this function:

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

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

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

Definition at line 562 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:
$rangeString: IP range
Returns:
array(string, string)

Definition at line 512 of file IP.php.

Referenced by Block\getRangeEnd(), Block\getRangeStart(), Block\newLoad(), and SpecialBlockList\showList().

Here is the caller graph for this function:

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:
$range
Returns:
array(string, string)

Definition at line 602 of file IP.php.

static IP::sanitizeIP ( ip) [static]

Convert an IP into a nice standard form.

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

Parameters:
$ipString: IP address in quad or octet form (CIDR or not).
Returns:
String

Definition at line 141 of file IP.php.

Referenced by User\getName(), Block\parseTarget(), Title\secureAndSplit(), and IPTest\testSanitizeIP().

Here is the caller graph for this function:

static IP::sanitizeRange ( range) [static]

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

Parameters:
$rangeString: IP address to normalize
Returns:
string

Definition at line 706 of file IP.php.

Referenced by FileCacheBase\incrMissesRecent(), Block\normaliseRange(), Block\parseTarget(), and IPTest\testSanitizeRange().

Here is the caller graph for this function:

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:
$bothThe string with the host and port
Returns:
array

Definition at line 202 of file IP.php.

Referenced by WebRequest\detectServer(), and IPTest\testSplitHostAndPort().

Here is the caller graph for this function:

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:
$ipString: quad dotted/octet IP address.
Returns:
String

Definition at line 405 of file IP.php.

Referenced by ApiQueryBlocks\execute(), Block\getRangeEnd(), Block\getRangeStart(), and Block\newLoad().

Here is the caller graph for this function:

static IP::toOctet ( ip_int) [static]

Given an unsigned integer, returns an IPv6 address in octet notation.

Parameters:
$ip_intString: IP address.
Returns:
String

Definition at line 267 of file IP.php.

static IP::toUnsigned ( ip) [static]

Given an IP address in dotted-quad/octet notation, returns an unsigned integer.

Like ip2long() except that it actually works and has a consistent error return value. Comes from ProxyTools.php

Parameters:
$ipString: quad dotted IP address.
Returns:
Mixed: string/int/false

Definition at line 443 of file IP.php.

Referenced by IPTest\testip2longWrapper().

Here is the caller graph for this function:

static IP::toUnsigned6 ( ip) [static, private]
Parameters:
$ip
Returns:
String

Definition at line 459 of file IP.php.


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