Source for file class.modrewrite.php

Documentation is available at class.modrewrite.php

  1. <?php
  2. /**
  3.  * Includes 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.  * @author      Stefan Seifarth / stese
  36.  * @author      Murat Purc <murat@purc.de>
  37.  * @package     Contenido
  38.  * @subpackage  ModRewrite
  39.  */
  40. class ModRewrite extends ModRewriteBase {
  41.  
  42.  
  43.     /**
  44.      * Initialization, is to call at least once, also possible to call multible
  45.      * times, if different client configuration is to load.
  46.      *
  47.      * Loads configuration of passed client and sets some properties.
  48.      *
  49.      * @param  int  $clientId  Client id
  50.      */
  51.     public static function initialize($clientId) {
  52.         mr_loadConfiguration($clientId, true);
  53.         parent::initialize();
  54.     }
  55.  
  56.  
  57.     /**
  58.      * Check categories on websafe name
  59.      *
  60.      * Check all categories in the main parent category on existing same websafe name
  61.      *
  62.      * @param   string  $sName    Websafe name to check
  63.      * @param   int     $iCatId   Current category id
  64.      * @param   int     $iLangId  Current language id
  65.      * @return     bool    True if websafename already exists, false if not
  66.      */
  67.     public static function isInCategories($sName='', $iCatId=0, $iLangId=0) {
  68.         $iCatId  = (int) $iCatId;
  69.         $iLangId = (int) $iLangId;
  70.  
  71.         $aTab = parent::$_oGlobals->get('cfg/tab');
  72.  
  73.         // get parentid
  74.         $iParentId = 0;
  75.         $sql = "SELECT parentid FROM " . $aTab['cat'] . " WHERE idcat = '$iCatId'";
  76.         if ($aData = mr_queryAndNextRecord($sql)) {
  77.             $iParentId = ($aData['parentid'] > 0 ) ? (int) $aData['parentid'] : 0;
  78.         }
  79.  
  80.         // check if websafe name is in this category
  81.         $sql = "SELECT count(cl.idcat) as numcats FROM " . $aTab['cat_lang'] . " cl "
  82.              . "LEFT JOIN " . $aTab['cat'] . " c ON cl.idcat = c.idcat WHERE "
  83.              . "c.parentid = '$iParentId' AND cl.idlang = '" . $iLangId . "' AND "
  84.              . "cl.urlname = '" . $sName . "' AND cl.idcat <> '$iCatId'";
  85.         self::$_oDebug->log($sql, 'ModRewrite::isInCategories $sql');
  86.  
  87.         if ($aData = mr_queryAndNextRecord($sql)) {
  88.             return ($aData['numcats'] > 0) ? true : false;
  89.         }
  90.  
  91.         return false;
  92.     }
  93.  
  94.  
  95.     /**
  96.      * Check articles on websafe name.
  97.      *
  98.      * Check all articles in the current category on existing same websafe name.
  99.      *
  100.      * @param    string  $sName    Websafe name to check
  101.      * @param    int     $iArtId   Current article id
  102.      * @param    int     $iLangId  Current language id
  103.      * @param   int     $iCatId   Category id
  104.      * @return     bool    True if websafename already exists, false if not
  105.      */
  106.     public static function isInCatArticles($sName='', $iArtId=0, $iLangId=0, $iCatId=0) {
  107.         $iArtId  = (int) $iArtId;
  108.         $iLangId = (int) $iLangId;
  109.         $iCatId  = (int) $iCatId;
  110.  
  111.         $aTab = parent::$_oGlobals->get('cfg/tab');
  112.  
  113.         // handle multipages
  114.         if ($iCatId == 0) {
  115.             // get category id if not set
  116.             $sql = "SELECT idcat FROM " . $aTab['cat_art'] . " WHERE idart = '$iArtId'";
  117.             if ($aData = mr_queryAndNextRecord($sql)) {
  118.                 $iCatId = ($aData['idcat'] > 0) ? (int) $aData['idcat'] : 0;
  119.             }
  120.         }
  121.  
  122.         // check if websafe name is in this category
  123.         $sql = "SELECT count(al.idart) as numcats FROM " . $aTab['art_lang'] . " al "
  124.              . "LEFT JOIN " . $aTab['cat_art'] . " ca ON al.idart = ca.idart WHERE "
  125.              . " ca.idcat='$iCatId' AND al.idlang='" . $iLangId . "' AND "
  126.              . "al.urlname='" . $sName . "' AND al.idart <> '$iArtId'";
  127.         if ($aData = mr_queryAndNextRecord($sql)) {
  128.             return ($aData['numcats'] > 0) ? true : false;
  129.         }
  130.  
  131.         return false;
  132.     }
  133.  
  134.  
  135.     /**
  136.      * Set websafe name in article list.
  137.      *
  138.      * Insert new websafe name in article list
  139.      *
  140.      * @param   string  $sName    Original name (will be converted)
  141.      * @param   int     $iArtId   Current article id
  142.      * @param   int     $iLangId  Current language id
  143.      * @param   int     $iCatId   Category id
  144.      * @return     bool    True if insert was successfully
  145.      */
  146.     public static function setArtWebsafeName($sName='', $iArtId=0, $iLangId=0, $iCatId=0) {
  147.         $iArtId  = (int) $iArtId;
  148.         $iLangId = (int) $iLangId;
  149.         $iCatId  = (int) $iCatId;
  150.  
  151. ##++##        $sArtWordSep = parent::getConfig('article_word_seperator');
  152.         $sArtWordSep = '-';
  153.  
  154.         // get websafe name
  155.         $sNewName = capiStrCleanURLCharacters(html_entity_decode($sName));
  156.  
  157.         // pre check
  158. ##++##        $sNewName = str_replace(parent::getConfig('article_seperator'), $sArtWordSep, $sNewName);
  159.  
  160.         // article word separator
  161.         if ($sArtWordSep !== '') {
  162.             if ($sArtWordSep !== '-') {
  163.                 // replace set '-' by capiStrCleanURLCharacters against configured one
  164.                 $sNewName = str_replace('-', $sArtWordSep, $sNewName);
  165.             }
  166.  
  167.             // remove double or more separators
  168.             $sNewName = mr_removeMultipleChars($sArtWordSep, $sNewName);
  169.         }
  170.  
  171.         // check if websafe name already exists
  172.         if (self::isInCatArticles($sNewName, $iArtId, $iLangId, $iCatId)) {
  173.             // create new websafe name if exists
  174.             $sNewName = $sNewName . $iArtId;
  175.         }
  176.  
  177.         // check again - and set name
  178.         if (!self::isInCatArticles($sNewName, $iArtId, $iLangId, $iCatId)) {
  179.             // insert websafe name in article list
  180.             $db  = new DB_Contenido;
  181.             $tab = parent::$_oGlobals->get('cfg/tab/art_lang');
  182.             $sql = "UPDATE " . $tab . " SET urlname = '$sNewName' WHERE idart = '$iArtId' AND idlang = '$iLangId'";
  183.             return $db->query($sql);
  184.         } else {
  185.             return false;
  186.         }
  187.     }
  188.  
  189.  
  190.     /**
  191.      * Set websafe name in category list.
  192.      *
  193.      * Insert new websafe name in category list.
  194.      *
  195.      * @param   string  $sName    Original name (will be converted) or alias
  196.      * @param   int     $iCatId   Category id
  197.      * @param   int     $iLangId  Language id
  198.      * @return  bool    True if insert was successfully
  199.      */
  200.     public static function setCatWebsafeName($sName='', $iCatId=0, $iLangId=0) {
  201.         $iCatId  = (int) $iCatId;
  202.         $iLangId = (int) $iLangId;
  203.  
  204. ##++##        $sCatWordSep = parent::getConfig('category_word_seperator');
  205.         $sCatWordSep = '-';
  206.  
  207.         // create websafe name
  208.         $sNewName = capiStrCleanURLCharacters(html_entity_decode($sName));
  209.  
  210.         // some prechecks
  211. ##++##        $sNewName = str_replace(parent::getConfig('category_seperator'), $sCatWordSep, $sNewName);
  212. ##++##        $sNewName = str_replace(parent::getConfig('article_seperator'), $sCatWordSep, $sNewName);
  213.  
  214.         // category word separator
  215.         if (strlen(trim($sCatWordSep)) > 0) {
  216.             if ($sCatWordSep !== '-') {
  217.                 // replace set '-' by capiStrCleanURLCharacters against configured one
  218.                 $sNewName = str_replace('-', $sCatWordSep, $sNewName);
  219.             }
  220.  
  221.             // remove double or more separators
  222.             $sNewName = mr_removeMultipleChars($sCatWordSep, $sNewName);
  223.         }
  224.  
  225.         // check if websafe name already exists
  226.         if (self::isInCategories($sNewName, $iCatId, $iLangId)) {
  227.             // create new websafe name if exists
  228.             $sNewName = $sNewName . $iCatId;
  229.         }
  230.  
  231.         // check again - and set name
  232.         if (!self::isInCategories($sNewName, $iCatId, $iLangId)) {
  233.             // insert websafe name in article list
  234.             $db  = new DB_Contenido;
  235.             $tag = parent::$_oGlobals->get('cfg/tab/cat_lang');
  236.             $sql = "UPDATE " . $tag . " SET urlname = '$sNewName' WHERE idcat = '$iCatId' AND idlang = '$iLangId'";
  237.  
  238.             self::$_oDebug->log(array(
  239.                 'sName'    => $sName,
  240.                 'iCatId'   => $iCatId,
  241.                 'iLangId'  => $iLangId,
  242.                 'sNewName' => $sNewName
  243.                 ), 'ModRewrite::setCatWebsafeName $data'
  244.             );
  245.  
  246.             return $db->query($sql);
  247.  
  248.         } else {
  249.             return false;
  250.         }
  251.     }
  252.  
  253.  
  254.     /**
  255.      * Set urlpath of category
  256.      *
  257.      * @param   int     $iCatId   Category id
  258.      * @param   int     $iLangId  Language id
  259.      * @return  bool    True if insert was successfully
  260.      */
  261.     public static function setCatUrlPath($iCatId=0, $iLangId=0) {
  262.         $sPath = ModRewrite::buildRecursivPath($iCatId, $iLangId);
  263.  
  264.         // insert websafe name in article list
  265.         $db  = new DB_Contenido;
  266.         $tab = parent::$_oGlobals->get('cfg/tab/cat_lang');
  267.         $sql = "UPDATE " . $tab . " SET urlpath = '$sPath' WHERE idcat = '$iCatId' AND idlang = '$iLangId'";
  268.  
  269.         self::$_oDebug->log(array(
  270.             'iCatId'   => $iCatId,
  271.             'iLangId'  => $iLangId,
  272.             'sPath'    => $sPath
  273.             ), 'ModRewrite::setCatUrlPath $data'
  274.         );
  275.  
  276.         return $db->query($sql);
  277.     }
  278.  
  279.  
  280.     /**
  281.      * Get article id and language id from article language id
  282.      *
  283.      * @param   int    $iArtlangId  Current article id
  284.      * @return  array  Array with idart and idlang of current article
  285.      */
  286.     public static function getArtIdByArtlangId($iArtlangId=0) {
  287.         $iArtlangId = (int) $iArtlangId;
  288.         $tab = parent::$_oGlobals->get('cfg/tab/art_lang');
  289.         $sql = "SELECT idart, idlang FROM " . $tab . " WHERE idartlang = '$iArtlangId'";
  290.         if ($aData = mr_queryAndNextRecord($sql)) {
  291.             return $aData;
  292.         }
  293.         return array();
  294.     }
  295.  
  296.  
  297.     /**
  298.      * Get article id by article websafe name
  299.      *
  300.      * @param   string  Websafe name
  301.      * @param   int     Category id
  302.      * @return  int     Recent article id
  303.      */
  304.     function getArtIdByWebsafeName($sArtName='', $iCatId=0, $iLangId=0) {
  305.         static $db;
  306.  
  307.         if (!isset($db)) {
  308.             $db = new DB_Contenido;
  309.         }
  310.  
  311.         $aTab = parent::$_oGlobals->get('cfg/tab');
  312.  
  313.         $iArtId = false;
  314.  
  315.         $sWhere = '';
  316.         if ((int) $iLangId !== 0) {
  317.             $sWhere = ' AND al.idlang=' . $iLangId;
  318.         }
  319.         // only article name were given
  320.         if ($iCatId == 0) {
  321.             // get all basic category ids with parentid=0
  322.             $aCatIds = array();
  323.             $sql = "SELECT idcat FROM " . $aTab['cat'] . " WHERE parentid = '0'";
  324.             $db->query($sql);
  325.             while ($db->next_record()) {
  326.                 $aCatIds[] = "idcat = '" . $db->f('idcat') . "'";
  327.             }
  328.             $sWhere .= " AND ( " . join(" OR ", $aCatIds) . ")";
  329.         } else {
  330.             $sWhere .= " AND ca.idcat = '$iCatId'";
  331.         }
  332.  
  333.         $sql = "SELECT al.idart FROM " . $aTab['art_lang'] . " al "
  334.              . "LEFT JOIN " . $aTab['cat_art'] . " ca ON al.idart = ca.idart "
  335.              . "WHERE al.urlname = '$sArtName'" . $sWhere;
  336.  
  337.         if ($aData = mr_queryAndNextRecord($sql)) {
  338.             $iArtId = $aData['idart'];
  339.         }
  340.  
  341.         return $iArtId;
  342.     }
  343.  
  344.  
  345.     /**
  346.      * Get category name from category id and language id.
  347.      *
  348.      * @param   int     $iCatId   Category id
  349.      * @param   int     $iLangId  Language id
  350.      * @return  string  Category name
  351.      */
  352.     public static function getCatName($iCatId=0, $iLangId=0) {
  353.         $iCatId  = (int) $iCatId;
  354.         $iLangId = (int) $iLangId;
  355.         $key     = $iCatId . '-' . $iLangId;
  356.  
  357.         $key = 'mr_statics/catname_by_catid_idlang/' . $iCatId . '_' . $iLangId;
  358.         $catName = parent::$_oGlobals->get($key);
  359.         if ($catName !== null) {
  360.             return $catName;
  361.         }
  362.  
  363.         $tab = parent::$_oGlobals->get('cfg/tab/cat_lang');
  364.         $sql = "SELECT name FROM " . $tab . " WHERE idcat = '$iCatId' AND idlang = '$iLangId'";
  365.         if ($aData = mr_queryAndNextRecord($sql)) {
  366.             $catName = $aData['name'];
  367.         } else {
  368.             $catName = '';
  369.         }
  370.  
  371.         parent::$_oGlobals->set($key, $catName);
  372.         return $catName;
  373.     }
  374.  
  375.  
  376.     /**
  377.      * Funcion to return cat id by path.
  378.      *
  379.      * Caches the paths at first call to provode faster processing at further calls.
  380.      *
  381.      * @param   string  $path  Category path
  382.      * @return  int  Category id
  383.      */
  384.     public static function getCatIdByUrlPath($path) {
  385.         if (strpos($path, '/') === 0) {
  386.             $path = substr($path, 1);
  387.         }
  388.         if (strrpos($path, '/') === strlen($path)-1) {
  389.             $path = substr($path, 0, -1);
  390.         }
  391.  
  392. ##++##        $catSeperator = parent::getConfig('category_seperator');
  393.         $catSeperator = '/';
  394.         $startFromRoot = parent::getConfig('startfromroot');
  395.  
  396.         $path = str_replace('/', parent::getConfig('category_seperator'), $path);
  397.  
  398.         $key = 'mr_statics/cat_ids_and_urlpath';
  399.         $aPathsCache = parent::$_oGlobals->get($key, array());
  400.  
  401.         if (count($aPathsCache) == 0) {
  402.             $aTab   = parent::$_oGlobals->get('cfg/tab');
  403.             $client = parent::$_oGlobals->get('client');
  404.             $lang   = parent::$_oGlobals->get('lang');
  405.             $db     = new DB_Contenido();
  406.  
  407.             $sql = "SELECT cl.idcat, cl.urlpath FROM " . $aTab['cat_lang'] . " AS cl, " . $aTab['cat'] . " AS c WHERE "
  408.                  . "c.idclient = " . $client . " AND c.idcat = cl.idcat AND cl.idlang = " . $lang;
  409.             $db->query($sql);
  410.             while ($db->next_record()) {
  411.                 $urlPath = $db->f('urlpath');
  412.                 if ($startFromRoot == 0 && strpos($urlPath, $catSeperator) > 0) {
  413.                     // paths are stored with prefixed main category, but created
  414.                     // urls doesn't contain the main cat, remove it...
  415.                     $urlPath = substr($urlPath, strpos($urlPath, $catSeperator)+1);
  416.                 }
  417.  
  418.                 // store path
  419.                 $aPathsCache[$db->f('idcat')] = $urlPath;
  420.             }
  421.         }
  422.         parent::$_oGlobals->set($key, $aPathsCache);
  423.  
  424.         // compare paths using the similar_text algorithm
  425.         $fPercent = 0;
  426.         $aResults = array();
  427.         foreach ($aPathsCache as $id => $pathItem) {
  428.             similar_text($path, $pathItem, $fPercent);
  429.             $aResults[$id] = $fPercent;
  430.         }
  431.  
  432.         arsort($aResults, SORT_NUMERIC);
  433.         reset($aResults);
  434.  
  435.         parent::$_oDebug->addDebug($path, 'ModRewrite::getCatIdByUrlPath() $path');
  436.         parent::$_oDebug->addDebug($aPathsCache, 'ModRewrite::getCatIdByUrlPath() $aPathsCache');
  437.         parent::$_oDebug->addDebug($aResults, 'ModRewrite::getCatIdByUrlPath() $aResults');
  438.  
  439.         $iMinPercentage = (int) parent::getConfig('category_resolve_min_percentage', 0);
  440.         $catId = key($aResults);
  441.         if ($iMinPercentage > 0 && $aResults[$catId] < $iMinPercentage) {
  442.             return 0;
  443.         } else {
  444.             return $catId;
  445.         }
  446.     }
  447.  
  448.     /**
  449.      * Get article name from article id and language id
  450.      *
  451.      * @NOTE: seems to be not used???
  452.      *
  453.      * @param   int     $iArtId   Article id
  454.      * @param   int     $iLangId  Language id
  455.      * @return  string  Article name
  456.      */
  457.     public static function getArtTitle($iArtId=0, $iLangId=0) {
  458.         $iArtId  = (int) $iArtId;
  459.         $iLangId = (int) $iLangId;
  460.  
  461.         $tab = parent::$_oGlobals->get('cfg/tab/art_lang');
  462.  
  463.         $sql = "SELECT title FROM " . $tab . " WHERE idart = '$iArtId' AND idlang = '$iLangId'";
  464.         if ($aData = mr_queryAndNextRecord($sql)) {
  465.             return $aData['title'];
  466.         }
  467.         return '';
  468.     }
  469.  
  470.  
  471.     /**
  472.      * Get language ids from category id
  473.      *
  474.      * @param   int    $iCatId  Category id
  475.      * @return  array  Used language ids
  476.      */
  477.     public static function getCatLanguages($iCatId=0) {
  478.         $iCatId = (int) $iCatId;
  479.  
  480.         $key = 'mr_statics/cat_idlang_by_catid/' . $iCatId;
  481.         $aLanguages = parent::$_oGlobals->get($key);
  482.         if ($aLanguages !== null) {
  483.             return $aLanguages;
  484.         }
  485.  
  486.         $aLanguages = array();
  487.  
  488.         $db  = new DB_Contenido();
  489.         $tab = parent::$_oGlobals->get('cfg/tab/cat_lang');
  490.         $sql = "SELECT idlang FROM " . $tab . " WHERE idcat = '$iCatId'";
  491.         $db->query($sql);
  492.         while ($db->next_record()) {
  493.             $aLanguages[] = $db->f('idlang');
  494.         }
  495.  
  496.         parent::$_oGlobals->set($key, $aLanguages);
  497.         return $aLanguages;
  498.     }
  499.  
  500.  
  501.     /**
  502.      * Get article urlname and language id
  503.      *
  504.      * @param   int    $iArtlangId  idartlang
  505.      * @return  array  Urlname, idlang of empty array
  506.      */
  507.     public static function getArtIds($iArtlangId=0) {
  508.         $iArtlangId = (int) $iArtlangId;
  509.         $tab = parent::$_oGlobals->get('cfg/tab/art_lang');
  510.         $sql = "SELECT urlname, idlang FROM " . $tab . " WHERE idartlang = '$iArtlangId'";
  511.         if ($aData = mr_queryAndNextRecord($sql)) {
  512.             return $aData;
  513.         }
  514.         return array();
  515.     }
  516.  
  517.  
  518.     /**
  519.      * Build a recursiv path for mod_rewrite rule like server directories
  520.      * (dir1/dir2/dir3)
  521.      *
  522.      * @param   int     $iCatId   Latest category id
  523.      * @param   int     $iLangId  Language id
  524.      * @param   int     $iLastId  Last category id
  525.      * @return     string    linkpath with correct uri
  526.      */
  527.     public static function buildRecursivPath($iCatId=0, $iLangId=0, $iLastId=0) {
  528.         $aDirectories   = array();
  529.         $bFinish        = false;
  530.         $iTmpCatId      = $iCatId;
  531.  
  532.         $aTabs = parent::$_oGlobals->get('cfg/tab');
  533.  
  534.         while ($bFinish == false) {
  535.             $sql = "SELECT cl.urlname, c.parentid FROM " . $aTabs['cat_lang'] . " cl "
  536.                  . "LEFT JOIN " . $aTabs['cat'] . " c ON cl.idcat = c.idcat "
  537.                  . "WHERE cl.idcat = '$iTmpCatId' AND cl.idlang = '$iLangId'";
  538.             if ($aData = mr_queryAndNextRecord($sql)) {
  539.                 $aDirectories[] = $aData['urlname'];
  540.                 $iTmpCatId      = $aData['parentid'];
  541.  
  542.                 if ($aData['parentid'] == 0 || $aData['parentid'] == $iLastId) {
  543.                     $bFinish = true;
  544.                 }
  545.             } else {
  546.                 $bFinish = true;
  547.             }
  548.         }
  549.  
  550. ##++##        $sPathSeparator = parent::getConfig('category_seperator');
  551.         $sPathSeparator = '/';
  552.  
  553.         // reverse array entries and create directory string
  554.         $sPath = join($sPathSeparator, array_reverse($aDirectories));
  555.  
  556.         return $sPath;
  557.     }
  558.  
  559.  
  560.     /**
  561.      * Return full contenido url from single anchor
  562.      *
  563.      * @param   array   $aMatches [0] = complete anchor, [1] = pre arguments, [2] = anchor name, [3] = post arguments
  564.      * @return  string  New anchor
  565.      */
  566.     public static function rewriteHtmlAnchor(array $aMatches=array()) {
  567.         $artname = parent::$_oGlobals->get('artname');
  568.         $sess    = parent::$_oGlobals->get('sess');
  569.  
  570.         // set article name
  571.         $sArtParam = '';
  572.         if (isset($artname) && strlen($artname) > 0) {
  573.             $sArtParam = '&idart=' . parent::$_oGlobals->get('idart');
  574.         }
  575.  
  576.         // check for additional parameter in url
  577.         $aParamsToIgnore = array ('idcat', 'idart', 'lang', 'client', 'idcatart', 'changelang', 'changeclient', 'idartlang', 'parts', 'artname');
  578.         $sOtherParams = '';
  579.  
  580.         if (isset($_GET) && count($_GET) > 0) {
  581.             foreach ($_GET as $key => $value) {
  582.                 if (!in_array($key, $aParamsToIgnore) && strlen(trim($value)) > 0) {
  583.                     $aNoAnchor     = explode('#', $value);
  584.                     $sOtherParams .= '&' . urlencode(urldecode($key)) . '=' . urlencode(urldecode($value));
  585.                 }
  586.             }
  587.         }
  588.  
  589.         $idcat  = parent::$_oGlobals->get('idcat');
  590.         $client = parent::$_oGlobals->get('client');
  591.         $lang   = parent::$_oGlobals->get('lang');
  592.  
  593.         $url = $sess->url(
  594.             'front_content.php?' . 'idcat=' . $idcat . '&client=' . $client .
  595.             '&changelang=' . $lang . $sArtParam . $sOtherParams . '#' . $aMatches[2]
  596.         );
  597.  
  598.         $sNewUrl = '<a' . $aMatches[1] . 'href="' . $url . '"' . $aMatches[3] . '>';
  599.  
  600.         return $sNewUrl;
  601.     }
  602.  
  603.  
  604.     /**
  605.      * Return full contenido url from single anchor
  606.      *
  607.      * @param   array   $aMatches [0] = complete anchor, [1] = pre arguments, [2] = anchor name, [3] = post arguments
  608.      * @return  string  New anchor
  609.      */
  610.     public static function contenidoHtmlAnchor(array $aMatches=array(), $bXHTML=true) {
  611.         $artname = parent::$_oGlobals->get('artname');
  612.         $sess    = parent::$_oGlobals->get('sess');
  613.  
  614.         $aParams    = array();
  615.         $sAmpersand = $bXHTML ? '&amp;' : '&';
  616.  
  617.         foreach ($_GET as $key => $value) {
  618.             $aNoAnchor = explode('#', $value);
  619.             $aParams[] = urlencode(urldecode($key)) . '=' . urlencode(urldecode($aNoAnchor[0]));
  620.         }
  621.  
  622.         $url =  $sess->url( 'front_content.php?' . implode($sAmpersand, $aParams) . '#' . $aMatches[2]);
  623.         $sNewUrl = '<a' . $aMatches[1] . 'href="' . $url . '"' . $aMatches[3] . '>';
  624.  
  625.         return $sNewUrl;
  626.     }
  627.  
  628.  
  629.     /**
  630.      * Get article websafe name from article id and language id.
  631.      *
  632.      * @param    int     $iArtId   Article id
  633.      * @param    int     $iLangId  Language id
  634.      * @return     string    Article websafe name
  635.      */
  636.     public static function getArtWebsafeName($iArtId=0, $iLangId=0) {
  637.         $iArtId  = (int) $iArtId;
  638.         $iLangId = (int) $iLangId;
  639.         $tab = parent::$_oGlobals->get('cfg/tab/art_lang');
  640.         $sql = "SELECT urlname FROM " . $tab . " WHERE "
  641.              . "idart = '" . $iArtId . "' AND idlang = '" . $iLangId . "'";
  642.         if ($aData = mr_queryAndNextRecord($sql)) {
  643.             return urldecode($aData['urlname']);
  644.         }
  645.         return null;
  646.     }
  647.  
  648.  
  649.     /**
  650.      * Get article websafe name from idartlang.
  651.      *
  652.      * @param    int     $iArtLangId  idartlang
  653.      * @return     string    Article websafe name
  654.      */
  655.     public static function getArtLangWebsafeName($iArtLangId=0) {
  656.         $iArtLangId = (int) $iArtLangId;
  657.         $tab = parent::$_oGlobals->get('cfg/tab/art_lang');
  658.         $sql = "SELECT urlname FROM " . $tab . " WHERE idartlang = '". $iArtLangId . "'";
  659.         if ($aData = mr_queryAndNextRecord($sql)) {
  660.             return urldecode($aData['urlname']);
  661.         }
  662.         return null;
  663.     }
  664.  
  665.  
  666.     /**
  667.      * Get name of client by id.
  668.      *
  669.      * @param   int     $clientId  Client id
  670.      * @return  string  Client name
  671.      */
  672.     public static function getClientName($clientId=0) {
  673.         $clientId = (int) $clientId;
  674.  
  675.         $key = 'mr_statics/clientname_by_clientid/' . $clientId;
  676.         $clientName = parent::$_oGlobals->get($key);
  677.         if ($clientName !== null) {
  678.             return $clientName;
  679.         }
  680.  
  681.         $tab = parent::$_oGlobals->get('cfg/tab/clients');
  682.         $sql = "SELECT name FROM " . $tab . " WHERE idclient = '$clientId'";
  683.         if ($aData = mr_queryAndNextRecord($sql)) {
  684.             $clientName = $aData['name'];
  685.         } else {
  686.             $clientName = '';
  687.         }
  688.         parent::$_oGlobals->set($key, $clientName);
  689.         return $clientName;
  690.     }
  691.  
  692.  
  693.     /**
  694.      * Get client id from client name
  695.      *
  696.      * @param   string   Client name
  697.      * @return  integer  Client id
  698.      */
  699.     public static function getClientId($sClientName='') {
  700.         $key = 'mr_statics/clientid_by_name/' . $sClientName;
  701.         $clientId = parent::$_oGlobals->get($key);
  702.         if ($clientId !== null) {
  703.             return $clientId;
  704.         }
  705.  
  706.         $tab = parent::$_oGlobals->get('cfg/tab/clients');
  707.         $sql = "SELECT idclient FROM " . $tab . " WHERE name = '" . urldecode($sClientName) . "'";
  708.         if ($aData = mr_queryAndNextRecord($sql)) {
  709.             $clientId = $aData['idclient'];
  710.         } else {
  711.             $clientId = false;
  712.         }
  713.         parent::$_oGlobals->set($key, $clientId);
  714.         return $clientId;
  715.     }
  716.  
  717.  
  718.     /**
  719.      * Returns name of language by id.
  720.      *
  721.      * @param   int     $languageId  Language id
  722.      * @return  string  Lanuage name
  723.      */
  724.     public static function getLanguageName($languageId=0) {
  725.         $languageId = (int) $languageId;
  726.  
  727.         $key = 'mr_statics/languagename_by_id/' . $languageId;
  728.         $languageName = parent::$_oGlobals->get($key);
  729.         if ($languageName !== null) {
  730.             return $languageName;
  731.         }
  732.  
  733.         $tab = parent::$_oGlobals->get('cfg/tab/lang');
  734.         $sql = "SELECT name FROM " . $tab . " WHERE idlang = '$languageId'";
  735.         if ($aData = mr_queryAndNextRecord($sql)) {
  736.             $languageName = $aData['name'];
  737.         } else {
  738.             $languageName = '';
  739.         }
  740.         parent::$_oGlobals->set($key, $languageName);
  741.         return $languageName;
  742.     }
  743.  
  744.  
  745.     /**
  746.      * Get language id from language name thanks to Nicolas Dickinson for multi
  747.      * Client/Language BugFix
  748.      *
  749.      * @param  string   $sLanguageName  Language name
  750.      * @param  int      $iClientId      Client id
  751.      * @return integer  Language id
  752.      */
  753.     public static function getLanguageId($sLanguageName='', $iClientId=1) {
  754.         $iClientId = (int) $iClientId;
  755.  
  756.         $key = 'mr_statics/langid_by_langname_clientid/' . $sLanguageName . '_' . $iClientId;
  757.         $languageId = parent::$_oGlobals->get($key);
  758.         if ($languageId !== null) {
  759.             return $languageId;
  760.         }
  761.  
  762.         $aTab = parent::$_oGlobals->get('cfg/tab');
  763.         $sql  = "SELECT l.idlang FROM " . $aTab['lang'] . " as l "
  764.               . "LEFT JOIN " . $aTab['clients_lang'] . " AS cl ON l.idlang = cl.idlang "
  765.               . "WHERE cl.idclient = '". $iClientId . "' AND l.name = '" . urldecode($sLanguageName) . "'";
  766.         if ($aData = mr_queryAndNextRecord($sql)) {
  767.             $languageId = $aData['idlang'];
  768.         } else {
  769.             $languageId = false;
  770.         }
  771.         parent::$_oGlobals->set($key, $languageId);
  772.         return $languageId;
  773.     }
  774.  
  775.  
  776.     /**
  777.      * Splits passed argument into scheme://host and path/query.
  778.      *
  779.      * Example:
  780.      * input  = http://host/front_content.php?idcat=123
  781.      * return = array('htmlpath' => 'http://host', 'url' => 'front_content.php?idcat=123')
  782.      *
  783.      * @param  string  $url  URL to split
  784.      * @return array   Assoziative array including the two parts:
  785.      *                  - array('htmlpath' => $path, 'url' => $url)
  786.      */
  787.     public static function getClientFullUrlParts($url) {
  788.         $path = 'cfgClient/' . parent::$_oGlobals->get('client') . '/path/htmlpath';
  789.         $clientPath = parent::$_oGlobals->get($path);
  790.  
  791.         if (stristr($url, $clientPath) !== false) {
  792.  
  793.             // url includes full html path (scheme host path, etc.)
  794.             $url      = str_replace($clientPath, '', $url);
  795.             $htmlPath = $clientPath;
  796.             $aComp    = parse_url($htmlPath);
  797.  
  798.             // check if path matches to defined rootdir from mod_rewrite conf
  799.             if (isset($aComp['path']) && $aComp['path'] !== parent::getConfig('rootdir')) {
  800.                 // replace not matching path agaings configured one
  801.                 // this will replace e. g. "http://host/cms/" against "http://host/"
  802.                 $htmlPath = str_replace($aComp['path'], parent::getConfig('rootdir'), $htmlPath);
  803.                 if (substr($htmlPath, strlen($htmlPath)-1) == '/') {
  804.                     // remove last slash
  805.                     $htmlPath = substr($htmlPath, 0, strlen($htmlPath)-1);
  806.                 }
  807.             }
  808.         } else {
  809.             $htmlPath = '';
  810.         }
  811.         return array('htmlpath' => $htmlPath, 'url' => $url);
  812.     }
  813.  
  814.  
  815.     /**
  816.      * Function to preclean a url.
  817.      *
  818.      * Removes absolute path declaration '/front_content.php' or relative path
  819.      * definition to actual dir './front_content.php', ampersand entities '&amp;'
  820.      * and returns a url like 'front_content.php?idart=12&idlang=1'
  821.      *
  822.      * @param   string  $url  Url to clean
  823.      * @return  string  Cleaned Url
  824.      */
  825.     public static function urlPreClean($url) {
  826.         // some preparation of different front_content.php occurence
  827.         if (strpos($url, './front_content.php') === 0) {
  828.             $url = str_replace('./front_content.php', 'front_content.php', $url);
  829.         } elseif (strpos($url, '/front_content.php') === 0) {
  830.             $url = str_replace('/front_content.php', 'front_content.php', $url);
  831.         }
  832.         $url = str_replace('&amp;', '&', $url);
  833.         return $url;
  834.     }
  835.  
  836.  
  837.     /**
  838.      * Method to reset all aliases in categories.
  839.      */
  840.     public static function resetCategoriesAliases() {
  841.         $db = new DB_Contenido();
  842.  
  843.         $tab = parent::$_oGlobals->get('cfg/tab/cat_lang');
  844.  
  845.         // empty all aliases
  846.         $db->query("UPDATE " . $tab . " SET urlname = ''");
  847.  
  848.         $aCats = array();
  849.  
  850.         // get all categories
  851.         $db->query("SELECT name, idcat, idlang FROM " . $tab);
  852.         while ($db->next_record()) {
  853.             //set new alias
  854.             self::setCatWebsafeName($db->f('name'), $db->f('idcat'), $db->f('idlang'));
  855.             $aCats[] = array('idcat' => $db->f('idcat'), 'idlang' => $db->f('idlang'));
  856.         }
  857.  
  858.         foreach ($aCats as $p => $item) {
  859.             self::setCatUrlPath($item['idcat'], $item['idlang']);
  860.         }
  861.  
  862.         unset($db, $aCats);
  863.     }
  864.  
  865.  
  866.     /**
  867.      * Method to reset all aliases in articles.
  868.      */
  869.     public static function resetArticlesAliases() {
  870.         $db = new DB_Contenido();
  871.  
  872.         $tab = parent::$_oGlobals->get('cfg/tab/art_lang');
  873.  
  874.         // empty all aliases
  875.         $db->query("UPDATE " . $tab . " SET urlname = ''");
  876.  
  877.         // get all articles
  878.         $db->query("SELECT title, idart, idlang FROM " . $tab);
  879.         while ($db->next_record()) {
  880.             //set new alias
  881.             self::setArtWebsafeName($db->f('title'), $db->f('idart'), $db->f('idlang'));
  882.         }
  883.  
  884.         unset ($db);
  885.     }
  886.  
  887.  
  888.     /**
  889.      * Method to reset all aliases (categories and articles).
  890.      */
  891.     public static function resetAliases() {
  892.         self::resetCategoriesAliases();
  893.         self::resetArticlesAliases();
  894.     }
  895.  
  896.  
  897.     /**
  898.      * Used to postprocess resolved path
  899.      *
  900.      * Error site handling if category not found
  901.      *
  902.      * if percentage == 100 and there is no 100 percentage result value,
  903.      * error site will be shown - can be adjust by user settings for
  904.      * smooth similar effects - 80 to 95 will be best but have to check by user
  905.      *
  906.      * @deprecated Is no more used
  907.      * @todo remove usage at /contenido/includes/functions.pathresolver.php
  908.      *
  909.      * @param   array  $results  Pathresolver results array
  910.      * @return  mixed  Categoryid or false
  911.      */
  912.     public static function getIdFromPathresolverResult($results) {
  913.         $iMinPercentage = (int) parent::getConfig('category_resolve_min_percentage', 0);
  914.         $catId = key($results);
  915.         if ($iMinPercentage > 0 && $results[$catId] < $iMinPercentage) {
  916.             return false;
  917.         } else {
  918.             return $catId;
  919.         }
  920.     }
  921.  
  922. }

Documentation generated on Tue, 25 Nov 2008 22:07:21 +0100 by phpDocumentor 1.4.1