BackgroundJob.php 7.2 KB

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