Completed
Push — master ( 4c59ee...b4c5a5 )
by Christoph
08:38
created

VersionCheck::check()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 50
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 50
rs 8.6315
cc 6
eloc 33
nc 13
nop 1
1
<?php
2
/**
3
 * @author Arthur Schiwon <[email protected]>
4
 * @author Bart Visscher <[email protected]>
5
 * @author Björn Schießle <[email protected]>
6
 * @author Frank Karlitschek <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Lukas Reschke <[email protected]>
9
 * @author Morris Jobke <[email protected]>
10
 * @author Robin Appelman <[email protected]>
11
 * @author Robin McCorkell <[email protected]>
12
 * @author Steffen Lindner <[email protected]>
13
 * @author Thomas Müller <[email protected]>
14
 * @author Victor Dubiniuk <[email protected]>
15
 * @author Vincent Petry <[email protected]>
16
 *
17
 * @copyright Copyright (c) 2016, ownCloud, Inc.
18
 * @license AGPL-3.0
19
 *
20
 * This code is free software: you can redistribute it and/or modify
21
 * it under the terms of the GNU Affero General Public License, version 3,
22
 * as published by the Free Software Foundation.
23
 *
24
 * This program is distributed in the hope that it will be useful,
25
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
 * GNU Affero General Public License for more details.
28
 *
29
 * You should have received a copy of the GNU Affero General Public License, version 3,
30
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
31
 *
32
 */
33
34
namespace OC\Updater;
35
36
use OC_Util;
37
use OCP\Http\Client\IClientService;
38
use OCP\IConfig;
39
use OC\Setup;
40
use OCP\Util;
41
42
class VersionCheck {
43
44
	/** @var IClientService */
45
	private $clientService;
46
	
47
	/** @var IConfig */
48
	private $config;
49
50
	/**
51
	 * @param IClientService $clientService
52
	 * @param IConfig $config
53
	 */
54
	public function __construct(IClientService $clientService,
55
								IConfig $config) {
56
		$this->clientService = $clientService;
57
		$this->config = $config;
58
	}
59
60
61
	/**
62
	 * Check if a new version is available
63
	 *
64
	 * @param string $updaterUrl the url to check, i.e. 'http://apps.owncloud.com/updater.php'
65
	 * @return array|bool
66
	 */
67
	public function check($updaterUrl = null) {
68
69
		// Look up the cache - it is invalidated all 30 minutes
70
		if (((int)$this->config->getAppValue('core', 'lastupdatedat') + 1800) > time()) {
71
			return json_decode($this->config->getAppValue('core', 'lastupdateResult'), true);
72
		}
73
74
		if (is_null($updaterUrl)) {
75
			$updaterUrl = 'https://updates.owncloud.com/server/';
76
		}
77
78
		$this->config->setAppValue('core', 'lastupdatedat', time());
79
80
		if ($this->config->getAppValue('core', 'installedat', '') === '') {
81
			$this->config->setAppValue('core', 'installedat', microtime(true));
82
		}
83
84
		$version = Util::getVersion();
85
		$version['installed'] = $this->config->getAppValue('core', 'installedat');
86
		$version['updated'] = $this->config->getAppValue('core', 'lastupdatedat');
87
		$version['updatechannel'] = \OC_Util::getChannel();
88
		$version['edition'] = \OC_Util::getEditionString();
89
		$version['build'] = \OC_Util::getBuild();
90
		$versionString = implode('x', $version);
91
92
		//fetch xml data from updater
93
		$url = $updaterUrl . '?version=' . $versionString;
94
95
		$tmp = [];
96
		$xml = $this->getUrlContent($url);
97
		if ($xml) {
98
			$loadEntities = libxml_disable_entity_loader(true);
99
			$data = @simplexml_load_string($xml);
100
			libxml_disable_entity_loader($loadEntities);
101
			if ($data !== false) {
102
				$tmp['version'] = (string)$data->version;
103
				$tmp['versionstring'] = (string)$data->versionstring;
104
				$tmp['url'] = (string)$data->url;
105
				$tmp['web'] = (string)$data->web;
106
			} else {
107
				libxml_clear_errors();
108
			}
109
		} else {
110
			$data = [];
111
		}
112
113
		// Cache the result
114
		$this->config->setAppValue('core', 'lastupdateResult', json_encode($data));
115
		return $tmp;
116
	}
117
118
	/**
119
	 * @codeCoverageIgnore
120
	 * @param string $url
121
	 * @return bool|resource|string
122
	 */
123 View Code Duplication
	protected function getUrlContent($url) {
124
		try {
125
			$client = $this->clientService->newClient();
126
			$response = $client->get($url);
127
			return $response->getBody();
128
		} catch (\Exception $e) {
129
			return false;
130
		}
131
	}
132
}
133
134