<?php
/******************************************************************************
 @Author:     Björn Brockmann, Paul Eppner (paul@twomoons.de)
                              Sven Bräutigam, Jürgen Brändle
 @Homepage    http://www.der-dirigent.de
 Last update : rh - 24.11.2005 - Permissions check für security reasons
 Part of     : Update to Version 01.00.02 
******************************************************************************/
if (!is_object($perm)) die();
function tpl_change($idlay) {
    global $db;
        $list = browse_layout_for_containers($idlay);
        if (is_array($list['id'])) {
                foreach ($list['id'] as $i) {
                        global ${'C'.$i.'MOD_VAR'};
                        if (${'C'.$i.'MOD_VAR'}) $cconfig[$i] = make_array_to_urlstring(${'C'.$i.'MOD_VAR'});
                }
        }
    // Content aus Cache löschen
        $db->delete_cache('frontend_content');
        return $cconfig;
}

function tpl_save($idtpl, $idlay, $tplname, $description, $tpl_overwrite_all) {
        global $db, $auth, $client, $dedi_db, $dedi_lang, $cfg_client, $tpl, $perm;

        // Eintrag in 'tpl' Tabelle
        if ($tplname == '') $tplname = $dedi_lang['tpl_defaultname'];
        set_magic_quotes_gpc($tplname);
        set_magic_quotes_gpc($description);
        if (!$idtpl) {
                $sql = "INSERT INTO ".$dedi_db['tpl']." (name, description, idlay, idclient, author, created
                , lastmodified) VALUES ('$tplname', '$description', '$idlay', '$client'
                , '".$auth->auth['uid']."', '".time()."', '".time()."')";
                $db->query($sql);
                $idtpl = mysql_insert_id();

                // Event
                fire_event('tpl_new', array('idtpl' => $idtpl, 'name' => $tplname));
        } else {
                $sql = "UPDATE ".$dedi_db['tpl']." SET        name='$tplname', description='$description', idlay='$idlay',
                author='".$auth->auth['uid']."', lastmodified='".time()."' WHERE idtpl='$idtpl'";
                $db->query($sql);

                //rechte setzen
                if ($perm->have_perm('6', 'tpl', $idtpl)) {
                        global $dedi_gruppenids, $dedi_gruppenrechte;
                        $perm->set_group_rights( 'tpl', $idtpl, $dedi_gruppenids, $dedi_gruppenrechte, '', 0x35);
                }

                // Event
                fire_event('tpl_edit', array('idtpl' => $idtpl, 'name' => $tplname));
        }

        // Array der alten Container erstellen
        $used_container = browse_template_for_module($idtpl, '0');
        if (!is_array($used_container['id'])) $used_container['id']['0'] = '0';

        // Array der neuen Container erstellen
        $list = browse_layout_for_containers($idlay);
        if (is_array($list['id'])) {
                foreach ($list['id'] as $i) {
                        global ${'C'.$i.'MOD_VAR'}, ${'c'.$i}, ${'cview'.$i}, ${'cedit'.$i};

                        if (${'c'.$i}) $container['id'][] = $i;
                        if (${'C'.$i.'MOD_VAR'}) $cconfig[$i] = make_array_to_urlstring(${'C'.$i.'MOD_VAR'});
                }
        }
        if (!is_array($container['id'])) $container['id']['0'] = '0';
        foreach ($container['id'] as $value) {
                if ($value != '0') {
                        // neue Container hinzufügen
                        if (!in_array($value,$used_container['id'])) {
                                $sql = "INSERT INTO $dedi_db[container] (idtpl, container, idmod) VALUES ('$idtpl'
                        , '$value', '".${'c'.$value}."')";
                                $db->query($sql);
                                $idcontainer = mysql_insert_id();
                                $sql = "INSERT INTO $dedi_db[container_conf] (idcontainer, config, view, edit)
                        VALUES ('$idcontainer', '$cconfig[$value]', '".${'cview'.$value}."', '".${'cedit'.$value}."')";
                                $db->query($sql);

                                // Templatekopien suchen
                                if (!$tpllist) {
                                        $sql = "SELECT idtplconf FROM ". $dedi_db['tpl_conf'] ." WHERE idtpl='$idtpl'";
                                        $db->query($sql);
                                        while ($db->next_record()) $tpllist[] = $db->f('idtplconf');
                                }
                                if (is_array($tpllist)) {
                                        foreach ($tpllist as $idtplconf) {
                                                $sql = "INSERT INTO ".$dedi_db['container_conf']." (idtplconf, idcontainer, config
                                , view, edit) VALUES ('$idtplconf', '$idcontainer', '$cconfig[$value]'
                                , '".${'cview'.$value}."', '".${'cedit'.$value}."')";
                                                $db->query($sql);
                                                $change = 'true';
                                        }
                                }
                        }

                        // geänderte Container updaten
                        if (${'c'.$value} == $used_container[$value]['idmod'] && ($cconfig[$value] != $used_container[$value]['config']
                || ${'cview'.$value} != $used_container[$value]['view']        || ${'cedit'.$value} != $used_container[$value]['edit'])) {

                                // Seiten / Ordnertemplates ändern
                                if ($tpl_overwrite_all != '1') $tpl_overwrite = " AND idtplconf='0'";
                                else {
                                        $tpl_overwrite = '';
                                        $change = 'true';
                }
                                $sql = "UPDATE        ".$dedi_db['container_conf']." SET config='".$cconfig[$value]."', view='".${'cview'.$value}."'
                        , edit='".${'cedit'.$value}."' WHERE idcontainer='".$used_container[$value]['idcontainer']."'$tpl_overwrite";
                                $db->query($sql);
                        }

                        // Modul wurde geändert
                        if (${'c'.$value} != $used_container[$value]['idmod'] && $used_container[$value]['idmod']) {
                                $sql = "UPDATE ".$dedi_db['container']." SET idmod='".${'c'.$value}."'
                        WHERE idcontainer='".$used_container[$value]['idcontainer']."'";
                                $db->query($sql);
                                $sql = "UPDATE ".$dedi_db['container_conf']." SET config='".$cconfig[$value]."', view='".${'cview'.$value}."'
                        , edit='".${'cedit'.$value}."' WHERE idcontainer='".$used_container[$value]['idcontainer']."'";
                                $db->query($sql);
                                $change = 'true';
                                $empty_container[] = $value;
                        }
                }
        }

          // alte Container löschen
          foreach ($used_container['id'] as $value) {
                if ($value != '0') {
                          if (!in_array($value,$container['id'])) {
                                  $sql = "DELETE FROM $dedi_db[container] WHERE idcontainer='".$used_container[$value]['idcontainer']."'";
                                  $db->query($sql);
                                  $sql = "DELETE FROM $dedi_db[container_conf] WHERE idcontainer='".$used_container[$value]['idcontainer']."'";
                                  $db->query($sql);
                                $change = 'true';
                                $empty_container[] = $value;
                          }
                  }
          }

        if ($change) {
                $list = get_idtplconf_by_using_type($idtpl, 'tpl');

                // lösche alte 'content' Einträge
                if (is_array($empty_container)) {
                        $list2 = get_idsidelang_by_idtplconf($list);
                        $sql = "DELETE FROM $dedi_db[content] WHERE idsidelang IN(".implode(',', $list2).")
                    AND container IN(".implode(',', $empty_container).")";
                        $db->query($sql);
                        $sql = "DELETE FROM $dedi_db[content_external] WHERE idsidelang
                    IN(".implode(',', $list2).") AND container IN(".implode(',', $empty_container).")";
                        $db->query($sql);
                        unset($list2);
                }

                // Status der 'code' Tabelle ändern
                // jb_todo: change_code_status im dateimanager einführen
                $list = get_idcode_by_idtplconf($list);
                change_code_status($list, '1');
                unset($list);
        }
    // Content aus Cache löschen
        $db->delete_cache('frontend_content');
}

