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