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

Source for file DB_Simple.php

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

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