закоммитил правки
This commit is contained in:
1 parent
4387d38dce
commit
0ad5080edc
14 files changed
+2254
-12
No files matched your search
@@ -0,0 +1,263 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2022 Roman Grinko <rsgrinko@gmail.com>
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Класс для управления удаленными хостами
|
||||
*
|
||||
* @version 1.0.1
|
||||
* @author Roman Grinko <rsgrinko@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Core\ExternalServices;
|
||||
|
||||
use Core\CoreException;
|
||||
use Core\Models\DB;
|
||||
|
||||
class RemoteHosts
|
||||
{
|
||||
private const TABLE = 'remote_hosts';
|
||||
|
||||
private ?int $selectedHostId;
|
||||
|
||||
private array $selectedHostParams = [];
|
||||
|
||||
/** @var string $method Метод */
|
||||
private string $method;
|
||||
|
||||
/** @var array $params Параметры */
|
||||
private array $params = [];
|
||||
|
||||
/** @var int $httpCode Код ответа */
|
||||
private int $httpCode = 0;
|
||||
|
||||
/** @var array $response Ответ */
|
||||
private array $response = [];
|
||||
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws CoreException
|
||||
*/
|
||||
public static function getHosts(string $limit = '10', string $sort = 'ASC'): array
|
||||
{
|
||||
/** @var DB $DB Объект БД */
|
||||
$DB = DB::getInstance();
|
||||
$res = $DB->query('SELECT * FROM `' . self::TABLE . '` ORDER BY `id` ' . $sort . ' LIMIT ' . $limit);
|
||||
return $res ?? [];
|
||||
}
|
||||
|
||||
public static function getAllCount(): int
|
||||
{
|
||||
/** @var DB $DB Объект БД */
|
||||
$DB = DB::getInstance();
|
||||
$res = $DB->query('SELECT count(id) as count FROM ' . self::TABLE);
|
||||
if ($res !== null) {
|
||||
return (int)$res[0]['count'];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public function getHostData(): array
|
||||
{
|
||||
/** @var DB $DB Объект БД */
|
||||
$DB = DB::getInstance();
|
||||
$res = $DB->getItem(self::TABLE, ['id' => $this->selectedHostId]);
|
||||
if ($res !== null) {
|
||||
return $res;
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
public function selectHost(int $id): self
|
||||
{
|
||||
/** @var DB $DB Объект БД */
|
||||
$DB = DB::getInstance();
|
||||
$this->selectedHostParams = $DB->getItem(self::TABLE, ['id' => $id]);
|
||||
$this->selectedHostId = $id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function sendRequest()
|
||||
{
|
||||
$queryFields = array_merge(['cmd' => $this->method], $this->params);
|
||||
|
||||
$ch = curl_init($this->selectedHostParams['url']);
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $queryFields);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HEADER, false);
|
||||
$res = curl_exec($ch);
|
||||
$this->httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
$result = json_decode($res, true);
|
||||
if (!empty($result)) {
|
||||
$this->response = $result;
|
||||
} else {
|
||||
$this->response = [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Установка метода
|
||||
*
|
||||
* @param string $method Метод
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function setMethod(string $method): self
|
||||
{
|
||||
$this->method = $method;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Установка параметров
|
||||
*
|
||||
* @param array $params Параметры
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function setParams(array $params): self
|
||||
{
|
||||
$this->params = $params;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Выполнить
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function execute(): self
|
||||
{
|
||||
$this->sendRequest();
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить ответ
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getResponse(): array
|
||||
{
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить результат выполнения
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getResponseData()
|
||||
{
|
||||
return $this->response['data'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить код ответа
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getHttpCode(): int
|
||||
{
|
||||
return $this->httpCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Проверка площадки на доступность
|
||||
*/
|
||||
public function isOnline(): bool
|
||||
{
|
||||
$this->method = 'ping';
|
||||
$this->execute();
|
||||
return $this->getResponseData() === 'pong';
|
||||
}
|
||||
|
||||
/**
|
||||
* Выполнение произвольного кода
|
||||
*
|
||||
* @param string $code Код
|
||||
*/
|
||||
public function eval(string $code)
|
||||
{
|
||||
$this->method = 'eval';
|
||||
$this->params = [
|
||||
'evalString' => base64_encode($code),
|
||||
];
|
||||
$this->execute();
|
||||
return $this->getResponseData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Выполнение произвольного кода
|
||||
*/
|
||||
public function ping()
|
||||
{
|
||||
$this->method = 'ping';
|
||||
$this->execute();
|
||||
return $this->getResponseData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Получение информации о площадке
|
||||
*/
|
||||
public function getHostInfo()
|
||||
{
|
||||
$this->method = 'getHostInfo';
|
||||
$this->execute();
|
||||
return $this->getResponseData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Получение hostname площадки
|
||||
*/
|
||||
public function getHostname()
|
||||
{
|
||||
$this->method = 'getHostInfo';
|
||||
$this->execute();
|
||||
return $this->getResponseData()['hostname'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Получение uptime площадки
|
||||
*/
|
||||
public function getUptime()
|
||||
{
|
||||
$this->method = 'getHostInfo';
|
||||
$this->execute();
|
||||
return $this->getResponseData()['uptime'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Получение uname площадки
|
||||
*/
|
||||
public function getUname()
|
||||
{
|
||||
$this->method = 'getHostInfo';
|
||||
$this->execute();
|
||||
return $this->getResponseData()['uname'];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -683,4 +683,60 @@
|
||||
|
||||
echo $twig->render('index.tpl', ['name' => 'Roman']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Сравнение текстов
|
||||
*
|
||||
* @param array $old Старый текст
|
||||
* @param array $new Новый текст
|
||||
*
|
||||
* @return array|array[]
|
||||
*/
|
||||
public static function diffText(array $old, array $new): array
|
||||
{
|
||||
$matrix = [];
|
||||
$maxlen = 0;
|
||||
foreach ($old as $oindex => $ovalue) {
|
||||
$nkeys = array_keys($new, $ovalue);
|
||||
foreach ($nkeys as $nindex) {
|
||||
$matrix[$oindex][$nindex] = isset($matrix[$oindex - 1][$nindex - 1]) ? $matrix[$oindex - 1][$nindex - 1] + 1 : 1;
|
||||
if ($matrix[$oindex][$nindex] > $maxlen) {
|
||||
$maxlen = $matrix[$oindex][$nindex];
|
||||
$omax = $oindex + 1 - $maxlen;
|
||||
$nmax = $nindex + 1 - $maxlen;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($maxlen === 0) {
|
||||
return [['d' => $old, 'i' => $new]];
|
||||
}
|
||||
return array_merge(
|
||||
self::diffText(array_slice($old, 0, $omax), array_slice($new, 0, $nmax)),
|
||||
array_slice($new, $nmax, $maxlen),
|
||||
self::diffText(array_slice($old, $omax + $maxlen), array_slice($new, $nmax + $maxlen))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Сравнение текстов и отдача HTML результата
|
||||
*
|
||||
* @param string $old Старый текст
|
||||
* @param string $new Новый текст
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function htmlDiffText(string $old, string $new): string
|
||||
{
|
||||
$result = '';
|
||||
$diff = self::diffText(preg_split('/[\s]+/', $old), preg_split('/[\s]+/', $new));
|
||||
foreach ($diff as $k) {
|
||||
if (is_array($k)) {
|
||||
$result .= (!empty($k['d']) ? '<del style="background: red;color: white;">' . implode(' ', $k['d']) . '</del> ' : '')
|
||||
. (!empty($k['i']) ? '<ins style="background: #009d22;color: white;">' . implode(' ', $k['i']) . '</ins> ' : '');
|
||||
} else {
|
||||
$result .= $k . ' ';
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -193,13 +193,15 @@
|
||||
if($i < 1 ) {
|
||||
throw new CoreException('Новых шуток не найдено');
|
||||
}
|
||||
return 'Добавлено ' . $i . ' шуток из ' . count($arJokes);
|
||||
|
||||
return ['method' => __FUNCTION__, 'status' => 'OK', 'message' => 'Добавлено ' . $i . ' шуток из ' . count($arJokes)];
|
||||
}
|
||||
|
||||
public static function anekdot2()
|
||||
{
|
||||
$result = 'Ничего нового не добавлено';
|
||||
/** @var DB $DB */
|
||||
//$result = 'Ничего нового не добавлено';
|
||||
$result = null;
|
||||
/** @var DB $DB */
|
||||
$DB = DB::getInstance();
|
||||
|
||||
/**
|
||||
@@ -236,7 +238,10 @@
|
||||
$result = 'Добавлен новый элемент с ID ' . $itemId;
|
||||
}
|
||||
|
||||
return $result;
|
||||
if (empty($result)) {
|
||||
throw new CoreException('Ничего нового не добавлено');
|
||||
}
|
||||
return ['method' => __FUNCTION__, 'status' => 'OK', 'message' => $result];
|
||||
}
|
||||
|
||||
|
||||
@@ -335,4 +340,10 @@ DELETE from `jokes` WHERE `jokes`.id not in (SELECT id FROM t_temp);'
|
||||
return 'Обновление не требуется';
|
||||
}
|
||||
}
|
||||
|
||||
public static function test(string $cmd, array $params = [])
|
||||
{
|
||||
$res = file_get_contents('https://dev.it-stories.ru/test.php?cmd=' . $cmd . '&' . http_build_query($params));
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
@@ -80,7 +80,7 @@
|
||||
/**
|
||||
* Лимит количества запущенных воркеров
|
||||
*/
|
||||
const WORKERS_LIMIT = 5;
|
||||
const WORKERS_LIMIT = 2;
|
||||
|
||||
/**
|
||||
* @var DB|null $DB Объект базы
|
||||
@@ -349,7 +349,7 @@
|
||||
}
|
||||
return;
|
||||
}
|
||||
$this->run(true);
|
||||
//$this->run(true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user