You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Admin.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\UpdateNotification\Settings;
  27. use OCA\UpdateNotification\UpdateChecker;
  28. use OCP\AppFramework\Http\TemplateResponse;
  29. use OCP\IConfig;
  30. use OCP\IDateTimeFormatter;
  31. use OCP\IGroupManager;
  32. use OCP\IUserSession;
  33. use OCP\L10N\IFactory;
  34. use OCP\Settings\ISettings;
  35. use OCP\Util;
  36. class Admin implements ISettings {
  37. /** @var IConfig */
  38. private $config;
  39. /** @var UpdateChecker */
  40. private $updateChecker;
  41. /** @var IGroupManager */
  42. private $groupManager;
  43. /** @var IDateTimeFormatter */
  44. private $dateTimeFormatter;
  45. /** @var IUserSession */
  46. private $session;
  47. /** @var IFactory */
  48. private $l10nFactory;
  49. public function __construct(
  50. IConfig $config,
  51. UpdateChecker $updateChecker,
  52. IGroupManager $groupManager,
  53. IDateTimeFormatter $dateTimeFormatter,
  54. IUserSession $session,
  55. IFactory $l10nFactory
  56. ) {
  57. $this->config = $config;
  58. $this->updateChecker = $updateChecker;
  59. $this->groupManager = $groupManager;
  60. $this->dateTimeFormatter = $dateTimeFormatter;
  61. $this->session = $session;
  62. $this->l10nFactory = $l10nFactory;
  63. }
  64. /**
  65. * @return TemplateResponse
  66. */
  67. public function getForm(): TemplateResponse {
  68. $lastUpdateCheckTimestamp = $this->config->getAppValue('core', 'lastupdatedat');
  69. $lastUpdateCheck = $this->dateTimeFormatter->formatDateTime($lastUpdateCheckTimestamp);
  70. $channels = [
  71. 'daily',
  72. 'beta',
  73. 'stable',
  74. 'production',
  75. ];
  76. $currentChannel = Util::getChannel();
  77. if ($currentChannel === 'git') {
  78. $channels[] = 'git';
  79. }
  80. $updateState = $this->updateChecker->getUpdateState();
  81. $notifyGroups = json_decode($this->config->getAppValue('updatenotification', 'notify_groups', '["admin"]'), true);
  82. $defaultUpdateServerURL = 'https://updates.nextcloud.com/updater_server/';
  83. $updateServerURL = $this->config->getSystemValue('updater.server.url', $defaultUpdateServerURL);
  84. $params = [
  85. 'isNewVersionAvailable' => !empty($updateState['updateAvailable']),
  86. 'isUpdateChecked' => $lastUpdateCheckTimestamp > 0,
  87. 'lastChecked' => $lastUpdateCheck,
  88. 'currentChannel' => $currentChannel,
  89. 'channels' => $channels,
  90. 'newVersion' => empty($updateState['updateVersion']) ? '' : $updateState['updateVersion'],
  91. 'newVersionString' => empty($updateState['updateVersionString']) ? '' : $updateState['updateVersionString'],
  92. 'downloadLink' => empty($updateState['downloadLink']) ? '' : $updateState['downloadLink'],
  93. 'changes' => $this->filterChanges($updateState['changes'] ?? []),
  94. 'updaterEnabled' => empty($updateState['updaterEnabled']) ? false : $updateState['updaterEnabled'],
  95. 'versionIsEol' => empty($updateState['versionIsEol']) ? false : $updateState['versionIsEol'],
  96. 'isDefaultUpdateServerURL' => $updateServerURL === $defaultUpdateServerURL,
  97. 'updateServerURL' => $updateServerURL,
  98. 'notifyGroups' => $this->getSelectedGroups($notifyGroups),
  99. ];
  100. $params = [
  101. 'json' => json_encode($params),
  102. ];
  103. return new TemplateResponse('updatenotification', 'admin', $params, '');
  104. }
  105. protected function filterChanges(array $changes): array {
  106. $filtered = [];
  107. if(isset($changes['changelogURL'])) {
  108. $filtered['changelogURL'] = $changes['changelogURL'];
  109. }
  110. if(!isset($changes['whatsNew'])) {
  111. return $filtered;
  112. }
  113. $iterator = $this->l10nFactory->getLanguageIterator();
  114. do {
  115. $lang = $iterator->current();
  116. if(isset($changes['whatsNew'][$lang])) {
  117. $filtered['whatsNew'] = $changes['whatsNew'][$lang];
  118. return $filtered;
  119. }
  120. $iterator->next();
  121. } while($lang !== 'en' && $iterator->valid());
  122. return $filtered;
  123. }
  124. /**
  125. * @param array $groupIds
  126. * @return array
  127. */
  128. protected function getSelectedGroups(array $groupIds): array {
  129. $result = [];
  130. foreach ($groupIds as $groupId) {
  131. $group = $this->groupManager->get($groupId);
  132. if ($group === null) {
  133. continue;
  134. }
  135. $result[] = ['value' => $group->getGID(), 'label' => $group->getDisplayName()];
  136. }
  137. return $result;
  138. }
  139. /**
  140. * @return string the section ID, e.g. 'sharing'
  141. */
  142. public function getSection(): string {
  143. return 'overview';
  144. }
  145. /**
  146. * @return int whether the form should be rather on the top or bottom of
  147. * the admin section. The forms are arranged in ascending order of the
  148. * priority values. It is required to return a value between 0 and 100.
  149. *
  150. * E.g.: 70
  151. */
  152. public function getPriority(): int {
  153. return 11;
  154. }
  155. }