@@ -40,685 +40,685 @@ |
||
| 40 | 40 | |
| 41 | 41 | class KeyManager { |
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @var Session |
|
| 45 | - */ |
|
| 46 | - protected $session; |
|
| 47 | - /** |
|
| 48 | - * @var IStorage |
|
| 49 | - */ |
|
| 50 | - private $keyStorage; |
|
| 51 | - /** |
|
| 52 | - * @var Crypt |
|
| 53 | - */ |
|
| 54 | - private $crypt; |
|
| 55 | - /** |
|
| 56 | - * @var string |
|
| 57 | - */ |
|
| 58 | - private $recoveryKeyId; |
|
| 59 | - /** |
|
| 60 | - * @var string |
|
| 61 | - */ |
|
| 62 | - private $publicShareKeyId; |
|
| 63 | - /** |
|
| 64 | - * @var string |
|
| 65 | - */ |
|
| 66 | - private $masterKeyId; |
|
| 67 | - /** |
|
| 68 | - * @var string UserID |
|
| 69 | - */ |
|
| 70 | - private $keyId; |
|
| 71 | - /** |
|
| 72 | - * @var string |
|
| 73 | - */ |
|
| 74 | - private $publicKeyId = 'publicKey'; |
|
| 75 | - /** |
|
| 76 | - * @var string |
|
| 77 | - */ |
|
| 78 | - private $privateKeyId = 'privateKey'; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @var string |
|
| 82 | - */ |
|
| 83 | - private $shareKeyId = 'shareKey'; |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @var string |
|
| 87 | - */ |
|
| 88 | - private $fileKeyId = 'fileKey'; |
|
| 89 | - /** |
|
| 90 | - * @var IConfig |
|
| 91 | - */ |
|
| 92 | - private $config; |
|
| 93 | - /** |
|
| 94 | - * @var ILogger |
|
| 95 | - */ |
|
| 96 | - private $log; |
|
| 97 | - /** |
|
| 98 | - * @var Util |
|
| 99 | - */ |
|
| 100 | - private $util; |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * @param IStorage $keyStorage |
|
| 104 | - * @param Crypt $crypt |
|
| 105 | - * @param IConfig $config |
|
| 106 | - * @param IUserSession $userSession |
|
| 107 | - * @param Session $session |
|
| 108 | - * @param ILogger $log |
|
| 109 | - * @param Util $util |
|
| 110 | - */ |
|
| 111 | - public function __construct( |
|
| 112 | - IStorage $keyStorage, |
|
| 113 | - Crypt $crypt, |
|
| 114 | - IConfig $config, |
|
| 115 | - IUserSession $userSession, |
|
| 116 | - Session $session, |
|
| 117 | - ILogger $log, |
|
| 118 | - Util $util |
|
| 119 | - ) { |
|
| 120 | - |
|
| 121 | - $this->util = $util; |
|
| 122 | - $this->session = $session; |
|
| 123 | - $this->keyStorage = $keyStorage; |
|
| 124 | - $this->crypt = $crypt; |
|
| 125 | - $this->config = $config; |
|
| 126 | - $this->log = $log; |
|
| 127 | - |
|
| 128 | - $this->recoveryKeyId = $this->config->getAppValue('encryption', |
|
| 129 | - 'recoveryKeyId'); |
|
| 130 | - if (empty($this->recoveryKeyId)) { |
|
| 131 | - $this->recoveryKeyId = 'recoveryKey_' . substr(md5(time()), 0, 8); |
|
| 132 | - $this->config->setAppValue('encryption', |
|
| 133 | - 'recoveryKeyId', |
|
| 134 | - $this->recoveryKeyId); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - $this->publicShareKeyId = $this->config->getAppValue('encryption', |
|
| 138 | - 'publicShareKeyId'); |
|
| 139 | - if (empty($this->publicShareKeyId)) { |
|
| 140 | - $this->publicShareKeyId = 'pubShare_' . substr(md5(time()), 0, 8); |
|
| 141 | - $this->config->setAppValue('encryption', 'publicShareKeyId', $this->publicShareKeyId); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - $this->masterKeyId = $this->config->getAppValue('encryption', |
|
| 145 | - 'masterKeyId'); |
|
| 146 | - if (empty($this->masterKeyId)) { |
|
| 147 | - $this->masterKeyId = 'master_' . substr(md5(time()), 0, 8); |
|
| 148 | - $this->config->setAppValue('encryption', 'masterKeyId', $this->masterKeyId); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - $this->keyId = $userSession && $userSession->isLoggedIn() ? $userSession->getUser()->getUID() : false; |
|
| 152 | - $this->log = $log; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * check if key pair for public link shares exists, if not we create one |
|
| 157 | - */ |
|
| 158 | - public function validateShareKey() { |
|
| 159 | - $shareKey = $this->getPublicShareKey(); |
|
| 160 | - if (empty($shareKey)) { |
|
| 161 | - $keyPair = $this->crypt->createKeyPair(); |
|
| 162 | - |
|
| 163 | - // Save public key |
|
| 164 | - $this->keyStorage->setSystemUserKey( |
|
| 165 | - $this->publicShareKeyId . '.publicKey', $keyPair['publicKey'], |
|
| 166 | - Encryption::ID); |
|
| 167 | - |
|
| 168 | - // Encrypt private key empty passphrase |
|
| 169 | - $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], ''); |
|
| 170 | - $header = $this->crypt->generateHeader(); |
|
| 171 | - $this->setSystemPrivateKey($this->publicShareKeyId, $header . $encryptedKey); |
|
| 172 | - } |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * check if a key pair for the master key exists, if not we create one |
|
| 177 | - */ |
|
| 178 | - public function validateMasterKey() { |
|
| 179 | - |
|
| 180 | - if ($this->util->isMasterKeyEnabled() === false) { |
|
| 181 | - return; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - $publicMasterKey = $this->getPublicMasterKey(); |
|
| 185 | - if (empty($publicMasterKey)) { |
|
| 186 | - $keyPair = $this->crypt->createKeyPair(); |
|
| 187 | - |
|
| 188 | - // Save public key |
|
| 189 | - $this->keyStorage->setSystemUserKey( |
|
| 190 | - $this->masterKeyId . '.publicKey', $keyPair['publicKey'], |
|
| 191 | - Encryption::ID); |
|
| 192 | - |
|
| 193 | - // Encrypt private key with system password |
|
| 194 | - $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $this->getMasterKeyPassword(), $this->masterKeyId); |
|
| 195 | - $header = $this->crypt->generateHeader(); |
|
| 196 | - $this->setSystemPrivateKey($this->masterKeyId, $header . $encryptedKey); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - if (!$this->session->isPrivateKeySet()) { |
|
| 200 | - $masterKey = $this->getSystemPrivateKey($this->masterKeyId); |
|
| 201 | - $decryptedMasterKey = $this->crypt->decryptPrivateKey($masterKey, $this->getMasterKeyPassword(), $this->masterKeyId); |
|
| 202 | - $this->session->setPrivateKey($decryptedMasterKey); |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - // after the encryption key is available we are ready to go |
|
| 206 | - $this->session->setStatus(Session::INIT_SUCCESSFUL); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * @return bool |
|
| 211 | - */ |
|
| 212 | - public function recoveryKeyExists() { |
|
| 213 | - $key = $this->getRecoveryKey(); |
|
| 214 | - return !empty($key); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * get recovery key |
|
| 219 | - * |
|
| 220 | - * @return string |
|
| 221 | - */ |
|
| 222 | - public function getRecoveryKey() { |
|
| 223 | - return $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.publicKey', Encryption::ID); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * get recovery key ID |
|
| 228 | - * |
|
| 229 | - * @return string |
|
| 230 | - */ |
|
| 231 | - public function getRecoveryKeyId() { |
|
| 232 | - return $this->recoveryKeyId; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * @param string $password |
|
| 237 | - * @return bool |
|
| 238 | - */ |
|
| 239 | - public function checkRecoveryPassword($password) { |
|
| 240 | - $recoveryKey = $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.privateKey', Encryption::ID); |
|
| 241 | - $decryptedRecoveryKey = $this->crypt->decryptPrivateKey($recoveryKey, $password); |
|
| 242 | - |
|
| 243 | - if ($decryptedRecoveryKey) { |
|
| 244 | - return true; |
|
| 245 | - } |
|
| 246 | - return false; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * @param string $uid |
|
| 251 | - * @param string $password |
|
| 252 | - * @param string $keyPair |
|
| 253 | - * @return bool |
|
| 254 | - */ |
|
| 255 | - public function storeKeyPair($uid, $password, $keyPair) { |
|
| 256 | - // Save Public Key |
|
| 257 | - $this->setPublicKey($uid, $keyPair['publicKey']); |
|
| 258 | - |
|
| 259 | - $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $password, $uid); |
|
| 260 | - |
|
| 261 | - $header = $this->crypt->generateHeader(); |
|
| 262 | - |
|
| 263 | - if ($encryptedKey) { |
|
| 264 | - $this->setPrivateKey($uid, $header . $encryptedKey); |
|
| 265 | - return true; |
|
| 266 | - } |
|
| 267 | - return false; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * @param string $password |
|
| 272 | - * @param array $keyPair |
|
| 273 | - * @return bool |
|
| 274 | - */ |
|
| 275 | - public function setRecoveryKey($password, $keyPair) { |
|
| 276 | - // Save Public Key |
|
| 277 | - $this->keyStorage->setSystemUserKey($this->getRecoveryKeyId(). |
|
| 278 | - '.publicKey', |
|
| 279 | - $keyPair['publicKey'], |
|
| 280 | - Encryption::ID); |
|
| 281 | - |
|
| 282 | - $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $password); |
|
| 283 | - $header = $this->crypt->generateHeader(); |
|
| 284 | - |
|
| 285 | - if ($encryptedKey) { |
|
| 286 | - $this->setSystemPrivateKey($this->getRecoveryKeyId(), $header . $encryptedKey); |
|
| 287 | - return true; |
|
| 288 | - } |
|
| 289 | - return false; |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - /** |
|
| 293 | - * @param $userId |
|
| 294 | - * @param $key |
|
| 295 | - * @return bool |
|
| 296 | - */ |
|
| 297 | - public function setPublicKey($userId, $key) { |
|
| 298 | - return $this->keyStorage->setUserKey($userId, $this->publicKeyId, $key, Encryption::ID); |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * @param $userId |
|
| 303 | - * @param string $key |
|
| 304 | - * @return bool |
|
| 305 | - */ |
|
| 306 | - public function setPrivateKey($userId, $key) { |
|
| 307 | - return $this->keyStorage->setUserKey($userId, |
|
| 308 | - $this->privateKeyId, |
|
| 309 | - $key, |
|
| 310 | - Encryption::ID); |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * write file key to key storage |
|
| 315 | - * |
|
| 316 | - * @param string $path |
|
| 317 | - * @param string $key |
|
| 318 | - * @return boolean |
|
| 319 | - */ |
|
| 320 | - public function setFileKey($path, $key) { |
|
| 321 | - return $this->keyStorage->setFileKey($path, $this->fileKeyId, $key, Encryption::ID); |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - /** |
|
| 325 | - * set all file keys (the file key and the corresponding share keys) |
|
| 326 | - * |
|
| 327 | - * @param string $path |
|
| 328 | - * @param array $keys |
|
| 329 | - */ |
|
| 330 | - public function setAllFileKeys($path, $keys) { |
|
| 331 | - $this->setFileKey($path, $keys['data']); |
|
| 332 | - foreach ($keys['keys'] as $uid => $keyFile) { |
|
| 333 | - $this->setShareKey($path, $uid, $keyFile); |
|
| 334 | - } |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - /** |
|
| 338 | - * write share key to the key storage |
|
| 339 | - * |
|
| 340 | - * @param string $path |
|
| 341 | - * @param string $uid |
|
| 342 | - * @param string $key |
|
| 343 | - * @return boolean |
|
| 344 | - */ |
|
| 345 | - public function setShareKey($path, $uid, $key) { |
|
| 346 | - $keyId = $uid . '.' . $this->shareKeyId; |
|
| 347 | - return $this->keyStorage->setFileKey($path, $keyId, $key, Encryption::ID); |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - /** |
|
| 351 | - * Decrypt private key and store it |
|
| 352 | - * |
|
| 353 | - * @param string $uid user id |
|
| 354 | - * @param string $passPhrase users password |
|
| 355 | - * @return boolean |
|
| 356 | - */ |
|
| 357 | - public function init($uid, $passPhrase) { |
|
| 358 | - |
|
| 359 | - $this->session->setStatus(Session::INIT_EXECUTED); |
|
| 360 | - |
|
| 361 | - try { |
|
| 362 | - if($this->util->isMasterKeyEnabled()) { |
|
| 363 | - $uid = $this->getMasterKeyId(); |
|
| 364 | - $passPhrase = $this->getMasterKeyPassword(); |
|
| 365 | - $privateKey = $this->getSystemPrivateKey($uid); |
|
| 366 | - } else { |
|
| 367 | - $privateKey = $this->getPrivateKey($uid); |
|
| 368 | - } |
|
| 369 | - $privateKey = $this->crypt->decryptPrivateKey($privateKey, $passPhrase, $uid); |
|
| 370 | - } catch (PrivateKeyMissingException $e) { |
|
| 371 | - return false; |
|
| 372 | - } catch (DecryptionFailedException $e) { |
|
| 373 | - return false; |
|
| 374 | - } catch (\Exception $e) { |
|
| 375 | - $this->log->logException($e, [ |
|
| 376 | - 'message' => 'Could not decrypt the private key from user "' . $uid . '"" during login. Assume password change on the user back-end.', |
|
| 377 | - 'level' => \OCP\Util::WARN, |
|
| 378 | - 'app' => 'encryption', |
|
| 379 | - ]); |
|
| 380 | - return false; |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - if ($privateKey) { |
|
| 384 | - $this->session->setPrivateKey($privateKey); |
|
| 385 | - $this->session->setStatus(Session::INIT_SUCCESSFUL); |
|
| 386 | - return true; |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - return false; |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - /** |
|
| 393 | - * @param $userId |
|
| 394 | - * @return string |
|
| 395 | - * @throws PrivateKeyMissingException |
|
| 396 | - */ |
|
| 397 | - public function getPrivateKey($userId) { |
|
| 398 | - $privateKey = $this->keyStorage->getUserKey($userId, |
|
| 399 | - $this->privateKeyId, Encryption::ID); |
|
| 400 | - |
|
| 401 | - if (strlen($privateKey) !== 0) { |
|
| 402 | - return $privateKey; |
|
| 403 | - } |
|
| 404 | - throw new PrivateKeyMissingException($userId); |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - /** |
|
| 408 | - * @param string $path |
|
| 409 | - * @param $uid |
|
| 410 | - * @return string |
|
| 411 | - */ |
|
| 412 | - public function getFileKey($path, $uid) { |
|
| 413 | - if ($uid === '') { |
|
| 414 | - $uid = null; |
|
| 415 | - } |
|
| 416 | - $publicAccess = is_null($uid); |
|
| 417 | - $encryptedFileKey = $this->keyStorage->getFileKey($path, $this->fileKeyId, Encryption::ID); |
|
| 418 | - |
|
| 419 | - if (empty($encryptedFileKey)) { |
|
| 420 | - return ''; |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - if ($this->util->isMasterKeyEnabled()) { |
|
| 424 | - $uid = $this->getMasterKeyId(); |
|
| 425 | - $shareKey = $this->getShareKey($path, $uid); |
|
| 426 | - if ($publicAccess) { |
|
| 427 | - $privateKey = $this->getSystemPrivateKey($uid); |
|
| 428 | - $privateKey = $this->crypt->decryptPrivateKey($privateKey, $this->getMasterKeyPassword(), $uid); |
|
| 429 | - } else { |
|
| 430 | - // when logged in, the master key is already decrypted in the session |
|
| 431 | - $privateKey = $this->session->getPrivateKey(); |
|
| 432 | - } |
|
| 433 | - } else if ($publicAccess) { |
|
| 434 | - // use public share key for public links |
|
| 435 | - $uid = $this->getPublicShareKeyId(); |
|
| 436 | - $shareKey = $this->getShareKey($path, $uid); |
|
| 437 | - $privateKey = $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.privateKey', Encryption::ID); |
|
| 438 | - $privateKey = $this->crypt->decryptPrivateKey($privateKey); |
|
| 439 | - } else { |
|
| 440 | - $shareKey = $this->getShareKey($path, $uid); |
|
| 441 | - $privateKey = $this->session->getPrivateKey(); |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - if ($encryptedFileKey && $shareKey && $privateKey) { |
|
| 445 | - return $this->crypt->multiKeyDecrypt($encryptedFileKey, |
|
| 446 | - $shareKey, |
|
| 447 | - $privateKey); |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - return ''; |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - /** |
|
| 454 | - * Get the current version of a file |
|
| 455 | - * |
|
| 456 | - * @param string $path |
|
| 457 | - * @param View $view |
|
| 458 | - * @return int |
|
| 459 | - */ |
|
| 460 | - public function getVersion($path, View $view) { |
|
| 461 | - $fileInfo = $view->getFileInfo($path); |
|
| 462 | - if($fileInfo === false) { |
|
| 463 | - return 0; |
|
| 464 | - } |
|
| 465 | - return $fileInfo->getEncryptedVersion(); |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - /** |
|
| 469 | - * Set the current version of a file |
|
| 470 | - * |
|
| 471 | - * @param string $path |
|
| 472 | - * @param int $version |
|
| 473 | - * @param View $view |
|
| 474 | - */ |
|
| 475 | - public function setVersion($path, $version, View $view) { |
|
| 476 | - $fileInfo= $view->getFileInfo($path); |
|
| 477 | - |
|
| 478 | - if($fileInfo !== false) { |
|
| 479 | - $cache = $fileInfo->getStorage()->getCache(); |
|
| 480 | - $cache->update($fileInfo->getId(), ['encrypted' => $version, 'encryptedVersion' => $version]); |
|
| 481 | - } |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - /** |
|
| 485 | - * get the encrypted file key |
|
| 486 | - * |
|
| 487 | - * @param string $path |
|
| 488 | - * @return string |
|
| 489 | - */ |
|
| 490 | - public function getEncryptedFileKey($path) { |
|
| 491 | - $encryptedFileKey = $this->keyStorage->getFileKey($path, |
|
| 492 | - $this->fileKeyId, Encryption::ID); |
|
| 493 | - |
|
| 494 | - return $encryptedFileKey; |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - /** |
|
| 498 | - * delete share key |
|
| 499 | - * |
|
| 500 | - * @param string $path |
|
| 501 | - * @param string $keyId |
|
| 502 | - * @return boolean |
|
| 503 | - */ |
|
| 504 | - public function deleteShareKey($path, $keyId) { |
|
| 505 | - return $this->keyStorage->deleteFileKey( |
|
| 506 | - $path, |
|
| 507 | - $keyId . '.' . $this->shareKeyId, |
|
| 508 | - Encryption::ID); |
|
| 509 | - } |
|
| 510 | - |
|
| 511 | - |
|
| 512 | - /** |
|
| 513 | - * @param $path |
|
| 514 | - * @param $uid |
|
| 515 | - * @return mixed |
|
| 516 | - */ |
|
| 517 | - public function getShareKey($path, $uid) { |
|
| 518 | - $keyId = $uid . '.' . $this->shareKeyId; |
|
| 519 | - return $this->keyStorage->getFileKey($path, $keyId, Encryption::ID); |
|
| 520 | - } |
|
| 521 | - |
|
| 522 | - /** |
|
| 523 | - * check if user has a private and a public key |
|
| 524 | - * |
|
| 525 | - * @param string $userId |
|
| 526 | - * @return bool |
|
| 527 | - * @throws PrivateKeyMissingException |
|
| 528 | - * @throws PublicKeyMissingException |
|
| 529 | - */ |
|
| 530 | - public function userHasKeys($userId) { |
|
| 531 | - $privateKey = $publicKey = true; |
|
| 532 | - $exception = null; |
|
| 533 | - |
|
| 534 | - try { |
|
| 535 | - $this->getPrivateKey($userId); |
|
| 536 | - } catch (PrivateKeyMissingException $e) { |
|
| 537 | - $privateKey = false; |
|
| 538 | - $exception = $e; |
|
| 539 | - } |
|
| 540 | - try { |
|
| 541 | - $this->getPublicKey($userId); |
|
| 542 | - } catch (PublicKeyMissingException $e) { |
|
| 543 | - $publicKey = false; |
|
| 544 | - $exception = $e; |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - if ($privateKey && $publicKey) { |
|
| 548 | - return true; |
|
| 549 | - } elseif (!$privateKey && !$publicKey) { |
|
| 550 | - return false; |
|
| 551 | - } else { |
|
| 552 | - throw $exception; |
|
| 553 | - } |
|
| 554 | - } |
|
| 555 | - |
|
| 556 | - /** |
|
| 557 | - * @param $userId |
|
| 558 | - * @return mixed |
|
| 559 | - * @throws PublicKeyMissingException |
|
| 560 | - */ |
|
| 561 | - public function getPublicKey($userId) { |
|
| 562 | - $publicKey = $this->keyStorage->getUserKey($userId, $this->publicKeyId, Encryption::ID); |
|
| 563 | - |
|
| 564 | - if (strlen($publicKey) !== 0) { |
|
| 565 | - return $publicKey; |
|
| 566 | - } |
|
| 567 | - throw new PublicKeyMissingException($userId); |
|
| 568 | - } |
|
| 569 | - |
|
| 570 | - public function getPublicShareKeyId() { |
|
| 571 | - return $this->publicShareKeyId; |
|
| 572 | - } |
|
| 573 | - |
|
| 574 | - /** |
|
| 575 | - * get public key for public link shares |
|
| 576 | - * |
|
| 577 | - * @return string |
|
| 578 | - */ |
|
| 579 | - public function getPublicShareKey() { |
|
| 580 | - return $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.publicKey', Encryption::ID); |
|
| 581 | - } |
|
| 582 | - |
|
| 583 | - /** |
|
| 584 | - * @param string $purpose |
|
| 585 | - * @param string $uid |
|
| 586 | - */ |
|
| 587 | - public function backupUserKeys($purpose, $uid) { |
|
| 588 | - $this->keyStorage->backupUserKeys(Encryption::ID, $purpose, $uid); |
|
| 589 | - } |
|
| 590 | - |
|
| 591 | - /** |
|
| 592 | - * creat a backup of the users private and public key and then delete it |
|
| 593 | - * |
|
| 594 | - * @param string $uid |
|
| 595 | - */ |
|
| 596 | - public function deleteUserKeys($uid) { |
|
| 597 | - $this->deletePublicKey($uid); |
|
| 598 | - $this->deletePrivateKey($uid); |
|
| 599 | - } |
|
| 600 | - |
|
| 601 | - /** |
|
| 602 | - * @param $uid |
|
| 603 | - * @return bool |
|
| 604 | - */ |
|
| 605 | - public function deletePublicKey($uid) { |
|
| 606 | - return $this->keyStorage->deleteUserKey($uid, $this->publicKeyId, Encryption::ID); |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - /** |
|
| 610 | - * @param string $uid |
|
| 611 | - * @return bool |
|
| 612 | - */ |
|
| 613 | - private function deletePrivateKey($uid) { |
|
| 614 | - return $this->keyStorage->deleteUserKey($uid, $this->privateKeyId, Encryption::ID); |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - /** |
|
| 618 | - * @param string $path |
|
| 619 | - * @return bool |
|
| 620 | - */ |
|
| 621 | - public function deleteAllFileKeys($path) { |
|
| 622 | - return $this->keyStorage->deleteAllFileKeys($path); |
|
| 623 | - } |
|
| 624 | - |
|
| 625 | - /** |
|
| 626 | - * @param array $userIds |
|
| 627 | - * @return array |
|
| 628 | - * @throws PublicKeyMissingException |
|
| 629 | - */ |
|
| 630 | - public function getPublicKeys(array $userIds) { |
|
| 631 | - $keys = []; |
|
| 632 | - |
|
| 633 | - foreach ($userIds as $userId) { |
|
| 634 | - try { |
|
| 635 | - $keys[$userId] = $this->getPublicKey($userId); |
|
| 636 | - } catch (PublicKeyMissingException $e) { |
|
| 637 | - continue; |
|
| 638 | - } |
|
| 639 | - } |
|
| 640 | - |
|
| 641 | - return $keys; |
|
| 642 | - |
|
| 643 | - } |
|
| 644 | - |
|
| 645 | - /** |
|
| 646 | - * @param string $keyId |
|
| 647 | - * @return string returns openssl key |
|
| 648 | - */ |
|
| 649 | - public function getSystemPrivateKey($keyId) { |
|
| 650 | - return $this->keyStorage->getSystemUserKey($keyId . '.' . $this->privateKeyId, Encryption::ID); |
|
| 651 | - } |
|
| 652 | - |
|
| 653 | - /** |
|
| 654 | - * @param string $keyId |
|
| 655 | - * @param string $key |
|
| 656 | - * @return string returns openssl key |
|
| 657 | - */ |
|
| 658 | - public function setSystemPrivateKey($keyId, $key) { |
|
| 659 | - return $this->keyStorage->setSystemUserKey( |
|
| 660 | - $keyId . '.' . $this->privateKeyId, |
|
| 661 | - $key, |
|
| 662 | - Encryption::ID); |
|
| 663 | - } |
|
| 664 | - |
|
| 665 | - /** |
|
| 666 | - * add system keys such as the public share key and the recovery key |
|
| 667 | - * |
|
| 668 | - * @param array $accessList |
|
| 669 | - * @param array $publicKeys |
|
| 670 | - * @param string $uid |
|
| 671 | - * @return array |
|
| 672 | - * @throws PublicKeyMissingException |
|
| 673 | - */ |
|
| 674 | - public function addSystemKeys(array $accessList, array $publicKeys, $uid) { |
|
| 675 | - if (!empty($accessList['public'])) { |
|
| 676 | - $publicShareKey = $this->getPublicShareKey(); |
|
| 677 | - if (empty($publicShareKey)) { |
|
| 678 | - throw new PublicKeyMissingException($this->getPublicShareKeyId()); |
|
| 679 | - } |
|
| 680 | - $publicKeys[$this->getPublicShareKeyId()] = $publicShareKey; |
|
| 681 | - } |
|
| 682 | - |
|
| 683 | - if ($this->recoveryKeyExists() && |
|
| 684 | - $this->util->isRecoveryEnabledForUser($uid)) { |
|
| 685 | - |
|
| 686 | - $publicKeys[$this->getRecoveryKeyId()] = $this->getRecoveryKey(); |
|
| 687 | - } |
|
| 688 | - |
|
| 689 | - return $publicKeys; |
|
| 690 | - } |
|
| 691 | - |
|
| 692 | - /** |
|
| 693 | - * get master key password |
|
| 694 | - * |
|
| 695 | - * @return string |
|
| 696 | - * @throws \Exception |
|
| 697 | - */ |
|
| 698 | - public function getMasterKeyPassword() { |
|
| 699 | - $password = $this->config->getSystemValue('secret'); |
|
| 700 | - if (empty($password)){ |
|
| 701 | - throw new \Exception('Can not get secret from Nextcloud instance'); |
|
| 702 | - } |
|
| 703 | - |
|
| 704 | - return $password; |
|
| 705 | - } |
|
| 706 | - |
|
| 707 | - /** |
|
| 708 | - * return master key id |
|
| 709 | - * |
|
| 710 | - * @return string |
|
| 711 | - */ |
|
| 712 | - public function getMasterKeyId() { |
|
| 713 | - return $this->masterKeyId; |
|
| 714 | - } |
|
| 715 | - |
|
| 716 | - /** |
|
| 717 | - * get public master key |
|
| 718 | - * |
|
| 719 | - * @return string |
|
| 720 | - */ |
|
| 721 | - public function getPublicMasterKey() { |
|
| 722 | - return $this->keyStorage->getSystemUserKey($this->masterKeyId . '.publicKey', Encryption::ID); |
|
| 723 | - } |
|
| 43 | + /** |
|
| 44 | + * @var Session |
|
| 45 | + */ |
|
| 46 | + protected $session; |
|
| 47 | + /** |
|
| 48 | + * @var IStorage |
|
| 49 | + */ |
|
| 50 | + private $keyStorage; |
|
| 51 | + /** |
|
| 52 | + * @var Crypt |
|
| 53 | + */ |
|
| 54 | + private $crypt; |
|
| 55 | + /** |
|
| 56 | + * @var string |
|
| 57 | + */ |
|
| 58 | + private $recoveryKeyId; |
|
| 59 | + /** |
|
| 60 | + * @var string |
|
| 61 | + */ |
|
| 62 | + private $publicShareKeyId; |
|
| 63 | + /** |
|
| 64 | + * @var string |
|
| 65 | + */ |
|
| 66 | + private $masterKeyId; |
|
| 67 | + /** |
|
| 68 | + * @var string UserID |
|
| 69 | + */ |
|
| 70 | + private $keyId; |
|
| 71 | + /** |
|
| 72 | + * @var string |
|
| 73 | + */ |
|
| 74 | + private $publicKeyId = 'publicKey'; |
|
| 75 | + /** |
|
| 76 | + * @var string |
|
| 77 | + */ |
|
| 78 | + private $privateKeyId = 'privateKey'; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @var string |
|
| 82 | + */ |
|
| 83 | + private $shareKeyId = 'shareKey'; |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @var string |
|
| 87 | + */ |
|
| 88 | + private $fileKeyId = 'fileKey'; |
|
| 89 | + /** |
|
| 90 | + * @var IConfig |
|
| 91 | + */ |
|
| 92 | + private $config; |
|
| 93 | + /** |
|
| 94 | + * @var ILogger |
|
| 95 | + */ |
|
| 96 | + private $log; |
|
| 97 | + /** |
|
| 98 | + * @var Util |
|
| 99 | + */ |
|
| 100 | + private $util; |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * @param IStorage $keyStorage |
|
| 104 | + * @param Crypt $crypt |
|
| 105 | + * @param IConfig $config |
|
| 106 | + * @param IUserSession $userSession |
|
| 107 | + * @param Session $session |
|
| 108 | + * @param ILogger $log |
|
| 109 | + * @param Util $util |
|
| 110 | + */ |
|
| 111 | + public function __construct( |
|
| 112 | + IStorage $keyStorage, |
|
| 113 | + Crypt $crypt, |
|
| 114 | + IConfig $config, |
|
| 115 | + IUserSession $userSession, |
|
| 116 | + Session $session, |
|
| 117 | + ILogger $log, |
|
| 118 | + Util $util |
|
| 119 | + ) { |
|
| 120 | + |
|
| 121 | + $this->util = $util; |
|
| 122 | + $this->session = $session; |
|
| 123 | + $this->keyStorage = $keyStorage; |
|
| 124 | + $this->crypt = $crypt; |
|
| 125 | + $this->config = $config; |
|
| 126 | + $this->log = $log; |
|
| 127 | + |
|
| 128 | + $this->recoveryKeyId = $this->config->getAppValue('encryption', |
|
| 129 | + 'recoveryKeyId'); |
|
| 130 | + if (empty($this->recoveryKeyId)) { |
|
| 131 | + $this->recoveryKeyId = 'recoveryKey_' . substr(md5(time()), 0, 8); |
|
| 132 | + $this->config->setAppValue('encryption', |
|
| 133 | + 'recoveryKeyId', |
|
| 134 | + $this->recoveryKeyId); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + $this->publicShareKeyId = $this->config->getAppValue('encryption', |
|
| 138 | + 'publicShareKeyId'); |
|
| 139 | + if (empty($this->publicShareKeyId)) { |
|
| 140 | + $this->publicShareKeyId = 'pubShare_' . substr(md5(time()), 0, 8); |
|
| 141 | + $this->config->setAppValue('encryption', 'publicShareKeyId', $this->publicShareKeyId); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + $this->masterKeyId = $this->config->getAppValue('encryption', |
|
| 145 | + 'masterKeyId'); |
|
| 146 | + if (empty($this->masterKeyId)) { |
|
| 147 | + $this->masterKeyId = 'master_' . substr(md5(time()), 0, 8); |
|
| 148 | + $this->config->setAppValue('encryption', 'masterKeyId', $this->masterKeyId); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + $this->keyId = $userSession && $userSession->isLoggedIn() ? $userSession->getUser()->getUID() : false; |
|
| 152 | + $this->log = $log; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * check if key pair for public link shares exists, if not we create one |
|
| 157 | + */ |
|
| 158 | + public function validateShareKey() { |
|
| 159 | + $shareKey = $this->getPublicShareKey(); |
|
| 160 | + if (empty($shareKey)) { |
|
| 161 | + $keyPair = $this->crypt->createKeyPair(); |
|
| 162 | + |
|
| 163 | + // Save public key |
|
| 164 | + $this->keyStorage->setSystemUserKey( |
|
| 165 | + $this->publicShareKeyId . '.publicKey', $keyPair['publicKey'], |
|
| 166 | + Encryption::ID); |
|
| 167 | + |
|
| 168 | + // Encrypt private key empty passphrase |
|
| 169 | + $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], ''); |
|
| 170 | + $header = $this->crypt->generateHeader(); |
|
| 171 | + $this->setSystemPrivateKey($this->publicShareKeyId, $header . $encryptedKey); |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * check if a key pair for the master key exists, if not we create one |
|
| 177 | + */ |
|
| 178 | + public function validateMasterKey() { |
|
| 179 | + |
|
| 180 | + if ($this->util->isMasterKeyEnabled() === false) { |
|
| 181 | + return; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + $publicMasterKey = $this->getPublicMasterKey(); |
|
| 185 | + if (empty($publicMasterKey)) { |
|
| 186 | + $keyPair = $this->crypt->createKeyPair(); |
|
| 187 | + |
|
| 188 | + // Save public key |
|
| 189 | + $this->keyStorage->setSystemUserKey( |
|
| 190 | + $this->masterKeyId . '.publicKey', $keyPair['publicKey'], |
|
| 191 | + Encryption::ID); |
|
| 192 | + |
|
| 193 | + // Encrypt private key with system password |
|
| 194 | + $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $this->getMasterKeyPassword(), $this->masterKeyId); |
|
| 195 | + $header = $this->crypt->generateHeader(); |
|
| 196 | + $this->setSystemPrivateKey($this->masterKeyId, $header . $encryptedKey); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + if (!$this->session->isPrivateKeySet()) { |
|
| 200 | + $masterKey = $this->getSystemPrivateKey($this->masterKeyId); |
|
| 201 | + $decryptedMasterKey = $this->crypt->decryptPrivateKey($masterKey, $this->getMasterKeyPassword(), $this->masterKeyId); |
|
| 202 | + $this->session->setPrivateKey($decryptedMasterKey); |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + // after the encryption key is available we are ready to go |
|
| 206 | + $this->session->setStatus(Session::INIT_SUCCESSFUL); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * @return bool |
|
| 211 | + */ |
|
| 212 | + public function recoveryKeyExists() { |
|
| 213 | + $key = $this->getRecoveryKey(); |
|
| 214 | + return !empty($key); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * get recovery key |
|
| 219 | + * |
|
| 220 | + * @return string |
|
| 221 | + */ |
|
| 222 | + public function getRecoveryKey() { |
|
| 223 | + return $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.publicKey', Encryption::ID); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * get recovery key ID |
|
| 228 | + * |
|
| 229 | + * @return string |
|
| 230 | + */ |
|
| 231 | + public function getRecoveryKeyId() { |
|
| 232 | + return $this->recoveryKeyId; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * @param string $password |
|
| 237 | + * @return bool |
|
| 238 | + */ |
|
| 239 | + public function checkRecoveryPassword($password) { |
|
| 240 | + $recoveryKey = $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.privateKey', Encryption::ID); |
|
| 241 | + $decryptedRecoveryKey = $this->crypt->decryptPrivateKey($recoveryKey, $password); |
|
| 242 | + |
|
| 243 | + if ($decryptedRecoveryKey) { |
|
| 244 | + return true; |
|
| 245 | + } |
|
| 246 | + return false; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * @param string $uid |
|
| 251 | + * @param string $password |
|
| 252 | + * @param string $keyPair |
|
| 253 | + * @return bool |
|
| 254 | + */ |
|
| 255 | + public function storeKeyPair($uid, $password, $keyPair) { |
|
| 256 | + // Save Public Key |
|
| 257 | + $this->setPublicKey($uid, $keyPair['publicKey']); |
|
| 258 | + |
|
| 259 | + $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $password, $uid); |
|
| 260 | + |
|
| 261 | + $header = $this->crypt->generateHeader(); |
|
| 262 | + |
|
| 263 | + if ($encryptedKey) { |
|
| 264 | + $this->setPrivateKey($uid, $header . $encryptedKey); |
|
| 265 | + return true; |
|
| 266 | + } |
|
| 267 | + return false; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * @param string $password |
|
| 272 | + * @param array $keyPair |
|
| 273 | + * @return bool |
|
| 274 | + */ |
|
| 275 | + public function setRecoveryKey($password, $keyPair) { |
|
| 276 | + // Save Public Key |
|
| 277 | + $this->keyStorage->setSystemUserKey($this->getRecoveryKeyId(). |
|
| 278 | + '.publicKey', |
|
| 279 | + $keyPair['publicKey'], |
|
| 280 | + Encryption::ID); |
|
| 281 | + |
|
| 282 | + $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $password); |
|
| 283 | + $header = $this->crypt->generateHeader(); |
|
| 284 | + |
|
| 285 | + if ($encryptedKey) { |
|
| 286 | + $this->setSystemPrivateKey($this->getRecoveryKeyId(), $header . $encryptedKey); |
|
| 287 | + return true; |
|
| 288 | + } |
|
| 289 | + return false; |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + /** |
|
| 293 | + * @param $userId |
|
| 294 | + * @param $key |
|
| 295 | + * @return bool |
|
| 296 | + */ |
|
| 297 | + public function setPublicKey($userId, $key) { |
|
| 298 | + return $this->keyStorage->setUserKey($userId, $this->publicKeyId, $key, Encryption::ID); |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * @param $userId |
|
| 303 | + * @param string $key |
|
| 304 | + * @return bool |
|
| 305 | + */ |
|
| 306 | + public function setPrivateKey($userId, $key) { |
|
| 307 | + return $this->keyStorage->setUserKey($userId, |
|
| 308 | + $this->privateKeyId, |
|
| 309 | + $key, |
|
| 310 | + Encryption::ID); |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * write file key to key storage |
|
| 315 | + * |
|
| 316 | + * @param string $path |
|
| 317 | + * @param string $key |
|
| 318 | + * @return boolean |
|
| 319 | + */ |
|
| 320 | + public function setFileKey($path, $key) { |
|
| 321 | + return $this->keyStorage->setFileKey($path, $this->fileKeyId, $key, Encryption::ID); |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + /** |
|
| 325 | + * set all file keys (the file key and the corresponding share keys) |
|
| 326 | + * |
|
| 327 | + * @param string $path |
|
| 328 | + * @param array $keys |
|
| 329 | + */ |
|
| 330 | + public function setAllFileKeys($path, $keys) { |
|
| 331 | + $this->setFileKey($path, $keys['data']); |
|
| 332 | + foreach ($keys['keys'] as $uid => $keyFile) { |
|
| 333 | + $this->setShareKey($path, $uid, $keyFile); |
|
| 334 | + } |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + /** |
|
| 338 | + * write share key to the key storage |
|
| 339 | + * |
|
| 340 | + * @param string $path |
|
| 341 | + * @param string $uid |
|
| 342 | + * @param string $key |
|
| 343 | + * @return boolean |
|
| 344 | + */ |
|
| 345 | + public function setShareKey($path, $uid, $key) { |
|
| 346 | + $keyId = $uid . '.' . $this->shareKeyId; |
|
| 347 | + return $this->keyStorage->setFileKey($path, $keyId, $key, Encryption::ID); |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + /** |
|
| 351 | + * Decrypt private key and store it |
|
| 352 | + * |
|
| 353 | + * @param string $uid user id |
|
| 354 | + * @param string $passPhrase users password |
|
| 355 | + * @return boolean |
|
| 356 | + */ |
|
| 357 | + public function init($uid, $passPhrase) { |
|
| 358 | + |
|
| 359 | + $this->session->setStatus(Session::INIT_EXECUTED); |
|
| 360 | + |
|
| 361 | + try { |
|
| 362 | + if($this->util->isMasterKeyEnabled()) { |
|
| 363 | + $uid = $this->getMasterKeyId(); |
|
| 364 | + $passPhrase = $this->getMasterKeyPassword(); |
|
| 365 | + $privateKey = $this->getSystemPrivateKey($uid); |
|
| 366 | + } else { |
|
| 367 | + $privateKey = $this->getPrivateKey($uid); |
|
| 368 | + } |
|
| 369 | + $privateKey = $this->crypt->decryptPrivateKey($privateKey, $passPhrase, $uid); |
|
| 370 | + } catch (PrivateKeyMissingException $e) { |
|
| 371 | + return false; |
|
| 372 | + } catch (DecryptionFailedException $e) { |
|
| 373 | + return false; |
|
| 374 | + } catch (\Exception $e) { |
|
| 375 | + $this->log->logException($e, [ |
|
| 376 | + 'message' => 'Could not decrypt the private key from user "' . $uid . '"" during login. Assume password change on the user back-end.', |
|
| 377 | + 'level' => \OCP\Util::WARN, |
|
| 378 | + 'app' => 'encryption', |
|
| 379 | + ]); |
|
| 380 | + return false; |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + if ($privateKey) { |
|
| 384 | + $this->session->setPrivateKey($privateKey); |
|
| 385 | + $this->session->setStatus(Session::INIT_SUCCESSFUL); |
|
| 386 | + return true; |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + return false; |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + /** |
|
| 393 | + * @param $userId |
|
| 394 | + * @return string |
|
| 395 | + * @throws PrivateKeyMissingException |
|
| 396 | + */ |
|
| 397 | + public function getPrivateKey($userId) { |
|
| 398 | + $privateKey = $this->keyStorage->getUserKey($userId, |
|
| 399 | + $this->privateKeyId, Encryption::ID); |
|
| 400 | + |
|
| 401 | + if (strlen($privateKey) !== 0) { |
|
| 402 | + return $privateKey; |
|
| 403 | + } |
|
| 404 | + throw new PrivateKeyMissingException($userId); |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + /** |
|
| 408 | + * @param string $path |
|
| 409 | + * @param $uid |
|
| 410 | + * @return string |
|
| 411 | + */ |
|
| 412 | + public function getFileKey($path, $uid) { |
|
| 413 | + if ($uid === '') { |
|
| 414 | + $uid = null; |
|
| 415 | + } |
|
| 416 | + $publicAccess = is_null($uid); |
|
| 417 | + $encryptedFileKey = $this->keyStorage->getFileKey($path, $this->fileKeyId, Encryption::ID); |
|
| 418 | + |
|
| 419 | + if (empty($encryptedFileKey)) { |
|
| 420 | + return ''; |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + if ($this->util->isMasterKeyEnabled()) { |
|
| 424 | + $uid = $this->getMasterKeyId(); |
|
| 425 | + $shareKey = $this->getShareKey($path, $uid); |
|
| 426 | + if ($publicAccess) { |
|
| 427 | + $privateKey = $this->getSystemPrivateKey($uid); |
|
| 428 | + $privateKey = $this->crypt->decryptPrivateKey($privateKey, $this->getMasterKeyPassword(), $uid); |
|
| 429 | + } else { |
|
| 430 | + // when logged in, the master key is already decrypted in the session |
|
| 431 | + $privateKey = $this->session->getPrivateKey(); |
|
| 432 | + } |
|
| 433 | + } else if ($publicAccess) { |
|
| 434 | + // use public share key for public links |
|
| 435 | + $uid = $this->getPublicShareKeyId(); |
|
| 436 | + $shareKey = $this->getShareKey($path, $uid); |
|
| 437 | + $privateKey = $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.privateKey', Encryption::ID); |
|
| 438 | + $privateKey = $this->crypt->decryptPrivateKey($privateKey); |
|
| 439 | + } else { |
|
| 440 | + $shareKey = $this->getShareKey($path, $uid); |
|
| 441 | + $privateKey = $this->session->getPrivateKey(); |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + if ($encryptedFileKey && $shareKey && $privateKey) { |
|
| 445 | + return $this->crypt->multiKeyDecrypt($encryptedFileKey, |
|
| 446 | + $shareKey, |
|
| 447 | + $privateKey); |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + return ''; |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + /** |
|
| 454 | + * Get the current version of a file |
|
| 455 | + * |
|
| 456 | + * @param string $path |
|
| 457 | + * @param View $view |
|
| 458 | + * @return int |
|
| 459 | + */ |
|
| 460 | + public function getVersion($path, View $view) { |
|
| 461 | + $fileInfo = $view->getFileInfo($path); |
|
| 462 | + if($fileInfo === false) { |
|
| 463 | + return 0; |
|
| 464 | + } |
|
| 465 | + return $fileInfo->getEncryptedVersion(); |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + /** |
|
| 469 | + * Set the current version of a file |
|
| 470 | + * |
|
| 471 | + * @param string $path |
|
| 472 | + * @param int $version |
|
| 473 | + * @param View $view |
|
| 474 | + */ |
|
| 475 | + public function setVersion($path, $version, View $view) { |
|
| 476 | + $fileInfo= $view->getFileInfo($path); |
|
| 477 | + |
|
| 478 | + if($fileInfo !== false) { |
|
| 479 | + $cache = $fileInfo->getStorage()->getCache(); |
|
| 480 | + $cache->update($fileInfo->getId(), ['encrypted' => $version, 'encryptedVersion' => $version]); |
|
| 481 | + } |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + /** |
|
| 485 | + * get the encrypted file key |
|
| 486 | + * |
|
| 487 | + * @param string $path |
|
| 488 | + * @return string |
|
| 489 | + */ |
|
| 490 | + public function getEncryptedFileKey($path) { |
|
| 491 | + $encryptedFileKey = $this->keyStorage->getFileKey($path, |
|
| 492 | + $this->fileKeyId, Encryption::ID); |
|
| 493 | + |
|
| 494 | + return $encryptedFileKey; |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + /** |
|
| 498 | + * delete share key |
|
| 499 | + * |
|
| 500 | + * @param string $path |
|
| 501 | + * @param string $keyId |
|
| 502 | + * @return boolean |
|
| 503 | + */ |
|
| 504 | + public function deleteShareKey($path, $keyId) { |
|
| 505 | + return $this->keyStorage->deleteFileKey( |
|
| 506 | + $path, |
|
| 507 | + $keyId . '.' . $this->shareKeyId, |
|
| 508 | + Encryption::ID); |
|
| 509 | + } |
|
| 510 | + |
|
| 511 | + |
|
| 512 | + /** |
|
| 513 | + * @param $path |
|
| 514 | + * @param $uid |
|
| 515 | + * @return mixed |
|
| 516 | + */ |
|
| 517 | + public function getShareKey($path, $uid) { |
|
| 518 | + $keyId = $uid . '.' . $this->shareKeyId; |
|
| 519 | + return $this->keyStorage->getFileKey($path, $keyId, Encryption::ID); |
|
| 520 | + } |
|
| 521 | + |
|
| 522 | + /** |
|
| 523 | + * check if user has a private and a public key |
|
| 524 | + * |
|
| 525 | + * @param string $userId |
|
| 526 | + * @return bool |
|
| 527 | + * @throws PrivateKeyMissingException |
|
| 528 | + * @throws PublicKeyMissingException |
|
| 529 | + */ |
|
| 530 | + public function userHasKeys($userId) { |
|
| 531 | + $privateKey = $publicKey = true; |
|
| 532 | + $exception = null; |
|
| 533 | + |
|
| 534 | + try { |
|
| 535 | + $this->getPrivateKey($userId); |
|
| 536 | + } catch (PrivateKeyMissingException $e) { |
|
| 537 | + $privateKey = false; |
|
| 538 | + $exception = $e; |
|
| 539 | + } |
|
| 540 | + try { |
|
| 541 | + $this->getPublicKey($userId); |
|
| 542 | + } catch (PublicKeyMissingException $e) { |
|
| 543 | + $publicKey = false; |
|
| 544 | + $exception = $e; |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + if ($privateKey && $publicKey) { |
|
| 548 | + return true; |
|
| 549 | + } elseif (!$privateKey && !$publicKey) { |
|
| 550 | + return false; |
|
| 551 | + } else { |
|
| 552 | + throw $exception; |
|
| 553 | + } |
|
| 554 | + } |
|
| 555 | + |
|
| 556 | + /** |
|
| 557 | + * @param $userId |
|
| 558 | + * @return mixed |
|
| 559 | + * @throws PublicKeyMissingException |
|
| 560 | + */ |
|
| 561 | + public function getPublicKey($userId) { |
|
| 562 | + $publicKey = $this->keyStorage->getUserKey($userId, $this->publicKeyId, Encryption::ID); |
|
| 563 | + |
|
| 564 | + if (strlen($publicKey) !== 0) { |
|
| 565 | + return $publicKey; |
|
| 566 | + } |
|
| 567 | + throw new PublicKeyMissingException($userId); |
|
| 568 | + } |
|
| 569 | + |
|
| 570 | + public function getPublicShareKeyId() { |
|
| 571 | + return $this->publicShareKeyId; |
|
| 572 | + } |
|
| 573 | + |
|
| 574 | + /** |
|
| 575 | + * get public key for public link shares |
|
| 576 | + * |
|
| 577 | + * @return string |
|
| 578 | + */ |
|
| 579 | + public function getPublicShareKey() { |
|
| 580 | + return $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.publicKey', Encryption::ID); |
|
| 581 | + } |
|
| 582 | + |
|
| 583 | + /** |
|
| 584 | + * @param string $purpose |
|
| 585 | + * @param string $uid |
|
| 586 | + */ |
|
| 587 | + public function backupUserKeys($purpose, $uid) { |
|
| 588 | + $this->keyStorage->backupUserKeys(Encryption::ID, $purpose, $uid); |
|
| 589 | + } |
|
| 590 | + |
|
| 591 | + /** |
|
| 592 | + * creat a backup of the users private and public key and then delete it |
|
| 593 | + * |
|
| 594 | + * @param string $uid |
|
| 595 | + */ |
|
| 596 | + public function deleteUserKeys($uid) { |
|
| 597 | + $this->deletePublicKey($uid); |
|
| 598 | + $this->deletePrivateKey($uid); |
|
| 599 | + } |
|
| 600 | + |
|
| 601 | + /** |
|
| 602 | + * @param $uid |
|
| 603 | + * @return bool |
|
| 604 | + */ |
|
| 605 | + public function deletePublicKey($uid) { |
|
| 606 | + return $this->keyStorage->deleteUserKey($uid, $this->publicKeyId, Encryption::ID); |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + /** |
|
| 610 | + * @param string $uid |
|
| 611 | + * @return bool |
|
| 612 | + */ |
|
| 613 | + private function deletePrivateKey($uid) { |
|
| 614 | + return $this->keyStorage->deleteUserKey($uid, $this->privateKeyId, Encryption::ID); |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + /** |
|
| 618 | + * @param string $path |
|
| 619 | + * @return bool |
|
| 620 | + */ |
|
| 621 | + public function deleteAllFileKeys($path) { |
|
| 622 | + return $this->keyStorage->deleteAllFileKeys($path); |
|
| 623 | + } |
|
| 624 | + |
|
| 625 | + /** |
|
| 626 | + * @param array $userIds |
|
| 627 | + * @return array |
|
| 628 | + * @throws PublicKeyMissingException |
|
| 629 | + */ |
|
| 630 | + public function getPublicKeys(array $userIds) { |
|
| 631 | + $keys = []; |
|
| 632 | + |
|
| 633 | + foreach ($userIds as $userId) { |
|
| 634 | + try { |
|
| 635 | + $keys[$userId] = $this->getPublicKey($userId); |
|
| 636 | + } catch (PublicKeyMissingException $e) { |
|
| 637 | + continue; |
|
| 638 | + } |
|
| 639 | + } |
|
| 640 | + |
|
| 641 | + return $keys; |
|
| 642 | + |
|
| 643 | + } |
|
| 644 | + |
|
| 645 | + /** |
|
| 646 | + * @param string $keyId |
|
| 647 | + * @return string returns openssl key |
|
| 648 | + */ |
|
| 649 | + public function getSystemPrivateKey($keyId) { |
|
| 650 | + return $this->keyStorage->getSystemUserKey($keyId . '.' . $this->privateKeyId, Encryption::ID); |
|
| 651 | + } |
|
| 652 | + |
|
| 653 | + /** |
|
| 654 | + * @param string $keyId |
|
| 655 | + * @param string $key |
|
| 656 | + * @return string returns openssl key |
|
| 657 | + */ |
|
| 658 | + public function setSystemPrivateKey($keyId, $key) { |
|
| 659 | + return $this->keyStorage->setSystemUserKey( |
|
| 660 | + $keyId . '.' . $this->privateKeyId, |
|
| 661 | + $key, |
|
| 662 | + Encryption::ID); |
|
| 663 | + } |
|
| 664 | + |
|
| 665 | + /** |
|
| 666 | + * add system keys such as the public share key and the recovery key |
|
| 667 | + * |
|
| 668 | + * @param array $accessList |
|
| 669 | + * @param array $publicKeys |
|
| 670 | + * @param string $uid |
|
| 671 | + * @return array |
|
| 672 | + * @throws PublicKeyMissingException |
|
| 673 | + */ |
|
| 674 | + public function addSystemKeys(array $accessList, array $publicKeys, $uid) { |
|
| 675 | + if (!empty($accessList['public'])) { |
|
| 676 | + $publicShareKey = $this->getPublicShareKey(); |
|
| 677 | + if (empty($publicShareKey)) { |
|
| 678 | + throw new PublicKeyMissingException($this->getPublicShareKeyId()); |
|
| 679 | + } |
|
| 680 | + $publicKeys[$this->getPublicShareKeyId()] = $publicShareKey; |
|
| 681 | + } |
|
| 682 | + |
|
| 683 | + if ($this->recoveryKeyExists() && |
|
| 684 | + $this->util->isRecoveryEnabledForUser($uid)) { |
|
| 685 | + |
|
| 686 | + $publicKeys[$this->getRecoveryKeyId()] = $this->getRecoveryKey(); |
|
| 687 | + } |
|
| 688 | + |
|
| 689 | + return $publicKeys; |
|
| 690 | + } |
|
| 691 | + |
|
| 692 | + /** |
|
| 693 | + * get master key password |
|
| 694 | + * |
|
| 695 | + * @return string |
|
| 696 | + * @throws \Exception |
|
| 697 | + */ |
|
| 698 | + public function getMasterKeyPassword() { |
|
| 699 | + $password = $this->config->getSystemValue('secret'); |
|
| 700 | + if (empty($password)){ |
|
| 701 | + throw new \Exception('Can not get secret from Nextcloud instance'); |
|
| 702 | + } |
|
| 703 | + |
|
| 704 | + return $password; |
|
| 705 | + } |
|
| 706 | + |
|
| 707 | + /** |
|
| 708 | + * return master key id |
|
| 709 | + * |
|
| 710 | + * @return string |
|
| 711 | + */ |
|
| 712 | + public function getMasterKeyId() { |
|
| 713 | + return $this->masterKeyId; |
|
| 714 | + } |
|
| 715 | + |
|
| 716 | + /** |
|
| 717 | + * get public master key |
|
| 718 | + * |
|
| 719 | + * @return string |
|
| 720 | + */ |
|
| 721 | + public function getPublicMasterKey() { |
|
| 722 | + return $this->keyStorage->getSystemUserKey($this->masterKeyId . '.publicKey', Encryption::ID); |
|
| 723 | + } |
|
| 724 | 724 | } |
@@ -32,56 +32,56 @@ |
||
| 32 | 32 | |
| 33 | 33 | class Admin implements ISettings { |
| 34 | 34 | |
| 35 | - /** @var IniGetWrapper */ |
|
| 36 | - private $iniWrapper; |
|
| 35 | + /** @var IniGetWrapper */ |
|
| 36 | + private $iniWrapper; |
|
| 37 | 37 | |
| 38 | - /** @var IRequest */ |
|
| 39 | - private $request; |
|
| 38 | + /** @var IRequest */ |
|
| 39 | + private $request; |
|
| 40 | 40 | |
| 41 | - public function __construct(IniGetWrapper $iniWrapper, IRequest $request) { |
|
| 42 | - $this->iniWrapper = $iniWrapper; |
|
| 43 | - $this->request = $request; |
|
| 44 | - } |
|
| 41 | + public function __construct(IniGetWrapper $iniWrapper, IRequest $request) { |
|
| 42 | + $this->iniWrapper = $iniWrapper; |
|
| 43 | + $this->request = $request; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * @return TemplateResponse |
|
| 48 | - */ |
|
| 49 | - public function getForm() { |
|
| 50 | - $htaccessWorking = (getenv('htaccessWorking') === 'true'); |
|
| 51 | - $htaccessWritable = is_writable(\OC::$SERVERROOT.'/.htaccess'); |
|
| 52 | - $userIniWritable = is_writable(\OC::$SERVERROOT.'/.user.ini'); |
|
| 46 | + /** |
|
| 47 | + * @return TemplateResponse |
|
| 48 | + */ |
|
| 49 | + public function getForm() { |
|
| 50 | + $htaccessWorking = (getenv('htaccessWorking') === 'true'); |
|
| 51 | + $htaccessWritable = is_writable(\OC::$SERVERROOT.'/.htaccess'); |
|
| 52 | + $userIniWritable = is_writable(\OC::$SERVERROOT.'/.user.ini'); |
|
| 53 | 53 | |
| 54 | - $upload_max_filesize = $this->iniWrapper->getBytes('upload_max_filesize'); |
|
| 55 | - $post_max_size = $this->iniWrapper->getBytes('post_max_size'); |
|
| 56 | - $maxUploadFilesize = Util::humanFileSize(min($upload_max_filesize, $post_max_size)); |
|
| 54 | + $upload_max_filesize = $this->iniWrapper->getBytes('upload_max_filesize'); |
|
| 55 | + $post_max_size = $this->iniWrapper->getBytes('post_max_size'); |
|
| 56 | + $maxUploadFilesize = Util::humanFileSize(min($upload_max_filesize, $post_max_size)); |
|
| 57 | 57 | |
| 58 | - $parameters = [ |
|
| 59 | - 'uploadChangable' => ($htaccessWorking and $htaccessWritable) or $userIniWritable, |
|
| 60 | - 'uploadMaxFilesize' => $maxUploadFilesize, |
|
| 61 | - // max possible makes only sense on a 32 bit system |
|
| 62 | - 'displayMaxPossibleUploadSize' => PHP_INT_SIZE === 4, |
|
| 63 | - 'maxPossibleUploadSize' => Util::humanFileSize(PHP_INT_MAX), |
|
| 64 | - ]; |
|
| 58 | + $parameters = [ |
|
| 59 | + 'uploadChangable' => ($htaccessWorking and $htaccessWritable) or $userIniWritable, |
|
| 60 | + 'uploadMaxFilesize' => $maxUploadFilesize, |
|
| 61 | + // max possible makes only sense on a 32 bit system |
|
| 62 | + 'displayMaxPossibleUploadSize' => PHP_INT_SIZE === 4, |
|
| 63 | + 'maxPossibleUploadSize' => Util::humanFileSize(PHP_INT_MAX), |
|
| 64 | + ]; |
|
| 65 | 65 | |
| 66 | - return new TemplateResponse('files', 'admin', $parameters, ''); |
|
| 67 | - } |
|
| 66 | + return new TemplateResponse('files', 'admin', $parameters, ''); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * @return string the section ID, e.g. 'sharing' |
|
| 71 | - */ |
|
| 72 | - public function getSection() { |
|
| 73 | - return 'additional'; |
|
| 74 | - } |
|
| 69 | + /** |
|
| 70 | + * @return string the section ID, e.g. 'sharing' |
|
| 71 | + */ |
|
| 72 | + public function getSection() { |
|
| 73 | + return 'additional'; |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * @return int whether the form should be rather on the top or bottom of |
|
| 78 | - * the admin section. The forms are arranged in ascending order of the |
|
| 79 | - * priority values. It is required to return a value between 0 and 100. |
|
| 80 | - * |
|
| 81 | - * E.g.: 70 |
|
| 82 | - */ |
|
| 83 | - public function getPriority() { |
|
| 84 | - return 5; |
|
| 85 | - } |
|
| 76 | + /** |
|
| 77 | + * @return int whether the form should be rather on the top or bottom of |
|
| 78 | + * the admin section. The forms are arranged in ascending order of the |
|
| 79 | + * priority values. It is required to return a value between 0 and 100. |
|
| 80 | + * |
|
| 81 | + * E.g.: 70 |
|
| 82 | + */ |
|
| 83 | + public function getPriority() { |
|
| 84 | + return 5; |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | 87 | } |
@@ -39,266 +39,266 @@ |
||
| 39 | 39 | |
| 40 | 40 | class ScanAppData extends Base { |
| 41 | 41 | |
| 42 | - /** @var IRootFolder */ |
|
| 43 | - protected $root; |
|
| 44 | - /** @var IConfig */ |
|
| 45 | - protected $config; |
|
| 46 | - /** @var float */ |
|
| 47 | - protected $execTime = 0; |
|
| 48 | - /** @var int */ |
|
| 49 | - protected $foldersCounter = 0; |
|
| 50 | - /** @var int */ |
|
| 51 | - protected $filesCounter = 0; |
|
| 42 | + /** @var IRootFolder */ |
|
| 43 | + protected $root; |
|
| 44 | + /** @var IConfig */ |
|
| 45 | + protected $config; |
|
| 46 | + /** @var float */ |
|
| 47 | + protected $execTime = 0; |
|
| 48 | + /** @var int */ |
|
| 49 | + protected $foldersCounter = 0; |
|
| 50 | + /** @var int */ |
|
| 51 | + protected $filesCounter = 0; |
|
| 52 | 52 | |
| 53 | - public function __construct(IRootFolder $rootFolder, IConfig $config) { |
|
| 54 | - parent::__construct(); |
|
| 53 | + public function __construct(IRootFolder $rootFolder, IConfig $config) { |
|
| 54 | + parent::__construct(); |
|
| 55 | 55 | |
| 56 | - $this->root = $rootFolder; |
|
| 57 | - $this->config = $config; |
|
| 58 | - } |
|
| 56 | + $this->root = $rootFolder; |
|
| 57 | + $this->config = $config; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - protected function configure() { |
|
| 61 | - parent::configure(); |
|
| 60 | + protected function configure() { |
|
| 61 | + parent::configure(); |
|
| 62 | 62 | |
| 63 | - $this |
|
| 64 | - ->setName('files:scan-app-data') |
|
| 65 | - ->setDescription('rescan the AppData folder') |
|
| 66 | - ->addOption( |
|
| 67 | - 'quiet', |
|
| 68 | - 'q', |
|
| 69 | - InputOption::VALUE_NONE, |
|
| 70 | - 'suppress any output' |
|
| 71 | - ) |
|
| 72 | - ->addOption( |
|
| 73 | - 'verbose', |
|
| 74 | - '-v|vv|vvv', |
|
| 75 | - InputOption::VALUE_NONE, |
|
| 76 | - 'verbose the output' |
|
| 77 | - ); |
|
| 78 | - } |
|
| 63 | + $this |
|
| 64 | + ->setName('files:scan-app-data') |
|
| 65 | + ->setDescription('rescan the AppData folder') |
|
| 66 | + ->addOption( |
|
| 67 | + 'quiet', |
|
| 68 | + 'q', |
|
| 69 | + InputOption::VALUE_NONE, |
|
| 70 | + 'suppress any output' |
|
| 71 | + ) |
|
| 72 | + ->addOption( |
|
| 73 | + 'verbose', |
|
| 74 | + '-v|vv|vvv', |
|
| 75 | + InputOption::VALUE_NONE, |
|
| 76 | + 'verbose the output' |
|
| 77 | + ); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - public function checkScanWarning($fullPath, OutputInterface $output) { |
|
| 81 | - $normalizedPath = basename(\OC\Files\Filesystem::normalizePath($fullPath)); |
|
| 82 | - $path = basename($fullPath); |
|
| 80 | + public function checkScanWarning($fullPath, OutputInterface $output) { |
|
| 81 | + $normalizedPath = basename(\OC\Files\Filesystem::normalizePath($fullPath)); |
|
| 82 | + $path = basename($fullPath); |
|
| 83 | 83 | |
| 84 | - if ($normalizedPath !== $path) { |
|
| 85 | - $output->writeln("\t<error>Entry \"" . $fullPath . '" will not be accessible due to incompatible encoding</error>'); |
|
| 86 | - } |
|
| 87 | - } |
|
| 84 | + if ($normalizedPath !== $path) { |
|
| 85 | + $output->writeln("\t<error>Entry \"" . $fullPath . '" will not be accessible due to incompatible encoding</error>'); |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - protected function scanFiles($verbose, OutputInterface $output) { |
|
| 90 | - try { |
|
| 91 | - $appData = $this->getAppDataFolder(); |
|
| 92 | - } catch (NotFoundException $e) { |
|
| 93 | - $output->writeln('NoAppData folder found'); |
|
| 94 | - return; |
|
| 95 | - } |
|
| 89 | + protected function scanFiles($verbose, OutputInterface $output) { |
|
| 90 | + try { |
|
| 91 | + $appData = $this->getAppDataFolder(); |
|
| 92 | + } catch (NotFoundException $e) { |
|
| 93 | + $output->writeln('NoAppData folder found'); |
|
| 94 | + return; |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - $connection = $this->reconnectToDatabase($output); |
|
| 98 | - $scanner = new \OC\Files\Utils\Scanner(null, $connection, \OC::$server->getLogger()); |
|
| 99 | - # check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception |
|
| 100 | - # printout and count |
|
| 101 | - if ($verbose) { |
|
| 102 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { |
|
| 103 | - $output->writeln("\tFile <info>$path</info>"); |
|
| 104 | - $this->filesCounter += 1; |
|
| 105 | - if ($this->hasBeenInterrupted()) { |
|
| 106 | - throw new InterruptedException(); |
|
| 107 | - } |
|
| 108 | - }); |
|
| 109 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) { |
|
| 110 | - $output->writeln("\tFolder <info>$path</info>"); |
|
| 111 | - $this->foldersCounter += 1; |
|
| 112 | - if ($this->hasBeenInterrupted()) { |
|
| 113 | - throw new InterruptedException(); |
|
| 114 | - } |
|
| 115 | - }); |
|
| 116 | - $scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) { |
|
| 117 | - $output->writeln("Error while scanning, storage not available (" . $e->getMessage() . ")"); |
|
| 118 | - }); |
|
| 119 | - # count only |
|
| 120 | - } else { |
|
| 121 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function () use ($output) { |
|
| 122 | - $this->filesCounter += 1; |
|
| 123 | - if ($this->hasBeenInterrupted()) { |
|
| 124 | - throw new InterruptedException(); |
|
| 125 | - } |
|
| 126 | - }); |
|
| 127 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function () use ($output) { |
|
| 128 | - $this->foldersCounter += 1; |
|
| 129 | - if ($this->hasBeenInterrupted()) { |
|
| 130 | - throw new InterruptedException(); |
|
| 131 | - } |
|
| 132 | - }); |
|
| 133 | - } |
|
| 134 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function($path) use ($output) { |
|
| 135 | - $this->checkScanWarning($path, $output); |
|
| 136 | - }); |
|
| 137 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function($path) use ($output) { |
|
| 138 | - $this->checkScanWarning($path, $output); |
|
| 139 | - }); |
|
| 97 | + $connection = $this->reconnectToDatabase($output); |
|
| 98 | + $scanner = new \OC\Files\Utils\Scanner(null, $connection, \OC::$server->getLogger()); |
|
| 99 | + # check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception |
|
| 100 | + # printout and count |
|
| 101 | + if ($verbose) { |
|
| 102 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { |
|
| 103 | + $output->writeln("\tFile <info>$path</info>"); |
|
| 104 | + $this->filesCounter += 1; |
|
| 105 | + if ($this->hasBeenInterrupted()) { |
|
| 106 | + throw new InterruptedException(); |
|
| 107 | + } |
|
| 108 | + }); |
|
| 109 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) { |
|
| 110 | + $output->writeln("\tFolder <info>$path</info>"); |
|
| 111 | + $this->foldersCounter += 1; |
|
| 112 | + if ($this->hasBeenInterrupted()) { |
|
| 113 | + throw new InterruptedException(); |
|
| 114 | + } |
|
| 115 | + }); |
|
| 116 | + $scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) { |
|
| 117 | + $output->writeln("Error while scanning, storage not available (" . $e->getMessage() . ")"); |
|
| 118 | + }); |
|
| 119 | + # count only |
|
| 120 | + } else { |
|
| 121 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function () use ($output) { |
|
| 122 | + $this->filesCounter += 1; |
|
| 123 | + if ($this->hasBeenInterrupted()) { |
|
| 124 | + throw new InterruptedException(); |
|
| 125 | + } |
|
| 126 | + }); |
|
| 127 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function () use ($output) { |
|
| 128 | + $this->foldersCounter += 1; |
|
| 129 | + if ($this->hasBeenInterrupted()) { |
|
| 130 | + throw new InterruptedException(); |
|
| 131 | + } |
|
| 132 | + }); |
|
| 133 | + } |
|
| 134 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function($path) use ($output) { |
|
| 135 | + $this->checkScanWarning($path, $output); |
|
| 136 | + }); |
|
| 137 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function($path) use ($output) { |
|
| 138 | + $this->checkScanWarning($path, $output); |
|
| 139 | + }); |
|
| 140 | 140 | |
| 141 | - try { |
|
| 142 | - $scanner->scan($appData->getPath()); |
|
| 143 | - } catch (ForbiddenException $e) { |
|
| 144 | - $output->writeln("<error>Storage not writable</error>"); |
|
| 145 | - $output->writeln("Make sure you're running the scan command only as the user the web server runs as"); |
|
| 146 | - } catch (InterruptedException $e) { |
|
| 147 | - # exit the function if ctrl-c has been pressed |
|
| 148 | - $output->writeln('Interrupted by user'); |
|
| 149 | - } catch (NotFoundException $e) { |
|
| 150 | - $output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>'); |
|
| 151 | - } catch (\Exception $e) { |
|
| 152 | - $output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>'); |
|
| 153 | - $output->writeln('<error>' . $e->getTraceAsString() . '</error>'); |
|
| 154 | - } |
|
| 155 | - } |
|
| 141 | + try { |
|
| 142 | + $scanner->scan($appData->getPath()); |
|
| 143 | + } catch (ForbiddenException $e) { |
|
| 144 | + $output->writeln("<error>Storage not writable</error>"); |
|
| 145 | + $output->writeln("Make sure you're running the scan command only as the user the web server runs as"); |
|
| 146 | + } catch (InterruptedException $e) { |
|
| 147 | + # exit the function if ctrl-c has been pressed |
|
| 148 | + $output->writeln('Interrupted by user'); |
|
| 149 | + } catch (NotFoundException $e) { |
|
| 150 | + $output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>'); |
|
| 151 | + } catch (\Exception $e) { |
|
| 152 | + $output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>'); |
|
| 153 | + $output->writeln('<error>' . $e->getTraceAsString() . '</error>'); |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | 156 | |
| 157 | 157 | |
| 158 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 159 | - # no messaging level option means: no full printout but statistics |
|
| 160 | - # $quiet means no print at all |
|
| 161 | - # $verbose means full printout including statistics |
|
| 162 | - # -q -v full stat |
|
| 163 | - # 0 0 no yes |
|
| 164 | - # 0 1 yes yes |
|
| 165 | - # 1 -- no no (quiet overrules verbose) |
|
| 166 | - $verbose = $input->getOption('verbose'); |
|
| 167 | - $quiet = $input->getOption('quiet'); |
|
| 168 | - # restrict the verbosity level to VERBOSITY_VERBOSE |
|
| 169 | - if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) { |
|
| 170 | - $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); |
|
| 171 | - } |
|
| 172 | - if ($quiet) { |
|
| 173 | - $verbose = false; |
|
| 174 | - } |
|
| 158 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 159 | + # no messaging level option means: no full printout but statistics |
|
| 160 | + # $quiet means no print at all |
|
| 161 | + # $verbose means full printout including statistics |
|
| 162 | + # -q -v full stat |
|
| 163 | + # 0 0 no yes |
|
| 164 | + # 0 1 yes yes |
|
| 165 | + # 1 -- no no (quiet overrules verbose) |
|
| 166 | + $verbose = $input->getOption('verbose'); |
|
| 167 | + $quiet = $input->getOption('quiet'); |
|
| 168 | + # restrict the verbosity level to VERBOSITY_VERBOSE |
|
| 169 | + if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) { |
|
| 170 | + $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); |
|
| 171 | + } |
|
| 172 | + if ($quiet) { |
|
| 173 | + $verbose = false; |
|
| 174 | + } |
|
| 175 | 175 | |
| 176 | - $output->writeln("\nScanning AppData for files"); |
|
| 176 | + $output->writeln("\nScanning AppData for files"); |
|
| 177 | 177 | |
| 178 | - $this->initTools(); |
|
| 178 | + $this->initTools(); |
|
| 179 | 179 | |
| 180 | - $this->scanFiles($verbose, $output); |
|
| 180 | + $this->scanFiles($verbose, $output); |
|
| 181 | 181 | |
| 182 | - # stat: printout statistics if $quiet was not set |
|
| 183 | - if (!$quiet) { |
|
| 184 | - $this->presentStats($output); |
|
| 185 | - } |
|
| 186 | - } |
|
| 182 | + # stat: printout statistics if $quiet was not set |
|
| 183 | + if (!$quiet) { |
|
| 184 | + $this->presentStats($output); |
|
| 185 | + } |
|
| 186 | + } |
|
| 187 | 187 | |
| 188 | - /** |
|
| 189 | - * Initialises some useful tools for the Command |
|
| 190 | - */ |
|
| 191 | - protected function initTools() { |
|
| 192 | - // Start the timer |
|
| 193 | - $this->execTime = -microtime(true); |
|
| 194 | - // Convert PHP errors to exceptions |
|
| 195 | - set_error_handler([$this, 'exceptionErrorHandler'], E_ALL); |
|
| 196 | - } |
|
| 188 | + /** |
|
| 189 | + * Initialises some useful tools for the Command |
|
| 190 | + */ |
|
| 191 | + protected function initTools() { |
|
| 192 | + // Start the timer |
|
| 193 | + $this->execTime = -microtime(true); |
|
| 194 | + // Convert PHP errors to exceptions |
|
| 195 | + set_error_handler([$this, 'exceptionErrorHandler'], E_ALL); |
|
| 196 | + } |
|
| 197 | 197 | |
| 198 | - /** |
|
| 199 | - * Processes PHP errors as exceptions in order to be able to keep track of problems |
|
| 200 | - * |
|
| 201 | - * @see https://secure.php.net/manual/en/function.set-error-handler.php |
|
| 202 | - * |
|
| 203 | - * @param int $severity the level of the error raised |
|
| 204 | - * @param string $message |
|
| 205 | - * @param string $file the filename that the error was raised in |
|
| 206 | - * @param int $line the line number the error was raised |
|
| 207 | - * |
|
| 208 | - * @throws \ErrorException |
|
| 209 | - */ |
|
| 210 | - public function exceptionErrorHandler($severity, $message, $file, $line) { |
|
| 211 | - if (!(error_reporting() & $severity)) { |
|
| 212 | - // This error code is not included in error_reporting |
|
| 213 | - return; |
|
| 214 | - } |
|
| 215 | - throw new \ErrorException($message, 0, $severity, $file, $line); |
|
| 216 | - } |
|
| 198 | + /** |
|
| 199 | + * Processes PHP errors as exceptions in order to be able to keep track of problems |
|
| 200 | + * |
|
| 201 | + * @see https://secure.php.net/manual/en/function.set-error-handler.php |
|
| 202 | + * |
|
| 203 | + * @param int $severity the level of the error raised |
|
| 204 | + * @param string $message |
|
| 205 | + * @param string $file the filename that the error was raised in |
|
| 206 | + * @param int $line the line number the error was raised |
|
| 207 | + * |
|
| 208 | + * @throws \ErrorException |
|
| 209 | + */ |
|
| 210 | + public function exceptionErrorHandler($severity, $message, $file, $line) { |
|
| 211 | + if (!(error_reporting() & $severity)) { |
|
| 212 | + // This error code is not included in error_reporting |
|
| 213 | + return; |
|
| 214 | + } |
|
| 215 | + throw new \ErrorException($message, 0, $severity, $file, $line); |
|
| 216 | + } |
|
| 217 | 217 | |
| 218 | - /** |
|
| 219 | - * @param OutputInterface $output |
|
| 220 | - */ |
|
| 221 | - protected function presentStats(OutputInterface $output) { |
|
| 222 | - // Stop the timer |
|
| 223 | - $this->execTime += microtime(true); |
|
| 224 | - $output->writeln(""); |
|
| 218 | + /** |
|
| 219 | + * @param OutputInterface $output |
|
| 220 | + */ |
|
| 221 | + protected function presentStats(OutputInterface $output) { |
|
| 222 | + // Stop the timer |
|
| 223 | + $this->execTime += microtime(true); |
|
| 224 | + $output->writeln(""); |
|
| 225 | 225 | |
| 226 | - $headers = [ |
|
| 227 | - 'Folders', 'Files', 'Elapsed time' |
|
| 228 | - ]; |
|
| 226 | + $headers = [ |
|
| 227 | + 'Folders', 'Files', 'Elapsed time' |
|
| 228 | + ]; |
|
| 229 | 229 | |
| 230 | - $this->showSummary($headers, null, $output); |
|
| 231 | - } |
|
| 230 | + $this->showSummary($headers, null, $output); |
|
| 231 | + } |
|
| 232 | 232 | |
| 233 | - /** |
|
| 234 | - * Shows a summary of operations |
|
| 235 | - * |
|
| 236 | - * @param string[] $headers |
|
| 237 | - * @param string[] $rows |
|
| 238 | - * @param OutputInterface $output |
|
| 239 | - */ |
|
| 240 | - protected function showSummary($headers, $rows, OutputInterface $output) { |
|
| 241 | - $niceDate = $this->formatExecTime(); |
|
| 242 | - if (!$rows) { |
|
| 243 | - $rows = [ |
|
| 244 | - $this->foldersCounter, |
|
| 245 | - $this->filesCounter, |
|
| 246 | - $niceDate, |
|
| 247 | - ]; |
|
| 248 | - } |
|
| 249 | - $table = new Table($output); |
|
| 250 | - $table |
|
| 251 | - ->setHeaders($headers) |
|
| 252 | - ->setRows([$rows]); |
|
| 253 | - $table->render(); |
|
| 254 | - } |
|
| 233 | + /** |
|
| 234 | + * Shows a summary of operations |
|
| 235 | + * |
|
| 236 | + * @param string[] $headers |
|
| 237 | + * @param string[] $rows |
|
| 238 | + * @param OutputInterface $output |
|
| 239 | + */ |
|
| 240 | + protected function showSummary($headers, $rows, OutputInterface $output) { |
|
| 241 | + $niceDate = $this->formatExecTime(); |
|
| 242 | + if (!$rows) { |
|
| 243 | + $rows = [ |
|
| 244 | + $this->foldersCounter, |
|
| 245 | + $this->filesCounter, |
|
| 246 | + $niceDate, |
|
| 247 | + ]; |
|
| 248 | + } |
|
| 249 | + $table = new Table($output); |
|
| 250 | + $table |
|
| 251 | + ->setHeaders($headers) |
|
| 252 | + ->setRows([$rows]); |
|
| 253 | + $table->render(); |
|
| 254 | + } |
|
| 255 | 255 | |
| 256 | 256 | |
| 257 | - /** |
|
| 258 | - * Formats microtime into a human readable format |
|
| 259 | - * |
|
| 260 | - * @return string |
|
| 261 | - */ |
|
| 262 | - protected function formatExecTime() { |
|
| 263 | - list($secs, ) = explode('.', sprintf("%.1f", $this->execTime)); |
|
| 257 | + /** |
|
| 258 | + * Formats microtime into a human readable format |
|
| 259 | + * |
|
| 260 | + * @return string |
|
| 261 | + */ |
|
| 262 | + protected function formatExecTime() { |
|
| 263 | + list($secs, ) = explode('.', sprintf("%.1f", $this->execTime)); |
|
| 264 | 264 | |
| 265 | - # if you want to have microseconds add this: . '.' . $tens; |
|
| 266 | - return date('H:i:s', $secs); |
|
| 267 | - } |
|
| 265 | + # if you want to have microseconds add this: . '.' . $tens; |
|
| 266 | + return date('H:i:s', $secs); |
|
| 267 | + } |
|
| 268 | 268 | |
| 269 | - /** |
|
| 270 | - * @return \OCP\IDBConnection |
|
| 271 | - */ |
|
| 272 | - protected function reconnectToDatabase(OutputInterface $output) { |
|
| 273 | - /** @var Connection | IDBConnection $connection*/ |
|
| 274 | - $connection = \OC::$server->getDatabaseConnection(); |
|
| 275 | - try { |
|
| 276 | - $connection->close(); |
|
| 277 | - } catch (\Exception $ex) { |
|
| 278 | - $output->writeln("<info>Error while disconnecting from database: {$ex->getMessage()}</info>"); |
|
| 279 | - } |
|
| 280 | - while (!$connection->isConnected()) { |
|
| 281 | - try { |
|
| 282 | - $connection->connect(); |
|
| 283 | - } catch (\Exception $ex) { |
|
| 284 | - $output->writeln("<info>Error while re-connecting to database: {$ex->getMessage()}</info>"); |
|
| 285 | - sleep(60); |
|
| 286 | - } |
|
| 287 | - } |
|
| 288 | - return $connection; |
|
| 289 | - } |
|
| 269 | + /** |
|
| 270 | + * @return \OCP\IDBConnection |
|
| 271 | + */ |
|
| 272 | + protected function reconnectToDatabase(OutputInterface $output) { |
|
| 273 | + /** @var Connection | IDBConnection $connection*/ |
|
| 274 | + $connection = \OC::$server->getDatabaseConnection(); |
|
| 275 | + try { |
|
| 276 | + $connection->close(); |
|
| 277 | + } catch (\Exception $ex) { |
|
| 278 | + $output->writeln("<info>Error while disconnecting from database: {$ex->getMessage()}</info>"); |
|
| 279 | + } |
|
| 280 | + while (!$connection->isConnected()) { |
|
| 281 | + try { |
|
| 282 | + $connection->connect(); |
|
| 283 | + } catch (\Exception $ex) { |
|
| 284 | + $output->writeln("<info>Error while re-connecting to database: {$ex->getMessage()}</info>"); |
|
| 285 | + sleep(60); |
|
| 286 | + } |
|
| 287 | + } |
|
| 288 | + return $connection; |
|
| 289 | + } |
|
| 290 | 290 | |
| 291 | - /** |
|
| 292 | - * @return \OCP\Files\Folder |
|
| 293 | - * @throws NotFoundException |
|
| 294 | - */ |
|
| 295 | - private function getAppDataFolder() { |
|
| 296 | - $instanceId = $this->config->getSystemValue('instanceid', null); |
|
| 291 | + /** |
|
| 292 | + * @return \OCP\Files\Folder |
|
| 293 | + * @throws NotFoundException |
|
| 294 | + */ |
|
| 295 | + private function getAppDataFolder() { |
|
| 296 | + $instanceId = $this->config->getSystemValue('instanceid', null); |
|
| 297 | 297 | |
| 298 | - if ($instanceId === null) { |
|
| 299 | - throw new NotFoundException(); |
|
| 300 | - } |
|
| 298 | + if ($instanceId === null) { |
|
| 299 | + throw new NotFoundException(); |
|
| 300 | + } |
|
| 301 | 301 | |
| 302 | - return $this->root->get('appdata_'.$instanceId); |
|
| 303 | - } |
|
| 302 | + return $this->root->get('appdata_'.$instanceId); |
|
| 303 | + } |
|
| 304 | 304 | } |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | $path = basename($fullPath); |
| 83 | 83 | |
| 84 | 84 | if ($normalizedPath !== $path) { |
| 85 | - $output->writeln("\t<error>Entry \"" . $fullPath . '" will not be accessible due to incompatible encoding</error>'); |
|
| 85 | + $output->writeln("\t<error>Entry \"".$fullPath.'" will not be accessible due to incompatible encoding</error>'); |
|
| 86 | 86 | } |
| 87 | 87 | } |
| 88 | 88 | |
@@ -99,32 +99,32 @@ discard block |
||
| 99 | 99 | # check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception |
| 100 | 100 | # printout and count |
| 101 | 101 | if ($verbose) { |
| 102 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { |
|
| 102 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function($path) use ($output) { |
|
| 103 | 103 | $output->writeln("\tFile <info>$path</info>"); |
| 104 | 104 | $this->filesCounter += 1; |
| 105 | 105 | if ($this->hasBeenInterrupted()) { |
| 106 | 106 | throw new InterruptedException(); |
| 107 | 107 | } |
| 108 | 108 | }); |
| 109 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) { |
|
| 109 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function($path) use ($output) { |
|
| 110 | 110 | $output->writeln("\tFolder <info>$path</info>"); |
| 111 | 111 | $this->foldersCounter += 1; |
| 112 | 112 | if ($this->hasBeenInterrupted()) { |
| 113 | 113 | throw new InterruptedException(); |
| 114 | 114 | } |
| 115 | 115 | }); |
| 116 | - $scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) { |
|
| 117 | - $output->writeln("Error while scanning, storage not available (" . $e->getMessage() . ")"); |
|
| 116 | + $scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function(StorageNotAvailableException $e) use ($output) { |
|
| 117 | + $output->writeln("Error while scanning, storage not available (".$e->getMessage().")"); |
|
| 118 | 118 | }); |
| 119 | 119 | # count only |
| 120 | 120 | } else { |
| 121 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function () use ($output) { |
|
| 121 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function() use ($output) { |
|
| 122 | 122 | $this->filesCounter += 1; |
| 123 | 123 | if ($this->hasBeenInterrupted()) { |
| 124 | 124 | throw new InterruptedException(); |
| 125 | 125 | } |
| 126 | 126 | }); |
| 127 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function () use ($output) { |
|
| 127 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function() use ($output) { |
|
| 128 | 128 | $this->foldersCounter += 1; |
| 129 | 129 | if ($this->hasBeenInterrupted()) { |
| 130 | 130 | throw new InterruptedException(); |
@@ -147,10 +147,10 @@ discard block |
||
| 147 | 147 | # exit the function if ctrl-c has been pressed |
| 148 | 148 | $output->writeln('Interrupted by user'); |
| 149 | 149 | } catch (NotFoundException $e) { |
| 150 | - $output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>'); |
|
| 150 | + $output->writeln('<error>Path not found: '.$e->getMessage().'</error>'); |
|
| 151 | 151 | } catch (\Exception $e) { |
| 152 | - $output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>'); |
|
| 153 | - $output->writeln('<error>' . $e->getTraceAsString() . '</error>'); |
|
| 152 | + $output->writeln('<error>Exception during scan: '.$e->getMessage().'</error>'); |
|
| 153 | + $output->writeln('<error>'.$e->getTraceAsString().'</error>'); |
|
| 154 | 154 | } |
| 155 | 155 | } |
| 156 | 156 | |
@@ -260,7 +260,7 @@ discard block |
||
| 260 | 260 | * @return string |
| 261 | 261 | */ |
| 262 | 262 | protected function formatExecTime() { |
| 263 | - list($secs, ) = explode('.', sprintf("%.1f", $this->execTime)); |
|
| 263 | + list($secs,) = explode('.', sprintf("%.1f", $this->execTime)); |
|
| 264 | 264 | |
| 265 | 265 | # if you want to have microseconds add this: . '.' . $tens; |
| 266 | 266 | return date('H:i:s', $secs); |
@@ -44,310 +44,310 @@ |
||
| 44 | 44 | |
| 45 | 45 | class Scan extends Base { |
| 46 | 46 | |
| 47 | - /** @var IUserManager $userManager */ |
|
| 48 | - private $userManager; |
|
| 49 | - /** @var float */ |
|
| 50 | - protected $execTime = 0; |
|
| 51 | - /** @var int */ |
|
| 52 | - protected $foldersCounter = 0; |
|
| 53 | - /** @var int */ |
|
| 54 | - protected $filesCounter = 0; |
|
| 47 | + /** @var IUserManager $userManager */ |
|
| 48 | + private $userManager; |
|
| 49 | + /** @var float */ |
|
| 50 | + protected $execTime = 0; |
|
| 51 | + /** @var int */ |
|
| 52 | + protected $foldersCounter = 0; |
|
| 53 | + /** @var int */ |
|
| 54 | + protected $filesCounter = 0; |
|
| 55 | 55 | |
| 56 | - public function __construct(IUserManager $userManager) { |
|
| 57 | - $this->userManager = $userManager; |
|
| 58 | - parent::__construct(); |
|
| 59 | - } |
|
| 56 | + public function __construct(IUserManager $userManager) { |
|
| 57 | + $this->userManager = $userManager; |
|
| 58 | + parent::__construct(); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - protected function configure() { |
|
| 62 | - parent::configure(); |
|
| 61 | + protected function configure() { |
|
| 62 | + parent::configure(); |
|
| 63 | 63 | |
| 64 | - $this |
|
| 65 | - ->setName('files:scan') |
|
| 66 | - ->setDescription('rescan filesystem') |
|
| 67 | - ->addArgument( |
|
| 68 | - 'user_id', |
|
| 69 | - InputArgument::OPTIONAL | InputArgument::IS_ARRAY, |
|
| 70 | - 'will rescan all files of the given user(s)' |
|
| 71 | - ) |
|
| 72 | - ->addOption( |
|
| 73 | - 'path', |
|
| 74 | - 'p', |
|
| 75 | - InputArgument::OPTIONAL, |
|
| 76 | - 'limit rescan to this path, eg. --path="/alice/files/Music", the user_id is determined by the path and the user_id parameter and --all are ignored' |
|
| 77 | - ) |
|
| 78 | - ->addOption( |
|
| 79 | - 'quiet', |
|
| 80 | - 'q', |
|
| 81 | - InputOption::VALUE_NONE, |
|
| 82 | - 'suppress any output' |
|
| 83 | - ) |
|
| 84 | - ->addOption( |
|
| 85 | - 'verbose', |
|
| 86 | - '-v|vv|vvv', |
|
| 87 | - InputOption::VALUE_NONE, |
|
| 88 | - 'verbose the output' |
|
| 89 | - ) |
|
| 90 | - ->addOption( |
|
| 91 | - 'all', |
|
| 92 | - null, |
|
| 93 | - InputOption::VALUE_NONE, |
|
| 94 | - 'will rescan all files of all known users' |
|
| 95 | - )->addOption( |
|
| 96 | - 'unscanned', |
|
| 97 | - null, |
|
| 98 | - InputOption::VALUE_NONE, |
|
| 99 | - 'only scan files which are marked as not fully scanned' |
|
| 100 | - ); |
|
| 101 | - } |
|
| 64 | + $this |
|
| 65 | + ->setName('files:scan') |
|
| 66 | + ->setDescription('rescan filesystem') |
|
| 67 | + ->addArgument( |
|
| 68 | + 'user_id', |
|
| 69 | + InputArgument::OPTIONAL | InputArgument::IS_ARRAY, |
|
| 70 | + 'will rescan all files of the given user(s)' |
|
| 71 | + ) |
|
| 72 | + ->addOption( |
|
| 73 | + 'path', |
|
| 74 | + 'p', |
|
| 75 | + InputArgument::OPTIONAL, |
|
| 76 | + 'limit rescan to this path, eg. --path="/alice/files/Music", the user_id is determined by the path and the user_id parameter and --all are ignored' |
|
| 77 | + ) |
|
| 78 | + ->addOption( |
|
| 79 | + 'quiet', |
|
| 80 | + 'q', |
|
| 81 | + InputOption::VALUE_NONE, |
|
| 82 | + 'suppress any output' |
|
| 83 | + ) |
|
| 84 | + ->addOption( |
|
| 85 | + 'verbose', |
|
| 86 | + '-v|vv|vvv', |
|
| 87 | + InputOption::VALUE_NONE, |
|
| 88 | + 'verbose the output' |
|
| 89 | + ) |
|
| 90 | + ->addOption( |
|
| 91 | + 'all', |
|
| 92 | + null, |
|
| 93 | + InputOption::VALUE_NONE, |
|
| 94 | + 'will rescan all files of all known users' |
|
| 95 | + )->addOption( |
|
| 96 | + 'unscanned', |
|
| 97 | + null, |
|
| 98 | + InputOption::VALUE_NONE, |
|
| 99 | + 'only scan files which are marked as not fully scanned' |
|
| 100 | + ); |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - public function checkScanWarning($fullPath, OutputInterface $output) { |
|
| 104 | - $normalizedPath = basename(\OC\Files\Filesystem::normalizePath($fullPath)); |
|
| 105 | - $path = basename($fullPath); |
|
| 103 | + public function checkScanWarning($fullPath, OutputInterface $output) { |
|
| 104 | + $normalizedPath = basename(\OC\Files\Filesystem::normalizePath($fullPath)); |
|
| 105 | + $path = basename($fullPath); |
|
| 106 | 106 | |
| 107 | - if ($normalizedPath !== $path) { |
|
| 108 | - $output->writeln("\t<error>Entry \"" . $fullPath . '" will not be accessible due to incompatible encoding</error>'); |
|
| 109 | - } |
|
| 110 | - } |
|
| 107 | + if ($normalizedPath !== $path) { |
|
| 108 | + $output->writeln("\t<error>Entry \"" . $fullPath . '" will not be accessible due to incompatible encoding</error>'); |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - protected function scanFiles($user, $path, $verbose, OutputInterface $output, $backgroundScan = false) { |
|
| 113 | - $connection = $this->reconnectToDatabase($output); |
|
| 114 | - $scanner = new \OC\Files\Utils\Scanner($user, $connection, \OC::$server->getLogger()); |
|
| 115 | - # check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception |
|
| 116 | - # printout and count |
|
| 117 | - if ($verbose) { |
|
| 118 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { |
|
| 119 | - $output->writeln("\tFile <info>$path</info>"); |
|
| 120 | - $this->filesCounter += 1; |
|
| 121 | - if ($this->hasBeenInterrupted()) { |
|
| 122 | - throw new InterruptedException(); |
|
| 123 | - } |
|
| 124 | - }); |
|
| 125 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) { |
|
| 126 | - $output->writeln("\tFolder <info>$path</info>"); |
|
| 127 | - $this->foldersCounter += 1; |
|
| 128 | - if ($this->hasBeenInterrupted()) { |
|
| 129 | - throw new InterruptedException(); |
|
| 130 | - } |
|
| 131 | - }); |
|
| 132 | - $scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) { |
|
| 133 | - $output->writeln("Error while scanning, storage not available (" . $e->getMessage() . ")"); |
|
| 134 | - }); |
|
| 135 | - # count only |
|
| 136 | - } else { |
|
| 137 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function () use ($output) { |
|
| 138 | - $this->filesCounter += 1; |
|
| 139 | - if ($this->hasBeenInterrupted()) { |
|
| 140 | - throw new InterruptedException(); |
|
| 141 | - } |
|
| 142 | - }); |
|
| 143 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function () use ($output) { |
|
| 144 | - $this->foldersCounter += 1; |
|
| 145 | - if ($this->hasBeenInterrupted()) { |
|
| 146 | - throw new InterruptedException(); |
|
| 147 | - } |
|
| 148 | - }); |
|
| 149 | - } |
|
| 150 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { |
|
| 151 | - $this->checkScanWarning($path, $output); |
|
| 152 | - }); |
|
| 153 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) { |
|
| 154 | - $this->checkScanWarning($path, $output); |
|
| 155 | - }); |
|
| 112 | + protected function scanFiles($user, $path, $verbose, OutputInterface $output, $backgroundScan = false) { |
|
| 113 | + $connection = $this->reconnectToDatabase($output); |
|
| 114 | + $scanner = new \OC\Files\Utils\Scanner($user, $connection, \OC::$server->getLogger()); |
|
| 115 | + # check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception |
|
| 116 | + # printout and count |
|
| 117 | + if ($verbose) { |
|
| 118 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { |
|
| 119 | + $output->writeln("\tFile <info>$path</info>"); |
|
| 120 | + $this->filesCounter += 1; |
|
| 121 | + if ($this->hasBeenInterrupted()) { |
|
| 122 | + throw new InterruptedException(); |
|
| 123 | + } |
|
| 124 | + }); |
|
| 125 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) { |
|
| 126 | + $output->writeln("\tFolder <info>$path</info>"); |
|
| 127 | + $this->foldersCounter += 1; |
|
| 128 | + if ($this->hasBeenInterrupted()) { |
|
| 129 | + throw new InterruptedException(); |
|
| 130 | + } |
|
| 131 | + }); |
|
| 132 | + $scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) { |
|
| 133 | + $output->writeln("Error while scanning, storage not available (" . $e->getMessage() . ")"); |
|
| 134 | + }); |
|
| 135 | + # count only |
|
| 136 | + } else { |
|
| 137 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function () use ($output) { |
|
| 138 | + $this->filesCounter += 1; |
|
| 139 | + if ($this->hasBeenInterrupted()) { |
|
| 140 | + throw new InterruptedException(); |
|
| 141 | + } |
|
| 142 | + }); |
|
| 143 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function () use ($output) { |
|
| 144 | + $this->foldersCounter += 1; |
|
| 145 | + if ($this->hasBeenInterrupted()) { |
|
| 146 | + throw new InterruptedException(); |
|
| 147 | + } |
|
| 148 | + }); |
|
| 149 | + } |
|
| 150 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { |
|
| 151 | + $this->checkScanWarning($path, $output); |
|
| 152 | + }); |
|
| 153 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) { |
|
| 154 | + $this->checkScanWarning($path, $output); |
|
| 155 | + }); |
|
| 156 | 156 | |
| 157 | - try { |
|
| 158 | - if ($backgroundScan) { |
|
| 159 | - $scanner->backgroundScan($path); |
|
| 160 | - } else { |
|
| 161 | - $scanner->scan($path); |
|
| 162 | - } |
|
| 163 | - } catch (ForbiddenException $e) { |
|
| 164 | - $output->writeln("<error>Home storage for user $user not writable</error>"); |
|
| 165 | - $output->writeln("Make sure you're running the scan command only as the user the web server runs as"); |
|
| 166 | - } catch (InterruptedException $e) { |
|
| 167 | - # exit the function if ctrl-c has been pressed |
|
| 168 | - $output->writeln('Interrupted by user'); |
|
| 169 | - } catch (NotFoundException $e) { |
|
| 170 | - $output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>'); |
|
| 171 | - } catch (\Exception $e) { |
|
| 172 | - $output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>'); |
|
| 173 | - $output->writeln('<error>' . $e->getTraceAsString() . '</error>'); |
|
| 174 | - } |
|
| 175 | - } |
|
| 157 | + try { |
|
| 158 | + if ($backgroundScan) { |
|
| 159 | + $scanner->backgroundScan($path); |
|
| 160 | + } else { |
|
| 161 | + $scanner->scan($path); |
|
| 162 | + } |
|
| 163 | + } catch (ForbiddenException $e) { |
|
| 164 | + $output->writeln("<error>Home storage for user $user not writable</error>"); |
|
| 165 | + $output->writeln("Make sure you're running the scan command only as the user the web server runs as"); |
|
| 166 | + } catch (InterruptedException $e) { |
|
| 167 | + # exit the function if ctrl-c has been pressed |
|
| 168 | + $output->writeln('Interrupted by user'); |
|
| 169 | + } catch (NotFoundException $e) { |
|
| 170 | + $output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>'); |
|
| 171 | + } catch (\Exception $e) { |
|
| 172 | + $output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>'); |
|
| 173 | + $output->writeln('<error>' . $e->getTraceAsString() . '</error>'); |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | 177 | |
| 178 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 179 | - $inputPath = $input->getOption('path'); |
|
| 180 | - if ($inputPath) { |
|
| 181 | - $inputPath = '/' . trim($inputPath, '/'); |
|
| 182 | - list (, $user,) = explode('/', $inputPath, 3); |
|
| 183 | - $users = array($user); |
|
| 184 | - } else if ($input->getOption('all')) { |
|
| 185 | - $users = $this->userManager->search(''); |
|
| 186 | - } else { |
|
| 187 | - $users = $input->getArgument('user_id'); |
|
| 188 | - } |
|
| 178 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 179 | + $inputPath = $input->getOption('path'); |
|
| 180 | + if ($inputPath) { |
|
| 181 | + $inputPath = '/' . trim($inputPath, '/'); |
|
| 182 | + list (, $user,) = explode('/', $inputPath, 3); |
|
| 183 | + $users = array($user); |
|
| 184 | + } else if ($input->getOption('all')) { |
|
| 185 | + $users = $this->userManager->search(''); |
|
| 186 | + } else { |
|
| 187 | + $users = $input->getArgument('user_id'); |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | - # no messaging level option means: no full printout but statistics |
|
| 191 | - # $quiet means no print at all |
|
| 192 | - # $verbose means full printout including statistics |
|
| 193 | - # -q -v full stat |
|
| 194 | - # 0 0 no yes |
|
| 195 | - # 0 1 yes yes |
|
| 196 | - # 1 -- no no (quiet overrules verbose) |
|
| 197 | - $verbose = $input->getOption('verbose'); |
|
| 198 | - $quiet = $input->getOption('quiet'); |
|
| 199 | - # restrict the verbosity level to VERBOSITY_VERBOSE |
|
| 200 | - if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) { |
|
| 201 | - $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); |
|
| 202 | - } |
|
| 203 | - if ($quiet) { |
|
| 204 | - $verbose = false; |
|
| 205 | - } |
|
| 190 | + # no messaging level option means: no full printout but statistics |
|
| 191 | + # $quiet means no print at all |
|
| 192 | + # $verbose means full printout including statistics |
|
| 193 | + # -q -v full stat |
|
| 194 | + # 0 0 no yes |
|
| 195 | + # 0 1 yes yes |
|
| 196 | + # 1 -- no no (quiet overrules verbose) |
|
| 197 | + $verbose = $input->getOption('verbose'); |
|
| 198 | + $quiet = $input->getOption('quiet'); |
|
| 199 | + # restrict the verbosity level to VERBOSITY_VERBOSE |
|
| 200 | + if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) { |
|
| 201 | + $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); |
|
| 202 | + } |
|
| 203 | + if ($quiet) { |
|
| 204 | + $verbose = false; |
|
| 205 | + } |
|
| 206 | 206 | |
| 207 | - # check quantity of users to be process and show it on the command line |
|
| 208 | - $users_total = count($users); |
|
| 209 | - if ($users_total === 0) { |
|
| 210 | - $output->writeln("<error>Please specify the user id to scan, \"--all\" to scan for all users or \"--path=...\"</error>"); |
|
| 211 | - return; |
|
| 212 | - } else { |
|
| 213 | - if ($users_total > 1) { |
|
| 214 | - $output->writeln("\nScanning files for $users_total users"); |
|
| 215 | - } |
|
| 216 | - } |
|
| 207 | + # check quantity of users to be process and show it on the command line |
|
| 208 | + $users_total = count($users); |
|
| 209 | + if ($users_total === 0) { |
|
| 210 | + $output->writeln("<error>Please specify the user id to scan, \"--all\" to scan for all users or \"--path=...\"</error>"); |
|
| 211 | + return; |
|
| 212 | + } else { |
|
| 213 | + if ($users_total > 1) { |
|
| 214 | + $output->writeln("\nScanning files for $users_total users"); |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | 217 | |
| 218 | - $this->initTools(); |
|
| 218 | + $this->initTools(); |
|
| 219 | 219 | |
| 220 | - $user_count = 0; |
|
| 221 | - foreach ($users as $user) { |
|
| 222 | - if (is_object($user)) { |
|
| 223 | - $user = $user->getUID(); |
|
| 224 | - } |
|
| 225 | - $path = $inputPath ? $inputPath : '/' . $user; |
|
| 226 | - $user_count += 1; |
|
| 227 | - if ($this->userManager->userExists($user)) { |
|
| 228 | - # add an extra line when verbose is set to optical separate users |
|
| 229 | - if ($verbose) { |
|
| 230 | - $output->writeln(""); |
|
| 231 | - } |
|
| 232 | - $output->writeln("Starting scan for user $user_count out of $users_total ($user)"); |
|
| 233 | - # full: printout data if $verbose was set |
|
| 234 | - $this->scanFiles($user, $path, $verbose, $output, $input->getOption('unscanned')); |
|
| 235 | - } else { |
|
| 236 | - $output->writeln("<error>Unknown user $user_count $user</error>"); |
|
| 237 | - } |
|
| 238 | - # check on each user if there was a user interrupt (ctrl-c) and exit foreach |
|
| 239 | - if ($this->hasBeenInterrupted()) { |
|
| 240 | - break; |
|
| 241 | - } |
|
| 242 | - } |
|
| 220 | + $user_count = 0; |
|
| 221 | + foreach ($users as $user) { |
|
| 222 | + if (is_object($user)) { |
|
| 223 | + $user = $user->getUID(); |
|
| 224 | + } |
|
| 225 | + $path = $inputPath ? $inputPath : '/' . $user; |
|
| 226 | + $user_count += 1; |
|
| 227 | + if ($this->userManager->userExists($user)) { |
|
| 228 | + # add an extra line when verbose is set to optical separate users |
|
| 229 | + if ($verbose) { |
|
| 230 | + $output->writeln(""); |
|
| 231 | + } |
|
| 232 | + $output->writeln("Starting scan for user $user_count out of $users_total ($user)"); |
|
| 233 | + # full: printout data if $verbose was set |
|
| 234 | + $this->scanFiles($user, $path, $verbose, $output, $input->getOption('unscanned')); |
|
| 235 | + } else { |
|
| 236 | + $output->writeln("<error>Unknown user $user_count $user</error>"); |
|
| 237 | + } |
|
| 238 | + # check on each user if there was a user interrupt (ctrl-c) and exit foreach |
|
| 239 | + if ($this->hasBeenInterrupted()) { |
|
| 240 | + break; |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | 243 | |
| 244 | - # stat: printout statistics if $quiet was not set |
|
| 245 | - if (!$quiet) { |
|
| 246 | - $this->presentStats($output); |
|
| 247 | - } |
|
| 248 | - } |
|
| 244 | + # stat: printout statistics if $quiet was not set |
|
| 245 | + if (!$quiet) { |
|
| 246 | + $this->presentStats($output); |
|
| 247 | + } |
|
| 248 | + } |
|
| 249 | 249 | |
| 250 | - /** |
|
| 251 | - * Initialises some useful tools for the Command |
|
| 252 | - */ |
|
| 253 | - protected function initTools() { |
|
| 254 | - // Start the timer |
|
| 255 | - $this->execTime = -microtime(true); |
|
| 256 | - // Convert PHP errors to exceptions |
|
| 257 | - set_error_handler([$this, 'exceptionErrorHandler'], E_ALL); |
|
| 258 | - } |
|
| 250 | + /** |
|
| 251 | + * Initialises some useful tools for the Command |
|
| 252 | + */ |
|
| 253 | + protected function initTools() { |
|
| 254 | + // Start the timer |
|
| 255 | + $this->execTime = -microtime(true); |
|
| 256 | + // Convert PHP errors to exceptions |
|
| 257 | + set_error_handler([$this, 'exceptionErrorHandler'], E_ALL); |
|
| 258 | + } |
|
| 259 | 259 | |
| 260 | - /** |
|
| 261 | - * Processes PHP errors as exceptions in order to be able to keep track of problems |
|
| 262 | - * |
|
| 263 | - * @see https://secure.php.net/manual/en/function.set-error-handler.php |
|
| 264 | - * |
|
| 265 | - * @param int $severity the level of the error raised |
|
| 266 | - * @param string $message |
|
| 267 | - * @param string $file the filename that the error was raised in |
|
| 268 | - * @param int $line the line number the error was raised |
|
| 269 | - * |
|
| 270 | - * @throws \ErrorException |
|
| 271 | - */ |
|
| 272 | - public function exceptionErrorHandler($severity, $message, $file, $line) { |
|
| 273 | - if (!(error_reporting() & $severity)) { |
|
| 274 | - // This error code is not included in error_reporting |
|
| 275 | - return; |
|
| 276 | - } |
|
| 277 | - throw new \ErrorException($message, 0, $severity, $file, $line); |
|
| 278 | - } |
|
| 260 | + /** |
|
| 261 | + * Processes PHP errors as exceptions in order to be able to keep track of problems |
|
| 262 | + * |
|
| 263 | + * @see https://secure.php.net/manual/en/function.set-error-handler.php |
|
| 264 | + * |
|
| 265 | + * @param int $severity the level of the error raised |
|
| 266 | + * @param string $message |
|
| 267 | + * @param string $file the filename that the error was raised in |
|
| 268 | + * @param int $line the line number the error was raised |
|
| 269 | + * |
|
| 270 | + * @throws \ErrorException |
|
| 271 | + */ |
|
| 272 | + public function exceptionErrorHandler($severity, $message, $file, $line) { |
|
| 273 | + if (!(error_reporting() & $severity)) { |
|
| 274 | + // This error code is not included in error_reporting |
|
| 275 | + return; |
|
| 276 | + } |
|
| 277 | + throw new \ErrorException($message, 0, $severity, $file, $line); |
|
| 278 | + } |
|
| 279 | 279 | |
| 280 | - /** |
|
| 281 | - * @param OutputInterface $output |
|
| 282 | - */ |
|
| 283 | - protected function presentStats(OutputInterface $output) { |
|
| 284 | - // Stop the timer |
|
| 285 | - $this->execTime += microtime(true); |
|
| 286 | - $output->writeln(""); |
|
| 280 | + /** |
|
| 281 | + * @param OutputInterface $output |
|
| 282 | + */ |
|
| 283 | + protected function presentStats(OutputInterface $output) { |
|
| 284 | + // Stop the timer |
|
| 285 | + $this->execTime += microtime(true); |
|
| 286 | + $output->writeln(""); |
|
| 287 | 287 | |
| 288 | - $headers = [ |
|
| 289 | - 'Folders', 'Files', 'Elapsed time' |
|
| 290 | - ]; |
|
| 288 | + $headers = [ |
|
| 289 | + 'Folders', 'Files', 'Elapsed time' |
|
| 290 | + ]; |
|
| 291 | 291 | |
| 292 | - $this->showSummary($headers, null, $output); |
|
| 293 | - } |
|
| 292 | + $this->showSummary($headers, null, $output); |
|
| 293 | + } |
|
| 294 | 294 | |
| 295 | - /** |
|
| 296 | - * Shows a summary of operations |
|
| 297 | - * |
|
| 298 | - * @param string[] $headers |
|
| 299 | - * @param string[] $rows |
|
| 300 | - * @param OutputInterface $output |
|
| 301 | - */ |
|
| 302 | - protected function showSummary($headers, $rows, OutputInterface $output) { |
|
| 303 | - $niceDate = $this->formatExecTime(); |
|
| 304 | - if (!$rows) { |
|
| 305 | - $rows = [ |
|
| 306 | - $this->foldersCounter, |
|
| 307 | - $this->filesCounter, |
|
| 308 | - $niceDate, |
|
| 309 | - ]; |
|
| 310 | - } |
|
| 311 | - $table = new Table($output); |
|
| 312 | - $table |
|
| 313 | - ->setHeaders($headers) |
|
| 314 | - ->setRows([$rows]); |
|
| 315 | - $table->render(); |
|
| 316 | - } |
|
| 295 | + /** |
|
| 296 | + * Shows a summary of operations |
|
| 297 | + * |
|
| 298 | + * @param string[] $headers |
|
| 299 | + * @param string[] $rows |
|
| 300 | + * @param OutputInterface $output |
|
| 301 | + */ |
|
| 302 | + protected function showSummary($headers, $rows, OutputInterface $output) { |
|
| 303 | + $niceDate = $this->formatExecTime(); |
|
| 304 | + if (!$rows) { |
|
| 305 | + $rows = [ |
|
| 306 | + $this->foldersCounter, |
|
| 307 | + $this->filesCounter, |
|
| 308 | + $niceDate, |
|
| 309 | + ]; |
|
| 310 | + } |
|
| 311 | + $table = new Table($output); |
|
| 312 | + $table |
|
| 313 | + ->setHeaders($headers) |
|
| 314 | + ->setRows([$rows]); |
|
| 315 | + $table->render(); |
|
| 316 | + } |
|
| 317 | 317 | |
| 318 | 318 | |
| 319 | - /** |
|
| 320 | - * Formats microtime into a human readable format |
|
| 321 | - * |
|
| 322 | - * @return string |
|
| 323 | - */ |
|
| 324 | - protected function formatExecTime() { |
|
| 325 | - list($secs, ) = explode('.', sprintf("%.1f", $this->execTime)); |
|
| 319 | + /** |
|
| 320 | + * Formats microtime into a human readable format |
|
| 321 | + * |
|
| 322 | + * @return string |
|
| 323 | + */ |
|
| 324 | + protected function formatExecTime() { |
|
| 325 | + list($secs, ) = explode('.', sprintf("%.1f", $this->execTime)); |
|
| 326 | 326 | |
| 327 | - # if you want to have microseconds add this: . '.' . $tens; |
|
| 328 | - return date('H:i:s', $secs); |
|
| 329 | - } |
|
| 327 | + # if you want to have microseconds add this: . '.' . $tens; |
|
| 328 | + return date('H:i:s', $secs); |
|
| 329 | + } |
|
| 330 | 330 | |
| 331 | - /** |
|
| 332 | - * @return \OCP\IDBConnection |
|
| 333 | - */ |
|
| 334 | - protected function reconnectToDatabase(OutputInterface $output) { |
|
| 335 | - /** @var Connection | IDBConnection $connection */ |
|
| 336 | - $connection = \OC::$server->getDatabaseConnection(); |
|
| 337 | - try { |
|
| 338 | - $connection->close(); |
|
| 339 | - } catch (\Exception $ex) { |
|
| 340 | - $output->writeln("<info>Error while disconnecting from database: {$ex->getMessage()}</info>"); |
|
| 341 | - } |
|
| 342 | - while (!$connection->isConnected()) { |
|
| 343 | - try { |
|
| 344 | - $connection->connect(); |
|
| 345 | - } catch (\Exception $ex) { |
|
| 346 | - $output->writeln("<info>Error while re-connecting to database: {$ex->getMessage()}</info>"); |
|
| 347 | - sleep(60); |
|
| 348 | - } |
|
| 349 | - } |
|
| 350 | - return $connection; |
|
| 351 | - } |
|
| 331 | + /** |
|
| 332 | + * @return \OCP\IDBConnection |
|
| 333 | + */ |
|
| 334 | + protected function reconnectToDatabase(OutputInterface $output) { |
|
| 335 | + /** @var Connection | IDBConnection $connection */ |
|
| 336 | + $connection = \OC::$server->getDatabaseConnection(); |
|
| 337 | + try { |
|
| 338 | + $connection->close(); |
|
| 339 | + } catch (\Exception $ex) { |
|
| 340 | + $output->writeln("<info>Error while disconnecting from database: {$ex->getMessage()}</info>"); |
|
| 341 | + } |
|
| 342 | + while (!$connection->isConnected()) { |
|
| 343 | + try { |
|
| 344 | + $connection->connect(); |
|
| 345 | + } catch (\Exception $ex) { |
|
| 346 | + $output->writeln("<info>Error while re-connecting to database: {$ex->getMessage()}</info>"); |
|
| 347 | + sleep(60); |
|
| 348 | + } |
|
| 349 | + } |
|
| 350 | + return $connection; |
|
| 351 | + } |
|
| 352 | 352 | |
| 353 | 353 | } |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | $path = basename($fullPath); |
| 106 | 106 | |
| 107 | 107 | if ($normalizedPath !== $path) { |
| 108 | - $output->writeln("\t<error>Entry \"" . $fullPath . '" will not be accessible due to incompatible encoding</error>'); |
|
| 108 | + $output->writeln("\t<error>Entry \"".$fullPath.'" will not be accessible due to incompatible encoding</error>'); |
|
| 109 | 109 | } |
| 110 | 110 | } |
| 111 | 111 | |
@@ -115,42 +115,42 @@ discard block |
||
| 115 | 115 | # check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception |
| 116 | 116 | # printout and count |
| 117 | 117 | if ($verbose) { |
| 118 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { |
|
| 118 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function($path) use ($output) { |
|
| 119 | 119 | $output->writeln("\tFile <info>$path</info>"); |
| 120 | 120 | $this->filesCounter += 1; |
| 121 | 121 | if ($this->hasBeenInterrupted()) { |
| 122 | 122 | throw new InterruptedException(); |
| 123 | 123 | } |
| 124 | 124 | }); |
| 125 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) { |
|
| 125 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function($path) use ($output) { |
|
| 126 | 126 | $output->writeln("\tFolder <info>$path</info>"); |
| 127 | 127 | $this->foldersCounter += 1; |
| 128 | 128 | if ($this->hasBeenInterrupted()) { |
| 129 | 129 | throw new InterruptedException(); |
| 130 | 130 | } |
| 131 | 131 | }); |
| 132 | - $scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) { |
|
| 133 | - $output->writeln("Error while scanning, storage not available (" . $e->getMessage() . ")"); |
|
| 132 | + $scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function(StorageNotAvailableException $e) use ($output) { |
|
| 133 | + $output->writeln("Error while scanning, storage not available (".$e->getMessage().")"); |
|
| 134 | 134 | }); |
| 135 | 135 | # count only |
| 136 | 136 | } else { |
| 137 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function () use ($output) { |
|
| 137 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function() use ($output) { |
|
| 138 | 138 | $this->filesCounter += 1; |
| 139 | 139 | if ($this->hasBeenInterrupted()) { |
| 140 | 140 | throw new InterruptedException(); |
| 141 | 141 | } |
| 142 | 142 | }); |
| 143 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function () use ($output) { |
|
| 143 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function() use ($output) { |
|
| 144 | 144 | $this->foldersCounter += 1; |
| 145 | 145 | if ($this->hasBeenInterrupted()) { |
| 146 | 146 | throw new InterruptedException(); |
| 147 | 147 | } |
| 148 | 148 | }); |
| 149 | 149 | } |
| 150 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { |
|
| 150 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function($path) use ($output) { |
|
| 151 | 151 | $this->checkScanWarning($path, $output); |
| 152 | 152 | }); |
| 153 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) { |
|
| 153 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function($path) use ($output) { |
|
| 154 | 154 | $this->checkScanWarning($path, $output); |
| 155 | 155 | }); |
| 156 | 156 | |
@@ -167,10 +167,10 @@ discard block |
||
| 167 | 167 | # exit the function if ctrl-c has been pressed |
| 168 | 168 | $output->writeln('Interrupted by user'); |
| 169 | 169 | } catch (NotFoundException $e) { |
| 170 | - $output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>'); |
|
| 170 | + $output->writeln('<error>Path not found: '.$e->getMessage().'</error>'); |
|
| 171 | 171 | } catch (\Exception $e) { |
| 172 | - $output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>'); |
|
| 173 | - $output->writeln('<error>' . $e->getTraceAsString() . '</error>'); |
|
| 172 | + $output->writeln('<error>Exception during scan: '.$e->getMessage().'</error>'); |
|
| 173 | + $output->writeln('<error>'.$e->getTraceAsString().'</error>'); |
|
| 174 | 174 | } |
| 175 | 175 | } |
| 176 | 176 | |
@@ -178,7 +178,7 @@ discard block |
||
| 178 | 178 | protected function execute(InputInterface $input, OutputInterface $output) { |
| 179 | 179 | $inputPath = $input->getOption('path'); |
| 180 | 180 | if ($inputPath) { |
| 181 | - $inputPath = '/' . trim($inputPath, '/'); |
|
| 181 | + $inputPath = '/'.trim($inputPath, '/'); |
|
| 182 | 182 | list (, $user,) = explode('/', $inputPath, 3); |
| 183 | 183 | $users = array($user); |
| 184 | 184 | } else if ($input->getOption('all')) { |
@@ -222,7 +222,7 @@ discard block |
||
| 222 | 222 | if (is_object($user)) { |
| 223 | 223 | $user = $user->getUID(); |
| 224 | 224 | } |
| 225 | - $path = $inputPath ? $inputPath : '/' . $user; |
|
| 225 | + $path = $inputPath ? $inputPath : '/'.$user; |
|
| 226 | 226 | $user_count += 1; |
| 227 | 227 | if ($this->userManager->userExists($user)) { |
| 228 | 228 | # add an extra line when verbose is set to optical separate users |
@@ -322,7 +322,7 @@ discard block |
||
| 322 | 322 | * @return string |
| 323 | 323 | */ |
| 324 | 324 | protected function formatExecTime() { |
| 325 | - list($secs, ) = explode('.', sprintf("%.1f", $this->execTime)); |
|
| 325 | + list($secs,) = explode('.', sprintf("%.1f", $this->execTime)); |
|
| 326 | 326 | |
| 327 | 327 | # if you want to have microseconds add this: . '.' . $tens; |
| 328 | 328 | return date('H:i:s', $secs); |
@@ -28,40 +28,40 @@ |
||
| 28 | 28 | namespace OCA\Files; |
| 29 | 29 | |
| 30 | 30 | class App { |
| 31 | - /** |
|
| 32 | - * @var \OCP\INavigationManager |
|
| 33 | - */ |
|
| 34 | - private static $navigationManager; |
|
| 31 | + /** |
|
| 32 | + * @var \OCP\INavigationManager |
|
| 33 | + */ |
|
| 34 | + private static $navigationManager; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Returns the app's navigation manager |
|
| 38 | - * |
|
| 39 | - * @return \OCP\INavigationManager |
|
| 40 | - */ |
|
| 41 | - public static function getNavigationManager() { |
|
| 42 | - // TODO: move this into a service in the Application class |
|
| 43 | - if (self::$navigationManager === null) { |
|
| 44 | - self::$navigationManager = new \OC\NavigationManager( |
|
| 45 | - \OC::$server->getAppManager(), |
|
| 46 | - \OC::$server->getURLGenerator(), |
|
| 47 | - \OC::$server->getL10NFactory(), |
|
| 48 | - \OC::$server->getUserSession(), |
|
| 49 | - \OC::$server->getGroupManager(), |
|
| 50 | - \OC::$server->getConfig() |
|
| 51 | - ); |
|
| 52 | - self::$navigationManager->clear(false); |
|
| 53 | - } |
|
| 54 | - return self::$navigationManager; |
|
| 55 | - } |
|
| 36 | + /** |
|
| 37 | + * Returns the app's navigation manager |
|
| 38 | + * |
|
| 39 | + * @return \OCP\INavigationManager |
|
| 40 | + */ |
|
| 41 | + public static function getNavigationManager() { |
|
| 42 | + // TODO: move this into a service in the Application class |
|
| 43 | + if (self::$navigationManager === null) { |
|
| 44 | + self::$navigationManager = new \OC\NavigationManager( |
|
| 45 | + \OC::$server->getAppManager(), |
|
| 46 | + \OC::$server->getURLGenerator(), |
|
| 47 | + \OC::$server->getL10NFactory(), |
|
| 48 | + \OC::$server->getUserSession(), |
|
| 49 | + \OC::$server->getGroupManager(), |
|
| 50 | + \OC::$server->getConfig() |
|
| 51 | + ); |
|
| 52 | + self::$navigationManager->clear(false); |
|
| 53 | + } |
|
| 54 | + return self::$navigationManager; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - public static function extendJsConfig($settings) { |
|
| 58 | - $appConfig = json_decode($settings['array']['oc_appconfig'], true); |
|
| 57 | + public static function extendJsConfig($settings) { |
|
| 58 | + $appConfig = json_decode($settings['array']['oc_appconfig'], true); |
|
| 59 | 59 | |
| 60 | - $maxChunkSize = (int)\OC::$server->getConfig()->getAppValue('files', 'max_chunk_size', 10 * 1024 * 1024); |
|
| 61 | - $appConfig['files'] = [ |
|
| 62 | - 'max_chunk_size' => $maxChunkSize |
|
| 63 | - ]; |
|
| 60 | + $maxChunkSize = (int)\OC::$server->getConfig()->getAppValue('files', 'max_chunk_size', 10 * 1024 * 1024); |
|
| 61 | + $appConfig['files'] = [ |
|
| 62 | + 'max_chunk_size' => $maxChunkSize |
|
| 63 | + ]; |
|
| 64 | 64 | |
| 65 | - $settings['array']['oc_appconfig'] = json_encode($appConfig); |
|
| 66 | - } |
|
| 65 | + $settings['array']['oc_appconfig'] = json_encode($appConfig); |
|
| 66 | + } |
|
| 67 | 67 | } |
@@ -57,7 +57,7 @@ |
||
| 57 | 57 | public static function extendJsConfig($settings) { |
| 58 | 58 | $appConfig = json_decode($settings['array']['oc_appconfig'], true); |
| 59 | 59 | |
| 60 | - $maxChunkSize = (int)\OC::$server->getConfig()->getAppValue('files', 'max_chunk_size', 10 * 1024 * 1024); |
|
| 60 | + $maxChunkSize = (int) \OC::$server->getConfig()->getAppValue('files', 'max_chunk_size', 10 * 1024 * 1024); |
|
| 61 | 61 | $appConfig['files'] = [ |
| 62 | 62 | 'max_chunk_size' => $maxChunkSize |
| 63 | 63 | ]; |
@@ -32,208 +32,208 @@ |
||
| 32 | 32 | |
| 33 | 33 | class Backend { |
| 34 | 34 | |
| 35 | - /** @var IDBConnection */ |
|
| 36 | - private $db; |
|
| 37 | - /** @var IUserManager */ |
|
| 38 | - private $userManager; |
|
| 39 | - /** @var IGroupManager */ |
|
| 40 | - private $groupManager; |
|
| 41 | - /** @var Principal */ |
|
| 42 | - private $principalBackend; |
|
| 43 | - /** @var string */ |
|
| 44 | - private $resourceType; |
|
| 45 | - |
|
| 46 | - const ACCESS_OWNER = 1; |
|
| 47 | - const ACCESS_READ_WRITE = 2; |
|
| 48 | - const ACCESS_READ = 3; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @param IDBConnection $db |
|
| 52 | - * @param IUserManager $userManager |
|
| 53 | - * @param IGroupManager $groupManager |
|
| 54 | - * @param Principal $principalBackend |
|
| 55 | - * @param string $resourceType |
|
| 56 | - */ |
|
| 57 | - public function __construct(IDBConnection $db, IUserManager $userManager, IGroupManager $groupManager, Principal $principalBackend, $resourceType) { |
|
| 58 | - $this->db = $db; |
|
| 59 | - $this->userManager = $userManager; |
|
| 60 | - $this->groupManager = $groupManager; |
|
| 61 | - $this->principalBackend = $principalBackend; |
|
| 62 | - $this->resourceType = $resourceType; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @param IShareable $shareable |
|
| 67 | - * @param string[] $add |
|
| 68 | - * @param string[] $remove |
|
| 69 | - */ |
|
| 70 | - public function updateShares($shareable, $add, $remove) { |
|
| 71 | - foreach($add as $element) { |
|
| 72 | - $this->shareWith($shareable, $element); |
|
| 73 | - } |
|
| 74 | - foreach($remove as $element) { |
|
| 75 | - $this->unshare($shareable, $element); |
|
| 76 | - } |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * @param IShareable $shareable |
|
| 81 | - * @param string $element |
|
| 82 | - */ |
|
| 83 | - private function shareWith($shareable, $element) { |
|
| 84 | - $user = $element['href']; |
|
| 85 | - $parts = explode(':', $user, 2); |
|
| 86 | - if ($parts[0] !== 'principal') { |
|
| 87 | - return; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - // don't share with owner |
|
| 91 | - if ($shareable->getOwner() === $parts[1]) { |
|
| 92 | - return; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - $principal = explode('/', $parts[1], 3); |
|
| 96 | - if (count($principal) !== 3 || $principal[0] !== 'principals' || !in_array($principal[1], ['users', 'groups'], true)) { |
|
| 97 | - // Invalid principal |
|
| 98 | - return; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - if (($principal[1] === 'users' && !$this->userManager->userExists($principal[2])) || |
|
| 102 | - ($principal[1] === 'groups' && !$this->groupManager->groupExists($principal[2]))) { |
|
| 103 | - // User or group does not exist |
|
| 104 | - return; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - // remove the share if it already exists |
|
| 108 | - $this->unshare($shareable, $element['href']); |
|
| 109 | - $access = self::ACCESS_READ; |
|
| 110 | - if (isset($element['readOnly'])) { |
|
| 111 | - $access = $element['readOnly'] ? self::ACCESS_READ : self::ACCESS_READ_WRITE; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - $query = $this->db->getQueryBuilder(); |
|
| 115 | - $query->insert('dav_shares') |
|
| 116 | - ->values([ |
|
| 117 | - 'principaluri' => $query->createNamedParameter($parts[1]), |
|
| 118 | - 'type' => $query->createNamedParameter($this->resourceType), |
|
| 119 | - 'access' => $query->createNamedParameter($access), |
|
| 120 | - 'resourceid' => $query->createNamedParameter($shareable->getResourceId()) |
|
| 121 | - ]); |
|
| 122 | - $query->execute(); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * @param $resourceId |
|
| 127 | - */ |
|
| 128 | - public function deleteAllShares($resourceId) { |
|
| 129 | - $query = $this->db->getQueryBuilder(); |
|
| 130 | - $query->delete('dav_shares') |
|
| 131 | - ->where($query->expr()->eq('resourceid', $query->createNamedParameter($resourceId))) |
|
| 132 | - ->andWhere($query->expr()->eq('type', $query->createNamedParameter($this->resourceType))) |
|
| 133 | - ->execute(); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - public function deleteAllSharesByUser($principaluri) { |
|
| 137 | - $query = $this->db->getQueryBuilder(); |
|
| 138 | - $query->delete('dav_shares') |
|
| 139 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principaluri))) |
|
| 140 | - ->andWhere($query->expr()->eq('type', $query->createNamedParameter($this->resourceType))) |
|
| 141 | - ->execute(); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * @param IShareable $shareable |
|
| 146 | - * @param string $element |
|
| 147 | - */ |
|
| 148 | - private function unshare($shareable, $element) { |
|
| 149 | - $parts = explode(':', $element, 2); |
|
| 150 | - if ($parts[0] !== 'principal') { |
|
| 151 | - return; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - // don't share with owner |
|
| 155 | - if ($shareable->getOwner() === $parts[1]) { |
|
| 156 | - return; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - $query = $this->db->getQueryBuilder(); |
|
| 160 | - $query->delete('dav_shares') |
|
| 161 | - ->where($query->expr()->eq('resourceid', $query->createNamedParameter($shareable->getResourceId()))) |
|
| 162 | - ->andWhere($query->expr()->eq('type', $query->createNamedParameter($this->resourceType))) |
|
| 163 | - ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($parts[1]))) |
|
| 164 | - ; |
|
| 165 | - $query->execute(); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Returns the list of people whom this resource is shared with. |
|
| 170 | - * |
|
| 171 | - * Every element in this array should have the following properties: |
|
| 172 | - * * href - Often a mailto: address |
|
| 173 | - * * commonName - Optional, for example a first + last name |
|
| 174 | - * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
| 175 | - * * readOnly - boolean |
|
| 176 | - * * summary - Optional, a description for the share |
|
| 177 | - * |
|
| 178 | - * @param int $resourceId |
|
| 179 | - * @return array |
|
| 180 | - */ |
|
| 181 | - public function getShares($resourceId) { |
|
| 182 | - $query = $this->db->getQueryBuilder(); |
|
| 183 | - $result = $query->select(['principaluri', 'access']) |
|
| 184 | - ->from('dav_shares') |
|
| 185 | - ->where($query->expr()->eq('resourceid', $query->createNamedParameter($resourceId))) |
|
| 186 | - ->andWhere($query->expr()->eq('type', $query->createNamedParameter($this->resourceType))) |
|
| 187 | - ->execute(); |
|
| 188 | - |
|
| 189 | - $shares = []; |
|
| 190 | - while($row = $result->fetch()) { |
|
| 191 | - $p = $this->principalBackend->getPrincipalByPath($row['principaluri']); |
|
| 192 | - $shares[]= [ |
|
| 193 | - 'href' => "principal:${row['principaluri']}", |
|
| 194 | - 'commonName' => isset($p['{DAV:}displayname']) ? $p['{DAV:}displayname'] : '', |
|
| 195 | - 'status' => 1, |
|
| 196 | - 'readOnly' => (int) $row['access'] === self::ACCESS_READ, |
|
| 197 | - '{http://owncloud.org/ns}principal' => $row['principaluri'], |
|
| 198 | - '{http://owncloud.org/ns}group-share' => is_null($p) |
|
| 199 | - ]; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - return $shares; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * For shared resources the sharee is set in the ACL of the resource |
|
| 207 | - * |
|
| 208 | - * @param int $resourceId |
|
| 209 | - * @param array $acl |
|
| 210 | - * @return array |
|
| 211 | - */ |
|
| 212 | - public function applyShareAcl($resourceId, $acl) { |
|
| 213 | - |
|
| 214 | - $shares = $this->getShares($resourceId); |
|
| 215 | - foreach ($shares as $share) { |
|
| 216 | - $acl[] = [ |
|
| 217 | - 'privilege' => '{DAV:}read', |
|
| 218 | - 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
| 219 | - 'protected' => true, |
|
| 220 | - ]; |
|
| 221 | - if (!$share['readOnly']) { |
|
| 222 | - $acl[] = [ |
|
| 223 | - 'privilege' => '{DAV:}write', |
|
| 224 | - 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
| 225 | - 'protected' => true, |
|
| 226 | - ]; |
|
| 227 | - } else if ($this->resourceType === 'calendar') { |
|
| 228 | - // Allow changing the properties of read only calendars, |
|
| 229 | - // so users can change the visibility. |
|
| 230 | - $acl[] = [ |
|
| 231 | - 'privilege' => '{DAV:}write-properties', |
|
| 232 | - 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
| 233 | - 'protected' => true, |
|
| 234 | - ]; |
|
| 235 | - } |
|
| 236 | - } |
|
| 237 | - return $acl; |
|
| 238 | - } |
|
| 35 | + /** @var IDBConnection */ |
|
| 36 | + private $db; |
|
| 37 | + /** @var IUserManager */ |
|
| 38 | + private $userManager; |
|
| 39 | + /** @var IGroupManager */ |
|
| 40 | + private $groupManager; |
|
| 41 | + /** @var Principal */ |
|
| 42 | + private $principalBackend; |
|
| 43 | + /** @var string */ |
|
| 44 | + private $resourceType; |
|
| 45 | + |
|
| 46 | + const ACCESS_OWNER = 1; |
|
| 47 | + const ACCESS_READ_WRITE = 2; |
|
| 48 | + const ACCESS_READ = 3; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @param IDBConnection $db |
|
| 52 | + * @param IUserManager $userManager |
|
| 53 | + * @param IGroupManager $groupManager |
|
| 54 | + * @param Principal $principalBackend |
|
| 55 | + * @param string $resourceType |
|
| 56 | + */ |
|
| 57 | + public function __construct(IDBConnection $db, IUserManager $userManager, IGroupManager $groupManager, Principal $principalBackend, $resourceType) { |
|
| 58 | + $this->db = $db; |
|
| 59 | + $this->userManager = $userManager; |
|
| 60 | + $this->groupManager = $groupManager; |
|
| 61 | + $this->principalBackend = $principalBackend; |
|
| 62 | + $this->resourceType = $resourceType; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @param IShareable $shareable |
|
| 67 | + * @param string[] $add |
|
| 68 | + * @param string[] $remove |
|
| 69 | + */ |
|
| 70 | + public function updateShares($shareable, $add, $remove) { |
|
| 71 | + foreach($add as $element) { |
|
| 72 | + $this->shareWith($shareable, $element); |
|
| 73 | + } |
|
| 74 | + foreach($remove as $element) { |
|
| 75 | + $this->unshare($shareable, $element); |
|
| 76 | + } |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * @param IShareable $shareable |
|
| 81 | + * @param string $element |
|
| 82 | + */ |
|
| 83 | + private function shareWith($shareable, $element) { |
|
| 84 | + $user = $element['href']; |
|
| 85 | + $parts = explode(':', $user, 2); |
|
| 86 | + if ($parts[0] !== 'principal') { |
|
| 87 | + return; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + // don't share with owner |
|
| 91 | + if ($shareable->getOwner() === $parts[1]) { |
|
| 92 | + return; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + $principal = explode('/', $parts[1], 3); |
|
| 96 | + if (count($principal) !== 3 || $principal[0] !== 'principals' || !in_array($principal[1], ['users', 'groups'], true)) { |
|
| 97 | + // Invalid principal |
|
| 98 | + return; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + if (($principal[1] === 'users' && !$this->userManager->userExists($principal[2])) || |
|
| 102 | + ($principal[1] === 'groups' && !$this->groupManager->groupExists($principal[2]))) { |
|
| 103 | + // User or group does not exist |
|
| 104 | + return; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + // remove the share if it already exists |
|
| 108 | + $this->unshare($shareable, $element['href']); |
|
| 109 | + $access = self::ACCESS_READ; |
|
| 110 | + if (isset($element['readOnly'])) { |
|
| 111 | + $access = $element['readOnly'] ? self::ACCESS_READ : self::ACCESS_READ_WRITE; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + $query = $this->db->getQueryBuilder(); |
|
| 115 | + $query->insert('dav_shares') |
|
| 116 | + ->values([ |
|
| 117 | + 'principaluri' => $query->createNamedParameter($parts[1]), |
|
| 118 | + 'type' => $query->createNamedParameter($this->resourceType), |
|
| 119 | + 'access' => $query->createNamedParameter($access), |
|
| 120 | + 'resourceid' => $query->createNamedParameter($shareable->getResourceId()) |
|
| 121 | + ]); |
|
| 122 | + $query->execute(); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * @param $resourceId |
|
| 127 | + */ |
|
| 128 | + public function deleteAllShares($resourceId) { |
|
| 129 | + $query = $this->db->getQueryBuilder(); |
|
| 130 | + $query->delete('dav_shares') |
|
| 131 | + ->where($query->expr()->eq('resourceid', $query->createNamedParameter($resourceId))) |
|
| 132 | + ->andWhere($query->expr()->eq('type', $query->createNamedParameter($this->resourceType))) |
|
| 133 | + ->execute(); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + public function deleteAllSharesByUser($principaluri) { |
|
| 137 | + $query = $this->db->getQueryBuilder(); |
|
| 138 | + $query->delete('dav_shares') |
|
| 139 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principaluri))) |
|
| 140 | + ->andWhere($query->expr()->eq('type', $query->createNamedParameter($this->resourceType))) |
|
| 141 | + ->execute(); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * @param IShareable $shareable |
|
| 146 | + * @param string $element |
|
| 147 | + */ |
|
| 148 | + private function unshare($shareable, $element) { |
|
| 149 | + $parts = explode(':', $element, 2); |
|
| 150 | + if ($parts[0] !== 'principal') { |
|
| 151 | + return; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + // don't share with owner |
|
| 155 | + if ($shareable->getOwner() === $parts[1]) { |
|
| 156 | + return; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + $query = $this->db->getQueryBuilder(); |
|
| 160 | + $query->delete('dav_shares') |
|
| 161 | + ->where($query->expr()->eq('resourceid', $query->createNamedParameter($shareable->getResourceId()))) |
|
| 162 | + ->andWhere($query->expr()->eq('type', $query->createNamedParameter($this->resourceType))) |
|
| 163 | + ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($parts[1]))) |
|
| 164 | + ; |
|
| 165 | + $query->execute(); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Returns the list of people whom this resource is shared with. |
|
| 170 | + * |
|
| 171 | + * Every element in this array should have the following properties: |
|
| 172 | + * * href - Often a mailto: address |
|
| 173 | + * * commonName - Optional, for example a first + last name |
|
| 174 | + * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
| 175 | + * * readOnly - boolean |
|
| 176 | + * * summary - Optional, a description for the share |
|
| 177 | + * |
|
| 178 | + * @param int $resourceId |
|
| 179 | + * @return array |
|
| 180 | + */ |
|
| 181 | + public function getShares($resourceId) { |
|
| 182 | + $query = $this->db->getQueryBuilder(); |
|
| 183 | + $result = $query->select(['principaluri', 'access']) |
|
| 184 | + ->from('dav_shares') |
|
| 185 | + ->where($query->expr()->eq('resourceid', $query->createNamedParameter($resourceId))) |
|
| 186 | + ->andWhere($query->expr()->eq('type', $query->createNamedParameter($this->resourceType))) |
|
| 187 | + ->execute(); |
|
| 188 | + |
|
| 189 | + $shares = []; |
|
| 190 | + while($row = $result->fetch()) { |
|
| 191 | + $p = $this->principalBackend->getPrincipalByPath($row['principaluri']); |
|
| 192 | + $shares[]= [ |
|
| 193 | + 'href' => "principal:${row['principaluri']}", |
|
| 194 | + 'commonName' => isset($p['{DAV:}displayname']) ? $p['{DAV:}displayname'] : '', |
|
| 195 | + 'status' => 1, |
|
| 196 | + 'readOnly' => (int) $row['access'] === self::ACCESS_READ, |
|
| 197 | + '{http://owncloud.org/ns}principal' => $row['principaluri'], |
|
| 198 | + '{http://owncloud.org/ns}group-share' => is_null($p) |
|
| 199 | + ]; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + return $shares; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * For shared resources the sharee is set in the ACL of the resource |
|
| 207 | + * |
|
| 208 | + * @param int $resourceId |
|
| 209 | + * @param array $acl |
|
| 210 | + * @return array |
|
| 211 | + */ |
|
| 212 | + public function applyShareAcl($resourceId, $acl) { |
|
| 213 | + |
|
| 214 | + $shares = $this->getShares($resourceId); |
|
| 215 | + foreach ($shares as $share) { |
|
| 216 | + $acl[] = [ |
|
| 217 | + 'privilege' => '{DAV:}read', |
|
| 218 | + 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
| 219 | + 'protected' => true, |
|
| 220 | + ]; |
|
| 221 | + if (!$share['readOnly']) { |
|
| 222 | + $acl[] = [ |
|
| 223 | + 'privilege' => '{DAV:}write', |
|
| 224 | + 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
| 225 | + 'protected' => true, |
|
| 226 | + ]; |
|
| 227 | + } else if ($this->resourceType === 'calendar') { |
|
| 228 | + // Allow changing the properties of read only calendars, |
|
| 229 | + // so users can change the visibility. |
|
| 230 | + $acl[] = [ |
|
| 231 | + 'privilege' => '{DAV:}write-properties', |
|
| 232 | + 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
| 233 | + 'protected' => true, |
|
| 234 | + ]; |
|
| 235 | + } |
|
| 236 | + } |
|
| 237 | + return $acl; |
|
| 238 | + } |
|
| 239 | 239 | } |
@@ -39,169 +39,169 @@ |
||
| 39 | 39 | */ |
| 40 | 40 | class SystemTagsObjectMappingCollection implements ICollection { |
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * @var string |
|
| 44 | - */ |
|
| 45 | - private $objectId; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @var string |
|
| 49 | - */ |
|
| 50 | - private $objectType; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @var ISystemTagManager |
|
| 54 | - */ |
|
| 55 | - private $tagManager; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @var ISystemTagObjectMapper |
|
| 59 | - */ |
|
| 60 | - private $tagMapper; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * User |
|
| 64 | - * |
|
| 65 | - * @var IUser |
|
| 66 | - */ |
|
| 67 | - private $user; |
|
| 68 | - |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Constructor |
|
| 72 | - * |
|
| 73 | - * @param string $objectId object id |
|
| 74 | - * @param string $objectType object type |
|
| 75 | - * @param IUser $user user |
|
| 76 | - * @param ISystemTagManager $tagManager tag manager |
|
| 77 | - * @param ISystemTagObjectMapper $tagMapper tag mapper |
|
| 78 | - */ |
|
| 79 | - public function __construct( |
|
| 80 | - $objectId, |
|
| 81 | - $objectType, |
|
| 82 | - IUser $user, |
|
| 83 | - ISystemTagManager $tagManager, |
|
| 84 | - ISystemTagObjectMapper $tagMapper |
|
| 85 | - ) { |
|
| 86 | - $this->tagManager = $tagManager; |
|
| 87 | - $this->tagMapper = $tagMapper; |
|
| 88 | - $this->objectId = $objectId; |
|
| 89 | - $this->objectType = $objectType; |
|
| 90 | - $this->user = $user; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - function createFile($tagId, $data = null) { |
|
| 94 | - try { |
|
| 95 | - $tags = $this->tagManager->getTagsByIds([$tagId]); |
|
| 96 | - $tag = current($tags); |
|
| 97 | - if (!$this->tagManager->canUserSeeTag($tag, $this->user)) { |
|
| 98 | - throw new PreconditionFailed('Tag with id ' . $tagId . ' does not exist, cannot assign'); |
|
| 99 | - } |
|
| 100 | - if (!$this->tagManager->canUserAssignTag($tag, $this->user)) { |
|
| 101 | - throw new Forbidden('No permission to assign tag ' . $tagId); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - $this->tagMapper->assignTags($this->objectId, $this->objectType, $tagId); |
|
| 105 | - } catch (TagNotFoundException $e) { |
|
| 106 | - throw new PreconditionFailed('Tag with id ' . $tagId . ' does not exist, cannot assign'); |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - function createDirectory($name) { |
|
| 111 | - throw new Forbidden('Permission denied to create collections'); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - function getChild($tagId) { |
|
| 115 | - try { |
|
| 116 | - if ($this->tagMapper->haveTag([$this->objectId], $this->objectType, $tagId, true)) { |
|
| 117 | - $tag = $this->tagManager->getTagsByIds([$tagId]); |
|
| 118 | - $tag = current($tag); |
|
| 119 | - if ($this->tagManager->canUserSeeTag($tag, $this->user)) { |
|
| 120 | - return $this->makeNode($tag); |
|
| 121 | - } |
|
| 122 | - } |
|
| 123 | - throw new NotFound('Tag with id ' . $tagId . ' not present for object ' . $this->objectId); |
|
| 124 | - } catch (\InvalidArgumentException $e) { |
|
| 125 | - throw new BadRequest('Invalid tag id', 0, $e); |
|
| 126 | - } catch (TagNotFoundException $e) { |
|
| 127 | - throw new NotFound('Tag with id ' . $tagId . ' not found', 0, $e); |
|
| 128 | - } |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - function getChildren() { |
|
| 132 | - $tagIds = current($this->tagMapper->getTagIdsForObjects([$this->objectId], $this->objectType)); |
|
| 133 | - if (empty($tagIds)) { |
|
| 134 | - return []; |
|
| 135 | - } |
|
| 136 | - $tags = $this->tagManager->getTagsByIds($tagIds); |
|
| 137 | - |
|
| 138 | - // filter out non-visible tags |
|
| 139 | - $tags = array_filter($tags, function($tag) { |
|
| 140 | - return $this->tagManager->canUserSeeTag($tag, $this->user); |
|
| 141 | - }); |
|
| 142 | - |
|
| 143 | - return array_values(array_map(function($tag) { |
|
| 144 | - return $this->makeNode($tag); |
|
| 145 | - }, $tags)); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - function childExists($tagId) { |
|
| 149 | - try { |
|
| 150 | - $result = $this->tagMapper->haveTag([$this->objectId], $this->objectType, $tagId, true); |
|
| 151 | - |
|
| 152 | - if ($result) { |
|
| 153 | - $tags = $this->tagManager->getTagsByIds([$tagId]); |
|
| 154 | - $tag = current($tags); |
|
| 155 | - if (!$this->tagManager->canUserSeeTag($tag, $this->user)) { |
|
| 156 | - return false; |
|
| 157 | - } |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - return $result; |
|
| 161 | - } catch (\InvalidArgumentException $e) { |
|
| 162 | - throw new BadRequest('Invalid tag id', 0, $e); |
|
| 163 | - } catch (TagNotFoundException $e) { |
|
| 164 | - return false; |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - function delete() { |
|
| 169 | - throw new Forbidden('Permission denied to delete this collection'); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - function getName() { |
|
| 173 | - return $this->objectId; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - function setName($name) { |
|
| 177 | - throw new Forbidden('Permission denied to rename this collection'); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Returns the last modification time, as a unix timestamp |
|
| 182 | - * |
|
| 183 | - * @return int |
|
| 184 | - */ |
|
| 185 | - function getLastModified() { |
|
| 186 | - return null; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Create a sabre node for the mapping of the |
|
| 191 | - * given system tag to the collection's object |
|
| 192 | - * |
|
| 193 | - * @param ISystemTag $tag |
|
| 194 | - * |
|
| 195 | - * @return SystemTagMappingNode |
|
| 196 | - */ |
|
| 197 | - private function makeNode(ISystemTag $tag) { |
|
| 198 | - return new SystemTagMappingNode( |
|
| 199 | - $tag, |
|
| 200 | - $this->objectId, |
|
| 201 | - $this->objectType, |
|
| 202 | - $this->user, |
|
| 203 | - $this->tagManager, |
|
| 204 | - $this->tagMapper |
|
| 205 | - ); |
|
| 206 | - } |
|
| 42 | + /** |
|
| 43 | + * @var string |
|
| 44 | + */ |
|
| 45 | + private $objectId; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @var string |
|
| 49 | + */ |
|
| 50 | + private $objectType; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @var ISystemTagManager |
|
| 54 | + */ |
|
| 55 | + private $tagManager; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @var ISystemTagObjectMapper |
|
| 59 | + */ |
|
| 60 | + private $tagMapper; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * User |
|
| 64 | + * |
|
| 65 | + * @var IUser |
|
| 66 | + */ |
|
| 67 | + private $user; |
|
| 68 | + |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Constructor |
|
| 72 | + * |
|
| 73 | + * @param string $objectId object id |
|
| 74 | + * @param string $objectType object type |
|
| 75 | + * @param IUser $user user |
|
| 76 | + * @param ISystemTagManager $tagManager tag manager |
|
| 77 | + * @param ISystemTagObjectMapper $tagMapper tag mapper |
|
| 78 | + */ |
|
| 79 | + public function __construct( |
|
| 80 | + $objectId, |
|
| 81 | + $objectType, |
|
| 82 | + IUser $user, |
|
| 83 | + ISystemTagManager $tagManager, |
|
| 84 | + ISystemTagObjectMapper $tagMapper |
|
| 85 | + ) { |
|
| 86 | + $this->tagManager = $tagManager; |
|
| 87 | + $this->tagMapper = $tagMapper; |
|
| 88 | + $this->objectId = $objectId; |
|
| 89 | + $this->objectType = $objectType; |
|
| 90 | + $this->user = $user; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + function createFile($tagId, $data = null) { |
|
| 94 | + try { |
|
| 95 | + $tags = $this->tagManager->getTagsByIds([$tagId]); |
|
| 96 | + $tag = current($tags); |
|
| 97 | + if (!$this->tagManager->canUserSeeTag($tag, $this->user)) { |
|
| 98 | + throw new PreconditionFailed('Tag with id ' . $tagId . ' does not exist, cannot assign'); |
|
| 99 | + } |
|
| 100 | + if (!$this->tagManager->canUserAssignTag($tag, $this->user)) { |
|
| 101 | + throw new Forbidden('No permission to assign tag ' . $tagId); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + $this->tagMapper->assignTags($this->objectId, $this->objectType, $tagId); |
|
| 105 | + } catch (TagNotFoundException $e) { |
|
| 106 | + throw new PreconditionFailed('Tag with id ' . $tagId . ' does not exist, cannot assign'); |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + function createDirectory($name) { |
|
| 111 | + throw new Forbidden('Permission denied to create collections'); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + function getChild($tagId) { |
|
| 115 | + try { |
|
| 116 | + if ($this->tagMapper->haveTag([$this->objectId], $this->objectType, $tagId, true)) { |
|
| 117 | + $tag = $this->tagManager->getTagsByIds([$tagId]); |
|
| 118 | + $tag = current($tag); |
|
| 119 | + if ($this->tagManager->canUserSeeTag($tag, $this->user)) { |
|
| 120 | + return $this->makeNode($tag); |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | + throw new NotFound('Tag with id ' . $tagId . ' not present for object ' . $this->objectId); |
|
| 124 | + } catch (\InvalidArgumentException $e) { |
|
| 125 | + throw new BadRequest('Invalid tag id', 0, $e); |
|
| 126 | + } catch (TagNotFoundException $e) { |
|
| 127 | + throw new NotFound('Tag with id ' . $tagId . ' not found', 0, $e); |
|
| 128 | + } |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + function getChildren() { |
|
| 132 | + $tagIds = current($this->tagMapper->getTagIdsForObjects([$this->objectId], $this->objectType)); |
|
| 133 | + if (empty($tagIds)) { |
|
| 134 | + return []; |
|
| 135 | + } |
|
| 136 | + $tags = $this->tagManager->getTagsByIds($tagIds); |
|
| 137 | + |
|
| 138 | + // filter out non-visible tags |
|
| 139 | + $tags = array_filter($tags, function($tag) { |
|
| 140 | + return $this->tagManager->canUserSeeTag($tag, $this->user); |
|
| 141 | + }); |
|
| 142 | + |
|
| 143 | + return array_values(array_map(function($tag) { |
|
| 144 | + return $this->makeNode($tag); |
|
| 145 | + }, $tags)); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + function childExists($tagId) { |
|
| 149 | + try { |
|
| 150 | + $result = $this->tagMapper->haveTag([$this->objectId], $this->objectType, $tagId, true); |
|
| 151 | + |
|
| 152 | + if ($result) { |
|
| 153 | + $tags = $this->tagManager->getTagsByIds([$tagId]); |
|
| 154 | + $tag = current($tags); |
|
| 155 | + if (!$this->tagManager->canUserSeeTag($tag, $this->user)) { |
|
| 156 | + return false; |
|
| 157 | + } |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + return $result; |
|
| 161 | + } catch (\InvalidArgumentException $e) { |
|
| 162 | + throw new BadRequest('Invalid tag id', 0, $e); |
|
| 163 | + } catch (TagNotFoundException $e) { |
|
| 164 | + return false; |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + function delete() { |
|
| 169 | + throw new Forbidden('Permission denied to delete this collection'); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + function getName() { |
|
| 173 | + return $this->objectId; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + function setName($name) { |
|
| 177 | + throw new Forbidden('Permission denied to rename this collection'); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Returns the last modification time, as a unix timestamp |
|
| 182 | + * |
|
| 183 | + * @return int |
|
| 184 | + */ |
|
| 185 | + function getLastModified() { |
|
| 186 | + return null; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Create a sabre node for the mapping of the |
|
| 191 | + * given system tag to the collection's object |
|
| 192 | + * |
|
| 193 | + * @param ISystemTag $tag |
|
| 194 | + * |
|
| 195 | + * @return SystemTagMappingNode |
|
| 196 | + */ |
|
| 197 | + private function makeNode(ISystemTag $tag) { |
|
| 198 | + return new SystemTagMappingNode( |
|
| 199 | + $tag, |
|
| 200 | + $this->objectId, |
|
| 201 | + $this->objectType, |
|
| 202 | + $this->user, |
|
| 203 | + $this->tagManager, |
|
| 204 | + $this->tagMapper |
|
| 205 | + ); |
|
| 206 | + } |
|
| 207 | 207 | } |
@@ -58,582 +58,582 @@ |
||
| 58 | 58 | * @property string ldapExpertUUIDGroupAttr |
| 59 | 59 | */ |
| 60 | 60 | class Connection extends LDAPUtility { |
| 61 | - private $ldapConnectionRes = null; |
|
| 62 | - private $configPrefix; |
|
| 63 | - private $configID; |
|
| 64 | - private $configured = false; |
|
| 65 | - private $hasPagedResultSupport = true; |
|
| 66 | - //whether connection should be kept on __destruct |
|
| 67 | - private $dontDestruct = false; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @var bool runtime flag that indicates whether supported primary groups are available |
|
| 71 | - */ |
|
| 72 | - public $hasPrimaryGroups = true; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @var bool runtime flag that indicates whether supported POSIX gidNumber are available |
|
| 76 | - */ |
|
| 77 | - public $hasGidNumber = true; |
|
| 78 | - |
|
| 79 | - //cache handler |
|
| 80 | - protected $cache; |
|
| 81 | - |
|
| 82 | - /** @var Configuration settings handler **/ |
|
| 83 | - protected $configuration; |
|
| 84 | - |
|
| 85 | - protected $doNotValidate = false; |
|
| 86 | - |
|
| 87 | - protected $ignoreValidation = false; |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Constructor |
|
| 91 | - * @param ILDAPWrapper $ldap |
|
| 92 | - * @param string $configPrefix a string with the prefix for the configkey column (appconfig table) |
|
| 93 | - * @param string|null $configID a string with the value for the appid column (appconfig table) or null for on-the-fly connections |
|
| 94 | - */ |
|
| 95 | - public function __construct(ILDAPWrapper $ldap, $configPrefix = '', $configID = 'user_ldap') { |
|
| 96 | - parent::__construct($ldap); |
|
| 97 | - $this->configPrefix = $configPrefix; |
|
| 98 | - $this->configID = $configID; |
|
| 99 | - $this->configuration = new Configuration($configPrefix, |
|
| 100 | - !is_null($configID)); |
|
| 101 | - $memcache = \OC::$server->getMemCacheFactory(); |
|
| 102 | - if($memcache->isAvailable()) { |
|
| 103 | - $this->cache = $memcache->createDistributed(); |
|
| 104 | - } |
|
| 105 | - $helper = new Helper(\OC::$server->getConfig()); |
|
| 106 | - $this->doNotValidate = !in_array($this->configPrefix, |
|
| 107 | - $helper->getServerConfigurationPrefixes()); |
|
| 108 | - $this->hasPagedResultSupport = |
|
| 109 | - (int)$this->configuration->ldapPagingSize !== 0 |
|
| 110 | - || $this->ldap->hasPagedResultSupport(); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - public function __destruct() { |
|
| 114 | - if(!$this->dontDestruct && $this->ldap->isResource($this->ldapConnectionRes)) { |
|
| 115 | - @$this->ldap->unbind($this->ldapConnectionRes); |
|
| 116 | - } |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * defines behaviour when the instance is cloned |
|
| 121 | - */ |
|
| 122 | - public function __clone() { |
|
| 123 | - $this->configuration = new Configuration($this->configPrefix, |
|
| 124 | - !is_null($this->configID)); |
|
| 125 | - $this->ldapConnectionRes = null; |
|
| 126 | - $this->dontDestruct = true; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * @param string $name |
|
| 131 | - * @return bool|mixed |
|
| 132 | - */ |
|
| 133 | - public function __get($name) { |
|
| 134 | - if(!$this->configured) { |
|
| 135 | - $this->readConfiguration(); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - if($name === 'hasPagedResultSupport') { |
|
| 139 | - return $this->hasPagedResultSupport; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - return $this->configuration->$name; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * @param string $name |
|
| 147 | - * @param mixed $value |
|
| 148 | - */ |
|
| 149 | - public function __set($name, $value) { |
|
| 150 | - $this->doNotValidate = false; |
|
| 151 | - $before = $this->configuration->$name; |
|
| 152 | - $this->configuration->$name = $value; |
|
| 153 | - $after = $this->configuration->$name; |
|
| 154 | - if($before !== $after) { |
|
| 155 | - if ($this->configID !== '' && $this->configID !== null) { |
|
| 156 | - $this->configuration->saveConfiguration(); |
|
| 157 | - } |
|
| 158 | - $this->validateConfiguration(); |
|
| 159 | - } |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * sets whether the result of the configuration validation shall |
|
| 164 | - * be ignored when establishing the connection. Used by the Wizard |
|
| 165 | - * in early configuration state. |
|
| 166 | - * @param bool $state |
|
| 167 | - */ |
|
| 168 | - public function setIgnoreValidation($state) { |
|
| 169 | - $this->ignoreValidation = (bool)$state; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * initializes the LDAP backend |
|
| 174 | - * @param bool $force read the config settings no matter what |
|
| 175 | - */ |
|
| 176 | - public function init($force = false) { |
|
| 177 | - $this->readConfiguration($force); |
|
| 178 | - $this->establishConnection(); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Returns the LDAP handler |
|
| 183 | - */ |
|
| 184 | - public function getConnectionResource() { |
|
| 185 | - if(!$this->ldapConnectionRes) { |
|
| 186 | - $this->init(); |
|
| 187 | - } else if(!$this->ldap->isResource($this->ldapConnectionRes)) { |
|
| 188 | - $this->ldapConnectionRes = null; |
|
| 189 | - $this->establishConnection(); |
|
| 190 | - } |
|
| 191 | - if(is_null($this->ldapConnectionRes)) { |
|
| 192 | - \OCP\Util::writeLog('user_ldap', 'No LDAP Connection to server ' . $this->configuration->ldapHost, \OCP\Util::ERROR); |
|
| 193 | - throw new ServerNotAvailableException('Connection to LDAP server could not be established'); |
|
| 194 | - } |
|
| 195 | - return $this->ldapConnectionRes; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * resets the connection resource |
|
| 200 | - */ |
|
| 201 | - public function resetConnectionResource() { |
|
| 202 | - if(!is_null($this->ldapConnectionRes)) { |
|
| 203 | - @$this->ldap->unbind($this->ldapConnectionRes); |
|
| 204 | - $this->ldapConnectionRes = null; |
|
| 205 | - } |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * @param string|null $key |
|
| 210 | - * @return string |
|
| 211 | - */ |
|
| 212 | - private function getCacheKey($key) { |
|
| 213 | - $prefix = 'LDAP-'.$this->configID.'-'.$this->configPrefix.'-'; |
|
| 214 | - if(is_null($key)) { |
|
| 215 | - return $prefix; |
|
| 216 | - } |
|
| 217 | - return $prefix.md5($key); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * @param string $key |
|
| 222 | - * @return mixed|null |
|
| 223 | - */ |
|
| 224 | - public function getFromCache($key) { |
|
| 225 | - if(!$this->configured) { |
|
| 226 | - $this->readConfiguration(); |
|
| 227 | - } |
|
| 228 | - if(is_null($this->cache) || !$this->configuration->ldapCacheTTL) { |
|
| 229 | - return null; |
|
| 230 | - } |
|
| 231 | - $key = $this->getCacheKey($key); |
|
| 232 | - |
|
| 233 | - return json_decode(base64_decode($this->cache->get($key)), true); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * @param string $key |
|
| 238 | - * @param mixed $value |
|
| 239 | - * |
|
| 240 | - * @return string |
|
| 241 | - */ |
|
| 242 | - public function writeToCache($key, $value) { |
|
| 243 | - if(!$this->configured) { |
|
| 244 | - $this->readConfiguration(); |
|
| 245 | - } |
|
| 246 | - if(is_null($this->cache) |
|
| 247 | - || !$this->configuration->ldapCacheTTL |
|
| 248 | - || !$this->configuration->ldapConfigurationActive) { |
|
| 249 | - return null; |
|
| 250 | - } |
|
| 251 | - $key = $this->getCacheKey($key); |
|
| 252 | - $value = base64_encode(json_encode($value)); |
|
| 253 | - $this->cache->set($key, $value, $this->configuration->ldapCacheTTL); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - public function clearCache() { |
|
| 257 | - if(!is_null($this->cache)) { |
|
| 258 | - $this->cache->clear($this->getCacheKey(null)); |
|
| 259 | - } |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * Caches the general LDAP configuration. |
|
| 264 | - * @param bool $force optional. true, if the re-read should be forced. defaults |
|
| 265 | - * to false. |
|
| 266 | - * @return null |
|
| 267 | - */ |
|
| 268 | - private function readConfiguration($force = false) { |
|
| 269 | - if((!$this->configured || $force) && !is_null($this->configID)) { |
|
| 270 | - $this->configuration->readConfiguration(); |
|
| 271 | - $this->configured = $this->validateConfiguration(); |
|
| 272 | - } |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * set LDAP configuration with values delivered by an array, not read from configuration |
|
| 277 | - * @param array $config array that holds the config parameters in an associated array |
|
| 278 | - * @param array &$setParameters optional; array where the set fields will be given to |
|
| 279 | - * @return boolean true if config validates, false otherwise. Check with $setParameters for detailed success on single parameters |
|
| 280 | - */ |
|
| 281 | - public function setConfiguration($config, &$setParameters = null) { |
|
| 282 | - if(is_null($setParameters)) { |
|
| 283 | - $setParameters = array(); |
|
| 284 | - } |
|
| 285 | - $this->doNotValidate = false; |
|
| 286 | - $this->configuration->setConfiguration($config, $setParameters); |
|
| 287 | - if(count($setParameters) > 0) { |
|
| 288 | - $this->configured = $this->validateConfiguration(); |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - |
|
| 292 | - return $this->configured; |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - /** |
|
| 296 | - * saves the current Configuration in the database and empties the |
|
| 297 | - * cache |
|
| 298 | - * @return null |
|
| 299 | - */ |
|
| 300 | - public function saveConfiguration() { |
|
| 301 | - $this->configuration->saveConfiguration(); |
|
| 302 | - $this->clearCache(); |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * get the current LDAP configuration |
|
| 307 | - * @return array |
|
| 308 | - */ |
|
| 309 | - public function getConfiguration() { |
|
| 310 | - $this->readConfiguration(); |
|
| 311 | - $config = $this->configuration->getConfiguration(); |
|
| 312 | - $cta = $this->configuration->getConfigTranslationArray(); |
|
| 313 | - $result = array(); |
|
| 314 | - foreach($cta as $dbkey => $configkey) { |
|
| 315 | - switch($configkey) { |
|
| 316 | - case 'homeFolderNamingRule': |
|
| 317 | - if(strpos($config[$configkey], 'attr:') === 0) { |
|
| 318 | - $result[$dbkey] = substr($config[$configkey], 5); |
|
| 319 | - } else { |
|
| 320 | - $result[$dbkey] = ''; |
|
| 321 | - } |
|
| 322 | - break; |
|
| 323 | - case 'ldapBase': |
|
| 324 | - case 'ldapBaseUsers': |
|
| 325 | - case 'ldapBaseGroups': |
|
| 326 | - case 'ldapAttributesForUserSearch': |
|
| 327 | - case 'ldapAttributesForGroupSearch': |
|
| 328 | - if(is_array($config[$configkey])) { |
|
| 329 | - $result[$dbkey] = implode("\n", $config[$configkey]); |
|
| 330 | - break; |
|
| 331 | - } //else follows default |
|
| 332 | - default: |
|
| 333 | - $result[$dbkey] = $config[$configkey]; |
|
| 334 | - } |
|
| 335 | - } |
|
| 336 | - return $result; |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - private function doSoftValidation() { |
|
| 340 | - //if User or Group Base are not set, take over Base DN setting |
|
| 341 | - foreach(array('ldapBaseUsers', 'ldapBaseGroups') as $keyBase) { |
|
| 342 | - $val = $this->configuration->$keyBase; |
|
| 343 | - if(empty($val)) { |
|
| 344 | - $this->configuration->$keyBase = $this->configuration->ldapBase; |
|
| 345 | - } |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - foreach(array('ldapExpertUUIDUserAttr' => 'ldapUuidUserAttribute', |
|
| 349 | - 'ldapExpertUUIDGroupAttr' => 'ldapUuidGroupAttribute') |
|
| 350 | - as $expertSetting => $effectiveSetting) { |
|
| 351 | - $uuidOverride = $this->configuration->$expertSetting; |
|
| 352 | - if(!empty($uuidOverride)) { |
|
| 353 | - $this->configuration->$effectiveSetting = $uuidOverride; |
|
| 354 | - } else { |
|
| 355 | - $uuidAttributes = Access::UUID_ATTRIBUTES; |
|
| 356 | - array_unshift($uuidAttributes, 'auto'); |
|
| 357 | - if(!in_array($this->configuration->$effectiveSetting, |
|
| 358 | - $uuidAttributes) |
|
| 359 | - && (!is_null($this->configID))) { |
|
| 360 | - $this->configuration->$effectiveSetting = 'auto'; |
|
| 361 | - $this->configuration->saveConfiguration(); |
|
| 362 | - \OCP\Util::writeLog('user_ldap', |
|
| 363 | - 'Illegal value for the '. |
|
| 364 | - $effectiveSetting.', '.'reset to '. |
|
| 365 | - 'autodetect.', \OCP\Util::INFO); |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - } |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - $backupPort = (int)$this->configuration->ldapBackupPort; |
|
| 372 | - if ($backupPort <= 0) { |
|
| 373 | - $this->configuration->backupPort = $this->configuration->ldapPort; |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - //make sure empty search attributes are saved as simple, empty array |
|
| 377 | - $saKeys = array('ldapAttributesForUserSearch', |
|
| 378 | - 'ldapAttributesForGroupSearch'); |
|
| 379 | - foreach($saKeys as $key) { |
|
| 380 | - $val = $this->configuration->$key; |
|
| 381 | - if(is_array($val) && count($val) === 1 && empty($val[0])) { |
|
| 382 | - $this->configuration->$key = array(); |
|
| 383 | - } |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - if((stripos($this->configuration->ldapHost, 'ldaps://') === 0) |
|
| 387 | - && $this->configuration->ldapTLS) { |
|
| 388 | - $this->configuration->ldapTLS = false; |
|
| 389 | - \OCP\Util::writeLog('user_ldap', |
|
| 390 | - 'LDAPS (already using secure connection) and '. |
|
| 391 | - 'TLS do not work together. Switched off TLS.', |
|
| 392 | - \OCP\Util::INFO); |
|
| 393 | - } |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - /** |
|
| 397 | - * @return bool |
|
| 398 | - */ |
|
| 399 | - private function doCriticalValidation() { |
|
| 400 | - $configurationOK = true; |
|
| 401 | - $errorStr = 'Configuration Error (prefix '. |
|
| 402 | - (string)$this->configPrefix .'): '; |
|
| 403 | - |
|
| 404 | - //options that shall not be empty |
|
| 405 | - $options = array('ldapHost', 'ldapPort', 'ldapUserDisplayName', |
|
| 406 | - 'ldapGroupDisplayName', 'ldapLoginFilter'); |
|
| 407 | - foreach($options as $key) { |
|
| 408 | - $val = $this->configuration->$key; |
|
| 409 | - if(empty($val)) { |
|
| 410 | - switch($key) { |
|
| 411 | - case 'ldapHost': |
|
| 412 | - $subj = 'LDAP Host'; |
|
| 413 | - break; |
|
| 414 | - case 'ldapPort': |
|
| 415 | - $subj = 'LDAP Port'; |
|
| 416 | - break; |
|
| 417 | - case 'ldapUserDisplayName': |
|
| 418 | - $subj = 'LDAP User Display Name'; |
|
| 419 | - break; |
|
| 420 | - case 'ldapGroupDisplayName': |
|
| 421 | - $subj = 'LDAP Group Display Name'; |
|
| 422 | - break; |
|
| 423 | - case 'ldapLoginFilter': |
|
| 424 | - $subj = 'LDAP Login Filter'; |
|
| 425 | - break; |
|
| 426 | - default: |
|
| 427 | - $subj = $key; |
|
| 428 | - break; |
|
| 429 | - } |
|
| 430 | - $configurationOK = false; |
|
| 431 | - \OCP\Util::writeLog('user_ldap', |
|
| 432 | - $errorStr.'No '.$subj.' given!', |
|
| 433 | - \OCP\Util::WARN); |
|
| 434 | - } |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - //combinations |
|
| 438 | - $agent = $this->configuration->ldapAgentName; |
|
| 439 | - $pwd = $this->configuration->ldapAgentPassword; |
|
| 440 | - if ( |
|
| 441 | - ($agent === '' && $pwd !== '') |
|
| 442 | - || ($agent !== '' && $pwd === '') |
|
| 443 | - ) { |
|
| 444 | - \OCP\Util::writeLog('user_ldap', |
|
| 445 | - $errorStr.'either no password is given for the '. |
|
| 446 | - 'user agent or a password is given, but not an '. |
|
| 447 | - 'LDAP agent.', |
|
| 448 | - \OCP\Util::WARN); |
|
| 449 | - $configurationOK = false; |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - $base = $this->configuration->ldapBase; |
|
| 453 | - $baseUsers = $this->configuration->ldapBaseUsers; |
|
| 454 | - $baseGroups = $this->configuration->ldapBaseGroups; |
|
| 455 | - |
|
| 456 | - if(empty($base) && empty($baseUsers) && empty($baseGroups)) { |
|
| 457 | - \OCP\Util::writeLog('user_ldap', |
|
| 458 | - $errorStr.'Not a single Base DN given.', |
|
| 459 | - \OCP\Util::WARN); |
|
| 460 | - $configurationOK = false; |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - if(mb_strpos($this->configuration->ldapLoginFilter, '%uid', 0, 'UTF-8') |
|
| 464 | - === false) { |
|
| 465 | - \OCP\Util::writeLog('user_ldap', |
|
| 466 | - $errorStr.'login filter does not contain %uid '. |
|
| 467 | - 'place holder.', |
|
| 468 | - \OCP\Util::WARN); |
|
| 469 | - $configurationOK = false; |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - return $configurationOK; |
|
| 473 | - } |
|
| 474 | - |
|
| 475 | - /** |
|
| 476 | - * Validates the user specified configuration |
|
| 477 | - * @return bool true if configuration seems OK, false otherwise |
|
| 478 | - */ |
|
| 479 | - private function validateConfiguration() { |
|
| 480 | - |
|
| 481 | - if($this->doNotValidate) { |
|
| 482 | - //don't do a validation if it is a new configuration with pure |
|
| 483 | - //default values. Will be allowed on changes via __set or |
|
| 484 | - //setConfiguration |
|
| 485 | - return false; |
|
| 486 | - } |
|
| 487 | - |
|
| 488 | - // first step: "soft" checks: settings that are not really |
|
| 489 | - // necessary, but advisable. If left empty, give an info message |
|
| 490 | - $this->doSoftValidation(); |
|
| 491 | - |
|
| 492 | - //second step: critical checks. If left empty or filled wrong, mark as |
|
| 493 | - //not configured and give a warning. |
|
| 494 | - return $this->doCriticalValidation(); |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - |
|
| 498 | - /** |
|
| 499 | - * Connects and Binds to LDAP |
|
| 500 | - */ |
|
| 501 | - private function establishConnection() { |
|
| 502 | - if(!$this->configuration->ldapConfigurationActive) { |
|
| 503 | - return null; |
|
| 504 | - } |
|
| 505 | - static $phpLDAPinstalled = true; |
|
| 506 | - if(!$phpLDAPinstalled) { |
|
| 507 | - return false; |
|
| 508 | - } |
|
| 509 | - if(!$this->ignoreValidation && !$this->configured) { |
|
| 510 | - \OCP\Util::writeLog('user_ldap', |
|
| 511 | - 'Configuration is invalid, cannot connect', |
|
| 512 | - \OCP\Util::WARN); |
|
| 513 | - return false; |
|
| 514 | - } |
|
| 515 | - if(!$this->ldapConnectionRes) { |
|
| 516 | - if(!$this->ldap->areLDAPFunctionsAvailable()) { |
|
| 517 | - $phpLDAPinstalled = false; |
|
| 518 | - \OCP\Util::writeLog('user_ldap', |
|
| 519 | - 'function ldap_connect is not available. Make '. |
|
| 520 | - 'sure that the PHP ldap module is installed.', |
|
| 521 | - \OCP\Util::ERROR); |
|
| 522 | - |
|
| 523 | - return false; |
|
| 524 | - } |
|
| 525 | - if($this->configuration->turnOffCertCheck) { |
|
| 526 | - if(putenv('LDAPTLS_REQCERT=never')) { |
|
| 527 | - \OCP\Util::writeLog('user_ldap', |
|
| 528 | - 'Turned off SSL certificate validation successfully.', |
|
| 529 | - \OCP\Util::DEBUG); |
|
| 530 | - } else { |
|
| 531 | - \OCP\Util::writeLog('user_ldap', |
|
| 532 | - 'Could not turn off SSL certificate validation.', |
|
| 533 | - \OCP\Util::WARN); |
|
| 534 | - } |
|
| 535 | - } |
|
| 536 | - |
|
| 537 | - $isOverrideMainServer = ($this->configuration->ldapOverrideMainServer |
|
| 538 | - || $this->getFromCache('overrideMainServer')); |
|
| 539 | - $isBackupHost = (trim($this->configuration->ldapBackupHost) !== ""); |
|
| 540 | - $bindStatus = false; |
|
| 541 | - $error = -1; |
|
| 542 | - try { |
|
| 543 | - if (!$isOverrideMainServer) { |
|
| 544 | - $this->doConnect($this->configuration->ldapHost, |
|
| 545 | - $this->configuration->ldapPort); |
|
| 546 | - $bindStatus = $this->bind(); |
|
| 547 | - $error = $this->ldap->isResource($this->ldapConnectionRes) ? |
|
| 548 | - $this->ldap->errno($this->ldapConnectionRes) : -1; |
|
| 549 | - } |
|
| 550 | - if($bindStatus === true) { |
|
| 551 | - return $bindStatus; |
|
| 552 | - } |
|
| 553 | - } catch (ServerNotAvailableException $e) { |
|
| 554 | - if(!$isBackupHost) { |
|
| 555 | - throw $e; |
|
| 556 | - } |
|
| 557 | - } |
|
| 558 | - |
|
| 559 | - //if LDAP server is not reachable, try the Backup (Replica!) Server |
|
| 560 | - if($isBackupHost && ($error !== 0 || $isOverrideMainServer)) { |
|
| 561 | - $this->doConnect($this->configuration->ldapBackupHost, |
|
| 562 | - $this->configuration->ldapBackupPort); |
|
| 563 | - $bindStatus = $this->bind(); |
|
| 564 | - $error = $this->ldap->isResource($this->ldapConnectionRes) ? |
|
| 565 | - $this->ldap->errno($this->ldapConnectionRes) : -1; |
|
| 566 | - if($bindStatus && $error === 0 && !$this->getFromCache('overrideMainServer')) { |
|
| 567 | - //when bind to backup server succeeded and failed to main server, |
|
| 568 | - //skip contacting him until next cache refresh |
|
| 569 | - $this->writeToCache('overrideMainServer', true); |
|
| 570 | - } |
|
| 571 | - } |
|
| 572 | - |
|
| 573 | - return $bindStatus; |
|
| 574 | - } |
|
| 575 | - return null; |
|
| 576 | - } |
|
| 577 | - |
|
| 578 | - /** |
|
| 579 | - * @param string $host |
|
| 580 | - * @param string $port |
|
| 581 | - * @return bool |
|
| 582 | - * @throws \OC\ServerNotAvailableException |
|
| 583 | - */ |
|
| 584 | - private function doConnect($host, $port) { |
|
| 585 | - if ($host === '') { |
|
| 586 | - return false; |
|
| 587 | - } |
|
| 588 | - |
|
| 589 | - $this->ldapConnectionRes = $this->ldap->connect($host, $port); |
|
| 590 | - |
|
| 591 | - if(!$this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_PROTOCOL_VERSION, 3)) { |
|
| 592 | - throw new ServerNotAvailableException('Could not set required LDAP Protocol version.'); |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - if(!$this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_REFERRALS, 0)) { |
|
| 596 | - throw new ServerNotAvailableException('Could not disable LDAP referrals.'); |
|
| 597 | - } |
|
| 598 | - |
|
| 599 | - if($this->configuration->ldapTLS) { |
|
| 600 | - if(!$this->ldap->startTls($this->ldapConnectionRes)) { |
|
| 601 | - throw new ServerNotAvailableException('Start TLS failed, when connecting to LDAP host ' . $host . '.'); |
|
| 602 | - } |
|
| 603 | - } |
|
| 604 | - |
|
| 605 | - return true; |
|
| 606 | - } |
|
| 607 | - |
|
| 608 | - /** |
|
| 609 | - * Binds to LDAP |
|
| 610 | - */ |
|
| 611 | - public function bind() { |
|
| 612 | - if(!$this->configuration->ldapConfigurationActive) { |
|
| 613 | - return false; |
|
| 614 | - } |
|
| 615 | - $cr = $this->getConnectionResource(); |
|
| 616 | - if(!$this->ldap->isResource($cr)) { |
|
| 617 | - return false; |
|
| 618 | - } |
|
| 619 | - $ldapLogin = @$this->ldap->bind($cr, |
|
| 620 | - $this->configuration->ldapAgentName, |
|
| 621 | - $this->configuration->ldapAgentPassword); |
|
| 622 | - if(!$ldapLogin) { |
|
| 623 | - $errno = $this->ldap->errno($cr); |
|
| 624 | - |
|
| 625 | - \OCP\Util::writeLog('user_ldap', |
|
| 626 | - 'Bind failed: ' . $errno . ': ' . $this->ldap->error($cr), |
|
| 627 | - \OCP\Util::WARN); |
|
| 628 | - |
|
| 629 | - // Set to failure mode, if LDAP error code is not LDAP_SUCCESS or LDAP_INVALID_CREDENTIALS |
|
| 630 | - if($errno !== 0x00 && $errno !== 0x31) { |
|
| 631 | - $this->ldapConnectionRes = null; |
|
| 632 | - } |
|
| 633 | - |
|
| 634 | - return false; |
|
| 635 | - } |
|
| 636 | - return true; |
|
| 637 | - } |
|
| 61 | + private $ldapConnectionRes = null; |
|
| 62 | + private $configPrefix; |
|
| 63 | + private $configID; |
|
| 64 | + private $configured = false; |
|
| 65 | + private $hasPagedResultSupport = true; |
|
| 66 | + //whether connection should be kept on __destruct |
|
| 67 | + private $dontDestruct = false; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @var bool runtime flag that indicates whether supported primary groups are available |
|
| 71 | + */ |
|
| 72 | + public $hasPrimaryGroups = true; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @var bool runtime flag that indicates whether supported POSIX gidNumber are available |
|
| 76 | + */ |
|
| 77 | + public $hasGidNumber = true; |
|
| 78 | + |
|
| 79 | + //cache handler |
|
| 80 | + protected $cache; |
|
| 81 | + |
|
| 82 | + /** @var Configuration settings handler **/ |
|
| 83 | + protected $configuration; |
|
| 84 | + |
|
| 85 | + protected $doNotValidate = false; |
|
| 86 | + |
|
| 87 | + protected $ignoreValidation = false; |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Constructor |
|
| 91 | + * @param ILDAPWrapper $ldap |
|
| 92 | + * @param string $configPrefix a string with the prefix for the configkey column (appconfig table) |
|
| 93 | + * @param string|null $configID a string with the value for the appid column (appconfig table) or null for on-the-fly connections |
|
| 94 | + */ |
|
| 95 | + public function __construct(ILDAPWrapper $ldap, $configPrefix = '', $configID = 'user_ldap') { |
|
| 96 | + parent::__construct($ldap); |
|
| 97 | + $this->configPrefix = $configPrefix; |
|
| 98 | + $this->configID = $configID; |
|
| 99 | + $this->configuration = new Configuration($configPrefix, |
|
| 100 | + !is_null($configID)); |
|
| 101 | + $memcache = \OC::$server->getMemCacheFactory(); |
|
| 102 | + if($memcache->isAvailable()) { |
|
| 103 | + $this->cache = $memcache->createDistributed(); |
|
| 104 | + } |
|
| 105 | + $helper = new Helper(\OC::$server->getConfig()); |
|
| 106 | + $this->doNotValidate = !in_array($this->configPrefix, |
|
| 107 | + $helper->getServerConfigurationPrefixes()); |
|
| 108 | + $this->hasPagedResultSupport = |
|
| 109 | + (int)$this->configuration->ldapPagingSize !== 0 |
|
| 110 | + || $this->ldap->hasPagedResultSupport(); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + public function __destruct() { |
|
| 114 | + if(!$this->dontDestruct && $this->ldap->isResource($this->ldapConnectionRes)) { |
|
| 115 | + @$this->ldap->unbind($this->ldapConnectionRes); |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * defines behaviour when the instance is cloned |
|
| 121 | + */ |
|
| 122 | + public function __clone() { |
|
| 123 | + $this->configuration = new Configuration($this->configPrefix, |
|
| 124 | + !is_null($this->configID)); |
|
| 125 | + $this->ldapConnectionRes = null; |
|
| 126 | + $this->dontDestruct = true; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * @param string $name |
|
| 131 | + * @return bool|mixed |
|
| 132 | + */ |
|
| 133 | + public function __get($name) { |
|
| 134 | + if(!$this->configured) { |
|
| 135 | + $this->readConfiguration(); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + if($name === 'hasPagedResultSupport') { |
|
| 139 | + return $this->hasPagedResultSupport; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + return $this->configuration->$name; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * @param string $name |
|
| 147 | + * @param mixed $value |
|
| 148 | + */ |
|
| 149 | + public function __set($name, $value) { |
|
| 150 | + $this->doNotValidate = false; |
|
| 151 | + $before = $this->configuration->$name; |
|
| 152 | + $this->configuration->$name = $value; |
|
| 153 | + $after = $this->configuration->$name; |
|
| 154 | + if($before !== $after) { |
|
| 155 | + if ($this->configID !== '' && $this->configID !== null) { |
|
| 156 | + $this->configuration->saveConfiguration(); |
|
| 157 | + } |
|
| 158 | + $this->validateConfiguration(); |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * sets whether the result of the configuration validation shall |
|
| 164 | + * be ignored when establishing the connection. Used by the Wizard |
|
| 165 | + * in early configuration state. |
|
| 166 | + * @param bool $state |
|
| 167 | + */ |
|
| 168 | + public function setIgnoreValidation($state) { |
|
| 169 | + $this->ignoreValidation = (bool)$state; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * initializes the LDAP backend |
|
| 174 | + * @param bool $force read the config settings no matter what |
|
| 175 | + */ |
|
| 176 | + public function init($force = false) { |
|
| 177 | + $this->readConfiguration($force); |
|
| 178 | + $this->establishConnection(); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Returns the LDAP handler |
|
| 183 | + */ |
|
| 184 | + public function getConnectionResource() { |
|
| 185 | + if(!$this->ldapConnectionRes) { |
|
| 186 | + $this->init(); |
|
| 187 | + } else if(!$this->ldap->isResource($this->ldapConnectionRes)) { |
|
| 188 | + $this->ldapConnectionRes = null; |
|
| 189 | + $this->establishConnection(); |
|
| 190 | + } |
|
| 191 | + if(is_null($this->ldapConnectionRes)) { |
|
| 192 | + \OCP\Util::writeLog('user_ldap', 'No LDAP Connection to server ' . $this->configuration->ldapHost, \OCP\Util::ERROR); |
|
| 193 | + throw new ServerNotAvailableException('Connection to LDAP server could not be established'); |
|
| 194 | + } |
|
| 195 | + return $this->ldapConnectionRes; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * resets the connection resource |
|
| 200 | + */ |
|
| 201 | + public function resetConnectionResource() { |
|
| 202 | + if(!is_null($this->ldapConnectionRes)) { |
|
| 203 | + @$this->ldap->unbind($this->ldapConnectionRes); |
|
| 204 | + $this->ldapConnectionRes = null; |
|
| 205 | + } |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * @param string|null $key |
|
| 210 | + * @return string |
|
| 211 | + */ |
|
| 212 | + private function getCacheKey($key) { |
|
| 213 | + $prefix = 'LDAP-'.$this->configID.'-'.$this->configPrefix.'-'; |
|
| 214 | + if(is_null($key)) { |
|
| 215 | + return $prefix; |
|
| 216 | + } |
|
| 217 | + return $prefix.md5($key); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * @param string $key |
|
| 222 | + * @return mixed|null |
|
| 223 | + */ |
|
| 224 | + public function getFromCache($key) { |
|
| 225 | + if(!$this->configured) { |
|
| 226 | + $this->readConfiguration(); |
|
| 227 | + } |
|
| 228 | + if(is_null($this->cache) || !$this->configuration->ldapCacheTTL) { |
|
| 229 | + return null; |
|
| 230 | + } |
|
| 231 | + $key = $this->getCacheKey($key); |
|
| 232 | + |
|
| 233 | + return json_decode(base64_decode($this->cache->get($key)), true); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * @param string $key |
|
| 238 | + * @param mixed $value |
|
| 239 | + * |
|
| 240 | + * @return string |
|
| 241 | + */ |
|
| 242 | + public function writeToCache($key, $value) { |
|
| 243 | + if(!$this->configured) { |
|
| 244 | + $this->readConfiguration(); |
|
| 245 | + } |
|
| 246 | + if(is_null($this->cache) |
|
| 247 | + || !$this->configuration->ldapCacheTTL |
|
| 248 | + || !$this->configuration->ldapConfigurationActive) { |
|
| 249 | + return null; |
|
| 250 | + } |
|
| 251 | + $key = $this->getCacheKey($key); |
|
| 252 | + $value = base64_encode(json_encode($value)); |
|
| 253 | + $this->cache->set($key, $value, $this->configuration->ldapCacheTTL); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + public function clearCache() { |
|
| 257 | + if(!is_null($this->cache)) { |
|
| 258 | + $this->cache->clear($this->getCacheKey(null)); |
|
| 259 | + } |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * Caches the general LDAP configuration. |
|
| 264 | + * @param bool $force optional. true, if the re-read should be forced. defaults |
|
| 265 | + * to false. |
|
| 266 | + * @return null |
|
| 267 | + */ |
|
| 268 | + private function readConfiguration($force = false) { |
|
| 269 | + if((!$this->configured || $force) && !is_null($this->configID)) { |
|
| 270 | + $this->configuration->readConfiguration(); |
|
| 271 | + $this->configured = $this->validateConfiguration(); |
|
| 272 | + } |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * set LDAP configuration with values delivered by an array, not read from configuration |
|
| 277 | + * @param array $config array that holds the config parameters in an associated array |
|
| 278 | + * @param array &$setParameters optional; array where the set fields will be given to |
|
| 279 | + * @return boolean true if config validates, false otherwise. Check with $setParameters for detailed success on single parameters |
|
| 280 | + */ |
|
| 281 | + public function setConfiguration($config, &$setParameters = null) { |
|
| 282 | + if(is_null($setParameters)) { |
|
| 283 | + $setParameters = array(); |
|
| 284 | + } |
|
| 285 | + $this->doNotValidate = false; |
|
| 286 | + $this->configuration->setConfiguration($config, $setParameters); |
|
| 287 | + if(count($setParameters) > 0) { |
|
| 288 | + $this->configured = $this->validateConfiguration(); |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + |
|
| 292 | + return $this->configured; |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + /** |
|
| 296 | + * saves the current Configuration in the database and empties the |
|
| 297 | + * cache |
|
| 298 | + * @return null |
|
| 299 | + */ |
|
| 300 | + public function saveConfiguration() { |
|
| 301 | + $this->configuration->saveConfiguration(); |
|
| 302 | + $this->clearCache(); |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * get the current LDAP configuration |
|
| 307 | + * @return array |
|
| 308 | + */ |
|
| 309 | + public function getConfiguration() { |
|
| 310 | + $this->readConfiguration(); |
|
| 311 | + $config = $this->configuration->getConfiguration(); |
|
| 312 | + $cta = $this->configuration->getConfigTranslationArray(); |
|
| 313 | + $result = array(); |
|
| 314 | + foreach($cta as $dbkey => $configkey) { |
|
| 315 | + switch($configkey) { |
|
| 316 | + case 'homeFolderNamingRule': |
|
| 317 | + if(strpos($config[$configkey], 'attr:') === 0) { |
|
| 318 | + $result[$dbkey] = substr($config[$configkey], 5); |
|
| 319 | + } else { |
|
| 320 | + $result[$dbkey] = ''; |
|
| 321 | + } |
|
| 322 | + break; |
|
| 323 | + case 'ldapBase': |
|
| 324 | + case 'ldapBaseUsers': |
|
| 325 | + case 'ldapBaseGroups': |
|
| 326 | + case 'ldapAttributesForUserSearch': |
|
| 327 | + case 'ldapAttributesForGroupSearch': |
|
| 328 | + if(is_array($config[$configkey])) { |
|
| 329 | + $result[$dbkey] = implode("\n", $config[$configkey]); |
|
| 330 | + break; |
|
| 331 | + } //else follows default |
|
| 332 | + default: |
|
| 333 | + $result[$dbkey] = $config[$configkey]; |
|
| 334 | + } |
|
| 335 | + } |
|
| 336 | + return $result; |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + private function doSoftValidation() { |
|
| 340 | + //if User or Group Base are not set, take over Base DN setting |
|
| 341 | + foreach(array('ldapBaseUsers', 'ldapBaseGroups') as $keyBase) { |
|
| 342 | + $val = $this->configuration->$keyBase; |
|
| 343 | + if(empty($val)) { |
|
| 344 | + $this->configuration->$keyBase = $this->configuration->ldapBase; |
|
| 345 | + } |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + foreach(array('ldapExpertUUIDUserAttr' => 'ldapUuidUserAttribute', |
|
| 349 | + 'ldapExpertUUIDGroupAttr' => 'ldapUuidGroupAttribute') |
|
| 350 | + as $expertSetting => $effectiveSetting) { |
|
| 351 | + $uuidOverride = $this->configuration->$expertSetting; |
|
| 352 | + if(!empty($uuidOverride)) { |
|
| 353 | + $this->configuration->$effectiveSetting = $uuidOverride; |
|
| 354 | + } else { |
|
| 355 | + $uuidAttributes = Access::UUID_ATTRIBUTES; |
|
| 356 | + array_unshift($uuidAttributes, 'auto'); |
|
| 357 | + if(!in_array($this->configuration->$effectiveSetting, |
|
| 358 | + $uuidAttributes) |
|
| 359 | + && (!is_null($this->configID))) { |
|
| 360 | + $this->configuration->$effectiveSetting = 'auto'; |
|
| 361 | + $this->configuration->saveConfiguration(); |
|
| 362 | + \OCP\Util::writeLog('user_ldap', |
|
| 363 | + 'Illegal value for the '. |
|
| 364 | + $effectiveSetting.', '.'reset to '. |
|
| 365 | + 'autodetect.', \OCP\Util::INFO); |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + } |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + $backupPort = (int)$this->configuration->ldapBackupPort; |
|
| 372 | + if ($backupPort <= 0) { |
|
| 373 | + $this->configuration->backupPort = $this->configuration->ldapPort; |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + //make sure empty search attributes are saved as simple, empty array |
|
| 377 | + $saKeys = array('ldapAttributesForUserSearch', |
|
| 378 | + 'ldapAttributesForGroupSearch'); |
|
| 379 | + foreach($saKeys as $key) { |
|
| 380 | + $val = $this->configuration->$key; |
|
| 381 | + if(is_array($val) && count($val) === 1 && empty($val[0])) { |
|
| 382 | + $this->configuration->$key = array(); |
|
| 383 | + } |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + if((stripos($this->configuration->ldapHost, 'ldaps://') === 0) |
|
| 387 | + && $this->configuration->ldapTLS) { |
|
| 388 | + $this->configuration->ldapTLS = false; |
|
| 389 | + \OCP\Util::writeLog('user_ldap', |
|
| 390 | + 'LDAPS (already using secure connection) and '. |
|
| 391 | + 'TLS do not work together. Switched off TLS.', |
|
| 392 | + \OCP\Util::INFO); |
|
| 393 | + } |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + /** |
|
| 397 | + * @return bool |
|
| 398 | + */ |
|
| 399 | + private function doCriticalValidation() { |
|
| 400 | + $configurationOK = true; |
|
| 401 | + $errorStr = 'Configuration Error (prefix '. |
|
| 402 | + (string)$this->configPrefix .'): '; |
|
| 403 | + |
|
| 404 | + //options that shall not be empty |
|
| 405 | + $options = array('ldapHost', 'ldapPort', 'ldapUserDisplayName', |
|
| 406 | + 'ldapGroupDisplayName', 'ldapLoginFilter'); |
|
| 407 | + foreach($options as $key) { |
|
| 408 | + $val = $this->configuration->$key; |
|
| 409 | + if(empty($val)) { |
|
| 410 | + switch($key) { |
|
| 411 | + case 'ldapHost': |
|
| 412 | + $subj = 'LDAP Host'; |
|
| 413 | + break; |
|
| 414 | + case 'ldapPort': |
|
| 415 | + $subj = 'LDAP Port'; |
|
| 416 | + break; |
|
| 417 | + case 'ldapUserDisplayName': |
|
| 418 | + $subj = 'LDAP User Display Name'; |
|
| 419 | + break; |
|
| 420 | + case 'ldapGroupDisplayName': |
|
| 421 | + $subj = 'LDAP Group Display Name'; |
|
| 422 | + break; |
|
| 423 | + case 'ldapLoginFilter': |
|
| 424 | + $subj = 'LDAP Login Filter'; |
|
| 425 | + break; |
|
| 426 | + default: |
|
| 427 | + $subj = $key; |
|
| 428 | + break; |
|
| 429 | + } |
|
| 430 | + $configurationOK = false; |
|
| 431 | + \OCP\Util::writeLog('user_ldap', |
|
| 432 | + $errorStr.'No '.$subj.' given!', |
|
| 433 | + \OCP\Util::WARN); |
|
| 434 | + } |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + //combinations |
|
| 438 | + $agent = $this->configuration->ldapAgentName; |
|
| 439 | + $pwd = $this->configuration->ldapAgentPassword; |
|
| 440 | + if ( |
|
| 441 | + ($agent === '' && $pwd !== '') |
|
| 442 | + || ($agent !== '' && $pwd === '') |
|
| 443 | + ) { |
|
| 444 | + \OCP\Util::writeLog('user_ldap', |
|
| 445 | + $errorStr.'either no password is given for the '. |
|
| 446 | + 'user agent or a password is given, but not an '. |
|
| 447 | + 'LDAP agent.', |
|
| 448 | + \OCP\Util::WARN); |
|
| 449 | + $configurationOK = false; |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + $base = $this->configuration->ldapBase; |
|
| 453 | + $baseUsers = $this->configuration->ldapBaseUsers; |
|
| 454 | + $baseGroups = $this->configuration->ldapBaseGroups; |
|
| 455 | + |
|
| 456 | + if(empty($base) && empty($baseUsers) && empty($baseGroups)) { |
|
| 457 | + \OCP\Util::writeLog('user_ldap', |
|
| 458 | + $errorStr.'Not a single Base DN given.', |
|
| 459 | + \OCP\Util::WARN); |
|
| 460 | + $configurationOK = false; |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + if(mb_strpos($this->configuration->ldapLoginFilter, '%uid', 0, 'UTF-8') |
|
| 464 | + === false) { |
|
| 465 | + \OCP\Util::writeLog('user_ldap', |
|
| 466 | + $errorStr.'login filter does not contain %uid '. |
|
| 467 | + 'place holder.', |
|
| 468 | + \OCP\Util::WARN); |
|
| 469 | + $configurationOK = false; |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + return $configurationOK; |
|
| 473 | + } |
|
| 474 | + |
|
| 475 | + /** |
|
| 476 | + * Validates the user specified configuration |
|
| 477 | + * @return bool true if configuration seems OK, false otherwise |
|
| 478 | + */ |
|
| 479 | + private function validateConfiguration() { |
|
| 480 | + |
|
| 481 | + if($this->doNotValidate) { |
|
| 482 | + //don't do a validation if it is a new configuration with pure |
|
| 483 | + //default values. Will be allowed on changes via __set or |
|
| 484 | + //setConfiguration |
|
| 485 | + return false; |
|
| 486 | + } |
|
| 487 | + |
|
| 488 | + // first step: "soft" checks: settings that are not really |
|
| 489 | + // necessary, but advisable. If left empty, give an info message |
|
| 490 | + $this->doSoftValidation(); |
|
| 491 | + |
|
| 492 | + //second step: critical checks. If left empty or filled wrong, mark as |
|
| 493 | + //not configured and give a warning. |
|
| 494 | + return $this->doCriticalValidation(); |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + |
|
| 498 | + /** |
|
| 499 | + * Connects and Binds to LDAP |
|
| 500 | + */ |
|
| 501 | + private function establishConnection() { |
|
| 502 | + if(!$this->configuration->ldapConfigurationActive) { |
|
| 503 | + return null; |
|
| 504 | + } |
|
| 505 | + static $phpLDAPinstalled = true; |
|
| 506 | + if(!$phpLDAPinstalled) { |
|
| 507 | + return false; |
|
| 508 | + } |
|
| 509 | + if(!$this->ignoreValidation && !$this->configured) { |
|
| 510 | + \OCP\Util::writeLog('user_ldap', |
|
| 511 | + 'Configuration is invalid, cannot connect', |
|
| 512 | + \OCP\Util::WARN); |
|
| 513 | + return false; |
|
| 514 | + } |
|
| 515 | + if(!$this->ldapConnectionRes) { |
|
| 516 | + if(!$this->ldap->areLDAPFunctionsAvailable()) { |
|
| 517 | + $phpLDAPinstalled = false; |
|
| 518 | + \OCP\Util::writeLog('user_ldap', |
|
| 519 | + 'function ldap_connect is not available. Make '. |
|
| 520 | + 'sure that the PHP ldap module is installed.', |
|
| 521 | + \OCP\Util::ERROR); |
|
| 522 | + |
|
| 523 | + return false; |
|
| 524 | + } |
|
| 525 | + if($this->configuration->turnOffCertCheck) { |
|
| 526 | + if(putenv('LDAPTLS_REQCERT=never')) { |
|
| 527 | + \OCP\Util::writeLog('user_ldap', |
|
| 528 | + 'Turned off SSL certificate validation successfully.', |
|
| 529 | + \OCP\Util::DEBUG); |
|
| 530 | + } else { |
|
| 531 | + \OCP\Util::writeLog('user_ldap', |
|
| 532 | + 'Could not turn off SSL certificate validation.', |
|
| 533 | + \OCP\Util::WARN); |
|
| 534 | + } |
|
| 535 | + } |
|
| 536 | + |
|
| 537 | + $isOverrideMainServer = ($this->configuration->ldapOverrideMainServer |
|
| 538 | + || $this->getFromCache('overrideMainServer')); |
|
| 539 | + $isBackupHost = (trim($this->configuration->ldapBackupHost) !== ""); |
|
| 540 | + $bindStatus = false; |
|
| 541 | + $error = -1; |
|
| 542 | + try { |
|
| 543 | + if (!$isOverrideMainServer) { |
|
| 544 | + $this->doConnect($this->configuration->ldapHost, |
|
| 545 | + $this->configuration->ldapPort); |
|
| 546 | + $bindStatus = $this->bind(); |
|
| 547 | + $error = $this->ldap->isResource($this->ldapConnectionRes) ? |
|
| 548 | + $this->ldap->errno($this->ldapConnectionRes) : -1; |
|
| 549 | + } |
|
| 550 | + if($bindStatus === true) { |
|
| 551 | + return $bindStatus; |
|
| 552 | + } |
|
| 553 | + } catch (ServerNotAvailableException $e) { |
|
| 554 | + if(!$isBackupHost) { |
|
| 555 | + throw $e; |
|
| 556 | + } |
|
| 557 | + } |
|
| 558 | + |
|
| 559 | + //if LDAP server is not reachable, try the Backup (Replica!) Server |
|
| 560 | + if($isBackupHost && ($error !== 0 || $isOverrideMainServer)) { |
|
| 561 | + $this->doConnect($this->configuration->ldapBackupHost, |
|
| 562 | + $this->configuration->ldapBackupPort); |
|
| 563 | + $bindStatus = $this->bind(); |
|
| 564 | + $error = $this->ldap->isResource($this->ldapConnectionRes) ? |
|
| 565 | + $this->ldap->errno($this->ldapConnectionRes) : -1; |
|
| 566 | + if($bindStatus && $error === 0 && !$this->getFromCache('overrideMainServer')) { |
|
| 567 | + //when bind to backup server succeeded and failed to main server, |
|
| 568 | + //skip contacting him until next cache refresh |
|
| 569 | + $this->writeToCache('overrideMainServer', true); |
|
| 570 | + } |
|
| 571 | + } |
|
| 572 | + |
|
| 573 | + return $bindStatus; |
|
| 574 | + } |
|
| 575 | + return null; |
|
| 576 | + } |
|
| 577 | + |
|
| 578 | + /** |
|
| 579 | + * @param string $host |
|
| 580 | + * @param string $port |
|
| 581 | + * @return bool |
|
| 582 | + * @throws \OC\ServerNotAvailableException |
|
| 583 | + */ |
|
| 584 | + private function doConnect($host, $port) { |
|
| 585 | + if ($host === '') { |
|
| 586 | + return false; |
|
| 587 | + } |
|
| 588 | + |
|
| 589 | + $this->ldapConnectionRes = $this->ldap->connect($host, $port); |
|
| 590 | + |
|
| 591 | + if(!$this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_PROTOCOL_VERSION, 3)) { |
|
| 592 | + throw new ServerNotAvailableException('Could not set required LDAP Protocol version.'); |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + if(!$this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_REFERRALS, 0)) { |
|
| 596 | + throw new ServerNotAvailableException('Could not disable LDAP referrals.'); |
|
| 597 | + } |
|
| 598 | + |
|
| 599 | + if($this->configuration->ldapTLS) { |
|
| 600 | + if(!$this->ldap->startTls($this->ldapConnectionRes)) { |
|
| 601 | + throw new ServerNotAvailableException('Start TLS failed, when connecting to LDAP host ' . $host . '.'); |
|
| 602 | + } |
|
| 603 | + } |
|
| 604 | + |
|
| 605 | + return true; |
|
| 606 | + } |
|
| 607 | + |
|
| 608 | + /** |
|
| 609 | + * Binds to LDAP |
|
| 610 | + */ |
|
| 611 | + public function bind() { |
|
| 612 | + if(!$this->configuration->ldapConfigurationActive) { |
|
| 613 | + return false; |
|
| 614 | + } |
|
| 615 | + $cr = $this->getConnectionResource(); |
|
| 616 | + if(!$this->ldap->isResource($cr)) { |
|
| 617 | + return false; |
|
| 618 | + } |
|
| 619 | + $ldapLogin = @$this->ldap->bind($cr, |
|
| 620 | + $this->configuration->ldapAgentName, |
|
| 621 | + $this->configuration->ldapAgentPassword); |
|
| 622 | + if(!$ldapLogin) { |
|
| 623 | + $errno = $this->ldap->errno($cr); |
|
| 624 | + |
|
| 625 | + \OCP\Util::writeLog('user_ldap', |
|
| 626 | + 'Bind failed: ' . $errno . ': ' . $this->ldap->error($cr), |
|
| 627 | + \OCP\Util::WARN); |
|
| 628 | + |
|
| 629 | + // Set to failure mode, if LDAP error code is not LDAP_SUCCESS or LDAP_INVALID_CREDENTIALS |
|
| 630 | + if($errno !== 0x00 && $errno !== 0x31) { |
|
| 631 | + $this->ldapConnectionRes = null; |
|
| 632 | + } |
|
| 633 | + |
|
| 634 | + return false; |
|
| 635 | + } |
|
| 636 | + return true; |
|
| 637 | + } |
|
| 638 | 638 | |
| 639 | 639 | } |
@@ -99,19 +99,19 @@ discard block |
||
| 99 | 99 | $this->configuration = new Configuration($configPrefix, |
| 100 | 100 | !is_null($configID)); |
| 101 | 101 | $memcache = \OC::$server->getMemCacheFactory(); |
| 102 | - if($memcache->isAvailable()) { |
|
| 102 | + if ($memcache->isAvailable()) { |
|
| 103 | 103 | $this->cache = $memcache->createDistributed(); |
| 104 | 104 | } |
| 105 | 105 | $helper = new Helper(\OC::$server->getConfig()); |
| 106 | 106 | $this->doNotValidate = !in_array($this->configPrefix, |
| 107 | 107 | $helper->getServerConfigurationPrefixes()); |
| 108 | 108 | $this->hasPagedResultSupport = |
| 109 | - (int)$this->configuration->ldapPagingSize !== 0 |
|
| 109 | + (int) $this->configuration->ldapPagingSize !== 0 |
|
| 110 | 110 | || $this->ldap->hasPagedResultSupport(); |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | public function __destruct() { |
| 114 | - if(!$this->dontDestruct && $this->ldap->isResource($this->ldapConnectionRes)) { |
|
| 114 | + if (!$this->dontDestruct && $this->ldap->isResource($this->ldapConnectionRes)) { |
|
| 115 | 115 | @$this->ldap->unbind($this->ldapConnectionRes); |
| 116 | 116 | } |
| 117 | 117 | } |
@@ -131,11 +131,11 @@ discard block |
||
| 131 | 131 | * @return bool|mixed |
| 132 | 132 | */ |
| 133 | 133 | public function __get($name) { |
| 134 | - if(!$this->configured) { |
|
| 134 | + if (!$this->configured) { |
|
| 135 | 135 | $this->readConfiguration(); |
| 136 | 136 | } |
| 137 | 137 | |
| 138 | - if($name === 'hasPagedResultSupport') { |
|
| 138 | + if ($name === 'hasPagedResultSupport') { |
|
| 139 | 139 | return $this->hasPagedResultSupport; |
| 140 | 140 | } |
| 141 | 141 | |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | $before = $this->configuration->$name; |
| 152 | 152 | $this->configuration->$name = $value; |
| 153 | 153 | $after = $this->configuration->$name; |
| 154 | - if($before !== $after) { |
|
| 154 | + if ($before !== $after) { |
|
| 155 | 155 | if ($this->configID !== '' && $this->configID !== null) { |
| 156 | 156 | $this->configuration->saveConfiguration(); |
| 157 | 157 | } |
@@ -166,7 +166,7 @@ discard block |
||
| 166 | 166 | * @param bool $state |
| 167 | 167 | */ |
| 168 | 168 | public function setIgnoreValidation($state) { |
| 169 | - $this->ignoreValidation = (bool)$state; |
|
| 169 | + $this->ignoreValidation = (bool) $state; |
|
| 170 | 170 | } |
| 171 | 171 | |
| 172 | 172 | /** |
@@ -182,14 +182,14 @@ discard block |
||
| 182 | 182 | * Returns the LDAP handler |
| 183 | 183 | */ |
| 184 | 184 | public function getConnectionResource() { |
| 185 | - if(!$this->ldapConnectionRes) { |
|
| 185 | + if (!$this->ldapConnectionRes) { |
|
| 186 | 186 | $this->init(); |
| 187 | - } else if(!$this->ldap->isResource($this->ldapConnectionRes)) { |
|
| 187 | + } else if (!$this->ldap->isResource($this->ldapConnectionRes)) { |
|
| 188 | 188 | $this->ldapConnectionRes = null; |
| 189 | 189 | $this->establishConnection(); |
| 190 | 190 | } |
| 191 | - if(is_null($this->ldapConnectionRes)) { |
|
| 192 | - \OCP\Util::writeLog('user_ldap', 'No LDAP Connection to server ' . $this->configuration->ldapHost, \OCP\Util::ERROR); |
|
| 191 | + if (is_null($this->ldapConnectionRes)) { |
|
| 192 | + \OCP\Util::writeLog('user_ldap', 'No LDAP Connection to server '.$this->configuration->ldapHost, \OCP\Util::ERROR); |
|
| 193 | 193 | throw new ServerNotAvailableException('Connection to LDAP server could not be established'); |
| 194 | 194 | } |
| 195 | 195 | return $this->ldapConnectionRes; |
@@ -199,7 +199,7 @@ discard block |
||
| 199 | 199 | * resets the connection resource |
| 200 | 200 | */ |
| 201 | 201 | public function resetConnectionResource() { |
| 202 | - if(!is_null($this->ldapConnectionRes)) { |
|
| 202 | + if (!is_null($this->ldapConnectionRes)) { |
|
| 203 | 203 | @$this->ldap->unbind($this->ldapConnectionRes); |
| 204 | 204 | $this->ldapConnectionRes = null; |
| 205 | 205 | } |
@@ -211,7 +211,7 @@ discard block |
||
| 211 | 211 | */ |
| 212 | 212 | private function getCacheKey($key) { |
| 213 | 213 | $prefix = 'LDAP-'.$this->configID.'-'.$this->configPrefix.'-'; |
| 214 | - if(is_null($key)) { |
|
| 214 | + if (is_null($key)) { |
|
| 215 | 215 | return $prefix; |
| 216 | 216 | } |
| 217 | 217 | return $prefix.md5($key); |
@@ -222,10 +222,10 @@ discard block |
||
| 222 | 222 | * @return mixed|null |
| 223 | 223 | */ |
| 224 | 224 | public function getFromCache($key) { |
| 225 | - if(!$this->configured) { |
|
| 225 | + if (!$this->configured) { |
|
| 226 | 226 | $this->readConfiguration(); |
| 227 | 227 | } |
| 228 | - if(is_null($this->cache) || !$this->configuration->ldapCacheTTL) { |
|
| 228 | + if (is_null($this->cache) || !$this->configuration->ldapCacheTTL) { |
|
| 229 | 229 | return null; |
| 230 | 230 | } |
| 231 | 231 | $key = $this->getCacheKey($key); |
@@ -240,10 +240,10 @@ discard block |
||
| 240 | 240 | * @return string |
| 241 | 241 | */ |
| 242 | 242 | public function writeToCache($key, $value) { |
| 243 | - if(!$this->configured) { |
|
| 243 | + if (!$this->configured) { |
|
| 244 | 244 | $this->readConfiguration(); |
| 245 | 245 | } |
| 246 | - if(is_null($this->cache) |
|
| 246 | + if (is_null($this->cache) |
|
| 247 | 247 | || !$this->configuration->ldapCacheTTL |
| 248 | 248 | || !$this->configuration->ldapConfigurationActive) { |
| 249 | 249 | return null; |
@@ -254,7 +254,7 @@ discard block |
||
| 254 | 254 | } |
| 255 | 255 | |
| 256 | 256 | public function clearCache() { |
| 257 | - if(!is_null($this->cache)) { |
|
| 257 | + if (!is_null($this->cache)) { |
|
| 258 | 258 | $this->cache->clear($this->getCacheKey(null)); |
| 259 | 259 | } |
| 260 | 260 | } |
@@ -266,7 +266,7 @@ discard block |
||
| 266 | 266 | * @return null |
| 267 | 267 | */ |
| 268 | 268 | private function readConfiguration($force = false) { |
| 269 | - if((!$this->configured || $force) && !is_null($this->configID)) { |
|
| 269 | + if ((!$this->configured || $force) && !is_null($this->configID)) { |
|
| 270 | 270 | $this->configuration->readConfiguration(); |
| 271 | 271 | $this->configured = $this->validateConfiguration(); |
| 272 | 272 | } |
@@ -279,12 +279,12 @@ discard block |
||
| 279 | 279 | * @return boolean true if config validates, false otherwise. Check with $setParameters for detailed success on single parameters |
| 280 | 280 | */ |
| 281 | 281 | public function setConfiguration($config, &$setParameters = null) { |
| 282 | - if(is_null($setParameters)) { |
|
| 282 | + if (is_null($setParameters)) { |
|
| 283 | 283 | $setParameters = array(); |
| 284 | 284 | } |
| 285 | 285 | $this->doNotValidate = false; |
| 286 | 286 | $this->configuration->setConfiguration($config, $setParameters); |
| 287 | - if(count($setParameters) > 0) { |
|
| 287 | + if (count($setParameters) > 0) { |
|
| 288 | 288 | $this->configured = $this->validateConfiguration(); |
| 289 | 289 | } |
| 290 | 290 | |
@@ -311,10 +311,10 @@ discard block |
||
| 311 | 311 | $config = $this->configuration->getConfiguration(); |
| 312 | 312 | $cta = $this->configuration->getConfigTranslationArray(); |
| 313 | 313 | $result = array(); |
| 314 | - foreach($cta as $dbkey => $configkey) { |
|
| 315 | - switch($configkey) { |
|
| 314 | + foreach ($cta as $dbkey => $configkey) { |
|
| 315 | + switch ($configkey) { |
|
| 316 | 316 | case 'homeFolderNamingRule': |
| 317 | - if(strpos($config[$configkey], 'attr:') === 0) { |
|
| 317 | + if (strpos($config[$configkey], 'attr:') === 0) { |
|
| 318 | 318 | $result[$dbkey] = substr($config[$configkey], 5); |
| 319 | 319 | } else { |
| 320 | 320 | $result[$dbkey] = ''; |
@@ -325,7 +325,7 @@ discard block |
||
| 325 | 325 | case 'ldapBaseGroups': |
| 326 | 326 | case 'ldapAttributesForUserSearch': |
| 327 | 327 | case 'ldapAttributesForGroupSearch': |
| 328 | - if(is_array($config[$configkey])) { |
|
| 328 | + if (is_array($config[$configkey])) { |
|
| 329 | 329 | $result[$dbkey] = implode("\n", $config[$configkey]); |
| 330 | 330 | break; |
| 331 | 331 | } //else follows default |
@@ -338,23 +338,23 @@ discard block |
||
| 338 | 338 | |
| 339 | 339 | private function doSoftValidation() { |
| 340 | 340 | //if User or Group Base are not set, take over Base DN setting |
| 341 | - foreach(array('ldapBaseUsers', 'ldapBaseGroups') as $keyBase) { |
|
| 341 | + foreach (array('ldapBaseUsers', 'ldapBaseGroups') as $keyBase) { |
|
| 342 | 342 | $val = $this->configuration->$keyBase; |
| 343 | - if(empty($val)) { |
|
| 343 | + if (empty($val)) { |
|
| 344 | 344 | $this->configuration->$keyBase = $this->configuration->ldapBase; |
| 345 | 345 | } |
| 346 | 346 | } |
| 347 | 347 | |
| 348 | - foreach(array('ldapExpertUUIDUserAttr' => 'ldapUuidUserAttribute', |
|
| 348 | + foreach (array('ldapExpertUUIDUserAttr' => 'ldapUuidUserAttribute', |
|
| 349 | 349 | 'ldapExpertUUIDGroupAttr' => 'ldapUuidGroupAttribute') |
| 350 | 350 | as $expertSetting => $effectiveSetting) { |
| 351 | 351 | $uuidOverride = $this->configuration->$expertSetting; |
| 352 | - if(!empty($uuidOverride)) { |
|
| 352 | + if (!empty($uuidOverride)) { |
|
| 353 | 353 | $this->configuration->$effectiveSetting = $uuidOverride; |
| 354 | 354 | } else { |
| 355 | 355 | $uuidAttributes = Access::UUID_ATTRIBUTES; |
| 356 | 356 | array_unshift($uuidAttributes, 'auto'); |
| 357 | - if(!in_array($this->configuration->$effectiveSetting, |
|
| 357 | + if (!in_array($this->configuration->$effectiveSetting, |
|
| 358 | 358 | $uuidAttributes) |
| 359 | 359 | && (!is_null($this->configID))) { |
| 360 | 360 | $this->configuration->$effectiveSetting = 'auto'; |
@@ -368,7 +368,7 @@ discard block |
||
| 368 | 368 | } |
| 369 | 369 | } |
| 370 | 370 | |
| 371 | - $backupPort = (int)$this->configuration->ldapBackupPort; |
|
| 371 | + $backupPort = (int) $this->configuration->ldapBackupPort; |
|
| 372 | 372 | if ($backupPort <= 0) { |
| 373 | 373 | $this->configuration->backupPort = $this->configuration->ldapPort; |
| 374 | 374 | } |
@@ -376,14 +376,14 @@ discard block |
||
| 376 | 376 | //make sure empty search attributes are saved as simple, empty array |
| 377 | 377 | $saKeys = array('ldapAttributesForUserSearch', |
| 378 | 378 | 'ldapAttributesForGroupSearch'); |
| 379 | - foreach($saKeys as $key) { |
|
| 379 | + foreach ($saKeys as $key) { |
|
| 380 | 380 | $val = $this->configuration->$key; |
| 381 | - if(is_array($val) && count($val) === 1 && empty($val[0])) { |
|
| 381 | + if (is_array($val) && count($val) === 1 && empty($val[0])) { |
|
| 382 | 382 | $this->configuration->$key = array(); |
| 383 | 383 | } |
| 384 | 384 | } |
| 385 | 385 | |
| 386 | - if((stripos($this->configuration->ldapHost, 'ldaps://') === 0) |
|
| 386 | + if ((stripos($this->configuration->ldapHost, 'ldaps://') === 0) |
|
| 387 | 387 | && $this->configuration->ldapTLS) { |
| 388 | 388 | $this->configuration->ldapTLS = false; |
| 389 | 389 | \OCP\Util::writeLog('user_ldap', |
@@ -399,15 +399,15 @@ discard block |
||
| 399 | 399 | private function doCriticalValidation() { |
| 400 | 400 | $configurationOK = true; |
| 401 | 401 | $errorStr = 'Configuration Error (prefix '. |
| 402 | - (string)$this->configPrefix .'): '; |
|
| 402 | + (string) $this->configPrefix.'): '; |
|
| 403 | 403 | |
| 404 | 404 | //options that shall not be empty |
| 405 | 405 | $options = array('ldapHost', 'ldapPort', 'ldapUserDisplayName', |
| 406 | 406 | 'ldapGroupDisplayName', 'ldapLoginFilter'); |
| 407 | - foreach($options as $key) { |
|
| 407 | + foreach ($options as $key) { |
|
| 408 | 408 | $val = $this->configuration->$key; |
| 409 | - if(empty($val)) { |
|
| 410 | - switch($key) { |
|
| 409 | + if (empty($val)) { |
|
| 410 | + switch ($key) { |
|
| 411 | 411 | case 'ldapHost': |
| 412 | 412 | $subj = 'LDAP Host'; |
| 413 | 413 | break; |
@@ -438,7 +438,7 @@ discard block |
||
| 438 | 438 | $agent = $this->configuration->ldapAgentName; |
| 439 | 439 | $pwd = $this->configuration->ldapAgentPassword; |
| 440 | 440 | if ( |
| 441 | - ($agent === '' && $pwd !== '') |
|
| 441 | + ($agent === '' && $pwd !== '') |
|
| 442 | 442 | || ($agent !== '' && $pwd === '') |
| 443 | 443 | ) { |
| 444 | 444 | \OCP\Util::writeLog('user_ldap', |
@@ -453,14 +453,14 @@ discard block |
||
| 453 | 453 | $baseUsers = $this->configuration->ldapBaseUsers; |
| 454 | 454 | $baseGroups = $this->configuration->ldapBaseGroups; |
| 455 | 455 | |
| 456 | - if(empty($base) && empty($baseUsers) && empty($baseGroups)) { |
|
| 456 | + if (empty($base) && empty($baseUsers) && empty($baseGroups)) { |
|
| 457 | 457 | \OCP\Util::writeLog('user_ldap', |
| 458 | 458 | $errorStr.'Not a single Base DN given.', |
| 459 | 459 | \OCP\Util::WARN); |
| 460 | 460 | $configurationOK = false; |
| 461 | 461 | } |
| 462 | 462 | |
| 463 | - if(mb_strpos($this->configuration->ldapLoginFilter, '%uid', 0, 'UTF-8') |
|
| 463 | + if (mb_strpos($this->configuration->ldapLoginFilter, '%uid', 0, 'UTF-8') |
|
| 464 | 464 | === false) { |
| 465 | 465 | \OCP\Util::writeLog('user_ldap', |
| 466 | 466 | $errorStr.'login filter does not contain %uid '. |
@@ -478,7 +478,7 @@ discard block |
||
| 478 | 478 | */ |
| 479 | 479 | private function validateConfiguration() { |
| 480 | 480 | |
| 481 | - if($this->doNotValidate) { |
|
| 481 | + if ($this->doNotValidate) { |
|
| 482 | 482 | //don't do a validation if it is a new configuration with pure |
| 483 | 483 | //default values. Will be allowed on changes via __set or |
| 484 | 484 | //setConfiguration |
@@ -499,21 +499,21 @@ discard block |
||
| 499 | 499 | * Connects and Binds to LDAP |
| 500 | 500 | */ |
| 501 | 501 | private function establishConnection() { |
| 502 | - if(!$this->configuration->ldapConfigurationActive) { |
|
| 502 | + if (!$this->configuration->ldapConfigurationActive) { |
|
| 503 | 503 | return null; |
| 504 | 504 | } |
| 505 | 505 | static $phpLDAPinstalled = true; |
| 506 | - if(!$phpLDAPinstalled) { |
|
| 506 | + if (!$phpLDAPinstalled) { |
|
| 507 | 507 | return false; |
| 508 | 508 | } |
| 509 | - if(!$this->ignoreValidation && !$this->configured) { |
|
| 509 | + if (!$this->ignoreValidation && !$this->configured) { |
|
| 510 | 510 | \OCP\Util::writeLog('user_ldap', |
| 511 | 511 | 'Configuration is invalid, cannot connect', |
| 512 | 512 | \OCP\Util::WARN); |
| 513 | 513 | return false; |
| 514 | 514 | } |
| 515 | - if(!$this->ldapConnectionRes) { |
|
| 516 | - if(!$this->ldap->areLDAPFunctionsAvailable()) { |
|
| 515 | + if (!$this->ldapConnectionRes) { |
|
| 516 | + if (!$this->ldap->areLDAPFunctionsAvailable()) { |
|
| 517 | 517 | $phpLDAPinstalled = false; |
| 518 | 518 | \OCP\Util::writeLog('user_ldap', |
| 519 | 519 | 'function ldap_connect is not available. Make '. |
@@ -522,8 +522,8 @@ discard block |
||
| 522 | 522 | |
| 523 | 523 | return false; |
| 524 | 524 | } |
| 525 | - if($this->configuration->turnOffCertCheck) { |
|
| 526 | - if(putenv('LDAPTLS_REQCERT=never')) { |
|
| 525 | + if ($this->configuration->turnOffCertCheck) { |
|
| 526 | + if (putenv('LDAPTLS_REQCERT=never')) { |
|
| 527 | 527 | \OCP\Util::writeLog('user_ldap', |
| 528 | 528 | 'Turned off SSL certificate validation successfully.', |
| 529 | 529 | \OCP\Util::DEBUG); |
@@ -547,23 +547,23 @@ discard block |
||
| 547 | 547 | $error = $this->ldap->isResource($this->ldapConnectionRes) ? |
| 548 | 548 | $this->ldap->errno($this->ldapConnectionRes) : -1; |
| 549 | 549 | } |
| 550 | - if($bindStatus === true) { |
|
| 550 | + if ($bindStatus === true) { |
|
| 551 | 551 | return $bindStatus; |
| 552 | 552 | } |
| 553 | 553 | } catch (ServerNotAvailableException $e) { |
| 554 | - if(!$isBackupHost) { |
|
| 554 | + if (!$isBackupHost) { |
|
| 555 | 555 | throw $e; |
| 556 | 556 | } |
| 557 | 557 | } |
| 558 | 558 | |
| 559 | 559 | //if LDAP server is not reachable, try the Backup (Replica!) Server |
| 560 | - if($isBackupHost && ($error !== 0 || $isOverrideMainServer)) { |
|
| 560 | + if ($isBackupHost && ($error !== 0 || $isOverrideMainServer)) { |
|
| 561 | 561 | $this->doConnect($this->configuration->ldapBackupHost, |
| 562 | 562 | $this->configuration->ldapBackupPort); |
| 563 | 563 | $bindStatus = $this->bind(); |
| 564 | 564 | $error = $this->ldap->isResource($this->ldapConnectionRes) ? |
| 565 | 565 | $this->ldap->errno($this->ldapConnectionRes) : -1; |
| 566 | - if($bindStatus && $error === 0 && !$this->getFromCache('overrideMainServer')) { |
|
| 566 | + if ($bindStatus && $error === 0 && !$this->getFromCache('overrideMainServer')) { |
|
| 567 | 567 | //when bind to backup server succeeded and failed to main server, |
| 568 | 568 | //skip contacting him until next cache refresh |
| 569 | 569 | $this->writeToCache('overrideMainServer', true); |
@@ -588,17 +588,17 @@ discard block |
||
| 588 | 588 | |
| 589 | 589 | $this->ldapConnectionRes = $this->ldap->connect($host, $port); |
| 590 | 590 | |
| 591 | - if(!$this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_PROTOCOL_VERSION, 3)) { |
|
| 591 | + if (!$this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_PROTOCOL_VERSION, 3)) { |
|
| 592 | 592 | throw new ServerNotAvailableException('Could not set required LDAP Protocol version.'); |
| 593 | 593 | } |
| 594 | 594 | |
| 595 | - if(!$this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_REFERRALS, 0)) { |
|
| 595 | + if (!$this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_REFERRALS, 0)) { |
|
| 596 | 596 | throw new ServerNotAvailableException('Could not disable LDAP referrals.'); |
| 597 | 597 | } |
| 598 | 598 | |
| 599 | - if($this->configuration->ldapTLS) { |
|
| 600 | - if(!$this->ldap->startTls($this->ldapConnectionRes)) { |
|
| 601 | - throw new ServerNotAvailableException('Start TLS failed, when connecting to LDAP host ' . $host . '.'); |
|
| 599 | + if ($this->configuration->ldapTLS) { |
|
| 600 | + if (!$this->ldap->startTls($this->ldapConnectionRes)) { |
|
| 601 | + throw new ServerNotAvailableException('Start TLS failed, when connecting to LDAP host '.$host.'.'); |
|
| 602 | 602 | } |
| 603 | 603 | } |
| 604 | 604 | |
@@ -609,25 +609,25 @@ discard block |
||
| 609 | 609 | * Binds to LDAP |
| 610 | 610 | */ |
| 611 | 611 | public function bind() { |
| 612 | - if(!$this->configuration->ldapConfigurationActive) { |
|
| 612 | + if (!$this->configuration->ldapConfigurationActive) { |
|
| 613 | 613 | return false; |
| 614 | 614 | } |
| 615 | 615 | $cr = $this->getConnectionResource(); |
| 616 | - if(!$this->ldap->isResource($cr)) { |
|
| 616 | + if (!$this->ldap->isResource($cr)) { |
|
| 617 | 617 | return false; |
| 618 | 618 | } |
| 619 | 619 | $ldapLogin = @$this->ldap->bind($cr, |
| 620 | 620 | $this->configuration->ldapAgentName, |
| 621 | 621 | $this->configuration->ldapAgentPassword); |
| 622 | - if(!$ldapLogin) { |
|
| 622 | + if (!$ldapLogin) { |
|
| 623 | 623 | $errno = $this->ldap->errno($cr); |
| 624 | 624 | |
| 625 | 625 | \OCP\Util::writeLog('user_ldap', |
| 626 | - 'Bind failed: ' . $errno . ': ' . $this->ldap->error($cr), |
|
| 626 | + 'Bind failed: '.$errno.': '.$this->ldap->error($cr), |
|
| 627 | 627 | \OCP\Util::WARN); |
| 628 | 628 | |
| 629 | 629 | // Set to failure mode, if LDAP error code is not LDAP_SUCCESS or LDAP_INVALID_CREDENTIALS |
| 630 | - if($errno !== 0x00 && $errno !== 0x31) { |
|
| 630 | + if ($errno !== 0x00 && $errno !== 0x31) { |
|
| 631 | 631 | $this->ldapConnectionRes = null; |
| 632 | 632 | } |
| 633 | 633 | |
@@ -28,248 +28,248 @@ |
||
| 28 | 28 | namespace OCA\User_LDAP; |
| 29 | 29 | |
| 30 | 30 | class Group_Proxy extends Proxy implements \OCP\GroupInterface, IGroupLDAP { |
| 31 | - private $backends = array(); |
|
| 32 | - private $refBackend = null; |
|
| 31 | + private $backends = array(); |
|
| 32 | + private $refBackend = null; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Constructor |
|
| 36 | - * @param string[] $serverConfigPrefixes array containing the config Prefixes |
|
| 37 | - */ |
|
| 38 | - public function __construct($serverConfigPrefixes, ILDAPWrapper $ldap, GroupPluginManager $groupPluginManager) { |
|
| 39 | - parent::__construct($ldap); |
|
| 40 | - foreach($serverConfigPrefixes as $configPrefix) { |
|
| 41 | - $this->backends[$configPrefix] = |
|
| 42 | - new \OCA\User_LDAP\Group_LDAP($this->getAccess($configPrefix), $groupPluginManager); |
|
| 43 | - if(is_null($this->refBackend)) { |
|
| 44 | - $this->refBackend = &$this->backends[$configPrefix]; |
|
| 45 | - } |
|
| 46 | - } |
|
| 47 | - } |
|
| 34 | + /** |
|
| 35 | + * Constructor |
|
| 36 | + * @param string[] $serverConfigPrefixes array containing the config Prefixes |
|
| 37 | + */ |
|
| 38 | + public function __construct($serverConfigPrefixes, ILDAPWrapper $ldap, GroupPluginManager $groupPluginManager) { |
|
| 39 | + parent::__construct($ldap); |
|
| 40 | + foreach($serverConfigPrefixes as $configPrefix) { |
|
| 41 | + $this->backends[$configPrefix] = |
|
| 42 | + new \OCA\User_LDAP\Group_LDAP($this->getAccess($configPrefix), $groupPluginManager); |
|
| 43 | + if(is_null($this->refBackend)) { |
|
| 44 | + $this->refBackend = &$this->backends[$configPrefix]; |
|
| 45 | + } |
|
| 46 | + } |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Tries the backends one after the other until a positive result is returned from the specified method |
|
| 51 | - * @param string $gid the gid connected to the request |
|
| 52 | - * @param string $method the method of the group backend that shall be called |
|
| 53 | - * @param array $parameters an array of parameters to be passed |
|
| 54 | - * @return mixed, the result of the method or false |
|
| 55 | - */ |
|
| 56 | - protected function walkBackends($gid, $method, $parameters) { |
|
| 57 | - $cacheKey = $this->getGroupCacheKey($gid); |
|
| 58 | - foreach($this->backends as $configPrefix => $backend) { |
|
| 59 | - if($result = call_user_func_array(array($backend, $method), $parameters)) { |
|
| 60 | - $this->writeToCache($cacheKey, $configPrefix); |
|
| 61 | - return $result; |
|
| 62 | - } |
|
| 63 | - } |
|
| 64 | - return false; |
|
| 65 | - } |
|
| 49 | + /** |
|
| 50 | + * Tries the backends one after the other until a positive result is returned from the specified method |
|
| 51 | + * @param string $gid the gid connected to the request |
|
| 52 | + * @param string $method the method of the group backend that shall be called |
|
| 53 | + * @param array $parameters an array of parameters to be passed |
|
| 54 | + * @return mixed, the result of the method or false |
|
| 55 | + */ |
|
| 56 | + protected function walkBackends($gid, $method, $parameters) { |
|
| 57 | + $cacheKey = $this->getGroupCacheKey($gid); |
|
| 58 | + foreach($this->backends as $configPrefix => $backend) { |
|
| 59 | + if($result = call_user_func_array(array($backend, $method), $parameters)) { |
|
| 60 | + $this->writeToCache($cacheKey, $configPrefix); |
|
| 61 | + return $result; |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | + return false; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Asks the backend connected to the server that supposely takes care of the gid from the request. |
|
| 69 | - * @param string $gid the gid connected to the request |
|
| 70 | - * @param string $method the method of the group backend that shall be called |
|
| 71 | - * @param array $parameters an array of parameters to be passed |
|
| 72 | - * @param mixed $passOnWhen the result matches this variable |
|
| 73 | - * @return mixed, the result of the method or false |
|
| 74 | - */ |
|
| 75 | - protected function callOnLastSeenOn($gid, $method, $parameters, $passOnWhen) { |
|
| 76 | - $cacheKey = $this->getGroupCacheKey($gid); |
|
| 77 | - $prefix = $this->getFromCache($cacheKey); |
|
| 78 | - //in case the uid has been found in the past, try this stored connection first |
|
| 79 | - if(!is_null($prefix)) { |
|
| 80 | - if(isset($this->backends[$prefix])) { |
|
| 81 | - $result = call_user_func_array(array($this->backends[$prefix], $method), $parameters); |
|
| 82 | - if($result === $passOnWhen) { |
|
| 83 | - //not found here, reset cache to null if group vanished |
|
| 84 | - //because sometimes methods return false with a reason |
|
| 85 | - $groupExists = call_user_func_array( |
|
| 86 | - array($this->backends[$prefix], 'groupExists'), |
|
| 87 | - array($gid) |
|
| 88 | - ); |
|
| 89 | - if(!$groupExists) { |
|
| 90 | - $this->writeToCache($cacheKey, null); |
|
| 91 | - } |
|
| 92 | - } |
|
| 93 | - return $result; |
|
| 94 | - } |
|
| 95 | - } |
|
| 96 | - return false; |
|
| 97 | - } |
|
| 67 | + /** |
|
| 68 | + * Asks the backend connected to the server that supposely takes care of the gid from the request. |
|
| 69 | + * @param string $gid the gid connected to the request |
|
| 70 | + * @param string $method the method of the group backend that shall be called |
|
| 71 | + * @param array $parameters an array of parameters to be passed |
|
| 72 | + * @param mixed $passOnWhen the result matches this variable |
|
| 73 | + * @return mixed, the result of the method or false |
|
| 74 | + */ |
|
| 75 | + protected function callOnLastSeenOn($gid, $method, $parameters, $passOnWhen) { |
|
| 76 | + $cacheKey = $this->getGroupCacheKey($gid); |
|
| 77 | + $prefix = $this->getFromCache($cacheKey); |
|
| 78 | + //in case the uid has been found in the past, try this stored connection first |
|
| 79 | + if(!is_null($prefix)) { |
|
| 80 | + if(isset($this->backends[$prefix])) { |
|
| 81 | + $result = call_user_func_array(array($this->backends[$prefix], $method), $parameters); |
|
| 82 | + if($result === $passOnWhen) { |
|
| 83 | + //not found here, reset cache to null if group vanished |
|
| 84 | + //because sometimes methods return false with a reason |
|
| 85 | + $groupExists = call_user_func_array( |
|
| 86 | + array($this->backends[$prefix], 'groupExists'), |
|
| 87 | + array($gid) |
|
| 88 | + ); |
|
| 89 | + if(!$groupExists) { |
|
| 90 | + $this->writeToCache($cacheKey, null); |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | + return $result; |
|
| 94 | + } |
|
| 95 | + } |
|
| 96 | + return false; |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * is user in group? |
|
| 101 | - * @param string $uid uid of the user |
|
| 102 | - * @param string $gid gid of the group |
|
| 103 | - * @return bool |
|
| 104 | - * |
|
| 105 | - * Checks whether the user is member of a group or not. |
|
| 106 | - */ |
|
| 107 | - public function inGroup($uid, $gid) { |
|
| 108 | - return $this->handleRequest($gid, 'inGroup', array($uid, $gid)); |
|
| 109 | - } |
|
| 99 | + /** |
|
| 100 | + * is user in group? |
|
| 101 | + * @param string $uid uid of the user |
|
| 102 | + * @param string $gid gid of the group |
|
| 103 | + * @return bool |
|
| 104 | + * |
|
| 105 | + * Checks whether the user is member of a group or not. |
|
| 106 | + */ |
|
| 107 | + public function inGroup($uid, $gid) { |
|
| 108 | + return $this->handleRequest($gid, 'inGroup', array($uid, $gid)); |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * Get all groups a user belongs to |
|
| 113 | - * @param string $uid Name of the user |
|
| 114 | - * @return string[] with group names |
|
| 115 | - * |
|
| 116 | - * This function fetches all groups a user belongs to. It does not check |
|
| 117 | - * if the user exists at all. |
|
| 118 | - */ |
|
| 119 | - public function getUserGroups($uid) { |
|
| 120 | - $groups = array(); |
|
| 111 | + /** |
|
| 112 | + * Get all groups a user belongs to |
|
| 113 | + * @param string $uid Name of the user |
|
| 114 | + * @return string[] with group names |
|
| 115 | + * |
|
| 116 | + * This function fetches all groups a user belongs to. It does not check |
|
| 117 | + * if the user exists at all. |
|
| 118 | + */ |
|
| 119 | + public function getUserGroups($uid) { |
|
| 120 | + $groups = array(); |
|
| 121 | 121 | |
| 122 | - foreach($this->backends as $backend) { |
|
| 123 | - $backendGroups = $backend->getUserGroups($uid); |
|
| 124 | - if (is_array($backendGroups)) { |
|
| 125 | - $groups = array_merge($groups, $backendGroups); |
|
| 126 | - } |
|
| 127 | - } |
|
| 122 | + foreach($this->backends as $backend) { |
|
| 123 | + $backendGroups = $backend->getUserGroups($uid); |
|
| 124 | + if (is_array($backendGroups)) { |
|
| 125 | + $groups = array_merge($groups, $backendGroups); |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | - return $groups; |
|
| 130 | - } |
|
| 129 | + return $groups; |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | - /** |
|
| 133 | - * get a list of all users in a group |
|
| 134 | - * @return string[] with user ids |
|
| 135 | - */ |
|
| 136 | - public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
| 137 | - $users = array(); |
|
| 132 | + /** |
|
| 133 | + * get a list of all users in a group |
|
| 134 | + * @return string[] with user ids |
|
| 135 | + */ |
|
| 136 | + public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
| 137 | + $users = array(); |
|
| 138 | 138 | |
| 139 | - foreach($this->backends as $backend) { |
|
| 140 | - $backendUsers = $backend->usersInGroup($gid, $search, $limit, $offset); |
|
| 141 | - if (is_array($backendUsers)) { |
|
| 142 | - $users = array_merge($users, $backendUsers); |
|
| 143 | - } |
|
| 144 | - } |
|
| 139 | + foreach($this->backends as $backend) { |
|
| 140 | + $backendUsers = $backend->usersInGroup($gid, $search, $limit, $offset); |
|
| 141 | + if (is_array($backendUsers)) { |
|
| 142 | + $users = array_merge($users, $backendUsers); |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - return $users; |
|
| 147 | - } |
|
| 146 | + return $users; |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - /** |
|
| 150 | - * @param string $gid |
|
| 151 | - * @return bool |
|
| 152 | - */ |
|
| 153 | - public function createGroup($gid) { |
|
| 154 | - return $this->handleRequest( |
|
| 155 | - $gid, 'createGroup', array($gid)); |
|
| 156 | - } |
|
| 149 | + /** |
|
| 150 | + * @param string $gid |
|
| 151 | + * @return bool |
|
| 152 | + */ |
|
| 153 | + public function createGroup($gid) { |
|
| 154 | + return $this->handleRequest( |
|
| 155 | + $gid, 'createGroup', array($gid)); |
|
| 156 | + } |
|
| 157 | 157 | |
| 158 | - /** |
|
| 159 | - * delete a group |
|
| 160 | - * @param string $gid gid of the group to delete |
|
| 161 | - * @return bool |
|
| 162 | - */ |
|
| 163 | - public function deleteGroup($gid) { |
|
| 164 | - return $this->handleRequest( |
|
| 165 | - $gid, 'deleteGroup', array($gid)); |
|
| 166 | - } |
|
| 158 | + /** |
|
| 159 | + * delete a group |
|
| 160 | + * @param string $gid gid of the group to delete |
|
| 161 | + * @return bool |
|
| 162 | + */ |
|
| 163 | + public function deleteGroup($gid) { |
|
| 164 | + return $this->handleRequest( |
|
| 165 | + $gid, 'deleteGroup', array($gid)); |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - /** |
|
| 169 | - * Add a user to a group |
|
| 170 | - * @param string $uid Name of the user to add to group |
|
| 171 | - * @param string $gid Name of the group in which add the user |
|
| 172 | - * @return bool |
|
| 173 | - * |
|
| 174 | - * Adds a user to a group. |
|
| 175 | - */ |
|
| 176 | - public function addToGroup($uid, $gid) { |
|
| 177 | - return $this->handleRequest( |
|
| 178 | - $gid, 'addToGroup', array($uid, $gid)); |
|
| 179 | - } |
|
| 168 | + /** |
|
| 169 | + * Add a user to a group |
|
| 170 | + * @param string $uid Name of the user to add to group |
|
| 171 | + * @param string $gid Name of the group in which add the user |
|
| 172 | + * @return bool |
|
| 173 | + * |
|
| 174 | + * Adds a user to a group. |
|
| 175 | + */ |
|
| 176 | + public function addToGroup($uid, $gid) { |
|
| 177 | + return $this->handleRequest( |
|
| 178 | + $gid, 'addToGroup', array($uid, $gid)); |
|
| 179 | + } |
|
| 180 | 180 | |
| 181 | - /** |
|
| 182 | - * Removes a user from a group |
|
| 183 | - * @param string $uid Name of the user to remove from group |
|
| 184 | - * @param string $gid Name of the group from which remove the user |
|
| 185 | - * @return bool |
|
| 186 | - * |
|
| 187 | - * removes the user from a group. |
|
| 188 | - */ |
|
| 189 | - public function removeFromGroup($uid, $gid) { |
|
| 190 | - return $this->handleRequest( |
|
| 191 | - $gid, 'removeFromGroup', array($uid, $gid)); |
|
| 192 | - } |
|
| 181 | + /** |
|
| 182 | + * Removes a user from a group |
|
| 183 | + * @param string $uid Name of the user to remove from group |
|
| 184 | + * @param string $gid Name of the group from which remove the user |
|
| 185 | + * @return bool |
|
| 186 | + * |
|
| 187 | + * removes the user from a group. |
|
| 188 | + */ |
|
| 189 | + public function removeFromGroup($uid, $gid) { |
|
| 190 | + return $this->handleRequest( |
|
| 191 | + $gid, 'removeFromGroup', array($uid, $gid)); |
|
| 192 | + } |
|
| 193 | 193 | |
| 194 | - /** |
|
| 195 | - * returns the number of users in a group, who match the search term |
|
| 196 | - * @param string $gid the internal group name |
|
| 197 | - * @param string $search optional, a search string |
|
| 198 | - * @return int|bool |
|
| 199 | - */ |
|
| 200 | - public function countUsersInGroup($gid, $search = '') { |
|
| 201 | - return $this->handleRequest( |
|
| 202 | - $gid, 'countUsersInGroup', array($gid, $search)); |
|
| 203 | - } |
|
| 194 | + /** |
|
| 195 | + * returns the number of users in a group, who match the search term |
|
| 196 | + * @param string $gid the internal group name |
|
| 197 | + * @param string $search optional, a search string |
|
| 198 | + * @return int|bool |
|
| 199 | + */ |
|
| 200 | + public function countUsersInGroup($gid, $search = '') { |
|
| 201 | + return $this->handleRequest( |
|
| 202 | + $gid, 'countUsersInGroup', array($gid, $search)); |
|
| 203 | + } |
|
| 204 | 204 | |
| 205 | - /** |
|
| 206 | - * get an array with group details |
|
| 207 | - * @param string $gid |
|
| 208 | - * @return array|false |
|
| 209 | - */ |
|
| 210 | - public function getGroupDetails($gid) { |
|
| 211 | - return $this->handleRequest( |
|
| 212 | - $gid, 'getGroupDetails', array($gid)); |
|
| 213 | - } |
|
| 205 | + /** |
|
| 206 | + * get an array with group details |
|
| 207 | + * @param string $gid |
|
| 208 | + * @return array|false |
|
| 209 | + */ |
|
| 210 | + public function getGroupDetails($gid) { |
|
| 211 | + return $this->handleRequest( |
|
| 212 | + $gid, 'getGroupDetails', array($gid)); |
|
| 213 | + } |
|
| 214 | 214 | |
| 215 | - /** |
|
| 216 | - * get a list of all groups |
|
| 217 | - * @return string[] with group names |
|
| 218 | - * |
|
| 219 | - * Returns a list with all groups |
|
| 220 | - */ |
|
| 221 | - public function getGroups($search = '', $limit = -1, $offset = 0) { |
|
| 222 | - $groups = array(); |
|
| 215 | + /** |
|
| 216 | + * get a list of all groups |
|
| 217 | + * @return string[] with group names |
|
| 218 | + * |
|
| 219 | + * Returns a list with all groups |
|
| 220 | + */ |
|
| 221 | + public function getGroups($search = '', $limit = -1, $offset = 0) { |
|
| 222 | + $groups = array(); |
|
| 223 | 223 | |
| 224 | - foreach($this->backends as $backend) { |
|
| 225 | - $backendGroups = $backend->getGroups($search, $limit, $offset); |
|
| 226 | - if (is_array($backendGroups)) { |
|
| 227 | - $groups = array_merge($groups, $backendGroups); |
|
| 228 | - } |
|
| 229 | - } |
|
| 224 | + foreach($this->backends as $backend) { |
|
| 225 | + $backendGroups = $backend->getGroups($search, $limit, $offset); |
|
| 226 | + if (is_array($backendGroups)) { |
|
| 227 | + $groups = array_merge($groups, $backendGroups); |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | 230 | |
| 231 | - return $groups; |
|
| 232 | - } |
|
| 231 | + return $groups; |
|
| 232 | + } |
|
| 233 | 233 | |
| 234 | - /** |
|
| 235 | - * check if a group exists |
|
| 236 | - * @param string $gid |
|
| 237 | - * @return bool |
|
| 238 | - */ |
|
| 239 | - public function groupExists($gid) { |
|
| 240 | - return $this->handleRequest($gid, 'groupExists', array($gid)); |
|
| 241 | - } |
|
| 234 | + /** |
|
| 235 | + * check if a group exists |
|
| 236 | + * @param string $gid |
|
| 237 | + * @return bool |
|
| 238 | + */ |
|
| 239 | + public function groupExists($gid) { |
|
| 240 | + return $this->handleRequest($gid, 'groupExists', array($gid)); |
|
| 241 | + } |
|
| 242 | 242 | |
| 243 | - /** |
|
| 244 | - * Check if backend implements actions |
|
| 245 | - * @param int $actions bitwise-or'ed actions |
|
| 246 | - * @return boolean |
|
| 247 | - * |
|
| 248 | - * Returns the supported actions as int to be |
|
| 249 | - * compared with \OCP\GroupInterface::CREATE_GROUP etc. |
|
| 250 | - */ |
|
| 251 | - public function implementsActions($actions) { |
|
| 252 | - //it's the same across all our user backends obviously |
|
| 253 | - return $this->refBackend->implementsActions($actions); |
|
| 254 | - } |
|
| 243 | + /** |
|
| 244 | + * Check if backend implements actions |
|
| 245 | + * @param int $actions bitwise-or'ed actions |
|
| 246 | + * @return boolean |
|
| 247 | + * |
|
| 248 | + * Returns the supported actions as int to be |
|
| 249 | + * compared with \OCP\GroupInterface::CREATE_GROUP etc. |
|
| 250 | + */ |
|
| 251 | + public function implementsActions($actions) { |
|
| 252 | + //it's the same across all our user backends obviously |
|
| 253 | + return $this->refBackend->implementsActions($actions); |
|
| 254 | + } |
|
| 255 | 255 | |
| 256 | - /** |
|
| 257 | - * Return access for LDAP interaction. |
|
| 258 | - * @param string $gid |
|
| 259 | - * @return Access instance of Access for LDAP interaction |
|
| 260 | - */ |
|
| 261 | - public function getLDAPAccess($gid) { |
|
| 262 | - return $this->handleRequest($gid, 'getLDAPAccess', [$gid]); |
|
| 263 | - } |
|
| 256 | + /** |
|
| 257 | + * Return access for LDAP interaction. |
|
| 258 | + * @param string $gid |
|
| 259 | + * @return Access instance of Access for LDAP interaction |
|
| 260 | + */ |
|
| 261 | + public function getLDAPAccess($gid) { |
|
| 262 | + return $this->handleRequest($gid, 'getLDAPAccess', [$gid]); |
|
| 263 | + } |
|
| 264 | 264 | |
| 265 | - /** |
|
| 266 | - * Return a new LDAP connection for the specified group. |
|
| 267 | - * The connection needs to be closed manually. |
|
| 268 | - * @param string $gid |
|
| 269 | - * @return resource of the LDAP connection |
|
| 270 | - */ |
|
| 271 | - public function getNewLDAPConnection($gid) { |
|
| 272 | - return $this->handleRequest($gid, 'getNewLDAPConnection', array($gid)); |
|
| 273 | - } |
|
| 265 | + /** |
|
| 266 | + * Return a new LDAP connection for the specified group. |
|
| 267 | + * The connection needs to be closed manually. |
|
| 268 | + * @param string $gid |
|
| 269 | + * @return resource of the LDAP connection |
|
| 270 | + */ |
|
| 271 | + public function getNewLDAPConnection($gid) { |
|
| 272 | + return $this->handleRequest($gid, 'getNewLDAPConnection', array($gid)); |
|
| 273 | + } |
|
| 274 | 274 | |
| 275 | 275 | } |
@@ -37,10 +37,10 @@ discard block |
||
| 37 | 37 | */ |
| 38 | 38 | public function __construct($serverConfigPrefixes, ILDAPWrapper $ldap, GroupPluginManager $groupPluginManager) { |
| 39 | 39 | parent::__construct($ldap); |
| 40 | - foreach($serverConfigPrefixes as $configPrefix) { |
|
| 40 | + foreach ($serverConfigPrefixes as $configPrefix) { |
|
| 41 | 41 | $this->backends[$configPrefix] = |
| 42 | 42 | new \OCA\User_LDAP\Group_LDAP($this->getAccess($configPrefix), $groupPluginManager); |
| 43 | - if(is_null($this->refBackend)) { |
|
| 43 | + if (is_null($this->refBackend)) { |
|
| 44 | 44 | $this->refBackend = &$this->backends[$configPrefix]; |
| 45 | 45 | } |
| 46 | 46 | } |
@@ -55,8 +55,8 @@ discard block |
||
| 55 | 55 | */ |
| 56 | 56 | protected function walkBackends($gid, $method, $parameters) { |
| 57 | 57 | $cacheKey = $this->getGroupCacheKey($gid); |
| 58 | - foreach($this->backends as $configPrefix => $backend) { |
|
| 59 | - if($result = call_user_func_array(array($backend, $method), $parameters)) { |
|
| 58 | + foreach ($this->backends as $configPrefix => $backend) { |
|
| 59 | + if ($result = call_user_func_array(array($backend, $method), $parameters)) { |
|
| 60 | 60 | $this->writeToCache($cacheKey, $configPrefix); |
| 61 | 61 | return $result; |
| 62 | 62 | } |
@@ -76,17 +76,17 @@ discard block |
||
| 76 | 76 | $cacheKey = $this->getGroupCacheKey($gid); |
| 77 | 77 | $prefix = $this->getFromCache($cacheKey); |
| 78 | 78 | //in case the uid has been found in the past, try this stored connection first |
| 79 | - if(!is_null($prefix)) { |
|
| 80 | - if(isset($this->backends[$prefix])) { |
|
| 79 | + if (!is_null($prefix)) { |
|
| 80 | + if (isset($this->backends[$prefix])) { |
|
| 81 | 81 | $result = call_user_func_array(array($this->backends[$prefix], $method), $parameters); |
| 82 | - if($result === $passOnWhen) { |
|
| 82 | + if ($result === $passOnWhen) { |
|
| 83 | 83 | //not found here, reset cache to null if group vanished |
| 84 | 84 | //because sometimes methods return false with a reason |
| 85 | 85 | $groupExists = call_user_func_array( |
| 86 | 86 | array($this->backends[$prefix], 'groupExists'), |
| 87 | 87 | array($gid) |
| 88 | 88 | ); |
| 89 | - if(!$groupExists) { |
|
| 89 | + if (!$groupExists) { |
|
| 90 | 90 | $this->writeToCache($cacheKey, null); |
| 91 | 91 | } |
| 92 | 92 | } |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | public function getUserGroups($uid) { |
| 120 | 120 | $groups = array(); |
| 121 | 121 | |
| 122 | - foreach($this->backends as $backend) { |
|
| 122 | + foreach ($this->backends as $backend) { |
|
| 123 | 123 | $backendGroups = $backend->getUserGroups($uid); |
| 124 | 124 | if (is_array($backendGroups)) { |
| 125 | 125 | $groups = array_merge($groups, $backendGroups); |
@@ -136,7 +136,7 @@ discard block |
||
| 136 | 136 | public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
| 137 | 137 | $users = array(); |
| 138 | 138 | |
| 139 | - foreach($this->backends as $backend) { |
|
| 139 | + foreach ($this->backends as $backend) { |
|
| 140 | 140 | $backendUsers = $backend->usersInGroup($gid, $search, $limit, $offset); |
| 141 | 141 | if (is_array($backendUsers)) { |
| 142 | 142 | $users = array_merge($users, $backendUsers); |
@@ -221,7 +221,7 @@ discard block |
||
| 221 | 221 | public function getGroups($search = '', $limit = -1, $offset = 0) { |
| 222 | 222 | $groups = array(); |
| 223 | 223 | |
| 224 | - foreach($this->backends as $backend) { |
|
| 224 | + foreach ($this->backends as $backend) { |
|
| 225 | 225 | $backendGroups = $backend->getGroups($search, $limit, $offset); |
| 226 | 226 | if (is_array($backendGroups)) { |
| 227 | 227 | $groups = array_merge($groups, $backendGroups); |