<?php

define
('MAX_XML_SIZE',1524288);



function 
get_children($vals, &$i){
  
$children = array();     // Contains node data

  /* Node has CDATA before it's children */
  
if (isset($vals[$i]['value']))
    
$children['VALUE'] = $vals[$i]['value'];

  
/* Loop through children */
  
while (++$i count($vals))
  {
    switch (
$vals[$i]['type'])
    {
      
/* Node has CDATA after one of it's children
        (Add to cdata found before if this is the case) */
      
case 'cdata':
        if (isset(
$children['VALUE']))
          
$children['VALUE'] .= $vals[$i]['value'];
        else
          
$children['VALUE'] = $vals[$i]['value'];
        break;
      
/* At end of current branch */
      
case 'complete':
        if (isset(
$vals[$i]['attributes'])) {
          
$children[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
          
$index count($children[$vals[$i]['tag']])-1;

          if (isset(
$vals[$i]['value']))
            
$children[$vals[$i]['tag']][$index]['VALUE'] = $vals[$i]['value'];
          else
            
$children[$vals[$i]['tag']][$index]['VALUE'] = '';
        } else {
          if (isset(
$vals[$i]['value']))
            
$children[$vals[$i]['tag']][]['VALUE'] = $vals[$i]['value'];
          else
            
$children[$vals[$i]['tag']][]['VALUE'] = '';
        }
        break;
      
/* Node has more children */
      
case 'open':
        if (isset(
$vals[$i]['attributes'])) {
          
$children[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
          
$index count($children[$vals[$i]['tag']])-1;
          
$children[$vals[$i]['tag']][$index] = array_merge($children[$vals[$i]['tag']][$index],get_children($vals$i));
        } else {
          
$children[$vals[$i]['tag']][] = get_children($vals$i);
        }
        break;
      
/* End of node, return collected data */
      
case 'close':
        return 
$children;
    }
  }
}

/**
 * fonction qui renvoit tous les composants de l'xml
 *  $is_file est par defaut a true, s'il est à false alors $xmlloc est une chaine
 *
 */
function get_xml_tree($xmlloc,$is_file=true)
{
    if ( 
$is_file && file_exists($xmlloc)){
        
$data implode(''file($xmlloc));
    }elseif( !
$is_file ){
        
$data $xmlloc;
    }else{
        return 
FALSE;
    }

    
$parser xml_parser_create('ISO-8859-1');
    
xml_parser_set_option($parserXML_OPTION_SKIP_WHITE1);
    
xml_parser_set_option($parserXML_OPTION_CASE_FOLDING0);
    
xml_parse_into_struct($parser$data$vals$index);
    if(
$error_code=xml_get_error_code($parser)!=XML_ERROR_NONE){
        return 
FALSE;
    }
    
xml_parser_free($parser);

    
$tree = array();
    
$i 0;
    if( 
count($vals)==)    {
        return 
FALSE;
    }
    if (isset(
$vals[$i]['attributes'])){
        
$tree[$vals[$i]['tag']]['ATTRIBUTES'] = $vals[$i]['attributes'];
        
$tree[$vals[$i]['tag']][] = get_children($vals$i);
    }else
        
$tree[$vals[$i]['tag']][] = get_children($vals$i);

    return 
$tree;
}

function 
get_xml_tree_http($xmlloc){
    if( !( 
$fp = @fopen($xmlloc,'r')) ){
        return 
FALSE;
    }else
    {
        
$data "";
        while(
$html=fread($fpMAX_XML_SIZE)){
            
$data.=$html;
        }
        
fclose($fp);
    }

    
// nettoyage du fichier xml pour que le parser ne plante pas.
    
$data=str_replace('(View Source for full doctype...)','',$data);
    
$parser xml_parser_create('ISO-8859-1');
    
xml_parser_set_option($parserXML_OPTION_SKIP_WHITE1);
    
xml_parser_set_option($parserXML_OPTION_CASE_FOLDING0);
    
xml_parse_into_struct($parser$data$vals$index);
    if( 
$error_code=xml_get_error_code($parser)!=XML_ERROR_NONE )    {
        return 
FALSE;
    }
    
xml_parser_free($parser);

    
$tree = array();
    
$i 0;
    if(
count($vals)==0){
          return 
FALSE;
    }
    if (isset(
$vals[$i]['attributes'])){
        
$tree[$vals[$i]['tag']]['ATTRIBUTES'] = $vals[$i]['attributes'];
        
$tree[$vals[$i]['tag']][] = get_children($vals$i);
    }
    else
        
$tree[$vals[$i]['tag']][] = get_children($vals$i);

    return 
$tree;
}

function 
edit_time($time)
{
    if (
$time 100 && $time != 0)
        {
            
$time "1'";
        }
    else
        {
            if (
round(($time)/100) == 0)
                
$time "";
            else {
            
$time round(($time)/100) ."'";
            }
        }

    return 
$time;
}

?>