Example #1
0
 /**
  * Set the ihost. Returns true on success, false on failure (if there are
  * any invalid characters).
  *
  * @param string $ihost
  * @return bool
  */
 protected function set_host($ihost)
 {
     if ($ihost === null) {
         $this->ihost = null;
         return true;
     } elseif (substr($ihost, 0, 1) === '[' && substr($ihost, -1) === ']') {
         if (Requests_IPv6::check_ipv6(substr($ihost, 1, -1))) {
             $this->ihost = '[' . Requests_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 #2
0
/**
 * Determines if an IP address is valid.
 *
 * Handles both IPv4 and IPv6 addresses.
 *
 * @since 4.7.0
 *
 * @param  string $ip IP address.
 * @return string|false The valid IP address, otherwise false.
 */
function rest_is_ip_address($ip)
{
    $ipv4_pattern = '/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/';
    if (!preg_match($ipv4_pattern, $ip) && !Requests_IPv6::check_ipv6($ip)) {
        return false;
    }
    return $ip;
}