streams/Code/Widget/Photo.php

62 lines
1.4 KiB
PHP
Raw Normal View History

<?php
2022-02-16 04:08:28 +00:00
namespace Code\Widget;
2022-10-23 21:18:44 +00:00
class Photo implements WidgetInterface
2021-12-02 23:02:31 +00:00
{
2021-12-02 23:02:31 +00:00
/**
* @brief Widget to display a single photo.
*
2022-10-24 03:39:49 +00:00
* @param array $arguments associative array with
2021-12-02 23:02:31 +00:00
* * \e string \b src URL of photo; URL must be an http or https URL
* * \e boolean \b zrl use zid in URL
* * \e string \b style CSS string
*
* @return string with parsed HTML
*/
2022-10-24 03:39:49 +00:00
public function widget(array $arguments): string
2021-12-02 23:02:31 +00:00
{
2021-12-02 23:02:31 +00:00
$style = $zrl = false;
2022-10-24 03:39:49 +00:00
if (array_key_exists('src', $arguments) && isset($arguments['src'])) {
$url = $arguments['src'];
2021-12-03 03:01:39 +00:00
}
2022-10-23 21:18:44 +00:00
if (!str_starts_with($url, 'http')) {
2021-12-02 23:02:31 +00:00
return '';
2021-12-03 03:01:39 +00:00
}
2022-10-24 03:39:49 +00:00
if (array_key_exists('style', $arguments) && isset($arguments['style'])) {
$style = $arguments['style'];
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
// ensure they can't sneak in an eval(js) function
2021-12-03 03:01:39 +00:00
if (strpbrk($style, '(\'"<>') !== false) {
2021-12-02 23:02:31 +00:00
$style = '';
2021-12-03 03:01:39 +00:00
}
2022-10-24 03:39:49 +00:00
if (array_key_exists('zrl', $arguments) && isset($arguments['zrl'])) {
$zrl = (($arguments['zrl']) ? true : false);
2021-12-03 03:01:39 +00:00
}
2021-12-03 03:01:39 +00:00
if ($zrl) {
2021-12-02 23:02:31 +00:00
$url = zid($url);
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
$o = '<div class="widget">';
2021-12-02 23:02:31 +00:00
$o .= '<img ' . (($zrl) ? ' class="zrl" ' : '')
. (($style) ? ' style="' . $style . '"' : '')
. ' src="' . $url . '" alt="' . t('photo/image') . '">';
2021-12-02 23:02:31 +00:00
$o .= '</div>';
2021-12-02 23:02:31 +00:00
return $o;
}
}