Passed
Push — master ( 49e88e...27428c )
by Pauli
02:49 queued 10s
created

adjustCsp()   B

Complexity

Conditions 8
Paths 36

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 17
c 0
b 0
f 0
nc 36
nop 1
dl 0
loc 31
rs 8.4444
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 - 2021
13
 */
14
15
namespace OCA\Music\App;
16
17
use \OCP\AppFramework\IAppContainer;
18
19
$app = new Music();
20
21
$c = $app->getContainer();
22
$appName = $c->query('AppName');
23
24
/**
25
 * add navigation
26
 */
27
\OC::$server->getNavigationManager()->add(function () use ($c, $appName) {
28
	return [
29
		'id' => $appName,
30
		'order' => 10,
31
		'name' => $c->query('L10N')->t('Music'),
32
		'href' => $c->query('URLGenerator')->linkToRoute('music.page.index'),
33
		'icon' => \OCA\Music\Utility\HtmlUtil::getSvgPath('music')
34
	];
35
});
36
37
/**
38
 * register regular task
39
 */
40
\OC::$server->getJobList()->add('OC\BackgroundJob\Legacy\RegularJob', ['OCA\Music\Backgroundjob\Cleanup', 'run']);
41
42
/**
43
 * register hooks
44
 */
45
$c->query('FileHooks')->register();
46
$c->query('ShareHooks')->register();
47
$c->query('UserHooks')->register();
48
49
/**
50
 * register search provider
51
 */
52
$c->getServer()->getSearch()->registerProvider(
53
		'OCA\Music\Search\Provider',
54
		['app' => $appName, 'apps' => ['files']]
55
);
56
57
/**
58
 * Set content security policy to allow streaming media from the configured external sources if we have a logged-in user
59
 */
60
function adjustCsp(IAppContainer $container) {
61
	/** @var \OCP\IConfig $config */
62
	$config = $container->query('Config');
63
	$radioSources = $config->getSystemValue('music.allowed_radio_src', ['http://*:*', 'https://*:*']);
64
	$radioHlsSources = $config->getSystemValue('music.allowed_radio_hls_src', []);
65
66
	if (\is_string($radioSources)) {
67
		$radioSources = [$radioSources];
68
	}
69
	if (\is_string($radioHlsSources)) {
70
		$radioHlsSources = [$radioHlsSources];
71
	}
72
73
	if (!empty($radioSources) || !empty($radioHlsSources)) {
74
		$policy = new \OCP\AppFramework\Http\ContentSecurityPolicy();
75
76
		foreach ($radioSources as $source) {
77
			$policy->addAllowedMediaDomain($source);
78
		}
79
80
		foreach ($radioHlsSources as $source) {
81
			$policy->addAllowedConnectDomain($source);
82
		}
83
84
		// Also the media sources data: and blob: are needed if there are any allowed HLS sources
85
		if (!empty($radioHlsSources)) {
86
			$policy->addAllowedMediaDomain('data:');
87
			$policy->addAllowedMediaDomain('blob:');
88
		}
89
90
		$container->getServer()->getContentSecurityPolicyManager()->addDefaultPolicy($policy);
91
	}
92
}
93
94
/**
95
 * Load embedded music player for Files and Sharing apps
96
 *
97
 * The nice way to do this would be
98
 * \OC::$server->getEventDispatcher()->addListener('OCA\Files::loadAdditionalScripts', $loadEmbeddedMusicPlayer);
99
 * \OC::$server->getEventDispatcher()->addListener('OCA\Files_Sharing::loadAdditionalScripts', $loadEmbeddedMusicPlayer);
100
 * ... but this doesn't work for shared files on ownCloud 10.0, at least. Hence, we load the scripts
101
 * directly if the requested URL seems to be for Files or Sharing.
102
 */
103
function loadEmbeddedMusicPlayer() {
104
	\OCA\Music\Utility\HtmlUtil::addWebpackScript('files_music_player');
105
	\OCA\Music\Utility\HtmlUtil::addWebpackStyle('files_music_player');
106
}
107
108
$request = \OC::$server->getRequest();
109
if (isset($request->server['REQUEST_URI'])) {
110
	$url = $request->server['REQUEST_URI'];
111
	$url = \explode('?', $url)[0]; // get rid of any query args
112
	$isFilesUrl = \preg_match('%/apps/files(/.*)?%', $url);
113
	$isShareUrl = \preg_match('%/s/.+%', $url)
114
		&& !\preg_match('%/apps/.*%', $url)
115
		&& !\preg_match('%.*/authenticate%', $url);
116
	$isMusicUrl = \preg_match('%/apps/music(/.*)?%', $url);
117
118
	if ($isFilesUrl) {
119
		adjustCsp($c);
120
		loadEmbeddedMusicPlayer();
121
	} elseif ($isShareUrl) {
122
		loadEmbeddedMusicPlayer();
123
	} elseif ($isMusicUrl) {
124
		adjustCsp($c);
125
	}
126
}
127