BackgroundJob.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\UpdateNotification\Notification;
  26. use OC\BackgroundJob\TimedJob;
  27. use OC\Installer;
  28. use OC\Updater\VersionCheck;
  29. use OCP\App\IAppManager;
  30. use OCP\IConfig;
  31. use OCP\IGroup;
  32. use OCP\IGroupManager;
  33. use OCP\Notification\IManager;
  34. class BackgroundJob extends TimedJob {
  35. protected $connectionNotifications = [3, 7, 14, 30];
  36. protected IConfig $config;
  37. protected IManager $notificationManager;
  38. protected IGroupManager $groupManager;
  39. protected IAppManager $appManager;
  40. protected Installer $installer;
  41. protected VersionCheck $versionCheck;
  42. /** @var string[] */
  43. protected $users;
  44. public function __construct(
  45. IConfig $config,
  46. IManager $notificationManager,
  47. IGroupManager $groupManager,
  48. IAppManager $appManager,
  49. Installer $installer,
  50. VersionCheck $versionCheck
  51. ) {
  52. // Run once a day
  53. $this->setInterval(60 * 60 * 24);
  54. $this->config = $config;
  55. $this->notificationManager = $notificationManager;
  56. $this->groupManager = $groupManager;
  57. $this->appManager = $appManager;
  58. $this->installer = $installer;
  59. $this->versionCheck = $versionCheck;
  60. }
  61. protected function run($argument) {
  62. if (\OC::$CLI && !$this->config->getSystemValueBool('debug', false)) {
  63. try {
  64. // Jitter the pinging of the updater server and the appstore a bit.
  65. // Otherwise all Nextcloud installations are pinging the servers
  66. // in one of 288
  67. sleep(random_int(1, 180));
  68. } catch (\Exception $e) {
  69. }
  70. }
  71. $this->checkCoreUpdate();
  72. $this->checkAppUpdates();
  73. }
  74. /**
  75. * Check for ownCloud update
  76. */
  77. protected function checkCoreUpdate() {
  78. if (\in_array($this->getChannel(), ['daily', 'git'], true)) {
  79. // "These aren't the update channels you're looking for." - Ben Obi-Wan Kenobi
  80. return;
  81. }
  82. $status = $this->versionCheck->check();
  83. if ($status === false) {
  84. $errors = 1 + (int) $this->config->getAppValue('updatenotification', 'update_check_errors', '0');
  85. $this->config->setAppValue('updatenotification', 'update_check_errors', (string) $errors);
  86. if (\in_array($errors, $this->connectionNotifications, true)) {
  87. $this->sendErrorNotifications($errors);
  88. }
  89. } elseif (\is_array($status)) {
  90. $this->config->setAppValue('updatenotification', 'update_check_errors', 0);
  91. $this->clearErrorNotifications();
  92. if (isset($status['version'])) {
  93. $this->createNotifications('core', $status['version'], $status['versionstring']);
  94. }
  95. }
  96. }
  97. /**
  98. * Send a message to the admin when the update server could not be reached
  99. * @param int $numDays
  100. */
  101. protected function sendErrorNotifications($numDays) {
  102. $this->clearErrorNotifications();
  103. $notification = $this->notificationManager->createNotification();
  104. try {
  105. $notification->setApp('updatenotification')
  106. ->setDateTime(new \DateTime())
  107. ->setObject('updatenotification', 'error')
  108. ->setSubject('connection_error', ['days' => $numDays]);
  109. foreach ($this->getUsersToNotify() as $uid) {
  110. $notification->setUser($uid);
  111. $this->notificationManager->notify($notification);
  112. }
  113. } catch (\InvalidArgumentException $e) {
  114. return;
  115. }
  116. }
  117. /**
  118. * Remove error notifications again
  119. */
  120. protected function clearErrorNotifications() {
  121. $notification = $this->notificationManager->createNotification();
  122. try {
  123. $notification->setApp('updatenotification')
  124. ->setSubject('connection_error')
  125. ->setObject('updatenotification', 'error');
  126. } catch (\InvalidArgumentException $e) {
  127. return;
  128. }
  129. $this->notificationManager->markProcessed($notification);
  130. }
  131. /**
  132. * Check all installed apps for updates
  133. */
  134. protected function checkAppUpdates() {
  135. $apps = $this->appManager->getInstalledApps();
  136. foreach ($apps as $app) {
  137. $update = $this->isUpdateAvailable($app);
  138. if ($update !== false) {
  139. $this->createNotifications($app, $update);
  140. }
  141. }
  142. }
  143. /**
  144. * Create notifications for this app version
  145. *
  146. * @param string $app
  147. * @param string $version
  148. * @param string $visibleVersion
  149. */
  150. protected function createNotifications($app, $version, $visibleVersion = '') {
  151. $lastNotification = $this->config->getAppValue('updatenotification', $app, false);
  152. if ($lastNotification === $version) {
  153. // We already notified about this update
  154. return;
  155. }
  156. if ($lastNotification !== false) {
  157. // Delete old updates
  158. $this->deleteOutdatedNotifications($app, $lastNotification);
  159. }
  160. $notification = $this->notificationManager->createNotification();
  161. try {
  162. $notification->setApp('updatenotification')
  163. ->setDateTime(new \DateTime())
  164. ->setObject($app, $version);
  165. if ($visibleVersion !== '') {
  166. $notification->setSubject('update_available', ['version' => $visibleVersion]);
  167. } else {
  168. $notification->setSubject('update_available');
  169. }
  170. foreach ($this->getUsersToNotify() as $uid) {
  171. $notification->setUser($uid);
  172. $this->notificationManager->notify($notification);
  173. }
  174. } catch (\InvalidArgumentException $e) {
  175. return;
  176. }
  177. $this->config->setAppValue('updatenotification', $app, $version);
  178. }
  179. /**
  180. * @return string[]
  181. */
  182. protected function getUsersToNotify(): array {
  183. if ($this->users !== null) {
  184. return $this->users;
  185. }
  186. $notifyGroups = (array) json_decode($this->config->getAppValue('updatenotification', 'notify_groups', '["admin"]'), true);
  187. $this->users = [];
  188. foreach ($notifyGroups as $group) {
  189. $groupToNotify = $this->groupManager->get($group);
  190. if ($groupToNotify instanceof IGroup) {
  191. foreach ($groupToNotify->getUsers() as $user) {
  192. $this->users[$user->getUID()] = true;
  193. }
  194. }
  195. }
  196. $this->users = array_keys($this->users);
  197. return $this->users;
  198. }
  199. /**
  200. * Delete notifications for old updates
  201. *
  202. * @param string $app
  203. * @param string $version
  204. */
  205. protected function deleteOutdatedNotifications($app, $version) {
  206. $notification = $this->notificationManager->createNotification();
  207. try {
  208. $notification->setApp('updatenotification')
  209. ->setObject($app, $version);
  210. } catch (\InvalidArgumentException $e) {
  211. return;
  212. }
  213. $this->notificationManager->markProcessed($notification);
  214. }
  215. /**
  216. * @return string
  217. */
  218. protected function getChannel(): string {
  219. return \OC_Util::getChannel();
  220. }
  221. /**
  222. * @param string $app
  223. * @return string|false
  224. */
  225. protected function isUpdateAvailable($app) {
  226. return $this->installer->isUpdateAvailable($app);
  227. }
  228. }