|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2018 Julius Härtl <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @author Julius Härtl <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @license GNU AGPL version 3 or any later version |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
12
|
|
|
* License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
namespace OC\Core\Controller; |
|
24
|
|
|
|
|
25
|
|
|
use OCP\AppFramework\Http\DataResponse; |
|
26
|
|
|
use OCP\AppFramework\OCSController; |
|
|
|
|
|
|
27
|
|
|
use OCP\INavigationManager; |
|
28
|
|
|
use OCP\IRequest; |
|
29
|
|
|
use OCP\IURLGenerator; |
|
30
|
|
|
|
|
31
|
|
|
class NavigationController extends OCSController { |
|
32
|
|
|
|
|
33
|
|
|
/** @var INavigationManager */ |
|
34
|
|
|
private $navigationManager; |
|
35
|
|
|
|
|
36
|
|
|
/** @var IURLGenerator */ |
|
37
|
|
|
private $urlGenerator; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct(string $appName, IRequest $request, INavigationManager $navigationManager, IURLGenerator $urlGenerator) { |
|
40
|
|
|
parent::__construct($appName, $request); |
|
41
|
|
|
$this->navigationManager = $navigationManager; |
|
42
|
|
|
$this->urlGenerator = $urlGenerator; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @NoAdminRequired |
|
47
|
|
|
* @NoCSRFRequired |
|
48
|
|
|
* |
|
49
|
|
|
* @param bool $absolute |
|
50
|
|
|
* @return DataResponse |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getAppsNavigation(bool $absolute = false): DataResponse { |
|
53
|
|
|
$navigation = $this->navigationManager->getAll(); |
|
54
|
|
|
if ($absolute) { |
|
55
|
|
|
$navigation = $this->rewriteToAbsoluteUrls($navigation); |
|
56
|
|
|
} |
|
57
|
|
|
return new DataResponse($navigation); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @NoAdminRequired |
|
62
|
|
|
* @NoCSRFRequired |
|
63
|
|
|
* |
|
64
|
|
|
* @param bool $absolute |
|
65
|
|
|
* @return DataResponse |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getSettingsNavigation(bool $absolute = false): DataResponse { |
|
68
|
|
|
$navigation = $this->navigationManager->getAll('settings'); |
|
69
|
|
|
if ($absolute) { |
|
70
|
|
|
$navigation = $this->rewriteToAbsoluteUrls($navigation); |
|
71
|
|
|
} |
|
72
|
|
|
return new DataResponse($navigation); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Rewrite href attribute of navigation entries to an absolute URL |
|
77
|
|
|
* |
|
78
|
|
|
* @param array $navigation |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
|
|
private function rewriteToAbsoluteUrls(array $navigation): array { |
|
82
|
|
|
foreach ($navigation as &$entry) { |
|
83
|
|
View Code Duplication |
if (0 !== strpos($entry['href'], $this->urlGenerator->getBaseUrl())) { |
|
84
|
|
|
$entry['href'] = $this->urlGenerator->getAbsoluteURL($entry['href']); |
|
85
|
|
|
} |
|
86
|
|
View Code Duplication |
if (0 !== strpos($entry['icon'], $this->urlGenerator->getBaseUrl())) { |
|
87
|
|
|
$entry['icon'] = $this->urlGenerator->getAbsoluteURL($entry['icon']); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
return $navigation; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: