BackgroundJob.php 6.8 KB

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