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

Documentation generated on Tue, 12 Aug 2008 00:06:14 +0200 by phpDocumentor 1.4.0