2014-11-24 18:24:26 +03:00
|
|
|
<?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 {
|
2014-11-25 12:22:06 +03:00
|
|
|
/**
|
|
|
|
* @var \OC\HTTPHelper
|
|
|
|
*/
|
|
|
|
private $httpHelper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var IURLGenerator
|
|
|
|
*/
|
|
|
|
private $urlGenerator;
|
2014-11-24 18:24:26 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \OC\HTTPHelper $httpHelper
|
|
|
|
* @param IURLGenerator $urlGenerator
|
|
|
|
*/
|
|
|
|
public function __construct(\OC\HTTPHelper $httpHelper, IURLGenerator $urlGenerator) {
|
|
|
|
$this->httpHelper = $httpHelper;
|
|
|
|
$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) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-12-02 13:03:25 +03:00
|
|
|
$array = $this->xmlToArray($xml, false);
|
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)) {
|
|
|
|
$array['info'] = array();
|
|
|
|
}
|
|
|
|
if (!array_key_exists('remote', $array)) {
|
|
|
|
$array['remote'] = array();
|
|
|
|
}
|
|
|
|
if (!array_key_exists('public', $array)) {
|
|
|
|
$array['public'] = array();
|
|
|
|
}
|
2014-12-01 19:35:01 +03:00
|
|
|
if (!array_key_exists('types', $array)) {
|
|
|
|
$array['types'] = array();
|
|
|
|
}
|
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
|
|
|
|
if (!$this->httpHelper->isHTTPURL($url)) {
|
|
|
|
$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 {
|
|
|
|
$array['types'] = array();
|
2014-11-24 18:24:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$array = array();
|
|
|
|
foreach ($xml->children() as $element => $node) {
|
|
|
|
$totalElement = count($xml->{$element});
|
|
|
|
|
|
|
|
if (!isset($array[$element])) {
|
|
|
|
$array[$element] = "";
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @var \SimpleXMLElement $node
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Has attributes
|
|
|
|
if ($attributes = $node->attributes()) {
|
|
|
|
$data = array(
|
|
|
|
'@attributes' => array(),
|
|
|
|
);
|
|
|
|
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 18:24:26 +03:00
|
|
|
}
|