public function Send(array $request)
 {
     $method = isset($request['METHOD']) ? strtoupper($request['METHOD']) : '';
     if ($method !== \Bitrix\Main\Web\HttpClient::HTTP_GET && $method !== \Bitrix\Main\Web\HttpClient::HTTP_POST) {
         throw new Bitrix\Main\ArgumentException("Could not find 'METHOD'.", 'request');
     }
     $path = isset($request['PATH']) && is_string($request['PATH']) ? $request['PATH'] : '';
     if ($path === '') {
         throw new Bitrix\Main\ArgumentException("Could not find 'PATH'.", 'request');
     }
     $postData = $method === \Bitrix\Main\Web\HttpClient::HTTP_POST && isset($request['BODY']) ? $request['BODY'] : null;
     if (!$this->client) {
         $this->client = new \Bitrix\Main\Web\HttpClient();
     }
     $this->client->setRedirect(false);
     if ($method === \Bitrix\Main\Web\HttpClient::HTTP_POST && is_array($postData)) {
         //Force UTF encoding
         $this->client->setCharset('UTF-8');
         if ((!isset($request['UTF']) || !$request['UTF']) && !defined('BX_UTF')) {
             $postData = \Bitrix\Main\Text\Encoding::convertEncodingArray($postData, SITE_CHARSET, 'UTF-8');
         }
     }
     $headers = isset($request['HEADERS']) ? $request['HEADERS'] : null;
     if (is_array($headers)) {
         foreach ($headers as $k => $v) {
             $this->client->setHeader($k, $v, true);
         }
     }
     if (!empty($this->cookies)) {
         $this->client->setCookies($this->cookies);
     }
     if ($this->enableProxy) {
         $this->client->setProxy($this->proxyServer, $this->proxyPort, $this->proxyUserName, $this->proxyUserPassword);
     }
     if ($this->userName !== '') {
         $this->client->setAuthorization($this->userName, $this->userPassword);
     }
     $this->client->setHeader('User-Agent', $this->userAgent, true);
     $absolutePath = $this->GetUrl() . $path;
     if (!$this->client->query($method, $absolutePath, $postData)) {
         $this->responseData = null;
         $this->errors = $this->client->getError();
     } else {
         /**@var \Bitrix\Main\Web\HttpHeaders*/
         $responseHeaders = $this->client->getHeaders();
         //STATUS.VERSION & STATUS.PHRASE are delcared for backward compatibility only.
         $this->responseData = array('STATUS' => array('VERSION' => '', 'CODE' => $this->client->getStatus(), 'PHRASE' => ''), 'CONTENT' => array('TYPE' => $this->client->getContentType(), 'ENCODING' => $this->client->getCharset()), 'HEADERS' => $responseHeaders, 'BODY' => $this->client->getResult());
         if ($responseHeaders->get('Set-Cookie', false) !== null) {
             $this->cookies = array_merge($this->cookies, $this->client->getCookies()->toArray());
             CCrmExternalSale::Update($this->externalSaleId, array('COOKIE' => serialize($this->cookies)));
         }
         $this->errors = array();
     }
     return $this->responseData;
 }
예제 #2
0
 public static function authorize()
 {
     \CSocServAuthManager::SetUniqueKey();
     $redirectUri = static::getRedirectUri();
     $url = static::getEngine()->getInterface()->GetAuthUrl($redirectUri, "backurl=" . urlencode($redirectUri . '?check_key=' . $_SESSION["UNIQUE_KEY"]));
     $httpClient = new HttpClient(array("redirect" => false));
     $result = $httpClient->get($url);
     if ($httpClient->getStatus() == 302) {
         return array("location" => $httpClient->getHeaders()->get("Location"));
     }
     throw new SystemException("Wrong response: " . $result);
 }
