3e34cf05f1b57587666221910070d3e98742b2fb
[Packages/TYPO3.CMS.git] / typo3 / sysext / lang / Classes / Utility / Connection / Ter.php
1 <?php
2 namespace TYPO3\CMS\Lang\Utility\Connection;
3 /**
4 * This file is part of the TYPO3 CMS project.
5 *
6 * It is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License, either version 2
8 * of the License, or any later version.
9 *
10 * For the full copyright and license information, please read the
11 * LICENSE.txt file that was distributed with this source code.
12 *
13 * The TYPO3 project - inspiring people to share!
14 */
15
16 /**
17 * Extends of extensionmanager ter connection to enrich with translation related methods
18 *
19 * @author Sebastian Fischer <typo3@evoweb.de>
20 */
21 class Ter extends \TYPO3\CMS\Extensionmanager\Utility\Connection\TerUtility {
22
23 /**
24 * Fetches extensions translation status
25 *
26 * @param string $extensionKey Extension Key
27 * @param string $mirrorUrl URL of mirror to use
28 * @return mixed
29 */
30 public function fetchTranslationStatus($extensionKey, $mirrorUrl) {
31 $result = FALSE;
32 $extPath = \TYPO3\CMS\Core\Utility\GeneralUtility::strtolower($extensionKey);
33 $mirrorUrl .= $extPath[0] . '/' . $extPath[1] . '/' . $extPath . '-l10n/' . $extPath . '-l10n.xml';
34 $remote = \TYPO3\CMS\Core\Utility\GeneralUtility::getURL($mirrorUrl, 0, array(TYPO3_user_agent));
35
36 if ($remote !== FALSE) {
37 $parsed = $this->parseL10nXML($remote);
38 $result = $parsed['languagePackIndex'];
39 }
40
41 return $result;
42 }
43
44 /**
45 * Parses content of *-l10n.xml into a suitable array
46 *
47 * @param string $string: XML data to parse
48 * @throws \TYPO3\CMS\Lang\Exception\XmlParser
49 * @return array Array representation of XML data
50 */
51 protected function parseL10nXML($string) {
52 // Create parser:
53 $parser = xml_parser_create();
54 $values = array();
55 $index = array();
56
57 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
58 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0);
59
60 // Parse content
61 xml_parse_into_struct($parser, $string, $values, $index);
62
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
77 // Traverse the parsed XML structure:
78 foreach ($values as $val) {
79 // First, process the tag-name (which is used in both cases, whether "complete" or "close")
80 $tagName = ($val['tag'] == 'languagepack' && $val['type'] == 'open') ? $val['attributes']['language'] : $val['tag'];
81 if (!$documentTag) {
82 $documentTag = $tagName;
83 }
84
85 // Setting tag-values, manage stack:
86 switch ($val['type']) {
87 // If open tag it means there is an array stored in sub-elements.
88 // Therefore increase the stackpointer and reset the accumulation array
89 case 'open':
90 // Setting blank place holder
91 $current[$tagName] = array();
92 $stack[$stacktop++] = $current;
93 $current = array();
94 break;
95 // If the tag is "close" then it is an array which is closing and we decrease the stack pointer.
96 case 'close':
97 $oldCurrent = $current;
98 $current = $stack[--$stacktop];
99 // Going to the end of array to get placeholder key, key($current), and fill in array next
100 end($current);
101 $current[key($current)] = $oldCurrent;
102 unset($oldCurrent);
103 break;
104 // If "complete", then it's a value. Omits the tag if the value is empty.
105 case 'complete':
106 $trimmedValue = trim((string)$val['value']);
107 if ($trimmedValue !== '') {
108 $current[$tagName] = $trimmedValue;
109 }
110 break;
111 }
112 }
113 $result = $current[$tagName];
114 }
115
116 return $result;
117 }
118
119 /**
120 * Install translations for all selected languages for an extension
121 *
122 * @param string $extensionKey The extension key to install the translations for
123 * @param string $language Language code of translation to fetch
124 * @param string $mirrorUrl Mirror URL to fetch data from
125 * @return boolean TRUE on success, error string on fauilure
126 */
127 public function updateTranslation($extensionKey, $language, $mirrorUrl) {
128 $result = FALSE;
129 try {
130 $l10n = $this->fetchTranslation($extensionKey, $language, $mirrorUrl);
131 if (is_array($l10n)) {
132 $absolutePathToZipFile = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName('typo3temp/' . $extensionKey . '-l10n-' . $language . '.zip');
133 $relativeLanguagePath = 'l10n' . '/' . $language . '/';
134 $absoluteLanguagePath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName(PATH_typo3conf . $relativeLanguagePath);
135 $absoluteExtensionLanguagePath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName(PATH_typo3conf . $relativeLanguagePath. $extensionKey . '/');
136 if (empty($absolutePathToZipFile) || empty($absoluteLanguagePath) || empty($absoluteExtensionLanguagePath)) {
137 throw new \TYPO3\CMS\Lang\Exception\Lang('Given path is invalid.', 1352565336);
138 }
139 if (!is_dir($absoluteLanguagePath)) {
140 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep(PATH_typo3conf, $relativeLanguagePath);
141 }
142 \TYPO3\CMS\Core\Utility\GeneralUtility::writeFile($absolutePathToZipFile, $l10n[0]);
143 if (is_dir($absoluteExtensionLanguagePath)) {
144 \TYPO3\CMS\Core\Utility\GeneralUtility::rmdir($absoluteExtensionLanguagePath, TRUE);
145 }
146
147 if ($this->unzipTranslationFile($absolutePathToZipFile, $absoluteLanguagePath)) {
148 $result = TRUE;
149 }
150 }
151 } catch (\TYPO3\CMS\Core\Exception $exception) {
152 // @todo logging
153 }
154 return $result;
155 }
156
157 /**
158 * Fetches an extensions l10n file from the given mirror
159 *
160 * @param string $extensionKey Extension Key
161 * @param string $language The language code of the translation to fetch
162 * @param string $mirrorUrl URL of mirror to use
163 * @throws \TYPO3\CMS\Lang\Exception\XmlParser
164 * @return array Array containing l10n data
165 */
166 protected function fetchTranslation($extensionKey, $language, $mirrorUrl) {
167 $extensionPath = \TYPO3\CMS\Core\Utility\GeneralUtility::strtolower($extensionKey);
168 $mirrorUrl .= $extensionPath[0] . '/' . $extensionPath[1] . '/' . $extensionPath .
169 '-l10n/' . $extensionPath . '-l10n-' . $language . '.zip';
170 $l10nResponse = \TYPO3\CMS\Core\Utility\GeneralUtility::getURL($mirrorUrl, 0, array(TYPO3_user_agent));
171
172 if ($l10nResponse === FALSE) {
173 throw new \TYPO3\CMS\Lang\Exception\XmlParser('Error: Translation could not be fetched.', 1345736785);
174 } else {
175 return array($l10nResponse);
176 }
177 }
178
179 /**
180 * Unzip an language.zip.
181 *
182 * @param string $file path to zip file
183 * @param string $path path to extract to
184 * @throws \TYPO3\CMS\Lang\Exception\Lang
185 * @return boolean
186 */
187 protected function unzipTranslationFile($file, $path) {
188 $zip = zip_open($file);
189 if (is_resource($zip)) {
190 $result = TRUE;
191
192 if (!is_dir($path)) {
193 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep($path);
194 }
195
196 while (($zipEntry = zip_read($zip)) !== FALSE) {
197 $zipEntryName = zip_entry_name($zipEntry);
198 if (strpos($zipEntryName, '/') !== FALSE) {
199 $zipEntryPathSegments = explode('/', $zipEntryName);
200 $fileName = array_pop($zipEntryPathSegments);
201 // It is a folder, because the last segment is empty, let's create it
202 if (empty($fileName)) {
203 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep($path, implode('/', $zipEntryPathSegments));
204 } else {
205 $absoluteTargetPath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($path . implode('/', $zipEntryPathSegments) . '/' . $fileName);
206 if (strlen(trim($absoluteTargetPath)) > 0) {
207 $return = \TYPO3\CMS\Core\Utility\GeneralUtility::writeFile(
208 $absoluteTargetPath, zip_entry_read($zipEntry, zip_entry_filesize($zipEntry))
209 );
210 if ($return === FALSE) {
211 throw new \TYPO3\CMS\Lang\Exception\Lang('Could not write file ' . $zipEntryName, 1345304560);
212 }
213 } else {
214 throw new \TYPO3\CMS\Lang\Exception\Lang('Could not write file ' . $zipEntryName, 1352566904);
215 }
216 }
217 } else {
218 throw new \TYPO3\CMS\Lang\Exception\Lang('Extension directory missing in zip file!', 1352566905);
219 }
220 }
221 } else {
222 throw new \TYPO3\CMS\Lang\Exception\Lang('Unable to open zip file ' . $file, 1345304561);
223 }
224
225 return $result;
226 }
227 }