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