InfoParser.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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('dependencies', $array)) {
  109. $array['dependencies'] = [];
  110. }
  111. if (!array_key_exists('backend', $array['dependencies'])) {
  112. $array['dependencies']['backend'] = [];
  113. }
  114. if (array_key_exists('types', $array)) {
  115. if (is_array($array['types'])) {
  116. foreach ($array['types'] as $type => $v) {
  117. unset($array['types'][$type]);
  118. if (is_string($type)) {
  119. $array['types'][] = $type;
  120. }
  121. }
  122. } else {
  123. $array['types'] = [];
  124. }
  125. }
  126. if (isset($array['repair-steps']['install']['step']) && is_array($array['repair-steps']['install']['step'])) {
  127. $array['repair-steps']['install'] = $array['repair-steps']['install']['step'];
  128. }
  129. if (isset($array['repair-steps']['pre-migration']['step']) && is_array($array['repair-steps']['pre-migration']['step'])) {
  130. $array['repair-steps']['pre-migration'] = $array['repair-steps']['pre-migration']['step'];
  131. }
  132. if (isset($array['repair-steps']['post-migration']['step']) && is_array($array['repair-steps']['post-migration']['step'])) {
  133. $array['repair-steps']['post-migration'] = $array['repair-steps']['post-migration']['step'];
  134. }
  135. if (isset($array['repair-steps']['live-migration']['step']) && is_array($array['repair-steps']['live-migration']['step'])) {
  136. $array['repair-steps']['live-migration'] = $array['repair-steps']['live-migration']['step'];
  137. }
  138. if (isset($array['repair-steps']['uninstall']['step']) && is_array($array['repair-steps']['uninstall']['step'])) {
  139. $array['repair-steps']['uninstall'] = $array['repair-steps']['uninstall']['step'];
  140. }
  141. if (isset($array['background-jobs']['job']) && is_array($array['background-jobs']['job'])) {
  142. $array['background-jobs'] = $array['background-jobs']['job'];
  143. }
  144. if (isset($array['commands']['command']) && is_array($array['commands']['command'])) {
  145. $array['commands'] = $array['commands']['command'];
  146. }
  147. if (isset($array['two-factor-providers']['provider']) && is_array($array['two-factor-providers']['provider'])) {
  148. $array['two-factor-providers'] = $array['two-factor-providers']['provider'];
  149. }
  150. if (isset($array['activity']['filters']['filter']) && is_array($array['activity']['filters']['filter'])) {
  151. $array['activity']['filters'] = $array['activity']['filters']['filter'];
  152. }
  153. if (isset($array['activity']['settings']['setting']) && is_array($array['activity']['settings']['setting'])) {
  154. $array['activity']['settings'] = $array['activity']['settings']['setting'];
  155. }
  156. if (isset($array['activity']['providers']['provider']) && is_array($array['activity']['providers']['provider'])) {
  157. $array['activity']['providers'] = $array['activity']['providers']['provider'];
  158. }
  159. if (isset($array['collaboration']['collaborators']['searchPlugins']['searchPlugin'])
  160. && is_array($array['collaboration']['collaborators']['searchPlugins']['searchPlugin'])
  161. && !isset($array['collaboration']['collaborators']['searchPlugins']['searchPlugin']['class'])
  162. ) {
  163. $array['collaboration']['collaborators']['searchPlugins'] = $array['collaboration']['collaborators']['searchPlugins']['searchPlugin'];
  164. }
  165. if (isset($array['settings']['admin']) && !is_array($array['settings']['admin'])) {
  166. $array['settings']['admin'] = [$array['settings']['admin']];
  167. }
  168. if (isset($array['settings']['admin-section']) && !is_array($array['settings']['admin-section'])) {
  169. $array['settings']['admin-section'] = [$array['settings']['admin-section']];
  170. }
  171. if (isset($array['settings']['personal']) && !is_array($array['settings']['personal'])) {
  172. $array['settings']['personal'] = [$array['settings']['personal']];
  173. }
  174. if (isset($array['settings']['personal-section']) && !is_array($array['settings']['personal-section'])) {
  175. $array['settings']['personal-section'] = [$array['settings']['personal-section']];
  176. }
  177. if (isset($array['navigations']['navigation']) && $this->isNavigationItem($array['navigations']['navigation'])) {
  178. $array['navigations']['navigation'] = [$array['navigations']['navigation']];
  179. }
  180. if (isset($array['dependencies']['backend']) && !is_array($array['dependencies']['backend'])) {
  181. $array['dependencies']['backend'] = [$array['dependencies']['backend']];
  182. }
  183. if ($this->cache !== null) {
  184. $this->cache->set($fileCacheKey, json_encode($array));
  185. }
  186. return $array;
  187. }
  188. /**
  189. * @param $data
  190. * @return bool
  191. */
  192. private function isNavigationItem($data): bool {
  193. // Allow settings navigation items with no route entry
  194. $type = $data['type'] ?? 'link';
  195. if ($type === 'settings') {
  196. return isset($data['name']);
  197. }
  198. return isset($data['name'], $data['route']);
  199. }
  200. /**
  201. * @param \SimpleXMLElement $xml
  202. * @return array
  203. */
  204. public function xmlToArray($xml) {
  205. if (!$xml->children()) {
  206. return (string)$xml;
  207. }
  208. $array = [];
  209. foreach ($xml->children() as $element => $node) {
  210. $totalElement = count($xml->{$element});
  211. if (!isset($array[$element])) {
  212. $array[$element] = $totalElement > 1 ? [] : "";
  213. }
  214. /** @var \SimpleXMLElement $node */
  215. // Has attributes
  216. if ($attributes = $node->attributes()) {
  217. $data = [
  218. '@attributes' => [],
  219. ];
  220. if (!count($node->children())) {
  221. $value = (string)$node;
  222. if (!empty($value)) {
  223. $data['@value'] = $value;
  224. }
  225. } else {
  226. $data = array_merge($data, $this->xmlToArray($node));
  227. }
  228. foreach ($attributes as $attr => $value) {
  229. $data['@attributes'][$attr] = (string)$value;
  230. }
  231. if ($totalElement > 1) {
  232. $array[$element][] = $data;
  233. } else {
  234. $array[$element] = $data;
  235. }
  236. // Just a value
  237. } else {
  238. if ($totalElement > 1) {
  239. $array[$element][] = $this->xmlToArray($node);
  240. } else {
  241. $array[$element] = $this->xmlToArray($node);
  242. }
  243. }
  244. }
  245. return $array;
  246. }
  247. }