MaintenancePlugin.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Connector\Sabre;
  8. use OCA\DAV\Exception\ServerMaintenanceMode;
  9. use OCP\IConfig;
  10. use OCP\IL10N;
  11. use OCP\Util;
  12. use Sabre\DAV\Exception\ServiceUnavailable;
  13. use Sabre\DAV\ServerPlugin;
  14. class MaintenancePlugin extends ServerPlugin {
  15. /** @var IL10N */
  16. private $l10n;
  17. /**
  18. * Reference to main server object
  19. *
  20. * @var Server
  21. */
  22. private $server;
  23. /**
  24. * @param IConfig $config
  25. */
  26. public function __construct(
  27. private IConfig $config,
  28. IL10N $l10n,
  29. ) {
  30. $this->l10n = \OC::$server->getL10N('dav');
  31. }
  32. /**
  33. * This initializes the plugin.
  34. *
  35. * This function is called by \Sabre\DAV\Server, after
  36. * addPlugin is called.
  37. *
  38. * This method should set up the required event subscriptions.
  39. *
  40. * @param \Sabre\DAV\Server $server
  41. * @return void
  42. */
  43. public function initialize(\Sabre\DAV\Server $server) {
  44. $this->server = $server;
  45. $this->server->on('beforeMethod:*', [$this, 'checkMaintenanceMode'], 1);
  46. }
  47. /**
  48. * This method is called before any HTTP method and returns http status code 503
  49. * in case the system is in maintenance mode.
  50. *
  51. * @throws ServiceUnavailable
  52. * @return bool
  53. */
  54. public function checkMaintenanceMode() {
  55. if ($this->config->getSystemValueBool('maintenance')) {
  56. throw new ServerMaintenanceMode($this->l10n->t('System is in maintenance mode.'));
  57. }
  58. if (Util::needUpgrade()) {
  59. throw new ServerMaintenanceMode($this->l10n->t('Upgrade needed'));
  60. }
  61. return true;
  62. }
  63. }