Fetcher.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Steffen Lindner <mail@steffen-lindner.de>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OC\App\AppStore\Fetcher;
  28. use OC\Files\AppData\Factory;
  29. use GuzzleHttp\Exception\ConnectException;
  30. use OCP\AppFramework\Http;
  31. use OCP\AppFramework\Utility\ITimeFactory;
  32. use OCP\Files\IAppData;
  33. use OCP\Files\NotFoundException;
  34. use OCP\Http\Client\IClientService;
  35. use OCP\IConfig;
  36. use OCP\ILogger;
  37. use OCP\Util;
  38. abstract class Fetcher {
  39. const INVALIDATE_AFTER_SECONDS = 300;
  40. /** @var IAppData */
  41. protected $appData;
  42. /** @var IClientService */
  43. protected $clientService;
  44. /** @var ITimeFactory */
  45. protected $timeFactory;
  46. /** @var IConfig */
  47. protected $config;
  48. /** @var Ilogger */
  49. protected $logger;
  50. /** @var string */
  51. protected $fileName;
  52. /** @var string */
  53. protected $endpointUrl;
  54. /** @var string */
  55. protected $version;
  56. /**
  57. * @param Factory $appDataFactory
  58. * @param IClientService $clientService
  59. * @param ITimeFactory $timeFactory
  60. * @param IConfig $config
  61. * @param ILogger $logger
  62. */
  63. public function __construct(Factory $appDataFactory,
  64. IClientService $clientService,
  65. ITimeFactory $timeFactory,
  66. IConfig $config,
  67. ILogger $logger) {
  68. $this->appData = $appDataFactory->get('appstore');
  69. $this->clientService = $clientService;
  70. $this->timeFactory = $timeFactory;
  71. $this->config = $config;
  72. $this->logger = $logger;
  73. }
  74. /**
  75. * Fetches the response from the server
  76. *
  77. * @param string $ETag
  78. * @param string $content
  79. *
  80. * @return array
  81. */
  82. protected function fetch($ETag, $content) {
  83. $appstoreenabled = $this->config->getSystemValue('appstoreenabled', true);
  84. if (!$appstoreenabled) {
  85. return [];
  86. }
  87. $options = [
  88. 'timeout' => 10,
  89. ];
  90. if ($ETag !== '') {
  91. $options['headers'] = [
  92. 'If-None-Match' => $ETag,
  93. ];
  94. }
  95. $client = $this->clientService->newClient();
  96. $response = $client->get($this->endpointUrl, $options);
  97. $responseJson = [];
  98. if ($response->getStatusCode() === Http::STATUS_NOT_MODIFIED) {
  99. $responseJson['data'] = json_decode($content, true);
  100. } else {
  101. $responseJson['data'] = json_decode($response->getBody(), true);
  102. $ETag = $response->getHeader('ETag');
  103. }
  104. $responseJson['timestamp'] = $this->timeFactory->getTime();
  105. $responseJson['ncversion'] = $this->getVersion();
  106. if ($ETag !== '') {
  107. $responseJson['ETag'] = $ETag;
  108. }
  109. return $responseJson;
  110. }
  111. /**
  112. * Returns the array with the categories on the appstore server
  113. *
  114. * @return array
  115. */
  116. public function get() {
  117. $appstoreenabled = $this->config->getSystemValue('appstoreenabled', true);
  118. $internetavailable = $this->config->getSystemValue('has_internet_connection', true);
  119. if (!$appstoreenabled || !$internetavailable) {
  120. return [];
  121. }
  122. $rootFolder = $this->appData->getFolder('/');
  123. $ETag = '';
  124. $content = '';
  125. try {
  126. // File does already exists
  127. $file = $rootFolder->getFile($this->fileName);
  128. $jsonBlob = json_decode($file->getContent(), true);
  129. if (is_array($jsonBlob)) {
  130. // No caching when the version has been updated
  131. if (isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->getVersion()) {
  132. // If the timestamp is older than 300 seconds request the files new
  133. if ((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS)) {
  134. return $jsonBlob['data'];
  135. }
  136. if (isset($jsonBlob['ETag'])) {
  137. $ETag = $jsonBlob['ETag'];
  138. $content = json_encode($jsonBlob['data']);
  139. }
  140. }
  141. }
  142. } catch (NotFoundException $e) {
  143. // File does not already exists
  144. $file = $rootFolder->newFile($this->fileName);
  145. }
  146. // Refresh the file content
  147. try {
  148. $responseJson = $this->fetch($ETag, $content);
  149. $file->putContent(json_encode($responseJson));
  150. return json_decode($file->getContent(), true)['data'];
  151. } catch (ConnectException $e) {
  152. $this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::INFO, 'message' => 'Could not connect to appstore']);
  153. return [];
  154. } catch (\Exception $e) {
  155. $this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::INFO]);
  156. return [];
  157. }
  158. }
  159. /**
  160. * Get the currently Nextcloud version
  161. * @return string
  162. */
  163. protected function getVersion() {
  164. if ($this->version === null) {
  165. $this->version = $this->config->getSystemValue('version', '0.0.0');
  166. }
  167. return $this->version;
  168. }
  169. /**
  170. * Set the current Nextcloud version
  171. * @param string $version
  172. */
  173. public function setVersion(string $version) {
  174. $this->version = $version;
  175. }
  176. }