Fetcher.php 7.2 KB

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