Location: PHPKode > projects > PhpMyForum > phpmyforum_4_1_1/admin/lang.php
<?php
define('PMF_INCLUDE', 1);
### Import || Export ?
if (!empty($_GET['action']) && ($_GET['action'] == 'import' || $_GET['action'] == 'export')) {
    define('PMF_NO_HEADER', true);
}
require('head.php');
access('acp_lang');

require_once($_cfg['MAIN'].'/lib/sys.inc.php');
$_sys = new pmf_sys();
require($_cfg['MAIN'].'/lib/admin.inc.php');


#
#
### Übersicht (Sprachen)
if (empty($_POST) && empty($_GET['action'])) {
    // Anzahl Vars
    $vars = $_db->fetch_first('SELECT COUNT(id) AS `count`
                               FROM `'.$_cfg['DB_PREFIX'].'lang_var`');
    // Sprachen
    $result = $_db->query('SELECT l.*, COUNT(v.`lang_id`) AS `count`
                           FROM `'.$_cfg['DB_PREFIX'].'lang` AS l
                                LEFT JOIN `'.$_cfg['DB_PREFIX'].'lang_value` AS v ON l.`id` = v.`lang_id`
                           GROUP BY l.id
                           ORDER BY l.`name` ASC');
    $lang_lines = '';
    while ($Daten = $_db->fetch_array($result)) {
        $lang_lines .= eval ('return ("'.$_style->get('acp_lang_line').'");');
    }
    eval('echo "'.$_style->get('acp_lang').'";');



#
#
### Sprache bearbeiten / kopieren / hinzufügen (form)
} elseif ($_GET['action'] == 'editlang' || $_GET['action'] == 'copylang' || $_GET['action'] == 'addlang') {
    $Daten = $_db->fetch_first('SELECT * FROM `'.$_cfg['DB_PREFIX'].'lang` WHERE id = '.intval($_GET['id']));
    $choice[$Daten['choice']] = $_style->cfg['template_choice'];
    
    eval('echo "'.$_style->get('acp_lang_'.substr($_GET['action'], 0, -4)).'";');



#
#
### Sprache speichern
} elseif ($_GET['action'] == 'savelang') {
    ### hinzufügen / kopieren
    if (!is_numeric($_POST['id'])) {
        $_db->query('INSERT INTO `'.$_cfg['DB_PREFIX'].'lang` (`name`,`code`,`choice`)
                     VALUES (\''.gpc_addslashes($_POST['name']).'\',\''.gpc_addslashes($_POST['code']).'\','.intval($_POST['choice']).')');
        $id = $_db->insert_id();
        
        # kopieren
        if (is_numeric($_POST['copy'])) {
            // übersetze Variablen
            $result = $_db->query('SELECT `var_id`, `replacement`
                                   FROM `'.$_cfg['DB_PREFIX'].'lang_value`
                                   WHERE `lang_id` = '.$_POST['copy']);
            $val_lines = array();
            while ($Daten = $_db->fetch_array($result)) {
                $val_lines[] = '('.$Daten['var_id'].','.$id.',\''.addslashes($Daten['replacement']).'\')';
            }
            if (count($val_lines) > 0) {
                $_db->u_query('INSERT INTO `'.$_cfg['DB_PREFIX'].'lang_value` (`var_id`,`lang_id`,`replacement`)
                               VALUES '.implode(',', $val_lines));
            }
        }
        
        
    ### bearbeiten
    } elseif ($_POST['id']) {
        $_db->u_query('UPDATE `'.$_cfg['DB_PREFIX'].'lang`
                       SET `name` = \''.gpc_addslashes($_POST['name']).'\',
                           `code` = \''.gpc_addslashes($_POST['code']).'\',
                           `choice` = '.(($_config['LANG_DEFAULT'] != $_POST['id']) ? intval($_POST['choice']) : 1).'
                       WHERE `id` = '.intval($_POST['id']));
    }
    msg('error_acp_save', 'lang.php?'.$_sess['url']);



#
#
### Sprachvariablen - Werte (form)
} elseif ($_GET['action'] == 'values' && is_numeric($_GET['id'])) {
    ### Sprache
    $lang = $_db->fetch_first('SELECT *
                               FROM `'.$_cfg['DB_PREFIX'].'lang`
                               WHERE `id` = '.$_GET['id']);
    
    ### Dateien
    $result = $_db->query('SELECT f.*
                           FROM `'.$_cfg['DB_PREFIX'].'lang_file` AS f,
                                `'.$_cfg['DB_PREFIX'].'lang_index` AS i,
                                `'.$_cfg['DB_PREFIX'].'lang_var` AS v
                           WHERE i.`file_id` = f.`id`
                             AND v.`id` = i.`var_id`
                           GROUP BY f.`id`
                           ORDER BY f.`name`');
    $file_lines = '';
    while ($Daten = $_db->fetch_array($result)) {
        $file_lines .= '<option value="'.$Daten['id'].'"';
        $file_lines .= ($_GET['file'] == $Daten['id']) ? ' selected="selected"' : '';
        $file_lines .= '>'.$Daten['name'].' ('.$Daten['comment'].')</option>';
    }
    
    ### Variablen anzeigen
    if (is_numeric($_GET['file'])) {
        $result = $_db->query('SELECT var.*, val.`replacement`
                               FROM `'.$_cfg['DB_PREFIX'].'lang_index` AS i
                                    LEFT JOIN `'.$_cfg['DB_PREFIX'].'lang_var` AS var ON i.`var_id` = var.`id`
                                    LEFT JOIN `'.$_cfg['DB_PREFIX'].'lang_value` AS val ON (var.`id` = val.`var_id`
                                              AND val.`lang_id` = '.$_GET['id'].')
                               WHERE i.`file_id` = \''.$_GET['file'].'\'
                               GROUP BY var.`id`
                               ORDER BY var.`name` ASC');
        $Daten['count'] = $_db->num_rows($result);
        
    ### Suchergebnis
    } elseif (!empty($_GET['search'])) {
        $result = $_db->query('SELECT var.*, val.`replacement`
                               FROM `'.$_cfg['DB_PREFIX'].'lang_index` AS i
                                    LEFT JOIN `'.$_cfg['DB_PREFIX'].'lang_var` AS var ON i.`var_id` = var.`id`
                                    LEFT JOIN `'.$_cfg['DB_PREFIX'].'lang_value` AS val ON (var.`id` = val.`var_id`
                                              AND val.`lang_id` = '.$_GET['id'].')
                               WHERE (val.`replacement` LIKE \'%'.gpc_addslashes( urldecode($_GET['search'])).'%\'
                                      '.(($_GET['default']) ? ' OR var.`default` LIKE \'%'.gpc_addslashes( urldecode($_GET['search'])).'%\'' : '').')
                                 '.(($_GET['name']) ? 'AND var.`name` REGEXP \''.gpc_addslashes($_GET['name']).'\'' : '').'
                               GROUP BY var.`id`
                               ORDER BY var.`name` ASC');
        $Daten['count'] = $_db->num_rows($result);
    }
    
    ### Variablen
    if ($Daten['count'] > 0) {
        $value_lines = '';
        $n = 1;
        while ($Daten = $_db->fetch_array($result)) {
            $Daten['default'] = htmlspecialchars($Daten['default'], ENT_QUOTES);
            $Daten['replacement'] = htmlentities($Daten['replacement'], ENT_QUOTES);
            $value_lines .= eval('return ("'.$_style->get('acp_lang_values_line').'");');
            $n = ($n == 2) ? 1 : 2;
        }
    }
    
    eval('echo "'.$_style->get('acp_lang_values').'";');



#
#
### Sprachvariablen - Werte (speichern)
} elseif ($_GET['action'] == 'savevalues' && is_array($_POST['var']) && is_numeric($_POST['lang_id'])) {
    ### alte Werte löschen
    $_db->u_query('DELETE FROM `'.$_cfg['DB_PREFIX'].'lang_value`
                   WHERE `var_id` IN ('.implode(',', gpc_addslashes(array_keys($_POST['var']))).')
                     AND `lang_id` = '.$_POST['lang_id']);
    
    ### insert "basteln"
    $values = array();
    foreach ($_POST['var'] AS $id => $value) {
        if (strlen($value) > 0) {
    	   $values[] = '('.$id.','.$_POST['lang_id'].',\''.gpc_addslashes($value).'\')';
        }
    }
    
    ### insert
    if (!empty($values)) {
        $_db->u_query('INSERT INTO `'.$_cfg['DB_PREFIX'].'lang_value` (`var_id`,`lang_id`,`replacement`)
                       VALUES '.implode(',', $values));
    }
    
    # Cache Dateien löschen
    unlink_lang_cache();
    
    msg('error_acp_save', 'lang.php?action=values&id='.$_POST['lang_id'].'&'.$_sess['url']);



#
#
### Sprachvariablen - Variablen (form)
} elseif ($_GET['action'] == 'vars') {
    ### Dateien
    $result = $_db->query('SELECT f.*
                           FROM `'.$_cfg['DB_PREFIX'].'lang_file` AS f,
                                `'.$_cfg['DB_PREFIX'].'lang_index` AS i,
                                `'.$_cfg['DB_PREFIX'].'lang_var` AS v
                           WHERE i.`file_id` = f.`id`
                             AND v.`id` = i.`var_id`
                           GROUP BY f.`id`
                           ORDER BY f.`name`');
    $file_lines = '';
    while ($Daten = $_db->fetch_array($result)) {
        $file_lines .= '<option value="'.$Daten['id'].'"';
        $file_lines .= ($_GET['file'] == $Daten['id']) ? ' selected="selected"' : '';
        $file_lines .= '>'.$Daten['name'].' ('.$Daten['comment'].')</option>';
    }
    
    ### Variablen anzeigen
    if (is_numeric($_GET['file']) || $_GET['file'] == '%') {
        $result = $_db->query('SELECT v.*
                               FROM `'.$_cfg['DB_PREFIX'].'lang_var` AS v
                                    LEFT JOIN `'.$_cfg['DB_PREFIX'].'lang_index` AS i ON v.id = i.var_id
                               WHERE (i.file_id LIKE \''.$_GET['file'].'\' OR i.file_id IS NULL)
                               GROUP BY v.id
                               ORDER BY v.`name` ASC');
        $Daten['count'] = $_db->num_rows($result);
    }
    
    ### Variablen
    if ($Daten['count'] > 0) {
        $var_lines = '';
        while ($var = $_db->fetch_array($result)) {
            $var_lines .= '<option value="'.$var['id'].'">'.$var['name'].'</option>';
        }
    }
    
    eval('echo "'.$_style->get('acp_lang_vars').'";');



#
#
### Variable hinzufügen / bearbeiten / kopieren (form)
} elseif ((($_GET['action'] == 'editvar' || $_GET['action'] == 'copyvar') && is_numeric($_GET['var_id'])) || $_GET['action'] == 'addvar') {
    ### bearbeiten / kopieren
    if ($_GET['action'] == 'editvar' || $_GET['action'] == 'copyvar') {
        $Daten = $_db->fetch_first('SELECT v.*
                                    FROM `'.$_cfg['DB_PREFIX'].'lang_var` AS v
                                    WHERE v.`id` = '.$_GET['var_id']);
        $Daten['name'] = htmlentities($Daten['name'], ENT_QUOTES);
        $Daten['default'] = htmlentities($Daten['default'], ENT_QUOTES);
        
        // Dateien
        $result = $_db->query('SELECT f.*, COUNT(i.`file_id`) AS `selected`
                               FROM `'.$_cfg['DB_PREFIX'].'lang_file` AS f
                                    LEFT JOIN `'.$_cfg['DB_PREFIX'].'lang_index` AS i ON (f.`id` = i.`file_id` AND i.`var_id` = '.$_GET['var_id'].')
                               GROUP BY f.id
                               ORDER BY `selected` DESC, f.`name`');
        $template = ($_GET['action'] == 'editvar') ? 'acp_lang_vars_edit' : 'acp_lang_vars_copy';
        
    ### hinzufügen
    } else {
        // Dateien
        $result = $_db->query('SELECT f.*
                               FROM `'.$_cfg['DB_PREFIX'].'lang_file` AS f
                               ORDER BY f.`name`');
        $template = 'acp_lang_vars_add';
    }
    
    ### Allgemein
    // Dateien
    $file_lines = '';
    while ($file = $_db->fetch_array($result)) {
        $file_lines .= '<option value="'.$file['id'].'"';
        $file_lines .= ($file['selected']) ? ' selected="selected"' : '';
        $file_lines .= '>'.$file['name'].' ('.$file['comment'].')</option>';
    }
    
    eval('echo "'.$_style->get($template).'";');



#
#
### Variable speichern
} elseif ($_GET['action'] == 'savevar') {
    ### Name auf Sonderzeichen überprüfen
    if (!preg_match('/^[_a-z0-9]+$/i', gpc_stripslashes($_POST['name']))) {
        msg('error_acp_lang_name_fault', 'back()');
    }
    
    ### schon Vorhanden ?
    $count = $_db->fetch_first('SELECT COUNT(*) AS `anz`
                                FROM `'.$_cfg['DB_PREFIX'].'lang_var`
                                WHERE `name` = \''.gpc_addslashes($_POST['name']).'\'');
    ### hinzufügen / kopieren
    if (!is_numeric($_POST['id'])) {
        if ($count['anz'] > 0) msg('error_acp_lang_name_fault', 'back()');
        
        $_db->query('INSERT INTO `'.$_cfg['DB_PREFIX'].'lang_var` (`name`,`default`)
                     VALUES (\''.gpc_addslashes($_POST['name']).'\',\''.gpc_addslashes($_POST['default']).'\')');
        $id = $_db->insert_id();
        
    ### bearbeiten
    } else {
        $id = intval($_POST['id']);
        $old = $_db->fetch_first('SELECT `name`
                                  FROM `'.$_cfg['DB_PREFIX'].'lang_var`
                                  WHERE `id` = '.$id);
        if (($old['name'] != $_POST['name'] && $count['anz'] > 0) || $count['anz'] > 1) msg('error_acp_lang_name_fault', 'back()');
        
        // Variable
        $_db->u_query('UPDATE `'.$_cfg['DB_PREFIX'].'lang_var`
                       SET `name` = \''.gpc_addslashes($_POST['name']).'\',
                           `default` = \''.gpc_addslashes($_POST['default']).'\'
                       WHERE `id` = '.$id);
        // Datei - Index löschen
        $_db->u_query('DELETE FROM `'.$_cfg['DB_PREFIX'].'lang_index` WHERE `var_id` = '.$id);
    }
    
    # Dateien - Index hinzufügen
    if (count($_POST['files']) > 0) {
        $files = array();
        foreach ($_POST['files'] AS $file_id) {
            $files[] = '('.$id.', '.$file_id.')';
        }
        $_db->u_query('INSERT INTO `'.$_cfg['DB_PREFIX'].'lang_index` (`var_id`,`file_id`)
                       VALUES '.implode(',', $files));
    }
    
    # Cache Dateien löschen
    unlink_lang_cache();

    msg('error_acp_save', 'lang.php?action=vars&'.$_sess['url']);



#
#
### Variable löschen
} elseif ($_GET['action'] == 'delvar' && is_numeric($_GET['var_id'])) {
    $Daten = $_db->fetch_first('SELECT *
                                FROM `'.$_cfg['DB_PREFIX'].'lang_var`
                                WHERE `id` = '.$_GET['var_id']);
    
    ### Formular anzeigen
    if (!$_GET['ok']) {
        eval('echo "'.$_style->get('acp_lang_vars_del').'";');
        
    ### wirklich löschen
    } else {
        // Cache löschen
        unlink_lang_cache();
        
        $_db->u_query('DELETE FROM `'.$_cfg['DB_PREFIX'].'lang_var` WHERE `id` = '.$_GET['var_id']);
        $_db->u_query('DELETE FROM `'.$_cfg['DB_PREFIX'].'lang_index` WHERE `var_id` = '.$_GET['var_id']);
        $_db->u_query('DELETE FROM `'.$_cfg['DB_PREFIX'].'lang_value` WHERE `var_id` = '.$_GET['var_id']);
        
        msg('error_acp_del', 'lang.php?action=vars&'.$_sess['url']);
    }



#
#
### Sprache löschen
} elseif ($_GET['action'] == 'dellang' && is_numeric($_GET['id'])) {
    $Daten = $_db->fetch_first('SELECT *
                                FROM `'.$_cfg['DB_PREFIX'].'lang`
                                WHERE `id` = '.$_GET['id']);
    
    ### löschen moeglich ?
    $anz = $_db->fetch_first('SELECT COUNT(*) AS `count` FROM `'.$_cfg['DB_PREFIX'].'lang`');
    if ($anz['count'] < 2 || $_GET['id'] == $_config['LANG_DEFAULT']) msg('error_acp_lang_del_fault', 'back()');
    
    ### Formular anzeigen
    if (!$_GET['ok']) {
        eval('echo "'.$_style->get('acp_lang_del').'";');
        
    ### wirklich löschen
    } else {
        // Cache löschen
        $d = dir($_cfg['MAIN'].'/cache/lang/');
        while ($file = $d->read()) {
            if (substr($file, 0, strlen($_GET['id'])+1) == $_GET['id'].'_') @unlink($_cfg['MAIN'].'/cache/lang/'.$file);
        }
        
        $_db->u_query('DELETE FROM `'.$_cfg['DB_PREFIX'].'lang_value` WHERE `lang_id` = '.$_GET['id']);
        $_db->u_query('DELETE FROM `'.$_cfg['DB_PREFIX'].'lang` WHERE `id` = '.$_GET['id']);
        $_db->u_query('UPDATE `'.$_cfg['DB_PREFIX'].'user` SET `lang` = '.$_config['LANG_DEFAULT'].' WHERE `lang` = '.$_GET['id']);
        
        msg('error_acp_del', 'lang.php?'.$_sess['url']);
    }



#
#
### Übersetzen (form && speichern)
} elseif ($_GET['action'] == 'translate') {
    
    ### Allgemeines Formular
    if (!is_numeric($_POST['source']) || !is_numeric($_POST['target']) || !is_numeric($_POST['limit'])) {
        // Sprachen
        $result = $_db->query('SELECT id, name
                               FROM `'.$_cfg['DB_PREFIX'].'lang`
                               ORDER BY name ASC');
        $lang_lines = '';
        while ($Daten = $_db->fetch_array($result)) {
            $lang_lines .= '<option value="'.$Daten['id'].'">'.$Daten['name'].'</option>';
        }
        eval('echo "'.$_style->get('acp_lang_translate').'";');
        
    ### Liste Übersetzung (und Speichern)
    } else {
        ### Speichern
        if (is_array($_POST['var'])) {
            // "basteln"
            $values = array();
            foreach ($_POST['var'] AS $var_id => $value) {
                if (strlen($value) > 0) {
                	$values[] = '('.intval($var_id).','.$_POST['target'].',\''.gpc_addslashes($value).'\')';
                }
            }
            // insert
            if (!empty($values)) {
                $_db->query('REPLACE INTO `'.$_cfg['DB_PREFIX'].'lang_value` (`var_id`,`lang_id`,`replacement`)
                             VALUES '.implode(',', $values));
            }
        }
        
        ### Liste
        // Quelle
        $source = $_db->fetch_first('SELECT *
                                     FROM `'.$_cfg['DB_PREFIX'].'lang`
                                     WHERE `id` = '.$_POST['source']);
        // Ziel
        $target = $_db->fetch_first('SELECT *
                                     FROM `'.$_cfg['DB_PREFIX'].'lang`
                                     WHERE `id` = '.$_POST['target']);
        // Liste
        $result = $_db->query('SELECT var.*, v1.`replacement`, v2.`replacement` AS `test`
                               FROM `'.$_cfg['DB_PREFIX'].'lang_var` AS var
                                    LEFT JOIN `'.$_cfg['DB_PREFIX'].'lang_value` AS v1 ON (var.`id` = v1.`var_id` AND v1.`lang_id` = '.$_POST['source'].')
                                    LEFT JOIN `'.$_cfg['DB_PREFIX'].'lang_value` AS v2 ON (var.`id` = v2.`var_id` AND v2.`lang_id` = '.$_POST['target'].')
                               WHERE v2.`replacement` IS NULL
                               ORDER BY var.`id`
                               LIMIT '.$_POST['limit']);
        // fertig ?
        if ($_db->num_rows($result) < 1) msg('error_acp_lang_translate', 'lang.php?'.$_sess['url']);
        $translate_lines = '';
        $n = 1;
        while ($Daten = $_db->fetch_array($result)) {
            $Daten['replacement'] = empty($Daten['replacement']) ? $Daten['default'] : $Daten['replacement'];
            $Daten['replacement_enc'] = urlencode($Daten['replacement']);
            $Daten['replacement'] = htmlentities($Daten['replacement'], ENT_QUOTES);
            $translate_lines .= eval('return ("'.$_style->get('acp_lang_translate_list_line').'");');
            $n = ($n == 2) ? 1 : 2;
        }
        
        eval('echo "'.$_style->get('acp_lang_translate_list').'";');
    }



#
#
### Sprachdateien (form)
} elseif ($_GET['action'] == 'files') {
    $result = $_db->query('SELECT *
                           FROM `'.$_cfg['DB_PREFIX'].'lang_file`
                           ORDER BY name ASC');
    $file_lines = '';
    while ($Daten = $_db->fetch_array($result)) {
        $file_lines .= eval ('return ("'.$_style->get('acp_lang_files_line').'");');
    }
    eval('echo "'.$_style->get('acp_lang_files').'";');



#
#
### Sprachdatei hinzufügen
} elseif ($_GET['action'] == 'addfile') {
    ### Formular
    if (!$_POST['name']) {
        eval('echo "'.$_style->get('acp_lang_files_add').'";');
        
    ### Speichern
    } else {
        $_db->u_query('INSERT INTO `'.$_cfg['DB_PREFIX'].'lang_file` (`name`,`comment`)
                       VALUES (\''.gpc_addslashes($_POST['name']).'\',\''.gpc_addslashes($_POST['comment']).'\')');
        
        msg('error_acp_save', 'lang.php?action=files&'.$_sess['url']);
    }



#
#
### Sprachdatei bearbeiten
} elseif ($_GET['action'] == 'editfile' && is_numeric($_GET['id'])) {
    ### Formular
    if (!$_POST['name']) {
        $Daten = $_db->fetch_first('SELECT * FROM `'.$_cfg['DB_PREFIX'].'lang_file` WHERE id = '.$_GET['id']);
        $Daten['name'] = htmlentities($Daten['name'], ENT_QUOTES);
        $Daten['comment'] = htmlentities($Daten['comment'], ENT_QUOTES);
        
        eval('echo "'.$_style->get('acp_lang_files_edit').'";');
        
    ### Speichern
    } else {
        $_db->u_query('UPDATE `'.$_cfg['DB_PREFIX'].'lang_file`
                       SET `name` = \''.gpc_addslashes($_POST['name']).'\',
                           `comment` = \''.gpc_addslashes($_POST['comment']).'\'
                       WHERE id = '.$_GET['id']);
        
        msg('error_acp_save', 'lang.php?action=files&'.$_sess['url']);
    }



#
#
### Sprachdatei löschen
} elseif ($_GET['action'] == 'delfile' && is_numeric($_GET['id'])) {
    $Daten = $_db->fetch_first('SELECT *
                                FROM `'.$_cfg['DB_PREFIX'].'lang_file`
                                WHERE `id` = '.$_GET['id']);
    
    ### Formular
    if (!$_GET['ok']) {
        eval('echo "'.$_style->get('acp_lang_files_del').'";');
        
    ### löschen
    } else {
        # Cache Dateien löschen
        $exp = explode('/', substr($Daten['name'], 1));
        $cache_file = substr( (($exp[0] == 'admin') ? 'acp_'.$exp[1] : $exp[0]), 0, -4);
        $d = dir($_cfg['MAIN'].'/cache/lang/');
        while ($file = $d->read()) {
            if ($file != '.' && $file != '..' && preg_match("/^[0-9]+_".$cache_file."\.php$/i", $file)) {
                @unlink($_cfg['MAIN'].'/cache/lang/'.$file);
            }
        }
        
        $_db->u_query('DELETE FROM `'.$_cfg['DB_PREFIX'].'lang_file` WHERE id = '.$_GET['id']);
        $_db->u_query('DELETE FROM `'.$_cfg['DB_PREFIX'].'lang_index` WHERE file_id = '.$_GET['id']);
        
        msg('error_acp_del', 'lang.php?action=files&'.$_sess['url']);
    }



#
#
### Import / Export (form)
} elseif ($_GET['action'] == 'import/export') {
    // Sprachen
    $result = $_db->query('SELECT `id`, `name`
                           FROM `'.$_cfg['DB_PREFIX'].'lang`
                           ORDER BY `name` ASC');
    $lang_lines = array();
    while ($lng = $_db->fetch_array($result)) {
        $lang_lines[$tmpl['id']] = '<option value="'.$lng['id'].'"';
        $lang_lines[$tmpl['id']] .= ($lng['id'] == $_GET['lang_id']) ? ' selected="selected"' : '';
        $lang_lines[$tmpl['id']] .= '>'.$lng['name'].'</option>';
    }
    $Daten['lang_id'] = implode("\n", $lang_lines);
    
    // lokale Quellen
    $Daten['source_local'] = upload_liste($_cfg['MAIN'].'/upload', 'source_local', null, '.pmflng');
    
    // Templates (Export)
    if (is_numeric($_GET['templateset'])) {
        $tpl_set = $_db->fetch_first('SELECT typ
                                      FROM `'.$_cfg['DB_PREFIX'].'template_set`
                                      WHERE id = '.$_GET['templateset']);
        $tpls = $_admin->search($_GET['templateset'], $tpl_set['typ']);
        $templates = array();
        foreach ($tpls AS $name) {
            $templates[] = '<option value="'.$name.'" selected="selected">'.$name.'</option>';
        }
        $Daten['templates'] = implode("\n", $templates);
    }
    
    eval ('echo "'.$_style->get('acp_lang_import_export').'";');



#
#
### Export (wirklich)
} elseif ($_GET['action'] == 'export' && is_numeric($_POST['lang_id'])) {
    
    # Sprache abfragen
    $lang = $_db->fetch_first('SELECT `name`,`code`,`choice`
                               FROM `'.$_cfg['DB_PREFIX'].'lang`
                               WHERE `id` = '.$_POST['lang_id']);
    
    # 'Files' abfragen
    $result = $_db->query('SELECT *
                           FROM `'.$_cfg['DB_PREFIX'].'lang_file`');
    $files = array();
    while ($Daten = $_db->fetch_array($result)) {
    	$files[$Daten['name']] = array('id' => $Daten['id'],
    	                               'comment' => $Daten['comment']);
    }
    
    # 'Vars' abfragen
    $result = $_db->query('SELECT *
                           FROM `'.$_cfg['DB_PREFIX'].'lang_var`');
    $vars = array();
    while ($Daten = $_db->fetch_array($result)) {
    	$vars[$Daten['name']] = array('id' => $Daten['id'],
    	                              'default' => $Daten['default']);
    }
    
    # 'Index' abfragen
    $result = $_db->query('SELECT *
                           FROM `'.$_cfg['DB_PREFIX'].'lang_index`');
    $index = array();
    while ($Daten = $_db->fetch_array($result)) {
    	$index[$Daten['var_id']][] = $Daten['file_id'];
    }
    
    # 'Values' abfragen
    $result = $_db->query('SELECT `var_id`, `replacement`
                           FROM `'.$_cfg['DB_PREFIX'].'lang_value`
                           WHERE `lang_id` = '.$_POST['lang_id']);
    $values = array();
    while ($Daten = $_db->fetch_array($result)) {
    	$values[$Daten['var_id']] = $Daten['replacement'];
    }
    
    # Daten vorbereiten
    $pmf = array('type' => 'lang',
                 'version' => $_config['VER'],
                 'data' => array('lang' => $lang,
                                 'files' => $files,
                                 'vars' => $vars,
                                 'index' => $index,
                                 'values' => $values));
    $filename = $_sys->export_create($pmf, 0);
    
    # Header senden
    header('Cache-Control: max-age=60');
    header('Expires: '.gmdate('D, d M Y H:i:s', PMF_TIME+60).' GMT');
    header('Content-Disposition: filename='.$_sys->nice_filename($lang['name']).'.pmflng');
    header('Content-Type: application/octetstream');
    
    # Daten senden
    $fp = fopen($filename, 'rb');
    fpassthru($fp);
    fclose($fp);
    unlink($filename);
    exit();



#
#
### Import (wirklich)
} elseif ($_GET['action'] == 'import') {
    // Script-Zeitlimit
    @set_time_limit(0);
    
    ### Art der Quelle
    // per Upload
    if ($_FILES['source']['size'] > 0) {
        $filename = $_sys->tmpfile();
        move_uploaded_file($_FILES['source']['tmp_name'], $filename);
        $_sys->set_chmod($filename);
        $unlink = 1;
        
    // vom Server
    } else {
        $filename =  $_POST['source_local'];
        $unlink = 0;
    }
    
    ### Quelle vorhanden ?
    if (!file_exists($filename)) msg('error_acp_lang_import_fault', 'back()');
    
    ### laden
    $pmf = $_sys->export_restore($filename, 0);
    
    ### Temp-Datei löschen ?
    if ($unlink) @unlink($filename);
    
    ### richtige Version ?
    if ($pmf['type'] != 'lang' || $pmf['version'] != $_config['VER']) msg('error_acp_lang_import_fault', 'back()');

    
    ### Sprache anlegen
    $_db->query('INSERT INTO `'.$_cfg['DB_PREFIX'].'lang` (`name`,`code`,`choice`)
                 VALUES (\''.addslashes($pmf['data']['lang']['name']).'\',\''.addslashes($pmf['data']['lang']['code']).'\',\''.addslashes($pmf['data']['lang']['choice']).'\')');
    $lang_id = $_db->insert_id();
    
    ### 'Files' importieren
    $result = $_db->query('SELECT *
                           FROM `'.$_cfg['DB_PREFIX'].'lang_file`');
    $files = array();
    while ($Daten = $_db->fetch_array($result)) {
        // Vorhanden
        if (is_array($pmf['data']['files'][$Daten['name']])) {
        	$files[$pmf['data']['files'][$Daten['name']]['id']] = $Daten['id'];
        	unset($pmf['data']['files'][$Daten['name']]);
        }
    }
    // weitere Anlegen ?
    if ($_POST['create_missed'] && count($pmf['data']['files']) > 0) {
        foreach ($pmf['data']['files'] AS $name => $value) {
        	$_db->query('INSERT INTO `'.$_cfg['DB_PREFIX'].'lang_file` (`name`,`comment`)
        	             VALUES (\''.addslashes($name).'\',\''.addslashes($value['comment']).'\')');
        	$files[$value['id']] = $_db->insert_id();
        }
    }
    $pmf['data']['files'] = $files;
    
    
    ### 'Vars' && 'Index' importieren
    $result = $_db->query('SELECT *
                           FROM `'.$_cfg['DB_PREFIX'].'lang_var`');
    $vars = array();
    while ($Daten = $_db->fetch_array($result)) {
        // Vorhanden
        if (is_array($pmf['data']['vars'][$Daten['name']])) {
        	$vars[$pmf['data']['vars'][$Daten['name']]['id']] = $Daten['id'];
        	unset($pmf['data']['vars'][$Daten['name']]);
        }
    }
    // weitere Anlegen ?
    if ($_POST['create_missed'] && count($pmf['data']['vars']) > 0) {
        foreach ($pmf['data']['vars'] AS $name => $value) {
        	$_db->query('INSERT INTO `'.$_cfg['DB_PREFIX'].'lang_var` (`name`,`default`)
        	             VALUES (\''.addslashes($name).'\',\''.addslashes($value['default']).'\')');
        	$vars[$value['id']] = $_db->insert_id();
        	// index anlegen
        	if (count($pmf['data']['index'][$value['id']]) > 0) {
        	    $index = array();
        	    foreach ($pmf['data']['index'][$value['id']] AS $file_id) {
        	    	$index[] = '('.$vars[$value['id']].','.$pmf['data']['files'][$file_id].')';
        	    }
        	    $_db->u_query('INSERT INTO `'.$_cfg['DB_PREFIX'].'lang_index` (`var_id`,`file_id`)
        	                   VALUES '.implode(',', $index));
        	}
        }
    }
    unset($pmf['data']['index']);
    $pmf['data']['vars'] = $vars;
    
    
    ### 'Values' importieren
    if (count($pmf['data']['values']) > 0) {
        $values = array();
        foreach ($pmf['data']['values'] AS $var_id => $value) {
        	$values[] = '('.intval($pmf['data']['vars'][$var_id]).','.$lang_id.',\''.addslashes($value).'\')';
        }
    	$_db->query('INSERT INTO `'.$_cfg['DB_PREFIX'].'lang_value` (`var_id`,`lang_id`,`replacement`)
             VALUES '.implode(',', $values));
    }
    
    ### Cache Dateien löschen
    unlink_lang_cache();
    
    msg('error_acp_import', 'lang.php?'.$_sess['url']);



#
#
### Suchen && Ersetzen (form)
} elseif ($_GET['action'] == 'search') {
    ### Sprachen
    $result = $_db->query('SELECT `id`, `name`
                           FROM `'.$_cfg['DB_PREFIX'].'lang`
                           ORDER BY `name` ASC');
    $lang = '';
    while ($lng = $_db->fetch_array($result)) {
        $lang .= '<option value="'.$lng['id'].'">'.$lng['name'].'</option>'."\n";
    }
    
    eval('echo "'.$_style->get('acp_lang_search_replace').'";');



#
#
### Ersetzen (wirklich)
} elseif ($_GET['action'] == 'replace' && $_POST['search'] && is_numeric($_POST['id'])) {
    ### bestimmter Name ?
    $ids = array();
    if ($_POST['name']) {
        $result = $_db->query('SELECT `id`
                               FROM `'.$_cfg['DB_PREFIX'].'lang_var`
                               WHERE `name` REGEXP \''.gpc_addslashes($_POST['name']).'\'
                               ORDER BY `name` ASC');
        while ($Daten = $_db->fetch_array($result)) {
        	$ids[] = $Daten['id'];
        }
    }
    
    ### ersetzen
    $_db->u_query('UPDATE `'.$_cfg['DB_PREFIX'].'lang_value`
                   SET `replacement` = REPLACE(`replacement`,\''.gpc_addslashes($_POST['search']).'\',\''.gpc_addslashes($_POST['replace']).'\')
                   WHERE `lang_id` = '.$_POST['id'].'
                   '.((count($ids)) ? ' AND `var_id` IN ('.implode(',', $ids).')' : ''));
    # Cache Dateien löschen
    unlink_lang_cache();
    
    msg('error_acp_replace', 'lang.php?action=search&'.$_sess['url']);
}
?>
Return current item: PhpMyForum