%PDF- %PDF-
Server IP : 37.220.80.31 / Your IP : 3.129.70.113 Web Server : Apache/2.4.52 (Ubuntu) System : Linux 3051455-guretool.twc1.net 5.15.0-107-generic #117-Ubuntu SMP Fri Apr 26 12:26:49 UTC 2024 x86_64 User : www-root ( 1010) PHP Version : 7.4.33 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority, MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /var/www/www-root/data/www/dev.artlot24.ru/bitrix/modules/sale/lib/exchange/integration/rest/client/ |
Upload File : |
<?php namespace Bitrix\Sale\Exchange\Integration\Rest\Client; use Bitrix\Main\Application; use Bitrix\Main\ObjectPropertyException; use Bitrix\Main\SystemException; use Bitrix\Main\Text\Encoding; use Bitrix\Main\Web\HttpClient; use Bitrix\Main\Web\Json; use Bitrix\Sale\Exchange\Integration\OAuth; class Base { const LOG_DIR = '/bitrix/modules/sale/lib/exchange/integration/log'; const LOG_PATH = 'rest_client.log'; protected $accessToken; protected $refreshToken; protected $endPoint; protected $expiresIn; public function __construct(array $settings) { if (isset($settings["accessToken"])) { $this->setAccessToken($settings["accessToken"]); } if (isset($settings["refreshToken"])) { $this->setRefreshToken($settings["refreshToken"]); } if (isset($settings["endPoint"])) { $this->setEndPoint($settings["endPoint"]); } } public function getAccessToken() { return $this->accessToken; } public function setAccessToken($accessToken) { if (is_string($accessToken) && !empty($accessToken)) { $this->accessToken = $accessToken; } } public function getRefreshToken() { return $this->refreshToken; } public function setRefreshToken($refreshToken) { if (is_string($refreshToken) && !empty($refreshToken)) { $this->refreshToken = $refreshToken; } } public function getEndPoint() { return $this->endPoint; } public function setEndPoint($endPoint) { if (is_string($endPoint) && !empty($endPoint)) { $this->endPoint = $endPoint; } } public function getExpiresIn() { return $this->expiresIn; } public function setExpiresIn($expiresIn) { $this->expiresIn = $expiresIn; } public function call($method, $params = []) { $response = $this->makeRequest($method, $params); if (isset($response["error"]) && $response["error"] === "expired_token") { if ($this->refreshAccessToken()) { $response = $this->makeRequest($method, $params); } } return $response; } protected function makeRequest($method, $params = []) { $accessToken = $this->getAccessToken(); if ($accessToken === null) { throw new ObjectPropertyException("Access Token must be set."); } $endPoint = $this->getEndPoint(); if ($endPoint === null) { throw new ObjectPropertyException("End Point URL must be set."); } $httpClient = new HttpClient(); $httpClient->setHeader("User-Agent", "Bitrix Integration B24"); $httpClient->setCharset("UTF-8"); $params["auth"] = $accessToken; if (!Application::getInstance()->isUtfMode()) { $params = Encoding::convertEncoding($params, SITE_CHARSET, "UTF-8"); } $this->log("\\----------\n"); $this->log(['endpoint'=>$endPoint.$method,'params'=>$params]); $this->log([$endPoint.$method.'?'.http_build_query($params)]); $success = $httpClient->post($endPoint.$method, $params); if (!$success) { throw new SystemException("Wrong Rest Response. ".$endPoint.$method); } $result = $httpClient->getResult(); $this->log(['result'=>$result]); $this->log("\n ----------//\n"); try { $response = Json::decode($result); } catch (\Exception $exception) { throw new SystemException( "Wrong Rest Response. ".$endPoint.$method."\n\n".mb_substr($result, 0, 1024) ); } return $response; } protected function refreshAccessToken() { $refreshToken = $this->getRefreshToken(); if ($refreshToken !== null) { $oauthClient = new OAuth\Bitrix24(); $response = $oauthClient->getAccessToken( "refresh_token", ["refresh_token" => $this->refreshToken] ); if (!isset($response["error"]) && is_array($response)) { $this->setAccessToken($response["access_token"]); $this->setRefreshToken($response["refresh_token"]); return true; } } return false; } /** * @param $params * @return void */ protected function log($params) { if($this->isOnLog() == false) { return; } $dir = $_SERVER['DOCUMENT_ROOT'].static::LOG_DIR; if(is_dir($dir) || @mkdir($dir, BX_DIR_PERMISSIONS)) { $f = fopen($dir.'/'.static::LOG_PATH, "a+"); fwrite($f, print_r($params, true)); fclose($f); } } protected function isOnLog() { return \Bitrix\Main\Config\Option::get("sale", "log_integration_b24_rest_client", 'N') == 'Y'; } }