BackgroundJob.php 7.4 KB

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