MaintenancePlugin.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. * @author Valdnet <47037905+Valdnet@users.noreply.github.com>
  11. * @author Vincent Petry <vincent@nextcloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\DAV\Connector\Sabre;
  29. use OCA\DAV\Exception\ServerMaintenanceMode;
  30. use OCP\IConfig;
  31. use OCP\IL10N;
  32. use OCP\Util;
  33. use Sabre\DAV\Exception\ServiceUnavailable;
  34. use Sabre\DAV\ServerPlugin;
  35. class MaintenancePlugin extends ServerPlugin {
  36. /** @var IConfig */
  37. private $config;
  38. /** @var \OCP\IL10N */
  39. private $l10n;
  40. /**
  41. * Reference to main server object
  42. *
  43. * @var Server
  44. */
  45. private $server;
  46. /**
  47. * @param IConfig $config
  48. */
  49. public function __construct(IConfig $config, IL10N $l10n) {
  50. $this->config = $config;
  51. $this->l10n = \OC::$server->getL10N('dav');
  52. }
  53. /**
  54. * This initializes the plugin.
  55. *
  56. * This function is called by \Sabre\DAV\Server, after
  57. * addPlugin is called.
  58. *
  59. * This method should set up the required event subscriptions.
  60. *
  61. * @param \Sabre\DAV\Server $server
  62. * @return void
  63. */
  64. public function initialize(\Sabre\DAV\Server $server) {
  65. $this->server = $server;
  66. $this->server->on('beforeMethod:*', [$this, 'checkMaintenanceMode'], 1);
  67. }
  68. /**
  69. * This method is called before any HTTP method and returns http status code 503
  70. * in case the system is in maintenance mode.
  71. *
  72. * @throws ServiceUnavailable
  73. * @return bool
  74. */
  75. public function checkMaintenanceMode() {
  76. if ($this->config->getSystemValueBool('maintenance')) {
  77. throw new ServerMaintenanceMode($this->l10n->t('System is in maintenance mode.'));
  78. }
  79. if (Util::needUpgrade()) {
  80. throw new ServerMaintenanceMode($this->l10n->t('Upgrade needed'));
  81. }
  82. return true;
  83. }
  84. }