appName = $AppName; $this->urlGenerator = $urlGenerator; $this->logger = \OCP\Server::get(LoggerInterface::class); $this->config = $config; $this->trans = $trans; $this->crypt = $crypt; $this->groupManager = $groupManager; $this->setInterval($this->config->getEditorsCheckInterval()); $this->setTimeSensitivity(IJob::TIME_SENSITIVE); $mailer = \OCP\Server::get(IMailer::class); $userManager = \OCP\Server::get(IUserManager::class); $this->emailManager = new EmailManager($AppName, $trans, $this->logger, $mailer, $userManager, $urlGenerator); } /** * Makes the background check * * @param array $argument unused argument */ protected function run($argument) { if (empty($this->config->getDocumentServerUrl())) { $this->logger->debug("Settings are empty"); return; } if (!$this->config->settingsAreSuccessful()) { $this->logger->debug("Settings are not correct"); return; } $fileUrl = $this->urlGenerator->linkToRouteAbsolute($this->appName . ".callback.emptyfile"); if (!$this->config->useDemo() && !empty($this->config->getStorageUrl())) { $fileUrl = str_replace($this->urlGenerator->getAbsoluteURL("/"), $this->config->getStorageUrl(), $fileUrl); } $host = parse_url($fileUrl)["host"]; if ($host === "localhost" || $host === "127.0.0.1") { $this->logger->debug("Localhost is not alowed for cron editors availability check. Please provide server address for internal requests from ONLYOFFICE Docs"); return; } $this->logger->debug("ONLYOFFICE check started by cron"); $documentService = new DocumentService($this->trans, $this->config); list($error, $version) = $documentService->checkDocServiceUrl($this->urlGenerator, $this->crypt); if (!empty($error)) { $this->logger->info("ONLYOFFICE server is not available"); $this->config->setSettingsError($error); $this->notifyAdmins(); } else { $this->logger->debug("ONLYOFFICE server availability check is finished successfully"); } } /** * Get the list of users to notify * * @return string[] */ private function getUsersToNotify() { $notifyGroups = ["admin"]; $notifyUsers = []; foreach ($notifyGroups as $notifyGroup) { $group = $this->groupManager->get($notifyGroup); if ($group === null || !($group instanceof IGroup)) { continue; } $users = $group->getUsers(); foreach ($users as $user) { $notifyUsers[] = $user->getUID(); } } return $notifyUsers; } /** * Send notification to admins * @return void */ private function notifyAdmins() { $notificationManager = \OC::$server->getNotificationManager(); $notification = $notificationManager->createNotification(); $notification->setApp($this->appName) ->setDateTime(new \DateTime()) ->setObject("editorsCheck", $this->trans->t("ONLYOFFICE server is not available")) ->setSubject("editorscheck_info"); foreach ($this->getUsersToNotify() as $uid) { $notification->setUser($uid); $notificationManager->notify($notification); if ($this->config->getEmailNotifications()) { $this->emailManager->notifyEditorsCheckEmail($uid); } } } }