TYPO3 CMS  TYPO3_6-2
Ter.php
Go to the documentation of this file.
1 <?php
22 
30  public function fetchTranslationStatus($extensionKey, $mirrorUrl) {
31  $result = FALSE;
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 
51  protected function parseL10nXML($string) {
52  // Create parser:
53  $parser = xml_parser_create();
54  // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
55  $previousValueOfEntityLoader = libxml_disable_entity_loader(TRUE);
56  $values = array();
57  $index = array();
58 
59  xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
60  xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0);
61 
62  // Parse content
63  xml_parse_into_struct($parser, $string, $values, $index);
64  libxml_disable_entity_loader($previousValueOfEntityLoader);
65  // If error, return error message
66  if (xml_get_error_code($parser)) {
67  $line = xml_get_current_line_number($parser);
68  $error = xml_error_string(xml_get_error_code($parser));
69  xml_parser_free($parser);
70  throw new \TYPO3\CMS\Lang\Exception\XmlParser('Error in XML parser while decoding l10n XML file. Line ' . $line . ': ' . $error, 1345736517);
71  } else {
72  // Init vars
73  $stack = array(array());
74  $stacktop = 0;
75  $current = array();
76  $tagName = '';
77  $documentTag = '';
78 
79  // Traverse the parsed XML structure:
80  foreach ($values as $val) {
81  // First, process the tag-name (which is used in both cases, whether "complete" or "close")
82  $tagName = ($val['tag'] == 'languagepack' && $val['type'] == 'open') ? $val['attributes']['language'] : $val['tag'];
83  if (!$documentTag) {
84  $documentTag = $tagName;
85  }
86 
87  // Setting tag-values, manage stack:
88  switch ($val['type']) {
89  // If open tag it means there is an array stored in sub-elements.
90  // Therefore increase the stackpointer and reset the accumulation array
91  case 'open':
92  // Setting blank place holder
93  $current[$tagName] = array();
94  $stack[$stacktop++] = $current;
95  $current = array();
96  break;
97  // If the tag is "close" then it is an array which is closing and we decrease the stack pointer.
98  case 'close':
99  $oldCurrent = $current;
100  $current = $stack[--$stacktop];
101  // Going to the end of array to get placeholder key, key($current), and fill in array next
102  end($current);
103  $current[key($current)] = $oldCurrent;
104  unset($oldCurrent);
105  break;
106  // If "complete", then it's a value. Omits the tag if the value is empty.
107  case 'complete':
108  $trimmedValue = trim((string)$val['value']);
109  if ($trimmedValue !== '') {
110  $current[$tagName] = $trimmedValue;
111  }
112  break;
113  }
114  }
115  $result = $current[$tagName];
116  }
117 
118  return $result;
119  }
120 
129  public function updateTranslation($extensionKey, $language, $mirrorUrl) {
130  $result = FALSE;
131  try {
132  $l10n = $this->fetchTranslation($extensionKey, $language, $mirrorUrl);
133  if (is_array($l10n)) {
134  $absolutePathToZipFile = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName('typo3temp/' . $extensionKey . '-l10n-' . $language . '.zip');
135  $relativeLanguagePath = 'l10n' . '/' . $language . '/';
136  $absoluteLanguagePath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName(PATH_typo3conf . $relativeLanguagePath);
137  $absoluteExtensionLanguagePath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName(PATH_typo3conf . $relativeLanguagePath. $extensionKey . '/');
138  if (empty($absolutePathToZipFile) || empty($absoluteLanguagePath) || empty($absoluteExtensionLanguagePath)) {
139  throw new \TYPO3\CMS\Lang\Exception\Lang('Given path is invalid.', 1352565336);
140  }
141  if (!is_dir($absoluteLanguagePath)) {
142  \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep(PATH_typo3conf, $relativeLanguagePath);
143  }
144  \TYPO3\CMS\Core\Utility\GeneralUtility::writeFile($absolutePathToZipFile, $l10n[0]);
145  if (is_dir($absoluteExtensionLanguagePath)) {
146  \TYPO3\CMS\Core\Utility\GeneralUtility::rmdir($absoluteExtensionLanguagePath, TRUE);
147  }
148 
149  if ($this->unzipTranslationFile($absolutePathToZipFile, $absoluteLanguagePath)) {
150  $result = TRUE;
151  }
152  }
153  } catch (\TYPO3\CMS\Core\Exception $exception) {
154  // @todo logging
155  }
156  return $result;
157  }
158 
168  protected function fetchTranslation($extensionKey, $language, $mirrorUrl) {
169  $extensionPath = \TYPO3\CMS\Core\Utility\GeneralUtility::strtolower($extensionKey);
170  $mirrorUrl .= $extensionPath[0] . '/' . $extensionPath[1] . '/' . $extensionPath .
171  '-l10n/' . $extensionPath . '-l10n-' . $language . '.zip';
172  $l10nResponse = \TYPO3\CMS\Core\Utility\GeneralUtility::getURL($mirrorUrl, 0, array(TYPO3_user_agent));
173 
174  if ($l10nResponse === FALSE) {
175  throw new \TYPO3\CMS\Lang\Exception\XmlParser('Error: Translation could not be fetched.', 1345736785);
176  } else {
177  return array($l10nResponse);
178  }
179  }
180 
189  protected function unzipTranslationFile($file, $path) {
190  $zip = zip_open($file);
191  if (is_resource($zip)) {
192  $result = TRUE;
193 
194  if (!is_dir($path)) {
196  }
197 
198  while (($zipEntry = zip_read($zip)) !== FALSE) {
199  $zipEntryName = zip_entry_name($zipEntry);
200  if (strpos($zipEntryName, '/') !== FALSE) {
201  $zipEntryPathSegments = explode('/', $zipEntryName);
202  $fileName = array_pop($zipEntryPathSegments);
203  // It is a folder, because the last segment is empty, let's create it
204  if (empty($fileName)) {
205  \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep($path, implode('/', $zipEntryPathSegments));
206  } else {
207  $absoluteTargetPath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($path . implode('/', $zipEntryPathSegments) . '/' . $fileName);
208  if (strlen(trim($absoluteTargetPath)) > 0) {
210  $absoluteTargetPath, zip_entry_read($zipEntry, zip_entry_filesize($zipEntry))
211  );
212  if ($return === FALSE) {
213  throw new \TYPO3\CMS\Lang\Exception\Lang('Could not write file ' . $zipEntryName, 1345304560);
214  }
215  } else {
216  throw new \TYPO3\CMS\Lang\Exception\Lang('Could not write file ' . $zipEntryName, 1352566904);
217  }
218  }
219  } else {
220  throw new \TYPO3\CMS\Lang\Exception\Lang('Extension directory missing in zip file!', 1352566905);
221  }
222  }
223  } else {
224  throw new \TYPO3\CMS\Lang\Exception\Lang('Unable to open zip file ' . $file, 1345304561);
225  }
226 
227  return $result;
228  }
229 }
static mkdir_deep($directory, $deepDirectory='')
static writeFile($file, $content, $changePermissions=FALSE)
unzipTranslationFile($file, $path)
Definition: Ter.php:189
static rmdir($path, $removeNonEmpty=FALSE)
fetchTranslation($extensionKey, $language, $mirrorUrl)
Definition: Ter.php:168
$remote
Definition: server.php:68
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.
updateTranslation($extensionKey, $language, $mirrorUrl)
Definition: Ter.php:129
static getFileAbsFileName($filename, $onlyRelative=TRUE, $relToTYPO3_mainDir=FALSE)
fetchTranslationStatus($extensionKey, $mirrorUrl)
Definition: Ter.php:30