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

Source for file DB_Simple.php

Documentation is available at DB_Simple.php

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

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