Passed
Push — master ( 62f837...274910 )
by Pauli
13:24
created

isShareUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
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 - 2023
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
		$policy->addAllowedImageDomain($source); // for podcast images
76
	}
77
78
	// Also the media sources 'data:' and 'blob:' are needed for HLS streaming
79
	if ($enableHls) {
80
		$policy->addAllowedMediaDomain('data:');
81
		$policy->addAllowedMediaDomain('blob:');
82
	}
83
84
	$container->getServer()->getContentSecurityPolicyManager()->addDefaultPolicy($policy);
85
}
86
87
/**
88
 * Load embedded music player for Files and Sharing apps
89
 *
90
 * The nice way to do this would be
91
 * \OC::$server->getEventDispatcher()->addListener('OCA\Files::loadAdditionalScripts', $loadEmbeddedMusicPlayer);
92
 * \OC::$server->getEventDispatcher()->addListener('OCA\Files_Sharing::loadAdditionalScripts', $loadEmbeddedMusicPlayer);
93
 * ... but this doesn't work for shared files on ownCloud 10.0, at least. Hence, we load the scripts
94
 * directly if the requested URL seems to be for Files or Sharing.
95
 */
96
function loadEmbeddedMusicPlayer() {
97
	\OCA\Music\Utility\HtmlUtil::addWebpackScript('files_music_player');
98
	\OCA\Music\Utility\HtmlUtil::addWebpackStyle('files_music_player');
99
}
100
101
function isFilesUrl($url) {
102
	return \preg_match('%/apps/files/?$%', $url);
103
}
104
105
function isShareUrl($url) {
106
	return \preg_match('%/s/[^/]+$%', $url) && !\preg_match('%/apps/.*%', $url);
107
}
108
109
function isMusicUrl($url) {
110
	return \preg_match('%/apps/music/?$%', $url);
111
}
112
113
$request = \OC::$server->getRequest();
114
if (isset($request->server['REQUEST_URI'])) {
115
	$url = $request->server['REQUEST_URI'];
116
	$url = \explode('?', $url)[0]; // get rid of any query args
117
	$url = \explode('#', $url)[0]; // get rid of any hash part
118
119
	if (isFilesUrl($url) || isShareUrl($url)) {
120
		adjustCsp($c);
121
		loadEmbeddedMusicPlayer();
122
	} elseif (isMusicUrl($url)) {
123
		adjustCsp($c);
124
	}
125
}
126