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 function GetCurrentUserFriends($limit, &$next)
 {
     if ($this->access_token === false) {
         return false;
     }
     $http = new HttpClient();
     $http->setHeader('GData-Version', '3.0');
     $http->setHeader('Authorization', 'Bearer ' . $this->access_token);
     $url = static::FRIENDS_URL . '?';
     $limit = intval($limit);
     $next = intval($next);
     if ($limit > 0) {
         $url .= '&max-results=' . $limit;
     }
     if ($next > 0) {
         $url .= '&start-index=' . $next;
     }
     $result = $http->get($url);
     if (!defined("BX_UTF")) {
         $result = CharsetConverter::ConvertCharset($result, "utf-8", LANG_CHARSET);
     }
     if ($http->getStatus() == 200) {
         $obXml = new \CDataXML();
         if ($obXml->loadString($result)) {
             $tree = $obXml->getTree();
             $total = $tree->elementsByName("totalResults");
             $total = intval($total[0]->textContent());
             $limitNode = $tree->elementsByName("itemsPerPage");
             $next += intval($limitNode[0]->textContent());
             if ($next >= $total) {
                 $next = '__finish__';
             }
             $arFriends = array();
             $arEntries = $tree->elementsByName('entry');
             foreach ($arEntries as $entry) {
                 $arEntry = array();
                 $entryChildren = $entry->children();
                 foreach ($entryChildren as $child) {
                     $tag = $child->name();
                     switch ($tag) {
                         case 'category':
                         case 'updated':
                         case 'edited':
                             break;
                         case 'name':
                             $arEntry[$tag] = array();
                             foreach ($child->children() as $subChild) {
                                 $arEntry[$tag][$subChild->name()] = $subChild->textContent();
                             }
                             break;
                         case 'email':
                             if ($child->getAttribute('primary') == 'true') {
                                 $arEntry[$tag] = $child->getAttribute('address');
                             }
                             break;
                         default:
                             $tagContent = $tag == 'link' ? $child->getAttribute('href') : $child->textContent();
                             if ($child->getAttribute('rel')) {
                                 if (!isset($arEntry[$tag])) {
                                     $arEntry[$tag] = array();
                                 }
                                 $arEntry[$tag][preg_replace("/^[^#]*#/", "", $child->getAttribute('rel'))] = $tagContent;
                             } elseif (isset($arEntry[$tag])) {
                                 if (!is_array($arEntry[$tag][0]) || !isset($arEntry[$tag][0])) {
                                     $arEntry[$tag] = array($arEntry[$tag], $tagContent);
                                 } else {
                                     $arEntry[$tag][] = $tagContent;
                                 }
                             } else {
                                 $arEntry[$tag] = $tagContent;
                             }
                     }
                 }
                 if ($arEntry['email']) {
                     $arFriends[] = $arEntry;
                 }
             }
             return $arFriends;
         }
     }
     return false;
 }
Пример #3
0
 protected function query($url)
 {
     if ($this->access_token === false) {
         return false;
     }
     $http = new HttpClient();
     $http->setHeader("authorization", "Bearer " . $this->access_token);
     $result = $http->get($url);
     if (!defined("BX_UTF")) {
         $result = CharsetConverter::ConvertCharset($result, "utf-8", LANG_CHARSET);
     }
     $result = CUtil::JsObjectToPhp($result);
     return $result;
 }
Пример #4
0
 private function getFileSizeInternal($downloadUrl)
 {
     $accessToken = $this->getAccessToken();
     $http = new HttpClient(array('socketTimeout' => 10, 'streamTimeout' => 30, 'version' => HttpClient::HTTP_1_1));
     $http->setHeader('Authorization', "Bearer {$accessToken}");
     if ($http->query('HEAD', $downloadUrl) === false) {
         $errorString = implode('; ', array_keys($http->getError()));
         $this->errorCollection->add(array(new Error($errorString, self::ERROR_HTTP_GET_METADATA)));
         return null;
     }
     if (!$this->checkHttpResponse($http)) {
         return null;
     }
     return $http->getHeaders()->get('Content-Length');
 }
Пример #5
0
 public function finance_query($method, $masterToken, $operationNum, $param = array(), $skipRefreshAuth = false)
 {
     if ($this->engineSettings['AUTH']) {
         $http = new HttpClient();
         $http->setRedirect(false);
         $http->setHeader("Content-Type", "application/json; charset=utf-8");
         $auth = $this->getCurrentUser();
         $financeToken = hash("sha256", $masterToken . $operationNum . $method . $auth['login']);
         $postData = array("method" => $method, "finance_token" => $financeToken, "operation_num" => $operationNum, "locale" => $this->locale, "token" => $this->engineSettings['AUTH']['access_token']);
         if (!empty($param)) {
             $postData["param"] = $param;
         }
         $postData = YandexJson::encode($postData, JSON_UNESCAPED_UNICODE);
         $http->post(self::API_URL, $postData);
         if ($http->getStatus() == 401 && !$skipRefreshAuth) {
             if ($this->checkAuthExpired(false)) {
                 $this->query($method, $param, true);
             }
         }
         return $http;
     } else {
         throw new SystemException("No Yandex auth data");
     }
 }
