Fetcher.php 6.0 KB

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