Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * @package CRM | |
06b69b18 | 30 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
31 | * $Id$ |
32 | */ | |
33 | ||
34 | class CRM_Utils_XML { | |
35 | ||
36 | /** | |
37 | * Read a well-formed XML file | |
38 | * | |
39 | * @return array (0 => SimpleXMLElement|FALSE, 1 => errorMessage|FALSE) | |
40 | */ | |
41 | public static function parseFile($file) { | |
42 | $xml = FALSE; // SimpleXMLElement | |
43 | $error = FALSE; // string | |
44 | ||
45 | if (! file_exists($file)) { | |
46 | $error = 'File ' . $file . ' does not exist.'; | |
47 | } else { | |
48 | $oldLibXMLErrors = libxml_use_internal_errors(); | |
49 | libxml_use_internal_errors(TRUE); | |
50 | ||
51 | $xml = simplexml_load_file($file, | |
52 | 'SimpleXMLElement', LIBXML_NOCDATA | |
53 | ); | |
54 | if ($xml === FALSE) { | |
55 | $error = self::formatErrors(libxml_get_errors()); | |
56 | } | |
57 | ||
58 | libxml_use_internal_errors($oldLibXMLErrors); | |
59 | } | |
60 | ||
61 | return array($xml, $error); | |
62 | } | |
63 | ||
64 | /** | |
65 | * Read a well-formed XML file | |
66 | * | |
67 | * @return array (0 => SimpleXMLElement|FALSE, 1 => errorMessage|FALSE) | |
68 | */ | |
69 | public static function parseString($string) { | |
70 | $xml = FALSE; // SimpleXMLElement | |
71 | $error = FALSE; // string | |
72 | ||
73 | $oldLibXMLErrors = libxml_use_internal_errors(); | |
74 | libxml_use_internal_errors(TRUE); | |
75 | ||
76 | $xml = simplexml_load_string($string, | |
77 | 'SimpleXMLElement', LIBXML_NOCDATA | |
78 | ); | |
79 | if ($xml === FALSE) { | |
80 | $error = self::formatErrors(libxml_get_errors()); | |
81 | } | |
82 | ||
83 | libxml_use_internal_errors($oldLibXMLErrors); | |
84 | ||
85 | return array($xml, $error); | |
86 | } | |
87 | ||
88 | protected static function formatErrors($errors) { | |
89 | $messages = array(); | |
90 | ||
91 | foreach ($errors as $error) { | |
92 | if ($error->level != LIBXML_ERR_ERROR && $error->level != LIBXML_ERR_FATAL) { | |
93 | continue; | |
94 | } | |
95 | ||
96 | $parts = array(); | |
97 | if ($error->file) { | |
98 | $parts[] = "File=$error->file"; | |
99 | } | |
100 | $parts[] = "Line=$error->line"; | |
101 | $parts[] = "Column=$error->column"; | |
102 | $parts[] = "Code=$error->code"; | |
103 | ||
104 | $messages[] = implode(" ", $parts) . ": " . trim($error->message); | |
105 | } | |
106 | ||
107 | return implode("\n", $messages); | |
108 | } | |
109 | ||
110 | /** | |
111 | * Convert an XML element to an array | |
112 | * | |
113 | * @pararm $obj SimpleXMLElement | |
114 | * @return array | |
115 | */ | |
116 | public static function xmlObjToArray($obj) { | |
117 | $arr = array(); | |
118 | if (is_object($obj)) { | |
119 | $obj = get_object_vars($obj); | |
120 | } | |
121 | if (is_array($obj)) { | |
122 | foreach ($obj as $i => $v) { | |
123 | if (is_object($v) || is_array($v)) { | |
124 | $v = self::xmlObjToArray($v); | |
125 | } | |
126 | if (empty($v)) { | |
127 | $arr[$i] = NULL; | |
128 | } | |
129 | else { | |
130 | $arr[$i] = $v; | |
131 | } | |
132 | } | |
133 | } | |
134 | return $arr; | |
135 | } | |
136 | } |