TYPO3  7.6
TerService.php
Go to the documentation of this file.
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 
19 
25 {
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 
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 
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 
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 
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)) {
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 }