infoparser.php 1.98 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
 /**
 * @author Thomas Müller
 * @copyright 2014 Thomas Müller deepdiver@owncloud.com
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

namespace OC\App;

use OCP\IURLGenerator;

class InfoParser {
Thomas Müller's avatar
Thomas Müller committed
16
17
18
19
20
21
22
23
24
	/**
	 * @var \OC\HTTPHelper
	 */
	private $httpHelper;

	/**
	 * @var IURLGenerator
	 */
	private $urlGenerator;
25
26
27
28
29
30
31
32
33
34
35

	/**
	 * @param \OC\HTTPHelper $httpHelper
	 * @param IURLGenerator $urlGenerator
	 */
	public function __construct(\OC\HTTPHelper $httpHelper, IURLGenerator $urlGenerator) {
		$this->httpHelper = $httpHelper;
		$this->urlGenerator = $urlGenerator;
	}

	/**
Thomas Müller's avatar
Thomas Müller committed
36
37
	 * @param string $file the xml file to be loaded
	 * @return null|array where null is an indicator for an error
38
39
40
41
42
43
	 */
	public function parse($file) {
		if (!file_exists($file)) {
			return null;
		}

Thomas Müller's avatar
Thomas Müller committed
44
45
46
47
48
49
		$loadEntities = libxml_disable_entity_loader(false);
		$xml = @simplexml_load_file($file);
		libxml_disable_entity_loader($loadEntities);
		if ($xml == false) {
			return null;
		}
Thomas Müller's avatar
Thomas Müller committed
50
51
		$array = json_decode(json_encode((array)$xml), TRUE);
		if (is_null($array)) {
52
53
			return null;
		}
Thomas Müller's avatar
Thomas Müller committed
54
55
56
57
58
59
60
61
62
		if (!array_key_exists('info', $array)) {
			$array['info'] = array();
		}
		if (!array_key_exists('remote', $array)) {
			$array['remote'] = array();
		}
		if (!array_key_exists('public', $array)) {
			$array['public'] = array();
		}
63
64
65
		if (!array_key_exists('types', $array)) {
			$array['types'] = array();
		}
Thomas Müller's avatar
Thomas Müller committed
66
67
68
69
70
71
72

		if (array_key_exists('documentation', $array)) {
			foreach ($array['documentation'] as $key => $url) {
				// If it is not an absolute URL we assume it is a key
				// i.e. admin-ldap will get converted to go.php?to=admin-ldap
				if (!$this->httpHelper->isHTTPURL($url)) {
					$url = $this->urlGenerator->linkToDocs($url);
73
74
				}

Thomas Müller's avatar
Thomas Müller committed
75
				$array['documentation'][$key] = $url;
Thomas Müller's avatar
Thomas Müller committed
76
77
78
79
80
81
			}
		}
		if (array_key_exists('types', $array)) {
			foreach ($array['types'] as $type => $v) {
				unset($array['types'][$type]);
				$array['types'][] = $type;
82
83
84
			}
		}

Thomas Müller's avatar
Thomas Müller committed
85
		return $array;
86
87
	}
}