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