You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InfoParser.php 9.3KB

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