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

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