<?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 : 2005-11-24       - Bugfix for multibyte charsets
  Part of     : Update to Version 01.00.02
******************************************************************************/
if (!defined('IS_DEDI')) die();
include($dedi_path.'inc/fnc.type.php');

// Template suchen
$sql = "SELECT A.idtplconf, B.idtpl FROM $dedi_db[side_lang] A LEFT JOIN $dedi_db[tpl_conf] B USING(idtplconf) WHERE A.idside='$idside' AND A.idlang='$lang' AND A.idtplconf!='0'";
$db->query($sql);
if (!$db->affected_rows()) {
	$sql = "SELECT A.idtplconf, B.idtpl FROM $dedi_db[cat_lang] A LEFT JOIN $dedi_db[tpl_conf] B USING(idtplconf) WHERE A.idcat='$idcat' AND A.idlang='$lang' AND A.idtplconf!='0'";
	$db->query($sql);
}
if ($db->next_record()) {
	// Templatekonfiguration suchen
	$idtpl = $db->f('idtpl');
	$sql = "SELECT A.config, A.view, A.edit, B.container, C.name, C.output, C.idmod, C.verbose FROM $dedi_db[container_conf] A LEFT JOIN $dedi_db[container] B USING(idcontainer) LEFT JOIN $dedi_db[mod] AS C USING(idmod) WHERE A.idtplconf='".$db->f('idtplconf')."'";
	$db->query($sql);
	while ($db->next_record()) $container[$db->f('container')] = array ($db->f('config'), $db->f('view'), $db->f('edit'), $db->f('name'), $db->f('output'), $db->f('idmod'), $db->f('verbose'));

	// Content-Array erstellen
	$sql = "SELECT A.idcontent, container, number, idtype, typenumber, value FROM $dedi_db[content] A LEFT JOIN $dedi_db[side_lang] B USING(idsidelang) WHERE B.idside='$idside' AND B.idlang='$lang' ORDER BY number";
	$db->query($sql);
	while ($db->next_record()) $content[$db->f('container')][$db->f('number')][$db->f('idtype')][$db->f('typenumber')] = array($db->f('idcontent'), $db->f('value'));

	// Layout suchen
	$sql = "SELECT A.idlay, B.code FROM ".$dedi_db['tpl']." A LEFT JOIN ".$dedi_db['lay']." B USING(idlay) WHERE A.idtpl='$idtpl'";
	$db->query($sql);
	$db->next_record();
	$layout = $db->f('code');
	$layout = str_replace('<DEDIPHP:CACHE>', '<?PHP ', $layout);
	$layout = str_replace('</DEDIPHP:CACHE>', ' ?>', $layout);
	$idlay = $db->f('idlay');

	// Container generieren
	$list = extract_dedi_tags($layout);
	if (is_array($list)) {
		foreach ($list as $dedi_mod['container']) {
			// Head-Container?
			if ($dedi_mod['container']['type'] == 'head') {
				$code = '';

				// Meta-Tags generieren
				$code .= "<!--START head//-->\n";
				$code .= '<meta http-equiv="content-type" content="text/html; charset='.$lang_charset.'" />'."\n";
				$code .= "<meta name=\"generator\" content=\"Der Dirigent ".$cfg_dedi['version']." / www.der-dirigent.de\" />\n";
				$code .= '<?PHP if ($con_side[$idcatside][\'meta_author\'] != \'\') echo \'<meta name="author" content="\'.$con_side[$idcatside][\'meta_author\'].\'" />\'."\n"; ?>';
				$code .= '<?PHP if ($con_side[$idcatside][\'meta_description\'] != \'\') echo \'<meta name="description" content="\'.$con_side[$idcatside][\'meta_description\'].\'" />\'."\n"; ?>';
				$code .= '<?PHP if ($con_side[$idcatside][\'meta_keywords\'] != \'\') echo \'<meta name="keywords" content="\'.$con_side[$idcatside][\'meta_keywords\'].\'" />\'."\n"; ?>';
				$code .= '<?PHP if ($con_side[$idcatside][\'meta_robots\'] != \'\') echo \'<meta name="robots" content="\'.$con_side[$idcatside][\'meta_robots\'].\'" />\'."\n"; ?>';


				//JS and CSS file include
				$sql = "SELECT
							C.filetype, D.dirname, B.filename
						FROM
							". $dedi_db['lay_upl'] ." A
							LEFT JOIN ". $dedi_db['upl'] ." B USING(idupl)
							LEFT JOIN ". $dedi_db['filetype'] ." C USING(idfiletype)
							LEFT JOIN ". $dedi_db['directory'] ." D on B.iddirectory=D.iddirectory
						WHERE
							idlay='$idlay'";
				$db->query($sql);
				while ($db->next_record()) {
					if ($db->f('filetype') == 'js') $code .= "<script src=\"".$db->f('dirname').$db->f('filename')."\" type=\"text/javascript\"></script>\n";
					if ($db->f('filetype') == 'css') $code .= "<link rel=\"stylesheet\" href=\"".$db->f('dirname').$db->f('filename')."\" type=\"text/css\" />\n";
				}
				$code .= "<!--END head//-->\n";
				$search[] = $dedi_mod['container']['full_tag'];
				$replace[] = $code;

			// Seitenkonfigurationslayer?
			} elseif ($dedi_mod['container']['type'] == 'config') {
				$search[] = $dedi_mod['container']['full_tag'];
				$replace[] = '<DEDIPHP> if ($dedi_side[\'view\']) echo $con_side[$idcatside][\'config\'];</DEDIPHP>';
			} else {
				unset($code);
				unset($config);
				unset($output);

				// darf Modul gesehen werden?
				if ($container[$dedi_mod['container']['id']]['1'] == '0' || $container[$dedi_mod['container']['id']]['1'] == $auth->auth['uid']) {
					// Container konfigurieren
					$code = $container[$dedi_mod['container']['id']]['4'];
					$config = preg_split('/&/', $container[$dedi_mod['container']['id']]['0']);
					foreach ($config as $key1 => $value1) {
						$tmp2 = explode('=', $value1);
						if ($tmp2['1'] != '') {
							// $mod_value Array schreiben
							$dedi_mod['value'][$tmp2['0']] = dedi_stripslashes(urldecode($tmp2['1']));

							// MOD_VALUE[x] ersetzen
							$code = str_replace('MOD_VALUE['.$tmp2['0'].']', str_replace("\'","'", urldecode($tmp2['1'])), $code);
						}
						unset($tmp2);
					}

					// nicht benutzte Variablen strippen
					$code = preg_replace('/MOD_VALUE\[\d*\]/', '', $code);

					$code = str_replace('<DEDIPHP:CACHE>', '<?PHP ', $code);
			                $code = str_replace('</DEDIPHP:CACHE>', ' ?>', $code);

					if (stristr ($code, '<dedi:mod constant="tagmode" />')) {
						$code = str_replace('<dedi:mod constant="tagmode" />', '', $code);
						$code = dedi_stripslashes($code);
					}

					// Das Modul existiert noch nicht in der Datenbank
					if (!is_array($content[$dedi_mod['container']['id']])) $content[$dedi_mod['container']['id']]['1'] = 'neu';

					// Alle MOD_TAGS[] im Container ersetzen
					$used_type = extract_dedi_tags($code);

					// alle Module in einem Container generieren
					if(is_array($content[$dedi_mod['container']['id']])){
						foreach ($content[$dedi_mod['container']['id']] as $key3 => $value3) {
							// letztes Modul in diesem Container?
							if (!$content[$dedi_mod['container']['id']][$key3+1]) {
								$dedi_mod['modul']['lastentry'] = 'true';
								$pre_container_code = '<DEDIPHP> $dedi_mod[\'modul\'][\'lastentry\']=\'true\'; </DEDIPHP>';
							} else {
								unset($dedi_mod['modul']['lastentry']);
								$pre_container_code = '<DEDIPHP> unset($dedi_mod[\'modul\'][\'lastentry\']); </DEDIPHP>';
							}

							// erstes Modul generieren?
							if ($key3 == '1') {
								$container_code = $code;
								if (is_array($used_type)) {
									// DEDI-TAG in Funktionsaufruf umwandeln
									foreach ($used_type as $value4) {
										// DEDI-TAG Konfiguration auslesen
										$dedi_type_config = '\'';
										foreach ($value4 as $key5=>$value5) {
											if ($key5 != 'type' && $key5 != 'id' && $key5 != 'full_tag') $dedi_type_config .= '\''.$key5.'\'=>\''.str_replace('\"','"', dedi_addslashes($value5)).'\',';
										}

										// letztes Komma entfernen
										$dedi_type_config = substr ($dedi_type_config,  1, -1);
										if (!$value4['id']) $value4['id'] = '0';
										if ($value4['addslashes'] == 'true') $container_code = str_replace($value4['full_tag'], 'type_output_'.strtolower($value4['type']).'('.$dedi_mod['container']['id'].', $dedi_mod[\'modul\'][\'id\'], '.$value4['id'].', array('.$dedi_type_config.'))', $container_code);
										else $container_code = str_replace($value4['full_tag'], '<?PHP echo type_output_'.strtolower($value4['type']).'('.$dedi_mod['container']['id'].', $dedi_mod[\'modul\'][\'id\'], '.$value4['id'].', array('.$dedi_type_config.')); ?>', $container_code);
									}
								}

								// Modul cachen
								eval('$dedi_mod[\'modul\'][\'id\'] = $key3;$dedi_mod[\'key\'] = \'mod\'.$dedi_mod[\'container\'][\'id\'].\'_\'.$key3.\'_\'; ?>'.$pre_container_code.$container_code);
								$container_code_final = ob_get_contents ();
								ob_end_clean ();
								ob_start();
								$output = '<DEDIPHP> $dedi_mod[\'modul\'][\'id\']=\''.$key3.'\';$dedi_mod[\'key\'] = \'mod\'.$dedi_mod[\'container\'][\'id\'].\'_'.$key3.'_\'; </DEDIPHP>'.$container_code_final;

							// alle weiteren Module dranhängen
							} else {
								// Modul cachen
								eval('$dedi_mod[\'modul\'][\'id\'] = $key3;$dedi_mod[\'key\'] = \'mod\'.$dedi_mod[\'container\'][\'id\'].\'_\'.$key3.\'_\'; ?>'.$pre_container_code.$container_code);
								$container_code_final = ob_get_contents ();
								ob_end_clean ();
								ob_start();
								$output .= '<DEDIPHP> $dedi_mod[\'modul\'][\'id\']=\''.$key3.'\';$dedi_mod[\'key\'] = \'mod\'.$dedi_mod[\'container\'][\'id\'].\'_'.$key3.'_\'; </DEDIPHP>'.$container_code_final;
							}
						}
					}

					// Container in Array schreiben
					$search[] = $dedi_mod['container']['full_tag'];

					// ist Modul sichtbar?
					if ($container[$dedi_mod['container']['id']]['1'] == '0') $replace[] = '<!--START '.$dedi_mod['container']['id'].'//--><DEDIPHP> $dedi_mod[\'container\'][\'id\']=\''.$dedi_mod['container']['id'].'\'; </DEDIPHP>'.$output.'<!--END '.$dedi_mod['container']['id'].'//-->';
					else $replace[] = '<!--START '.$dedi_mod['container']['id'].'//--><DEDIPHP> $dedi_mod[\'container\'][\'id\']=\''.$dedi_mod['container']['id'].'\'; if(\''.$container[$dedi_mod['container']['id']]['1'].'\'==$auth->auth[\'uid\']) { </DEDIPHP>'.$output.'<DEDIPHP> } </DEDIPHP><!--END '.$dedi_mod['container']['id'].'//-->';
					unset($output);
					unset($used_type, $dedi_mod['value']);
				} else {
					// Modul ist nicht sichtbar
					$search[] = $dedi_mod['container']['full_tag'];
					$replace[] = '';
				}
			}
		}

		// Seite erstellen
		$code = $layout;
		foreach ($search as $key=>$value) $code = str_replace($value, $replace[$key], $code);
	} else $code = $layout;
} else $code = $dedi_lang['con_notemplate'];

// Dynamisches PHP beibehalten
eval(' ?>'.$code);
$code = ob_get_contents ();
ob_end_clean ();
ob_start();
$code = str_replace('<DEDIPHP>', '<?PHP ', $code);
$code = str_replace('</DEDIPHP>', ' ?>', $code);

// Dateilinks ersetzen
if ($cfg_client['download_control'] != 1) {
	// Dateilinks suchen...
	preg_match_all("!http://dedilink/fileid=(\d+)!", $code, $internlinks);
	$sql_links = implode(',', $internlinks[1]);
	if ($sql_links != '') {
		$sql = "SELECT A.idupl id, A.filename filename,B.dirname dirname
			FROM ".$dedi_db['upl']." as A
			LEFT JOIN ". $dedi_db['directory'] ." as B USING(iddirectory)
			WHERE A.idclient=$client
			AND A.idupl IN ($sql_links)";
		$db->query($sql);
		while($db->next_record()) $dedi_file[$db->f('id')] = $cfg_client['upl_htmlpath'].$db->f('dirname').$db->f('filename');
	}

	// ...und ersetzen
	$in ="'http://dedilink/fileid=(\d+)'e";
	$out = '\$dedi_file[\\1]';
	$code = preg_replace($in, $out, $code);
}
?>