Completed
Pull Request — master (#355)
by Victor
03:09
created

Locator::getSecretFromConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 6
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
class Locator {
27
28
	/**
29
	 * absolute path to ownCloud root
30
	 * @var string 
31
	 */
32
	protected $ownCloudRootPath;
33
34
	/**
35
	 * absolute path to updater root
36
	 * @var string
37
	 */
38
	protected $updaterRootPath;
39
40
	/**
41
	 *
42
	 * @param string $baseDir
43
	 */
44
	public function __construct($baseDir){
45
		$this->updaterRootPath = $baseDir;
46
		$this->ownCloudRootPath = dirname($baseDir);
47
	}
48
49
	public function getOwnCloudRootPath(){
50
		return $this->ownCloudRootPath;
51
	}
52
53
	/**
54
	 * expected items in the core
55
	 * @return string[]
56
	 */
57
	public function getRootDirContent(){
58
		return [
59
			"3rdparty",
60
			"config",
61
			"core",
62
			"l10n",
63
			"lib",
64
			"ocs",
65
			"ocs-provider",
66
			"resources",
67
			"settings",
68
			".htaccess",
69
			".mailmap",
70
			".tag",
71
			".user.ini",
72
			"AUTHORS",
73
			"console.php",
74
			"COPYING-AGPL",
75
			"cron.php",
76
			"db_structure.xml",
77
			"index.html",
78
			"index.php",
79
			"indie.json",
80
			"occ",
81
			"public.php",
82
			"remote.php",
83
			"robots.txt",
84
			"status.php",
85
			"version.php"
86
		];
87
	}
88
89
	public function getUpdaterContent(){
90
		return [
91
			'app',
92
			'application.php',
93
			'box.json',
94
			'composer.json',
95
			'composer.lock',
96
			'CONTRIBUTING.md',
97
			'COPYING-AGPL',
98
			'index.php',
99
			'pub',
100
			'src',
101
			'vendor',
102
			'README.md',
103
			'.travis.yml',
104
			'.scrutinizer.yml',
105
		];
106
	}
107
108
	/**
109
	 * Absolute path to core root dir content
110
	 * @return array
111
	 */
112
	public function getRootDirItems(){
113
		$items = $this->getRootDirContent();
114
		$items = array_map(
115
			function($item){ return $this->ownCloudRootPath . "/" . $item;	},
116
			$items
117
		);
118
		return $items;
119
	}
120
121
	/**
122
	 * Absolute path
123
	 * @return string
124
	 * @throws \Exception
125
	 */
126
	public function getDataDir(){
127
		$container = Application::$container;
128
		if (isset($container['utils.configReader']) && $container['utils.configReader']->getIsLoaded()){
129
			return $container['utils.configReader']->getByPath('system.datadirectory');
130
		}
131
132
		// Fallback case
133
		include $this->getPathToConfigFile();
134
		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...
135
			return $CONFIG['datadirectory'];
136
		}
137
138
		// Something went wrong
139
		throw new \Exception('Unable to detect datadirectory');
140
	}
141
142
	/**
143
	 * Absolute path to updater root dir
144
	 * @return string
145
	 */
146
	public function getUpdaterBaseDir(){
147
		return $this->getDataDir() . '/updater-data';
148
	}
149
150
	/**
151
	 * Absolute path to create a core and apps backups
152
	 * @return string
153
	 */
154
	public function getCheckpointDir(){
155
		return $this->getUpdaterBaseDir() . '/checkpoint';
156
	}
157
158
	/**
159
	 * Absolute path to store downloaded packages
160
	 * @return string
161
	 */
162
	public function getDownloadBaseDir(){
163
		return $this->getUpdaterBaseDir() . '/download';
164
	}
165
166
	/**
167
	 * Absolute path to a temporary directory
168
	 * to extract downloaded packages into
169
	 * @return string
170
	 */
171
	public function getExtractionBaseDir(){
172
		 return $this->getUpdaterBaseDir() . "/_oc_upgrade";
173
	}
174
175
	/**
176
	 *
177
	 * @return string
178
	 */
179
	public function getPathToOccFile(){
180
		return $this->ownCloudRootPath . '/occ';
181
	}
182
183
	/**
184
	 *
185
	 * @return string
186
	 */
187
	public function getInstalledVersion(){
188
		include $this->getPathToVersionFile();
189
190
		/** @var $OC_Version string */
191
		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...
192
	}
193
194
	/**
195
	 *
196
	 * @return string
197
	 */
198
	public function getChannelFromVersionsFile(){
199
		include $this->getPathToVersionFile();
200
201
		/** @var $OC_Version string */
202
		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...
203
	}
204
205
	/**
206
	 *
207
	 * @return string
208
	 */
209
	public function getBuild(){
210
		include $this->getPathToVersionFile();
211
212
		/** @var $OC_Build string */
213
		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...
214
	}
215
216
	/**
217
	 * @return string
218
	 */
219
	public function getSecretFromConfig(){
220
		include $this->getPathToConfigFile();
221
		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...
222
			return $CONFIG['updater.secret'];
223
		}
224
		return '';
225
	}
226
227
	public function getPathtoConfigFiles($filePostfix = 'config.php'){
228
		// Only config.php for now
229
		return [
230
			$this->ownCloudRootPath . '/config/' . $filePostfix
231
		];
232
	}
233
234
	public function getPathToConfigFile(){
235
		return $this->ownCloudRootPath . '/config/config.php';
236
	}
237
238
	/**
239
	 *
240
	 * @return string
241
	 */
242
	public function getPathToVersionFile(){
243
		return $this->ownCloudRootPath . '/version.php';
244
	}
245
246
}
247