MaintenancePlugin.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 IConfig */
  16. private $config;
  17. /** @var \OCP\IL10N */
  18. private $l10n;
  19. /**
  20. * Reference to main server object
  21. *
  22. * @var Server
  23. */
  24. private $server;
  25. /**
  26. * @param IConfig $config
  27. */
  28. public function __construct(IConfig $config, IL10N $l10n) {
  29. $this->config = $config;
  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. }