Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
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 | |
8c9251b3 | 30 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 | 31 | */ |
6a488035 TO |
32 | class 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) { | |
43 | $xml = FALSE; // SimpleXMLElement | |
44 | $error = FALSE; // string | |
45 | ||
353ffa53 | 46 | if (!file_exists($file)) { |
6a488035 | 47 | $error = 'File ' . $file . ' does not exist.'; |
0db6c3e1 TO |
48 | } |
49 | else { | |
6a488035 TO |
50 | $oldLibXMLErrors = libxml_use_internal_errors(); |
51 | libxml_use_internal_errors(TRUE); | |
52 | ||
53 | $xml = simplexml_load_file($file, | |
54 | 'SimpleXMLElement', LIBXML_NOCDATA | |
55 | ); | |
56 | if ($xml === FALSE) { | |
57 | $error = self::formatErrors(libxml_get_errors()); | |
58 | } | |
59 | ||
60 | libxml_use_internal_errors($oldLibXMLErrors); | |
61 | } | |
62 | ||
63 | return array($xml, $error); | |
64 | } | |
65 | ||
66 | /** | |
67 | * Read a well-formed XML file | |
68 | * | |
f4aaa82a EM |
69 | * @param $string |
70 | * | |
a6c01b45 CW |
71 | * @return array |
72 | * (0 => SimpleXMLElement|FALSE, 1 => errorMessage|FALSE) | |
6a488035 TO |
73 | */ |
74 | public static function parseString($string) { | |
75 | $xml = FALSE; // SimpleXMLElement | |
76 | $error = FALSE; // string | |
77 | ||
78 | $oldLibXMLErrors = libxml_use_internal_errors(); | |
79 | libxml_use_internal_errors(TRUE); | |
80 | ||
81 | $xml = simplexml_load_string($string, | |
82 | 'SimpleXMLElement', LIBXML_NOCDATA | |
83 | ); | |
84 | if ($xml === FALSE) { | |
85 | $error = self::formatErrors(libxml_get_errors()); | |
86 | } | |
87 | ||
88 | libxml_use_internal_errors($oldLibXMLErrors); | |
89 | ||
90 | return array($xml, $error); | |
91 | } | |
92 | ||
5bc392e6 EM |
93 | /** |
94 | * @param $errors | |
95 | * | |
96 | * @return string | |
97 | */ | |
6a488035 TO |
98 | protected static function formatErrors($errors) { |
99 | $messages = array(); | |
100 | ||
101 | foreach ($errors as $error) { | |
102 | if ($error->level != LIBXML_ERR_ERROR && $error->level != LIBXML_ERR_FATAL) { | |
103 | continue; | |
104 | } | |
105 | ||
106 | $parts = array(); | |
107 | if ($error->file) { | |
e7292422 | 108 | $parts[] = "File=$error->file"; |
6a488035 TO |
109 | } |
110 | $parts[] = "Line=$error->line"; | |
111 | $parts[] = "Column=$error->column"; | |
112 | $parts[] = "Code=$error->code"; | |
113 | ||
114 | $messages[] = implode(" ", $parts) . ": " . trim($error->message); | |
115 | } | |
116 | ||
117 | return implode("\n", $messages); | |
118 | } | |
119 | ||
120 | /** | |
fe482240 | 121 | * Convert an XML element to an array. |
6a488035 | 122 | * |
77855840 TO |
123 | * @param $obj |
124 | * SimpleXMLElement. | |
f4aaa82a | 125 | * |
6a488035 TO |
126 | * @return array |
127 | */ | |
128 | public static function xmlObjToArray($obj) { | |
129 | $arr = array(); | |
130 | if (is_object($obj)) { | |
131 | $obj = get_object_vars($obj); | |
132 | } | |
133 | if (is_array($obj)) { | |
134 | foreach ($obj as $i => $v) { | |
135 | if (is_object($v) || is_array($v)) { | |
136 | $v = self::xmlObjToArray($v); | |
137 | } | |
138 | if (empty($v)) { | |
139 | $arr[$i] = NULL; | |
140 | } | |
141 | else { | |
142 | $arr[$i] = $v; | |
143 | } | |
144 | } | |
145 | } | |
146 | return $arr; | |
147 | } | |
96025800 | 148 | |
6a488035 | 149 | } |