function tpl_delete_template($idtpl) {
        global $db, $client, $dedi_db, $perm;

        if(! (is_numeric($idtpl) || is_int($idtpl) ) ) return;

        // Wird Template noch verwendet?
        $sql = "SELECT * FROM $dedi_db[tpl_conf] WHERE idtpl='$idtpl' LIMIT 0, 1";
        $db->query($sql);
        if ($db->affected_rows()) return '0501';
        else {
                // Modulkonfiguration löschen
                $sql = "SELECT idcontainer FROM $dedi_db[container] WHERE idtpl='$idtpl'";
                $db->query($sql);
                while ($db->next_record()) $containerlist[] = $db->f('idcontainer');
                if (is_array($containerlist)) {
                        $sql = "DELETE FROM $dedi_db[container_conf] WHERE idcontainer IN(".implode(',', $containerlist).")";
                        $db->query($sql);
                }

                // Module aus dem Template löschen
                $sql = "DELETE FROM $dedi_db[container] WHERE idtpl='$idtpl'";
                $db->query($sql);

                // Template löschen
                $sql = "DELETE FROM $dedi_db[tpl] WHERE idtpl='$idtpl'";
                $db->query($sql);

                //delete perm
                $perm->delete_perms($idtpl, 'tpl');

                // Event
                fire_event('tpl_delete', array('idtpl' => $idtpl));
        }
    // Content aus Cache löschen
        $db->delete_cache('frontend_content');
}

