use core log table instead of system file
This commit is contained in:
parent
b1e0ecbc7f
commit
56552d12d3
2 changed files with 93 additions and 49 deletions
|
@ -78,8 +78,13 @@ class Config extends Process
|
||||||
|
|
||||||
if (!empty($_POST['send_report'])) {
|
if (!empty($_POST['send_report'])) {
|
||||||
Utils::sendReport(true);
|
Utils::sendReport(true);
|
||||||
|
$error = Utils::getError();
|
||||||
|
if (!empty($error)) {
|
||||||
|
Notices::AddWarningNotice($error);
|
||||||
|
} else {
|
||||||
Notices::AddSuccessNotice(__('Report sent.'));
|
Notices::AddSuccessNotice(__('Report sent.'));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
dcCore::app()->admin->url->redirect('admin.plugins', ['module' => My::id(), 'conf' => '1']);
|
dcCore::app()->admin->url->redirect('admin.plugins', ['module' => My::id(), 'conf' => '1']);
|
||||||
|
|
||||||
|
|
129
src/Utils.php
129
src/Utils.php
|
@ -15,9 +15,11 @@ declare(strict_types=1);
|
||||||
namespace Dotclear\Plugin\DotclearWatch;
|
namespace Dotclear\Plugin\DotclearWatch;
|
||||||
|
|
||||||
use dcCore;
|
use dcCore;
|
||||||
|
use dcLog;
|
||||||
use dcModuleDefine;
|
use dcModuleDefine;
|
||||||
use dcThemes;
|
use dcThemes;
|
||||||
use Dotclear\Helper\Crypt;
|
use Dotclear\Helper\Crypt;
|
||||||
|
use Dotclear\Helper\Date;
|
||||||
use Dotclear\Helper\File\Files;
|
use Dotclear\Helper\File\Files;
|
||||||
use Dotclear\Helper\File\Path;
|
use Dotclear\Helper\File\Path;
|
||||||
use Dotclear\Helper\Network\HttpClient;
|
use Dotclear\Helper\Network\HttpClient;
|
||||||
|
@ -138,6 +140,18 @@ class Utils
|
||||||
return self::check() ? self::uid() : '';
|
return self::check() ? self::uid() : '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get request error.
|
||||||
|
*/
|
||||||
|
public static function getError(): string
|
||||||
|
{
|
||||||
|
$rs = dcCore::app()->log->getLogs([
|
||||||
|
'log_table' => My::id() . '_error',
|
||||||
|
]);
|
||||||
|
|
||||||
|
return $rs->isEmpty() || !is_string($rs->f('log_msg')) ? '' : $rs->f('log_msg');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clear cache directory.
|
* Clear cache directory.
|
||||||
*/
|
*/
|
||||||
|
@ -162,31 +176,46 @@ class Utils
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$file = self::file();
|
if (!$force && !self::expired()) {
|
||||||
|
|
||||||
if (!$force && !self::expired($file)) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$contents = self::contents();
|
$contents = self::contents();
|
||||||
|
|
||||||
self::write($file, $contents);
|
self::write($contents);
|
||||||
|
|
||||||
|
$status = 500;
|
||||||
|
$response = '';
|
||||||
|
$url = sprintf(self::url(), 'report');
|
||||||
|
$path = '';
|
||||||
|
if ($client = HttpClient::initClient($url, $path)) {
|
||||||
try {
|
try {
|
||||||
$rsp = HttpClient::quickPost(sprintf(self::url(), 'report'), ['key' => self::key(), 'report' => $contents]);
|
$client->setUserAgent('Dotclear.watch ' . My::id() . '/' . self::DISTANT_API_VERSION);
|
||||||
if ($rsp !== 'ok') {
|
$client->useGzip(false);
|
||||||
throw new Exception('bad API response');
|
$client->setPersistReferers(false);
|
||||||
|
$client->post($path, ['key' => self::key(), 'report' => $contents]);
|
||||||
|
|
||||||
|
$status = $client->getStatus();
|
||||||
|
$response = $client->getContent();
|
||||||
|
unset($client);
|
||||||
|
if ($status != 202) {
|
||||||
|
self::error((string) '(' . $status . ') ' . $response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
if ($force) {
|
unset($client);
|
||||||
dcCore::app()->error->add(__('Dotclear.watch report failed'));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($force) {
|
||||||
|
self::error('Dotclear.watch report failed');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function check(): bool
|
private static function check(): bool
|
||||||
{
|
{
|
||||||
return defined('DC_CRYPT_ALGO') && defined('DC_TPL_CACHE') && is_dir(DC_TPL_CACHE) && is_writable(DC_TPL_CACHE);
|
return defined('DC_CRYPT_ALGO');
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function key(): string
|
private static function key(): string
|
||||||
|
@ -212,61 +241,71 @@ class Utils
|
||||||
return md5(self::uid() . dcCore::app()->blog->uid);
|
return md5(self::uid() . dcCore::app()->blog->uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function url()
|
private static function url(): string
|
||||||
{
|
{
|
||||||
$api_url = My::settings()->getGlobal('distant_api_url');
|
$api_url = My::settings()->getGlobal('distant_api_url');
|
||||||
|
|
||||||
return (is_string($api_url) ? $api_url : self::DISTANT_API_URL) . '/' . self::DISTANT_API_VERSION . '/%s/' . self::uid();
|
return (is_string($api_url) ? $api_url : self::DISTANT_API_URL) . '/' . self::DISTANT_API_VERSION . '/%s/' . self::uid();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function file(): string
|
|
||||||
{
|
|
||||||
$file = self::buid();
|
|
||||||
|
|
||||||
return sprintf(
|
|
||||||
'%s/%s/%s/%s/%s.json',
|
|
||||||
(string) Path::real(DC_TPL_CACHE),
|
|
||||||
My::id(),
|
|
||||||
substr($file, 0, 2),
|
|
||||||
substr($file, 2, 2),
|
|
||||||
$file
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static function clear(): void
|
private static function clear(): void
|
||||||
{
|
{
|
||||||
$path = (string) Path::real(DC_TPL_CACHE) . DIRECTORY_SEPARATOR . My::id();
|
$rs = dcCore::app()->log->getLogs([
|
||||||
if (is_dir($path)) {
|
'log_table' => [
|
||||||
Files::delTree($path);
|
My::id() . '_report',
|
||||||
}
|
My::id() . '_error',
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
if ($rs->isEmpty()) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function write(string $file, string $contents): void
|
$logs = [];
|
||||||
|
while($rs->fetch()) {
|
||||||
|
$logs[] = (int )$rs->f('log_id');
|
||||||
|
}
|
||||||
|
dcCore::app()->log->delLogs($logs);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function error(string $message): void
|
||||||
{
|
{
|
||||||
$dir = dirname($file);
|
self::clear();
|
||||||
if (!is_dir($dir)) {
|
|
||||||
Files::makeDir($dir, true);
|
$cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . dcLog::LOG_TABLE_NAME);
|
||||||
}
|
$cur->setField('log_table', My::id() . '_error');
|
||||||
file_put_contents($file, $contents);
|
$cur->setField('log_msg', $message);
|
||||||
|
|
||||||
|
dcCore::app()->log->addLog($cur);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function read(string $file): string
|
private static function write(string $contents): void
|
||||||
{
|
{
|
||||||
return is_file($file) && is_readable($file) ? (string) file_get_contents($file) : '';
|
self::clear();
|
||||||
|
|
||||||
|
$cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . dcLog::LOG_TABLE_NAME);
|
||||||
|
$cur->setField('log_table', My::id() . '_report');
|
||||||
|
$cur->setField('log_msg', $contents);
|
||||||
|
|
||||||
|
dcCore::app()->log->addLog($cur);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function expired(string $file): bool
|
private static function read(): string
|
||||||
{
|
{
|
||||||
if (!is_file($file) || !is_readable($file) || ($time = filemtime($file)) === false) {
|
$rs = dcCore::app()->log->getLogs([
|
||||||
return true;
|
'log_table' => My::id() . '_report'
|
||||||
|
]);
|
||||||
|
|
||||||
|
return $rs->isEmpty() || !is_string($rs->f('log_msg')) ? '' : $rs->f('log_msg');
|
||||||
}
|
}
|
||||||
|
|
||||||
$time = date('U', $time);
|
private static function expired(): bool
|
||||||
if (!is_numeric($time) || (int) $time + self::EXPIRED_DELAY < time()) {
|
{
|
||||||
return true;
|
$rs = dcCore::app()->log->getLogs([
|
||||||
}
|
'log_table' => My::id() . '_report'
|
||||||
|
]);
|
||||||
|
|
||||||
return false;
|
return $rs->isEmpty() || !is_string($rs->f('log_dt')) || (int) Date::str('%s',$rs->f('log_dt')) + self::EXPIRED_DELAY < time();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function contents(): string
|
private static function contents(): string
|
||||||
|
|
Loading…
Reference in a new issue