예제 #3
0
 protected function checkHttpResponse(HttpClient $http)
 {
     $status = (int) $http->getStatus();
     if ($status === 401) {
         $this->errorCollection->add(array(new Error('Invalid credentials (401)', self::ERROR_CODE_INVALID_CREDENTIALS)));
     } elseif ($status === 403) {
         $headers = $http->getHeaders();
         $response = $http->getResult();
         $errorMessage = '';
         if ($response && is_string($response)) {
             $jsonResponse = Json::decode($response);
             if (isset($jsonResponse['error']['message'])) {
                 $errorMessage = $jsonResponse['error']['message'];
             }
             unset($jsonResponse, $response);
         }
         $headerAuthenticate = $headers->get('WWW-Authenticate');
         if (is_string($headerAuthenticate) && strpos($headerAuthenticate, 'insufficient') !== false) {
             $this->errorCollection->add(array(new Error('Insufficient scope (403)', self::ERROR_CODE_INSUFFICIENT_SCOPE)));
             return false;
         } elseif (strpos($errorMessage, 'The authenticated user has not installed the app with client') !== false) {
             $this->errorCollection->add(array(new Error('The authenticated user has not installed the app (403)', self::ERROR_CODE_NOT_INSTALLED_APP)));
         } elseif (strpos($errorMessage, 'The authenticated user has not granted the app') !== false) {
             $this->errorCollection->add(array(new Error('The authenticated user has not granted the app (403)', self::ERROR_CODE_NOT_GRANTED_APP)));
         } elseif (strpos($errorMessage, 'Invalid accessLevel') !== false) {
             $this->errorCollection->add(array(new Error('Invalid accessLevel (403)', self::ERROR_CODE_INVALID_ACCESS_LEVEL)));
         } elseif (strpos($errorMessage, 'is not properly configured as a Google Drive app') !== false) {
             $this->errorCollection->add(array(new Error('The app does not exist or is not properly configured as a Google Drive app (403)', self::ERROR_CODE_APP_NOT_CONFIGURED)));
         } elseif (strpos($errorMessage, 'is blacklisted') !== false) {
             $this->errorCollection->add(array(new Error('The app is blacklisted as a Google Drive app. (403)', self::ERROR_CODE_APP_IN_BLACKLIST)));
         } elseif ($errorMessage) {
             $this->errorCollection->add(array(new Error($errorMessage, self::ERROR_CODE_UNKNOWN)));
         }
     }
     if ($this->errorCollection->hasErrors()) {
         return false;
     }
     return parent::checkHttpResponse($http);
 }
예제 #4
0
 /**
  * @param HttpClient $http
  * @return bool
  */
 protected function checkHttpResponse(HttpClient $http)
 {
     $status = (int) $http->getStatus();
     if ($status === 401) {
         $this->errorCollection->add(array(new Error('Invalid credentials (401)', self::ERROR_CODE_INVALID_CREDENTIALS)));
         return false;
     } elseif ($status === 403) {
         $headers = $http->getHeaders();
         $headerAuthenticate = $headers->get('WWW-Authenticate');
         if (is_string($headerAuthenticate) && strpos($headerAuthenticate, 'insufficient') !== false) {
             $this->errorCollection->add(array(new Error('Insufficient scope (403)', self::ERROR_CODE_INSUFFICIENT_SCOPE)));
             return false;
         }
     } elseif (!($status >= 200 && $status < 300)) {
         $this->errorCollection->add(array(new Error("Invalid response status code ({$status})", self::ERROR_INVALID_RESPONSE_STATUS)));
         return false;
     }
     return true;
 }
예제 #5
0
 /**
  * @param string $url Image's URL.
  * @return integer Saved file identifier
  */
 protected static function saveImage($url)
 {
     $fileId = false;
     $file = new \CFile();
     $httpClient = new HttpClient();
     $httpClient->setTimeout(5);
     $httpClient->setStreamTimeout(5);
     $urlComponents = parse_url($url);
     if ($urlComponents && strlen($urlComponents["path"]) > 0) {
         $tempPath = $file->GetTempName('', bx_basename($urlComponents["path"]));
     } else {
         $tempPath = $file->GetTempName('', bx_basename($url));
     }
     $httpClient->download($url, $tempPath);
     $fileName = $httpClient->getHeaders()->getFilename();
     $localFile = \CFile::MakeFileArray($tempPath);
     if (is_array($localFile)) {
         if (strlen($fileName) > 0) {
             $localFile['name'] = $fileName;
         }
         if (\CFile::CheckImageFile($localFile, 0, 0, 0, array("IMAGE")) === null) {
             $fileId = $file->SaveFile($localFile, 'urlpreview', true);
         }
     }
     return $fileId === false ? null : $fileId;
 }