Passed
Push — master ( 598605...ab3682 )
by Pauli
02:53 queued 19s
created

adjustCsp()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
c 0
b 0
f 0
nc 8
nop 1
dl 0
loc 27
rs 9.7998
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;
0 ignored issues
show
Bug introduced by
The type \OCP\AppFramework\IAppContainer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
$app = \OC::$server->query(Music::class);
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 tasks
39
 */
40
\OC::$server->getJobList()->add('OC\BackgroundJob\Legacy\RegularJob', ['OCA\Music\Backgroundjob\Cleanup', 'run']);
41
\OC::$server->getJobList()->add('OC\BackgroundJob\Legacy\RegularJob', ['OCA\Music\Backgroundjob\PodcastUpdateCheck', 'run']);
42
43
/**
44
 * register hooks
45
 */
46
$c->query('FileHooks')->register();
47
$c->query('ShareHooks')->register();
48
$c->query('UserHooks')->register();
49
50
/**
51
 * register search provider
52
 */
53
$c->getServer()->getSearch()->registerProvider(
54
		'OCA\Music\Search\Provider',
55
		['app' => $appName, 'apps' => ['files']]
56
);
57
58
/**
59
 * Set content security policy to allow streaming media from the configured external sources
60
 */
61
function adjustCsp(IAppContainer $container) {
62
	/** @var \OCP\IConfig $config */
63
	$config = $container->query('Config');
64
	$radioSources = $config->getSystemValue('music.allowed_radio_src', ['http://*:*', 'https://*:*']);
65
	$enableHls = $config->getSystemValue('music.enable_radio_hls', true);
66
67
	if (\is_string($radioSources)) {
68
		$radioSources = [$radioSources];
69
	}
70
71
	$policy = new \OCP\AppFramework\Http\ContentSecurityPolicy();
72
73
	foreach ($radioSources as $source) {
74
		$policy->addAllowedMediaDomain($source);
75
	}
76
77
	// Also the media sources data: and blob: are needed for HLS streaming
78
	if ($enableHls) {
79
		$policy->addAllowedMediaDomain('data:');
80
		$policy->addAllowedMediaDomain('blob:');
81
	}
82
83
	// Allow loading (podcast cover) images from external sources
84
	$policy->addAllowedImageDomain('http://*:*');
85
	$policy->addAllowedImageDomain('https://*:*');
86
87
	$container->getServer()->getContentSecurityPolicyManager()->addDefaultPolicy($policy);
88
}
89
90
/**
91
 * Load embedded music player for Files and Sharing apps
92
 *
93
 * The nice way to do this would be
94
 * \OC::$server->getEventDispatcher()->addListener('OCA\Files::loadAdditionalScripts', $loadEmbeddedMusicPlayer);
95
 * \OC::$server->getEventDispatcher()->addListener('OCA\Files_Sharing::loadAdditionalScripts', $loadEmbeddedMusicPlayer);
96
 * ... but this doesn't work for shared files on ownCloud 10.0, at least. Hence, we load the scripts
97
 * directly if the requested URL seems to be for Files or Sharing.
98
 */
99
function loadEmbeddedMusicPlayer() {
100
	\OCA\Music\Utility\HtmlUtil::addWebpackScript('files_music_player');
101
	\OCA\Music\Utility\HtmlUtil::addWebpackStyle('files_music_player');
102
}
103
104
$request = \OC::$server->getRequest();
105
if (isset($request->server['REQUEST_URI'])) {
106
	$url = $request->server['REQUEST_URI'];
107
	$url = \explode('?', $url)[0]; // get rid of any query args
108
	$isFilesUrl = \preg_match('%/apps/files(/.*)?%', $url);
109
	$isShareUrl = \preg_match('%/s/.+%', $url)
110
		&& !\preg_match('%/apps/.*%', $url)
111
		&& !\preg_match('%.*/authenticate%', $url);
112
	$isMusicUrl = \preg_match('%/apps/music(/.*)?%', $url);
113
114
	if ($isFilesUrl) {
115
		adjustCsp($c);
116
		loadEmbeddedMusicPlayer();
117
	} elseif ($isShareUrl) {
118
		loadEmbeddedMusicPlayer();
119
	} elseif ($isMusicUrl) {
120
		adjustCsp($c);
121
	}
122
}
123