TYPO3 CMS  TYPO3_7-6
LocallangArrayParser.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 
20 
28 {
32  protected $cacheFileName;
33 
37  protected $csConvObj;
38 
42  protected $hashSource;
43 
47  protected $sourceCharset;
48 
52  protected $targetCharset;
53 
59  public function __construct()
60  {
62  $this->createCsConvObject();
63  }
64 
74  public function getParsedData($sourcePath, $languageKey, $charset = '')
75  {
76  $this->validateParameters($sourcePath, $languageKey);
77  $this->setCharsets($languageKey, $charset);
78  $this->generateCacheFileName($sourcePath, $languageKey);
79  if (!file_exists($this->cacheFileName)) {
80  $LOCAL_LANG = $this->generateCacheFile($sourcePath, $languageKey);
81  } else {
82  $LOCAL_LANG = $this->getContentFromCacheFile();
83  }
84  $xliff = $this->convertToXLIFF($LOCAL_LANG);
85  return $xliff;
86  }
87 
94  protected function convertToXLIFF(array $LOCAL_LANG)
95  {
96  foreach ($LOCAL_LANG as &$keysLabels) {
97  foreach ($keysLabels as &$label) {
98  $label = [
99  0 => [
100  'target' => $label
101  ]
102  ];
103  }
104  unset($label);
105  }
106  return $LOCAL_LANG;
107  }
108 
114  protected function createCsConvObject()
115  {
116  if (is_object($GLOBALS['LANG'])) {
117  $this->csConvObj = $GLOBALS['LANG']->csConvObj;
118  } elseif (is_object($GLOBALS['TSFE'])) {
119  $this->csConvObj = $GLOBALS['TSFE']->csConvObj;
120  } else {
121  $this->csConvObj = GeneralUtility::makeInstance(CharsetConverter::class);
122  }
123  }
124 
133  protected function generateCacheFile($sourcePath, $languageKey)
134  {
135  $LOCAL_LANG = [];
136  // Get PHP data
137  include $sourcePath;
138  if (!is_array($LOCAL_LANG)) {
139  $fileName = PathUtility::stripPathSitePrefix($sourcePath);
140  throw new \RuntimeException('TYPO3 Fatal Error: "' . $fileName . '" is no TYPO3 language file!', 1308898491);
141  }
142  // Converting the default language (English)
143  // This needs to be done for a few accented loan words and extension names
144  if (is_array($LOCAL_LANG['default']) && $this->targetCharset !== 'utf-8') {
145  foreach ($LOCAL_LANG['default'] as &$labelValue) {
146  $labelValue = $this->csConvObj->conv($labelValue, 'utf-8', $this->targetCharset);
147  }
148  unset($labelValue);
149  }
150  if ($languageKey !== 'default' && is_array($LOCAL_LANG[$languageKey]) && $this->sourceCharset !== $this->targetCharset) {
151  foreach ($LOCAL_LANG[$languageKey] as &$labelValue) {
152  $labelValue = $this->csConvObj->conv($labelValue, $this->sourceCharset, $this->targetCharset);
153  }
154  unset($labelValue);
155  }
156  // Cache the content now:
157  if (isset($LOCAL_LANG[$languageKey])) {
158  $serContent = ['origFile' => $this->hashSource, 'LOCAL_LANG' => ['default' => $LOCAL_LANG['default'], $languageKey => $LOCAL_LANG[$languageKey]]];
159  } else {
160  $serContent = ['origFile' => $this->hashSource, 'LOCAL_LANG' => ['default' => $LOCAL_LANG['default']]];
161  }
162  $res = GeneralUtility::writeFileToTypo3tempDir($this->cacheFileName, serialize($serContent));
163  if ($res) {
164  throw new \RuntimeException('TYPO3 Fatal Error: "' . $res, 1308898501);
165  }
166  return $LOCAL_LANG;
167  }
168 
176  protected function generateCacheFileName($sourcePath, $languageKey)
177  {
178  $this->hashSource = PathUtility::stripPathSitePrefix($sourcePath) . '|' . date('d-m-Y H:i:s', filemtime($sourcePath)) . '|version=2.3';
179  $this->cacheFileName = PATH_site . 'typo3temp/llxml/' . substr(basename($sourcePath), 10, 15) . '_' . GeneralUtility::shortMD5($this->hashSource) . '.' . $languageKey . '.' . $this->targetCharset . '.cache';
180  }
181 
187  protected function getContentFromCacheFile()
188  {
189  $serContent = (array)unserialize(file_get_contents($this->cacheFileName));
190  $LOCAL_LANG = $serContent['LOCAL_LANG'];
191  return (array)$LOCAL_LANG;
192  }
193 
200  protected function isWithinWebRoot($fileName)
201  {
202  return (bool)GeneralUtility::getFileAbsFileName($fileName);
203  }
204 
212  protected function setCharsets($languageKey, $charset)
213  {
214  $this->sourceCharset = $this->csConvObj->parse_charset($this->csConvObj->charSetArray[$languageKey] ?: 'utf-8');
215  if ($charset) {
216  $this->targetCharset = $this->csConvObj->parse_charset($charset);
217  } else {
218  $this->targetCharset = 'utf-8';
219  }
220  }
221 
230  protected function validateParameters($sourcePath, $languageKey)
231  {
232  if (!$this->isWithinWebRoot($sourcePath) || !@is_file($sourcePath) || !$languageKey) {
233  throw new \RuntimeException(sprintf('Invalid source path (%s) or languageKey (%s)', $sourcePath, $languageKey), 1309245002);
234  }
235  }
236 }
static writeFileToTypo3tempDir($filepath, $content)
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']