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

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