InfoParser.php 9.3 KB

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