function con_config_tpl_save($idtpl, $idlay, $idcatlang, $idsidelang, $idtplconf, $have_perm_save_configdata = true) {
        global $db, $dedi_db, $cfg_client, $configtpl;

        if(! (is_numeric($idtpl) || is_int($idtpl) ) ) return;
        if(! (is_numeric($idtplconf) || is_int($idtplconf) ) ) return;

        // Array der alten Container erstellen
        $used_container = browse_template_for_module('0', $idtplconf);
        if (!is_array($used_container['id'])) $used_container['id']['0'] = '0';

        // Eintrag in 'tpl_conf' Tabelle
        if ($idtplconf == '0') {
                if ($idtpl != '0') {
                        // Template erstellen
                        $sql = "INSERT INTO $dedi_db[tpl_conf] (idtpl) VALUES ('$idtpl')";
                        $db->query($sql);
                        $idtplconf = mysql_insert_id();
                } else return;
        } else {
                if ($idtpl != '0' && $idtpl != $configtpl) {
                        // Template ändern
                $sql = "UPDATE $dedi_db[tpl_conf] SET idtpl='$idtpl' WHERE idtplconf='$idtplconf'";
                        $db->query($sql);
                } elseif ($idtpl == '0') {
                        // Template löschen
                        $sql = "DELETE FROM $dedi_db[container_conf] WHERE idtplconf='$idtplconf'";
                        $db->query($sql);
                        $sql = "DELETE FROM $dedi_db[tpl_conf] WHERE idtplconf='$idtplconf'";
                        $db->query($sql);

                        // Status der 'code' Tabelle ändern
                        $list = get_idcode_by_idtplconf($idtplconf);
                        change_code_status($list, '1');
                        unset($list);

                        // Containerinhalt löschen
                        $list = get_idsidelang_by_idtplconf($idtplconf);
                        $sql = "DELETE FROM $dedi_db[content] WHERE idsidelang IN(".implode(',', $list).")";
                        $db->query($sql);
                        $sql = "DELETE FROM $dedi_db[content_external] WHERE idsidelang IN(".implode(',', $list).")";
                        $db->query($sql);
                        unset($list);
                        $idtplconf = '0';
                }
        }

    // Content aus Cache löschen
        $db->delete_cache('frontend_content');

        // Template bei Seite oder Ordner eintragen
        if ($idcatlang) $sql = "UPDATE $dedi_db[cat_lang] SET idtplconf='$idtplconf' WHERE idcatlang='$idcatlang'";
        else $sql = "UPDATE $dedi_db[side_lang] SET idtplconf='$idtplconf' WHERE idsidelang='$idsidelang'";
        if ($idcatlang || $idsidelang) $db->query($sql);
        if ($idtplconf == '0') return;

        //Abbruch, wenn der Benutzer nicht das Recht hat, Templates zu konfigurieren
        if(! $have_perm_save_configdata) return;



        // Array der neuen Container erstellen
        if ($idlay) {
                $list = browse_layout_for_containers($idlay);
                if (is_array($list['id'])) {
                        foreach ($list['id'] as $i) {
                                global ${'C'.$i.'MOD_VAR'}, ${'c'.$i}, ${'cview'.$i}, ${'cedit'.$i};

                                if (${'c'.$i}) $container['id'][] = $i;
                                if (${'C'.$i.'MOD_VAR'}) $cconfig[$i] = make_array_to_urlstring(${'C'.$i.'MOD_VAR'});
                        }
                }
        } else {
                // Templatevorlage kopieren
                $sql = "SELECT container, config, view, edit FROM $dedi_db[container] A LEFT JOIN $dedi_db[container_conf] B
                USING(idcontainer) WHERE A.idtpl='$idtpl' AND B.idtplconf='0'";
                $db->query($sql);
                while ($db->next_record()) {
                        $container['id'][] = $db->f('container');
                        $cconfig[$db->f('container')]  = $db->f('config');
                        ${'cview'.$db->f('container')} = $db->f('view');
                        ${'cedit'.$db->f('container')} = $db->f('edit');
                }
        }
        if (!is_array($container['id'])) $container['id']['0'] = '0';
        foreach ($container['id'] as $value) {
                if ($value != '0') {
                        // neue Container hinzufügen
                        if (!in_array($value,$used_container['id'])) {
                                $sql = "SELECT idcontainer FROM $dedi_db[container] WHERE idtpl='$idtpl' AND container='$value'";
                                $db->query($sql);
                                $db->next_record();
                                $sql = "INSERT INTO $dedi_db[container_conf] (idtplconf, idcontainer, config, view, edit)
                        VALUES ('$idtplconf', '".$db->f('idcontainer')."', '$cconfig[$value]', '".${'cview'.$value}."', '".${'cedit'.$value}."')";
                                $db->query($sql);
                                $change = 'true';
                        }

                        // geänderte Container updaten
                        if (in_array($value,$used_container['id'])) {
                                // Modulkonfiguration hat sich geändert
                                if ($used_container[$value]['config'] != $cconfig[$value] || $used_container[$value]['view'] != ${'cview'.$value}
                    || $used_container[$value]['edit'] != ${'cedit'.$value}) {
                                        $sql = "UPDATE $dedi_db[container_conf] SET config='".$cconfig[$value]."', view='".${'cview'.$value}."', edit='".${'cedit'.$value}."' WHERE idcontainer='".$used_container[$value]['idcontainer']."' AND idtplconf='$idtplconf'";
                                        $db->query($sql);
                                        $change = 'true';
                                }

                                // Modul hat sich durch Templatewechsel geändert
                                if ($idtpl != $configtpl) {
                                $sql = "SELECT idcontainer FROM $dedi_db[container] WHERE idtpl='$idtpl' AND container='$value'";
                                        $db->query($sql);
                                        $db->next_record();
                                        $sql = "UPDATE $dedi_db[container_conf] SET idcontainer='".$db->f('idcontainer')."'
                            WHERE idcontainer='".$used_container[$value]['idcontainer']."' AND idtplconf='$idtplconf'";
                                        $db->query($sql);
                                        if ($used_container[$value]['idmod'] != ${'c'.$value}) $empty_container[] = $value;
                                }
                        }
                }
        }

          // alte Container löschen
          foreach ($used_container['id'] as $value) {
                if ($value != '0') {
                          if (!in_array($value,$container['id'])) {
                                  $sql = "DELETE FROM $dedi_db[container_conf] WHERE idcontainer='".$used_container[$value]['idcontainer']."'
                        AND idtplconf='$idtplconf'";
                                  $db->query($sql);
                                $change = 'true';
                                $empty_container[] = $value;
                          }
                  }
          }

        // Status der 'code' Tabelle ändern
        if ($change) {
                $list = get_idcode_by_idtplconf($idtplconf);
                change_code_status($list, '1');
                unset($list);
        }

                // Containerinhalt löschen
        if (is_array($empty_container)) {
                $list = get_idsidelang_by_idtplconf($idtplconf);
                if(is_array($list)){
                        $sql = "DELETE FROM $dedi_db[content] WHERE idsidelang IN(".implode(',', $list).")
                    AND container IN(".implode(',', $empty_container).")";
                        $db->query($sql);
                        $sql = "DELETE FROM $dedi_db[content_external] WHERE idsidelang IN(".implode(',', $list).")
                    AND container IN(".implode(',', $empty_container).")";
                        $db->query($sql);
                        unset($list);
                }
        }
}

