Server IP : 80.87.202.40 / Your IP : 216.73.216.169 Web Server : Apache System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64 User : bitrix ( 600) PHP Version : 8.2.27 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/bitrix/ext_www/rospirotorg.ru/bitrix/modules/lpost.delivery/lib/ |
Upload File : |
<? namespace Lpost\Delivery; use \Bitrix\Main\Localization\Loc; use \Lpost\Delivery\Base; trait Connector { static $token = 'not authorize'; public function getData($method, $data = [], $requestMethod = false) { if(Settings::getInstance()->isReady() OR !empty($data['secret'])) { $data['method'] = $method; $data['ver'] = 1; $data['json'] = empty($data['json']) ? "{}" : $data['json']; $url = Settings::getInstance()->getServiceUrl(); return $this->request($data, $url, $requestMethod); } else { $this->errors[] = loc::getMessage("LPOST_OPTION_SETTING_ERROR_SECRET_TOKEN"); } return false; } public function getTokenSystem($secret) { //date_default_timezone_set('Europe/Moscow'); $data = $this->getData('Auth', ['secret' => $secret]); if($data != false) { if(!empty($data['errorMessage'])) { $error = static::convertInfo($data['errorMessage']); $this->errors[] = $error; $this->setError("getTokenSystem Error Auth", true, ['error' => $error, 'secret' => $secret]); return false; } else { if(!empty($data['valid_till'])){ $dateTimeServer = new \DateTime("now", new \DateTimeZone(date_default_timezone_get())); $hourOffsetServer = $dateTimeServer->getOffset(); $dateTimeMoscow = new \DateTimeImmutable("now", new \DateTimeZone('Europe/Moscow')); $hourOffsetMoscow = $dateTimeMoscow->getOffset(); $diffTime = $hourOffsetServer - $hourOffsetMoscow; $time = time() + 3600 - $diffTime; } else $time = strtotime($data['valid_till']); Settings::getInstance()->setOption('lpost_token_time', $time); Settings::getInstance()->setOption('lpost_token', $data['token']); return $data['token']; } } } public function getToken($secret = false) { $options = Settings::getInstance()->getOptions(); //date_default_timezone_set('Europe/Moscow'); $dateTimeServer = new \DateTime("now", new \DateTimeZone(date_default_timezone_get())); $hourOffsetServer = $dateTimeServer->getOffset(); $dateTimeMoscow = new \DateTimeImmutable("now", new \DateTimeZone('Europe/Moscow')); $hourOffsetMoscow = $dateTimeMoscow->getOffset(); $diffTime = $hourOffsetServer - $hourOffsetMoscow; $timeNow = time() + 60 - $diffTime; if( ($secret && $secret != Settings::getInstance()->getSecretKey()) || empty($options['lpost_token']) || $options['lpost_token_time'] <= $timeNow ) { if(!$secret) $secret = Settings::getInstance()->getSecretKey(); $data = $this->getData('Auth', ['secret' => $secret]); if(empty($data['errorMessage'])) { static::$token = $data['token']; Settings::getInstance()->setOption('lpost_token_time', (!empty($data['valid_till']) ? strtotime($data['valid_till']) : time() + 3600)); Settings::getInstance()->setOption('lpost_token', static::$token); } else { $error = static::convertInfo($data['errorMessage']); $this->errors[] = $error; $this->setError("getToken Error Auth",true,[ 'error' => $error, 'lpost_token_time' => date('d.m.Y H:i:s', $options['lpost_token_time']), 'timeNow' => date('d.m.Y H:i:s', $timeNow), 'secret' => $secret, ] ); static::$token = false; } return static::$token; } return $options['lpost_token']; } public function getErrors() { return empty($this->errors) ? [] : $this->errors; } public function request($data, $url, $method, $format = "json") { //$start = microtime(true); $ch = curl_init(); if($method == "PUT") { curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); } else if($method == "POST") { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } else { $url .= "?" . http_build_query($data); } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ch, CURLOPT_TIMEOUT, 60); $output = curl_exec($ch); //$end = microtime(true); //$time = $end - $start; //$this->setDebug('request()', "$url $method, time: " . $time . " seconds"); //$this->setDebug('request() output', $output); if(curl_error($ch)) { $this->setError( "Error request service", true, [ 'requestData' => $data, 'url' => $url, 'error' => curl_error($ch) ] ); $this->errors[] = static::convertInfo(curl_error($ch)); } else { curl_close($ch); switch($format) { case "json": return json_decode($output, true); case "xml": return new \SimpleXMLElement($output); } } curl_close($ch); } } ?>