1
0

Admin.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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\Support\Subscription\IRegistry;
  36. use OCP\Util;
  37. class Admin implements ISettings {
  38. /** @var IConfig */
  39. private $config;
  40. /** @var UpdateChecker */
  41. private $updateChecker;
  42. /** @var IGroupManager */
  43. private $groupManager;
  44. /** @var IDateTimeFormatter */
  45. private $dateTimeFormatter;
  46. /** @var IFactory */
  47. private $l10nFactory;
  48. /** @var IRegistry */
  49. private $subscriptionRegistry;
  50. public function __construct(
  51. IConfig $config,
  52. UpdateChecker $updateChecker,
  53. IGroupManager $groupManager,
  54. IDateTimeFormatter $dateTimeFormatter,
  55. IFactory $l10nFactory,
  56. IRegistry $subscriptionRegistry
  57. ) {
  58. $this->config = $config;
  59. $this->updateChecker = $updateChecker;
  60. $this->groupManager = $groupManager;
  61. $this->dateTimeFormatter = $dateTimeFormatter;
  62. $this->l10nFactory = $l10nFactory;
  63. $this->subscriptionRegistry = $subscriptionRegistry;
  64. }
  65. /**
  66. * @return TemplateResponse
  67. */
  68. public function getForm(): TemplateResponse {
  69. $lastUpdateCheckTimestamp = $this->config->getAppValue('core', 'lastupdatedat');
  70. $lastUpdateCheck = $this->dateTimeFormatter->formatDateTime($lastUpdateCheckTimestamp);
  71. $channels = [
  72. 'daily',
  73. 'beta',
  74. 'stable',
  75. 'production',
  76. ];
  77. $currentChannel = Util::getChannel();
  78. if ($currentChannel === 'git') {
  79. $channels[] = 'git';
  80. }
  81. $updateState = $this->updateChecker->getUpdateState();
  82. $notifyGroups = json_decode($this->config->getAppValue('updatenotification', 'notify_groups', '["admin"]'), true);
  83. $defaultUpdateServerURL = 'https://updates.nextcloud.com/updater_server/';
  84. $updateServerURL = $this->config->getSystemValue('updater.server.url', $defaultUpdateServerURL);
  85. $defaultCustomerUpdateServerURLPrefix = 'https://updates.nextcloud.com/customers/';
  86. $isDefaultUpdateServerURL = $updateServerURL === $defaultUpdateServerURL
  87. || $updateServerURL === substr($updateServerURL, 0, strlen($defaultCustomerUpdateServerURLPrefix));
  88. $hasValidSubscription = $this->subscriptionRegistry->delegateHasValidSubscription();
  89. $params = [
  90. 'isNewVersionAvailable' => !empty($updateState['updateAvailable']),
  91. 'isUpdateChecked' => $lastUpdateCheckTimestamp > 0,
  92. 'lastChecked' => $lastUpdateCheck,
  93. 'currentChannel' => $currentChannel,
  94. 'channels' => $channels,
  95. 'newVersion' => empty($updateState['updateVersion']) ? '' : $updateState['updateVersion'],
  96. 'newVersionString' => empty($updateState['updateVersionString']) ? '' : $updateState['updateVersionString'],
  97. 'downloadLink' => empty($updateState['downloadLink']) ? '' : $updateState['downloadLink'],
  98. 'changes' => $this->filterChanges($updateState['changes'] ?? []),
  99. 'updaterEnabled' => empty($updateState['updaterEnabled']) ? false : $updateState['updaterEnabled'],
  100. 'versionIsEol' => empty($updateState['versionIsEol']) ? false : $updateState['versionIsEol'],
  101. 'isDefaultUpdateServerURL' => $isDefaultUpdateServerURL,
  102. 'updateServerURL' => $updateServerURL,
  103. 'notifyGroups' => $this->getSelectedGroups($notifyGroups),
  104. 'hasValidSubscription' => $hasValidSubscription,
  105. ];
  106. $params = [
  107. 'json' => json_encode($params),
  108. ];
  109. return new TemplateResponse('updatenotification', 'admin', $params, '');
  110. }
  111. protected function filterChanges(array $changes): array {
  112. $filtered = [];
  113. if(isset($changes['changelogURL'])) {
  114. $filtered['changelogURL'] = $changes['changelogURL'];
  115. }
  116. if(!isset($changes['whatsNew'])) {
  117. return $filtered;
  118. }
  119. $iterator = $this->l10nFactory->getLanguageIterator();
  120. do {
  121. $lang = $iterator->current();
  122. if(isset($changes['whatsNew'][$lang])) {
  123. $filtered['whatsNew'] = $changes['whatsNew'][$lang];
  124. return $filtered;
  125. }
  126. $iterator->next();
  127. } while($lang !== 'en' && $iterator->valid());
  128. return $filtered;
  129. }
  130. /**
  131. * @param array $groupIds
  132. * @return array
  133. */
  134. protected function getSelectedGroups(array $groupIds): array {
  135. $result = [];
  136. foreach ($groupIds as $groupId) {
  137. $group = $this->groupManager->get($groupId);
  138. if ($group === null) {
  139. continue;
  140. }
  141. $result[] = ['value' => $group->getGID(), 'label' => $group->getDisplayName()];
  142. }
  143. return $result;
  144. }
  145. /**
  146. * @return string the section ID, e.g. 'sharing'
  147. */
  148. public function getSection(): string {
  149. return 'overview';
  150. }
  151. /**
  152. * @return int whether the form should be rather on the top or bottom of
  153. * the admin section. The forms are arranged in ascending order of the
  154. * priority values. It is required to return a value between 0 and 100.
  155. *
  156. * E.g.: 70
  157. */
  158. public function getPriority(): int {
  159. return 11;
  160. }
  161. }