Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

Contains a simple class providing some useful internet protocol-related functions.

Author: Jake Dallimore <jrhdallimore@gmail.com>
Copyright: 2016 Jake Dallimore
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 266 lines (12 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: ip_utils  - X-Ref

Static helper class providing some useful internet-protocol-related functions.

is_domain_name($domainname)   X-Ref
Syntax checking for domain names, including fully qualified domain names.

This function does not verify the existence of the domain name. It only verifies syntactic correctness.
This is based on RFC1034/1035 and does not provide support for validation of internationalised domain names (IDNs).
All IDNs must be prior-converted to their ascii-compatible encoding before being passed to this function.

param: string $domainname the input string to check.
return: bool true if the string has valid syntax, false otherwise.

is_domain_matching_pattern($pattern)   X-Ref
Checks whether the input string is a valid wildcard domain matching pattern.

A domain matching pattern is essentially a domain name with a single, leading wildcard (*) label, and at least one other
label. The wildcard label is considered to match at least one label at or above (to the left of) its position in the string,
but will not match the trailing domain (everything to its right).

The string must be dot-separated, and the whole pattern must follow the domain name syntax rules defined in RFC1034/1035.
Namely, the character type (ascii), total-length (253) and label-length (63) restrictions. This function only confirms
syntactic correctness. It does not check for the existence of the domain/subdomains.

For example, the string '*.example.com' is a pattern deemed to match any direct subdomain of
example.com (such as test.example.com), any higher level subdomains (e.g. another.test.example.com) but will not match
the 'example.com' domain itself.

param: string $pattern the string to check.
return: bool true if the input string is a valid domain wildcard matching pattern, false otherwise.

is_ip_address($address)   X-Ref
Syntax validation for IP addresses, supporting both IPv4 and Ipv6 formats.

param: string $address the address to check.
return: bool true if the address is a valid IPv4 of IPv6 address, false otherwise.

is_ipv4_address($address)   X-Ref
Syntax validation for IPv4 addresses.

param: string $address the address to check.
return: bool true if the address is a valid IPv4 address, false otherwise.

is_ipv4_range($addressrange)   X-Ref
Syntax checking for IPv4 address ranges.
Supports CIDR notation and last-group ranges.
Eg. 127.0.0.0/24 or 127.0.0.80-255

param: string $addressrange the address range to check.
return: bool true if the string is a valid range representation, false otherwise.

is_ipv6_address($address)   X-Ref
Syntax validation for IPv6 addresses.
This function does not check whether the address is assigned, only its syntactical correctness.

param: string $address the address to check.
return: bool true if the address is a valid IPv6 address, false otherwise.

is_ipv6_range($addressrange)   X-Ref
Syntax validation for IPv6 address ranges.
Supports CIDR notation and last-group ranges.
Eg. fe80::d98c/64 or fe80::d98c-ffee

param: string $addressrange the IPv6 address range to check.
return: bool true if the string is a valid range representation, false otherwise.

is_domain_in_allowed_list($domain, $alloweddomains)   X-Ref
Checks the domain name against a list of allowed domains. The list of allowed domains may use wildcards
that match {@see is_domain_matching_pattern()}. Domains are compared in a case-insensitive manner

param: string $domain Domain address
param: array $alloweddomains An array of allowed domains.
return: boolean True if the domain matches one of the entries in the allowed domains list.

is_ip_in_subnet_list($ip, $list, $delim = "\n")   X-Ref
Is an ip in a given list of subnets?

param: string $ip - the IP to test against the list
param: string $list - the list of IP subnets
param: string $delim a delimiter of the list
return: bool

get_ip_address(string $hostname)   X-Ref
Return IP address for given hostname, or null on failure

param: string $hostname
return: string|null