public function GetAccessToken() { if (($tokens = $this->getStorageTokens()) && is_array($tokens)) { $this->access_token = $tokens["OATOKEN"]; if ($this->checkAccessToken()) { return true; } } if ($this->code === false) { return false; } $h = new \Bitrix\Main\Web\HttpClient(array("socketTimeout" => $this->httpTimeout)); $h->setAuthorization($this->appID, $this->appSecret); $result = $h->post(self::TOKEN_URL, array("grant_type" => "authorization_code", "code" => $this->code, "client_id" => $this->appID)); $this->arResult = \Bitrix\Main\Web\Json::decode($result); if (isset($this->arResult["access_token"]) && $this->arResult["access_token"] != '') { // yandex doesn't send refresh tokens but I leave it here in case they will if (isset($this->arResult["refresh_token"]) && $this->arResult["refresh_token"] != '') { $this->refresh_token = $this->arResult["refresh_token"]; } $this->access_token = $this->arResult["access_token"]; $this->accessTokenExpires = $this->arResult["expires_in"] + time(); $_SESSION["OAUTH_DATA"] = array("OATOKEN" => $this->access_token); return true; } return false; }
private function Process($service, $id = false, $jsdecode = true) { $token = $this->GetToken(); $s = curl_init(); if ($id !== false) { $service = str_replace("#ID#", $id, $service); } curl_setopt($s, CURLOPT_URL, "https://api.ozon.ru/{$service}"); //?applicationid={$this->appID}&token=$token curl_setopt($s, CURLOPT_HTTPHEADER, array("x-ApiVersion: 0.1", "accept:application/json", "x-applicationid:{$this->appID}", "x-token:{$token}")); curl_setopt($s, CURLOPT_RETURNTRANSFER, true); $reply = curl_exec($s); curl_close($s); if ($jsdecode) { if (strtoupper(SITE_CHARSET) == "UTF-8") { $reply = mb_convert_encoding($reply, "utf8", "cp1251"); } try { $reply = \Bitrix\Main\Web\Json::decode($reply); } catch (Exception $e) { $reply = array(); } } return $reply; }
/** * Response constructor. * @param $response */ public function __construct($response) { $this->response = $response; $this->fields = Json::decode($this->response); if ($this->fields['error_code']) { $this->setError($this->fields); } $this->state = 'ok'; if ($this->getErrors()) { $this->state = 'error'; } }
public function verifyGetToken($domain, $dir) { $data = array("verificationMethod" => "FILE", "site" => array("identifier" => self::SCOPE_DOMAIN_PROTOCOL . $domain . $dir, "type" => "SITE")); $queryResult = $this->queryJson(static::QUERY_VERIFY_TOKEN, "POST", Json::encode($data)); if (!$queryResult) { return false; } if ($queryResult->getStatus() == self::HTTP_STATUS_OK && strlen($queryResult->getResult()) > 0) { $result = Json::decode($queryResult->getResult()); return $result["token"]; } else { throw new \Exception('Query error! ' . $queryResult->getStatus() . ': ' . $queryResult->getResult()); } }
/** * 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; }
protected function getSharedEmbedLink(FileData $fileData) { if (!$this->checkRequiredInputParams($fileData->toArray(), array('id'))) { return null; } $accessToken = $this->getAccessToken(); $http = new HttpClient(array('socketTimeout' => 10, 'streamTimeout' => 30, 'version' => HttpClient::HTTP_1_1)); $accessToken = urlencode($accessToken); if ($http->get("https://apis.live.net/v5.0/{$fileData->getId()}/embed?access_token={$accessToken}") === false) { $errorString = implode('; ', array_keys($http->getError())); $this->errorCollection->add(array(new Error($errorString, self::ERROR_SHARED_EMBED_LINK))); return null; } if (!$this->checkHttpResponse($http)) { return null; } $finalOutput = Json::decode($http->getResult()); if ($finalOutput === null) { $this->errorCollection->add(array(new Error('Could not decode response as json', self::ERROR_BAD_JSON))); return null; } if (!preg_match('%src="(.*)"%iuU', $finalOutput['embed_html'], $m)) { $this->errorCollection->add(array(new Error(Loc::getMessage('DISK_ONE_DRIVE_HANDLER_ERROR_COULD_NOT_FIND_EMBED_LINK'), self::ERROR_COULD_NOT_FIND_EMBED_LINK))); return null; } return $m[1]; }
protected function prepareAnswer($result) { return \Bitrix\Main\Web\Json::decode($result); }
public function unsign($signedValue, $salt = null) { $encodedValue = parent::unsign($signedValue, $salt); return Json::decode(base64_decode($encodedValue)); }
public function GetAppInfo() { if ($this->access_token === false) { return false; } $h = new \Bitrix\Main\Web\HttpClient(); $h->setTimeout($this->httpTimeout); $result = $h->post(static::TOKENINFO_URL . '?access_token=' . urlencode($this->access_token)); $result = \Bitrix\Main\Web\Json::decode($result); if (is_array($result) && $result["audience"]) { $result["id"] = $result["audience"]; } return $result; }
public function GetCurrentUser() { if ($this->access_token === false) { return false; } $h = new \Bitrix\Main\Web\HttpClient(); $h->setHeader("Authorization", "Bearer " . $this->access_token); $result = $h->get(static::ACCOUNT_URL); $result = \Bitrix\Main\Web\Json::decode($result); if (is_array($result)) { $result["access_token"] = $this->access_token; } return $result; }
public function GetAppInfo() { if ($this->access_token === false) { return false; } $h = new \Bitrix\Main\Web\HttpClient(); $h->setTimeout($this->httpTimeout); $result = $h->get(self::GRAPH_URL . '/debug_token?input_token=' . $this->access_token . '&access_token=' . $this->appID . "|" . $this->appSecret); $result = \Bitrix\Main\Web\Json::decode($result); if ($result["data"]["app_id"]) { $result["id"] = $result["data"]["app_id"]; } return $result; }
public function GetCurrentUser() { if ($this->access_token === false) { return false; } $httpClient = new \Bitrix\Main\Web\HttpClient(); $httpClient->setHeader("Authorization", "Bearer " . $this->access_token); $result = $httpClient->get($this->resource . static::VERSION . static::CONTACTS_URL); $result = \Bitrix\Main\Web\Json::decode($result); if (is_array($result)) { $result["access_token"] = $this->access_token; $result["refresh_token"] = $this->refresh_token; $result["expires_in"] = $this->accessTokenExpires; } return $result; }
protected function prepareAnswer($result) { return Json::decode($result); }
public static function Get($messageId, $params = false) { $arResult = array(); if (is_array($messageId)) { if (!empty($messageId)) { foreach ($messageId as $key => $value) { $messageId[$key] = intval($value); $arResult[$messageId[$key]] = array(); } } else { return $arResult; } } else { $messageId = intval($messageId); if ($messageId <= 0) { return false; } $arResult[$messageId] = array(); } $filter = array('=MESSAGE_ID' => $messageId); if ($params && strlen($params) > 0) { $filter['=PARAM_NAME'] = $params; } $messageParameters = IM\MessageParamTable::getList(array('select' => array('ID', 'MESSAGE_ID', 'PARAM_NAME', 'PARAM_VALUE', 'PARAM_JSON'), 'filter' => $filter)); while ($ar = $messageParameters->fetch()) { if (strlen($ar["PARAM_JSON"])) { $value = \Bitrix\Main\Web\Json::decode($ar["PARAM_JSON"]); } else { $value = $ar["PARAM_VALUE"]; } $arResult[$ar["MESSAGE_ID"]][$ar["PARAM_NAME"]][] = $value; } if (is_array($messageId)) { foreach ($messageId as $key) { $arResult[$key] = self::PrepareValues($arResult[$key]); } } else { $arResult = self::PrepareValues($arResult[$messageId]); } return $arResult; }
/** * Returns current Yandex user data * * @return array * * @throws SystemException * @throws YandexException */ public function getCurrentUser() { if (!array_key_exists('AUTH_USER', $this->engineSettings) || !is_array($this->engineSettings['AUTH_USER'])) { $queryResult = self::query(self::QUERY_USER); if ($queryResult->getStatus() == self::HTTP_STATUS_OK && strlen($queryResult->getResult()) > 0) { $res = Web\Json::decode($queryResult->getResult()); if (is_array($res)) { $this->engineSettings['AUTH_USER'] = $res; $this->saveSettings(); return $this->engineSettings['AUTH_USER']; } } throw new Engine\YandexException($queryResult); } else { return $this->engineSettings['AUTH_USER']; } }
/** * 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; }
protected function prepareResponse($result) { $result = Json::decode($result); if (is_array($result) && is_array($result["result"]) && array_key_exists(static::REPONSE_KEY_BROADCAST, $result["result"])) { try { Network::processBroadcastData($result["result"][static::REPONSE_KEY_BROADCAST]); } catch (Exception $e) { AddMessage2Log(array($e->getMessage(), $e->getFile(), $e->getLine())); } unset($result["result"][static::REPONSE_KEY_BROADCAST]); } return $result; }
/** * Gets a file's metadata by ID * @param FileData $fileData * @return array|null */ private function getFileMetadataInternal(FileData $fileData) { if (!$this->checkRequiredInputParams($fileData->toArray(), array('id'))) { return null; } $accessToken = $this->getAccessToken(); $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 {$accessToken}"); if ($http->get(self::API_URL_V2 . '/files/' . $fileData->getId()) === 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; } $file = Json::decode($http->getResult()); if ($file === null) { $this->errorCollection->add(array(new Error('Could not decode response as json', self::ERROR_BAD_JSON))); return null; } return $file; }
/** Get day context singleton instance. * @return self */ public static function getInstance() { if (!self::$instance) { $instance = new self(); $varName = self::getVarName(); $session =& $_SESSION[$varName]; $expire = strtotime('today 23:59'); if (!(is_array($session) && is_int($session['ID']) && $session['EXPIRE'] === $expire)) { $session = array('ID' => null, 'EXPIRE' => $expire, 'UNIQUE' => array()); //global $APPLICATION; $cookie = $APPLICATION->get_cookie($varname); if ($cookie = $_COOKIE[$varName]) { try { $cookie = Json::decode($cookie); } catch (ArgumentException $e) { } } // validate cookie if (is_array($cookie) && is_array($cookie['UNIQUE']) && $cookie['EXPIRE'] === $expire && ($id = $cookie['ID']) !== null && is_int($id) && ($id === self::EMPTY_CONTEXT_ID || Internals\ContextTable::getByPrimary($id)->fetch())) { // 1. valid cookie $session['ID'] = $id; $session['UNIQUE'] = $cookie['UNIQUE']; } else { // 2. invalid cookie Asset::getInstance()->addString(' <script type="text/javascript"> BX.ajax.post( "/bitrix/tools/conversion/ajax_counter.php", { SITE_ID: BX.message("SITE_ID"), sessid: BX.message("bitrix_sessid"), HTTP_REFERER: "' . \CUtil::JSEscape($_SERVER['HTTP_REFERER']) . '" }, function () {} ); </script> '); } } $instance->id = $session['ID']; self::$session =& $session; self::$instance = $instance; } return self::$instance; }
public static function register() { $httpClient = new HttpClient(); require_once $_SERVER["DOCUMENT_ROOT"] . "/bitrix/modules/main/classes/general/update_client.php"; $queryParams = array("action" => "register", "key" => md5(\CUpdateClient::GetLicenseKey()), "redirect_uri" => static::getRedirectUri()); $result = $httpClient->post(static::SERVICE_URL . static::REGISTER, $queryParams); $result = Json::decode($result); if ($result["error"]) { throw new SystemException($result["error"]); } else { static::setAccessSettings($result); } }
/** Get day context singleton instance. * @return self */ public static function getInstance() { if (!self::$instance) { $instance = new self(); $varName = self::getVarName(); $session =& $_SESSION[$varName]; $expire = strtotime('today 23:59'); if (!(is_array($session) && is_int($session['ID']) && $session['EXPIRE'] === $expire)) { $session = array('ID' => null, 'EXPIRE' => $expire, 'UNIQUE' => array()); //global $APPLICATION; $cookie = $APPLICATION->get_cookie($varname); if ($cookie = $_COOKIE[$varName]) { try { $cookie = Json::decode($cookie); } catch (ArgumentException $e) { } } // check if cookie is valid if (is_array($cookie) && is_array($cookie['UNIQUE']) && $cookie['EXPIRE'] === $expire && ($id = $cookie['ID']) !== null && is_int($id) && ($id === self::EMPTY_CONTEXT_ID || Internals\ContextTable::getByPrimary($id)->fetch())) { $session['ID'] = $id; $session['UNIQUE'] = $cookie['UNIQUE']; } } $instance->id = $session['ID']; self::$session =& $session; self::$instance = $instance; } return self::$instance; }
public function GetCurrentUser() { if ($this->access_token === false) { return false; } $h = new \Bitrix\Main\Web\HttpClient(); $result = $h->get(self::USERINFO_URL . '?format=json&oauth_token=' . urlencode($this->access_token)); $result = \Bitrix\Main\Web\Json::decode($result); if (is_array($result)) { $result["access_token"] = $this->access_token; $result["refresh_token"] = $this->refresh_token; $result["expires_in"] = $this->accessTokenExpires; } return $result; }
public function GetAppInfo() { if ($this->access_token === false) { return false; } $h = new \Bitrix\Main\Web\HttpClient(); $h->setTimeout($this->httpTimeout); $result = $h->get(self::APP_URL . '?fields=id&access_token=' . urlencode($this->access_token)); $result = \Bitrix\Main\Web\Json::decode($result); return $result['response']; }
public static function registerSite($domain) { $query = new \Bitrix\Main\Web\HttpClient(); $result = $query->get(B24NETWORK_URL . '/client.php?action=register&redirect_uri=' . urlencode($domain . '/bitrix/tools/oauth/bitrix24net.php') . '&key=' . urlencode(LICENSE_KEY)); $arResult = \Bitrix\Main\Web\Json::decode($result); if (is_array($arResult)) { return $arResult; } else { return array("error" => "Unknown response", "error_details" => $result); } }
public function GetCurrentUserFriends($limit, &$next) { if ($this->access_token === false) { return false; } if (empty($next)) { $url = self::GRAPH_URL . '/me/friends?access_token=' . $this->access_token . "&fields=picture,id,name,first_name,last_name,gender,email"; if ($limit > 0) { $url .= "&limit=" . intval($limit) . "&offset=" . intval($next); } } else { $url = $next; } $http = new HttpClient(); $http->setTimeout($this->httpTimeout); $result = $http->get($url); $result = Json::decode($result); if (is_array($result['paging']) && !empty($result['paging']['next'])) { $next = $result['paging']['next']; } else { $next = ''; } return $result; }