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: $a_c = 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: $a_d = 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: tplPreparseLayout($idlay);
71: $tmp_returnstring = tplBrowseLayoutForContainers($idlay);
72: $a_container = explode('&', $tmp_returnstring);
73:
74: foreach ($a_container as $key => $value) {
75: if (!isset($a_d[$value]) || !is_numeric($a_d[$value])) {
76:
77:
78: $this->_resetModule();
79: $this->_processCmsContainer($value);
80: continue;
81: }
82:
83: $oModule = new cApiModule($a_d[$value]);
84: $module = $oModule->toArray();
85: if (false === $module) {
86: $module = array();
87: }
88:
89: $this->_resetModule();
90:
91: $this->_modulePrefix[] = '$cCurrentModule = ' . $a_d[$value] . ';';
92: $this->_modulePrefix[] = '$cCurrentContainer = ' . $value . ';';
93:
94: $moduleHandler = new cModuleHandler($a_d[$value]);
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($value, $a_c[$value]);
117:
118:
119: if ($containerCmsValues) {
120: $this->_modulePrefix[] = $containerCmsValues;
121: }
122:
123:
124: $this->_processFrontendDebug($value, $module);
125:
126:
127: $this->_processCmsContainer($value);
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: $cssFile = '<link rel="stylesheet" type="text/css" href="' . $myFileCss . '" />';
151: }
152: }
153:
154: $jsFile = '';
155: if (strlen($this->_jsData) > 0) {
156: if (($myFileJs = $moduleHandler->saveContentToFile($this->_tplName, 'js', $this->_jsData)) !== false) {
157: $jsFile = '<script src="' . $myFileJs . '" type="text/javascript"></script>';
158: }
159: }
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177: if (strpos($this->_layoutCode, '{CSS}') !== false) {
178: $this->_layoutCode = cString::iReplaceOnce('{CSS}', $cssFile, $this->_layoutCode);
179: } else if (!empty($cssFile)) {
180: if (strpos($this->_layoutCode, '</title>') !== false) {
181: $this->_layoutCode = cString::iReplaceOnce('</title>', '</title>' . $cssFile, $this->_layoutCode);
182: } else {
183: $this->_layoutCode = cString::iReplaceOnce('<head>', '<head>' . $cssFile, $this->_layoutCode);
184: }
185: }
186:
187:
188:
189: if (strpos($this->_layoutCode, '{JS}') !== false) {
190: $this->_layoutCode = cString::iReplaceOnce('{JS}', $jsFile, $this->_layoutCode);
191: } else if (!empty($jsFile)) {
192: $this->_layoutCode = cString::iReplaceOnce('</body>', $jsFile . '</body>', $this->_layoutCode);
193: }
194:
195:
196: $this->_saveGeneratedCode($idcatart);
197:
198: return $this->_layoutCode;
199: }
200:
201: 202: 203: 204: 205: 206: 207: 208: 209:
210: protected function _processNoConfigurationError($idcatart) {
211: cDebug::out('Neither CAT or ART are configured!<br><br>');
212:
213: $code = '<html><body>No code was created for this article in this category.</body><html>';
214: $this->_saveGeneratedCode($idcatart, $code, false);
215: }
216:
217: 218: 219: 220: 221: 222: 223:
224: protected function _processCodeTitleTag() {
225: if ($this->_pageTitle == '') {
226: cApiCecHook::setDefaultReturnValue($this->_pageTitle);
227: $this->_pageTitle = cApiCecHook::executeAndReturn('Contenido.Content.CreateTitletag');
228: }
229:
230:
231: if ($this->_pageTitle != '') {
232: $this->_layoutCode = preg_replace('/<title>.*?<\/title>/is', '{TITLE}', $this->_layoutCode, 1);
233: if (strstr($this->_layoutCode, '{TITLE}')) {
234: $this->_layoutCode = str_ireplace('{TITLE}', '<title>' . $this->_pageTitle . '</title>', $this->_layoutCode);
235: } else {
236: $this->_layoutCode = cString::iReplaceOnce('</head>', '<title>' . $this->_pageTitle . "</title>\n</head>", $this->_layoutCode);
237: }
238: } else {
239: $this->_layoutCode = str_replace('<title></title>', '', $this->_layoutCode);
240: }
241:
242: return $this->_layoutCode;
243: }
244:
245: 246: 247: 248: 249: 250: 251: 252:
253: protected function _processCodeMetaTags() {
254: global $encoding;
255:
256:
257: $metaTags = $this->_getBasicMetaTags();
258:
259:
260: $_cecIterator = cRegistry::getCecRegistry()->getIterator('Contenido.Content.CreateMetatags');
261: if ($_cecIterator->count() > 0) {
262: while (false !== $chainEntry = $_cecIterator->next()) {
263: $metaTags = $chainEntry->execute($metaTags);
264: }
265: }
266:
267: $sMetatags = '';
268: foreach ($metaTags as $value) {
269:
270:
271:
272: if (isset($value['content'])) {
273: $value['content'] = conHtmlEntityDecode($value['content'], ENT_QUOTES, strtoupper($encoding[$this->_lang]));
274: $value['content'] = htmlspecialchars_decode($value['content'], ENT_QUOTES);
275: }
276:
277:
278: $oMetaTagGen = new cHTML();
279: $oMetaTagGen->setTag('meta');
280: $oMetaTagGen->updateAttributes($value);
281:
282:
283: $oMetaTagGen->removeAttribute('id');
284:
285:
286: $sPattern = '/(<meta(?:\s+)name(?:\s*)=(?:\s*)(?:\\"|\\\')(?:\s*)' . $value['name'] . '(?:\s*)(?:\\"|\\\')(?:[^>]+)>\n?)/i';
287: if (preg_match($sPattern, $this->_layoutCode, $aMatch)) {
288: $this->_layoutCode = str_replace($aMatch[1], $oMetaTagGen->render() . "\n", $this->_layoutCode);
289: } else {
290: $sMetatags .= $oMetaTagGen->render() . "\n";
291: }
292: }
293:
294:
295: $this->_layoutCode = cString::iReplaceOnce('</head>', $sMetatags . '</head>', $this->_layoutCode);
296: }
297:
298: 299: 300: 301: 302: 303: 304: 305: 306: 307:
308: protected function _saveGeneratedCode($idcatart, $code = '', $flagCreateCode = true) {
309: global $cfgClient;
310:
311:
312:
313: if ($this->_layout == false && $this->_save == true) {
314: if (!is_dir($cfgClient[$this->_client]['code']['path'])) {
315: mkdir($cfgClient[$this->_client]['code']['path']);
316: chmod($cfgClient[$this->_client]['code']['path'], 0777);
317: cFileHandler::write($cfgClient[$this->_client]['code']['path'] . '.htaccess', "Order Deny,Allow\nDeny from all\n");
318: }
319:
320: $fileCode = ($code == '')? $this->_layoutCode : $code;
321:
322: $code = "<?php\ndefined('CON_FRAMEWORK') or die('Illegal call');\n\n?>\n" . $fileCode;
323: cFileHandler::write($cfgClient[$this->_client]['code']['path'] . $this->_client . '.' . $this->_lang . '.' . $idcatart . '.php', $code, false);
324:
325:
326: if ($flagCreateCode == true) {
327: $oCatArtColl = new cApiCategoryArticleCollection();
328: $oCatArtColl->setCreateCodeFlag($idcatart, 0);
329: }
330: }
331: }
332:
333: 334: 335: 336: 337: 338:
339: protected function _getBasicMetaTags() {
340:
341:
342: $metaTags = array();
343: foreach (conGetAvailableMetaTagTypes() as $key => $value) {
344: $metaValue = conGetMetaValue($this->_idartlang, $key);
345: if (0 < strlen($metaValue)) {
346: $metaTags[] = array(
347: $value['fieldname'] => $value['metatype'],
348: 'content' => $metaValue
349: );
350: }
351: }
352:
353:
354:
355:
356: $generator = 'CMS CONTENIDO';
357: $addVersion = true;
358: if ($addVersion) {
359: $cfg = cRegistry::getConfig();
360: $aVersion = explode('.', $cfg['version']);
361: $generator .= ' ' . $aVersion[0] . '.' . $aVersion[1];
362: }
363: $metaTags[] = array(
364: 'name' => 'generator',
365: 'content' => $generator
366: );
367:
368:
369: global $encoding;
370: if (getEffectiveSetting('generator', 'html5', 'false') == 'true') {
371: $metaTags[] = array(
372: 'charset' => $encoding[$this->_lang]
373: );
374: } elseif (getEffectiveSetting('generator', 'xhtml', 'false') == 'true') {
375: $metaTags[] = array(
376: 'http-equiv' => 'Content-Type',
377: 'content' => 'application/xhtml+xml; charset=' . $encoding[$this->_lang]
378: );
379: } else {
380: $metaTags[] = array(
381: 'http-equiv' => 'Content-Type',
382: 'content' => 'text/html; charset=' . $encoding[$this->_lang]
383: );
384: }
385:
386:
387:
388:
389:
390:
391: return $metaTags;
392: }
393:
394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405:
406: protected function _updateMetaRobots(array $metaTags, $index, $follow) {
407:
408:
409: list($metaTags, $metaRobots) = $this->_extractMetaElement($metaTags, 'name', 'robots');
410:
411: if (is_null($metaRobots)) {
412:
413: $metaRobots = array(
414: 'name' => 'robots',
415: 'content' => ''
416: );
417: } else {
418: $content = array_map('trim', explode(',', $metaRobots['content']));
419:
420: if (is_null($index)) {
421: $index = (bool) (in_array('all', $content) || in_array('index', $content));
422: if (in_array('index', $content) || in_array('all', $content)) {
423: $index = true;
424: } else if (in_array('noindex', $content)) {
425: $index = true;
426: } else {
427: $index = NULL;
428: }
429: }
430:
431: if (is_null($follow)) {
432: if (in_array('follow', $content) || in_array('all', $content)) {
433: $follow = true;
434: } else if (in_array('nofollow', $content)) {
435: $follow = true;
436: } else {
437: $follow = NULL;
438: }
439: }
440: }
441:
442:
443: $content = array();
444: if (true === $index && true === $follow) {
445: $content[] = 'all';
446: } else {
447: if (!is_null($index)) {
448: $content[] = $index? 'index' : 'noindex';
449: }
450: if (!is_null($follow)) {
451: $content[] = $follow? 'follow' : 'nofollow';
452: }
453: }
454: $metaRobots['content'] = implode(',', $content);
455:
456:
457: $metaTags[] = $metaRobots;
458:
459:
460: return $metaTags;
461: }
462:
463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476:
477: protected function (array $metaTags, $type, $nameOrEquiv) {
478:
479:
480: $result = array(
481: array(),
482: NULL
483: );
484:
485:
486: foreach ($metaTags as $metaTag) {
487: if (!is_array($metaTag)) {
488:
489: continue;
490: } else if (!array_key_exists($type, $metaTag)) {
491:
492: array_push($result[0], $metaTag);
493: } else if ($metaTag[$type] !== $nameOrEquiv) {
494:
495: array_push($result[0], $metaTag);
496: } else {
497:
498: $result[1] = $metaTag;
499: }
500: }
501:
502:
503: return $result;
504: }
505: }
506: