Debian Codegraph @

YouTube.php

raw symbols SCIP
1 <?php
2 /**
3 * Parser hook-based extension to show audio and video players
4 * from YouTube and other similar sites.
5 *
6 * @file
7 * @ingroup Extensions
8 * @author Przemek Piotrowski <[email protected]> for Wikia, Inc.
9 * @copyright © 2006-2008, Wikia Inc.
10 * @license GPL-2.0-or-later
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
25 * USA
26 *
27 * @todo one class (family) to rule 'em all
28 * @todo make width/height_max != width/height_default; aoaudio height may be large - long playlist
29 * @todo smart <video> and <audio> tag
30 */
31
32 class YouTube {
33
34 /**
35 * Register all the new tags with the Parser.
36 *
37 * @param Parser &$parser
38 */
39 public static function registerTags( &$parser ) {
40 $parser->setHook( 'youtube', [ __CLASS__, 'embedYouTube' ] );
41 $parser->setHook( 'aovideo', [ __CLASS__, 'embedArchiveOrgVideo' ] );
42 $parser->setHook( 'aoaudio', [ __CLASS__, 'embedArchiveOrgAudio' ] );
43 $parser->setHook( 'nicovideo', [ __CLASS__, 'embedNicovideo' ] );
44 }
45
46 /**
47 * Get the YouTube video ID from the supplied URL.
48 *
49 * @param string $url YouTube video URL
50 * @return string|bool Video ID on success, boolean false on failure
51 */
52 public static function url2ytid( $url ) {
53 // @see http://linuxpanda.wordpress.com/2013/07/24/ultimate-best-regex-pattern-to-get-grab-parse-youtube-video-id-from-any-youtube-link-url/
54 $pattern = '~(?:http|https|)(?::\/\/|)(?:www.|)(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/ytscreeningroom\?v=|\/feeds\/api\/videos\/|\/user\S*[^\w\-\s]|\S*[^\w\-\s]))([\w\-]{11})[a-z0-9;:@?&%=+\/\$_.-]*~i';
55 $id = false;
56
57 if ( preg_match( $pattern, $url, $preg ) ) {
58 $id = $preg[1];
59 } elseif ( preg_match( '/([0-9A-Za-z_-]+)/', $url, $preg ) ) {
60 $id = $preg[1];
61 }
62
63 return $id;
64 }
65
66 /**
67 * @param string $input
68 * @param array $argv
69 * @param Parser $parser
70 *
71 * @return string
72 */
73 public static function embedYouTube( $input, $argv, $parser ) {
74 global $wgYouTubeEnableLazyLoad;
75
76 // Loads necessary modules for lazy loading:
77 // Video poster image will be loaded first and replaced by the actual video once clicked
78 if ( $wgYouTubeEnableLazyLoad ) {
79 $parser->getOutput()->addModules( [ 'ext.youtube.lazyload' ] );
80 }
81
82 $ytid = '';
83 $width = $width_max = 425;
84 $height = $height_max = 355;
85
86 if ( !empty( $argv['ytid'] ) ) {
87 $ytid = self::url2ytid( $argv['ytid'] );
88 } elseif ( !empty( $input ) ) {
89 $ytid = self::url2ytid( $input );
90 }
91
92 // Did we not get an ID at all? That can happen if someone enters outright
93 // gibberish and/or something that's not a YouTube URL.
94 // Let's not even bother with generating useless HTML.
95 if ( $ytid === false ) {
96 return '';
97 }
98
99 // Support the pixel unit (px) in height/width parameters, because apparently
100 // a lot of people use it there.
101 // This way these parameters won't fail the filter_var() tests below if the
102 // user-supplied values were like 450px or 200px or something instead of
103 // 450 or 200
104 if ( !empty( $argv['height'] ) ) {
105 $argv['height'] = str_replace( 'px', '', $argv['height'] );
106 }
107
108 if ( !empty( $argv['width'] ) ) {
109 $argv['width'] = str_replace( 'px', '', $argv['width'] );
110 }
111
112 // Define urlArgs - container for every URL argument.
113 $urlArgs = [];
114
115 // Got a timestamp to start on? If yes, include it in URL.
116 if (
117 !empty( $argv['start'] ) &&
118 filter_var( $argv['start'], FILTER_VALIDATE_INT, [ 'options' => [ 'min_range' => 0 ] ] )
119 ) {
120 $urlArgs['start'] = $argv['start'];
121 }
122
123 // Adds ?autoplay=1 to the URL is the param is set
124 if ( !empty( $argv['autoplay'] ) || $wgYouTubeEnableLazyLoad ) {
125 $urlArgs['autoplay'] = '1';
126 }
127
128 // Go through all the potential URL arguments and get them into one string.
129 $argsStr = '';
130 if ( !empty( $urlArgs ) ) {
131 $argsStr = wfArrayToCgi( $urlArgs );
132 }
133
134 // If the type argument wasn't supplied, default to HTML5, since that's
135 // what YouTube offers by default as well
136 $width = 560;
137 $height = 315;
138 $maxWidth = 960;
139 $maxHeight = 720;
140
141 if (
142 !empty( $argv['width'] ) &&
143 filter_var( $argv['width'], FILTER_VALIDATE_INT, [ 'options' => [ 'min_range' => 0 ] ] ) &&
144 $argv['width'] <= $maxWidth
145 ) {
146 $width = $argv['width'];
147 }
148 if (
149 !empty( $argv['height'] ) &&
150 filter_var( $argv['height'], FILTER_VALIDATE_INT, [ 'options' => [ 'min_range' => 0 ] ] ) &&
151 $argv['height'] <= $maxHeight
152 ) {
153 $height = $argv['height'];
154 }
155
156 // Support YouTube's "enhanced privacy mode", in which "YouTube won’t
157 // store information about visitors on your web page unless they play
158 // the video" if the privacy argument was supplied
159 // @see https://support.google.com/youtube/answer/171780?expand=PrivacyEnhancedMode#privacy
160 $urlBase = '//www.youtube-nocookie.com/embed/';
161
162 if ( !empty( $ytid ) ) {
163 $url = $urlBase . $ytid . '?' . $argsStr;
164 $content = $iframe = "<iframe data-extension=\"youtube\" width=\"{$width}\" height=\"{$height}\" src=\"{$url}\" frameborder=\"0\" allowfullscreen></iframe>";
165 if ( $wgYouTubeEnableLazyLoad ) {
166 $img =
167 '<img width="' . $width . '" height="' . $height . '" src="'
168 . '//img.youtube.com/vi/' . $ytid . '/default.jpg" />';
169 $content =
170 '<div style="width: ' . $width . 'px; height:' . $height . 'px;"'
171 . 'class="ext-YouTube-video ext-YouTube-video--lazy" data-ytid="' . $ytid . '">'
172 . $img
173 . '<!-- ' . $iframe . ' -->'
174 . '</div>';
175 }
176 return $content;
177 }
178 }
179
180 public static function url2aovid( $url ) {
181 $id = $url;
182
183 if ( preg_match( '/http:\/\/www\.archive\.org\/download\/(.+)\.flv$/', $url, $preg ) ) {
184 $id = $preg[1];
185 }
186
187 preg_match( '/([0-9A-Za-z_\/.-]+)/', $id, $preg );
188 $id = $preg[1];
189
190 return $id;
191 }
192
193 public static function embedArchiveOrgVideo( $input, $argv, $parser ) {
194 $aovid = '';
195 $width = $width_max = 320;
196 $height = $height_max = 263;
197
198 if ( !empty( $argv['aovid'] ) ) {
199 $aovid = self::url2aovid( $argv['aovid'] );
200 } elseif ( !empty( $input ) ) {
201 $aovid = self::url2aovid( $input );
202 }
203 if (
204 !empty( $argv['width'] ) &&
205 settype( $argv['width'], 'integer' ) &&
206 ( $width_max >= $argv['width'] )
207 ) {
208 $width = $argv['width'];
209 }
210 if (
211 !empty( $argv['height'] ) &&
212 settype( $argv['height'], 'integer' ) &&
213 ( $height_max >= $argv['height'] )
214 ) {
215 $height = $argv['height'];
216 }
217
218 if ( !empty( $aovid ) ) {
219 $url = "http://www.archive.org/download/{$aovid}.flv";
220 return "<object type=\"application/x-shockwave-flash\" data=\"http://www.archive.org/flv/FlowPlayerWhite.swf\" width=\"{$width}\" height=\"{$height}\"><param name=\"movie\" value=\"http://www.archive.org/flv/FlowPlayerWhite.swf\"/><param name=\"flashvars\" value=\"config={loop: false, videoFile: '{$url}', autoPlay: false}\"/></object>";
221 }
222 }
223
224 public static function url2aoaid( $url ) {
225 $id = $url;
226
227 if ( preg_match( '/http:\/\/www\.archive\.org\/details\/(.+)$/', $url, $preg ) ) {
228 $id = $preg[1];
229 }
230
231 preg_match( '/([0-9A-Za-z_\/.-]+)/', $id, $preg );
232 $id = $preg[1];
233
234 return $id;
235 }
236
237 public static function embedArchiveOrgAudio( $input, $argv, $parser ) {
238 $aoaid = '';
239 $width = $width_max = 500;
240 $height = $height_max = 190;
241
242 if ( !empty( $argv['aoaid'] ) ) {
243 $aoaid = self::url2aoaid( $argv['aoaid'] );
244 } elseif ( !empty( $input ) ) {
245 $aoaid = self::url2aoaid( $input );
246 }
247 if (
248 !empty( $argv['width'] ) &&
249 settype( $argv['width'], 'integer' ) &&
250 ( $width_max >= $argv['width'] )
251 ) {
252 $width = $argv['width'];
253 }
254 if (
255 !empty( $argv['height'] ) &&
256 settype( $argv['height'], 'integer' ) &&
257 ( $height_max >= $argv['height'] )
258 ) {
259 $height = $argv['height'];
260 }
261
262 if ( !empty( $aoaid ) ) {
263 $uri = "https://archive.org/embed/$aoaid";
264 if ( !empty( $argv['playlist'] ) ) {
265 $uri .= "&playlist=" . (bool)$argv['playlist'];
266 }
267 return "<iframe data-extension=\"youtube\" src=\"$uri\" width=\"$width\" height=\"$height\" frameborder=\"0\" webkitallowfullscreen=\"true\" mozallowfullscreen=\"true\" allowfullscreen></iframe>";
268 }
269 }
270
271 public static function url2nvid( $url ) {
272 $id = $url;
273
274 preg_match( '/([0-9A-Za-z]+)/', $id, $preg );
275 $id = $preg[1];
276
277 return $id;
278 }
279
280 public static function embedNicovideo( $input, $argv, $parser ) {
281 $nvid = '';
282 $width = 400;
283 $height = 326;
284
285 if ( !empty( $argv['nvid'] ) ) {
286 $nvid = self::url2nvid( $argv['nvid'] );
287 } elseif ( !empty( $input ) ) {
288 $nvid = self::url2nvid( $input );
289 }
290 if (
291 !empty( $argv['width'] ) &&
292 settype( $argv['width'], 'integer' )
293 ) {
294 $width = $argv['width'];
295 }
296 if (
297 !empty( $argv['height'] ) &&
298 settype( $argv['height'], 'integer' )
299 ) {
300 $height = $argv['height'];
301 }
302
303 if ( !empty( $nvid ) ) {
304 $url = "https://embed.nicovideo.jp/watch/{$nvid}";
305 return "<iframe data-extension=\"youtube\" width=\"{$width}\" height=\"{$height}\" src=\"{$url}\"></iframe>";
306 }
307 }
308
309 }