Initial commit
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\AdminNotifications\Jobs;
|
||||
|
||||
use Espo\Core\Job\JobDataLess;
|
||||
use Espo\Core\Name\Field;
|
||||
use Espo\Core\Utils\Config;
|
||||
use Espo\Core\Utils\Config\ConfigWriter;
|
||||
use Espo\Entities\Extension;
|
||||
use Espo\ORM\EntityManager;
|
||||
use Espo\ORM\Name\Attribute;
|
||||
use Espo\Tools\AdminNotifications\LatestReleaseDataRequester;
|
||||
|
||||
/**
|
||||
* Checking for new extension versions.
|
||||
*/
|
||||
class CheckNewExtensionVersion implements JobDataLess
|
||||
{
|
||||
public function __construct(
|
||||
private Config $config,
|
||||
private ConfigWriter $configWriter,
|
||||
private EntityManager $entityManager,
|
||||
private LatestReleaseDataRequester $requester
|
||||
) {}
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
if (
|
||||
!$this->config->get('adminNotifications') ||
|
||||
!$this->config->get('adminNotificationsNewExtensionVersion')
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
$query = $this->entityManager
|
||||
->getQueryBuilder()
|
||||
->select()
|
||||
->from(Extension::ENTITY_TYPE)
|
||||
->select([Attribute::ID, Field::NAME, 'version', 'checkVersionUrl'])
|
||||
->where([
|
||||
Attribute::DELETED => false,
|
||||
'isInstalled' => true,
|
||||
])
|
||||
->order([Field::CREATED_AT])
|
||||
->build();
|
||||
|
||||
$sth = $this->entityManager->getQueryExecutor()->execute($query);
|
||||
|
||||
$latestReleases = [];
|
||||
|
||||
while ($row = $sth->fetch()) {
|
||||
$url = !empty($row['checkVersionUrl']) ? $row['checkVersionUrl'] : null;
|
||||
|
||||
$extensionName = $row['name'];
|
||||
|
||||
$latestRelease = $this->requester->request($url, [
|
||||
'name' => $extensionName,
|
||||
]);
|
||||
|
||||
if (!empty($latestRelease) && !isset($latestRelease['error'])) {
|
||||
$latestReleases[$extensionName] = $latestRelease;
|
||||
}
|
||||
}
|
||||
|
||||
$latestExtensionVersions = $this->config->get('latestExtensionVersions', []);
|
||||
|
||||
$save = false;
|
||||
|
||||
foreach ($latestReleases as $extensionName => $extensionData) {
|
||||
if (empty($latestExtensionVersions[$extensionName])) {
|
||||
$latestExtensionVersions[$extensionName] = $extensionData['version'];
|
||||
$save = true;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($latestExtensionVersions[$extensionName] != $extensionData['version']) {
|
||||
$latestExtensionVersions[$extensionName] = $extensionData['version'];
|
||||
|
||||
/*if (!empty($extensionData['notes'])) {
|
||||
//todo: create notification
|
||||
}*/
|
||||
|
||||
$save = true;
|
||||
|
||||
//continue;
|
||||
}
|
||||
|
||||
/*if (!empty($extensionData['notes'])) {
|
||||
//todo: find and modify notification
|
||||
}*/
|
||||
}
|
||||
|
||||
if ($save) {
|
||||
$this->configWriter->set('latestExtensionVersions', $latestExtensionVersions);
|
||||
|
||||
$this->configWriter->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\AdminNotifications\Jobs;
|
||||
|
||||
use Espo\Core\Job\JobDataLess;
|
||||
use Espo\Core\Utils\Config;
|
||||
use Espo\Core\Utils\Config\ConfigWriter;
|
||||
use Espo\Tools\AdminNotifications\LatestReleaseDataRequester;
|
||||
|
||||
/**
|
||||
* Checking for a new EspoCRM version.
|
||||
*/
|
||||
class CheckNewVersion implements JobDataLess
|
||||
{
|
||||
public function __construct(
|
||||
private Config $config,
|
||||
private ConfigWriter $configWriter,
|
||||
private LatestReleaseDataRequester $requester
|
||||
) {}
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
if (
|
||||
!$this->config->get('adminNotifications') ||
|
||||
!$this->config->get('adminNotificationsNewVersion')
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
$latestRelease = $this->requester->request();
|
||||
|
||||
if ($latestRelease === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (empty($latestRelease['version'])) {
|
||||
// @todo Check the logic. WTF?
|
||||
$this->configWriter->set('latestVersion', $latestRelease['version']);
|
||||
|
||||
$this->configWriter->save();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->config->get('latestVersion') != $latestRelease['version']) {
|
||||
$this->configWriter->set('latestVersion', $latestRelease['version']);
|
||||
|
||||
/*if (!empty($latestRelease['notes'])) {
|
||||
// @todo Create a notification.
|
||||
}*/
|
||||
|
||||
$this->configWriter->save();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/*if (!empty($latestRelease['notes'])) {
|
||||
// @todo Find and modify notification.
|
||||
}*/
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\AdminNotifications;
|
||||
|
||||
class LatestReleaseDataRequester
|
||||
{
|
||||
/**
|
||||
* @param array<string, mixed> $requestData
|
||||
* @return ?array<int|string, mixed>
|
||||
*/
|
||||
public function request(
|
||||
?string $url = null,
|
||||
array $requestData = [],
|
||||
string $urlPath = 'release/latest'
|
||||
): ?array {
|
||||
|
||||
if (!function_exists('curl_version')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$ch = curl_init();
|
||||
|
||||
$requestUrl = $url ? trim($url) : base64_decode('aHR0cHM6Ly9zLmVzcG9jcm0uY29tLw==');
|
||||
$requestUrl = str_ends_with($requestUrl, '/') ? $requestUrl : $requestUrl . '/';
|
||||
|
||||
$requestUrl .= empty($requestData) ?
|
||||
$urlPath . '/' :
|
||||
$urlPath . '/?' . http_build_query($requestData);
|
||||
|
||||
curl_setopt($ch, CURLOPT_URL, $requestUrl);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
|
||||
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS | CURLPROTO_HTTP);
|
||||
|
||||
/** @var string|false $result */
|
||||
$result = curl_exec($ch);
|
||||
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
|
||||
curl_close($ch);
|
||||
|
||||
if ($result === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($httpCode !== 200) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$data = json_decode($result, true);
|
||||
|
||||
if (!is_array($data)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
278
application/Espo/Tools/AdminNotifications/Manager.php
Normal file
278
application/Espo/Tools/AdminNotifications/Manager.php
Normal file
@@ -0,0 +1,278 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\AdminNotifications;
|
||||
|
||||
use Espo\Core\Name\Field;
|
||||
use Espo\Core\Utils\Config;
|
||||
use Espo\Core\Utils\Language;
|
||||
use Espo\Core\Utils\ScheduledJob;
|
||||
use Espo\Core\Utils\Util;
|
||||
use Espo\Entities\Extension;
|
||||
use Espo\ORM\EntityManager;
|
||||
|
||||
/**
|
||||
* Notifications on the admin panel.
|
||||
*/
|
||||
class Manager
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManager $entityManager,
|
||||
private Config $config,
|
||||
private Language $language,
|
||||
private ScheduledJob $scheduledJob,
|
||||
private Config\SystemConfig $systemConfig,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return array<int, array{id: string, type: string, message: string}>
|
||||
*/
|
||||
public function getNotificationList(): array
|
||||
{
|
||||
$notificationList = [];
|
||||
|
||||
if (!$this->config->get('adminNotifications')) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (!$this->systemConfig->useCache()) {
|
||||
$notificationList[] = [
|
||||
'id' => 'cacheIsDisabled',
|
||||
'type' => 'cacheIsDisabled',
|
||||
'message' => $this->language->translateLabel('cacheIsDisabled', 'messages', 'Admin'),
|
||||
];
|
||||
}
|
||||
|
||||
if ($this->config->get('adminNotificationsCronIsNotConfigured')) {
|
||||
if ($this->config->get('cronDisabled')) {
|
||||
$notificationList[] = [
|
||||
'id' => 'cronIsDisabled',
|
||||
'type' => 'cronIsDisabled',
|
||||
'message' => $this->language->translateLabel('cronIsDisabled', 'messages', 'Admin'),
|
||||
];
|
||||
}
|
||||
|
||||
if (!$this->isCronConfigured()) {
|
||||
$notificationList[] = [
|
||||
'id' => 'cronIsNotConfigured',
|
||||
'type' => 'cronIsNotConfigured',
|
||||
'message' => $this->language->translateLabel('cronIsNotConfigured', 'messages', 'Admin'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->config->get('adminNotificationsNewVersion')) {
|
||||
$instanceNeedingUpgrade = $this->getInstanceNeedingUpgrade();
|
||||
|
||||
if (!empty($instanceNeedingUpgrade)) {
|
||||
$message = $this->language->translateLabel('newVersionIsAvailable', 'messages', 'Admin');
|
||||
|
||||
$notificationList[] = [
|
||||
'id' => 'newVersionIsAvailable',
|
||||
'type' => 'newVersionIsAvailable',
|
||||
'message' => $this->prepareMessage($message, $instanceNeedingUpgrade),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->config->get('adminNotificationsNewExtensionVersion')) {
|
||||
$extensionsNeedingUpgrade = $this->getExtensionsNeedingUpgrade();
|
||||
|
||||
foreach ($extensionsNeedingUpgrade as $extensionName => $extensionDetails) {
|
||||
$label = 'new' . Util::toCamelCase($extensionName, ' ', true) . 'VersionIsAvailable';
|
||||
|
||||
$message = $this->language->get(['Admin', 'messages', $label]);
|
||||
|
||||
if (!$message) {
|
||||
$message = $this->language
|
||||
->translate('newExtensionVersionIsAvailable', 'messages', 'Admin');
|
||||
}
|
||||
|
||||
$notificationList[] = [
|
||||
'id' => 'newExtensionVersionIsAvailable' . Util::toCamelCase($extensionName, ' ', true),
|
||||
'type' => 'newExtensionVersionIsAvailable',
|
||||
'message' => $this->prepareMessage($message, $extensionDetails)
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
if (!$this->config->get('adminNotificationsExtensionLicenseDisabled')) {
|
||||
$notificationList = array_merge(
|
||||
$notificationList,
|
||||
$this->getExtensionLicenseNotificationList()
|
||||
);
|
||||
}
|
||||
|
||||
return $notificationList;
|
||||
}
|
||||
|
||||
private function isCronConfigured(): bool
|
||||
{
|
||||
return $this->scheduledJob->isCronConfigured();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ?array{currentVersion:string,latestVersion:string}
|
||||
*/
|
||||
private function getInstanceNeedingUpgrade(): ?array
|
||||
{
|
||||
$latestVersion = $this->config->get('latestVersion');
|
||||
|
||||
if (!isset($latestVersion)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$currentVersion = $this->systemConfig->getVersion();
|
||||
|
||||
if ($currentVersion === 'dev') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (version_compare($latestVersion, $currentVersion, '>')) {
|
||||
return [
|
||||
'currentVersion' => $currentVersion,
|
||||
'latestVersion' => $latestVersion,
|
||||
];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return array<string, array{currentVersion: string, latestVersion: string, extensionName: string}>
|
||||
*/
|
||||
private function getExtensionsNeedingUpgrade(): array
|
||||
{
|
||||
$extensions = [];
|
||||
|
||||
$latestExtensionVersions = $this->config->get('latestExtensionVersions');
|
||||
|
||||
if (empty($latestExtensionVersions) || !is_array($latestExtensionVersions)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
foreach ($latestExtensionVersions as $extensionName => $extensionLatestVersion) {
|
||||
$currentVersion = $this->getExtensionLatestInstalledVersion($extensionName);
|
||||
|
||||
if (isset($currentVersion) && version_compare($extensionLatestVersion, $currentVersion, '>')) {
|
||||
$extensions[$extensionName] = [
|
||||
'currentVersion' => $currentVersion,
|
||||
'latestVersion' => $extensionLatestVersion,
|
||||
'extensionName' => $extensionName,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $extensions;
|
||||
}
|
||||
|
||||
private function getExtensionLatestInstalledVersion(string $extensionName): ?string
|
||||
{
|
||||
$extension = $this->entityManager
|
||||
->getRDBRepository(Extension::ENTITY_TYPE)
|
||||
->select(['version'])
|
||||
->where([
|
||||
'name' => $extensionName,
|
||||
'isInstalled' => true,
|
||||
])
|
||||
->order(Field::CREATED_AT, true)
|
||||
->findOne();
|
||||
|
||||
if (!$extension) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $extension->get('version');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, string> $data
|
||||
*/
|
||||
private function prepareMessage(string $message, array $data = []): string
|
||||
{
|
||||
foreach ($data as $name => $value) {
|
||||
$message = str_replace('{'.$name.'}', $value, $message);
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array{id: string, type: string, message: string}>
|
||||
*/
|
||||
private function getExtensionLicenseNotificationList(): array
|
||||
{
|
||||
$extensionList = $this->entityManager
|
||||
->getRDBRepositoryByClass(Extension::class)
|
||||
->where([
|
||||
'licenseStatus' => [
|
||||
Extension::LICENSE_STATUS_INVALID,
|
||||
Extension::LICENSE_STATUS_EXPIRED,
|
||||
Extension::LICENSE_STATUS_SOFT_EXPIRED,
|
||||
],
|
||||
])
|
||||
->find();
|
||||
|
||||
$list = [];
|
||||
|
||||
foreach ($extensionList as $extension) {
|
||||
$message =
|
||||
$extension->getLicenseStatusMessage() ??
|
||||
$this->getExtensionLicenseMessageLabel($extension);
|
||||
|
||||
if (!$message) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$message = $this->language->translateLabel($message, 'messages');
|
||||
|
||||
$name = $extension->getName();
|
||||
|
||||
$list[] = [
|
||||
'id' => 'newExtensionVersionIsAvailable' . Util::toCamelCase($name, ' ', true),
|
||||
'type' => 'newExtensionVersionIsAvailable',
|
||||
'message' => $this->prepareMessage($message, ['name' => $name]),
|
||||
];
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
private function getExtensionLicenseMessageLabel(Extension $extension): ?string
|
||||
{
|
||||
$status = $extension->getLicenseStatus();
|
||||
|
||||
if (!$status) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return 'extensionLicense' . ucfirst(Util::hyphenToCamelCase($status));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user