From: Tim Düsterhus Date: Fri, 14 Sep 2018 13:03:29 +0000 (+0200) Subject: Improve support for YouTube's time parameters in media provider X-Git-Tag: 5.2.0_Alpha_1~364^2~31 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=a0e8f9f47e709cc519285d882756c6d8c0da2c53;p=GitHub%2FWoltLab%2FWCF.git Improve support for YouTube's time parameters in media provider Closes #2608 --- diff --git a/com.woltlab.wcf/mediaProvider.xml b/com.woltlab.wcf/mediaProvider.xml index 1186163035..d1f4131039 100644 --- a/com.woltlab.wcf/mediaProvider.xml +++ b/com.woltlab.wcf/mediaProvider.xml @@ -3,7 +3,7 @@ YouTube - [a-zA-Z0-9_-]+)(?:(?:\?|&)t=(?P[0-9hms]+)$)?]]> + [a-zA-Z0-9_-]+)]]> diff --git a/wcfsetup/install/files/lib/system/bbcode/media/provider/YouTubeBBCodeMediaProvider.class.php b/wcfsetup/install/files/lib/system/bbcode/media/provider/YouTubeBBCodeMediaProvider.class.php index 1dc106fb1b..03cc07e16e 100644 --- a/wcfsetup/install/files/lib/system/bbcode/media/provider/YouTubeBBCodeMediaProvider.class.php +++ b/wcfsetup/install/files/lib/system/bbcode/media/provider/YouTubeBBCodeMediaProvider.class.php @@ -1,5 +1,6 @@ \d+)h)?(?P\d+)m(?P\d+))|(?P\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 '
'; + } + + /** + * Converts the given time parameter into seconds. + * + * @param string $time + * @return int + */ + protected function timeToSeconds($time) { + $result = 0; + if (preg_match('~^(?:(?:(?P\d+)h)?(?P\d+)m(?P\d+))|(?P\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 '
'; + return $result; } }