Cleanup: Updated copyright comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / em / classes / translations / class.tx_em_translations.php
1 <?php
2 /* **************************************************************
3 * Copyright notice
4 *
5 * (c) webservices.nl
6 * (c) 2006-2011 Karsten Dambekalns <karsten@typo3.org>
7 * All rights reserved
8 *
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 * A copy is found in the textfile GPL.txt and important notices to the license
18 * from the author is found in LICENSE.txt distributed with these scripts.
19 *
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28 /* $Id: class.tx_em_translations.php 2018 2010-03-14 12:25:58Z steffenk $ */
29
30 /**
31 * Class for handling translations
32 *
33 */
34 class tx_em_Translations {
35
36
37 protected $parentObject;
38 protected $terConnection;
39
40
41 /**
42 * Constructor
43 *
44 * @param object $parentObject
45 * @return void
46 */
47 public function __construct($parentObject) {
48 $this->parentObject = $parentObject;
49 $this->terConnection = t3lib_div::makeInstance('tx_em_Connection_Ter', $this);
50 $this->terConnection->wsdlURL = $GLOBALS['TYPO3_CONF_VARS']['EXT']['em_wsdlURL'];
51 }
52
53
54 /**
55 * Install translations for all selected languages for an extension
56 *
57 * @param string $extKey The extension key to install the translations for
58 * @param string $mirrorURL Mirror URL to fetch data from
59 * @return mixed true on success, error string on fauilure
60 */
61 function installTranslationsForExtension($extKey, $mirrorURL) {
62 $selectedLanguages = unserialize($this->parentObject->MOD_SETTINGS['selectedLanguages']);
63 if (!is_array($selectedLanguages)) {
64 $selectedLanguages = array();
65 }
66 foreach ($selectedLanguages as $lang) {
67 $l10n = $this->parentObject->terConnection->fetchTranslation($extKey, $lang, $mirrorURL);
68 if (is_array($l10n)) {
69 $file = PATH_typo3conf . 'l10n/' . $extKey . '-l10n-' . $lang . '.zip';
70 $path = 'l10n/' . $lang . '/' . $extKey;
71 t3lib_div::writeFile($file, $l10n[0]);
72 if (!is_dir(PATH_typo3conf . $path)) {
73 t3lib_div::mkdir_deep(PATH_typo3conf, $path);
74 }
75 if (tx_em_Tools::unzip($file, PATH_typo3conf . $path)) {
76 return true;
77 } else {
78 return $GLOBALS['LANG']->getLL('translation_unpacking_failed');
79 }
80 } else {
81 return $l10n;
82 }
83 }
84 }
85
86 /**
87 * Install translations for all selected languages for an extension
88 *
89 * @param string $extKey The extension key to install the translations for
90 * @param string $lang Language code of translation to fetch
91 * @param string $mirrorURL Mirror URL to fetch data from
92 * @return mixed true on success, error string on fauilure
93 */
94 function updateTranslation($extKey, $lang, $mirrorURL) {
95 $l10n = $this->parentObject->terConnection->fetchTranslation($extKey, $lang, $mirrorURL);
96 if (is_array($l10n)) {
97 $file = PATH_site . 'typo3temp/' . $extKey . '-l10n-' . $lang . '.zip';
98 $path = 'l10n/' . $lang . '/';
99 if (!is_dir(PATH_typo3conf . $path)) {
100 t3lib_div::mkdir_deep(PATH_typo3conf, $path);
101 }
102 t3lib_div::writeFile($file, $l10n[0]);
103 // SteffenG tried this to get first update without errors
104 //t3lib_div::rmdir(PATH_typo3conf . $path, TRUE);
105 if (tx_em_Tools::unzip($file, PATH_typo3conf . $path)) {
106 return true;
107 } else {
108 return $GLOBALS['LANG']->getLL('translation_unpacking_failed');
109 }
110 } else {
111 return $l10n;
112 }
113 }
114
115 /**
116 * Renders translation module
117 *
118 * @return string or direct output
119 */
120 public function translationHandling() {
121 global $LANG, $TYPO3_LOADED_EXT;
122 $LANG->includeLLFile('EXT:setup/mod/locallang.xml');
123
124 //prepare docheader
125 $docHeaderButtons = $this->parentObject->getButtons();
126 $markers = array(
127 'CSH' => $docHeaderButtons['csh'],
128 'FUNC_MENU' => $this->parentObject->getFuncMenu(),
129 );
130
131
132 $incoming = t3lib_div::_POST('SET');
133 if (isset($incoming['selectedLanguages']) && is_array($incoming['selectedLanguages'])) {
134 t3lib_BEfunc::getModuleData($this->parentObject->MOD_MENU, array('selectedLanguages' => serialize($incoming['selectedLanguages'])), $this->parentObject->MCONF['name'], '', 'selectedLanguages');
135 $this->parentObject->MOD_SETTINGS['selectedLanguages'] = serialize($incoming['selectedLanguages']);
136 }
137
138 $selectedLanguages = unserialize($this->parentObject->MOD_SETTINGS['selectedLanguages']);
139 if (count($selectedLanguages) == 1 && empty($selectedLanguages[0])) {
140 $selectedLanguages = array();
141 }
142 $theLanguages = t3lib_div::trimExplode('|', TYPO3_languages);
143 foreach ($theLanguages as $val) {
144 if ($val != 'default') {
145 $localLabel = ' - [' . htmlspecialchars($GLOBALS['LOCAL_LANG']['default']['lang_' . $val]) . ']';
146 $selected = (is_array($selectedLanguages) && in_array($val, $selectedLanguages)) ? ' selected="selected"' : '';
147 $opt[$GLOBALS['LANG']->getLL('lang_' . $val, 1) . '--' . $val] = '
148 <option value="' . $val . '"' . $selected . '>' . $LANG->getLL('lang_' . $val, 1) . $localLabel . '</option>';
149 }
150 }
151 ksort($opt);
152
153 $headline = $GLOBALS['LANG']->getLL('translation_settings');
154 $headline = t3lib_BEfunc::wrapInHelp('_MOD_tools_em', 'translation', $headline);
155
156 // Prepare the HTML output:
157 $content .= '
158 <form action="' . $this->parentObject->script . '" method="post" name="translationform">
159 <fieldset><legend>' . $GLOBALS['LANG']->getLL('translation_settings') . '</legend>
160 <table border="0" cellpadding="2" cellspacing="2">
161 <tr class="bgColor4">
162 <td>' . $GLOBALS['LANG']->getLL('languages_to_fetch') . '</td>
163 <td>
164 <select name="SET[selectedLanguages][]" multiple="multiple" size="10">
165 <option>&nbsp;</option>' .
166 implode('', $opt) . '
167 </select>
168 </td>
169 </tr>
170 </table>
171 <br />
172 <p>' . $GLOBALS['LANG']->getLL('translation_info') . '<br />
173 <br />' . $GLOBALS['LANG']->getLL('translation_loaded_exts') . '</p>
174 </fieldset>
175 <br />
176 <input type="submit" value="' . $GLOBALS['LANG']->getLL('translation_save_selection') . '" />
177 <br />
178 </form>';
179
180 $this->parentObject->content .= $this->parentObject->doc->section($headline, $content, FALSE, TRUE, FALSE, TRUE);
181
182 if (count($selectedLanguages) > 0) {
183 $mirrorURL = $this->parentObject->getMirrorURL();
184 $content = '<input type="button" value="' . $GLOBALS['LANG']->getLL('translation_check_status_button') .
185 '" onclick="document.location.href=\'' . htmlspecialchars(t3lib_div::linkThisScript(array('l10n' => 'check'))) .
186 '\'" />&nbsp;<input type="button" value="' . $GLOBALS['LANG']->getLL('translation_update_button') .
187 '" onclick="document.location.href=\'' . htmlspecialchars(t3lib_div::linkThisScript(array('l10n' => 'update'))) .
188 '\'" />';
189
190 // as this page loads dynamically, quit output buffering caused by ob_gzhandler
191 t3lib_div::cleanOutputBuffers();
192
193 if (t3lib_div::_GET('l10n') == 'check') {
194 $loadedExtensions = array_keys($TYPO3_LOADED_EXT);
195 $loadedExtensions = array_diff($loadedExtensions, array('_CACHEFILE'));
196
197 // Override content output - we now do that ourselves:
198 $this->parentObject->content .= $this->parentObject->doc->section($GLOBALS['LANG']->getLL('translation_status'), $content, 0, 1);
199 // Setting up the buttons and markers for docheader
200 $content = $this->parentObject->doc->startPage('Extension Manager');
201 $content .= $this->parentObject->doc->moduleBody($this->parentObject->pageinfo, $docHeaderButtons, $markers);
202 $contentParts = explode('###CONTENT###', $content);
203
204 echo $contentParts[0] . $this->parentObject->content;
205
206 $this->parentObject->doPrintContent = FALSE;
207 flush();
208
209 echo '
210 <br />
211 <br />
212 <p id="progress-message">
213 ' . $GLOBALS['LANG']->getLL('translation_check_status') . '
214 </p>
215 <br />
216 <div style="width:100%; height:20px; border: 1px solid black;">
217 <div id="progress-bar" style="float: left; width: 0%; height: 20px; background-color:green;">&nbsp;</div>
218 <div id="transparent-bar" style="float: left; width: 100%; height: 20px; background-color:' . $this->parentObject->doc->bgColor2 . ';">&nbsp;</div>
219 </div>
220 <br />
221 <br /><p>' . $GLOBALS['LANG']->getLL('translation_table_check') . '</p><br />
222 <table border="0" cellpadding="2" cellspacing="2">
223 <tr class="t3-row-header"><td>' . $GLOBALS['LANG']->getLL('translation_extension_key') . '</td>
224 ';
225
226 foreach ($selectedLanguages as $lang) {
227 echo ('<td>' . $LANG->getLL('lang_' . $lang, 1) . '</td>');
228 }
229 echo ('</tr>');
230
231 $counter = 1;
232 foreach ($loadedExtensions as $extKey) {
233
234 $percentDone = intval(($counter / count($loadedExtensions)) * 100);
235 echo ('
236 <script type="text/javascript">
237 document.getElementById("progress-bar").style.width = "' . $percentDone . '%";
238 document.getElementById("transparent-bar").style.width = "' . (100 - $percentDone) . '%";
239 document.getElementById("progress-message").firstChild.data="' .
240 sprintf($GLOBALS['LANG']->getLL('translation_checking_extension'), $extKey) . '";
241 </script>
242 ');
243
244 flush();
245 $translationStatusArr = $this->parentObject->terConnection->fetchTranslationStatus($extKey, $mirrorURL);
246
247 echo ('<tr class="bgColor4"><td>' . $extKey . '</td>');
248 foreach ($selectedLanguages as $lang) {
249 // remote unknown -> no l10n available
250 if (!isset($translationStatusArr[$lang])) {
251 echo ('<td title="' . $GLOBALS['LANG']->getLL('translation_no_translation') . '">' .
252 $GLOBALS['LANG']->getLL('translation_n_a') . '</td>');
253 continue;
254 }
255 // determine local md5 from zip
256 if (is_file(PATH_site . 'typo3temp/' . $extKey . '-l10n-' . $lang . '.zip')) {
257 $localmd5 = md5_file(PATH_site . 'typo3temp/' . $extKey . '-l10n-' . $lang . '.zip');
258 } else {
259 echo ('<td title="' . $GLOBALS['LANG']->getLL('translation_not_installed') .
260 '" style="background-color:#ff0">' . $GLOBALS['LANG']->getLL('translation_status_unknown') .
261 '</td>');
262 continue;
263 }
264 // local!=remote -> needs update
265 if ($localmd5 != $translationStatusArr[$lang]['md5']) {
266 echo ('<td title="' . $GLOBALS['LANG']->getLL('translation_needs_update') .
267 '" style="background-color:#ff0">' . $GLOBALS['LANG']->getLL('translation_status_update') .
268 '</td>');
269 continue;
270 }
271 echo ('<td title="' . $GLOBALS['LANG']->getLL('translation_is_ok') .
272 '" style="background-color:#69a550">' . $GLOBALS['LANG']->getLL('translation_status_ok') .
273 '</td>');
274 }
275 echo ('</tr>');
276
277 $counter++;
278 }
279 echo '</table>
280 <script type="text/javascript">
281 document.getElementById("progress-message").firstChild.data="' .
282 $GLOBALS['LANG']->getLL('translation_check_done') . '";
283 </script>
284 ';
285 echo $contentParts[1] . $this->parentObject->doc->endPage();
286 exit;
287
288 } elseif (t3lib_div::_GET('l10n') == 'update') {
289 $loadedExtensions = array_keys($TYPO3_LOADED_EXT);
290 $loadedExtensions = array_diff($loadedExtensions, array('_CACHEFILE'));
291
292 // Override content output - we now do that ourselves:
293 $this->parentObject->content .= $this->parentObject->doc->section($GLOBALS['LANG']->getLL('translation_status'), $content, 0, 1);
294 // Setting up the buttons and markers for docheader
295 $content = $this->parentObject->doc->startPage('Extension Manager');
296 $content .= $this->parentObject->doc->moduleBody($this->parentObject->pageinfo, $docHeaderButtons, $markers);
297 $contentParts = explode('###CONTENT###', $content);
298
299 echo $contentParts[0] . $this->parentObject->content;
300
301 $this->parentObject->doPrintContent = FALSE;
302 flush();
303
304 echo ('
305 <br />
306 <br />
307 <p id="progress-message">
308 ' . $GLOBALS['LANG']->getLL('translation_update_status') . '
309 </p>
310 <br />
311 <div style="width:100%; height:20px; border: 1px solid black;">
312 <div id="progress-bar" style="float: left; width: 0%; height: 20px; background-color:green;">&nbsp;</div>
313 <div id="transparent-bar" style="float: left; width: 100%; height: 20px; background-color:' . $this->parentObject->doc->bgColor2 . ';">&nbsp;</div>
314 </div>
315 <br />
316 <br /><p>' . $GLOBALS['LANG']->getLL('translation_table_update') . '<br />
317 <em>' . $GLOBALS['LANG']->getLL('translation_full_check_update') . '</em></p><br />
318 <table border="0" cellpadding="2" cellspacing="2">
319 <tr class="t3-row-header"><td>' . $GLOBALS['LANG']->getLL('translation_extension_key') . '</td>
320 ');
321
322 foreach ($selectedLanguages as $lang) {
323 echo '<td>' . $LANG->getLL('lang_' . $lang, 1) . '</td>';
324 }
325 echo '</tr>';
326
327 $counter = 1;
328 foreach ($loadedExtensions as $extKey) {
329 $percentDone = intval(($counter / count($loadedExtensions)) * 100);
330 echo ('
331 <script type="text/javascript">
332 document.getElementById("progress-bar").style.width = "' . $percentDone . '%";
333 document.getElementById("transparent-bar").style.width = "' . (100 - $percentDone) . '%";
334 document.getElementById("progress-message").firstChild.data="' .
335 sprintf($GLOBALS['LANG']->getLL('translation_updating_extension'), $extKey) . '";
336 </script>
337 ');
338
339 flush();
340 $translationStatusArr = $this->parentObject->terConnection->fetchTranslationStatus($extKey, $mirrorURL);
341
342 echo ('<tr class="bgColor4"><td>' . $extKey . '</td>');
343 if (is_array($translationStatusArr)) {
344 foreach ($selectedLanguages as $lang) {
345 // remote unknown -> no l10n available
346 if (!isset($translationStatusArr[$lang])) {
347 echo ('<td title="' . $GLOBALS['LANG']->getLL('translation_no_translation') .
348 '">' . $GLOBALS['LANG']->getLL('translation_n_a') . '</td>');
349 continue;
350 }
351 // determine local md5 from zip
352 if (is_file(PATH_site . 'typo3temp/' . $extKey . '-l10n-' . $lang . '.zip')) {
353 $localmd5 = md5_file(PATH_site . 'typo3temp/' . $extKey . '-l10n-' . $lang . '.zip');
354 } else {
355 $localmd5 = 'zzz';
356 }
357 // local!=remote or not installed -> needs update
358 if ($localmd5 != $translationStatusArr[$lang]['md5']) {
359 $ret = $this->updateTranslation($extKey, $lang, $mirrorURL);
360 if ($ret === true) {
361 echo ('<td title="' . $GLOBALS['LANG']->getLL('translation_has_been_updated') .
362 '" style="background-color:#69a550">' . $GLOBALS['LANG']->getLL('translation_status_update') .
363 '</td>');
364 } else {
365 echo ('<td title="' . htmlspecialchars($ret) .
366 '" style="background-color:#cb3352">' . $GLOBALS['LANG']->getLL('translation_status_error') .
367 '</td>');
368 }
369 continue;
370 }
371 echo ('<td title="' . $GLOBALS['LANG']->getLL('translation_is_ok') .
372 '" style="background-color:#69a550">' . $GLOBALS['LANG']->getLL('translation_status_ok') . '</td>');
373 }
374 } else {
375 echo ('<td colspan="' . count($selectedLanguages) .
376 '" title="' . $GLOBALS['LANG']->getLL('translation_problems') .
377 '">' . $GLOBALS['LANG']->getLL('translation_status_could_not_fetch') . '</td>');
378 }
379 echo ('</tr>');
380 $counter++;
381 }
382 echo '</table>
383 <script type="text/javascript">
384 document.getElementById("progress-message").firstChild.data="' .
385 $GLOBALS['LANG']->getLL('translation_update_done') . '";
386 </script>
387 ';
388
389 // Fix permissions on unzipped language xml files in the entire l10n folder and all subfolders
390 t3lib_div::fixPermissions(PATH_typo3conf . 'l10n', TRUE);
391
392 echo $contentParts[1] . $this->parentObject->doc->endPage();
393 exit;
394 }
395
396 $this->parentObject->content .= $this->parentObject->doc->section($GLOBALS['LANG']->getLL('translation_status'), $content, 0, 1);
397 }
398 }
399
400
401 }
402
403 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/sysext/em/classes/translations/class.tx_em_translations.php'])) {
404 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/sysext/em/classes/translations/class.tx_em_translations.php']);
405 }
406
407 ?>