Passed
Push — master ( 2447d4...95d3a4 )
by Pauli
02:17
created

allowMediaSourcesInCsp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Morris Jobke <[email protected]>
10
 * @author Pauli Järvinen <[email protected]>
11
 * @copyright Morris Jobke 2013, 2014
12
 * @copyright Pauli Järvinen 2017 - 2020
13
 */
14
15
namespace OCA\Music\App;
16
17
$app = new Music();
18
19
$c = $app->getContainer();
20
$appName = $c->query('AppName');
21
22
/**
23
 * add navigation
24
 */
25
\OC::$server->getNavigationManager()->add(function () use ($c, $appName) {
26
	return [
27
		'id' => $appName,
28
		'order' => 10,
29
		'name' => $c->query('L10N')->t('Music'),
30
		'href' => $c->query('URLGenerator')->linkToRoute('music.page.index'),
31
		'icon' => \OCA\Music\Utility\HtmlUtil::getSvgPath('music')
32
	];
33
});
34
35
/**
36
 * register regular task
37
 */
38
\OC::$server->getJobList()->add('OC\BackgroundJob\Legacy\RegularJob', ['OCA\Music\Backgroundjob\CleanUp', 'run']);
39
40
/**
41
 * register hooks
42
 */
43
$c->query('FileHooks')->register();
44
$c->query('ShareHooks')->register();
45
$c->query('UserHooks')->register();
46
47
/**
48
 * register search provider
49
 */
50
$c->getServer()->getSearch()->registerProvider(
51
		'OCA\Music\Search\Provider',
52
		['app' => $appName, 'apps' => ['files']]
53
);
54
55
/**
56
 * Set default content security policy to allow loading media from data URL or any http(s) URL.
57
 */
58
function allowMediaSourcesInCsp() {
59
	$policy = new \OCP\AppFramework\Http\ContentSecurityPolicy();
60
	$policy->addAllowedMediaDomain('data:');
61
	$policy->addAllowedMediaDomain('http://*:*');
62
	$policy->addAllowedMediaDomain('https://*:*');
63
	\OC::$server->getContentSecurityPolicyManager()->addDefaultPolicy($policy);
64
}
65
66
/**
67
 * Load embedded music player for Files and Sharing apps
68
 *
69
 * The nice way to do this would be
70
 * \OC::$server->getEventDispatcher()->addListener('OCA\Files::loadAdditionalScripts', $loadEmbeddedMusicPlayer);
71
 * \OC::$server->getEventDispatcher()->addListener('OCA\Files_Sharing::loadAdditionalScripts', $loadEmbeddedMusicPlayer);
72
 * ... but this doesn't work for shared files on ownCloud 10.0, at least. Hence, we load the scripts
73
 * directly if the requested URL seems to be for Files or Sharing.
74
 */
75
function loadEmbeddedMusicPlayer() {
76
	\OCA\Music\Utility\HtmlUtil::addWebpackScript('files_music_player');
77
	\OCA\Music\Utility\HtmlUtil::addWebpackStyle('files_music_player');
78
}
79
80
$request = \OC::$server->getRequest();
81
if (isset($request->server['REQUEST_URI'])) {
82
	$url = $request->server['REQUEST_URI'];
83
	$url = \explode('?', $url)[0]; // get rid of any query args
84
	$isFilesUrl = \preg_match('%/apps/files(/.*)?%', $url);
85
	$isShareUrl = \preg_match('%/s/.+%', $url)
86
		&& !\preg_match('%/apps/.*%', $url)
87
		&& !\preg_match('%.*/authenticate%', $url);
88
	$isMusicUrl = \preg_match('%/apps/music(/.*)?%', $url);
89
90
	if ($isFilesUrl || $isShareUrl) {
91
		allowMediaSourcesInCsp();
92
		loadEmbeddedMusicPlayer();
93
	}
94
	elseif ($isMusicUrl) {
95
		allowMediaSourcesInCsp();
96
	}
97
}
98