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