function con_config_folder_save($idcat, $idcatside, $idtpl, $view, $idtplconf, $description, $name, $parent, $area, $idlay) {
        global $db, $client, $lang, $dedi_db, $auth, $dedi_lang, $perm, $sess, $cfg_client;

        if(! (is_numeric($idtpl) || is_int($idtpl) ) ) return;
        if(! (is_numeric($idtplconf) || is_int($idtplconf) ) ) return;

        // Im Frontend muß ein Template angegeben werden
        // bb_todo: frontend??? config???
        if ($area == 'frontend_configcat' && $idtpl == '0') return;

        // default Name setzen und formatieren
        if (empty($name)) $name = $dedi_lang['con_newfolder'];
        set_magic_quotes_gpc($name);
        set_magic_quotes_gpc($description);

        // existiert dieser Ordner schon?
        if (empty($idcat)) {
                // Rootparent suchen
                if ($parent != '0') {
                        $sql = "SELECT rootparent FROM $dedi_db[cat] WHERE idcat='$parent' AND idclient='$client'";
                        $db->query($sql);
                        $db->next_record();
                        $rootparent = $db->f('rootparent');
                }
                // Sortindex suchen
                $sql = "SELECT MAX(sortindex) AS sortindex FROM $dedi_db[cat] WHERE parent='$parent' AND idclient='$client'";
                $db->query($sql);
                $db->next_record();
                $sortindex = $db->f('sortindex') + 1;

                // Neuen Ordner in 'dedi_cat' schreiben
                $sql = "INSERT INTO ".$dedi_db['cat']." (parent, sortindex, idclient) VALUES('$parent', '$sortindex', '$client')";
                $db->query($sql);

                // neue idcat suchen
                $idcat = mysql_insert_id();
                if ($parent == '0') $rootparent = $idcat;

                $sql   = "UPDATE ".$dedi_db['cat']." SET rootparent='$rootparent' WHERE idcat='$idcat'";
                $db->query($sql);
                $languages = get_languages_by_client($client);



                // Ordnername in 'cat_lang'-Tabelle eintragen
                foreach ($languages as $value) {
                        $sql = "INSERT INTO ".$dedi_db['cat_lang']."
                                 (idcat, idlang, name, description, visible, author, created, lastmodified)
                                VALUES
                                 ('$idcat', '$value', '$name', '$description', '0', '".$auth->auth['uid']."', '".time()."', '".time()."')";
                        $db->query($sql);

                        // kein Hauptordner, erbt Rechte des "Mutter- Ordners"
                        if ($parent != '0') $perm->xcopy_perm($parent, 'cat', $idcat, 'cat', 0xFFFFFFFF, 0, $value, false);
                }

                // Ordner öffnen
                $sql = "INSERT INTO ".$dedi_db['cat_expand']." (idusers, idcat) VALUES('".$auth->auth['uid']."', '$idcat')";
                $db->query($sql);

                // idcatlang für die Templateerstellung raussuchen
                if ($perm -> have_perm(1, 'tpl', $idtpl)) {
                         $sql = "SELECT idcatlang FROM ".$dedi_db['cat_lang']." WHERE idcat='$idcat'";
                        $db->query($sql);
                        while ($db->next_record()) $tmp_idcatlang[] = $db->f('idcatlang');

                        // Template erstellen
                        if ($db->affected_rows()){
                                foreach ($tmp_idcatlang as $value) con_config_tpl_save($idtpl, $idlay, $value, '', $idtplconf);
                        }
                }

                // Event
                fire_event('con_cat_new', array('idcat' => $idcat, 'name' => $name, 'parent' => $parent, 'idtpl' => $idtpl));
        // Content aus Cache löschen
        $db->delete_cache('frontend_content');
                // Danach ins Frontend?
                if ($view) $url_location = $sess->url($cfg_client['htmlpath'].$cfg_client['contentfile'].'?lang='.$lang.'&idcat='.$idcat.'&idcatside='.$idcatside.'&view='.$view);
                else       $url_location = $sess->url("main.php?area=con");

        // Ordner aktualisieren
        } else {
                // Ordner umbenennen
                $sql = "UPDATE $dedi_db[cat_lang] SET name='$name', description='$description', author='".$auth->auth['uid']."'
                , lastmodified='".time()."' WHERE idcat='$idcat' AND idlang='$lang'";
                $db->query($sql);

                // Template konfigurieren
                $have_perm_save_configdata = $perm->have_perm(11, 'cat', $idcat);
                /* if ($idtplconf == '0' && $idtpl != '0') {
                        // idcatlang suchen
                         $sql = "SELECT idcatlang FROM ".$dedi_db['cat_lang'] ." WHERE idcat='$idcat' AND idlang='$lang'";
                        $db->query($sql);

                        while ($db->next_record()){
                                $tmp_idcatlang[] = $db->f('idcatlang');
                        }

                        // Template erstellen
                        if (count($tmp_idcatlang) >0){
                                foreach ($tmp_idcatlang as $value) con_config_tpl_save($idtpl, $idlay, $value, '', $idtplconf, $have_perm_save_configdata);
                        }
                } else */
				$sql = "SELECT idcatlang FROM ".$dedi_db['cat_lang'] ." WHERE idcat='$idcat' AND idlang='$lang'";
				$db->query($sql);
				$db->next_record();
				$idcatlang = $db->f('idcatlang');

                con_config_tpl_save($idtpl, $idlay, $idcatlang, '', $idtplconf, $have_perm_save_configdata);

                // rechte setzen
                if ($perm->have_perm(6, 'cat', $idcat)) {
                        global $dedi_gruppenids, $dedi_gruppenrechte;
                        $perm->set_group_rights( 'cat', $idcat, $dedi_gruppenids, $dedi_gruppenrechte, '', 0xFFFFFFFF);
                }

                // Event
                fire_event('con_cat_edit', array('idcat' => $idcat, 'name' => $name));
        // Content aus Cache löschen
        $db->delete_cache('frontend_content');
                // Danach ins Frontend?
                if ($view) $url_location = $sess->url($cfg_client['htmlpath'].$cfg_client['contentfile'].'?lang='.$lang.'&idcat='.$idcat.'&idcatside='.$idcatside.'&view='.$view);
                else $url_location = $sess->url("main.php?area=con");
        }

        // Cache-Group Frontend löschen
        $db->delete_cache('frontend');
        redirect_page($url_location);
}

