|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00012 require_once(dirname(__FILE__) . '/cookies.php'); 00013 require_once(dirname(__FILE__) . '/http.php'); 00014 require_once(dirname(__FILE__) . '/encoding.php'); 00015 require_once(dirname(__FILE__) . '/authentication.php'); 00018 if (! defined('DEFAULT_MAX_REDIRECTS')) { 00019 define('DEFAULT_MAX_REDIRECTS', 3); 00020 } 00021 if (! defined('DEFAULT_CONNECTION_TIMEOUT')) { 00022 define('DEFAULT_CONNECTION_TIMEOUT', 15); 00023 } 00024 00031 class SimpleUserAgent { 00032 var $_cookie_jar; 00033 var $_cookies_enabled = true; 00034 var $_authenticator; 00035 var $_max_redirects = DEFAULT_MAX_REDIRECTS; 00036 var $_proxy = false; 00037 var $_proxy_username = false; 00038 var $_proxy_password = false; 00039 var $_connection_timeout = DEFAULT_CONNECTION_TIMEOUT; 00040 var $_additional_headers = array(); 00041 00046 function SimpleUserAgent() { 00047 $this->_cookie_jar = new SimpleCookieJar(); 00048 $this->_authenticator = new SimpleAuthenticator(); 00049 } 00050 00060 function restart($date = false) { 00061 $this->_cookie_jar->restartSession($date); 00062 $this->_authenticator->restartSession(); 00063 } 00064 00071 function addHeader($header) { 00072 $this->_additional_headers[] = $header; 00073 } 00074 00080 function ageCookies($interval) { 00081 $this->_cookie_jar->agePrematurely($interval); 00082 } 00083 00094 function setCookie($name, $value, $host = false, $path = '/', $expiry = false) { 00095 $this->_cookie_jar->setCookie($name, $value, $host, $path, $expiry); 00096 } 00097 00108 function getCookieValue($host, $path, $name) { 00109 return $this->_cookie_jar->getCookieValue($host, $path, $name); 00110 } 00111 00120 function getBaseCookieValue($name, $base) { 00121 if (! $base) { 00122 return null; 00123 } 00124 return $this->getCookieValue($base->getHost(), $base->getPath(), $name); 00125 } 00126 00131 function ignoreCookies() { 00132 $this->_cookies_enabled = false; 00133 } 00134 00139 function useCookies() { 00140 $this->_cookies_enabled = true; 00141 } 00142 00148 function setConnectionTimeout($timeout) { 00149 $this->_connection_timeout = $timeout; 00150 } 00151 00158 function setMaximumRedirects($max) { 00159 $this->_max_redirects = $max; 00160 } 00161 00171 function useProxy($proxy, $username, $password) { 00172 if (! $proxy) { 00173 $this->_proxy = false; 00174 return; 00175 } 00176 if ((strncmp($proxy, 'http://', 7) != 0) && (strncmp($proxy, 'https://', 8) != 0)) { 00177 $proxy = 'http://'. $proxy; 00178 } 00179 $this->_proxy = new SimpleUrl($proxy); 00180 $this->_proxy_username = $username; 00181 $this->_proxy_password = $password; 00182 } 00183 00190 function _isTooManyRedirects($redirects) { 00191 return ($redirects > $this->_max_redirects); 00192 } 00193 00202 function setIdentity($host, $realm, $username, $password) { 00203 $this->_authenticator->setIdentityForRealm($host, $realm, $username, $password); 00204 } 00205 00214 function &fetchResponse($url, $encoding) { 00215 if ($encoding->getMethod() != 'POST') { 00216 $url->addRequestParameters($encoding); 00217 $encoding->clear(); 00218 } 00219 $response = &$this->_fetchWhileRedirected($url, $encoding); 00220 if ($headers = $response->getHeaders()) { 00221 if ($headers->isChallenge()) { 00222 $this->_authenticator->addRealm( 00223 $url, 00224 $headers->getAuthentication(), 00225 $headers->getRealm()); 00226 } 00227 } 00228 return $response; 00229 } 00230 00239 function &_fetchWhileRedirected($url, $encoding) { 00240 $redirects = 0; 00241 do { 00242 $response = &$this->_fetch($url, $encoding); 00243 if ($response->isError()) { 00244 return $response; 00245 } 00246 $headers = $response->getHeaders(); 00247 $location = new SimpleUrl($headers->getLocation()); 00248 $url = $location->makeAbsolute($url); 00249 if ($this->_cookies_enabled) { 00250 $headers->writeCookiesToJar($this->_cookie_jar, $url); 00251 } 00252 if (! $headers->isRedirect()) { 00253 break; 00254 } 00255 $encoding = new SimpleGetEncoding(); 00256 } while (! $this->_isTooManyRedirects(++$redirects)); 00257 return $response; 00258 } 00259 00267 function &_fetch($url, $encoding) { 00268 $request = &$this->_createRequest($url, $encoding); 00269 $response = &$request->fetch($this->_connection_timeout); 00270 return $response; 00271 } 00272 00280 function &_createRequest($url, $encoding) { 00281 $request = &$this->_createHttpRequest($url, $encoding); 00282 $this->_addAdditionalHeaders($request); 00283 if ($this->_cookies_enabled) { 00284 $request->readCookiesFromJar($this->_cookie_jar, $url); 00285 } 00286 $this->_authenticator->addHeaders($request, $url); 00287 return $request; 00288 } 00289 00297 function &_createHttpRequest($url, $encoding) { 00298 $request = new SimpleHttpRequest($this->_createRoute($url), $encoding); 00299 return $request; 00300 } 00301 00308 function &_createRoute($url) { 00309 if ($this->_proxy) { 00310 $route = new SimpleProxyRoute( 00311 $url, 00312 $this->_proxy, 00313 $this->_proxy_username, 00314 $this->_proxy_password); 00315 } else { 00316 $route = new SimpleRoute($url); 00317 } 00318 return $route; 00319 } 00320 00326 function _addAdditionalHeaders(&$request) { 00327 foreach ($this->_additional_headers as $header) { 00328 $request->addHeaderLine($header); 00329 } 00330 } 00331 } 00332 ?>