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

Documentation generated on Mon, 26 May 2008 19:42:15 +0200 by phpDocumentor 1.4.0