mirror of
https://github.com/Karaka-Management/oms-OnlineResourceWatcher.git
synced 2026-02-08 20:38:40 +00:00
update
This commit is contained in:
parent
ad6a10b563
commit
30068bf81c
|
|
@ -52,8 +52,20 @@ use phpOMS\Utils\StringUtils;
|
|||
*/
|
||||
final class ApiController extends Controller
|
||||
{
|
||||
/**
|
||||
* Text renderable file formats
|
||||
*
|
||||
* @var string[]
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public const TEXT_RENDERABLE = ['md', 'txt', 'doc', 'docx', 'pdf', 'xls', 'xlsx'];
|
||||
|
||||
/**
|
||||
* Image renderable file formats
|
||||
*
|
||||
* @var string[]
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public const IMG_RENDERABLE = ['png', 'jpg', 'jpeg', 'gif', 'bmp', 'svg'];
|
||||
|
||||
/**
|
||||
|
|
@ -78,7 +90,9 @@ final class ApiController extends Controller
|
|||
|
||||
if ($request->hasData('path')) {
|
||||
// @todo check if user has permission?
|
||||
$this->app->moduleManager->get('Media', 'Api')->apiMediaExport($request, $response, ['guard' => __DIR__ . '/../Files']);
|
||||
$this->app->moduleManager->get('Media', 'Api')
|
||||
->apiMediaExport($request, $response, ['guard' => __DIR__ . '/../Files']);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -220,7 +234,9 @@ final class ApiController extends Controller
|
|||
->where('checkedAt', (new \DateTime('now'))->sub(new \DateInterval('PT12H')), '<')
|
||||
->execute();
|
||||
|
||||
$this->checkResources($request, $resources);
|
||||
$changes = $this->checkResources($request, $resources);
|
||||
$this->informEmail($changes);
|
||||
|
||||
$this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Resources', 'Resources were checked.', null);
|
||||
}
|
||||
|
||||
|
|
@ -246,13 +262,125 @@ final class ApiController extends Controller
|
|||
->where('createdAt', $dateTime, '>=')
|
||||
->execute();
|
||||
|
||||
/*
|
||||
foreach ($reports as $report) {
|
||||
// @todo get templates
|
||||
// @todo get users to inform
|
||||
// @todo inform users
|
||||
$ids = \array_map(
|
||||
function (Report $report) : void {
|
||||
$report->resource;
|
||||
},
|
||||
$reports
|
||||
);
|
||||
|
||||
$resources = ResourceMapper::getAll()
|
||||
->with('owner')
|
||||
->with('owner/l11n')
|
||||
->where('id', $ids, 'IN')
|
||||
->execute();
|
||||
|
||||
$this->informEmail($resources);
|
||||
$this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Inform', 'Users were informed', null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inform users about changed resources via email
|
||||
*
|
||||
* @param Resource[] $resources Changed resources
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function informEmail(array $resources) : void
|
||||
{
|
||||
$handler = $this->app->moduleManager->get('Admin', 'Api')->setUpServerMailHandler();
|
||||
|
||||
/** @var \Model\Setting $emailSettings */
|
||||
$emailSettings = $this->app->appSettings->get(
|
||||
names: SettingsEnum::MAIL_SERVER_ADDR,
|
||||
module: 'OnlineResourceWatcher'
|
||||
);
|
||||
|
||||
/** @var \Model\Setting $templateSettings */
|
||||
$templateSettings = $this->app->appSettings->get(
|
||||
names: OrwSettingsEnum::ORW_CHANGE_MAIL_TEMPLATE,
|
||||
module: 'OnlineResourceWatcher'
|
||||
);
|
||||
|
||||
/** @var \Modules\Messages\Models\Email $baseEmail */
|
||||
$baseEmail = EmailMapper::get()
|
||||
->with('l11n')
|
||||
->where('id', (int) $templateSettings->content)
|
||||
->execute();
|
||||
|
||||
/** @var \Modules\OnlineResourceWatcher\Models\InformBlacklist[] */
|
||||
$blacklist = InformBlacklistMapper::getAll()
|
||||
->execute();
|
||||
|
||||
foreach ($resources as $resource) {
|
||||
$owner = new Inform();
|
||||
$owner->email = $resource->owner->getEmail();
|
||||
$resource->inform[] = $owner;
|
||||
|
||||
foreach ($resource->inform as $inform) {
|
||||
if (empty($inform->email)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($blacklist as $block) {
|
||||
if (\stripos($inform->email, $block->email) !== false) {
|
||||
continue 2;
|
||||
}
|
||||
}
|
||||
|
||||
$mail = clone $baseEmail;
|
||||
$mail->setFrom($emailSettings->content);
|
||||
|
||||
$mailL11n = $baseEmail->getL11nByLanguage($resource->owner->l11n->language);
|
||||
if ($mailL11n->id === 0) {
|
||||
$mailL11n = $baseEmail->getL11nByLanguage($this->app->l11nServer->language);
|
||||
}
|
||||
|
||||
if ($mailL11n->id === 0) {
|
||||
$mailL11n = $baseEmail->getL11nByLanguage('en');
|
||||
}
|
||||
|
||||
$mail->subject = $mailL11n->subject;
|
||||
|
||||
$mail->body = \str_replace(
|
||||
[
|
||||
'{resource.id}',
|
||||
'{email}',
|
||||
'{resource.url}',
|
||||
'{owner_email}',
|
||||
],
|
||||
[
|
||||
$resource->id,
|
||||
$inform->email,
|
||||
$resource->uri,
|
||||
$resource->owner->getEmail(),
|
||||
],
|
||||
$mailL11n->body
|
||||
);
|
||||
$mail->msgHTML($mail->body);
|
||||
|
||||
$mail->bodyAlt = \str_replace(
|
||||
[
|
||||
'{resource.id}',
|
||||
'{email}',
|
||||
'{resource.url}',
|
||||
'{owner_email}',
|
||||
],
|
||||
[
|
||||
$resource->id,
|
||||
$inform->email,
|
||||
$resource->uri,
|
||||
$resource->owner->getEmail(),
|
||||
],
|
||||
$mailL11n->bodyAlt
|
||||
);
|
||||
|
||||
$mail->addTo($inform->email);
|
||||
$handler->send($mail);
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -262,7 +390,7 @@ final class ApiController extends Controller
|
|||
* @param array $resources Resources to check
|
||||
* @param mixed $data Generic data
|
||||
*
|
||||
* @return array
|
||||
* @return Resource[]
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @todo implement iterative approach where you can define a "offset" and "limit" to check only a few resources at a time
|
||||
|
|
@ -310,30 +438,6 @@ final class ApiController extends Controller
|
|||
}
|
||||
}
|
||||
|
||||
$handler = $this->app->moduleManager->get('Admin', 'Api')->setUpServerMailHandler();
|
||||
|
||||
/** @var \Model\Setting $emailSettings */
|
||||
$emailSettings = $this->app->appSettings->get(
|
||||
names: SettingsEnum::MAIL_SERVER_ADDR,
|
||||
module: 'OnlineResourceWatcher'
|
||||
);
|
||||
|
||||
/** @var \Model\Setting $templateSettings */
|
||||
$templateSettings = $this->app->appSettings->get(
|
||||
names: OrwSettingsEnum::ORW_CHANGE_MAIL_TEMPLATE,
|
||||
module: 'OnlineResourceWatcher'
|
||||
);
|
||||
|
||||
/** @var \Modules\Messages\Models\Email $baseEmail */
|
||||
$baseEmail = EmailMapper::get()
|
||||
->with('l11n')
|
||||
->where('id', (int) $templateSettings->content)
|
||||
->execute();
|
||||
|
||||
/** @var \Modules\OnlineResourceWatcher\Models\InformBlacklist[] */
|
||||
$blacklist = InformBlacklistMapper::getAll()
|
||||
->execute();
|
||||
|
||||
// Check downloaded resources
|
||||
$totalCount = \count($toCheck);
|
||||
$maxLoops = (int) \min(60 * 10, $totalCount * 10 / 4); // At most run 600 times or 2.5 times the resource count
|
||||
|
|
@ -593,7 +697,9 @@ final class ApiController extends Controller
|
|||
$resource->lastVersionDate = $report->createdAt;
|
||||
$resource->hash = $md5New;
|
||||
|
||||
$changed[] = $report;
|
||||
$resource->reports[] = $report;
|
||||
|
||||
$changed[] = $resource;
|
||||
|
||||
Directory::copy($path, $basePath . '/' . $resource->id . '/' . $check['timestamp']);
|
||||
|
||||
|
|
@ -615,69 +721,6 @@ final class ApiController extends Controller
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
// @todo move to informUsers function
|
||||
$owner = new Inform();
|
||||
$owner->email = $resource->owner->getEmail();
|
||||
$resource->inform[] = $owner;
|
||||
|
||||
foreach ($resource->inform as $inform) {
|
||||
foreach ($blacklist as $block) {
|
||||
if (\stripos($inform->email, $block->email) !== false) {
|
||||
continue 2;
|
||||
}
|
||||
}
|
||||
|
||||
$mail = clone $baseEmail;
|
||||
$mail->setFrom($emailSettings->content);
|
||||
|
||||
$mailL11n = $baseEmail->getL11nByLanguage($resource->owner->l11n->language);
|
||||
if ($mailL11n->id === 0) {
|
||||
$mailL11n = $baseEmail->getL11nByLanguage($this->app->l11nServer->language);
|
||||
}
|
||||
|
||||
if ($mailL11n->id === 0) {
|
||||
$mailL11n = $baseEmail->getL11nByLanguage('en');
|
||||
}
|
||||
|
||||
$mail->subject = $mailL11n->subject;
|
||||
|
||||
$mail->body = \str_replace(
|
||||
[
|
||||
'{resource.id}',
|
||||
'{email}',
|
||||
'{resource.url}',
|
||||
'{owner_email}',
|
||||
],
|
||||
[
|
||||
$resource->id,
|
||||
$inform->email,
|
||||
$resource->uri,
|
||||
$resource->owner->getEmail(),
|
||||
],
|
||||
$mailL11n->body
|
||||
);
|
||||
$mail->msgHTML($mail->body);
|
||||
|
||||
$mail->bodyAlt = \str_replace(
|
||||
[
|
||||
'{resource.id}',
|
||||
'{email}',
|
||||
'{resource.url}',
|
||||
'{owner_email}',
|
||||
],
|
||||
[
|
||||
$resource->id,
|
||||
$inform->email,
|
||||
$resource->uri,
|
||||
$resource->owner->getEmail(),
|
||||
],
|
||||
$mailL11n->bodyAlt
|
||||
);
|
||||
|
||||
$mail->addTo($inform->email);
|
||||
$handler->send($mail);
|
||||
}
|
||||
}
|
||||
|
||||
$this->createModel($request->header->account, $report, ReportMapper::class, 'report', $request->getOrigin());
|
||||
|
|
@ -694,21 +737,11 @@ final class ApiController extends Controller
|
|||
\usleep(100000); // 100ms delay to give the tools time to download between steps
|
||||
}
|
||||
|
||||
// Make sure that no wkhtmltoimage processes are running
|
||||
$time = \time();
|
||||
if ($time - $minTime < 3) {
|
||||
// The shortest time interval we give the script to download the images
|
||||
\sleep(3);
|
||||
}
|
||||
|
||||
if ($time > $minTime) {
|
||||
// @todo create a separate function which is called async minTime - time seconds after
|
||||
// This solution is just a workaround for small lists which would otherwise be forced to wait at least 60 seconds.
|
||||
if (OperatingSystem::getSystem() === SystemType::LINUX) {
|
||||
SystemUtils::runProc('pkill', '-9 -f wkhtmltoimage', true);
|
||||
} else {
|
||||
SystemUtils::runProc('taskkill', '/F /IM wkhtmltoimage.exe', true);
|
||||
}
|
||||
// Kill running processes in x seconds that shouldn't be running any longer
|
||||
if (OperatingSystem::getSystem() === SystemType::LINUX) {
|
||||
SystemUtils::runProc('sleep', \max(0, $time - $minTime) . ' && pkill -9 -f wkhtmltoimage', true);
|
||||
} else {
|
||||
SystemUtils::runProc('timeout', '/t ' . \max(0, $time - $minTime) . ' > NUL && taskkill /F /IM wkhtmltoimage.exe', true);
|
||||
}
|
||||
|
||||
Directory::delete($basePath . '/temp');
|
||||
|
|
@ -900,9 +933,9 @@ final class ApiController extends Controller
|
|||
return;
|
||||
}
|
||||
|
||||
$resource = $this->createInformFromRequest($request);
|
||||
$this->createModel($request->header->account, $resource, InformMapper::class, 'resource', $request->getOrigin());
|
||||
$this->createStandardCreateResponse($request, $response, $resource);
|
||||
$inform = $this->createInformFromRequest($request);
|
||||
$this->createModel($request->header->account, $inform, InformMapper::class, 'inform', $request->getOrigin());
|
||||
$this->createStandardCreateResponse($request, $response, $inform);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ use Web\Backend\Views\TableView;
|
|||
final class BackendController extends Controller
|
||||
{
|
||||
/**
|
||||
* Routing end-point for application behaviour.
|
||||
* Routing end-point for application behavior.
|
||||
*
|
||||
* @param RequestAbstract $request Request
|
||||
* @param ResponseAbstract $response Response
|
||||
|
|
@ -116,7 +116,7 @@ final class BackendController extends Controller
|
|||
}
|
||||
|
||||
/**
|
||||
* Routing end-point for application behaviour.
|
||||
* Routing end-point for application behavior.
|
||||
*
|
||||
* @param RequestAbstract $request Request
|
||||
* @param ResponseAbstract $response Response
|
||||
|
|
@ -145,7 +145,7 @@ final class BackendController extends Controller
|
|||
}
|
||||
|
||||
/**
|
||||
* Routing end-point for application behaviour.
|
||||
* Routing end-point for application behavior.
|
||||
*
|
||||
* @param RequestAbstract $request Request
|
||||
* @param ResponseAbstract $response Response
|
||||
|
|
@ -162,7 +162,7 @@ final class BackendController extends Controller
|
|||
}
|
||||
|
||||
/**
|
||||
* Routing end-point for application behaviour.
|
||||
* Routing end-point for application behavior.
|
||||
*
|
||||
* @param RequestAbstract $request Request
|
||||
* @param ResponseAbstract $response Response
|
||||
|
|
@ -179,7 +179,7 @@ final class BackendController extends Controller
|
|||
}
|
||||
|
||||
/**
|
||||
* Routing end-point for application behaviour.
|
||||
* Routing end-point for application behavior.
|
||||
*
|
||||
* @param RequestAbstract $request Request
|
||||
* @param ResponseAbstract $response Response
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ namespace Modules\OnlineResourceWatcher\Models;
|
|||
use phpOMS\Stdlib\Base\Enum;
|
||||
|
||||
/**
|
||||
* Permision state enum.
|
||||
* Permission category enum.
|
||||
*
|
||||
* @package Modules\OnlineResourceWatcher\Models
|
||||
* @license OMS License 2.0
|
||||
|
|
|
|||
|
|
@ -70,7 +70,7 @@ use phpOMS\Uri\UriFactory;
|
|||
<div class="col-xs-12">
|
||||
<div class="portlet">
|
||||
<div class="portlet-head"><?= $this->getHtml('Inform', '0', '0'); ?></div>
|
||||
<table class="default">
|
||||
<table class="default sticky">
|
||||
<thead>
|
||||
<tr>
|
||||
<td><?= $this->getHtml('ID', '0', '0'); ?><i class="sort-asc g-icon">expand_less</i><i class="sort-desc g-icon">expand_more</i>
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ $next = $tableView->getNextLink(
|
|||
<?= $tableView->renderTitle(
|
||||
$this->getHtml('Resources')
|
||||
); ?>
|
||||
<a class="button button end-xs save" href="<?= UriFactory::build('{/base}/'); ?>orw/resources/create"><?= $this->getHtml('New', '0', '0'); ?></a>
|
||||
<a class="button end-xs save" href="<?= UriFactory::build('{/base}/'); ?>orw/resources/create"><?= $this->getHtml('New', '0', '0'); ?></a>
|
||||
</div>
|
||||
<div class="slider">
|
||||
<table id="<?= $tableView->id; ?>" class="default sticky">
|
||||
|
|
|
|||
|
|
@ -60,7 +60,7 @@ $reports = $resource->reports;
|
|||
<div class="portlet">
|
||||
<div class="portlet-head"><?= $this->getHtml('History'); ?></div>
|
||||
<div class="slider">
|
||||
<table class="default">
|
||||
<table class="default sticky">
|
||||
<thead>
|
||||
<tr>
|
||||
<td><?= $this->getHtml('Date'); ?>
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user