TYPO3 CMS  TYPO3_6-2
LocallangArrayParser.php
Go to the documentation of this file.
1 <?php
3 
23 
27  protected $cacheFileName;
28 
32  protected $csConvObj;
33 
37  protected $hashSource;
38 
42  protected $sourceCharset;
43 
47  protected $targetCharset;
48 
54  public function __construct() {
55  $this->createCsConvObject();
56  }
57 
67  public function getParsedData($sourcePath, $languageKey, $charset = '') {
68  $this->validateParameters($sourcePath, $languageKey);
69  $this->setCharsets($languageKey, $charset);
70  $this->generateCacheFileName($sourcePath, $languageKey);
71  if (!file_exists($this->cacheFileName)) {
72  $LOCAL_LANG = $this->generateCacheFile($sourcePath, $languageKey);
73  } else {
75  }
76  $xliff = $this->convertToXLIFF($LOCAL_LANG);
77  return $xliff;
78  }
79 
86  protected function convertToXLIFF(array $LOCAL_LANG) {
87  foreach ($LOCAL_LANG as &$keysLabels) {
88  foreach ($keysLabels as &$label) {
89  $label = array(
90  0 => array(
91  'target' => $label
92  )
93  );
94  }
95  unset($label);
96  }
97  return $LOCAL_LANG;
98  }
99 
105  protected function createCsConvObject() {
106  if (is_object($GLOBALS['LANG'])) {
107  $this->csConvObj = $GLOBALS['LANG']->csConvObj;
108  } elseif (is_object($GLOBALS['TSFE'])) {
109  $this->csConvObj = $GLOBALS['TSFE']->csConvObj;
110  } else {
111  $this->csConvObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Charset\\CharsetConverter');
112  }
113  }
114 
123  protected function generateCacheFile($sourcePath, $languageKey) {
124  $LOCAL_LANG = array();
125  // Get PHP data
126  include $sourcePath;
127  if (!is_array($LOCAL_LANG)) {
129  throw new \RuntimeException('TYPO3 Fatal Error: "' . $fileName . '" is no TYPO3 language file!', 1308898491);
130  }
131  // Converting the default language (English)
132  // This needs to be done for a few accented loan words and extension names
133  if (is_array($LOCAL_LANG['default']) && $this->targetCharset !== 'utf-8') {
134  foreach ($LOCAL_LANG['default'] as &$labelValue) {
135  $labelValue = $this->csConvObj->conv($labelValue, 'utf-8', $this->targetCharset);
136  }
137  unset($labelValue);
138  }
139  if ($languageKey !== 'default' && is_array($LOCAL_LANG[$languageKey]) && $this->sourceCharset != $this->targetCharset) {
140  foreach ($LOCAL_LANG[$languageKey] as &$labelValue) {
141  $labelValue = $this->csConvObj->conv($labelValue, $this->sourceCharset, $this->targetCharset);
142  }
143  unset($labelValue);
144  }
145  // Cache the content now:
146  if (isset($LOCAL_LANG[$languageKey])) {
147  $serContent = array('origFile' => $this->hashSource, 'LOCAL_LANG' => array('default' => $LOCAL_LANG['default'], $languageKey => $LOCAL_LANG[$languageKey]));
148  } else {
149  $serContent = array('origFile' => $this->hashSource, 'LOCAL_LANG' => array('default' => $LOCAL_LANG['default']));
150  }
151  $res = \TYPO3\CMS\Core\Utility\GeneralUtility::writeFileToTypo3tempDir($this->cacheFileName, serialize($serContent));
152  if ($res) {
153  throw new \RuntimeException('TYPO3 Fatal Error: "' . $res, 1308898501);
154  }
155  return $LOCAL_LANG;
156  }
157 
165  protected function generateCacheFileName($sourcePath, $languageKey) {
166  $this->hashSource = \TYPO3\CMS\Core\Utility\PathUtility::stripPathSitePrefix($sourcePath) . '|' . date('d-m-Y H:i:s', filemtime($sourcePath)) . '|version=2.3';
167  $this->cacheFileName = PATH_site . 'typo3temp/llxml/' . substr(basename($sourcePath), 10, 15) . '_' . \TYPO3\CMS\Core\Utility\GeneralUtility::shortMD5($this->hashSource) . '.' . $languageKey . '.' . $this->targetCharset . '.cache';
168  }
169 
175  protected function getContentFromCacheFile() {
176  $serContent = (array) unserialize(file_get_contents($this->cacheFileName));
177  $LOCAL_LANG = $serContent['LOCAL_LANG'];
178  return (array) $LOCAL_LANG;
179  }
180 
187  protected function isWithinWebRoot($fileName) {
189  }
190 
198  protected function setCharsets($languageKey, $charset) {
199  $this->sourceCharset = $this->csConvObj->parse_charset($this->csConvObj->charSetArray[$languageKey] ?: 'utf-8');
200  if ($charset) {
201  $this->targetCharset = $this->csConvObj->parse_charset($charset);
202  } else {
203  $this->targetCharset = 'utf-8';
204  }
205  }
206 
215  protected function validateParameters($sourcePath, $languageKey) {
216  if (!$this->isWithinWebRoot($sourcePath) || !@is_file($sourcePath) || !$languageKey) {
217  throw new \RuntimeException(sprintf('Invalid source path (%s) or languageKey (%s)', $sourcePath, $languageKey), 1309245002);
218  }
219  }
220 
221 }
static writeFileToTypo3tempDir($filepath, $content)
getParsedData($sourcePath, $languageKey, $charset='')
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
static getFileAbsFileName($filename, $onlyRelative=TRUE, $relToTYPO3_mainDir=FALSE)