[TASK] Fix typo in copyright notice
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Localization / Parser / LocallangArrayParser.php
1 <?php
2 namespace TYPO3\CMS\Core\Localization\Parser;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011-2013 Dominique Feyer <dfeyer@reelpeek.net>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Parser for PHP locallang array.
31 *
32 * @author Dominique Feyer <dfeyer@reelpeek.net>
33 * @author Dmitry Dulepov <dmitry.dulepov@gmail.com>
34 */
35 class LocallangArrayParser implements \TYPO3\CMS\Core\Localization\Parser\LocalizationParserInterface {
36
37 /**
38 * @var string
39 */
40 protected $cacheFileName;
41
42 /**
43 * @var \TYPO3\CMS\Core\Charset\CharsetConverter
44 */
45 protected $csConvObj;
46
47 /**
48 * @var string
49 */
50 protected $hashSource;
51
52 /**
53 * @var string
54 */
55 protected $sourceCharset;
56
57 /**
58 * @var string
59 */
60 protected $targetCharset;
61
62 /**
63 * Initializes the parser.
64 *
65 * @return void
66 */
67 public function __construct() {
68 $this->createCsConvObject();
69 }
70
71 /**
72 * Returns parsed representation of PHP locallang file.
73 *
74 * @param string $sourcePath Source file path
75 * @param string $languageKey Language key
76 * @param string $charset Charset
77 * @return array
78 * @throws \RuntimeException
79 */
80 public function getParsedData($sourcePath, $languageKey, $charset = '') {
81 $this->validateParameters($sourcePath, $languageKey);
82 $this->setCharsets($languageKey, $charset);
83 $this->generateCacheFileName($sourcePath, $languageKey);
84 if (!file_exists($this->cacheFileName)) {
85 $LOCAL_LANG = $this->generateCacheFile($sourcePath, $languageKey);
86 } else {
87 $LOCAL_LANG = $this->getContentFromCacheFile();
88 }
89 $xliff = $this->convertToXLIFF($LOCAL_LANG);
90 return $xliff;
91 }
92
93 /**
94 * Converts the LOCAL_LANG array to XLIFF structure.
95 *
96 * @param array $LOCAL_LANG
97 * @return array
98 */
99 protected function convertToXLIFF(array $LOCAL_LANG) {
100 foreach ($LOCAL_LANG as &$keysLabels) {
101 foreach ($keysLabels as &$label) {
102 $label = array(
103 0 => array(
104 'target' => $label
105 )
106 );
107 }
108 unset($label);
109 }
110 return $LOCAL_LANG;
111 }
112
113 /**
114 * Creates a character conversion object.
115 *
116 * @return void
117 */
118 protected function createCsConvObject() {
119 if (is_object($GLOBALS['LANG'])) {
120 $this->csConvObj = $GLOBALS['LANG']->csConvObj;
121 } elseif (is_object($GLOBALS['TSFE'])) {
122 $this->csConvObj = $GLOBALS['TSFE']->csConvObj;
123 } else {
124 $this->csConvObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Charset\\CharsetConverter');
125 }
126 }
127
128 /**
129 * Generates the cache file.
130 *
131 * @param string $sourcePath
132 * @param string $languageKey
133 * @return array
134 * @throws \RuntimeException
135 */
136 protected function generateCacheFile($sourcePath, $languageKey) {
137 $LOCAL_LANG = array();
138 // Get PHP data
139 include $sourcePath;
140 if (!is_array($LOCAL_LANG)) {
141 $fileName = \TYPO3\CMS\Core\Utility\PathUtility::stripPathSitePrefix($sourcePath);
142 throw new \RuntimeException('TYPO3 Fatal Error: "' . $fileName . '" is no TYPO3 language file!', 1308898491);
143 }
144 // Converting the default language (English)
145 // This needs to be done for a few accented loan words and extension names
146 if (is_array($LOCAL_LANG['default']) && $this->targetCharset !== 'utf-8') {
147 foreach ($LOCAL_LANG['default'] as &$labelValue) {
148 $labelValue = $this->csConvObj->conv($labelValue, 'utf-8', $this->targetCharset);
149 }
150 unset($labelValue);
151 }
152 if ($languageKey !== 'default' && is_array($LOCAL_LANG[$languageKey]) && $this->sourceCharset != $this->targetCharset) {
153 foreach ($LOCAL_LANG[$languageKey] as &$labelValue) {
154 $labelValue = $this->csConvObj->conv($labelValue, $this->sourceCharset, $this->targetCharset);
155 }
156 unset($labelValue);
157 }
158 // Cache the content now:
159 if (isset($LOCAL_LANG[$languageKey])) {
160 $serContent = array('origFile' => $this->hashSource, 'LOCAL_LANG' => array('default' => $LOCAL_LANG['default'], $languageKey => $LOCAL_LANG[$languageKey]));
161 } else {
162 $serContent = array('origFile' => $this->hashSource, 'LOCAL_LANG' => array('default' => $LOCAL_LANG['default']));
163 }
164 $res = \TYPO3\CMS\Core\Utility\GeneralUtility::writeFileToTypo3tempDir($this->cacheFileName, serialize($serContent));
165 if ($res) {
166 throw new \RuntimeException('TYPO3 Fatal Error: "' . $res, 1308898501);
167 }
168 return $LOCAL_LANG;
169 }
170
171 /**
172 * Generates the name of the cached file.
173 *
174 * @param string $sourcePath
175 * @param string $languageKey
176 * @return void
177 */
178 protected function generateCacheFileName($sourcePath, $languageKey) {
179 $this->hashSource = \TYPO3\CMS\Core\Utility\PathUtility::stripPathSitePrefix($sourcePath) . '|' . date('d-m-Y H:i:s', filemtime($sourcePath)) . '|version=2.3';
180 $this->cacheFileName = PATH_site . 'typo3temp/llxml/' . substr(basename($sourcePath), 10, 15) . '_' . \TYPO3\CMS\Core\Utility\GeneralUtility::shortMD5($this->hashSource) . '.' . $languageKey . '.' . $this->targetCharset . '.cache';
181 }
182
183 /**
184 * Obtains the content from the cache file.
185 *
186 * @return array
187 */
188 protected function getContentFromCacheFile() {
189 $serContent = (array) unserialize(file_get_contents($this->cacheFileName));
190 $LOCAL_LANG = $serContent['LOCAL_LANG'];
191 return (array) $LOCAL_LANG;
192 }
193
194 /**
195 * Checks if the file is within the web root.
196 *
197 * @param string $fileName
198 * @return boolean
199 */
200 protected function isWithinWebRoot($fileName) {
201 return (bool) \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($fileName);
202 }
203
204 /**
205 * Sets character sets for the language key.
206 *
207 * @param string $languageKey
208 * @param string $charset
209 * @return void
210 */
211 protected function setCharsets($languageKey, $charset) {
212 $this->sourceCharset = $this->csConvObj->parse_charset($this->csConvObj->charSetArray[$languageKey] ? $this->csConvObj->charSetArray[$languageKey] : 'utf-8');
213 if ($charset) {
214 $this->targetCharset = $this->csConvObj->parse_charset($charset);
215 } else {
216 $this->targetCharset = 'utf-8';
217 }
218 }
219
220 /**
221 * Validates parameters for the function.
222 *
223 * @param string $sourcePath
224 * @param string $languageKey
225 * @return void
226 * @throws \RuntimeException
227 */
228 protected function validateParameters($sourcePath, $languageKey) {
229 if (!$this->isWithinWebRoot($sourcePath) || !@is_file($sourcePath) || !$languageKey) {
230 throw new \RuntimeException(sprintf('Invalid source path (%s) or languageKey (%s)', $sourcePath, $languageKey), 1309245002);
231 }
232 }
233
234 }