Source for file class.modrewrite.php

Documentation is available at class.modrewrite.php

  1. <?php
  2. /**
  3.  * Include class to create websafe names
  4.  *
  5.  * @author      Stefan Seifarth / stese
  6.  * @copyright   © www.polycoder.de
  7.  * @author      Murat Purc <murat@purc.de>
  8.  * @date        04.12.2004
  9.  * @date        28.12.2005
  10.  * @package     Contenido
  11.  * @subpackage  ModRewrite
  12.  */
  13.  
  14. /******************************************
  15.  * File      :   class.modrewrite.php
  16.  * Project   :   Contenido
  17.  * Descr     :   class to create websafe names
  18.  *
  19.  * Author    :   Stefan Seifarth / stese
  20.  * Created   :   04.12.2004
  21.  * Modified  :   28.12.2005
  22.  *
  23.  * © www.polycoder.de
  24.  */
  25.  
  26.  
  27. if(!defined('CON_FRAMEWORK')) {
  28.     die('Illegal call');
  29. }
  30.  
  31.  
  32. /**
  33.  * Class to create websafe names
  34.  *
  35.  * TODO: Adapt to PHP 5 coding standards...
  36.  *
  37.  * @author      Stefan Seifarth / stese
  38.  * @package     Contenido
  39.  * @subpackage  ModRewrite
  40.  */
  41. class ModRewrite {
  42.  
  43.     /**
  44.      * Constructor Function
  45.      */
  46.     function ModRewrite() {
  47.         // empty
  48.     }
  49.  
  50.  
  51.     /**
  52.      * Returns enabled state of mod rewrite plugin
  53.      *
  54.      * @return  bool 
  55.      */
  56.     function is_enabled(){
  57.         global $cfg;
  58.         return ($cfg['mod_rewrite']['use'] == 1) ? true : false;
  59.     }
  60.  
  61.  
  62.     /**
  63.      * Returns configuration of mod rewrite, content of gobal $cfg['mod_rewrite']
  64.      *
  65.      * @return  array  Assoziative array of mr configuration
  66.      */
  67.     function getConfig() {
  68.         global $cfg;
  69.         return $cfg['mod_rewrite'];
  70.     }
  71.  
  72.  
  73.     /**
  74.      * Check categories on websafe name
  75.      *
  76.      * Check all categories in the main parent category on existing same websafe name
  77.      *
  78.      * @param    string     Websafe name to check
  79.      * @param    int         Current category id
  80.      * @param    int      Current language id
  81.      * @return     bool     True if websafename already exists, false if not
  82.      */
  83.     function in_category($str_name="", $int_id=0, $int_lang_id=0) {
  84.         global $cfg;
  85.  
  86.         $int_id      = (int) $int_id;
  87.         $int_lang_id = (int) $int_lang_id;
  88.  
  89.         // get parentid
  90.         $int_parent_id = 0;
  91.         $sql = "SELECT parentid FROM " . $cfg["tab"]["cat"] . " WHERE idcat = '$int_id'";
  92.         if ($aData = mr_query_n_next_record($sql)) {
  93.             $int_parent_id = ($aData['parentid'] > 0 ) ? (int) $aData['parentid'] : 0;
  94.         }
  95.  
  96.         $str_where = " c.parentid = '$int_parent_id' AND"
  97.                    . " cl.idlang = '" . $int_lang_id . "' AND "
  98.                    . " cl.urlname = '" . $str_name . "' AND "
  99.                    . " cl.idcat <> '$int_id'";
  100.  
  101.         // check if websafe name is in this category
  102.         $int_count = 0;
  103.         $sql = "SELECT count(cl.idcat) as numcats FROM " . $cfg["tab"]["cat_lang"] . " cl LEFT JOIN " . $cfg["tab"]["cat"] . " c ON cl.idcat = c.idcat WHERE " . $str_where;
  104.  
  105.         $GLOBALS['mpDebug']->log($sql, 'ModRewrite::in_category $sql');
  106.  
  107.         if ($aData = mr_query_n_next_record($sql)) {
  108.             return ($aData['numcats'] > 0) ? true : false;
  109.         }
  110.  
  111.         return false;
  112.     }
  113.  
  114.  
  115.     /**
  116.      * Check articles on websafe name.
  117.      *
  118.      * Check all articles in the current category on existing same websafe name.
  119.      *
  120.      * @param    string  Websafe name to check
  121.      * @param    int     Current article id
  122.      * @param    int     Current language id
  123.      * @return     bool    True if websafename already exists, false if not
  124.      */
  125.     function in_articles($str_name="", $int_id=0, $int_lang_id=0, $int_idcat=0) {
  126.         global $cfg;
  127.  
  128.         $int_id      = (int) $int_id;
  129.         $int_lang_id = (int) $int_lang_id;
  130.         $int_idcat   = (int) $int_idcat;
  131.  
  132.         // handle multipages
  133.         $int_category_id = 0;
  134.         if ($int_idcat > 0) {
  135.             $int_category_id = $int_idcat;
  136.         } else {
  137.             // get category id if not set
  138.             $sql = "SELECT idcat FROM " . $cfg["tab"]["cat_art"] . " WHERE idart = '$int_id'";
  139.             if ($aData = mr_query_n_next_record($sql)) {
  140.                 $int_category_id = ($aData['idcat'] > 0 ) ? (int) $aData['idcat'] : 0;
  141.             }
  142.         }
  143.  
  144.         $str_where = " ca.idcat = '$int_category_id' AND";
  145.         $str_where.= "     al.idlang = '" . $int_lang_id . "' AND
  146.                         al.urlname = '" . $str_name . "' AND
  147.         al.idart <> '$int_id'";
  148.  
  149.         // check if websafe name is in this category
  150.         $sql = "SELECT count(al.idart) as numcats FROM " . $cfg["tab"]["art_lang"] . " al LEFT JOIN " . $cfg["tab"]["cat_art"] . " ca ON al.idart = ca.idart WHERE " . $str_where;
  151.         if ($aData = mr_query_n_next_record($sql)) {
  152.             return ($aData['numcats'] > 0) ? true : false;
  153.         }
  154.  
  155.         return false;
  156.     }
  157.  
  158.  
  159.     /**
  160.      * Set websafe name in article list.
  161.      *
  162.      * Insert new websafe name in article list
  163.      *
  164.      * @param   string  Original name (will be converted)
  165.      * @param   int     Current article id
  166.      * @param   int     Current language id
  167.      * @return     bool    True if insert was successfully
  168.      */
  169.     function set_article($str_name="", $int_id=0, $int_lang_id=0, $int_idcat=0) {
  170.         global $cfg;
  171.  
  172.         $int_id      = (int) $int_id;
  173.         $int_lang_id = (int) $int_lang_id;
  174.         $int_idcat   = (int) $int_idcat;
  175.  
  176.         // create websafe name
  177.         $str_new_name = capiStrCleanURLCharacters(html_entity_decode($str_name));
  178.  
  179.         // only html files part
  180.             $str_new_name = str_replace($cfg["mod_rewrite"]['article_seperator'], $cfg["mod_rewrite"]['article_word_seperator'], $str_new_name);
  181.         }
  182.  
  183.         // check for double word separators
  184.         if (strlen(trim($cfg["mod_rewrite"]['article_word_seperator'])) > 0) {
  185.  
  186.             if ($cfg["mod_rewrite"]['article_word_seperator'] !== '-') {
  187.                 // replace set '-' by capiStrCleanURLCharacters against configured one
  188.                 $str_new_name = str_replace('-', $cfg["mod_rewrite"]['article_word_seperator'], $str_new_name);
  189.             }
  190.  
  191.             // remove double or more separators
  192.             $str_new_name = preg_replace('#' . $cfg["mod_rewrite"]['article_word_seperator'] .'{2,}#', $cfg["mod_rewrite"]['article_word_seperator'], $str_new_name);
  193.         }
  194.  
  195.         // check if websafe name already exists
  196.         if (ModRewrite::in_articles($str_new_name, $int_id, $int_lang_id, $int_idcat)) {
  197.             // create new websafe name if exists
  198.             $str_new_name = $str_new_name . $int_id;
  199.         }
  200.  
  201.         // check again - and set name
  202.         if (!ModRewrite::in_articles($str_new_name, $int_id, $int_lang_id, $int_idcat)) {
  203.  
  204.             // insert websafe name in article list
  205.             $db = new DB_Contenido;
  206.             $sql = "UPDATE " . $cfg["tab"]["art_lang"] . " SET urlname = '$str_new_name' WHERE idart = '$int_id' AND idlang = '$int_lang_id'";
  207.             return $db->query($sql);
  208.  
  209.         } else {
  210.             return false;
  211.         }
  212.     }
  213.  
  214.  
  215.     /**
  216.      * Set websafe name in category list.
  217.      *
  218.      * Insert new websafe name in category list.
  219.      *
  220.      * @param   string  Original name (will be converted) or alias
  221.      * @param   int     Current article id
  222.      * @param   int     Current language id
  223.      * @return  bool    True if insert was successfully
  224.      */
  225.     function set_category($str_name="", $int_id=0, $int_lang_id=0) {
  226.         global $cfg;
  227.  
  228.         $int_id      = (int) $int_id;
  229.         $int_lang_id = (int) $int_lang_id;
  230.  
  231.         // create websafe name
  232.         $str_new_name = capiStrCleanURLCharacters(html_entity_decode($str_name));
  233.  
  234.         // only html files part
  235.             $str_new_name = str_replace( $cfg["mod_rewrite"]['category_seperator'], $cfg["mod_rewrite"]['category_word_seperator'], $str_new_name );
  236.             $str_new_name = str_replace( $cfg["mod_rewrite"]['article_seperator'], $cfg["mod_rewrite"]['category_word_seperator'], $str_new_name );
  237.         }
  238.  
  239.         // check for double word seperators
  240.         if (strlen(trim($cfg["mod_rewrite"]['category_word_seperator'])) > 0) {
  241.             while (ereg($cfg["mod_rewrite"]['category_word_seperator'].$cfg["mod_rewrite"]['category_word_seperator'],$str_new_name)) {
  242.                 $str_new_name = str_replace($cfg["mod_rewrite"]['category_word_seperator'].$cfg["mod_rewrite"]['category_word_seperator'], $cfg["mod_rewrite"]['category_word_seperator'],$str_new_name);
  243.             }
  244.         }
  245.  
  246.         // check if websafe name already exists
  247.         if (ModRewrite::in_category($str_new_name, $int_id, $int_lang_id)) {
  248.             // create new websafe name if exists
  249.             $str_new_name = $str_new_name . $int_id;
  250.         }
  251.  
  252.  
  253.         // check again - and set name
  254.         if (!ModRewrite::in_category($str_new_name, $int_id, $int_lang_id)) {
  255.  
  256.             // insert websafe name in article list
  257.             $db = new DB_Contenido;
  258.             $sql = "UPDATE " . $cfg["tab"]["cat_lang"] . " SET urlname = '$str_new_name' WHERE idcat = '$int_id' AND idlang = '$int_lang_id'";
  259.  
  260.             $GLOBALS['mpDebug']->log(array(
  261.                 'str_name'     => $str_name,
  262.                 'int_id'       => $int_id,
  263.                 'int_lang_id'  => $int_lang_id,
  264.                 'str_new_name' => $str_new_name
  265.                 ), 'ModRewrite::set_category $data'
  266.             );
  267.  
  268.             return $db->query($sql);
  269.  
  270.         } else {
  271.             return false;
  272.         }
  273.     }
  274.  
  275.  
  276.     /**
  277.      * Get article id and language id from article language id
  278.      *
  279.      * @param   int    Current article id
  280.      * @return  array  Array with idart and idlang of current article
  281.      */
  282.     function get_id_from_idartlang($int_id=0) {
  283.         global $cfg;
  284.  
  285.         $int_id = (int) $int_id;
  286.  
  287.         $sql = "SELECT idart, idlang FROM " . $cfg["tab"]["art_lang"] . " WHERE idartlang = '$int_id'";
  288.         if ($aData = mr_query_n_next_record($sql)) {
  289.             return $aData;
  290.         }
  291.         return array();
  292.     }
  293.  
  294.  
  295.     /**
  296.      * Get category name from category id and language id.
  297.      *
  298.      * @param   int     Category id
  299.      * @param   int     Language id
  300.      * @return  string  Category name
  301.      */
  302.     function get_catname($int_id=0, $int_lang_id=0) {
  303.         global $cfg;
  304.  
  305.         $int_id      = (int) $int_id;
  306.         $int_lang_id = (int) $int_lang_id;
  307.         $key         = $int_id . '-' . $int_lang_id;
  308.  
  309.         static $aCatName;
  310.         if (!isset($aCatName)) {
  311.             $aCatName = array();
  312.         } elseif (isset($aCatName[$key])) {
  313.             return $aCatName[$key];
  314.         }
  315.  
  316.         $sql = "SELECT name FROM " . $cfg["tab"]["cat_lang"] . " WHERE idcat = '$int_id' AND idlang = '$int_lang_id'";
  317.         if ($aData = mr_query_n_next_record($sql)) {
  318.             $aCatName[$key] = $aData['name'];
  319.         } else {
  320.             $aCatName[$key] = '';
  321.         }
  322.         return $aCatName[$key];
  323.     }
  324.  
  325.  
  326.     /**
  327.      * Get article name from article id and language id
  328.      *
  329.      * @param   int     Article id
  330.      * @param   int     Language id
  331.      * @return  string  Article name
  332.      */
  333.     function get_arttitle($int_id=0, $int_lang_id=0) {
  334.         global $cfg;
  335.  
  336.         $int_id      = (int) $int_id;
  337.         $int_lang_id = (int) $int_lang_id;
  338.  
  339.         $sql = "SELECT title FROM " . $cfg["tab"]["art_lang"] . " WHERE idart = '$int_id' AND idlang = '$int_lang_id'";
  340.         if ($aData = mr_query_n_next_record($sql)) {
  341.             return $aData['title'];
  342.         }
  343.         return '';
  344.     }
  345.  
  346.  
  347.     /**
  348.      * Get language ids from category id
  349.      *
  350.      * @param   int    Category id
  351.      * @return  array  Used language ids
  352.      */
  353.     function get_cat_languages($int_id=0) {
  354.         global $cfg;
  355.  
  356.         $int_id = (int) $int_id;
  357.  
  358.         static $aCatLang;
  359.         if (!isset($aCatLang)) {
  360.             $aCatLang = array();
  361.         } elseif (isset($aCatLang[$int_id])) {
  362.             return $aCatLang[$int_id];
  363.         }
  364.  
  365.         $aLanguages = Array();
  366.  
  367.         $db = new DB_Contenido;
  368.         $sql = "SELECT idlang FROM " . $cfg["tab"]["cat_lang"] . " WHERE idcat = '$int_id'";
  369.         $db->query($sql);
  370.         while ($db->next_record()) {
  371.             $aLanguages[] = $db->f('idlang');
  372.         }
  373.  
  374.         $aCatLang[$int_id] = $aLanguages;
  375.  
  376.         return $aCatLang[$int_id];
  377.     }
  378.  
  379.  
  380.     /**
  381.      * get_artids()
  382.      *
  383.      * get article title, language id
  384.      *
  385.      * @param    integer    idartlang
  386.      * @return     array    title, idlang
  387.      */
  388.     function get_artids($int_id=0) {
  389.         global $cfg;
  390.         $sql = "SELECT urlname, idlang FROM " . $cfg["tab"]["art_lang"] . " WHERE idartlang = '$int_id'";
  391.         if ($aData = mr_query_n_next_record($sql)) {
  392.             return $aData;
  393.         }
  394.         return array();
  395.     }
  396.  
  397.  
  398.     /**
  399.      * build_recursiv_path()
  400.      *
  401.      * build a recursiv path for mod_rewrite rule like
  402.      * server directories ( dir1/dir2/dir3 )
  403.      *
  404.      * @param    integer    latest category id
  405.      * @return     string    linkpath with correct uri
  406.      */
  407.     function build_recursiv_path ( $int_id = 0, $int_lang_id = 0, $int_lastid = 0 ) {
  408.         global $cfg;
  409.  
  410.         $arr_directories = Array();
  411.         $bool_finish = false;
  412.         $int_tmp_idcat = $int_id;
  413.         $str_join_parameter = '/';
  414.  
  415.         while ($bool_finish == false ) {
  416.             $sql = "SELECT cl.urlname, c.parentid
  417.                     FROM " . $cfg["tab"]["cat_lang"] . " cl
  418.                     LEFT JOIN  " . $cfg["tab"]["cat"] . " c
  419.             ON cl.idcat = c.idcat
  420.             WHERE cl.idcat = '$int_tmp_idcat'
  421.             AND cl.idlang = '$int_lang_id'";
  422.             if ($aData = mr_query_n_next_record($sql)) {
  423.                 $arr_directories[] = $aData['urlname'];
  424.                 $int_tmp_idcat     = $aData['parentid'];
  425.  
  426.                 if ($aData['parentid'] == 0 || $aData['parentid'] == $int_lastid) {
  427.                     $bool_finish = true;
  428.                 }
  429.             } else {
  430.                 $bool_finish = true;
  431.             }
  432.         }
  433.  
  434.         // only html files part
  435.             $str_join_parameter = $cfg["mod_rewrite"]['category_seperator'];
  436.         }
  437.  
  438.         // reverse array entries and create directory string
  439.         $str_path = join($str_join_parameter,array_reverse($arr_directories));
  440.  
  441.         return $str_path;
  442.  
  443.     }    // end function
  444.  
  445.  
  446.     /**
  447.      * return full contenido url from single anchor
  448.      *
  449.      * @param array $arr_matches [0] = complete anchor, [1] = pre arguments, [2] = anchor name, [3] = post arguments
  450.      * @return string new anchor
  451.      */
  452.     function rewrite_html_anchor( $arr_matches = array() ) {
  453.         global $parts, $idcat, $idart, $artname, $client, $lang, $sess;
  454.  
  455.         // set article name
  456.         $str_idart = '';
  457.         if (isset ( $artname ) && strlen($artname) > 0 ) {
  458.             $str_idart = '&idart=' . $idart;
  459.         }
  460.  
  461.         // check for additional parameter in url
  462.         $arr_ignored_params = array ( 'idcat', 'idart', 'lang', 'client', 'idcatart', 'changelang', 'changeclient', 'idartlang', 'parts', 'artname' );
  463.         $str_other_params = '';
  464.  
  465.         //print_r ( $arr_matches);
  466.  
  467.         if (isset($_GET) && count($_GET)>0 ) {
  468.             //print_r ( $_GET);
  469.  
  470.             foreach ( $_GET as $str_key => $str_value) {
  471.                 if (!in_array($str_key, $arr_ignored_params) && strlen(trim($str_value)) > 0 ) {
  472.                     $arr_no_anchor = explode("#",$str_value);
  473.  
  474.                     $str_other_params .= '&' . urlencode(urldecode($str_key)) . '=' . urlencode(urldecode($str_value));
  475.                 }
  476.             }
  477.         }
  478.         $str_new_url = '<a' . $arr_matches[1] . 'href="' . $sess->url(
  479.             'front_content.php?' .
  480.             'idcat=' . $idcat .
  481.             '&client=' . $client .
  482.             '&changelang=' . $lang .
  483.         $str_idart .
  484.         $str_other_params .
  485.             '#' . $arr_matches[2]
  486.         ) . '"' . $arr_matches[3] . '>';
  487.  
  488.         return $str_new_url;
  489.     }
  490.  
  491.  
  492.     /**
  493.      * return full contenido url from single anchor
  494.      *
  495.      * @param array $arr_matches [0] = complete anchor, [1] = pre arguments, [2] = anchor name, [3] = post arguments
  496.      * @return string new anchor
  497.      */
  498.     function contenido_html_anchor( $arr_matches = array(), $str_xhtml = true ) {
  499.         global $parts, $idcat, $idart, $artname, $client, $lang, $sess;
  500.  
  501.         $arr_params = array();
  502.         $str_join_parameter = $str_xhtml ? '&amp;' : '&';
  503.  
  504.         foreach ( $_GET as $str_key => $str_value ) {
  505.             $arr_no_anchor = explode("#",$str_value);
  506.             $arr_params[] = urlencode(urldecode($str_key)) . '=' . urlencode(urldecode($arr_no_anchor[0]));
  507.         }
  508.  
  509.         $str_new_url = '<a' . $arr_matches[1] . 'href="' . $sess->url( 'front_content.php?' . implode($str_join_parameter, $arr_params) . '#' . $arr_matches[2] ) . '"' . $arr_matches[3] . '>';
  510.  
  511.         return $str_new_url;
  512.     }
  513.  
  514.  
  515.     /**
  516.      * build_new_url()
  517.      *
  518.      * build new url from given arguments
  519.      *
  520.      * get querystring of front_content.php and
  521.      * convert this url to the new mod_rewrite url
  522.      * method will be startet before the complete
  523.      * output of the front site will be executed
  524.      *
  525.      * @deprecated Functionaliy will be managed by Contenido_UrlBuilder_MR
  526.      *              Is no more used and should removed, Murat Purc
  527.      *
  528.      * @param    string   given arguments
  529.      * @return   string   new url
  530.      * @modified Stefan Seifarth 2005-08-14
  531.      */
  532.     function build_new_url ( $str_args = "", $str_xhtml = true ) {
  533.         global $cfg, $lang, $client;
  534.  
  535.         $str_new_url = "";
  536.         $str_categories = "";
  537.         $str_article = "";
  538.         $arr_args = array();
  539.         $str_join_parameter = "/";
  540.         $str_file_extension = "";
  541.         $arr_parts = array();
  542.  
  543.         // only html files part
  544.             $str_join_parameter = $cfg['mod_rewrite']['category_seperator'];
  545.             $str_file_extension = $cfg['mod_rewrite']['file_extension'];
  546.         }
  547.  
  548.         // check arguments ... and split
  549.         $str_anchor = "";
  550.         // check for anchor
  551.         $arr_args = explode("#", $str_args);
  552.         if (is_array($arr_args) && count($arr_args)>1) {
  553.             $str_args = $arr_args[0];
  554.             $str_anchor = '#' . urlencode(urldecode($arr_args[1]));
  555.         }
  556.  
  557.         $str_args = str_replace('?', '', $str_args);
  558.         $str_args = str_replace('&amp;', '&', $str_args);
  559.         // extract arguments into current scope
  560.         parse_str ($str_args);
  561.  
  562.         // some preparations to avoid too much checks in further code blocks
  563.         $idcat        = (isset($idcat)) ? (int) $idcat : 0;
  564.         $idart        = (isset($idart)) ? (int) $idart : 0;
  565.         $idcatart     = (isset($idcatart)) ? (int) $idcatart : 0;
  566.         $idartlang    = (isset($idartlang)) ? (int) $idartlang : 0;
  567.         $changelang   = (isset($changelang)) ? (int) $changelang : 0;
  568.         $changeclient = (isset($changeclient)) ? (int) $changeclient : 0;
  569.  
  570.         // get additional non contenido parameters
  571.         $str_additional_params = '';
  572.         $arr_additional_params = array();
  573.         $arr_param_pairs = split( "&", $str_args );
  574.  
  575.         $arr_ignored_params = array ( 'idcat', 'idart', 'lang', 'client', 'idcatart', 'changelang', 'changeclient', 'idartlang' );
  576.  
  577.         foreach ( $arr_param_pairs as $str_pair ) {
  578.             $bol_found_bad = false;
  579.             $arr_param = split ( "=", $str_pair );
  580.  
  581.             foreach ( $arr_ignored_params as $str_key ) {
  582.                 if ( $str_key == strtolower(trim($arr_param[0])) ) {
  583.                     $bol_found_bad = true;
  584.                     break;
  585.                 }
  586.             }
  587.  
  588.             if ( $bol_found_bad == false ) {
  589.                 $arr_additional_params[] = urlencode(urldecode($arr_param[0])) . '=' . urlencode(urldecode($arr_param[1]));
  590.             }
  591.         }
  592.  
  593.         if ( count ( $arr_additional_params ) > 0 ) {
  594.             $str_glue = ( $str_xhtml == true ) ? '&amp;' : '&';
  595.             $str_additional_params = '?' . implode ( $str_glue, $arr_additional_params );
  596.         }
  597.  
  598.         $idlang   = ($changelang > 0) ? $changelang : $lang;
  599.         $idclient = ($changeclient > 0) ? $changeclient : $client;
  600.  
  601.         // set client?
  602.         if ( $cfg['mod_rewrite']['use_client'] == 1 ) {
  603.             if ( $cfg['mod_rewrite']['use_client_name'] == 1 ) {
  604.                 $arr_parts[] = urlencode(ModRewrite::get_client_name($idclient));
  605.             } else {
  606.                 $arr_parts[] = $idclient;
  607.             }
  608.         }
  609.  
  610.         // set language?
  611.         if ( $cfg['mod_rewrite']['use_language'] == 1 ) {
  612.             if ( $cfg['mod_rewrite']['use_language_name'] == 1 ) {
  613.                 $arr_parts[] = urlencode(ModRewrite::get_language_name($idlang));
  614.             } else {
  615.                 $arr_parts[] = $idlang;
  616.             }
  617.         }
  618.  
  619.         // define rootpath ...
  620.         $str_new_url = $cfg['mod_rewrite']['rootdir'];
  621.  
  622.         // handle idcatart
  623.         if ($idcatart > 0 && $idcat == 0 && $idart == 0 && $idartlang == 0) {
  624.             $sql = "SELECT idcat, idart FROM " . $cfg["tab"]["cat_art"] . " WHERE idcatart = '$idcatart'";
  625.             if ($aData = mr_query_n_next_record($sql)) {
  626.                 $idcat = $aData['idcat'];
  627.                 $idart = $aData['idart'];
  628.             }
  629.         }
  630.  
  631.         // check if article id is given and set article url
  632.         if ($idart > 0 && $idartlang == 0) {
  633.             $str_article = ModRewrite::get_art_websafename ( $idart, $idlang );
  634.         } elseif ($idartlang > 0 && $idart == 0) {
  635.             // handle idartlang
  636.             $str_article = ModRewrite::get_art_lang_websafename ( $idartlang );
  637.         }
  638.  
  639.         // check if only article id is given, cat id have to rebuild
  640.         if ($idart > 0 && $idcat == 0 && $idartlang == 0) {
  641.             $sql = "SELECT idcat FROM " . $cfg["tab"]["cat_art"] . " WHERE idart = '$idart'";
  642.             if ($aData = mr_query_n_next_record($sql)) {
  643.                 $idcat = $aData['idcat'];
  644.             }
  645.         }
  646.  
  647.         // idartlang is given
  648.         if ($idartlang > 0 && $idcat == 0 && $idart == 0) {
  649.             $sql = "SELECT ca.idcat
  650.                     FROM " . $cfg["tab"]["art_lang"] . " al
  651.                     LEFT JOIN " . $cfg["tab"]["cat_art"] . " ca ON al.idart = ca.idart
  652.             WHERE al.idartlang = '$idartlang'";
  653.             if ($aData = mr_query_n_next_record($sql)) {
  654.                 $idcat = $aData['idcat'];
  655.             }
  656.         }
  657.  
  658.         // we have category but no article name and start article name should be added to url
  659.         if ($idcat > 0 && $str_article == '' && $cfg['mod_rewrite']['add_startart_name_to_url']) {
  660.             if ($cfg['mod_rewrite']['default_startart_name'] == '') {
  661.  
  662.                 // no default article name is configured get startarticle
  663.                 cInclude('classes', 'class.article.php');
  664.                 $artColl = new ArticleCollection(array('idcat' => $idcat, 'start' => 1));
  665.                 if ($artItem = $artColl->startArticle()) {
  666.                     $idart       = $artItem->get('idart');
  667.                     $str_article = ModRewrite::get_art_websafename($idart, $idlang);
  668.                 }
  669.  
  670.             } else {
  671.  
  672.                 // use default start article name
  673.                 $str_article = $cfg['mod_rewrite']['default_startart_name'];
  674.             }
  675.         }
  676.  
  677.         if ($str_article) {
  678.             $str_file_extension = $cfg['mod_rewrite']['file_extension'];
  679.         }
  680.  
  681.         // ok build dir list, if idcat found ...
  682.         if ($idcat > 0) {
  683.  
  684.             $str_categories = ModRewrite::build_recursiv_path ( $idcat, $idlang );
  685.  
  686.             // check start directory settings
  687.             if ( $cfg['mod_rewrite']['startfromroot'] == 0 ) {
  688.                 // splitt string in array
  689.                 $arr_categories = split( $str_join_parameter, $str_categories);
  690.  
  691.                 // remove first category
  692.                 $str_first_cat = array_shift ( $arr_categories );
  693.  
  694.                 // implode array with categories to new string
  695.                 $str_categories = join ( $str_join_parameter,  $arr_categories );
  696.             }
  697.  
  698.             if (strlen($str_categories)> 0) {
  699.                 $arr_parts[] = $str_categories;
  700.             }
  701.         }
  702.         $str_parts = implode( $str_join_parameter, $arr_parts);
  703.  
  704.         // only html files part
  705.         if (strlen($str_parts) > 0 && strlen($str_article) > 0
  706.             $str_parts.= $cfg['mod_rewrite']['article_seperator'];
  707.         } else if ((int)$cfg['mod_rewrite']['use_categories_as_html_file'] != 1 && strlen($str_parts) > 0) {
  708.             $str_parts.= $str_join_parameter;
  709.         }
  710.  
  711.         // using lowercase uri?
  712.         if ( $cfg['mod_rewrite']['use_lowercase_uri'] == 1 ) {
  713.             $str_full_url = $str_new_url . strtolower( $str_parts . $str_article . $str_file_extension ) . $str_additional_params . $str_anchor;
  714.         } else {
  715.             $str_full_url = $str_new_url . $str_parts . $str_article . $str_file_extension . $str_additional_params . $str_anchor;
  716.         }
  717.  
  718.         // remove double or more slashes
  719.         $str_full_url = preg_replace('#' . $str_join_parameter .'{2,}#', $str_join_parameter, $str_full_url);
  720.  
  721.         if (substr ($str_full_url, -2) == "?=") {
  722.             $str_full_url = substr_replace($str_full_url, "", -2);
  723.         }
  724.  
  725.         return $str_full_url;
  726.  
  727.     } // end function
  728.  
  729.  
  730.     /**
  731.      * get_art_websafename()
  732.      *
  733.      * get article websafe name from article id and language id
  734.      *
  735.      * @param    integer    article id
  736.      * @param    integer    language id
  737.      * @return     string    article websafe name
  738.      */
  739.     function get_art_websafename($int_id=0, $int_lang_id=0) {
  740.         global $cfg;
  741.         $sql = "SELECT urlname FROM " . $cfg["tab"]["art_lang"] . " WHERE idart = '$int_id' AND idlang = '$int_lang_id'";
  742.         if ($aData = mr_query_n_next_record($sql)) {
  743.             return urldecode($aData['urlname']);
  744.         }
  745.         return null;
  746.     }    // end function
  747.  
  748.  
  749.     /**
  750.      * get_art_lang_websafename()
  751.      *
  752.      * get article websafe name from idartlang
  753.      *
  754.      * @param    integer    idartlang
  755.      * @return     string    article websafe name
  756.      */
  757.     function get_art_lang_websafename($int_id=0) {
  758.         global $cfg;
  759.         $sql = "SELECT urlname FROM " . $cfg["tab"]["art_lang"] . " WHERE idartlang = '$int_id'";
  760.         if ($aData = mr_query_n_next_record($sql)) {
  761.             return urldecode($aData['urlname']);
  762.         }
  763.         return null;
  764.     }    // end function
  765.  
  766.  
  767.     /**
  768.      * Get name of client by id
  769.      *
  770.      * @param    int        $clientId  Client id
  771.      * @return     string    Client name
  772.      */
  773.     function get_client_name($clientId=0) {
  774.         global $cfg;
  775.         $clientId = (int) $clientId;
  776.  
  777.         static $aClientName;
  778.         if (!isset($aClientName)) {
  779.             $aClientName = array();
  780.         } elseif (isset($aClientName[$clientId])) {
  781.             return $aClientName[$clientId];
  782.         }
  783.  
  784.         $sql = "SELECT name FROM " . $cfg["tab"]["clients"] . " WHERE idclient = '$clientId'";
  785.         if ($aData = mr_query_n_next_record($sql)) {
  786.             $aClientName[$clientId] = $aData['name'];
  787.         } else {
  788.             $aClientName[$clientId] = '';
  789.         }
  790.         return $aClientName[$clientId];
  791.     }
  792.  
  793.  
  794.     /**
  795.      * Returns name of language by id
  796.      *
  797.      * @param   int     $languageId  Language id
  798.      * @return  string  Lanuage name
  799.      */
  800.     function get_language_name($languageId=0) {
  801.         global $cfg;
  802.         $languageId = (int) $languageId;
  803.  
  804.         static $aLanguageName;
  805.         if (!isset($aLanguageName)) {
  806.             $aLanguageName = array();
  807.         } elseif (isset($aLanguageName[$languageId])) {
  808.             return $aLanguageName[$languageId];
  809.         }
  810.  
  811.         $sql = "SELECT name FROM " . $cfg["tab"]["lang"] . " WHERE idlang = '$languageId'";
  812.         if ($aData = mr_query_n_next_record($sql)) {
  813.             $aLanguageName[$languageId] = $aData['name'];
  814.         } else {
  815.             $aLanguageName[$languageId] = '';
  816.         }
  817.         return $aLanguageName[$languageId];
  818.     }
  819.  
  820.  
  821.     /**
  822.      * get_client_full_url_parts()
  823.      * Splits passed argument into scheme://host and path/query.
  824.      *
  825.      * Example:
  826.      * input  = http://host/front_content.php?idcat=123
  827.      * return = array('htmlpath' => 'http://host', 'url' => 'front_content.php?idcat=123')
  828.      *
  829.      * @param  string  $url  URL to split
  830.      * @return array  Assoziative array including the two parts:
  831.      *                 - array('htmlpath' => $path, 'url' => $url)
  832.      */
  833.     function get_client_full_url_parts($url) {
  834.         global $cfg, $cfgClient, $client;
  835.         $clientPath = $cfgClient[$client]['path']['htmlpath'];
  836.  
  837.         if (stristr($url, $clientPath) !== false) {
  838.  
  839.             // url includes full html path (scheme host path, etc.)
  840.             $url      = str_replace($clientPath, '', $url);
  841.             $htmlPath = $clientPath;
  842.             $aComp    = parse_url($htmlPath);
  843.  
  844.             // check if path matches to defined rootdir from mod_rewrite conf
  845.             if (isset($aComp['path']) && $aComp['path'] !== $cfg['mod_rewrite']['rootdir']) {
  846.                 // replace not matching path agaings configured one
  847.                 // this will replace e. g. "http://host/cms/" against "http://host/"
  848.                 $htmlPath = str_replace($aComp['path'], $cfg['mod_rewrite']['rootdir'], $htmlPath);
  849.                 if (substr($htmlPath, strlen($htmlPath)-1) == '/') {
  850.                     // remove last slash
  851.                     $htmlPath = substr($htmlPath, 0, strlen($htmlPath)-1);
  852.                 }
  853.             }
  854.         } else {
  855.             $htmlPath = '';
  856.         }
  857.         return array('htmlpath' => $htmlPath, 'url' => $url);
  858.     }
  859.  
  860.  
  861.     /**
  862.      * reset_categories_aliases()
  863.      *
  864.      * method to reset all aliases in categories
  865.      */
  866.     function reset_categories_aliases() {
  867.         global $cfg;
  868.  
  869.         $db = new DB_Contenido();
  870.  
  871.         // empty all aliases
  872.         $sql = "UPDATE " . $cfg["tab"]["cat_lang"] . " SET urlname = ''";
  873.         $db->query($sql);
  874.  
  875.         // get all categories
  876.         $sql = "SELECT name, idcat, idlang FROM " . $cfg["tab"]["cat_lang"];
  877.         $db->query($sql);
  878.  
  879.         while ( $db->next_record() ) {
  880.             //set new alias
  881.             ModRewrite::set_category($db->f('name'), $db->f('idcat'), $db->f('idlang'));
  882.         }
  883.         unset ($db);
  884.  
  885.         mr_resetPathResolverCache();
  886.     }
  887.  
  888.  
  889.     /**
  890.      * reset_articles_aliases()
  891.      *
  892.      * method to reset all aliases in articles
  893.      */
  894.     function reset_articles_aliases () {
  895.         global $cfg;
  896.  
  897.         $db = new DB_Contenido();
  898.  
  899.         // empty all aliases
  900.         $sql = "UPDATE " . $cfg["tab"]["art_lang"] . " SET urlname = ''";
  901.         $db->query($sql);
  902.  
  903.         // get all articles
  904.         $sql = "SELECT title, idart, idlang FROM " . $cfg["tab"]["art_lang"];
  905.         $db->query($sql);
  906.  
  907.         while ( $db->next_record() ) {
  908.             //set new alias
  909.             ModRewrite::set_article($db->f('title'), $db->f('idart'), $db->f('idlang'));
  910.         }
  911.  
  912.         unset ($db);
  913.     }
  914.  
  915.     /**
  916.      * reset_aliases()
  917.      *
  918.      * method to reset all aliases (category and article)
  919.      */
  920.     function reset_aliases () {
  921.         mr_resetPathResolverCache();
  922.     }
  923.  
  924.  
  925.     /**
  926.      * Used to postprocess resolved path
  927.      *
  928.      * Error site handling if category not found
  929.      *
  930.      * if percentage == 100 and there is no 100 percentage result value,
  931.      * error site will be shown - can be adjust by user settings for
  932.      * smooth similar effects - 80 to 95 will be best but have to check by user
  933.      *
  934.      * @param   array  $results  Pathresolver results array
  935.      * @return  mixed  Categoryid or false
  936.      */
  937.     function get_id_from_pathresolver_result($results) {
  938.         global $cfg;
  939.  
  940.         $intMinPercentage = $cfg["mod_rewrite"]['category_resolve_min_percentage'];
  941.         $catId = key($results);
  942.  
  943.         if (isset($intMinPercentage) && (int)$intMinPercentage > 0 && $results[$catId] < $intMinPercentage) {
  944.             return false;
  945.         } else {
  946.             return $catId;
  947.         }
  948.     }
  949.  
  950.  
  951.     /**
  952.      * Analyses the settings for usage of categories as a html file and returns
  953.      * the result as a boolean
  954.      *
  955.      * @return  bool  True if settings are active and valid otherwhise false.
  956.      */
  957.         global $cfg;
  958.  
  959.         if ((int) $cfg["mod_rewrite"]['use_categories_as_html_file'] == 1
  960.             && strlen($cfg["mod_rewrite"]['article_seperator']) > 0
  961.             && strlen($cfg["mod_rewrite"]['category_seperator']) > 0
  962.             && $cfg["mod_rewrite"]['category_seperator'] != $cfg["mod_rewrite"]['article_seperator']) {
  963.             // settings are ok, return true
  964.             return true;
  965.         } else {
  966.             // settings are are deactivated or faulty
  967.             return false;
  968.         }
  969.     }
  970.  
  971. } // end class

Documentation generated on Mon, 08 Sep 2008 03:08:37 +0200 by phpDocumentor 1.4.0