InfoParser.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 Christoph Wurst <christoph@owncloud.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\App;
  28. use OCP\ICache;
  29. class InfoParser {
  30. /** @var \OCP\ICache|null */
  31. private $cache;
  32. /**
  33. * @param ICache|null $cache
  34. */
  35. public function __construct(ICache $cache = null) {
  36. $this->cache = $cache;
  37. }
  38. /**
  39. * @param string $file the xml file to be loaded
  40. * @return null|array where null is an indicator for an error
  41. */
  42. public function parse($file) {
  43. if (!file_exists($file)) {
  44. return null;
  45. }
  46. if(!is_null($this->cache)) {
  47. $fileCacheKey = $file . filemtime($file);
  48. if ($cachedValue = $this->cache->get($fileCacheKey)) {
  49. return json_decode($cachedValue, true);
  50. }
  51. }
  52. libxml_use_internal_errors(true);
  53. $loadEntities = libxml_disable_entity_loader(false);
  54. $xml = simplexml_load_file($file);
  55. libxml_disable_entity_loader($loadEntities);
  56. if ($xml === false) {
  57. libxml_clear_errors();
  58. return null;
  59. }
  60. $array = $this->xmlToArray($xml);
  61. if (is_null($array)) {
  62. return null;
  63. }
  64. if (!array_key_exists('info', $array)) {
  65. $array['info'] = [];
  66. }
  67. if (!array_key_exists('remote', $array)) {
  68. $array['remote'] = [];
  69. }
  70. if (!array_key_exists('public', $array)) {
  71. $array['public'] = [];
  72. }
  73. if (!array_key_exists('types', $array)) {
  74. $array['types'] = [];
  75. }
  76. if (!array_key_exists('repair-steps', $array)) {
  77. $array['repair-steps'] = [];
  78. }
  79. if (!array_key_exists('install', $array['repair-steps'])) {
  80. $array['repair-steps']['install'] = [];
  81. }
  82. if (!array_key_exists('pre-migration', $array['repair-steps'])) {
  83. $array['repair-steps']['pre-migration'] = [];
  84. }
  85. if (!array_key_exists('post-migration', $array['repair-steps'])) {
  86. $array['repair-steps']['post-migration'] = [];
  87. }
  88. if (!array_key_exists('live-migration', $array['repair-steps'])) {
  89. $array['repair-steps']['live-migration'] = [];
  90. }
  91. if (!array_key_exists('uninstall', $array['repair-steps'])) {
  92. $array['repair-steps']['uninstall'] = [];
  93. }
  94. if (!array_key_exists('background-jobs', $array)) {
  95. $array['background-jobs'] = [];
  96. }
  97. if (!array_key_exists('two-factor-providers', $array)) {
  98. $array['two-factor-providers'] = [];
  99. }
  100. if (!array_key_exists('commands', $array)) {
  101. $array['commands'] = [];
  102. }
  103. if (!array_key_exists('activity', $array)) {
  104. $array['activity'] = [];
  105. }
  106. if (!array_key_exists('filters', $array['activity'])) {
  107. $array['activity']['filters'] = [];
  108. }
  109. if (!array_key_exists('settings', $array['activity'])) {
  110. $array['activity']['settings'] = [];
  111. }
  112. if (!array_key_exists('providers', $array['activity'])) {
  113. $array['activity']['providers'] = [];
  114. }
  115. if (array_key_exists('types', $array)) {
  116. if (is_array($array['types'])) {
  117. foreach ($array['types'] as $type => $v) {
  118. unset($array['types'][$type]);
  119. if (is_string($type)) {
  120. $array['types'][] = $type;
  121. }
  122. }
  123. } else {
  124. $array['types'] = [];
  125. }
  126. }
  127. if (isset($array['repair-steps']['install']['step']) && is_array($array['repair-steps']['install']['step'])) {
  128. $array['repair-steps']['install'] = $array['repair-steps']['install']['step'];
  129. }
  130. if (isset($array['repair-steps']['pre-migration']['step']) && is_array($array['repair-steps']['pre-migration']['step'])) {
  131. $array['repair-steps']['pre-migration'] = $array['repair-steps']['pre-migration']['step'];
  132. }
  133. if (isset($array['repair-steps']['post-migration']['step']) && is_array($array['repair-steps']['post-migration']['step'])) {
  134. $array['repair-steps']['post-migration'] = $array['repair-steps']['post-migration']['step'];
  135. }
  136. if (isset($array['repair-steps']['live-migration']['step']) && is_array($array['repair-steps']['live-migration']['step'])) {
  137. $array['repair-steps']['live-migration'] = $array['repair-steps']['live-migration']['step'];
  138. }
  139. if (isset($array['repair-steps']['uninstall']['step']) && is_array($array['repair-steps']['uninstall']['step'])) {
  140. $array['repair-steps']['uninstall'] = $array['repair-steps']['uninstall']['step'];
  141. }
  142. if (isset($array['background-jobs']['job']) && is_array($array['background-jobs']['job'])) {
  143. $array['background-jobs'] = $array['background-jobs']['job'];
  144. }
  145. if (isset($array['commands']['command']) && is_array($array['commands']['command'])) {
  146. $array['commands'] = $array['commands']['command'];
  147. }
  148. if (isset($array['activity']['filters']['filter']) && is_array($array['activity']['filters']['filter'])) {
  149. $array['activity']['filters'] = $array['activity']['filters']['filter'];
  150. }
  151. if (isset($array['activity']['settings']['setting']) && is_array($array['activity']['settings']['setting'])) {
  152. $array['activity']['settings'] = $array['activity']['settings']['setting'];
  153. }
  154. if (isset($array['activity']['providers']['provider']) && is_array($array['activity']['providers']['provider'])) {
  155. $array['activity']['providers'] = $array['activity']['providers']['provider'];
  156. }
  157. if(!is_null($this->cache)) {
  158. $this->cache->set($fileCacheKey, json_encode($array));
  159. }
  160. return $array;
  161. }
  162. /**
  163. * @param \SimpleXMLElement $xml
  164. * @return array
  165. */
  166. function xmlToArray($xml) {
  167. if (!$xml->children()) {
  168. return (string)$xml;
  169. }
  170. $array = [];
  171. foreach ($xml->children() as $element => $node) {
  172. $totalElement = count($xml->{$element});
  173. if (!isset($array[$element])) {
  174. $array[$element] = $totalElement > 1 ? [] : "";
  175. }
  176. /** @var \SimpleXMLElement $node */
  177. // Has attributes
  178. if ($attributes = $node->attributes()) {
  179. $data = [
  180. '@attributes' => [],
  181. ];
  182. if (!count($node->children())){
  183. $value = (string)$node;
  184. if (!empty($value)) {
  185. $data['@value'] = (string)$node;
  186. }
  187. } else {
  188. $data = array_merge($data, $this->xmlToArray($node));
  189. }
  190. foreach ($attributes as $attr => $value) {
  191. $data['@attributes'][$attr] = (string)$value;
  192. }
  193. if ($totalElement > 1) {
  194. $array[$element][] = $data;
  195. } else {
  196. $array[$element] = $data;
  197. }
  198. // Just a value
  199. } else {
  200. if ($totalElement > 1) {
  201. $array[$element][] = $this->xmlToArray($node);
  202. } else {
  203. $array[$element] = $this->xmlToArray($node);
  204. }
  205. }
  206. }
  207. return $array;
  208. }
  209. }