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: public function __construct($tags = false) {
85: if (is_array($tags)) {
86: $this->tags = $tags;
87: }
88:
89: $this->setEncoding("");
90: }
91:
92: 93: 94: 95: 96:
97: public function setDomain($sDomain) {
98: $this->_sDomain = $sDomain;
99: }
100:
101: 102: 103: 104: 105: 106: 107: 108: 109: 110:
111: public function set($which, $needle, $replacement) {
112: if ($which == 's') {
113:
114: $this->needles[] = sprintf($this->tags['static'], $needle);
115: $this->replacements[] = $replacement;
116: } else {
117:
118: $this->Dyn_needles[$this->dyn_cnt][] = sprintf($this->tags['static'], $needle);
119: $this->Dyn_replacements[$this->dyn_cnt][] = $replacement;
120: }
121: }
122:
123: 124: 125: 126: 127:
128: public function setEncoding($encoding) {
129: $this->_encoding = $encoding;
130: }
131:
132: 133: 134:
135: public function next() {
136: $this->dyn_cnt++;
137: }
138:
139: 140: 141:
142: public function reset() {
143: $this->dyn_cnt = 0;
144: $this->needles = array();
145: $this->replacements = array();
146: $this->Dyn_needles = array();
147: $this->Dyn_replacements = array();
148: }
149:
150: 151: 152: 153: 154: 155: 156: 157: 158: 159:
160: public function generate($template, $return = 0, $note = 0) {
161: global $cCurrentModule, $cfg;
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: } else {
174:
175: $content = &$template;
176: }
177: } else {
178: if (is_object($moduleHandler) && is_file($moduleHandler->getTemplatePath($template))) {
179:
180: $content = $moduleHandler->getFilesContent('template', '', $template);
181: } else {
182:
183: $content = implode('', file($template));
184: }
185: }
186:
187: $content = (($note) ? "<!-- Generated by CONTENIDO " . $cfg['version'] . "-->\n" : "") . $content;
188:
189: $pieces = array();
190:
191:
192: $this->replacei18n($content, 'i18n');
193: $this->replacei18n($content, 'trans');
194:
195:
196: $startQ = preg_quote($this->tags['start'], '/');
197: $endQ = preg_quote($this->tags['end'], '/');
198: if (preg_match('/^.*' . $startQ . '.*?' . $endQ . '.*$/s', $content)) {
199:
200: preg_match_all('/^(.*)' . $startQ . '(.*?)' . $endQ . '(.*)$/s', $content, $pieces);
201:
202: array_shift($pieces);
203: $content = '';
204:
205:
206: $content .= str_replace($this->needles, $this->replacements, $pieces[0][0]);
207: unset($pieces[0][0]);
208:
209:
210: for ($a = 0; $a < $this->dyn_cnt; $a++) {
211: $content .= str_replace($this->Dyn_needles[$a], $this->Dyn_replacements[$a], $pieces[1][0]);
212: }
213: unset($pieces[1][0]);
214:
215:
216: $content .= str_replace($this->needles, $this->replacements, $pieces[2][0]);
217: unset($pieces[2][0]);
218: } else {
219: $content = str_replace($this->needles, $this->replacements, $content);
220: }
221:
222: if ($this->_encoding != '') {
223: $content = str_replace("</head>", '<meta http-equiv="Content-Type" content="text/html; charset=' . $this->_encoding . '">' . "\n" . '</head>', $content);
224: }
225:
226: if ($return) {
227: return $content;
228: } else {
229: echo $content;
230: }
231: }
232:
233: 234: 235: 236: 237: 238: 239:
240: public function replacei18n(&$template, $functionName) {
241: $container = array();
242:
243:
244: $php_matches = array();
245: 246: 247: 248: 249: 250:
251:
252: $functionNameQ = preg_quote($functionName, '/');
253:
254:
255: $matches = array();
256: preg_match_all('/' . $functionNameQ . "\\(([\\\"\\'])(.*?)\\1\\)/s", $template, $matches);
257:
258: $matches = array_values(array_unique($matches[2]));
259: for ($a = 0; $a < count($matches); $a++) {
260: $template = preg_replace('/' . $functionNameQ . "\\([\\\"\\']" . preg_quote($matches[$a], '/') . "[\\\"\\']\\)/s", i18n($matches[$a], $this->_sDomain), $template);
261: }
262:
263:
264: if (is_array($container)) {
265: foreach ($container as $x => $php_match) {
266: $template = str_replace('{PHP#' . $x . '#PHP}', $php_match, $template);
267: }
268: }
269: }
270:
271: }
272: