Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions cache/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
class cache
{
/** @var string Cache key used for decoded custom media widths */
protected const MAX_WIDTH_CACHE_KEY = '_phpbb_mediaembed_max_width';

/** @var cache_driver */
protected $cache;

Expand Down Expand Up @@ -70,4 +73,35 @@ public function purge_textformatter_cache()
$this->cache->destroy($this->cache_key_parser);
$this->cache->destroy($this->cache_key_renderer);
}

/**
* Get cached custom media widths.
*
* @return array|false Cached widths, or false on a cache miss
*/
public function get_media_embed_max_width()
{
return $this->cache->get(self::MAX_WIDTH_CACHE_KEY);
}

/**
* Cache custom media widths.
*
* @param array $max_widths Decoded custom media widths
* @return void
*/
public function set_media_embed_max_width(array $max_widths)
{
$this->cache->put(self::MAX_WIDTH_CACHE_KEY, $max_widths);
}

/**
* Purge cached custom media widths.
*
* @return void
*/
public function purge_media_embed_max_width()
{
$this->cache->destroy(self::MAX_WIDTH_CACHE_KEY);
}
}
2 changes: 2 additions & 0 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ services:
phpbb.mediaembed.display_listener:
class: phpbb\mediaembed\event\display_listener
arguments:
- '@auth'
- '@config'
- '@config_text'
- '@language'
- '@phpbb.mediaembed.cache'
- '@template'
tags:
- { name: event.listener }
Expand Down
1 change: 1 addition & 0 deletions controller/acp_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public function save_settings()
$this->config->set('media_embed_full_width', $this->request->variable('media_embed_full_width', 0));

$this->set_media_embed_max_width();
$this->media_cache->purge_media_embed_max_width();

$this->media_cache->purge_textformatter_cache();

Expand Down
106 changes: 94 additions & 12 deletions event/display_listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@

namespace phpbb\mediaembed\event;

use phpbb\auth\auth;
use phpbb\config\config;
use phpbb\config\db_text;
use phpbb\language\language;
use phpbb\mediaembed\cache\cache as media_cache;
use phpbb\template\template;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

