Completed
Push — master ( 5bc8c9...07e638 )
by Morris
52:34 queued 36:57
created

Instance::getStatus()   C

Complexity

Conditions 7
Paths 11

Size

Total Lines 32
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 25
nc 11
nop 0
dl 0
loc 32
rs 6.7272
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Robin Appelman <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OC\Remote;
23
24
use OC\Remote\Api\NotFoundException;
25
use OCP\Http\Client\IClientService;
26
use OCP\ICache;
27
use OCP\Remote\IInstance;
28
29
/**
30
 * Provides some basic info about a remote Nextcloud instance
31
 */
32
class Instance implements IInstance {
33
	/** @var string */
34
	private $url;
35
36
	/** @var ICache */
37
	private $cache;
38
39
	/** @var IClientService */
40
	private $clientService;
41
42
	/** @var array|null */
43
	private $status;
44
45
	/**
46
	 * @param string $url
47
	 * @param ICache $cache
48
	 * @param IClientService $clientService
49
	 */
50
	public function __construct($url, ICache $cache, IClientService $clientService) {
51
		$url = str_replace('https://', '', $url);
52
		$this->url = str_replace('http://', '', $url);
53
		$this->cache = $cache;
54
		$this->clientService = $clientService;
55
	}
56
57
	/**
58
	 * @return string The url of the remote server without protocol
59
	 */
60
	public function getUrl() {
61
		return $this->url;
62
	}
63
64
	/**
65
	 * @return string The of of the remote server with protocol
66
	 */
67
	public function getFullUrl() {
68
		return $this->getProtocol() . '://' . $this->getUrl();
69
	}
70
71
	/**
72
	 * @return string The full version string in '13.1.2.3' format
73
	 */
74
	public function getVersion() {
75
		$status = $this->getStatus();
76
		return $status['version'];
77
	}
78
79
	/**
80
	 * @return string 'http' or 'https'
81
	 */
82
	public function getProtocol() {
83
		$status = $this->getStatus();
84
		return $status['protocol'];
85
	}
86
87
	/**
88
	 * Check that the remote server is installed and not in maintenance mode
89
	 *
90
	 * @return bool
91
	 */
92
	public function isActive() {
93
		$status = $this->getStatus();
94
		return $status['installed'] && !$status['maintenance'];
95
	}
96
97
	/**
98
	 * @return array
99
	 * @throws NotFoundException
100
	 * @throws \Exception
101
	 */
102
	private function getStatus() {
103
		if ($this->status) {
104
			return $this->status;
105
		}
106
		$key = 'remote/' . $this->url . '/status';
107
		$httpsKey = 'remote/' . $this->url . '/https';
108
		$status = $this->cache->get($key);
109
		if (!$status) {
110
			$response = $this->downloadStatus('https://' . $this->getUrl() . '/status.php');
111
			$protocol = 'https';
112
			if (!$response) {
113
				if ($status = $this->cache->get($httpsKey)) {
0 ignored issues
show
Unused Code introduced by
$status is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
114
					throw new \Exception('refusing to connect to remote instance(' . $this->url . ') over http that was previously accessible over https');
115
				}
116
				$response = $this->downloadStatus('http://' . $this->getUrl() . '/status.php');
117
				$protocol = 'http';
118
			} else {
119
				$this->cache->set($httpsKey, true, 60 * 60 * 24 * 365);
120
			}
121
			$status = json_decode($response, true);
122
			if ($status) {
123
				$status['protocol'] = $protocol;
124
			}
125
			if ($status) {
126
				$this->cache->set($key, $status, 5 * 60);
127
				$this->status = $status;
128
			} else {
129
				throw new NotFoundException('Remote server not found at address ' . $this->url);
130
			}
131
		}
132
		return $status;
133
	}
134
135
	/**
136
	 * @param string $url
137
	 * @return bool|string
138
	 */
139
	private function downloadStatus($url) {
140
		try {
141
			$request = $this->clientService->newClient()->get($url);
142
			return $request->getBody();
143
		} catch (\Exception $e) {
144
			return false;
145
		}
146
	}
147
}
148