InfoParser.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\App;
  8. use OCP\ICache;
  9. use function simplexml_load_string;
  10. class InfoParser {
  11. /**
  12. * @param ICache|null $cache
  13. */
  14. public function __construct(
  15. private ?ICache $cache = null,
  16. ) {
  17. }
  18. /**
  19. * @param string $file the xml file to be loaded
  20. * @return null|array where null is an indicator for an error
  21. */
  22. public function parse($file) {
  23. if (!file_exists($file)) {
  24. return null;
  25. }
  26. if ($this->cache !== null) {
  27. $fileCacheKey = $file . filemtime($file);
  28. if ($cachedValue = $this->cache->get($fileCacheKey)) {
  29. return json_decode($cachedValue, true);
  30. }
  31. }
  32. libxml_use_internal_errors(true);
  33. $xml = simplexml_load_string(file_get_contents($file));
  34. if ($xml === false) {
  35. libxml_clear_errors();
  36. return null;
  37. }
  38. $array = $this->xmlToArray($xml);
  39. if (is_null($array)) {
  40. return null;
  41. }
  42. if (!array_key_exists('info', $array)) {
  43. $array['info'] = [];
  44. }
  45. if (!array_key_exists('remote', $array)) {
  46. $array['remote'] = [];
  47. }
  48. if (!array_key_exists('public', $array)) {
  49. $array['public'] = [];
  50. }
  51. if (!array_key_exists('types', $array)) {
  52. $array['types'] = [];
  53. }
  54. if (!array_key_exists('repair-steps', $array)) {
  55. $array['repair-steps'] = [];
  56. }
  57. if (!array_key_exists('install', $array['repair-steps'])) {
  58. $array['repair-steps']['install'] = [];
  59. }
  60. if (!array_key_exists('pre-migration', $array['repair-steps'])) {
  61. $array['repair-steps']['pre-migration'] = [];
  62. }
  63. if (!array_key_exists('post-migration', $array['repair-steps'])) {
  64. $array['repair-steps']['post-migration'] = [];
  65. }
  66. if (!array_key_exists('live-migration', $array['repair-steps'])) {
  67. $array['repair-steps']['live-migration'] = [];
  68. }
  69. if (!array_key_exists('uninstall', $array['repair-steps'])) {
  70. $array['repair-steps']['uninstall'] = [];
  71. }
  72. if (!array_key_exists('background-jobs', $array)) {
  73. $array['background-jobs'] = [];
  74. }
  75. if (!array_key_exists('two-factor-providers', $array)) {
  76. $array['two-factor-providers'] = [];
  77. }
  78. if (!array_key_exists('commands', $array)) {
  79. $array['commands'] = [];
  80. }
  81. if (!array_key_exists('activity', $array)) {
  82. $array['activity'] = [];
  83. }
  84. if (!array_key_exists('filters', $array['activity'])) {
  85. $array['activity']['filters'] = [];
  86. }
  87. if (!array_key_exists('settings', $array['activity'])) {
  88. $array['activity']['settings'] = [];
  89. }
  90. if (!array_key_exists('providers', $array['activity'])) {
  91. $array['activity']['providers'] = [];
  92. }
  93. if (!array_key_exists('settings', $array)) {
  94. $array['settings'] = [];
  95. }
  96. if (!array_key_exists('admin', $array['settings'])) {
  97. $array['settings']['admin'] = [];
  98. }
  99. if (!array_key_exists('admin-section', $array['settings'])) {
  100. $array['settings']['admin-section'] = [];
  101. }
  102. if (!array_key_exists('personal', $array['settings'])) {
  103. $array['settings']['personal'] = [];
  104. }
  105. if (!array_key_exists('personal-section', $array['settings'])) {
  106. $array['settings']['personal-section'] = [];
  107. }
  108. if (array_key_exists('types', $array)) {
  109. if (is_array($array['types'])) {
  110. foreach ($array['types'] as $type => $v) {
  111. unset($array['types'][$type]);
  112. if (is_string($type)) {
  113. $array['types'][] = $type;
  114. }
  115. }
  116. } else {
  117. $array['types'] = [];
  118. }
  119. }
  120. if (isset($array['repair-steps']['install']['step']) && is_array($array['repair-steps']['install']['step'])) {
  121. $array['repair-steps']['install'] = $array['repair-steps']['install']['step'];
  122. }
  123. if (isset($array['repair-steps']['pre-migration']['step']) && is_array($array['repair-steps']['pre-migration']['step'])) {
  124. $array['repair-steps']['pre-migration'] = $array['repair-steps']['pre-migration']['step'];
  125. }
  126. if (isset($array['repair-steps']['post-migration']['step']) && is_array($array['repair-steps']['post-migration']['step'])) {
  127. $array['repair-steps']['post-migration'] = $array['repair-steps']['post-migration']['step'];
  128. }
  129. if (isset($array['repair-steps']['live-migration']['step']) && is_array($array['repair-steps']['live-migration']['step'])) {
  130. $array['repair-steps']['live-migration'] = $array['repair-steps']['live-migration']['step'];
  131. }
  132. if (isset($array['repair-steps']['uninstall']['step']) && is_array($array['repair-steps']['uninstall']['step'])) {
  133. $array['repair-steps']['uninstall'] = $array['repair-steps']['uninstall']['step'];
  134. }
  135. if (isset($array['background-jobs']['job']) && is_array($array['background-jobs']['job'])) {
  136. $array['background-jobs'] = $array['background-jobs']['job'];
  137. }
  138. if (isset($array['commands']['command']) && is_array($array['commands']['command'])) {
  139. $array['commands'] = $array['commands']['command'];
  140. }
  141. if (isset($array['two-factor-providers']['provider']) && is_array($array['two-factor-providers']['provider'])) {
  142. $array['two-factor-providers'] = $array['two-factor-providers']['provider'];
  143. }
  144. if (isset($array['activity']['filters']['filter']) && is_array($array['activity']['filters']['filter'])) {
  145. $array['activity']['filters'] = $array['activity']['filters']['filter'];
  146. }
  147. if (isset($array['activity']['settings']['setting']) && is_array($array['activity']['settings']['setting'])) {
  148. $array['activity']['settings'] = $array['activity']['settings']['setting'];
  149. }
  150. if (isset($array['activity']['providers']['provider']) && is_array($array['activity']['providers']['provider'])) {
  151. $array['activity']['providers'] = $array['activity']['providers']['provider'];
  152. }
  153. if (isset($array['collaboration']['collaborators']['searchPlugins']['searchPlugin'])
  154. && is_array($array['collaboration']['collaborators']['searchPlugins']['searchPlugin'])
  155. && !isset($array['collaboration']['collaborators']['searchPlugins']['searchPlugin']['class'])
  156. ) {
  157. $array['collaboration']['collaborators']['searchPlugins'] = $array['collaboration']['collaborators']['searchPlugins']['searchPlugin'];
  158. }
  159. if (isset($array['settings']['admin']) && !is_array($array['settings']['admin'])) {
  160. $array['settings']['admin'] = [$array['settings']['admin']];
  161. }
  162. if (isset($array['settings']['admin-section']) && !is_array($array['settings']['admin-section'])) {
  163. $array['settings']['admin-section'] = [$array['settings']['admin-section']];
  164. }
  165. if (isset($array['settings']['personal']) && !is_array($array['settings']['personal'])) {
  166. $array['settings']['personal'] = [$array['settings']['personal']];
  167. }
  168. if (isset($array['settings']['personal-section']) && !is_array($array['settings']['personal-section'])) {
  169. $array['settings']['personal-section'] = [$array['settings']['personal-section']];
  170. }
  171. if (isset($array['navigations']['navigation']) && $this->isNavigationItem($array['navigations']['navigation'])) {
  172. $array['navigations']['navigation'] = [$array['navigations']['navigation']];
  173. }
  174. if ($this->cache !== null) {
  175. $this->cache->set($fileCacheKey, json_encode($array));
  176. }
  177. return $array;
  178. }
  179. /**
  180. * @param $data
  181. * @return bool
  182. */
  183. private function isNavigationItem($data): bool {
  184. // Allow settings navigation items with no route entry
  185. $type = $data['type'] ?? 'link';
  186. if ($type === 'settings') {
  187. return isset($data['name']);
  188. }
  189. return isset($data['name'], $data['route']);
  190. }
  191. /**
  192. * @param \SimpleXMLElement $xml
  193. * @return array
  194. */
  195. public function xmlToArray($xml) {
  196. if (!$xml->children()) {
  197. return (string)$xml;
  198. }
  199. $array = [];
  200. foreach ($xml->children() as $element => $node) {
  201. $totalElement = count($xml->{$element});
  202. if (!isset($array[$element])) {
  203. $array[$element] = $totalElement > 1 ? [] : "";
  204. }
  205. /** @var \SimpleXMLElement $node */
  206. // Has attributes
  207. if ($attributes = $node->attributes()) {
  208. $data = [
  209. '@attributes' => [],
  210. ];
  211. if (!count($node->children())) {
  212. $value = (string)$node;
  213. if (!empty($value)) {
  214. $data['@value'] = $value;
  215. }
  216. } else {
  217. $data = array_merge($data, $this->xmlToArray($node));
  218. }
  219. foreach ($attributes as $attr => $value) {
  220. $data['@attributes'][$attr] = (string)$value;
  221. }
  222. if ($totalElement > 1) {
  223. $array[$element][] = $data;
  224. } else {
  225. $array[$element] = $data;
  226. }
  227. // Just a value
  228. } else {
  229. if ($totalElement > 1) {
  230. $array[$element][] = $this->xmlToArray($node);
  231. } else {
  232. $array[$element] = $this->xmlToArray($node);
  233. }
  234. }
  235. }
  236. return $array;
  237. }
  238. }