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

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