InfoParser.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. // Ensure some fields are always arrays
  184. if (isset($array['screenshot']) && !is_array($array['screenshot'])) {
  185. $array['screenshot'] = [$array['screenshot']];
  186. }
  187. if (isset($array['author']) && !is_array($array['author'])) {
  188. $array['author'] = [$array['author']];
  189. }
  190. if (isset($array['category']) && !is_array($array['category'])) {
  191. $array['category'] = [$array['category']];
  192. }
  193. if ($this->cache !== null) {
  194. $this->cache->set($fileCacheKey, json_encode($array));
  195. }
  196. return $array;
  197. }
  198. /**
  199. * @param $data
  200. * @return bool
  201. */
  202. private function isNavigationItem($data): bool {
  203. // Allow settings navigation items with no route entry
  204. $type = $data['type'] ?? 'link';
  205. if ($type === 'settings') {
  206. return isset($data['name']);
  207. }
  208. return isset($data['name'], $data['route']);
  209. }
  210. /**
  211. * @param \SimpleXMLElement $xml
  212. * @return array
  213. */
  214. public function xmlToArray($xml) {
  215. if (!$xml->children()) {
  216. return (string)$xml;
  217. }
  218. $array = [];
  219. foreach ($xml->children() as $element => $node) {
  220. $totalElement = count($xml->{$element});
  221. if (!isset($array[$element])) {
  222. $array[$element] = $totalElement > 1 ? [] : '';
  223. }
  224. /** @var \SimpleXMLElement $node */
  225. // Has attributes
  226. if ($attributes = $node->attributes()) {
  227. $data = [
  228. '@attributes' => [],
  229. ];
  230. if (!count($node->children())) {
  231. $value = (string)$node;
  232. if (!empty($value)) {
  233. $data['@value'] = $value;
  234. }
  235. } else {
  236. $data = array_merge($data, $this->xmlToArray($node));
  237. }
  238. foreach ($attributes as $attr => $value) {
  239. $data['@attributes'][$attr] = (string)$value;
  240. }
  241. if ($totalElement > 1) {
  242. $array[$element][] = $data;
  243. } else {
  244. $array[$element] = $data;
  245. }
  246. // Just a value
  247. } else {
  248. if ($totalElement > 1) {
  249. $array[$element][] = $this->xmlToArray($node);
  250. } else {
  251. $array[$element] = $this->xmlToArray($node);
  252. }
  253. }
  254. }
  255. return $array;
  256. }
  257. }