1
0

InfoParser.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. /** @var \OCP\ICache|null */
  36. private $cache;
  37. /**
  38. * @param ICache|null $cache
  39. */
  40. public function __construct(ICache $cache = null) {
  41. $this->cache = $cache;
  42. }
  43. /**
  44. * @param string $file the xml file to be loaded
  45. * @return null|array where null is an indicator for an error
  46. */
  47. public function parse($file) {
  48. if (!file_exists($file)) {
  49. return null;
  50. }
  51. if ($this->cache !== null) {
  52. $fileCacheKey = $file . filemtime($file);
  53. if ($cachedValue = $this->cache->get($fileCacheKey)) {
  54. return json_decode($cachedValue, true);
  55. }
  56. }
  57. libxml_use_internal_errors(true);
  58. if ((PHP_VERSION_ID < 80000)) {
  59. $loadEntities = libxml_disable_entity_loader(false);
  60. $xml = simplexml_load_string(file_get_contents($file));
  61. libxml_disable_entity_loader($loadEntities);
  62. } else {
  63. $xml = simplexml_load_string(file_get_contents($file));
  64. }
  65. if ($xml === false) {
  66. libxml_clear_errors();
  67. return null;
  68. }
  69. $array = $this->xmlToArray($xml);
  70. if (is_null($array)) {
  71. return null;
  72. }
  73. if (!array_key_exists('info', $array)) {
  74. $array['info'] = [];
  75. }
  76. if (!array_key_exists('remote', $array)) {
  77. $array['remote'] = [];
  78. }
  79. if (!array_key_exists('public', $array)) {
  80. $array['public'] = [];
  81. }
  82. if (!array_key_exists('types', $array)) {
  83. $array['types'] = [];
  84. }
  85. if (!array_key_exists('repair-steps', $array)) {
  86. $array['repair-steps'] = [];
  87. }
  88. if (!array_key_exists('install', $array['repair-steps'])) {
  89. $array['repair-steps']['install'] = [];
  90. }
  91. if (!array_key_exists('pre-migration', $array['repair-steps'])) {
  92. $array['repair-steps']['pre-migration'] = [];
  93. }
  94. if (!array_key_exists('post-migration', $array['repair-steps'])) {
  95. $array['repair-steps']['post-migration'] = [];
  96. }
  97. if (!array_key_exists('live-migration', $array['repair-steps'])) {
  98. $array['repair-steps']['live-migration'] = [];
  99. }
  100. if (!array_key_exists('uninstall', $array['repair-steps'])) {
  101. $array['repair-steps']['uninstall'] = [];
  102. }
  103. if (!array_key_exists('background-jobs', $array)) {
  104. $array['background-jobs'] = [];
  105. }
  106. if (!array_key_exists('two-factor-providers', $array)) {
  107. $array['two-factor-providers'] = [];
  108. }
  109. if (!array_key_exists('commands', $array)) {
  110. $array['commands'] = [];
  111. }
  112. if (!array_key_exists('activity', $array)) {
  113. $array['activity'] = [];
  114. }
  115. if (!array_key_exists('filters', $array['activity'])) {
  116. $array['activity']['filters'] = [];
  117. }
  118. if (!array_key_exists('settings', $array['activity'])) {
  119. $array['activity']['settings'] = [];
  120. }
  121. if (!array_key_exists('providers', $array['activity'])) {
  122. $array['activity']['providers'] = [];
  123. }
  124. if (!array_key_exists('settings', $array)) {
  125. $array['settings'] = [];
  126. }
  127. if (!array_key_exists('admin', $array['settings'])) {
  128. $array['settings']['admin'] = [];
  129. }
  130. if (!array_key_exists('admin-section', $array['settings'])) {
  131. $array['settings']['admin-section'] = [];
  132. }
  133. if (!array_key_exists('personal', $array['settings'])) {
  134. $array['settings']['personal'] = [];
  135. }
  136. if (!array_key_exists('personal-section', $array['settings'])) {
  137. $array['settings']['personal-section'] = [];
  138. }
  139. if (array_key_exists('types', $array)) {
  140. if (is_array($array['types'])) {
  141. foreach ($array['types'] as $type => $v) {
  142. unset($array['types'][$type]);
  143. if (is_string($type)) {
  144. $array['types'][] = $type;
  145. }
  146. }
  147. } else {
  148. $array['types'] = [];
  149. }
  150. }
  151. if (isset($array['repair-steps']['install']['step']) && is_array($array['repair-steps']['install']['step'])) {
  152. $array['repair-steps']['install'] = $array['repair-steps']['install']['step'];
  153. }
  154. if (isset($array['repair-steps']['pre-migration']['step']) && is_array($array['repair-steps']['pre-migration']['step'])) {
  155. $array['repair-steps']['pre-migration'] = $array['repair-steps']['pre-migration']['step'];
  156. }
  157. if (isset($array['repair-steps']['post-migration']['step']) && is_array($array['repair-steps']['post-migration']['step'])) {
  158. $array['repair-steps']['post-migration'] = $array['repair-steps']['post-migration']['step'];
  159. }
  160. if (isset($array['repair-steps']['live-migration']['step']) && is_array($array['repair-steps']['live-migration']['step'])) {
  161. $array['repair-steps']['live-migration'] = $array['repair-steps']['live-migration']['step'];
  162. }
  163. if (isset($array['repair-steps']['uninstall']['step']) && is_array($array['repair-steps']['uninstall']['step'])) {
  164. $array['repair-steps']['uninstall'] = $array['repair-steps']['uninstall']['step'];
  165. }
  166. if (isset($array['background-jobs']['job']) && is_array($array['background-jobs']['job'])) {
  167. $array['background-jobs'] = $array['background-jobs']['job'];
  168. }
  169. if (isset($array['commands']['command']) && is_array($array['commands']['command'])) {
  170. $array['commands'] = $array['commands']['command'];
  171. }
  172. if (isset($array['two-factor-providers']['provider']) && is_array($array['two-factor-providers']['provider'])) {
  173. $array['two-factor-providers'] = $array['two-factor-providers']['provider'];
  174. }
  175. if (isset($array['activity']['filters']['filter']) && is_array($array['activity']['filters']['filter'])) {
  176. $array['activity']['filters'] = $array['activity']['filters']['filter'];
  177. }
  178. if (isset($array['activity']['settings']['setting']) && is_array($array['activity']['settings']['setting'])) {
  179. $array['activity']['settings'] = $array['activity']['settings']['setting'];
  180. }
  181. if (isset($array['activity']['providers']['provider']) && is_array($array['activity']['providers']['provider'])) {
  182. $array['activity']['providers'] = $array['activity']['providers']['provider'];
  183. }
  184. if (isset($array['collaboration']['collaborators']['searchPlugins']['searchPlugin'])
  185. && is_array($array['collaboration']['collaborators']['searchPlugins']['searchPlugin'])
  186. && !isset($array['collaboration']['collaborators']['searchPlugins']['searchPlugin']['class'])
  187. ) {
  188. $array['collaboration']['collaborators']['searchPlugins'] = $array['collaboration']['collaborators']['searchPlugins']['searchPlugin'];
  189. }
  190. if (isset($array['settings']['admin']) && !is_array($array['settings']['admin'])) {
  191. $array['settings']['admin'] = [$array['settings']['admin']];
  192. }
  193. if (isset($array['settings']['admin-section']) && !is_array($array['settings']['admin-section'])) {
  194. $array['settings']['admin-section'] = [$array['settings']['admin-section']];
  195. }
  196. if (isset($array['settings']['personal']) && !is_array($array['settings']['personal'])) {
  197. $array['settings']['personal'] = [$array['settings']['personal']];
  198. }
  199. if (isset($array['settings']['personal-section']) && !is_array($array['settings']['personal-section'])) {
  200. $array['settings']['personal-section'] = [$array['settings']['personal-section']];
  201. }
  202. if (isset($array['navigations']['navigation']) && $this->isNavigationItem($array['navigations']['navigation'])) {
  203. $array['navigations']['navigation'] = [$array['navigations']['navigation']];
  204. }
  205. if ($this->cache !== null) {
  206. $this->cache->set($fileCacheKey, json_encode($array));
  207. }
  208. return $array;
  209. }
  210. /**
  211. * @param $data
  212. * @return bool
  213. */
  214. private function isNavigationItem($data): bool {
  215. // Allow settings navigation items with no route entry
  216. $type = $data['type'] ?? 'link';
  217. if ($type === 'settings') {
  218. return isset($data['name']);
  219. }
  220. return isset($data['name'], $data['route']);
  221. }
  222. /**
  223. * @param \SimpleXMLElement $xml
  224. * @return array
  225. */
  226. public function xmlToArray($xml) {
  227. if (!$xml->children()) {
  228. return (string)$xml;
  229. }
  230. $array = [];
  231. foreach ($xml->children() as $element => $node) {
  232. $totalElement = count($xml->{$element});
  233. if (!isset($array[$element])) {
  234. $array[$element] = $totalElement > 1 ? [] : "";
  235. }
  236. /** @var \SimpleXMLElement $node */
  237. // Has attributes
  238. if ($attributes = $node->attributes()) {
  239. $data = [
  240. '@attributes' => [],
  241. ];
  242. if (!count($node->children())) {
  243. $value = (string)$node;
  244. if (!empty($value)) {
  245. $data['@value'] = $value;
  246. }
  247. } else {
  248. $data = array_merge($data, $this->xmlToArray($node));
  249. }
  250. foreach ($attributes as $attr => $value) {
  251. $data['@attributes'][$attr] = (string)$value;
  252. }
  253. if ($totalElement > 1) {
  254. $array[$element][] = $data;
  255. } else {
  256. $array[$element] = $data;
  257. }
  258. // Just a value
  259. } else {
  260. if ($totalElement > 1) {
  261. $array[$element][] = $this->xmlToArray($node);
  262. } else {
  263. $array[$element] = $this->xmlToArray($node);
  264. }
  265. }
  266. }
  267. return $array;
  268. }
  269. }