Expand All @@ -23,6 +25,9 @@ class display_listener implements EventSubscriberInterface
{
public const MEDIA_DEMO_URL = 'https://youtu.be/Ne18ZQ7LLI0';

/** @var auth */
protected $auth;

/** @var config */
protected $config;

Expand All @@ -32,15 +37,24 @@ class display_listener implements EventSubscriberInterface
/** @var language */
protected $language;

/** @var media_cache */
protected $media_cache;

/** @var template */
protected $template;

/** @var bool */
protected $media_allowed = true;

/**
* {@inheritDoc}
*/
public static function getSubscribedEvents()
{
return [
'core.posting_modify_template_vars' => 'set_forum_button_permission',
'core.ucp_pm_compose_template' => 'set_pm_button_permission',
'core.ucp_profile_modify_signature' => 'set_signature_button_permission',
'core.display_custom_bbcodes' => 'setup_media_bbcode',
'core.help_manager_add_block_before' => 'media_embed_help',
'core.page_header' => 'setup_media_configs',
Expand All @@ -51,16 +65,20 @@ public static function getSubscribedEvents()
/**
* Constructor.
*
* @param config $config Configuration service
* @param db_text $config_text Text configuration service
* @param language $language Language service
* @param template $template Template service
* @param auth $auth Authentication service
* @param config $config Configuration service
* @param db_text $config_text Text configuration service
* @param language $language Language service
* @param media_cache $media_cache MediaEmbed cache service
* @param template $template Template service
*/
public function __construct(config $config, db_text $config_text, language $language, template $template)
public function __construct(auth $auth, config $config, db_text $config_text, language $language, media_cache $media_cache, template $template)
{
$this->auth = $auth;
$this->config = $config;
$this->config_text = $config_text;
$this->language = $language;
$this->media_cache = $media_cache;
$this->template = $template;
}

Expand All @@ -71,8 +89,55 @@ public function __construct(config $config, db_text $config_text, language $lang
*/
public function setup_media_bbcode()
{
if ($this->template->retrieve_var('S_SIGNATURE'))
{
$this->set_signature_button_permission();
}

$this->language->add_lang('common', 'phpbb/mediaembed');
$this->template->assign_var('S_BBCODE_MEDIA', $this->config->offsetGet('media_embed_bbcode'));
$this->template->assign_var(
'S_BBCODE_MEDIA',
$this->config->offsetGet('media_embed_bbcode') && $this->media_allowed
);
}

/**
* Set Media BBCode button permission for forum posting.
*
* @param \phpbb\event\data $event Event data
* @return void
*/
public function set_forum_button_permission($event)
{
$media_allowed = $this->config->offsetGet('media_embed_bbcode')
&& $this->config->offsetGet('allow_bbcode')
&& $this->auth->acl_get('f_bbcode', $event['forum_id'])
&& $this->auth->acl_get('f_mediaembed', $event['forum_id']);

$event->update_subarray('page_data', 'S_BBCODE_MEDIA', $media_allowed);
}

/**
* Set Media BBCode button permission for private messages.
*
* @param \phpbb\event\data $event Event data
* @return void
*/
public function set_pm_button_permission($event)
{
$this->media_allowed = !empty($event['template_ary']['S_BBCODE_ALLOWED'])
&& $this->auth->acl_get('u_pm_mediaembed');
}

/**
* Set Media BBCode button permission for signatures.
*
* @return void
*/
public function set_signature_button_permission()
{
$this->media_allowed = $this->config->offsetGet('allow_sig_bbcode')
&& $this->config->offsetGet('media_embed_allow_sig');
}

/**
Expand All @@ -94,14 +159,24 @@ public function media_embed_help($event)
'SWITCH_COLUMN' => false,
]);

$uid = $bitfield = $flags = '';
$demo_text = self::MEDIA_DEMO_URL;
generate_text_for_storage($demo_text, $uid, $bitfield, $flags, true, true);
$demo_display = generate_text_for_display($demo_text, $uid, $bitfield, $flags);
$site_ids = $this->get_site_ids();
$plain_url_help = $this->config->offsetGet('media_embed_parse_urls')
? $this->language->lang('HELP_EMBEDDING_MEDIA_PLAIN_URL')
: '';
$demo_help = '';

if (in_array('youtube', $site_ids, true))
{
$uid = $bitfield = $flags = '';
$demo_text = '[media]' . self::MEDIA_DEMO_URL . '[/media]';
generate_text_for_storage($demo_text, $uid, $bitfield, $flags, true, true);
$demo_display = generate_text_for_display($demo_text, $uid, $bitfield, $flags);
$demo_help = $this->language->lang('HELP_EMBEDDING_MEDIA_DEMO', self::MEDIA_DEMO_URL, $demo_display);
}

$this->template->assign_block_vars('faq_block.faq_row', [
'FAQ_QUESTION' => $this->language->lang('HELP_EMBEDDING_MEDIA_QUESTION'),
'FAQ_ANSWER' => $this->language->lang('HELP_EMBEDDING_MEDIA_ANSWER', self::MEDIA_DEMO_URL, $demo_display, implode(', ', $this->get_site_ids())),
'FAQ_ANSWER' => $this->language->lang('HELP_EMBEDDING_MEDIA_ANSWER', $plain_url_help, $demo_help, implode(', ', $site_ids)),
]);
}

Expand All @@ -112,9 +187,16 @@ public function media_embed_help($event)
*/
public function setup_media_configs()
{
$max_widths = $this->media_cache->get_media_embed_max_width();
if ($max_widths === false)
{
$max_widths = json_decode($this->config_text->get('media_embed_max_width'), true) ?: [];
$this->media_cache->set_media_embed_max_width($max_widths);
}

$this->template->assign_vars([
'S_MEDIA_EMBED_FULL_WIDTH' => $this->config->offsetGet('media_embed_full_width'),
'S_MEDIA_EMBED_MAX_WIDTHS' => json_decode($this->config_text->get('media_embed_max_width'), true),
'S_MEDIA_EMBED_MAX_WIDTHS' => $max_widths,
]);
}

Expand Down
50 changes: 21 additions & 29 deletions event/formatter_listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class formatter_listener implements EventSubscriberInterface
public static function getSubscribedEvents()
{
return [
'core.text_formatter_s9e_configure_after' => [['add_custom_sites', 3], ['enable_media_sites', 2], ['configure_url_parsing', 1], ['modify_tag_templates', 0]],
'core.text_formatter_s9e_configure_after' => [['add_custom_sites', 2], ['enable_media_sites', 1], ['modify_tag_templates', 0]],
'core.text_formatter_s9e_parser_setup' => 'setup_cache_dir',
];
}
Expand Down Expand Up @@ -97,36 +97,14 @@ public function enable_media_sites($event)
}

/**
* Configure plain URL parsing.
*
* @param \phpbb\event\data $event Event data
* @return void
*/
public function configure_url_parsing($event)
{
if (!$this->config->offsetGet('media_embed_parse_urls'))
{
$event['configurator']->MediaEmbed->finalize();
unset($event['configurator']->MediaEmbed);
}
}

/**
* Apply compatibility and privacy changes to generated tag templates.
* Apply site-specific changes to generated tag templates.
*
* @param \phpbb\event\data $event Event data
* @return void
*/
public function modify_tag_templates($event)
{
try
{
$this->configure_youtube($event['configurator']->tags['YOUTUBE']);
}
catch (\RuntimeException $e)
{
// YouTube is not enabled.
}
$this->configure_youtube($event['configurator']);
}

/**
Expand Down Expand Up @@ -158,20 +136,34 @@ protected function get_site_ids()
/**
* Apply YouTube privacy, compatibility, and Shorts rendering changes.
*
* @param s9e\TextFormatter\Configurator\Items\Tag $tag Tag to configure
* @param \s9e\TextFormatter\Configurator $configurator TextFormatter configurator
* @return void
*/
public function configure_youtube($tag)
public function configure_youtube($configurator)
{
if (isset($configurator->BBCodes['youtube']))
{
return;
}

try
{
$tag = $configurator->tags['YOUTUBE'];
}
catch (\RuntimeException $e)
{
return;
}

$template = str_replace('www.youtube.com', 'www.youtube-nocookie.com', (string) $tag->template);
if (!$this->sites->is_phpbb4())
{
$template = str_replace(' allowfullscreen', ' referrerpolicy="origin" allowfullscreen', $template);
}

$shorts_template = str_replace(
['max-width:640px', 'padding-bottom:56.25%'],
['max-width:360px', 'padding-bottom:177.777778%'],
['max-width:640px', 'padding-bottom:56.25%', 'data-s9e-mediaembed="youtube"'],
['max-width:360px', 'padding-bottom:177.777778%', 'data-s9e-mediaembed="youtube" data-mediaembed-shorts="1"'],
$template
);

Expand Down
9 changes: 4 additions & 5 deletions event/parsing_listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,20 @@ public function set_parse_policy($event)
&& !$this->config->offsetGet('media_embed_allow_sig');

$this->disable_tag = !$this->media_allowed || $signature_disabled || !$event['allow_bbcode'];
$this->disable_plugin = $this->disable_tag
|| !$event['allow_magic_url']
|| !$this->config->offsetGet('media_embed_parse_urls');
$this->disable_plugin = $this->disable_tag || !$event['allow_magic_url'];
}

/**
* Apply current message policy to runtime parser.
* Apply global and current message policy to runtime parser.
*
* @param \phpbb\event\data $event Event data
* @return void
*/
public function configure_parser($event)
{
$parser = $event['parser']->get_parser();
$plugin_method = $this->disable_plugin ? 'disablePlugin' : 'enablePlugin';
$disable_plugin = $this->disable_plugin || !$this->config->offsetGet('media_embed_parse_urls');
$plugin_method = $disable_plugin ? 'disablePlugin' : 'enablePlugin';
$tag_method = $this->disable_tag ? 'disableTag' : 'enableTag';

$parser->{$plugin_method}('MediaEmbed');
Expand Down
11 changes: 5 additions & 6 deletions language/ar/help.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@
'HELP_EMBEDDING_MEDIA' => 'إدراج مُحتوى الوسائط',
'HELP_EMBEDDING_MEDIA_QUESTION' => 'كيف أستطيع إدراج مُحتوى الوسائط من المواقع الأخرى في المُشاركات؟',
'HELP_EMBEDDING_MEDIA_ANSWER' => 'يستطيع الأعضاء إدراج محتوى الوسائط مثل الفيديو والمقاطع الصوتية من المواقع المسموح بها بإستخدام
الوسم <strong>[media][/media]</strong>, أو ببساطة كتابة الرابط لأحد المواقع المدعومة
كما هو بدون الوسوم. مثال:<br><br>
<strong>[media]</strong>%1$s<strong>[/media]</strong>
<br><br>كما تلاحظ في المثال أعلاه, تستطيع أيضاً استخدام الرابط بدون الوسم
<strong>[media]</strong>.
<br><br>ستكون النتيجة للمثال المذكور أعلاه كالآتي:<br><br>%2$s
الوسم <strong>[media][/media]</strong>%1$s.%2$s
<br><br>القائمة التالية تحتوي على المواقع المدعومة:<br><samp>%3$s.</samp>
<br><br>للمزيد من المعلومات والأمثلة للمواقع المدعومة,
انقر على هذا الرابط <a href="https://s9etextformatter.readthedocs.io/Plugins/MediaEmbed/Sites/">
معلومات إدراج مُحتوى الوسائط</a>.',
'HELP_EMBEDDING_MEDIA_PLAIN_URL' => ', أو ببساطة كتابة الرابط لأحد المواقع المدعومة كما هو بدون الوسوم',
'HELP_EMBEDDING_MEDIA_DEMO' => '<br><br>مثال:<br><br>
<strong>[media]</strong>%1$s<strong>[/media]</strong>
<br><br>ستكون النتيجة للمثال المذكور أعلاه كالآتي:<br><br>%2$s',
]);
10 changes: 5 additions & 5 deletions language/cs/help.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@
$lang = array_merge($lang, [
'HELP_EMBEDDING_MEDIA' => 'Vkládání médií',
'HELP_EMBEDDING_MEDIA_QUESTION' => 'Jak vkládat média z jiných stránek do příspěvků.',
'HELP_EMBEDDING_MEDIA_ANSWER' => 'Uživatelů mohou vkládat obsah jako jsou videa nebo zvukové stopy z povolených stránek používáním <strong>[media][/media]</strong> nebo jednoduše vložením povoleného URL do textu. Například:<br><br>
<strong>[media]</strong>%1$s<strong>[/media]</strong>
<br><br>Jak bylo výše zmíněno, odkaz může být také vložen pomocí
<strong>[media]</strong> tagů.
<br><br>Uvedený příklad bude vypadat následovně:<br><br>%2$s
'HELP_EMBEDDING_MEDIA_ANSWER' => 'Uživatelů mohou vkládat obsah jako jsou videa nebo zvukové stopy z povolených stránek používáním <strong>[media][/media]</strong>%1$s.%2$s
<br><br>Tyto stránky jsou podporovány:<br><samp>%3$s.</samp>
<br><br>Pro kompletní dokumentaci podporovaných tránek a příklady URL navštivte <a href="https://s9etextformatter.readthedocs.io/Plugins/MediaEmbed/Sites/">
MediaEmbed Plugin dokumentace</a>.',
'HELP_EMBEDDING_MEDIA_PLAIN_URL' => ' nebo jednoduše vložením povoleného URL do textu',
'HELP_EMBEDDING_MEDIA_DEMO' => '<br><br>Například:<br><br>
<strong>[media]</strong>%1$s<strong>[/media]</strong>
<br><br>Uvedený příklad bude vypadat následovně:<br><br>%2$s',
]);
Loading