// jb_todo: alle aufrufe
function con_config_side_save($idcat, $idside, $idtpl, $idtplconf, $idsidelang, $idcatside, $idcatnew
                                       , $author, $title, $meta_keywords, $summary, $online, $user_protected
                                       , $view, $created, $lastmodified, $startdate, $starttime, $enddate, $endtime
                                       , $meta_author, $meta_description, $meta_robots, $meta_redirect_time
                                       , $meta_redirect, $meta_redirect_url, $idlay) {
        global $db, $client, $sess, $perm, $lang, $dedi_db, $cfg_client, $dedi_lang;

        if(! (is_numeric($idtpl) || is_int($idtpl) ) ) return;
        if(! (is_numeric($idtplconf) || is_int($idtplconf) ) ) return;

        // idcatside für rechte
        $idcatside_for_rights = $idcatside;
        $idcat_for_rights = $idcat;

        // default Name setzen und formatieren
        if (empty($title))        $title         = $dedi_lang['con_defaulttitle'];
        if (!is_array($idcatnew)) $idcatnew['0'] = $idcat;
        $start             = createDate($startdate, $starttime);
        $end               = createDate($enddate, $endtime);
          $meta_redirect     = ($meta_redirect == '1') ? '1' : '0';
        $meta_redirect_url = ($meta_redirect_url == 'http://' || $meta_redirect_url == '') ? '' : $meta_redirect_url;
        set_magic_quotes_gpc($title);
        set_magic_quotes_gpc($summary);
        set_magic_quotes_gpc($meta_author);
        set_magic_quotes_gpc($meta_description);
        set_magic_quotes_gpc($meta_keywords);
        set_magic_quotes_gpc($meta_robots);
        set_magic_quotes_gpc($meta_redirect_url);
        if (empty($idside)) {
                // Seite erstellen
                $sql = "INSERT INTO $dedi_db[side] (idclient) VALUES ('$client')";
                $db->query($sql);

                // neue idside suchen
                $idside = mysql_insert_id();

                // Seite in alle Ordner einfügen
                foreach ($idcatnew as $value) {
                        $sql = "SELECT * FROM $dedi_db[cat_side] WHERE idcat='$value' AND is_start='1'";
                        $db->query($sql);
                        $is_start = ($db->next_record()) ? '0' : '1';
                        $sql = "INSERT INTO $dedi_db[cat_side] (idcat, idside, is_start) VALUES ('$value', '$idside', '$is_start')";
                        $db->query($sql);
                }

                // idcatside suchen
                $sql = "SELECT idcatside FROM $dedi_db[cat_side] WHERE idside='$idside'";
                $idcatside = array();
                getIdList($sql, $idcatside, '', 'idcatside');

                // für jede Sprache erstellen
                $a_languages = get_languages_by_client($client);
                foreach ($a_languages as $tmp_lang) {
                        if ($tmp_lang == $lang)        {
                                $side_online = $online;
                                $side_start  = $start;
                                $side_end    = $end;
                        } else {
                                $side_online = 0;
                                $side_start  = time();
                                $side_end    = time();
                        }
                        $sql  = 'INSERT INTO ' . $dedi_db['side_lang'];
                        $sql .= ' (idside, idlang, title, meta_keywords, summary, created, lastmodified, author, meta_redirect, meta_redirect_url,';
                        $sql .= ' user_protected, online, start, end, meta_author, meta_description, meta_robots, meta_redirect_time) ';
                        $sql .= 'VALUES (';
                        $sql .= " '$idside', '$tmp_lang', '$title', '$meta_keywords', '$summary', '$created', '$lastmodified', '$author', ";
                        $sql .= " '$meta_redirect', '$meta_redirect_url', '$user_protected', '$side_online', '$side_start', '$side_end', ";
                        $sql .= " '$meta_author', '$meta_description', '$meta_robots', '$meta_redirect_time')";
                        $db->query($sql);
                }

                // idsidelang für die Templateerstellung raussuchen
                 $sql = "SELECT idsidelang FROM ".$dedi_db['side_lang']." WHERE idside='$idside'";
                $tmp_idsidelang = array();
                $affectedrows = getIdList($sql, $tmp_idsidelang, '', 'idsidelang');

                // Template erstellen
                if ($affectedrows) foreach ($tmp_idsidelang as $value) con_config_tpl_save($idtpl, $idlay, '', $value, $idtplconf);

                // Seite für Frontend erzeugen
                if ($cfg_client['publish'] == '1') {
                        foreach ($a_languages as $tmp_lang) {
                                $sql = 'INSERT INTO ' . $dedi_db['code'] . "(idlang, idcatside, changed) VALUES ('$tmp_lang', '".$idcatside['0']."', '1')";
                                $db->query($sql);
                        }
                }

                // Event
                fire_event('con_side_new', array('idside' => $idside, 'name' => $title));
        // Content aus Cache löschen
        $db->delete_cache('frontend_content');
                // Danach ins Frontend?
                // ermittle redirect-url
                if ($view) $url_location = $sess->url($cfg_client['htmlpath'].$cfg_client['contentfile'].'?lang='.$lang.'&idcatside='.$idcatside['0'].'&view='.$view);
                else $url_location = $sess->url("main.php?area=con_editframe&idcatside=".$idcatside['0']);
        } else {
                // handle $online-Angabe
                // 0 -> offline setzen
                // 1 -> online setzen
                // 2 -> zeitsteuerung setzen
                switch ((int)$online) {
                        case   0:
                                $change_online = 'online & 0xFC';
                                break;
                        case   1:
                                $change_online = '((online & 0xFC) | 0x01)';
                                break;
                        case   2:
                                $change_online = '((online & 0xFC) | 0x02)';
                                break;
                        default:
                                $change_online = '0';
                                break;
                }

                // update der 'side_lang' Tabelle
                $sql  = 'UPDATE ' . $dedi_db['side_lang']. ' ';
                $sql .= 'SET';
                $sql .= " title='$title', meta_keywords='$meta_keywords', summary='$summary', meta_redirect='$meta_redirect', ";
                $sql .= " meta_redirect_url='$meta_redirect_url', user_protected = '$user_protected', online = $change_online, start='$start', ";
                $sql .= " end='$end', meta_author='$meta_author', meta_description='$meta_description', meta_robots='$meta_robots', ";
                $sql .= " meta_redirect_time = '$meta_redirect_time' ";
                $sql .= 'WHERE idsidelang = ' . $idsidelang;
                $db->query($sql);

                // in welchem Ordner existiert die Seite?
                $sql = 'SELECT idcat FROM ' . $dedi_db['cat_side'] . ' WHERE idside = ' . $idside;
                $tmp_idcat = array();
                getIdList($sql, $tmp_idcat, 'idcat');

                if (is_array($tmp_idcat)) {
                        // Seite in neue Ordner einfügen
                        foreach ($idcatnew as $value) {
                                if (!in_array($value,$tmp_idcat)) {
                                        $sql = 'SELECT * FROM ' . $dedi_db['cat_side'] . ' WHERE idcat = ' . $value . ' AND is_start = 1';
                                        $db->query($sql);
                                        $is_start = ($db->next_record()) ? '0' : '1';
                                        if($value == $idcatnew['0'] && !in_array($idcat,$idcatnew)) {
                                        $sql  = 'UPDATE ' . $dedi_db['cat_side'] . ' ';
                                                $sql .= 'SET';
                                                $sql .= ' idcat    = ' . $value    . ',';
                                                $sql .= ' is_start = ' . $is_start . ' ';
                                                $sql .= 'WHERE  idcat = ' . $idcat;
                                                $sql .= ' AND  idside = ' . $idside;
                                                if (in_array($idcat,$idcatnew)) unset($tmp_idcat[$idcat]);
                                        } else {
                                                $sql  = 'INSERT INTO ' . $dedi_db['cat_side'] . ' ';
                                                $sql .= ' (idcat , idside , is_start ) ';
                                                $sql .= 'VALUES';
                                                $sql .= " ($value, $idside, $is_start) ";
                                        }
                                        $db->query($sql);
                                }
                        }

                        // Seite aus nicht benutzen Ordnern löschen
                        // jb_todo: rechte löschen??
                        foreach ($tmp_idcat as $value) {
                                if (!in_array($value,$idcatnew)) {

                                          // suche alle idcatsides, die nicht mehr existieren
                                        $sql = 'SELECT idcatside FROM ' . $dedi_db['cat_side'] . " WHERE idcat='$value' AND idside='$idside'";
                                        $db->query($sql);
                                        $db->next_record();

                                        // lösche alte 'code' Einträge
                                        $sql = 'DELETE FROM ' . $dedi_db['code'] . " WHERE idcatside='".$db->f('idcatside')."'";
                                        $db->query($sql);

                                        // lösche alte 'cat_side' Einträge
                                        $sql = 'DELETE FROM ' . $dedi_db['cat_side'] . " WHERE idside='$idside' AND idcat='$value'";
                                        $db->query($sql);

                                          // falls kein Startartikel mehr vorhanden neuen setzen
                                        $sql = 'SELECT * FROM ' . $dedi_db['cat_side'] . " WHERE idcat='$value' AND is_start='1'";
                                        $db->query($sql);
                                        if (!$db->affected_rows()) {
                                                $sql = 'UPDATE ' . $dedi_db['cat_side'] . " SET is_start = '1' WHERE idcat='$value' ORDER BY sortindex LIMIT 1";
                                                $db->query($sql);
                                        }

                                        // jb_todo:
                                        // lösche alte 'tpl_conf' Einträge
                                        // muß noch eingetragen werden

                                        //Event
                                        fire_event('get_unused_idcatside_by_save_side', array ('idside' => $idside, 'idcat' => $value, 'idcatside' => $db->f('idcatside')));
                                }
                        }
                }

                // Template konfigurieren
                $have_perm_save_configdata = $perm->have_perm(27, 'side', $idcatside_for_rights, $idcat_for_rights);
                /* if ($idtplconf == '0' && $idtpl != '0') {
                         $sql = 'SELECT idsidelang FROM ' . $dedi_db['side_lang'] . " WHERE idside = $idside";
                        $tmp_idsidelang = array();
                        $affected_rows = getIdList($sql, $tmp_idsidelang, '', 'idsidelang');

                        // Template erstellen
                        if ($affected_rows) {
                                foreach ($tmp_idsidelang as $value) con_config_tpl_save($idtpl, $idlay, '', $value, $idtplconf, $have_perm_save_configdata);
                        }
                }
                else */
                        con_config_tpl_save($idtpl, $idlay, '', $idsidelang, $idtplconf, $have_perm_save_configdata);

                // Rechte setzen
                if ($perm->have_perm(22, 'side', $idcatside_for_rights, $idcat_for_rights)) {
                        global $dedi_gruppenids, $dedi_gruppenrechte;
                        $perm->set_group_rights( 'side', $idcatside_for_rights, $dedi_gruppenids, $dedi_gruppenrechte, '', 0x7FFD0000, $idcat_for_rights, 0x7FFD0000);
                }

                // Codestatus ändern
                change_code_status($idcatside_for_rights, 1, 'idcatside');

                // Event
                fire_event('con_side_edit', array('idside' => $idside, 'name' => $title));
        // Content aus Cache löschen
        $db->delete_cache('frontend_content');
                // ermittle redirect-url
                if ($view) $url_location = $sess->url($cfg_client['htmlpath'].$cfg_client['contentfile'].'?lang='.$lang.'&idcatside='.$idcatside.'&view='.$view);
                else $url_location = $sess->url('main.php?area=con');
        }

        // Cache-Group Frontend löschen
        $db->delete_cache('frontend');
        redirect_page($url_location);
}

