Skip to content

Commit

Permalink
Widget Fixes (xibosignage#2068)
Browse files Browse the repository at this point in the history
* Dashboards: adjust token and cache TTL, 500 on unauthorised display. fixes xibosignage/xibo#3128
* Widget: can't use duration field in status rule. fixes xibosignage/xibo#3112
* Widget: don't output an image if we don't have one. fixes xibosignage/xibo#3120
* Widget: support for different image enclosures and importing a v3 layout. fixes xibosignage/xibo#3121
* Welcome page: remove product support URL (not collected in WL)
  • Loading branch information
dasgarner authored Sep 8, 2023
1 parent d94ac16 commit fbd73ed
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 20 deletions.
10 changes: 9 additions & 1 deletion lib/Connector/XiboDashboardConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class XiboDashboardConnector implements ConnectorInterface
{
use ConnectorTrait;

/** @var float|int The token TTL */
const TOKEN_TTL_SECONDS = 3600 * 24 * 2;

/** @var string Used when rendering the form */
private $errorMessage;

Expand Down Expand Up @@ -517,7 +520,7 @@ public function onDataRequest(DashboardDataRequestEvent $event, $eventName, Even
try {
$tokenEvent = new XmdsConnectorTokenEvent();
$tokenEvent->setTargets($event->getDataProvider()->getDisplayId(), $event->getDataProvider()->getWidgetId());
$tokenEvent->setTtl(3600 * 24 * 2);
$tokenEvent->setTtl(self::TOKEN_TTL_SECONDS);
$dispatcher->dispatch($tokenEvent, XmdsConnectorTokenEvent::$NAME);
$token = $tokenEvent->getToken();

Expand All @@ -537,6 +540,11 @@ public function onDataRequest(DashboardDataRequestEvent $event, $eventName, Even
$item['token'] = $token;
$item['isPreview'] = $event->getDataProvider()->isPreview();

// We make sure our data cache expires shortly before the token itself expires (so that we have a new token
// generated for it).
$event->getDataProvider()->setCacheTtl(self::TOKEN_TTL_SECONDS - 3600);

// Add our item and set handled
$event->getDataProvider()->addItem($item);
$event->getDataProvider()->setIsHandled();
}
Expand Down
3 changes: 3 additions & 0 deletions lib/Entity/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -1853,6 +1853,9 @@ public function assessWidgetStatus(Module $module, Widget $widget, int &$status)
}
}
} catch (GeneralException $xiboException) {
$this->getLog()->debug('assessWidgetStatus: ' . $module->moduleId . ' invalid, e: '
. $xiboException->getMessage());

$moduleStatus = Status::$STATUS_INVALID;

// Include the exception on
Expand Down
39 changes: 39 additions & 0 deletions lib/Widget/Compatibility/RssWidgetCompatibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,45 @@ public function upgradeWidget(Widget $widget, int $fromSchema, int $toSchema): b
}
$widget->setOptionValue('templateId', 'attrib', $newTemplateId);

// If the new templateId is custom, we need to parse the old template for image enclosures
if ($newTemplateId === 'article_custom_html') {
$template = $widget->getOptionValue('template', null);
if (!empty($template)) {
$modified = false;
$matches = [];
preg_match_all('/\[(.*?)\]/', $template, $matches);

for ($i = 0; $i < count($matches[1]); $i++) {
// We have a [Link] or a [xxx|image] tag
$match = $matches[1][$i];
if ($match === 'Link' || $match === 'Link|image') {
// This is a straight-up enclosure (which is the default).
$template = str_replace($matches[0][$i], '<img src="[image]" alt="Image" />', $template);
$modified = true;
} else if (str_contains($match, '|image')) {
// [tag|image|attribute]
// Set the necessary options depending on how our tag is made up
$parts = explode('|', $match);
$tag = $parts[0];
$attribute = $parts[2] ?? null;

$widget->setOptionValue('imageSource', 'attrib', 'custom');
$widget->setOptionValue('imageSourceTag', 'attrib', $tag);
if (!empty($attribute)) {
$widget->setOptionValue('imageSourceAttribute', 'attrib', $attribute);
}

$template = str_replace($matches[0][$i], '<img src="[image]" alt="Image"/>', $template);
$modified = true;
}
}

if ($modified) {
$widget->setOptionValue('template', 'attrib', $template);
}
}
}

// Change some other options if they have been set.
foreach ($widget->widgetOptions as $option) {
$widgetChangeOption = null;
Expand Down
34 changes: 27 additions & 7 deletions lib/Widget/RssProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,21 @@ public function fetchData(DataProviderInterface $dataProvider): WidgetProviderIn
}
}

