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

Source for file MDB_Simple.php

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

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