123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- <?php
- require_once(dirname(__FILE__) . '/cookies.php');
- require_once(dirname(__FILE__) . '/http.php');
- require_once(dirname(__FILE__) . '/encoding.php');
- require_once(dirname(__FILE__) . '/authentication.php');
- if (! defined('DEFAULT_MAX_REDIRECTS')) {
- define('DEFAULT_MAX_REDIRECTS', 3);
- }
- if (! defined('DEFAULT_CONNECTION_TIMEOUT')) {
- define('DEFAULT_CONNECTION_TIMEOUT', 15);
- }
- class SimpleUserAgent {
- var $_cookie_jar;
- var $_cookies_enabled = true;
- var $_authenticator;
- var $_max_redirects = DEFAULT_MAX_REDIRECTS;
- var $_proxy = false;
- var $_proxy_username = false;
- var $_proxy_password = false;
- var $_connection_timeout = DEFAULT_CONNECTION_TIMEOUT;
- var $_additional_headers = array();
-
- function SimpleUserAgent() {
- $this->_cookie_jar = &new SimpleCookieJar();
- $this->_authenticator = &new SimpleAuthenticator();
- }
-
- function restart($date = false) {
- $this->_cookie_jar->restartSession($date);
- $this->_authenticator->restartSession();
- }
-
- function addHeader($header) {
- $this->_additional_headers[] = $header;
- }
-
- function ageCookies($interval) {
- $this->_cookie_jar->agePrematurely($interval);
- }
-
- function setCookie($name, $value, $host = false, $path = '/', $expiry = false) {
- $this->_cookie_jar->setCookie($name, $value, $host, $path, $expiry);
- }
-
- function getCookieValue($host, $path, $name) {
- return $this->_cookie_jar->getCookieValue($host, $path, $name);
- }
-
- function getBaseCookieValue($name, $base) {
- if (! $base) {
- return null;
- }
- return $this->getCookieValue($base->getHost(), $base->getPath(), $name);
- }
-
- function ignoreCookies() {
- $this->_cookies_enabled = false;
- }
-
- function useCookies() {
- $this->_cookies_enabled = true;
- }
-
- function setConnectionTimeout($timeout) {
- $this->_connection_timeout = $timeout;
- }
-
- function setMaximumRedirects($max) {
- $this->_max_redirects = $max;
- }
-
- function useProxy($proxy, $username, $password) {
- if (! $proxy) {
- $this->_proxy = false;
- return;
- }
- if ((strncmp($proxy, 'http://', 7) != 0) && (strncmp($proxy, 'https://', 8) != 0)) {
- $proxy = 'http://'. $proxy;
- }
- $this->_proxy = &new SimpleUrl($proxy);
- $this->_proxy_username = $username;
- $this->_proxy_password = $password;
- }
-
- function _isTooManyRedirects($redirects) {
- return ($redirects > $this->_max_redirects);
- }
-
- function setIdentity($host, $realm, $username, $password) {
- $this->_authenticator->setIdentityForRealm($host, $realm, $username, $password);
- }
-
- function &fetchResponse($url, $encoding) {
- if ($encoding->getMethod() != 'POST') {
- $url->addRequestParameters($encoding);
- $encoding->clear();
- }
- $response = &$this->_fetchWhileRedirected($url, $encoding);
- if ($headers = $response->getHeaders()) {
- if ($headers->isChallenge()) {
- $this->_authenticator->addRealm(
- $url,
- $headers->getAuthentication(),
- $headers->getRealm());
- }
- }
- return $response;
- }
-
- function &_fetchWhileRedirected($url, $encoding) {
- $redirects = 0;
- do {
- $response = &$this->_fetch($url, $encoding);
- if ($response->isError()) {
- return $response;
- }
- $headers = $response->getHeaders();
- $location = new SimpleUrl($headers->getLocation());
- $url = $location->makeAbsolute($url);
- if ($this->_cookies_enabled) {
- $headers->writeCookiesToJar($this->_cookie_jar, $url);
- }
- if (! $headers->isRedirect()) {
- break;
- }
- $encoding = new SimpleGetEncoding();
- } while (! $this->_isTooManyRedirects(++$redirects));
- return $response;
- }
-
- function &_fetch($url, $encoding) {
- $request = &$this->_createRequest($url, $encoding);
- $response = &$request->fetch($this->_connection_timeout);
- return $response;
- }
-
- function &_createRequest($url, $encoding) {
- $request = &$this->_createHttpRequest($url, $encoding);
- $this->_addAdditionalHeaders($request);
- if ($this->_cookies_enabled) {
- $request->readCookiesFromJar($this->_cookie_jar, $url);
- }
- $this->_authenticator->addHeaders($request, $url);
- return $request;
- }
-
- function &_createHttpRequest($url, $encoding) {
- $request = &new SimpleHttpRequest($this->_createRoute($url), $encoding);
- return $request;
- }
-
- function &_createRoute($url) {
- if ($this->_proxy) {
- $route = &new SimpleProxyRoute(
- $url,
- $this->_proxy,
- $this->_proxy_username,
- $this->_proxy_password);
- } else {
- $route = &new SimpleRoute($url);
- }
- return $route;
- }
-
- function _addAdditionalHeaders(&$request) {
- foreach ($this->_additional_headers as $header) {
- $request->addHeaderLine($header);
- }
- }
- }
- ?>
|