Completed
Push — master ( 45e7bb...57e088 )
by Morris
13:03
created

InfoChecker::analyse()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 7
nop 1
dl 0
loc 32
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 *
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OC\App\CodeChecker;
25
26
use OC\Hooks\BasicEmitter;
27
use OCP\App\AppPathNotFoundException;
28
use OCP\App\IAppManager;
29
30
class InfoChecker extends BasicEmitter {
31
32
	/** @var string[] */
33
	private $shippedApps;
34
35
	/** @var string[] */
36
	private $alwaysEnabled;
37
38
	/**
39
	 * @param string $appId
40
	 * @return array
41
	 * @throws \RuntimeException
42
	 */
43
	public function analyse($appId): array {
44
		$appPath = \OC_App::getAppPath($appId);
45
		if ($appPath === false) {
46
			throw new \RuntimeException("No app with given id <$appId> known.");
47
		}
48
49
		$xml = new \DOMDocument();
50
		$xml->load($appPath . '/appinfo/info.xml');
51
52
		$schema = \OC::$SERVERROOT . '/resources/app-info.xsd';
53
		try {
54
			if ($this->isShipped($appId)) {
55
				// Shipped apps are allowed to have the public and default_enabled tags
56
				$schema = \OC::$SERVERROOT . '/resources/app-info-shipped.xsd';
57
			}
58
		} catch (\Exception $e) {
59
			// Assume it is not shipped
60
		}
61
62
		$errors = [];
63
		if (!$xml->schemaValidate($schema)) {
64
			foreach (libxml_get_errors() as $error) {
65
				$errors[] = [
66
					'type' => 'parseError',
67
					'field' => $error->message,
68
				];
69
				$this->emit('InfoChecker', 'parseError', [$error->message]);
70
			}
71
		}
72
73
		return $errors;
74
	}
75
76
	/**
77
	 * This is a copy of \OC\App\AppManager::isShipped(), keep both in sync.
78
	 * This method is copied, so the code checker works even when Nextcloud is
79
	 * not installed yet. The AppManager requires a database connection, which
80
	 * fails in that case.
81
	 *
82
	 * @param string $appId
83
	 * @return bool
84
	 * @throws \Exception
85
	 */
86
	protected function isShipped(string $appId): bool {
87
		$this->loadShippedJson();
88
		return \in_array($appId, $this->shippedApps, true);
89
	}
90
91
	/**
92
	 * This is a copy of \OC\App\AppManager::loadShippedJson(), keep both in sync
93
	 * This method is copied, so the code checker works even when Nextcloud is
94
	 * not installed yet. The AppManager requires a database connection, which
95
	 * fails in that case.
96
	 *
97
	 * @throws \Exception
98
	 */
99 View Code Duplication
	protected function loadShippedJson() {
100
		if ($this->shippedApps === null) {
101
			$shippedJson = \OC::$SERVERROOT . '/core/shipped.json';
102
			if (!file_exists($shippedJson)) {
103
				throw new \Exception("File not found: $shippedJson");
104
			}
105
			$content = json_decode(file_get_contents($shippedJson), true);
106
			$this->shippedApps = $content['shippedApps'];
107
			$this->alwaysEnabled = $content['alwaysEnabled'];
108
		}
109
	}
110
}
111