Improve support for YouTube's time parameters in media provider
authorTim Düsterhus <duesterhus@woltlab.com>
Fri, 14 Sep 2018 13:03:29 +0000 (15:03 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Fri, 14 Sep 2018 13:03:29 +0000 (15:03 +0200)
Closes #2608

com.woltlab.wcf/mediaProvider.xml
wcfsetup/install/files/lib/system/bbcode/media/provider/YouTubeBBCodeMediaProvider.class.php

index 1186163035173446bc887fd8fc201fb1ffb6a11a..d1f4131039351514aa5b9155d90e02d61da12fbc 100644 (file)
@@ -3,7 +3,7 @@
        <import>
                <provider name="youtube">
                        <title>YouTube</title>
-                       <regex><![CDATA[https?://(?:.+?\.)?youtu(?:\.be/|be\.com/(?:#/)?watch\?(?:.*?&)?v=)(?P<ID>[a-zA-Z0-9_-]+)(?:(?:\?|&)t=(?P<start>[0-9hms]+)$)?]]></regex>
+                       <regex><![CDATA[https?://(?:.+?\.)?youtu(?:\.be/|be\.com/(?:#/)?watch\?(?:.*?&)?v=)(?P<ID>[a-zA-Z0-9_-]+)]]></regex>
                        <className><![CDATA[wcf\system\bbcode\media\provider\YouTubeBBCodeMediaProvider]]></className>
                </provider>
                
index 1dc106fb1b9b8509fb95a2f4e4efcc4866c656cb..03cc07e16e5692177d08c950e8281d5d5b9f6eab 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 namespace wcf\system\bbcode\media\provider;
+use wcf\util\Url;
 
 /**
  * Media provider callback for YouTube urls.
@@ -15,24 +16,40 @@ class YouTubeBBCodeMediaProvider implements IBBCodeMediaProvider {
         * @inheritDoc
         */
        public function parse($url, array $matches = []) {
-               $start = 0;
-               if (!empty($matches['start'])) {
-                       if (preg_match('~^(?:(?:(?P<h>\d+)h)?(?P<m>\d+)m(?P<s>\d+))|(?P<t>\d+)~', $matches['start'], $match)) {
-                               if (!empty($match['h'])) {
-                                       $start += intval($match['h']) * 3600;
-                               }
-                               if (!empty($match['m'])) {
-                                       $start += intval($match['m']) * 60;
-                               }
-                               if (!empty($match['s'])) {
-                                       $start += intval($match['s']);
-                               }
-                               if (!empty($match['t'])) {
-                                       $start += intval($match['t']);
-                               }
+               $parsedUrl = Url::parse($url);
+               parse_str($parsedUrl['query'], $queryString);
+               $startParameter = $queryString['t'] ?? $queryString['time_continue'] ?? $queryString['start'] ?? '';
+               $endParameter = $queryString['end'] ?? '';
+
+               $start = $this->timeToSeconds($startParameter);
+               $end = $this->timeToSeconds($endParameter);
+               
+               return '<div class="videoContainer"><iframe src="https://www.youtube-nocookie.com/embed/' . $matches['ID'] . '?wmode=transparent' . ($start ? '&amp;start='.$start : '') . ($end ? '&amp;end='.$end : '') . '&amp;rel=0" allowfullscreen></iframe></div>';
+       }
+       
+       /**
+        * Converts the given time parameter into seconds.
+        * 
+        * @param       string  $time
+        * @return      int
+        */
+       protected function timeToSeconds($time) {
+               $result = 0;
+               if (preg_match('~^(?:(?:(?P<h>\d+)h)?(?P<m>\d+)m(?P<s>\d+))|(?P<t>\d+)~', $time, $match)) {
+                       if (!empty($match['h'])) {
+                               $result += intval($match['h']) * 3600;
+                       }
+                       if (!empty($match['m'])) {
+                               $result += intval($match['m']) * 60;
+                       }
+                       if (!empty($match['s'])) {
+                               $result += intval($match['s']);
+                       }
+                       if (!empty($match['t'])) {
+                               $result += intval($match['t']);
                        }
                }
                
-               return '<div class="videoContainer"><iframe src="https://www.youtube-nocookie.com/embed/' . $matches['ID'] . '?wmode=transparent' . ($start ? '&amp;start='.$start : '') . '&amp;rel=0" allowfullscreen></iframe></div>';
+               return $result;
        }
 }