Author: Alexander Merz (alexander.merz@web.de)
Author: elfrink at introweb dot nl
Author: Geoffrey Sneddon (geoffers@gmail.com)
 public static function checkIPv6($ip)
 {
     $ipPart = SimplePie_Net_IPv6::SplitV64($ip);
     $count = 0;
     if (!empty($ipPart[0])) {
         $ipv6 = explode(':', $ipPart[0]);
         for ($i = 0; $i < count($ipv6); $i++) {
             $dec = hexdec($ipv6[$i]);
             $hex = strtoupper(preg_replace('/^[0]{1,3}(.*[0-9a-fA-F])$/', '\\1', $ipv6[$i]));
             if ($ipv6[$i] >= 0 && $dec <= 65535 && $hex === strtoupper(dechex($dec))) {
                 $count++;
             }
         }
         if ($count === 8) {
             return true;
         } elseif ($count === 6 && !empty($ipPart[1])) {
             $ipv4 = explode('.', $ipPart[1]);
             $count = 0;
             foreach ($ipv4 as $ipv4_part) {
                 if ($ipv4_part >= 0 && $ipv4_part <= 255 && preg_match('/^\\d{1,3}$/', $ipv4_part)) {
                     $count++;
                 }
             }
             if ($count === 4) {
                 return true;
             }
         } else {
             return false;
         }
     } else {
         return false;
     }
 }
 /**
  * Set the ihost. Returns true on success, false on failure (if there are
  * any invalid characters).
  *
  * @param string $ihost
  * @return bool
  */
 public function set_host($ihost)
 {
     if ($ihost === null) {
         $this->ihost = null;
         return true;
     } elseif (substr($ihost, 0, 1) === '[' && substr($ihost, -1) === ']') {
         if (SimplePie_Net_IPv6::check_ipv6(substr($ihost, 1, -1))) {
             $this->ihost = '[' . SimplePie_Net_IPv6::compress(substr($ihost, 1, -1)) . ']';
         } else {
             $this->ihost = null;
             return false;
         }
     } else {
         $ihost = $this->replace_invalid_with_pct_encoding($ihost, '!$&\'()*+,;=');
         // Lowercase, but ignore pct-encoded sections (as they should
         // remain uppercase). This must be done after the previous step
         // as that can add unescaped characters.
         $position = 0;
         $strlen = strlen($ihost);
         while (($position += strcspn($ihost, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ%', $position)) < $strlen) {
             if ($ihost[$position] === '%') {
                 $position += 3;
             } else {
                 $ihost[$position] = strtolower($ihost[$position]);
                 $position++;
             }
         }
         $this->ihost = $ihost;
     }
     $this->scheme_normalization();
     return true;
 }
Example #3
0
 /**
  * Set the host. Returns true on success, false on failure (if there are
  * any invalid characters).
  *
  * @access public
  * @param string $host
  * @return bool
  */
 public function set_host($host)
 {
     if ($host === null || $host === '') {
         $this->host = null;
         $this->valid[__FUNCTION__] = true;
         return true;
     } elseif ($host[0] === '[' && substr($host, -1) === ']') {
         if (SimplePie_Net_IPv6::checkIPv6(substr($host, 1, -1))) {
             $this->host = $host;
             $this->valid[__FUNCTION__] = true;
             return true;
         } else {
             $this->host = null;
             $this->valid[__FUNCTION__] = false;
             return false;
         }
     } else {
         $this->host = $this->replace_invalid_with_pct_encoding($host, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~!$&\'()*+,;=', SIMPLEPIE_LOWERCASE);
         $this->valid[__FUNCTION__] = true;
         return true;
     }
 }