UpdateAvailableNotifications.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Ferdinand Thiessen <opensource@fthiessen.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\BackgroundJob;
  27. use OC\Installer;
  28. use OC\Updater\VersionCheck;
  29. use OCP\App\IAppManager;
  30. use OCP\AppFramework\Services\IAppConfig;
  31. use OCP\AppFramework\Utility\ITimeFactory;
  32. use OCP\BackgroundJob\TimedJob;
  33. use OCP\IConfig;
  34. use OCP\IGroup;
  35. use OCP\IGroupManager;
  36. use OCP\Notification\IManager;
  37. class UpdateAvailableNotifications extends TimedJob {
  38. protected $connectionNotifications = [3, 7, 14, 30];
  39. /** @var string[] */
  40. protected $users;
  41. public function __construct(
  42. ITimeFactory $timeFactory,
  43. protected IConfig $config,
  44. protected IAppConfig $appConfig,
  45. protected IManager $notificationManager,
  46. protected IGroupManager $groupManager,
  47. protected IAppManager $appManager,
  48. protected Installer $installer,
  49. protected VersionCheck $versionCheck,
  50. ) {
  51. parent::__construct($timeFactory);
  52. // Run once a day
  53. $this->setInterval(60 * 60 * 24);
  54. }
  55. protected function run($argument) {
  56. // Do not check for updates if not connected to the internet
  57. if (!$this->config->getSystemValueBool('has_internet_connection', true)) {
  58. return;
  59. }
  60. if (\OC::$CLI && !$this->config->getSystemValueBool('debug', false)) {
  61. try {
  62. // Jitter the pinging of the updater server and the appstore a bit.
  63. // Otherwise all Nextcloud installations are pinging the servers
  64. // in one of 288
  65. sleep(random_int(1, 180));
  66. } catch (\Exception $e) {
  67. }
  68. }
  69. $this->checkCoreUpdate();
  70. $this->checkAppUpdates();
  71. }
  72. /**
  73. * Check for ownCloud update
  74. */
  75. protected function checkCoreUpdate() {
  76. if (\in_array($this->getChannel(), ['daily', 'git'], true)) {
  77. // "These aren't the update channels you're looking for." - Ben Obi-Wan Kenobi
  78. return;
  79. }
  80. $status = $this->versionCheck->check();
  81. if ($status === false) {
  82. $errors = 1 + $this->appConfig->getAppValueInt('update_check_errors', 0);
  83. $this->appConfig->setAppValueInt('update_check_errors', $errors);
  84. if (\in_array($errors, $this->connectionNotifications, true)) {
  85. $this->sendErrorNotifications($errors);
  86. }
  87. } elseif (\is_array($status)) {
  88. $this->appConfig->setAppValueInt('update_check_errors', 0);
  89. $this->clearErrorNotifications();
  90. if (isset($status['version'])) {
  91. $this->createNotifications('core', $status['version'], $status['versionstring']);
  92. }
  93. }
  94. }
  95. /**
  96. * Send a message to the admin when the update server could not be reached
  97. * @param int $numDays
  98. */
  99. protected function sendErrorNotifications($numDays) {
  100. $this->clearErrorNotifications();
  101. $notification = $this->notificationManager->createNotification();
  102. try {
  103. $notification->setApp('updatenotification')
  104. ->setDateTime(new \DateTime())
  105. ->setObject('updatenotification', 'error')
  106. ->setSubject('connection_error', ['days' => $numDays]);
  107. foreach ($this->getUsersToNotify() as $uid) {
  108. $notification->setUser($uid);
  109. $this->notificationManager->notify($notification);
  110. }
  111. } catch (\InvalidArgumentException $e) {
  112. return;
  113. }
  114. }
  115. /**
  116. * Remove error notifications again
  117. */
  118. protected function clearErrorNotifications() {
  119. $notification = $this->notificationManager->createNotification();
  120. try {
  121. $notification->setApp('updatenotification')
  122. ->setSubject('connection_error')
  123. ->setObject('updatenotification', 'error');
  124. } catch (\InvalidArgumentException $e) {
  125. return;
  126. }
  127. $this->notificationManager->markProcessed($notification);
  128. }
  129. /**
  130. * Check all installed apps for updates
  131. */
  132. protected function checkAppUpdates() {
  133. $apps = $this->appManager->getInstalledApps();
  134. foreach ($apps as $app) {
  135. $update = $this->isUpdateAvailable($app);
  136. if ($update !== false) {
  137. $this->createNotifications($app, $update);
  138. }
  139. }
  140. }
  141. /**
  142. * Create notifications for this app version
  143. *
  144. * @param string $app
  145. * @param string $version
  146. * @param string $visibleVersion
  147. */
  148. protected function createNotifications($app, $version, $visibleVersion = '') {
  149. $lastNotification = $this->appConfig->getAppValueString($app, '');
  150. if ($lastNotification === $version) {
  151. // We already notified about this update
  152. return;
  153. }
  154. if ($lastNotification !== '') {
  155. // Delete old updates
  156. $this->deleteOutdatedNotifications($app, $lastNotification);
  157. }
  158. $notification = $this->notificationManager->createNotification();
  159. try {
  160. $notification->setApp('updatenotification')
  161. ->setDateTime(new \DateTime())
  162. ->setObject($app, $version);
  163. if ($visibleVersion !== '') {
  164. $notification->setSubject('update_available', ['version' => $visibleVersion]);
  165. } else {
  166. $notification->setSubject('update_available');
  167. }
  168. foreach ($this->getUsersToNotify() as $uid) {
  169. $notification->setUser($uid);
  170. $this->notificationManager->notify($notification);
  171. }
  172. } catch (\InvalidArgumentException $e) {
  173. return;
  174. }
  175. $this->appConfig->setAppValueString($app, $version);
  176. }
  177. /**
  178. * @return string[]
  179. */
  180. protected function getUsersToNotify(): array {
  181. if ($this->users !== null) {
  182. return $this->users;
  183. }
  184. $notifyGroups = $this->appConfig->getAppValueArray('notify_groups', ['admin']);
  185. $this->users = [];
  186. foreach ($notifyGroups as $group) {
  187. $groupToNotify = $this->groupManager->get($group);
  188. if ($groupToNotify instanceof IGroup) {
  189. foreach ($groupToNotify->getUsers() as $user) {
  190. $this->users[] = $user->getUID();
  191. }
  192. }
  193. }
  194. $this->users = array_values(array_unique($this->users));
  195. return $this->users;
  196. }
  197. /**
  198. * Delete notifications for old updates
  199. *
  200. * @param string $app
  201. * @param string $version
  202. */
  203. protected function deleteOutdatedNotifications($app, $version) {
  204. $notification = $this->notificationManager->createNotification();
  205. try {
  206. $notification->setApp('updatenotification')
  207. ->setObject($app, $version);
  208. } catch (\InvalidArgumentException $e) {
  209. return;
  210. }
  211. $this->notificationManager->markProcessed($notification);
  212. }
  213. /**
  214. * @return string
  215. */
  216. protected function getChannel(): string {
  217. return \OC_Util::getChannel();
  218. }
  219. /**
  220. * @param string $app
  221. * @return string|false
  222. */
  223. protected function isUpdateAvailable($app) {
  224. return $this->installer->isUpdateAvailable($app);
  225. }
  226. }