Source for file functions.mod_rewrite.php

Documentation is available at functions.mod_rewrite.php

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

Documentation generated on Sun, 08 Feb 2009 22:00:56 +0100 by phpDocumentor 1.4.1