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