Till Brehm
2015-06-03 5af0cfd99a13fda9afad3380b0c50a3428acd299
commit | author | age
8dd29e 1 <?php
T 2 /*
3 Copyright (c) 2008, Till Brehm, projektfarm Gmbh
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without modification,
7 are permitted provided that the following conditions are met:
8
9     * Redistributions of source code must retain the above copyright notice,
10       this list of conditions and the following disclaimer.
11     * Redistributions in binary form must reproduce the above copyright notice,
12       this list of conditions and the following disclaimer in the documentation
13       and/or other materials provided with the distribution.
14     * Neither the name of ISPConfig nor the names of its contributors
15       may be used to endorse or promote products derived from this software without
16       specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
7fe908 30 require_once '../../lib/config.inc.php';
MC 31 require_once '../../lib/app.inc.php';
8dd29e 32
T 33 //* Check permissions for module
34 $app->auth->check_module_permissions('admin');
9edea9 35 $app->auth->check_security_permissions('admin_allow_langedit');
91624b 36 if($conf['demo_mode'] == true) $app->error('This function is disabled in demo mode.');
8dd29e 37
T 38 //* This is only allowed for administrators
39 if(!$app->auth->is_admin()) die('only allowed for administrators.');
40
41 $app->uses('tpl');
42
43 $app->tpl->newTemplate('form.tpl.htm');
44 $app->tpl->setInclude('content_tpl', 'templates/language_complete.htm');
45
46 //* reading languages
47 $language_option = '';
48 $error = '';
49 $msg = '';
7fe908 50 $selected_language = (isset($_REQUEST['lng_select']))?substr($_REQUEST['lng_select'], 0, 2):'en';
8dd29e 51 if(!preg_match("/^[a-z]{2}$/i", $selected_language)) die('unallowed characters in selected language name.');
T 52
7fe908 53 $handle = opendir(ISPC_ROOT_PATH.'/lib/lang/');
MC 54 while ($file = readdir($handle)) {
55     if ($file != '.' && $file != '..') {
56         $tmp_lng = substr($file, 0, -4);
8dd29e 57         if($tmp_lng !='' && $tmp_lng != 'en') {
T 58             $selected = ($tmp_lng == $selected_language)?'SELECTED':'';
59             $language_option .= "<option value='$tmp_lng' $selected>$tmp_lng</option>";
60             //if(isset($_POST['lng_new']) && $_POST['lng_new'] == $tmp_lng) $error = 'Language exists already.';
61         }
62     }
63 }
7fe908 64 $app->tpl->setVar('language_option', $language_option);
MC 65 $app->tpl->setVar('error', $error);
8dd29e 66
T 67 // Export the language file
68 if(isset($_POST['lng_select']) && $error == '') {
7fe908 69
5af0cf 70     //* CSRF Check
TB 71     $app->auth->csrf_token_check();
72     
8dd29e 73     // complete the global langauge file
7fe908 74     merge_langfile(ISPC_LIB_PATH."/lang/".$selected_language.".lng", ISPC_LIB_PATH."/lang/en.lng");
MC 75
8dd29e 76     // Go trough all language files
T 77     $bgcolor = '#FFFFFF';
78     $language_files_list = array();
7fe908 79     $handle = @opendir(ISPC_WEB_PATH);
MC 80     while ($file = @readdir($handle)) {
81         if ($file != '.' && $file != '..') {
82             if(@is_dir(ISPC_WEB_PATH.'/'.$file.'/lib/lang')) {
8dd29e 83                 $handle2 = opendir(ISPC_WEB_PATH.'/'.$file.'/lib/lang');
7fe908 84                 while ($lang_file = @readdir($handle2)) {
MC 85                     if ($lang_file != '.' && $lang_file != '..' && substr($lang_file, 0, 2) == 'en') {
86                         $target_lang_file = $selected_language.substr($lang_file, 2);
87                         merge_langfile(ISPC_WEB_PATH.'/'.$file.'/lib/lang/'.$target_lang_file, ISPC_WEB_PATH.'/'.$file.'/lib/lang/'.$lang_file);
8dd29e 88                     }
T 89                 }
dea284 90                 $handle2 = opendir(ISPC_WEB_PATH.'/'.$file.'/lib/lang');
7fe908 91                 while ($lang_file = @readdir($handle2)) {
MC 92                     if ($lang_file != '.' && $lang_file != '..' && substr($lang_file, 0, 2) == $selected_language) {
93                         $master_lang_file=ISPC_WEB_PATH.'/'.$file.'/lib/lang/en'.substr($lang_file, 2);
dea284 94                         $target_lang_file=ISPC_WEB_PATH.'/'.$file.'/lib/lang/'.$lang_file;
F 95                         if(!file_exists($master_lang_file)){
7fe908 96                             unlink($target_lang_file);
MC 97                             $msg.="File $target_lang_file removed because does not exist in master language<br />";
dea284 98                         }
F 99                     }
100                 }//Finish of remove the files how not exists in master language
8dd29e 101             }
T 102         }
103     }
7fe908 104     if($msg=='')
MC 105         $msg="No files created, removed or modified<br />";
8dd29e 106 }
T 107
7fe908 108 function merge_langfile($langfile, $masterfile) {
8dd29e 109     global $msg;
7fe908 110
8dd29e 111     if(is_file($langfile)) {
7fe908 112
8dd29e 113         // Load the english language file
7fe908 114         include $masterfile;
8dd29e 115         if(isset($wb) && is_array($wb)) {
T 116             $wb_master = $wb;
117             unset($wb);
118         } else {
119             $wb_master = array();
120         }
7fe908 121
8dd29e 122         // Load the incomplete language file
T 123         $wb = array();
7fe908 124         include $langfile;
MC 125
8dd29e 126         $n = 0;
T 127         foreach($wb_master as $key => $val) {
128             if(!isset($wb[$key])) {
129                 $wb[$key] = $val;
130                 $n++;
131             }
132         }
7fe908 133
c2d926 134         $r = 0;
F 135         foreach($wb as $key => $val) {
136             if(!isset($wb_master[$key])) {
137                 unset($wb[$key]);
138                 $r++;
139             }
140         }
7fe908 141
8dd29e 142         $file_content = "<?php\n";
T 143         foreach($wb as $key => $val) {
7fe908 144             $val = str_replace("'", "\\'", $val);
MC 145             $val = str_replace('"', '\"', $val);
8dd29e 146             $file_content .= '$wb['."'$key'".'] = '."'$val';\n";
T 147         }
148         $file_content .= "?>\n";
7fe908 149
15b753 150         if($n!=0)
7fe908 151             $msg .= "Added $n lines to the file $langfile<br />";
c2d926 152         if($r!=0)
7fe908 153             $msg .= "Removed $r lines to the file $langfile<br />";
MC 154         file_put_contents($langfile , $file_content);
8dd29e 155     } else {
T 156         $msg .= "File does not exist yet. Copied file $masterfile to $langfile<br />";
7fe908 157         copy($masterfile, $langfile);
8dd29e 158     }
T 159 }
160
7fe908 161 $app->tpl->setVar('msg', $msg);
8dd29e 162
5af0cf 163 //* SET csrf token
TB 164 $csrf_token = $app->auth->csrf_token_get('language_merge');
165 $app->tpl->setVar('_csrf_id',$csrf_token['csrf_id']);
166 $app->tpl->setVar('_csrf_key',$csrf_token['csrf_key']);
167
7fe908 168 //* load language file
8dd29e 169 $lng_file = 'lib/lang/'.$_SESSION['s']['language'].'_language_complete.lng';
7fe908 170 include $lng_file;
8dd29e 171 $app->tpl->setVar($wb);
T 172
173 $app->tpl_defaults();
174 $app->tpl->pparse();
175
176
7fe908 177 ?>