17dfcfad6cc1665b50cf181af2ce31e6dfb5a81e
[Packages/TYPO3.CMS.git] / typo3 / sysext / lang / Classes / Service / TerService.php
1 <?php
2 namespace TYPO3\CMS\Lang\Service;
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
17 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Extends of extensionmanager ter connection to enrich with translation
22 * related methods
23 */
24 class TerService extends \TYPO3\CMS\Extensionmanager\Utility\Connection\TerUtility implements \TYPO3\CMS\Core\SingletonInterface
25 {
26 /**
27 * Fetches extensions translation status
28 *
29 * @param string $extensionKey Extension Key
30 * @param string $mirrorUrl URL of mirror to use
31 * @return mixed
32 */
33 public function fetchTranslationStatus($extensionKey, $mirrorUrl)
34 {
35 $result = false;
36 $extPath = GeneralUtility::strtolower($extensionKey);
37 $mirrorUrl .= $extPath[0] . '/' . $extPath[1] . '/' . $extPath . '-l10n/' . $extPath . '-l10n.xml';
38 $remote = GeneralUtility::getURL($mirrorUrl, 0, array(TYPO3_user_agent));
39 if ($remote !== false) {
40 $parsed = $this->parseL10nXML($remote);
41 $result = $parsed['languagePackIndex'];
42 }
43 return $result;
44 }
45
46 /**
47 * Parses content of *-l10n.xml into a suitable array
48 *
49 * @param string $string: XML data to parse
50 * @throws \TYPO3\CMS\Lang\Exception\XmlParser
51 * @return array Array representation of XML data
52 */
53 protected function parseL10nXML($string)
54 {
55 // Create parser:
56 $parser = xml_parser_create();
57 $values = array();
58 $index = array();
59 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
60 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0);
61 // Parse content
62 xml_parse_into_struct($parser, $string, $values, $index);
63 // If error, return error message
64 if (xml_get_error_code($parser)) {
65 $line = xml_get_current_line_number($parser);
66 $error = xml_error_string(xml_get_error_code($parser));
67 xml_parser_free($parser);
68 throw new \TYPO3\CMS\Lang\Exception\XmlParser('Error in XML parser while decoding l10n XML file. Line ' . $line . ': ' . $error, 1345736517);
69 } else {
70 // Init vars
71 $stack = array(array());
72 $stacktop = 0;
73 $current = array();
74 $tagName = '';
75 $documentTag = '';
76 // Traverse the parsed XML structure:
77 foreach ($values as $val) {
78 // First, process the tag-name (which is used in both cases, whether "complete" or "close")
79 $tagName = ($val['tag'] == 'languagepack' && $val['type'] == 'open') ? $val['attributes']['language'] : $val['tag'];
80 if (!$documentTag) {
81 $documentTag = $tagName;
82 }
83 // Setting tag-values, manage stack:
84 switch ($val['type']) {
85 // If open tag it means there is an array stored in sub-elements.
86 // Therefore increase the stackpointer and reset the accumulation array
87 case 'open':
88 // Setting blank place holder
89 $current[$tagName] = array();
90 $stack[$stacktop++] = $current;
91 $current = array();
92 break;
93 // If the tag is "close" then it is an array which is closing and we decrease the stack pointer.
94 case 'close':
95 $oldCurrent = $current;
96 $current = $stack[--$stacktop];
97 // Going to the end of array to get placeholder key, key($current), and fill in array next
98 end($current);
99 $current[key($current)] = $oldCurrent;
100 unset($oldCurrent);
101 break;
102 // If "complete", then it's a value. Omits the tag if the value is empty.
103 case 'complete':
104 $trimmedValue = trim((string)$val['value']);
105 if ($trimmedValue !== '') {
106 $current[$tagName] = $trimmedValue;
107 }
108 break;
109 }
110 }
111 $result = $current[$tagName];
112 }
113 return $result;
114 }
115
116 /**
117 * Install translations for all selected languages for an extension
118 *
119 * @param string $extensionKey The extension key to install the translations for
120 * @param string $language Language code of translation to fetch
121 * @param string $mirrorUrl Mirror URL to fetch data from
122 * @return bool TRUE on success, error string on failure
123 */
124 public function updateTranslation($extensionKey, $language, $mirrorUrl)
125 {
126 $result = false;
127 try {
128 $l10n = $this->fetchTranslation($extensionKey, $language, $mirrorUrl);
129 if (is_array($l10n)) {
130 $absolutePathToZipFile = GeneralUtility::getFileAbsFileName('typo3temp/Language/' . $extensionKey . '-l10n-' . $language . '.zip');
131 $relativeLanguagePath = 'l10n' . '/' . $language . '/';
132 $absoluteLanguagePath = GeneralUtility::getFileAbsFileName(PATH_typo3conf . $relativeLanguagePath);
133 $absoluteExtensionLanguagePath = GeneralUtility::getFileAbsFileName(PATH_typo3conf . $relativeLanguagePath . $extensionKey . '/');
134 if (empty($absolutePathToZipFile) || empty($absoluteLanguagePath) || empty($absoluteExtensionLanguagePath)) {
135 throw new \TYPO3\CMS\Lang\Exception\Language('Given path is invalid.', 1352565336);
136 }
137 if (!is_dir($absoluteLanguagePath)) {
138 GeneralUtility::mkdir_deep(PATH_typo3conf, $relativeLanguagePath);
139 }
140 GeneralUtility::writeFileToTypo3tempDir($absolutePathToZipFile, $l10n[0]);
141 if (is_dir($absoluteExtensionLanguagePath)) {
142 GeneralUtility::rmdir($absoluteExtensionLanguagePath, true);
143 }
144 if ($this->unzipTranslationFile($absolutePathToZipFile, $absoluteLanguagePath)) {
145 $result = true;
146 }
147 }
148 } catch (\TYPO3\CMS\Core\Exception $exception) {
149 // @todo logging
150 }
151 return $result;
152 }
153
154 /**
155 * Fetches an extensions l10n file from the given mirror
156 *
157 * @param string $extensionKey Extension Key
158 * @param string $language The language code of the translation to fetch
159 * @param string $mirrorUrl URL of mirror to use
160 * @throws \TYPO3\CMS\Lang\Exception\XmlParser
161 * @return array Array containing l10n data
162 */
163 protected function fetchTranslation($extensionKey, $language, $mirrorUrl)
164 {
165 $extensionPath = GeneralUtility::strtolower($extensionKey);
166 // Typical non sysext path, Hungarian:
167 // http://my.mirror/ter/a/n/anextension-l10n/anextension-l10n-hu.zip
168 $packageUrl = $extensionPath[0] . '/' . $extensionPath[1] . '/' . $extensionPath .
169 '-l10n/' . $extensionPath . '-l10n-' . $language . '.zip';
170
171 try {
172 $path = ExtensionManagementUtility::extPath($extensionPath);
173 if (strpos($path, '/sysext/') !== false) {
174 // This is a system extension and the package URL should be adapted
175 list($majorVersion, ) = explode('.', TYPO3_branch);
176 // Typical non sysext path, mind the additional version part, French
177 // http://my.mirror/ter/b/a/backend-l10n/backend-l10n-fr.v7.zip
178 $packageUrl = $extensionPath[0] . '/' . $extensionPath[1] . '/' . $extensionPath .
179 '-l10n/' . $extensionPath . '-l10n-' . $language . '.v' . $majorVersion . '.zip';
180 }
181 } catch (\BadFunctionCallException $e) {
182 // Nothing to do
183 }
184
185 $l10nResponse = GeneralUtility::getURL($mirrorUrl . $packageUrl, 0, array(TYPO3_user_agent));
186 if ($l10nResponse === false) {
187 throw new \TYPO3\CMS\Lang\Exception\XmlParser('Error: Translation could not be fetched.', 1345736785);
188 } else {
189 return array($l10nResponse);
190 }
191 }
192
193 /**
194 * Unzip an language zip file
195 *
196 * @param string $file path to zip file
197 * @param string $path path to extract to
198 * @throws \TYPO3\CMS\Lang\Exception\Language
199 * @return bool
200 */
201 protected function unzipTranslationFile($file, $path)
202 {
203 $zip = zip_open($file);
204 if (is_resource($zip)) {
205 $result = true;
206 if (!is_dir($path)) {
207 GeneralUtility::mkdir_deep($path);
208 }
209 while (($zipEntry = zip_read($zip)) !== false) {
210 $zipEntryName = zip_entry_name($zipEntry);
211 if (strpos($zipEntryName, '/') !== false) {
212 $zipEntryPathSegments = explode('/', $zipEntryName);
213 $fileName = array_pop($zipEntryPathSegments);
214 // It is a folder, because the last segment is empty, let's create it
215 if (empty($fileName)) {
216 GeneralUtility::mkdir_deep($path, implode('/', $zipEntryPathSegments));
217 } else {
218 $absoluteTargetPath = GeneralUtility::getFileAbsFileName($path . implode('/', $zipEntryPathSegments) . '/' . $fileName);
219 if (trim($absoluteTargetPath) !== '') {
220 $return = GeneralUtility::writeFile(
221 $absoluteTargetPath, zip_entry_read($zipEntry, zip_entry_filesize($zipEntry))
222 );
223 if ($return === false) {
224 throw new \TYPO3\CMS\Lang\Exception\Language('Could not write file ' . $zipEntryName, 1345304560);
225 }
226 } else {
227 throw new \TYPO3\CMS\Lang\Exception\Language('Could not write file ' . $zipEntryName, 1352566904);
228 }
229 }
230 } else {
231 throw new \TYPO3\CMS\Lang\Exception\Language('Extension directory missing in zip file!', 1352566905);
232 }
233 }
234 } else {
235 throw new \TYPO3\CMS\Lang\Exception\Language('Unable to open zip file ' . $file, 1345304561);
236 }
237 return $result;
238 }
239 }