Location: PHPKode > projects > PhpMyForum > phpmyforum_4_1_1/admin/template.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_template');

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


#
#
### Template-Set Liste (form)
if (empty($_GET['action'])) {
    $result = $_db->query('SELECT *
                           FROM `'.$_cfg['DB_PREFIX'].'template_set`
                           ORDER BY name ASC');
    $templateset_lines = '';
    while ($Daten = $_db->fetch_array($result)) {
        $templates = $_admin->search($Daten['id'], $Daten['typ']);
        $Daten['count'] = count($templates);
        $Daten['name'] = htmlentities($Daten['name'], ENT_QUOTES);
        $templateset_lines .= eval ('return ("'.$_style->get('acp_templateset_line').'");');
    }
    eval ('echo "'.$_style->get('acp_templateset').'";');



#
#
### Template-Set hinzufügen (form)
} elseif ($_GET['action'] == 'addtemplateset') {
    eval ('echo "'.$_style->get('acp_templateset_add').'";');



#
#
### Template-Set bearbeiten / kopieren (form)
} elseif (is_numeric($_GET['id']) && ($_GET['action'] == 'edittemplateset' || $_GET['action'] == 'copytemplateset')) {
    # Template-Set
    $Daten = $_db->fetch_first('SELECT *
                                FROM `'.$_cfg['DB_PREFIX'].'template_set`
                                WHERE id = '.$_GET['id']);
    $typ[$Daten['typ']] = ' selected="selected"';
    $Daten['name'] = htmlentities($Daten['name'], ENT_QUOTES);
    
    eval ('echo "'.$_style->get((($_GET['action'] == 'edittemplateset') ? 'acp_templateset_edit' : 'acp_templateset_copy')).'";');



#
#
### Template-Set Speichern
} elseif ($_GET['action'] == 'savetemplateset') {
    @set_time_limit(900);
    # hinzufügen
    if (!$_POST['id'] && !$_POST['copy']) {
        $_db->u_query('INSERT INTO `'.$_cfg['DB_PREFIX'].'template_set` (`name`,`typ`)
                       VALUES (\''.gpc_addslashes($_POST['name']).'\','.intval($_POST['typ']).')');
        
    # kopieren
    } elseif ($_POST['copy']) {
        $_db->query('INSERT INTO `'.$_cfg['DB_PREFIX'].'template_set` (`name`,`typ`)
                     VALUES (\''.gpc_addslashes($_POST['name']).'\','.intval($_POST['typ']).')');
        $new_id = $_db->insert_id();
        
        // alte Daten
        $copy = $_db->fetch_first('SELECT *
                                   FROM `'.$_cfg['DB_PREFIX'].'template_set`
                                   WHERE `id` = '.intval($_POST['copy']));
        // Templates
        $templates = $_admin->load($copy['id'], $copy['typ']);
        foreach ($templates AS $name => $Daten) {
            $_admin->create($new_id, intval($_POST['typ']), $Daten['name'], $Daten['template'], $Daten['start'], $Daten['login'], $Daten['mod'], $Daten['admin']);
        }

    # bearbeiten
    } else {
        // alte Daten
        $old = $_db->fetch_first('SELECT *
                                  FROM `'.$_cfg['DB_PREFIX'].'template_set`
                                  WHERE id = '.intval($_POST['id']));
       
        // Typ: geändert ?
        if ($old['typ'] != intval($_POST['typ'])) {
            $templates = $_admin->load($old['id'], $old['typ']);
            foreach ($templates AS $name => $Daten) {
                $_admin->update($old['id'], $old['id'], $old['typ'], intval($_POST['typ']), $Daten['name'], $Daten['name'], $Daten['template'], $Daten['start'], $Daten['login'], $Daten['mod'], $Daten['admin']);
            }
        }
        
        // Update
        $_db->u_query('UPDATE `'.$_cfg['DB_PREFIX'].'template_set`
                       SET `name` = \''.gpc_addslashes($_POST['name']).'\',
                           `typ` = '.intval($_POST['typ']).'
                       WHERE id = '.intval($_POST['id']));

    }
    msg('error_acp_save', 'template.php?'.$_sess['url']);



#
#
### Template Liste & Suchergebnis (form)
} elseif ($_GET['action'] == 'templates') {
    # TPL-Set - ID
    $_GET['id'] = ($_POST['search']) ? $_POST['templateset'] : $_GET['id'];
    
    # TPL-Set
    $Daten = $_db->fetch_first('SELECT id, typ, name AS templateset
                                FROM `'.$_cfg['DB_PREFIX'].'template_set`
                                WHERE id = '.intval($_GET['id']));
    
    # Werte suchen
    $liste = $_admin->search($Daten['id'], $Daten['typ'], gpc_stripslashes($_POST['search']), gpc_stripslashes($_POST['name']));
    $Daten['count'] = count($liste);
    
    $templates = array();
    foreach ($liste AS $name) {
        $templates[] = '<option value="'.$name.'">'.$name.'</option>';
    }
    $Daten['templates'] = implode("\n", $templates);
    
    eval ('echo "'.$_style->get('acp_template').'";');



#
#
### Template hinzufügen (form)
} elseif ($_GET['action'] == 'add') {
    // Template-Sets
    $result = $_db->query('SELECT id, name
                           FROM `'.$_cfg['DB_PREFIX'].'template_set`
                           ORDER BY name ASC');
    $templates = array();
    while ($tpl = $_db->fetch_array($result)) {
        $templates[] = '<option value="'.$tpl['id'].'">'.$tpl['name'].'</option>';
    }
    $Daten['templatesets'] = implode("\n", $templates);

    eval ('echo "'.$_style->get('acp_template_add').'";');



#
#
#### Template bearbeiten / kopieren (form)
} elseif ($_GET['template'] && is_numeric($_GET['templateset']) && ($_GET['action'] == 'edit' || $_GET['action'] == 'copy')) {
    # Template-Daten
    $tpl_set = $_db->fetch_first('SELECT `typ`
                                  FROM `'.$_cfg['DB_PREFIX'].'template_set`
                                  WHERE `id` = '.$_GET['templateset']);
    $template = $_admin->load($_GET['templateset'], $tpl_set['typ'], array(gpc_stripslashes($_GET['template'])));
    $Daten = $template[gpc_stripslashes($_GET['template'])];
    
    # Template-Sets
    $result = $_db->query('SELECT `id`, `name`
                           FROM `'.$_cfg['DB_PREFIX'].'template_set`
                           ORDER BY `name` ASC');
    $Daten['templatesets'] = '';
    while ($tpl = $_db->fetch_array($result)) {
        $Daten['templatesets'] .= '<option value="'.$tpl['id'].'"';
        $Daten['templatesets'] .= ($_GET['templateset'] == $tpl['id']) ? ' selected="selected"' : '';
        $Daten['templatesets'] .= '>'.$tpl['name'].'</option>'."\n";
    }

    # weitere Werte anpassen
    $start[$Daten['start']] = $_style->cfg['template_choice'];
    $login[$Daten['login']] = $_style->cfg['template_choice'];
    $mod[$Daten['mod']] = $_style->cfg['template_choice'];
    $admin[$Daten['admin']] = $_style->cfg['template_choice'];
    $Daten['name'] = htmlentities($Daten['name'], ENT_QUOTES);
    $Daten['template'] = htmlentities($Daten['template'], ENT_QUOTES);

    eval ('echo "'.$_style->get('acp_template_'.$_GET['action']).'";');



#
#
### Template Speichern
} elseif ($_GET['action'] == 'save') {
    # Name auf Sonderzeichen überprüfen
    if (!preg_match('/^[_a-z0-9]+$/', gpc_stripslashes($_POST['name']))) {
        msg('error_acp_templates_name_fault', 'back()');
    }
    
    # Cache-Klasse
    require($_cfg['MAIN'].'/lib/tpl_cache.inc.php');
    $cache = new pmf_tpl_cache();
    
    $location = 0;
    
    # Template-Set
    $tpl_set = $_db->fetch_first('SELECT `id`, `typ`
                                  FROM `'.$_cfg['DB_PREFIX'].'template_set`
                                  WHERE `id` = '.intval($_POST['templateset']));
    
    # Template schon vorhanden ?
    $count = count($_admin->search($tpl_set['id'], $tpl_set['typ'], null, gpc_stripslashes($_POST['name'])));
    
    # hinzufügen / kopieren
    if (!is_numeric($_POST['old_templateset']) && empty($_POST['old_template'])) {
        if ($count > 0) msg('error_acp_templates_name_fault', 'back()');
        
        // erstellen
        $_admin->create($tpl_set['id'], $tpl_set['typ'], gpc_stripslashes($_POST['name']), gpc_stripslashes($_POST['template']), $_POST['start'], $_POST['login'], $_POST['mod'], $_POST['admin']);
    
    # bearbeiten
    } else {
        // Templateset geändert & schon vorhanden ?
        if (($_POST['old_templateset'] != $_POST['templateset'] && $count > 0) || $count > 1 || (gpc_stripslashes($_POST['old_template']) != gpc_stripslashes($_POST['name']) && $count > 0)) {
            msg('error_acp_templates_name_fault', 'back()');
        }
        
        $old = $_db->fetch_first('SELECT *
                                   FROM `'.$_cfg['DB_PREFIX'].'template_set`
                                   WHERE `id` = '.intval($_POST['old_templateset']));

        
        $_admin->update($_POST['old_templateset'], $_POST['templateset'],
                        $old['typ'], $tpl_set['typ'],
                        gpc_stripslashes($_POST['old_template']), gpc_stripslashes($_POST['name']),
                        gpc_stripslashes($_POST['template']), $_POST['start'], $_POST['login'], $_POST['mod'], $_POST['admin']);
        // altes Cachefile löschen
        $cache->del($old['templateset'], array($old['name']));
        
        $location = 1;
    }
    
    # Cachefile erstellen
    $cache->create( intval($_POST['templateset']), array(gpc_stripslashes($_POST['name'])) );
    
    # weiter bearbeiten
    if (!empty($location)) header('Location: template.php?action=edit&template='.$_POST['name'].'&templateset='.$_POST['templateset'].'&'.$_sess['url']);
    
    msg('error_acp_save', 'template.php?action=templates&id='.$_POST['templateset'].'&'.$_sess['url']);



#
#
### Template löschen
} elseif ($_GET['action'] == 'del' && is_numeric($_GET['templateset']) && $_GET['template']) {
    $tpl_set = $_db->fetch_first('SELECT typ
                                  FROM `'.$_cfg['DB_PREFIX'].'template_set`
                                  WHERE id = '.$_GET['templateset']);
    $Daten = $_admin->load($_GET['templateset'], $tpl_set['typ'], array($_GET['template']));
    
    # Formular anzeigen
    if (!$_GET['ok']) {
        eval ('echo "'.$_style->get('acp_template_del').'";');
        
    # wirklich löschen
    } else {
        $_admin->delete($_GET['templateset'], $tpl_set['typ'], array($_GET['template']));
        // cachefile l�schen
        require($_cfg['MAIN'].'/lib/tpl_cache.inc.php');
        $cache = new pmf_tpl_cache();
        $cache->del($_GET['templateset'], array($_GET['template']));
        msg('error_acp_del', 'template.php?action=templates&id='.$_GET['templateset'].'&'.$_sess['url']);
    }



#
#
### Template-Set löschen
} elseif ($_GET['action'] == 'deltemplateset' && is_numeric($_GET['id'])) {
    // löschen möglich ?
    if ($_db->num_rows( $_db->query('SELECT id FROM `'.$_cfg['DB_PREFIX'].'style` WHERE templateset = '.$_GET['id'])) > 0) {
        msg('error_acp_templateset_del_fault', 'back()');
    }
    
    $Daten = $_db->fetch_first('SELECT * FROM `'.$_cfg['DB_PREFIX'].'template_set` WHERE id = '.$_GET['id']);
    
    # Formular anzeigen
    if (!$_GET['ok']) {
        eval ('echo "'.$_style->get('acp_templateset_del').'";');
        
    # wirklich löschen
    } else {
        $templates = $_admin->search($Daten['id'], $Daten['typ']);
        
        // Templates Cachefiles löschen
        require($_cfg['MAIN'].'/lib/tpl_cache.inc.php');
        $cache = new pmf_tpl_cache();
        $cache->del($_GET['id'], $templates);
        
        // Templateset & Templates löschen
        $_admin->delete($Daten['id'], $Daten['typ'], $templates);
        $_db->u_query('DELETE FROM `'.$_cfg['DB_PREFIX'].'template_set` WHERE id = '.$_GET['id']);
        
        msg('error_acp_del', 'template.php?'.$_sess['url']);
    }



#
#
### in Templates suchen / ersetzen (form)
} elseif ($_GET['action'] == 'search') {
    // Template-Sets
    $result = $_db->query('SELECT id, name
                           FROM `'.$_cfg['DB_PREFIX'].'template_set`
                           ORDER BY name ASC');
    $templatesets = '';
    while ($tmpl = $_db->fetch_array($result)) {
        $templatesets .= '<option value="'.$tmpl['id'].'">'.$tmpl['name'].'</option>'."\n";
    }

    eval ('echo "'.$_style->get('acp_template_search_replace').'";');



#
#
### Template ersetzen (wirklich)
} elseif ($_GET['action'] == 'replace') {
    $tpl_set = $_db->fetch_first('SELECT typ
                                  FROM `'.$_cfg['DB_PREFIX'].'template_set`
                                  WHERE id = '.intval($_POST['templateset']));
    if ($_admin->replace($_POST['templateset'], $tpl_set['typ'], gpc_stripslashes($_POST['name']), gpc_stripslashes($_POST['search']), gpc_stripslashes($_POST['replace'])) > 0) {
        require_once($_cfg['MAIN'].'/lib/tpl_cache.inc.php');
        $_cache = new pmf_tpl_cache();
        $_cache->del($_POST['templateset'], $_admin->search($_POST['templateset'], $tpl_set['typ']));
        msg('error_acp_replace', 'template.php?action=search&'.$_sess['url']);
    } else {
        msg('error_acp_templates_replace_fault', 'back()');
    }



#
#
### Templates importieren / exportieren (form)
} elseif ($_GET['action'] == 'import/export') {
    // Template-Sets
    $result = $_db->query('SELECT id, name
                           FROM `'.$_cfg['DB_PREFIX'].'template_set`
                           ORDER BY name ASC');
    $templatesets = array();
    while ($tmpl = $_db->fetch_array($result)) {
        $templatesets[$tmpl['id']] = '<option value="'.$tmpl['id'].'"';
        $templatesets[$tmpl['id']] .= ($tmpl['id'] == $_GET['templateset']) ? ' selected="selected"' : '';
        $templatesets[$tmpl['id']] .= '>'.$tmpl['name'].'</option>';
    }
    $Daten['templatesets'] = implode("\n", $templatesets);
    
    // lokale Quellen
    $Daten['source_local'] = upload_liste($_cfg['MAIN'].'/upload', 'source_local', null, '.pmftpl');
    
    // 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_template_import_export').'";');



#
#
### Templates importieren (wirklich)
} elseif ($_GET['action'] == 'import' && ($_FILES['source']['size'] > 0 || $_POST['source_local'])) {
    // 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;
        $compressed = $_sys->is_compressed($_FILES['source']['name']);
        
    // vom Server
    } else {
        $filename =  $_POST['source_local'];
        $unlink = 0;
        $compressed = $_sys->is_compressed($filename);
    }
    
    ### Quelle vorhanden ?
    if (!file_exists($filename)) msg('error_acp_templates_import_fault', 'back()');

    ### Template-Set
    $tpl_set = $_db->fetch_first('SELECT id, typ
                                  FROM `'.$_cfg['DB_PREFIX'].'template_set`
                                  WHERE id = '.intval($_POST['templateset']));
    
    ### alten Cache löschen
    $names = $_admin->search($tpl_set['id'], $tpl_set['typ']);
    # Cache-Klasse
    require($_cfg['MAIN'].'/lib/tpl_cache.inc.php');
    $cache = new pmf_tpl_cache();
    $cache->del($tpl_set['id'], $names);
    unset($cache);
    
    ### laden
    $pmf = $_sys->export_restore($filename, $compressed);
    
    ### Temp-Datei löschen ?
    if ($unlink) @unlink($filename);
    
    ### richtige Version & Typ & Tpl-Set Vorhanden ?
    if ($pmf['type'] != 'templates' || $pmf['version'] != $_config['VER'] || !$tpl_set['id']) msg('error_acp_templates_import_fault', 'back()');
    
    ### Import
    foreach ($pmf['data'] AS $name => $data) {
        # Typ: DB
        if (!$tpl_set['typ']) {
            $_db->u_query('REPLACE INTO `'.$_cfg['DB_PREFIX'].'template` (`templateset`,`name`,`template`,`start`,`login`,`mod`,`admin`)
                           VALUES ('.$tpl_set['id'].',\''.addslashes($name).'\',\''.addslashes($data['template']).'\',
                                   '.intval($data['start']).','.intval($data['login']).','.intval($data['mod']).','.intval($data['admin']).')');
        # Typ: Files
        } else {
            $_admin->create($tpl_set['id'], 1, $name, $data['template'], $data['start'], $data['login'], $data['mod'], $data['admin']);
        }
    }
    
    msg('error_acp_import', 'template.php?'.$_sess['url']);



#
#
### Templates exportieren (wirklich)
} elseif ($_GET['action'] == 'export' && is_numeric($_POST['templateset']) && is_array($_POST['templates'])) {
    
    # Template-Set
    $tplset = $_db->fetch_first('SELECT name, typ
                                 FROM `'.$_cfg['DB_PREFIX'].'template_set`
                                 WHERE id = '.$_POST['templateset']);
    
    # 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($tplset['name']).'.pmftpl'.(($_POST['compress']) ? '.gz' : ''));
    header('Content-Type: application/octetstream');
    
    # Daten vorbereiten
    $pmf = array('type' => 'templates',
                 'version' => $_config['VER'],
                 'data' => $_admin->load($_POST['templateset'], $tplset['typ'], $_POST['templates']));
    $filename = $_sys->export_create($pmf, $_POST['compress']);
                 
    # Daten senden
    $fp = fopen($filename, 'rb');
    fpassthru($fp);
    fclose($fp);
    unlink($filename);
    exit();
}
?>
Return current item: PhpMyForum