function getIdList($sql, &$array_for_ids, $idfield, $valfield = '') {
        global $db;

        // ermittle werte-feld und verhindere einen ungültigen namen
        $value_field = (empty($valfield)) ? $idfield:        $valfield;
        if (empty($value_field)) return 0;

        // führe sql aus und lade die gewünschten werte ins array
        $db->query($sql);
        if (empty($idfield)) {
                // erstelle array durch anhängen der neuen werte
                while ($db->next_record()) $array_for_ids[] = $db->f($value_field);
        } else {
                // erstelle array mit benannten elementen
                while ($db->next_record()) $array_for_ids[$db->f($idfield)] = $db->f($value_field);
        }
        return ($db->affected_rows());
}

function createDate($dateval, $timeval) {
        // datumswert vorbesetzen
        $datum = time();

        // zerlege übergabetexte
        $arr_date = @explode('.', $dateval);
        $arr_time = @explode(':', $timeval);

        // prüfe ob felder vorhanden, dann erzeuge datum
        if ($arr_time[0] && $arr_time[1] && $arr_date[1] && $arr_date[0] && $arr_date[2]) {
                $datum = mktime($arr_time[0], $arr_time[1], 0, $arr_date[1], $arr_date[0], $arr_date[2] );
        }

        // datumswert zurückliefern
        return $datum;
}

function redirect_page($url_location) {
        header ('Status: 302 Moved Temporarily');
        header ('Location:' . $url_location );
        exit;
}
?>