1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16:
17: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
18:
19: 20: 21: 22: 23: 24:
25: class cCodeGeneratorStandard extends cCodeGeneratorAbstract {
26:
27: 28: 29:
30: public function _generate($contype = true) {
31: global $cfg, $code;
32:
33: $this->_cssData = '';
34: $this->_jsData = '';
35: $this->_tplName = '';
36:
37: cDebug::out("conGenerateCode($this->_idcat, $this->_idart, $this->_lang, $this->_client, $this->_layout);<br>");
38:
39:
40: $idcatart = conGetCategoryArticleId($this->_idcat, $this->_idart);
41:
42:
43: $this->_idtplcfg = $this->_getTemplateConfigurationId();
44: if (NULL === $this->_idtplcfg) {
45: $this->_processNoConfigurationError($idcatart);
46: return '0601';
47: }
48:
49:
50: $containerConfigurations = conGetContainerConfiguration($this->_idtplcfg);
51:
52:
53: $data = $this->_getTemplateData();
54: $idlay = $data['idlay'];
55: $idtpl = $data['idtpl'];
56: $this->_tplName = cApiStrCleanURLCharacters($data['name']);
57:
58:
59: $containerModules = conGetUsedModules($idtpl);
60:
61:
62: $layoutInFile = new cLayoutHandler($idlay, '', $cfg, $this->_lang);
63: $this->_layoutCode = $layoutInFile->getLayoutCode();
64: $this->_layoutCode = cApiStrNormalizeLineEndings($this->_layoutCode, "\n");
65:
66: $moduleHandler = new cModuleHandler();
67:
68:
69: if ($idlay) {
70: cInclude('includes', 'functions.tpl.php');
71: $containerNumbers = tplGetContainerNumbersInLayout($idlay);
72:
73: foreach ($containerNumbers as $containerNr) {
74: if (!isset($containerModules[$containerNr]) || !is_numeric($containerModules[$containerNr])) {
75:
76:
77: $this->_resetModule();
78: $this->_processCmsContainer($containerNr);
79: continue;
80: }
81:
82: $containerModuleId = $containerModules[$containerNr];
83: $oModule = new cApiModule($containerModuleId);
84: $module = $oModule->toArray();
85: if (false === $module) {
86: $module = array();
87: }
88:
89: $this->_resetModule();
90:
91: $this->_modulePrefix[] = '$cCurrentModule = ' . $containerModuleId . ';';
92: $this->_modulePrefix[] = '$cCurrentContainer = ' . $containerNr . ';';
93:
94: $moduleHandler = new cModuleHandler($containerModuleId);
95: $input = '';
96:
97:
98:
99: if ($moduleHandler->modulePathExists() == true) {
100: $this->_moduleCode = $moduleHandler->readOutput();
101:
102: if ($moduleHandler->getFilesContent('css', 'css') !== false) {
103: $this->_cssData .= $moduleHandler->getFilesContent('css', 'css');
104: }
105:
106: if ($moduleHandler->getFilesContent('js', 'js') !== false) {
107: $this->_jsData .= $moduleHandler->getFilesContent('js', 'js');
108: }
109:
110: $input = $moduleHandler->readInput();
111: }
112:
113: $this->_moduleCode = $this->_moduleCode . "\n";
114:
115:
116: $containerCmsValues = $this->_processCmsValueTags($containerNr, $containerConfigurations[$containerNr]);
117:
118:
119: if ($containerCmsValues) {
120: $this->_modulePrefix[] = $containerCmsValues;
121: }
122:
123:
124: $this->_processFrontendDebug($containerNr, $module);
125:
126:
127: $this->_processCmsContainer($containerNr);
128: }
129: }
130:
131:
132: $a_content = $this->_getUsedCmsTypesData();
133:
134:
135: if ($contype) {
136: $this->_processCmsTags($a_content, true);
137: }
138:
139:
140: $this->_processCodeTitleTag();
141:
142:
143: $this->_processCodeMetaTags();
144:
145:
146:
147: $cssFile = '';
148: if (strlen($this->_cssData) > 0) {
149: if (($myFileCss = $moduleHandler->saveContentToFile($this->_tplName, 'css', $this->_cssData)) !== false) {
150: $oHTML = new cHTML(array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => $myFileCss));
151: $oHTML->setTag('link');
152: $cssFile = $oHTML->render();
153: }
154: }
155:
156: $jsFile = '';
157: if (strlen($this->_jsData) > 0) {
158: if (($myFileJs = $moduleHandler->saveContentToFile($this->_tplName, 'js', $this->_jsData)) !== false) {
159: $jsFile = '<script src="' . $myFileJs . '" type="text/javascript"></script>';
160: }
161: }
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179: if (strpos($this->_layoutCode, '{CSS}') !== false) {
180: $this->_layoutCode = cString::iReplaceOnce('{CSS}', $cssFile, $this->_layoutCode);
181: } else if (!empty($cssFile)) {
182: if (strpos($this->_layoutCode, '</title>') !== false) {
183: $this->_layoutCode = cString::iReplaceOnce('</title>', '</title>' . $cssFile, $this->_layoutCode);
184: } else {
185: $this->_layoutCode = cString::iReplaceOnce('<head>', '<head>' . $cssFile, $this->_layoutCode);
186: }
187: }
188:
189:
190:
191: if (strpos($this->_layoutCode, '{JS}') !== false) {
192: $this->_layoutCode = cString::iReplaceOnce('{JS}', $jsFile, $this->_layoutCode);
193: } else if (!empty($jsFile)) {
194: $this->_layoutCode = cString::iReplaceOnce('</body>', $jsFile . '</body>', $this->_layoutCode);
195: }
196:
197: if ($this->_feDebugOptions['general_information']) {
198: $debugPrefix = '';
199:
200: $debugPrefix .= "<?php\nif (\$frontend_debug['general_information']) {\n";
201: $debugPrefix .= "\techo(\"<!-- \\n\\n\");\n";
202:
203: $layout = new cApiLayout($idlay);
204: $layouName = $layout->get('name');
205: $debugPrefix .= "\techo(\"Layout: " . $layouName . " (" . $idlay . ")\\n\");\n";
206:
207: $debugPrefix .= "\techo(\"Template: " . $this->_tplName . " (" . $idtpl . ")\\n\");\n";
208:
209: $article = new cApiArticleLanguage($this->_idartlang);
210: $catart = new cApiCategoryArticle();
211: $cat = new cApiCategoryLanguage();
212: $cat->loadByCategoryIdAndLanguageId($this->_idcat, $article->get('idlang'));
213: $catart->loadByMany(array(
214: 'idcat' => $cat->get('idcat'),
215: 'idart' => $article->get('idart')
216: ));
217: $lang = new cApiLanguage($article->get('idlang'));
218: $debugPrefix .= "\techo(\"Language: " . $lang->get('idlang') . " (" . $lang->get('name') . ")\\n\");\n";
219:
220: $debugPrefix .= "\techo(\"Category: " . $cat->get('idcat') . " (" . $cat->get('name') . ")\\n\");\n";
221:
222: $articleName = $article->get('title');
223: $debugPrefix .= "\techo(\"Article: " . $articleName . " (catart = " . $catart->get('idcatart') . ", artlang = " . $this->_idartlang . ", art = " . $article->get('idart') . ")\\n\");\n";
224:
225:
226: $debugPrefix .= "\techo(\"\\n--> \\n\");\n";
227: $debugPrefix .= "}\n?>";
228:
229: $this->_layoutCode = $debugPrefix . $this->_layoutCode;
230: }
231:
232:
233: $this->_saveGeneratedCode($idcatart);
234:
235: return $this->_layoutCode;
236: }
237:
238: 239: 240: 241: 242: 243: 244: 245: 246:
247: protected function _processNoConfigurationError($idcatart) {
248: cDebug::out('Neither CAT or ART are configured!<br><br>');
249:
250: $code = '<html><body>No code was created for this article in this category.</body><html>';
251: $this->_saveGeneratedCode($idcatart, $code, false);
252: }
253:
254: 255: 256: 257: 258: 259: 260: 261:
262: protected function _processCodeTitleTag() {
263: if ($this->_pageTitle == '') {
264: cApiCecHook::setDefaultReturnValue($this->_pageTitle);
265: $this->_pageTitle = cApiCecHook::executeAndReturn('Contenido.Content.CreateTitletag');
266: }
267:
268:
269: if ($this->_pageTitle != '') {
270: $this->_layoutCode = preg_replace('/<title>.*?<\/title>/is', '{TITLE}', $this->_layoutCode, 1);
271: if (strstr($this->_layoutCode, '{TITLE}')) {
272: $this->_layoutCode = str_ireplace('{TITLE}', '<title>' . $this->_pageTitle . '</title>', $this->_layoutCode);
273: } else {
274: $this->_layoutCode = cString::iReplaceOnce('</head>', '<title>' . $this->_pageTitle . "</title>\n</head>", $this->_layoutCode);
275: }
276: } else {
277: $this->_layoutCode = str_replace('<title></title>', '', $this->_layoutCode);
278: }
279:
280: return $this->_layoutCode;
281: }
282:
283: 284: 285: 286: 287: 288: 289: 290:
291: protected function _processCodeMetaTags() {
292: global $encoding;
293:
294:
295: $metaTags = $this->_getBasicMetaTags();
296:
297:
298: $_cecIterator = cRegistry::getCecRegistry()->getIterator('Contenido.Content.CreateMetatags');
299: if ($_cecIterator->count() > 0) {
300: while (false !== $chainEntry = $_cecIterator->next()) {
301: $metaTags = $chainEntry->execute($metaTags);
302: }
303: }
304:
305: $sMetatags = '';
306: foreach ($metaTags as $value) {
307:
308:
309: $valueKeys = array_keys($value);
310: $nameKey = 'name';
311: foreach ($valueKeys as $key) {
312:
313: if ($key != 'content')
314: $nameKey = $key;
315: }
316:
317:
318:
319: if (isset($value['content'])) {
320: $value['content'] = conHtmlEntityDecode($value['content'], ENT_QUOTES, strtoupper($encoding[$this->_lang]));
321: $value['content'] = htmlspecialchars_decode($value['content'], ENT_QUOTES);
322: }
323:
324:
325: $oMetaTagGen = new cHTML();
326: $oMetaTagGen->setTag('meta');
327: $oMetaTagGen->updateAttributes($value);
328:
329:
330: $oMetaTagGen->removeAttribute('id');
331:
332:
333: $sPattern = '/(<meta(?:\s+)' . $nameKey . '(?:\s*)=(?:\s*)(?:\\"|\\\')(?:\s*)' . $value[$nameKey] . '(?:\s*)(?:\\"|\\\')(?:[^>]+)>\n?)/i';
334: if (preg_match($sPattern, $this->_layoutCode, $aMatch)) {
335: $this->_layoutCode = str_replace($aMatch[1], $oMetaTagGen->render() . "\n", $this->_layoutCode);
336: } else {
337: $sMetatags .= $oMetaTagGen->render() . "\n";
338: }
339: }
340:
341:
342: $this->_layoutCode = cString::iReplaceOnce('</head>', $sMetatags . '</head>', $this->_layoutCode);
343: }
344:
345: 346: 347: 348: 349: 350: 351: 352: 353: 354:
355: protected function _saveGeneratedCode($idcatart, $code = '', $flagCreateCode = true) {
356: global $cfgClient;
357:
358:
359:
360: if ($this->_layout == false && $this->_save == true) {
361: if (!is_dir($cfgClient[$this->_client]['code']['path'])) {
362: mkdir($cfgClient[$this->_client]['code']['path']);
363: @chmod($cfgClient[$this->_client]['code']['path'], 0777);
364: cFileHandler::write($cfgClient[$this->_client]['code']['path'] . '.htaccess', "Order Deny,Allow\nDeny from all\n");
365: }
366:
367: $fileCode = ($code == '') ? $this->_layoutCode : $code;
368:
369: $code = "<?php\ndefined('CON_FRAMEWORK') or die('Illegal call');\n\n?>\n" . $fileCode;
370: cFileHandler::write($cfgClient[$this->_client]['code']['path'] . $this->_client . '.' . $this->_lang . '.' . $idcatart . '.php', $code, false);
371:
372:
373: if ($flagCreateCode == true) {
374: $oCatArtColl = new cApiCategoryArticleCollection();
375: $oCatArtColl->setCreateCodeFlag($idcatart, 0);
376: }
377: }
378: }
379:
380: 381: 382: 383: 384: 385:
386: protected function _getBasicMetaTags() {
387:
388:
389: $metaTags = array();
390: foreach (conGetAvailableMetaTagTypes() as $key => $value) {
391: $metaValue = conGetMetaValue($this->_idartlang, $key);
392: if (0 < strlen($metaValue)) {
393: $metaTags[] = array(
394: $value['fieldname'] => $value['metatype'],
395: 'content' => $metaValue
396: );
397: }
398: }
399:
400:
401:
402:
403: $generator = 'CMS CONTENIDO';
404: $addVersion = true;
405: if ($addVersion) {
406: $cfg = cRegistry::getConfig();
407: $aVersion = explode('.', CON_VERSION);
408: $generator .= ' ' . $aVersion[0] . '.' . $aVersion[1];
409: }
410: $metaTags[] = array(
411: 'name' => 'generator',
412: 'content' => $generator
413: );
414:
415:
416: global $encoding;
417: if (getEffectiveSetting('generator', 'html5', 'false') == 'true') {
418: $metaTags[] = array(
419: 'charset' => $encoding[$this->_lang]
420: );
421: } elseif (getEffectiveSetting('generator', 'xhtml', 'false') == 'true') {
422: $metaTags[] = array(
423: 'http-equiv' => 'Content-Type',
424: 'content' => 'application/xhtml+xml; charset=' . $encoding[$this->_lang]
425: );
426: } else {
427: $metaTags[] = array(
428: 'http-equiv' => 'Content-Type',
429: 'content' => 'text/html; charset=' . $encoding[$this->_lang]
430: );
431: }
432:
433:
434:
435:
436:
437:
438: return $metaTags;
439: }
440:
441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452:
453: protected function _updateMetaRobots(array $metaTags, $index, $follow) {
454:
455:
456: list($metaTags, $metaRobots) = $this->_extractMetaElement($metaTags, 'name', 'robots');
457:
458: if (is_null($metaRobots)) {
459:
460: $metaRobots = array(
461: 'name' => 'robots',
462: 'content' => ''
463: );
464: } else {
465: $content = array_map('trim', explode(',', $metaRobots['content']));
466:
467: if (is_null($index)) {
468: $index = (bool) (in_array('all', $content) || in_array('index', $content));
469: if (in_array('index', $content) || in_array('all', $content)) {
470: $index = true;
471: } else if (in_array('noindex', $content)) {
472: $index = true;
473: } else {
474: $index = NULL;
475: }
476: }
477:
478: if (is_null($follow)) {
479: if (in_array('follow', $content) || in_array('all', $content)) {
480: $follow = true;
481: } else if (in_array('nofollow', $content)) {
482: $follow = true;
483: } else {
484: $follow = NULL;
485: }
486: }
487: }
488:
489:
490: $content = array();
491: if (true === $index && true === $follow) {
492: $content[] = 'all';
493: } else {
494: if (!is_null($index)) {
495: $content[] = $index? 'index' : 'noindex';
496: }
497: if (!is_null($follow)) {
498: $content[] = $follow? 'follow' : 'nofollow';
499: }
500: }
501: $metaRobots['content'] = implode(',', $content);
502:
503:
504: $metaTags[] = $metaRobots;
505:
506:
507: return $metaTags;
508: }
509:
510: 511: 512: 513: 514: 515: 516: 517: 518: 519: 520: 521: 522: 523:
524: protected function (array $metaTags, $type, $nameOrEquiv) {
525:
526:
527: $result = array(
528: array(),
529: NULL
530: );
531:
532:
533: foreach ($metaTags as $metaTag) {
534: if (!is_array($metaTag)) {
535:
536: continue;
537: } else if (!array_key_exists($type, $metaTag)) {
538:
539: array_push($result[0], $metaTag);
540: } else if ($metaTag[$type] !== $nameOrEquiv) {
541:
542: array_push($result[0], $metaTag);
543: } else {
544:
545: $result[1] = $metaTag;
546: }
547: }
548:
549:
550: return $result;
551: }
552: }
553: