Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- define("NET_INVALID_IP", 0x1);
- define("NET_INVALID_PORT", 0x2);
- define("NET_VALID_IPv4", 0x4);
- define("NET_VALID_IPv6", 0x8);
- define("NET_VALID_IP_WITH_PORT", 0x10)
- /**
- * Validate a port number
- * @param integer $port
- * @return boolean
- */
- public static function isValidPort($port) {
- return $port > 0 && $port < 65535;
- }
- /**
- * Validate an address, and point out if port is part of the address
- * @param string $ip
- * @param integer $port
- * @return string
- */
- public static function isValidIp($ip, &$port) {
- // IPv6 with port
- $ipv6_with_port = Array();
- preg_match("/^\[(?P<address>.*?)\]:(?P<port>\d{0,})$/", $ip, $ipv6_with_port);
- // IPv4 with port
- $ipv4_with_port = Array();
- preg_match("/^(?P<address>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(?P<port>\d{0,})$/", $ip, $ipv4_with_port);
- if(count($ipv6_with_port) > 0) {
- if(!NET_IPv6::checkIPv6($ipv6_with_port["address"])) {
- return NET_INVALID_IP;
- } else {
- $port = $ipv6_with_port["port"];
- if(ODDNS::isValidPort($ipv6_with_port["port"])) {
- return NET_VALID_IPv6 + ODDNS_VALID_IP_WITH_PORT;
- } else {
- return NET_INVALID_PORT;
- }
- }
- } else if(count($ipv4_with_port) > 0) {
- if(!NET_IPv4::validateIP($ipv4_with_port["address"])) {
- return NET_INVALID_IP;
- } else {
- $port = $ipv4_with_port["port"];
- if(ODDNS::isValidPort($ipv4_with_port["port"])) {
- return NET_VALID_IPv4 + ODDNS_VALID_IP_WITH_PORT;
- } else {
- return NET_INVALID_PORT;
- }
- }
- } else if(preg_match("/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/", $ip) == 1) {
- if(NET_IPv4::validateIP($ip)) {
- return NET_VALID_IPv4;
- }
- } else {
- if(NET_IPv6::checkIPv6($ip)) {
- return NET_VALID_IPv6;
- }
- }
- return NET_INVALID_IP;
- }
Advertisement
Add Comment
Please, Sign In to add comment