GlisWeb framework
_xml.tools.php
Vai alla documentazione di questo file.
1 <?php
2 
20  function array2xml( $data, $file = false, &$xml = NULL ) {
21 
22  // debug
23  // print_r( $data );
24 
25  // TODO questa funzione è da rifare con i seguenti obiettivi:
26  // 1) gestire in maniera trasparente l'array generato da xml2array
27  // 2) gestire correttamente i namespace senza l'accrocchio del pipe
28  // 3) mantenere la compatibilità con gli script che usano attualmente questa funzione
29 
30  if( $xml === NULL ) {
31  $dtk = array_keys( $data );
32  $root = array_shift( $dtk );
33  $xml = new SimpleXMLElement( '<?xml version="1.0" encoding="utf-8"?><' . $root . '></' . $root . '>' );
34 # $xml->registerXPathNamespace( 'xhtml', 'http://www.w3.org/1999/xhtml' );
35  array2xml( array_shift( $data ), $file, $xml );
36  }
37 
38  foreach( $data as $key => $value ) {
39 
40  $key = str_replace( ':', '|', $key );
41 
42  if( is_array( $value ) ) {
43 
44  $keys = array_keys( $value );
45 
46  if( $key === '@' ) {
47 
48  foreach( $value as $attrName => $attrVal ) {
49 # $xml[ $attrName ] = $attrVal;
50 # if( strpos( $attrName, ':' ) ) {
51 # $xml->addAttribute( $attrName, $attrVal, substr( $attrName, 0, strpos( $attrName, ':' ) ) );
52 # } else {
53  $attrName = str_replace( ':', '|', $attrName );
54  $xml->addAttribute( $attrName, $attrVal );
55 # }
56  }
57 
58  } elseif( array_shift( $keys ) === '#' ) {
59 
60  $node = $xml->addChild( $key, $value['#'] );
61 
62  } elseif( is_numeric( array_shift( $keys ) ) ) {
63 
64  foreach( $value as $item ) {
65 
66  if( strpos( $key, ':' ) ) {
67  $node = $xml->addChild( $key, NULL, substr( $key, 0, strpos( $key, ':' ) ) );
68 # $node = $xml->addChild( $key, NULL, 'http://www.w3.org/1999/xhtml' );
69 # $node = $xml->addChild( 'link', NULL, 'xhtml' );
70 # $node = $xml->addChild( 'link', NULL, 'http://www.w3.org/1999/xhtml' );
71  } else {
72  $node = $xml->addChild( $key );
73  }
74  array2xml( $item, NULL, $node );
75  }
76 
77  } else {
78 
79  if( strpos( $key, ':' ) ) {
80 # $node = $xml->addChild( $key, NULL, 'http://www.w3.org/1999/xhtml' );
81  } else {
82  $node = $xml->addChild( $key );
83  }
84 # $node = $xml->addChild( $key );
85  array2xml( $value, NULL, $node );
86 
87  }
88 
89  } else {
90 
91  if( substr( $key, 0, 1 ) == '@' ) {
92  $xml[ substr( $key, 1 ) ] = $value;
93  } elseif( strpos( $key, ':' ) ) {
94 # $xml->addChild( $key, htmlspecialchars( $value ), substr( $key, strpos( $key, ':' ) + 1 ) );
95  } else {
96  $xml->addChild( $key, htmlspecialchars( $value ) );
97  }
98 
99  }
100 
101  }
102 
103  if( $file !== NULL ) {
104 
105  // echo str_replace('|',':',$xml->asXML());
106 
107  $domxml = new DOMDocument('1.0');
108  $domxml->preserveWhiteSpace = false;
109  $domxml->formatOutput = true;
110  $domxml->loadXML( str_replace('|',':',$xml->asXML()) );
111 
112  if( $file === false ) {
113  return $domxml->saveXML();
114  } elseif( is_writeable( dirname( DIRECTORY_BASE . $file ) ) ) {
115  return $domxml->save( DIRECTORY_BASE . $file );
116  } else {
117  logWrite( 'impossibile scrivere il file xml, permessi insufficienti', 'filesystem', LOG_ERR );
118  }
119 
120  }
121 
122  }
123 
129 // function xml2array( $file, $get_attributes = true, $priority = 'tag' ) {
130  function xml2array( $contents, $get_attributes = true, $priority = 'tag' ) {
131 
132  // TODO valutare se questa funzione va bene o se è da rifare in base a quanto
133  // detto per array2xml()
134 
135  // $contents = readFromFile( $file, READ_FILE_AS_STRING );
136 
137  // logWrite( 'codifica contenuto: ' . mb_detect_encoding( $contenuto ), 'xml', LOG_DEBUG );
138 
139  if( ! $contents ) {
140  return array();
141  }
142 
143  if( ! function_exists('xml_parser_create')) {
144  logWrite( 'la funzione xml_parser_create() non esiste', 'xml', LOG_FATAL );
145  return array();
146  }
147 
148  $parser = xml_parser_create( '' );
149  xml_parser_set_option( $parser, XML_OPTION_TARGET_ENCODING, 'UTF-8' );
150  xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, 0 );
151  xml_parser_set_option( $parser, XML_OPTION_SKIP_WHITE, 1 );
152  xml_parse_into_struct( $parser, trim( $contents ), $xml_values );
153  xml_parser_free( $parser );
154 
155  $err = xml_get_error_code( $parser );
156 
157  if( ! empty( $err ) ) {
158  logWrite( 'errore #'.xml_get_error_code( $parser ).' ('.xml_error_string( $err ).')', 'xml', LOG_ERR );
159  }
160 
161  if( ! $xml_values ) {
162  return array();
163  } else {
164  logWrite( print_r( $xml_values, true ), 'xml', LOG_DEBUG );
165  }
166 
167  $xml_array = array();
168  $parents = array();
169  $opened_tags = array();
170  $arr = array();
171 
172  $current =& $xml_array;
173 
174  $repeated_tag_index = array();
175 
176  foreach( $xml_values as $data ) {
177 
178  unset( $attributes, $value );
179  extract( $data );
180 
181  $result = array();
182  $attributes_data = array();
183 
184  if( isset( $value ) ) {
185  if( $priority == 'tag' ) {
186  $result['#'] = $value;
187  } else {
188  $result = $value;
189  }
190  }
191 
192  if( isset( $attributes ) and $get_attributes ) {
193  foreach( $attributes as $attr => $val ) {
194  if( $priority == 'tag' ) {
195  $result['@'][$attr] = $val;
196  } else {
197  $attributes_data[$attr] = $val;
198  }
199  }
200 
201  }
202 
203  if( $type == "open" ) {
204 
205  $parent[$level-1] =& $current;
206 
207  if( ! is_array( $current ) || ( ! in_array( $tag, array_keys( $current ) ) ) ) {
208 
209  $current[$tag] = $result;
210 
211  if( $attributes_data ) {
212  $current[$tag. '_attr'] = $attributes_data;
213  }
214 
215  $repeated_tag_index[$tag.'_'.$level] = 1;
216  $current =& $current[$tag];
217 
218  } else {
219 
220  if( isset( $current[$tag][0] ) ) {
221 
222  $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
223 
224  if($attributes_data) {
225  $current[$tag][$repeated_tag_index[$tag.'_'.$level].'_attr'] = $attributes_data;
226  }
227 
228  $repeated_tag_index[$tag.'_'.$level]++;
229 
230  } else {
231 
232  $current[$tag] = array( $current[$tag], $result );
233  $repeated_tag_index[$tag.'_'.$level] = 2;
234 
235  if( isset( $current[$tag.'_attr'] ) ) {
236  $current[$tag]['0_attr'] = $current[$tag.'_attr'];
237  unset($current[$tag.'_attr']);
238  }
239 
240  if($attributes_data) {
241  $current[$tag]['1_attr'] = $attributes_data;
242  }
243 
244  }
245 
246  $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
247  $current =& $current[$tag][$last_item_index];
248 
249  }
250 
251  } elseif( $type == "complete" ) {
252 
253  if( ! isset( $current[$tag] ) ) {
254 
255  $current[$tag] = $result;
256  $repeated_tag_index[$tag.'_'.$level] = 1;
257 
258  if( $priority == 'attribute' && $attributes_data ) {
259  $current[$tag. '_attr'] = $attributes_data;
260  }
261 
262  } else {
263 
264  if( isset( $current[$tag][0] ) && is_array( $current[$tag] ) ) {
265 
266  $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
267 
268  if( $priority == 'tag' && $get_attributes && $attributes_data) {
269  $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
270  }
271 
272  $repeated_tag_index[$tag.'_'.$level]++;
273 
274  } else {
275 
276  $current[$tag] = array( $current[$tag], $result );
277  $repeated_tag_index[$tag.'_'.$level] = 1;
278 
279  if($priority == 'tag' && $get_attributes) {
280 
281  if( isset( $current[$tag.'_attr'] ) ) {
282  $current[$tag]['0_attr'] = $current[$tag.'_attr'];
283  unset($current[$tag.'_attr']);
284  }
285 
286  if( $attributes_data ) {
287  $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
288  }
289 
290  }
291 
292  $repeated_tag_index[$tag.'_'.$level]++;
293 
294  }
295 
296  }
297 
298  } elseif( $type == 'close' ) {
299 
300  $current =& $parent[$level-1];
301 
302  }
303 
304  }
305 
306  return( $xml_array );
307 
308  }
309 
315  if( ! function_exists( 'xmlEntities' ) ) {
316  function xmlEntities( $t ) {
317  $t = iconv( 'UTF-8', 'ASCII//TRANSLIT//IGNORE', $t );
318  $t = str_replace( '€', 'EURO', $t );
319  $t = str_replace( ',', '.', $t );
320 // $t = str_replace( 'ù', 'u', $t );
321  return $t;
322  }
323  }
324 
330  if( ! function_exists( 'xmlFloat' ) ) {
331  function xmlFloat( $t ) {
332  $t = str_replace( ',', '.', sprintf( '%01.2f', $t ) );
333  return $t;
334  }
335  }
336 
337 ?>
logWrite( $m, $f='site', $l=LOG_NOTICE, $d=DIRECTORY_LOG, $t=CURRENT_LOG_LEVEL, $s=SITE_STATUS)
scrive un messaggio nei log del sito
Definition: _log.utils.php:48
array2xml( $data, $file=false, &$xml=NULL)
Definition: _xml.tools.php:20
$file
Definition: _osm.php:32
const DIRECTORY_BASE
Definition: _osm.php:3
xml2array( $contents, $get_attributes=true, $priority='tag')
Definition: _xml.tools.php:130
if(! isset( $_REQUEST['__view__'][ $ct['view']['id']]['__extra__']['assegnato'])|| $_REQUEST['__view__'][ $ct['view']['id']]['__extra__']['assegnato']=='__me__') elseif($_REQUEST[ '__view__'][$ct[ 'view'][ 'id']][ '__extra__'][ 'assegnato']=='__nessuno__')
$parent