Source for file functions.mod_rewrite_setup.php

Documentation is available at functions.mod_rewrite_setup.php

  1. <?php
  2. /**
  3.  * Installer for Advanced Mod Rewrite Plugin, used by plugin setup.
  4.  *
  5.  * Some features are taken over from initial functions.mod_rewrite_setup.php file beeing created by
  6.  * Stefan Seifarth.
  7.  *
  8.  * @author      Murat Purc <murat@purc.de>
  9.  * @copyright   © ww.purc.de
  10.  * @package     Contenido
  11.  * @subpackage  ModRewrite
  12.  */
  13.  
  14.  
  15. if(!defined('CON_FRAMEWORK')) {
  16.     die('Illegal call');
  17. }
  18.  
  19. error_reporting (E_ALL E_NOTICE);
  20.  
  21.  
  22. /**
  23.  * Plugin setup class
  24.  *
  25.  * @todo should extend PluginSetupAbstract and implement IPluginSetup
  26.  *
  27.  * @author      Murat Purc (murat@purc.de)
  28.  * @package     Contenido
  29.  * @subpackage  PluginInstaller
  30.  */
  31. class PluginSetup {
  32.  
  33.     private static $_bInitialized false;
  34.  
  35.     private static $_cfg;
  36.  
  37.     private static $_db;
  38.  
  39.  
  40.     public static function initialize(){
  41.         if (self::$_bInitialized == true{
  42.             return;
  43.         }
  44.         self::$_cfg $GLOBALS['cfg'];
  45.         self::$_db  new DB_Contenido();
  46.     }
  47.  
  48.  
  49.     /**
  50.      * Upgrade plugin
  51.      *
  52.      * Handle upgrading of mod rewrite needed database table columns
  53.      */
  54.     public static function install(){
  55.  
  56.         self::initialize();
  57.  
  58.         // check the existance of art_lang.urlname
  59.         $sql "SELECT urlname FROM " self::$_cfg['tab']['art_lang'" LIMIT 0,1";
  60.         self::$_db->query($sql);
  61.         if (!self::$_db->next_record()) {
  62.             // add field 'urlname' to table
  63.             $sql "ALTER TABLE " self::$_cfg['tab']['art_lang'" ADD urlname VARCHAR( 128 ) AFTER title";
  64.             self::$_db->query($sql);
  65.         }
  66.  
  67.         // check the existance of cat_lang.urlpath
  68.         $sql "SELECT urlpath FROM " self::$_cfg['tab']['cat_lang'" LIMIT 0,1";
  69.         self::$_db->query($sql);
  70.         if (!self::$_db->next_record()) {
  71.             // add field 'urlpath' to table
  72.             $sql "ALTER TABLE " self::$_cfg['tab']['cat_lang'" ADD urlpath VARCHAR( 255 ) AFTER urlname";
  73.             self::$_db->query($sql);
  74.         }
  75.  
  76.         // check for empty article fields
  77.         $sql "SELECT idlang, title, idart FROM " self::$_cfg['tab']['art_lang'" WHERE urlname IS NULL OR urlname = ''";
  78.         self::$_db->query($sql);
  79.         while (self::$_db->next_record()) {
  80.             self::_setArticle(self::$_db->f('title')self::$_db->f('idart')self::$_db->f('idlang'));
  81.         }
  82.  
  83.         // check for empty category urlname
  84.         $sql "SELECT name, idcat, idlang FROM " self::$_cfg['tab']['cat_lang'" WHERE urlname IS NULL OR urlname = ''";
  85.         self::$_db->query($sql);
  86.         while (self::$_db->next_record()) {
  87.             self::_setCategory(self::$_db->f('name')self::$_db->f('idcat')self::$_db->f('idlang'));
  88.         }
  89.  
  90.         // check for empty category urlpath
  91.         $sql "SELECT name, idcat, idlang FROM " self::$_cfg['tab']['cat_lang'" WHERE urlpath IS NULL OR urlpath = ''";
  92.         self::$_db->query($sql);
  93.         while (self::$_db->next_record()) {
  94.             self::_setCategoryPath(self::$_db->f('idcat')self::$_db->f('idlang'));
  95.         }
  96.     }
  97.  
  98.  
  99.     /**
  100.      * Upgrade plugin
  101.      *
  102.      * Handle upgrading of mod rewrite needed database table columns
  103.      */
  104.     public static function upgrade(){
  105.         self::install();
  106.     }
  107.  
  108.     public static function uninstall({
  109.         self::initialize();
  110.         // remove field 'urlpath' from 'cat_lang' table
  111.         $sql "ALTER TABLE " self::$_cfg['tab']['cat_lang'" DROP urlpath";
  112.         self::$_db->query($sql);
  113.     }
  114.  
  115.  
  116.     /**
  117.      * Set websafe name in article list
  118.      *
  119.      * insert new websafe name in article list
  120.      *
  121.      * @param   string  original name (will be converted)
  122.      * @param   integer current article id
  123.      * @param   integer current language id
  124.      * @return  boolean true if insert was successfully
  125.      */
  126.     private static function _setArticle($sName=""$iArtId=0$iLangId=0$iCatId=0{
  127.         static $db;
  128.  
  129.         if (!isset($db)) {
  130.             $db new DB_Contenido();
  131.         }
  132.  
  133.         // create websafe name
  134.         $sNewName capiStrCleanURLCharacters($sName);
  135.  
  136.         // check if websafe name already exists
  137.         if (self::_inArticles($sNewName$iArtId$iLangId$iCatId)) {
  138.             // create new websafe name if exists
  139.             $sNewName capiStrCleanURLCharacters($sName'_' $iArtId;
  140.         }
  141.  
  142.         // check again - and set name
  143.         if (!self::_inArticles($sNewName$iArtId$iLangId$iCatId)) {
  144.             // insert websafe name in article list
  145.             $sql "UPDATE " self::$_cfg['tab']['art_lang'" SET urlname = '" $sNewName "' WHERE idart = '" $iArtId "' AND idlang = '" $iLangId "'";
  146.             return $db->query($sql);
  147.         else {
  148.             return false;
  149.         }
  150.     }
  151.  
  152.  
  153.     /**
  154.      * Set websafe name in category list
  155.      *
  156.      * insert new websafe name in category list
  157.      *
  158.      * @param   string  original name (will be converted)
  159.      * @param   integer current article id
  160.      * @param   integer current language id
  161.      * @return  boolean true if insert was successfully
  162.      */
  163.     private static function _setCategory($sName=''$iCatId=0$iLangId=0{
  164.         static $db;
  165.  
  166.         if (!isset($db)) {
  167.             $db new DB_Contenido();
  168.         }
  169.  
  170.         // create websafe name
  171.         $sNewName capiStrCleanURLCharacters($sName);
  172.  
  173.         // check if websafe name already exists
  174.         if (self::_inCategory($sNewName$iCatId$iLangId)) {
  175.             // create new websafe name if exists
  176.             $sNewName capiStrCleanURLCharacters($sName'_' $iCatId;
  177.         }
  178.  
  179.         // check again - and set name
  180.         if (!self::_inCategory($sNewName$iCatId$iLangId)) {
  181.             // insert websafe name in article list
  182.             $sql "UPDATE " self::$_cfg['tab']['cat_lang'" SET urlname = '$sNewName' WHERE idcat = '$iCatId' AND idlang = '$iLangId'";
  183.             return $db->query($sql);
  184.         else {
  185.             return false;
  186.         }
  187.     }
  188.  
  189.  
  190.     /**
  191.      * Build and set recursiv path for mod_rewrite rule like server directories
  192.      * (dir1/dir2/dir3)
  193.      *
  194.      * @param   int     $iCatId   Latest category id
  195.      * @param   int     $iLangId  Language id
  196.      * @param   int     $iLastId  Last category id
  197.      * @return     string    linkpath with correct uri
  198.      */
  199.     private static function _setCategoryPath($iCatId=0$iLangId=0$iLastId=0{
  200.         static $db;
  201.  
  202.         if (!isset($db)) {
  203.             $db new DB_Contenido();
  204.         }
  205.  
  206.         $aDirs     array();
  207.         $bFinish   false;
  208.         $iTmpCatId $iCatId;
  209.  
  210.         while ($bFinish == false{
  211.             $sql "SELECT cl.urlname, c.parentid FROM " self::$_cfg['tab']['cat_lang'" cl "
  212.                  . "LEFT JOIN " self::$_cfg['tab']['cat'" c ON cl.idcat = c.idcat "
  213.                  . "WHERE cl.idcat = '$iTmpCatId' AND cl.idlang = '$iLangId'";
  214.             $db->query($sql);
  215.             if ($db->next_record()) {
  216.                 $aDirs[]   $db->f('urlname');
  217.                 $iTmpCatId $db->f('parentid');
  218.  
  219.                 if ($db->f('parentid'== || $db->f('parentid'== $iLastId{
  220.                     $bFinish true;
  221.                 }
  222.             else {
  223.                 $bFinish true;
  224.             }
  225.         }
  226.  
  227.         // reverse array entries and create directory string
  228.         $sPath join('/'array_reverse($aDirs));
  229.  
  230.         // insert urlpath for category
  231.         $sql "UPDATE " self::$_cfg['tab']['cat_lang'" SET urlpath = '$sPath' WHERE idcat = '$iCatId' AND idlang = '$iLangId'";
  232.         return $db->query($sql);
  233.     }
  234.  
  235.     /**
  236.      * Check articles on websafe name
  237.      *
  238.      * Check all articles in the current category on existing same websafe name
  239.      *
  240.      * @param   string  Websafe name to check
  241.      * @param   integer current article id
  242.      * @param   integer current language id
  243.      * @param   integer current category id
  244.      * @return  boolean true if websafename already exists, false if not
  245.      */
  246.     private static function _inArticles($sName=''$iArtId=0$iLangId=0$iCatId=0{
  247.         static $db;
  248.  
  249.         if (!isset($db)) {
  250.             $db new DB_Contenido();
  251.         }
  252.  
  253.         $iCatId = (int) $iCatId;
  254.  
  255.         // handle multipages
  256.         if ($iCatId == 0{
  257.             // get category id if not set
  258.             $sql "SELECT idcat FROM " self::$_cfg['tab']['cat_art'" WHERE idart = '$iArtId'";
  259.             $db->query($sql);
  260.             $db->next_record();
  261.             $iCatId ($db->f('idcat'0$db->f('idcat''0';
  262.         }
  263.  
  264.         $sWhere " ca.idcat = '$iCatId' AND al.idlang = '$iLangId "' AND"
  265.                 . " al.urlname = '" $sName "' AND al.idart <> '$iArtId'";
  266.  
  267.         // check if websafe name is in this category
  268.         $sql "SELECT count(al.idart) as numcats FROM " self::$_cfg['tab']['art_lang'" al LEFT JOIN " self::$_cfg['tab']['cat_art'" ca ON al.idart = ca.idart WHERE " $sWhere;
  269.         $db->query($sql);
  270.         $db->next_record();
  271.  
  272.         return ($db->f('numcats'0true false;
  273.     }
  274.  
  275.  
  276.     /**
  277.      * Check categories on websafe name
  278.      *
  279.      * Check all categories in the main parent category on existing same websafe name
  280.      *
  281.      * @param   string  Websafe name to check
  282.      * @param   integer current category id
  283.      * @param   integer current language id
  284.      * @return  boolean true if websafename already exists, false if not
  285.      */
  286.     private static function _inCategory($sName=''$iCatId=0$iLangId=0{
  287.         static $db;
  288.  
  289.         if (!isset($db)) {
  290.             $db new DB_Contenido();
  291.         }
  292.  
  293.         // get parentid
  294.         $sql "SELECT parentid FROM " self::$_cfg['tab']['cat'" WHERE idcat = '$iCatId'";
  295.         $db->query($sql);
  296.         $db->next_record();
  297.         $iParentId ($db->f('parentid'0$db->f('parentid''0';
  298.  
  299.         $sWhere " c.parentid = '$iParentId' AND cl.idlang = '$iLangId "' AND"
  300.                 . " cl.urlname = '" $sName "' AND cl.idcat <> '$iCatId'";
  301.  
  302.         // check if websafe name is in this category
  303.         $sql "SELECT count(cl.idcat) as numcats FROM " self::$_cfg['tab']['cat_lang'" cl LEFT JOIN " self::$_cfg['tab']['cat'" c ON cl.idcat = c.idcat WHERE " $sWhere;
  304.         $db->query($sql);
  305.         $db->next_record();
  306.  
  307.         return ($db->f('numcats'0true false;
  308.     }
  309.  
  310. }
  311.  
  312.  
  313. /**
  314.  * upgradeModRewrite
  315.  *
  316.  * handle upgrading of mod rewrite needed
  317.  * database table columns
  318.  */
  319. /*
  320. ##++##
  321. function upgradeModRewrite() {
  322.     global $cfg;
  323.  
  324.     $db = new DB_Contenido();
  325.  
  326.     // check the existance of field urlname
  327.     $sql = "SELECT urlname FROM " . $cfg["tab"]["art_lang"] . " LIMIT 0,1";
  328.     $db->query($sql);
  329.     if (!$db->next_record()) {
  330.         // add field 'urlname' to table
  331.         $sql = "ALTER TABLE " . $cfg["tab"]["art_lang"] . " ADD urlname VARCHAR( 128 ) AFTER title";
  332.         $db->query($sql);
  333.     }
  334.  
  335.     // check for empty article fields
  336.     $sql = "SELECT idlang, title, idart FROM " . $cfg["tab"]["art_lang"] . " WHERE urlname IS NULL OR urlname = ''";
  337.     $db->query($sql);
  338.     while ($db->next_record()) {
  339.         set_article($db->f('title'), $db->f('idart'), $db->f('idlang'));
  340.     }
  341.  
  342.     // check for empty category fields
  343.     $sql = "SELECT name, idcat, idlang FROM " . $cfg["tab"]["cat_lang"] . " WHERE urlname IS NULL OR urlname = ''";
  344.     $db->query($sql);
  345.     while ($db->next_record()) {
  346.         set_category($db->f('name'), $db->f('idcat'), $db->f('idlang'));
  347.     }
  348. }
  349.  
  350. function uninstallModRewrite() {
  351.     global $cfg;
  352.     $db = new DB_Contenido();
  353.     $sql = "ALTER TABLE " . $cfg["tab"]["art_lang"] . " DROP urlname";
  354.     $db->query( $sql );
  355. }
  356. */
  357.  
  358. /**
  359.  * set_article()
  360.  *
  361.  * set websafe name in article list
  362.  *
  363.  * insert new websafe name in article list
  364.  *
  365.  * @param   string  original name (will be converted)
  366.  * @param   integer current article id
  367.  * @param   integer current language id
  368.  * @return  boolean true if insert was successfully
  369.  */
  370. /*
  371. function set_article($str_name="", $int_id=0, $int_lang_id=0, $int_idcat=0) {
  372.     global $cfg;
  373.     static $db;
  374.  
  375.     if (!isset($db)) {
  376.         $db = new DB_Contenido();
  377.     }
  378.  
  379.     // create websafe name
  380.     $str_new_name = capiStrCleanURLCharacters($str_name);
  381.  
  382.     // check if websafe name already exists
  383.     if (in_articles($str_new_name, $int_id, $int_lang_id, $int_idcat) ) {
  384.         // create new websafe name if exists
  385.         $str_new_name = capiStrCleanURLCharacters($str_name) . "_" . $int_id;
  386.     }
  387.  
  388.     // check again - and set name
  389.     if (!in_articles($str_new_name, $int_id, $int_lang_id, $int_idcat)) {
  390.         // insert websafe name in article list
  391.         $sql = "UPDATE " . $cfg["tab"]["art_lang"] . " SET urlname = '" . $str_new_name . "' WHERE idart = '" . $int_id . "' AND idlang = '" . $int_lang_id . "'";
  392.         return $db->query($sql);
  393.     } else {
  394.         return false;
  395.     }
  396. }
  397. */
  398.  
  399.  
  400.  
  401. /**
  402.  * set_category()
  403.  *
  404.  * set websafe name in category list
  405.  *
  406.  * insert new websafe name in category list
  407.  *
  408.  * @param   string  original name (will be converted)
  409.  * @param   integer current article id
  410.  * @param   integer current language id
  411.  * @return  boolean true if insert was successfully
  412.  */
  413. /*
  414. function set_category($str_name='', $int_id=0, $int_lang_id=0) {
  415.     global $cfg;
  416.     static $db;
  417.  
  418.     if (!isset($db)) {
  419.         $db = new DB_Contenido();
  420.     }
  421.  
  422.     // create websafe name
  423.     $str_new_name = capiStrCleanURLCharacters($str_name);
  424.  
  425.     // check if websafe name already exists
  426.     if (in_category($str_new_name, $int_id, $int_lang_id)) {
  427.         // create new websafe name if exists
  428.         $str_new_name = capiStrCleanURLCharacters($str_name) . "_" . $int_id;
  429.     }
  430.  
  431.     // check again - and set name
  432.     if (!in_category($str_new_name, $int_id, $int_lang_id)) {
  433.         // insert websafe name in article list
  434.         $sql = "UPDATE " . $cfg["tab"]["cat_lang"] . " SET urlname = '$str_new_name' WHERE idcat = '$int_id' AND idlang = '$int_lang_id'";
  435.         return $db->query($sql);
  436.     } else {
  437.         return false;
  438.     }
  439. }
  440. */
  441.  
  442.  
  443. /**
  444.  * in_articles()
  445.  *
  446.  * Check articles on websafe name
  447.  *
  448.  * Check all articles in the current category
  449.  * on existing same websafe name
  450.  *
  451.  * @param   string  Websafe name to check
  452.  * @param   integer current article id
  453.  * @param   integer current language id
  454.  * @param   integer current category id
  455.  * @return  boolean true if websafename already exists, false if not
  456.  */
  457. /*
  458. function in_articles ( $str_name = "", $int_id = 0, $int_lang_id = 0, $int_idcat = 0) {
  459.     global $cfg;
  460.     static $db;
  461.  
  462.     if (!isset($db)) {
  463.         $db = new DB_Contenido();
  464.     }
  465.  
  466.     // handle multipages
  467.     if ($int_idcat > 0) {
  468.         $int_category_id = $int_idcat;
  469.     } else {
  470.         // get category id if not set
  471.         $sql = "SELECT idcat FROM " . $cfg["tab"]["cat_art"] . " WHERE idart = '$int_id'";
  472.         $db->query($sql);
  473.         $db->next_record();
  474.         $int_category_id = ($db->f("idcat") > 0 ) ? $db->f("idcat") : "0";
  475.     }
  476.  
  477.     $str_where = " ca.idcat = '$int_category_id' AND"
  478.                . " al.idlang = '" . $int_lang_id . "' AND"
  479.                . " al.urlname = '" . $str_name . "' AND"
  480.                . " al.idart <> '$int_id'";
  481.  
  482.     // check if websafe name is in this category
  483.     $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;
  484.     $db->query($sql);
  485.     $db->next_record();
  486.  
  487.     $int_count = $db->f("numcats");
  488.  
  489.     return ($int_count > 0) ? true : false;
  490. }
  491. */
  492.  
  493. /**
  494.  * in_categories()
  495.  *
  496.  * Check categories on websafe name
  497.  *
  498.  * Check all categories in the main parent
  499.  * category on existing same websafe name
  500.  *
  501.  * @param   string  Websafe name to check
  502.  * @param   integer current category id
  503.  * @param   integer current language id
  504.  * @return  boolean true if websafename already exists, false if not
  505.  */
  506. /*
  507. function in_category($str_name='', $int_id=0, $int_lang_id=0) {
  508.     global $cfg;
  509.     static $db;
  510.  
  511.     if (!isset($db)) {
  512.         $db = new DB_Contenido();
  513.     }
  514.  
  515.     // get parentid
  516.     $sql = "SELECT parentid FROM " . $cfg["tab"]["cat"] . " WHERE idcat = '$int_id'";
  517.     $db->query($sql);
  518.     $db->next_record();
  519.     $int_parent_id = ($db->f("parentid") > 0 ) ? $db->f("parentid") : "0";
  520.  
  521.     $str_where = " c.parentid = '$int_parent_id' AND"
  522.                . " cl.idlang = '" . $int_lang_id . "' AND"
  523.                . " cl.urlname = '" . $str_name . "' AND"
  524.                . " cl.idcat <> '$int_id'";
  525.  
  526.     // check if websafe name is in this category
  527.     $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;
  528.     $db->query($sql);
  529.     $db->next_record();
  530.  
  531.     $int_count = $db->f("numcats");
  532.  
  533.     return ($int_count > 0) ? true : false;
  534. }
  535. */

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