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