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

Source for file MDB2_Simple.php

Documentation is available at MDB2_Simple.php

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

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