@@ -36,141 +36,141 @@ |
||
| 36 | 36 | |
| 37 | 37 | class Notifier implements INotifier { |
| 38 | 38 | |
| 39 | - /** @var IURLGenerator */ |
|
| 40 | - protected $url; |
|
| 41 | - |
|
| 42 | - /** @var IConfig */ |
|
| 43 | - protected $config; |
|
| 44 | - |
|
| 45 | - /** @var IManager */ |
|
| 46 | - protected $notificationManager; |
|
| 47 | - |
|
| 48 | - /** @var IFactory */ |
|
| 49 | - protected $l10NFactory; |
|
| 50 | - |
|
| 51 | - /** @var IUserSession */ |
|
| 52 | - protected $userSession; |
|
| 53 | - |
|
| 54 | - /** @var IGroupManager */ |
|
| 55 | - protected $groupManager; |
|
| 56 | - |
|
| 57 | - /** @var string[] */ |
|
| 58 | - protected $appVersions; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Notifier constructor. |
|
| 62 | - * |
|
| 63 | - * @param IURLGenerator $url |
|
| 64 | - * @param IConfig $config |
|
| 65 | - * @param IManager $notificationManager |
|
| 66 | - * @param IFactory $l10NFactory |
|
| 67 | - * @param IUserSession $userSession |
|
| 68 | - * @param IGroupManager $groupManager |
|
| 69 | - */ |
|
| 70 | - public function __construct(IURLGenerator $url, IConfig $config, IManager $notificationManager, IFactory $l10NFactory, IUserSession $userSession, IGroupManager $groupManager) { |
|
| 71 | - $this->url = $url; |
|
| 72 | - $this->notificationManager = $notificationManager; |
|
| 73 | - $this->config = $config; |
|
| 74 | - $this->l10NFactory = $l10NFactory; |
|
| 75 | - $this->userSession = $userSession; |
|
| 76 | - $this->groupManager = $groupManager; |
|
| 77 | - $this->appVersions = $this->getAppVersions(); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @param INotification $notification |
|
| 82 | - * @param string $languageCode The code of the language that should be used to prepare the notification |
|
| 83 | - * @return INotification |
|
| 84 | - * @throws \InvalidArgumentException When the notification was not prepared by a notifier |
|
| 85 | - * @since 9.0.0 |
|
| 86 | - */ |
|
| 87 | - public function prepare(INotification $notification, $languageCode) { |
|
| 88 | - if ($notification->getApp() !== 'updatenotification') { |
|
| 89 | - throw new \InvalidArgumentException(); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - $l = $this->l10NFactory->get('updatenotification', $languageCode); |
|
| 93 | - if ($notification->getSubject() === 'connection_error') { |
|
| 94 | - $errors = (int) $this->config->getAppValue('updatenotification', 'update_check_errors', 0); |
|
| 95 | - if ($errors === 0) { |
|
| 96 | - $this->notificationManager->markProcessed($notification); |
|
| 97 | - throw new \InvalidArgumentException(); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - $notification->setParsedSubject($l->t('The update server could not be reached since %d days to check for new updates.', [$errors])) |
|
| 101 | - ->setParsedMessage($l->t('Please check the nextcloud and server log files for errors.')); |
|
| 102 | - } elseif ($notification->getObjectType() === 'core') { |
|
| 103 | - $this->updateAlreadyInstalledCheck($notification, $this->getCoreVersions()); |
|
| 104 | - |
|
| 105 | - $parameters = $notification->getSubjectParameters(); |
|
| 106 | - $notification->setParsedSubject($l->t('Update to %1$s is available.', [$parameters['version']])); |
|
| 107 | - |
|
| 108 | - if ($this->isAdmin()) { |
|
| 109 | - $notification->setLink($this->url->linkToRouteAbsolute('settings.AdminSettings.index') . '#updater'); |
|
| 110 | - } |
|
| 111 | - } else { |
|
| 112 | - $appInfo = $this->getAppInfo($notification->getObjectType()); |
|
| 113 | - $appName = ($appInfo === null) ? $notification->getObjectType() : $appInfo['name']; |
|
| 114 | - |
|
| 115 | - if (isset($this->appVersions[$notification->getObjectType()])) { |
|
| 116 | - $this->updateAlreadyInstalledCheck($notification, $this->appVersions[$notification->getObjectType()]); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - $notification->setParsedSubject($l->t('Update for %1$s to version %2$s is available.', [$appName, $notification->getObjectId()])) |
|
| 120 | - ->setRichSubject($l->t('Update for {app} to version %s is available.', $notification->getObjectId()), [ |
|
| 121 | - 'app' => [ |
|
| 122 | - 'type' => 'app', |
|
| 123 | - 'id' => $notification->getObjectType(), |
|
| 124 | - 'name' => $appName, |
|
| 125 | - ] |
|
| 126 | - ]); |
|
| 127 | - |
|
| 128 | - if ($this->isAdmin()) { |
|
| 129 | - $notification->setLink($this->url->linkToRouteAbsolute('settings.AppSettings.viewApps') . '#app-' . $notification->getObjectType()); |
|
| 130 | - } |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - $notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('updatenotification', 'notification.svg'))); |
|
| 134 | - |
|
| 135 | - return $notification; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Remove the notification and prevent rendering, when the update is installed |
|
| 140 | - * |
|
| 141 | - * @param INotification $notification |
|
| 142 | - * @param string $installedVersion |
|
| 143 | - * @throws \InvalidArgumentException When the update is already installed |
|
| 144 | - */ |
|
| 145 | - protected function updateAlreadyInstalledCheck(INotification $notification, $installedVersion) { |
|
| 146 | - if (version_compare($notification->getObjectId(), $installedVersion, '<=')) { |
|
| 147 | - $this->notificationManager->markProcessed($notification); |
|
| 148 | - throw new \InvalidArgumentException(); |
|
| 149 | - } |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * @return bool |
|
| 154 | - */ |
|
| 155 | - protected function isAdmin() { |
|
| 156 | - $user = $this->userSession->getUser(); |
|
| 157 | - |
|
| 158 | - if ($user instanceof IUser) { |
|
| 159 | - return $this->groupManager->isAdmin($user->getUID()); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - return false; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - protected function getCoreVersions() { |
|
| 166 | - return implode('.', \OCP\Util::getVersion()); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - protected function getAppVersions() { |
|
| 170 | - return \OC_App::getAppVersions(); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - protected function getAppInfo($appId) { |
|
| 174 | - return \OC_App::getAppInfo($appId); |
|
| 175 | - } |
|
| 39 | + /** @var IURLGenerator */ |
|
| 40 | + protected $url; |
|
| 41 | + |
|
| 42 | + /** @var IConfig */ |
|
| 43 | + protected $config; |
|
| 44 | + |
|
| 45 | + /** @var IManager */ |
|
| 46 | + protected $notificationManager; |
|
| 47 | + |
|
| 48 | + /** @var IFactory */ |
|
| 49 | + protected $l10NFactory; |
|
| 50 | + |
|
| 51 | + /** @var IUserSession */ |
|
| 52 | + protected $userSession; |
|
| 53 | + |
|
| 54 | + /** @var IGroupManager */ |
|
| 55 | + protected $groupManager; |
|
| 56 | + |
|
| 57 | + /** @var string[] */ |
|
| 58 | + protected $appVersions; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Notifier constructor. |
|
| 62 | + * |
|
| 63 | + * @param IURLGenerator $url |
|
| 64 | + * @param IConfig $config |
|
| 65 | + * @param IManager $notificationManager |
|
| 66 | + * @param IFactory $l10NFactory |
|
| 67 | + * @param IUserSession $userSession |
|
| 68 | + * @param IGroupManager $groupManager |
|
| 69 | + */ |
|
| 70 | + public function __construct(IURLGenerator $url, IConfig $config, IManager $notificationManager, IFactory $l10NFactory, IUserSession $userSession, IGroupManager $groupManager) { |
|
| 71 | + $this->url = $url; |
|
| 72 | + $this->notificationManager = $notificationManager; |
|
| 73 | + $this->config = $config; |
|
| 74 | + $this->l10NFactory = $l10NFactory; |
|
| 75 | + $this->userSession = $userSession; |
|
| 76 | + $this->groupManager = $groupManager; |
|
| 77 | + $this->appVersions = $this->getAppVersions(); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @param INotification $notification |
|
| 82 | + * @param string $languageCode The code of the language that should be used to prepare the notification |
|
| 83 | + * @return INotification |
|
| 84 | + * @throws \InvalidArgumentException When the notification was not prepared by a notifier |
|
| 85 | + * @since 9.0.0 |
|
| 86 | + */ |
|
| 87 | + public function prepare(INotification $notification, $languageCode) { |
|
| 88 | + if ($notification->getApp() !== 'updatenotification') { |
|
| 89 | + throw new \InvalidArgumentException(); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + $l = $this->l10NFactory->get('updatenotification', $languageCode); |
|
| 93 | + if ($notification->getSubject() === 'connection_error') { |
|
| 94 | + $errors = (int) $this->config->getAppValue('updatenotification', 'update_check_errors', 0); |
|
| 95 | + if ($errors === 0) { |
|
| 96 | + $this->notificationManager->markProcessed($notification); |
|
| 97 | + throw new \InvalidArgumentException(); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + $notification->setParsedSubject($l->t('The update server could not be reached since %d days to check for new updates.', [$errors])) |
|
| 101 | + ->setParsedMessage($l->t('Please check the nextcloud and server log files for errors.')); |
|
| 102 | + } elseif ($notification->getObjectType() === 'core') { |
|
| 103 | + $this->updateAlreadyInstalledCheck($notification, $this->getCoreVersions()); |
|
| 104 | + |
|
| 105 | + $parameters = $notification->getSubjectParameters(); |
|
| 106 | + $notification->setParsedSubject($l->t('Update to %1$s is available.', [$parameters['version']])); |
|
| 107 | + |
|
| 108 | + if ($this->isAdmin()) { |
|
| 109 | + $notification->setLink($this->url->linkToRouteAbsolute('settings.AdminSettings.index') . '#updater'); |
|
| 110 | + } |
|
| 111 | + } else { |
|
| 112 | + $appInfo = $this->getAppInfo($notification->getObjectType()); |
|
| 113 | + $appName = ($appInfo === null) ? $notification->getObjectType() : $appInfo['name']; |
|
| 114 | + |
|
| 115 | + if (isset($this->appVersions[$notification->getObjectType()])) { |
|
| 116 | + $this->updateAlreadyInstalledCheck($notification, $this->appVersions[$notification->getObjectType()]); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + $notification->setParsedSubject($l->t('Update for %1$s to version %2$s is available.', [$appName, $notification->getObjectId()])) |
|
| 120 | + ->setRichSubject($l->t('Update for {app} to version %s is available.', $notification->getObjectId()), [ |
|
| 121 | + 'app' => [ |
|
| 122 | + 'type' => 'app', |
|
| 123 | + 'id' => $notification->getObjectType(), |
|
| 124 | + 'name' => $appName, |
|
| 125 | + ] |
|
| 126 | + ]); |
|
| 127 | + |
|
| 128 | + if ($this->isAdmin()) { |
|
| 129 | + $notification->setLink($this->url->linkToRouteAbsolute('settings.AppSettings.viewApps') . '#app-' . $notification->getObjectType()); |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + $notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('updatenotification', 'notification.svg'))); |
|
| 134 | + |
|
| 135 | + return $notification; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Remove the notification and prevent rendering, when the update is installed |
|
| 140 | + * |
|
| 141 | + * @param INotification $notification |
|
| 142 | + * @param string $installedVersion |
|
| 143 | + * @throws \InvalidArgumentException When the update is already installed |
|
| 144 | + */ |
|
| 145 | + protected function updateAlreadyInstalledCheck(INotification $notification, $installedVersion) { |
|
| 146 | + if (version_compare($notification->getObjectId(), $installedVersion, '<=')) { |
|
| 147 | + $this->notificationManager->markProcessed($notification); |
|
| 148 | + throw new \InvalidArgumentException(); |
|
| 149 | + } |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * @return bool |
|
| 154 | + */ |
|
| 155 | + protected function isAdmin() { |
|
| 156 | + $user = $this->userSession->getUser(); |
|
| 157 | + |
|
| 158 | + if ($user instanceof IUser) { |
|
| 159 | + return $this->groupManager->isAdmin($user->getUID()); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + return false; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + protected function getCoreVersions() { |
|
| 166 | + return implode('.', \OCP\Util::getVersion()); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + protected function getAppVersions() { |
|
| 170 | + return \OC_App::getAppVersions(); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + protected function getAppInfo($appId) { |
|
| 174 | + return \OC_App::getAppInfo($appId); |
|
| 175 | + } |
|
| 176 | 176 | } |
@@ -34,225 +34,225 @@ |
||
| 34 | 34 | |
| 35 | 35 | class BackgroundJob extends TimedJob { |
| 36 | 36 | |
| 37 | - protected $connectionNotifications = [3, 7, 14, 30]; |
|
| 38 | - |
|
| 39 | - /** @var IConfig */ |
|
| 40 | - protected $config; |
|
| 41 | - |
|
| 42 | - /** @var IManager */ |
|
| 43 | - protected $notificationManager; |
|
| 44 | - |
|
| 45 | - /** @var IGroupManager */ |
|
| 46 | - protected $groupManager; |
|
| 47 | - |
|
| 48 | - /** @var IAppManager */ |
|
| 49 | - protected $appManager; |
|
| 50 | - |
|
| 51 | - /** @var IClientService */ |
|
| 52 | - protected $client; |
|
| 53 | - |
|
| 54 | - /** @var string[] */ |
|
| 55 | - protected $users; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * NotificationBackgroundJob constructor. |
|
| 59 | - * |
|
| 60 | - * @param IConfig $config |
|
| 61 | - * @param IManager $notificationManager |
|
| 62 | - * @param IGroupManager $groupManager |
|
| 63 | - * @param IAppManager $appManager |
|
| 64 | - * @param IClientService $client |
|
| 65 | - */ |
|
| 66 | - public function __construct(IConfig $config, IManager $notificationManager, IGroupManager $groupManager, IAppManager $appManager, IClientService $client) { |
|
| 67 | - // Run once a day |
|
| 68 | - $this->setInterval(60 * 60 * 24); |
|
| 69 | - |
|
| 70 | - $this->config = $config; |
|
| 71 | - $this->notificationManager = $notificationManager; |
|
| 72 | - $this->groupManager = $groupManager; |
|
| 73 | - $this->appManager = $appManager; |
|
| 74 | - $this->client = $client; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - protected function run($argument) { |
|
| 78 | - $this->checkCoreUpdate(); |
|
| 79 | - $this->checkAppUpdates(); |
|
| 80 | - } |
|
| 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 | - |
|
| 91 | - $updater = $this->createVersionCheck(); |
|
| 92 | - |
|
| 93 | - $status = $updater->check(); |
|
| 94 | - if ($status === false) { |
|
| 95 | - $errors = 1 + (int) $this->config->getAppValue('updatenotification', 'update_check_errors', 0); |
|
| 96 | - $this->config->setAppValue('updatenotification', 'update_check_errors', $errors); |
|
| 97 | - |
|
| 98 | - if (in_array($errors, $this->connectionNotifications, true)) { |
|
| 99 | - $this->sendErrorNotifications($errors); |
|
| 100 | - } |
|
| 101 | - } else if (isset($status['version'])) { |
|
| 102 | - $this->config->setAppValue('updatenotification', 'update_check_errors', 0); |
|
| 103 | - $this->clearErrorNotifications(); |
|
| 104 | - |
|
| 105 | - $this->createNotifications('core', $status['version'], $status['versionstring']); |
|
| 106 | - } |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Send a message to the admin when the update server could not be reached |
|
| 111 | - * @param int $numDays |
|
| 112 | - */ |
|
| 113 | - protected function sendErrorNotifications($numDays) { |
|
| 114 | - $this->clearErrorNotifications(); |
|
| 115 | - |
|
| 116 | - $notification = $this->notificationManager->createNotification(); |
|
| 117 | - try { |
|
| 118 | - $notification->setApp('updatenotification') |
|
| 119 | - ->setDateTime(new \DateTime()) |
|
| 120 | - ->setObject('updatenotification', 'error') |
|
| 121 | - ->setSubject('connection_error', ['days' => $numDays]); |
|
| 122 | - |
|
| 123 | - foreach ($this->getUsersToNotify() as $uid) { |
|
| 124 | - $notification->setUser($uid); |
|
| 125 | - $this->notificationManager->notify($notification); |
|
| 126 | - } |
|
| 127 | - } catch (\InvalidArgumentException $e) { |
|
| 128 | - return; |
|
| 129 | - } |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Remove error notifications again |
|
| 134 | - */ |
|
| 135 | - protected function clearErrorNotifications() { |
|
| 136 | - $notification = $this->notificationManager->createNotification(); |
|
| 137 | - try { |
|
| 138 | - $notification->setApp('updatenotification') |
|
| 139 | - ->setSubject('connection_error') |
|
| 140 | - ->setObject('updatenotification', 'error'); |
|
| 141 | - } catch (\InvalidArgumentException $e) { |
|
| 142 | - return; |
|
| 143 | - } |
|
| 144 | - $this->notificationManager->markProcessed($notification); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Check all installed apps for updates |
|
| 149 | - */ |
|
| 150 | - protected function checkAppUpdates() { |
|
| 151 | - $apps = $this->appManager->getInstalledApps(); |
|
| 152 | - foreach ($apps as $app) { |
|
| 153 | - $update = $this->isUpdateAvailable($app); |
|
| 154 | - if ($update !== false) { |
|
| 155 | - $this->createNotifications($app, $update); |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Create notifications for this app version |
|
| 162 | - * |
|
| 163 | - * @param string $app |
|
| 164 | - * @param string $version |
|
| 165 | - * @param string $visibleVersion |
|
| 166 | - */ |
|
| 167 | - protected function createNotifications($app, $version, $visibleVersion = '') { |
|
| 168 | - $lastNotification = $this->config->getAppValue('updatenotification', $app, false); |
|
| 169 | - if ($lastNotification === $version) { |
|
| 170 | - // We already notified about this update |
|
| 171 | - return; |
|
| 172 | - } else if ($lastNotification !== false) { |
|
| 173 | - // Delete old updates |
|
| 174 | - $this->deleteOutdatedNotifications($app, $lastNotification); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - |
|
| 178 | - $notification = $this->notificationManager->createNotification(); |
|
| 179 | - $notification->setApp('updatenotification') |
|
| 180 | - ->setDateTime(new \DateTime()) |
|
| 181 | - ->setObject($app, $version); |
|
| 182 | - |
|
| 183 | - if ($visibleVersion !== '') { |
|
| 184 | - $notification->setSubject('update_available', ['version' => $visibleVersion]); |
|
| 185 | - } else { |
|
| 186 | - $notification->setSubject('update_available'); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - foreach ($this->getUsersToNotify() as $uid) { |
|
| 190 | - $notification->setUser($uid); |
|
| 191 | - $this->notificationManager->notify($notification); |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - $this->config->setAppValue('updatenotification', $app, $version); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * @return string[] |
|
| 199 | - */ |
|
| 200 | - protected function getUsersToNotify() { |
|
| 201 | - if ($this->users !== null) { |
|
| 202 | - return $this->users; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - $notifyGroups = json_decode($this->config->getAppValue('updatenotification', 'notify_groups', '["admin"]'), true); |
|
| 206 | - $this->users = []; |
|
| 207 | - foreach ($notifyGroups as $group) { |
|
| 208 | - $groupToNotify = $this->groupManager->get($group); |
|
| 209 | - if ($groupToNotify instanceof IGroup) { |
|
| 210 | - foreach ($groupToNotify->getUsers() as $user) { |
|
| 211 | - $this->users[$user->getUID()] = true; |
|
| 212 | - } |
|
| 213 | - } |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - $this->users = array_keys($this->users); |
|
| 217 | - |
|
| 218 | - return $this->users; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * Delete notifications for old updates |
|
| 223 | - * |
|
| 224 | - * @param string $app |
|
| 225 | - * @param string $version |
|
| 226 | - */ |
|
| 227 | - protected function deleteOutdatedNotifications($app, $version) { |
|
| 228 | - $notification = $this->notificationManager->createNotification(); |
|
| 229 | - $notification->setApp('updatenotification') |
|
| 230 | - ->setObject($app, $version); |
|
| 231 | - $this->notificationManager->markProcessed($notification); |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * @return VersionCheck |
|
| 236 | - */ |
|
| 237 | - protected function createVersionCheck() { |
|
| 238 | - return new VersionCheck( |
|
| 239 | - $this->client, |
|
| 240 | - $this->config |
|
| 241 | - ); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * @return string |
|
| 246 | - */ |
|
| 247 | - protected function getChannel() { |
|
| 248 | - return \OC_Util::getChannel(); |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * @param string $app |
|
| 253 | - * @return string|false |
|
| 254 | - */ |
|
| 255 | - protected function isUpdateAvailable($app) { |
|
| 256 | - return Installer::isUpdateAvailable($app, \OC::$server->getAppFetcher()); |
|
| 257 | - } |
|
| 37 | + protected $connectionNotifications = [3, 7, 14, 30]; |
|
| 38 | + |
|
| 39 | + /** @var IConfig */ |
|
| 40 | + protected $config; |
|
| 41 | + |
|
| 42 | + /** @var IManager */ |
|
| 43 | + protected $notificationManager; |
|
| 44 | + |
|
| 45 | + /** @var IGroupManager */ |
|
| 46 | + protected $groupManager; |
|
| 47 | + |
|
| 48 | + /** @var IAppManager */ |
|
| 49 | + protected $appManager; |
|
| 50 | + |
|
| 51 | + /** @var IClientService */ |
|
| 52 | + protected $client; |
|
| 53 | + |
|
| 54 | + /** @var string[] */ |
|
| 55 | + protected $users; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * NotificationBackgroundJob constructor. |
|
| 59 | + * |
|
| 60 | + * @param IConfig $config |
|
| 61 | + * @param IManager $notificationManager |
|
| 62 | + * @param IGroupManager $groupManager |
|
| 63 | + * @param IAppManager $appManager |
|
| 64 | + * @param IClientService $client |
|
| 65 | + */ |
|
| 66 | + public function __construct(IConfig $config, IManager $notificationManager, IGroupManager $groupManager, IAppManager $appManager, IClientService $client) { |
|
| 67 | + // Run once a day |
|
| 68 | + $this->setInterval(60 * 60 * 24); |
|
| 69 | + |
|
| 70 | + $this->config = $config; |
|
| 71 | + $this->notificationManager = $notificationManager; |
|
| 72 | + $this->groupManager = $groupManager; |
|
| 73 | + $this->appManager = $appManager; |
|
| 74 | + $this->client = $client; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + protected function run($argument) { |
|
| 78 | + $this->checkCoreUpdate(); |
|
| 79 | + $this->checkAppUpdates(); |
|
| 80 | + } |
|
| 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 | + |
|
| 91 | + $updater = $this->createVersionCheck(); |
|
| 92 | + |
|
| 93 | + $status = $updater->check(); |
|
| 94 | + if ($status === false) { |
|
| 95 | + $errors = 1 + (int) $this->config->getAppValue('updatenotification', 'update_check_errors', 0); |
|
| 96 | + $this->config->setAppValue('updatenotification', 'update_check_errors', $errors); |
|
| 97 | + |
|
| 98 | + if (in_array($errors, $this->connectionNotifications, true)) { |
|
| 99 | + $this->sendErrorNotifications($errors); |
|
| 100 | + } |
|
| 101 | + } else if (isset($status['version'])) { |
|
| 102 | + $this->config->setAppValue('updatenotification', 'update_check_errors', 0); |
|
| 103 | + $this->clearErrorNotifications(); |
|
| 104 | + |
|
| 105 | + $this->createNotifications('core', $status['version'], $status['versionstring']); |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Send a message to the admin when the update server could not be reached |
|
| 111 | + * @param int $numDays |
|
| 112 | + */ |
|
| 113 | + protected function sendErrorNotifications($numDays) { |
|
| 114 | + $this->clearErrorNotifications(); |
|
| 115 | + |
|
| 116 | + $notification = $this->notificationManager->createNotification(); |
|
| 117 | + try { |
|
| 118 | + $notification->setApp('updatenotification') |
|
| 119 | + ->setDateTime(new \DateTime()) |
|
| 120 | + ->setObject('updatenotification', 'error') |
|
| 121 | + ->setSubject('connection_error', ['days' => $numDays]); |
|
| 122 | + |
|
| 123 | + foreach ($this->getUsersToNotify() as $uid) { |
|
| 124 | + $notification->setUser($uid); |
|
| 125 | + $this->notificationManager->notify($notification); |
|
| 126 | + } |
|
| 127 | + } catch (\InvalidArgumentException $e) { |
|
| 128 | + return; |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Remove error notifications again |
|
| 134 | + */ |
|
| 135 | + protected function clearErrorNotifications() { |
|
| 136 | + $notification = $this->notificationManager->createNotification(); |
|
| 137 | + try { |
|
| 138 | + $notification->setApp('updatenotification') |
|
| 139 | + ->setSubject('connection_error') |
|
| 140 | + ->setObject('updatenotification', 'error'); |
|
| 141 | + } catch (\InvalidArgumentException $e) { |
|
| 142 | + return; |
|
| 143 | + } |
|
| 144 | + $this->notificationManager->markProcessed($notification); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Check all installed apps for updates |
|
| 149 | + */ |
|
| 150 | + protected function checkAppUpdates() { |
|
| 151 | + $apps = $this->appManager->getInstalledApps(); |
|
| 152 | + foreach ($apps as $app) { |
|
| 153 | + $update = $this->isUpdateAvailable($app); |
|
| 154 | + if ($update !== false) { |
|
| 155 | + $this->createNotifications($app, $update); |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Create notifications for this app version |
|
| 162 | + * |
|
| 163 | + * @param string $app |
|
| 164 | + * @param string $version |
|
| 165 | + * @param string $visibleVersion |
|
| 166 | + */ |
|
| 167 | + protected function createNotifications($app, $version, $visibleVersion = '') { |
|
| 168 | + $lastNotification = $this->config->getAppValue('updatenotification', $app, false); |
|
| 169 | + if ($lastNotification === $version) { |
|
| 170 | + // We already notified about this update |
|
| 171 | + return; |
|
| 172 | + } else if ($lastNotification !== false) { |
|
| 173 | + // Delete old updates |
|
| 174 | + $this->deleteOutdatedNotifications($app, $lastNotification); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + |
|
| 178 | + $notification = $this->notificationManager->createNotification(); |
|
| 179 | + $notification->setApp('updatenotification') |
|
| 180 | + ->setDateTime(new \DateTime()) |
|
| 181 | + ->setObject($app, $version); |
|
| 182 | + |
|
| 183 | + if ($visibleVersion !== '') { |
|
| 184 | + $notification->setSubject('update_available', ['version' => $visibleVersion]); |
|
| 185 | + } else { |
|
| 186 | + $notification->setSubject('update_available'); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + foreach ($this->getUsersToNotify() as $uid) { |
|
| 190 | + $notification->setUser($uid); |
|
| 191 | + $this->notificationManager->notify($notification); |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + $this->config->setAppValue('updatenotification', $app, $version); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * @return string[] |
|
| 199 | + */ |
|
| 200 | + protected function getUsersToNotify() { |
|
| 201 | + if ($this->users !== null) { |
|
| 202 | + return $this->users; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + $notifyGroups = json_decode($this->config->getAppValue('updatenotification', 'notify_groups', '["admin"]'), true); |
|
| 206 | + $this->users = []; |
|
| 207 | + foreach ($notifyGroups as $group) { |
|
| 208 | + $groupToNotify = $this->groupManager->get($group); |
|
| 209 | + if ($groupToNotify instanceof IGroup) { |
|
| 210 | + foreach ($groupToNotify->getUsers() as $user) { |
|
| 211 | + $this->users[$user->getUID()] = true; |
|
| 212 | + } |
|
| 213 | + } |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + $this->users = array_keys($this->users); |
|
| 217 | + |
|
| 218 | + return $this->users; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * Delete notifications for old updates |
|
| 223 | + * |
|
| 224 | + * @param string $app |
|
| 225 | + * @param string $version |
|
| 226 | + */ |
|
| 227 | + protected function deleteOutdatedNotifications($app, $version) { |
|
| 228 | + $notification = $this->notificationManager->createNotification(); |
|
| 229 | + $notification->setApp('updatenotification') |
|
| 230 | + ->setObject($app, $version); |
|
| 231 | + $this->notificationManager->markProcessed($notification); |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * @return VersionCheck |
|
| 236 | + */ |
|
| 237 | + protected function createVersionCheck() { |
|
| 238 | + return new VersionCheck( |
|
| 239 | + $this->client, |
|
| 240 | + $this->config |
|
| 241 | + ); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * @return string |
|
| 246 | + */ |
|
| 247 | + protected function getChannel() { |
|
| 248 | + return \OC_Util::getChannel(); |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * @param string $app |
|
| 253 | + * @return string|false |
|
| 254 | + */ |
|
| 255 | + protected function isUpdateAvailable($app) { |
|
| 256 | + return Installer::isUpdateAvailable($app, \OC::$server->getAppFetcher()); |
|
| 257 | + } |
|
| 258 | 258 | } |
@@ -31,95 +31,95 @@ |
||
| 31 | 31 | |
| 32 | 32 | class VersionCheck { |
| 33 | 33 | |
| 34 | - /** @var IClientService */ |
|
| 35 | - private $clientService; |
|
| 34 | + /** @var IClientService */ |
|
| 35 | + private $clientService; |
|
| 36 | 36 | |
| 37 | - /** @var IConfig */ |
|
| 38 | - private $config; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @param IClientService $clientService |
|
| 42 | - * @param IConfig $config |
|
| 43 | - */ |
|
| 44 | - public function __construct(IClientService $clientService, |
|
| 45 | - IConfig $config) { |
|
| 46 | - $this->clientService = $clientService; |
|
| 47 | - $this->config = $config; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Check if a new version is available |
|
| 53 | - * |
|
| 54 | - * @return array|bool |
|
| 55 | - */ |
|
| 56 | - public function check() { |
|
| 57 | - // Look up the cache - it is invalidated all 30 minutes |
|
| 58 | - if (((int)$this->config->getAppValue('core', 'lastupdatedat') + 1800) > time()) { |
|
| 59 | - return json_decode($this->config->getAppValue('core', 'lastupdateResult'), true); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - $updaterUrl = $this->config->getSystemValue('updater.server.url', 'https://updates.nextcloud.com/updater_server/'); |
|
| 63 | - |
|
| 64 | - $this->config->setAppValue('core', 'lastupdatedat', time()); |
|
| 65 | - |
|
| 66 | - if ($this->config->getAppValue('core', 'installedat', '') === '') { |
|
| 67 | - $this->config->setAppValue('core', 'installedat', microtime(true)); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $version = Util::getVersion(); |
|
| 71 | - $version['installed'] = $this->config->getAppValue('core', 'installedat'); |
|
| 72 | - $version['updated'] = $this->config->getAppValue('core', 'lastupdatedat'); |
|
| 73 | - $version['updatechannel'] = \OC_Util::getChannel(); |
|
| 74 | - $version['edition'] = ''; |
|
| 75 | - $version['build'] = \OC_Util::getBuild(); |
|
| 76 | - $version['php_major'] = PHP_MAJOR_VERSION; |
|
| 77 | - $version['php_minor'] = PHP_MINOR_VERSION; |
|
| 78 | - $version['php_release'] = PHP_RELEASE_VERSION; |
|
| 79 | - $versionString = implode('x', $version); |
|
| 80 | - |
|
| 81 | - //fetch xml data from updater |
|
| 82 | - $url = $updaterUrl . '?version=' . $versionString; |
|
| 83 | - |
|
| 84 | - $tmp = []; |
|
| 85 | - try { |
|
| 86 | - $xml = $this->getUrlContent($url); |
|
| 87 | - } catch (\Exception $e) { |
|
| 88 | - return false; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - if ($xml) { |
|
| 92 | - $loadEntities = libxml_disable_entity_loader(true); |
|
| 93 | - $data = @simplexml_load_string($xml); |
|
| 94 | - libxml_disable_entity_loader($loadEntities); |
|
| 95 | - if ($data !== false) { |
|
| 96 | - $tmp['version'] = (string)$data->version; |
|
| 97 | - $tmp['versionstring'] = (string)$data->versionstring; |
|
| 98 | - $tmp['url'] = (string)$data->url; |
|
| 99 | - $tmp['web'] = (string)$data->web; |
|
| 100 | - $tmp['autoupdater'] = (string)$data->autoupdater; |
|
| 101 | - } else { |
|
| 102 | - libxml_clear_errors(); |
|
| 103 | - } |
|
| 104 | - } else { |
|
| 105 | - $data = []; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - // Cache the result |
|
| 109 | - $this->config->setAppValue('core', 'lastupdateResult', json_encode($data)); |
|
| 110 | - return $tmp; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * @codeCoverageIgnore |
|
| 115 | - * @param string $url |
|
| 116 | - * @return resource|string |
|
| 117 | - * @throws \Exception |
|
| 118 | - */ |
|
| 119 | - protected function getUrlContent($url) { |
|
| 120 | - $client = $this->clientService->newClient(); |
|
| 121 | - $response = $client->get($url); |
|
| 122 | - return $response->getBody(); |
|
| 123 | - } |
|
| 37 | + /** @var IConfig */ |
|
| 38 | + private $config; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @param IClientService $clientService |
|
| 42 | + * @param IConfig $config |
|
| 43 | + */ |
|
| 44 | + public function __construct(IClientService $clientService, |
|
| 45 | + IConfig $config) { |
|
| 46 | + $this->clientService = $clientService; |
|
| 47 | + $this->config = $config; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Check if a new version is available |
|
| 53 | + * |
|
| 54 | + * @return array|bool |
|
| 55 | + */ |
|
| 56 | + public function check() { |
|
| 57 | + // Look up the cache - it is invalidated all 30 minutes |
|
| 58 | + if (((int)$this->config->getAppValue('core', 'lastupdatedat') + 1800) > time()) { |
|
| 59 | + return json_decode($this->config->getAppValue('core', 'lastupdateResult'), true); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + $updaterUrl = $this->config->getSystemValue('updater.server.url', 'https://updates.nextcloud.com/updater_server/'); |
|
| 63 | + |
|
| 64 | + $this->config->setAppValue('core', 'lastupdatedat', time()); |
|
| 65 | + |
|
| 66 | + if ($this->config->getAppValue('core', 'installedat', '') === '') { |
|
| 67 | + $this->config->setAppValue('core', 'installedat', microtime(true)); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $version = Util::getVersion(); |
|
| 71 | + $version['installed'] = $this->config->getAppValue('core', 'installedat'); |
|
| 72 | + $version['updated'] = $this->config->getAppValue('core', 'lastupdatedat'); |
|
| 73 | + $version['updatechannel'] = \OC_Util::getChannel(); |
|
| 74 | + $version['edition'] = ''; |
|
| 75 | + $version['build'] = \OC_Util::getBuild(); |
|
| 76 | + $version['php_major'] = PHP_MAJOR_VERSION; |
|
| 77 | + $version['php_minor'] = PHP_MINOR_VERSION; |
|
| 78 | + $version['php_release'] = PHP_RELEASE_VERSION; |
|
| 79 | + $versionString = implode('x', $version); |
|
| 80 | + |
|
| 81 | + //fetch xml data from updater |
|
| 82 | + $url = $updaterUrl . '?version=' . $versionString; |
|
| 83 | + |
|
| 84 | + $tmp = []; |
|
| 85 | + try { |
|
| 86 | + $xml = $this->getUrlContent($url); |
|
| 87 | + } catch (\Exception $e) { |
|
| 88 | + return false; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + if ($xml) { |
|
| 92 | + $loadEntities = libxml_disable_entity_loader(true); |
|
| 93 | + $data = @simplexml_load_string($xml); |
|
| 94 | + libxml_disable_entity_loader($loadEntities); |
|
| 95 | + if ($data !== false) { |
|
| 96 | + $tmp['version'] = (string)$data->version; |
|
| 97 | + $tmp['versionstring'] = (string)$data->versionstring; |
|
| 98 | + $tmp['url'] = (string)$data->url; |
|
| 99 | + $tmp['web'] = (string)$data->web; |
|
| 100 | + $tmp['autoupdater'] = (string)$data->autoupdater; |
|
| 101 | + } else { |
|
| 102 | + libxml_clear_errors(); |
|
| 103 | + } |
|
| 104 | + } else { |
|
| 105 | + $data = []; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + // Cache the result |
|
| 109 | + $this->config->setAppValue('core', 'lastupdateResult', json_encode($data)); |
|
| 110 | + return $tmp; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * @codeCoverageIgnore |
|
| 115 | + * @param string $url |
|
| 116 | + * @return resource|string |
|
| 117 | + * @throws \Exception |
|
| 118 | + */ |
|
| 119 | + protected function getUrlContent($url) { |
|
| 120 | + $client = $this->clientService->newClient(); |
|
| 121 | + $response = $client->get($url); |
|
| 122 | + return $response->getBody(); |
|
| 123 | + } |
|
| 124 | 124 | } |
| 125 | 125 | |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | */ |
| 56 | 56 | public function check() { |
| 57 | 57 | // Look up the cache - it is invalidated all 30 minutes |
| 58 | - if (((int)$this->config->getAppValue('core', 'lastupdatedat') + 1800) > time()) { |
|
| 58 | + if (((int) $this->config->getAppValue('core', 'lastupdatedat') + 1800) > time()) { |
|
| 59 | 59 | return json_decode($this->config->getAppValue('core', 'lastupdateResult'), true); |
| 60 | 60 | } |
| 61 | 61 | |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | $versionString = implode('x', $version); |
| 80 | 80 | |
| 81 | 81 | //fetch xml data from updater |
| 82 | - $url = $updaterUrl . '?version=' . $versionString; |
|
| 82 | + $url = $updaterUrl.'?version='.$versionString; |
|
| 83 | 83 | |
| 84 | 84 | $tmp = []; |
| 85 | 85 | try { |
@@ -93,11 +93,11 @@ discard block |
||
| 93 | 93 | $data = @simplexml_load_string($xml); |
| 94 | 94 | libxml_disable_entity_loader($loadEntities); |
| 95 | 95 | if ($data !== false) { |
| 96 | - $tmp['version'] = (string)$data->version; |
|
| 97 | - $tmp['versionstring'] = (string)$data->versionstring; |
|
| 98 | - $tmp['url'] = (string)$data->url; |
|
| 99 | - $tmp['web'] = (string)$data->web; |
|
| 100 | - $tmp['autoupdater'] = (string)$data->autoupdater; |
|
| 96 | + $tmp['version'] = (string) $data->version; |
|
| 97 | + $tmp['versionstring'] = (string) $data->versionstring; |
|
| 98 | + $tmp['url'] = (string) $data->url; |
|
| 99 | + $tmp['web'] = (string) $data->web; |
|
| 100 | + $tmp['autoupdater'] = (string) $data->autoupdater; |
|
| 101 | 101 | } else { |
| 102 | 102 | libxml_clear_errors(); |
| 103 | 103 | } |