// Where should we get images?
$imageSource = $dataProvider->getProperty('imageSource', 'enclosure');
$imageTag = match ($imageSource) {
'mediaContent' => 'media:content',
'image' => 'image',
'custom' => $dataProvider->getProperty('imageSourceTag', 'image'),
default => 'enclosure'
};
$imageSourceAttribute = null;
if ($imageSource === 'mediaContent') {
$imageSourceAttribute = 'url';
} else if ($imageSource === 'custom') {
$imageSourceAttribute = $dataProvider->getProperty('imageSourceAttribute', null);
}

$countItems = 0;
// Parse each item into an article
foreach ($feedItems as $item) {
Expand All @@ -159,14 +174,19 @@ public function fetchData(DataProviderInterface $dataProvider): WidgetProviderIn
$article->summary = trim($descriptionTag ? strip_tags($descriptionTag[0]) : $article->content);

// Do we have an image included?
if (stripos($item->getEnclosureType(), 'image') > -1) {
$link = $item->getEnclosureUrl();

if (!(empty($link))) {
$article->image = $dataProvider->addImage('ticker_' . md5($link), $link, $expiresImage);
} else {
$this->getLog()->debug('No image found for image tag using getEnclosureUrl');
$link = null;
if ($imageTag === 'enclosure') {
if (stripos($item->getEnclosureType(), 'image') > -1) {
$link = $item->getEnclosureUrl();
}
} else {
$link = $item->getTag($imageTag, $imageSourceAttribute)[0] ?? null;
}

if (!(empty($link))) {
$article->image = $dataProvider->addImage('ticker_' . md5($link), $link, $expiresImage);
} else {
$this->getLog()->debug('fetchData: no image found for image tag using ' . $imageTag);
}

if ($dataProvider->getProperty('decodeHtml') == 1) {
Expand Down
2 changes: 1 addition & 1 deletion modules/countdown-clock.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
<condition field="countdownType" type="eq">2</condition>
</test>
</visibility>
<rule>
<rule onStatus="false">
<test type="or" message="Warning duration needs to be lower than the countdown main duration.">
<condition type="eq"></condition>
<condition field="countdownType" type="neq">2</condition>
Expand Down
2 changes: 1 addition & 1 deletion modules/countdown-custom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
<condition field="countdownType" type="eq">2</condition>
</test>
</visibility>
<rule>
<rule onStatus="false">
<test type="or" message="Warning duration needs to be lower than the countdown main duration.">
<condition type="eq"></condition>
<condition field="countdownType" type="neq">2</condition>
Expand Down
2 changes: 1 addition & 1 deletion modules/countdown-days.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
<condition field="countdownType" type="eq">2</condition>
</test>
</visibility>
<rule>
<rule onStatus="false">
<test type="or" message="Warning duration needs to be lower than the countdown main duration.">
<condition type="eq"></condition>
<condition field="countdownType" type="neq">2</condition>
Expand Down
2 changes: 1 addition & 1 deletion modules/countdown-table.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
<condition field="countdownType" type="eq">2</condition>
</test>
</visibility>
<rule>
<rule onStatus="false">
<test type="or" message="Warning duration needs to be lower than the countdown main duration.">
<condition type="eq"></condition>
<condition field="countdownType" type="neq">2</condition>
Expand Down
2 changes: 1 addition & 1 deletion modules/countdown-text.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
<condition field="countdownType" type="eq">2</condition>
</test>
</visibility>
<rule>
<rule onStatus="false">
<test type="or" message="Warning duration needs to be lower than the countdown main duration.">
<condition type="eq"></condition>
<condition field="countdownType" type="neq">2</condition>
Expand Down
33 changes: 32 additions & 1 deletion modules/rss-ticker.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<type>rss-ticker</type>
<legacyType>ticker</legacyType>
<dataType>article</dataType>
<dataCacheKey>%uri%_%numItems%_%stripTags%</dataCacheKey>
<dataCacheKey>%uri%_%numItems%_%stripTags%_%imageSource%_%imageSourceTag%</dataCacheKey>
<schemaVersion>2</schemaVersion>
<assignable>1</assignable>
<regionSpecific>1</regionSpecific>
Expand Down Expand Up @@ -99,6 +99,37 @@
<helpText>Should the order of the feed be randomised? When enabled each time the Widget is shown the items will be randomly shuffled and displayed in a random order.</helpText>
<default>0</default>
</property>
<property id="imageSource" type="dropdown" mode="single">
<title>Image Tag</title>
<helpText>Choose the tag in the feed to get an image URL</helpText>
<default>enclosure</default>
<options>
<option name="enclosure">Enclosure</option>
<option name="mediaContent">Media Content</option>
<option name="image">Image</option>
<option name="custom">Custom</option>
</options>
</property>
<property id="imageSourceTag" type="text">
<title>Custom Tag</title>
<helpText>A valid tag name which appears in this feed and will be used to get an image URL.</helpText>
<default></default>
<visibility>
<test>
<condition field="imageSource" type="eq">custom</condition>
</test>
</visibility>
</property>
<property id="imageSourceAttribute" type="text">
<title>Custom Tag Attribute</title>
<helpText>If the image URL is on an attribute of the custom tag, provide the attribute name.</helpText>
<default></default>
<visibility>
<test>
<condition field="imageSource" type="eq">custom</condition>
</test>
</visibility>
</property>
<property id="allowedAttributes" type="text">
<title>Allowable Attributes</title>
<helpText>A comma separated list of attributes that should not be stripped from the incoming feed.</helpText>
Expand Down
6 changes: 3 additions & 3 deletions modules/templates/article-static.xml
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ $(target).xiboTextRender(properties, $(target).find('#content > *'));
<stencil>
<hbs><![CDATA[
<div class="image">
<img src="{{image}}"/>
{{#if image}}<img src="{{image}}"/>{{/if}}
</div>
]]></hbs>
<twig><![CDATA[
Expand Down Expand Up @@ -321,7 +321,7 @@ $(target).xiboTextRender(properties, $(target).find('#content > .image, #content
<stencil>
<hbs><![CDATA[
<div class="image">
<img src="{{image}}"/>
{{#if image}}<img src="{{image}}"/>{{/if}}
<div class="cycle-overlay background">
<div class="title">{{title}}</div>
<div class="description">{{{content}}}</div>
Expand Down Expand Up @@ -516,7 +516,7 @@ $(target).xiboTextRender(properties, $(target).find('#content > .image, #content
<stencil>
<hbs><![CDATA[
<div class="image">
<img src="{{image}}"/>
{{#if image}}<img src="{{image}}"/>{{/if}}
<div class="cycle-overlay background">
<div class="title">{{title}}</div>
</div>
Expand Down
2 changes: 0 additions & 2 deletions views/welcome-page.twig
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
<a href="https://xibosignage.com/install-upgrade#upgrade" target="_blank" class="card-link">Upgrade</a>
{% else %}
<p class="card-text">{{ "Your service provider can help you with installation and upgrade."|trans }}</p>
<a href="{{ theme.getThemeConfig("product_support_url", "https://community.xibo.org.uk/c/support") }}" target="_blank" class="card-link">Get Help</a>
{% endif %}
</div>
</div>
Expand Down Expand Up @@ -159,7 +158,6 @@
<a href="https://community.xibo.org.uk" target="_blank" class="card-link">Community</a>
{% else %}
<p class="card-text">{{ "Contact your service provider for assistance."|trans }}</p>
<a href="{{ theme.getThemeConfig("product_support_url", "https://community.xibo.org.uk/c/support") }}" target="_blank" class="card-link">{{ "Get Help"|trans }}</a>
{% endif %}
</div>
</div>
Expand Down
19 changes: 18 additions & 1 deletion web/xmds.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,16 @@
throw new \Xibo\Support\Exception\InstanceSuspendedException('Bandwidth Exceeded');
}

// Check the display specific limit next.
// Get the display
/** @var \Xibo\Entity\Display $display */
$display = $container->get('displayFactory')->getById($displayId);

// Check it is still authorised.
if ($display->licensed == 0) {
throw new NotFoundException(__('Display unauthorised'));
}

// Check the display specific limit next.
$usage = 0;
if ($container->get('bandwidthFactory')->isBandwidthExceeded(
$display->bandwidthLimit,
Expand Down Expand Up @@ -283,6 +291,15 @@
exit;
}

// Get the display
/** @var \Xibo\Entity\Display $display */
$display = $container->get('displayFactory')->getById($tokenEvent->getDisplayId());

// Check it is still authorised.
if ($display->licensed == 0) {
throw new NotFoundException(__('Display unauthorised'));
}

// Check the widgetId is permissible, and in required files for the display.
/** @var \Xibo\Entity\RequiredFile $file */
$file = $container->get('requiredFileFactory')->getByDisplayAndWidget(
Expand Down

0 comments on commit fbd73ed

Please sign in to comment.