Merge branch 'master' of ssh://git.pk910.de:16110/PHP-P10
[PHP-P10.git] / Uplink / IPAddr.class.php
index 1eddcbed0aa7f872ff688d122121426430ffd534..b1912e8926c3c503d395415aa0b2b46c275ba707 100644 (file)
@@ -28,6 +28,7 @@ class IPAddr {
        private static $pattern_IPv4 = '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(|\/[0-9]{1,2})$/';
        private $ip6 = array();
        private $addr_is_ipv6 = false;
+       private $is_server_addr = false;
 
        public function __construct($initial_value) {
                for($i = 0; $i < 8; $i++) {
@@ -96,6 +97,46 @@ class IPAddr {
        public function isIPv6() {
                return $this->addr_is_ipv6;
        }
+       
+       public function setServerAddr($serverAddr) {
+               $this->is_server_addr = $serverAddr;
+       }
+       
+       public function isServerAddr() {
+               return $this->is_server_addr;
+       }
+       
+       public function isLocalAddress($serverAddr = false) {
+               /* checks if address is out of:
+               * 127.0.0.1/32
+               * 10.0.0.0/8
+               * 192.168.0.0/16
+               * 172.16.0.0/12
+               * ::1/128
+               * fc00::/7
+               *
+               * if $serverAddr is true also return true for server IP's
+               */
+               if($this->addr_is_ipv6) {
+                       if(
+                               (($this->ip6[0] & 0xFE00) == 0xFC00) || /* fc00::/7 */
+                               ($this->ip6[0] == 0 && $this->ip6[1] == 0 && $this->ip6[2] == 0 && $this->ip6[3] == 0 && 
+                                $this->ip6[4] == 0 && $this->ip6[5] == 0 && $this->ip6[6] == 0 && $this->ip6[7] == 1)
+                         )
+                               return true;
+               } else {
+                       if(
+                               (($this->ip6[6] & 0xFFFF) == 0x7F00 && ($this->ip6[7] & 0xFFFF) == 0x0001) || /* 127.0.0.1/32 */
+                               (($this->ip6[6] & 0xFF00) == 0x0A00) || /* 10.0.0.0/8 */
+                               (($this->ip6[6] & 0xFFF0) == 0xAC10) || /* 172.16.0.0/12 */
+                               (($this->ip6[6] & 0xFFFF) == 0xC0A8) /* 192.168.0.0/16 */
+                         )
+                               return true;
+               }
+               if($serverAddr && $this->is_server_addr)
+                       return true;
+               return false;
+       }
 
        public function getAddress() {
                if($this->isIPv6()) {