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:
195:
196: if (strpos($this->_layoutCode, '{JS}') !== false) {
197: $this->_layoutCode = cString::iReplaceOnce('{JS}', $jsFile, $this->_layoutCode);
198: } else if (!empty($jsFile)) {
199: $this->_layoutCode = cString::iReplaceOnce('</body>', $jsFile . '</body>', $this->_layoutCode);
200: }
201:
202: if ($this->_feDebugOptions['general_information']) {
203: $debugPrefix = '';
204:
205: $debugPrefix .= "<?php\nif (\$frontend_debug['general_information']) {\n";
206: $debugPrefix .= "\techo(\"<!-- \\n\\n\");\n";
207:
208: $layout = new cApiLayout($idlay);
209: $layouName = $layout->get('name');
210: $debugPrefix .= "\techo(\"Layout: " . $layouName . " (" . $idlay . ")\\n\");\n";
211:
212: $debugPrefix .= "\techo(\"Template: " . $this->_tplName . " (" . $idtpl . ")\\n\");\n";
213:
214: $article = new cApiArticleLanguage($this->_idartlang);
215: $catart = new cApiCategoryArticle();
216: $cat = new cApiCategoryLanguage();
217: $cat->loadByCategoryIdAndLanguageId($this->_idcat, $article->get('idlang'));
218: $catart->loadByMany(array(
219: 'idcat' => $cat->get('idcat'),
220: 'idart' => $article->get('idart')
221: ));
222: $lang = new cApiLanguage($article->get('idlang'));
223: $debugPrefix .= "\techo(\"Language: " . $lang->get('idlang') . " (" . $lang->get('name') . ")\\n\");\n";
224:
225: $debugPrefix .= "\techo(\"Category: " . $cat->get('idcat') . " (" . $cat->get('name') . ")\\n\");\n";
226:
227: $articleName = $article->get('title');
228: $debugPrefix .= "\techo(\"Article: " . $articleName . " (catart = " . $catart->get('idcatart') . ", artlang = " . $this->_idartlang . ", art = " . $article->get('idart') . ")\\n\");\n";
229:
230: $debugPrefix .= "\techo(\"\\n--> \\n\");\n";
231: $debugPrefix .= "}\n?>";
232:
233: $this->_layoutCode = $debugPrefix . $this->_layoutCode;
234: }
235:
236:
237: $this->_saveGeneratedCode($idcatart);
238:
239: return $this->_layoutCode;
240: }
241:
242: 243: 244: 245: 246: 247: 248: 249: 250:
251: protected function _processNoConfigurationError($idcatart) {
252: cDebug::out('Neither CAT or ART are configured!<br><br>');
253:
254: $code = '<html><body>No code was created for this article in this category.</body><html>';
255: $this->_saveGeneratedCode($idcatart, $code, false);
256: }
257:
258: 259: 260: 261: 262: 263: 264: 265:
266: protected function _processCodeTitleTag() {
267: if ($this->_pageTitle == '') {
268: cApiCecHook::setDefaultReturnValue($this->_pageTitle);
269: $this->_pageTitle = cApiCecHook::executeAndReturn('Contenido.Content.CreateTitletag');
270: }
271:
272:
273: if ($this->_pageTitle != '') {
274: $replaceTag = '{__TITLE__' . md5(rand().time()) . '}';
275: $this->_layoutCode = preg_replace('/<title>.*?<\/title>/is', $replaceTag, $this->_layoutCode, 1);
276: if (strstr($this->_layoutCode, $replaceTag)) {
277: $this->_layoutCode = str_ireplace($replaceTag, '<title>' . $this->_pageTitle . '</title>', $this->_layoutCode);
278: } else {
279: $this->_layoutCode = cString::iReplaceOnce('</head>', '<title>' . $this->_pageTitle . "</title>\n</head>", $this->_layoutCode);
280: }
281: } else {
282: $this->_layoutCode = str_replace('<title></title>', '', $this->_layoutCode);
283: }
284:
285: return $this->_layoutCode;
286: }
287:
288: 289: 290: 291: 292: 293: 294: 295:
296: protected function _processCodeMetaTags() {
297: global $encoding;
298:
299:
300: $metaTags = $this->_getBasicMetaTags();
301:
302:
303: $_cecIterator = cRegistry::getCecRegistry()->getIterator('Contenido.Content.CreateMetatags');
304: if ($_cecIterator->count() > 0) {
305: while (false !== $chainEntry = $_cecIterator->next()) {
306: $metaTags = $chainEntry->execute($metaTags);
307: }
308: }
309:
310: $sMetatags = '';
311:
312: foreach ($metaTags as $value) {
313:
314:
315: $valueKeys = array_keys($value);
316: $nameKey = 'name';
317: foreach ($valueKeys as $key) {
318:
319: if ($key != 'content')
320: $nameKey = $key;
321: }
322:
323:
324:
325: if (isset($value['content'])) {
326: $value['content'] = str_replace('"', '\"', (conHtmlEntityDecode(stripslashes($value['content']))));
327: }
328:
329:
330: $oMetaTagGen = new cHTML();
331: $oMetaTagGen->setTag('meta');
332: $oMetaTagGen->updateAttributes($value);
333:
334:
335: $oMetaTagGen->removeAttribute('id');
336:
337:
338: $sPattern = '/(<meta(?:\s+)' . $nameKey . '(?:\s*)=(?:\s*)(?:\\"|\\\')(?:\s*)' . $value[$nameKey] . '(?:\s*)(?:\\"|\\\')(?:[^>]+)>\n?)/i';
339: if (preg_match($sPattern, $this->_layoutCode, $aMatch)) {
340:
341:
342: $replace = true;
343: foreach ($value as $test) {
344: if ($test == '') {
345: $replace = false;
346: break;
347: }
348: }
349: if ($replace) {
350: $this->_layoutCode = str_replace($aMatch[1], $oMetaTagGen->render() . "\n", $this->_layoutCode);
351: }
352: } else {
353: $sMetatags .= $oMetaTagGen->render() . "\n";
354: }
355: }
356:
357:
358: $this->_layoutCode = cString::iReplaceOnce('</head>', $sMetatags . '</head>', $this->_layoutCode);
359: }
360:
361: 362: 363: 364: 365: 366: 367: 368: 369: 370:
371: protected function _saveGeneratedCode($idcatart, $code = '', $flagCreateCode = true) {
372: global $cfgClient;
373:
374:
375:
376: if ($this->_layout == false && $this->_save == true) {
377: if (!is_dir($cfgClient[$this->_client]['code']['path'])) {
378: mkdir($cfgClient[$this->_client]['code']['path']);
379: @chmod($cfgClient[$this->_client]['code']['path'], 0777);
380: cFileHandler::write($cfgClient[$this->_client]['code']['path'] . '.htaccess', "Order Deny,Allow\nDeny from all\n");
381: }
382:
383: $fileCode = ($code == '')? $this->_layoutCode : $code;
384:
385: $code = "<?php\ndefined('CON_FRAMEWORK') or die('Illegal call');\n\n?>\n" . $fileCode;
386: cFileHandler::write($cfgClient[$this->_client]['code']['path'] . $this->_client . '.' . $this->_lang . '.' . $idcatart . '.php', $code, false);
387:
388:
389: if ($flagCreateCode == true) {
390: $oCatArtColl = new cApiCategoryArticleCollection();
391: $oCatArtColl->setCreateCodeFlag($idcatart, 0);
392: }
393: }
394: }
395:
396: 397: 398: 399: 400: 401:
402: protected function _getBasicMetaTags() {
403:
404:
405: $metaTags = array();
406: foreach (conGetAvailableMetaTagTypes() as $key => $value) {
407: $metaValue = conGetMetaValue($this->_idartlang, $key);
408: if (0 < strlen($metaValue)) {
409: $metaTags[] = array(
410: $value['fieldname'] => $value['metatype'],
411: 'content' => $metaValue
412: );
413: }
414: }
415:
416:
417:
418:
419: $generator = 'CMS CONTENIDO';
420: $addVersion = true;
421: if ($addVersion) {
422: $cfg = cRegistry::getConfig();
423: $aVersion = explode('.', CON_VERSION);
424: $generator .= ' ' . $aVersion[0] . '.' . $aVersion[1];
425: }
426: $metaTags[] = array(
427: 'name' => 'generator',
428: 'content' => $generator
429: );
430:
431:
432: global $encoding;
433: if (getEffectiveSetting('generator', 'html5', 'false') == 'true') {
434: $metaTags[] = array(
435: 'charset' => $encoding[$this->_lang]
436: );
437: } elseif (getEffectiveSetting('generator', 'xhtml', 'false') == 'true') {
438: $metaTags[] = array(
439: 'http-equiv' => 'Content-Type',
440: 'content' => 'application/xhtml+xml; charset=' . $encoding[$this->_lang]
441: );
442: } else {
443: $metaTags[] = array(
444: 'http-equiv' => 'Content-Type',
445: 'content' => 'text/html; charset=' . $encoding[$this->_lang]
446: );
447: }
448:
449:
450:
451:
452:
453:
454: return $metaTags;
455: }
456:
457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468:
469: protected function _updateMetaRobots(array $metaTags, $index, $follow) {
470:
471:
472: list($metaTags, $metaRobots) = $this->_extractMetaElement($metaTags, 'name', 'robots');
473:
474: if (is_null($metaRobots)) {
475:
476: $metaRobots = array(
477: 'name' => 'robots',
478: 'content' => ''
479: );
480: } else {
481: $content = array_map('trim', explode(',', $metaRobots['content']));
482:
483: if (is_null($index)) {
484: $index = (bool) (in_array('all', $content) || in_array('index', $content));
485: if (in_array('index', $content) || in_array('all', $content)) {
486: $index = true;
487: } else if (in_array('noindex', $content)) {
488: $index = true;
489: } else {
490: $index = NULL;
491: }
492: }
493:
494: if (is_null($follow)) {
495: if (in_array('follow', $content) || in_array('all', $content)) {
496: $follow = true;
497: } else if (in_array('nofollow', $content)) {
498: $follow = true;
499: } else {
500: $follow = NULL;
501: }
502: }
503: }
504:
505:
506: $content = array();
507: if (true === $index && true === $follow) {
508: $content[] = 'all';
509: } else {
510: if (!is_null($index)) {
511: $content[] = $index? 'index' : 'noindex';
512: }
513: if (!is_null($follow)) {
514: $content[] = $follow? 'follow' : 'nofollow';
515: }
516: }
517: $metaRobots['content'] = implode(',', $content);
518:
519:
520: $metaTags[] = $metaRobots;
521:
522:
523: return $metaTags;
524: }
525:
526: 527: 528: 529: 530: 531: 532: 533: 534: 535: 536: 537: 538: 539:
540: protected function (array $metaTags, $type, $nameOrEquiv) {
541:
542:
543: $result = array(
544: array(),
545: NULL
546: );
547:
548:
549: foreach ($metaTags as $metaTag) {
550: if (!is_array($metaTag)) {
551:
552: continue;
553: } else if (!array_key_exists($type, $metaTag)) {
554:
555: array_push($result[0], $metaTag);
556: } else if ($metaTag[$type] !== $nameOrEquiv) {
557:
558: array_push($result[0], $metaTag);
559: } else {
560:
561: $result[1] = $metaTag;
562: }
563: }
564:
565:
566: return $result;
567: }
568:
569: }
570: