MediaWiki
1.32.5
|
A collection of public static functions to play with IP address and IP ranges. More...
Static Public Member Functions | |
static | canonicalize ( $addr) |
Convert some unusual representations of IPv4 addresses to their canonical dotted quad representation. More... | |
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. More... | |
static | formatHex ( $hex) |
Convert an IPv4 or IPv6 hexadecimal representation back to readable format. More... | |
static | getSubnet ( $ip) |
Returns the subnet of a given IP. More... | |
static | hexToOctet ( $ip_hex) |
Converts a hexadecimal number to an IPv6 address in octet notation. More... | |
static | hexToQuad ( $ip_hex) |
Converts a hexadecimal number to an IPv4 address in quad-dotted notation. More... | |
static | isInRange ( $addr, $range) |
Determine if a given IPv4/IPv6 address is in a given CIDR network. More... | |
static | isInRanges ( $ip, $ranges) |
Determines if an IP address is a list of CIDR a.b.c.d/n ranges. More... | |
static | isIPAddress ( $ip) |
Determine if a string is as valid IP address or network (CIDR prefix). More... | |
static | isIPv4 ( $ip) |
Given a string, determine if it as valid IP in IPv4 only. More... | |
static | isIPv6 ( $ip) |
Given a string, determine if it as valid IP in IPv6 only. More... | |
static | isPublic ( $ip) |
Determine if an IP address really is an IP address, and if it is public, i.e. More... | |
static | isValid ( $ip) |
Validate an IP address. More... | |
static | isValidBlock ( $ipRange) |
Validate an IP range (valid address with a valid CIDR prefix). More... | |
static | isValidRange ( $ipRange) |
Validate an IP range (valid address with a valid CIDR prefix). More... | |
static | parseCIDR ( $range) |
Convert a network specification in CIDR notation to an integer network and a number of bits. More... | |
static | parseRange ( $range) |
Given a string range in a number of formats, return the start and end of the range in hexadecimal. More... | |
static | prettifyIP ( $ip) |
Prettify an IP for display to end users. More... | |
static | sanitizeIP ( $ip) |
Convert an IP into a verbose, uppercase, normalized form. More... | |
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. More... | |
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. More... | |
static | toHex ( $ip) |
Return a zero-padded upper case hexadecimal representation of an IP address. More... | |
Static Private Member Functions | |
static | IPv6ToRawHex ( $ip) |
Given an IPv6 address in octet notation, returns a pure hex string. More... | |
static | parseCIDR6 ( $range) |
Convert a network specification in IPv6 CIDR notation to an integer network and a number of bits. More... | |
static | parseRange6 ( $range) |
Given a string range in a number of formats, return the start and end of the range in hexadecimal. More... | |
|
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.
string | $addr | Something that might be an IP address |
Definition at line 687 of file IP.php.
Referenced by IPTest\testIPCanonicalizeMappedAddress(), and IPTest\testIPCanonicalizeOnValidIp().
|
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
string | $host | |
int | $port | |
bool | int | $defaultPort |
Definition at line 315 of file IP.php.
Referenced by WebRequest\detectServer(), EtcdConfig\fetchAllFromEtcd(), and IPTest\testCombineHostAndPort().
|
static |
Convert an IPv4 or IPv6 hexadecimal representation back to readable format.
string | $hex | Number, with "v6-" prefix if it is IPv6 |
Definition at line 332 of file IP.php.
Referenced by ApiQueryBlocks\execute().
|
static |
Returns the subnet of a given IP.
string | $ip |
Definition at line 742 of file IP.php.
Referenced by User\pingLimiter().
|
static |
Converts a hexadecimal number to an IPv6 address in octet notation.
string | $ip_hex | Pure hex (no v6- prefix) |
Definition at line 346 of file IP.php.
Referenced by IPTest\testHexToOctet().
|
static |
Converts a hexadecimal number to an IPv4 address in quad-dotted notation.
string | $ip_hex | Pure hex |
Definition at line 366 of file IP.php.
Referenced by IPTest\testHexToQuad().
|
staticprivate |
|
static |
Determine if a given IPv4/IPv6 address is in a given CIDR network.
string | $addr | The address to check against the given range. |
string | $range | The range to check the given address against. |
Definition at line 650 of file IP.php.
Referenced by Autopromote\checkCondition(), MWRestrictions\checkIP(), Block\isWhitelistedFromAutoblocks(), and IPTest\testIPIsInRange().
|
static |
Determines if an IP address is a list of CIDR a.b.c.d/n ranges.
Definition at line 668 of file IP.php.
Referenced by User\getBlockedStatus(), SimpleCaptcha\isIPWhitelisted(), and User\isPingLimitable().
|
static |
Determine if a string is as valid IP address or network (CIDR prefix).
SIIT IPv4-translated addresses are rejected.
string | $ip | Possible IP address |
Definition at line 77 of file IP.php.
Referenced by SimpleCaptcha\buildValidIPs(), RecentChange\checkIPAddress(), SpecialContributions\execute(), DeletedContributionsPage\execute(), User\getGlobalBlock(), ManualLogEntry\getRecentChange(), SpecialBlock\getTargetUserTitle(), User\isLocallyBlockedProxy(), MWRestrictions\loadFromArray(), ConfirmEditHooks\onAlternateEditPreview(), RevDelList\setVisibility(), IPTest\testisIPAddress(), IPTest\testIsNotIPAddress(), HTMLRestrictionsField\validate(), and ApiBase\validateUser().
|
static |
Given a string, determine if it as valid IP in IPv4 only.
string | $ip | Possible IP address |
Definition at line 99 of file IP.php.
Referenced by ApiQueryBlocks\execute(), SpecialContributions\execute(), SquidPurgeClient\getIP(), User\inDnsBlacklist(), ContribsPager\isQueryableRange(), HTMLUserTextField\isValidIPRange(), UDPTransport\newFromString(), IPTest\testIsIPv4(), IPTest\testisNotIPv4(), and SpecialBlock\validateTarget().
|
static |
Given a string, determine if it as valid IP in IPv6 only.
string | $ip | Possible IP address |
Definition at line 88 of file IP.php.
Referenced by ApiQueryBlocks\execute(), SquidPurgeClient\getIP(), FileCacheBase\incrMissesRecent(), User\isIP(), ContribsPager\isQueryableRange(), HTMLUserTextField\isValidIPRange(), IPTest\testisIPv6(), and SpecialBlock\validateTarget().
|
static |
Determine if an IP address really is an IP address, and if it is public, i.e.
not RFC 1918 or similar
string | $ip |
Definition at line 388 of file IP.php.
Referenced by IPTest\testIsPublic().
|
static |
Validate an IP address.
Ranges are NOT considered valid. SIIT IPv4-translated addresses are rejected.
string | $ip |
Definition at line 111 of file IP.php.
Referenced by WikiPage\archiveRevisions(), PopulateIpChanges\doDBUpdates(), GetLagTimes\execute(), ResetAuthenticationThrottle\execute(), Block\getBlocksForIPList(), Article\getRobotPolicy(), RequestContext\importScopedSession(), FileCacheBase\incrMissesRecent(), Revision\RevisionStore\insertIpChangesRow(), Block\parseTarget(), MediaWiki\Auth\TemporaryPasswordPrimaryAuthenticationProvider\providerAllowsAuthenticationDataChange(), MediaWiki\Auth\TemporaryPasswordPrimaryAuthenticationProvider\sendPasswordResetEmail(), Article\showViewFooter(), IPTest\testInvalidIPs(), IPTest\testValidIPs(), HTMLUserTextField\validate(), and XmlDumpWriter\writeContributor().
|
static |
Validate an IP range (valid address with a valid CIDR prefix).
SIIT IPv4-translated addresses are rejected.
string | $ipRange |
Definition at line 125 of file IP.php.
Referenced by HTMLUserTextField\isValidIPRange().
|
static |
Validate an IP range (valid address with a valid CIDR prefix).
SIIT IPv4-translated addresses are rejected.
string | $ipRange |
Definition at line 138 of file IP.php.
Referenced by SpecialContributions\execute(), User\isIPRange(), NukeHooks\nukeContributionsLinks(), Block\parseTarget(), IPTest\testInvalidRanges(), and IPTest\testValidRanges().
|
static |
Convert a network specification in CIDR notation to an integer network and a number of bits.
string | $range | IP with CIDR prefix |
Definition at line 470 of file IP.php.
Referenced by IPTest\assertFalseCIDR(), IPTest\assertNet(), ApiQueryBlocks\execute(), ContribsPager\isQueryableRange(), and IPTest\testCIDRParsing().
|
staticprivate |
|
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
string | $range | IP range |
Definition at line 513 of file IP.php.
Referenced by ApiQueryBlocks\execute(), SpecialBlockList\getBlockListPager(), ContribsPager\getIpRangeConds(), Block\getRangeEnd(), Block\getRangeStart(), and Block\newLoad().
|
staticprivate |
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
string | $range |
|
static |
Prettify an IP for display to end users.
This will make it more compact and lower-case.
string | $ip |
Definition at line 213 of file IP.php.
Referenced by IPTest\testPrettifyIP(), and Linker\userLink().
|
static |
Convert an IP into a verbose, uppercase, normalized form.
Both IPv4 and IPv6 addresses are trimmed. Additionally, IPv6 addresses in octet notation are expanded to 8 words; IPv4 addresses have leading zeros, in each octet, removed.
string | $ip | IP address in quad or octet form (CIDR or not). |
Definition at line 152 of file IP.php.
Referenced by BlockTest\addXffBlocks(), User\getBlockedStatus(), User\getName(), Block\parseTarget(), MediaWikiTitleCodec\splitTitleString(), IPTest\testSanitizeIP(), and ApiBase\validateUser().
|
static |
Gets rid of unneeded numbers in quad-dotted/octet IP strings For example, 127.111.113.151/24 -> 127.111.113.0/24.
string | $range | IP address to normalize |
Definition at line 725 of file IP.php.
Referenced by BlockTest\addXffBlocks(), FileCacheBase\incrMissesRecent(), Block\parseTarget(), and IPTest\testSanitizeRange().
|
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.
string | $both | The string with the host and port |
Definition at line 266 of file IP.php.
Referenced by WebRequest\detectServer(), Wikimedia\Rdbms\DatabaseMysqli\mysqlConnect(), MysqlMaintenance\runMysql(), and IPTest\testSplitHostAndPort().
|
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.
string | $ip | Quad dotted/octet IP address. |
Definition at line 417 of file IP.php.
Referenced by Block\chooseBlock(), PopulateIpChanges\doDBUpdates(), Block\getBlocksForIPList(), Block\getRangeEnd(), Block\getRangeStart(), Revision\RevisionStore\insertIpChangesRow(), Block\newLoad(), RevisionDbTestBase\testInsertOn(), IPTest\testToHex(), and PageArchiveTestBase\testUndeleteRevisions().