123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- <?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 {
- private $cookie_jar;
- private $cookies_enabled = true;
- private $authenticator;
- private $max_redirects = DEFAULT_MAX_REDIRECTS;
- private $proxy = false;
- private $proxy_username = false;
- private $proxy_password = false;
- private $connection_timeout = DEFAULT_CONNECTION_TIMEOUT;
- private $additional_headers = array();
-
-
-
- function __construct() {
- $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;
- }
-
-
-
- protected 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;
- }
-
-
-
- protected 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;
- }
-
-
-
- protected function fetch($url, $encoding) {
- $request = $this->createRequest($url, $encoding);
- return $request->fetch($this->connection_timeout);
- }
-
-
-
- protected 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;
- }
-
-
-
- protected function createHttpRequest($url, $encoding) {
- return new SimpleHttpRequest($this->createRoute($url), $encoding);
- }
-
-
-
- protected function createRoute($url) {
- if ($this->proxy) {
- return new SimpleProxyRoute(
- $url,
- $this->proxy,
- $this->proxy_username,
- $this->proxy_password);
- }
- return new SimpleRoute($url);
- }
-
-
-
- protected function addAdditionalHeaders(&$request) {
- foreach ($this->additional_headers as $header) {
- $request->addHeaderLine($header);
- }
- }
- }
- ?>
|