assets/s2ditemplate.php /// Art: Include /// Inhalt: Klasse template /// Beschreibung: Verarbeitet Templates für shop to date 6 /// Benötigt: config.php /// CCML-Parsing: nein /// ////////////////////////////////////////////////////////////////////////////////////////// /// /// Letzte Änderungen: /// 09.07.2008 Mehrdimensionale Loops eingeführt /// ////////////////////////////////////////////////////////////////////////////////////////// ///<09.07.2008/6.0.2/> if (!defined('SHOP_TO_DATE')) die('Forbidden'); // Klasse für Templates class template { var $filename; var $markers = array(); var $vars = array(); var $lang = array(); var $loops = array(); var $shopmode; // Konstruktor legt Template Datei an function template($filename, $mode = 1) { $this->filename = $filename; if ($filename) $this->validate(); $this->shopmode = $mode; } // Überprüfung auf Gültigkeit, ggf. Fehler melden function validate() { $stack = array(); $modes = array(null, 'ENDIF', 'ENDLOOP', 'ENDIFLOOPVAR'); $ln = 0; if ($this->shopmode == 2) $lines = array_merge(file(CC_TPL_OSHEADER), file($this->filename), file(CC_TPL_OSFOOTER)); else if ($this->shopmode == 0) $lines = file($this->filename); else $lines = array_merge(file(CC_TPL_HEADER), file($this->filename)); foreach($lines as $line) { $ln++; if (preg_match('//', $line)) { array_push($stack, 1); } else if (preg_match('//', $line)) { array_push($stack, 1); } else if (preg_match('//', $line)) { array_push($stack, 1); } else if (preg_match('//', $line)) { } else if (preg_match('//', $line)) { if ($m = array_pop($stack) != 1) { var_dump($m); var_dump(1); die("Template error in $this->filename: ".'Found ENDIF, expected '.$modes[$m]." ($m) on line $ln"); } } else if (preg_match('//', $line)) { array_push($stack, 2); } else if (preg_match('//', $line)) { if ($m = array_pop($stack) != 2) die("Template error in $this->filename: ".'Found ENDLOOP, expected '.$modes[$m]." on line $ln"); } else if (preg_match('//', $line)) { array_push($stack, 4); } else if (preg_match('//', $line)) { if ($m = array_pop($stack) != 4) die("Template error in $this->filename: ".'Found ENDSUBLOOP, expected '.$modes[$m]." on line $ln"); } else if (preg_match('//', $line)) { array_push($stack, 3); } else if (preg_match('//', $line)) { } else if (preg_match('//', $line)) { if ($m = array_pop($stack) != 3) die("Template error in $this->filename: ".'Found ENDIFLOOPVAR, expected '.$modes[$m]." on line $ln"); } else if (preg_match('//', $line, $parts)) ; else if (preg_match('//', $line, $parts)) die("Template error in $this->filename: ".'Unknown command '.$parts[1]." on line $ln"); } if ($m = array_pop($stack)) die("Template error in $this->filename: ".'Eof, expected '.$modes[$m]); } // Bestimmten Teil IF ENDIF zeigen function show($marker) { $this->markers[$marker] = true; } // Bestimmten Teil IF ENDIF zeigen, wenn Bedingung gegeben ist function show_cond($cond, $marker) { if ($cond) $this->show($marker); } // Variablen zuordnen function assign($vars) { foreach ($vars as $k => $v) if (is_scalar($v)) $this->vars[$k] = $v; } // Variablen zuordnen, wenn Bedingung gegeben ist function assign_cond($cond, $vars) { if ($cond) $this->assign($vars); } // Schleifeninhalt LOOP ENDLOOP definieren function loop($loopname, $vars, $object = null) { if ($object) foreach (get_object_vars($object) as $k => $v) if (is_scalar($v) || is_null($v)) $vars = array_merge($vars, array($k => $v)); if (!isset($this->loops[$loopname])) $this->loops[$loopname] = array(); array_push($this->loops[$loopname], $vars); } // Ausgabe erstellen function getparsed() { global $lines, $html, $orig, $repl; if ($this->shopmode == 2) $lines = array_merge(file(CC_TPL_OSHEADER), file($this->filename), file(CC_TPL_OSFOOTER)); else if ($this->shopmode == 0) $lines = file($this->filename); else $lines = array_merge(file(CC_TPL_HEADER), file($this->filename)); $html = array(); $orig = array(); $repl = array(); // Template parsen und je nach Seitenansicht filtern, Schleifen ausführen $this->parse(true); // Ersetzungsfelder vorbereiten foreach ($this->vars as $k => $v) { $orig[] = "/{".$k."}/"; $repl[] = strstr($v, 'selected') || strstr($v, 'checked') ? $v : (strstr($v, '') ? (str_replace('', '', $v)) : htmlentities($v)); } foreach ($this->lang as $k => $v) { $orig[] = "/{".$k."}/"; $repl[] = $v; } $orig[] = "/{DEBUG}/"; $repl[] = DEBUG ? 'true' : 'false'; // HTML Stream erzeugen $out = ''; foreach ($html as $h) $out .= $h; // CC Konstante ersetzen preg_match_all("/{([A-Z_0-9]+)}/", $out, $regs); foreach ($regs[1] as $r) { $orig[] = "/\{$r\}/"; $repl[] = defined($r) ? constant($r) : "MISSING CONSTANT -$r-"; } $orig[] = "/{([a-z0-9_])+?}/"; $repl[] = ''; // Seite ersetzen und ausgeben return preg_replace($orig, $repl, $out); } // Ausgabe ausgeben function out() { echo $this->getparsed(); } // Rekursieve Parsung des Templates function parse($show) { global $html, $lines, $looplines, $loop, $orig, $repl; while($line = array_shift($lines)) { if (preg_match('//', $line, $found)) { if (isset($this->markers[$found[1]])) { if ($this->parse(true && $show)) $this->parse(false && $show); } else { if ($this->parse(false && $show)) $this->parse(true && $show); } } else if (preg_match('//', $line, $found)) { if (!isset($this->markers[$found[1]])) { if ($this->parse(true && $show)) $this->parse(false && $show); } else { if ($this->parse(false && $show)) $this->parse(true && $show); } } else if (preg_match('//', $line, $found)) { if (constant($found[1])) { if ($this->parse(true && $show)) $this->parse(false && $show); } else { if ($this->parse(false && $show)) $this->parse(true && $show); } } else if (preg_match('//', $line, $found)) { if (isset($this->markers[constant($found[1])])) { if ($this->parse(true && $show)) $this->parse(false && $show); } else { if ($this->parse(false && $show)) $this->parse(true && $show); } } else if (preg_match('//', $line, $found)) { return true; } else if (preg_match('//', $line, $found)) { return false; } else if (preg_match('//', $line, $found)) { $loop = $found[1]; $looplines = array(); $this->parse($show); } else if (preg_match('//', $line)) { if (!$loop) die('found ENDLOOP widthout LOOP'); if (isset($this->loops[$loop]) && $show) { foreach ($this->loops[$loop] as $vars) { $orig = array(); $repl = array(); foreach (array_keys($vars) as $key) { $orig[] = '/{'.$loop.".$key}/"; $repl[] = $vars[$key]; } $stop = false; $store = array(); foreach ($looplines as $loopline) array_push($store, preg_replace($orig, $repl, $loopline)); $subloop = null; foreach ($store as $loopline) { if (preg_match('//', $loopline, $found)) { if (!$vars[$found[1]]) $stop = true; } else if (preg_match('//', $loopline)) { $stop = !$stop; } else if (preg_match('//', $loopline)) $stop = false; else if (preg_match('//', $loopline, $found)) { $subloop = $found[1].$found[2]; $subloop_name = $found[1]; $sublooplines = array(); } else if (preg_match('//', $loopline)) { if (isset($this->loops[$subloop])) foreach ($this->loops[$subloop] as $subvars) { $suborig = array(); $subrepl = array(); foreach (array_keys($subvars) as $key) { $suborig[] = '/{'.$subloop_name.".$key}/"; $subrepl[] = $subvars[$key]; } foreach ($sublooplines as $subloopline) array_push($html, preg_replace($suborig, $subrepl, $subloopline)); } $subloop = false; } else if (!$stop) { if ($subloop) array_push($sublooplines, $loopline); else array_push($html, preg_replace($orig, $repl, $loopline)); } } } } $loop = null; return; } else if ($loop && $show) array_push($looplines, $line); else if ($show) array_push($html, $line); } } // Ressourcen hinzufügen function ressources($file) { $nohtml = false; foreach(file($file) as $line) { $line = trim($line); if (strlen($line)) { if (preg_match("/^\[/", $line)) $nohtml = false; else if (preg_match("/^;nohtml$/", $line, $found)) $nohtml = true; else if (preg_match("/^([a-z0-9]+)=(.+)$/", $line, $found)) { if ($nohtml) $this->lang[$found[1]] = $found[2]; else $this->vars[$found[1]] = $found[2]; } } } } } ?>