mirror of
https://github.com/bdnugget/magento2.git
synced 2026-11-07 09:17:35 +00:00
283 lines
7.5 KiB
PHP
283 lines
7.5 KiB
PHP
<?php
|
|
/**
|
|
* Copyright © Magento, Inc. All rights reserved.
|
|
* See COPYING.txt for license details.
|
|
*/
|
|
namespace Magento\AdminNotification\Model;
|
|
|
|
use Laminas\Http\Request;
|
|
use Magento\AdminNotification\Model\InboxFactory;
|
|
use Magento\Backend\App\ConfigInterface;
|
|
use Magento\Framework\App\DeploymentConfig;
|
|
use Magento\Framework\App\ObjectManager;
|
|
use Magento\Framework\App\ProductMetadataInterface;
|
|
use Magento\Framework\Config\ConfigOptionsListConstants;
|
|
use Magento\Framework\Data\Collection\AbstractDb;
|
|
use Magento\Framework\Escaper;
|
|
use Magento\Framework\HTTP\Adapter\Curl;
|
|
use Magento\Framework\HTTP\Adapter\CurlFactory;
|
|
use Magento\Framework\Model\AbstractModel;
|
|
use Magento\Framework\Model\Context;
|
|
use Magento\Framework\Model\ResourceModel\AbstractResource;
|
|
use Magento\Framework\Registry;
|
|
use Magento\Framework\UrlInterface;
|
|
use SimpleXMLElement;
|
|
|
|
/**
|
|
* AdminNotification Feed model
|
|
*
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
|
* @api
|
|
* @since 100.0.2
|
|
*/
|
|
class Feed extends AbstractModel
|
|
{
|
|
public const XML_USE_HTTPS_PATH = 'system/adminnotification/use_https';
|
|
|
|
public const XML_FEED_URL_PATH = 'system/adminnotification/feed_url';
|
|
|
|
public const XML_FREQUENCY_PATH = 'system/adminnotification/frequency';
|
|
|
|
public const XML_LAST_UPDATE_PATH = 'system/adminnotification/last_update';
|
|
|
|
/**
|
|
* @var Escaper
|
|
*/
|
|
private $escaper;
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected $_feedUrl;
|
|
|
|
/**
|
|
* @var ConfigInterface
|
|
*/
|
|
protected $_backendConfig;
|
|
|
|
/**
|
|
* @var InboxFactory
|
|
*/
|
|
protected $_inboxFactory;
|
|
|
|
/**
|
|
* @var CurlFactory
|
|
*
|
|
*/
|
|
protected $curlFactory;
|
|
|
|
/**
|
|
* Deployment configuration
|
|
*
|
|
* @var DeploymentConfig
|
|
*/
|
|
protected $_deploymentConfig;
|
|
|
|
/**
|
|
* @var ProductMetadataInterface
|
|
*/
|
|
protected $productMetadata;
|
|
|
|
/**
|
|
* @var UrlInterface
|
|
*/
|
|
protected $urlBuilder;
|
|
|
|
/**
|
|
* @param Context $context
|
|
* @param Registry $registry
|
|
* @param ConfigInterface $backendConfig
|
|
* @param InboxFactory $inboxFactory
|
|
* @param CurlFactory $curlFactory
|
|
* @param DeploymentConfig $deploymentConfig
|
|
* @param ProductMetadataInterface $productMetadata
|
|
* @param UrlInterface $urlBuilder
|
|
* @param AbstractResource|null $resource
|
|
* @param AbstractDb|null $resourceCollection
|
|
* @param array $data
|
|
* @param Escaper|null $escaper
|
|
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
|
|
*/
|
|
public function __construct(
|
|
Context $context,
|
|
Registry $registry,
|
|
ConfigInterface $backendConfig,
|
|
InboxFactory $inboxFactory,
|
|
CurlFactory $curlFactory,
|
|
DeploymentConfig $deploymentConfig,
|
|
ProductMetadataInterface $productMetadata,
|
|
UrlInterface $urlBuilder,
|
|
AbstractResource $resource = null,
|
|
AbstractDb $resourceCollection = null,
|
|
array $data = [],
|
|
Escaper $escaper = null
|
|
) {
|
|
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
|
|
$this->_backendConfig = $backendConfig;
|
|
$this->_inboxFactory = $inboxFactory;
|
|
$this->curlFactory = $curlFactory;
|
|
$this->_deploymentConfig = $deploymentConfig;
|
|
$this->productMetadata = $productMetadata;
|
|
$this->urlBuilder = $urlBuilder;
|
|
$this->escaper = $escaper ?? ObjectManager::getInstance()->get(
|
|
Escaper::class
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Init model
|
|
*
|
|
* @return void
|
|
* phpcs:disable Magento2.CodeAnalysis.EmptyBlock
|
|
*/
|
|
protected function _construct()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* Retrieve feed url
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getFeedUrl()
|
|
{
|
|
$httpPath = $this->_backendConfig->isSetFlag(self::XML_USE_HTTPS_PATH) ? 'https://' : 'http://';
|
|
if ($this->_feedUrl === null) {
|
|
$this->_feedUrl = $httpPath . $this->_backendConfig->getValue(self::XML_FEED_URL_PATH);
|
|
}
|
|
return $this->_feedUrl;
|
|
}
|
|
|
|
/**
|
|
* Check feed for modification
|
|
*
|
|
* @return $this
|
|
*/
|
|
public function checkUpdate()
|
|
{
|
|
if ($this->getFrequency() + $this->getLastUpdate() > time()) {
|
|
return $this;
|
|
}
|
|
|
|
$feedData = [];
|
|
|
|
$feedXml = $this->getFeedData();
|
|
|
|
$installDate = strtotime($this->_deploymentConfig->get(ConfigOptionsListConstants::CONFIG_PATH_INSTALL_DATE));
|
|
|
|
if ($feedXml && $feedXml->channel && $feedXml->channel->item) {
|
|
foreach ($feedXml->channel->item as $item) {
|
|
$itemPublicationDate = strtotime((string)$item->pubDate);
|
|
if ($installDate <= $itemPublicationDate) {
|
|
$feedData[] = [
|
|
'severity' => (int)$item->severity,
|
|
'date_added' => date('Y-m-d H:i:s', $itemPublicationDate),
|
|
'title' => $this->escapeString($item->title),
|
|
'description' => $this->escapeString($item->description),
|
|
'url' => $this->escapeString($item->link),
|
|
];
|
|
}
|
|
}
|
|
|
|
if ($feedData) {
|
|
$this->_inboxFactory->create()->parse(array_reverse($feedData));
|
|
}
|
|
}
|
|
$this->setLastUpdate();
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Retrieve Update Frequency
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getFrequency()
|
|
{
|
|
return $this->_backendConfig->getValue(self::XML_FREQUENCY_PATH) * 3600;
|
|
}
|
|
|
|
/**
|
|
* Retrieve Last update time
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getLastUpdate()
|
|
{
|
|
return $this->_cacheManager->load('admin_notifications_lastcheck');
|
|
}
|
|
|
|
/**
|
|
* Set last update time (now)
|
|
*
|
|
* @return $this
|
|
*/
|
|
public function setLastUpdate()
|
|
{
|
|
$this->_cacheManager->save(time(), 'admin_notifications_lastcheck');
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Retrieve feed data as XML element
|
|
*
|
|
* @return SimpleXMLElement
|
|
*/
|
|
public function getFeedData()
|
|
{
|
|
/** @var Curl $curl */
|
|
$curl = $this->curlFactory->create();
|
|
$curl->setOptions(
|
|
[
|
|
'timeout' => 2,
|
|
'useragent' => $this->productMetadata->getName()
|
|
. '/' . $this->productMetadata->getVersion()
|
|
. ' (' . $this->productMetadata->getEdition() . ')',
|
|
'referer' => $this->urlBuilder->getUrl('*/*/*')
|
|
]
|
|
);
|
|
$curl->write(Request::METHOD_GET, $this->getFeedUrl(), '1.0');
|
|
$data = $curl->read();
|
|
$data = preg_split('/^\r?$/m', $data, 2);
|
|
$data = trim($data[1] ?? '');
|
|
$curl->close();
|
|
|
|
try {
|
|
$xml = new SimpleXMLElement($data);
|
|
} catch (\Exception $e) {
|
|
return false;
|
|
}
|
|
|
|
return $xml;
|
|
}
|
|
|
|
/**
|
|
* Retrieve feed as XML element
|
|
*
|
|
* @return SimpleXMLElement
|
|
*/
|
|
public function getFeedXml()
|
|
{
|
|
try {
|
|
$data = $this->getFeedData();
|
|
$xml = new SimpleXMLElement($data);
|
|
} catch (\Exception $e) {
|
|
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8" ?>');
|
|
}
|
|
|
|
return $xml;
|
|
}
|
|
|
|
/**
|
|
* Converts incoming data to string format and escapes special characters.
|
|
*
|
|
* @param SimpleXMLElement $data
|
|
* @return string
|
|
*/
|
|
private function escapeString(SimpleXMLElement $data)
|
|
{
|
|
return $this->escaper->escapeHtml((string)$data);
|
|
}
|
|
}
|