⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 browser.php

📁 PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。
💻 PHP
📖 第 1 页 / 共 3 页
字号:
<?php
/**
 *  Base include file for SimpleTest
 *  @package    SimpleTest
 *  @subpackage WebTester
 *  @version    $Id: browser.php 1723 2008-04-08 00:34:10Z lastcraft $
 */

/**#@+
 *  include other SimpleTest class files
 */
require_once(dirname(__FILE__) . '/simpletest.php');
require_once(dirname(__FILE__) . '/http.php');
require_once(dirname(__FILE__) . '/encoding.php');
require_once(dirname(__FILE__) . '/page.php');
require_once(dirname(__FILE__) . '/selector.php');
require_once(dirname(__FILE__) . '/frames.php');
require_once(dirname(__FILE__) . '/user_agent.php');
/**#@-*/

if (!defined('DEFAULT_MAX_NESTED_FRAMES')) {
    define('DEFAULT_MAX_NESTED_FRAMES', 3);
}

/**
 *    Browser history list.
 *    @package SimpleTest
 *    @subpackage WebTester
 */
class SimpleBrowserHistory {
    var $_sequence;
    var $_position;

    /**
     *    Starts empty.
     *    @access public
     */
    function SimpleBrowserHistory() {
        $this->_sequence = array();
        $this->_position = -1;
    }

    /**
     *    Test for no entries yet.
     *    @return boolean        True if empty.
     *    @access private
     */
    function _isEmpty() {
        return ($this->_position == -1);
    }

    /**
     *    Test for being at the beginning.
     *    @return boolean        True if first.
     *    @access private
     */
    function _atBeginning() {
        return ($this->_position == 0) && ! $this->_isEmpty();
    }

    /**
     *    Test for being at the last entry.
     *    @return boolean        True if last.
     *    @access private
     */
    function _atEnd() {
        return ($this->_position + 1 >= count($this->_sequence)) && ! $this->_isEmpty();
    }

    /**
     *    Adds a successfully fetched page to the history.
     *    @param SimpleUrl $url                 URL of fetch.
     *    @param SimpleEncoding $parameters     Any post data with the fetch.
     *    @access public
     */
    function recordEntry($url, $parameters) {
        $this->_dropFuture();
        array_push(
                $this->_sequence,
                array('url' => $url, 'parameters' => $parameters));
        $this->_position++;
    }

    /**
     *    Last fully qualified URL for current history
     *    position.
     *    @return SimpleUrl        URL for this position.
     *    @access public
     */
    function getUrl() {
        if ($this->_isEmpty()) {
            return false;
        }
        return $this->_sequence[$this->_position]['url'];
    }

    /**
     *    Parameters of last fetch from current history
     *    position.
     *    @return SimpleFormEncoding    Post parameters.
     *    @access public
     */
    function getParameters() {
        if ($this->_isEmpty()) {
            return false;
        }
        return $this->_sequence[$this->_position]['parameters'];
    }

    /**
     *    Step back one place in the history. Stops at
     *    the first page.
     *    @return boolean     True if any previous entries.
     *    @access public
     */
    function back() {
        if ($this->_isEmpty() || $this->_atBeginning()) {
            return false;
        }
        $this->_position--;
        return true;
    }

    /**
     *    Step forward one place. If already at the
     *    latest entry then nothing will happen.
     *    @return boolean     True if any future entries.
     *    @access public
     */
    function forward() {
        if ($this->_isEmpty() || $this->_atEnd()) {
            return false;
        }
        $this->_position++;
        return true;
    }

    /**
     *    Ditches all future entries beyond the current
     *    point.
     *    @access private
     */
    function _dropFuture() {
        if ($this->_isEmpty()) {
            return;
        }
        while (! $this->_atEnd()) {
            array_pop($this->_sequence);
        }
    }
}

/**
 *    Simulated web browser. This is an aggregate of
 *    the user agent, the HTML parsing, request history
 *    and the last header set.
 *    @package SimpleTest
 *    @subpackage WebTester
 */
class SimpleBrowser {
    var $_user_agent;
    var $_page;
    var $_history;
    var $_ignore_frames;
    var $_maximum_nested_frames;

    /**
     *    Starts with a fresh browser with no
     *    cookie or any other state information. The
     *    exception is that a default proxy will be
     *    set up if specified in the options.
     *    @access public
     */
    function SimpleBrowser() {
        $this->_user_agent = &$this->_createUserAgent();
        $this->_user_agent->useProxy(
                SimpleTest::getDefaultProxy(),
                SimpleTest::getDefaultProxyUsername(),
                SimpleTest::getDefaultProxyPassword());
        $this->_page = &new SimplePage();
        $this->_history = &$this->_createHistory();
        $this->_ignore_frames = false;
        $this->_maximum_nested_frames = DEFAULT_MAX_NESTED_FRAMES;
    }

    /**
     *    Creates the underlying user agent.
     *    @return SimpleFetcher    Content fetcher.
     *    @access protected
     */
    function &_createUserAgent() {
        $user_agent = &new SimpleUserAgent();
        return $user_agent;
    }

