summaryrefslogtreecommitdiff
path: root/import.php
blob: 7d2c2002546de21d31497aa4fb2aea19807012b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
 * @package languages
 * @subpackage functions
 * @version $Header$
 */

// Copyright (c) 2002-2003, Luis Argerich, Garland Foster, Eduardo Polidor, et. al.
// All Rights Reserved. See below for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details.

/**
 * Initialization
 */
require_once '../kernel/includes/setup_inc.php';

$gBitSystem->verifyPermission( 'p_languages_import' );

$impMsg = [];

$mid = 'bitpackage:languages/import_languages.tpl';

// Lookup translated names for the languages
$impLanguages = $gBitLanguage->listLanguages( true, true );
foreach( array_keys($impLanguages) as $langCode ) {
	if( !$gBitLanguage->isImportFileAvailable( $langCode ) ) {
		unset( $impLanguages[$langCode] );
	}
}
$gBitSmarty->assign('impLanguages', $impLanguages );

if (isset($_REQUEST["exp_language"])) {
	$exp_language = $_REQUEST["exp_language"];
	$gBitSmarty->assign('exp_language', $exp_language);
}

// Import
if (isset($_REQUEST["import"])) {
	if( !empty( $_REQUEST['imp_languages'] ) ) {
		foreach( $_REQUEST['imp_languages'] as $impLang ) {
			if( $gBitLanguage->importTranslationStrings( $impLang, ( $_REQUEST['overwrite'] ) == 'y' )) {
				$impMsg['success'][] = "Imported lang/" . $impLang . "/language.php";
			} else {
				$impMsg['error'][] = "Language could not be imported";
			}
		}
	}

	if( !empty( $_REQUEST["import_master"] ) && $gBitUser->isAdmin() ) {
		$gBitLanguage->importMasterStrings( $_REQUEST['overwrite'] );
		$impMsg['success'] = "Imported lang/masters.php";
	}

	if( !empty( $_FILES['upload_file']['tmp_name'] ) ) {
		$gBitLanguage->importTranslationStrings( $_REQUEST['upload_lang_code'], ( $_REQUEST['overwrite'] == 'y' ), 'i18n_strings`', $_FILES['upload_file']['tmp_name'] );
	}

	if( ($_REQUEST['overwrite'] == 'r') && !empty( $gBitLanguage->mImportConflicts ) ) {
		unset( $impMsg['error'] );
		$impMsg['warning'][] = tra( "Conflicts occured during language import" );
		$gBitSmarty->assign( 'impConflicts', $gBitLanguage->mImportConflicts );
		$mid = 'bitpackage:languages/import_resolve.tpl';
	}

} elseif (isset($_REQUEST["resolve"])) {
	if( !empty( $_REQUEST['conflict'] ) ) {
		foreach( array_keys( $_REQUEST['conflict'] ) as $langCode ) {
			foreach( array_keys( $_REQUEST['conflict'][$langCode] ) as $sourceHash ) {
				if( !empty( $_REQUEST['conflict'][$langCode][$sourceHash] ) ) {
					$gBitLanguage->storeTranslationString( $langCode, $_REQUEST['conflict'][$langCode][$sourceHash], $sourceHash );
				}
			}
		}
		$impMsg['success'][] = "Language conflicts have been resolved.";
	}
} elseif (isset($_REQUEST["export"])) {
	$langCode = $_REQUEST['export_lang_code'];
	$gBitLanguage->loadLanguage( $langCode );
	$data  = "<?php\n";
	$data .= "// Save this to languages/lang/(lang_code)/language.php where lang_code is the language your are downloading.\n\n";
	$data .= "\$lang=Array(\n";

	//vd($gBitLanguage->mStrings);
	foreach( $gBitLanguage->mStrings[$_REQUEST['export_lang_code']] as $tran ) {
		if( !empty( $_REQUEST['all_trans'] ) ||  ( $tran['version'] == BIT_MAJOR_VERSION && !empty( $tran['trans'] ))) {
			if( !empty( $_REQUEST['include_empty'] ) || !empty( $tran['trans'] )) {
				$data .= "'" . str_replace( "'", "\\'", stripslashes( $tran["source"] )) . "' => '" . str_replace( "'", "\\'",stripslashes( $tran["trans"] )) . "',\n";
			}
		}
	}

	$data = $data . ");\n?>";
	if( $_REQUEST['target'] == 'download' ) {
		header ("Content-type: application/unknown");
		header ("Content-Disposition: attachment; filename=language-".$langCode.".php");
		echo $data;
		exit (0);
	} else {
		// This file MUST be name "...txt" for security reasons.
		// if the file ended with .php - an evil editor could enter evil shit into a translation, and export it to your temp dir, and then execute it.
		// XOXO spiderr
		$fileName = 'lang/'.$langCode.'-language.php.txt';
		$file = fopen( TEMP_PKG_PATH.$fileName, "w" );
		fwrite( $file, $data );
		fclose( $file );
		$impMsg['success'] = "Language file has been exported to <a href=\"".TEMP_PKG_URL.$fileName."\">$fileName</a>";
	}

	// unset this massive array to free up memory
	unset( $gBitLanguage->mStrings[$_REQUEST['export_lang_code']] );
}

// Get languages that can be exported
$expLanguages = $gBitLanguage->getImportedLanguages();
$gBitSmarty->assign('expLanguages', $expLanguages );

$gBitSmarty->assign( 'impmsg', $impMsg );

$gBitSystem->display( $mid, 'Languages Im/Export' , array( 'display_mode' => 'display' ));

?>