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

Documentation generated on Sun, 20 Jul 2008 16:26:48 +0200 by phpDocumentor 1.4.0