private function GetResponse() { if (!$this->connected) { return null; } $arHeaders = array(); $body = ""; while ($line = fgets($this->fp, 4096)) { if ($line == "\r\n") { break; } $arHeaders[] = trim($line); } if (count($arHeaders) <= 0) { return null; } $bChunked = $bConnectionClosed = false; $contentLength = null; foreach ($arHeaders as $value) { if (!$bChunked && preg_match("#Transfer-Encoding:\\s*chunked#i", $value)) { $bChunked = true; } if (!$bConnectionClosed && preg_match('#Connection:\\s*close#i', $value)) { $bConnectionClosed = true; } if (is_null($contentLength)) { if (preg_match('#Content-Length:\\s*([0-9]*)#i', $value, $arMatches)) { $contentLength = intval($arMatches[1]); } if (preg_match('#HTTP/1\\.1\\s+204#i', $value)) { $contentLength = 0; } } } if ($bChunked) { do { $line = fgets($this->fp, 4096); $line = strtolower($line); $chunkSize = ""; $i = 0; while ($i < strlen($line)) { $c = substr($line, $i, 1); if (in_array($c, array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"))) { $chunkSize .= $c; } else { break; } $i++; } $chunkSize = hexdec($chunkSize); if ($chunkSize > 0) { $lb = $chunkSize; $body1 = ''; while ($lb > 0) { $d = fread($this->fp, $lb); if ($d === false) { break; } $body1 .= $d; $lb = $chunkSize - (function_exists('mb_strlen') ? mb_strlen($body1, 'latin1') : strlen($body1)); } $body .= $body1; } fgets($this->fp, 4096); } while ($chunkSize); } elseif ($contentLength === 0) { } elseif ($contentLength > 0) { $lb = $contentLength; while ($lb > 0) { $d = fread($this->fp, $lb); if ($d === false) { break; } $body .= $d; $lb = $contentLength - (function_exists('mb_strlen') ? mb_strlen($body, 'latin1') : strlen($body)); } } else { socket_set_timeout($this->fp, 0); while (!feof($this->fp)) { $d = fread($this->fp, 4096); if ($d === false) { break; } $body .= $d; if (substr($body, -9) == "\r\n\r\n0\r\n\r\n") { $body = substr($body, 0, -9); break; } } socket_set_timeout($this->fp, $this->socketTimeout); } if ($bConnectionClosed) { $this->Disconnect(); } $responce = new CDavExchangeClientResponce($arHeaders, $body); $httpVersion = $responce->GetStatus('version'); if (is_null($httpVersion) || $httpVersion != 'HTTP/1.1' && $httpVersion != 'HTTP/1.0') { return null; } return $responce; }
private function Authenticate($request, $response) { $authenticate = $response->GetHeader('WWW-Authenticate'); $authenticateProxy = $response->GetHeader('Proxy-Authenticate'); if (is_null($authenticate) && is_null($authenticateProxy)) { return null; } if (!is_null($authenticate) && !is_array($authenticate)) { $authenticate = array($authenticate); } if (!is_null($authenticateProxy) && !is_array($authenticateProxy)) { $authenticateProxy = array($authenticateProxy); } if (!is_null($authenticate)) { $arAuth = array(); foreach ($authenticate as $auth) { $auth = trim($auth); $p = strpos($auth, " "); if ($p !== false) { $arAuth[strtolower(substr($auth, 0, $p))] = trim(substr($auth, $p)); } else { $arAuth[strtolower($auth)] = ""; } } if (array_key_exists("digest", $arAuth)) { $request = $this->AuthenticateDigest(CDavExchangeClientResponce::ExtractArray($arAuth["digest"]), $request, $response, "Authorization"); } elseif (array_key_exists("basic", $arAuth)) { $request = $this->AuthenticateBasic(CDavExchangeClientResponce::ExtractArray($arAuth["basic"]), $request, $response, "Authorization"); } elseif (array_key_exists("googlelogin", $arAuth)) { $request = $this->AuthenticateGoogleLogin(CDavExchangeClientResponce::ExtractArray($arAuth["basic"]), $request, $response, "Authorization"); if ($request === null) { return null; } } else { return null; } } if (!is_null($authenticateProxy)) { $arAuthProxy = array(); foreach ($authenticateProxy as $auth) { $auth = trim($auth); $p = strpos($auth, " "); if ($p !== false) { $arAuthProxy[strtolower(substr($auth, 0, $p))] = trim(substr($auth, $p)); } else { $arAuthProxy[strtolower($auth)] = ""; } } if (array_key_exists("digest", $arAuthProxy)) { $request = $this->AuthenticateDigest(CDavExchangeClientResponce::ExtractArray($arAuthProxy["digest"]), $request, $response, "Proxy-Authorization"); } elseif (array_key_exists("basic", $arAuthProxy)) { $request = $this->AuthenticateBasic(CDavExchangeClientResponce::ExtractArray($arAuthProxy["basic"]), $request, $response, "Proxy-Authorization"); } else { return null; } } return $request; }