http.php

来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 623 行 · 第 1/2 页

PHP
623
字号
		return strlen($this->_host) ? $this->_host : false;
	}

	/**
	 * Returns true if and only if the host string passes validation. If no host is passed,
	 * then the host contained in the instance variable is used.
	 *
	 * @param string $host
	 * @return boolean
	 * @uses Zend_Filter
	 */
    public function validateHost($host = null)
    {
        if ($host === null) {
            $host = $this->_host;
        }
        
        /**
         * If the host is empty, then it is considered invalid
         */
        if (strlen($host) == 0) {
            return false;
        }
        
        /**
         * Check the host against the allowed values; delegated to Zend_Filter.
         */
        $validate = new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_ALL);
        return $validate->isValid($host);
    }

    /**
     * Sets the host for the current URI, and returns the old host
     *
     * @param string $host
     * @throws Zend_Uri_Exception
     * @return string
     */
    public function setHost($host)
    {
        if (!$this->validateHost($host)) {
            throw new Zend_Uri_Exception("Host \"$host\" is not a valid HTTP host");
        }
        $oldHost = $this->_host;
        $this->_host = $host;
        return $oldHost;
    }

	/**
	 * Returns the TCP port, or FALSE if none.
	 *
	 * @return string
	 */
	public function getPort()
	{
		return strlen($this->_port) ? $this->_port : false;
	}

	/**
	 * Returns true if and only if the TCP port string passes validation. If no port is passed,
	 * then the port contained in the instance variable is used.
	 *
	 * @param string $port
	 * @return boolean
	 */
    public function validatePort($port = null)
    {
        if ($port === null) {
            $port = $this->_port;
        }
        
        // If the port is empty, then it is considered valid
        if (!strlen($port)) {
            return true;
        }

        // Check the port against the allowed values
        return ctype_digit((string)$port) && 1 <= $port && $port <= 65535;
    }

    /**
     * Sets the port for the current URI, and returns the old port
     *
     * @param string $port
     * @throws Zend_Uri_Exception
     * @return string
     */
    public function setPort($port)
    {
        if (!$this->validatePort($port)) {
            throw new Zend_Uri_Exception("Port \"$port\" is not a valid HTTP port.");
        }
        $oldPort = $this->_port;
        $this->_port = $port;
        return $oldPort;
    }

	/**
	 * Returns the path and filename portion of the URL, or FALSE if none.
	 *
	 * @return string
	 */
	public function getPath()
	{
		return strlen($this->_path) ? $this->_path : '/';
	}

	/**
	 * Returns true if and only if the path string passes validation. If no path is passed,
	 * then the path contained in the instance variable is used.
	 *
	 * @param string $path
	 * @throws Zend_Uri_Exception
	 * @return boolean
	 */
    public function validatePath($path = null)
    {
        if ($path === null) {
            $path = $this->_path;
        }
        /**
         * If the path is empty, then it is considered valid
         */
        if (strlen($path) == 0) {
            return true;
        }
        /**
         * Determine whether the path is well-formed
         */
        $pattern = '/^' . $this->_regex['path'] . '$/';
        $status = @preg_match($pattern, $path);
        if ($status === false) {
            throw new Zend_Uri_Exception('Internal error: path validation failed');
        }
        return (boolean) $status;
    }

    /**
     * Sets the path for the current URI, and returns the old path
     *
     * @param string $path
     * @throws Zend_Uri_Exception
     * @return string
     */
    public function setPath($path)
    {
        if (!$this->validatePath($path)) {
            throw new Zend_Uri_Exception("Path \"$path\" is not a valid HTTP path");
        }
        $oldPath = $this->_path;
        $this->_path = $path;
        return $oldPath;
    }

	/**
	 * Returns the query portion of the URL (after ?), or FALSE if none.
	 *
	 * @return string
	 */
	public function getQuery()
	{
		return strlen($this->_query) ? $this->_query : false;
	}

	/**
	 * Returns true if and only if the query string passes validation. If no query is passed,
	 * then the query string contained in the instance variable is used.
	 *
	 * @param string $query
	 * @throws Zend_Uri_Exception
	 * @return boolean
	 */
    public function validateQuery($query = null)
    {
        if ($query === null) {
            $query = $this->_query;
        }

        // If query is empty, it is considered to be valid
        if (strlen($query) == 0) {
            return true;
        }
        
        /**
         * Determine whether the query is well-formed
         *
         * @link http://www.faqs.org/rfcs/rfc2396.html
         */
        $pattern = '/^' . $this->_regex['uric'] . '*$/';
        $status = @preg_match($pattern, $query);
        if ($status === false) {
            throw new Zend_Uri_Exception('Internal error: query validation failed');
        }
        
        return $status == 1;
    }

    /**
     * Set the query string for the current URI, and return the old query 
     * string This method accepts both strings and arrays.
     *
     * @param string|array $query The query string or array
     * @return string Old query string
     */
    public function setQuery($query)
    {
    	$oldQuery = $this->_query;
        $this->_query = $this->_parseQuery($query);
        return $oldQuery;
    }

    /**
     * Parse a query string or array, validate it and return it as a query string
     *
     * @param string|array $query
     * @return string
     */
    protected function _parseQuery($query)
    {
    	// If query is empty, return an empty string
        if (empty($query)) {
            return '';
        }
        
        // If query is an array, make a string out of it
        if (is_array($query)) {
        	// fails on PHP < 5.1.2
        	$query_str = @http_build_query($query, '', '&');
        	// If it failed, try calling with only 2 args
        	if (!$query_str) {
        		$query_str = http_build_query($query, '');
        	}
        	// Just in case they use &amp; in their php.ini, replace it with &
       		$query_str = str_replace("&amp;", "&", $query_str);
       		
        	$query = $query_str;
        } else {
        	$query = (string) $query;
        }
        
        // Make sure the query is valid, and set it
       	if ($this->validateQuery($query)) {
       		return $query;
       	} else {
       		throw new Zend_Uri_Exception("'$query' is not a valid query string");
       	}
        return $query;
    }

	/**
	 * Returns the fragment portion of the URL (after #), or FALSE if none.
	 *
	 * @return string|false
	 */
	public function getFragment()
	{
		return strlen($this->_fragment) ? $this->_fragment : false;
	}

	/**
	 * Returns true if and only if the fragment passes validation. If no fragment is passed,
	 * then the fragment contained in the instance variable is used.
	 *
	 * @param string $fragment
	 * @throws Zend_Uri_Exception
	 * @return boolean
	 */
    public function validateFragment($fragment = null)
    {
        if ($fragment === null) {
            $fragment = $this->_fragment;
        }

        // If fragment is empty, it is considered to be valid
        if (strlen($fragment) == 0) {
            return true;
        }
        
        /**
         * Determine whether the fragment is well-formed
         *
         * @link http://www.faqs.org/rfcs/rfc2396.html
         */
        $pattern = '/^' . $this->_regex['uric'] . '*$/';
        $status = @preg_match($pattern, $fragment);
        if ($status === false) {
            throw new Zend_Uri_Exception('Internal error: fragment validation failed');
        }
        
        return (boolean) $status;
    }

    /**
     * Sets the fragment for the current URI, and returns the old fragment
     *
     * @param string $fragment
     * @throws Zend_Uri_Exception
     * @return string
     */
    public function setFragment($fragment)
    {
        if (!$this->validateFragment($fragment)) {
            throw new Zend_Uri_Exception("Fragment \"$fragment\" is not a valid HTTP fragment");
        }
        $oldFragment = $this->_fragment;
        $this->_fragment = $fragment;
        return $oldFragment;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?