-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathModule.php
438 lines (388 loc) · 16.2 KB
/
Module.php
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
<?php
/**
* This code is licensed under AGPLv3 license or Afterlogic Software License
* if commercial version of the product was purchased.
* For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
*/
namespace Aurora\Modules\CoreWebclient;
use Aurora\Api;
use Aurora\System\Application;
use Aurora\Modules\Core\Module as Core;
/**
* System module that provides Web application core functionality and UI framework.
*
* @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
* @license https://afterlogic.com/products/common-licensing Afterlogic Software License
* @copyright Copyright (c) 2023, Afterlogic Corp.
*
* @property Settings $oModuleSettings
*
* @package Modules
*/
class Module extends \Aurora\System\Module\AbstractWebclientModule
{
/**
* @return Module
*/
public static function getInstance()
{
return parent::getInstance();
}
/**
* @return Module
*/
public static function Decorator()
{
return parent::Decorator();
}
/**
* @return Settings
*/
public function getModuleSettings()
{
return $this->oModuleSettings;
}
/***** private functions *****/
/**
* Initializes CoreWebclient Module.
*
* @ignore
*/
public function init()
{
\Aurora\System\Router::getInstance()->registerArray(
self::GetName(),
[
'default' => [$this, 'EntryDefault'],
'error' => [$this, 'EntryDefault'],
'debugmode' => [$this, 'EntryDefault'],
'xdebug_session_start' => [$this, 'EntryDefault'],
'install' => [$this, 'EntryCompatibility'],
'sso' => [$this, 'EntrySso'],
'postlogin' => [$this, 'EntryPostlogin'],
]
);
$this->subscribeEvent('Core::UpdateSettings::after', array($this, 'onAfterUpdateSettings'));
$this->subscribeEvent('System::RunEntry::after', array($this, 'onAfterRunEntry'));
$this->denyMethodsCallByWebApi([
'SetHtmlOutputHeaders',
]);
}
/**
*
* @param array $aSystemList
* @return array
*/
private function getLanguageList($aSystemList)
{
$aResultList = [];
$aLanguageNames = $this->oModuleSettings->LanguageNames;
foreach ($aSystemList as $sLanguage) {
if (isset($aLanguageNames[$sLanguage])) {
$aResultList[] = [
'name' => json_decode('"' . $aLanguageNames[$sLanguage] . '"'),
'value' => $sLanguage
];
} else {
$aResultList[] = [
'name' => $sLanguage,
'value' => $sLanguage
];
}
}
return $aResultList;
}
/***** private functions *****/
/***** public functions *****/
/**
*
* @return array
*/
public function GetSettings()
{
\Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous);
$oUser = \Aurora\System\Api::getAuthenticatedUser();
$oIntegrator = Core::getInstance()->getIntegratorManager();
return array(
'AllowChangeSettings' => $this->oModuleSettings->AllowChangeSettings,
'AllowClientDebug' => $this->oModuleSettings->AllowClientDebug,
'AllowDesktopNotifications' => $oUser && null !== $oUser->getExtendedProp(self::GetName() . '::AllowDesktopNotifications') ? $oUser->getExtendedProp(self::GetName() . '::AllowDesktopNotifications') : $this->oModuleSettings->AllowDesktopNotifications,
'AllowMobile' => $this->oModuleSettings->AllowMobile,
'AllowPrefetch' => $this->oModuleSettings->AllowPrefetch,
'AttachmentSizeLimit' => $this->oModuleSettings->AttachmentSizeLimit,
'AutoRefreshIntervalMinutes' => $oUser && null !== $oUser->getExtendedProp(self::GetName() . '::AutoRefreshIntervalMinutes') ? $oUser->getExtendedProp(self::GetName() . '::AutoRefreshIntervalMinutes') : $this->oModuleSettings->AutoRefreshIntervalMinutes,
'CustomLogoutUrl' => $this->oModuleSettings->CustomLogoutUrl,
'DefaultAnonymScreenHash' => $this->oModuleSettings->DefaultAnonymScreenHash,
'DefaultUserScreenHash' => $this->oModuleSettings->DefaultUserScreenHash,
'GoogleAnalyticsAccount' => $this->oModuleSettings->GoogleAnalyticsAccount,
'HeaderModulesOrder' => $this->oModuleSettings->HeaderModulesOrder,
'IsDemo' => $this->oModuleSettings->IsDemo,
'IsMobile' => $oIntegrator->isMobile(),
'LanguageListWithNames' => $this->getLanguageList($oIntegrator->getLanguageList()),
'MultipleFilesUploadLimit' => $this->oModuleSettings->MultipleFilesUploadLimit,
'ShowQuotaBar' => $this->oModuleSettings->ShowQuotaBar,
'ShowQuotaBarTextAsTooltip' => $this->oModuleSettings->ShowQuotaBarTextAsTooltip,
'QuotaWarningPerc' => $this->oModuleSettings->QuotaWarningPerc,
'Theme' => $oUser && null !== $oUser->getExtendedProp(self::GetName() . '::Theme') ? $oUser->getExtendedProp(self::GetName() . '::Theme') : $this->oModuleSettings->Theme,
'ThemeList' => $this->oModuleSettings->ThemeList,
'HideLogout' => $this->oModuleSettings->HideLogout,
'BaseUrl' => Application::getBaseUrl(),
);
}
/**
*
* @param array $Args
* @param mixed $Result
*/
public function onAfterUpdateSettings($Args, &$Result)
{
\Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser);
$oUser = \Aurora\System\Api::getAuthenticatedUser();
if ($oUser && $oUser->isNormalOrTenant()) {
if (isset($Args['AllowDesktopNotifications'])) {
$oUser->setExtendedProp(self::GetName() . '::AllowDesktopNotifications', $Args['AllowDesktopNotifications']);
}
if (isset($Args['AutoRefreshIntervalMinutes'])) {
$oUser->setExtendedProp(self::GetName() . '::AutoRefreshIntervalMinutes', $Args['AutoRefreshIntervalMinutes']);
}
if (isset($Args['Theme'])) {
$oUser->setExtendedProp(self::GetName() . '::Theme', $Args['Theme']);
}
Core::Decorator()->UpdateUserObject($oUser);
}
if ($oUser && $oUser->Role === \Aurora\System\Enums\UserRole::SuperAdmin) {
if (isset($Args['Theme'])) {
$this->setConfig('Theme', $Args['Theme']);
}
$Result = $this->saveModuleConfig();
}
}
public function GetTemplates()
{
\Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous);
$oIntegrator = Core::getInstance()->getIntegratorManager();
return $oIntegrator->compileTemplates();
}
public function GetTranslation()
{
\Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous);
$oIntegrator = Core::getInstance()->getIntegratorManager();
list($sLanguage, $sTheme) = $oIntegrator->getThemeAndLanguage();
return $oIntegrator->getLanguage($sLanguage);
}
/**
* @ignore
*/
public function SetHtmlOutputHeaders()
{
@\header('Content-Type: text/html; charset=utf-8', true);
$sContentSecurityPolicy = $this->oModuleSettings->ContentSecurityPolicy;
if (!empty($sContentSecurityPolicy)) {
$aArgs = [];
$aAddDefault = [];
$this->broadcastEvent(
'AddToContentSecurityPolicyDefault',
$aArgs,
$aAddDefault
);
if (!empty($aAddDefault)) {
$aPieces = explode(';', $sContentSecurityPolicy);
foreach ($aPieces as $iIndex => $sPiece) {
$sPrepared = strtolower(trim($sPiece));
if (strpos($sPrepared, 'default-src') === 0) {
$aPieces[$iIndex] = implode(' ', array_merge([$sPiece], $aAddDefault));
}
}
$sContentSecurityPolicy = implode(';', $aPieces);
}
@\header('Content-Security-Policy: ' . $sContentSecurityPolicy, true);
}
}
/**
* @ignore
*/
public function EntryDefault()
{
$sResult = '';
$oIntegrator = \Aurora\System\Managers\Integrator::getInstance();
self::Decorator()->SetHtmlOutputHeaders();
$sUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
if (!\strpos(\strtolower($sUserAgent), 'firefox')) {
@\header('Last-Modified: ' . \gmdate('D, d M Y H:i:s') . ' GMT');
}
$oSettings = &\Aurora\System\Api::GetSettings();
if ($oSettings) {
if (($oSettings->CacheCtrl && isset($_COOKIE['aft-cache-ctrl']))) {
\Aurora\System\Api::setCookie(
'aft-cache-ctrl',
'',
\strtotime('-1 hour')
);
\MailSo\Base\Http::SingletonInstance()->StatusHeader(304);
exit();
}
}
$sResult = \file_get_contents($this->GetPath() . '/templates/Index.html');
if (\is_string($sResult)) {
if ($oSettings) {
$sFrameOptions = $oSettings->XFrameOptions;
if (0 < \strlen($sFrameOptions)) {
@\header('X-Frame-Options: ' . $sFrameOptions);
}
}
$sResult = strtr($sResult, array(
'{{IntegratorDir}}' => $oIntegrator->isRtl() ? 'rtl' : 'ltr',
'{{IntegratorLinks}}' => $oIntegrator->buildHeadersLink(),
'{{IntegratorBody}}' => $oIntegrator->buildBody()
));
}
return $sResult;
}
/**
* @ignore
*/
public function EntryCompatibility()
{
$mResult = '';
if (basename(\MailSo\Base\Http::SingletonInstance()->GetFullUrl()) !== 'adminpanel') { //TODO
\header("Location: ./");
}
$aCompatibilities = Core::Decorator()->GetCompatibilities();
$sContent = '';
$bResult = true;
foreach ($aCompatibilities as $sModule => $aItems) {
$sContent .= "<div class=\"row\">
<h2>Module: " . $sModule . "</h2>
</div><br />";
foreach ($aItems as $aItem) {
$sValue = '';
if ($aItem['Result']) {
$sValue = $this->getSuccessHtmlValue($aItem['Value']);
} else {
if (is_array($aItem['Value']) && count($aItem['Value']) > 0) {
$sValue = $this->getErrorHtmlValue($aItem['Value'][0], isset($aItem['Value'][1]) ? $aItem['Value'][1] : '');
}
}
$sContent .= "<div class=\"row\">
<span class=\"field_label\"><b>" . $aItem['Name'] . ":</b> </span>
<span class=\"field_value_limit\">" . $sValue . "</span>
</div>";
$bResult &= $aItem['Result'];
}
}
$sContent .= "<br />";
$sPath = $this->GetPath() . '/templates/Compatibility.html';
if (\file_exists($sPath)) {
$sResult = \file_get_contents($sPath);
if (\is_string($sResult)) {
$sResult = strtr($sResult, array(
'{{Compatibilities}}' => $sContent,
'{{Result}}' => $bResult ?
'The current server environment meets all the requirements. Click Next to proceed.' :
'Please make sure that all the requirements are met and click Retry.',
'{{NextButtonHref}}' => ($bResult) ? './' : './?install',
'{{ResultClassSuffix}}' => ($bResult) ? '_ok' : '_error',
'{{NextButtonName}}' => ($bResult) ? 'next_btn' : 'retry_btn',
'{{NextButtonValue}}' => ($bResult) ? 'Next' : 'Retry'
));
$mResult = $sResult;
}
}
return $mResult;
}
/**
* @ignore
*/
public function EntrySso()
{
try {
$sHash = $this->oHttp->GetRequest('hash');
if (!empty($sHash)) {
$sData = Api::Cacher()->get('SSO:' . $sHash, true);
$aData = Api::DecodeKeyValues($sData);
if (isset($aData['Password'], $aData['Email'])) {
$sLanguage = $this->oHttp->GetRequest('lang');
$oResult = Core::Decorator()->Login($aData['Email'], $aData['Password'], $sLanguage);
return \Aurora\System\Managers\Response::GetJsonFromObject('Json', ['Result' => $oResult]);
}
} else {
Core::Decorator()->Logout();
}
} catch (\Exception $oExc) {
Api::LogException($oExc);
}
}
/**
* @ignore
*/
public function EntryPostlogin()
{
if (Core::getInstance()->getConfig('AllowPostLogin')) {
$sEmail = trim((string) $this->oHttp->GetRequest('Email', ''));
$sLogin = (string) $this->oHttp->GetRequest('Login', '');
$sPassword = (string) $this->oHttp->GetRequest('Password', '');
if ($sLogin === '') {
$sLogin = $sEmail;
}
return \Aurora\System\Managers\Response::GetJsonFromObject('Json', ['Result' => Core::Decorator()->Login($sLogin, $sPassword)]);
}
}
protected function getSuccessHtmlValue($sValue)
{
return '<span class="state_ok">' . $sValue . '</span>';
}
protected function getErrorHtmlValue($sError, $sErrorHelp = '')
{
$sResult = '<span class="state_error">' . $sError . '</span>';
if (!empty($sErrorHelp)) {
$sResult .= '<span class="field_description">' . $sErrorHelp . '</span>';
}
return $sResult;
}
protected function getWarningHtmlValue($sVarning, $sVarningHelp = '')
{
$sResult = '<span class="state_warning"><img src="./images/alarm.png"> Not detected. <br />' . $sVarning . '</span>';
if (!empty($sVarningHelp)) {
$sResult .= '<span class="field_description">' . $sVarningHelp . '</span>';
}
return $sResult;
}
/**
* This subscription handles AuthToken cookie.
*/
public function onAfterRunEntry(&$aArgs, &$mResult)
{
$sXClientHeader = $this->oHttp->GetHeader('X-Client');
// Set cookie in browser only
$bWebClient = strtolower($sXClientHeader) === 'webclient';
if (($aArgs['EntryName'] === 'api' && $bWebClient) || in_array($aArgs['EntryName'], ['sso', 'postlogin'])) {
$sAuthTokenKey = Application::AUTH_TOKEN_KEY;
$oResult = @json_decode($mResult, true);
if ($oResult) {
if (isset($oResult['ErrorCode']) && in_array($oResult['ErrorCode'], [\Aurora\System\Notifications::AuthError, \Aurora\System\Notifications::InvalidToken])) {
Api::unsetAuthTokenCookie();
} elseif (isset($oResult['Result']) && isset($oResult['Result'][$sAuthTokenKey])) {
// Moving AuthToken to cookies
Api::setAuthTokenCookie($oResult['Result'][$sAuthTokenKey]);
$bChangeAuthToken = false;
$bRedirect = false;
if ($aArgs['EntryName'] === 'api') {
$bChangeAuthToken = true;
} elseif (in_array($aArgs['EntryName'], ['sso', 'postlogin'])) {
$bChangeAuthToken = true;
$bRedirect = true;
}
if ($bChangeAuthToken) {
$oResult['Result'][$sAuthTokenKey] = true;
$mResult = \Aurora\System\Managers\Response::GetJsonFromObject('Json', $oResult);
}
if ($bRedirect) {
Api::Location('./');
}
} elseif (isset($aArgs['Module']) && $aArgs['Module'] === 'Core' && $aArgs['Method'] === 'Logout' && $oResult['Result'] === true) {
Api::unsetAuthTokenCookie();
}
}
}
}
}