2014-11-24 18:24:26 +03:00
|
|
|
<?php
|
2015-03-26 13:44:34 +03:00
|
|
|
/**
|
2016-07-22 10:45:36 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Andreas Fischer <bantu@owncloud.com>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Christoph Wurst <christoph@owncloud.com>
|
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2014-11-24 18:24:26 +03:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2014-11-24 18:24:26 +03:00
|
|
|
namespace OC\App;
|
|
|
|
|
|
|
|
use OCP\IURLGenerator;
|
|
|
|
|
|
|
|
class InfoParser {
|
2014-11-25 12:22:06 +03:00
|
|
|
|
2016-04-19 13:19:39 +03:00
|
|
|
/** @var IURLGenerator */
|
2014-11-25 12:22:06 +03:00
|
|
|
private $urlGenerator;
|
2014-11-24 18:24:26 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IURLGenerator $urlGenerator
|
|
|
|
*/
|
2016-04-19 13:19:39 +03:00
|
|
|
public function __construct(IURLGenerator $urlGenerator) {
|
2014-11-24 18:24:26 +03:00
|
|
|
$this->urlGenerator = $urlGenerator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-11-25 12:22:06 +03:00
|
|
|
* @param string $file the xml file to be loaded
|
|
|
|
* @return null|array where null is an indicator for an error
|
2014-11-24 18:24:26 +03:00
|
|
|
*/
|
|
|
|
public function parse($file) {
|
|
|
|
if (!file_exists($file)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-12-17 19:00:10 +03:00
|
|
|
libxml_use_internal_errors(true);
|
2014-11-25 12:22:06 +03:00
|
|
|
$loadEntities = libxml_disable_entity_loader(false);
|
2014-12-17 19:00:10 +03:00
|
|
|
$xml = simplexml_load_file($file);
|
2014-11-25 12:22:06 +03:00
|
|
|
libxml_disable_entity_loader($loadEntities);
|
|
|
|
if ($xml == false) {
|
2015-12-17 19:28:44 +03:00
|
|
|
libxml_clear_errors();
|
2014-11-25 12:22:06 +03:00
|
|
|
return null;
|
|
|
|
}
|
2015-12-04 17:52:42 +03:00
|
|
|
$array = $this->xmlToArray($xml);
|
2014-11-24 19:26:07 +03:00
|
|
|
if (is_null($array)) {
|
2014-11-24 18:24:26 +03:00
|
|
|
return null;
|
|
|
|
}
|
2014-11-24 19:26:07 +03:00
|
|
|
if (!array_key_exists('info', $array)) {
|
2016-04-19 16:36:11 +03:00
|
|
|
$array['info'] = [];
|
2014-11-24 19:26:07 +03:00
|
|
|
}
|
|
|
|
if (!array_key_exists('remote', $array)) {
|
2016-04-19 16:36:11 +03:00
|
|
|
$array['remote'] = [];
|
2014-11-24 19:26:07 +03:00
|
|
|
}
|
|
|
|
if (!array_key_exists('public', $array)) {
|
2016-04-19 16:36:11 +03:00
|
|
|
$array['public'] = [];
|
2014-11-24 19:26:07 +03:00
|
|
|
}
|
2014-12-01 19:35:01 +03:00
|
|
|
if (!array_key_exists('types', $array)) {
|
2016-04-19 16:36:11 +03:00
|
|
|
$array['types'] = [];
|
2014-12-01 19:35:01 +03:00
|
|
|
}
|
2016-04-19 13:19:39 +03:00
|
|
|
if (!array_key_exists('repair-steps', $array)) {
|
2016-04-19 16:36:11 +03:00
|
|
|
$array['repair-steps'] = [];
|
2016-04-19 13:19:39 +03:00
|
|
|
}
|
2016-04-28 11:07:29 +03:00
|
|
|
if (!array_key_exists('install', $array['repair-steps'])) {
|
|
|
|
$array['repair-steps']['install'] = [];
|
|
|
|
}
|
2016-04-19 13:19:39 +03:00
|
|
|
if (!array_key_exists('pre-migration', $array['repair-steps'])) {
|
2016-04-19 16:36:11 +03:00
|
|
|
$array['repair-steps']['pre-migration'] = [];
|
2016-04-19 13:19:39 +03:00
|
|
|
}
|
|
|
|
if (!array_key_exists('post-migration', $array['repair-steps'])) {
|
2016-04-19 16:36:11 +03:00
|
|
|
$array['repair-steps']['post-migration'] = [];
|
2016-04-19 13:19:39 +03:00
|
|
|
}
|
2016-04-26 12:56:56 +03:00
|
|
|
if (!array_key_exists('live-migration', $array['repair-steps'])) {
|
|
|
|
$array['repair-steps']['live-migration'] = [];
|
|
|
|
}
|
2016-04-28 11:07:29 +03:00
|
|
|
if (!array_key_exists('uninstall', $array['repair-steps'])) {
|
|
|
|
$array['repair-steps']['uninstall'] = [];
|
|
|
|
}
|
2016-05-02 16:26:12 +03:00
|
|
|
if (!array_key_exists('background-jobs', $array)) {
|
|
|
|
$array['background-jobs'] = [];
|
|
|
|
}
|
2016-05-11 12:23:25 +03:00
|
|
|
if (!array_key_exists('two-factor-providers', $array)) {
|
|
|
|
$array['two-factor-providers'] = [];
|
|
|
|
}
|
2014-11-24 19:26:07 +03:00
|
|
|
|
2014-12-01 23:47:22 +03:00
|
|
|
if (array_key_exists('documentation', $array) && is_array($array['documentation'])) {
|
2014-11-24 19:26:07 +03:00
|
|
|
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
|
2016-04-19 13:19:39 +03:00
|
|
|
if (!$this->isHTTPURL($url)) {
|
2014-11-24 19:26:07 +03:00
|
|
|
$url = $this->urlGenerator->linkToDocs($url);
|
2014-11-24 18:24:26 +03:00
|
|
|
}
|
|
|
|
|
2014-11-25 12:22:06 +03:00
|
|
|
$array['documentation'][$key] = $url;
|
2014-11-24 19:26:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (array_key_exists('types', $array)) {
|
2014-12-02 17:06:51 +03:00
|
|
|
if (is_array($array['types'])) {
|
|
|
|
foreach ($array['types'] as $type => $v) {
|
|
|
|
unset($array['types'][$type]);
|
|
|
|
if (is_string($type)) {
|
|
|
|
$array['types'][] = $type;
|
|
|
|
}
|
2014-12-01 23:47:22 +03:00
|
|
|
}
|
2014-12-02 17:06:51 +03:00
|
|
|
} else {
|
2016-04-19 16:36:11 +03:00
|
|
|
$array['types'] = [];
|
2014-11-24 18:24:26 +03:00
|
|
|
}
|
|
|
|
}
|
2016-04-28 11:07:29 +03:00
|
|
|
if (isset($array['repair-steps']['install']['step']) && is_array($array['repair-steps']['install']['step'])) {
|
|
|
|
$array['repair-steps']['install'] = $array['repair-steps']['install']['step'];
|
|
|
|
}
|
2016-04-19 13:19:39 +03:00
|
|
|
if (isset($array['repair-steps']['pre-migration']['step']) && is_array($array['repair-steps']['pre-migration']['step'])) {
|
|
|
|
$array['repair-steps']['pre-migration'] = $array['repair-steps']['pre-migration']['step'];
|
|
|
|
}
|
|
|
|
if (isset($array['repair-steps']['post-migration']['step']) && is_array($array['repair-steps']['post-migration']['step'])) {
|
|
|
|
$array['repair-steps']['post-migration'] = $array['repair-steps']['post-migration']['step'];
|
|
|
|
}
|
2016-04-26 12:56:56 +03:00
|
|
|
if (isset($array['repair-steps']['live-migration']['step']) && is_array($array['repair-steps']['live-migration']['step'])) {
|
|
|
|
$array['repair-steps']['live-migration'] = $array['repair-steps']['live-migration']['step'];
|
|
|
|
}
|
2016-04-28 11:07:29 +03:00
|
|
|
if (isset($array['repair-steps']['uninstall']['step']) && is_array($array['repair-steps']['uninstall']['step'])) {
|
|
|
|
$array['repair-steps']['uninstall'] = $array['repair-steps']['uninstall']['step'];
|
|
|
|
}
|
2016-05-02 16:26:12 +03:00
|
|
|
if (isset($array['background-jobs']['job']) && is_array($array['background-jobs']['job'])) {
|
|
|
|
$array['background-jobs'] = $array['background-jobs']['job'];
|
|
|
|
}
|
2014-11-24 19:26:07 +03:00
|
|
|
return $array;
|
2014-11-24 18:24:26 +03:00
|
|
|
}
|
2014-12-02 13:03:25 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \SimpleXMLElement $xml
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function xmlToArray($xml) {
|
|
|
|
if (!$xml->children()) {
|
|
|
|
return (string)$xml;
|
|
|
|
}
|
|
|
|
|
2016-04-19 16:36:11 +03:00
|
|
|
$array = [];
|
2014-12-02 13:03:25 +03:00
|
|
|
foreach ($xml->children() as $element => $node) {
|
|
|
|
$totalElement = count($xml->{$element});
|
|
|
|
|
|
|
|
if (!isset($array[$element])) {
|
|
|
|
$array[$element] = "";
|
|
|
|
}
|
2016-05-02 16:26:12 +03:00
|
|
|
/** @var \SimpleXMLElement $node */
|
2014-12-02 13:03:25 +03:00
|
|
|
// Has attributes
|
|
|
|
if ($attributes = $node->attributes()) {
|
2016-04-19 16:36:11 +03:00
|
|
|
$data = [
|
|
|
|
'@attributes' => [],
|
|
|
|
];
|
2014-12-02 13:03:25 +03:00
|
|
|
if (!count($node->children())){
|
|
|
|
$value = (string)$node;
|
|
|
|
if (!empty($value)) {
|
|
|
|
$data['@value'] = (string)$node;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$data = array_merge($data, $this->xmlToArray($node));
|
|
|
|
}
|
|
|
|
foreach ($attributes as $attr => $value) {
|
|
|
|
$data['@attributes'][$attr] = (string)$value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($totalElement > 1) {
|
|
|
|
$array[$element][] = $data;
|
|
|
|
} else {
|
|
|
|
$array[$element] = $data;
|
|
|
|
}
|
|
|
|
// Just a value
|
|
|
|
} else {
|
|
|
|
if ($totalElement > 1) {
|
|
|
|
$array[$element][] = $this->xmlToArray($node);
|
|
|
|
} else {
|
|
|
|
$array[$element] = $this->xmlToArray($node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
}
|
2016-04-19 13:19:39 +03:00
|
|
|
|
|
|
|
private function isHTTPURL($url) {
|
|
|
|
return stripos($url, 'https://') === 0 || stripos($url, 'http://') === 0;
|
|
|
|
}
|
2014-11-24 18:24:26 +03:00
|
|
|
}
|