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.

278 lines
9.3 KiB

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