Пример #6
0
 /**
  * Lists folder contents
  * @param $path
  * @param $folderId
  * @return mixed
  */
 public function listFolder($path, $folderId)
 {
     if ($path === '/') {
         $folderId = '0';
     }
     $http = new HttpClient(array('socketTimeout' => 10, 'streamTimeout' => 30, 'version' => HttpClient::HTTP_1_1));
     $http->setHeader('Content-Type', 'application/json; charset=UTF-8');
     $http->setHeader('Authorization', "Bearer {$this->getAccessToken()}");
     if ($http->get(self::API_URL_V2 . "/folders/{$folderId}/items?fields=name,size,modified_at") === false) {
         $errorString = implode('; ', array_keys($http->getError()));
         $this->errorCollection->add(array(new Error($errorString, self::ERROR_HTTP_LIST_FOLDER)));
         return null;
     }
     if (!$this->checkHttpResponse($http)) {
         return null;
     }
     $items = Json::decode($http->getResult());
     if ($items === null) {
         $this->errorCollection->add(array(new Error('Could not decode response as json', self::ERROR_BAD_JSON)));
         return null;
     }
     if (!isset($items['entries'])) {
         $this->errorCollection->add(array(new Error('Could not find items in response', self::ERROR_HTTP_LIST_FOLDER)));
         return null;
     }
     $reformatItems = array();
     foreach ($items['entries'] as $item) {
         $isFolder = $item['type'] === 'folder';
         $dateTime = new \DateTime($item['modified_at']);
         $reformatItems[$item['id']] = array('id' => $item['id'], 'name' => $item['name'], 'type' => $isFolder ? 'folder' : 'file', 'size' => $isFolder ? '' : \CFile::formatSize($item['size']), 'sizeInt' => $isFolder ? '' : $item['size'], 'modifyBy' => '', 'modifyDate' => $dateTime->format('d.m.Y'), 'modifyDateInt' => $dateTime->getTimestamp(), 'provider' => static::getCode());
         if (!$isFolder) {
             $reformatItems[$item['id']]['storage'] = '';
             $reformatItems[$item['id']]['ext'] = getFileExtension($item['name']);
         }
     }
     unset($item);
     return $reformatItems;
 }
Пример #7
0
 /**
  * Returns HttpClient object with query result
  *
  * @param string $scope Url to call
  * @param string $method HTTP method (GET/POST/PUT supported)
  * @param array|null $data Post data
  * @param bool $skipRefreshAuth Skip authorization refresh
  *
  * @returns \Bitrix\Main\Web\HttpClient
  * @throws SystemException
  */
 protected function query($scope, $method = "GET", $data = null, $skipRefreshAuth = false)
 {
     if ($this->engineSettings['AUTH']) {
         $http = new Web\HttpClient();
         $http->setHeader('Authorization', 'OAuth ' . $this->engineSettings['AUTH']['access_token']);
         $http->setRedirect(false);
         switch ($method) {
             case 'GET':
                 $http->get($scope);
                 break;
             case 'POST':
                 $http->post($scope, $data);
                 break;
             case 'PUT':
                 $http->query($method, $scope, $data);
                 break;
             case 'DELETE':
                 break;
         }
         if ($http->getStatus() == 401 && !$skipRefreshAuth) {
             if ($this->checkAuthExpired(false)) {
                 $this->query($scope, $method, $data, true);
             }
         }
         return $http;
     } else {
         throw new SystemException("No Yandex auth data");
     }
 }
Пример #8
0
 protected function query($scope, $method = "GET", $data = null, $bSkipRefreshAuth = false, $contentType = 'application/json')
 {
     if ($this->engineSettings['AUTH']) {
         $http = new HttpClient();
         $http->setHeader("Authorization", 'Bearer ' . $this->engineSettings['AUTH']['access_token']);
         /*
         			$http->setAdditionalHeaders(
         				array(
         					'Authorization' => ,
         					'GData-Version' => '2'
         				)
         			);
         */
         switch ($method) {
             case 'GET':
                 $result = $http->get($scope);
                 break;
             case 'POST':
             case 'PUT':
                 $http->setHeader("Content-Type", $contentType);
                 $result = $http->query($method, $scope, $data);
                 break;
             case 'DELETE':
                 break;
         }
         if ($http->getStatus() == 401 && !$bSkipRefreshAuth) {
             if ($this->checkAuthExpired(true)) {
                 return $this->query($scope, $method, $data, true, $contentType);
             }
         }
         return $http;
     }
 }
