Source for file functions.mod_rewrite.php

Documentation is available at functions.mod_rewrite.php

  1. <?php
  2. /**
  3.  * Defines the 'modrewrite' related functions
  4.  *
  5.  * @author      Stefan Seifarth / stese
  6.  * @copyright   © www.polycoder.de
  7.  * @package     Contenido
  8.  * @subpackage  ModRewrite
  9.  */
  10.  
  11. /******************************************
  12. * File      :   functions.mod_rewrite.php
  13. * Project   :   Contenido
  14. * Descr     :   Defines the 'modrewrite' related
  15. *               functions
  16. *
  17. * Author    :   Stefan Seifarth
  18. * Created   :   04.12.2004
  19. * Modified  :   18.12.2005
  20. *
  21. * © www.polycoder.de
  22. ******************************************/
  23.  
  24.  
  25. /**
  26.  * mr_get_language_id()
  27.  *
  28.  * get language id from language name
  29.  * thanks to Nicolas Dickinson for multi
  30.  * Client/Language BugFix
  31.  *
  32.  * @param    string    language name
  33.  * @return integer    language id
  34.  */
  35. function mr_get_language_id $str_languagename ""$int_client {
  36.     global $cfg;
  37.     static $db;
  38.     
  39.     $int_lang_id false;
  40.     if (!isset($db)) {
  41.         $db new DB_Contenido;
  42.     }
  43.  
  44.     $sql "SELECT l.idlang
  45.            FROM " $cfg["tab"]["lang"" as l 
  46.            LEFT JOIN " $cfg["tab"]["clients_lang"" AS cl ON l.idlang = cl.idlang
  47.            WHERE
  48.               cl.idclient = '". (int)$int_client "' AND 
  49.               l.name = '" urldecode($str_languagename"'";
  50.  
  51.     $db->query($sql);
  52.     $db->next_record();
  53.         
  54.     if ($db->f('idlang'0{
  55.         $int_lang_id $db->f('idlang');
  56.     }
  57.  
  58.     return $int_lang_id;
  59. }
  60.  
  61.  
  62.  
  63. /**
  64.  * mr_get_client_id()
  65.  *
  66.  * get client id from client name
  67.  *
  68.  * @param      string    client name
  69.  * @return  integer   client id
  70.  */
  71. function mr_get_client_id $str_clientname "" {
  72.     global $cfg;
  73.     static $db;
  74.     
  75.     $int_client_id false;
  76.     if (!isset($db)) {
  77.         $db new DB_Contenido;
  78.     }
  79.     
  80.     $sql "SELECT idclient 
  81.             FROM " $cfg["tab"]["clients""
  82.             WHERE name = '" urldecode($str_clientname"'";
  83.     $db->query($sql);
  84.     $db->next_record();
  85.         
  86.     if ($db->f('idclient'0{
  87.         $int_client_id $db->f('idclient');
  88.     }
  89.  
  90.     return $int_client_id;
  91. }
  92.  
  93.  
  94.  
  95. /**
  96.  * mr_get_idart()
  97.  *
  98.  * get article id
  99.  *
  100.  * get recent article from websafe name tree
  101.  *
  102.  * @modified: 2005-09-11
  103.  * @param    string    Websafe name
  104.  * @param    integer    category id
  105.  * @return     integer    recent article id
  106.  */
  107. function mr_get_idart $str_artname ""$int_id 0$int_lang_id {
  108.     global $cfg;
  109.     static $db;
  110.     
  111.     $int_idart false;
  112.     
  113.     if (!isset($db)) {
  114.         $db new DB_Contenido;
  115.     }
  116.     
  117.     $str_where "";
  118.     // only article name were given
  119.     if ($int_id == 0{
  120.         // get all basic category ids with parentid=0
  121.         $sql "SELECT idcat FROM " $cfg["tab"]["cat"" WHERE parentid = '0'";
  122.         $db->query $sql );
  123.         
  124.         $arr_idcats array();
  125.         $arr_where array();
  126.         
  127.         while ($db->next_record()) {
  128.             $arr_idcats["idcat = '" $db->f("idcat""'";
  129.         }
  130.         $str_where " AND ( " join(" OR "$arr_idcats")";
  131.     else {
  132.         $str_where " AND ca.idcat = '$int_id'";
  133.     }
  134.     
  135.     $sql "SELECT al.idart 
  136.             FROM " $cfg["tab"]["art_lang"" al 
  137.             LEFT JOIN " $cfg["tab"]["cat_art"" ca 
  138.             ON al.idart = ca.idart 
  139.             WHERE al.urlname = '$str_artname'$str_where;
  140.             
  141.     $db->query($sql);
  142.     $db->next_record();
  143.     
  144.     if ($db->f('idart'0{
  145.         $int_idart $db->f('idart');
  146.     }
  147.     
  148.     return $int_idart;
  149. }
  150.  
  151.  
  152.  
  153.  
  154. /**
  155.  * mr_get_setting_override()
  156.  *
  157.  * override local cfg settings with client settings from database
  158.  *
  159.  * @param    integer    client id
  160.  */
  161. function mr_get_setting_override $int_client_id {
  162. // TODO: we don't relly need this!
  163. return;
  164.    global $cfg;
  165.    static $db;
  166.  
  167.     if (!isset($db)) {
  168.         $db new DB_Contenido;
  169.     }
  170.       
  171.    $sql "SELECT name, value FROM " $cfg["tab"]["properties"" WHERE type='ModRewrite' AND idclient = '" $int_client_id "' AND name NOT LIKE 'frontend_path%'";
  172.    $db->query($sql);
  173.    
  174.    while $db->next_record() ) {
  175.       $cfg['mod_rewrite'][urldecode($db->f("name"))urldecode($db->f("value"));
  176.    }
  177. }
  178.  
  179.  
  180. // plugin Advanced Mod Rewrite - Murat Purc (aka xmurrix)
  181.  
  182. /**
  183.  * Processes mod_rewrite related job after moving a category up.
  184.  *
  185.  * Will be called as a action code from table con_actions.
  186.  * 
  187.  * @param  int  $idcat  Id of category beeing moved up
  188.  */
  189. function mr_strMoveUpCategory($idcat{
  190.  
  191.     // category check
  192.     $cat new cApiCategory($idcat);
  193.     if (!$cat->get('preid')) {
  194.         return;
  195.     }
  196.  
  197.     // get all cat languages
  198.     $arr_idlang ModRewrite::get_cat_languages($idcat);
  199.  
  200.     // update ...
  201.     foreach ($arr_idlang as $int_idlang{
  202.         // get urlname
  203.         $str_catname ModRewrite::get_catname($idcat$int_idlang);
  204.         // set new urlname - because original set urlname isn't validated for double entries in same parent category
  205.         ModRewrite::set_category($str_catname$idcat$int_idlang);
  206.     }
  207.     
  208. }
  209.  
  210.  
  211. /**
  212.  * Processes mod_rewrite related job after moving a category down.
  213.  *
  214.  * Will be called as a action code from table con_actions.
  215.  * 
  216.  * @param  int  $idcat  Id of category beeing moved down
  217.  */
  218. function mr_strMoveDownCategory($idcat{
  219.     
  220.     // category check
  221.     $cat new cApiCategory($idcat);
  222.     if (!$cat->get('id')) {
  223.         return;
  224.     }
  225.     
  226.     // get all cat languages
  227.     $arr_idlang ModRewrite::get_cat_languages($idcat);
  228.     // update ...
  229.     foreach ($arr_idlang as $int_idlang{
  230.         // get urlname
  231.         $str_catname ModRewrite::get_catname($idcat$int_idlang);
  232.         // set new urlname - because original set urlname isn't validated for double entries in same parent category
  233.         ModRewrite::set_category($str_catname$idcat$int_idlang);
  234.     }
  235.  
  236. }
  237.  
  238.  
  239.  
  240. /**
  241.  * Processes mod_rewrite related job after moving a category subtree.
  242.  *
  243.  * Will be called as a action code from table con_actions.
  244.  * 
  245.  * @param  int  $idcat  Id of category where the subtree has beeen moved
  246.  */
  247. function mr_strMoveSubtree($idcat{
  248.     // category check
  249.     $cat new cApiCategory($idcat);
  250.     if (!$cat->get('id')) {
  251.         return;
  252.     }
  253.     
  254.     // get all cat languages
  255.     $arr_idlang ModRewrite::get_cat_languages($idcat);
  256.     // update ...
  257.     foreach ($arr_idlang as $int_idlang{
  258.         // get urlname
  259.         $str_catname ModRewrite::get_catname($idcat$int_idlang);
  260.         // set new urlname - because original set urlname isn't validated for double entries in same parent category
  261.         ModRewrite::set_category($str_catname$idcat$int_idlang);
  262.     }
  263. }
  264.  
  265.  
  266. /**
  267.  * Processes mod_rewrite related job after editing a article first time.
  268.  *
  269.  * Will be called as a action code from table con_actions.
  270.  * 
  271.  * @param  int  $newIdart  Id of new edited article
  272.  */
  273. function mr_conEditFirstTime($newIdart{
  274.     global $client$title$urlname$idcat;
  275.  
  276.     if (strlen(trim($urlname)) == 0{
  277.         $urlname $title;
  278.     }
  279.  
  280.     $a_languages getLanguagesByClient($client);
  281.  
  282.     foreach ($a_languages as $tmp_lang{               
  283.         ModRewrite::set_article($urlname$newIdart$tmp_lang$idcat);
  284.     }
  285. }
  286.  
  287.  
  288. /**
  289.  * Processes mod_rewrite related job after editing a article.
  290.  *
  291.  * Will be called as a action code from table con_actions.
  292.  * 
  293.  * @param  int  $idart  Id of edited article
  294.  */
  295. function mr_conEditArt($idart){
  296.     global $urlname$title$idartlang;
  297.  
  298.     if (strlen(trim($urlname)) == 0{
  299.         $urlname $title;
  300.     }
  301.  
  302.     $arr_art ModRewrite::get_id_from_idartlang($idartlang);
  303.     if (count($arr_art== 2{
  304.         ModRewrite::set_article($urlname$arr_art['idart']$arr_art['idlang']$idcat);
  305.     }
  306. }
  307.  
  308.  
  309. /**
  310.  * Processes mod_rewrite related job for articles beeing moved.
  311.  *
  312.  * Will be called by chain 'Contenido.Article.conMoveArticles_Loop'.
  313.  * 
  314.  * @param   array  $param  Assoziative array with record entries
  315.  * @return  array  Loop through of arguments
  316.  */
  317. function mr_conMoveArticles($param){
  318.     // too defensive but secure way
  319.     if (!is_array($param)) {
  320.         return $param;
  321.     elseif (!isset($param['idartlang'])) {
  322.         return $param;
  323.     elseif (!isset($param['idart'])) {
  324.         return $param;
  325.     }
  326.  
  327.     $arr_art ModRewrite::get_artids($param['idartlang']);
  328.     if (count($arr_art== 2{
  329.         ModRewrite::set_article($arr_art["urlname"]$param['idart']$arr_art["idlang"]);
  330.     }
  331.     
  332.     return $param;
  333. }
  334.  
  335.  
  336. /**
  337.  * Processes mod_rewrite related job for duplicated articles.
  338.  *
  339.  * Will be called by chain 'Contenido.Article.conCopyArtLang_AfterInsert'.
  340.  * 
  341.  * @param   array  $param  Assoziative array with record entries
  342.  * @return  array  Loop through of arguments
  343.  */
  344. function mr_conCopyArtLang($param{
  345.     // too defensive but secure way
  346.     if (!is_array($param)) {
  347.         return $param;
  348.     elseif (!isset($param['title'])) {
  349.         return $param;
  350.     elseif (!isset($param['idart'])) {
  351.         return $param;
  352.     elseif (!isset($param['idlang'])) {
  353.         return $param;
  354.     }
  355.  
  356.     ModRewrite::set_article($param['title']$param['idart']$param['idlang']);
  357.     
  358.     return $param;
  359. }
  360.  
  361.  
  362. /**
  363.  * Processes mod_rewrite related job for synchronized articles.
  364.  *
  365.  * Will be called by chain 'Contenido.Article.conSyncArticle_AfterInsert'.
  366.  * 
  367.  * @param   array  $param  Assoziative array with record entries as follows:
  368.  *  <code>
  369.  *  array(
  370.  *      'src_art_lang'  => Recordset (assoziative array) of source item from con_art_lang table
  371.  *      'dest_art_lang' => Recordset (assoziative array) of inserted destination item from con_art_lang table
  372.  *  );
  373.  *  </code>
  374.  * 
  375.  * @return  array  Loop through of argument
  376.  */
  377. function mr_conSyncArticle($param){
  378.     // too defensive but secure way
  379.     if (!is_array($param)) {
  380.         return $param;
  381.     elseif (!isset($param['src_art_lang']|| !is_array($param['src_art_lang'])) {
  382.         return $param;
  383.     elseif (!isset($param['dest_art_lang']|| !is_array($param['dest_art_lang'])) {
  384.         return $param;
  385.     elseif (!isset($param['dest_art_lang']['idart'])) {
  386.         return $param;
  387.     elseif (!isset($param['dest_art_lang']['idlang'])) {
  388.         return $param;
  389.     }
  390.     
  391.     if (!isset($param['src_art_lang']['urlname'])) {
  392.         $artLang new cApiArticleLanguage($param['src_art_lang']['idartlang']);
  393.         $urlname $artLang->get('urlname');
  394.     else {
  395.         $urlname $param['src_art_lang']['urlname'];
  396.     }
  397.  
  398.     if ($urlname{
  399.         ModRewrite::set_article($urlname$param['dest_art_lang']['idart']$param['dest_art_lang']['idlang']);
  400.     }
  401.     
  402.     return $param;
  403. }
  404.  
  405. /**
  406.  * Processes mod_rewrite related job after sychronizing a category.
  407.  *
  408.  * Will be called as a action code from table con_actions.
  409.  * 
  410.  * @param  int   $idcatParam  Id of category which has been sychronized
  411.  * @param  int   $sourcelang  Source language id
  412.  * @param  int   $targetlang  Destination language id
  413.  * @param  bool  $bMultiple   Flag to synchronize all existing subcategories
  414.  */
  415. /*
  416. // not good idea...
  417. function mr_strSyncCategory($idcatParam, $sourcelang, $targetlang, $bMultiple = false) {
  418.     global $cfg;
  419.  
  420.     $db2       = new DB_Contenido;
  421.     $bMultiple = (bool) $bMultiple;
  422.     
  423.     $aCatArray = array();
  424.     if ($bMultiple == true) {
  425.         $aCatArray = strDeeperCategoriesArray($idcatParam);
  426.     } else {
  427.         array_push($aCatArray, $idcatParam);
  428.     }
  429.  
  430.     foreach ($aCatArray as $idcat) {
  431.         
  432.         $sql = "SELECT name FROM ".$cfg["tab"]["cat_lang"]." WHERE idcat = '$idcat' AND idlang = '$sourcelang'";
  433.         $db2->query($sql);
  434.         if ($db2->next_record()) {
  435.             $name = $db2->f("name");
  436.             ModRewrite::set_category($name, $idcat, $targetlang);
  437.         }
  438.     }
  439. }
  440. */
  441.  
  442.  
  443.  
  444. /**
  445.  * mr_build_new_url()
  446.  * 
  447.  * Works as a wrapper for ModRewrite::build_new_url() function.
  448.  * 
  449.  * Will be called by chain 'Contenido.Frontend.CreateURL'.
  450.  * 
  451.  * @param   string  $url  URL to rebuild
  452.  * @return  string        New URL
  453.  */
  454. function mr_build_new_url($url{
  455.     global $cfg;
  456.     static $isXHTML;
  457.     if (!isset($isXHTML)) {
  458.         $isXHTML (getEffectiveSetting('generator''xhtml''false'== 'false'false true;
  459.     }
  460.  
  461.        $urlDebug['in'$url;
  462.     
  463.     // cleanup any existing & entities
  464.     $url str_replace('&amp;''&'$url);
  465.     
  466.     if ($cfg['mod_rewrite']['use'== 1{
  467.         $aUrl ModRewrite::get_client_full_url_parts($url);
  468.         if (preg_match("/^front_content\.php(.*|.+?)/i"$aUrl['url']$arr_hits== 1{
  469.             $url $aUrl['htmlpath'ModRewrite::build_new_url($arr_hits[1]);
  470.         else {
  471.                $GLOBALS['mpDebug']->addDebug($url'mr_build_new_url() nomatch');
  472.         }
  473.     }
  474.  
  475.     // replace & against entity, if xhtml is to use
  476.     if ($isXHTML{
  477.         $url str_replace('&''&amp;'$url);
  478.     }
  479.     
  480.        $urlDebug['out'$url;
  481.     $GLOBALS['mpDebug']->addDebug($urlDebug'mr_build_new_url() in -> out');
  482.     return $url;
  483. }
  484.  
  485.  
  486.  
  487. /**
  488.  * mr_build_generated_code()
  489.  * 
  490.  * Replaces existing ancors inside passed code, while rebuilding the urls.
  491.  * 
  492.  * Will be called by chain 'Contenido.Content.conGenerateCode' or
  493.  * 'Contenido.Frontend.HTMLCodeOutput' depening on mod_rewrite settings.
  494.  * 
  495.  * @param   string  $code   Code to prepare
  496.  * @return  string          New code
  497.  */
  498. function mr_build_generated_code($code{
  499.     global $cfg;
  500.     
  501.        $GLOBALS['mpDebug']->addDebug($code'mr_build_generated_code() in');
  502.     
  503.     // mod rewrite is activated
  504.     if ($cfg['mod_rewrite']['use'== 1{
  505.         $sseStarttime getmicrotime();
  506.         
  507.         // edit 060603 - anchor hack
  508.         $code preg_replace_callback(
  509.                 "/<a([^>]*)href\s*=\s*[\"|\'][\/]#(.?|.+?)[\"|\']([^>]*)>/i"
  510.                 create_function('$arr_matches' 'return ModRewrite::rewrite_html_anchor($arr_matches);'),
  511.                 $code
  512.         );
  513.         
  514.         // remove fucking tinymce single quote entities:
  515.         $code str_replace("&#39;""'"$code);
  516.         
  517.         // IE hack with wrong base href interpretation 
  518.         $code preg_replace("/([\"|\'|=])upload\/(.?|.+?)([\"|\'|>])/ie""stripslashes('\\1${str_base_uri}upload/\\2\\3')"$code);
  519.         
  520.         // ok let it beginn, start mod rewrite class
  521.         $code preg_replace_callback(
  522.                 "/([\"|\'|=])front_content\.php(.?|.+?)([\"|\'|>])/i"
  523.                 create_function('$arr_matches' 'return $arr_matches[1] . ModRewrite::build_new_url($arr_matches[2]) . $arr_matches[3];'),
  524.                 $code
  525.         );
  526.         $sseEndtime getmicrotime();
  527.     else {
  528.         // anchor hack for non modrewrite websites
  529.         $code preg_replace_callback(
  530.                 "/<a([^>]*)href\s*=\s*[\"|\'][\/]#(.?|.+?)[\"|\']([^>]*)>/i"
  531.                 create_function('$arr_matches' 'return ModRewrite::contenido_html_anchor($arr_matches, $GLOBALS["is_XHTML"]);'),
  532.                 $code
  533.         );
  534.     }
  535.  
  536.        $GLOBALS['mpDebug']->addDebug($code'mr_build_generated_code() out');
  537.     
  538.     return $code;
  539.     // print "\n\n<!-- modrewrite generation time: " . ($sseEndtime - $sseStarttime) . " seconds -->";
  540.     
  541. }
  542.  
  543.  
  544. function mr_set_client_language($client{
  545.     if ($GLOBALS['lang'|| (int) $GLOBALS['lang'0{
  546.         // there is nothing to do
  547.         return;
  548.     }
  549.     
  550.     // use the first language of this client
  551.     if (isset($GLOBALS['load_lang'])) {
  552.         // load_client is set in frontend/config.php
  553.         $GLOBALS['lang'$GLOBALS['load_lang'];
  554.     else {
  555.         $sql "SELECT B.idlang FROM
  556.                     ".$GLOBALS['cfg']['tab']['clients_lang']." AS A, 
  557.                     ".$GLOBALS['cfg']['tab']['lang']." AS B
  558.                 WHERE
  559.                     A.idclient='" ((int) $client"' AND 
  560.                     A.idlang=B.idlang
  561.                 LIMIT 0,1";
  562.  
  563.         if (!isset($GLOBALS['db'])) {
  564.             $GLOBALS['db'new DB_Contenido();
  565.         }
  566.         $GLOBALS['db']->query($sql);
  567.         if ($GLOBALS['db']->next_record()) {
  568.             $GLOBALS['lang'$GLOBALS['db']->f('idlang');
  569.         }
  570.     }
  571. }
  572.  
  573.  
  574.  
  575. /**
  576.  * Loads Advanced Mod Rewrite configuration for passed client using serialized
  577.  * file containing the settings.
  578.  * 
  579.  * File is placed in /contenido/mod_rewrite/includes/and is named like
  580.  * config.mod_rewrite_{client_id}.php.
  581.  * 
  582.  *
  583.  * @param  int  $clientId  Id of client
  584.  */
  585. function mr_load_configuration($clientId{
  586.     global $cfg;
  587.     static $aLoaded;
  588.     
  589.     $clientId = (int) $clientId;
  590.     if (!isset($aLoaded)) {
  591.         $aLoaded array();
  592.     elseif (isset($aLoaded[$clientId])) {
  593.         return;
  594.     }
  595.     
  596.     $options['key'$cfg['path']['contenido'$cfg['path']['plugins''mod_rewrite/includes/config.mod_rewrite_' $clientId '.php';
  597.     
  598.     $config ConfigFactory::get('serializer'$options);
  599.     $mrConfig $config->get();
  600.     if (is_array($mrConfig)) {
  601.         
  602.         // merge mod rewrite configuration with global cfg array
  603.         $cfg array_merge($cfg$mrConfig);
  604.         
  605.     else {
  606.     
  607.         // couldn't load configuration, set defaults
  608.         include_once($cfg['path']['contenido'$cfg['path']['plugins''mod_rewrite/includes/config.mod_rewrite_default.php');
  609.         
  610.     }
  611.     
  612.     $aLoaded[$clientIdtrue;
  613.     
  614. }
  615.  
  616. ?>

Documentation generated on Mon, 19 May 2008 22:47:04 +0200 by phpDocumentor 1.4.0