аякс диспетчера очереди, класс для шифрования

This commit is contained in:
rsgrinko committed 2022-07-14 00:17:12 +03:00
1 parent 2aab779005
commit 9d7349867e
12 files changed
+507 -158

No files matched your search

+5 -1
View File
@@ -20,8 +20,12 @@
if((time() - (int)$cronMyslo) > 600) {
$runMyslo = true;
Cache::set($cacheId, time());
sendTelegram('cron_myslo if if set (' . $cronMyslo . ')');
}
} else {
$runMyslo = true;
sendTelegram('cron_myslo global else (' . $cronMyslo . ')');
Cache::set($cacheId, time());
}
if($runMyslo === true) {
@@ -2,6 +2,161 @@
namespace Core\ExternalServices;
use Core\CoreException;
class Request
{
private $httpStatus = null;
private $httpCode = null;
private $maxRedirects = 2;
private $requestHeaders = [];
/**
* @var false|string
*/
private $responseBody = null;
private $responseHeader = null;
private $timeout = 5;
/**
* @return string|null
*/
public function getResponseBody(): ?string
{
return $this->responseBody;
}
/**
* @return null
*/
public function getResponseHeader()
{
return $this->responseHeader;
}
private $usertAgent = null;
private $requestType = null;
private $url = null;
private $params = [];
private $referer = '';
private $usePostMethod = false;
public function __construct()
{
}
public function setUrl(?string $url): self
{
$this->url = $url;
return $this;
}
public function post(): self
{
$this->usePostMethod = true;
return $this;
}
public function get(): self
{
$this->usePostMethod = true;
return $this;
}
public function setParams(?array $params = null): self
{
$this->params = $params ?: [];
return $this;
}
public function setHeaders(?array $arHeaders = null): self
{
$this->requestHeaders = $arHeaders ?: [];
return $this;
}
public function setTimeout(int $timeout): self
{
$this->timeout = $timeout;
return $this;
}
public function setMaxRedirects(int $redirects): self
{
$this->maxRedirects = $redirects;
return $this;
}
public function setReferer(?string $referer = null): self
{
$this->referer = $referer ?: '';
return $this;
}
public function setUserAgent(?string $userAgent = null): self
{
$this->userAgent = $userAgent ?: '';
return $this;
}
public function getCode(): ?int
{
return $this->httpCode;
}
public function send()
{
$res = $this->sendRequest();
return $res;
}
/**
* @throws CoreException
*/
private function sendRequest(): ?string
{
if (empty($this->url)) {
throw new CoreException('Адрес запроса не задан');
}
if ($this->usePostMethod === true) {
$ch = curl_init($this->url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->params);
} else {
$ch = curl_init($this->url . '?' . http_build_query($this->params));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_REFERER, $this->referer);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
curl_setopt($ch, CURLOPT_MAXREDIRS, $this->maxRedirects);
curl_setopt($ch, CURLOPT_USERAGENT, $this->usertAgent);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->requestHeaders);
curl_setopt($ch, CURLOPT_HEADER, 1);
$res = curl_exec($ch);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$this->responseHeader = substr($res, 0, $headerSize);
$this->responseBody = substr($res, $headerSize);
$this->httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $res;
}
}
+99
View File
@@ -0,0 +1,99 @@
<?php
namespace Core\Helpers;
class Crypto
{
/**
* @var string|null $key Ключ шифрования
*/
private $key = null;
/**
* @var string $method Метод шифрования
*/
private $method = 'AES-192-CBC';
/**
* @var string $algorithm Алгоритм
*/
private $algorithm = 'sha256';
/**
* Конструктор
*/
public function __construct()
{
}
/**
* Деструктор
*/
public function __destruct()
{
unset($this->key);
unset($this->method);
}
/**
* Генерация ключа шифрования
*
* @return string Ключ
*/
public static function generateKey(): string
{
return bin2hex(openssl_random_pseudo_bytes(40));
}
/**
* @param string $key
*
* @return Crypto
*/
public function setKey(string $key): self
{
$this->key = $key;
return $this;
}
/**
* Шифрование данных
*
* @param string $string Строка
*
* @return string
*/
public function encode(string $string): string
{
$ivlen = openssl_cipher_iv_length($this->method);
$iv = openssl_random_pseudo_bytes($ivlen);
$ciphertext_raw = openssl_encrypt($string, $this->method, $this->key, OPENSSL_RAW_DATA, $iv);
$hmac = hash_hmac($this->algorithm, $ciphertext_raw, $this->key, true);
$ciphertext = base64_encode($iv . $hmac . $ciphertext_raw);
return $ciphertext;
}
/**
* Расшифровка данных
*
* @param string $string Зашифрованная строка
*
* @return string|null
*/
public function decode(string $string): ?string
{
$c = base64_decode($string);
$ivlen = openssl_cipher_iv_length($this->method);
$iv = substr($c, 0, $ivlen);
$hmac = substr($c, $ivlen, $sha2len = 32);
$ciphertext_raw = substr($c, $ivlen + $sha2len);
$plaintext = openssl_decrypt($ciphertext_raw, $this->method, $this->key, OPENSSL_RAW_DATA, $iv);
$calcmac = hash_hmac($this->algorithm, $ciphertext_raw, $this->key, true);
if (hash_equals($hmac, $calcmac)) {
return $plaintext;
}
return null;
}
}
+4 -2
View File
@@ -259,6 +259,8 @@ DELETE from `jokes` WHERE `jokes`.id not in (SELECT id FROM t_temp);'
if($oldLink !== $link) {
sendTelegram('OLD (' .strlen($oldLink).'): ' . $oldLink . PHP_EOL . 'NEW (' .strlen($link).'): ' . $link);
sendTelegram('Обнаружен новый пост');
$full = file_get_contents($link);
preg_match_all('/<div class="h3 lid"><p>(.*?)<\/p><\/div>/us', $full, $lidArticle);
@@ -286,10 +288,10 @@ DELETE from `jokes` WHERE `jokes`.id not in (SELECT id FROM t_temp);'
$post .= PHP_EOL . '<a href="' . $link . '">Подробнее</a>';
$telegram = new \Core\ExternalServices\TelegramSender(TELEGRAM_BOT_TOKEN);
$telegram->setChat(TELEGRAM_NOTIFICATION_CHANNEL);
$telegram->setChat('-1001789206618');
$res = $telegram->sendPhoto($tmpFile, $post);
@unlink($tmpFile);
Cache::set($cacheId, $link);
Cache::set($cacheId, trim($link));
return $res;
} else {
return 'Обновление не требуется';
+1 -1
View File
@@ -160,7 +160,7 @@
*/
public static function test2(...$params): string
{
sleep(rand(10, 60));
sleep(rand(1, 5));
$result = rand(0, 100);
if (($result % 2) == 0) {
throw new CoreException('Сервер обмена временно недоступен');