forked from kaltura/mwEmbed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mwEmbedLoader.php
504 lines (459 loc) · 17.9 KB
/
mwEmbedLoader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
<?php
// Include configuration
require_once( realpath( dirname( __FILE__ ) ) . '/includes/DefaultSettings.php' );
require_once( realpath( dirname( __FILE__ ) ) . '/modules/KalturaSupport/KalturaCommon.php' );
// only include the iframe if we need to:
// Include MwEmbedWebStartSetup.php for all of mediawiki support
if( isset( $_GET['autoembed'] ) ){
$externalPlayersPath = realpath( dirname( __FILE__ ) ) . '/modules/ExternalPlayers/ExternalPlayers.json';
$plugins = json_decode( file_get_contents($externalPlayersPath ), TRUE );
require ( dirname( __FILE__ ) . '/includes/MwEmbedWebStartSetup.php' );
require_once( realpath( dirname( __FILE__ ) ) . '/modules/KalturaSupport/kalturaIframeClass.php' );
}
// Check for custom resource ps config file:
if( isset( $wgKalturaPSHtml5SettingsPath ) && is_file( $wgKalturaPSHtml5SettingsPath ) ){
require_once( $wgKalturaPSHtml5SettingsPath );
}
$mwEmbedLoader = new mwEmbedLoader();
$mwEmbedLoader->output();
class mwEmbedLoader {
var $uiconfObject = null; // lazy init
var $error = false;
var $request = null;
var $utility = null;
var $iframeHeaders = null;
var $eTagHash = null;
var $loaderFileList = array(
// Get main kWidget resource:
'kWidget/kWidget.js',
// Include json2 for old browsers that don't have JSON.stringify
'resources/json/json2.js',
// By default include deprecated globals ( will be deprecated in 1.8 )
'kWidget/kWidget.deprecatedGlobals.js',
// Get resource ( domReady.js )
'kWidget/kWidget.domReady.js',
// Get resource ( mwEmbedLoader.js )
'kWidget/mwEmbedLoader.js',
// Include checkUserAgentPlayer code
'kWidget/kWidget.checkUserAgentPlayerRules.js',
// Get kWidget utilities:
'kWidget/kWidget.util.js',
// kWidget basic api wrapper
'resources/crypto/MD5.js',
'kWidget/kWidget.storage.js',
'kWidget/kWidget.api.js'
);
function request() {
if( ! $this->request ) {
global $container;
$this->request = $container['request_helper'];
}
return $this->request;
}
function utility() {
if( ! $this->utility ) {
global $container;
$this->utility = $container['utility_helper'];
}
return $this->utility;
}
function getOutputHash( $o ){
if( !$this->eTagHash ){
$this->eTagHash = md5( $o );
}
return $this->eTagHash;
}
function output(){
global $wgEnableScriptDebug;
// Get the comment never minfied
$o = $this->getLoaderHeader();
// Check for special incloader flag to ~not~ include the loader.
if( ! isset( $_GET['incloader'] )
||
$_GET['incloader'] != 'false'
){
$o.= $this->getLoaderPayload();
}
// Once setup is complete run any embed param calls is set
if( isset( $_GET['autoembed'] ) ){
$o.= $this->getAutoEmbedCode();
}
// Support Etag and 304
if( $wgEnableScriptDebug == false && @trim($_SERVER['HTTP_IF_NONE_MATCH']) == $this->getOutputHash( $o ) ){
header("HTTP/1.1 304 Not Modified");
exit();
}
// send cache headers
$this->sendHeaders( $o );
// start gzip handler if possible:
if(!ob_start("ob_gzhandler")) ob_start();
// check for non-fatal errors:
if( $this->getError() ){
echo "if( console ){ console.log('" . json_encode($this->getError()) . "'); }";
}
// output the script output
echo $o;
}
private function getAutoEmbedCode(){
$o='';
// Get the kWidget call ( pass along iframe payload path )
// Check required params:
$wid = $this->request()->get('wid');
if( !$wid ){
$this->setError( "missing wid param");
return '';
}
$wid = htmlspecialchars( $wid );
$uiconf_id = $this->request()->get('uiconf_id');
if( !$uiconf_id ){
$this->setError( "missing uiconf_id param");
return '';
}
$uiconf_id = htmlspecialchars( $uiconf_id );
$playerId = $this->request()->get('playerId');
if( !$playerId ){
$this->setError( "missing playerId param");
return '';
}
// Check optional params
$width = ( $this->request()->get('width') )? htmlspecialchars( $this->request()->get('width') ): 400;
$height = ( $this->request()->get('height') )? htmlspecialchars( $this->request()->get('height') ): 330;
// Get the iframe payload
$kIframe = new kalturaIframeClass();
$this->iframeHeaders = $kIframe->getHeaders();
// get the kIframe
$json = array(
'content' => $kIframe->getIFramePageOutput()
);
$o.="kWidget.iframeAutoEmbedCache[ '{$playerId}' ] = " . json_encode( $json ) . ";\n";
$o.="if(!document.getElementById('{$playerId}')) { document.write( '<div id=\"{$playerId}\" style=\"width:{$width}px;height:{$height}px\"></div>' ); } \n";
$o.="kWidget.embed( '{$playerId}', { \n" .
"\t'wid': '{$wid}', \n" .
"\t'uiconf_id' : '{$uiconf_id}'";
// conditionally add in the entry id: ( no entry id in playlists )
if( $this->request()->get('entry_id') && ! $this->getUiConfObject()->isPlaylist() ){
$o.=",\n\t'entry_id': '" . htmlspecialchars( $this->request()->get('entry_id') ) . "'";
}
$flashVars = $this->request()->getFlashVars();
//$o.=",\n\t'width': {$width},\n\t'height': {$height}";
// conditionally output flashvars:
if( $flashVars && is_array($flashVars) ){
$o.= ",\n\t'flashvars': {";
$coma = '';
foreach( $flashVars as $fvKey => $fvValue) {
$o.= $coma;
$coma = ',';
// check for json flavar and set acordingly
if( is_object( json_decode( html_entity_decode( $fvValue ) ) ) ){
$o.= "\n\t\t'{$fvKey}':";
$fvSet = json_decode( html_entity_decode( $fvValue ) );
$o.= json_encode( $fvSet );
} else {
$o.= "\"{$fvKey}\"" . ':' . json_encode( $this->utility()->formatString( $fvValue ) );
}
}
$o.='}';
}
$o.="\n});";
return $o;
}
private function getLoaderPayload(){
$o = '';
// get the main payload minfied if possible
if( $this->isDebugMode() ){
$o = $this->getCombinedLoaderJs();
$o.= $this->getExportedConfig();
// get any per uiConf js:
$o.= $this->getPerUiConfJS();
} else {
$o.= $this->getMinCombinedLoaderJs();
// don't compress config
$o.= $this->getExportedConfig();
// get any per uiConf js:
$o.= $this->getMinPerUiConfJS();
}
// After we load everything ( issue the kWidget.Setup call as the last line in the loader )
$o.="\nkWidget.setup();\n";
return $o;
}
private function setError( $errorMsg ){
$this->error = $errorMsg;
}
private function getError(){
return $this->error;
}
private function isDebugMode(){
global $wgEnableScriptDebug;
return isset( $_GET['debug'] ) || $wgEnableScriptDebug;
}
private function getCacheFileContents( $key ){
global $wgScriptCacheDirectory;
if( is_file( $this->getCacheFilePath( $key ) ) ){
return @file_get_contents( $this->getCacheFilePath( $key ) );
}
return false;
}
private function getCacheFilePath( $key ){
global $wgScriptCacheDirectory;
return $wgScriptCacheDirectory . '/' . substr( $key, 0, 2) . '/'. substr( $key, 2 );
}
private function getMinPerUiConfJS(){
global $wgResourceLoaderMinifierStatementsOnOwnLine;
// mwEmbedLoader based uiConf values can be hashed by the uiconf
$uiConfJs = $this->getPerUiConfJS();
if( $uiConfJs == '' ){
return '';
}
// Hash the javascript string to see if we we can use a cached version and avoid JavaScriptMinifier call
$key = md5( $uiConfJs );
$cacheJS = $this->getCacheFileContents( $key );
if( $cacheJS !== false ){
return $cacheJS;
}
//minfy js
require_once( realpath( dirname( __FILE__ ) ) . '/includes/libs/JavaScriptMinifier.php' );
$minjs = JavaScriptMinifier::minify( $uiConfJs, $wgResourceLoaderMinifierStatementsOnOwnLine );
// output minified cache:
$this->outputFileCache( $key, $minjs);
return $minjs;
}
/** gets any defiend on-page uiConf js */
private function getPerUiConfJS(){
if( ! $this->request()->get('uiconf_id')
||
!$this->getUiConfObject()
||
( ! $this->request()->get('wid')
&&
! $this->request()->get('p')
)
){
// directly issue the UiConfJs callback
return 'kWidget.inLoaderUiConfJsCallback();';
}
// load the onPage js services
$mweUiConfJs = new mweApiUiConfJs();
// output is set to empty string:
$o='';
// always include UserAgentPlayerRules:
$o.= $mweUiConfJs->getUserAgentPlayerRules();
// support including special player rewrite flags if set in uiConf:
if( $this->getUiConfObject()->getPlayerConfig( null, 'Kaltura.LeadWithHTML5' ) === true
||
$this->getUiConfObject()->getPlayerConfig( null, 'KalturaSupport.LeadWithHTML5' ) === true
){
$o.="\n"."kWidget.addUserAgentRule('{$this->request()->get('uiconf_id')}', '/.*/', 'leadWithHTML5');";
}
if( $this->getUiConfObject()->getPlayerConfig( null, 'Kaltura.ForceFlashOnIE10' ) === true ){
$o.="\n".'mw.setConfig(\'Kaltura.ForceFlashOnIE10\', true );' . "\n";
}
if( $this->getUiConfObject()->getPlayerConfig( null, 'Kaltura.SupressNonProductionUrlsWarning' ) === true ){
$o.="\n".'mw.setConfig(\'Kaltura.SupressNonProductionUrlsWarning\', true );' . "\n";
}
if( $this->getUiConfObject()->isJson() ) {
$o.="\n"."kWidget.addUserAgentRule('{$this->request()->get('uiconf_id')}', '/.*/', 'leadWithHTML5');";
}
// If we have entry data
if( $this->request()->get('entry_id') ){
global $container, $wgExternalPlayersSupportedTypes;
try{
$entryResult = $container['entry_result'];
$entry = $entryResult->getResult();
$metaData = @get_object_vars($entry['meta']);
if ( isset( $metaData[ "externalSourceType" ] ) ) {
if ( in_array( strtolower( $metaData[ "externalSourceType" ] ), array_map('strtolower', $wgExternalPlayersSupportedTypes) ) ) {
$o.="\n".'mw.setConfig(\'forceMobileHTML5\', true );'. "\n";
}
}
} catch ( Exception $e ){
//
}
}
// Only include on page plugins if not in iframe Server
if( !isset( $_REQUEST['iframeServer'] ) ){
$o.= $mweUiConfJs->getPluginPageJs( 'kWidget.inLoaderUiConfJsCallback' );
} else{
$o.='kWidget.inLoaderUiConfJsCallback();';
}
// set the flag so that we don't have to request the services.php
$o.= "\n" . 'kWidget.uiConfScriptLoadList[\'' .
$this->request()->get('uiconf_id') .
'\'] = 1; ';
return $o;
}
/**
* The result object grabber, caches a local result object for easy access
* to result object properties.
*/
function getUiConfObject(){
global $container;
if( ! $this->uiconfObject ){
try {
// Init a new result object with the client tag:
$this->uiconfObject = $container['uiconf_result'];
} catch ( Exception $e ){
$this->setError( $e->getMessage() );
// don't throw any exception just return false;
// any uiConf level exception should not block normal loader response
// the error details will be displayed in the player
return false;
}
}
return $this->uiconfObject;
}
private function getMinCombinedLoaderJs(){
global $wgHTTPProtocol, $wgMwEmbedVersion, $wgResourceLoaderMinifierStatementsOnOwnLine;
$key = '/loader_' . $wgHTTPProtocol . '.min.' . $wgMwEmbedVersion . '.js' ;
$cacheJS = $this->getCacheFileContents( $key );
if( $cacheJS !== false ){
return $cacheJS;
}
// Else get from files:
$rawScript = $this->getCombinedLoaderJs();
// Get the JSmin class:
require_once( realpath( dirname( __FILE__ ) ) . '/includes/libs/JavaScriptMinifier.php' );
$minjs = JavaScriptMinifier::minify( $rawScript, $wgResourceLoaderMinifierStatementsOnOwnLine );
// output the file to the cache:
$this->outputFileCache( $key, $minjs);
// return the minified js:
return $minjs;
}
function outputFileCache( $key, $js ){
$path = $this->getCacheFilePath( $key );
$pathAry = explode( "/", $path );
$pathDir = implode( '/', array_slice( $pathAry, 0, count( $pathAry ) - 1 ) );
// Create cache directory if not exists
if( ! is_dir( $pathDir ) ) {
$created = @mkdir( $pathDir, 0777, true );
if( ! $created ) {
$this->setError( 'Error in creating cache directory' );
return ;
}
}
// make sure the path exists
if( !@file_put_contents( $path, $js ) ){
$this->setError( 'Error outputting file to cache');
}
}
private function getCombinedLoaderJs(){
global $wgResourceLoaderUrl, $wgMwEmbedVersion;
$loaderJs = '';
// Output all the files
foreach( $this->loaderFileList as $file ){
$loaderJs .= file_get_contents( $file );
}
return $loaderJs;
}
private function getExportedConfig(){
global $wgEnableScriptDebug, $wgResourceLoaderUrl, $wgMwEmbedVersion, $wgMwEmbedProxyUrl, $wgKalturaUseManifestUrls,
$wgKalturaUseManifestUrls, $wgHTTPProtocol, $wgKalturaServiceUrl, $wgKalturaServiceBase,
$wgKalturaCDNUrl, $wgKalturaStatsServiceUrl,$wgKalturaLiveStatsServiceUrl, $wgKalturaAnalyticsServiceUrl, $wgKalturaIframeRewrite, $wgEnableIpadHTMLControls,
$wgKalturaAllowIframeRemoteService, $wgKalturaUseAppleAdaptive, $wgKalturaEnableEmbedUiConfJs,
$wgKalturaGoogleAnalyticsUA, $wgHTML5PsWebPath, $wgAllowedVars, $wgAllowedPluginVars, $wgAllowedPluginVarsValPartials, $wgAllowedVarsKeyPartials,
$wgCacheTTL, $wgMaxCacheEntries, $wgKalturaSupressNonProductionUrlsWarning;
$exportedJS ='';
// Set up globals to be exported as mwEmbed config:
$exportedJsConfig= array(
'debug' => $wgEnableScriptDebug,
// export the http url for the loader
'Mw.XmlProxyUrl' => $wgMwEmbedProxyUrl,
'Kaltura.UseManifestUrls' => $wgKalturaUseManifestUrls,
'Kaltura.Protocol' => $wgHTTPProtocol,
'Kaltura.ServiceUrl' => $wgKalturaServiceUrl,
'Kaltura.ServiceBase' => $wgKalturaServiceBase,
'Kaltura.CdnUrl' => $wgKalturaCDNUrl,
'Kaltura.StatsServiceUrl' => $wgKalturaStatsServiceUrl,
'Kaltura.LiveStatsServiceUrl'=>$wgKalturaLiveStatsServiceUrl,
'Kaltura.AnalyticsUrl'=>$wgKalturaAnalyticsServiceUrl,
'Kaltura.IframeRewrite' => $wgKalturaIframeRewrite,
'EmbedPlayer.EnableIpadHTMLControls' => $wgEnableIpadHTMLControls,
'EmbedPlayer.UseFlashOnAndroid' => true,
'Kaltura.LoadScriptForVideoTags' => true,
'Kaltura.AllowIframeRemoteService' => $wgKalturaAllowIframeRemoteService,
'Kaltura.UseAppleAdaptive' => $wgKalturaUseAppleAdaptive,
'Kaltura.EnableEmbedUiConfJs' => $wgKalturaEnableEmbedUiConfJs,
'Kaltura.PageGoogleAnalytics' => $wgKalturaGoogleAnalyticsUA,
'Kaltura.SupressNonProductionUrlsWarning' => $wgKalturaSupressNonProductionUrlsWarning,
'Kaltura.APITimeout' => 10000,
'Kaltura.kWidgetPsUrl' => $wgHTML5PsWebPath,
'Kaltura.CacheTTL' => $wgCacheTTL,
'Kaltura.MaxCacheEntries' => $wgMaxCacheEntries,
'Kaltura.AllowedVars' => $wgAllowedVars,
'Kaltura.AllowedVarsKeyPartials' => $wgAllowedVarsKeyPartials,
'Kaltura.AllowedPluginVars' => $wgAllowedPluginVars,
'Kaltura.AllowedPluginVarsValPartials' => $wgAllowedPluginVarsValPartials
);
if( isset( $_GET['pskwidgetpath'] ) ){
$exportedJsConfig[ 'Kaltura.KWidgetPsPath' ] = htmlspecialchars( $_GET['pskwidgetpath'] );
}
//For embed services pass "AllowIframeRemoteService" to client so it will be able to pass back the alternative service URL
if ($this->request()->isEmbedServicesEnabled()){
$exportedJsConfig['Kaltura.AllowIframeRemoteService'] = true;
}
// Append Custom config:
foreach( $exportedJsConfig as $key => $val ){
// @@TODO use a library Boolean conversion routine:
$val = ( $val === true )? $val = 'true' : $val;
$val = ( $val === false )? $val = 'false' : $val;
$val = ( $val != 'true' && $val != 'false' )? "'" . addslashes( $val ) . "'": $val;
$exportedJS .= "mw.setConfig('". addslashes( $key ). "', $val );\n";
}
// Add user language
$language = json_encode($this->utility()->getUserLanguage());
$exportedJS .= "mw.setConfig('Kaltura.UserLanguage', $language );\n";
return $exportedJS;
}
// Kaltura Comment
private function getLoaderHeader(){
global $wgMwEmbedVersion, $wgResourceLoaderUrl, $wgMwEmbedVersion;
$o = "/**
* Kaltura HTML5 Library v$wgMwEmbedVersion
* http://html5video.org/kaltura-player/docs/
*
* This is free software released under the GPL2 see README more info
* http://html5video.org/kaltura-player/docs/readme
*
* Copyright " . date("Y") . " Kaltura Inc.
*/\n";
// Add the library version:
$o .= "window['MWEMBED_VERSION'] = '$wgMwEmbedVersion';\n";
// Append ResourceLoder path to loader.js
$o.= "window['SCRIPT_LOADER_URL'] = '". addslashes( $wgResourceLoaderUrl ) . "';\n";
return $o;
}
/** send the cdn headers */
private function sendHeaders( $o ){
global $wgEnableScriptDebug;
header("Etag: " . $this->getOutputHash( $o ) );
header("Content-Type: text/javascript");
if( isset( $_GET['debug'] ) || $wgEnableScriptDebug ){
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Pragma: no-cache");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Access-Control-Allow-Origin: *"); // allow 3rd party domains to access.
} else if ( isset($_GET['autoembed']) && $this->iframeHeaders ){
// Grab iframe headers and pass them to our loader
foreach( $this->iframeHeaders as $header ) {
// Don't include iframe Etag
// ( use mwEmbedLoader Etag that includes loader in the overall hash )
if( strpos($header, 'Etag:') !== -1 ){
header( $header );
}
}
} else {
// Default expire time for the loader to 10 min ( we support 304 not modified so no need for long expire )
$max_age = 60*10;
// if the loader request includes uiConf set age to 10 min ( uiConf updates should propgate in ~10 min )
if( $this->request()->get('uiconf_id') ){
$max_age = 60*10;
}
// Check for an error ( only cache for 60 seconds )
if( $this->getError() ){
$max_age = 60;
}
header("Cache-Control: public, max-age=$max_age max-stale=0");
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $max_age) . ' GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
}
}
}