InfoParser.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Andreas Fischer <bantu@owncloud.com>
  6. * @author Christoph Wurst <christoph@owncloud.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\App;
  27. use OCP\IURLGenerator;
  28. class InfoParser {
  29. /** @var IURLGenerator */
  30. private $urlGenerator;
  31. /**
  32. * @param IURLGenerator $urlGenerator
  33. */
  34. public function __construct(IURLGenerator $urlGenerator) {
  35. $this->urlGenerator = $urlGenerator;
  36. }
  37. /**
  38. * @param string $file the xml file to be loaded
  39. * @return null|array where null is an indicator for an error
  40. */
  41. public function parse($file) {
  42. if (!file_exists($file)) {
  43. return null;
  44. }
  45. libxml_use_internal_errors(true);
  46. $loadEntities = libxml_disable_entity_loader(false);
  47. $xml = simplexml_load_file($file);
  48. libxml_disable_entity_loader($loadEntities);
  49. if ($xml == false) {
  50. libxml_clear_errors();
  51. return null;
  52. }
  53. $array = $this->xmlToArray($xml);
  54. if (is_null($array)) {
  55. return null;
  56. }
  57. if (!array_key_exists('info', $array)) {
  58. $array['info'] = [];
  59. }
  60. if (!array_key_exists('remote', $array)) {
  61. $array['remote'] = [];
  62. }
  63. if (!array_key_exists('public', $array)) {
  64. $array['public'] = [];
  65. }
  66. if (!array_key_exists('types', $array)) {
  67. $array['types'] = [];
  68. }
  69. if (!array_key_exists('repair-steps', $array)) {
  70. $array['repair-steps'] = [];
  71. }
  72. if (!array_key_exists('install', $array['repair-steps'])) {
  73. $array['repair-steps']['install'] = [];
  74. }
  75. if (!array_key_exists('pre-migration', $array['repair-steps'])) {
  76. $array['repair-steps']['pre-migration'] = [];
  77. }
  78. if (!array_key_exists('post-migration', $array['repair-steps'])) {
  79. $array['repair-steps']['post-migration'] = [];
  80. }
  81. if (!array_key_exists('live-migration', $array['repair-steps'])) {
  82. $array['repair-steps']['live-migration'] = [];
  83. }
  84. if (!array_key_exists('uninstall', $array['repair-steps'])) {
  85. $array['repair-steps']['uninstall'] = [];
  86. }
  87. if (!array_key_exists('background-jobs', $array)) {
  88. $array['background-jobs'] = [];
  89. }
  90. if (!array_key_exists('two-factor-providers', $array)) {
  91. $array['two-factor-providers'] = [];
  92. }
  93. if (array_key_exists('documentation', $array) && is_array($array['documentation'])) {
  94. foreach ($array['documentation'] as $key => $url) {
  95. // If it is not an absolute URL we assume it is a key
  96. // i.e. admin-ldap will get converted to go.php?to=admin-ldap
  97. if (!$this->isHTTPURL($url)) {
  98. $url = $this->urlGenerator->linkToDocs($url);
  99. }
  100. $array['documentation'][$key] = $url;
  101. }
  102. }
  103. if (array_key_exists('types', $array)) {
  104. if (is_array($array['types'])) {
  105. foreach ($array['types'] as $type => $v) {
  106. unset($array['types'][$type]);
  107. if (is_string($type)) {
  108. $array['types'][] = $type;
  109. }
  110. }
  111. } else {
  112. $array['types'] = [];
  113. }
  114. }
  115. if (isset($array['repair-steps']['install']['step']) && is_array($array['repair-steps']['install']['step'])) {
  116. $array['repair-steps']['install'] = $array['repair-steps']['install']['step'];
  117. }
  118. if (isset($array['repair-steps']['pre-migration']['step']) && is_array($array['repair-steps']['pre-migration']['step'])) {
  119. $array['repair-steps']['pre-migration'] = $array['repair-steps']['pre-migration']['step'];
  120. }
  121. if (isset($array['repair-steps']['post-migration']['step']) && is_array($array['repair-steps']['post-migration']['step'])) {
  122. $array['repair-steps']['post-migration'] = $array['repair-steps']['post-migration']['step'];
  123. }
  124. if (isset($array['repair-steps']['live-migration']['step']) && is_array($array['repair-steps']['live-migration']['step'])) {
  125. $array['repair-steps']['live-migration'] = $array['repair-steps']['live-migration']['step'];
  126. }
  127. if (isset($array['repair-steps']['uninstall']['step']) && is_array($array['repair-steps']['uninstall']['step'])) {
  128. $array['repair-steps']['uninstall'] = $array['repair-steps']['uninstall']['step'];
  129. }
  130. if (isset($array['background-jobs']['job']) && is_array($array['background-jobs']['job'])) {
  131. $array['background-jobs'] = $array['background-jobs']['job'];
  132. }
  133. return $array;
  134. }
  135. /**
  136. * @param \SimpleXMLElement $xml
  137. * @return array
  138. */
  139. function xmlToArray($xml) {
  140. if (!$xml->children()) {
  141. return (string)$xml;
  142. }
  143. $array = [];
  144. foreach ($xml->children() as $element => $node) {
  145. $totalElement = count($xml->{$element});
  146. if (!isset($array[$element])) {
  147. $array[$element] = $totalElement > 1 ? [] : "";
  148. }
  149. /** @var \SimpleXMLElement $node */
  150. // Has attributes
  151. if ($attributes = $node->attributes()) {
  152. $data = [
  153. '@attributes' => [],
  154. ];
  155. if (!count($node->children())){
  156. $value = (string)$node;
  157. if (!empty($value)) {
  158. $data['@value'] = (string)$node;
  159. }
  160. } else {
  161. $data = array_merge($data, $this->xmlToArray($node));
  162. }
  163. foreach ($attributes as $attr => $value) {
  164. $data['@attributes'][$attr] = (string)$value;
  165. }
  166. if ($totalElement > 1) {
  167. $array[$element][] = $data;
  168. } else {
  169. $array[$element] = $data;
  170. }
  171. // Just a value
  172. } else {
  173. if ($totalElement > 1) {
  174. $array[$element][] = $this->xmlToArray($node);
  175. } else {
  176. $array[$element] = $this->xmlToArray($node);
  177. }
  178. }
  179. }
  180. return $array;
  181. }
  182. private function isHTTPURL($url) {
  183. return stripos($url, 'https://') === 0 || stripos($url, 'http://') === 0;
  184. }
  185. }