<?php
/**
 * EXPATH limited character encoding problem fix
 */
class modNavigationTree extends modNavigationTree_parent
{
    /**
     * Default EXPATH supported encodings
     * @var array
     */
    protected $_aSupportedExpathXmlEncodings = array( 'utf-8', 'utf-16', 'iso-8859-1', 'us-ascii' );

    /**
     * Loads data form XML file, and merges it with main oDomXML.
     *
     * @param string      $sMenuFile which file to load
     * @param DomDocument $oDom      where to load
     *
     * @return null
     */
    protected function _loadFromFile( $sMenuFile, $oDom )
    {
        $sXml = file_get_contents( $sMenuFile );

        // looking for non supported character encoding
        if ( preg_match( "/encoding\=(.*)\?\>/", $sXml, $aMatches ) !== 0 ) {
           if ( isset( $aMatches[1] ) ) {
               $sCurrEncoding = trim( $aMatches[1], "\"" );
               if ( !in_array( strtolower( $sCurrEncoding ), $this->_aSupportedExpathXmlEncodings ) ) {
                   $sXml = str_replace( $aMatches[1], "\"UTF-8\"", $sXml );
                   $sXml = iconv( $sCurrEncoding, "UTF-8", $sXml );
               } 
           }
        }        
        
        $oDomFile = new DomDocument();
        $oDomFile->preserveWhiteSpace = false;
        if ( @$oDomFile->loadXml( $sXml ) ) {
            $this->_merge( $oDomFile, $oDom );
        }
    }        
} 