1
0

InfoParser.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2016, Lukas Reschke <lukas@statuscode.ch>
  5. *
  6. * @author Andreas Fischer <bantu@owncloud.com>
  7. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OC\App;
  31. use OCP\ICache;
  32. use function libxml_disable_entity_loader;
  33. use function simplexml_load_string;
  34. class InfoParser {
  35. /**
  36. * @param ICache|null $cache
  37. */
  38. public function __construct(
  39. private ?ICache $cache = null,
  40. ) {
  41. }
  42. /**
  43. * @param string $file the xml file to be loaded
  44. * @return null|array where null is an indicator for an error
  45. */
  46. public function parse($file) {
  47. if (!file_exists($file)) {
  48. return null;
  49. }
  50. if ($this->cache !== null) {
  51. $fileCacheKey = $file . filemtime($file);
  52. if ($cachedValue = $this->cache->get($fileCacheKey)) {
  53. return json_decode($cachedValue, true);
  54. }
  55. }
  56. libxml_use_internal_errors(true);
  57. if ((PHP_VERSION_ID < 80000)) {
  58. $loadEntities = libxml_disable_entity_loader(false);
  59. $xml = simplexml_load_string(file_get_contents($file));
  60. libxml_disable_entity_loader($loadEntities);
  61. } else {
  62. $xml = simplexml_load_string(file_get_contents($file));
  63. }
  64. if ($xml === false) {
  65. libxml_clear_errors();
  66. return null;
  67. }
  68. $array = $this->xmlToArray($xml);
  69. if (is_null($array)) {
  70. return null;
  71. }
  72. if (!array_key_exists('info', $array)) {
  73. $array['info'] = [];
  74. }
  75. if (!array_key_exists('remote', $array)) {
  76. $array['remote'] = [];
  77. }
  78. if (!array_key_exists('public', $array)) {
  79. $array['public'] = [];
  80. }
  81. if (!array_key_exists('types', $array)) {
  82. $array['types'] = [];
  83. }
  84. if (!array_key_exists('repair-steps', $array)) {
  85. $array['repair-steps'] = [];
  86. }
  87. if (!array_key_exists('install', $array['repair-steps'])) {
  88. $array['repair-steps']['install'] = [];
  89. }
  90. if (!array_key_exists('pre-migration', $array['repair-steps'])) {
  91. $array['repair-steps']['pre-migration'] = [];
  92. }
  93. if (!array_key_exists('post-migration', $array['repair-steps'])) {
  94. $array['repair-steps']['post-migration'] = [];
  95. }
  96. if (!array_key_exists('live-migration', $array['repair-steps'])) {
  97. $array['repair-steps']['live-migration'] = [];
  98. }
  99. if (!array_key_exists('uninstall', $array['repair-steps'])) {
  100. $array['repair-steps']['uninstall'] = [];
  101. }
  102. if (!array_key_exists('background-jobs', $array)) {
  103. $array['background-jobs'] = [];
  104. }
  105. if (!array_key_exists('two-factor-providers', $array)) {
  106. $array['two-factor-providers'] = [];
  107. }
  108. if (!array_key_exists('commands', $array)) {
  109. $array['commands'] = [];
  110. }
  111. if (!array_key_exists('activity', $array)) {
  112. $array['activity'] = [];
  113. }
  114. if (!array_key_exists('filters', $array['activity'])) {
  115. $array['activity']['filters'] = [];
  116. }
  117. if (!array_key_exists('settings', $array['activity'])) {
  118. $array['activity']['settings'] = [];
  119. }
  120. if (!array_key_exists('providers', $array['activity'])) {
  121. $array['activity']['providers'] = [];
  122. }
  123. if (!array_key_exists('settings', $array)) {
  124. $array['settings'] = [];
  125. }
  126. if (!array_key_exists('admin', $array['settings'])) {
  127. $array['settings']['admin'] = [];
  128. }
  129. if (!array_key_exists('admin-section', $array['settings'])) {
  130. $array['settings']['admin-section'] = [];
  131. }
  132. if (!array_key_exists('personal', $array['settings'])) {
  133. $array['settings']['personal'] = [];
  134. }
  135. if (!array_key_exists('personal-section', $array['settings'])) {
  136. $array['settings']['personal-section'] = [];
  137. }
  138. if (array_key_exists('types', $array)) {
  139. if (is_array($array['types'])) {
  140. foreach ($array['types'] as $type => $v) {
  141. unset($array['types'][$type]);
  142. if (is_string($type)) {
  143. $array['types'][] = $type;
  144. }
  145. }
  146. } else {
  147. $array['types'] = [];
  148. }
  149. }
  150. if (isset($array['repair-steps']['install']['step']) && is_array($array['repair-steps']['install']['step'])) {
  151. $array['repair-steps']['install'] = $array['repair-steps']['install']['step'];
  152. }
  153. if (isset($array['repair-steps']['pre-migration']['step']) && is_array($array['repair-steps']['pre-migration']['step'])) {
  154. $array['repair-steps']['pre-migration'] = $array['repair-steps']['pre-migration']['step'];
  155. }
  156. if (isset($array['repair-steps']['post-migration']['step']) && is_array($array['repair-steps']['post-migration']['step'])) {
  157. $array['repair-steps']['post-migration'] = $array['repair-steps']['post-migration']['step'];
  158. }
  159. if (isset($array['repair-steps']['live-migration']['step']) && is_array($array['repair-steps']['live-migration']['step'])) {
  160. $array['repair-steps']['live-migration'] = $array['repair-steps']['live-migration']['step'];
  161. }
  162. if (isset($array['repair-steps']['uninstall']['step']) && is_array($array['repair-steps']['uninstall']['step'])) {
  163. $array['repair-steps']['uninstall'] = $array['repair-steps']['uninstall']['step'];
  164. }
  165. if (isset($array['background-jobs']['job']) && is_array($array['background-jobs']['job'])) {
  166. $array['background-jobs'] = $array['background-jobs']['job'];
  167. }
  168. if (isset($array['commands']['command']) && is_array($array['commands']['command'])) {
  169. $array['commands'] = $array['commands']['command'];
  170. }
  171. if (isset($array['two-factor-providers']['provider']) && is_array($array['two-factor-providers']['provider'])) {
  172. $array['two-factor-providers'] = $array['two-factor-providers']['provider'];
  173. }
  174. if (isset($array['activity']['filters']['filter']) && is_array($array['activity']['filters']['filter'])) {
  175. $array['activity']['filters'] = $array['activity']['filters']['filter'];
  176. }
  177. if (isset($array['activity']['settings']['setting']) && is_array($array['activity']['settings']['setting'])) {
  178. $array['activity']['settings'] = $array['activity']['settings']['setting'];
  179. }
  180. if (isset($array['activity']['providers']['provider']) && is_array($array['activity']['providers']['provider'])) {
  181. $array['activity']['providers'] = $array['activity']['providers']['provider'];
  182. }
  183. if (isset($array['collaboration']['collaborators']['searchPlugins']['searchPlugin'])
  184. && is_array($array['collaboration']['collaborators']['searchPlugins']['searchPlugin'])
  185. && !isset($array['collaboration']['collaborators']['searchPlugins']['searchPlugin']['class'])
  186. ) {
  187. $array['collaboration']['collaborators']['searchPlugins'] = $array['collaboration']['collaborators']['searchPlugins']['searchPlugin'];
  188. }
  189. if (isset($array['settings']['admin']) && !is_array($array['settings']['admin'])) {
  190. $array['settings']['admin'] = [$array['settings']['admin']];
  191. }
  192. if (isset($array['settings']['admin-section']) && !is_array($array['settings']['admin-section'])) {
  193. $array['settings']['admin-section'] = [$array['settings']['admin-section']];
  194. }
  195. if (isset($array['settings']['personal']) && !is_array($array['settings']['personal'])) {
  196. $array['settings']['personal'] = [$array['settings']['personal']];
  197. }
  198. if (isset($array['settings']['personal-section']) && !is_array($array['settings']['personal-section'])) {
  199. $array['settings']['personal-section'] = [$array['settings']['personal-section']];
  200. }
  201. if (isset($array['navigations']['navigation']) && $this->isNavigationItem($array['navigations']['navigation'])) {
  202. $array['navigations']['navigation'] = [$array['navigations']['navigation']];
  203. }
  204. if ($this->cache !== null) {
  205. $this->cache->set($fileCacheKey, json_encode($array));
  206. }
  207. return $array;
  208. }
  209. /**
  210. * @param $data
  211. * @return bool
  212. */
  213. private function isNavigationItem($data): bool {
  214. // Allow settings navigation items with no route entry
  215. $type = $data['type'] ?? 'link';
  216. if ($type === 'settings') {
  217. return isset($data['name']);
  218. }
  219. return isset($data['name'], $data['route']);
  220. }
  221. /**
  222. * @param \SimpleXMLElement $xml
  223. * @return array
  224. */
  225. public function xmlToArray($xml) {
  226. if (!$xml->children()) {
  227. return (string)$xml;
  228. }
  229. $array = [];
  230. foreach ($xml->children() as $element => $node) {
  231. $totalElement = count($xml->{$element});
  232. if (!isset($array[$element])) {
  233. $array[$element] = $totalElement > 1 ? [] : "";
  234. }
  235. /** @var \SimpleXMLElement $node */
  236. // Has attributes
  237. if ($attributes = $node->attributes()) {
  238. $data = [
  239. '@attributes' => [],
  240. ];
  241. if (!count($node->children())) {
  242. $value = (string)$node;
  243. if (!empty($value)) {
  244. $data['@value'] = $value;
  245. }
  246. } else {
  247. $data = array_merge($data, $this->xmlToArray($node));
  248. }
  249. foreach ($attributes as $attr => $value) {
  250. $data['@attributes'][$attr] = (string)$value;
  251. }
  252. if ($totalElement > 1) {
  253. $array[$element][] = $data;
  254. } else {
  255. $array[$element] = $data;
  256. }
  257. // Just a value
  258. } else {
  259. if ($totalElement > 1) {
  260. $array[$element][] = $this->xmlToArray($node);
  261. } else {
  262. $array[$element] = $this->xmlToArray($node);
  263. }
  264. }
  265. }
  266. return $array;
  267. }
  268. }