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.20 2004/04/27 00:45:44 mscifo 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.         if (is_array($connectOptions)) {
  47.             $function 'connect';
  48.             if (isset($connectOptions['function'])) {
  49.                 $function $connectOptions['function'];
  50.                 unset($connectOptions['function']);
  51.             }
  52.             foreach ($connectOptions as $key => $value{
  53.                 if (isset($this->$key)) {
  54.                     $this->$key $value;
  55.                 }
  56.             }
  57.             if (isset($connectOptions['connection']&&
  58.                     MDB2::isConnection($connectOptions['connection'])
  59.             {
  60.                 $this->dbc     &$connectOptions['connection'];
  61.                 $this->init_ok = true;
  62.             elseif (isset($connectOptions['dsn'])) {
  63.                 $this->dsn $connectOptions['dsn'];
  64.                 $options = null;
  65.                 if (isset($connectOptions['options'])) {
  66.                     $options $connectOptions['options'];
  67.                 }
  68.                 $options['portability'= MDB2_PORTABILITY_ALL;
  69.                 if ($function == 'singleton'{
  70.                     $this->dbc =MDB2::singleton($connectOptions['dsn']$options);
  71.                 else {
  72.                     $this->dbc =MDB2::connect($connectOptions['dsn']$options);
  73.                 }
  74.                 if (!MDB2::isError($this->dbc)) {
  75.                     $this->init_ok = true;
  76.                 }
  77.             }
  78.         }
  79.     }
  80.  
  81.     /**
  82.      * Gets the perm ID of a user.
  83.      *
  84.      * @access  public
  85.      * @param   string  Auth user ID.
  86.      * @param   string  Auth container name.
  87.      * @return  mixed   Permission ID or MDB2 error.
  88.      */
  89.     function getPermUserId($authId$authName)
  90.     {
  91.         $query '
  92.             SELECT
  93.                 perm_user_id
  94.             FROM
  95.                 ' $this->prefix . 'perm_users
  96.             WHERE
  97.                 auth_user_id = '.$this->dbc->quote($authId'text').'
  98.             AND
  99.                 auth_container_name = '.$this->dbc->quote($authName'text');
  100.         $permId $this->dbc->queryOne($query);
  101.  
  102.         return $permId;
  103.     // end func _getPermUserId
  104.  
  105.     function getAuthUserId($permId)
  106.     {
  107.         $query '
  108.             SELECT
  109.                 auth_user_id, auth_container_name
  110.             FROM
  111.                 ' $this->prefix . 'perm_users
  112.             WHERE
  113.                 perm_user_id = '.$this->dbc->quote($permId'text');
  114.                 $authId $this->dbc->queryRow($queryarray('text''text')MDB2_FETCHMODE_ASSOC);
  115.             return $authId;
  116.     // end func _getAuthUserId
  117.  
  118.     /**
  119.      * Reads all languages from databases and stores them in private variable
  120.      *
  121.      * $this->_langs is filled with this array structure:
  122.      *     two_letter_code => language_id
  123.      *
  124.      * @access private
  125.      * @return mixed boolean or MDB2 Error object
  126.      */
  127.     function _getLanguages()
  128.     {
  129.         if (sizeof($this->_langs< 1{
  130.             $query 'SELECT two_letter_name, language_id FROM ' $this->prefix . 'languages';
  131.             $langs $this->dbc->queryAll($querynullMDB2_FETCHMODE_ASSOCtrue);
  132.             if (MDB2::isError($langs)) {
  133.                 return $langs;
  134.             };
  135.             $this->_langs $langs;
  136.         };
  137.         return true;
  138.     }
  139.  
  140.     /**
  141.      * Set current language
  142.      *
  143.      * Returns false if the language is not known
  144.      *
  145.      * @access public
  146.      * @param  string language short name
  147.      * @return mixed   boolean or MDB2 Error object or false
  148.      */
  149.     function setCurrentLanguage($language)
  150.     {
  151.         // Get all language ids
  152.         if (MDB2::isError($result $this->_getLanguages())) {
  153.             return $result;
  154.         };
  155.  
  156.         // Check if language is a known one
  157.         if (!isset($this->_langs[$language])) {
  158.             return false;
  159.         };
  160.  
  161.         $this->_language $language;
  162.  
  163.         return true;
  164.     }
  165.  
  166.     /**
  167.      * Get current language
  168.      *
  169.      * @access public
  170.      * @return string name of the current language
  171.      */
  172.     function getCurrentLanguage()
  173.     {
  174.         return $this->_language;
  175.     }
  176.  
  177.     /**
  178.      * Set current application
  179.      *
  180.      * @access public
  181.      * @param  integer  id of application
  182.      * @return boolean always true
  183.      */
  184.     function setCurrentApplication($application_id)
  185.     {
  186.         $this->_application $application_id;
  187.  
  188.         return true;
  189.     }
  190.  
  191.     /**
  192.      * Get current application
  193.      *
  194.      * @access public
  195.      * @return string name of the current application
  196.      */
  197.     function getCurrentApplication()
  198.     {
  199.         return $this->_application;
  200.     }
  201.  
  202.     /**
  203.      * Assigns name (and description) in specified language to a section
  204.      *
  205.      * @access public
  206.      * @param integer id of [section]
  207.      * @param integer type of section
  208.      * @param string  language (two letter code) of name/description
  209.      * @param string  name of [section]
  210.      * @param string  description of [section]
  211.      * @return mixed boolean or MDB2 Error object
  212.      */
  213.     function addTranslation($section_id$section_type$language$name$description = null)
  214.     {
  215.         // Get all language ids
  216.         if (MDB2::isError($result $this->_getLanguages())) {
  217.             return $result;
  218.         };
  219.  
  220.         // Check if language is a known one
  221.         if (!isset($this->_langs[$language])) {
  222.             return false;
  223.         };
  224.  
  225.         // Register translation
  226.         $query 'INSERT INTO
  227.                   ' $this->prefix . 'translations
  228.                   (section_id, section_type, language_id, name, description)
  229.                 VALUES
  230.                   (
  231.                     ' $this->dbc->quote($section_id'integer'',
  232.                     ' $this->dbc->quote($section_type'integer'',
  233.                     ' $this->dbc->quote($this->_langs[$language]'integer'',
  234.                     ' $this->dbc->quote($name'text'',
  235.                     ' $this->dbc->quote($description'text''
  236.                   )';
  237.  
  238.         $result $this->dbc->query($query);
  239.  
  240.         if (MDB2::isError($result)) {
  241.             return $result;
  242.         };
  243.  
  244.         // name (and description) added ...
  245.         return true;
  246.     }
  247.  
  248.     /**
  249.      * Updates name (and description) of [section] in specified language
  250.      *
  251.      * @access public
  252.      * @param  integer id of [section]
  253.      * @param  integer type of section
  254.      * @param  string  language (two letter code) of name/description
  255.      * @param  string  name of [section]
  256.      * @param  string  description of [section]
  257.      * @return mixed boolean or MDB2 Error object
  258.      */
  259.     function updateTranslation($section_id$section_type$language$name$description = null)
  260.     {
  261.         // Get all language ids
  262.         if (MDB2::isError($result $this->_getLanguages())) {
  263.             return $result;
  264.         };
  265.  
  266.         // Check if language is a known one
  267.         if (!isset($this->_langs[$language])) {
  268.             return false;
  269.         };
  270.  
  271.         // Update translation
  272.         $query 'UPDATE
  273.                   ' $this->prefix . 'translations
  274.                 SET
  275.                   name        = ' $this->dbc->quote($name'text'',
  276.                   description = ' $this->dbc->quote($description'text''
  277.                 WHERE
  278.                   section_id    = ' $this->dbc->quote($section_id'integer'' AND
  279.                   section_type  = ' $this->dbc->quote($section_type'integer'' AND
  280.                   language_id   = ' $this->dbc->quote($this->_langs[$language]'integer');
  281.  
  282.         $result $this->dbc->query($query);
  283.  
  284.         if (MDB2::isError($result)) {
  285.             return $result;
  286.         };
  287.  
  288.         // Translation name (and description) updated ...
  289.         return true;
  290.     }
  291.  
  292.     /**
  293.      * Remove name (and description) of the [section] in specified language
  294.      *
  295.      * @access public
  296.      * @param  integer id of [section]
  297.      * @param  integer type of section
  298.      * @param  string  language (two letter code) of name/description
  299.      * @param  boolean recursive delete of all translations
  300.      * @return mixed boolean or MDB2 Error object
  301.      */
  302.     function removeTranslation($section_id$section_type$language$recursive = false)
  303.     {
  304.         // Get all language ids
  305.         if (MDB2::isError($result $this->_getLanguages())) {
  306.             return $result;
  307.         };
  308.  
  309.         // Check if language is a known one
  310.         if (!isset($this->_langs[$language])) {
  311.             return false;
  312.         };
  313.  
  314.         // Remove translation
  315.         $query 'DELETE FROM
  316.                   ' $this->prefix . 'translations
  317.                 WHERE
  318.                   section_id    = ' $this->dbc->quote($section_id'integer'' AND
  319.                   section_type  = ' $this->dbc->quote($section_type'integer');
  320.         if (!$recursive{
  321.             $query .= ' AND language_id = ' $this->dbc->quote($this->_langs[$language]'integer');
  322.         };
  323.  
  324.         $result $this->dbc->query($query);
  325.  
  326.         if (MDB2::isError($result)) {
  327.             return $result;
  328.         };
  329.  
  330.         // Translation name (and description) removed ...
  331.         return true;
  332.     }
  333.  
  334.     /**
  335.      * Get name (and description) of the [section] in specified language
  336.      *
  337.      * @access public
  338.      * @param  integer id of [section]
  339.      * @param  integer type of section
  340.      * @return mixed array or MDB2 Error object
  341.      */
  342.     function getTranslation($section_id$section_type)
  343.     {
  344.         // get translation
  345.         $query 'SELECT
  346.                   translations.name        AS name,
  347.                   translations.description AS description
  348.                 FROM
  349.                   ' $this->prefix . 'translations translations
  350.                 WHERE
  351.                   section_id    = ' $this->dbc->quote($section_id'integer'' AND
  352.                   section_type  = ' $this->dbc->quote($section_type'integer');
  353.         $translation $this->dbc->queryRow($querynullMDB2_FETCHMODE_ASSOC);
  354.         if (MDB2::isError($translation)) {
  355.             return $translation;
  356.         };
  357.  
  358.         if (!is_array($translation)) {
  359.             return array();
  360.         }
  361.         // Translation name (and description) removed ...
  362.         return $translation;
  363.     }
  364.  
  365.     /**
  366.      * Add a new language
  367.      *
  368.      * @access public
  369.      * @param  string two letter code of language
  370.      * @param  string native name of language
  371.      * @param  string name of language
  372.      * @param  string description of language
  373.      * @return mixed integer (language_id) or MDB2 Error object
  374.      */
  375.     function addLanguage($two_letter_code$native_name$language_name$language_description = null)
  376.     {
  377.         // Get next language id
  378.         $language_id $this->dbc->nextId($this->prefix . 'languages'true);
  379.  
  380.         if (MDB2::isError($language_id)) {
  381.             return $language_id;
  382.         };
  383.  
  384.         // Add language
  385.         $query 'INSERT INTO
  386.                   ' $this->prefix . 'languages
  387.                   (language_id, two_letter_name, native_name)
  388.                 VALUES
  389.                   (
  390.                     ' $language_id ',
  391.                     ' $this->dbc->quote($two_letter_code'text'',
  392.                     ' $this->dbc->quote($native_name'text''
  393.                   )';
  394.  
  395.         $result $this->dbc->query($query);
  396.  
  397.         if (MDB2::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 (MDB2::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 MDB2 Error object
  436.      */
  437.     function removeLanguage($language)
  438.     {
  439.         // Get all language ids
  440.         if (MDB2::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 = ' $this->dbc->quote($this->_langs[$language]'integer');
  454.  
  455.         $result $this->dbc->query($query);
  456.  
  457.         if (MDB2::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 (MDB2::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  native name of language
  481.      * @param  string  name of language
  482.      * @param  string  description of language
  483.      * @return mixed   boolean or MDB2 Error object
  484.      */
  485.     function updateLanguage($language$native_name$language_name$language_description = null)
  486.     {
  487.         // Get all language ids
  488.         if (MDB2::isError($result $this->_getLanguages())) {
  489.             return $result;
  490.         };
  491.  
  492.         // Check if language is a known one
  493.         if (!isset($this->_langs[$language])) {
  494.             return false;
  495.         };
  496.  
  497.         $query 'UPDATE
  498.                   ' $this->prefix . 'languages
  499.                 SET
  500.                   native_name     = ' $this->dbc->quote($native_name'text''
  501.                 WHERE
  502.                   language_id     = ' $this->dbc->quote($this->_langs[$language]'integer');
  503.  
  504.         $result $this->dbc->query($query);
  505.  
  506.         if (MDB2::isError($result)) {
  507.             return $result;
  508.         };
  509.  
  510.         // Update Language translation into Translations table
  511.         $result $this->updateTranslation(
  512.             $this->_langs[$language],
  513.             LIVEUSER_SECTION_LANGUAGE,
  514.             $this->getCurrentLanguage(),
  515.             $language_name,
  516.             $langauge_description
  517.         );
  518.  
  519.         if (MDB2::isError($result)) {
  520.             return $result;
  521.         };
  522.  
  523.         // Clear language cache
  524.         unset($this->_langs);
  525.  
  526.         // Job done ...
  527.         return true;
  528.     }
  529.  
  530.     /**
  531.      * Add an application
  532.      *
  533.      * @access public
  534.      * @param  string name of application constant
  535.      * @param  string name of application
  536.      * @param  string description of application
  537.      * @return mixed  integer (application_id) or MDB2 Error object
  538.      */
  539.     function addApplication($define_name$application_name$application_description = null)
  540.     {
  541.         // Get next application id
  542.         $application_id $this->dbc->nextId($this->prefix . 'applications'true);
  543.         if (MDB2::isError($application_id)) {
  544.             return $application_id;
  545.         };
  546.  
  547.         // Register new application
  548.         $query 'INSERT INTO
  549.                   ' $this->prefix . 'applications
  550.                   (application_id, application_define_name)
  551.                 VALUES
  552.                   (
  553.                     ' $this->dbc->quote($application_id'integer'',
  554.                     ' $this->dbc->quote($define_name'text''
  555.                   )';
  556.  
  557.         $result $this->dbc->query($query);
  558.  
  559.         if (MDB2::isError($result)) {
  560.             return $result;
  561.         };
  562.  
  563.         // Insert Application translation into Translations table
  564.         $result $this->addTranslation(
  565.             $application_id,
  566.             LIVEUSER_SECTION_APPLICATION,
  567.             $this->getCurrentLanguage(),
  568.             $application_name,
  569.             $application_description
  570.         );
  571.  
  572.         if (MDB2::isError($result)) {
  573.             return $result;
  574.         };
  575.  
  576.         return $application_id;
  577.     }
  578.  
  579.     /**
  580.      * Add an area
  581.      *
  582.      * @access public
  583.      * @param  string id of application
  584.      * @param  string name of area constant
  585.      * @param  string name of area
  586.      * @param  string description of area
  587.      * @return mixed  integer (area_id) or MDB2 Error object
  588.      */
  589.     function addArea($application_id$define_name$area_name$area_description = null)
  590.     {
  591.         // Get next area id
  592.         $area_id $this->dbc->nextId($this->prefix . 'areas'true);
  593.  
  594.         if (MDB2::isError($area_id)) {
  595.             return $area_id;
  596.         };
  597.  
  598.         // Register new area
  599.         $query 'INSERT INTO
  600.                   ' $this->prefix . 'areas
  601.                   (area_id, area_define_name, application_id)
  602.                 VALUES
  603.                   (
  604.                     ' $this->dbc->quote($area_id'integer'',
  605.                     ' $this->dbc->quote($define_name'text'',
  606.                     ' $this->dbc->quote($application_id'integer''
  607.                   )';
  608.  
  609.         $result $this->dbc->query($query);
  610.  
  611.         if (MDB2::isError($result)) {
  612.             return $result;
  613.         };
  614.  
  615.         // Insert Area translation into Translations table
  616.         $result $this->addTranslation(
  617.             $area_id,
  618.             LIVEUSER_SECTION_AREA,
  619.             $this->getCurrentLanguage(),
  620.             $area_name,
  621.             $area_description
  622.         );
  623.  
  624.         if (MDB2::isError($result)) {
  625.             return $result;
  626.         };
  627.  
  628.         return $area_id;
  629.     }
  630.  
  631.     /**
  632.      * Delete an application
  633.      *
  634.      * @access public
  635.      * @param  integer id of application
  636.      * @return mixed   boolean or MDB2 Error object or false
  637.      */
  638.     function removeApplication($application_id)
  639.     {
  640.         if (!is_numeric($application_id)) {
  641.             return false;
  642.         }
  643.  
  644.         // Get all areas within the application, no matter what language
  645.         $query '
  646.             SELECT
  647.                 area_id
  648.             FROM
  649.             ' $this->prefix . 'areas
  650.             WHERE
  651.                 application_id=' $this->dbc->quote($application_id'integer');
  652.  
  653.         $areas $this->dbc->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  654.  
  655.         if (MDB2::isError($areas)) {
  656.             return $areas;
  657.         }
  658.  
  659.         // Delete all areas within the application
  660.         if ($areas{
  661.             foreach($areas as $area{
  662.                 $res $this->removeArea($area['area_id']);
  663.                 if (MDB2::isError($res)) {
  664.                     return $res;
  665.                 }
  666.             }
  667.         }
  668.  
  669.         // Delete application translations
  670.         $result $this->removeTranslation($application_idLIVEUSER_SECTION_APPLICATION$this->getCurrentLanguage()true);
  671.  
  672.         if (MDB2::isError($result)) {
  673.             return $result;
  674.         };
  675.  
  676.         // Delete application itself
  677.         $query 'DELETE FROM
  678.                   ' $this->prefix . 'applications
  679.                 WHERE
  680.                   application_id = ' $application_id;
  681.  
  682.         $result $this->dbc->query($query);
  683.  
  684.         if (MDB2::isError($result)) {
  685.             return $result;
  686.         };
  687.  
  688.         return true;
  689.     }
  690.  
  691.     /**
  692.      * Delete an area
  693.      *
  694.      * @access public
  695.      * @param  integer id of area
  696.      * @return mixed   boolean or MDB2 Error object or false
  697.      */
  698.     function removeArea($area_id)
  699.     {
  700.         if (!is_numeric($area_id)) {
  701.             return false;
  702.         }
  703.  
  704.         // Delete all rights in this area
  705.         $query 'SELECT
  706.                   right_id
  707.                 FROM
  708.                   ' $this->prefix . 'rights
  709.                 WHERE
  710.                   area_id = ' $area_id;
  711.  
  712.         $result $this->dbc->queryCol($query);
  713.  
  714.         if (MDB2::isError($result)) {
  715.             return $result;
  716.         };
  717.  
  718.         if (is_array($result)) {
  719.             foreach($result as $right_id{
  720.                 $this->removeRight($right_id);
  721.             };
  722.         };
  723.  
  724.         // Delete area admins
  725.         $query '
  726.             DELETE FROM
  727.                 ' $this->prefix . 'area_admin_areas
  728.             WHERE
  729.                 area_id=' $area_id '
  730.         ';
  731.  
  732.         $result $this->dbc->query($query);
  733.  
  734.         if (MDB2::isError($result)) {
  735.             return $result;
  736.         };
  737.  
  738.         // Delete area itself
  739.         $query 'DELETE FROM
  740.                   ' $this->prefix . 'areas
  741.                 WHERE
  742.                   area_id = ' $this->dbc->quote($area_id'integer');
  743.  
  744.         $result $this->dbc->query($query);
  745.  
  746.         if (MDB2::isError($result)) {
  747.             return $result;
  748.         };
  749.  
  750.         // Delete area translations
  751.         $result $this->removeTranslation($area_idLIVEUSER_SECTION_AREA$this->getCurrentLanguage()true);
  752.  
  753.         if (MDB2::isError($result)) {
  754.             return $result;
  755.         };
  756.  
  757.         return true;
  758.     }
  759.  
  760.     /**
  761.      * Update an application
  762.      *
  763.      * @access public
  764.      * @param  integer id of application
  765.      * @param  string  name of application constant
  766.      * @param  string  name of application
  767.      * @param  string  description of application
  768.      * @return mixed   boolean or MDB2 Error object
  769.      */
  770.     function updateApplication($application_id$define_name$application_name$application_description = null)
  771.     {
  772.         $query 'UPDATE
  773.                   ' $this->prefix . 'applications
  774.                 SET
  775.                   application_define_name = ' $this->dbc->quote($define_name'text''
  776.                 WHERE
  777.                   application_id = ' $this->dbc->quote($application_id'integer');
  778.  
  779.         $result $this->dbc->query($query);
  780.         if (MDB2::isError($result)) {
  781.             return $result;
  782.         };
  783.  
  784.         // Update Application translation into Translations table
  785.         $result $this->updateTranslation(
  786.             $application_id,
  787.             LIVEUSER_SECTION_APPLICATION,
  788.             $this->getCurrentLanguage(),
  789.             $application_name,
  790.             $application_description
  791.         );
  792.  
  793.         if (MDB2::isError($result)) {
  794.             return $result;
  795.         };
  796.  
  797.         return true;
  798.     }
  799.  
  800.     /**
  801.      * Update an area
  802.      *
  803.      * @access public
  804.      * @param  integer id of area
  805.      * @param  int     id of application
  806.      * @param  string  name of area constant
  807.      * @param  string  name of area
  808.      * @param  string  description of area
  809.      * @return mixed   boolean or MDB2 Error object or false
  810.      */
  811.     function updateArea($area_id$application_id$define_name$area_name$area_description = null)
  812.     {
  813.         if (!is_numeric($area_id)) {
  814.             return false;
  815.         }
  816.  
  817.         $query 'UPDATE
  818.                   ' $this->prefix . 'areas
  819.                 SET
  820.                   application_id   = ' $this->dbc->quote($application_id'integer'',
  821.                   area_define_name = ' $this->dbc->quote($define_name'text''
  822.                 WHERE
  823.                   area_id = ' $this->dbc->quote($area_id'integer');
  824.  
  825.         $result $this->dbc->query($query);
  826.  
  827.         if (MDB2::isError($result)) {
  828.             return $result;
  829.         };
  830.  
  831.         // Update Area translation into Translations table
  832.         $result $this->updateTranslation(
  833.             $area_id,
  834.             LIVEUSER_SECTION_AREA,
  835.             $this->getCurrentLanguage(),
  836.             $area_name,
  837.             $area_description
  838.         );
  839.         if (MDB2::isError($result)) {
  840.             return $result;
  841.         };
  842.  
  843.         return true;
  844.     }
  845.  
  846.     /**
  847.      * Add a right in special area
  848.      *
  849.      * @access public
  850.      * @param  integer id of area
  851.      * @param  string  name of right constant
  852.      * @param  string  name of right
  853.      * @param  string  description of right
  854.      * @return mixed   integer (right_id) or MDB2 Error object
  855.      */
  856.     function addRight($area_id$define_name$right_name$right_description = null)
  857.     {
  858.         // Get next right id
  859.         $right_id $this->dbc->nextId($this->prefix . 'rights'true);
  860.  
  861.         if (MDB2::isError($right_id)) {
  862.             return $right_id;
  863.         };
  864.  
  865.         // Register right
  866.         $query 'INSERT INTO
  867.                   ' $this->prefix . 'rights
  868.                   (right_id, area_id, right_define_name)
  869.                 VALUES
  870.                   (
  871.                     ' $this->dbc->quote($right_id'integer'',
  872.                     ' $this->dbc->quote($area_id'integer'',
  873.                     ' $this->dbc->quote($define_name'text''
  874.                   )';
  875.  
  876.         $result $this->dbc->query($query);
  877.  
  878.         if (MDB2::isError($result)) {
  879.             return $result;
  880.         };
  881.  
  882.         // Insert Right translation into Translations table
  883.         $result $this->addTranslation(
  884.             $right_id,
  885.             LIVEUSER_SECTION_RIGHT,
  886.             $this->getCurrentLanguage(),
  887.             $right_name,
  888.             $right_description
  889.         );
  890.  
  891.         if (MDB2::isError($result)) {
  892.             return $result;
  893.         };
  894.  
  895.         // Job done ...
  896.         return $right_id;
  897.     }
  898.  
  899.     /**
  900.      * Delete a right
  901.      *
  902.      * @access public
  903.      * @param  integer id of right
  904.      * @return mixed   boolean or MDB2 Error object
  905.      */
  906.     function removeRight($right_id)
  907.     {
  908.         // Delete userright
  909.         $query 'DELETE FROM
  910.                   ' $this->prefix . 'userrights
  911.                 WHERE
  912.                   right_id = ' $this->dbc->quote($right_id'integer');
  913.  
  914.         $result $this->dbc->query($query);
  915.  
  916.         if (MDB2::isError($result)) {
  917.             return $result;
  918.         };
  919.  
  920.         // Delete group right
  921.         $query 'DELETE FROM
  922.                   ' $this->prefix . 'grouprights
  923.                 WHERE
  924.                   right_id = ' $this->dbc->quote($right_id'integer');
  925.         $result $this->dbc->query($query);
  926.  
  927.         if (MDB2::isError($result)) {
  928.             return $result;
  929.         };
  930.  
  931.         // Delete right translations
  932.         $result $this->removeTranslation($right_idLIVEUSER_SECTION_RIGHT$this->getCurrentLanguage()true);
  933.  
  934.         if (MDB2::isError($result)) {
  935.             return $result;
  936.         };
  937.  
  938.         // Delete right itself
  939.         $query 'DELETE FROM
  940.                   ' $this->prefix . 'rights
  941.                 WHERE
  942.                   right_id = ' $this->dbc->quote($right_id'integer');
  943.         $result $this->dbc->query($query);
  944.         if (MDB2::isError($result)) {
  945.             return $result;
  946.         };
  947.  
  948.         // Job done ...
  949.         return true;
  950.  
  951.     }
  952.  
  953.     /**
  954.      * Update a right
  955.      *
  956.      * @access public
  957.      * @param  integer id of right
  958.      * @param  integer id of area
  959.      * @param  string  name of right constant
  960.      * @param  string  name of right
  961.      * @param  string  description of right
  962.      * @return mixed   boolean or MDB2 Error object
  963.      */
  964.     function updateRight($right_id$area_id$define_name$right_name$right_description = null)
  965.     {
  966.         $query 'UPDATE
  967.                   ' $this->prefix . 'rights
  968.                 SET
  969.                   area_id           = ' $this->dbc->quote($area_id'integer'',
  970.                   right_define_name = ' $this->dbc->quote($define_name'text''
  971.                 WHERE
  972.                   right_id = ' $this->dbc->quote($right_id'integer');
  973.  
  974.         $result $this->dbc->query($query);
  975.  
  976.         if (MDB2::isError($result)) {
  977.             return $result;
  978.         };
  979.  
  980.         // Update Right translation into Translations table
  981.         $result $this->updateTranslation(
  982.             $right_id,
  983.             LIVEUSER_SECTION_RIGHT,
  984.             $this->getCurrentLanguage(),
  985.             $right_name,
  986.             $right_description
  987.         );
  988.  
  989.         if (MDB2::isError($result)) {
  990.             return $result;
  991.         };
  992.  
  993.         // Job done ...
  994.         return true;
  995.     }
  996.  
  997.     /**
  998.      * Add a user
  999.      *
  1000.      * @access  public
  1001.      * @param   string   $authId    Auth user ID of the user that should be added.
  1002.      * @param   string   $authname  Auth container name.
  1003.      * @param   int      $type      User type (constants defined in Perm/Common.php) (optional).
  1004.      * @param   mixed    $permId    If specificed no new ID will be automatically generated instead
  1005.      * @return mixed   string (perm_user_id) or MDB2 Error object
  1006.      */
  1007.     function addUser($authId$authName = null$type = LIVEUSER_USER_TYPE_ID$permId = null)
  1008.     {
  1009.         if (!$this->init_ok{
  1010.             return false;
  1011.         }
  1012.  
  1013.         if (is_null($authName)) {
  1014.             return LiveUser::raiseError(LIVEUSER_ERRORnullnull,
  1015.                     'Auth name has to be passed with the function');
  1016.         }
  1017.  
  1018.         if (is_null($permId)) {
  1019.             $permId $this->dbc->nextId($this->prefix . 'perm_users'true);
  1020.         }
  1021.  
  1022.         $query '
  1023.             INSERT INTO
  1024.                 ' $this->prefix . 'perm_users
  1025.                 (perm_user_id, auth_user_id, perm_type, auth_container_name)
  1026.             VALUES
  1027.                 (
  1028.                 ' $this->dbc->quote($permId'integer'',
  1029.                 ' $this->dbc->quote($authId'text'',
  1030.                 ' $this->dbc->quote($type'integer'',
  1031.                 ' $this->dbc->quote($authName'text''
  1032.                 )';
  1033.  
  1034.         $result $this->dbc->query($query);
  1035.  
  1036.         if (MDB2::isError($result)) {
  1037.             return $result;
  1038.         }
  1039.  
  1040.         return $permId;
  1041.     }
  1042.  
  1043.     /**
  1044.      * Updates auth_user_id in the mapping table.
  1045.      *
  1046.      * @access  public
  1047.      * @param   int     perm_user_id of the user
  1048.      * @param   mixed   new Auth user ID
  1049.      * @param   mixed   new Auth Container name
  1050.      * @return  mixed   true or DB Error object or false if there was an error
  1051.      *                   to begin with
  1052.      */
  1053.     function updateAuthUserId($permId$authId$authName = false)
  1054.     {
  1055.         if (!$this->init_ok{
  1056.             return false;
  1057.         }
  1058.  
  1059.         $query '
  1060.             UPDATE
  1061.                 ' $this->prefix . 'perm_users
  1062.             SET';
  1063.         if ($authName !== false{
  1064.             $query .= ' auth_container_name=' $this->dbc->quote($authName'text'',';
  1065.         }
  1066.         $query .= '
  1067.                 auth_user_id=' $this->dbc->quote($authId'text''
  1068.             WHERE
  1069.                 perm_user_id=' $this->dbc->quote($permId'integer');
  1070.  
  1071.         $result $this->dbc->query($query);
  1072.  
  1073.         if (MDB2::isError($result)) {
  1074.             return $result;
  1075.         }
  1076.  
  1077.         return true;
  1078.     }
  1079.  
  1080.     /**
  1081.      * Delete user
  1082.      *
  1083.      * @access public
  1084.      * @param  string  id of user
  1085.      * @return mixed   boolean or MDB2 Error object
  1086.      */
  1087.     function removeUser($permId)
  1088.     {
  1089.         if (!$this->init_ok{
  1090.             return false;
  1091.         }
  1092.  
  1093.         // Delete user from perm table (Perm/MDB2)
  1094.         $query '
  1095.             DELETE FROM
  1096.                 ' $this->prefix . 'perm_users
  1097.             WHERE
  1098.                 perm_user_id = ' $permId;
  1099.  
  1100.         $result $this->dbc->query($query);
  1101.  
  1102.         if (MDB2::isError($result)) {
  1103.             return $result;
  1104.         }
  1105.  
  1106.         // Delete group assignments
  1107.         $query 'DELETE FROM
  1108.                   ' $this->prefix . 'groupusers
  1109.                 WHERE
  1110.                   perm_user_id = ' $permId;
  1111.  
  1112.         $result $this->dbc->query($query);
  1113.  
  1114.         if (MDB2::isError($result)) {
  1115.             return $result;
  1116.         };
  1117.  
  1118.         // Delete right assignments
  1119.         $query 'DELETE FROM
  1120.                   ' $this->prefix . 'userrights
  1121.                 WHERE
  1122.                   perm_user_id = ' $permId;
  1123.  
  1124.         $result $this->dbc->query($query);
  1125.  
  1126.         if (MDB2::isError($result)) {
  1127.             return $result;
  1128.         };
  1129.  
  1130.         // remove user area admin relation
  1131.         $result $this->removeUserAreaAdmin($permId);
  1132.  
  1133.         if (MDB2::isError($result)) {
  1134.             return $result;
  1135.         };
  1136.  
  1137.         return true;
  1138.     }
  1139.  
  1140.     /**
  1141.      * Grant right to user
  1142.      *
  1143.      * @access public
  1144.      * @param  string  id of user
  1145.      * @param  integer id of right
  1146.      * @return mixed   boolean or MDB2 Error object
  1147.      */
  1148.     function grantUserRight($permId$right_id)
  1149.     {
  1150.         //return if this user already has right
  1151.         $query 'SELECT
  1152.                   count(*)
  1153.                 FROM
  1154.                   ' $this->prefix . 'userrights
  1155.                 WHERE
  1156.                   perm_user_id = ' $this->dbc->quote($right_id'integer''
  1157.                 AND
  1158.                   right_id     = ' $this->dbc->quote($right_id'integer');
  1159.  
  1160.         $count $this->dbc->queryOne($query);
  1161.  
  1162.         if (MDB2::isError($count|| $count != 0{
  1163.             return false;
  1164.         };
  1165.  
  1166.         $query 'INSERT INTO
  1167.                   ' $this->prefix . 'userrights
  1168.                   (perm_user_id, right_id, right_level)
  1169.                 VALUES
  1170.                   (
  1171.                     ' $this->dbc->quote($permId'integer'',
  1172.                     ' $this->dbc->quote($right_id'integer'', '.LIVEUSER_MAX_LEVEL.'
  1173.                   )';
  1174.  
  1175.         $result $this->dbc->query($query);
  1176.  
  1177.         if (MDB2::isError($result)) {
  1178.             return $result;
  1179.         };
  1180.  
  1181.         // Job done ...
  1182.         return true;
  1183.     }
  1184.  
  1185.     /**
  1186.      * Update right level of userRight
  1187.      *
  1188.      * @access public
  1189.      * @param  string  id of user
  1190.      * @param  integer id of right
  1191.      * @param  integer right level
  1192.      * @return mixed   boolean or DB Error object
  1193.      */
  1194.     function updateUserRight($permId$right_id$right_level)
  1195.     {
  1196.         $query 'UPDATE
  1197.                   ' $this->prefix . 'userrights
  1198.                 SET
  1199.                   right_level = ' $this->dbc->quote($right_level'integer''
  1200.                 WHERE
  1201.                   perm_user_id = ' $this->dbc->quote($permId'integer''
  1202.                 AND
  1203.                   right_id = ' $this->dbc->quote($right_id'integer');
  1204.         $result $this->dbc->query($query);
  1205.  
  1206.         if (MDB2::isError($result)) {
  1207.             return $result;
  1208.         };
  1209.  
  1210.         // Job done ...
  1211.         return true;
  1212.     }
  1213.  
  1214.     /**
  1215.      * Revoke right from user
  1216.      *
  1217.      * @access public
  1218.      * @param  string  id of user
  1219.      * @param  integer id of right
  1220.      * @return mixed   boolean or MDB2 Error object
  1221.      */
  1222.     function revokeUserRight($permId$right_id = null)
  1223.     {
  1224.         $query 'DELETE FROM
  1225.                   ' $this->prefix . 'userrights
  1226.                 WHERE
  1227.                   perm_user_id = ' $this->dbc->quote($permId'integer');
  1228.         if (!is_null($right_id)) {
  1229.             $query .= ' AND
  1230.               right_id = ' $this->dbc->quote($right_id'integer');
  1231.         }
  1232.  
  1233.         $result $this->dbc->query($query);
  1234.  
  1235.         if (MDB2::isError($result)) {
  1236.             return $result;
  1237.         };
  1238.  
  1239.         // Job done ...
  1240.         return true;
  1241.     }
  1242.  
  1243.     /**
  1244.      * Get list of all applications
  1245.      *
  1246.      * This method accepts the following options...
  1247.      *  'where_application_id' = [APPLICATION_ID]
  1248.      *
  1249.      * @access public
  1250.      * @param  array an array determining which fields and conditions to use
  1251.      * @return mixed array or MDB2 Error object
  1252.      */
  1253.     function getApplications($options = null)
  1254.     {
  1255.         $query 'SELECT
  1256.                   applications.application_id          AS application_id,
  1257.                   applications.application_define_name AS define_name,
  1258.                   translations.name                    AS name,
  1259.                   translations.description             AS description
  1260.                 FROM
  1261.                   ' $this->prefix . 'applications applications,
  1262.                   ' $this->prefix . 'translations translations
  1263.                 WHERE';
  1264.  
  1265.         if (isset($options['where_application_id'])
  1266.                 && is_numeric($options['where_application_id'])) {
  1267.             $query .= ' applications.application_id = '
  1268.                 . $this->dbc->quote($options['where_application_id']'integer'' AND ';
  1269.         }
  1270.  
  1271.         $query .= ' applications.application_id = translations.section_id AND
  1272.                   translations.section_type = '
  1273.                     . LIVEUSER_SECTION_APPLICATION . ' AND
  1274.                   translations.language_id = '
  1275.                     . $this->dbc->quote($this->_langs[$this->getCurrentLanguage()]'integer''
  1276.                 ORDER BY
  1277.                   applications.application_id ASC';
  1278.  
  1279.         $applications $this->dbc->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  1280.  
  1281.         if (MDB2::isError($applications)) {
  1282.             return $applications;
  1283.         }
  1284.  
  1285.         return $applications;
  1286.     }
  1287.  
  1288.     /**
  1289.      * Get list of all areas within a given application
  1290.      *
  1291.      * This method accepts the following options...
  1292.      *  'where_area_id' = [AREA_ID],
  1293.      *  'where_application_id' = [APPLICATION_ID],
  1294.      *  'with_applications' = [BOOLEAN]
  1295.      *
  1296.      * @access public
  1297.      * @param  array an array determining which fields and conditions to use
  1298.      * @return mixed array or MDB2 Error object
  1299.      */
  1300.     function getAreas($options = null)
  1301.     {
  1302.         $query 'SELECT
  1303.                   areas.area_id            AS area_id,
  1304.                   areas.application_id     AS application_id,
  1305.                   translations.name        AS name,
  1306.                   translations.description AS description,
  1307.                   areas.area_define_name   AS define_name
  1308.                 FROM
  1309.                   ' $this->prefix . 'areas areas,
  1310.                   ' $this->prefix . 'translations translations
  1311.                 WHERE';
  1312.  
  1313.         if (isset($options['where_area_id'])
  1314.                 && is_numeric($options['where_area_id'])) {
  1315.                   $query .= ' areas.area_id=' $this->dbc->quote($options['where_area_id']'integer'' AND';
  1316.         };
  1317.  
  1318.         if (isset($options['where_application_id'])
  1319.                 && is_numeric($options['where_application_id'])) {
  1320.                   $query .= ' areas.application_id=' $this->dbc->quote($options['where_application_id']'integer'' AND';
  1321.         };
  1322.  
  1323.         $query .= ' areas.area_id = translations.section_id AND
  1324.                   translations.section_type = '.LIVEUSER_SECTION_AREA . ' AND
  1325.                   translations.language_id = ' $this->dbc->quote($this->_langs[$this->getCurrentLanguage()]'integer''
  1326.                 ORDER BY
  1327.                   areas.area_id ASC';
  1328.  
  1329.         $areas $this->dbc->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  1330.  
  1331.         if (MDB2::isError($areas)) {
  1332.             return $areas;
  1333.         }
  1334.  
  1335.         $_areas = array();
  1336.         if (is_array($areas)) {
  1337.             foreach($areas as $key => $value{
  1338.                 $id $value['area_id'];
  1339.                 $_areas[$id$value;
  1340.  
  1341.                 if (isset($options['with_applications'])) {
  1342.                     $_areas[$id]['application'$this->getTranslation($value['application_id']LIVEUSER_SECTION_APPLICATION);
  1343.                     if (MDB2::isError($_areas[$id]['application'])) {
  1344.                         return $_areas[$id]['application'];
  1345.                     }
  1346.                 };
  1347.             };
  1348.         };
  1349.  
  1350.         return $_areas;
  1351.     }
  1352.  
  1353.     /**
  1354.      * Get list of all languages
  1355.      *
  1356.      * This method accepts the following options...
  1357.      *  'where_language_id' = [LANGUAGE_ID],
  1358.      *  'with_translations' = [BOOLEAN]
  1359.      *
  1360.      * @access public
  1361.      * @param  array an array determining which fields and conditions to use
  1362.      * @return mixed array or MDB2 Error object
  1363.      */
  1364.     function getLanguages($options = null)
  1365.     {
  1366.         $query 'SELECT
  1367.                   languages.language_id     AS language_id,
  1368.                   languages.two_letter_name AS two_letter_code,
  1369.                   languages.native_name     AS native_name
  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 = ' $this->dbc->quote($options['where_language_id']'integer');
  1376.         };
  1377.  
  1378.         $langs $this->dbc->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  1379.  
  1380.         if (MDB2::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.             $query '
  1391.                     SELECT
  1392.                         translations.section_id     AS section_id,
  1393.                         translations.language_id    AS language_id,
  1394.                         languages.two_letter_name   AS two_letter_code,
  1395.                         translations.name           AS name
  1396.                     FROM
  1397.                         ' $this->prefix . 'languages languages,
  1398.                         ' $this->prefix . 'translations translations
  1399.                     WHERE
  1400.                         languages.language_id = translations.language_id
  1401.                         AND translations.section_type = ' LIVEUSER_SECTION_LANGUAGE . '
  1402.                         AND translations.language_id = ' . (int)$this->_langs[$this->getCurrentLanguage()];
  1403.  
  1404.             $trans $this->dbc->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  1405.  
  1406.             if (MDB2::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.             $value['name'$value['native_name'];
  1416.             $langs[$code$value;
  1417.  
  1418.             if ($options['with_translations'== true && is_array($trans)) {
  1419.                 foreach($trans as $translation{
  1420.                     if ($translation['section_id'== $value['language_id']{
  1421.                         $langs[$code]['name'$translation['name'];
  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 MDB2 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.                 . $this->dbc->quote($options['where_right_id']'integer'' AND';
  1489.         };
  1490.  
  1491.         if (isset($options['where_area_id'])
  1492.                 && is_numeric($options['where_area_id'])) {
  1493.             $query .= ' rights.area_id = '
  1494.                 . $this->dbc->quote($options['where_area_id']'integer'' AND';
  1495.         };
  1496.  
  1497.         if (isset($options['where_application_id'])
  1498.                 && is_numeric($options['where_application_id'])) {
  1499.             $query .= ' areas.application_id = '
  1500.                 . $this->dbc->quote($options['where_application_id']'integer'' AND';
  1501.         };
  1502.  
  1503.         if (isset($options['where_user_id'])) {
  1504.             $query .= ' userrights.perm_user_id = '
  1505.                 . $this->dbc->quote($options['where_user_id']'integer'' 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.                 . $this->dbc->quote($options['where_group_id']'integer'' 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.                     . $this->dbc->quote($this->_langs[$this->getCurrentLanguage()]'integer''
  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->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  1541.  
  1542.         if (MDB2::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 (MDB2::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 (MDB2::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, MDB2 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 ', ' $this->dbc->quote($area_id'integer''
  1596.             )
  1597.         ';
  1598.  
  1599.         $result $this->dbc->query($query);
  1600.  
  1601.         if (MDB2::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, MDB2 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= ' $this->dbc->quote($area_id'integer');
  1631.         }
  1632.  
  1633.         $result $this->dbc->query($query);
  1634.  
  1635.         if (MDB2::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 (' $this->dbc->datatype->implodeArray($filters['group_id']'integer'')';
  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.         $types = array('integer''text''integer''text');
  1691.         $res $this->dbc->queryAll($query$typesMDB2_FETCHMODE_ASSOC$rekey);
  1692.  
  1693.         if (is_array($res)) {
  1694.             foreach ($res as $k => $v{
  1695.                 if (isset($options['with_rights'])) {
  1696.                     $res[$k]['rights'$this->getRights(array('where_user_id' => $v['perm_user_id']));
  1697.                 }
  1698.                 if (isset($options['with_groups'])) {
  1699.                     $res[$k]['groups'$this->getGroups(array('where_user_id' => $v['perm_user_id']));
  1700.                 }
  1701.             }
  1702.         else if (!MDB2::isError($res)) {
  1703.             $res = array();
  1704.         }
  1705.         return $res;
  1706.     }
  1707. }
  1708. ?>

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