    /**
     *    Creates a new empty history list.
     *    @return SimpleBrowserHistory    New list.
     *    @access protected
     */
    function &_createHistory() {
        $history = &new SimpleBrowserHistory();
        return $history;
    }

    /**
     *    Disables frames support. Frames will not be fetched
     *    and the frameset page will be used instead.
     *    @access public
     */
    function ignoreFrames() {
        $this->_ignore_frames = true;
    }

    /**
     *    Enables frames support. Frames will be fetched from
     *    now on.
     *    @access public
     */
    function useFrames() {
        $this->_ignore_frames = false;
    }
    
    /**
     *    Switches off cookie sending and recieving.
     *    @access public
     */
    function ignoreCookies() {
        $this->_user_agent->ignoreCookies();
    }
    
    /**
     *    Switches back on the cookie sending and recieving.
     *    @access public
     */
    function useCookies() {
        $this->_user_agent->useCookies();
    }

    /**
     *    Parses the raw content into a page. Will load further
     *    frame pages unless frames are disabled.
     *    @param SimpleHttpResponse $response    Response from fetch.
     *    @param integer $depth                  Nested frameset depth.
     *    @return SimplePage                     Parsed HTML.
     *    @access private
     */
    function &_parse($response, $depth = 0) {
        $page = &$this->_buildPage($response);
        if ($this->_ignore_frames || ! $page->hasFrames() || ($depth > $this->_maximum_nested_frames)) {
            return $page;
        }
        $frameset = &new SimpleFrameset($page);
        foreach ($page->getFrameset() as $key => $url) {
            $frame = &$this->_fetch($url, new SimpleGetEncoding(), $depth + 1);
            $frameset->addFrame($frame, $key);
        }
        return $frameset;
    }
    
    /**
     *    Assembles the parsing machinery and actually parses
     *    a single page. Frees all of the builder memory and so
     *    unjams the PHP memory management.
     *    @param SimpleHttpResponse $response    Response from fetch.
     *    @return SimplePage                     Parsed top level page.
     *    @access protected
     */
    function &_buildPage($response) {
        $builder = &new SimplePageBuilder();
        $page = &$builder->parse($response);
        $builder->free();
        unset($builder);
        return $page;
    }

    /**
     *    Fetches a page. Jointly recursive with the _parse()
     *    method as it descends a frameset.
     *    @param string/SimpleUrl $url          Target to fetch.
     *    @param SimpleEncoding $encoding       GET/POST parameters.
     *    @param integer $depth                 Nested frameset depth protection.
     *    @return SimplePage                    Parsed page.
     *    @access private
     */
    function &_fetch($url, $encoding, $depth = 0) {
        $response = &$this->_user_agent->fetchResponse($url, $encoding);
        if ($response->isError()) {
            $page = &new SimplePage($response);
        } else {
            $page = &$this->_parse($response, $depth);
        }
        return $page;
    }

    /**
     *    Fetches a page or a single frame if that is the current
     *    focus.
     *    @param SimpleUrl $url                   Target to fetch.
     *    @param SimpleEncoding $parameters       GET/POST parameters.
     *    @return string                          Raw content of page.
     *    @access private
     */
    function _load($url, $parameters) {
        $frame = $url->getTarget();
        if (! $frame || ! $this->_page->hasFrames() || (strtolower($frame) == '_top')) {
            return $this->_loadPage($url, $parameters);
        }
        return $this->_loadFrame(array($frame), $url, $parameters);
    }

    /**
     *    Fetches a page and makes it the current page/frame.
     *    @param string/SimpleUrl $url            Target to fetch as string.
     *    @param SimplePostEncoding $parameters   POST parameters.
     *    @return string                          Raw content of page.
     *    @access private
     */
    function _loadPage($url, $parameters) {
        $this->_page = &$this->_fetch($url, $parameters);
        $this->_history->recordEntry(
                $this->_page->getUrl(),
                $this->_page->getRequestData());
        return $this->_page->getRaw();
    }

    /**
     *    Fetches a frame into the existing frameset replacing the
     *    original.
     *    @param array $frames                    List of names to drill down.
     *    @param string/SimpleUrl $url            Target to fetch as string.
     *    @param SimpleFormEncoding $parameters   POST parameters.
     *    @return string                          Raw content of page.
     *    @access private
     */
    function _loadFrame($frames, $url, $parameters) {
        $page = &$this->_fetch($url, $parameters);
        $this->_page->setFrame($frames, $page);
        return $page->getRaw();
    }

    /**
     *    Removes expired and temporary cookies as if
     *    the browser was closed and re-opened.
     *    @param string/integer $date   Time when session restarted.
     *                                  If omitted then all persistent
     *                                  cookies are kept.
     *    @access public
     */
    function restart($date = false) {
        $this->_user_agent->restart($date);
    }

    /**
     *    Adds a header to every fetch.
     *    @param string $header       Header line to add to every
     *                                request until cleared.
     *    @access public
     */
    function addHeader($header) {
        $this->_user_agent->addHeader($header);
    }

    /**
     *    Ages the cookies by the specified time.
     *    @param integer $interval    Amount in seconds.

⌨️ 快捷键说明

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