Skip to content

Commit dd02de3

Browse files
committed
- Improve regex for gifv and youtube urls
- Pass the ID when using url auto parsing, prevents using another regex to get it. - Use global scope inside closures Signed-off-by: Jessica González <suki@missallsunday.com>
1 parent ffd70ff commit dd02de3

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

Sources/OharaYTEmbed.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ function OYTE_bbc_add_code(&$codes)
4444
'tag' => 'yt',
4545
'type' => 'unparsed_content',
4646
'content' => '$1',
47-
'validate' => function (&$tag, &$data, $disabled) use ($txt)
47+
'validate' => function (&$tag, &$data, $disabled)
4848
{
49+
global $txt;
50+
4951
// This tag was disabled.
5052
if (!empty($disabled['yt']))
5153
return;
@@ -63,8 +65,10 @@ function OYTE_bbc_add_code(&$codes)
6365
'tag' => 'vimeo',
6466
'type' => 'unparsed_content',
6567
'content' => '$1',
66-
'validate' => function (&$tag, &$data, $disabled) use ($txt)
68+
'validate' => function (&$tag, &$data, $disabled)
6769
{
70+
global $txt;
71+
6872
// This tag was disabled.
6973
if (!empty($disabled['vimeo']))
7074
return;
@@ -82,8 +86,10 @@ function OYTE_bbc_add_code(&$codes)
8286
'tag' => 'gifv',
8387
'type' => 'unparsed_content',
8488
'content' => '$1',
85-
'validate' => function (&$tag, &$data, $disabled) use ($txt)
89+
'validate' => function (&$tag, &$data, $disabled)
8690
{
91+
global $txt;
92+
8793
// This tag was disabled.
8894
if (!empty($disabled['gifv']))
8995
return;
@@ -172,14 +178,14 @@ function OYTE_Main($data)
172178
$videoID = $data;
173179

174180
// We all love Regex.
175-
$pattern = '#^(?:https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch\?v=|/watch\?.+&v=))([\w-]{11})(?:.+)?$#x';
181+
$pattern = '#(?:https?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/watch\?.+&v=))([\w-]{11}) (?:[^\s]+) (?:[ \t\r\n])#xi';
176182

177183
// First attempt, pure regex.
178184
if (empty($videoID) && preg_match($pattern, $data, $matches))
179185
$videoID = isset($matches[1]) ? $matches[1] : false;
180186

181187
// Give another regex a chance.
182-
elseif (empty($videoID) && preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $data, $match))
188+
elseif (empty($videoID) && preg_match('%(?:youtube(?:-nocookie)?\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})%i', $data, $match))
183189
$videoID = isset($match[1]) ? $match[1] : false;
184190

185191
// No?, then one last chance, let PHPs native parse_url() function do the dirty work.
@@ -240,8 +246,12 @@ function OYTE_Gifv($data)
240246
$videoID = '';
241247
$result = '';
242248

249+
if (strpos($data, 'http') === false || strpos($data, '.com') === false)
250+
return '<video class="oharaEmbed gifv" autoplay loop style="max-width: '. (empty($modSettings['OYTE_video_width']) ? '480' : $modSettings['OYTE_video_width']) .'px; max-height: '. (empty($modSettings['OYTE_video_height']) ? '270' : $modSettings['OYTE_video_height']) .'px;" src="//i.imgur.com/'. $data .'.webm"><source src="//i.imgur.com/'. $data .'.webm" type="video/webm"></source></video>';
251+
252+
243253
// We all love Regex.
244-
$pattern = '/^(?:https?:\/\/)?(?:www\.)?i\.imgur\.com\/([a-z0-9]+)\.gifv/i';
254+
$pattern = '/^(?:https?:\/\/)?(?:www\.)?i\.imgur\.com\/([a-z0-9]+)\.(?:gifv|webm)/i';
245255

246256
// First attempt, pure regex.
247257
if (empty($videoID) && preg_match($pattern, $data, $matches))
@@ -254,9 +264,7 @@ function OYTE_Gifv($data)
254264

255265
// Got something!
256266
else
257-
$result = '<video class="oharaEmbed gifv" preload="auto" autoplay="autoplay" loop="loop" style="max-width: '. (empty($modSettings['OYTE_video_width']) ? '480' : $modSettings['OYTE_video_width']) .'px; max-height: '. (empty($modSettings['OYTE_video_height']) ? '270' : $modSettings['OYTE_video_height']) .'px;" src="//i.imgur.com/'. $videoID .'.webm">
258-
<source src="//i.imgur.com/'. $videoID .'.webm" type="video/webm"></source>
259-
</video>';
267+
$result = '<video class="oharaEmbed gifv" autoplay loop style="max-width: '. (empty($modSettings['OYTE_video_width']) ? '480' : $modSettings['OYTE_video_width']) .'px; max-height: '. (empty($modSettings['OYTE_video_height']) ? '270' : $modSettings['OYTE_video_height']) .'px;" src="//i.imgur.com/'. $videoID .'.webm"><source src="//i.imgur.com/'. $videoID .'.webm" type="video/webm"></source></video>';
260268

261269
return $result;
262270
}
@@ -275,15 +283,15 @@ function OYTE_Preparse($message)
275283

276284
// The extremely long regex...
277285
$vimeo = '~(?<=[\s>\.(;\'"]|^)(?:https?\:\/\/)?(?:www\.)?vimeo.com\/(?:album\/|groups\/(.*?)\/|channels\/(.*?)\/)?[0-9]+\??[/\w\-_\~%@\?;=#}\\\\]?~';
278-
$youtube = '~(?<=[\s>\.(;\'"]|^)(?:http|https):\/\/[\w\-_%@:|]?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch\?v=|/watch\?.+&v=))([\w-]{11})(?=[^\w-]|$)(?![?=&+%\w.-]*(?:[\'"][^<>]*> | </a> ))[?=&+%\w.-]*[/\w\-_\~%@\?;=#}\\\\]?~ix';
286+
$youtube = '~(?<=[\s>\.(;\'"]|^)(?:http|https):\/\/[\w\-_%@:|]?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/watch\?.+&v=))([\w-]{11})(?:[^\s]+)?(?=[^\w-]|$)(?![?=&+%\w.-]*(?:[\'"][^<>]*> | <\/a> ))[?=&+%\w.-]*[\/\w\-_\~%@\?;=#}\\\\]?~ix';
279287

280-
$gifv = '~(?<=[\s>\.(;\'"]|^)(?:http|https):\/\/[\w\-_%@:|]?(?:www\.)?i\.imgur\.com\/([a-z0-9]+)\.gifv(?=[^\w-]|$)(?![?=&+%\w.-]*(?:[\'"][^<>]*> | <\/a> ))[?=&+%\w.-]*[\/\w\-_\~%@\?;=#}\\\\]?~ix';
288+
$gifv = '~(?<=[\s>\.(;\'"]|^)(?:http|https):\/\/[\w\-_%@:|]?(?:www\.)?i\.imgur\.com\/([a-z0-9]+)\.(?:gifv|webm)(?=[^\w-]|$)(?![?=&+%\w.-]*(?:[\'"][^<>]*> | <\/a> ))[?=&+%\w.-]*[\/\w\-_\~%@\?;=#}\\\\]?~ix';
281289

282290
// Is this a YouTube video url?
283291
$message = preg_replace_callback(
284292
$youtube,
285293
function ($matches) {
286-
return '[youtube]'. $matches[0] .'[/youtube]';
294+
return '[youtube]'. $matches[1] .'[/youtube]';
287295
},
288296
$message
289297
);
@@ -301,7 +309,7 @@ function ($matches) {
301309
$message = preg_replace_callback(
302310
$gifv,
303311
function ($matches) {
304-
return '[gifv]'. $matches[0] .'[/gifv]';
312+
return '[gifv]'. $matches[1] .'[/gifv]';
305313
},
306314
$message
307315
);

0 commit comments

Comments
 (0)