Пример #9
0
 /**
  * Lists folder contents
  * @param $path
  * @param $folderId
  * @return mixed
  */
 public function listFolder($path, $folderId)
 {
     if ($path === '/') {
         $folderId = '';
     } else {
         $folderId = $this->getForApiDecodedId($folderId);
     }
     $http = new HttpClient(array('socketTimeout' => 10, 'streamTimeout' => 30, 'version' => HttpClient::HTTP_1_1));
     $http->setHeader('Content-Type', 'application/json; charset=UTF-8');
     $http->setHeader('Authorization', "Bearer {$this->getAccessToken()}");
     if ($http->get(self::API_URL . "/metadata/auto/{$folderId}") === false) {
         $errorString = implode('; ', array_keys($http->getError()));
         $this->errorCollection->add(array(new Error($errorString, self::ERROR_HTTP_LIST_FOLDER)));
         return null;
     }
     if (!$this->checkHttpResponse($http)) {
         return null;
     }
     $items = Json::decode($http->getResult());
     if ($items === null) {
         $this->errorCollection->add(array(new Error('Could not decode response as json', self::ERROR_BAD_JSON)));
         return null;
     }
     if (!isset($items['contents'])) {
         $this->errorCollection->add(array(new Error('Could not find items in response', self::ERROR_HTTP_LIST_FOLDER)));
         return null;
     }
     $reformatItems = array();
     foreach ($items['contents'] as $item) {
         $isFolder = (bool) $item['is_dir'];
         $dateTime = \DateTime::createFromFormat('D, d M Y H:i:s T', $item['modified']);
         $pseudoId = base64_encode($item['path']);
         $reformatItems[$pseudoId] = array('id' => $pseudoId, 'name' => bx_basename($item['path']), 'type' => $isFolder ? 'folder' : 'file', 'size' => $isFolder ? '' : \CFile::formatSize($item['bytes']), 'sizeInt' => $isFolder ? '' : $item['bytes'], 'modifyBy' => '', 'modifyDate' => $dateTime->format('d.m.Y'), 'modifyDateInt' => $dateTime->getTimestamp(), 'provider' => static::getCode());
         if (!$isFolder) {
             $reformatItems[$pseudoId]['storage'] = '';
             $reformatItems[$pseudoId]['ext'] = getFileExtension($reformatItems[$pseudoId]['name']);
         }
     }
     unset($item);
     return $reformatItems;
 }
Пример #10
0
 public function GetCurrentUser()
 {
     if ($this->access_token === false) {
         return false;
     }
     $h = new HttpClient();
     $h->setHeader("Authorization", "Bearer " . $this->access_token);
     $result = $h->get(static::ACCOUNT_URL);
     $result = Json::decode($result);
     if (is_array($result)) {
         $result["access_token"] = $this->access_token;
     }
     return $result;
 }
Пример #11
0
if (!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED !== true) {
    die;
}
use Bitrix\Main\Web\HttpClient;
use Bitrix\Main\Localization\Loc;
use Bitrix\Main\IO\Path;
Loc::loadLanguageFile(Path::combine(__DIR__, "statuses.php"));
$orderID = strlen(CSalePaySystemAction::GetParamValue("ORDER_ID")) > 0 ? CSalePaySystemAction::GetParamValue("ORDER_ID") : $GLOBALS["SALE_INPUT_PARAMS"]["ORDER"]["ID"];
$login = CSalePaySystemAction::GetParamValue("API_LOGIN");
$password = CSalePaySystemAction::GetParamValue("API_PASSWORD");
$shopId = CSalePaySystemAction::GetParamValue("SHOP_ID");
$changePayStatus = CSalePaySystemAction::GetParamValue("CHANGE_STATUS_PAY") == "Y";
$statusUrl = "https://w.qiwi.com/api/v2/prv/{prv_id}/bills/{bill_id}";
$request = new HttpClient();
$request->setAuthorization($login, $password);
$request->setHeader("Accept", "text/json");
$request->setCharset("utf-8");
$response = $request->get(str_replace(array("{prv_id}", "{bill_id}"), array($shopId, $orderID), $statusUrl));
if ($response === false) {
    return 1;
}
$response = (array) json_decode($response);
if (!$response || !isset($response['response'])) {
    return 1;
}
$response = (array) $response['response'];
if ((int) $response['result_code']) {
    CSaleOrder::Update($orderID, array("PS_STATUS" => "N", "PS_STATUS_CODE" => $response['result_code'], "PS_STATUS_MESSAGE" => Loc::getMessage("SALE_QWH_ERROR_CODE_" . $response['result_code']), "PS_STATUS_DESCRIPTION" => isset($response['description']) ? $response['description'] : "", "PS_RESPONSE_DATE" => \Bitrix\Main\Type\DateTime::createFromTimestamp(time())));
} elseif (isset($response['bill'])) {
    $bill = (array) $response['bill'];
    if ($order = CSaleOrder::getByID($bill['bill_id'])) {