summaryrefslogtreecommitdiffstats
path: root/config/appconfig.php
blob: 5292d35a78ee1ee8709023b8e9778cf4730be48b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
/**
 * ownCloud - News
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Alessandro Cosentino <cosenal@gmail.com>
 * @author Bernhard Posselt <dev@bernhard-posselt.com>
 * @copyright Alessandro Cosentino 2012
 * @copyright Bernhard Posselt 2012, 2014
 */

namespace OCA\News\Config;

use OCP\INavigationManager;
use OCP\IL10N;
use OCP\IURLGenerator;
use \OCP\Backgroundjob;
use \OCP\Util;


// Used to parse app.json file, should be in core at some point
class AppConfig {

	private $config;
	private $navigationManager;
	private $urlGenerator;
	private $phpVersion;
	private $ownCloudVersion;
	private $installedExtensions;
	private $databaseType;
	private $l10n;

	/**
	 * TODO: External deps that are needed:
	 * - add jobs
	 * - connect to hooks
	 */
	public function __construct(INavigationManager $navigationManager,
	                            IL10N $l10n,
	                            IURLGenerator $urlGenerator,
	                            $phpVersion,
	                            $ownCloudVersion,
	                            $installedExtensions,
	                            $databaseType) {
		$this->navigationManager = $navigationManager;
		$this->l10n = $l10n;
		$this->urlGenerator = $urlGenerator;
		$this->ownCloudVersion = $ownCloudVersion;
		$this->phpVersion = $phpVersion;
		$this->installedExtensions = $installedExtensions;
		$this->databaseType = $databaseType;
		$this->config = [];
	}


	/**
	 * @param string|array $data path to the config file or an array with the config
	 */
	public function loadConfig($data) {
		if(is_array($data)) {
			$this->config = $data;
		} else {
			$json = file_get_contents($data);
			$this->config = json_decode($json, true);
		}

		// fill config with default values if no navigation is added
		if(array_key_exists('navigation', $this->config)) {
			$nav =& $this->config['navigation'];

			// add defaults
			$defaults = [
				'id' => $this->config['id'],
				'route' => $this->config['id'] . '.page.index',
				'order' => 10,
				'icon' => 'app.svg',
				'name' => $this->config['name']
			];

			foreach($defaults as $key => $value) {
				if(!array_key_exists($key, $nav)) {
					$nav[$key] = $value;
				}
			}
		}
	}

	/**
	 * @param string $key if given returns the value of the config at index $key
	 * @return array|mixed the config
	 */
	public function getConfig($key=null) {
		// FIXME: is this function interface a good idea?
		if($key !== null) {
			return $this->config[$key];
		} else {
			return $this->config;
		}
	}


	/**
	 * Parses the navigation and creates a navigation entry if needed
	 */
	public function registerNavigation() {
		// if key is missing, don't create a navigation
		if(array_key_exists('navigation', $this->config)) {
			$nav =& $this->config['navigation'];

			$navConfig = [
				'id' => $nav['id'],
				'order' => $nav['order']
			];

			$navConfig['name'] = $this->l10n->t($nav['name']);
			$navConfig['href'] = $this->urlGenerator->linkToRoute($nav['route']);
			$navConfig['icon'] = $this->urlGenerator->imagePath($nav['id'],
				$nav['icon']);

			$this->navigationManager->add($navConfig);
		}

	}


	/**
	 * Registers all jobs in the config
	 */
	public function registerBackgroundJobs() {
		// FIXME: this is temporarily static because core jobs are not public
		// yet, therefore legacy code
		foreach ($this->config['jobs'] as $job) {
			Backgroundjob::addRegularTask($job, 'run');
		}
	}


	/**
	 * Registers all hooks in the config
	 */
	public function registerHooks() {
		// FIXME: this is temporarily static because core emitters are not future
		// proof, therefore legacy code in here
		foreach ($this->config['hooks'] as $listen => $react) {
			$listener = explode('::', $listen);
			$reaction = explode('::', $react);

			// config is written like HookNamespace::method => Class::method
			Util::connectHook($listener[0], $listener[1], $reaction[0],
			                       $reaction[1]);
		}
	}


	/**
	 * Validates all dependencies that the app has
	 * @throws DependencyException if one version is not satisfied
	 */
	public function testDependencies() {
		$msg = '';

		// test databases
		if(array_key_exists('databases', $this->config)) {
			if(!in_array($this->databaseType, $this->config['databases'])) {
				$msg .= 'Database ' . $this->databaseType . ' not supported.' .
					'App is only compatible with ' .
					implode(', ', $this->config['databases']);
			}
		}

		// test dependencies
		if(array_key_exists('dependencies', $this->config)) {

			$deps = $this->config['dependencies'];

			if(array_key_exists('php', $deps)) {
				$msg .= $this->requireVersion($this->phpVersion, $deps['php'],
					'PHP');
			}

			if(array_key_exists('owncloud', $deps)) {
				$msg .= $this->requireVersion($this->ownCloudVersion,
					$deps['owncloud'], 'ownCloud');
			}

			if(array_key_exists('libs', $deps)) {
				foreach ($deps['libs'] as $lib => $versions) {
					if(array_key_exists($lib, $this->installedExtensions)) {
						$msg .= $this->requireVersion($this->installedExtensions[$lib],
							$versions, 'PHP extension ' . $lib);
					} else {
						$msg .= 'PHP extension ' . $lib . ' required but not installed';
					}
				}
			}
		}

		if($msg !== '') {
			throw new DependencyException($msg);
		}
	}


	/**
	 * Compares a version with a version requirement string
	 * @param string $actual the actual version that is there
	 * @param string $required a version requirement in the form of
	 * <=5.3,>4.5 versions are separated with a comma
	 * @param string $versionType a description of the string that is prepended
	 * to the error message
	 * @return string an error message if the version is not met,
     * empty string if ok
	 */
	private function requireVersion($actual, $required, $versionType) {
		$requiredVersions = $this->splitVersions($required);

		foreach($requiredVersions as $version) {
			// accept * as wildcard for any version
			if($version['version'] === '*') {
				continue;
			}
			if(!version_compare($actual, $version['version'], $version['operator'])) {
				return $versionType . ' Version not satisfied: ' . $version['operator'] .
					$version['version'] . ' required but found ' . $actual . '\n';
			}
		}

		return '';
	}


	/**
	 * Versions can be separated by a comma so split them
	 * @param string $versions a version requirement in the form of
	 * <=5.3,>4.5 versions are separated with a comma
	 * @return array of arrays with key=version value=operator
	 */
	private function splitVersions($versions) {
		$result = [];
		$versions = explode(',', $versions);

		foreach($versions as $version) {
			preg_match('/^(?<operator><|<=|>=|>|<>)?(?<version>.*)$/', $version, $matches);
			if($matches['operator'] !== '') {
				$required = [
					'version' => $matches['version'],
					'operator' => $matches['operator'],
				];
			} else {
				$required = [
					'version' => $matches['version'],
					'operator' => '==',
				];
			}
			$result[] = $required;
		}

		return $result;
	}


}