LiveUser
[ class tree: LiveUser ] [ index: LiveUser ] [ all elements ]

Source for file MDB2_Simple.php

Documentation is available at MDB2_Simple.php

  1. <?php
  2. // LiveUser: A framework for authentication and authorization in PHP applications
  3. // Copyright (C) 2002-2003 Markus Wolff
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; either
  8. // version 2.1 of the License, or (at your option) any later version.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. // Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public
  16. // License along with this library; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19. /**
  20.  * Include parent class
  21.  */
  22. require_once 'LiveUser/Admin/Perm/Common.php';
  23. require_once 'MDB2.php';
  24.  
  25. /**
  26.  * Container for simple rights managements.
  27.  *
  28.  * With it you can only assign rights to users. For advanced uses like
  29.  * groups assignements and more see DB_Medium and DB_Complex.
  30.  *
  31.  * @category authentication
  32.  * @version $Id: MDB2_Simple.php,v 1.37 2004/10/01 21:26:57 dufuz Exp $
  33.  * @package LiveUser
  34.  */
  35. {
  36.     /**
  37.      * Columns of the perm table.
  38.      * Associative array with the names of the perm table columns.
  39.      * The 'group_id' and 'group_define_name' fields have to be set.
  40.      * 'group_type', 'is_active', 'owner_user_id' and 'owner_group_id' are optional.
  41.      * It doesn't make sense to set only one of the time columns without the
  42.      * other.
  43.      *
  44.      * The type attribute is only useful when using MDB or MDB2.
  45.      *
  46.      * @access public
  47.      * @var    array 
  48.      */
  49.     var $groupTableCols = array(
  50.         'required' => array(
  51.             'group_id' => array('type' => 'integer''name' => 'group_id'),
  52.             'group_define_name' => array('type' => 'text''name' => 'group_define_name')
  53.         ),
  54.         'optional' => array(
  55.             'group_type'    => array('type' => 'integer''name' => 'group_type'),
  56.             'is_active'    => array('type' => 'boolean''name' => 'is_active'),
  57.             'owner_user_id'  => array('type' => 'integer''name' => 'owner_user_id'),
  58.             'owner_group_id' => array('type' => 'integer''name' => 'owner_group_id')
  59.         )
  60.     );
  61.  
  62.     /**
  63.      * Constructor
  64.      *
  65.      * @access protected
  66.      * @param  array  full liveuser configuration array
  67.      * @return void 
  68.      * @see    LiveUser::factory()
  69.      */
  70.     function LiveUser_Admin_Perm_Container_MDB2_Simple(&$connectOptions)
  71.     {
  72.         $this->LiveUser_Admin_Perm_Common($connectOptions);
  73.         if (is_array($connectOptions)) {
  74.             foreach ($connectOptions as $key => $value{
  75.                 if (isset($this->$key)) {
  76.                     $this->$key $value;
  77.                 }
  78.             }
  79.             if (isset($connectOptions['connection']&&
  80.                     MDB2::isConnection($connectOptions['connection'])
  81.             {
  82.                 $this->dbc     &$connectOptions['connection'];
  83.                 $this->init_ok = true;
  84.             elseif (isset($connectOptions['dsn'])) {
  85.                 $this->dsn $connectOptions['dsn'];
  86.                 $function = null;
  87.                 if (isset($connectOptions['function'])) {
  88.                     $function $connectOptions['function'];
  89.                 }
  90.                 $options = null;
  91.                 if (isset($connectOptions['options'])) {
  92.                     $options $connectOptions['options'];
  93.                 }
  94.                 $options['portability'= MDB2_PORTABILITY_ALL;
  95.                 if ($function == 'singleton'{
  96.                     $this->dbc =MDB2::singleton($connectOptions['dsn']$options);
  97.                 else {
  98.                     $this->dbc =MDB2::connect($connectOptions['dsn']$options);
  99.                 }
  100.                 if (!MDB2::isError($this->dbc)) {
  101.                     $this->init_ok = true;
  102.                 }
  103.             }
  104.         }
  105.     }
  106.  
  107.     /**
  108.      * Gets the perm ID of a user.
  109.      *
  110.      * @access  public
  111.      * @param   string  Auth user ID.
  112.      * @param   string  Auth container name.
  113.      * @return  mixed   Permission ID or MDB2 error.
  114.      */
  115.     function getPermUserId($authId$authName)
  116.     {
  117.         $query '
  118.             SELECT
  119.                 perm_user_id
  120.             FROM
  121.                 ' $this->prefix . 'perm_users
  122.             WHERE
  123.                 auth_user_id = '.$this->dbc->quote($authId'text').'
  124.             AND
  125.                 auth_container_name = '.$this->dbc->quote($authName'text');
  126.         $permId $this->dbc->queryOne($query);
  127.  
  128.         return $permId;
  129.     // end func _getPermUserId
  130.  
  131.     /**
  132.      * Gets the auth ID of a user.
  133.      *
  134.      * @access  public
  135.      * @param   string  Perm user ID.
  136.      * @return  mixed   Permission ID or DB error.
  137.      */
  138.     function getAuthUserId($permId$id_only = false)
  139.     {
  140.         $query '
  141.             SELECT
  142.                 auth_user_id, auth_container_name
  143.             FROM
  144.                 ' $this->prefix . 'perm_users
  145.             WHERE
  146.                 perm_user_id = '.$this->dbc->quote($permId'text');
  147.                 $authId $this->dbc->queryRow($queryarray('text''text')MDB2_FETCHMODE_ASSOC);
  148.             if ($id_only{
  149.                 return $authId['auth_user_id'];
  150.             }
  151.             return $authId;
  152.     // end func _getAuthUserId
  153.  
  154.     /**
  155.      * Reads all languages from databases and stores them in private variable
  156.      *
  157.      * $this->_langs is filled with this array structure:
  158.      *     two_letter_code => language_id
  159.      *
  160.      * @access private
  161.      * @return mixed boolean or MDB2 Error object
  162.      */
  163.     function _getLanguages()
  164.     {
  165.         if (sizeof($this->_langs< 1{
  166.             $query 'SELECT two_letter_name, language_id FROM ' $this->prefix . 'languages';
  167.             $langs $this->dbc->queryAll($querynullMDB2_FETCHMODE_ASSOCtrue);
  168.             if (MDB2::isError($langs)) {
  169.                 return $langs;
  170.             }
  171.             $this->_langs $langs;
  172.         }
  173.         return true;
  174.     }
  175.  
  176.     /**
  177.      * Set current language
  178.      *
  179.      * Returns false if the language is not known
  180.      *
  181.      * @access public
  182.      * @param  string language short name
  183.      * @return mixed   boolean or MDB2 Error object or false
  184.      */
  185.     function setCurrentLanguage($language)
  186.     {
  187.         // Get all language ids
  188.         if (MDB2::isError($result $this->_getLanguages())) {
  189.             return $result;
  190.         }
  191.  
  192.         // Check if language is a known one
  193.         if (!isset($this->_langs[$language])) {
  194.             return false;
  195.         }
  196.  
  197.         $this->_language $language;
  198.  
  199.         return true;
  200.     }
  201.  
  202.     /**
  203.      * Get current language
  204.      *
  205.      * @access public
  206.      * @return string name of the current language
  207.      */
  208.     function getCurrentLanguage()
  209.     {
  210.         return $this->_language;
  211.     }
  212.  
  213.     /**
  214.      * Set current application
  215.      *
  216.      * @access public
  217.      * @param  integer  id of application
  218.      * @return boolean always true
  219.      */
  220.     function setCurrentApplication($applicationId)
  221.     {
  222.         $this->_application $applicationId;
  223.  
  224.         return true;
  225.     }
  226.  
  227.     /**
  228.      * Get current application
  229.      *
  230.      * @access public
  231.      * @return string name of the current application
  232.      */
  233.     function getCurrentApplication()
  234.     {
  235.         return $this->_application;
  236.     }
  237.  
  238.     /**
  239.      * Assigns name (and description) in specified language to a section
  240.      *
  241.      * @access public
  242.      * @param integer id of [section]
  243.      * @param integer type of section
  244.      * @param string  language (two letter code) of name/description
  245.      * @param string  name of [section]
  246.      * @param string  description of [section]
  247.      * @return mixed boolean or MDB2 Error object
  248.      */
  249.     function addTranslation($sectionId$section_type$language$name$description = null)
  250.     {
  251.         // Get all language ids
  252.         if (MDB2::isError($result $this->_getLanguages())) {
  253.             return $result;
  254.         }
  255.  
  256.         // Check if language is a known one
  257.         if (!isset($this->_langs[$language])) {
  258.             return false;
  259.         }
  260.  
  261.         // Register translation
  262.         $query 'INSERT INTO
  263.                   ' $this->prefix . 'translations
  264.                   (section_id, section_type, language_id, name, description)
  265.                 VALUES
  266.                   (
  267.                     ' $this->dbc->quote($sectionId'integer'',
  268.                     ' $this->dbc->quote($section_type'integer'',
  269.                     ' $this->dbc->quote($this->_langs[$language]'integer'',
  270.                     ' $this->dbc->quote($name'text'',
  271.                     ' $this->dbc->quote($description'text''
  272.                   )';
  273.  
  274.         $result $this->dbc->query($query);
  275.  
  276.         if (MDB2::isError($result)) {
  277.             return $result;
  278.         }
  279.  
  280.         // name (and description) added ...
  281.         return true;
  282.     }
  283.  
  284.     /**
  285.      * Updates name (and description) of [section] in specified language
  286.      *
  287.      * @access public
  288.      * @param  integer id of [section]
  289.      * @param  integer type of section
  290.      * @param  string  language (two letter code) of name/description
  291.      * @param  string  name of [section]
  292.      * @param  string  description of [section]
  293.      * @return mixed boolean or MDB2 Error object
  294.      */
  295.     function updateTranslation($sectionId$section_type$language$name$description = null)
  296.     {
  297.         // Get all language ids
  298.         if (MDB2::isError($result $this->_getLanguages())) {
  299.             return $result;
  300.         }
  301.  
  302.         // Check if language is a known one
  303.         if (!isset($this->_langs[$language])) {
  304.             return false;
  305.         }
  306.  
  307.         // Update translation
  308.         $query 'UPDATE
  309.                   ' $this->prefix . 'translations
  310.                 SET
  311.                   name        = ' $this->dbc->quote($name'text'',
  312.                   description = ' $this->dbc->quote($description'text''
  313.                 WHERE
  314.                   section_id    = ' $this->dbc->quote($sectionId'integer'' AND
  315.                   section_type  = ' $this->dbc->quote($section_type'integer'' AND
  316.                   language_id   = ' $this->dbc->quote($this->_langs[$language]'integer');
  317.  
  318.         $result $this->dbc->query($query);
  319.  
  320.         if (MDB2::isError($result)) {
  321.             return $result;
  322.         }
  323.  
  324.         // Translation name (and description) updated ...
  325.         return true;
  326.     }
  327.  
  328.     /**
  329.      * Remove name (and description) of the [section] in specified language
  330.      *
  331.      * @access public
  332.      * @param  integer id of [section]
  333.      * @param  integer type of section
  334.      * @param  string  language (two letter code) of name/description
  335.      * @param  boolean recursive delete of all translations
  336.      * @return mixed boolean or MDB2 Error object
  337.      */
  338.     function removeTranslation($sectionId$section_type$language$recursive = false)
  339.     {
  340.         // Get all language ids
  341.         if (MDB2::isError($result $this->_getLanguages())) {
  342.             return $result;
  343.         }
  344.  
  345.         // Check if language is a known one
  346.         if (!isset($this->_langs[$language])) {
  347.             return false;
  348.         }
  349.  
  350.         // Remove translation
  351.         $query 'DELETE FROM
  352.                   ' $this->prefix . 'translations
  353.                 WHERE
  354.                   section_id    = ' $this->dbc->quote($sectionId'integer'' AND
  355.                   section_type  = ' $this->dbc->quote($section_type'integer');
  356.         if (!$recursive{
  357.             $query .= ' AND language_id = ' $this->dbc->quote($this->_langs[$language]'integer');
  358.         }
  359.  
  360.         $result $this->dbc->query($query);
  361.  
  362.         if (MDB2::isError($result)) {
  363.             return $result;
  364.         }
  365.  
  366.         // Translation name (and description) removed ...
  367.         return true;
  368.     }
  369.  
  370.     /**
  371.      * Get name (and description) of the [section] in specified language
  372.      *
  373.      * @access public
  374.      * @param  integer id of [section]
  375.      * @param  integer type of section
  376.      * @return mixed array or MDB2 Error object
  377.      */
  378.     function getTranslation($sectionId$section_type)
  379.     {
  380.         // get translation
  381.         $query 'SELECT
  382.                   translations.name        AS name,
  383.                   translations.description AS description
  384.                 FROM
  385.                   ' $this->prefix . 'translations translations
  386.                 WHERE
  387.                   section_id    = ' $this->dbc->quote($sectionId'integer'' AND
  388.                   section_type  = ' $this->dbc->quote($section_type'integer');
  389.         $translation $this->dbc->queryRow($querynullMDB2_FETCHMODE_ASSOC);
  390.         if (MDB2::isError($translation)) {
  391.             return $translation;
  392.         }
  393.  
  394.         if (!is_array($translation)) {
  395.             return array();
  396.         }
  397.         // Translation name (and description) removed ...
  398.         return $translation;
  399.     }
  400.  
  401.     /**
  402.      * Add a new language
  403.      *
  404.      * @access public
  405.      * @param  string two letter code of language
  406.      * @param  string name of language
  407.      * @param  string description of language
  408.      * @return mixed integer (language_id) or MDB2 Error object
  409.      */
  410.     function addLanguage($two_letter_code$language_name$language_description = null)
  411.     {
  412.         // Get next language id
  413.         $languageId $this->dbc->nextId($this->prefix . 'languages'true);
  414.  
  415.         if (MDB2::isError($languageId)) {
  416.             return $languageId;
  417.         }
  418.  
  419.         // Add language
  420.         $query 'INSERT INTO
  421.                   ' $this->prefix . 'languages
  422.                   (language_id, two_letter_name)
  423.                 VALUES
  424.                   (
  425.                     ' $languageId ',
  426.                     ' $this->dbc->quote($two_letter_code'text''
  427.                   )';
  428.  
  429.         $result $this->dbc->query($query);
  430.  
  431.         if (MDB2::isError($result)) {
  432.             return $result;
  433.         }
  434.  
  435.         // force language reload in case it is the first language we create
  436.         $this->_getLanguages();
  437.  
  438.         if (sizeof($this->_langs== 1{
  439.             $lang $two_letter_code;
  440.         else {
  441.             $lang $this->getCurrentLanguage();
  442.         }
  443.  
  444.         // Insert Language translation into Translations table
  445.         $result $this->addTranslation(
  446.             $languageId,
  447.             LIVEUSER_SECTION_LANGUAGE,
  448.             $lang,
  449.             $language_name,
  450.             $language_description
  451.         );
  452.  
  453.         if (MDB2::isError($result)) {
  454.             return $result;
  455.         }
  456.  
  457.         // Clear language cache
  458.         unset($this->_langs);
  459.  
  460.         // Job done ...
  461.         return $languageId;
  462.     }
  463.  
  464.     /**
  465.      * Remove a language
  466.      *
  467.      * @access public
  468.      * @param  integer language (two letter code)
  469.      * @return mixed   boolean or MDB2 Error object
  470.      */
  471.     function removeLanguage($language)
  472.     {
  473.         // Get all language ids
  474.         if (MDB2::isError($result $this->_getLanguages())) {
  475.             return $result;
  476.         }
  477.  
  478.         // Check if language is a known one
  479.         if (!isset($this->_langs[$language])) {
  480.             return false;
  481.         }
  482.  
  483.         // Delete language
  484.         $query 'DELETE FROM
  485.                   ' $this->prefix . 'languages
  486.                 WHERE
  487.                   language_id = ' $this->dbc->quote($this->_langs[$language]'integer');
  488.  
  489.         $result $this->dbc->query($query);
  490.  
  491.         if (MDB2::isError($result)) {
  492.             return $result;
  493.         }
  494.  
  495.         // Delete language translations
  496.         $result $this->removeTranslation($this->_langs[$language]LIVEUSER_SECTION_LANGUAGE$this->getCurrentLanguage()true);
  497.  
  498.         if (MDB2::isError($result)) {
  499.             return $result;
  500.         }
  501.  
  502.         // Clear language cache
  503.         unset($this->_langs);
  504.  
  505.         // Job done ...
  506.         return true;
  507.     }
  508.  
  509.     /**
  510.      * Update language
  511.      *
  512.      * @access public
  513.      * @param  integer language (two letter code)
  514.      * @param  string  name of language
  515.      * @param  string  description of language
  516.      * @return mixed   boolean or MDB2 Error object
  517.      */
  518.     function updateLanguage($language$language_name$language_description = null)
  519.     {
  520.         // Get all language ids
  521.         if (MDB2::isError($result $this->_getLanguages())) {
  522.             return $result;
  523.         }
  524.  
  525.         // Check if language is a known one
  526.         if (!isset($this->_langs[$language])) {
  527.             return false;
  528.         }
  529.  
  530.         // Update Language translation into Translations table
  531.         $result $this->updateTranslation(
  532.             $this->_langs[$language],
  533.             LIVEUSER_SECTION_LANGUAGE,
  534.             $this->getCurrentLanguage(),
  535.             $language_name,
  536.             $langauge_description
  537.         );
  538.  
  539.         if (MDB2::isError($result)) {
  540.             return $result;
  541.         }
  542.  
  543.         // Clear language cache
  544.         unset($this->_langs);
  545.  
  546.         // Job done ...
  547.         return true;
  548.     }
  549.  
  550.     /**
  551.      * Add an application
  552.      *
  553.      * @access public
  554.      * @param  string name of application constant
  555.      * @param  string name of application
  556.      * @param  string description of application
  557.      * @return mixed  integer (application_id) or MDB2 Error object
  558.      */
  559.     function addApplication($define_name = null$application_name = null,
  560.         $application_description = null)
  561.     {
  562.         // Get next application id
  563.         $applicationId $this->dbc->nextId($this->prefix . 'applications'true);
  564.         if (MDB2::isError($applicationId)) {
  565.             return $applicationId;
  566.         }
  567.  
  568.         // Register new application
  569.         $query 'INSERT INTO
  570.                   ' $this->prefix . 'applications
  571.                   (application_id, application_define_name)
  572.                 VALUES
  573.                   (
  574.                     ' $this->dbc->quote($applicationId'integer'',
  575.                     ' $this->dbc->quote((!is_null($define_name$define_name $applicationId)'text''
  576.                   )';
  577.  
  578.         $result $this->dbc->query($query);
  579.  
  580.         if (MDB2::isError($result)) {
  581.             return $result;
  582.         }
  583.  
  584.         // Insert Application translation into Translations table
  585.         $result $this->addTranslation(
  586.             $applicationId,
  587.             LIVEUSER_SECTION_APPLICATION,
  588.             $this->getCurrentLanguage(),
  589.             $application_name,
  590.             $application_description
  591.         );
  592.  
  593.         if (MDB2::isError($result)) {
  594.             return $result;
  595.         }
  596.  
  597.         return $applicationId;
  598.     }
  599.  
  600.     /**
  601.      * Add an area
  602.      *
  603.      * @access public
  604.      * @param  string id of application
  605.      * @param  string name of area constant
  606.      * @param  string name of area
  607.      * @param  string description of area
  608.      * @return mixed  integer (area_id) or MDB2 Error object
  609.      */
  610.     function addArea($applicationId$define_name = null$area_name = null,
  611.         $area_description = null)
  612.     {
  613.         // Get next area id
  614.         $areaId $this->dbc->nextId($this->prefix . 'areas'true);
  615.  
  616.         if (MDB2::isError($areaId)) {
  617.             return $areaId;
  618.         }
  619.  
  620.         // Register new area
  621.         $query 'INSERT INTO
  622.                   ' $this->prefix . 'areas
  623.                   (area_id, area_define_name, application_id)
  624.                 VALUES
  625.                   (
  626.                     ' $this->dbc->quote($areaId'integer'',
  627.                     ' $this->dbc->quote((!is_null($define_name$define_name $areaId)'text'',
  628.                     ' $this->dbc->quote($applicationId'integer''
  629.                   )';
  630.  
  631.         $result $this->dbc->query($query);
  632.  
  633.         if (MDB2::isError($result)) {
  634.             return $result;
  635.         }
  636.  
  637.         // Insert Area translation into Translations table
  638.         $result $this->addTranslation(
  639.             $areaId,
  640.             LIVEUSER_SECTION_AREA,
  641.             $this->getCurrentLanguage(),
  642.             $area_name,
  643.             $area_description
  644.         );
  645.  
  646.         if (MDB2::isError($result)) {
  647.             return $result;
  648.         }
  649.  
  650.         return $areaId;
  651.     }
  652.  
  653.     /**
  654.      * Delete an application
  655.      *
  656.      * @access public
  657.      * @param  integer id of application
  658.      * @return mixed   boolean or MDB2 Error object or false
  659.      */
  660.     function removeApplication($applicationId)
  661.     {
  662.         if (!is_numeric($applicationId)) {
  663.             return false;
  664.         }
  665.  
  666.         // Get all areas within the application, no matter what language
  667.         $query '
  668.             SELECT
  669.                 area_id
  670.             FROM
  671.             ' $this->prefix . 'areas
  672.             WHERE
  673.                 application_id=' $this->dbc->quote($applicationId'integer');
  674.  
  675.         $areas $this->dbc->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  676.  
  677.         if (MDB2::isError($areas)) {
  678.             return $areas;
  679.         }
  680.  
  681.         // Delete all areas within the application
  682.         if ($areas{
  683.             foreach($areas as $area{
  684.                 $res $this->removeArea($area['area_id']);
  685.                 if (MDB2::isError($res)) {
  686.                     return $res;
  687.                 }
  688.             }
  689.         }
  690.  
  691.         // Delete application translations
  692.         $result $this->removeTranslation($applicationIdLIVEUSER_SECTION_APPLICATION$this->getCurrentLanguage()true);
  693.  
  694.         if (MDB2::isError($result)) {
  695.             return $result;
  696.         }
  697.  
  698.         // Delete application itself
  699.         $query 'DELETE FROM
  700.                   ' $this->prefix . 'applications
  701.                 WHERE
  702.                   application_id = ' $applicationId;
  703.  
  704.         $result $this->dbc->query($query);
  705.  
  706.         if (MDB2::isError($result)) {
  707.             return $result;
  708.         }
  709.  
  710.         return true;
  711.     }
  712.  
  713.     /**
  714.      * Delete an area
  715.      *
  716.      * @access public
  717.      * @param  integer id of area
  718.      * @return mixed   boolean or MDB2 Error object or false
  719.      */
  720.     function removeArea($areaId)
  721.     {
  722.         if (!is_numeric($areaId)) {
  723.             return false;
  724.         }
  725.  
  726.         // Delete all rights in this area
  727.         $query 'SELECT
  728.                   right_id
  729.                 FROM
  730.                   ' $this->prefix . 'rights
  731.                 WHERE
  732.                   area_id = ' $areaId;
  733.  
  734.         $result $this->dbc->queryCol($query);
  735.  
  736.         if (MDB2::isError($result)) {
  737.             return $result;
  738.         }
  739.  
  740.         if (is_array($result)) {
  741.             foreach($result as $rightId{
  742.                 $this->removeRight($rightId);
  743.             }
  744.         }
  745.  
  746.         // Delete area admins
  747.         $query '
  748.             DELETE FROM
  749.                 ' $this->prefix . 'area_admin_areas
  750.             WHERE
  751.                 area_id=' $areaId '
  752.         ';
  753.  
  754.         $result $this->dbc->query($query);
  755.  
  756.         if (MDB2::isError($result)) {
  757.             return $result;
  758.         }
  759.  
  760.         // Delete area itself
  761.         $query 'DELETE FROM
  762.                   ' $this->prefix . 'areas
  763.                 WHERE
  764.                   area_id = ' $this->dbc->quote($areaId'integer');
  765.  
  766.         $result $this->dbc->query($query);
  767.  
  768.         if (MDB2::isError($result)) {
  769.             return $result;
  770.         }
  771.  
  772.         // Delete area translations
  773.         $result $this->removeTranslation($areaIdLIVEUSER_SECTION_AREA$this->getCurrentLanguage()true);
  774.  
  775.         if (MDB2::isError($result)) {
  776.             return $result;
  777.         }
  778.  
  779.         return true;
  780.     }
  781.  
  782.     /**
  783.      * Update an application
  784.      *
  785.      * @access public
  786.      * @param  integer id of application
  787.      * @param  string  name of application constant
  788.      * @param  string  name of application
  789.      * @param  string  description of application
  790.      * @return mixed   boolean or MDB2 Error object
  791.      */
  792.     function updateApplication($applicationId$define_name = null,
  793.         $application_name = null$application_description = null)
  794.     {
  795.         if (!is_null($define_name)) {
  796.             $query 'UPDATE
  797.                       ' $this->prefix . 'applications
  798.                     SET
  799.                       application_define_name = ' $this->dbc->quote($define_name'text''
  800.                     WHERE
  801.                       application_id = ' $this->dbc->quote($applicationId'integer');
  802.  
  803.             $result $this->dbc->query($query);
  804.             if (MDB2::isError($result)) {
  805.                 return $result;
  806.             }
  807.         }
  808.  
  809.         // Update Application translation into Translations table
  810.         $result $this->updateTranslation(
  811.             $applicationId,
  812.             LIVEUSER_SECTION_APPLICATION,
  813.             $this->getCurrentLanguage(),
  814.             $application_name,
  815.             $application_description
  816.         );
  817.  
  818.         if (MDB2::isError($result)) {
  819.             return $result;
  820.         }
  821.  
  822.         return true;
  823.     }
  824.  
  825.     /**
  826.      * Update an area
  827.      *
  828.      * @access public
  829.      * @param  integer id of area
  830.      * @param  int     id of application
  831.      * @param  string  name of area constant
  832.      * @param  string  name of area
  833.      * @param  string  description of area
  834.      * @return mixed   boolean or MDB2 Error object or false
  835.      */
  836.     function updateArea($areaId$applicationId$define_name = null,
  837.         $area_name = null$area_description = null)
  838.     {
  839.         if (!is_numeric($areaId)) {
  840.             return false;
  841.         }
  842.  
  843.         $query 'UPDATE
  844.                   ' $this->prefix . 'areas
  845.                 SET
  846.                   application_id   = ' $this->dbc->quote($applicationId'integer'',
  847.                   area_define_name = ' $this->dbc->quote((!is_null($define_name$define_name $applicationId)'text''
  848.                 WHERE
  849.                   area_id = ' $this->dbc->quote($areaId'integer');
  850.  
  851.         $result $this->dbc->query($query);
  852.  
  853.         if (MDB2::isError($result)) {
  854.             return $result;
  855.         }
  856.  
  857.         // Update Area translation into Translations table
  858.         $result $this->updateTranslation(
  859.             $areaId,
  860.             LIVEUSER_SECTION_AREA,
  861.             $this->getCurrentLanguage(),
  862.             $area_name,
  863.             $area_description
  864.         );
  865.         if (MDB2::isError($result)) {
  866.             return $result;
  867.         }
  868.  
  869.         return true;
  870.     }
  871.  
  872.     /**
  873.      * Add a right in special area
  874.      *
  875.      * @access public
  876.      * @param  integer id of area
  877.      * @param  string  name of right constant
  878.      * @param  string  name of right
  879.      * @param  string  description of right
  880.      * @return mixed   integer (right_id) or MDB2 Error object
  881.      */
  882.     function addRight($areaId$define_name = null$right_name = null,
  883.         $right_description = null)
  884.     {
  885.         // Get next right id
  886.         $rightId $this->dbc->nextId($this->prefix . 'rights'true);
  887.  
  888.         if (MDB2::isError($rightId)) {
  889.             return $rightId;
  890.         }
  891.  
  892.         // Register right
  893.         $query 'INSERT INTO
  894.                   ' $this->prefix . 'rights
  895.                   (right_id, area_id, right_define_name)
  896.                 VALUES
  897.                   (
  898.                     ' $this->dbc->quote($rightId'integer'',
  899.                     ' $this->dbc->quote($areaId'integer'',
  900.                     ' $this->dbc->quote((!is_null($define_name$define_name $rightId)'text''
  901.                   )';
  902.  
  903.         $result $this->dbc->query($query);
  904.  
  905.         if (MDB2::isError($result)) {
  906.             return $result;
  907.         }
  908.  
  909.         // Insert Right translation into Translations table
  910.         $result $this->addTranslation(
  911.             $rightId,
  912.             LIVEUSER_SECTION_RIGHT,
  913.             $this->getCurrentLanguage(),
  914.             $right_name,
  915.             $right_description
  916.         );
  917.  
  918.         if (MDB2::isError($result)) {
  919.             return $result;
  920.         }
  921.  
  922.         // Job done ...
  923.         return $rightId;
  924.     }
  925.  
  926.     /**
  927.      * Delete a right
  928.      *
  929.      * @access public
  930.      * @param  integer id of right
  931.      * @return boolean true on success or false on failure
  932.      */
  933.     function removeRight($rightId)
  934.     {
  935.         // Delete userright
  936.         $query 'DELETE FROM
  937.                   ' $this->prefix . 'userrights
  938.                 WHERE
  939.                   right_id = ' $this->dbc->quote($rightId'integer');
  940.  
  941.         $result $this->dbc->query($query);
  942.  
  943.         if (MDB2::isError($result)) {
  944.             return false;
  945.         }
  946.  
  947.         // Delete right translations
  948.         $result $this->removeTranslation($rightIdLIVEUSER_SECTION_RIGHT$this->getCurrentLanguage()true);
  949.  
  950.         if (MDB2::isError($result)) {
  951.             return $result;
  952.         }
  953.  
  954.         // Delete right itself
  955.         $query 'DELETE FROM
  956.                   ' $this->prefix . 'rights
  957.                 WHERE
  958.                   right_id = ' $this->dbc->quote($rightId'integer');
  959.  
  960.         $result $this->dbc->query($query);
  961.  
  962.         if (MDB2::isError($result)) {
  963.             return false;
  964.         }
  965.  
  966.         // Job done ...
  967.         return true;
  968.  
  969.     }
  970.  
  971.     /**
  972.      * Update a right
  973.      *
  974.      * @access public
  975.      * @param  integer id of right
  976.      * @param  integer id of area
  977.      * @param  string  name of right constant
  978.      * @param  string  name of right
  979.      * @param  string  description of right
  980.      * @return mixed   boolean or MDB2 Error object
  981.      */
  982.     function updateRight($rightId$areaId$define_name = null,
  983.         $right_name = null$right_description = null)
  984.     {
  985.         $query 'UPDATE
  986.                   ' $this->prefix . 'rights
  987.                 SET
  988.                   area_id           = ' $this->dbc->quote($areaId'integer'',
  989.                   right_define_name = ' $this->dbc->quote((!is_null($define_name$define_name $areaId)'text''
  990.                 WHERE
  991.                   right_id = ' $this->dbc->quote($rightId'integer');
  992.  
  993.         $result $this->dbc->query($query);
  994.  
  995.         if (MDB2::isError($result)) {
  996.             return $result;
  997.         }
  998.  
  999.         // Update Right translation into Translations table
  1000.         $result $this->updateTranslation(
  1001.             $rightId,
  1002.             LIVEUSER_SECTION_RIGHT,
  1003.             $this->getCurrentLanguage(),
  1004.             $right_name,
  1005.             $right_description
  1006.         );
  1007.  
  1008.         if (MDB2::isError($result)) {
  1009.             return $result;
  1010.         }
  1011.  
  1012.         // Job done ...
  1013.         return true;
  1014.     }
  1015.  
  1016.     /**
  1017.      * Add a user
  1018.      *
  1019.      * @access  public
  1020.      * @param   string   $authId    Auth user ID of the user that should be added.
  1021.      * @param   string   $authname  Auth container name.
  1022.      * @param   int      $type      User type (constants defined in Perm/Common.php) (optional).
  1023.      * @param   mixed    $permId    If specificed no new ID will be automatically generated instead
  1024.      * @return mixed   string (perm_user_id) or MDB2 Error object
  1025.      */
  1026.     function addUser($authId$authName$type = LIVEUSER_USER_TYPE_ID$permId = null)
  1027.     {
  1028.         if (!$this->init_ok{
  1029.             return false;
  1030.         }
  1031.  
  1032.         if (is_null($permId)) {
  1033.             $permId $this->dbc->nextId($this->prefix . 'perm_users'true);
  1034.         }
  1035.  
  1036.         $query '
  1037.             INSERT INTO
  1038.                 ' $this->prefix . 'perm_users
  1039.                 (perm_user_id, auth_user_id, perm_type, auth_container_name)
  1040.             VALUES
  1041.                 (
  1042.                 ' $this->dbc->quote($permId'integer'',
  1043.                 ' $this->dbc->quote($authId'text'',
  1044.                 ' $this->dbc->quote($type'integer'',
  1045.                 ' $this->dbc->quote($authName'text''
  1046.                 )';
  1047.  
  1048.         $result $this->dbc->query($query);
  1049.  
  1050.         if (MDB2::isError($result)) {
  1051.             return $result;
  1052.         }
  1053.  
  1054.         return $permId;
  1055.     }
  1056.  
  1057.     /**
  1058.      * Updates auth_user_id in the mapping table.
  1059.      *
  1060.      * @access  public
  1061.      * @param   int     perm_user_id of the user
  1062.      * @param   mixed   new Auth user ID
  1063.      * @param   mixed   new Auth Container name
  1064.      * @param   mixed   new perm type
  1065.      * @return  mixed   true or MDB2 Error object or false if there was an error
  1066.      *                   to begin with
  1067.      */
  1068.     function updateUser($permId$authId = false$authName = false$type = false)
  1069.     {
  1070.         if (!$this->init_ok{
  1071.             return false;
  1072.         }
  1073.  
  1074.         $update = array();
  1075.         if ($authId !== false{
  1076.             $update[' auth_user_id=' $this->dbc->quote($authId'text');
  1077.         }
  1078.         if ($authName !== false{
  1079.             $update[' auth_container_name=' $this->dbc->quote($authName'text');
  1080.         }
  1081.         if ($type !== false{
  1082.             $update[' perm_type=' $this->dbc->quote($type'text');
  1083.         }
  1084.  
  1085.         if (!empty($update)) {
  1086.             $update implode(','$update);
  1087.             $query '
  1088.                 UPDATE
  1089.                     ' $this->prefix . 'perm_users
  1090.                 SET ' $update '
  1091.                 WHERE
  1092.                     perm_user_id=' $this->dbc->quote($permId'integer');
  1093.  
  1094.             $result $this->dbc->query($query);
  1095.  
  1096.             if (MDB2::isError($result)) {
  1097.                 return $result;
  1098.             }
  1099.         }
  1100.  
  1101.         return true;
  1102.     }
  1103.  
  1104.     /**
  1105.      * Delete user
  1106.      *
  1107.      * @access public
  1108.      * @param  string  id of user
  1109.      * @return mixed   boolean or MDB2 Error object
  1110.      */
  1111.     function removeUser($permId)
  1112.     {
  1113.         if (!$this->init_ok{
  1114.             return false;
  1115.         }
  1116.  
  1117.         // Delete group assignments
  1118.         $query 'DELETE FROM
  1119.                   ' $this->prefix . 'groupusers
  1120.                 WHERE
  1121.                   perm_user_id = ' $permId;
  1122.  
  1123.         $result $this->dbc->query($query);
  1124.  
  1125.         if (MDB2::isError($result)) {
  1126.             return $result;
  1127.         }
  1128.  
  1129.         // Delete right assignments
  1130.         $query 'DELETE FROM
  1131.                   ' $this->prefix . 'userrights
  1132.                 WHERE
  1133.                   perm_user_id = ' $permId;
  1134.  
  1135.         $result $this->dbc->query($query);
  1136.  
  1137.         if (MDB2::isError($result)) {
  1138.             return $result;
  1139.         }
  1140.  
  1141.         // remove user area admin relation
  1142.         $result $this->removeUserAreaAdmin($permId);
  1143.  
  1144.         if (MDB2::isError($result)) {
  1145.             return $result;
  1146.         }
  1147.  
  1148.         // Delete user from perm table (Perm/MDB2)
  1149.         $query '
  1150.             DELETE FROM
  1151.                 ' $this->prefix . 'perm_users
  1152.             WHERE
  1153.                 perm_user_id = ' $permId;
  1154.  
  1155.         $result $this->dbc->query($query);
  1156.  
  1157.         if (MDB2::isError($result)) {
  1158.             return $result;
  1159.         }
  1160.  
  1161.         return true;
  1162.     }
  1163.  
  1164.     /**
  1165.      * Grant right to user
  1166.      *
  1167.      * @access public
  1168.      * @param  string  id of user
  1169.      * @param  integer id of right
  1170.      * @return mixed   boolean or MDB2 Error object
  1171.      */
  1172.     function grantUserRight($permId$rightId)
  1173.     {
  1174.         //return if this user already has right
  1175.         $query 'SELECT
  1176.                   count(*)
  1177.                 FROM
  1178.                   ' $this->prefix . 'userrights
  1179.                 WHERE
  1180.                   perm_user_id = ' $this->dbc->quote($rightId'integer''
  1181.                 AND
  1182.                   right_id     = ' $this->dbc->quote($rightId'integer');
  1183.  
  1184.         $count $this->dbc->queryOne($query);
  1185.  
  1186.         if (MDB2::isError($count|| $count != 0{
  1187.             return false;
  1188.         }
  1189.  
  1190.         $query 'INSERT INTO
  1191.                   ' $this->prefix . 'userrights
  1192.                   (perm_user_id, right_id, right_level)
  1193.                 VALUES
  1194.                   (
  1195.                     ' $this->dbc->quote($permId'integer'',
  1196.                     ' $this->dbc->quote($rightId'integer'', '.LIVEUSER_MAX_LEVEL.'
  1197.                   )';
  1198.  
  1199.         $result $this->dbc->query($query);
  1200.  
  1201.         if (MDB2::isError($result)) {
  1202.             return $result;
  1203.         }
  1204.  
  1205.         // Job done ...
  1206.         return true;
  1207.     }
  1208.  
  1209.     /**
  1210.      * Update right level of userRight
  1211.      *
  1212.      * @access public
  1213.      * @param  string  id of user
  1214.      * @param  integer id of right
  1215.      * @param  integer right level
  1216.      * @return mixed   boolean or MDB2 Error object
  1217.      */
  1218.     function updateUserRight($permId$rightId$right_level)
  1219.     {
  1220.         $query 'UPDATE
  1221.                   ' $this->prefix . 'userrights
  1222.                 SET
  1223.                   right_level = ' $this->dbc->quote($right_level'integer''
  1224.                 WHERE
  1225.                   perm_user_id = ' $this->dbc->quote($permId'integer''
  1226.                 AND
  1227.                   right_id = ' $this->dbc->quote($rightId'integer');
  1228.         $result $this->dbc->query($query);
  1229.  
  1230.         if (MDB2::isError($result)) {
  1231.             return $result;
  1232.         }
  1233.  
  1234.         // Job done ...
  1235.         return true;
  1236.     }
  1237.  
  1238.     /**
  1239.      * Revoke right from user
  1240.      *
  1241.      * @access public
  1242.      * @param  string  id of user
  1243.      * @param  integer id of right
  1244.      * @return mixed   boolean or MDB2 Error object
  1245.      */
  1246.     function revokeUserRight($permId$rightId = null)
  1247.     {
  1248.         $query 'DELETE FROM
  1249.                   ' $this->prefix . 'userrights
  1250.                 WHERE
  1251.                   perm_user_id = ' $this->dbc->quote($permId'integer');
  1252.         if (!is_null($rightId)) {
  1253.             $query .= ' AND
  1254.               right_id = ' $this->dbc->quote($rightId'integer');
  1255.         }
  1256.  
  1257.         $result $this->dbc->query($query);
  1258.  
  1259.         if (MDB2::isError($result)) {
  1260.             return $result;
  1261.         }
  1262.  
  1263.         // Job done ...
  1264.         return true;
  1265.     }
  1266.  
  1267.     /**
  1268.      * Get list of all applications
  1269.      *
  1270.      * This method accepts the following options...
  1271.      *  'where_application_id' = [APPLICATION_ID]
  1272.      *
  1273.      * @access public
  1274.      * @param  array an array determining which fields and conditions to use
  1275.      * @return mixed array or MDB2 Error object
  1276.      */
  1277.     function getApplications($options = null)
  1278.     {
  1279.         $query 'SELECT
  1280.                   applications.application_id          AS application_id,
  1281.                   applications.application_define_name AS define_name,
  1282.                   translations.name                    AS name,
  1283.                   translations.description             AS description
  1284.                 FROM
  1285.                   ' $this->prefix . 'applications applications,
  1286.                   ' $this->prefix . 'translations translations
  1287.                 WHERE';
  1288.  
  1289.         if (isset($options['where_application_id'])
  1290.                 && is_numeric($options['where_application_id'])) {
  1291.             $query .= ' applications.application_id = '
  1292.                 . $this->dbc->quote($options['where_application_id']'integer'' AND ';
  1293.         }
  1294.  
  1295.         $query .= ' applications.application_id = translations.section_id AND
  1296.                   translations.section_type = '
  1297.                     . LIVEUSER_SECTION_APPLICATION . ' AND
  1298.                   translations.language_id = '
  1299.                     . $this->dbc->quote($this->_langs[$this->getCurrentLanguage()]'integer''
  1300.                 ORDER BY
  1301.                   applications.application_id ASC';
  1302.  
  1303.         $applications $this->dbc->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  1304.  
  1305.         if (MDB2::isError($applications)) {
  1306.             return $applications;
  1307.         }
  1308.  
  1309.         return $applications;
  1310.     }
  1311.  
  1312.     /**
  1313.      * Get list of all areas within a given application
  1314.      *
  1315.      * This method accepts the following options...
  1316.      *  'where_area_id' = [AREA_ID],
  1317.      *  'where_application_id' = [APPLICATION_ID],
  1318.      *  'with_applications' = [BOOLEAN]
  1319.      *
  1320.      * @access public
  1321.      * @param  array an array determining which fields and conditions to use
  1322.      * @return mixed array or MDB2 Error object
  1323.      */
  1324.     function getAreas($options = null)
  1325.     {
  1326.         $query 'SELECT
  1327.                   areas.area_id            AS area_id,
  1328.                   areas.application_id     AS application_id,
  1329.                   translations.name        AS name,
  1330.                   translations.description AS description,
  1331.                   areas.area_define_name   AS define_name
  1332.                 FROM
  1333.                   ' $this->prefix . 'areas areas,
  1334.                   ' $this->prefix . 'translations translations
  1335.                 WHERE';
  1336.  
  1337.         if (isset($options['where_area_id'])
  1338.                 && is_numeric($options['where_area_id'])) {
  1339.                   $query .= ' areas.area_id=' $this->dbc->quote($options['where_area_id']'integer'' AND';
  1340.         }
  1341.  
  1342.         if (isset($options['where_application_id'])
  1343.                 && is_numeric($options['where_application_id'])) {
  1344.                   $query .= ' areas.application_id=' $this->dbc->quote($options['where_application_id']'integer'' AND';
  1345.         }
  1346.  
  1347.         $query .= ' areas.area_id = translations.section_id AND
  1348.                   translations.section_type = '.LIVEUSER_SECTION_AREA . ' AND
  1349.                   translations.language_id = ' $this->dbc->quote($this->_langs[$this->getCurrentLanguage()]'integer''
  1350.                 ORDER BY
  1351.                   areas.area_id ASC';
  1352.  
  1353.         $areas $this->dbc->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  1354.  
  1355.         if (MDB2::isError($areas)) {
  1356.             return $areas;
  1357.         }
  1358.  
  1359.         $_areas = array();
  1360.         if (is_array($areas)) {
  1361.             foreach($areas as $key => $value{
  1362.                 $id $value['area_id'];
  1363.                 $_areas[$id$value;
  1364.  
  1365.                 if (isset($options['with_applications'])) {
  1366.                     $_areas[$id]['application'$this->getTranslation($value['application_id']LIVEUSER_SECTION_APPLICATION);
  1367.                     if (MDB2::isError($_areas[$id]['application'])) {
  1368.                         return $_areas[$id]['application'];
  1369.                     }
  1370.                 }
  1371.             }
  1372.         }
  1373.  
  1374.         return $_areas;
  1375.     }
  1376.  
  1377.     /**
  1378.      * Get list of all languages
  1379.      *
  1380.      * This method accepts the following options...
  1381.      *  'where_language_id' = [LANGUAGE_ID],
  1382.      *  'with_translations' = [BOOLEAN]
  1383.      *
  1384.      * @access public
  1385.      * @param  array an array determining which fields and conditions to use
  1386.      * @return mixed array or MDB2 Error object
  1387.      */
  1388.     function getLanguages($options = null)
  1389.     {
  1390.         $query 'SELECT
  1391.                   languages.language_id     AS language_id,
  1392.                   languages.two_letter_name AS two_letter_code
  1393.                 FROM
  1394.                   ' $this->prefix . 'languages languages';
  1395.  
  1396.         if (isset($options['where_language_id'])
  1397.                 && is_numeric($options['where_language_id'])) {
  1398.             $query .= ' WHERE languages.language_id = ' $this->dbc->quote($options['where_language_id']'integer');
  1399.         }
  1400.  
  1401.         $langs $this->dbc->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  1402.  
  1403.         if (MDB2::isError($langs)) {
  1404.             return $langs;
  1405.         }
  1406.  
  1407.         if (!is_array($langs)) {
  1408.             return array();
  1409.         }
  1410.  
  1411.         if (isset($options['with_translations'])
  1412.                 && $options['with_translations']
  1413.         {
  1414.             $query '
  1415.                     SELECT
  1416.                         translations.section_id     AS section_id,
  1417.                         translations.language_id    AS language_id,
  1418.                         languages.two_letter_name   AS two_letter_code,
  1419.                         translations.name           AS name
  1420.                     FROM
  1421.                         ' $this->prefix . 'languages languages,
  1422.                         ' $this->prefix . 'translations translations
  1423.                     WHERE
  1424.                         languages.language_id = translations.language_id
  1425.                         AND translations.section_type = ' LIVEUSER_SECTION_LANGUAGE;
  1426.  
  1427.             $trans $this->dbc->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  1428.  
  1429.             if (MDB2::isError($trans)) {
  1430.                 return $trans;
  1431.             }
  1432.         }
  1433.  
  1434.         foreach($langs as $key => $value{
  1435.             unset($langs[$key]);
  1436.             $code $value['two_letter_code'];
  1437.             unset($value['two_letter_code']);
  1438.             $langs[$code$value;
  1439.  
  1440.             if ($options['with_translations'== true && is_array($trans)) {
  1441.                 foreach($trans as $translation{
  1442.                     if ($translation['section_id'== $value['language_id']{
  1443.                         $langs[$code]['name'$translation['name'];
  1444.                     }
  1445.                 }
  1446.             }
  1447.         }
  1448.         return $langs;
  1449.     }
  1450.  
  1451.     /**
  1452.      * Get list of all rights
  1453.      *
  1454.      * This method accepts the following options...
  1455.      *  'where_user_id' = [AUTH_USER_ID],
  1456.      *  'where_group_id' = [GROUP_ID],
  1457.      *  'where_right_id' = [RIGHT_ID],
  1458.      *  'where_area_id' = [AREA_ID],
  1459.      *  'where_application_id' = [APPLICATION_ID],
  1460.      *  'with_areas' = [BOOLEAN],
  1461.      *  'with_applications' = [BOOLEAN]
  1462.      *
  1463.      * @access public
  1464.      * @param  array an array determining which fields and conditions to use
  1465.      * @return mixed array or MDB2 Error object
  1466.      */
  1467.     function getRights($options = null)
  1468.     {
  1469.         $types = array('integer''integer''integer''text''boolean''boolean''boolean''text''text');
  1470.         $query 'SELECT
  1471.                   rights.right_id      AS right_id,
  1472.                   rights.area_id       AS area_id,
  1473.                   areas.application_id AS application_id,
  1474.                   rights.right_define_name AS define_name,
  1475.                   rights.has_implied       AS has_implied,
  1476.                   rights.has_level         AS has_level,
  1477.                   rights.has_scope         AS has_scope,
  1478.                   translations.name        AS name,
  1479.                   translations.description AS description';
  1480.  
  1481.         if (isset($options['where_user_id'])) {
  1482.             $query .= ', userrights.perm_user_id AS user_id';
  1483.             $type['integer';
  1484.         else if (isset($options['where_group_id'])
  1485.                 && is_numeric($options['where_group_id'])) {
  1486.             $query .= ', grouprights.group_id AS group_id';
  1487.             $type['integer';
  1488.         }
  1489.  
  1490.         $query .= '
  1491.                 FROM
  1492.                   ' $this->prefix . 'rights rights,
  1493.                   ' $this->prefix . 'areas areas,
  1494.                   ' $this->prefix . 'applications applications,';
  1495.  
  1496.         if (isset($options['where_user_id'])) {
  1497.             $query .= ' ' $this->prefix . 'userrights userrights,';
  1498.         }
  1499.  
  1500.         if (isset($options['where_group_id'])
  1501.                 && is_numeric($options['where_group_id'])) {
  1502.             $query .= ' ' $this->prefix . 'grouprights grouprights,';
  1503.         }
  1504.  
  1505.         $query .= ' ' $this->prefix . 'translations translations
  1506.                 WHERE';
  1507.  
  1508.         if (isset($options['where_right_id'])
  1509.                 && is_numeric($options['where_right_id'])) {
  1510.             $query .= ' rights.right_id = '
  1511.                 . $this->dbc->quote($options['where_right_id']'integer'' AND';
  1512.         }
  1513.  
  1514.         if (isset($options['where_area_id'])
  1515.                 && is_numeric($options['where_area_id'])) {
  1516.             $query .= ' rights.area_id = '
  1517.                 . $this->dbc->quote($options['where_area_id']'integer'' AND';
  1518.         }
  1519.  
  1520.         if (isset($options['where_application_id'])
  1521.                 && is_numeric($options['where_application_id'])) {
  1522.             $query .= ' areas.application_id = '
  1523.                 . $this->dbc->quote($options['where_application_id']'integer'' AND';
  1524.         }
  1525.  
  1526.         if (isset($options['where_user_id'])) {
  1527.             $query .= ' userrights.perm_user_id = '
  1528.                 . $this->dbc->quote($options['where_user_id']'integer'' AND
  1529.                       userrights.right_id = rights.right_id AND';
  1530.         }
  1531.  
  1532.         if (isset($options['where_group_id'])
  1533.                 && is_numeric($options['where_group_id'])) {
  1534.             $query .= ' grouprights.' $this->groupTableCols['required']['group_id']['name'' = '
  1535.                 . $this->dbc->quote($options['where_group_id']'integer'' AND
  1536.                       grouprights.right_id = rights.right_id AND';
  1537.         }
  1538.  
  1539.         $query .= ' rights.area_id = areas.area_id AND
  1540.                   rights.right_id = translations.section_id AND
  1541.                   translations.section_type = ' LIVEUSER_SECTION_RIGHT . ' AND
  1542.                   translations.language_id = '
  1543.                     . $this->dbc->quote($this->_langs[$this->getCurrentLanguage()]'integer''
  1544.                 GROUP BY
  1545.                   rights.right_id, rights.area_id, areas.application_id';
  1546.  
  1547.         if (isset($options['where_user_id'])) {
  1548.             $query .= ',userrights.perm_user_id';
  1549.         }
  1550.  
  1551.         if (isset($options['where_group_id'])
  1552.                 && is_numeric($options['where_group_id'])) {
  1553.             $query .= ',grouprights.group_id';
  1554.         }
  1555.  
  1556.         $query .= '
  1557.                   ,rights.right_define_name, rights.has_implied,
  1558.                   rights.has_level, rights.has_scope,
  1559.                   translations.name, translations.description
  1560.                 ORDER BY
  1561.                   rights.area_id ASC';
  1562.  
  1563.         $rights $this->dbc->queryAll($query$typesMDB2_FETCHMODE_ASSOC);
  1564.  
  1565.         if (MDB2::isError($rights)) {
  1566.             return $rights;
  1567.         }
  1568.  
  1569.         $_rights = array();
  1570.         if (is_array($rights)) {
  1571.             foreach($rights as $key => $value)
  1572.             {
  1573.                 $id $value['right_id'];
  1574.                 $_rights[$id$value;
  1575.  
  1576.                 if (isset($options['with_areas'])) {
  1577.                     // Add area
  1578.                     $filter = array('where_area_id' => $value['area_id']);
  1579.                     $_rights[$id]['area'=
  1580.                         array_shift($this->getAreas($filter));
  1581.  
  1582.                     if (MDB2::isError($_rights[$id]['area'])) {
  1583.                         return $_rights[$id]['area'];
  1584.                     }
  1585.  
  1586.                     if (isset($options['with_applications'])) {
  1587.                         // Add application
  1588.                         $filter = array('where_application_id' => $value['application_id']);
  1589.                         $_rights[$id]['application'=
  1590.                             array_shift($this->getApplications($filter));
  1591.  
  1592.                         if (MDB2::isError($_rights[$id]['application'])) {
  1593.                             return $_rights[$id]['application'];
  1594.                         }
  1595.                     }
  1596.                 }
  1597.             }
  1598.         }
  1599.  
  1600.         return $_rights;
  1601.     }
  1602.  
  1603.     /**
  1604.      * Make a user an admin of a given area.
  1605.      *
  1606.      * @access public
  1607.      * @param  mixed  user identifier
  1608.      * @param  int    area identifier
  1609.      * @return mixed  true on success, MDB2 Error object or false
  1610.      */
  1611.     function addUserAreaAdmin($permId$areaId)
  1612.     {
  1613.         $query '
  1614.             INSERT INTO
  1615.                 ' $this->prefix . 'area_admin_areas
  1616.                 (perm_user_id, area_id)
  1617.             VALUES (
  1618.                 ' $permId ', ' $this->dbc->quote($areaId'integer''
  1619.             )
  1620.         ';
  1621.  
  1622.         $result $this->dbc->query($query);
  1623.  
  1624.         if (MDB2::isError($result)) {
  1625.             return $result;
  1626.         }
  1627.  
  1628.         return true;
  1629.     }
  1630.  
  1631.     /**
  1632.      * Remove the privilege of being an admin.
  1633.      *
  1634.      * If no area_id is provided the user will be removed asan admin
  1635.      * from all areas he was an admin for.
  1636.      *
  1637.      * @access public
  1638.      * @param  mixed  user identifier
  1639.      * @param  int    area identifier
  1640.      * @return mixed  true on success, MDB2 Error object or false
  1641.      */
  1642.     function removeUserAreaAdmin($permId$areaId = null)
  1643.     {
  1644.         $query '
  1645.             DELETE FROM
  1646.                 ' $this->prefix . 'area_admin_areas
  1647.             WHERE
  1648.                 perm_user_id=' $permId;
  1649.  
  1650.         if (!is_null($areaId&& is_numeric($areaId)) {
  1651.             $query .= '
  1652.             AND
  1653.                 area_id= ' $this->dbc->quote($areaId'integer');
  1654.         }
  1655.  
  1656.         $result $this->dbc->query($query);
  1657.  
  1658.         if (MDB2::isError($result)) {
  1659.             return $result;
  1660.         }
  1661.  
  1662.         return true;
  1663.     }
  1664.  
  1665.     /**
  1666.      * Fetch users from the database.
  1667.      *
  1668.      * The only supported filter is perm_user_id => 'value'
  1669.      *
  1670.      * The array will look like this:
  1671.      * <code>
  1672.      * $userData[0]['perm_user_id'] = 1;
  1673.      *             ['type']         = 1;
  1674.      *             ['container']    = '';
  1675.      *             ['rights']       = array(); // the array returned by getRights()
  1676.      * </code>
  1677.      *
  1678.      * @access  public
  1679.      * @param   array   filters to apply to fetched data
  1680.      * @param   boolean  If true the rights for each user will be retrieved.
  1681.      * @param   boolean will return an associative array with the auth_user_id
  1682.      *                   as the key by using the $rekey param in MDB2::fetchAll()
  1683.      * @return  mixed    Array with user data or error object.
  1684.      * @see     LiveUser_Admin_Perm_DB_Common::getRights()
  1685.      */
  1686.     function getUsers($filters = array()$options = array()$rekey = false)
  1687.     {
  1688.         $query 'SELECT
  1689.                       users.perm_user_id        AS perm_user_id,
  1690.                       users.auth_user_id        AS auth_user_id,
  1691.                       users.perm_type                AS type,
  1692.                       users.auth_container_name AS container
  1693.                   FROM
  1694.                   ' $this->prefix . 'perm_users users';
  1695.  
  1696.         if (isset($filters['group_id'])) {
  1697.             $query .= ', ' $this->prefix . 'groupusers groupusers';
  1698.         }
  1699.  
  1700.         if (isset($filters['group_id'])) {
  1701.             $filter_array['groupusers.perm_user_id=users.perm_user_id';
  1702.             $filter_array['groupusers.group_id IN (' $this->dbc->datatype->implodeArray($filters['group_id']'integer'')';
  1703.         }
  1704.  
  1705.         if (isset($filters['perm_user_id'])) {
  1706.             $filter_array['users.perm_user_id=' $filters['perm_user_id'];
  1707.         }
  1708.  
  1709.         if (isset($filter_array&& count($filter_array)) {
  1710.             $query .= ' WHERE '.implode(' AND '$filter_array);
  1711.         }
  1712.  
  1713.         $types = array('integer''text''integer''text');
  1714.         $res $this->dbc->queryAll($query$typesMDB2_FETCHMODE_ASSOC$rekey);
  1715.  
  1716.         if (is_array($res)) {
  1717.             foreach ($res as $k => $v{
  1718.                 if (isset($options['with_rights'])) {
  1719.                     $res[$k]['rights'$this->getRights(array('where_user_id' => $v['perm_user_id']));
  1720.                 }
  1721.                 if (isset($options['with_groups'])) {
  1722.                     $res[$k]['groups'$this->getGroups(array('where_user_id' => $v['perm_user_id']));
  1723.                 }
  1724.             }
  1725.         else if (!MDB2::isError($res)) {
  1726.             $res = array();
  1727.         }
  1728.         return $res;
  1729.     }
  1730. }
  1731. ?>

Documentation generated on Mon, 11 Mar 2019 13:56:30 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.