[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/utils/ -> IP.php (summary)

Functions and constants to play with IP addresses and ranges This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

Author: Antoine Musso "", Aaron Schulz
File Size: 738 lines (23 kb)
Included or required:0 times
Referenced: 1 time
Includes or requires: 0 files

Defines 1 class

IP:: (25 methods):
  isIPAddress()
  isIPv6()
  isIPv4()
  isValid()
  isValidBlock()
  sanitizeIP()
  prettifyIP()
  splitHostAndPort()
  combineHostAndPort()
  formatHex()
  hexToOctet()
  hexToQuad()
  isPublic()
  toHex()
  IPv6ToRawHex()
  parseCIDR()
  parseRange()
  parseCIDR6()
  parseRange6()
  isInRange()
  canonicalize()
  sanitizeRange()
  isTrustedProxy()
  isConfiguredProxy()
  clearCaches()


Class: IP  - X-Ref

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

isIPAddress( $ip )   X-Ref
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.

param: string $ip Possible IP address
return: bool

isIPv6( $ip )   X-Ref
Given a string, determine if it as valid IP in IPv6 only.
Note: Unlike isValid(), this looks for networks too.

param: string $ip Possible IP address
return: bool

isIPv4( $ip )   X-Ref
Given a string, determine if it as valid IP in IPv4 only.
Note: Unlike isValid(), this looks for networks too.

param: string $ip Possible IP address
return: bool

isValid( $ip )   X-Ref
Validate an IP address. Ranges are NOT considered valid.
SIIT IPv4-translated addresses are rejected.
Note: canonicalize() tries to convert translated addresses to IPv4.

param: string $ip
return: bool True if it is valid

isValidBlock( $ipblock )   X-Ref
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.

param: string $ipblock
return: bool True if it is valid

sanitizeIP( $ip )   X-Ref
Convert an IP into a verbose, uppercase, normalized form.
IPv6 addresses in octet notation are expanded to 8 words.
IPv4 addresses are just trimmed.

param: string $ip IP address in quad or octet form (CIDR or not).
return: string

prettifyIP( $ip )   X-Ref
Prettify an IP for display to end users.
This will make it more compact and lower-case.

param: string $ip
return: string

splitHostAndPort( $both )   X-Ref
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.

param: string $both The string with the host and port
return: array

combineHostAndPort( $host, $port, $defaultPort = false )   X-Ref
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

param: string $host
param: int $port
param: bool|int $defaultPort
return: string

formatHex( $hex )   X-Ref
Convert an IPv4 or IPv6 hexadecimal representation back to readable format

param: string $hex Number, with "v6-" prefix if it is IPv6
return: string Quad-dotted (IPv4) or octet notation (IPv6)

hexToOctet( $ip_hex )   X-Ref
Converts a hexadecimal number to an IPv6 address in octet notation

param: string $ip_hex Pure hex (no v6- prefix)
return: string (of format a:b:c:d:e:f:g:h)

hexToQuad( $ip_hex )   X-Ref
Converts a hexadecimal number to an IPv4 address in quad-dotted notation

param: string $ip_hex Pure hex
return: string (of format a.b.c.d)

isPublic( $ip )   X-Ref
Determine if an IP address really is an IP address, and if it is public,
i.e. not RFC 1918 or similar

param: string $ip
return: bool

toHex( $ip )   X-Ref
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.

param: string $ip Quad dotted/octet IP address.
return: string|bool False on failure

IPv6ToRawHex( $ip )   X-Ref
Given an IPv6 address in octet notation, returns a pure hex string.

param: string $ip Octet ipv6 IP address.
return: string|bool Pure hex (uppercase); false on failure

parseCIDR( $range )   X-Ref
Convert a network specification in CIDR notation
to an integer network and a number of bits

param: string $range IP with CIDR prefix
return: array(int or string, int)

parseRange( $range )   X-Ref
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
param: string $range IP range
return: array(string, string)

parseCIDR6( $range )   X-Ref
Convert a network specification in IPv6 CIDR notation to an
integer network and a number of bits

param: string $range
return: array(string, int)

parseRange6( $range )   X-Ref
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

param: string $range
return: array(string, string)

isInRange( $addr, $range )   X-Ref
Determine if a given IPv4/IPv6 address is in a given CIDR network

param: string $addr The address to check against the given range.
param: string $range The range to check the given address against.
return: bool Whether or not the given address is in the given range.

canonicalize( $addr )   X-Ref
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.

param: string $addr Something that might be an IP address
return: string Valid dotted quad IPv4 address or null

sanitizeRange( $range )   X-Ref
Gets rid of unneeded numbers in quad-dotted/octet IP strings
For example, 127.111.113.151/24 -> 127.111.113.0/24

param: string $range IP address to normalize
return: string

isTrustedProxy( $ip )   X-Ref
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.

param: string $ip
return: bool

isConfiguredProxy( $ip )   X-Ref
Checks if an IP matches a proxy we've configured

param: string $ip
return: bool

clearCaches()   X-Ref
Clears precomputed data used for proxy support.
Use this only for unit tests.




Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1