1 | <?php |
||
31 | class Fetcher { |
||
32 | |||
33 | const DEFAULT_BASE_URL = 'https://updates.owncloud.com/server/'; |
||
34 | |||
35 | /** |
||
36 | * @var Locator $locator |
||
37 | */ |
||
38 | protected $locator; |
||
39 | |||
40 | /** |
||
41 | * @var ConfigReader $configReader |
||
42 | */ |
||
43 | protected $configReader; |
||
44 | |||
45 | /** |
||
46 | * @var Client $httpClient |
||
47 | */ |
||
48 | protected $httpClient; |
||
49 | protected $requiredFeedEntries = [ |
||
50 | 'version', |
||
51 | 'versionstring', |
||
52 | 'url' |
||
53 | ]; |
||
54 | |||
55 | /** |
||
56 | * Constructor |
||
57 | * |
||
58 | * @param Client $httpClient |
||
59 | * @param Locator $locator |
||
60 | * @param ConfigReader $configReader |
||
61 | */ |
||
62 | 3 | public function __construct(Client $httpClient, Locator $locator, ConfigReader $configReader){ |
|
67 | |||
68 | /** |
||
69 | * Download new ownCloud package |
||
70 | * @param Feed $feed |
||
71 | * @param Callable $onProgress |
||
72 | * @throws \Exception |
||
73 | * @throws \UnexpectedValueException |
||
74 | */ |
||
75 | public function getOwncloud(Feed $feed, callable $onProgress){ |
||
76 | if ($feed->isValid()){ |
||
77 | $downloadPath = $this->getBaseDownloadPath($feed); |
||
78 | if (!is_writable(dirname($downloadPath))){ |
||
79 | throw new \Exception(dirname($downloadPath) . ' is not writable.'); |
||
80 | } |
||
81 | $url = $feed->getUrl(); |
||
82 | $request = $this->httpClient->createRequest( |
||
83 | 'GET', |
||
84 | $url, |
||
85 | [ |
||
86 | 'save_to' => $downloadPath, |
||
87 | 'timeout' => 600 |
||
88 | ] |
||
89 | ); |
||
90 | $request->getEmitter()->on('progress', $onProgress); |
||
91 | $response = $this->httpClient->send($request); |
||
92 | $this->validateResponse($response); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Produce a local path to save the package to |
||
98 | * @param Feed $feed |
||
99 | * @return string |
||
100 | */ |
||
101 | public function getBaseDownloadPath(Feed $feed){ |
||
105 | |||
106 | /** |
||
107 | * Get md5 sum for the package |
||
108 | * @param Feed $feed |
||
109 | * @return string |
||
110 | */ |
||
111 | public function getMd5(Feed $feed){ |
||
117 | |||
118 | /** |
||
119 | * Read update feed for new releases |
||
120 | * @return Feed |
||
121 | */ |
||
122 | 3 | public function getFeed(){ |
|
123 | 3 | $url = $this->getFeedUrl(); |
|
124 | 3 | $xml = $this->download($url); |
|
125 | 3 | $tmp = []; |
|
126 | 3 | if ($xml){ |
|
127 | 2 | $loadEntities = libxml_disable_entity_loader(true); |
|
128 | 2 | $data = @simplexml_load_string($xml); |
|
129 | 2 | libxml_disable_entity_loader($loadEntities); |
|
130 | 2 | if ($data !== false){ |
|
131 | 1 | $tmp['version'] = (string) $data->version; |
|
132 | 1 | $tmp['versionstring'] = (string) $data->versionstring; |
|
133 | 1 | $tmp['url'] = (string) $data->url; |
|
134 | 1 | $tmp['web'] = (string) $data->web; |
|
135 | } |
||
136 | } |
||
137 | |||
138 | 3 | return new Feed($tmp); |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * @return mixed|string |
||
143 | */ |
||
144 | 3 | public function getUpdateChannel(){ |
|
152 | |||
153 | /** |
||
154 | * Produce complete feed URL |
||
155 | * @return string |
||
156 | */ |
||
157 | 3 | protected function getFeedUrl(){ |
|
158 | 3 | $currentVersion = $this->configReader->getByPath('system.version'); |
|
159 | 3 | $version = explode('.', $currentVersion); |
|
160 | 3 | $version['installed'] = $this->configReader->getByPath('apps.core.installedat'); |
|
161 | 3 | $version['updated'] = $this->configReader->getByPath('apps.core.lastupdatedat'); |
|
162 | 3 | $version['updatechannel'] = $this->getUpdateChannel(); |
|
163 | 3 | $version['edition'] = $this->configReader->getEdition(); |
|
164 | 3 | $version['build'] = $this->locator->getBuild(); |
|
165 | |||
166 | // Read updater server URL from config |
||
167 | 3 | $updaterServerUrl = $this->configReader->get(['system', 'updater.server.url']); |
|
168 | 3 | if ((bool) $updaterServerUrl === false){ |
|
169 | 3 | $updaterServerUrl = self::DEFAULT_BASE_URL; |
|
170 | } |
||
171 | |||
172 | 3 | $url = $updaterServerUrl . '?version=' . implode('x', $version); |
|
173 | 3 | return $url; |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * Get URL content |
||
178 | * @param string $url |
||
179 | * @return string |
||
180 | * @throws \UnexpectedValueException |
||
181 | */ |
||
182 | 3 | protected function download($url){ |
|
187 | |||
188 | /** |
||
189 | * Check if request was successful |
||
190 | * @param \GuzzleHttp\Message\ResponseInterface $response |
||
191 | * @throws \UnexpectedValueException |
||
192 | */ |
||
193 | 3 | protected function validateResponse($response){ |
|
203 | |||
204 | } |
||
205 |