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\Dav;
9:
10: use Aurora\Modules\Core\Module as CoreModule;
11: use Aurora\System\Api;
12: use Aurora\System\Application;
13:
14: /**
15: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
16: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
17: * @copyright Copyright (c) 2023, Afterlogic Corp.
18: *
19: * @property Module $oModule
20: */
21: class Manager extends \Aurora\System\Managers\AbstractManager
22: {
23: /**
24: * @var array
25: */
26: protected $aDavClients;
27:
28: /**
29: *
30: * @param \Aurora\System\Module\AbstractModule $oModule
31: */
32: public function __construct(\Aurora\System\Module\AbstractModule $oModule = null)
33: {
34: parent::__construct($oModule);
35: $this->aDavClients = array();
36: }
37:
38: /**
39: * @param \Aurora\System\Classes\Account $oAccount
40: * @return Client|false
41: */
42: public function &GetDAVClient($oAccount)
43: {
44: $mResult = false;
45:
46: if ($oAccount instanceof \Aurora\System\Classes\Account) {
47: $login = $oAccount->getLogin();
48: if (!isset($this->aDavClients[$login])) {
49: $this->aDavClients[$login] = new Client(
50: $this->getServerUrl(),
51: $login,
52: $oAccount->getPassword()
53: );
54: }
55:
56: if (isset($this->aDavClients[$login])) {
57: $mResult = &$this->aDavClients[$login];
58: }
59: }
60:
61: return $mResult;
62: }
63:
64: /**
65: * @return string
66: */
67: public function getServerUrl()
68: {
69: $sServerUrl = $this->oModule->oModuleSettings->ExternalHostNameOfDAVServer;
70: if (empty($sServerUrl)) {
71: $sServerUrl = Application::getBaseUrl() . 'dav.php/';
72: }
73:
74: return \rtrim($sServerUrl, '/') . '/';
75: }
76:
77: /**
78: * @return string
79: */
80: public function getServerHost()
81: {
82: $mResult = '';
83: $sServerUrl = $this->getServerUrl();
84: if (!empty($sServerUrl)) {
85: $aUrlParts = parse_url($sServerUrl);
86: if (!empty($aUrlParts['host'])) {
87: $mResult = $aUrlParts['host'];
88: }
89: }
90: return $mResult;
91: }
92:
93: /**
94: * @return bool
95: */
96: public function isSsl()
97: {
98: $bResult = false;
99: $sServerUrl = $this->getServerUrl();
100: if (!empty($sServerUrl)) {
101: $aUrlParts = parse_url($sServerUrl);
102: if (!empty($aUrlParts['port']) && $aUrlParts['port'] === 443) {
103: $bResult = true;
104: }
105: if (!empty($aUrlParts['scheme']) && $aUrlParts['scheme'] === 'https') {
106: $bResult = true;
107: }
108: }
109: return $bResult;
110: }
111:
112: /**
113: * @return int
114: */
115: public function getServerPort()
116: {
117: $iResult = 80;
118: if ($this->isSsl()) {
119: $iResult = 443;
120: }
121:
122: $sServerUrl = $this->getServerUrl();
123: if (!empty($sServerUrl)) {
124: $aUrlParts = parse_url($sServerUrl);
125: if (!empty($aUrlParts['port'])) {
126: $iResult = (int) $aUrlParts['port'];
127: }
128: }
129: return $iResult;
130: }
131:
132: /**
133: * @param int $iUserId
134: *
135: * @return string
136: */
137: public function getPrincipalUrl($iUserId)
138: {
139: $mResult = false;
140: try {
141: $sServerUrl = $this->getServerUrl();
142: if (!empty($sServerUrl)) {
143: $aUrlParts = parse_url($sServerUrl);
144: $sPort = $sPath = '';
145: if (!empty($aUrlParts['port']) && (int)$aUrlParts['port'] !== 80) {
146: $sPort = ':' . $aUrlParts['port'];
147: }
148: if (!empty($aUrlParts['path'])) {
149: $sPath = $aUrlParts['path'];
150: }
151:
152: if (!empty($aUrlParts['scheme']) && !empty($aUrlParts['host'])) {
153: $sServerUrl = $aUrlParts['scheme'] . '://' . $aUrlParts['host'] . $sPort;
154:
155: $mResult = $sServerUrl . \rtrim($sPath, '/') . '/' . \Afterlogic\DAV\Constants::PRINCIPALS_PREFIX . $iUserId;
156: }
157: }
158: } catch (\Exception $oException) {
159: $mResult = false;
160: $this->setLastException($oException);
161: }
162: return $mResult;
163: }
164:
165: /**
166: * @param int $iUserId
167: *
168: * @return string
169: */
170: public function getLogin($iUserId)
171: {
172: return $iUserId;
173: }
174:
175: /**
176: * @return bool
177: */
178: public function isMobileSyncEnabled()
179: {
180: $bResult = false;
181:
182: if (class_exists('\Aurora\Modules\MobileSync\Module')) {
183: $oMobileSyncModule = \Aurora\Modules\MobileSync\Module::getInstance();
184: $bResult = !$oMobileSyncModule->oModuleSettings->Disabled;
185: }
186:
187: return $bResult;
188: }
189:
190: /**
191: *
192: * @param bool $bMobileSyncEnable
193: *
194: * @return bool
195: */
196: public function setMobileSyncEnable($bMobileSyncEnable)
197: {
198: $oMobileSyncModule = \Aurora\System\Api::GetModule('MobileSync');
199: $oMobileSyncModule->setConfig('Disabled', !$bMobileSyncEnable);
200: return $oMobileSyncModule->saveModuleConfig();
201: }
202:
203: /**
204: * @param int $UserId
205: *
206: * @return bool
207: */
208: public function testConnection($UserId)
209: {
210: $bResult = false;
211:
212: $Login = Api::getUserPublicIdById($UserId);
213: if (!empty($Login)) {
214: $oAccount = CoreModule::Decorator()->GetAccountUsedToAuthorize($Login);
215: if ($oAccount) {
216: $oDav = &$this->GetDAVClient($oAccount);
217: if ($oDav && $oDav->Connect()) {
218: $bResult = true;
219: }
220: }
221: }
222:
223: return $bResult;
224: }
225:
226: /**
227: * @param int $UserId
228: */
229: public function deletePrincipal($UserId) {}
230:
231: /**
232: * @param string $sData
233: * @return \Sabre\VObject\Document
234: */
235: public function getVCardObject($sData)
236: {
237: return \Sabre\VObject\Reader::read($sData, \Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES);
238: }
239: }
240: