1: <?php
2: /**
3: * This code is licensed under AGPLv3 license or Afterlogic Software License
4: * if commercial version of the product was purchased.
5: * For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
6: */
7:
8: namespace Aurora\Modules\BrandingWebclient;
9:
10: /**
11: * Adds ability to specify logos URL for login screen and main interface of application.
12: *
13: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
14: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
15: * @copyright Copyright (c) 2023, Afterlogic Corp.
16: *
17: * @property Settings $oModuleSettings
18: *
19: * @package Modules
20: */
21: class Module extends \Aurora\System\Module\AbstractWebclientModule
22: {
23: /***** private functions *****/
24: /**
25: * @return Module
26: */
27: public static function getInstance()
28: {
29: return parent::getInstance();
30: }
31:
32: /**
33: * @return Module
34: */
35: public static function Decorator()
36: {
37: return parent::Decorator();
38: }
39:
40: /**
41: * @return Settings
42: */
43: public function getModuleSettings()
44: {
45: return $this->oModuleSettings;
46: }
47:
48: private function _getUrlFromParts($aParts)
49: {
50: $sUrl = '';
51: if (!empty($aParts['scheme'])) {
52: $sUrl .= $aParts['scheme'] . ':';
53: }
54: if (!empty($aParts['user']) || !empty($aParts['host'])) {
55: $sUrl .= '//';
56: }
57: if (!empty($aParts['user'])) {
58: $sUrl .= $aParts['user'];
59: }
60: if (!empty($aParts['pass'])) {
61: $sUrl .= ':' . $aParts['pass'];
62: }
63: if (!empty($aParts['user'])) {
64: $sUrl .= '@';
65: }
66: if (!empty($aParts['host'])) {
67: $sUrl .= $aParts['host'];
68: }
69: if (!empty($aParts['port'])) {
70: $sUrl .= ':' . $aParts['port'];
71: }
72: if (!empty($aParts['path'])) {
73: $sUrl .= $aParts['path'];
74: }
75: if (!empty($aParts['query'])) {
76: if (is_array($aParts['query'])) {
77: $sUrl .= '?' . http_build_query($aParts['query']);
78: } else {
79: $sUrl .= '?' . $aParts['query'];
80: }
81: }
82: if (!empty($aParts['fragment'])) {
83: $sUrl .= '#' . $aParts['fragment'];
84: }
85:
86: return $sUrl;
87: }
88:
89: private function _getUrlWithSeed($sUrl)
90: {
91: $aParts = parse_url($sUrl);
92:
93: if (!empty($aParts['host']) || !empty($aParts['path'])) {
94: $aQuery = [];
95: if (isset($aParts['query']) && is_string($aParts['query']) && !empty($aParts['query'])) {
96: parse_str($aParts['query'], $aQuery);
97: }
98: $aQuery['datetimeseed'] = date('Y-m-d-H-i-s');
99: $aParts['query'] = http_build_query($aQuery);
100: }
101:
102: return $this->_getUrlFromParts($aParts);
103: }
104:
105: /***** private functions *****/
106:
107: /***** public functions might be called with web API *****/
108: /**
109: * Obtains list of module settings for authenticated user.
110: *
111: * @param int|null $TenantId Tenant ID
112: * @return array
113: */
114: public function GetSettings($TenantId = null)
115: {
116: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous);
117:
118: $sLoginLogo = '';
119: $sTabsbarLogo = '';
120:
121: if (!empty($TenantId)) {
122: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::TenantAdmin);
123: $oAuthenticatedUser = \Aurora\System\Api::getAuthenticatedUser();
124:
125: $oTenant = \Aurora\System\Api::getTenantById($TenantId);
126: if ($oTenant && ($oAuthenticatedUser->isAdmin() || $oAuthenticatedUser->IdTenant === $oTenant->Id)) {
127: $sLoginLogo = $this->oModuleSettings->GetTenantValue($oTenant->Name, 'LoginLogo', $sLoginLogo);
128: $sTabsbarLogo = $this->oModuleSettings->GetTenantValue($oTenant->Name, 'TabsbarLogo', $sTabsbarLogo);
129: }
130: } else {
131: $sLoginLogo = $this->oModuleSettings->LoginLogo;
132: $sTabsbarLogo = $this->oModuleSettings->TabsbarLogo;
133: }
134:
135: return array(
136: 'LoginLogo' => $sLoginLogo,
137: 'TabsbarLogo' => $sTabsbarLogo,
138: 'TopIframeUrl' => $this->_getUrlWithSeed($this->oModuleSettings->TopIframeUrl),
139: 'TopIframeHeightPx' => $this->oModuleSettings->TopIframeHeightPx,
140: );
141: }
142:
143: /**
144: * Updates module's settings - saves them to config.json file or to user settings in db.
145: *
146: * @param string $LoginLogo
147: * @param string $TabsbarLogo
148: * @param int $TenantId
149: * @return boolean
150: */
151: public function UpdateSettings($LoginLogo, $TabsbarLogo, $TenantId = null)
152: {
153: $result = false;
154: $oSettings = $this->oModuleSettings;
155: if (!empty($TenantId)) {
156: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::TenantAdmin);
157: $oTenant = \Aurora\System\Api::getTenantById($TenantId);
158: $oAuthenticatedUser = \Aurora\System\Api::getAuthenticatedUser();
159:
160: if ($oTenant && ($oAuthenticatedUser->isAdmin() || $oAuthenticatedUser->IdTenant === $oTenant->Id)) {
161: $result = $oSettings->SaveTenantSettings($oTenant->Name, [
162: 'LoginLogo' => $LoginLogo,
163: 'TabsbarLogo' => $TabsbarLogo
164: ]);
165: }
166: } else {
167: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::SuperAdmin);
168: $oSettings->LoginLogo = $LoginLogo;
169: $oSettings->TabsbarLogo = $TabsbarLogo;
170:
171: $result = $oSettings->Save();
172: }
173:
174: return $result;
175: }
176:
177: /***** public functions might be called with web API *****/
178: }
179: