Overview

Packages

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

Classes

  • cUriBuilderMR
  • ModRewrite
  • ModRewrite_ContentController
  • ModRewrite_ContentExpertController
  • ModRewrite_ContentTestController
  • ModRewrite_ControllerAbstract
  • ModRewriteBase
  • ModRewriteController
  • ModRewriteDebugger
  • ModRewriteTest
  • ModRewriteUrlStack
  • ModRewriteUrlUtil

Functions

  • mr_arrayValue
  • mr_buildGeneratedCode
  • mr_buildNewUrl
  • mr_conCopyArtLang
  • mr_conMoveArticles
  • mr_conSaveArticle
  • mr_conSyncArticle
  • mr_debugOutput
  • mr_getConfiguration
  • mr_getConfigurationFilePath
  • mr_getRequest
  • mr_header
  • mr_i18n
  • mr_loadConfiguration
  • mr_queryAndNextRecord
  • mr_removeMultipleChars
  • mr_requestCleanup
  • mr_runFrontendController
  • mr_setClientLanguageId
  • mr_setConfiguration
  • mr_strCopyCategory
  • mr_strMovedownCategory
  • mr_strMoveSubtree
  • mr_strMoveUpCategory
  • mr_strNewCategory
  • mr_strNewTree
  • mr_strRenameCategory
  • mr_strSyncCategory
  • Overview
  • Package
  • Function
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: /**
  3:  * Mod Rewrite front_content.php controller. Does some preprocessing jobs, tries
  4:  * to set following variables, depending on mod rewrite configuration and if
  5:  * request part exists:
  6:  * - $client
  7:  * - $changeclient
  8:  * - $lang
  9:  * - $changelang
 10:  * - $idart
 11:  * - $idcat
 12:  *
 13:  * @package     Plugin
 14:  * @subpackage  ModRewrite
 15:  * @id          $Id$:
 16:  * @author      Stefan Seifarth / stese
 17:  * @author      Murat Purc <murat@purc.de>
 18:  * @copyright   www.polycoder.de
 19:  * @copyright   four for business AG <www.4fb.de>
 20:  * @license     http://www.contenido.org/license/LIZENZ.txt
 21:  * @link        http://www.4fb.de
 22:  * @link        http://www.contenido.org
 23:  */
 24: 
 25: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 26: 
 27: /**
 28:  * Processes mod_rewrite related job for created new tree.
 29:  *
 30:  * Will be called by chain 'Contenido.Action.str_newtree.AfterCall'.
 31:  *
 32:  * @param   array  $data  Assoziative array with some values
 33:  * @return  array  Passed parameter
 34:  */
 35: function mr_strNewTree(array $data) {
 36:     global $lang;
 37: 
 38:     ModRewriteDebugger::log($data, 'mr_strNewTree $data');
 39: 
 40:     if ((int) $data['newcategoryid'] > 0) {
 41:         $mrCatAlias = (trim($data['categoryalias']) !== '') ? trim($data['categoryalias']) : trim($data['categoryname']);
 42:         // set new urlname - because original set urlname isn''t validated for double entries in same parent category
 43:         ModRewrite::setCatWebsafeName($mrCatAlias, $data['newcategoryid'], $lang);
 44:         ModRewrite::setCatUrlPath($data['newcategoryid'], $lang);
 45:     }
 46: 
 47:     return $data;
 48: }
 49: 
 50: /**
 51:  * Processes mod_rewrite related job for created new category.
 52:  *
 53:  * Will be called by chain 'Contenido.Action.str_newcat.AfterCall'.
 54:  *
 55:  * @param   array  $data  Assoziative array with some values
 56:  * @return  array  Passed parameter
 57:  */
 58: function mr_strNewCategory(array $data) {
 59:     global $lang;
 60: 
 61:     ModRewriteDebugger::log($data, 'mr_strNewCategory $data');
 62: 
 63:     if ((int) $data['newcategoryid'] > 0) {
 64:         $mrCatAlias = (trim($data['categoryalias']) !== '') ? trim($data['categoryalias']) : trim($data['categoryname']);
 65:         // set new urlname - because original set urlname isn''t validated for double entries in same parent category
 66:         ModRewrite::setCatWebsafeName($mrCatAlias, $data['newcategoryid'], $lang);
 67:         ModRewrite::setCatUrlPath($data['newcategoryid'], $lang);
 68:     }
 69: 
 70:     return $data;
 71: }
 72: 
 73: /**
 74:  * Processes mod_rewrite related job for renamed category
 75:  * 2010-02-01: and now all existing subcategories and modify their paths too...
 76:  * 2010-02-01: max 50 recursion level
 77:  *
 78:  * Will be called by chain 'Contenido.Action.str_renamecat.AfterCall'.
 79:  *
 80:  * @param   array  $data  Assoziative array with some values
 81:  * @return  array  Passed parameter
 82:  */
 83: function mr_strRenameCategory(array $data) {
 84:     ModRewriteDebugger::log($data, 'mr_strRenameCategory $data');
 85: 
 86:     // hes 20100102
 87:     // maximal 50 recursion level
 88:     $recursion = (is_int($data['recursion'])) ? $data['recursion'] : 1;
 89:     if ($recursion > 50) {
 90:         exit("#20100201-1503: sorry - maximum function nesting level of " . $recursion . " reached");
 91:     }
 92: 
 93:     $mrCatAlias = (trim($data['newcategoryalias']) !== '') ? trim($data['newcategoryalias']) : trim($data['newcategoryname']);
 94:     if ($mrCatAlias != '') {
 95:         // set new urlname - because original set urlname isn''t validated for double entries in same parent category
 96:         ModRewrite::setCatWebsafeName($mrCatAlias, $data['idcat'], $data['lang']);
 97:         ModRewrite::setCatUrlPath($data['idcat'], $data['lang']);
 98:     }
 99: 
100:     // hes 20100102
101:     // now dive into all existing subcategories and modify their paths too...
102:     $str = 'parentid=' . $data['idcat'];
103:     $oCatColl = new cApiCategoryCollection($str);
104: 
105:     while ($oCat = $oCatColl->next()) {
106:         // hes 20100102
107:         $str = 'idcat=' . $oCat->get('idcat') . ' AND idlang=' . (int) $data['lang'];
108:         $oCatLanColl = new cApiCategoryLanguageCollection($str);
109:         if ($oCatLan = $oCatLanColl->next()) {
110:             // hes 20100102
111:             $childData = array(
112:                 'idcat' => $oCat->get('idcat'),
113:                 'lang' => (int) $data['lang'],
114:                 'newcategoryname' => $oCatLan->get('name'),
115:                 'newcategoryalias' => $oCatLan->get('urlname'),
116:                 'recursion' => $recursion + 1
117:             );
118: 
119:             $resData = mr_strRenameCategory($childData);
120:         }
121:     }
122: 
123:     return $data;
124: }
125: 
126: /**
127:  * Processes mod_rewrite related job after moving a category up.
128:  *
129:  * Will be called by chain 'Contenido.Action.str_moveupcat.AfterCall'.
130:  *
131:  * @todo  do we really need processing of the category? there is no mr relevant data
132:  *        changes while moving the category on same level, level and name won't change
133:  *
134:  * @param   int  $idcat  Category id
135:  * @return  int  Category id
136:  */
137: function mr_strMoveUpCategory($idcat) {
138:     ModRewriteDebugger::log($idcat, 'mr_strMoveUpCategory $idcat');
139: 
140:     // category check
141:     $cat = new cApiCategory((int) $idcat);
142:     if (!$cat->get('preid')) {
143:         return;
144:     }
145: 
146:     // get all cat languages
147:     $aIdLang = ModRewrite::getCatLanguages($idcat);
148: 
149:     // update ...
150:     foreach ($aIdLang as $iIdLang) {
151:         // get urlname
152:         $sCatname = ModRewrite::getCatName($idcat, $iIdLang);
153:         // set new urlname - because original set urlname isn't validated for double entries in same parent category
154:         ModRewrite::setCatWebsafeName($sCatname, $idcat, $iIdLang);
155:     }
156: 
157:     return $idcat;
158: }
159: 
160: /**
161:  * Processes mod_rewrite related job after moving a category down.
162:  *
163:  * Will be called by chain 'Contenido.Action.str_movedowncat.AfterCall'.
164:  *
165:  * @todo  do we really need processing of the category? there is no mr relevant data
166:  *        changes while moving the category on same level, level and name won't change
167:  *
168:  * @param   int  $idcat  Id of category beeing moved down
169:  * @return  int  Category id
170:  */
171: function mr_strMovedownCategory($idcat) {
172:     ModRewriteDebugger::log($idcat, 'mr_strMovedownCategory $idcat');
173: 
174:     // category check
175:     $cat = new cApiCategory((int) $idcat);
176:     if (!$cat->get('id')) {
177:         return;
178:     }
179: 
180:     // get all cat languages
181:     $aIdLang = ModRewrite::getCatLanguages($idcat);
182:     // update ...
183:     foreach ($aIdLang as $iIdLang) {
184:         // get urlname
185:         $sCatname = ModRewrite::getCatName($idcat, $iIdLang);
186:         // set new urlname - because original set urlname isn't validated for double entries in same parent category
187:         ModRewrite::setCatWebsafeName($sCatname, $idcat, $iIdLang);
188:     }
189: 
190:     return $idcat;
191: }
192: 
193: /**
194:  * Processes mod_rewrite related job after moving a category subtree.
195:  *
196:  * Will be called by chain 'Contenido.Action.str_movesubtree.AfterCall'.
197:  *
198:  * @param   array  $data  Assoziative array with some values
199:  * @return  array  Passed parameter
200:  */
201: function mr_strMoveSubtree(array $data) {
202:     ModRewriteDebugger::log($data, 'mr_strMoveSubtree $data');
203: 
204:     // category check
205:     if ((int) $data['idcat'] <= 0) {
206:         return;
207:     }
208: 
209:     // next category check
210:     $cat = new cApiCategory($data['idcat']);
211:     if (!$cat->get('idcat')) {
212:         return;
213:     }
214: 
215:     // get all cat languages
216:     $aIdLang = ModRewrite::getCatLanguages($data['idcat']);
217:     // update all languages
218:     foreach ($aIdLang as $iIdLang) {
219:         // get urlname
220:         $sCatname = ModRewrite::getCatName($data['idcat'], $iIdLang);
221:         // set new urlname - because original set urlname isn't validated for double entries in same parent category
222:         ModRewrite::setCatWebsafeName($sCatname, $data['idcat'], $iIdLang);
223:         ModRewrite::setCatUrlPath($data['idcat'], $iIdLang);
224:     }
225: 
226:     // now dive into all existing subcategories and modify their paths too...
227:     $oCatColl = new cApiCategoryCollection('parentid=' . $data['idcat']);
228:     while ($oCat = $oCatColl->next()) {
229:         mr_strMoveSubtree(array('idcat' => $oCat->get('idcat')));
230:     }
231: 
232:     return $data;
233: }
234: 
235: /**
236:  * Processes mod_rewrite related job after copying a category subtree.
237:  *
238:  * Will be called by chain 'Contenido.Category.strCopyCategory'.
239:  *
240:  * @param   array  $data  Assoziative array with some values
241:  * @return  array  Passed parameter
242:  */
243: function mr_strCopyCategory(array $data) {
244:     ModRewriteDebugger::log($data, 'mr_strCopyCategory $data');
245: 
246:     $idcat = (int) $data['newcat']->get('idcat');
247:     if ($idcat <= 0) {
248:         return $data;
249:     }
250: 
251:     // get all cat languages
252:     $aIdLang = ModRewrite::getCatLanguages($idcat);
253:     // update ...
254:     foreach ($aIdLang as $iIdLang) {
255:         // get urlname
256:         $sCatname = ModRewrite::getCatName($idcat, $iIdLang);
257:         // set new urlname - because original set urlname isn't validated for double entries in same parent category
258:         ModRewrite::setCatWebsafeName($sCatname, $idcat, $iIdLang);
259:         ModRewrite::setCatUrlPath($idcat, $iIdLang);
260:     }
261: }
262: 
263: /**
264:  * Processes mod_rewrite related job during structure synchronisation process,
265:  * sets the urlpath of current category.
266:  *
267:  * Will be called by chain 'Contenido.Category.strSyncCategory_Loop'.
268:  *
269:  * @param   array  $data  Assoziative array with some values
270:  * @return  array  Passed parameter
271:  */
272: function mr_strSyncCategory(array $data) {
273:     ModRewriteDebugger::log($data, 'mr_strSyncCategory $data');
274:     ModRewrite::setCatUrlPath($data['idcat'], $data['idlang']);
275:     return $data;
276: }
277: 
278: /**
279:  * Processes mod_rewrite related job for saved articles (new or modified article).
280:  *
281:  * Will be called by chain 'Contenido.Action.con_saveart.AfterCall'.
282:  *
283:  * @param   array  $data  Assoziative array with some article properties
284:  * @return  array  Passed parameter
285:  */
286: function mr_conSaveArticle(array $data) {
287:     global $tmp_firstedit, $client;
288: 
289:     ModRewriteDebugger::log($data, 'mr_conSaveArticle $data');
290: 
291:     if ((int) $data['idart'] == 0) {
292:         return $data;
293:     }
294: 
295:     if (cString::getStringLength(trim($data['urlname'])) == 0) {
296:         $data['urlname'] = $data['title'];
297:     }
298: 
299:     if (1 == $tmp_firstedit) {
300:         // new article
301:         $aLanguages = getLanguagesByClient($client);
302: 
303:         foreach ($aLanguages as $iLang) {
304:             ModRewrite::setArtWebsafeName($data['urlname'], $data['idart'], $iLang, $data['idcat']);
305:         }
306:     } else {
307:         // modified article
308:         $aArticle = ModRewrite::getArtIdByArtlangId($data['idartlang']);
309: 
310:         if (isset($aArticle['idart']) && isset($aArticle['idlang'])) {
311:             ModRewrite::setArtWebsafeName($data['urlname'], $aArticle['idart'], $aArticle['idlang'], $data['idcat']);
312:         }
313:     }
314: 
315:     return $data;
316: }
317: 
318: /**
319:  * Processes mod_rewrite related job for articles beeing moved.
320:  *
321:  * Will be called by chain 'Contenido.Article.conMoveArticles_Loop'.
322:  *
323:  * @param   array  $data  Assoziative array with record entries
324:  * @return  array  Loop through of arguments
325:  */
326: function mr_conMoveArticles($data) {
327:     ModRewriteDebugger::log($data, 'mr_conMoveArticles $data');
328: 
329:     // too defensive but secure way
330:     if (!is_array($data)) {
331:         return $data;
332:     } elseif (!isset($data['idartlang'])) {
333:         return $data;
334:     } elseif (!isset($data['idart'])) {
335:         return $data;
336:     }
337: 
338:     $arr_art = ModRewrite::getArtIds($data['idartlang']);
339:     if (count($arr_art) == 2) {
340:         ModRewrite::setArtWebsafeName($arr_art["urlname"], $data['idart'], $arr_art["idlang"]);
341:     }
342: 
343:     return $data;
344: }
345: 
346: /**
347:  * Processes mod_rewrite related job for duplicated articles.
348:  *
349:  * Will be called by chain 'Contenido.Article.conCopyArtLang_AfterInsert'.
350:  *
351:  * @param   array  $data  Assoziative array with record entries
352:  * @return  array  Loop through of arguments
353:  */
354: function mr_conCopyArtLang($data) {
355:     ModRewriteDebugger::log($data, 'mr_conCopyArtLang $data');
356: 
357:     // too defensive but secure way
358:     if (!is_array($data)) {
359:         return $data;
360:     } elseif (!isset($data['title'])) {
361:         return $data;
362:     } elseif (!isset($data['idart'])) {
363:         return $data;
364:     } elseif (!isset($data['idlang'])) {
365:         return $data;
366:     }
367: 
368:     ModRewrite::setArtWebsafeName($data['title'], $data['idart'], $data['idlang']);
369: 
370:     return $data;
371: }
372: 
373: /**
374:  * Processes mod_rewrite related job for synchronized articles.
375:  *
376:  * Will be called by chain 'Contenido.Article.conSyncArticle_AfterInsert'.
377:  *
378:  * @param   array  $data  Assoziative array with record entries as follows:
379:  * <code>
380:  * array(
381:  *     'src_art_lang'  => Recordset (assoziative array) of source item from con_art_lang table
382:  *     'dest_art_lang' => Recordset (assoziative array) of inserted destination item from con_art_lang table
383:  * );
384:  * </code>
385:  *
386:  * @return  array  Loop through of argument
387:  */
388: function mr_conSyncArticle($data) {
389:     ModRewriteDebugger::log($data, 'mr_conSyncArticle $data');
390: 
391:     // too defensive but secure way
392:     if (!is_array($data)) {
393:         return $data;
394:     } elseif (!isset($data['src_art_lang']) || !is_array($data['src_art_lang'])) {
395:         return $data;
396:     } elseif (!isset($data['dest_art_lang']) || !is_array($data['dest_art_lang'])) {
397:         return $data;
398:     } elseif (!isset($data['dest_art_lang']['idart'])) {
399:         return $data;
400:     } elseif (!isset($data['dest_art_lang']['idlang'])) {
401:         return $data;
402:     }
403: 
404:     if (!isset($data['src_art_lang']['urlname'])) {
405:         $artLang = new cApiArticleLanguage($data['src_art_lang']['idartlang']);
406:         $urlname = $artLang->get('urlname');
407:     } else {
408:         $urlname = $data['src_art_lang']['urlname'];
409:     }
410: 
411:     if ($urlname) {
412:         ModRewrite::setArtWebsafeName($urlname, $data['dest_art_lang']['idart'], $data['dest_art_lang']['idlang']);
413:     }
414: 
415:     return $data;
416: }
417: 
418: /**
419:  * Works as a wrapper for Contenido_Url.
420:  *
421:  * Will also be called by chain 'Contenido.Frontend.CreateURL'.
422:  *
423:  * @todo: Still exists bcause of downwards compatibility (some other modules/plugins are using it)
424:  *
425:  * @param   string  $url  URL to rebuild
426:  * @return  string        New URL
427:  */
428: function mr_buildNewUrl($url) {
429:     global $lang;
430: 
431:     ModRewriteDebugger::add($url, 'mr_buildNewUrl() in -> $url');
432: 
433:     $oUrl = cUri::getInstance();
434:     $aUrl = $oUrl->parse($url);
435: 
436:     // add language, if not exists
437:     if (!isset($aUrl['params']['lang'])) {
438:         $aUrl['params']['lang'] = $lang;
439:     }
440: 
441:     // build url
442:     $newUrl = $oUrl->build($aUrl['params']);
443: 
444:     // add existing fragment
445:     if (isset($aUrl['fragment'])) {
446:         $newUrl .= '#' . $aUrl['fragment'];
447:     }
448: 
449:     $arr = array(
450:         'in' => $url,
451:         'out' => $newUrl,
452:     );
453:     ModRewriteDebugger::add($arr, 'mr_buildNewUrl() in -> out');
454: 
455:     return $newUrl;
456: }
457: 
458: /**
459:  * Replaces existing ancors inside passed code, while rebuilding the urls.
460:  *
461:  * Will be called by chain 'Contenido.Content.conGenerateCode' or
462:  * 'Contenido.Frontend.HTMLCodeOutput' depening on mod_rewrite settings.
463:  *
464:  * @param   string  $code   Code to prepare
465:  * @return  string          New code
466:  */
467: function mr_buildGeneratedCode($code) {
468:     global $client, $cfgClient;
469: 
470:     ModRewriteDebugger::add($code, 'mr_buildGeneratedCode() in');
471: 
472:     $sseStartTime = getmicrotime();
473: 
474:     // mod rewrite is activated
475:     if (ModRewrite::isEnabled()) {
476: 
477:         // anchor hack
478:         $code = preg_replace_callback("/<a([^>]*)href\s*=\s*[\"|\'][\/]#(.?|.+?)[\"|\']([^>]*)>/i", function($match) {
479:             return ModRewrite::rewriteHtmlAnchor($match);
480:         }, $code);
481: 
482:         // remove fucking tinymce single quote entities:
483:         $code = str_replace("&#39;", "'", $code);
484: 
485:         // == IE hack with wrong base href interpretation
486:         // get base uri
487: //         $sBaseUri = cRegistry::getFrontendUrl();
488: //         $sBaseUri = cApiCecHook::execute("Contenido.Frontend.BaseHrefGeneration", $sBaseUri);
489: //         $code = preg_replace("/([\"|\'|=])upload\/(.?|.+?)([\"|\'|>])/ie", "stripslashes('\\1{$sBaseUri}upload/\\2\\3')", $code);
490: 
491:         $baseUri = cRegistry::getFrontendUrl();
492:         $baseUri = cApiCecHook::executeAndReturn("Contenido.Frontend.BaseHrefGeneration", $baseUri);
493: 
494:         // CON-1389 modifier /e is deprecated as of PHP 5.5
495:         $code = preg_replace_callback("/([\"|\'|=])upload\/(.?|.+?)([\"|\'|>])/i", function($match) use ($baseUri) {
496:             return stripslashes($match[1] . $baseUri . 'upload/' . $match[2] . $match[3]);
497:         }, $code);
498: 
499:         // define some preparations to replace /front_content.php & ./front_content.php
500:         // against front_content.php, because urls should start with front_content.php
501:         $aPattern = array(
502:             '/([\"|\'|=])\/front_content\.php(.?|.+?)([\"|\'|>])/i',
503:             '/([\"|\'|=])\.\/front_content\.php(.?|.+?)([\"|\'|>])/i'
504:         );
505: 
506:         $aReplace = array(
507:             '\1front_content.php\2\3',
508:             '\1front_content.php\2\3'
509:         );
510: 
511:         // perform the pre replacements
512:         $code = preg_replace($aPattern, $aReplace, $code);
513: 
514:         // create url stack object and fill it with found urls...
515:         $oMRUrlStack = ModRewriteUrlStack::getInstance();
516:         $oMRUrlStack->add('front_content.php');
517: 
518:         $matches = NULL;
519:         preg_match_all("/([\"|\'|=])front_content\.php(.?|.+?)([\"|\'|>])/i", $code, $matches, PREG_SET_ORDER);
520:         foreach ($matches as $val) {
521:             $oMRUrlStack->add('front_content.php' . $val[2]);
522:         }
523: 
524:         // ok let it beginn, start mod rewrite class
525:         $code = str_replace('"front_content.php"', '"' . mr_buildNewUrl('front_content.php') . '"', $code);
526:         $code = str_replace("'front_content.php'", "'" . mr_buildNewUrl('front_content.php') . "'", $code);
527:         $code = preg_replace_callback("/([\"|\'|=])front_content\.php(.?|.+?)([\"|\'|>])/i", function($match) {
528:             return $match[1] . mr_buildNewUrl('front_content.php' . $match[2]) . $match[3];
529:         }, $code);
530: 
531:         ModRewriteDebugger::add($code, 'mr_buildGeneratedCode() out');
532: 
533:     } else {
534:         // anchor hack for non modrewrite websites
535:         $code = preg_replace_callback("/<a([^>]*)href\s*=\s*[\"|\'][\/]#(.?|.+?)[\"|\']([^>]*)>/i", function($match) {
536:             return ModRewrite::contenidoHtmlAnchor($match, $GLOBALS['is_XHTML']);
537:         }, $code);
538:     }
539: 
540:     $sseEndTime = getmicrotime();
541: 
542:     ModRewriteDebugger::add(($sseEndTime - $sseStartTime), 'mr_buildGeneratedCode() total spend time');
543: 
544:     if ($debug = mr_debugOutput(false)) {
545:         $code = cString::iReplaceOnce("</body>", $debug . "\n</body>", $code);
546:     }
547: 
548:     return $code;
549:     // print "\n\n<!-- modrewrite generation time: " . ($sseEndTime - $sseStartTime) . " seconds -->";
550: }
551: 
552: /**
553:  * Sets language of client, like done in front_content.php
554:  *
555:  * @param  int  $client  Client id
556:  */
557: function mr_setClientLanguageId($client) {
558:     global $lang, $load_lang, $cfg;
559: 
560:     if ((int) $lang > 0) {
561:         // there is nothing to do
562:         return;
563:     } elseif ($load_lang) {
564:         // use the first language of this client, load_client is set in __FRONTEND_PATH__/data/config/config.php
565:         $lang = $load_lang;
566:         return;
567:     }
568: 
569:     // try to get clients language from table
570:     $sql = "SELECT B.idlang FROM "
571:             . $cfg['tab']['clients_lang'] . " AS A, "
572:             . $cfg['tab']['lang'] . " AS B "
573:             . "WHERE "
574:             . "A.idclient='" . ((int) $client) . "' AND A.idlang=B.idlang"
575:             . "LIMIT 0,1";
576: 
577:     if ($aData = mr_queryAndNextRecord($sql)) {
578:         $lang = $aData['idlang'];
579:     }
580: }
581: 
582: /**
583:  * Loads Advanced Mod Rewrite configuration for passed client using serialized
584:  * file containing the settings.
585:  *
586:  * File is placed in /contenido/mod_rewrite/includes/and is named like
587:  * config.mod_rewrite_{client_id}.php.
588:  *
589:  * @param  int   $clientId     Id of client
590:  * @param  bool  $forceReload  Flag to force to reload configuration, e. g. after
591:  *                             done changes on it
592:  */
593: function mr_loadConfiguration($clientId, $forceReload = false) {
594:     global $cfg;
595:     static $aLoaded;
596: 
597:     $clientId = (int) $clientId;
598:     if (!isset($aLoaded)) {
599:         $aLoaded = array();
600:     } elseif (isset($aLoaded[$clientId]) && $forceReload == false) {
601:         return;
602:     }
603: 
604:     $mrConfig = mr_getConfiguration($clientId);
605: 
606:     if (is_array($mrConfig)) {
607:         // merge mod rewrite configuration with global cfg array
608:         $cfg = array_merge($cfg, $mrConfig);
609:     } else {
610:         // couldn't load configuration, set defaults
611:         $backendPath = cRegistry::getBackendPath();
612:         include_once($backendPath . $cfg['path']['plugins'] . 'mod_rewrite/includes/config.mod_rewrite_default.php');
613:     }
614: 
615:     $aLoaded[$clientId] = true;
616: }
617: 
618: /**
619:  * Returns the path of the mod rewrite configuration file of an client.
620:  *
621:  * File is placed within client frontend path in directory "data/config/{CENVIRONMENT}/"
622:  * and has the name "config.mod_rewrite.php"
623:  *
624:  * @param   int   $clientId     Id of client
625:  * @return  string  File name and path
626:  */
627: function mr_getConfigurationFilePath($clientId) {
628:     $clientConfig = cRegistry::getClientConfig((int) $clientId);
629:     $fePath = $clientConfig['path']['frontend'];
630:     return $fePath . 'data/config/' . CON_ENVIRONMENT . '/config.mod_rewrite.php';
631: }
632: 
633: /**
634:  * Returns the mod rewrite configuration array of an client.
635:  *
636:  * File is placed in /contenido/mod_rewrite/includes/and is named like
637:  * config.mod_rewrite_{client_id}.php.
638:  *
639:  * @param   int   $clientId     Id of client
640:  * @return  array|NULL
641:  */
642: function mr_getConfiguration($clientId) {
643:     global $cfg;
644: 
645:     $clientId = (int) $clientId;
646: 
647:     $file = mr_getConfigurationFilePath($clientId);
648: 
649:     if (!is_file($file) || !is_readable($file)) {
650:         $backendPath = cRegistry::getBackendPath();
651:         $file = $backendPath . $cfg['path']['plugins'] . 'mod_rewrite/includes/config.mod_rewrite_' . $clientId . '.php';
652:     }
653: 
654:     if (!is_file($file) || !is_readable($file)) {
655:         return NULL;
656:     }
657:     if ($content = cFileHandler::read($file)) {
658:         return unserialize($content);
659:     } else {
660:         return NULL;
661:     }
662: }
663: 
664: /**
665:  * Saves the mod rewrite configuration array of an client.
666:  *
667:  * File is placed in /contenido/mod_rewrite/includes/and is named like
668:  * config.mod_rewrite_{client_id}.php.
669:  *
670:  * @param   int    $clientId     Id of client
671:  * @param   array  $config       Configuration to save
672:  * @return  bool
673:  */
674: function mr_setConfiguration($clientId, array $config) {
675:     global $cfg;
676: 
677:     $clientId = (int) $clientId;
678: 
679:     $file = mr_getConfigurationFilePath($clientId);
680:     $result = cFileHandler::write($file, serialize($config));
681: 
682:     $backendPath = cRegistry::getBackendPath();
683:     $file = $backendPath . $cfg['path']['plugins'] . 'mod_rewrite/includes/config.mod_rewrite_' . $clientId . '.php';
684:     if (is_file($file) && is_writeable($file)) {
685:         cFileHandler::remove($file, serialize($config));
686:     }
687: 
688:     return ($result) ? true : false;
689: }
690: 
691: /**
692:  * Includes the frontend controller script which parses the url and extacts
693:  * needed data like idcat, idart, lang and client from it.
694:  *
695:  * Will be called by chain 'Contenido.Frontend.AfterLoadPlugins' at front_content.php.
696:  *
697:  * @return  bool  Just a return value
698:  */
699: function mr_runFrontendController() {
700:     $iStartTime = getmicrotime();
701: 
702:     plugin_include('mod_rewrite', 'includes/config.plugin.php');
703: 
704:     if (ModRewrite::isEnabled() == true) {
705:         plugin_include('mod_rewrite', 'includes/front_content_controller.php');
706: 
707:         $totalTime = sprintf('%.4f', (getmicrotime() - $iStartTime));
708:         ModRewriteDebugger::add($totalTime, 'mr_runFrontendController() total time');
709:     }
710: 
711:     return true;
712: }
713: 
714: /**
715:  * Cleanups passed string from characters beeing repeated two or more times
716:  *
717:  * @param   string  $char    Character to remove
718:  * @param   string  $string  String to clean from character
719:  * @return  string  Cleaned string
720:  */
721: function mr_removeMultipleChars($char, $string) {
722:     while (cString::findFirstPos($string, $char . $char) !== false) {
723:         $string = str_replace($char . $char, $char, $string);
724:     }
725:     return $string;
726: }
727: 
728: /**
729:  * Returns amr related translation text
730:  *
731:  * @param   string  $key    The message id as string
732:  * @return  string  Related message
733:  */
734: function mr_i18n($key) {
735:     global $lngAMR;
736:     return (is_array($lngAMR) && isset($lngAMR[$key])) ? $lngAMR[$key] : 'n. a.';
737: }
738: 
739: ################################################################################
740: ### Some helper functions, which are not plugin specific
741: 
742: /**
743:  * Database query helper. Used to execute a select statement and to return the
744:  * result of first recordset.
745:  *
746:  * Minimizes following code:
747:  * <code>
748:  * // default way
749:  * $db  = cRegistry::getDb();
750:  * $sql = "SELECT * FROM foo WHERE bar='foobar'";
751:  * $db->query($sql);
752:  * $db->nextRecord();
753:  * $data = $db->getRecord();
754:  *
755:  * // new way
756:  * $sql  = "SELECT * FROM foo WHERE bar='foobar'";
757:  * $data = mr_queryAndNextRecord($sql);
758:  * </code>
759:  *
760:  * @param   string  $query  Query to execute
761:  * @return  mixed   Assoziative array including recordset or NULL
762:  */
763: function mr_queryAndNextRecord($query) {
764:     static $db;
765:     if (!isset($db)) {
766:         $db = cRegistry::getDb();
767:     }
768:     if (!$db->query($query)) {
769:         return NULL;
770:     }
771:     return ($db->nextRecord()) ? $db->getRecord() : NULL;
772: }
773: 
774: /**
775:  * Returns value of an array key (assoziative or indexed).
776:  *
777:  * Shortcut function for some ways to access to arrays:
778:  * <code>
779:  * // old way
780:  * if (is_array($foo) && isset($foo['bar']) && $foo['bar'] == 'yieeha') {
781:  *     // do something
782:  * }
783:  *
784:  * // new, more readable way:
785:  * if (mr_arrayValue($foo, 'bar') == 'yieeha') {
786:  *     // do something
787:  * }
788:  *
789:  * // old way
790:  * if (is_array($foo) && isset($foo['bar'])) {
791:  *     $jep = $foo['bar'];
792:  * } else {
793:  *     $jep = 'yummy';
794:  * }
795:  *
796:  * // new way
797:  * $jep = mr_arrayValue($foo, 'bar', 'yummy');
798:  * </code>
799:  *
800:  * @param   array  $array    The array
801:  * @param   mixed  $key      Position of an indexed array or key of an assoziative array
802:  * @param   mixed  $default  Default value to return
803:  * @return  mixed  Either the found value or the default value
804:  */
805: function mr_arrayValue($array, $key, $default = NULL) {
806:     if (!is_array($array)) {
807:         return $default;
808:     } elseif (!isset($array[$key])) {
809:         return $default;
810:     } else {
811:         return $array[$key];
812:     }
813: }
814: 
815: /**
816:  * Request cleanup function. Request data is allways tainted and must be filtered.
817:  * Pass the array to cleanup using several options.
818:  * Emulates array_walk_recursive().
819:  *
820:  * @param   mixed  $data     Data to cleanup
821:  * @param   array  $options  Default options array, provides only 'filter' key with several
822:  *                           filter functions which are to execute as follows:
823:  * <code>
824:  * $options['filter'] = array('trim', 'myFilterFunc');
825:  * </code>
826:  *                           If no filter functions are set, 'trim', 'strip_tags' and 'stripslashes'
827:  *                           will be used by default.
828:  *                           A userdefined function must accept the value as a parameter and must return
829:  *                           the filtered parameter, e. g.
830:  * <code>
831:  * function myFilter($data) {
832:  *    // do what you want with the data, e. g. cleanup of xss content
833:  *    return $data;
834:  * }
835:  * </code>
836:  *
837:  * @return  mixed  Cleaned data
838:  */
839: function mr_requestCleanup(&$data, $options = NULL) {
840:     if (!mr_arrayValue($options, 'filter')) {
841:         $options['filter'] = array('trim', 'strip_tags', 'stripslashes');
842:     }
843: 
844:     if (is_array($data)) {
845:         foreach ($data as $p => $v) {
846:             $data[$p] = mr_requestCleanup($v, $options);
847:         }
848:     } else {
849:         foreach ($options['filter'] as $filter) {
850:             if ($filter == 'trim') {
851:                 $data = trim($data);
852:             } elseif ($filter == 'strip_tags') {
853:                 $data = strip_tags($data);
854:             } elseif ($filter == 'stripslashes') {
855:                 $data = stripslashes($data);
856:             } elseif (function_exists($filter)) {
857:                 $data = call_user_func($filter, $data);
858:             }
859:         }
860:     }
861:     return $data;
862: }
863: 
864: /**
865:  * Minimalistic'n simple way to get request variables.
866:  *
867:  * Checks occurance in $_GET, then in $_POST. Uses trim() and strip_tags() to preclean data.
868:  *
869:  * @param   string  $key      Name of var to get
870:  * @param   mixed   $default  Default value to return
871:  * @return  mixed   The value
872:  */
873: function mr_getRequest($key, $default = NULL) {
874:     static $cache;
875:     if (!isset($cache)) {
876:         $cache = array();
877:     }
878:     if (isset($cache[$key])) {
879:         return $cache[$key];
880:     }
881:     if (isset($_GET[$key])) {
882:         $val = $_GET[$key];
883:     } elseif (isset($_POST[$key])) {
884:         $val = $_POST[$key];
885:     } else {
886:         $val = $default;
887:     }
888:     $cache[$key] = strip_tags(trim($val));
889:     return $cache[$key];
890: }
891: 
892: /**
893:  * Replaces calling of header method for redirects in front_content.php,
894:  * used during development.
895:  *
896:  * @param  $header  string Header value for redirect
897:  */
898: function mr_header($header) {
899:     header($header);
900:     return;
901: 
902:     $header = str_replace('Location: ', '', $header);
903:     echo '<html>
904:         <head></head>
905:         <body>
906:         <p><a href="' . $header . '">' . $header . '</a></p>';
907:     mr_debugOutput();
908:     echo '</body></html>';
909:     exit();
910: }
911: 
912: /**
913:  * Debug output only during development
914:  *
915:  * @param   bool  $print  Flag to echo the debug data
916:  * @return  mixed  Either the debug data, if parameter $print is set to true, or nothing
917:  */
918: function mr_debugOutput($print = true) {
919:     global $DB_Contenido_QueryCache;
920:     if (isset($DB_Contenido_QueryCache) && is_array($DB_Contenido_QueryCache) &&
921:             count($DB_Contenido_QueryCache) > 0) {
922:         ModRewriteDebugger::add($DB_Contenido_QueryCache, 'sql statements');
923: 
924:         // calculate total time consumption of queries
925:         $timeTotal = 0;
926:         foreach ($DB_Contenido_QueryCache as $pos => $item) {
927:             $timeTotal += $item['time'];
928:         }
929:         ModRewriteDebugger::add($timeTotal, 'sql total time');
930:     }
931: 
932:     $sOutput = ModRewriteDebugger::getAll();
933:     if ($print == true) {
934:         echo $sOutput;
935:     } else {
936:         return $sOutput;
937:     }
938: }
939: 
CMS CONTENIDO 4.10.0 API documentation generated by ApiGen 2.8.0