Merge pull request #15816 from MiyaNoctem/dev-core-1383-fix-reinstallation-of-extensi...
[civicrm-core.git] / CRM / Utils / XML.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 * @package CRM
6b83d5bd 30 * @copyright CiviCRM LLC (c) 2004-2019
6a488035 31 */
6a488035
TO
32class CRM_Utils_XML {
33
34 /**
35 * Read a well-formed XML file
36 *
f4aaa82a
EM
37 * @param $file
38 *
a6c01b45
CW
39 * @return array
40 * (0 => SimpleXMLElement|FALSE, 1 => errorMessage|FALSE)
6a488035
TO
41 */
42 public static function parseFile($file) {
6714d8d2
SL
43 // SimpleXMLElement
44 $xml = FALSE;
45 // string
46 $error = FALSE;
6a488035 47
353ffa53 48 if (!file_exists($file)) {
6a488035 49 $error = 'File ' . $file . ' does not exist.';
0db6c3e1
TO
50 }
51 else {
6a488035
TO
52 $oldLibXMLErrors = libxml_use_internal_errors();
53 libxml_use_internal_errors(TRUE);
54
55 $xml = simplexml_load_file($file,
56 'SimpleXMLElement', LIBXML_NOCDATA
57 );
58 if ($xml === FALSE) {
59 $error = self::formatErrors(libxml_get_errors());
60 }
61
62 libxml_use_internal_errors($oldLibXMLErrors);
63 }
64
be2fb01f 65 return [$xml, $error];
6a488035
TO
66 }
67
68 /**
69 * Read a well-formed XML file
70 *
f4aaa82a
EM
71 * @param $string
72 *
a6c01b45
CW
73 * @return array
74 * (0 => SimpleXMLElement|FALSE, 1 => errorMessage|FALSE)
6a488035
TO
75 */
76 public static function parseString($string) {
6714d8d2
SL
77 // SimpleXMLElement
78 $xml = FALSE;
79 // string
80 $error = FALSE;
6a488035
TO
81
82 $oldLibXMLErrors = libxml_use_internal_errors();
83 libxml_use_internal_errors(TRUE);
84
85 $xml = simplexml_load_string($string,
86 'SimpleXMLElement', LIBXML_NOCDATA
87 );
88 if ($xml === FALSE) {
89 $error = self::formatErrors(libxml_get_errors());
90 }
91
92 libxml_use_internal_errors($oldLibXMLErrors);
93
be2fb01f 94 return [$xml, $error];
6a488035
TO
95 }
96
5bc392e6
EM
97 /**
98 * @param $errors
99 *
100 * @return string
101 */
6a488035 102 protected static function formatErrors($errors) {
be2fb01f 103 $messages = [];
6a488035
TO
104
105 foreach ($errors as $error) {
106 if ($error->level != LIBXML_ERR_ERROR && $error->level != LIBXML_ERR_FATAL) {
107 continue;
108 }
109
be2fb01f 110 $parts = [];
6a488035 111 if ($error->file) {
e7292422 112 $parts[] = "File=$error->file";
6a488035
TO
113 }
114 $parts[] = "Line=$error->line";
115 $parts[] = "Column=$error->column";
116 $parts[] = "Code=$error->code";
117
118 $messages[] = implode(" ", $parts) . ": " . trim($error->message);
119 }
120
121 return implode("\n", $messages);
122 }
123
124 /**
fe482240 125 * Convert an XML element to an array.
6a488035 126 *
77855840
TO
127 * @param $obj
128 * SimpleXMLElement.
f4aaa82a 129 *
6a488035
TO
130 * @return array
131 */
132 public static function xmlObjToArray($obj) {
be2fb01f 133 $arr = [];
6a488035
TO
134 if (is_object($obj)) {
135 $obj = get_object_vars($obj);
136 }
137 if (is_array($obj)) {
138 foreach ($obj as $i => $v) {
139 if (is_object($v) || is_array($v)) {
140 $v = self::xmlObjToArray($v);
141 }
142 if (empty($v)) {
143 $arr[$i] = NULL;
144 }
145 else {
146 $arr[$i] = $v;
147 }
148 }
149 }
150 return $arr;
151 }
96025800 152
6a488035 153}