2014-11-24 15:24:26 +00:00
|
|
|
<?php
|
2015-03-26 10:44:34 +00:00
|
|
|
/**
|
2016-07-21 15:07:57 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2016-10-07 19:15:52 +00:00
|
|
|
* @copyright Copyright (c) 2016, Lukas Reschke <lukas@statuscode.ch>
|
2016-07-21 15:07:57 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Andreas Fischer <bantu@owncloud.com>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Christoph Wurst <christoph@owncloud.com>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @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 15:24:26 +00:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2014-11-24 15:24:26 +00:00
|
|
|
namespace OC\App;
|
|
|
|
|
2016-10-07 19:15:52 +00:00
|
|
|
use OCP\ICache;
|
|
|
|
|
2014-11-24 15:24:26 +00:00
|
|
|
class InfoParser {
|
2016-10-07 19:15:52 +00:00
|
|
|
/** @var \OCP\ICache|null */
|
|
|
|
private $cache;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ICache|null $cache
|
|
|
|
*/
|
|
|
|
public function __construct(ICache $cache = null) {
|
|
|
|
$this->cache = $cache;
|
|
|
|
}
|
2014-11-25 09:22:06 +00:00
|
|
|
|
2014-11-24 15:24:26 +00:00
|
|
|
/**
|
2014-11-25 09:22:06 +00:00
|
|
|
* @param string $file the xml file to be loaded
|
|
|
|
* @return null|array where null is an indicator for an error
|
2014-11-24 15:24:26 +00:00
|
|
|
*/
|
|
|
|
public function parse($file) {
|
|
|
|
if (!file_exists($file)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-02-22 14:45:55 +00:00
|
|
|
if ($this->cache !== null) {
|
2016-10-07 19:15:52 +00:00
|
|
|
$fileCacheKey = $file . filemtime($file);
|
|
|
|
if ($cachedValue = $this->cache->get($fileCacheKey)) {
|
|
|
|
return json_decode($cachedValue, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-17 16:00:10 +00:00
|
|
|
libxml_use_internal_errors(true);
|
2014-11-25 09:22:06 +00:00
|
|
|
$loadEntities = libxml_disable_entity_loader(false);
|
2014-12-17 16:00:10 +00:00
|
|
|
$xml = simplexml_load_file($file);
|
2016-09-23 19:47:47 +00:00
|
|
|
|
2014-11-25 09:22:06 +00:00
|
|
|
libxml_disable_entity_loader($loadEntities);
|
2016-09-23 19:47:47 +00:00
|
|
|
if ($xml === false) {
|
2015-12-17 16:28:44 +00:00
|
|
|
libxml_clear_errors();
|
2014-11-25 09:22:06 +00:00
|
|
|
return null;
|
|
|
|
}
|
2015-12-04 14:52:42 +00:00
|
|
|
$array = $this->xmlToArray($xml);
|
2016-09-23 19:47:47 +00:00
|
|
|
|
2014-11-24 16:26:07 +00:00
|
|
|
if (is_null($array)) {
|
2014-11-24 15:24:26 +00:00
|
|
|
return null;
|
|
|
|
}
|
2016-09-23 19:47:47 +00:00
|
|
|
|
2014-11-24 16:26:07 +00:00
|
|
|
if (!array_key_exists('info', $array)) {
|
2016-04-19 13:36:11 +00:00
|
|
|
$array['info'] = [];
|
2014-11-24 16:26:07 +00:00
|
|
|
}
|
|
|
|
if (!array_key_exists('remote', $array)) {
|
2016-04-19 13:36:11 +00:00
|
|
|
$array['remote'] = [];
|
2014-11-24 16:26:07 +00:00
|
|
|
}
|
|
|
|
if (!array_key_exists('public', $array)) {
|
2016-04-19 13:36:11 +00:00
|
|
|
$array['public'] = [];
|
2014-11-24 16:26:07 +00:00
|
|
|
}
|
2014-12-01 16:35:01 +00:00
|
|
|
if (!array_key_exists('types', $array)) {
|
2016-04-19 13:36:11 +00:00
|
|
|
$array['types'] = [];
|
2014-12-01 16:35:01 +00:00
|
|
|
}
|
2016-04-19 10:19:39 +00:00
|
|
|
if (!array_key_exists('repair-steps', $array)) {
|
2016-04-19 13:36:11 +00:00
|
|
|
$array['repair-steps'] = [];
|
2016-04-19 10:19:39 +00:00
|
|
|
}
|
2016-04-28 08:07:29 +00:00
|
|
|
if (!array_key_exists('install', $array['repair-steps'])) {
|
|
|
|
$array['repair-steps']['install'] = [];
|
|
|
|
}
|
2016-04-19 10:19:39 +00:00
|
|
|
if (!array_key_exists('pre-migration', $array['repair-steps'])) {
|
2016-04-19 13:36:11 +00:00
|
|
|
$array['repair-steps']['pre-migration'] = [];
|
2016-04-19 10:19:39 +00:00
|
|
|
}
|
|
|
|
if (!array_key_exists('post-migration', $array['repair-steps'])) {
|
2016-04-19 13:36:11 +00:00
|
|
|
$array['repair-steps']['post-migration'] = [];
|
2016-04-19 10:19:39 +00:00
|
|
|
}
|
2016-04-26 09:56:56 +00:00
|
|
|
if (!array_key_exists('live-migration', $array['repair-steps'])) {
|
|
|
|
$array['repair-steps']['live-migration'] = [];
|
|
|
|
}
|
2016-04-28 08:07:29 +00:00
|
|
|
if (!array_key_exists('uninstall', $array['repair-steps'])) {
|
|
|
|
$array['repair-steps']['uninstall'] = [];
|
|
|
|
}
|
2016-05-02 13:26:12 +00:00
|
|
|
if (!array_key_exists('background-jobs', $array)) {
|
|
|
|
$array['background-jobs'] = [];
|
|
|
|
}
|
2016-05-11 09:23:25 +00:00
|
|
|
if (!array_key_exists('two-factor-providers', $array)) {
|
|
|
|
$array['two-factor-providers'] = [];
|
|
|
|
}
|
2016-09-30 08:09:52 +00:00
|
|
|
if (!array_key_exists('commands', $array)) {
|
|
|
|
$array['commands'] = [];
|
|
|
|
}
|
2016-10-20 15:57:44 +00:00
|
|
|
if (!array_key_exists('activity', $array)) {
|
|
|
|
$array['activity'] = [];
|
|
|
|
}
|
|
|
|
if (!array_key_exists('filters', $array['activity'])) {
|
|
|
|
$array['activity']['filters'] = [];
|
|
|
|
}
|
2016-10-25 16:00:25 +00:00
|
|
|
if (!array_key_exists('settings', $array['activity'])) {
|
|
|
|
$array['activity']['settings'] = [];
|
|
|
|
}
|
2016-11-04 10:33:33 +00:00
|
|
|
if (!array_key_exists('providers', $array['activity'])) {
|
|
|
|
$array['activity']['providers'] = [];
|
|
|
|
}
|
2017-10-04 14:35:10 +00:00
|
|
|
if (!array_key_exists('settings', $array)) {
|
|
|
|
$array['settings'] = [];
|
|
|
|
}
|
|
|
|
if (!array_key_exists('admin', $array['settings'])) {
|
|
|
|
$array['settings']['admin'] = [];
|
|
|
|
}
|
|
|
|
if (!array_key_exists('admin-section', $array['settings'])) {
|
|
|
|
$array['settings']['admin-section'] = [];
|
|
|
|
}
|
|
|
|
if (!array_key_exists('personal', $array['settings'])) {
|
|
|
|
$array['settings']['personal'] = [];
|
|
|
|
}
|
|
|
|
if (!array_key_exists('personal-section', $array['settings'])) {
|
|
|
|
$array['settings']['personal-section'] = [];
|
|
|
|
}
|
2014-11-24 16:26:07 +00:00
|
|
|
|
|
|
|
if (array_key_exists('types', $array)) {
|
2014-12-02 14:06:51 +00: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 20:47:22 +00:00
|
|
|
}
|
2014-12-02 14:06:51 +00:00
|
|
|
} else {
|
2016-04-19 13:36:11 +00:00
|
|
|
$array['types'] = [];
|
2014-11-24 15:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-28 08:07:29 +00: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 10:19:39 +00: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 09:56:56 +00: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 08:07:29 +00: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 13:26:12 +00:00
|
|
|
if (isset($array['background-jobs']['job']) && is_array($array['background-jobs']['job'])) {
|
|
|
|
$array['background-jobs'] = $array['background-jobs']['job'];
|
|
|
|
}
|
2016-09-30 08:09:52 +00:00
|
|
|
if (isset($array['commands']['command']) && is_array($array['commands']['command'])) {
|
|
|
|
$array['commands'] = $array['commands']['command'];
|
|
|
|
}
|
2018-08-21 20:53:38 +00:00
|
|
|
if (isset($array['two-factor-providers']['provider']) && is_array($array['two-factor-providers']['provider'])) {
|
|
|
|
$array['two-factor-providers'] = $array['two-factor-providers']['provider'];
|
|
|
|
}
|
2016-10-20 15:57:44 +00:00
|
|
|
if (isset($array['activity']['filters']['filter']) && is_array($array['activity']['filters']['filter'])) {
|
|
|
|
$array['activity']['filters'] = $array['activity']['filters']['filter'];
|
|
|
|
}
|
2016-10-25 16:00:25 +00:00
|
|
|
if (isset($array['activity']['settings']['setting']) && is_array($array['activity']['settings']['setting'])) {
|
|
|
|
$array['activity']['settings'] = $array['activity']['settings']['setting'];
|
|
|
|
}
|
2016-11-04 10:33:33 +00:00
|
|
|
if (isset($array['activity']['providers']['provider']) && is_array($array['activity']['providers']['provider'])) {
|
|
|
|
$array['activity']['providers'] = $array['activity']['providers']['provider'];
|
|
|
|
}
|
2017-09-06 21:02:45 +00:00
|
|
|
if (isset($array['collaboration']['collaborators']['searchPlugins']['searchPlugin'])
|
|
|
|
&& is_array($array['collaboration']['collaborators']['searchPlugins']['searchPlugin'])
|
|
|
|
&& !isset($array['collaboration']['collaborators']['searchPlugins']['searchPlugin']['class'])
|
|
|
|
) {
|
2017-09-06 19:57:00 +00:00
|
|
|
$array['collaboration']['collaborators']['searchPlugins'] = $array['collaboration']['collaborators']['searchPlugins']['searchPlugin'];
|
|
|
|
}
|
2017-10-04 14:35:10 +00:00
|
|
|
if (isset($array['settings']['admin']) && !is_array($array['settings']['admin'])) {
|
|
|
|
$array['settings']['admin'] = [$array['settings']['admin']];
|
|
|
|
}
|
|
|
|
if (isset($array['settings']['admin-section']) && !is_array($array['settings']['admin-section'])) {
|
|
|
|
$array['settings']['admin-section'] = [$array['settings']['admin-section']];
|
|
|
|
}
|
|
|
|
if (isset($array['settings']['personal']) && !is_array($array['settings']['personal'])) {
|
|
|
|
$array['settings']['personal'] = [$array['settings']['personal']];
|
|
|
|
}
|
|
|
|
if (isset($array['settings']['personal-section']) && !is_array($array['settings']['personal-section'])) {
|
|
|
|
$array['settings']['personal-section'] = [$array['settings']['personal-section']];
|
|
|
|
}
|
2016-10-07 19:15:52 +00:00
|
|
|
|
2019-02-22 14:45:55 +00:00
|
|
|
if (isset($array['navigations']['navigation']) && $this->isNavigationItem($array['navigations']['navigation'])) {
|
|
|
|
$array['navigations']['navigation'] = [$array['navigations']['navigation']];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->cache !== null) {
|
2016-10-07 19:15:52 +00:00
|
|
|
$this->cache->set($fileCacheKey, json_encode($array));
|
|
|
|
}
|
2014-11-24 16:26:07 +00:00
|
|
|
return $array;
|
2014-11-24 15:24:26 +00:00
|
|
|
}
|
2014-12-02 10:03:25 +00:00
|
|
|
|
2019-02-22 14:45:55 +00:00
|
|
|
/**
|
|
|
|
* @param $data
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function isNavigationItem($data): bool {
|
|
|
|
return isset($data['name'], $data['route']);
|
|
|
|
}
|
|
|
|
|
2014-12-02 10:03:25 +00:00
|
|
|
/**
|
|
|
|
* @param \SimpleXMLElement $xml
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-07-23 19:03:26 +00:00
|
|
|
public function xmlToArray($xml) {
|
2014-12-02 10:03:25 +00:00
|
|
|
if (!$xml->children()) {
|
|
|
|
return (string)$xml;
|
|
|
|
}
|
|
|
|
|
2016-04-19 13:36:11 +00:00
|
|
|
$array = [];
|
2014-12-02 10:03:25 +00:00
|
|
|
foreach ($xml->children() as $element => $node) {
|
|
|
|
$totalElement = count($xml->{$element});
|
|
|
|
|
|
|
|
if (!isset($array[$element])) {
|
2016-07-04 11:47:03 +00:00
|
|
|
$array[$element] = $totalElement > 1 ? [] : "";
|
2014-12-02 10:03:25 +00:00
|
|
|
}
|
2016-05-02 13:26:12 +00:00
|
|
|
/** @var \SimpleXMLElement $node */
|
2014-12-02 10:03:25 +00:00
|
|
|
// Has attributes
|
|
|
|
if ($attributes = $node->attributes()) {
|
2016-04-19 13:36:11 +00:00
|
|
|
$data = [
|
|
|
|
'@attributes' => [],
|
|
|
|
];
|
2014-12-02 10:03:25 +00: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;
|
|
|
|
}
|
2014-11-24 15:24:26 +00:00
|
|
|
}
|