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:
161: public function generate($template, $return = 0, $note = 0) {
162: global $cCurrentModule, $cfg, $frontend_debug;
163:
164: $moduleHandler = NULL;
165: if (!is_null($cCurrentModule)) {
166: $moduleHandler = new cModuleHandler($cCurrentModule);
167: }
168:
169:
170: if (!@is_file($template)) {
171: if (is_object($moduleHandler) && is_file($moduleHandler->getTemplatePath($template))) {
172:
173: $content = $moduleHandler->getFilesContent('template', '', $template);
174: if ($frontend_debug['template_display']) {
175: echo('<!-- CTEMPLATE ' . $template . ' -->');
176: }
177: } else {
178:
179: $content = &$template;
180: }
181: } else {
182: if (is_object($moduleHandler) && is_file($moduleHandler->getTemplatePath($template))) {
183:
184: $content = $moduleHandler->getFilesContent('template', '', $template);
185: } else {
186:
187: $content = implode('', file($template));
188: }
189: }
190:
191: $content = (($note) ? "<!-- Generated by CONTENIDO " . CON_VERSION . "-->\n" : "") . $content;
192:
193: $pieces = array();
194:
195:
196: $this->replacei18n($content, 'i18n');
197: $this->replacei18n($content, 'trans');
198:
199:
200: $startQ = preg_quote($this->tags['start'], '/');
201: $endQ = preg_quote($this->tags['end'], '/');
202: if (preg_match('/^.*' . $startQ . '.*?' . $endQ . '.*$/s', $content)) {
203:
204: preg_match_all('/^(.*)' . $startQ . '(.*?)' . $endQ . '(.*)$/s', $content, $pieces);
205:
206: array_shift($pieces);
207: $content = '';
208:
209:
210: $content .= str_replace($this->needles, $this->replacements, $pieces[0][0]);
211: unset($pieces[0][0]);
212:
213:
214: for ($a = 0; $a < $this->dyn_cnt; $a++) {
215: $content .= str_replace($this->Dyn_needles[$a], $this->Dyn_replacements[$a], $pieces[1][0]);
216: }
217: unset($pieces[1][0]);
218:
219:
220: $content .= str_replace($this->needles, $this->replacements, $pieces[2][0]);
221: unset($pieces[2][0]);
222: } else {
223: $content = str_replace($this->needles, $this->replacements, $content);
224: }
225:
226: if ($this->_encoding != '') {
227: $content = str_replace("</head>", '<meta http-equiv="Content-Type" content="text/html; charset=' . $this->_encoding . '">' . "\n" . '</head>', $content);
228: }
229:
230: if ($return) {
231: return $content;
232: } else {
233: echo $content;
234: }
235: }
236:
237: 238: 239: 240: 241: 242: 243:
244: public function replacei18n(&$template, $functionName) {
245: $container = array();
246:
247:
248: $php_matches = array();
249: 250: 251: 252: 253: 254:
255:
256: $functionNameQ = preg_quote($functionName, '/');
257:
258:
259: $matches = array();
260: preg_match_all('/' . $functionNameQ . "\\(([\\\"\\'])(.*?)\\1\\)/s", $template, $matches);
261:
262: $matches = array_values(array_unique($matches[2]));
263: for ($a = 0; $a < count($matches); $a++) {
264: $template = preg_replace('/' . $functionNameQ . "\\([\\\"\\']" . preg_quote($matches[$a], '/') . "[\\\"\\']\\)/s", i18n($matches[$a], $this->_sDomain), $template);
265: }
266:
267:
268: if (is_array($container)) {
269: foreach ($container as $x => $php_match) {
270: $template = str_replace('{PHP#' . $x . '#PHP}', $php_match, $template);
271: }
272: }
273: }
274:
275: }
276: