Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
/**
* @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\IndieExternal;
use OCA\IndieExternal\Exceptions\GroupNotFoundException;
use OCA\IndieExternal\Exceptions\IconNotFoundException;
use OCA\IndieExternal\Exceptions\InvalidDeviceException;
use OCA\IndieExternal\Exceptions\InvalidNameException;
use OCA\IndieExternal\Exceptions\InvalidTypeException;
use OCA\IndieExternal\Exceptions\InvalidURLException;
use OCA\IndieExternal\Exceptions\LanguageNotFoundException;
use OCA\IndieExternal\Exceptions\SiteNotFoundException;
use OCP\App\IAppManager;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserSession;
use OCP\L10N\IFactory;
class SitesManager {
const TYPE_LINK = 'link';
const TYPE_SETTING = 'settings';
const TYPE_LOGIN = 'guest';
const TYPE_QUOTA = 'quota';
const DEVICE_ALL = '';
const DEVICE_ANDROID = 'android';
const DEVICE_IOS = 'ios';
const DEVICE_DESKTOP = 'desktop';
const DEVICE_BROWSER = 'browser';
/** @var IRequest */
protected $request;
/** @var IConfig */
protected $config;
/** @var IFactory */
protected $languageFactory;
/** @var IAppManager */
protected $appManager;
/** @var IGroupManager */
protected $groupManager;
/** @var IUserSession */
protected $userSession;
/** @var IAppData */
protected $appData;
public function __construct(IRequest $request,
IConfig $config,
IAppManager $appManager,
IGroupManager $groupManager,
IUserSession $userSession,
IFactory $languageFactory,
IAppData $appData) {
$this->request = $request;
$this->config = $config;
$this->appManager = $appManager;
$this->groupManager = $groupManager;
$this->userSession = $userSession;
$this->languageFactory = $languageFactory;
$this->appData = $appData;
}
/**
* @param int $id
* @return array
* @throws SiteNotFoundException
*/
public function getSiteById($id) {
$sites = $this->getSites();
if (isset($sites[$id])) {
$site = $sites[$id];
$user = $this->userSession->getUser();
$email= $user instanceof IUser ? $user->getEMailAddress() : '';
$uid = $user instanceof IUser ? $user->getUID() : '';
$displayName = $user instanceof IUser ? $user->getDisplayName() : '';
$chat_url = getenv('CHAT_URL', true) ?: getenv('CHAT_URL');
$saml_idp_url = getenv('SAML_IDP_URL', true) ?: getenv('SAML_IDP_URL');
$saml_realm = getenv('SAML_REALM', true) ?: getenv('SAML_REALM');
$site['url'] = str_replace(['{email}', '{uid}', '{displayname}', '{chat_url}', '{saml_idp_url}', '{saml_realm}'], [$email, $uid, $displayName, $chat_url, $saml_idp_url, $saml_realm], $site['url']);
return $site;
}
throw new SiteNotFoundException();
}
/**
* @return array[]
*/
public function getSitesToDisplay() {
$sites = $this->getSites();
$lang = $this->languageFactory->findLanguage();
$device = $this->getDeviceFromUserAgent();
$user = $this->userSession->getUser();
if ($user instanceof IUser) {
$groups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
} else {
$groups = [];
}
$email= $user instanceof IUser ? $user->getEMailAddress() : '';
$uid = $user instanceof IUser ? $user->getUID() : '';
$displayName = $user instanceof IUser ? $user->getDisplayName() : '';
$chat_url = getenv('CHAT_URL', true) ?: getenv('CHAT_URL');
$saml_idp_url = getenv('SAML_IDP_URL', true) ?: getenv('SAML_IDP_URL');
$saml_realm = getenv('SAML_REALM', true) ?: getenv('SAML_REALM');
$langSites = [];
foreach ($sites as $id => $site) {
if ($site['lang'] !== '' && $site['lang'] !== $lang) {
continue;
}
if ($site['device'] !== self::DEVICE_ALL && $site['device'] !== $device) {
continue;
}
if (!empty($site['groups']) && empty(array_intersect($site['groups'], $groups))) {
continue;
}
$site['url'] = str_replace(['{email}', '{uid}', '{displayname}', '{chat_url}', '{saml_idp_url}', '{saml_realm}'], [$email, $uid, $displayName, $chat_url, $saml_idp_url, $saml_realm], $site['url']);
$langSites[$id] = $site;
}
return $langSites;
}
/**
* @return array[]
*/
public function getSites() {
$jsonEncodedList = file_get_contents(__DIR__ . '/sites.json');
$sites = json_decode($jsonEncodedList, true);
$sites = array_map([$this, 'fillSiteArray'], $sites);
return $sites;
}
/**
* Adds default values for new attributes of sites
* @param array $site
* @return array
*/
protected function fillSiteArray(array $site) {
return array_merge([
'icon' => 'external.svg',
'lang' => '',
'type' => self::TYPE_LINK,
'device' => self::DEVICE_ALL,
'groups' => [],
'redirect' => false,
],
$site
);
}
/**
* @return string
*/
protected function getDeviceFromUserAgent() {
if ($this->request->isUserAgent([IRequest::USER_AGENT_CLIENT_ANDROID])) {
return self::DEVICE_ANDROID;
}
if ($this->request->isUserAgent([IRequest::USER_AGENT_CLIENT_IOS])) {
return self::DEVICE_IOS;
}
if ($this->request->isUserAgent([IRequest::USER_AGENT_CLIENT_DESKTOP])) {
return self::DEVICE_DESKTOP;
}
return self::DEVICE_BROWSER;
}
}