Overview

Packages

  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Datatype
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob

Classes

  • cCodeGeneratorAbstract
  • cCodeGeneratorFactory
  • cCodeGeneratorStandard
  • cContentTypeAbstract
  • cContentTypeAbstractTabbed
  • cContentTypeDate
  • cContentTypeFilelist
  • cContentTypeHead
  • cContentTypeHtml
  • cContentTypeHtmlhead
  • cContentTypeImg
  • cContentTypeImgdescr
  • cContentTypeImgeditor
  • cContentTypeLink
  • cContentTypeLinkdescr
  • cContentTypeLinkeditor
  • cContentTypeLinktarget
  • cContentTypeTeaser
  • cContentTypeText
  • cTypeGenerator
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: 
  3: /**
  4:  * CONTENIDO standard code generator
  5:  *
  6:  * @package Core
  7:  * @subpackage ContentType
  8:  * @version SVN Revision $Rev:$
  9:  *
 10:  * @author Murat Purc <murat@purc.de>
 11:  * @copyright four for business AG <www.4fb.de>
 12:  * @license http://www.contenido.org/license/LIZENZ.txt
 13:  * @link http://www.4fb.de
 14:  * @link http://www.contenido.org
 15:  */
 16: 
 17: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 18: 
 19: /**
 20:  * CONTENIDO standard code generator.
 21:  *
 22:  * @package Core
 23:  * @subpackage ContentType
 24:  */
 25: class cCodeGeneratorStandard extends cCodeGeneratorAbstract {
 26: 
 27:     /**
 28:      * {@inheritdoc}
 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:         // Set category article id
 40:         $idcatart = conGetCategoryArticleId($this->_idcat, $this->_idart);
 41: 
 42:         // Set configuration for article
 43:         $this->_idtplcfg = $this->_getTemplateConfigurationId();
 44:         if (NULL === $this->_idtplcfg) {
 45:             $this->_processNoConfigurationError($idcatart);
 46:             return '0601';
 47:         }
 48: 
 49:         // List of configured container
 50:         $containerConfigurations = conGetContainerConfiguration($this->_idtplcfg);
 51: 
 52:         // Set idlay and idmod array
 53:         $data = $this->_getTemplateData();
 54:         $idlay = $data['idlay'];
 55:         $idtpl = $data['idtpl'];
 56:         $this->_tplName = cApiStrCleanURLCharacters($data['name']);
 57: 
 58:         // List of used modules
 59:         $containerModules = conGetUsedModules($idtpl);
 60: 
 61:         // Load layout code from file
 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:         // Create code for all containers
 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:                     // No configured module in this container
 76:                     // reset current module state and process empty container
 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:                 // Get the contents of input and output from files and not from
 98:                 // db-table
 99:                 if ($moduleHandler->modulePathExists() == true) {
100:                     $this->_moduleCode = $moduleHandler->readOutput();
101:                     // Load css and js content of the js/css files
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:                 // Process CMS value tags
116:                 $containerCmsValues = $this->_processCmsValueTags($containerNr, $containerConfigurations[$containerNr]);
117: 
118:                 // Add CMS value code to module prefix code
119:                 if ($containerCmsValues) {
120:                     $this->_modulePrefix[] = $containerCmsValues;
121:                 }
122: 
123:                 // Process frontend debug
124:                 $this->_processFrontendDebug($containerNr, $module);
125: 
126:                 // Replace new containers
127:                 $this->_processCmsContainer($containerNr);
128:             }
129:         }
130: 
131:         // Find out what kind of CMS_... Vars are in use
132:         $a_content = $this->_getUsedCmsTypesData();
133: 
134:         // Replace all CMS_TAGS[]
135:         if ($contype) {
136:             $this->_processCmsTags($a_content, true);
137:         }
138: 
139:         // Add/replace title tag
140:         $this->_processCodeTitleTag();
141: 
142:         // Add/replace meta tags
143:         $this->_processCodeMetaTags();
144: 
145:         // Save the collected css/js data and save it under the template name
146:         // ([templatename].css , [templatename].js in cache dir
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:         // // show toolbar with revisions of article This toolbar is only shown
164:         // in
165:         // // backend in edit mode if revision control is enabled
166:         // $revionsToolBar = $this->_revisionControl->getToolbar();
167:         // if (NULL !== $revionsToolBar) {
168:         // if (false !== strpos($this->_layoutCode, '<body>')) {
169:         // $this->_layoutCode = str_ireplace_once('<body>', '<body>' .
170:         // $revionsToolBar, $this->_layoutCode);
171:         // } else {
172:         // // TODO wo füg ich die dann ein?
173:         // // $this->_layoutCode .= $revionsToolBar;
174:         // }
175:         // }
176: 
177:         // add module CSS at {CSS} position, after title or after opening head
178:         // tag
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:         // add module JS at {JS} position or before closing body tag if there is
190:         // no {JS}
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:         // Save the generated code
233:         $this->_saveGeneratedCode($idcatart);
234: 
235:         return $this->_layoutCode;
236:     }
237: 
238:     /**
239:      * Will be invoked, if code generation wasn't able to find a configured
240:      * article
241:      * or category.
242:      *
243:      * Creates a error message and writes this into the code cache.
244:      *
245:      * @param int $idcatart Category article id
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:      * Processes and adds or replaces title tag for an article.
256:      * Calls also the CEC 'Contenido.Content.CreateTitletag' for user defined
257:      * title creation.
258:      *
259:      * @see cCodeGeneratorAbstract::_processCodeTitleTag()
260:      * @return string
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:         // Add or replace title
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:      * Processes and adds or replaces all meta tags for an article.
285:      *
286:      * Calls also the CEC 'Contenido.Content.CreateMetatags' for user defined
287:      * meta tags creation.
288:      *
289:      * @global array $encoding
290:      */
291:     protected function _processCodeMetaTags() {
292:         global $encoding;
293: 
294:         // get basic meta tags (from article & system)
295:         $metaTags = $this->_getBasicMetaTags();
296: 
297:         // process chain Contenido.Content.CreateMetatags to update meta tags
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:             // get meta tag keys
309:             $valueKeys = array_keys($value);
310:             $nameKey = 'name';
311:             foreach ($valueKeys as $key) {
312: 
313:                 if ($key != 'content')
314:                     $nameKey = $key;
315:             }
316: 
317:             // decode entities and htmlspecialchars, content will be converted
318:             // later using conHtmlSpecialChars() by render() function
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:             // build up metatag string
325:             $oMetaTagGen = new cHTML();
326:             $oMetaTagGen->setTag('meta');
327:             $oMetaTagGen->updateAttributes($value);
328: 
329:             // HTML does not allow ID for meta tags
330:             $oMetaTagGen->removeAttribute('id');
331: 
332:             // check if metatag already exists
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:         // add meta tags
342:         $this->_layoutCode = cString::iReplaceOnce('</head>', $sMetatags . '</head>', $this->_layoutCode);
343:     }
344: 
345:     /**
346:      * Saves the generated code (if layout flag is false and save flag is true)
347:      *
348:      * @global array $cfgClient
349:      * @param int $idcatart Category article id
350:      * @param string $code parameter for setting code manually instead of using
351:      *        the generated layout code
352:      * @param bool $flagCreateCode whether the create code flag in cat_art
353:      *        should be set or not (optional)
354:      */
355:     protected function _saveGeneratedCode($idcatart, $code = '', $flagCreateCode = true) {
356:         global $cfgClient;
357: 
358:         // Write code in the cache of the client. If the folder does not exist
359:         // create one.
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:             // Update create code flag
373:             if ($flagCreateCode == true) {
374:                 $oCatArtColl = new cApiCategoryArticleCollection();
375:                 $oCatArtColl->setCreateCodeFlag($idcatart, 0);
376:             }
377:         }
378:     }
379: 
380:     /**
381:      * Collects and return basic meta tags/elements.
382:      *
383:      * @global array $encoding
384:      * @return array List of assozative meta tag values
385:      */
386:     protected function _getBasicMetaTags() {
387: 
388:         // collect all available meta tag entries with non empty values
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:         // add generator meta tag
401:         // if the version is appended should be configurable due to security
402:         // reasons
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:         // add charset or content type meta tag
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:         // update (!) index setting of robots meta tag
434:         // the follow value will not be changed
435:         // $index = (bool) $this->getArtLangObject()->get('searchable');
436:         // $metaTags = $this->_updateMetaRobots($metaTags, $index, NULL);
437: 
438:         return $metaTags;
439:     }
440: 
441:     /**
442:      * This method allows to set new values for the robots meta element.
443:      *
444:      * If NULL is given for $index or $follow, existing settings are *not*
445:      * overwritten. If article should be indexed and followed, 'all' will be
446:      * set.
447:      *
448:      * @param array $metaTags array of meta elements to amend
449:      * @param bool|NULL $index if article should be indexed
450:      * @param bool|NULL $follow if links in article should be followed
451:      * @return array
452:      */
453:     protected function _updateMetaRobots(array $metaTags, $index, $follow) {
454: 
455:         // extract robots setting from current meta elements
456:         list($metaTags, $metaRobots) = $this->_extractMetaElement($metaTags, 'name', 'robots');
457: 
458:         if (is_null($metaRobots)) {
459:             // build new meta element if none could be found
460:             $metaRobots = array(
461:                 'name' => 'robots',
462:                 'content' => ''
463:             );
464:         } else {
465:             $content = array_map('trim', explode(',', $metaRobots['content']));
466:             // determine index from extracted element if given value is NULL
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:             // determine follow from extracted element if given value is NULL
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:         // build and set new content for robots element
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:         // add robots meta element
504:         $metaTags[] = $metaRobots;
505: 
506:         // what do you expect?
507:         return $metaTags;
508:     }
509: 
510:     /**
511:      * Extracts a meta element of type $type (either 'name' or 'http-equiv') and
512:      * name or HTTP header equivalent $nameOrEquiv from the given array of meta
513:      * elements.
514:      *
515:      * Both, the reduced array of meta elements and the meta element to be
516:      * extracted are returned as an array. If the meta element to be extracted
517:      * could not be found, NULL will be returned in its place.
518:      *
519:      * @param array $metaTags
520:      * @param string $type either 'name' or 'http-equiv'
521:      * @param string $nameOrEquiv
522:      * @return array
523:      */
524:     protected function _extractMetaElement(array $metaTags, $type, $nameOrEquiv) {
525: 
526:         // prepare result structure
527:         $result = array(
528:             array(),
529:             NULL
530:         );
531: 
532:         // loop all given meta elements
533:         foreach ($metaTags as $metaTag) {
534:             if (!is_array($metaTag)) {
535:                 // skip $metaTag if it's no array
536:                 continue;
537:             } else if (!array_key_exists($type, $metaTag)) {
538:                 // add element to reduced array if it's of different type
539:                 array_push($result[0], $metaTag);
540:             } else if ($metaTag[$type] !== $nameOrEquiv) {
541:                 // add element to reduced array if it has different name
542:                 array_push($result[0], $metaTag);
543:             } else {
544:                 // set element as extracted element
545:                 $result[1] = $metaTag;
546:             }
547:         }
548: 
549:         // what do you expect?
550:         return $result;
551:     }
552: }
553: 
CMS CONTENIDO 4.9.3 API documentation generated by ApiGen 2.8.0