|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00012 require_once(dirname(__FILE__) . '/socket.php'); 00013 require_once(dirname(__FILE__) . '/cookies.php'); 00014 require_once(dirname(__FILE__) . '/url.php'); 00023 class SimpleRoute { 00024 var $_url; 00025 00031 function SimpleRoute($url) { 00032 $this->_url = $url; 00033 } 00034 00040 function getUrl() { 00041 return $this->_url; 00042 } 00043 00050 function _getRequestLine($method) { 00051 return $method . ' ' . $this->_url->getPath() . 00052 $this->_url->getEncodedRequest() . ' HTTP/1.0'; 00053 } 00054 00060 function _getHostLine() { 00061 $line = 'Host: ' . $this->_url->getHost(); 00062 if ($this->_url->getPort()) { 00063 $line .= ':' . $this->_url->getPort(); 00064 } 00065 return $line; 00066 } 00067 00075 function &createConnection($method, $timeout) { 00076 $default_port = ('https' == $this->_url->getScheme()) ? 443 : 80; 00077 $socket = &$this->_createSocket( 00078 $this->_url->getScheme() ? $this->_url->getScheme() : 'http', 00079 $this->_url->getHost(), 00080 $this->_url->getPort() ? $this->_url->getPort() : $default_port, 00081 $timeout); 00082 if (! $socket->isError()) { 00083 $socket->write($this->_getRequestLine($method) . "\r\n"); 00084 $socket->write($this->_getHostLine() . "\r\n"); 00085 $socket->write("Connection: close\r\n"); 00086 } 00087 return $socket; 00088 } 00089 00099 function &_createSocket($scheme, $host, $port, $timeout) { 00100 if (in_array($scheme, array('https'))) { 00101 $socket = new SimpleSecureSocket($host, $port, $timeout); 00102 } else { 00103 $socket = new SimpleSocket($host, $port, $timeout); 00104 } 00105 return $socket; 00106 } 00107 } 00108 00115 class SimpleProxyRoute extends SimpleRoute { 00116 var $_proxy; 00117 var $_username; 00118 var $_password; 00119 00128 function SimpleProxyRoute($url, $proxy, $username = false, $password = false) { 00129 $this->SimpleRoute($url); 00130 $this->_proxy = $proxy; 00131 $this->_username = $username; 00132 $this->_password = $password; 00133 } 00134 00142 function _getRequestLine($method) { 00143 $url = $this->getUrl(); 00144 $scheme = $url->getScheme() ? $url->getScheme() : 'http'; 00145 $port = $url->getPort() ? ':' . $url->getPort() : ''; 00146 return $method . ' ' . $scheme . '://' . $url->getHost() . $port . 00147 $url->getPath() . $url->getEncodedRequest() . ' HTTP/1.0'; 00148 } 00149 00156 function _getHostLine() { 00157 $host = 'Host: ' . $this->_proxy->getHost(); 00158 $port = $this->_proxy->getPort() ? $this->_proxy->getPort() : 8080; 00159 return "$host:$port"; 00160 } 00161 00169 function &createConnection($method, $timeout) { 00170 $socket = &$this->_createSocket( 00171 $this->_proxy->getScheme() ? $this->_proxy->getScheme() : 'http', 00172 $this->_proxy->getHost(), 00173 $this->_proxy->getPort() ? $this->_proxy->getPort() : 8080, 00174 $timeout); 00175 if ($socket->isError()) { 00176 return $socket; 00177 } 00178 $socket->write($this->_getRequestLine($method) . "\r\n"); 00179 $socket->write($this->_getHostLine() . "\r\n"); 00180 if ($this->_username && $this->_password) { 00181 $socket->write('Proxy-Authorization: Basic ' . 00182 base64_encode($this->_username . ':' . $this->_password) . 00183 "\r\n"); 00184 } 00185 $socket->write("Connection: close\r\n"); 00186 return $socket; 00187 } 00188 } 00189 00196 class SimpleHttpRequest { 00197 var $_route; 00198 var $_encoding; 00199 var $_headers; 00200 var $_cookies; 00201 00211 function SimpleHttpRequest(&$route, $encoding) { 00212 $this->_route = &$route; 00213 $this->_encoding = $encoding; 00214 $this->_headers = array(); 00215 $this->_cookies = array(); 00216 } 00217 00226 function &fetch($timeout) { 00227 $socket = &$this->_route->createConnection($this->_encoding->getMethod(), $timeout); 00228 if (! $socket->isError()) { 00229 $this->_dispatchRequest($socket, $this->_encoding); 00230 } 00231 $response = &$this->_createResponse($socket); 00232 return $response; 00233 } 00234 00243 function _dispatchRequest(&$socket, $encoding) { 00244 foreach ($this->_headers as $header_line) { 00245 $socket->write($header_line . "\r\n"); 00246 } 00247 if (count($this->_cookies) > 0) { 00248 $socket->write("Cookie: " . implode(";", $this->_cookies) . "\r\n"); 00249 } 00250 $encoding->writeHeadersTo($socket); 00251 $socket->write("\r\n"); 00252 $encoding->writeTo($socket); 00253 } 00254 00260 function addHeaderLine($header_line) { 00261 $this->_headers[] = $header_line; 00262 } 00263 00271 function readCookiesFromJar($jar, $url) { 00272 $this->_cookies = $jar->selectAsPairs($url); 00273 } 00274 00281 function &_createResponse(&$socket) { 00282 $response = new SimpleHttpResponse( 00283 $socket, 00284 $this->_route->getUrl(), 00285 $this->_encoding); 00286 return $response; 00287 } 00288 } 00289 00295 class SimpleHttpHeaders { 00296 var $_raw_headers; 00297 var $_response_code; 00298 var $_http_version; 00299 var $_mime_type; 00300 var $_location; 00301 var $_cookies; 00302 var $_authentication; 00303 var $_realm; 00304 00310 function SimpleHttpHeaders($headers) { 00311 $this->_raw_headers = $headers; 00312 $this->_response_code = false; 00313 $this->_http_version = false; 00314 $this->_mime_type = ''; 00315 $this->_location = false; 00316 $this->_cookies = array(); 00317 $this->_authentication = false; 00318 $this->_realm = false; 00319 foreach (explode("\r\n", $headers) as $header_line) { 00320 $this->_parseHeaderLine($header_line); 00321 } 00322 } 00323 00329 function getHttpVersion() { 00330 return $this->_http_version; 00331 } 00332 00338 function getRaw() { 00339 return $this->_raw_headers; 00340 } 00341 00347 function getResponseCode() { 00348 return (integer)$this->_response_code; 00349 } 00350 00357 function getLocation() { 00358 return $this->_location; 00359 } 00360 00366 function isRedirect() { 00367 return in_array($this->_response_code, array(301, 302, 303, 307)) && 00368 (boolean)$this->getLocation(); 00369 } 00370 00377 function isChallenge() { 00378 return ($this->_response_code == 401) && 00379 (boolean)$this->_authentication && 00380 (boolean)$this->_realm; 00381 } 00382 00388 function getMimeType() { 00389 return $this->_mime_type; 00390 } 00391 00397 function getAuthentication() { 00398 return $this->_authentication; 00399 } 00400 00406 function getRealm() { 00407 return $this->_realm; 00408 } 00409 00416 function writeCookiesToJar(&$jar, $url) { 00417 foreach ($this->_cookies as $cookie) { 00418 $jar->setCookie( 00419 $cookie->getName(), 00420 $cookie->getValue(), 00421 $url->getHost(), 00422 $cookie->getPath(), 00423 $cookie->getExpiry()); 00424 } 00425 } 00426 00433 function _parseHeaderLine($header_line) { 00434 if (preg_match('/HTTP\/(\d+\.\d+)\s+(\d+)/i', $header_line, $matches)) { 00435 $this->_http_version = $matches[1]; 00436 $this->_response_code = $matches[2]; 00437 } 00438 if (preg_match('/Content-type:\s*(.*)/i', $header_line, $matches)) { 00439 $this->_mime_type = trim($matches[1]); 00440 } 00441 if (preg_match('/Location:\s*(.*)/i', $header_line, $matches)) { 00442 $this->_location = trim($matches[1]); 00443 } 00444 if (preg_match('/Set-cookie:(.*)/i', $header_line, $matches)) { 00445 $this->_cookies[] = $this->_parseCookie($matches[1]); 00446 } 00447 if (preg_match('/WWW-Authenticate:\s+(\S+)\s+realm=\"(.*?)\"/i', $header_line, $matches)) { 00448 $this->_authentication = $matches[1]; 00449 $this->_realm = trim($matches[2]); 00450 } 00451 } 00452 00459 function _parseCookie($cookie_line) { 00460 $parts = explode(";", $cookie_line); 00461 $cookie = array(); 00462 preg_match('/\s*(.*?)\s*=(.*)/', array_shift($parts), $cookie); 00463 foreach ($parts as $part) { 00464 if (preg_match('/\s*(.*?)\s*=(.*)/', $part, $matches)) { 00465 $cookie[$matches[1]] = trim($matches[2]); 00466 } 00467 } 00468 return new SimpleCookie( 00469 $cookie[1], 00470 trim($cookie[2]), 00471 isset($cookie["path"]) ? $cookie["path"] : "", 00472 isset($cookie["expires"]) ? $cookie["expires"] : false); 00473 } 00474 } 00475 00481 class SimpleHttpResponse extends SimpleStickyError { 00482 var $_url; 00483 var $_encoding; 00484 var $_sent; 00485 var $_content; 00486 var $_headers; 00487 00497 function SimpleHttpResponse(&$socket, $url, $encoding) { 00498 $this->SimpleStickyError(); 00499 $this->_url = $url; 00500 $this->_encoding = $encoding; 00501 $this->_sent = $socket->getSent(); 00502 $this->_content = false; 00503 $raw = $this->_readAll($socket); 00504 if ($socket->isError()) { 00505 $this->_setError('Error reading socket [' . $socket->getError() . ']'); 00506 return; 00507 } 00508 $this->_parse($raw); 00509 } 00510 00516 function _parse($raw) { 00517 if (! $raw) { 00518 $this->_setError('Nothing fetched'); 00519 $this->_headers = new SimpleHttpHeaders(''); 00520 } elseif (! strstr($raw, "\r\n\r\n")) { 00521 $this->_setError('Could not split headers from content'); 00522 $this->_headers = new SimpleHttpHeaders($raw); 00523 } else { 00524 list($headers, $this->_content) = explode("\r\n\r\n", $raw, 2); 00525 $this->_headers = new SimpleHttpHeaders($headers); 00526 } 00527 } 00528 00534 function getMethod() { 00535 return $this->_encoding->getMethod(); 00536 } 00537 00543 function getUrl() { 00544 return $this->_url; 00545 } 00546 00552 function getRequestData() { 00553 return $this->_encoding; 00554 } 00555 00561 function getSent() { 00562 return $this->_sent; 00563 } 00564 00571 function getContent() { 00572 return $this->_content; 00573 } 00574 00581 function getHeaders() { 00582 return $this->_headers; 00583 } 00584 00590 function getNewCookies() { 00591 return $this->_headers->getNewCookies(); 00592 } 00593 00602 function _readAll(&$socket) { 00603 $all = ''; 00604 while (! $this->_isLastPacket($next = $socket->read())) { 00605 $all .= $next; 00606 } 00607 return $all; 00608 } 00609 00617 function _isLastPacket($packet) { 00618 if (is_string($packet)) { 00619 return $packet === ''; 00620 } 00621 return ! $packet; 00622 } 00623 } 00624 ?>