Completed
Pull Request — master (#522)
by Victor
01:50
created

Locator::getRootDirItemsFromSignature()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 9
cp 0
rs 9.8333
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 12
1
<?php
2
/**
3
 * @author Victor Dubiniuk <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2015, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
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, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace Owncloud\Updater\Utils;
23
24
use \Owncloud\Updater\Console\Application;
25
26
/**
27
 * Class Locator
28
 *
29
 * @package Owncloud\Updater\Utils
30
 */
31
class Locator {
32
33
	/**
34
	 * absolute path to ownCloud root
35
	 * @var string
36
	 */
37
	protected $ownCloudRootPath;
38
39
	/**
40
	 * absolute path to updater root
41
	 * @var string
42
	 */
43
	protected $updaterRootPath;
44
45
	/**
46
	 *
47
	 * @param string $baseDir
48
	 */
49
	public function __construct($baseDir){
50
		$this->updaterRootPath = $baseDir;
51
		$this->ownCloudRootPath = dirname($baseDir);
52
	}
53
54
	/**
55
	 * @return string
56
	 */
57
	public function getOwnCloudRootPath(){
58
		return $this->ownCloudRootPath;
59
	}
60
61
	/**
62
	 * expected items in the core
63
	 * @return string[]
64
	 */
65
	public function getRootDirContent(){
66
		return [
67
			"3rdparty",
68
			"config",
69
			"core",
70
			"l10n",
71
			"lib",
72
			"ocm-provider",
73
			"ocs",
74
			"ocs-provider",
75
			"resources",
76
			"settings",
77
			".htaccess",
78
			".mailmap",
79
			".tag",
80
			".user.ini",
81
			"AUTHORS",
82
			"CHANGELOG.md",
83
			"console.php",
84
			"COPYING",
85
			"cron.php",
86
			"db_structure.xml",
87
			"index.html",
88
			"index.php",
89
			"indie.json",
90
			"occ",
91
			"public.php",
92
			"remote.php",
93
			"robots.txt",
94
			"status.php",
95
			"version.php",
96
		];
97
	}
98
99
	/**
100
	 * @return array
101
	 */
102
	public function getUpdaterContent(){
103
		return [
104
			'app',
105
			'application.php',
106
			'box.json',
107
			'composer.json',
108
			'composer.lock',
109
			'CONTRIBUTING.md',
110
			'COPYING-AGPL',
111
			'index.php',
112
			'pub',
113
			'src',
114
			'vendor',
115
			'README.md',
116
			'.travis.yml',
117
			'.scrutinizer.yml',
118
			'nbproject',
119
		];
120
	}
121
122
	/**
123
	 * Get all files and directories in the OC root dir using signature.json as a source
124
	 *
125
	 * @param string $basePath
126
	 *
127
	 * @return array
128
	 */
129
	public function getRootDirItemsFromSignature($basePath) {
130
		$signature = $this->getSignature($basePath);
131
		$items = [];
132
		if (isset($signature['hashes'])) {
133
			$allItems = array_keys($signature['hashes']);
134
			foreach ($allItems as $k => $v) {
135
				// Get the part of the string before the first slash or entire string if there is no slash
136
				$allItems[$k] = strtok($v, '/');
137
			}
138
			$items = array_unique($allItems);
139
		}
140
		return $items;
141
	}
142
143
	/**
144
	 * Absolute path to core root dir content
145
	 * @return array
146
	 */
147
	public function getRootDirItems(){
148
		$items = $this->getRootDirContent();
149
		$items = array_map(
150
			function($item){ return $this->ownCloudRootPath . "/" . $item;	},
151
			$items
152
		);
153
		return $items;
154
	}
155
156
	/**
157
	 * Absolute path
158
	 * @return string
159
	 * @throws \Exception
160
	 */
161
	public function getDataDir(){
162
		$container = Application::$container;
163
		if (isset($container['utils.configReader']) && $container['utils.configReader']->getIsLoaded()){
164
			return $container['utils.configReader']->getByPath('system.datadirectory');
165
		}
166
167
		// Fallback case
168
		include $this->getPathToConfigFile();
169
		if (isset($CONFIG['datadirectory'])){
0 ignored issues
show
Bug introduced by
The variable $CONFIG seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
170
			return $CONFIG['datadirectory'];
171
		}
172
173
		// Something went wrong
174
		throw new \Exception('Unable to detect datadirectory');
175
	}
176
177
	/**
178
	 * Absolute path to updater root dir
179
	 * @return string
180
	 */
181
	public function getUpdaterBaseDir(){
182
		return $this->getDataDir() . '/updater-data';
183
	}
184
185
	/**
186
	 * Absolute path to create a core and apps backups
187
	 * @return string
188
	 */
189
	public function getCheckpointDir(){
190
		return $this->getUpdaterBaseDir() . '/checkpoint';
191
	}
192
193
	/**
194
	 * Absolute path to store downloaded packages
195
	 * @return string
196
	 */
197
	public function getDownloadBaseDir(){
198
		return $this->getUpdaterBaseDir() . '/download';
199
	}
200
201
	/**
202
	 * Absolute path to a temporary directory
203
	 * to extract downloaded packages into
204
	 * @return string
205
	 */
206
	public function getExtractionBaseDir(){
207
		 return $this->getUpdaterBaseDir() . "/_oc_upgrade";
208
	}
209
210
	/**
211
	 *
212
	 * @return string
213
	 */
214
	public function getPathToOccFile(){
215
		return $this->ownCloudRootPath . '/occ';
216
	}
217
218
	/**
219
	 *
220
	 * @return string
221
	 */
222
	public function getInstalledVersion(){
223
		include $this->getPathToVersionFile();
224
225
		/** @var $OC_Version string */
226
		return $OC_Version;
0 ignored issues
show
Bug introduced by
The variable $OC_Version does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
227
	}
228
229
	/**
230
	 *
231
	 * @return string
232
	 */
233
	public function getChannelFromVersionsFile(){
234
		include $this->getPathToVersionFile();
235
236
		/** @var $OC_Channel string */
237
		return $OC_Channel;
0 ignored issues
show
Bug introduced by
The variable $OC_Channel does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
238
	}
239
240
	/**
241
	 *
242
	 * @return string
243
	 */
244
	public function getBuild(){
245
		include $this->getPathToVersionFile();
246
247
		/** @var $OC_Build string */
248
		return $OC_Build;
0 ignored issues
show
Bug introduced by
The variable $OC_Build does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
249
	}
250
251
	/**
252
	 * @return string
253
	 */
254
	public function getSecretFromConfig(){
255
		include $this->getPathToConfigFile();
256
		if (isset($CONFIG['updater.secret'])){
0 ignored issues
show
Bug introduced by
The variable $CONFIG seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
257
			return $CONFIG['updater.secret'];
258
		}
259
		return '';
260
	}
261
262
	/**
263
	 * @param string $filePostfix
264
	 * @return array
265
	 */
266
	public function getPathtoConfigFiles($filePostfix = 'config.php'){
267
		// Only config.php for now
268
		return [
269
			$this->ownCloudRootPath . '/config/' . $filePostfix
270
		];
271
	}
272
273
	/**
274
	 * @return string
275
	 */
276
	public function getPathToConfigFile(){
277
		return $this->ownCloudRootPath . '/config/config.php';
278
	}
279
280
	/**
281
	 *
282
	 * @return string
283
	 */
284
	public function getPathToVersionFile(){
285
		return $this->ownCloudRootPath . '/version.php';
286
	}
287
288
	/**
289
	 * @param string $rootPath
290
	 *
291
	 * @return array|mixed
292
	 */
293
	private function getSignature($rootPath) {
294
		$signature = [];
295
		$signaturePath = $rootPath . '/core/signature.json';
296
		if (is_file($signaturePath)) {
297
			$signature = \json_decode(file_get_contents($signaturePath), true);
298
			if (!is_array($signature)) {
299
				$signature = [];
300
			}
301
		}
302
		return $signature;
303
	}
304
}
305