Пример #1
0
 protected function updateForUser($userId)
 {
     $userSettings = $this->getUserSettings($userId);
     $localUris = $userSettings->getLocalURIs();
     $xmlrpc = new XmlRpc($userSettings->getUsername(), $userSettings->getPassword());
     $start = (new \DateTime())->sub($userSettings->getSyncPeriodAsInterval());
     $end = new \DateTime();
     $this->logger->debug("Fetching history from %s to %s for user '%s' for localUris: %s", $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s'), $userId, count($localUris) === 0 ? 'all' : implode($localUris, ', '));
     try {
         $history = $xmlrpc->historyGetByDate($start, $end, count($localUris) === 0 ? null : $localUris);
     } catch (XmlRpcException $e) {
         $this->logger->error('An XMLRPC exception occurred: %s', $e->getMessage());
         throw $e;
     } catch (HttpException $e) {
         $this->loger->error('Remote backend failed with HTTP status code %d', $e->getCode());
         throw $e;
     }
     $this->logger->debug("Fetched %d entries for user '%s'", count($history), $userId);
     $count = 0;
     foreach ($history as $entry) {
         $count += $this->insert($userId, $entry, $userSettings);
     }
     $this->logger->debug("Inserted %d entries for user '%s'", $count, $userId);
     $userSettings->setLastUpdated();
 }
Пример #2
0
 protected function getDataToImport(UserSettings $userSettings)
 {
     $xmlrpc = new XmlRpc($userSettings->getUsername(), $userSettings->getPassword());
     $result = [];
     try {
         $listEntry = $xmlrpc->phonebookListGet();
         $entryIds = array_map(function (PhonebookListEntry $entry) {
             return $entry->getEntryId();
         }, $listEntry);
         $entries = $xmlrpc->phonebookEntryGet($entryIds);
     } catch (XmlRpcException $e) {
         $this->logError('An XMLRPC exception occurred: %s', $e->getMessage());
         return $result;
     } catch (HttpException $e) {
         $this->logError('Remote backend failed with HTTP status code %d', $e->getCode());
         return $result;
     }
     foreach ($entries as $entry) {
         /** @var PhonebookEntry $entry */
         try {
             $numbers = $entry->getPhoneNumbers($userSettings->getDefaultCountryCode());
             foreach ($numbers as $number) {
                 $result[] = new Item($entry->getEntryId(), $entry->getFullName(), $number);
             }
         } catch (NumberParseException $e) {
             $this->logError('Error parsing phone number for entry #%s (code %d): %s', $entry->getEntryId(), $e->getCode(), $e->getMessage());
         } catch (ParseException $e) {
             $this->logError('Error parsing VCard for entry #%s: %s', $entry->getEntryId(), $e->getMessage());
         }
     }
     return $result;
 }
 /**
  * @NoAdminRequired
  * @NoCSRFRequired
  *
  * @param string $username
  * @param string $password
  * @return JsonResponse
  */
 public function testSettings($username, $password)
 {
     $xmlrpc = new XmlRpc($username, $password);
     try {
         $serverInfo = $xmlrpc->serverInfo();
     } catch (HttpException $exception) {
         if ($exception->getCode() === Http::STATUS_UNAUTHORIZED) {
             $this->logger->info('Settings test failed (unauthorized)', ['app' => $this->appName]);
             return new JSONResponse(['success' => false, 'error' => 'Authorization failed'], Http::STATUS_UNAUTHORIZED);
         } else {
             $this->logger->warn(sprintf('Settings test failed (%d: %s)', $exception->getCode(), $exception->getMessage()), ['app' => $this->appName]);
             return new JSONResponse(['success' => false, 'error' => $exception->getMessage()], Http::STATUS_BAD_GATEWAY);
         }
     } catch (\Exception $exception) {
         $this->logger->error(sprintf('Settings test failed with exception (%s)', $exception->getMessage()), ['app' => $this->appName]);
         return new JSONResponse(['success' => false, 'error' => $exception->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
     }
     $this->logger->info('Settings test successful', ['app' => $this->appName]);
     return new JSONResponse(['success' => true, 'serverInfo' => $serverInfo->toJSON()]);
 }