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
    • NavigationMain
    • 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:         $a_c = conGetContainerConfiguration($this->_idtplcfg);
 50: 
 51:         // Set idlay and idmod array
 52:         $data = $this->_getTemplateData();
 53:         $idlay = $data['idlay'];
 54:         $idtpl = $data['idtpl'];
 55:         $this->_tplName = cApiStrCleanURLCharacters($data['name']);
 56: 
 57:         // List of used modules
 58:         $a_d = conGetUsedModules($idtpl);
 59: 
 60:         // Load layout code from file
 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:         // Create code for all containers
 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:                     // No configured module in this container, reset current module
 77:                     // state and process empty container
 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:                 // 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($value, $a_c[$value]);
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($value, $module);
125: 
126:                 // Replace new containers
127:                 $this->_processCmsContainer($value);
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:                 $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:         // // show toolbar with revisions of article This toolbar is only shown
162:         // in
163:         // // backend in edit mode if revision control is enabled
164:         // $revionsToolBar = $this->_revisionControl->getToolbar();
165:         // if (NULL !== $revionsToolBar) {
166:         // if (false !== strpos($this->_layoutCode, '<body>')) {
167:         // $this->_layoutCode = str_ireplace_once('<body>', '<body>' .
168:         // $revionsToolBar, $this->_layoutCode);
169:         // } else {
170:         // // TODO wo füg ich die dann ein?
171:         // // $this->_layoutCode .= $revionsToolBar;
172:         // }
173:         // }
174: 
175:         // add module CSS at {CSS} position, after title or after opening head
176:         // tag
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:         // add module JS at {JS} position or before closing body tag if there is
188:         // no {JS}
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:         // Save the generated code
196:         $this->_saveGeneratedCode($idcatart);
197: 
198:         return $this->_layoutCode;
199:     }
200: 
201:     /**
202:      * Will be invoked, if code generation wasn't able to find a configured
203:      * article
204:      * or category.
205:      *
206:      * Creates a error message and writes this into the code cache.
207:      *
208:      * @param int $idcatart Category article id
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:      * Processes and adds or replaces title tag for an article.
219:      *
220:      * Calls also the CEC 'Contenido.Content.CreateTitletag' for user defined
221:      * title
222:      * creation.
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:         // Add or replace title
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:      * Processes and adds or replaces all meta tags for an article.
247:      *
248:      * Calls also the CEC 'Contenido.Content.CreateMetatags' for user defined
249:      * meta tags creation.
250:      *
251:      * @global array $encoding
252:      */
253:     protected function _processCodeMetaTags() {
254:         global $encoding;
255: 
256:         // get basic meta tags (from article & system)
257:         $metaTags = $this->_getBasicMetaTags();
258: 
259:         // process chain Contenido.Content.CreateMetatags to update meta tags
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:             // decode entities and htmlspecialchars, content will be converted
271:             // later using conHtmlSpecialChars() by render() function
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:             // build up metatag string
278:             $oMetaTagGen = new cHTML();
279:             $oMetaTagGen->setTag('meta');
280:             $oMetaTagGen->updateAttributes($value);
281: 
282:             // HTML does not allow ID for meta tags
283:             $oMetaTagGen->removeAttribute('id');
284: 
285:             // check if metatag already exists
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:         // add meta tags
295:         $this->_layoutCode = cString::iReplaceOnce('</head>', $sMetatags . '</head>', $this->_layoutCode);
296:     }
297: 
298:     /**
299:      * Saves the generated code (if layout flag is false and save flag is true)
300:      *
301:      * @global array $cfgClient
302:      * @param int $idcatart Category article id
303:      * @param string $code parameter for setting code manually instead of using
304:      *        the generated layout code
305:      * @param bool $flagCreateCode whether the create code flag in cat_art
306:      *        should be set or not (optional)
307:      */
308:     protected function _saveGeneratedCode($idcatart, $code = '', $flagCreateCode = true) {
309:         global $cfgClient;
310: 
311:         // Write code in the cache of the client. If the folder does not exist
312:         // create one.
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:             // Update create code flag
326:             if ($flagCreateCode == true) {
327:                 $oCatArtColl = new cApiCategoryArticleCollection();
328:                 $oCatArtColl->setCreateCodeFlag($idcatart, 0);
329:             }
330:         }
331:     }
332: 
333:     /**
334:      * Collects and return basic meta tags/elements.
335:      *
336:      * @global array $encoding
337:      * @return array List of assozative meta tag values
338:      */
339:     protected function _getBasicMetaTags() {
340: 
341:         // collect all available meta tag entries with non empty values
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:         // add generator meta tag
354:         // if the version is appended should be configurable due to security
355:         // reasons
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:         // add charset or content type meta tag
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:         // update (!) index setting of robots meta tag
387:         // the follow value will not be changed
388:         //$index = (bool) $this->getArtLangObject()->get('searchable');
389:         //$metaTags = $this->_updateMetaRobots($metaTags, $index, NULL);
390: 
391:         return $metaTags;
392:     }
393: 
394:     /**
395:      * This method allows to set new values for the robots meta element.
396:      *
397:      * If NULL is given for $index or $follow, existing settings are *not*
398:      * overwritten. If article should be indexed and followed, 'all' will be
399:      * set.
400:      *
401:      * @param array $metaTags array of meta elements to amend
402:      * @param bool|NULL $index if article should be indexed
403:      * @param bool|NULL $follow if links in article should be followed
404:      * @return multitype:multitype: Ambigous <string, unknown>
405:      */
406:     protected function _updateMetaRobots(array $metaTags, $index, $follow) {
407: 
408:         // extract robots setting from current meta elements
409:         list($metaTags, $metaRobots) = $this->_extractMetaElement($metaTags, 'name', 'robots');
410: 
411:         if (is_null($metaRobots)) {
412:             // build new meta element if none could be found
413:             $metaRobots = array(
414:                 'name' => 'robots',
415:                 'content' => ''
416:             );
417:         } else {
418:             $content = array_map('trim', explode(',', $metaRobots['content']));
419:             // determine index from extracted element if given value is NULL
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:             // determine follow from extracted element if given value is NULL
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:         // build and set new content for robots element
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:         // add robots meta element
457:         $metaTags[] = $metaRobots;
458: 
459:         // what do you expect?
460:         return $metaTags;
461:     }
462: 
463:     /**
464:      * Extracts a meta element of type $type (either 'name' or 'http-equiv') and
465:      * name or HTTP header equivalent $nameOrEquiv from the given array of meta
466:      * elements.
467:      *
468:      * Both, the reduced array of meta elements and the meta element to be
469:      * extracted are returned as an array. If the meta element to be extracted
470:      * could ot be found, NULL will be returned in its place.
471:      *
472:      * @param array $metaTags
473:      * @param string $type either 'name' or 'http-equiv'
474:      * @param string $nameOrEquiv
475:      * @return multitype:multitype: Ambigous <string, unknown>
476:      */
477:     protected function _extractMetaElement(array $metaTags, $type, $nameOrEquiv) {
478: 
479:         // prepare result structure
480:         $result = array(
481:             array(),
482:             NULL
483:         );
484: 
485:         // loop all given meta elements
486:         foreach ($metaTags as $metaTag) {
487:             if (!is_array($metaTag)) {
488:                 // skip $metaTag if it's no array
489:                 continue;
490:             } else if (!array_key_exists($type, $metaTag)) {
491:                 // add element to reduced array if it's of different type
492:                 array_push($result[0], $metaTag);
493:             } else if ($metaTag[$type] !== $nameOrEquiv) {
494:                 // add element to reduced array if it has different name
495:                 array_push($result[0], $metaTag);
496:             } else {
497:                 // set element as extracted element
498:                 $result[1] = $metaTag;
499:             }
500:         }
501: 
502:         // what do you expect?
503:         return $result;
504:     }
505: }
506: 
CMS CONTENIDO 4.9.0 API documentation generated by ApiGen 2.8.0