TYPO3 CMS  TYPO3_7-6
AbstractXmlParser.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
21 
26 {
30  protected $sourcePath;
31 
35  protected $languageKey;
36 
40  protected $charset;
41 
52  {
53  $this->sourcePath = $sourcePath;
54  $this->languageKey = $languageKey;
55  $this->charset = $this->getCharset($languageKey, $charset);
56  if ($this->languageKey !== 'default') {
57  $this->sourcePath = GeneralUtility::getFileAbsFileName(GeneralUtility::llXmlAutoFileName($this->sourcePath, $this->languageKey));
58  if (!@is_file($this->sourcePath)) {
59  // Global localization is not available, try split localization file
61  }
62  if (!@is_file($this->sourcePath)) {
63  throw new FileNotFoundException('Localization file does not exist', 1306332397);
64  }
65  }
66  $LOCAL_LANG = [];
67  $LOCAL_LANG[$languageKey] = $this->parseXmlFile();
68  return $LOCAL_LANG;
69  }
70 
78  protected function getCharset($languageKey, $charset = '')
79  {
81  if (is_object($GLOBALS['LANG'])) {
82  $charsetConverter = $GLOBALS['LANG']->csConvObj;
83  } elseif (is_object($GLOBALS['TSFE'])) {
84  $charsetConverter = $GLOBALS['TSFE']->csConvObj;
85  } else {
86  $charsetConverter = GeneralUtility::makeInstance(CharsetConverter::class);
87  }
88  if ($charset !== '') {
89  $targetCharset = $charsetConverter->parse_charset($charset);
90  } else {
91  $targetCharset = 'utf-8';
92  }
93  return $targetCharset;
94  }
95 
102  protected function parseXmlFile()
103  {
104  $xmlContent = file_get_contents($this->sourcePath);
105  // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
106  $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
107  $rootXmlNode = simplexml_load_string($xmlContent, 'SimpleXMLElement', LIBXML_NOWARNING);
108  libxml_disable_entity_loader($previousValueOfEntityLoader);
109  if (!isset($rootXmlNode) || $rootXmlNode === false) {
110  $xmlError = libxml_get_last_error();
111  throw new InvalidXmlFileException(
112  'The path provided does not point to existing and accessible well-formed XML file. Reason: ' . $xmlError->message . ' in ' . $this->sourcePath . ', line ' . $xmlError->line,
113  1278155988
114  );
115  }
116  return $this->doParsingFromRoot($rootXmlNode);
117  }
118 
125  abstract protected function doParsingFromRoot(\SimpleXMLElement $root);
126 }
static llXmlAutoFileName($fileRef, $language, $sameLocation=false)
getParsedData($sourcePath, $languageKey, $charset='')
static getFileAbsFileName($filename, $onlyRelative=true, $relToTYPO3_mainDir=false)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']