1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: 19: 20: 21: 22: 23: 24:
25: class cTemplate {
26:
27: 28: 29: 30: 31:
32: public $needles = array();
33:
34: 35: 36: 37: 38:
39: public $replacements = array();
40:
41: 42: 43: 44: 45:
46: public $Dyn_needles = array();
47:
48: 49: 50: 51: 52:
53: public $Dyn_replacements = array();
54:
55:
56: 57: 58: 59: 60:
61: public $dyn_cnt = 0;
62:
63: 64: 65: 66: 67:
68: public $tags = array(
69: 'static' => '{%s}',
70: 'start' => '<!-- BEGIN:BLOCK -->',
71: 'end' => '<!-- END:BLOCK -->'
72: );
73:
74: 75: 76: 77: 78:
79: protected $_sDomain = 'contenido';
80:
81: 82: 83: 84:
85: public function __construct($tags = false) {
86: if (is_array($tags)) {
87: $this->tags = $tags;
88: }
89:
90: $this->setEncoding("");
91: }
92:
93: 94: 95: 96: 97:
98: public function setDomain($sDomain) {
99: $this->_sDomain = $sDomain;
100: }
101:
102: 103: 104: 105: 106: 107: 108: 109: 110: 111:
112: public function set($which, $needle, $replacement) {
113: if ($which == 's') {
114:
115: $this->needles[] = sprintf($this->tags['static'], $needle);
116: $this->replacements[] = $replacement;
117: } else {
118:
119: $this->Dyn_needles[$this->dyn_cnt][] = sprintf($this->tags['static'], $needle);
120: $this->Dyn_replacements[$this->dyn_cnt][] = $replacement;
121: }
122: }
123:
124: 125: 126: 127: 128:
129: public function setEncoding($encoding) {
130: $this->_encoding = $encoding;
131: }
132:
133: 134: 135:
136: public function next() {
137: $this->dyn_cnt++;
138: }
139:
140: 141: 142:
143: public function reset() {
144: $this->dyn_cnt = 0;
145: $this->needles = array();
146: $this->replacements = array();
147: $this->Dyn_needles = array();
148: $this->Dyn_replacements = array();
149: }
150:
151: 152: 153: 154: 155: 156: 157: 158: 159:
160: public function generate($template, $return = 0, $note = 0) {
161: global $cCurrentModule, $cfg, $frontend_debug;
162:
163: $moduleHandler = NULL;
164: if (!is_null($cCurrentModule)) {
165: $moduleHandler = new cModuleHandler($cCurrentModule);
166: }
167:
168:
169: if (!@is_file($template)) {
170: if (is_object($moduleHandler) && is_file($moduleHandler->getTemplatePath($template))) {
171:
172: $content = $moduleHandler->getFilesContent('template', '', $template);
173: if ($frontend_debug['template_display']) {
174: echo('<!-- CTEMPLATE ' . $template . ' -->');
175: }
176: } else {
177:
178: $content = &$template;
179: }
180: } else {
181: if (is_object($moduleHandler) && is_file($moduleHandler->getTemplatePath($template))) {
182:
183: $content = $moduleHandler->getFilesContent('template', '', $template);
184: } else {
185:
186: $content = implode('', file($template));
187: }
188: }
189:
190: $content = (($note) ? "<!-- Generated by CONTENIDO " . CON_VERSION . "-->\n" : "") . $content;
191:
192:
193: $content = cApiCecHook::executeAndReturn('Contenido.Template.BeforeParse', $content, $this);
194:
195: $pieces = array();
196:
197:
198: $this->replacei18n($content, 'i18n');
199: $this->replacei18n($content, 'trans');
200:
201:
202: $startQ = preg_quote($this->tags['start'], '/');
203: $endQ = preg_quote($this->tags['end'], '/');
204: if (preg_match('/^.*' . $startQ . '.*?' . $endQ . '.*$/s', $content)) {
205:
206: preg_match_all('/^(.*)' . $startQ . '(.*?)' . $endQ . '(.*)$/s', $content, $pieces);
207:
208: array_shift($pieces);
209: $content = '';
210:
211:
212: $content .= str_replace($this->needles, $this->replacements, $pieces[0][0]);
213: unset($pieces[0][0]);
214:
215:
216: for ($a = 0; $a < $this->dyn_cnt; $a++) {
217: $content .= str_replace($this->Dyn_needles[$a], $this->Dyn_replacements[$a], $pieces[1][0]);
218: }
219: unset($pieces[1][0]);
220:
221:
222: $content .= str_replace($this->needles, $this->replacements, $pieces[2][0]);
223: unset($pieces[2][0]);
224: } else {
225: $content = str_replace($this->needles, $this->replacements, $content);
226: }
227:
228: if ($this->_encoding != '') {
229:
230: }
231:
232: if ($return) {
233: return $content;
234: } else {
235: echo $content;
236: }
237: }
238:
239: 240: 241: 242: 243: 244: 245:
246: public function replacei18n(&$template, $functionName) {
247: $container = array();
248:
249:
250: $php_matches = array();
251: 252: 253: 254: 255: 256:
257:
258: $functionNameQ = preg_quote($functionName, '/');
259:
260:
261: $matches = array();
262: preg_match_all('/' . $functionNameQ . "\\(([\\\"\\'])(.*?)\\1\\)/s", $template, $matches);
263:
264: $matches = array_values(array_unique($matches[2]));
265: for ($a = 0; $a < count($matches); $a++) {
266: $template = preg_replace('/' . $functionNameQ . "\\([\\\"\\']" . preg_quote($matches[$a], '/') . "[\\\"\\']\\)/s", i18n($matches[$a], $this->_sDomain), $template);
267: }
268:
269:
270: if (is_array($container)) {
271: foreach ($container as $x => $php_match) {
272: $template = str_replace('{PHP#' . $x . '#PHP}', $php_match, $template);
273: }
274: }
275: }
276:
277: }
278: