BackgroundJob.php 6.9 KB

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